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