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