curl_setup_once.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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, <[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. * 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. #ifndef UNDER_CE
  35. #include <errno.h>
  36. #endif
  37. #ifdef HAVE_SYS_TYPES_H
  38. #include <sys/types.h>
  39. #endif
  40. #include <sys/stat.h>
  41. #if !defined(_WIN32) || defined(__MINGW32__)
  42. #include <sys/time.h>
  43. #endif
  44. #ifdef HAVE_IO_H
  45. #include <io.h>
  46. #endif
  47. #ifdef HAVE_FCNTL_H
  48. #include <fcntl.h>
  49. #endif
  50. #if defined(HAVE_STDBOOL_H) && defined(HAVE_BOOL_T)
  51. #include <stdbool.h>
  52. #endif
  53. #ifdef HAVE_UNISTD_H
  54. #include <unistd.h>
  55. #endif
  56. /* Macro to strip 'const' without triggering a compiler warning.
  57. Use it for APIs that do not or cannot support the const qualifier. */
  58. #ifdef HAVE_STDINT_H
  59. # define CURL_UNCONST(p) ((void *)(uintptr_t)(const void *)(p))
  60. #elif defined(_WIN32) /* for VS2008 */
  61. # define CURL_UNCONST(p) ((void *)(ULONG_PTR)(const void *)(p))
  62. #else
  63. # define CURL_UNCONST(p) ((void *)(p)) /* Fall back to simple cast */
  64. #endif
  65. #ifdef USE_SCHANNEL
  66. /* Must set this before <schannel.h> is included directly or indirectly by
  67. another Windows header. */
  68. # define SCHANNEL_USE_BLACKLISTS 1
  69. #endif
  70. #ifdef __hpux
  71. # if !defined(_XOPEN_SOURCE_EXTENDED) || defined(_KERNEL)
  72. # ifdef _APP32_64BIT_OFF_T
  73. # define OLD_APP32_64BIT_OFF_T _APP32_64BIT_OFF_T
  74. # undef _APP32_64BIT_OFF_T
  75. # else
  76. # undef OLD_APP32_64BIT_OFF_T
  77. # endif
  78. # endif
  79. #endif
  80. #ifndef _WIN32
  81. #include <sys/socket.h>
  82. #endif
  83. #include "functypes.h"
  84. #ifdef __hpux
  85. # if !defined(_XOPEN_SOURCE_EXTENDED) || defined(_KERNEL)
  86. # ifdef OLD_APP32_64BIT_OFF_T
  87. # define _APP32_64BIT_OFF_T OLD_APP32_64BIT_OFF_T
  88. # undef OLD_APP32_64BIT_OFF_T
  89. # endif
  90. # endif
  91. #endif
  92. /*
  93. * Definition of timeval struct for platforms that do not have it.
  94. */
  95. #ifndef HAVE_STRUCT_TIMEVAL
  96. struct timeval {
  97. long tv_sec;
  98. long tv_usec;
  99. };
  100. #endif
  101. /*
  102. * If we have the MSG_NOSIGNAL define, make sure we use
  103. * it as the fourth argument of function send()
  104. */
  105. #ifdef HAVE_MSG_NOSIGNAL
  106. #define SEND_4TH_ARG MSG_NOSIGNAL
  107. #else
  108. #define SEND_4TH_ARG 0
  109. #endif
  110. #ifdef __minix
  111. /* Minix does not support recv on TCP sockets */
  112. #define sread(x,y,z) (ssize_t)read((RECV_TYPE_ARG1)(x), \
  113. (RECV_TYPE_ARG2)(y), \
  114. (RECV_TYPE_ARG3)(z))
  115. #elif defined(HAVE_RECV)
  116. /*
  117. * The definitions for the return type and arguments types
  118. * of functions recv() and send() belong and come from the
  119. * configuration file. Do not define them in any other place.
  120. *
  121. * HAVE_RECV is defined if you have a function named recv()
  122. * which is used to read incoming data from sockets. If your
  123. * function has another name then do not define HAVE_RECV.
  124. *
  125. * If HAVE_RECV is defined then RECV_TYPE_ARG1, RECV_TYPE_ARG2,
  126. * RECV_TYPE_ARG3, RECV_TYPE_ARG4 and RECV_TYPE_RETV must also
  127. * be defined.
  128. *
  129. * HAVE_SEND is defined if you have a function named send()
  130. * which is used to write outgoing data on a connected socket.
  131. * If yours has another name then do not define HAVE_SEND.
  132. *
  133. * If HAVE_SEND is defined then SEND_TYPE_ARG1, SEND_QUAL_ARG2,
  134. * SEND_TYPE_ARG2, SEND_TYPE_ARG3, SEND_TYPE_ARG4 and
  135. * SEND_TYPE_RETV must also be defined.
  136. */
  137. #define sread(x,y,z) (ssize_t)recv((RECV_TYPE_ARG1)(x), \
  138. (RECV_TYPE_ARG2)(y), \
  139. (RECV_TYPE_ARG3)(z), \
  140. (RECV_TYPE_ARG4)(0))
  141. #else /* HAVE_RECV */
  142. #ifndef sread
  143. #error "Missing definition of macro sread!"
  144. #endif
  145. #endif /* HAVE_RECV */
  146. #ifdef __minix
  147. /* Minix does not support send on TCP sockets */
  148. #define swrite(x,y,z) (ssize_t)write((SEND_TYPE_ARG1)(x), \
  149. (SEND_TYPE_ARG2)CURL_UNCONST(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)CURL_UNCONST(y), \
  154. (SEND_TYPE_ARG3)(z), \
  155. (SEND_TYPE_ARG4)(SEND_4TH_ARG))
  156. #else /* HAVE_SEND */
  157. #ifndef swrite
  158. #error "Missing definition of macro swrite!"
  159. #endif
  160. #endif /* HAVE_SEND */
  161. /*
  162. * Function-like macro definition used to close a socket.
  163. */
  164. #ifdef HAVE_CLOSESOCKET
  165. # define CURL_SCLOSE(x) closesocket((x))
  166. #elif defined(HAVE_CLOSESOCKET_CAMEL)
  167. # define CURL_SCLOSE(x) CloseSocket((x))
  168. #elif defined(MSDOS) /* Watt-32 */
  169. # define CURL_SCLOSE(x) close_s((x))
  170. #elif defined(USE_LWIPSOCK)
  171. # define CURL_SCLOSE(x) lwip_close((x))
  172. #else
  173. # define CURL_SCLOSE(x) close((x))
  174. #endif
  175. /*
  176. * Stack-independent version of fcntl() on sockets:
  177. */
  178. #ifdef USE_LWIPSOCK
  179. # define sfcntl lwip_fcntl
  180. #else
  181. # define sfcntl fcntl
  182. #endif
  183. /*
  184. * 'bool' stuff compatible with HP-UX headers.
  185. */
  186. #if defined(__hpux) && !defined(HAVE_BOOL_T)
  187. typedef int bool;
  188. # define false 0
  189. # define true 1
  190. # define HAVE_BOOL_T
  191. #endif
  192. /*
  193. * 'bool' exists on platforms with <stdbool.h>, i.e. C99 platforms.
  194. * On non-C99 platforms there is no bool, so define an enum for that.
  195. * On C99 platforms 'false' and 'true' also exist. Enum uses a
  196. * global namespace though, so use bool_false and bool_true.
  197. */
  198. #ifndef HAVE_BOOL_T
  199. typedef enum {
  200. bool_false = 0,
  201. bool_true = 1
  202. } bool;
  203. /*
  204. * Use a define to let 'true' and 'false' use those enums. There
  205. * are currently no use of true and false in libcurl proper, but
  206. * there are some in the examples. This will cater for any later
  207. * code happening to use true and false.
  208. */
  209. # define false bool_false
  210. # define true bool_true
  211. # define HAVE_BOOL_T
  212. #endif
  213. /* the type we use for storing a single boolean bit */
  214. #ifdef _MSC_VER
  215. typedef bool bit;
  216. #define BIT(x) bool x
  217. #else
  218. typedef unsigned int bit;
  219. #define BIT(x) bit x:1
  220. #endif
  221. /*
  222. * Redefine TRUE and FALSE too, to catch current use. With this
  223. * change, 'bool found = 1' will give a warning on MIPSPro, but
  224. * 'bool found = TRUE' will not. Change tested on IRIX/MIPSPro,
  225. * AIX 5.1/Xlc, Tru64 5.1/cc, w/make test too.
  226. */
  227. #ifndef TRUE
  228. #define TRUE true
  229. #endif
  230. #ifndef FALSE
  231. #define FALSE false
  232. #endif
  233. #include "curl_ctype.h"
  234. /*
  235. * Macro used to include code only in debug builds.
  236. */
  237. #ifdef DEBUGBUILD
  238. #define DEBUGF(x) x
  239. #else
  240. #define DEBUGF(x) do { } while(0)
  241. #endif
  242. /*
  243. * Macro used to include assertion code only in debug builds.
  244. */
  245. #undef DEBUGASSERT
  246. #ifdef DEBUGBUILD
  247. #define DEBUGASSERT(x) assert(x)
  248. #else
  249. #define DEBUGASSERT(x) do { } while(0)
  250. #endif
  251. /*
  252. * Macro SOCKERRNO / SET_SOCKERRNO() returns / sets the *socket-related* errno
  253. * (or equivalent) on this platform to hide platform details to code using it.
  254. */
  255. #ifdef USE_WINSOCK
  256. #define SOCKERRNO ((int)WSAGetLastError())
  257. #define SET_SOCKERRNO(x) (WSASetLastError((int)(x)))
  258. #else
  259. #define SOCKERRNO (errno)
  260. #define SET_SOCKERRNO(x) (errno = (x))
  261. #endif
  262. /*
  263. * Portable error number symbolic names defined to Winsock error codes.
  264. */
  265. #ifdef USE_WINSOCK
  266. #define SOCKEACCES WSAEACCES
  267. #define SOCKEADDRINUSE WSAEADDRINUSE
  268. #define SOCKEADDRNOTAVAIL WSAEADDRNOTAVAIL
  269. #define SOCKEAFNOSUPPORT WSAEAFNOSUPPORT
  270. #define SOCKEBADF WSAEBADF
  271. #define SOCKECONNREFUSED WSAECONNREFUSED
  272. #define SOCKECONNRESET WSAECONNRESET
  273. #define SOCKEINPROGRESS WSAEINPROGRESS
  274. #define SOCKEINTR WSAEINTR
  275. #define SOCKEINVAL WSAEINVAL
  276. #define SOCKEISCONN WSAEISCONN
  277. #define SOCKEMSGSIZE WSAEMSGSIZE
  278. #define SOCKENOMEM WSA_NOT_ENOUGH_MEMORY
  279. #define SOCKETIMEDOUT WSAETIMEDOUT
  280. #define SOCKEWOULDBLOCK WSAEWOULDBLOCK
  281. #else
  282. #define SOCKEACCES EACCES
  283. #define SOCKEADDRINUSE EADDRINUSE
  284. #define SOCKEADDRNOTAVAIL EADDRNOTAVAIL
  285. #define SOCKEAFNOSUPPORT EAFNOSUPPORT
  286. #define SOCKEBADF EBADF
  287. #define SOCKECONNREFUSED ECONNREFUSED
  288. #define SOCKECONNRESET ECONNRESET
  289. #define SOCKEINPROGRESS EINPROGRESS
  290. #define SOCKEINTR EINTR
  291. #define SOCKEINVAL EINVAL
  292. #define SOCKEISCONN EISCONN
  293. #define SOCKEMSGSIZE EMSGSIZE
  294. #define SOCKENOMEM ENOMEM
  295. #ifdef ETIMEDOUT
  296. #define SOCKETIMEDOUT ETIMEDOUT
  297. #endif
  298. #define SOCKEWOULDBLOCK EWOULDBLOCK
  299. #endif
  300. /*
  301. * Macro argv_item_t hides platform details to code using it.
  302. */
  303. #ifdef __VMS
  304. #define argv_item_t __char_ptr32
  305. #elif defined(_UNICODE) && !defined(UNDER_CE)
  306. #define argv_item_t wchar_t *
  307. #else
  308. #define argv_item_t char *
  309. #endif
  310. /*
  311. * We use this ZERO_NULL to avoid picky compiler warnings,
  312. * when assigning a NULL pointer to a function pointer var.
  313. */
  314. #define ZERO_NULL 0
  315. #endif /* HEADER_CURL_SETUP_ONCE_H */