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 don't 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 doesn't 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 don't 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 don't 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   /* */
168   Error Missing_definition_of_macro_sread
169   /* */
170 #endif
171 #endif /* HAVE_RECV */
172 
173 
174 #if defined(__minix)
175 /* Minix doesn't support send on TCP sockets */
176 #define swrite(x,y,z) (ssize_t)write((SEND_TYPE_ARG1)(x), \
177                                     (SEND_TYPE_ARG2)(y), \
178                                     (SEND_TYPE_ARG3)(z))
179 
180 #elif defined(HAVE_SEND)
181 #define swrite(x,y,z) (ssize_t)send((SEND_TYPE_ARG1)(x), \
182                                     (SEND_QUAL_ARG2 SEND_TYPE_ARG2)(y), \
183                                     (SEND_TYPE_ARG3)(z), \
184                                     (SEND_TYPE_ARG4)(SEND_4TH_ARG))
185 #else /* HAVE_SEND */
186 #ifndef swrite
187   /* */
188   Error Missing_definition_of_macro_swrite
189   /* */
190 #endif
191 #endif /* HAVE_SEND */
192 
193 
194 /*
195  * Function-like macro definition used to close a socket.
196  */
197 
198 #if defined(HAVE_CLOSESOCKET)
199 #  define sclose(x)  closesocket((x))
200 #elif defined(HAVE_CLOSESOCKET_CAMEL)
201 #  define sclose(x)  CloseSocket((x))
202 #elif defined(HAVE_CLOSE_S)
203 #  define sclose(x)  close_s((x))
204 #elif defined(USE_LWIPSOCK)
205 #  define sclose(x)  lwip_close((x))
206 #else
207 #  define sclose(x)  close((x))
208 #endif
209 
210 /*
211  * Stack-independent version of fcntl() on sockets:
212  */
213 #if defined(USE_LWIPSOCK)
214 #  define sfcntl  lwip_fcntl
215 #else
216 #  define sfcntl  fcntl
217 #endif
218 
219 /*
220  * 'bool' stuff compatible with HP-UX headers.
221  */
222 
223 #if defined(__hpux) && !defined(HAVE_BOOL_T)
224    typedef int bool;
225 #  define false 0
226 #  define true 1
227 #  define HAVE_BOOL_T
228 #endif
229 
230 
231 /*
232  * 'bool' exists on platforms with <stdbool.h>, i.e. C99 platforms.
233  * On non-C99 platforms there's no bool, so define an enum for that.
234  * On C99 platforms 'false' and 'true' also exist. Enum uses a
235  * global namespace though, so use bool_false and bool_true.
236  */
237 
238 #ifndef HAVE_BOOL_T
239   typedef enum {
240       bool_false = 0,
241       bool_true  = 1
242   } bool;
243 
244 /*
245  * Use a define to let 'true' and 'false' use those enums.  There
246  * are currently no use of true and false in libcurl proper, but
247  * there are some in the examples. This will cater for any later
248  * code happening to use true and false.
249  */
250 #  define false bool_false
251 #  define true  bool_true
252 #  define HAVE_BOOL_T
253 #endif
254 
255 /* the type we use for storing a single boolean bit */
256 #ifdef _MSC_VER
257 typedef bool bit;
258 #define BIT(x) bool x
259 #else
260 typedef unsigned int bit;
261 #define BIT(x) bit x:1
262 #endif
263 
264 /*
265  * Redefine TRUE and FALSE too, to catch current use. With this
266  * change, 'bool found = 1' will give a warning on MIPSPro, but
267  * 'bool found = TRUE' will not. Change tested on IRIX/MIPSPro,
268  * AIX 5.1/Xlc, Tru64 5.1/cc, w/make test too.
269  */
270 
271 #ifndef TRUE
272 #define TRUE true
273 #endif
274 #ifndef FALSE
275 #define FALSE false
276 #endif
277 
278 #include "curl_ctype.h"
279 
280 
281 /*
282  * Macro used to include code only in debug builds.
283  */
284 
285 #ifdef DEBUGBUILD
286 #define DEBUGF(x) x
287 #else
288 #define DEBUGF(x) do { } while(0)
289 #endif
290 
291 
292 /*
293  * Macro used to include assertion code only in debug builds.
294  */
295 
296 #undef DEBUGASSERT
297 #if defined(DEBUGBUILD)
298 #define DEBUGASSERT(x) assert(x)
299 #else
300 #define DEBUGASSERT(x) do { } while(0)
301 #endif
302 
303 
304 /*
305  * Macro SOCKERRNO / SET_SOCKERRNO() returns / sets the *socket-related* errno
306  * (or equivalent) on this platform to hide platform details to code using it.
307  */
308 
309 #ifdef USE_WINSOCK
310 #define SOCKERRNO         ((int)WSAGetLastError())
311 #define SET_SOCKERRNO(x)  (WSASetLastError((int)(x)))
312 #else
313 #define SOCKERRNO         (errno)
314 #define SET_SOCKERRNO(x)  (errno = (x))
315 #endif
316 
317 
318 /*
319  * Portable error number symbolic names defined to Winsock error codes.
320  */
321 
322 #ifdef USE_WINSOCK
323 #undef  EBADF            /* override definition in errno.h */
324 #define EBADF            WSAEBADF
325 #undef  EINTR            /* override definition in errno.h */
326 #define EINTR            WSAEINTR
327 #undef  EINVAL           /* override definition in errno.h */
328 #define EINVAL           WSAEINVAL
329 #undef  EWOULDBLOCK      /* override definition in errno.h */
330 #define EWOULDBLOCK      WSAEWOULDBLOCK
331 #undef  EINPROGRESS      /* override definition in errno.h */
332 #define EINPROGRESS      WSAEINPROGRESS
333 #undef  EALREADY         /* override definition in errno.h */
334 #define EALREADY         WSAEALREADY
335 #undef  ENOTSOCK         /* override definition in errno.h */
336 #define ENOTSOCK         WSAENOTSOCK
337 #undef  EDESTADDRREQ     /* override definition in errno.h */
338 #define EDESTADDRREQ     WSAEDESTADDRREQ
339 #undef  EMSGSIZE         /* override definition in errno.h */
340 #define EMSGSIZE         WSAEMSGSIZE
341 #undef  EPROTOTYPE       /* override definition in errno.h */
342 #define EPROTOTYPE       WSAEPROTOTYPE
343 #undef  ENOPROTOOPT      /* override definition in errno.h */
344 #define ENOPROTOOPT      WSAENOPROTOOPT
345 #undef  EPROTONOSUPPORT  /* override definition in errno.h */
346 #define EPROTONOSUPPORT  WSAEPROTONOSUPPORT
347 #define ESOCKTNOSUPPORT  WSAESOCKTNOSUPPORT
348 #undef  EOPNOTSUPP       /* override definition in errno.h */
349 #define EOPNOTSUPP       WSAEOPNOTSUPP
350 #define EPFNOSUPPORT     WSAEPFNOSUPPORT
351 #undef  EAFNOSUPPORT     /* override definition in errno.h */
352 #define EAFNOSUPPORT     WSAEAFNOSUPPORT
353 #undef  EADDRINUSE       /* override definition in errno.h */
354 #define EADDRINUSE       WSAEADDRINUSE
355 #undef  EADDRNOTAVAIL    /* override definition in errno.h */
356 #define EADDRNOTAVAIL    WSAEADDRNOTAVAIL
357 #undef  ENETDOWN         /* override definition in errno.h */
358 #define ENETDOWN         WSAENETDOWN
359 #undef  ENETUNREACH      /* override definition in errno.h */
360 #define ENETUNREACH      WSAENETUNREACH
361 #undef  ENETRESET        /* override definition in errno.h */
362 #define ENETRESET        WSAENETRESET
363 #undef  ECONNABORTED     /* override definition in errno.h */
364 #define ECONNABORTED     WSAECONNABORTED
365 #undef  ECONNRESET       /* override definition in errno.h */
366 #define ECONNRESET       WSAECONNRESET
367 #undef  ENOBUFS          /* override definition in errno.h */
368 #define ENOBUFS          WSAENOBUFS
369 #undef  EISCONN          /* override definition in errno.h */
370 #define EISCONN          WSAEISCONN
371 #undef  ENOTCONN         /* override definition in errno.h */
372 #define ENOTCONN         WSAENOTCONN
373 #define ESHUTDOWN        WSAESHUTDOWN
374 #define ETOOMANYREFS     WSAETOOMANYREFS
375 #undef  ETIMEDOUT        /* override definition in errno.h */
376 #define ETIMEDOUT        WSAETIMEDOUT
377 #undef  ECONNREFUSED     /* override definition in errno.h */
378 #define ECONNREFUSED     WSAECONNREFUSED
379 #undef  ELOOP            /* override definition in errno.h */
380 #define ELOOP            WSAELOOP
381 #ifndef ENAMETOOLONG     /* possible previous definition in errno.h */
382 #define ENAMETOOLONG     WSAENAMETOOLONG
383 #endif
384 #define EHOSTDOWN        WSAEHOSTDOWN
385 #undef  EHOSTUNREACH     /* override definition in errno.h */
386 #define EHOSTUNREACH     WSAEHOSTUNREACH
387 #ifndef ENOTEMPTY        /* possible previous definition in errno.h */
388 #define ENOTEMPTY        WSAENOTEMPTY
389 #endif
390 #define EPROCLIM         WSAEPROCLIM
391 #define EUSERS           WSAEUSERS
392 #define EDQUOT           WSAEDQUOT
393 #define ESTALE           WSAESTALE
394 #define EREMOTE          WSAEREMOTE
395 #endif
396 
397 /*
398  * Macro argv_item_t hides platform details to code using it.
399  */
400 
401 #ifdef __VMS
402 #define argv_item_t  __char_ptr32
403 #elif defined(_UNICODE)
404 #define argv_item_t wchar_t *
405 #else
406 #define argv_item_t  char *
407 #endif
408 
409 
410 /*
411  * We use this ZERO_NULL to avoid picky compiler warnings,
412  * when assigning a NULL pointer to a function pointer var.
413  */
414 
415 #define ZERO_NULL 0
416 
417 
418 #endif /* HEADER_CURL_SETUP_ONCE_H */
419