6 /* Copyright (C) 2004 - 2008 by Daniel Stenberg et al
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.
20 /********************************************************************
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. *
27 ********************************************************************/
31 * Inclusion of common header files.
41 #ifdef HAVE_SYS_TYPES_H
42 #include <sys/types.h>
45 #ifdef HAVE_SYS_STAT_H
49 #ifdef HAVE_SYS_TIME_H
51 #ifdef TIME_WITH_SYS_TIME
71 * Definition of timeval struct for platforms that don't have it.
74 #ifndef HAVE_STRUCT_TIMEVAL
83 * If we have the MSG_NOSIGNAL define, make sure we use
84 * it as the fourth argument of function send()
87 #ifdef HAVE_MSG_NOSIGNAL
88 #define SEND_4TH_ARG MSG_NOSIGNAL
90 #define SEND_4TH_ARG 0
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.
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
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), \
117 #elif defined(HAVE_RECV)
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.
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.
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
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.
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.
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)
146 Error Missing_definition_of_return_and_arguments_types_of_recv
149 #define sread(x,y,z) (ssize_t)recv((RECV_TYPE_ARG1)(x), \
150 (RECV_TYPE_ARG2)(y), \
151 (RECV_TYPE_ARG3)(z), \
154 #else /* HAVE_RECV */
157 Error Missing_definition_of_macro_sread
160 #endif /* HAVE_RECV */
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), \
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)
177 Error Missing_definition_of_return_and_arguments_types_of_send
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))
185 #else /* HAVE_SEND */
188 Error Missing_definition_of_macro_swrite
191 #endif /* HAVE_SEND */
194 #if defined(HAVE_RECVFROM)
196 * Currently recvfrom is only used on udp sockets.
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)
206 Error Missing_definition_of_return_and_arguments_types_of_recvfrom
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))
216 #else /* HAVE_RECVFROM */
219 Error Missing_definition_of_macro_sreadfrom
222 #endif /* HAVE_RECVFROM */
225 #ifdef RECVFROM_TYPE_ARG6_IS_VOID
226 # define RECVFROM_ARG6_T int
228 # define RECVFROM_ARG6_T RECVFROM_TYPE_ARG6
233 * Uppercase macro versions of ANSI/ISO is*() functions/macros which
234 * avoid negative number inputs with argument byte codes > 127.
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)))
247 #define ISBLANK(x) (int)((((unsigned char)x) == ' ') || \
248 (((unsigned char)x) == '\t'))
252 * Typedef to 'unsigned char' if bool is not an available 'typedefed' type.
256 typedef unsigned char bool;
262 * Default definition of uppercase TRUE and FALSE.
274 * Typedef to 'int' if sig_atomic_t is not an available 'typedefed' type.
277 #ifndef HAVE_SIG_ATOMIC_T
278 typedef int sig_atomic_t;
279 #define HAVE_SIG_ATOMIC_T
284 * Convenience SIG_ATOMIC_T definition
287 #ifdef HAVE_SIG_ATOMIC_T_VOLATILE
288 #define SIG_ATOMIC_T static sig_atomic_t
290 #define SIG_ATOMIC_T static volatile sig_atomic_t
295 * Default return type for signal handlers.
299 #define RETSIGTYPE void
304 * Macro used to include code only in debug builds.
310 #define DEBUGF(x) do { } while (0)
315 * Macro used to include assertion code only in debug builds.
318 #if defined(CURLDEBUG) && defined(HAVE_ASSERT_H)
319 #define DEBUGASSERT(x) assert(x)
321 #define DEBUGASSERT(x) do { } while (0)
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.
331 #define SOCKERRNO ((int)WSAGetLastError())
332 #define SET_SOCKERRNO(x) (WSASetLastError((int)(x)))
334 #define SOCKERRNO (errno)
335 #define SET_SOCKERRNO(x) (errno = (x))
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.
345 #define ERRNO ((int)GetLastError())
346 #define SET_ERRNO(x) (SetLastError((DWORD)(x)))
348 #define ERRNO (errno)
349 #define SET_ERRNO(x) (errno = (x))
354 * Portable error number symbolic names defined to Winsock error codes.
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
395 #define EHOSTDOWN WSAEHOSTDOWN
396 #define EHOSTUNREACH WSAEHOSTUNREACH
397 #ifndef ENOTEMPTY /* possible previous definition in errno.h */
398 #define ENOTEMPTY WSAENOTEMPTY
400 #define EPROCLIM WSAEPROCLIM
401 #define EUSERS WSAEUSERS
402 #define EDQUOT WSAEDQUOT
403 #define ESTALE WSAESTALE
404 #define EREMOTE WSAEREMOTE
409 * Actually use __32_getpwuid() on 64-bit VMS builds for getpwuid()
412 #if defined(VMS) && \
413 defined(__INITIAL_POINTER_SIZE) && (__INITIAL_POINTER_SIZE == 64)
414 #define getpwuid __32_getpwuid
419 * Macro argv_item_t hides platform details to code using it.
423 #define argv_item_t __char_ptr32
425 #define argv_item_t char *
430 * We use this ZERO_NULL to avoid picky compiler warnings,
431 * when assigning a NULL pointer to a function pointer var.
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
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.
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.
449 Note that some socket calls are allowed to have a NULL pointer for
453 inline static int Curl_hp_getsockname(int s, struct sockaddr *name,
459 rc = getsockname(s, name, &len);
463 rc = getsockname(s, name, 0);
467 inline static int Curl_hp_getsockopt(int s, int level, int optname,
468 void *optval, socklen_t *optlen)
473 rc = getsockopt(s, level, optname, optval, &len);
477 rc = getsockopt(s, level, optname, optval, 0);
481 inline static int Curl_hp_accept(int sockfd, struct sockaddr *addr,
487 rc = accept(sockfd, addr, &len);
491 rc = accept(sockfd, addr, 0);
496 inline static ssize_t Curl_hp_recvfrom(int s, void *buf, size_t len, int flags,
497 struct sockaddr *from,
502 int fromlen32 = *fromlen;
503 rc = recvfrom(s, buf, len, flags, from, &fromlen32);
504 *fromlen = fromlen32;
507 rc = recvfrom(s, buf, len, flags, from, 0);
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))
517 #endif /* HPUX work-around */
520 #endif /* __SETUP_ONCE_H */