2 * Copyright (c) 2023 Thomas Adam <thomas@xteddy.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #ifndef _GOT_COMPAT_H_2
18 #define _GOT_COMPAT_H_2
29 #if defined(__FreeBSD__)
30 #include <sys/endian.h>
31 #include <sys/capsicum.h>
32 #elif defined(__APPLE__)
33 #include <machine/endian.h>
34 #include <libkern/OSByteOrder.h>
35 #include "compat/bsd-poll.h"
37 #define FMT_SCALED_STRSIZE 7 /* minus sign, 4 digits, suffix, null byte */
39 #define htobe16(x) OSSwapHostToBigInt16(x)
40 #define htole16(x) OSSwapHostToLittleInt16(x)
41 #define be16toh(x) OSSwapBigToHostInt16(x)
42 #define le16toh(x) OSSwapLittleToHostInt16(x)
44 #define htobe32(x) OSSwapHostToBigInt32(x)
45 #define htole32(x) OSSwapHostToLittleInt32(x)
46 #define be32toh(x) OSSwapBigToHostInt32(x)
47 #define le32toh(x) OSSwapLittleToHostInt32(x)
49 #define htobe64(x) OSSwapHostToBigInt64(x)
50 #define htole64(x) OSSwapHostToLittleInt64(x)
51 #define be64toh(x) OSSwapBigToHostInt64(x)
52 #define le64toh(x) OSSwapLittleToHostInt64(x)
54 #define st_atim st_atimespec
55 #define st_ctim st_ctimespec
56 #define st_mtim st_mtimespec
58 #else /* Linux, etc... */
65 #define __attribute__(a)
67 #define __bounded__(a, b, c)
73 # define UID_MAX UINT_MAX /* max value for a uid_t */
76 # define GID_MAX UINT_MAX /* max value for a gid_t */
88 #define flock(fd, op) (0)
93 /* POSIX doesn't define WAIT_ANY, so provide it if it's not found. */
98 /* On FreeBSD (and possibly others), EAI_NODATA was removed, in favour of
102 #define EAI_NODATA EAI_NONAME
106 #define __dead __attribute__ ((__noreturn__))
110 #define __unused __attribute__ ((__unused__))
114 #define pledge(s, p) (0)
115 #define unveil(s, p) (0)
119 #define cap_enter() (0)
122 #ifndef HAVE_B64_NTOP
125 int b64_ntop(u_char const *, size_t, char *, size_t);
126 int b64_pton(char const *, u_char *, size_t);
129 #ifndef HAVE_SETRESGID
130 #define setresgid(a, b, c) (0)
133 #ifndef HAVE_SETRESUID
134 #define setresuid(a, b, c) (0)
137 #ifndef HAVE_LINUX_LANDLOCK_H
138 #define landlock_no_fs() (0)
140 int landlock_no_fs(void);
147 #ifndef HAVE_BSD_UUID
148 #include <uuid/uuid.h>
150 #define uuid_s_bad_version 1
151 #define uuid_s_invalid_string_uuid 2
152 #define uuid_s_no_memory 3
154 /* Length of a node address (an IEEE 802 address). */
155 #define _UUID_NODE_LEN 6
160 uint16_t time_hi_and_version;
161 uint8_t clock_seq_hi_and_reserved;
162 uint8_t clock_seq_low;
163 uint8_t node[_UUID_NODE_LEN];
166 int32_t uuid_equal(struct uuid *, struct uuid *, uint32_t *);
167 int32_t uuid_is_nil(struct uuid *, uint32_t *);
168 void uuid_create(uuid_t *, uint32_t *);
169 void uuid_create_nil(struct uuid *, uint32_t *);
170 void uuid_from_string(const char *, uuid_t *, uint32_t *);
171 void uuid_to_string(uuid_t *, char **, uint32_t *);
177 #include <sys/queue.h>
181 #include "compat/tree.h"
183 #include <sys/tree.h>
190 #ifdef HAVE_LIBUTIL_H
195 # define IOV_MAX 1024
199 #include "compat/imsg.h"
205 #include "compat/siphash.h"
210 /* Include Apple-specific headers. Mostly for crypto.*/
211 #if defined(__APPLE__)
212 #define COMMON_DIGEST_FOR_OPENSSL
213 #include <CommonCrypto/CommonDigest.h>
216 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
217 #define SHA256Init SHA256_Init
218 #define SHA256Update SHA256_Update
219 #define SHA256Final SHA256_Final
220 #define SHA2_CTX SHA256_CTX
238 /* Catch-all for systems where the header files don't exist and/or the below
239 * still are not defined.
241 #ifndef SHA256_DIGEST_LENGTH
242 #define SHA256_DIGEST_LENGTH 32
245 #ifndef SHA256_DIGEST_STRING_LENGTH
246 #define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1)
249 #if defined(__DragonFly__)
250 #include <openssl/sha.h>
253 #ifndef SHA1_DIGEST_LENGTH
254 #define SHA1_DIGEST_LENGTH SHA_DIGEST_LENGTH
255 #define SHA1_DIGEST_STRING_LENGTH (SHA1_DIGEST_LENGTH * 2 + 1)
257 #define SHA1_CTX SHA_CTX
258 #define SHA1Init SHA1_Init
259 #define SHA1Update SHA1_Update
260 #define SHA1Final SHA1_Final
264 * The following SA_LEN/SS_LEN dance comes from various source, notably
265 * OpenSMTP by way of OpenNTPD and OpenBGPD (thanks everyone!). got-portable
266 * has tweaked a lot of the following macros to suit the needs of
270 /* From OpenNTPD portable */
272 # if defined(HAVE_STRUCT_SOCKADDR_SA_LEN)
273 # define SA_LEN(x) ((x)->sa_len)
275 # define SA_LEN(x) ((x)->sa_family == AF_INET6 ? \
276 sizeof(struct sockaddr_in6) : \
277 sizeof(struct sockaddr_in))
282 /* From OpenBGPD portable */
284 # if defined(HAVE_STRUCT_SOCKADDR_STORAGE_SS_LEN)
285 # define SS_LEN(x) ((x)->ss_len)
287 # define SS_LEN(x) SA_LEN((struct sockaddr *)(x))
291 /* SOCK_NONBLOCK isn't available across BSDs... */
292 #if !defined(SOCK_NONBLOCK) && !defined(__linux__)
293 #define SOCK_NONBLOCK 00004000
296 #ifndef HAVE_ASPRINTF
298 int asprintf(char **, const char *, ...);
299 int vasprintf(char **, const char *, va_list);
302 #ifndef HAVE_EXPLICIT_BZERO
303 /* explicit_bzero.c */
304 void explicit_bzero(void *, size_t);
307 #ifndef HAVE_GETDTABLECOUNT
308 /* getdtablecount.c */
309 int getdtablecount(void);
312 #ifndef HAVE_CLOSEFROM
319 char *strsep(char **, const char *);
322 #ifndef HAVE_STRTONUM
324 long long strtonum(const char *, long long, long long, const char **);
329 size_t strlcpy(char *, const char *, size_t);
334 size_t strlcat(char *, const char *, size_t);
339 size_t strnlen(const char *, size_t);
344 char *strndup(const char *, size_t);
347 #ifndef HAVE_GETPROGNAME
349 const char *getprogname(void);
354 ssize_t getline(char **, size_t *, FILE *);
357 #ifndef HAVE_FREEZERO
359 void freezero(void *, size_t);
362 #ifndef HAVE_GETDTABLECOUNT
363 /* getdtablecount.c */
364 int getdtablecount(void);
367 #ifndef HAVE_REALLOCARRAY
369 void *reallocarray(void *, size_t, size_t);
372 #ifndef HAVE_RECALLOCARRAY
373 /* recallocarray.c */
374 void *recallocarray(void *, size_t, size_t, size_t);
377 #ifndef HAVE_SETPROCTITLE
379 void setproctitle(const char *, ...);
382 #ifndef HAVE_FMT_SCALED
384 int fmt_scaled(long long, char *);
385 int scan_scaled(char *, long long *);
386 #define FMT_SCALED_STRSIZE 7 /* minus sign, 4 digits, suffix, null byte */
389 #if !defined(HAVE_LIBBSD) && !defined(HAVE_GETOPT_OPTRESET)
391 extern int BSDopterr;
392 extern int BSDoptind;
393 extern int BSDoptopt;
394 extern int BSDoptreset;
395 extern char *BSDoptarg;
396 int BSDgetopt(int, char *const *, const char *);
397 #define getopt(ac, av, o) BSDgetopt(ac, av, o)
398 #define opterr BSDopterr
399 #define optind BSDoptind
400 #define optopt BSDoptopt
401 #define optreset BSDoptreset
402 #define optarg BSDoptarg
405 /* Check for some of the non-portable timespec*() functions.
406 * This should largely come from libbsd for systems which
407 * aren't BSD, but this will depend on how old the library
410 #ifndef timespecisset
411 #define timespecisset(tsp) \
412 ((tsp)->tv_sec || (tsp)->tv_nsec)
416 #define timespecsub(tsp, usp, vsp) \
418 (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
419 (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
420 if ((vsp)->tv_nsec < 0) { \
422 (vsp)->tv_nsec += 1000000000L; \
428 #define timespeccmp(tvp, uvp, cmp) \
429 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
430 ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
431 ((tvp)->tv_sec cmp (uvp)->tv_sec))
434 #ifndef HAVE_MERGESORT
436 int mergesort(void *, size_t, size_t, int (*)(const void *, const void *));