Add "extern C {...} to header files for C++ builds.
[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  *
8  * Version:     $Id$
9  *
10  */
11
12 #include <freeradius-devel/ident.h>
13 RCSIDH(missing_h, "$Id$")
14
15 #include <freeradius-devel/autoconf.h>
16
17 #ifdef HAVE_STDINT_H
18 #include <stdint.h>
19 #endif
20
21 #ifdef HAVE_SYS_TYPES_H
22 #include <sys/types.h>
23 #endif
24
25 #ifdef HAVE_INTTYPES_H
26 #include <inttypes.h>
27 #endif
28
29 #ifdef HAVE_STRINGS_H
30 #include <strings.h>
31 #endif
32
33 #ifdef HAVE_STRING_H
34 #include <string.h>
35 #endif
36
37 #ifdef HAVE_NETDB_H
38 #include <netdb.h>
39 #endif
40
41 #ifdef HAVE_NETINET_IN_H
42 #include        <netinet/in.h>
43 #endif
44
45 #ifdef HAVE_ARPA_INET_H
46 #include        <arpa/inet.h>
47 #endif
48
49 #ifdef HAVE_SYS_SELECT_H
50 #include <sys/select.h>
51 #endif
52
53 #ifdef HAVE_SYS_SOCKET_H
54 #include <sys/socket.h>
55 #endif
56
57 #ifdef HAVE_UNISTD_H
58 #include <unistd.h>
59 #endif
60
61 #ifndef HAVE_VSNPRINTF
62 #include <stdarg.h>
63 #endif
64
65 #ifdef HAVE_SYS_LOCKING_H
66 #include <sys/locking.h>
67 #endif
68
69 /*
70  *  Check for inclusion of <time.h>, versus <sys/time.h>
71  *  Taken verbatim from the autoconf manual.
72  */
73 #if TIME_WITH_SYS_TIME
74 # include <sys/time.h>
75 # include <time.h>
76 #else
77 # ifdef HAVE_SYS_TIME_H
78 #  include <sys/time.h>
79 # else
80 #  include <time.h>
81 # endif
82 #endif
83
84 /*
85  *      Don't look for winsock.h if we're on cygwin.
86  */
87 #ifndef __CYGWIN__
88 #ifdef HAVE_WINSOCK_H
89 #include <winsock.h>
90 #endif
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
106 #ifndef HAVE_STRNCASECMP
107 extern int strncasecmp(char *s1, char *s2, int n);
108 #endif
109
110 #ifndef HAVE_STRCASECMP
111 extern int strcasecmp(char *s1, char *s2);
112 #endif
113
114 #ifndef HAVE_STRSEP
115 extern char *strsep(char **stringp, const char *delim);
116 #endif
117
118 #ifndef HAVE_LOCALTIME_R
119 struct tm;
120 struct tm *localtime_r(const time_t *l_clock, struct tm *result);
121 #endif
122
123 #ifndef HAVE_CTIME_R
124 char *ctime_r(const time_t *l_clock, char *l_buf);
125 #endif
126
127 #if defined(NEED_DECLARATION_CRYPT) || !defined(HAVE_CRYPT)
128 char *crypt(char *key, char *salt);
129 #endif
130
131 #ifdef NEED_DECLARATION_STRNCASECMP
132 int strncasecmp(char *s1, char *s2, int n);
133 #endif
134
135 #ifdef NEED_DECLARATION_STRCASECMP
136 int strcasecmp(char *s1, char *s2);
137 #endif
138
139 #if defined(NEED_DECLARATION_INET_ATON) || !defined(HAVE_INET_ATON)
140 struct in_addr;
141 int inet_aton(const char *cp, struct in_addr *inp);
142 #endif
143
144 #ifndef HAVE_SETLINEBUF
145 #ifdef HAVE_SETVBUF
146 #define setlinebuf(x) setvbuf(x, NULL, _IOLBF, 0)
147 #else
148 #define setlinebuf(x)     0
149 #endif
150 #endif
151
152 #ifdef NEED_DECLARATION_SETLINEBUF
153 #define setlinebuf(x)     0
154 #endif
155
156 #ifdef NEED_DECLARATION_GETUSERSHELL
157 char *getusershell(void);
158 #endif
159
160 #ifdef NEED_DECLARATION_ENDUSERSHELL
161 void endusershell(void);
162 #endif
163
164 #ifndef INADDR_ANY
165 #define INADDR_ANY      ((uint32_t) 0x00000000)
166 #endif
167
168 #ifndef INADDR_LOOPBACK
169 #define INADDR_LOOPBACK ((uint32_t) 0x7f000001) /* Inet 127.0.0.1 */
170 #endif
171
172 #ifndef INADDR_NONE
173 #define INADDR_NONE     ((uint32_t) 0xffffffff)
174 #endif
175
176 #ifndef INADDRSZ
177 #define INADDRSZ 4
178 #endif
179
180 #ifndef INET_ADDRSTRLEN
181 #define INET_ADDRSTRLEN 16
182 #endif
183
184 #ifndef AF_UNSPEC
185 #define AF_UNSPEC 0
186 #endif
187
188 #ifndef AF_INET6
189 #define AF_INET6 10
190 #endif
191
192 #ifndef HAVE_STRUCT_IN6_ADDR
193 struct in6_addr
194 {
195         union {
196                 uint8_t u6_addr8[16];
197                 uint16_t u6_addr16[8];
198                 uint32_t u6_addr32[4];
199         } in6_u;
200 #define s6_addr                 in6_u.u6_addr8
201 #define s6_addr16               in6_u.u6_addr16
202 #define s6_addr32               in6_u.u6_addr32
203 };
204
205 #ifndef IN6ADDRSZ
206 #define IN6ADDRSZ 16
207 #endif
208
209 #ifndef INET6_ADDRSTRLEN
210 #define INET6_ADDRSTRLEN 46
211 #endif
212
213 #ifndef IN6ADDR_ANY_INIT
214 #define IN6ADDR_ANY_INIT        {{{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }}}
215 #endif
216
217 #ifndef IN6ADDR_LOOPBACK_INIT
218 #define IN6ADDR_LOOPBACK_INIT   {{{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 }}}
219 #endif
220
221 #ifndef IN6_IS_ADDR_UNSPECIFIED
222 #define IN6_IS_ADDR_UNSPECIFIED(a) \
223         (((__const uint32_t *) (a))[0] == 0                                   \
224          && ((__const uint32_t *) (a))[1] == 0                                \
225          && ((__const uint32_t *) (a))[2] == 0                                \
226          && ((__const uint32_t *) (a))[3] == 0)
227 #endif
228
229 #ifndef IN6_IS_ADDR_LOOPBACK
230 #define IN6_IS_ADDR_LOOPBACK(a) \
231         (((__const uint32_t *) (a))[0] == 0                                   \
232          && ((__const uint32_t *) (a))[1] == 0                                \
233          && ((__const uint32_t *) (a))[2] == 0                                \
234          && ((__const uint32_t *) (a))[3] == htonl (1))
235 #endif
236
237 #ifndef IN6_IS_ADDR_MULTICAST
238 #define IN6_IS_ADDR_MULTICAST(a) (((__const uint8_t *) (a))[0] == 0xff)
239 #endif
240
241 #ifndef IN6_IS_ADDR_LINKLOCAL
242 #define IN6_IS_ADDR_LINKLOCAL(a) \
243         ((((__const uint32_t *) (a))[0] & htonl (0xffc00000))                 \
244          == htonl (0xfe800000))
245 #endif
246
247 #ifndef IN6_IS_ADDR_SITELOCAL
248 #define IN6_IS_ADDR_SITELOCAL(a) \
249         ((((__const uint32_t *) (a))[0] & htonl (0xffc00000))                 \
250          == htonl (0xfec00000))
251 #endif
252
253 #ifndef IN6_IS_ADDR_V4MAPPED
254 #define IN6_IS_ADDR_V4MAPPED(a) \
255         ((((__const uint32_t *) (a))[0] == 0)                                 \
256          && (((__const uint32_t *) (a))[1] == 0)                              \
257          && (((__const uint32_t *) (a))[2] == htonl (0xffff)))
258 #endif
259
260 #ifndef IN6_IS_ADDR_V4COMPAT
261 #define IN6_IS_ADDR_V4COMPAT(a) \
262         ((((__const uint32_t *) (a))[0] == 0)                                 \
263          && (((__const uint32_t *) (a))[1] == 0)                              \
264          && (((__const uint32_t *) (a))[2] == 0)                              \
265          && (ntohl (((__const uint32_t *) (a))[3]) > 1))
266 #endif
267
268 #ifndef IN6_ARE_ADDR_EQUAL
269 #define IN6_ARE_ADDR_EQUAL(a,b) \
270         ((((__const uint32_t *) (a))[0] == ((__const uint32_t *) (b))[0])     \
271          && (((__const uint32_t *) (a))[1] == ((__const uint32_t *) (b))[1])  \
272          && (((__const uint32_t *) (a))[2] == ((__const uint32_t *) (b))[2])  \
273          && (((__const uint32_t *) (a))[3] == ((__const uint32_t *) (b))[3]))
274 #endif
275
276 #endif /* HAVE_STRUCT_IN6_ADDR */
277
278 /*
279  *      Functions from getaddrinfo.c
280  */
281
282 #ifndef HAVE_STRUCT_SOCKADDR_STORAGE
283 struct sockaddr_storage
284 {
285     uint16_t ss_family;         /* Address family, etc.  */
286     char ss_padding[128 - (sizeof(uint16_t))];
287 };
288 #endif /* HAVE_STRUCT_SOCKADDR_STORAGE */
289
290 #ifndef HAVE_STRUCT_ADDRINFO
291
292 /* for old netdb.h */
293 #ifndef EAI_SERVICE
294 #define EAI_MEMORY      2
295 #define EAI_FAMILY      5    /* ai_family not supported */
296 #define EAI_NONAME      8    /* hostname nor servname provided, or not known */
297 #define EAI_SERVICE     9   /* servname not supported for ai_socktype */
298 #endif
299
300 /* dummy value for old netdb.h */
301 #ifndef AI_PASSIVE
302 #define AI_PASSIVE      1
303 #define AI_CANONNAME    2
304 #define AI_NUMERICHOST  4
305 #define NI_NUMERICHOST  2
306 #define NI_NAMEREQD     4
307 #define NI_NUMERICSERV  8
308
309 struct addrinfo
310 {
311   int ai_flags;                 /* Input flags.  */
312   int ai_family;                /* Protocol family for socket.  */
313   int ai_socktype;              /* Socket type.  */
314   int ai_protocol;              /* Protocol for socket.  */
315   socklen_t ai_addrlen;         /* Length of socket address.  */
316   struct sockaddr *ai_addr;     /* Socket address for socket.  */
317   char *ai_canonname;           /* Canonical name for service location.  */
318   struct addrinfo *ai_next;     /* Pointer to next in list.  */
319 };
320
321 #endif /* AI_PASSIVE */
322
323 #endif /* HAVE_STRUCT_ADDRINFO */
324
325 /* Translate name of a service location and/or a service name to set of
326    socket addresses. */
327 #ifndef HAVE_GETADDRINFO
328 extern int getaddrinfo (const char *__name,
329                         const char *__service,
330                         const struct addrinfo *__req,
331                         struct addrinfo **__pai);
332
333 /* Free `addrinfo' structure AI including associated storage.  */
334 extern void freeaddrinfo (struct addrinfo *__ai);
335
336 /* Convert error return from getaddrinfo() to a string.  */
337 extern const char *gai_strerror (int __ecode);
338 #endif
339
340 /* Translate a socket address to a location and service name. */
341 #ifndef HAVE_GETNAMEINFO
342 extern int getnameinfo (const struct sockaddr *__sa,
343                         socklen_t __salen, char *__host,
344                         size_t __hostlen, char *__serv,
345                         size_t __servlen, unsigned int __flags);
346 #endif
347
348 /*
349  *      Functions from snprintf.c
350  */
351
352 #ifndef HAVE_VSNPRINTF
353 extern int vsnprintf(char *str, size_t count, const char *fmt, va_list arg);
354 #endif
355
356 #ifndef HAVE_SNPRINTF
357 extern int snprintf(char *str, size_t count, const char *fmt, ...);
358 #endif
359
360 /*
361  *      Functions from strl{cat,cpy}.c
362  */
363
364 #ifndef HAVE_STRLCPY
365 extern size_t strlcpy(char *dst, const char *src, size_t siz);
366 #endif
367
368 #ifndef HAVE_STRLCAT
369 extern size_t strlcat(char *dst, const char *src, size_t siz);
370 #endif
371
372 #ifndef INT16SZ
373 #define INT16SZ (2)
374 #endif
375
376 #ifndef HAVE_GMTIME_R
377 struct tm *gmtime_r(const time_t *l_clock, struct tm *result);
378 #endif
379
380 #ifndef HAVE_GETTIMEOFDAY
381 int gettimeofday (struct timeval *tv, void *tz);
382 #endif
383
384 #ifdef WIN32
385 #undef interface
386 #undef mkdir
387 #define mkdir(_d, _p) mkdir(_d)
388 #define FR_DIR_SEP '\\'
389 #define FR_DIR_IS_RELATIVE(p) ((*p && (p[1] != ':')) || ((*p != '\\') && (*p != '\\')))
390 #else
391 #define FR_DIR_SEP '/'
392 #define FR_DIR_IS_RELATIVE(p) ((*p) != '/')
393 #endif
394
395 #ifdef HAVE_SYS_LOCKING_H
396 #define lockf _locking
397
398 #define F_ULOCK _LK_UNLCK /* Unlock locked sections. */
399 #define F_LOCK  _LK_LOCK  /* Lock a section for exclusive use. */
400 #define F_TLOCK _LK_NBLCK /* Test and lock a section for exclusive use */
401 #define F_TEST  _LK_RLCK  /* Test section for locks by other processes. */
402 #endif
403
404 #ifndef offsetof
405 # define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
406 #endif
407
408 void timeval2ntp(const struct timeval *tv, uint8_t *ntp);
409 void ntp2timeval(struct timeval *tv, const char *ntp);
410
411 #ifdef __cplusplus
412 }
413 #endif
414
415 #endif /* _FR_MISSING_H */