Debian Codegraph @

src/include/missing-h

raw
1 #ifndef _FR_MISSING_H
2 #define _FR_MISSING_H
3
4 /*
5 * missing.h Replacements for functions that are or can be
6 * missing on some platforms.
7 * HAVE_* and WITH_* defines are substituted at
8 * build time by make with values from autoconf.h.
9 *
10 * Version: $Id: 5698797b0e0831c7d6b27ad6db46f815c6c3f23d $
11 *
12 */
13 RCSIDH(missing_h, "$Id: 5698797b0e0831c7d6b27ad6db46f815c6c3f23d $")
14
15 #ifdef HAVE_STDINT_H
16 # include <stdint.h>
17 #endif
18
19 #ifdef HAVE_STDDEF_H
20 # include <stddef.h>
21 #endif
22
23 #ifdef HAVE_SYS_TYPES_H
24 # include <sys/types.h>
25 #endif
26
27 #ifdef HAVE_INTTYPES_H
28 # include <inttypes.h>
29 #endif
30
31 #ifdef HAVE_STRINGS_H
32 # include <strings.h>
33 #endif
34
35 #ifdef HAVE_STRING_H
36 # include <string.h>
37 #endif
38
39 #ifdef HAVE_NETDB_H
40 # include <netdb.h>
41 #endif
42
43 #ifdef HAVE_NETINET_IN_H
44 # include <netinet/in.h>
45 #endif
46
47 #ifdef HAVE_ARPA_INET_H
48 # include <arpa/inet.h>
49 #endif
50
51 #ifdef HAVE_SYS_SELECT_H
52 # include <sys/select.h>
53 #endif
54
55 #ifdef HAVE_SYS_SOCKET_H
56 # include <sys/socket.h>
57 #endif
58
59 #ifdef HAVE_UNISTD_H
60 # include <unistd.h>
61 #endif
62
63 #ifndef HAVE_VSNPRINTF
64 # include <stdarg.h>
65 #endif
66
67 #ifdef HAVE_ERRNO_H
68 # include <errno.h>
69 #endif
70
71 /*
72 * Check for inclusion of <time.h>, versus <sys/time.h>
73 * Taken verbatim from the autoconf manual.
74 */
75 #ifdef TIME_WITH_SYS_TIME
76 # include <sys/time.h>
77 # include <time.h>
78 #else
79 # if HAVE_SYS_TIME_H
80 # include <sys/time.h>
81 # else
82 # include <time.h>
83 # endif
84 #endif
85
86 #ifdef HAVE_OPENSSL_SSL_H
87 # include <openssl/ssl.h>
88 #endif
89
90 #ifdef HAVE_OPENSSL_HMAC_H
91 # include <openssl/hmac.h>
92 #endif
93
94 #ifdef HAVE_OPENSSL_ASN1_H
95 # include <openssl/asn1.h>
96 #endif
97
98 #ifdef HAVE_OPENSSL_CONF_H
99 # include <openssl/conf.h>
100 #endif
101
102 /*
103 * Don't look for winsock.h if we're on cygwin.
104 */
105 #if !defined(__CYGWIN__) && defined(HAVE_WINSOCK_H)
106 # include <winsock.h>
107 #endif
108
109 #ifdef __APPLE__
110 #undef DARWIN
111 #define DARWIN (1)
112 #endif
113
114 #ifdef __cplusplus
115 extern "C" {
116 #endif
117
118 /*
119 * Functions from missing.c
120 */
121 #ifndef HAVE_STRNCASECMP
122 int strncasecmp(char *s1, char *s2, int n);
123 #endif
124
125 #ifndef HAVE_STRCASECMP
126 int strcasecmp(char *s1, char *s2);
127 #endif
128
129 #ifndef HAVE_STRSEP
130 char *strsep(char **stringp, char const *delim);
131 #endif
132
133 #ifndef HAVE_LOCALTIME_R
134 struct tm;
135 struct tm *localtime_r(time_t const *l_clock, struct tm *result);
136 #endif
137
138 #ifndef HAVE_CTIME_R
139 char *ctime_r(time_t const *l_clock, char *l_buf);
140 #endif
141
142 #ifndef HAVE_INET_PTON
143 int inet_pton(int af, char const *src, void *dst);
144 #endif
145 #ifndef HAVE_INET_NTOP
146 char const *inet_ntop(int af, void const *src, char *dst, size_t cnt);
147 #endif
148 #ifndef HAVE_CLOSEFROM
149 int closefrom(int fd);
150 #endif
151
152 #ifndef HAVE_SETLINEBUF
153 # ifdef HAVE_SETVBUF
154 # define setlinebuf(x) setvbuf(x, NULL, _IOLBF, 0)
155 # else
156 # define setlinebuf(x) 0
157 # endif
158 #endif
159
160 #ifndef INADDR_ANY
161 # define INADDR_ANY ((uint32_t) 0x00000000)
162 #endif
163
164 #ifndef INADDR_LOOPBACK
165 # define INADDR_LOOPBACK ((uint32_t) 0x7f000001) /* Inet 127.0.0.1 */
166 #endif
167
168 #ifndef INADDR_NONE
169 # define INADDR_NONE ((uint32_t) 0xffffffff)
170 #endif
171
172 #ifndef INADDRSZ
173 # define INADDRSZ 4
174 #endif
175
176 #ifndef INET_ADDRSTRLEN
177 # define INET_ADDRSTRLEN 16
178 #endif
179
180 #ifndef AF_UNSPEC
181 # define AF_UNSPEC 0
182 #endif
183
184 #ifndef AF_INET6
185 # define AF_INET6 10
186 #endif
187
188 #ifndef HAVE_STRUCT_IN6_ADDR
189 struct in6_addr
190 {
191 union {
192 uint8_t u6_addr8[16];
193 uint16_t u6_addr16[8];
194 uint32_t u6_addr32[4];
195 } in6_u;
196 # define s6_addr in6_u.u6_addr8
197 # define s6_addr16 in6_u.u6_addr16
198 # define s6_addr32 in6_u.u6_addr32
199 };
200
201 # ifndef IN6ADDRSZ
202 # define IN6ADDRSZ 16
203 # endif
204
205 # ifndef INET6_ADDRSTRLEN
206 # define INET6_ADDRSTRLEN 46
207 # endif
208
209 # ifndef IN6ADDR_ANY_INIT
210 # define IN6ADDR_ANY_INIT {{{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }}}
211 # endif
212
213 # ifndef IN6ADDR_LOOPBACK_INIT
214 # define IN6ADDR_LOOPBACK_INIT {{{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 }}}
215 # endif
216
217 # ifndef IN6_IS_ADDR_UNSPECIFIED
218 # define IN6_IS_ADDR_UNSPECIFIED(a) \
219 (((__const uint32_t *) (a))[0] == 0 \
220 && ((__const uint32_t *) (a))[1] == 0 \
221 && ((__const uint32_t *) (a))[2] == 0 \
222 && ((__const uint32_t *) (a))[3] == 0)
223 # endif
224
225 # ifndef IN6_IS_ADDR_LOOPBACK
226 # define IN6_IS_ADDR_LOOPBACK(a) \
227 (((__const uint32_t *) (a))[0] == 0 \
228 && ((__const uint32_t *) (a))[1] == 0 \
229 && ((__const uint32_t *) (a))[2] == 0 \
230 && ((__const uint32_t *) (a))[3] == htonl (1))
231 # endif
232
233 # ifndef IN6_IS_ADDR_MULTICAST
234 # define IN6_IS_ADDR_MULTICAST(a) (((__const uint8_t *) (a))[0] == 0xff)
235 # endif
236
237 # ifndef IN6_IS_ADDR_LINKLOCAL
238 # define IN6_IS_ADDR_LINKLOCAL(a) \
239 ((((__const uint32_t *) (a))[0] & htonl (0xffc00000)) \
240 == htonl (0xfe800000))
241 # endif
242
243 # ifndef IN6_IS_ADDR_SITELOCAL
244 # define IN6_IS_ADDR_SITELOCAL(a) \
245 ((((__const uint32_t *) (a))[0] & htonl (0xffc00000)) \
246 == htonl (0xfec00000))
247 # endif
248
249 # ifndef IN6_IS_ADDR_V4MAPPED
250 # define IN6_IS_ADDR_V4MAPPED(a) \
251 ((((__const uint32_t *) (a))[0] == 0) \
252 && (((__const uint32_t *) (a))[1] == 0) \
253 && (((__const uint32_t *) (a))[2] == htonl (0xffff)))
254 # endif
255
256 # ifndef IN6_IS_ADDR_V4COMPAT
257 # define IN6_IS_ADDR_V4COMPAT(a) \
258 ((((__const uint32_t *) (a))[0] == 0) \
259 && (((__const uint32_t *) (a))[1] == 0) \
260 && (((__const uint32_t *) (a))[2] == 0) \
261 && (ntohl (((__const uint32_t *) (a))[3]) > 1))
262 # endif
263
264 # ifndef IN6_ARE_ADDR_EQUAL
265 # define IN6_ARE_ADDR_EQUAL(a,b) \
266 ((((__const uint32_t *) (a))[0] == ((__const uint32_t *) (b))[0]) \
267 && (((__const uint32_t *) (a))[1] == ((__const uint32_t *) (b))[1]) \
268 && (((__const uint32_t *) (a))[2] == ((__const uint32_t *) (b))[2]) \
269 && (((__const uint32_t *) (a))[3] == ((__const uint32_t *) (b))[3]))
270 # endif
271 #endif /* HAVE_STRUCT_IN6_ADDR */
272
273 /*
274 * Functions from getaddrinfo.c
275 */
276
277 #ifndef HAVE_STRUCT_SOCKADDR_STORAGE
278 struct sockaddr_storage
279 {
280 uint16_t ss_family; /* Address family, etc. */
281 char ss_padding[128 - (sizeof(uint16_t))];
282 };
283 #endif
284
285 #ifndef HAVE_STRUCT_ADDRINFO
286 /* for old netdb.h */
287 # ifndef EAI_SERVICE
288 # define EAI_MEMORY 2
289 # define EAI_FAMILY 5 /* ai_family not supported */
290 # define EAI_NONAME 8 /* hostname nor servname provided, or not known */
291 # define EAI_SERVICE 9 /* servname not supported for ai_socktype */
292 # endif
293
294 /* dummy value for old netdb.h */
295 # ifndef AI_PASSIVE
296 # define AI_PASSIVE 1
297 # define AI_CANONNAME 2
298 # define AI_NUMERICHOST 4
299 # define NI_NUMERICHOST 2
300 # define NI_NAMEREQD 4
301 # define NI_NUMERICSERV 8
302
303 struct addrinfo
304 {
305 int ai_flags; /* Input flags. */
306 int ai_family; /* Protocol family for socket. */
307 int ai_socktype; /* Socket type. */
308 int ai_protocol; /* Protocol for socket. */
309 socklen_t ai_addrlen; /* Length of socket address. */
310 struct sockaddr *ai_addr; /* Socket address for socket. */
311 char *ai_canonname; /* Canonical name for service location. */
312 struct addrinfo *ai_next; /* Pointer to next in list. */
313 };
314
315 # endif /* AI_PASSIVE */
316 #endif /* HAVE_STRUCT_ADDRINFO */
317
318 /* Translate name of a service location and/or a service name to set of
319 socket addresses. */
320 #ifndef HAVE_GETADDRINFO
321 int getaddrinfo(char const *__name, char const *__service,
322 struct addrinfo const *__req,
323 struct addrinfo **__pai);
324
325 /* Free `addrinfo' structure AI including associated storage. */
326 void freeaddrinfo (struct addrinfo *__ai);
327
328 /* Convert error return from getaddrinfo() to a string. */
329 char const *gai_strerror (int __ecode);
330 #endif
331
332 /* Translate a socket address to a location and service name. */
333 #ifndef HAVE_GETNAMEINFO
334 int getnameinfo(struct sockaddr const *__sa,
335 socklen_t __salen, char *__host,
336 size_t __hostlen, char *__serv,
337 size_t __servlen, unsigned int __flags);
338 #endif
339
340 /*
341 * Functions from snprintf.c
342 */
343 #ifndef HAVE_VSNPRINTF
344 int vsnprintf(char *str, size_t count, char const *fmt, va_list arg);
345 #endif
346
347 #ifndef HAVE_SNPRINTF
348 int snprintf(char *str, size_t count, char const *fmt, ...);
349 #endif
350
351 /*
352 * Functions from strl{cat,cpy}.c
353 */
354 #ifndef HAVE_STRLCPY
355 size_t strlcpy(char *dst, char const *src, size_t siz);
356 #endif
357
358 #ifndef HAVE_STRLCAT
359 size_t strlcat(char *dst, char const *src, size_t siz);
360 #endif
361
362 #ifndef INT16SZ
363 # define INT16SZ (2)
364 #endif
365
366 #ifndef HAVE_GMTIME_R
367 struct tm *gmtime_r(time_t const *l_clock, struct tm *result);
368 #endif
369
370 #ifndef HAVE_VDPRINTF
371 int vdprintf (int fd, char const *format, va_list args);
372 #endif
373
374 #ifndef HAVE_GETTIMEOFDAY
375 int gettimeofday (struct timeval *tv, void *tz);
376 #endif
377
378 /*
379 * Work around different ctime_r styles
380 */
381 #if defined(CTIMERSTYLE) && (CTIMERSTYLE == SOLARISSTYLE)
382 # define CTIME_R(a,b,c) ctime_r(a,b,c)
383 # define ASCTIME_R(a,b,c) asctime_r(a,b,c)
384 #else
385 # define CTIME_R(a,b,c) ctime_r(a,b)
386 # define ASCTIME_R(a,b,c) asctime_r(a,b)
387 #endif
388
389 #ifdef WIN32
390 # undef interface
391 # undef mkdir
392 # define mkdir(_d, _p) mkdir(_d)
393 # define FR_DIR_SEP '\\'
394 # define FR_DIR_IS_RELATIVE(p) ((*p && (p[1] != ':')) || ((*p != '\\') && (*p != '\\')))
395 #else
396 # define FR_DIR_SEP '/'
397 # define FR_DIR_IS_RELATIVE(p) ((*p) != '/')
398 #endif
399
400 #ifndef offsetof
401 # define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
402 #endif
403
404 void timeval2ntp(struct timeval const *tv, uint8_t *ntp);
405 void ntp2timeval(struct timeval *tv, char const *ntp);
406
407 /*
408 * This is really hacky. Any code needing to perform operations on 128bit integers,
409 * or return 128BIT integers should check for HAVE_128BIT_INTEGERS.
410 */
411 #ifndef HAVE_UINT128_T
412 # ifdef HAVE___UINT128_T
413 # define HAVE_128BIT_INTEGERS
414 # define uint128_t __uint128_t
415 # define int128_t __int128_t
416 # else
417 typedef struct uint128_t { uint8_t v[16]; } uint128_t;
418 typedef struct int128_t { uint8_t v[16]; } int128_t;
419 # endif
420 #else
421 # define HAVE_128BIT_INTEGERS
422 #endif
423
424 /* abcd efgh -> dcba hgfe -> hgfe dcba */
425 #ifndef HAVE_HTONLL
426 # ifdef FR_LITTLE_ENDIAN
427 # ifdef HAVE_BUILTIN_BSWAP64
428 # define ntohll(x) __builtin_bswap64(x)
429 # else
430 # define ntohll(x) (((uint64_t)ntohl((uint32_t)(x >> 32))) | (((uint64_t)ntohl(((uint32_t) x)) << 32)))
431 # endif
432 # else
433 # define ntohll(x) (x)
434 # endif
435 # define htonll(x) ntohll(x)
436 #endif
437
438 #ifndef HAVE_HTONLLL
439 # ifdef FR_LITTLE_ENDIAN
440 # ifdef HAVE_128BIT_INTEGERS
441 # define ntohlll(x) (((uint128_t)ntohll((uint64_t)(x >> 64))) | (((uint128_t)ntohll(((uint64_t) x)) << 64)))
442 # else
443 uint128_t ntohlll(uint128_t num);
444 # endif
445 # else
446 # define ntohlll(x) (x)
447 # endif
448 # define htonlll(x) htohlll(x)
449 #endif
450
451 #ifndef HAVE_SIG_T
452 typedef void(*sig_t)(int);
453 #endif
454
455 #ifdef HAVE_OPENSSL_HMAC_H
456 # ifndef HAVE_HMAC_CTX_NEW
457 HMAC_CTX *HMAC_CTX_new(void);
458 # endif
459 # ifndef HAVE_HMAC_CTX_FREE
460 void HMAC_CTX_free(HMAC_CTX *ctx);
461 # endif
462 #endif
463
464 #ifdef HAVE_OPENSSL_ASN1_H
465 # ifndef HAVE_ASN1_STRING_GET0_DATA
466 static inline const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *x)
467 {
468 /*
469 * Trick the compiler into not issuing the warning on qualifier stripping.
470 * We know that ASN1_STRING_data doesn't change x, and we're casting
471 * the return value back to const immediately, so it's OK.
472 */
473 union {
474 const ASN1_STRING *c;
475 ASN1_STRING *nc;
476 } const_strip = {.c = x};
477 return ASN1_STRING_data(const_strip.nc);
478 }
479 # endif
480 #endif
481
482 #ifdef HAVE_OPENSSL_CONF_H
483 # ifndef HAVE_CONF_MODULES_LOAD_FILE
484 static inline int CONF_modules_load_file(const char *filename,
485 const char *appname,
486 unsigned long flags)
487 {
488 (void)filename;
489 (void)flags;
490 return OPENSSL_config(appname);
491 }
492 # endif
493 #endif
494
495 #ifdef __cplusplus
496 }
497 #endif
498
499 #ifdef HAVE_OPENSSL_SSL_H
500 # ifndef HAVE_SSL_GET_CLIENT_RANDOM
501 size_t SSL_get_client_random(const SSL *s, unsigned char *out, size_t outlen);
502 # endif
503 # ifndef HAVE_SSL_GET_SERVER_RANDOM
504 size_t SSL_get_server_random(const SSL *s, unsigned char *out, size_t outlen);
505 # endif
506 # ifndef HAVE_SSL_SESSION_GET_MASTER_KEY
507 size_t SSL_SESSION_get_master_key(const SSL_SESSION *s,
508 unsigned char *out, size_t outlen);
509 # endif
510 #endif
511
512 /*
513 * NetBSD doesn't have O_DIRECTORY.
514 */
515 #ifndef O_DIRECTORY
516 #define O_DIRECTORY 0
517 #endif
518
519 #ifndef O_NOFOLLOW
520 #define O_NOFOLLOW 0
521 #endif
522
523 /*
524 * Not really missing, but may be submitted as patches
525 * to the talloc project at some point in the future.
526 */
527 char *talloc_typed_strdup(const void *t, const char *p);
528 char *talloc_typed_asprintf(const void *t, const char *fmt, ...) CC_HINT(format (printf, 2, 3));
529 char *talloc_bstrndup(const void *t, char const *in, size_t inlen);
530 #endif /* _FR_MISSING_H */