curl_setup_once.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. #ifndef HEADER_CURL_SETUP_ONCE_H
  2. #define HEADER_CURL_SETUP_ONCE_H
  3. /***************************************************************************
  4. * _ _ ____ _
  5. * Project ___| | | | _ \| |
  6. * / __| | | | |_) | |
  7. * | (__| |_| | _ <| |___
  8. * \___|\___/|_| \_\_____|
  9. *
  10. * Copyright (C) 1998 - 2021, 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 https://curl.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. ***************************************************************************/
  24. /*
  25. * Inclusion of common header files.
  26. */
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <stdarg.h>
  31. #include <ctype.h>
  32. #include <time.h>
  33. #ifdef HAVE_ERRNO_H
  34. #include <errno.h>
  35. #endif
  36. #ifdef HAVE_SYS_TYPES_H
  37. #include <sys/types.h>
  38. #endif
  39. #ifdef NEED_MALLOC_H
  40. #include <malloc.h>
  41. #endif
  42. #ifdef NEED_MEMORY_H
  43. #include <memory.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. #endif
  51. #ifdef WIN32
  52. #include <io.h>
  53. #include <fcntl.h>
  54. #endif
  55. #if defined(HAVE_STDBOOL_H) && defined(HAVE_BOOL_T)
  56. #include <stdbool.h>
  57. #endif
  58. #ifdef HAVE_UNISTD_H
  59. #include <unistd.h>
  60. #endif
  61. #ifdef __hpux
  62. # if !defined(_XOPEN_SOURCE_EXTENDED) || defined(_KERNEL)
  63. # ifdef _APP32_64BIT_OFF_T
  64. # define OLD_APP32_64BIT_OFF_T _APP32_64BIT_OFF_T
  65. # undef _APP32_64BIT_OFF_T
  66. # else
  67. # undef OLD_APP32_64BIT_OFF_T
  68. # endif
  69. # endif
  70. #endif
  71. #ifdef HAVE_SYS_SOCKET_H
  72. #include <sys/socket.h>
  73. #endif
  74. #ifdef __hpux
  75. # if !defined(_XOPEN_SOURCE_EXTENDED) || defined(_KERNEL)
  76. # ifdef OLD_APP32_64BIT_OFF_T
  77. # define _APP32_64BIT_OFF_T OLD_APP32_64BIT_OFF_T
  78. # undef OLD_APP32_64BIT_OFF_T
  79. # endif
  80. # endif
  81. #endif
  82. /*
  83. * Definition of timeval struct for platforms that don't have it.
  84. */
  85. #ifndef HAVE_STRUCT_TIMEVAL
  86. struct timeval {
  87. long tv_sec;
  88. long tv_usec;
  89. };
  90. #endif
  91. /*
  92. * If we have the MSG_NOSIGNAL define, make sure we use
  93. * it as the fourth argument of function send()
  94. */
  95. #ifdef HAVE_MSG_NOSIGNAL
  96. #define SEND_4TH_ARG MSG_NOSIGNAL
  97. #else
  98. #define SEND_4TH_ARG 0
  99. #endif
  100. #if defined(__minix)
  101. /* Minix doesn't support recv on TCP sockets */
  102. #define sread(x,y,z) (ssize_t)read((RECV_TYPE_ARG1)(x), \
  103. (RECV_TYPE_ARG2)(y), \
  104. (RECV_TYPE_ARG3)(z))
  105. #elif defined(HAVE_RECV)
  106. /*
  107. * The definitions for the return type and arguments types
  108. * of functions recv() and send() belong and come from the
  109. * configuration file. Do not define them in any other place.
  110. *
  111. * HAVE_RECV is defined if you have a function named recv()
  112. * which is used to read incoming data from sockets. If your
  113. * function has another name then don't define HAVE_RECV.
  114. *
  115. * If HAVE_RECV is defined then RECV_TYPE_ARG1, RECV_TYPE_ARG2,
  116. * RECV_TYPE_ARG3, RECV_TYPE_ARG4 and RECV_TYPE_RETV must also
  117. * be defined.
  118. *
  119. * HAVE_SEND is defined if you have a function named send()
  120. * which is used to write outgoing data on a connected socket.
  121. * If yours has another name then don't define HAVE_SEND.
  122. *
  123. * If HAVE_SEND is defined then SEND_TYPE_ARG1, SEND_QUAL_ARG2,
  124. * SEND_TYPE_ARG2, SEND_TYPE_ARG3, SEND_TYPE_ARG4 and
  125. * SEND_TYPE_RETV must also be defined.
  126. */
  127. #if !defined(RECV_TYPE_ARG1) || \
  128. !defined(RECV_TYPE_ARG2) || \
  129. !defined(RECV_TYPE_ARG3) || \
  130. !defined(RECV_TYPE_ARG4) || \
  131. !defined(RECV_TYPE_RETV)
  132. /* */
  133. Error Missing_definition_of_return_and_arguments_types_of_recv
  134. /* */
  135. #else
  136. #define sread(x,y,z) (ssize_t)recv((RECV_TYPE_ARG1)(x), \
  137. (RECV_TYPE_ARG2)(y), \
  138. (RECV_TYPE_ARG3)(z), \
  139. (RECV_TYPE_ARG4)(0))
  140. #endif
  141. #else /* HAVE_RECV */
  142. #ifndef sread
  143. /* */
  144. Error Missing_definition_of_macro_sread
  145. /* */
  146. #endif
  147. #endif /* HAVE_RECV */
  148. #if defined(__minix)
  149. /* Minix doesn't support send on TCP sockets */
  150. #define swrite(x,y,z) (ssize_t)write((SEND_TYPE_ARG1)(x), \
  151. (SEND_TYPE_ARG2)(y), \
  152. (SEND_TYPE_ARG3)(z))
  153. #elif defined(HAVE_SEND)
  154. #if !defined(SEND_TYPE_ARG1) || \
  155. !defined(SEND_QUAL_ARG2) || \
  156. !defined(SEND_TYPE_ARG2) || \
  157. !defined(SEND_TYPE_ARG3) || \
  158. !defined(SEND_TYPE_ARG4) || \
  159. !defined(SEND_TYPE_RETV)
  160. /* */
  161. Error Missing_definition_of_return_and_arguments_types_of_send
  162. /* */
  163. #else
  164. #define swrite(x,y,z) (ssize_t)send((SEND_TYPE_ARG1)(x), \
  165. (SEND_QUAL_ARG2 SEND_TYPE_ARG2)(y), \
  166. (SEND_TYPE_ARG3)(z), \
  167. (SEND_TYPE_ARG4)(SEND_4TH_ARG))
  168. #endif
  169. #else /* HAVE_SEND */
  170. #ifndef swrite
  171. /* */
  172. Error Missing_definition_of_macro_swrite
  173. /* */
  174. #endif
  175. #endif /* HAVE_SEND */
  176. #if 0
  177. #if defined(HAVE_RECVFROM)
  178. /*
  179. * Currently recvfrom is only used on udp sockets.
  180. */
  181. #if !defined(RECVFROM_TYPE_ARG1) || \
  182. !defined(RECVFROM_TYPE_ARG2) || \
  183. !defined(RECVFROM_TYPE_ARG3) || \
  184. !defined(RECVFROM_TYPE_ARG4) || \
  185. !defined(RECVFROM_TYPE_ARG5) || \
  186. !defined(RECVFROM_TYPE_ARG6) || \
  187. !defined(RECVFROM_TYPE_RETV)
  188. /* */
  189. Error Missing_definition_of_return_and_arguments_types_of_recvfrom
  190. /* */
  191. #else
  192. #define sreadfrom(s,b,bl,f,fl) (ssize_t)recvfrom((RECVFROM_TYPE_ARG1) (s), \
  193. (RECVFROM_TYPE_ARG2 *)(b), \
  194. (RECVFROM_TYPE_ARG3) (bl), \
  195. (RECVFROM_TYPE_ARG4) (0), \
  196. (RECVFROM_TYPE_ARG5 *)(f), \
  197. (RECVFROM_TYPE_ARG6 *)(fl))
  198. #endif
  199. #else /* HAVE_RECVFROM */
  200. #ifndef sreadfrom
  201. /* */
  202. Error Missing_definition_of_macro_sreadfrom
  203. /* */
  204. #endif
  205. #endif /* HAVE_RECVFROM */
  206. #ifdef RECVFROM_TYPE_ARG6_IS_VOID
  207. # define RECVFROM_ARG6_T int
  208. #else
  209. # define RECVFROM_ARG6_T RECVFROM_TYPE_ARG6
  210. #endif
  211. #endif /* if 0 */
  212. /*
  213. * Function-like macro definition used to close a socket.
  214. */
  215. #if defined(HAVE_CLOSESOCKET)
  216. # define sclose(x) closesocket((x))
  217. #elif defined(HAVE_CLOSESOCKET_CAMEL)
  218. # define sclose(x) CloseSocket((x))
  219. #elif defined(HAVE_CLOSE_S)
  220. # define sclose(x) close_s((x))
  221. #elif defined(USE_LWIPSOCK)
  222. # define sclose(x) lwip_close((x))
  223. #else
  224. # define sclose(x) close((x))
  225. #endif
  226. /*
  227. * Stack-independent version of fcntl() on sockets:
  228. */
  229. #if defined(USE_LWIPSOCK)
  230. # define sfcntl lwip_fcntl
  231. #else
  232. # define sfcntl fcntl
  233. #endif
  234. #define TOLOWER(x) (tolower((int) ((unsigned char)x)))
  235. /*
  236. * 'bool' stuff compatible with HP-UX headers.
  237. */
  238. #if defined(__hpux) && !defined(HAVE_BOOL_T)
  239. typedef int bool;
  240. # define false 0
  241. # define true 1
  242. # define HAVE_BOOL_T
  243. #endif
  244. /*
  245. * 'bool' exists on platforms with <stdbool.h>, i.e. C99 platforms.
  246. * On non-C99 platforms there's no bool, so define an enum for that.
  247. * On C99 platforms 'false' and 'true' also exist. Enum uses a
  248. * global namespace though, so use bool_false and bool_true.
  249. */
  250. #ifndef HAVE_BOOL_T
  251. typedef enum {
  252. bool_false = 0,
  253. bool_true = 1
  254. } bool;
  255. /*
  256. * Use a define to let 'true' and 'false' use those enums. There
  257. * are currently no use of true and false in libcurl proper, but
  258. * there are some in the examples. This will cater for any later
  259. * code happening to use true and false.
  260. */
  261. # define false bool_false
  262. # define true bool_true
  263. # define HAVE_BOOL_T
  264. #endif
  265. /*
  266. * Redefine TRUE and FALSE too, to catch current use. With this
  267. * change, 'bool found = 1' will give a warning on MIPSPro, but
  268. * 'bool found = TRUE' will not. Change tested on IRIX/MIPSPro,
  269. * AIX 5.1/Xlc, Tru64 5.1/cc, w/make test too.
  270. */
  271. #ifndef TRUE
  272. #define TRUE true
  273. #endif
  274. #ifndef FALSE
  275. #define FALSE false
  276. #endif
  277. #include "curl_ctype.h"
  278. /*
  279. * Macro used to include code only in debug builds.
  280. */
  281. #ifdef DEBUGBUILD
  282. #define DEBUGF(x) x
  283. #else
  284. #define DEBUGF(x) do { } while(0)
  285. #endif
  286. /*
  287. * Macro used to include assertion code only in debug builds.
  288. */
  289. #undef DEBUGASSERT
  290. #if defined(DEBUGBUILD) && defined(HAVE_ASSERT_H)
  291. #define DEBUGASSERT(x) assert(x)
  292. #else
  293. #define DEBUGASSERT(x) do { } while(0)
  294. #endif
  295. /*
  296. * Macro SOCKERRNO / SET_SOCKERRNO() returns / sets the *socket-related* errno
  297. * (or equivalent) on this platform to hide platform details to code using it.
  298. */
  299. #ifdef USE_WINSOCK
  300. #define SOCKERRNO ((int)WSAGetLastError())
  301. #define SET_SOCKERRNO(x) (WSASetLastError((int)(x)))
  302. #else
  303. #define SOCKERRNO (errno)
  304. #define SET_SOCKERRNO(x) (errno = (x))
  305. #endif
  306. /*
  307. * Portable error number symbolic names defined to Winsock error codes.
  308. */
  309. #ifdef USE_WINSOCK
  310. #undef EBADF /* override definition in errno.h */
  311. #define EBADF WSAEBADF
  312. #undef EINTR /* override definition in errno.h */
  313. #define EINTR WSAEINTR
  314. #undef EINVAL /* override definition in errno.h */
  315. #define EINVAL WSAEINVAL
  316. #undef EWOULDBLOCK /* override definition in errno.h */
  317. #define EWOULDBLOCK WSAEWOULDBLOCK
  318. #undef EINPROGRESS /* override definition in errno.h */
  319. #define EINPROGRESS WSAEINPROGRESS
  320. #undef EALREADY /* override definition in errno.h */
  321. #define EALREADY WSAEALREADY
  322. #undef ENOTSOCK /* override definition in errno.h */
  323. #define ENOTSOCK WSAENOTSOCK
  324. #undef EDESTADDRREQ /* override definition in errno.h */
  325. #define EDESTADDRREQ WSAEDESTADDRREQ
  326. #undef EMSGSIZE /* override definition in errno.h */
  327. #define EMSGSIZE WSAEMSGSIZE
  328. #undef EPROTOTYPE /* override definition in errno.h */
  329. #define EPROTOTYPE WSAEPROTOTYPE
  330. #undef ENOPROTOOPT /* override definition in errno.h */
  331. #define ENOPROTOOPT WSAENOPROTOOPT
  332. #undef EPROTONOSUPPORT /* override definition in errno.h */
  333. #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
  334. #define ESOCKTNOSUPPORT WSAESOCKTNOSUPPORT
  335. #undef EOPNOTSUPP /* override definition in errno.h */
  336. #define EOPNOTSUPP WSAEOPNOTSUPP
  337. #define EPFNOSUPPORT WSAEPFNOSUPPORT
  338. #undef EAFNOSUPPORT /* override definition in errno.h */
  339. #define EAFNOSUPPORT WSAEAFNOSUPPORT
  340. #undef EADDRINUSE /* override definition in errno.h */
  341. #define EADDRINUSE WSAEADDRINUSE
  342. #undef EADDRNOTAVAIL /* override definition in errno.h */
  343. #define EADDRNOTAVAIL WSAEADDRNOTAVAIL
  344. #undef ENETDOWN /* override definition in errno.h */
  345. #define ENETDOWN WSAENETDOWN
  346. #undef ENETUNREACH /* override definition in errno.h */
  347. #define ENETUNREACH WSAENETUNREACH
  348. #undef ENETRESET /* override definition in errno.h */
  349. #define ENETRESET WSAENETRESET
  350. #undef ECONNABORTED /* override definition in errno.h */
  351. #define ECONNABORTED WSAECONNABORTED
  352. #undef ECONNRESET /* override definition in errno.h */
  353. #define ECONNRESET WSAECONNRESET
  354. #undef ENOBUFS /* override definition in errno.h */
  355. #define ENOBUFS WSAENOBUFS
  356. #undef EISCONN /* override definition in errno.h */
  357. #define EISCONN WSAEISCONN
  358. #undef ENOTCONN /* override definition in errno.h */
  359. #define ENOTCONN WSAENOTCONN
  360. #define ESHUTDOWN WSAESHUTDOWN
  361. #define ETOOMANYREFS WSAETOOMANYREFS
  362. #undef ETIMEDOUT /* override definition in errno.h */
  363. #define ETIMEDOUT WSAETIMEDOUT
  364. #undef ECONNREFUSED /* override definition in errno.h */
  365. #define ECONNREFUSED WSAECONNREFUSED
  366. #undef ELOOP /* override definition in errno.h */
  367. #define ELOOP WSAELOOP
  368. #ifndef ENAMETOOLONG /* possible previous definition in errno.h */
  369. #define ENAMETOOLONG WSAENAMETOOLONG
  370. #endif
  371. #define EHOSTDOWN WSAEHOSTDOWN
  372. #undef EHOSTUNREACH /* override definition in errno.h */
  373. #define EHOSTUNREACH WSAEHOSTUNREACH
  374. #ifndef ENOTEMPTY /* possible previous definition in errno.h */
  375. #define ENOTEMPTY WSAENOTEMPTY
  376. #endif
  377. #define EPROCLIM WSAEPROCLIM
  378. #define EUSERS WSAEUSERS
  379. #define EDQUOT WSAEDQUOT
  380. #define ESTALE WSAESTALE
  381. #define EREMOTE WSAEREMOTE
  382. #endif
  383. /*
  384. * Macro argv_item_t hides platform details to code using it.
  385. */
  386. #ifdef __VMS
  387. #define argv_item_t __char_ptr32
  388. #elif defined(_UNICODE)
  389. #define argv_item_t wchar_t *
  390. #else
  391. #define argv_item_t char *
  392. #endif
  393. /*
  394. * We use this ZERO_NULL to avoid picky compiler warnings,
  395. * when assigning a NULL pointer to a function pointer var.
  396. */
  397. #define ZERO_NULL 0
  398. #endif /* HEADER_CURL_SETUP_ONCE_H */