xref: /third_party/node/deps/cares/src/lib/setup_once.h (revision 1cb0ef41)
1/* MIT License
2 *
3 * Copyright (c) 2004 Daniel Stenberg
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 * SPDX-License-Identifier: MIT
25 */
26#ifndef __SETUP_ONCE_H
27#define __SETUP_ONCE_H
28
29
30/********************************************************************
31 *                              NOTICE                              *
32 *                             ========                             *
33 *                                                                  *
34 *  Content of header files lib/setup_once.h and ares/setup_once.h  *
35 *  must be kept in sync. Modify the other one if you change this.  *
36 *                                                                  *
37 ********************************************************************/
38
39
40/*
41 * Inclusion of common header files.
42 */
43
44#include <stdio.h>
45#include <stdlib.h>
46#include <string.h>
47#include <stdarg.h>
48#include <ctype.h>
49
50#ifdef HAVE_ERRNO_H
51#  include <errno.h>
52#endif
53
54#ifdef HAVE_SYS_TYPES_H
55#  include <sys/types.h>
56#endif
57
58#ifdef NEED_MALLOC_H
59#  include <malloc.h>
60#endif
61
62#ifdef NEED_MEMORY_H
63#  include <memory.h>
64#endif
65
66#ifdef HAVE_SYS_STAT_H
67#  include <sys/stat.h>
68#endif
69
70#ifdef HAVE_SYS_TIME_H
71#  include <sys/time.h>
72#endif
73
74#ifdef HAVE_TIME_H
75#  include <time.h>
76#endif
77
78#ifdef WIN32
79#  include <io.h>
80#  include <fcntl.h>
81#endif
82
83#ifdef HAVE_UNISTD_H
84#  include <unistd.h>
85#endif
86
87#ifdef __hpux
88#  if !defined(_XOPEN_SOURCE_EXTENDED) || defined(_KERNEL)
89#    ifdef _APP32_64BIT_OFF_T
90#      define OLD_APP32_64BIT_OFF_T _APP32_64BIT_OFF_T
91#      undef _APP32_64BIT_OFF_T
92#    else
93#      undef OLD_APP32_64BIT_OFF_T
94#    endif
95#  endif
96#endif
97
98#ifdef HAVE_SYS_SOCKET_H
99#  include <sys/socket.h>
100#endif
101
102#ifdef __hpux
103#  if !defined(_XOPEN_SOURCE_EXTENDED) || defined(_KERNEL)
104#    ifdef OLD_APP32_64BIT_OFF_T
105#      define _APP32_64BIT_OFF_T OLD_APP32_64BIT_OFF_T
106#      undef OLD_APP32_64BIT_OFF_T
107#    endif
108#  endif
109#endif
110
111
112/*
113 * Definition of timeval struct for platforms that don't have it.
114 */
115
116#ifndef HAVE_STRUCT_TIMEVAL
117struct timeval {
118  long tv_sec;
119  long tv_usec;
120};
121#endif
122
123
124/*
125 * If we have the MSG_NOSIGNAL define, make sure we use
126 * it as the fourth argument of function send()
127 */
128
129#ifdef HAVE_MSG_NOSIGNAL
130#  define SEND_4TH_ARG MSG_NOSIGNAL
131#else
132#  define SEND_4TH_ARG 0
133#endif
134
135
136#if defined(__minix)
137/* Minix doesn't support recv on TCP sockets */
138#  define sread(x, y, z) \
139    (ares_ssize_t)       \
140      read((RECV_TYPE_ARG1)(x), (RECV_TYPE_ARG2)(y), (RECV_TYPE_ARG3)(z))
141
142#elif defined(HAVE_RECV)
143/*
144 * The definitions for the return type and arguments types
145 * of functions recv() and send() belong and come from the
146 * configuration file. Do not define them in any other place.
147 *
148 * HAVE_RECV is defined if you have a function named recv()
149 * which is used to read incoming data from sockets. If your
150 * function has another name then don't define HAVE_RECV.
151 *
152 * If HAVE_RECV is defined then RECV_TYPE_ARG1, RECV_TYPE_ARG2,
153 * RECV_TYPE_ARG3, RECV_TYPE_ARG4 and RECV_TYPE_RETV must also
154 * be defined.
155 *
156 * HAVE_SEND is defined if you have a function named send()
157 * which is used to write outgoing data on a connected socket.
158 * If yours has another name then don't define HAVE_SEND.
159 *
160 * If HAVE_SEND is defined then SEND_TYPE_ARG1, SEND_QUAL_ARG2,
161 * SEND_TYPE_ARG2, SEND_TYPE_ARG3, SEND_TYPE_ARG4 and
162 * SEND_TYPE_RETV must also be defined.
163 */
164
165#  if !defined(RECV_TYPE_ARG1) || !defined(RECV_TYPE_ARG2) || \
166    !defined(RECV_TYPE_ARG3) || !defined(RECV_TYPE_ARG4) ||   \
167    !defined(RECV_TYPE_RETV)
168/* */
169Error Missing_definition_of_return_and_arguments_types_of_recv
170/* */
171#  else
172#    define sread(x, y, z)                                          \
173      (ares_ssize_t) recv((RECV_TYPE_ARG1)(x), (RECV_TYPE_ARG2)(y), \
174                          (RECV_TYPE_ARG3)(z), (RECV_TYPE_ARG4)(0))
175#  endif
176#else /* HAVE_RECV */
177#  ifndef sread
178/* */
179Error Missing_definition_of_macro_sread
180/* */
181#  endif
182#endif /* HAVE_RECV */
183
184
185#if defined(__minix)
186/* Minix doesn't support send on TCP sockets */
187#  define swrite(x, y, z) \
188    (ares_ssize_t)        \
189      write((SEND_TYPE_ARG1)(x), (SEND_TYPE_ARG2)(y), (SEND_TYPE_ARG3)(z))
190
191#elif defined(HAVE_SEND)
192#  if !defined(SEND_TYPE_ARG1) || !defined(SEND_QUAL_ARG2) || \
193    !defined(SEND_TYPE_ARG2) || !defined(SEND_TYPE_ARG3) ||   \
194    !defined(SEND_TYPE_ARG4) || !defined(SEND_TYPE_RETV)
195  /* */
196  Error Missing_definition_of_return_and_arguments_types_of_send
197/* */
198#  else
199#    define swrite(x, y, z)                                         \
200      (ares_ssize_t) send((SEND_TYPE_ARG1)(x), (SEND_TYPE_ARG2)(y), \
201                          (SEND_TYPE_ARG3)(z), (SEND_TYPE_ARG4)(SEND_4TH_ARG))
202#  endif
203#else /* HAVE_SEND */
204#  ifndef swrite
205  /* */
206  Error Missing_definition_of_macro_swrite
207/* */
208#  endif
209#endif /* HAVE_SEND */
210
211
212#if 0
213#  if defined(HAVE_RECVFROM)
214/*
215 * Currently recvfrom is only used on udp sockets.
216 */
217#    if !defined(RECVFROM_TYPE_ARG1) || !defined(RECVFROM_TYPE_ARG2) || \
218      !defined(RECVFROM_TYPE_ARG3) || !defined(RECVFROM_TYPE_ARG4) ||   \
219      !defined(RECVFROM_TYPE_ARG5) || !defined(RECVFROM_TYPE_ARG6) ||   \
220      !defined(RECVFROM_TYPE_RETV)
221  /* */
222  Error Missing_definition_of_return_and_arguments_types_of_recvfrom
223  /* */
224#    else
225#      define sreadfrom(s, b, bl, f, fl)                               \
226        (ares_ssize_t)                                                 \
227          recvfrom((RECVFROM_TYPE_ARG1)(s), (RECVFROM_TYPE_ARG2 *)(b), \
228                   (RECVFROM_TYPE_ARG3)(bl), (RECVFROM_TYPE_ARG4)(0),  \
229                   (RECVFROM_TYPE_ARG5 *)(f), (RECVFROM_TYPE_ARG6 *)(fl))
230#    endif
231#  else /* HAVE_RECVFROM */
232#    ifndef sreadfrom
233  /* */
234  Error Missing_definition_of_macro_sreadfrom
235  /* */
236#    endif
237#  endif /* HAVE_RECVFROM */
238
239
240#  ifdef RECVFROM_TYPE_ARG6_IS_VOID
241#    define RECVFROM_ARG6_T int
242#  else
243#    define RECVFROM_ARG6_T RECVFROM_TYPE_ARG6
244#  endif
245#endif /* if 0 */
246
247
248/*
249 * Function-like macro definition used to close a socket.
250 */
251
252#if defined(HAVE_CLOSESOCKET)
253#  define sclose(x) closesocket((x))
254#elif defined(HAVE_CLOSESOCKET_CAMEL)
255#  define sclose(x) CloseSocket((x))
256#elif defined(HAVE_CLOSE_S)
257#  define sclose(x) close_s((x))
258#else
259#  define sclose(x) close((x))
260#endif
261
262
263/*
264 * Uppercase macro versions of ANSI/ISO is*() functions/macros which
265 * avoid negative number inputs with argument byte codes > 127.
266 */
267
268#define ISSPACE(x)  (isspace((int)((unsigned char)x)))
269#define ISDIGIT(x)  (isdigit((int)((unsigned char)x)))
270#define ISALNUM(x)  (isalnum((int)((unsigned char)x)))
271#define ISXDIGIT(x) (isxdigit((int)((unsigned char)x)))
272#define ISGRAPH(x)  (isgraph((int)((unsigned char)x)))
273#define ISALPHA(x)  (isalpha((int)((unsigned char)x)))
274#define ISPRINT(x)  (isprint((int)((unsigned char)x)))
275#define ISUPPER(x)  (isupper((int)((unsigned char)x)))
276#define ISLOWER(x)  (islower((int)((unsigned char)x)))
277#define ISASCII(x)  (isascii((int)((unsigned char)x)))
278
279#define ISBLANK(x) \
280  (int)((((unsigned char)x) == ' ') || (((unsigned char)x) == '\t'))
281
282#define TOLOWER(x) (tolower((int)((unsigned char)x)))
283
284
285/*
286 * Macro WHILE_FALSE may be used to build single-iteration do-while loops,
287 * avoiding compiler warnings. Mostly intended for other macro definitions.
288 */
289
290#define WHILE_FALSE while (0)
291
292#if defined(_MSC_VER) && !defined(__POCC__)
293#  undef WHILE_FALSE
294#  if (_MSC_VER < 1500)
295#    define WHILE_FALSE while (1, 0)
296#  else
297#    define WHILE_FALSE                                                   \
298      __pragma(warning(push)) __pragma(warning(disable : 4127)) while (0) \
299        __pragma(warning(pop))
300#  endif
301#endif
302
303
304/*
305 * Macro used to include code only in debug builds.
306 */
307
308#ifdef DEBUGBUILD
309#  define DEBUGF(x) x
310#else
311#  define DEBUGF(x) \
312    do {            \
313    }               \
314    WHILE_FALSE
315#endif
316
317
318/*
319 * Macro used to include assertion code only in debug builds.
320 */
321
322#if defined(DEBUGBUILD) && defined(HAVE_ASSERT_H)
323#  define DEBUGASSERT(x) assert(x)
324#else
325#  define DEBUGASSERT(x) \
326    do {                 \
327    }                    \
328    WHILE_FALSE
329#endif
330
331
332/*
333 * Macro SOCKERRNO / SET_SOCKERRNO() returns / sets the *socket-related* errno
334 * (or equivalent) on this platform to hide platform details to code using it.
335 */
336
337#ifdef USE_WINSOCK
338#  define SOCKERRNO        ((int)WSAGetLastError())
339#  define SET_SOCKERRNO(x) (WSASetLastError((int)(x)))
340#else
341#  define SOCKERRNO        (errno)
342#  define SET_SOCKERRNO(x) (errno = (x))
343#endif
344
345
346/*
347 * Macro ERRNO / SET_ERRNO() returns / sets the NOT *socket-related* errno
348 * (or equivalent) on this platform to hide platform details to code using it.
349 */
350
351#if defined(WIN32) && !defined(WATT32)
352#  define ERRNO        ((int)GetLastError())
353#  define SET_ERRNO(x) (SetLastError((DWORD)(x)))
354#else
355#  define ERRNO        (errno)
356#  define SET_ERRNO(x) (errno = (x))
357#endif
358
359
360/*
361 * Portable error number symbolic names defined to Winsock error codes.
362 */
363
364#ifdef USE_WINSOCK
365#  undef EBADF           /* override definition in errno.h */
366#  define EBADF WSAEBADF
367#  undef EINTR           /* override definition in errno.h */
368#  define EINTR WSAEINTR
369#  undef EINVAL          /* override definition in errno.h */
370#  define EINVAL WSAEINVAL
371#  undef EWOULDBLOCK     /* override definition in errno.h */
372#  define EWOULDBLOCK WSAEWOULDBLOCK
373#  undef EINPROGRESS     /* override definition in errno.h */
374#  define EINPROGRESS WSAEINPROGRESS
375#  undef EALREADY        /* override definition in errno.h */
376#  define EALREADY WSAEALREADY
377#  undef ENOTSOCK        /* override definition in errno.h */
378#  define ENOTSOCK WSAENOTSOCK
379#  undef EDESTADDRREQ    /* override definition in errno.h */
380#  define EDESTADDRREQ WSAEDESTADDRREQ
381#  undef EMSGSIZE        /* override definition in errno.h */
382#  define EMSGSIZE WSAEMSGSIZE
383#  undef EPROTOTYPE      /* override definition in errno.h */
384#  define EPROTOTYPE WSAEPROTOTYPE
385#  undef ENOPROTOOPT     /* override definition in errno.h */
386#  define ENOPROTOOPT WSAENOPROTOOPT
387#  undef EPROTONOSUPPORT /* override definition in errno.h */
388#  define EPROTONOSUPPORT WSAEPROTONOSUPPORT
389#  define ESOCKTNOSUPPORT WSAESOCKTNOSUPPORT
390#  undef EOPNOTSUPP /* override definition in errno.h */
391#  define EOPNOTSUPP   WSAEOPNOTSUPP
392#  define EPFNOSUPPORT WSAEPFNOSUPPORT
393#  undef EAFNOSUPPORT  /* override definition in errno.h */
394#  define EAFNOSUPPORT WSAEAFNOSUPPORT
395#  undef EADDRINUSE    /* override definition in errno.h */
396#  define EADDRINUSE WSAEADDRINUSE
397#  undef EADDRNOTAVAIL /* override definition in errno.h */
398#  define EADDRNOTAVAIL WSAEADDRNOTAVAIL
399#  undef ENETDOWN      /* override definition in errno.h */
400#  define ENETDOWN WSAENETDOWN
401#  undef ENETUNREACH   /* override definition in errno.h */
402#  define ENETUNREACH WSAENETUNREACH
403#  undef ENETRESET     /* override definition in errno.h */
404#  define ENETRESET WSAENETRESET
405#  undef ECONNABORTED  /* override definition in errno.h */
406#  define ECONNABORTED WSAECONNABORTED
407#  undef ECONNRESET    /* override definition in errno.h */
408#  define ECONNRESET WSAECONNRESET
409#  undef ENOBUFS       /* override definition in errno.h */
410#  define ENOBUFS WSAENOBUFS
411#  undef EISCONN       /* override definition in errno.h */
412#  define EISCONN WSAEISCONN
413#  undef ENOTCONN      /* override definition in errno.h */
414#  define ENOTCONN     WSAENOTCONN
415#  define ESHUTDOWN    WSAESHUTDOWN
416#  define ETOOMANYREFS WSAETOOMANYREFS
417#  undef ETIMEDOUT     /* override definition in errno.h */
418#  define ETIMEDOUT WSAETIMEDOUT
419#  undef ECONNREFUSED  /* override definition in errno.h */
420#  define ECONNREFUSED WSAECONNREFUSED
421#  undef ELOOP         /* override definition in errno.h */
422#  define ELOOP WSAELOOP
423#  ifndef ENAMETOOLONG /* possible previous definition in errno.h */
424#    define ENAMETOOLONG WSAENAMETOOLONG
425#  endif
426#  define EHOSTDOWN WSAEHOSTDOWN
427#  undef EHOSTUNREACH /* override definition in errno.h */
428#  define EHOSTUNREACH WSAEHOSTUNREACH
429#  ifndef ENOTEMPTY   /* possible previous definition in errno.h */
430#    define ENOTEMPTY WSAENOTEMPTY
431#  endif
432#  define EPROCLIM WSAEPROCLIM
433#  define EUSERS   WSAEUSERS
434#  define EDQUOT   WSAEDQUOT
435#  define ESTALE   WSAESTALE
436#  define EREMOTE  WSAEREMOTE
437#endif
438
439
440/*
441 *  Actually use __32_getpwuid() on 64-bit VMS builds for getpwuid()
442 */
443
444#if defined(__VMS) && defined(__INITIAL_POINTER_SIZE) && \
445  (__INITIAL_POINTER_SIZE == 64)
446#  define getpwuid __32_getpwuid
447#endif
448
449
450/*
451 * Macro argv_item_t hides platform details to code using it.
452 */
453
454#ifdef __VMS
455#  define argv_item_t __char_ptr32
456#else
457#  define argv_item_t char *
458#endif
459
460
461/*
462 * We use this ZERO_NULL to avoid picky compiler warnings,
463 * when assigning a NULL pointer to a function pointer var.
464 */
465
466#define ZERO_NULL 0
467
468
469#endif /* __SETUP_ONCE_H */
470