Support Watt-32 under Win32.
[platform/upstream/c-ares.git] / setup_once.h
1 #ifndef __SETUP_ONCE_H
2 #define __SETUP_ONCE_H
3
4 /* $Id$ */
5
6 /* Copyright (C) 2004 - 2008 by Daniel Stenberg et al
7  *
8  * Permission to use, copy, modify, and distribute this software and its
9  * documentation for any purpose and without fee is hereby granted, provided
10  * that the above copyright notice appear in all copies and that both that
11  * copyright notice and this permission notice appear in supporting
12  * documentation, and that the name of M.I.T. not be used in advertising or
13  * publicity pertaining to distribution of the software without specific,
14  * written prior permission.  M.I.T. makes no representations about the
15  * suitability of this software for any purpose.  It is provided "as is"
16  * without express or implied warranty.
17  */
18
19
20 /********************************************************************
21  *                              NOTICE                              *
22  *                             ========                             *
23  *                                                                  *
24  *  Content of header files lib/setup_once.h and ares/setup_once.h  *
25  *  must be kept in sync. Modify the other one if you change this.  *
26  *                                                                  *
27  ********************************************************************/
28
29
30 /*
31  * Inclusion of common header files.
32  */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <stdarg.h>
38 #include <ctype.h>
39 #include <errno.h>
40
41 #ifdef HAVE_SYS_TYPES_H
42 #include <sys/types.h>
43 #endif
44
45 #ifdef HAVE_SYS_STAT_H
46 #include <sys/stat.h>
47 #endif
48
49 #ifdef HAVE_SYS_TIME_H
50 #include <sys/time.h>
51 #ifdef TIME_WITH_SYS_TIME
52 #include <time.h>
53 #endif
54 #else
55 #ifdef HAVE_TIME_H
56 #include <time.h>
57 #endif
58 #endif
59
60 #ifdef WIN32
61 #include <io.h>
62 #include <fcntl.h>
63 #endif
64
65 #ifdef HAVE_STDBOOL_H
66 #include <stdbool.h>
67 #endif
68
69
70 /*
71  * Definition of timeval struct for platforms that don't have it.
72  */
73
74 #ifndef HAVE_STRUCT_TIMEVAL
75 struct timeval {
76  long tv_sec;
77  long tv_usec;
78 };
79 #endif
80
81
82 /*
83  * If we have the MSG_NOSIGNAL define, make sure we use
84  * it as the fourth argument of function send()
85  */
86
87 #ifdef HAVE_MSG_NOSIGNAL
88 #define SEND_4TH_ARG MSG_NOSIGNAL
89 #else
90 #define SEND_4TH_ARG 0
91 #endif
92
93
94 /*
95  * Windows build targets have socklen_t definition in
96  * ws2tcpip.h but some versions of ws2tcpip.h do not
97  * have the definition. It seems that when the socklen_t
98  * definition is missing from ws2tcpip.h the definition
99  * for INET_ADDRSTRLEN is also missing, and that when one
100  * definition is present the other one also is available.
101  */
102
103 #if defined(WIN32) && !defined(HAVE_CONFIG_H)
104 #  if ( defined(_MSC_VER) && !defined(INET_ADDRSTRLEN) ) || \
105       (!defined(_MSC_VER) && !defined(HAVE_WS2TCPIP_H) )
106 #    define socklen_t int
107 #  endif
108 #endif
109
110
111 #if defined(__minix)
112 /* Minix doesn't support recv on TCP sockets */
113 #define sread(x,y,z) (ssize_t)read((RECV_TYPE_ARG1)(x), \
114                                    (RECV_TYPE_ARG2)(y), \
115                                    (RECV_TYPE_ARG3)(z))
116
117 #elif defined(HAVE_RECV)
118 /*
119  * The definitions for the return type and arguments types
120  * of functions recv() and send() belong and come from the
121  * configuration file. Do not define them in any other place.
122  *
123  * HAVE_RECV is defined if you have a function named recv()
124  * which is used to read incoming data from sockets. If your
125  * function has another name then don't define HAVE_RECV.
126  *
127  * If HAVE_RECV is defined then RECV_TYPE_ARG1, RECV_TYPE_ARG2,
128  * RECV_TYPE_ARG3, RECV_TYPE_ARG4 and RECV_TYPE_RETV must also
129  * be defined.
130  *
131  * HAVE_SEND is defined if you have a function named send()
132  * which is used to write outgoing data on a connected socket.
133  * If yours has another name then don't define HAVE_SEND.
134  *
135  * If HAVE_SEND is defined then SEND_TYPE_ARG1, SEND_QUAL_ARG2,
136  * SEND_TYPE_ARG2, SEND_TYPE_ARG3, SEND_TYPE_ARG4 and
137  * SEND_TYPE_RETV must also be defined.
138  */
139
140 #if !defined(RECV_TYPE_ARG1) || \
141     !defined(RECV_TYPE_ARG2) || \
142     !defined(RECV_TYPE_ARG3) || \
143     !defined(RECV_TYPE_ARG4) || \
144     !defined(RECV_TYPE_RETV)
145   /* */
146   Error Missing_definition_of_return_and_arguments_types_of_recv
147   /* */
148 #else
149 #define sread(x,y,z) (ssize_t)recv((RECV_TYPE_ARG1)(x), \
150                                    (RECV_TYPE_ARG2)(y), \
151                                    (RECV_TYPE_ARG3)(z), \
152                                    (RECV_TYPE_ARG4)(0))
153 #endif
154 #else /* HAVE_RECV */
155 #ifndef sread
156   /* */
157   Error Missing_definition_of_macro_sread
158   /* */
159 #endif
160 #endif /* HAVE_RECV */
161
162
163 #if defined(__minix)
164 /* Minix doesn't support send on TCP sockets */
165 #define swrite(x,y,z) (ssize_t)write((SEND_TYPE_ARG1)(x), \
166                                     (SEND_TYPE_ARG2)(y), \
167                                     (SEND_TYPE_ARG3)(z))
168
169 #elif defined(HAVE_SEND)
170 #if !defined(SEND_TYPE_ARG1) || \
171     !defined(SEND_QUAL_ARG2) || \
172     !defined(SEND_TYPE_ARG2) || \
173     !defined(SEND_TYPE_ARG3) || \
174     !defined(SEND_TYPE_ARG4) || \
175     !defined(SEND_TYPE_RETV)
176   /* */
177   Error Missing_definition_of_return_and_arguments_types_of_send
178   /* */
179 #else
180 #define swrite(x,y,z) (ssize_t)send((SEND_TYPE_ARG1)(x), \
181                                     (SEND_TYPE_ARG2)(y), \
182                                     (SEND_TYPE_ARG3)(z), \
183                                     (SEND_TYPE_ARG4)(SEND_4TH_ARG))
184 #endif
185 #else /* HAVE_SEND */
186 #ifndef swrite
187   /* */
188   Error Missing_definition_of_macro_swrite
189   /* */
190 #endif
191 #endif /* HAVE_SEND */
192
193
194 #if defined(HAVE_RECVFROM)
195 /*
196  * Currently recvfrom is only used on udp sockets.
197  */
198 #if !defined(RECVFROM_TYPE_ARG1) || \
199     !defined(RECVFROM_TYPE_ARG2) || \
200     !defined(RECVFROM_TYPE_ARG3) || \
201     !defined(RECVFROM_TYPE_ARG4) || \
202     !defined(RECVFROM_TYPE_ARG5) || \
203     !defined(RECVFROM_TYPE_ARG6) || \
204     !defined(RECVFROM_TYPE_RETV)
205   /* */
206   Error Missing_definition_of_return_and_arguments_types_of_recvfrom
207   /* */
208 #else
209 #define sreadfrom(s,b,bl,f,fl) (ssize_t)recvfrom((RECVFROM_TYPE_ARG1)  (s),  \
210                                                  (RECVFROM_TYPE_ARG2 *)(b),  \
211                                                  (RECVFROM_TYPE_ARG3)  (bl), \
212                                                  (RECVFROM_TYPE_ARG4)  (0),  \
213                                                  (RECVFROM_TYPE_ARG5 *)(f),  \
214                                                  (RECVFROM_TYPE_ARG6 *)(fl))
215 #endif
216 #else /* HAVE_RECVFROM */
217 #ifndef sreadfrom
218   /* */
219   Error Missing_definition_of_macro_sreadfrom
220   /* */
221 #endif
222 #endif /* HAVE_RECVFROM */
223
224
225 #ifdef RECVFROM_TYPE_ARG6_IS_VOID
226 #  define RECVFROM_ARG6_T int
227 #else
228 #  define RECVFROM_ARG6_T RECVFROM_TYPE_ARG6
229 #endif
230
231
232 /*
233  * Uppercase macro versions of ANSI/ISO is*() functions/macros which
234  * avoid negative number inputs with argument byte codes > 127.
235  */
236
237 #define ISSPACE(x)  (isspace((int)  ((unsigned char)x)))
238 #define ISDIGIT(x)  (isdigit((int)  ((unsigned char)x)))
239 #define ISALNUM(x)  (isalnum((int)  ((unsigned char)x)))
240 #define ISXDIGIT(x) (isxdigit((int) ((unsigned char)x)))
241 #define ISGRAPH(x)  (isgraph((int)  ((unsigned char)x)))
242 #define ISALPHA(x)  (isalpha((int)  ((unsigned char)x)))
243 #define ISPRINT(x)  (isprint((int)  ((unsigned char)x)))
244 #define ISUPPER(x)  (isupper((int)  ((unsigned char)x)))
245 #define ISLOWER(x)  (islower((int)  ((unsigned char)x)))
246
247 #define ISBLANK(x)  (int)((((unsigned char)x) == ' ') || \
248                           (((unsigned char)x) == '\t'))
249
250
251 /*
252  * Typedef to 'unsigned char' if bool is not an available 'typedefed' type.
253  */
254
255 #ifndef HAVE_BOOL_T
256 typedef unsigned char bool;
257 #define HAVE_BOOL_T
258 #endif
259
260
261 /*
262  * Default definition of uppercase TRUE and FALSE.
263  */
264
265 #ifndef TRUE
266 #define TRUE 1
267 #endif
268 #ifndef FALSE
269 #define FALSE 0
270 #endif
271
272
273 /*
274  * Typedef to 'int' if sig_atomic_t is not an available 'typedefed' type.
275  */
276
277 #ifndef HAVE_SIG_ATOMIC_T
278 typedef int sig_atomic_t;
279 #define HAVE_SIG_ATOMIC_T
280 #endif
281
282
283 /*
284  * Convenience SIG_ATOMIC_T definition
285  */
286
287 #ifdef HAVE_SIG_ATOMIC_T_VOLATILE
288 #define SIG_ATOMIC_T static sig_atomic_t
289 #else
290 #define SIG_ATOMIC_T static volatile sig_atomic_t
291 #endif
292
293
294 /*
295  * Default return type for signal handlers.
296  */
297
298 #ifndef RETSIGTYPE
299 #define RETSIGTYPE void
300 #endif
301
302
303 /*
304  * Macro used to include code only in debug builds.
305  */
306
307 #ifdef CURLDEBUG
308 #define DEBUGF(x) x
309 #else
310 #define DEBUGF(x) do { } while (0)
311 #endif
312
313
314 /*
315  * Macro used to include assertion code only in debug builds.
316  */
317
318 #if defined(CURLDEBUG) && defined(HAVE_ASSERT_H)
319 #define DEBUGASSERT(x) assert(x)
320 #else
321 #define DEBUGASSERT(x) do { } while (0)
322 #endif
323
324
325 /*
326  * Macro SOCKERRNO / SET_SOCKERRNO() returns / sets the *socket-related* errno
327  * (or equivalent) on this platform to hide platform details to code using it.
328  */
329
330 #ifdef USE_WINSOCK
331 #define SOCKERRNO         ((int)WSAGetLastError())
332 #define SET_SOCKERRNO(x)  (WSASetLastError((int)(x)))
333 #else
334 #define SOCKERRNO         (errno)
335 #define SET_SOCKERRNO(x)  (errno = (x))
336 #endif
337
338
339 /*
340  * Macro ERRNO / SET_ERRNO() returns / sets the NOT *socket-related* errno
341  * (or equivalent) on this platform to hide platform details to code using it.
342  */
343
344 #ifdef WIN32
345 #define ERRNO         ((int)GetLastError())
346 #define SET_ERRNO(x)  (SetLastError((DWORD)(x)))
347 #else
348 #define ERRNO         (errno)
349 #define SET_ERRNO(x)  (errno = (x))
350 #endif
351
352
353 /*
354  * Portable error number symbolic names defined to Winsock error codes.
355  */
356
357 #ifdef USE_WINSOCK
358 #undef  EBADF            /* override definition in errno.h */
359 #define EBADF            WSAEBADF
360 #undef  EINTR            /* override definition in errno.h */
361 #define EINTR            WSAEINTR
362 #undef  EINVAL           /* override definition in errno.h */
363 #define EINVAL           WSAEINVAL
364 #define EWOULDBLOCK      WSAEWOULDBLOCK
365 #define EINPROGRESS      WSAEINPROGRESS
366 #define EALREADY         WSAEALREADY
367 #define ENOTSOCK         WSAENOTSOCK
368 #define EDESTADDRREQ     WSAEDESTADDRREQ
369 #define EMSGSIZE         WSAEMSGSIZE
370 #define EPROTOTYPE       WSAEPROTOTYPE
371 #define ENOPROTOOPT      WSAENOPROTOOPT
372 #define EPROTONOSUPPORT  WSAEPROTONOSUPPORT
373 #define ESOCKTNOSUPPORT  WSAESOCKTNOSUPPORT
374 #define EOPNOTSUPP       WSAEOPNOTSUPP
375 #define EPFNOSUPPORT     WSAEPFNOSUPPORT
376 #define EAFNOSUPPORT     WSAEAFNOSUPPORT
377 #define EADDRINUSE       WSAEADDRINUSE
378 #define EADDRNOTAVAIL    WSAEADDRNOTAVAIL
379 #define ENETDOWN         WSAENETDOWN
380 #define ENETUNREACH      WSAENETUNREACH
381 #define ENETRESET        WSAENETRESET
382 #define ECONNABORTED     WSAECONNABORTED
383 #define ECONNRESET       WSAECONNRESET
384 #define ENOBUFS          WSAENOBUFS
385 #define EISCONN          WSAEISCONN
386 #define ENOTCONN         WSAENOTCONN
387 #define ESHUTDOWN        WSAESHUTDOWN
388 #define ETOOMANYREFS     WSAETOOMANYREFS
389 #define ETIMEDOUT        WSAETIMEDOUT
390 #define ECONNREFUSED     WSAECONNREFUSED
391 #define ELOOP            WSAELOOP
392 #ifndef ENAMETOOLONG     /* possible previous definition in errno.h */
393 #define ENAMETOOLONG     WSAENAMETOOLONG
394 #endif
395 #define EHOSTDOWN        WSAEHOSTDOWN
396 #define EHOSTUNREACH     WSAEHOSTUNREACH
397 #ifndef ENOTEMPTY        /* possible previous definition in errno.h */
398 #define ENOTEMPTY        WSAENOTEMPTY
399 #endif
400 #define EPROCLIM         WSAEPROCLIM
401 #define EUSERS           WSAEUSERS
402 #define EDQUOT           WSAEDQUOT
403 #define ESTALE           WSAESTALE
404 #define EREMOTE          WSAEREMOTE
405 #endif
406
407
408 /*
409  *  Actually use __32_getpwuid() on 64-bit VMS builds for getpwuid()
410  */
411
412 #if defined(VMS) && \
413     defined(__INITIAL_POINTER_SIZE) && (__INITIAL_POINTER_SIZE == 64)
414 #define getpwuid __32_getpwuid
415 #endif
416
417
418 /*
419  * Macro argv_item_t hides platform details to code using it.
420  */
421
422 #ifdef VMS
423 #define argv_item_t  __char_ptr32
424 #else
425 #define argv_item_t  char *
426 #endif
427
428
429 /*
430  * We use this ZERO_NULL to avoid picky compiler warnings,
431  * when assigning a NULL pointer to a function pointer var.
432  */
433
434 #define ZERO_NULL 0
435
436
437 #if defined (__LP64__) && defined(__hpux) && !defined(_XOPEN_SOURCE_EXTENDED)
438 #include <sys/socket.h>
439 /* HP-UX has this oddity where it features a few functions that don't work
440    with socklen_t so we need to convert to ints
441
442    This is due to socklen_t being a 64bit int under 64bit ABI, but the
443    pre-xopen (default) interfaces require an int, which is 32bits.
444
445    Therefore, Anytime socklen_t is passed by pointer, the libc function
446    truncates the 64bit socklen_t value by treating it as a 32bit value.
447
448
449    Note that some socket calls are allowed to have a NULL pointer for
450    the socklen arg.
451 */
452
453 inline static int Curl_hp_getsockname(int s, struct sockaddr *name,
454                                       socklen_t *namelen)
455 {
456   int rc;
457   if(namelen) {
458      int len = *namelen;
459      rc = getsockname(s, name, &len);
460      *namelen = len;
461    }
462   else
463      rc = getsockname(s, name, 0);
464   return rc;
465 }
466
467 inline static int Curl_hp_getsockopt(int  s, int level, int optname,
468                                      void *optval, socklen_t *optlen)
469 {
470   int rc;
471   if(optlen) {
472     int len = *optlen;
473     rc = getsockopt(s, level, optname, optval, &len);
474     *optlen = len;
475   }
476   else
477     rc = getsockopt(s, level, optname, optval, 0);
478   return rc;
479 }
480
481 inline static int Curl_hp_accept(int sockfd, struct sockaddr *addr,
482                                  socklen_t *addrlen)
483 {
484   int rc;
485   if(addrlen) {
486      int len = *addrlen;
487      rc = accept(sockfd, addr, &len);
488      *addrlen = len;
489   }
490   else
491      rc = accept(sockfd, addr, 0);
492   return rc;
493 }
494
495
496 inline static ssize_t Curl_hp_recvfrom(int s, void *buf, size_t len, int flags,
497                                        struct sockaddr *from,
498                                        socklen_t *fromlen)
499 {
500   ssize_t rc;
501   if(fromlen) {
502     int fromlen32 = *fromlen;
503     rc = recvfrom(s, buf, len, flags, from, &fromlen32);
504     *fromlen = fromlen32;
505   }
506   else {
507     rc = recvfrom(s, buf, len, flags, from, 0);
508   }
509   return rc;
510 }
511
512 #define getsockname(a,b,c) Curl_hp_getsockname((a),(b),(c))
513 #define getsockopt(a,b,c,d,e) Curl_hp_getsockopt((a),(b),(c),(d),(e))
514 #define accept(a,b,c) Curl_hp_accept((a),(b),(c))
515 #define recvfrom(a,b,c,d,e,f) Curl_hp_recvfrom((a),(b),(c),(d),(e),(f))
516
517 #endif /* HPUX work-around */
518
519
520 #endif /* __SETUP_ONCE_H */
521