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