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
103struct 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/*
328 * Macro used to include code only in debug builds.
329 */
330
331#ifdef DEBUGBUILD
332#define DEBUGF(x) x
333#else
334#define DEBUGF(x) do { } while(0)
335#endif
336
337
338/*
339 * Macro used to include assertion code only in debug builds.
340 */
341
342#undef DEBUGASSERT
343#if defined(DEBUGBUILD) && defined(HAVE_ASSERT_H)
344#define DEBUGASSERT(x) assert(x)
345#else
346#define DEBUGASSERT(x) do { } while(0)
347#endif
348
349
350/*
351 * Macro SOCKERRNO / SET_SOCKERRNO() returns / sets the *socket-related* errno
352 * (or equivalent) on this platform to hide platform details to code using it.
353 */
354
355#ifdef USE_WINSOCK
356#define SOCKERRNO ((int)WSAGetLastError())
357#define SET_SOCKERRNO(x) (WSASetLastError((int)(x)))
358#else
359#define SOCKERRNO (errno)
360#define SET_SOCKERRNO(x) (errno = (x))
361#endif
362
363
364/*
365 * Portable error number symbolic names defined to Winsock error codes.
366 */
367
368#ifdef USE_WINSOCK
369#undef EBADF /* override definition in errno.h */
370#define EBADF WSAEBADF
371#undef EINTR /* override definition in errno.h */
372#define EINTR WSAEINTR
373#undef EINVAL /* override definition in errno.h */
374#define EINVAL WSAEINVAL
375#undef EWOULDBLOCK /* override definition in errno.h */
376#define EWOULDBLOCK WSAEWOULDBLOCK
377#undef EINPROGRESS /* override definition in errno.h */
378#define EINPROGRESS WSAEINPROGRESS
379#undef EALREADY /* override definition in errno.h */
380#define EALREADY WSAEALREADY
381#undef ENOTSOCK /* override definition in errno.h */
382#define ENOTSOCK WSAENOTSOCK
383#undef EDESTADDRREQ /* override definition in errno.h */
384#define EDESTADDRREQ WSAEDESTADDRREQ
385#undef EMSGSIZE /* override definition in errno.h */
386#define EMSGSIZE WSAEMSGSIZE
387#undef EPROTOTYPE /* override definition in errno.h */
388#define EPROTOTYPE WSAEPROTOTYPE
389#undef ENOPROTOOPT /* override definition in errno.h */
390#define ENOPROTOOPT WSAENOPROTOOPT
391#undef EPROTONOSUPPORT /* override definition in errno.h */
392#define EPROTONOSUPPORT WSAEPROTONOSUPPORT
393#define ESOCKTNOSUPPORT WSAESOCKTNOSUPPORT
394#undef EOPNOTSUPP /* override definition in errno.h */
395#define EOPNOTSUPP WSAEOPNOTSUPP
396#define EPFNOSUPPORT WSAEPFNOSUPPORT
397#undef EAFNOSUPPORT /* override definition in errno.h */
398#define EAFNOSUPPORT WSAEAFNOSUPPORT
399#undef EADDRINUSE /* override definition in errno.h */
400#define EADDRINUSE WSAEADDRINUSE
401#undef EADDRNOTAVAIL /* override definition in errno.h */
402#define EADDRNOTAVAIL WSAEADDRNOTAVAIL
403#undef ENETDOWN /* override definition in errno.h */
404#define ENETDOWN WSAENETDOWN
405#undef ENETUNREACH /* override definition in errno.h */
406#define ENETUNREACH WSAENETUNREACH
407#undef ENETRESET /* override definition in errno.h */
408#define ENETRESET WSAENETRESET
409#undef ECONNABORTED /* override definition in errno.h */
410#define ECONNABORTED WSAECONNABORTED
411#undef ECONNRESET /* override definition in errno.h */
412#define ECONNRESET WSAECONNRESET
413#undef ENOBUFS /* override definition in errno.h */
414#define ENOBUFS WSAENOBUFS
415#undef EISCONN /* override definition in errno.h */
416#define EISCONN WSAEISCONN
417#undef ENOTCONN /* override definition in errno.h */
418#define ENOTCONN WSAENOTCONN
419#define ESHUTDOWN WSAESHUTDOWN
420#define ETOOMANYREFS WSAETOOMANYREFS
421#undef ETIMEDOUT /* override definition in errno.h */
422#define ETIMEDOUT WSAETIMEDOUT
423#undef ECONNREFUSED /* override definition in errno.h */
424#define ECONNREFUSED WSAECONNREFUSED
425#undef ELOOP /* override definition in errno.h */
426#define ELOOP WSAELOOP
427#ifndef ENAMETOOLONG /* possible previous definition in errno.h */
428#define ENAMETOOLONG WSAENAMETOOLONG
429#endif
430#define EHOSTDOWN WSAEHOSTDOWN
431#undef EHOSTUNREACH /* override definition in errno.h */
432#define EHOSTUNREACH WSAEHOSTUNREACH
433#ifndef ENOTEMPTY /* possible previous definition in errno.h */
434#define ENOTEMPTY WSAENOTEMPTY
435#endif
436#define EPROCLIM WSAEPROCLIM
437#define EUSERS WSAEUSERS
438#define EDQUOT WSAEDQUOT
439#define ESTALE WSAESTALE
440#define EREMOTE WSAEREMOTE
441#endif
442
443/*
444 * Macro argv_item_t hides platform details to code using it.
445 */
446
447#ifdef __VMS
448#define argv_item_t __char_ptr32
449#elif defined(_UNICODE)
450#define argv_item_t wchar_t *
451#else
452#define argv_item_t char *
453#endif
454
455
456/*
457 * We use this ZERO_NULL to avoid picky compiler warnings,
458 * when assigning a NULL pointer to a function pointer var.
459 */
460
461#define ZERO_NULL 0
462
463
464#endif /* HEADER_CURL_SETUP_ONCE_H */
465