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