curl_setup_once.h 11 KB

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