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