portable: gotd: listen
[got-portable.git] / include / got_compat2.h
blob67e4b9b0cb9658c973a7006eb116204532d08b27
1 /*
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
20 #include <limits.h>
21 #include <sys/uio.h>
23 #if defined(__FreeBSD__)
24 #include <sys/endian.h>
25 #include <sys/capsicum.h>
26 #elif defined(__APPLE__)
27 #include <machine/endian.h>
28 #include <libkern/OSByteOrder.h>
29 #include "compat/bsd-poll.h"
31 #define FMT_SCALED_STRSIZE 7 /* minus sign, 4 digits, suffix, null byte */
33 #define htobe16(x) OSSwapHostToBigInt16(x)
34 #define htole16(x) OSSwapHostToLittleInt16(x)
35 #define be16toh(x) OSSwapBigToHostInt16(x)
36 #define le16toh(x) OSSwapLittleToHostInt16(x)
38 #define htobe32(x) OSSwapHostToBigInt32(x)
39 #define htole32(x) OSSwapHostToLittleInt32(x)
40 #define be32toh(x) OSSwapBigToHostInt32(x)
41 #define le32toh(x) OSSwapLittleToHostInt32(x)
43 #define htobe64(x) OSSwapHostToBigInt64(x)
44 #define htole64(x) OSSwapHostToLittleInt64(x)
45 #define be64toh(x) OSSwapBigToHostInt64(x)
46 #define le64toh(x) OSSwapLittleToHostInt64(x)
48 #define st_atim st_atimespec
49 #define st_ctim st_ctimespec
50 #define st_mtim st_mtimespec
52 #else /* Linux, etc... */
53 #include <endian.h>
54 #include <grp.h>
55 #include <stdlib.h>
56 #endif
58 #ifndef __GNUC__
59 #define __attribute__(a)
60 #ifndef __bounded__
61 #define __bounded__(a, b, c)
62 #endif
63 #endif
66 #ifndef UID_MAX
67 # define UID_MAX UINT_MAX /* max value for a uid_t */
68 #endif
69 #ifndef GID_MAX
70 # define GID_MAX UINT_MAX /* max value for a gid_t */
71 #endif
73 /* For flock. */
74 #ifndef O_EXLOCK
75 #define O_EXLOCK 0
76 #endif
78 #ifndef HAVE_FLOCK
79 #define LOCK_SH 0
80 #define LOCK_EX 0
81 #define LOCK_NB 0
82 #define flock(fd, op) (0)
83 #else
84 #include <sys/file.h>
85 #endif
87 /* POSIX doesn't define WAIT_ANY, so provide it if it's not found. */
88 #ifndef WAIT_ANY
89 #define WAIT_ANY (-1)
90 #endif
92 /* On FreeBSD (and possibly others), EAI_NODATA was removed, in favour of
93 * using EAI_NONAME.
95 #ifndef EAI_NODATA
96 #define EAI_NODATA EAI_NONAME
97 #endif
99 #ifndef __dead
100 #define __dead __attribute__ ((__noreturn__))
101 #endif
103 #ifndef __unused
104 #define __unused __attribute__ ((__unused__))
105 #endif
107 #ifndef __OpenBSD__
108 #define pledge(s, p) (0)
109 #define unveil(s, p) (0)
110 #endif
112 #ifndef __FreeBSD__
113 #define cap_enter() (0)
114 #endif
116 #ifndef HAVE_B64_NTOP
117 #undef b64_ntop
118 #undef b64_pton
119 int b64_ntop(u_char const *, size_t, char *, size_t);
120 int b64_pton(char const *, u_char *, size_t);
121 #endif
123 #ifndef HAVE_SETRESGID
124 #define setresgid(a, b, c) (0)
125 #endif
127 #ifndef HAVE_SETRESUID
128 #define setresuid(a, b, c) (0)
129 #endif
131 #ifndef HAVE_LINUX_LANDLOCK_H
132 #define landlock_no_fs() (0)
133 #else
134 int landlock_no_fs(void);
135 #endif
137 #ifndef INFTIM
138 #define INFTIM -1
139 #endif
141 #ifndef HAVE_BSD_UUID
142 #include <uuid/uuid.h>
143 #define uuid_s_ok 0
144 #define uuid_s_bad_version 1
145 #define uuid_s_invalid_string_uuid 2
146 #define uuid_s_no_memory 3
148 /* Length of a node address (an IEEE 802 address). */
149 #define _UUID_NODE_LEN 6
151 struct uuid {
152 uint32_t time_low;
153 uint16_t time_mid;
154 uint16_t time_hi_and_version;
155 uint8_t clock_seq_hi_and_reserved;
156 uint8_t clock_seq_low;
157 uint8_t node[_UUID_NODE_LEN];
160 int32_t uuid_equal(struct uuid *, struct uuid *, uint32_t *);
161 int32_t uuid_is_nil(struct uuid *, uint32_t *);
162 void uuid_create(uuid_t *, uint32_t *);
163 void uuid_create_nil(struct uuid *, uint32_t *);
164 void uuid_from_string(const char *, uuid_t *, uint32_t *);
165 void uuid_to_string(uuid_t *, char **, uint32_t *);
166 #else
167 #include <uuid.h>
168 #endif
170 #ifdef HAVE_STDINT_H
171 #include <stdint.h>
172 #else
173 #include <inttypes.h>
174 #endif
176 #ifdef HAVE_QUEUE_H
177 #include <sys/queue.h>
178 #endif
180 #ifndef HAVE_TREE_H
181 #include "compat/tree.h"
182 #else
183 #include <sys/tree.h>
184 #endif
186 #ifdef HAVE_UTIL_H
187 #include <util.h>
188 #endif
190 #ifdef HAVE_LIBUTIL_H
191 #include <libutil.h>
192 #endif
194 #ifndef IOV_MAX
195 # define IOV_MAX 1024
196 #endif
198 #ifndef HAVE_IMSG
199 #include "compat/imsg.h"
200 #else
201 #include <imsg.h>
202 #endif
204 #ifndef HAVE_SIPHASH
205 #include "compat/siphash.h"
206 #else
207 #include <siphash.h>
208 #endif
210 /* Include Apple-specific headers. Mostly for crypto.*/
211 #if defined(__APPLE__)
212 #define COMMON_DIGEST_FOR_OPENSSL
213 #include <CommonCrypto/CommonDigest.h>
215 #define SHA512_BLOCK_LENGTH 128
216 typedef struct _SHA2_CTX {
217 union {
218 u_int32_t st32[8];
219 u_int64_t st64[8];
220 } state;
221 u_int64_t bitcount[2];
222 u_int8_t buffer[SHA512_BLOCK_LENGTH];
223 } SHA2_CTX;
224 #endif
226 #if defined(__APPLE__) || defined(__NetBSD__)
227 #define SHA256Init SHA256_Init
228 #define SHA256Update SHA256_Update
229 #define SHA256Final SHA256_Final
230 #endif
232 #ifndef __APPLE__
233 #ifdef HAVE_SHA_H
234 # include <sha.h>
235 #endif
236 #ifdef HAVE_SHA1_H
237 # include <sha1.h>
238 #endif
239 #ifdef HAVE_SHA2_H
240 # include <sha2.h>
241 #else
242 # include "sha2.h"
243 #endif
244 #ifdef HAVE_SHA256_H
245 # include <sha256.h>
246 #endif
247 #endif
249 /* Catch-all for systems where the header files don't exist and/or the below
250 * still are not defined.
252 #ifndef SHA256_DIGEST_LENGTH
253 #define SHA256_DIGEST_LENGTH 32
254 #endif
256 #ifndef SHA256_DIGEST_STRING_LENGTH
257 #define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1)
258 #endif
260 #if defined(__DragonFly__)
261 #include <openssl/sha.h>
262 #endif
264 #ifndef SHA1_DIGEST_LENGTH
265 #define SHA1_DIGEST_LENGTH SHA_DIGEST_LENGTH
266 #define SHA1_DIGEST_STRING_LENGTH (SHA1_DIGEST_LENGTH * 2 + 1)
268 #define SHA1_CTX SHA_CTX
269 #define SHA1Init SHA1_Init
270 #define SHA1Update SHA1_Update
271 #define SHA1Final SHA1_Final
272 #endif
275 * The following SA_LEN/SS_LEN dance comes from various source, notably
276 * OpenSMTP by way of OpenNTPD and OpenBGPD (thanks everyone!). got-portable
277 * has tweaked a lot of the following macros to suit the needs of
278 * got-portable.
281 /* From OpenNTPD portable */
282 #if !defined(SA_LEN)
283 # if defined(HAVE_STRUCT_SOCKADDR_SA_LEN)
284 # define SA_LEN(x) ((x)->sa_len)
285 # else
286 # define SA_LEN(x) ((x)->sa_family == AF_INET6 ? \
287 sizeof(struct sockaddr_in6) : \
288 sizeof(struct sockaddr_in))
289 # endif
291 #endif
293 /* From OpenBGPD portable */
294 #if !defined(SS_LEN)
295 # if defined(HAVE_STRUCT_SOCKADDR_STORAGE_SS_LEN)
296 # define SS_LEN(x) ((x)->ss_len)
297 # else
298 # define SS_LEN(x) SA_LEN((struct sockaddr *)(x))
299 # endif
300 #endif
302 /* SOCK_NONBLOCK isn't available across BSDs... */
303 #if !defined(SOCK_NONBLOCK) && !defined(__linux__)
304 #define SOCK_NONBLOCK 00004000
305 #endif
307 #ifndef HAVE_ASPRINTF
308 /* asprintf.c */
309 int asprintf(char **, const char *, ...);
310 int vasprintf(char **, const char *, va_list);
311 #endif
313 #ifndef HAVE_EXPLICIT_BZERO
314 /* explicit_bzero.c */
315 void explicit_bzero(void *, size_t);
316 #endif
318 #ifndef HAVE_GETDTABLECOUNT
319 /* getdtablecount.c */
320 int getdtablecount(void);
321 #endif
323 #ifndef HAVE_CLOSEFROM
324 /* closefrom.c */
325 void closefrom(int);
326 #endif
328 #ifndef HAVE_STRSEP
329 /* strsep.c */
330 char *strsep(char **, const char *);
331 #endif
333 #ifndef HAVE_STRTONUM
334 /* strtonum.c */
335 long long strtonum(const char *, long long, long long, const char **);
336 #endif
338 #ifndef HAVE_STRLCPY
339 /* strlcpy.c */
340 size_t strlcpy(char *, const char *, size_t);
341 #endif
343 #ifndef HAVE_STRLCAT
344 /* strlcat.c */
345 size_t strlcat(char *, const char *, size_t);
346 #endif
348 #ifndef HAVE_STRNLEN
349 /* strnlen.c */
350 size_t strnlen(const char *, size_t);
351 #endif
353 #ifndef HAVE_STRNDUP
354 /* strndup.c */
355 char *strndup(const char *, size_t);
356 #endif
358 #ifndef HAVE_GETPROGNAME
359 /* getprogname.c */
360 const char *getprogname(void);
361 #endif
363 #ifndef HAVE_GETLINE
364 /* getline.c */
365 ssize_t getline(char **, size_t *, FILE *);
366 #endif
368 #ifndef HAVE_FREEZERO
369 /* freezero.c */
370 void freezero(void *, size_t);
371 #endif
373 #ifndef HAVE_GETDTABLECOUNT
374 /* getdtablecount.c */
375 int getdtablecount(void);
376 #endif
378 #ifndef HAVE_REALLOCARRAY
379 /* reallocarray.c */
380 void *reallocarray(void *, size_t, size_t);
381 #endif
383 #ifndef HAVE_RECALLOCARRAY
384 /* recallocarray.c */
385 void *recallocarray(void *, size_t, size_t, size_t);
386 #endif
388 #ifndef HAVE_SETPROCTITLE
389 /* setproctitle.c */
390 void setproctitle(const char *, ...);
391 #endif
393 #ifndef HAVE_FMT_SCALED
394 /* fmt_scaled.c */
395 int fmt_scaled(long long, char *);
396 int scan_scaled(char *, long long *);
397 #define FMT_SCALED_STRSIZE 7 /* minus sign, 4 digits, suffix, null byte */
398 #endif
400 #if !defined(HAVE_LIBBSD) && !defined(HAVE_GETOPT_OPTRESET)
401 /* getopt.c */
402 extern int BSDopterr;
403 extern int BSDoptind;
404 extern int BSDoptopt;
405 extern int BSDoptreset;
406 extern char *BSDoptarg;
407 int BSDgetopt(int, char *const *, const char *);
408 #define getopt(ac, av, o) BSDgetopt(ac, av, o)
409 #define opterr BSDopterr
410 #define optind BSDoptind
411 #define optopt BSDoptopt
412 #define optreset BSDoptreset
413 #define optarg BSDoptarg
414 #endif
416 /* Check for some of the non-portable timespec*() functions.
417 * This should largely come from libbsd for systems which
418 * aren't BSD, but this will depend on how old the library
419 * is.
421 #ifndef timespecisset
422 #define timespecisset(tsp) \
423 ((tsp)->tv_sec || (tsp)->tv_nsec)
424 #endif
426 #ifndef timespecsub
427 #define timespecsub(tsp, usp, vsp) \
428 do { \
429 (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
430 (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
431 if ((vsp)->tv_nsec < 0) { \
432 (vsp)->tv_sec--; \
433 (vsp)->tv_nsec += 1000000000L; \
435 } while (0)
436 #endif
438 #ifndef timespeccmp
439 #define timespeccmp(tvp, uvp, cmp) \
440 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
441 ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
442 ((tvp)->tv_sec cmp (uvp)->tv_sec))
443 #endif
445 #ifndef HAVE_MERGESORT
446 /* mergesort.c */
447 int mergesort(void *, size_t, size_t, int (*)(const void *, const void *));
448 #endif
449 #endif