setup_once.h 14 KB

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