7 #if defined(__FreeBSD__)
8 #include <sys/endian.h>
9 #include <sys/capsicum.h>
10 #elif defined(__APPLE__)
11 #include <machine/endian.h>
12 #include <libkern/OSByteOrder.h>
13 #include "compat/bsd-poll.h"
15 #define FMT_SCALED_STRSIZE 7 /* minus sign, 4 digits, suffix, null byte */
17 #define htobe16(x) OSSwapHostToBigInt16(x)
18 #define htole16(x) OSSwapHostToLittleInt16(x)
19 #define be16toh(x) OSSwapBigToHostInt16(x)
20 #define le16toh(x) OSSwapLittleToHostInt16(x)
22 #define htobe32(x) OSSwapHostToBigInt32(x)
23 #define htole32(x) OSSwapHostToLittleInt32(x)
24 #define be32toh(x) OSSwapBigToHostInt32(x)
25 #define le32toh(x) OSSwapLittleToHostInt32(x)
27 #define htobe64(x) OSSwapHostToBigInt64(x)
28 #define htole64(x) OSSwapHostToLittleInt64(x)
29 #define be64toh(x) OSSwapBigToHostInt64(x)
30 #define le64toh(x) OSSwapLittleToHostInt64(x)
32 #define st_atim st_atimespec
33 #define st_ctim st_ctimespec
34 #define st_mtim st_mtimespec
36 #else /* Linux, etc... */
55 #define flock(fd, op) (0)
60 /* POSIX doesn't define WAIT_ANY, so provide it if it's not found. */
65 /* SOCK_NONBLOCK isn't available across BSDs... */
67 #define SOCK_NONBLOCK 00004000
70 /* On FreeBSD (and possibly others), EAI_NODATA was removed, in favour of
74 #define EAI_NODATA EAI_NONAME
78 #define __dead __attribute__ ((__noreturn__))
82 #define __unused __attribute__ ((__unused__))
86 #define pledge(s, p) (0)
87 #define unveil(s, p) (0)
91 #define cap_enter() (0)
94 #ifndef HAVE_SETRESGID
95 #define setresgid(a, b, c) (0)
98 #ifndef HAVE_SETRESUID
99 #define setresuid(a, b, c) (0)
102 #ifndef HAVE_LINUX_LANDLOCK_H
103 #define landlock_no_fs() (0)
105 int landlock_no_fs(void);
112 #ifndef HAVE_BSD_UUID
113 #include <uuid/uuid.h>
115 #define uuid_s_bad_version 1
116 #define uuid_s_invalid_string_uuid 2
117 #define uuid_s_no_memory 3
119 /* Length of a node address (an IEEE 802 address). */
120 #define _UUID_NODE_LEN 6
125 uint16_t time_hi_and_version;
126 uint8_t clock_seq_hi_and_reserved;
127 uint8_t clock_seq_low;
128 uint8_t node[_UUID_NODE_LEN];
131 int32_t uuid_equal(struct uuid *, struct uuid *, uint32_t *);
132 int32_t uuid_is_nil(struct uuid *, uint32_t *);
133 void uuid_create(uuid_t *, uint32_t *);
134 void uuid_create_nil(struct uuid *, uint32_t *);
135 void uuid_from_string(const char *, uuid_t *, uint32_t *);
136 void uuid_to_string(uuid_t *, char **, uint32_t *);
144 #include <inttypes.h>
148 #include <sys/queue.h>
152 #include <sys/tree.h>
154 #include "compat/tree.h"
161 #ifdef HAVE_LIBUTIL_H
167 #include "compat/imsg.h"
173 #include "compat/siphash.h"
176 /* Include Apple-specific headers. Mostly for crypto.*/
177 #if defined(__APPLE__)
178 #define COMMON_DIGEST_FOR_OPENSSL
179 #include <CommonCrypto/CommonDigest.h>
181 #define SHA512_BLOCK_LENGTH 128
182 typedef struct _SHA2_CTX {
187 u_int64_t bitcount[2];
188 u_int8_t buffer[SHA512_BLOCK_LENGTH];
190 #define SHA256Init SHA256_Init
191 #define SHA256Update SHA256_Update
192 #define SHA256Final SHA256_Final
196 #ifdef HAVE_SHA_AS_SHA1
199 #ifdef HAVE_SHA1_AS_SHA1
212 /* Catch-all for systems where the header files don't exist and/or the below
213 * still are not defined.
215 #ifndef SHA256_DIGEST_LENGTH
216 #define SHA256_DIGEST_LENGTH 32
219 #ifndef SHA256_DIGEST_STRING_LENGTH
220 #define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1)
223 #if defined(__DragonFly__)
224 #include <openssl/sha.h>
227 #ifdef NEEDS_SHA1_DEFS
228 #define SHA1_DIGEST_LENGTH SHA_DIGEST_LENGTH
229 #define SHA1_DIGEST_STRING_LENGTH (SHA1_DIGEST_LENGTH * 2 + 1)
231 #define SHA1_CTX SHA_CTX
232 #define SHA1Init SHA1_Init
233 #define SHA1Update SHA1_Update
234 #define SHA1Final SHA1_Final
238 * The following SA_LEN/SS_LEN dance comes from various source, notably
239 * OpenSMTP by way of OpenNTPD and OpenBGPD (thanks everyone!). got-portable
240 * has tweaked a lot of the following macros to suit the needs of
244 /* From OpenNTPD portable */
246 # if defined(HAVE_STRUCT_SOCKADDR_SA_LEN)
247 # define SA_LEN(x) ((x)->sa_len)
249 # define SA_LEN(x) ((x)->sa_family == AF_INET6 ? \
250 sizeof(struct sockaddr_in6) : \
251 sizeof(struct sockaddr_in))
256 /* From OpenBGPD portable */
258 # if defined(HAVE_STRUCT_SOCKADDR_STORAGE_SS_LEN)
259 # define SS_LEN(x) ((x)->ss_len)
261 # define SS_LEN(x) SA_LEN((struct sockaddr *)(x))
265 #ifndef HAVE_ASPRINTF
267 int asprintf(char **, const char *, ...);
268 int vasprintf(char **, const char *, va_list);
271 #ifndef HAVE_EXPLICIT_BZERO
272 /* explicit_bzero.c */
273 void explicit_bzero(void *, size_t);
276 #ifndef HAVE_GETDTABLECOUNT
277 /* getdtablecount.c */
278 int getdtablecount(void);
281 #ifndef HAVE_CLOSEFROM
286 #if defined (__FreeBSD__)
287 #define closefrom(fd) (closefrom(fd), 0)
292 char *strsep(char **, const char *);
295 #ifndef HAVE_STRTONUM
297 long long strtonum(const char *, long long, long long, const char **);
302 size_t strlcpy(char *, const char *, size_t);
307 size_t strlcat(char *, const char *, size_t);
312 size_t strnlen(const char *, size_t);
317 char *strndup(const char *, size_t);
320 #ifndef HAVE_GETPROGNAME
322 const char *getprogname(void);
327 ssize_t getline(char **, size_t *, FILE *);
330 #ifndef HAVE_FREEZERO
332 void freezero(void *, size_t);
335 #ifndef HAVE_GETDTABLECOUNT
336 /* getdtablecount.c */
337 int getdtablecount(void);
340 #ifndef HAVE_REALLOCARRAY
342 void *reallocarray(void *, size_t, size_t);
345 #ifndef HAVE_RECALLOCARRAY
346 /* recallocarray.c */
347 void *recallocarray(void *, size_t, size_t, size_t);
350 #ifndef HAVE_SETPROCTITLE
352 void setproctitle(const char *, ...);
355 #ifndef HAVE_FMT_SCALED
357 int fmt_scaled(long long, char *);
358 int scan_scaled(char *, long long *);
359 #define FMT_SCALED_STRSIZE 7 /* minus sign, 4 digits, suffix, null byte */
364 extern int BSDopterr;
365 extern int BSDoptind;
366 extern int BSDoptopt;
367 extern int BSDoptreset;
368 extern char *BSDoptarg;
369 int BSDgetopt(int, char *const *, const char *);
370 #define getopt(ac, av, o) BSDgetopt(ac, av, o)
371 #define opterr BSDopterr
372 #define optind BSDoptind
373 #define optopt BSDoptopt
374 #define optreset BSDoptreset
375 #define optarg BSDoptarg
379 /* Check for some of the non-portable timespec*() functions.
380 * This should largely come from libbsd for systems which
381 * aren't BSD, but this will depend on how old the library
384 #ifndef timespecisset
385 #define timespecisset(tsp) \
386 ((tsp)->tv_sec || (tsp)->tv_nsec)
390 #define timespecsub(tsp, usp, vsp) \
392 (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
393 (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
394 if ((vsp)->tv_nsec < 0) { \
396 (vsp)->tv_nsec += 1000000000L; \
402 #define timespeccmp(tvp, uvp, cmp) \
403 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
404 ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
405 ((tvp)->tv_sec cmp (uvp)->tv_sec))
408 #ifndef HAVE_BSD_MERGESORT
410 int mergesort(void *, size_t, size_t, int (*)(const void *, const void *));