Remove redundant file from freeradius-abfab list.
[freeradius.git] / src / include / missing-h
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$
11  *
12  */
13 RCSIDH(missing_h, "$Id$")
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 /*
87  *      Don't look for winsock.h if we're on cygwin.
88  */
89 #if !defined(__CYGWIN__) && defined(HAVE_WINSOCK_H)
90 #  include <winsock.h>
91 #endif
92
93 #ifdef __APPLE__
94 #undef DARWIN
95 #define DARWIN (1)
96 #endif
97
98 #ifdef __cplusplus
99 extern "C" {
100 #endif
101
102 /*
103  *      Functions from missing.c
104  */
105 #ifndef HAVE_STRNCASECMP
106 int strncasecmp(char *s1, char *s2, int n);
107 #endif
108
109 #ifndef HAVE_STRCASECMP
110 int strcasecmp(char *s1, char *s2);
111 #endif
112
113 #ifndef HAVE_STRSEP
114 char *strsep(char **stringp, char const *delim);
115 #endif
116
117 #ifndef HAVE_LOCALTIME_R
118 struct tm;
119 struct tm *localtime_r(time_t const *l_clock, struct tm *result);
120 #endif
121
122 #ifndef HAVE_CTIME_R
123 char *ctime_r(time_t const *l_clock, char *l_buf);
124 #endif
125
126 #ifndef HAVE_INET_PTON
127 int             inet_pton(int af, char const *src, void *dst);
128 #endif
129 #ifndef HAVE_INET_NTOP
130 char const      *inet_ntop(int af, void const *src, char *dst, size_t cnt);
131 #endif
132 #ifndef HAVE_CLOSEFROM
133 int             closefrom(int fd);
134 #endif
135
136 #ifndef HAVE_SETLINEBUF
137 #  ifdef HAVE_SETVBUF
138 #    define setlinebuf(x) setvbuf(x, NULL, _IOLBF, 0)
139 #  else
140 #    define setlinebuf(x)     0
141 #  endif
142 #endif
143
144 #ifndef INADDR_ANY
145 #  define INADDR_ANY      ((uint32_t) 0x00000000)
146 #endif
147
148 #ifndef INADDR_LOOPBACK
149 #  define INADDR_LOOPBACK ((uint32_t) 0x7f000001) /* Inet 127.0.0.1 */
150 #endif
151
152 #ifndef INADDR_NONE
153 #  define INADDR_NONE     ((uint32_t) 0xffffffff)
154 #endif
155
156 #ifndef INADDRSZ
157 #  define INADDRSZ 4
158 #endif
159
160 #ifndef INET_ADDRSTRLEN
161 #  define INET_ADDRSTRLEN 16
162 #endif
163
164 #ifndef AF_UNSPEC
165 #  define AF_UNSPEC 0
166 #endif
167
168 #ifndef AF_INET6
169 #  define AF_INET6 10
170 #endif
171
172 #ifndef HAVE_STRUCT_IN6_ADDR
173 struct in6_addr
174 {
175         union {
176                 uint8_t u6_addr8[16];
177                 uint16_t u6_addr16[8];
178                 uint32_t u6_addr32[4];
179         } in6_u;
180 #  define s6_addr       in6_u.u6_addr8
181 #  define s6_addr16     in6_u.u6_addr16
182 #  define s6_addr32     in6_u.u6_addr32
183 };
184
185 #  ifndef IN6ADDRSZ
186 #    define IN6ADDRSZ 16
187 #  endif
188
189 #  ifndef INET6_ADDRSTRLEN
190 #    define INET6_ADDRSTRLEN 46
191 #  endif
192
193 #  ifndef IN6ADDR_ANY_INIT
194 #    define IN6ADDR_ANY_INIT            {{{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }}}
195 #  endif
196
197 #  ifndef IN6ADDR_LOOPBACK_INIT
198 #    define IN6ADDR_LOOPBACK_INIT       {{{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 }}}
199 #  endif
200
201 #  ifndef IN6_IS_ADDR_UNSPECIFIED
202 #    define IN6_IS_ADDR_UNSPECIFIED(a) \
203         (((__const uint32_t *) (a))[0] == 0                                   \
204          && ((__const uint32_t *) (a))[1] == 0                                \
205          && ((__const uint32_t *) (a))[2] == 0                                \
206          && ((__const uint32_t *) (a))[3] == 0)
207 #  endif
208
209 #  ifndef IN6_IS_ADDR_LOOPBACK
210 #    define IN6_IS_ADDR_LOOPBACK(a) \
211         (((__const uint32_t *) (a))[0] == 0                                   \
212          && ((__const uint32_t *) (a))[1] == 0                                \
213          && ((__const uint32_t *) (a))[2] == 0                                \
214          && ((__const uint32_t *) (a))[3] == htonl (1))
215 #  endif
216
217 #  ifndef IN6_IS_ADDR_MULTICAST
218 #    define IN6_IS_ADDR_MULTICAST(a) (((__const uint8_t *) (a))[0] == 0xff)
219 #  endif
220
221 #  ifndef IN6_IS_ADDR_LINKLOCAL
222 #    define IN6_IS_ADDR_LINKLOCAL(a) \
223         ((((__const uint32_t *) (a))[0] & htonl (0xffc00000))                 \
224          == htonl (0xfe800000))
225 #  endif
226
227 #  ifndef IN6_IS_ADDR_SITELOCAL
228 #    define IN6_IS_ADDR_SITELOCAL(a) \
229         ((((__const uint32_t *) (a))[0] & htonl (0xffc00000))                 \
230          == htonl (0xfec00000))
231 #  endif
232
233 #  ifndef IN6_IS_ADDR_V4MAPPED
234 #    define IN6_IS_ADDR_V4MAPPED(a) \
235         ((((__const uint32_t *) (a))[0] == 0)                                 \
236          && (((__const uint32_t *) (a))[1] == 0)                              \
237          && (((__const uint32_t *) (a))[2] == htonl (0xffff)))
238 #  endif
239
240 #  ifndef IN6_IS_ADDR_V4COMPAT
241 #    define IN6_IS_ADDR_V4COMPAT(a) \
242         ((((__const uint32_t *) (a))[0] == 0)                                 \
243          && (((__const uint32_t *) (a))[1] == 0)                              \
244          && (((__const uint32_t *) (a))[2] == 0)                              \
245          && (ntohl (((__const uint32_t *) (a))[3]) > 1))
246 #  endif
247
248 #  ifndef IN6_ARE_ADDR_EQUAL
249 #    define IN6_ARE_ADDR_EQUAL(a,b) \
250         ((((__const uint32_t *) (a))[0] == ((__const uint32_t *) (b))[0])     \
251          && (((__const uint32_t *) (a))[1] == ((__const uint32_t *) (b))[1])  \
252          && (((__const uint32_t *) (a))[2] == ((__const uint32_t *) (b))[2])  \
253          && (((__const uint32_t *) (a))[3] == ((__const uint32_t *) (b))[3]))
254 #  endif
255 #endif /* HAVE_STRUCT_IN6_ADDR */
256
257 /*
258  *      Functions from getaddrinfo.c
259  */
260
261 #ifndef HAVE_STRUCT_SOCKADDR_STORAGE
262 struct sockaddr_storage
263 {
264     uint16_t ss_family;         /* Address family, etc.  */
265     char ss_padding[128 - (sizeof(uint16_t))];
266 };
267 #endif
268
269 #ifndef HAVE_STRUCT_ADDRINFO
270 /* for old netdb.h */
271 #  ifndef EAI_SERVICE
272 #    define EAI_MEMORY      2
273 #    define EAI_FAMILY      5   /* ai_family not supported */
274 #    define EAI_NONAME      8   /* hostname nor servname provided, or not known */
275 #    define EAI_SERVICE     9   /* servname not supported for ai_socktype */
276 #  endif
277
278 /* dummy value for old netdb.h */
279 #  ifndef AI_PASSIVE
280 #    define AI_PASSIVE      1
281 #    define AI_CANONNAME    2
282 #    define AI_NUMERICHOST  4
283 #    define NI_NUMERICHOST  2
284 #    define NI_NAMEREQD     4
285 #    define NI_NUMERICSERV  8
286
287 struct addrinfo
288 {
289   int ai_flags;                 /* Input flags.  */
290   int ai_family;                /* Protocol family for socket.  */
291   int ai_socktype;              /* Socket type.  */
292   int ai_protocol;              /* Protocol for socket.  */
293   socklen_t ai_addrlen;         /* Length of socket address.  */
294   struct sockaddr *ai_addr;     /* Socket address for socket.  */
295   char *ai_canonname;           /* Canonical name for service location.  */
296   struct addrinfo *ai_next;     /* Pointer to next in list.  */
297 };
298
299 #  endif /* AI_PASSIVE */
300 #endif /* HAVE_STRUCT_ADDRINFO */
301
302 /* Translate name of a service location and/or a service name to set of
303    socket addresses. */
304 #ifndef HAVE_GETADDRINFO
305 int getaddrinfo(char const *__name, char const *__service,
306                 struct addrinfo const *__req,
307                 struct addrinfo **__pai);
308
309 /* Free `addrinfo' structure AI including associated storage.  */
310 void freeaddrinfo (struct addrinfo *__ai);
311
312 /* Convert error return from getaddrinfo() to a string.  */
313 char const *gai_strerror (int __ecode);
314 #endif
315
316 /* Translate a socket address to a location and service name. */
317 #ifndef HAVE_GETNAMEINFO
318 int getnameinfo(struct sockaddr const *__sa,
319                 socklen_t __salen, char *__host,
320                 size_t __hostlen, char *__serv,
321                 size_t __servlen, unsigned int __flags);
322 #endif
323
324 /*
325  *      Functions from snprintf.c
326  */
327 #ifndef HAVE_VSNPRINTF
328 int vsnprintf(char *str, size_t count, char const *fmt, va_list arg);
329 #endif
330
331 #ifndef HAVE_SNPRINTF
332 int snprintf(char *str, size_t count, char const *fmt, ...);
333 #endif
334
335 /*
336  *      Functions from strl{cat,cpy}.c
337  */
338 #ifndef HAVE_STRLCPY
339 size_t strlcpy(char *dst, char const *src, size_t siz);
340 #endif
341
342 #ifndef HAVE_STRLCAT
343 size_t strlcat(char *dst, char const *src, size_t siz);
344 #endif
345
346 #ifndef INT16SZ
347 #  define INT16SZ (2)
348 #endif
349
350 #ifndef HAVE_GMTIME_R
351 struct tm *gmtime_r(time_t const *l_clock, struct tm *result);
352 #endif
353
354 #ifndef HAVE_GETTIMEOFDAY
355 int gettimeofday (struct timeval *tv, void *tz);
356 #endif
357
358 /*
359  *      Work around different ctime_r styles
360  */
361 #if defined(CTIMERSTYLE) && (CTIMERSTYLE == SOLARISSTYLE)
362 #  define CTIME_R(a,b,c) ctime_r(a,b,c)
363 #  define ASCTIME_R(a,b,c) asctime_r(a,b,c)
364 #else
365 #  define CTIME_R(a,b,c) ctime_r(a,b)
366 #  define ASCTIME_R(a,b,c) asctime_r(a,b)
367 #endif
368
369 #ifdef WIN32
370 #  undef interface
371 #  undef mkdir
372 #  define mkdir(_d, _p) mkdir(_d)
373 #  define FR_DIR_SEP '\\'
374 #  define FR_DIR_IS_RELATIVE(p) ((*p && (p[1] != ':')) || ((*p != '\\') && (*p != '\\')))
375 #else
376 #  define FR_DIR_SEP '/'
377 #  define FR_DIR_IS_RELATIVE(p) ((*p) != '/')
378 #endif
379
380 #ifndef offsetof
381 #  define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
382 #endif
383
384 void timeval2ntp(struct  timeval const *tv, uint8_t *ntp);
385 void ntp2timeval(struct timeval *tv, char const *ntp);
386
387 /*
388  *      This is really hacky. Any code needing to perform operations on 128bit integers,
389  *      or return 128BIT integers should check for HAVE_128BIT_INTEGERS.
390  */
391 #ifndef HAVE_UINT128_T
392 #  ifdef HAVE___UINT128_T
393 #    define HAVE_128BIT_INTEGERS
394 #    define uint128_t __uint128_t
395 #    define int128_t __int128_t
396 #  else
397 typedef struct uint128_t { uint8_t v[16]; } uint128_t;
398 typedef struct int128_t { uint8_t v[16]; } int128_t;
399 #  endif
400 #else
401 #  define HAVE_128BIT_INTEGERS
402 #endif
403
404 /* abcd efgh -> dcba hgfe -> hgfe dcba */
405 #ifndef HAVE_HTONLL
406 #  ifdef FR_LITTLE_ENDIAN
407 #    ifdef HAVE_BUILTIN_BSWAP64
408 #      define ntohll(x) __builtin_bswap64(x)
409 #    else
410 #      define ntohll(x) (((uint64_t)ntohl((uint32_t)(x >> 32))) | (((uint64_t)ntohl(((uint32_t) x)) << 32)))
411 #    endif
412 #  else
413 #    define ntohll(x) (x)
414 #  endif
415 #  define htonll(x) ntohll(x)
416 #endif
417
418 #ifndef HAVE_HTONLLL
419 #  ifdef FR_LITTLE_ENDIAN
420 #    ifdef HAVE_128BIT_INTEGERS
421 #      define ntohlll(x) (((uint128_t)ntohll((uint64_t)(x >> 64))) | (((uint128_t)ntohll(((uint64_t) x)) << 64)))
422 #    else
423 uint128_t ntohlll(uint128_t num);
424 #    endif
425 #  else
426 #    define ntohlll(x) (x)
427 #  endif
428 #  define htonlll(x) htohlll(x)
429 #endif
430
431 #ifndef HAVE_SIG_T
432 typedef void(*sig_t)(int);
433 #endif
434
435 #ifdef __cplusplus
436 }
437 #endif
438
439 /*
440  *      Not really missing, but may be submitted as patches
441  *      to the talloc project at some point in the future.
442  */
443 char *talloc_typed_strdup(const void *t, const char *p);
444 char *talloc_typed_asprintf(const void *t, const char *fmt, ...) CC_HINT(format (printf, 2, 3));
445 char *talloc_bstrndup(const void *t, char const *in, size_t inlen);
446 #endif /* _FR_MISSING_H */