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