2
0

curl_setup_once.h 11 KB

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