curl_setup_once.h 11 KB

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