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