OpenVPN
syshead.h
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
7 *
8 * Copyright (C) 2002-2025 OpenVPN Inc <sales@openvpn.net>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, see <https://www.gnu.org/licenses/>.
21 */
22
23#ifndef SYSHEAD_H
24#define SYSHEAD_H
25
26#include "compat.h"
27#include <stdbool.h>
28
29/* branch prediction hints */
30#if defined(__GNUC__)
31#define likely(x) __builtin_expect((x), 1)
32#define unlikely(x) __builtin_expect((x), 0)
33#else
34#define likely(x) (x)
35#define unlikely(x) (x)
36#endif
37
38#ifdef _WIN32
39#include <windows.h>
40#include <winsock2.h>
41#include <tlhelp32.h>
42#define sleep(x) Sleep((x) * 1000)
43#define random rand
44#define srandom srand
45#endif
46
47/* if inttypes.h is included this breaks rc.exe when using the ClangCL
48 * Toolchain as it pulls in a inttypes.h variant for clang that rc.exe does
49 * not understand (#include_next preprocessor directive) */
50#if defined(_WIN32) && !defined(RC_INVOKED)
51#include <inttypes.h>
52typedef uint32_t in_addr_t;
53typedef uint16_t in_port_t;
54
55#define SIGHUP 1
56#define SIGINT 2
57#define SIGUSR1 10
58#define SIGUSR2 12
59#define SIGTERM 15
60#endif
61
62#if defined(_MSC_VER) && !defined(RC_INVOKED)
63#include <BaseTsd.h>
64typedef SSIZE_T ssize_t;
65#define strncasecmp strnicmp
66#define strcasecmp _stricmp
67
68#define S_IRUSR _S_IREAD
69#define S_IWUSR _S_IWRITE
70#define R_OK 4
71#define W_OK 2
72#define X_OK 1
73#define F_OK 0
74#endif
75
76#if defined(_MSC_VER) && !defined(__clang__) /* Microsoft compiler */
77#define __func__ __FUNCTION__
78#define __attribute__(x)
79#endif
80
81#if defined(__APPLE__)
82#if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1070
83#define __APPLE_USE_RFC_3542 1
84#endif
85#endif
86
87#ifdef HAVE_SYS_TYPES_H
88#include <sys/types.h>
89#endif
90
91#ifdef HAVE_SYS_WAIT_H
92#include <sys/wait.h>
93#endif
94
95#ifndef _WIN32
96#ifndef WEXITSTATUS
97#define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
98#endif
99#ifndef WIFEXITED
100#define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
101#endif
102#endif
103
104#ifdef HAVE_SYS_TIME_H
105#include <sys/time.h>
106#endif
107
108#include <time.h>
109
110#ifdef HAVE_SYS_SOCKET_H
111#include <sys/socket.h>
112#endif
113
114#ifdef HAVE_SYS_UN_H
115#include <sys/un.h>
116#endif
117
118#ifdef HAVE_SYS_IOCTL_H
119#include <sys/ioctl.h>
120#endif
121
122#ifdef HAVE_SYS_STAT_H
123#include <sys/stat.h>
124#endif
125
126#ifdef HAVE_FCNTL_H
127#include <fcntl.h>
128#endif
129
130#ifdef HAVE_SYS_FILE_H
131#include <sys/file.h>
132#endif
133
134/* These headers belong to C99 and should be always be present */
135#include <stdlib.h>
136#include <inttypes.h>
137#include <stdint.h>
138#include <stdarg.h>
139#include <signal.h>
140#include <limits.h>
141#include <stdio.h>
142#include <ctype.h>
143#include <errno.h>
144
145#ifdef HAVE_UNISTD_H
146#include <unistd.h>
147#endif
148
149#ifdef HAVE_ERR_H
150#include <err.h>
151#endif
152
153#ifdef HAVE_SYSLOG_H
154#include <syslog.h>
155#endif
156
157#ifdef HAVE_PWD_H
158#include <pwd.h>
159#endif
160
161#ifdef HAVE_GRP_H
162#include <grp.h>
163#endif
164
165#ifdef HAVE_NETDB_H
166#include <netdb.h>
167#endif
168
169#ifdef HAVE_NETINET_IN_H
170#include <netinet/in.h>
171#endif
172
173#ifdef HAVE_RESOLV_H
174#include <resolv.h>
175#endif
176
177#ifdef HAVE_POLL_H
178#include <poll.h>
179#endif
180
181#ifdef ENABLE_SELINUX
182#include <selinux/selinux.h>
183#endif
184
185#if defined(HAVE_LIBGEN_H)
186#include <libgen.h>
187#endif
188
189#ifdef TARGET_SOLARIS
190#ifdef HAVE_STRINGS_H
191#include <strings.h>
192#endif
193#else
194#include <string.h>
195#endif
196
197#if defined(TARGET_HAIKU)
198#include <SupportDefs.h> /* uint32, etc */
199#include <net/if.h> /* ifconf etc */
200#include <sys/sockio.h> /* SIOCGRTTABLE, etc */
201#endif /* TARGET_HAIKU */
202
203#ifdef HAVE_ARPA_INET_H
204#include <arpa/inet.h>
205#endif
206
207#ifdef HAVE_NET_IF_H
208#include <net/if.h>
209#endif
210
211#ifdef TARGET_NETBSD
212#include <net/if_tap.h>
213#endif
214
215#if defined(TARGET_LINUX) || defined(TARGET_ANDROID)
216
217#define EXTENDED_SOCKET_ERROR_CAPABILITY 1
218
219#ifdef TARGET_LINUX
220#define ENABLE_FEATURE_TUN_PERSIST
221#endif
222
223#include <linux/if_tun.h>
224#include <linux/sockios.h>
225
226#ifdef HAVE_NETINET_IP_H
227#include <netinet/ip.h>
228#endif
229
230#ifdef HAVE_NETINET_TCP_H
231#include <netinet/tcp.h>
232#endif
233
234#endif /* TARGET_LINUX */
235
236#ifdef TARGET_SOLARIS
237
238#ifdef HAVE_STROPTS_H
239#include <stropts.h>
240#undef S_ERROR
241#endif
242
243#ifdef HAVE_NET_IF_TUN_H
244#include <net/if_tun.h>
245#endif
246
247#ifdef HAVE_SYS_SOCKIO_H
248#include <sys/sockio.h>
249#endif
250
251#ifdef HAVE_NETINET_IP_H
252#include <netinet/ip.h>
253#endif
254
255#ifdef HAVE_NETINET_TCP_H
256#include <netinet/tcp.h>
257#endif
258
259#endif /* TARGET_SOLARIS */
260
261#ifdef TARGET_OPENBSD
262
263#ifdef HAVE_SYS_UIO_H
264#include <sys/uio.h>
265#endif
266
267#ifdef HAVE_NETINET_IP_H
268#include <netinet/ip.h>
269#endif
270
271#ifdef HAVE_NETINET_TCP_H
272#include <netinet/tcp.h>
273#endif
274
275#ifdef HAVE_NET_IF_TUN_H
276#include <net/if_tun.h>
277#endif
278
279#endif /* TARGET_OPENBSD */
280
281#ifdef TARGET_FREEBSD
282
283#ifdef HAVE_SYS_UIO_H
284#include <sys/uio.h>
285#endif
286
287#ifdef HAVE_NETINET_IP_H
288#include <netinet/ip.h>
289#endif
290
291#ifdef HAVE_NETINET_TCP_H
292#include <netinet/tcp.h>
293#endif
294
295#ifdef HAVE_NET_IF_TUN_H
296#include <net/if_tun.h>
297#endif
298
299#endif /* TARGET_FREEBSD */
300
301#ifdef TARGET_NETBSD
302
303#ifdef HAVE_NET_IF_TUN_H
304#include <net/if_tun.h>
305#endif
306
307#ifdef HAVE_NETINET_TCP_H
308#include <netinet/tcp.h>
309#endif
310
311#endif /* TARGET_NETBSD */
312
313#ifdef TARGET_DRAGONFLY
314
315#ifdef HAVE_SYS_UIO_H
316#include <sys/uio.h>
317#endif
318
319#ifdef HAVE_NETINET_IP_H
320#include <netinet/ip.h>
321#endif
322
323#ifdef HAVE_NET_TUN_IF_TUN_H
324#include <net/tun/if_tun.h>
325#endif
326
327#endif /* TARGET_DRAGONFLY */
328
329#ifdef TARGET_DARWIN
330
331#ifdef HAVE_NETINET_TCP_H
332#include <netinet/tcp.h>
333#endif
334
335#endif /* TARGET_DARWIN */
336
337#ifdef _WIN32
338/* Missing declarations for MinGW 32. */
339#if defined(__MINGW32__)
340typedef int MIB_TCP_STATE;
341#endif
342#include <naptypes.h>
343#include <ntddndis.h>
344#include <iphlpapi.h>
345#include <wininet.h>
346#include <shellapi.h>
347#include <io.h>
348
349/* The following two headers are needed of PF_INET6 */
350#include <winsock2.h>
351#include <ws2tcpip.h>
352#endif
353
354#ifdef HAVE_SYS_MMAN_H
355#ifdef TARGET_DARWIN
356#define _P1003_1B_VISIBLE
357#endif /* TARGET_DARWIN */
358#include <sys/mman.h>
359#endif
360
361#ifndef _WIN32
362#include <sys/utsname.h>
363#endif
364
365/*
366 * Pedantic mode is meant to accomplish lint-style program checking,
367 * not to build a working executable.
368 */
369#ifdef PEDANTIC
370#undef HAVE_CPP_VARARG_MACRO_GCC
371#undef HAVE_CPP_VARARG_MACRO_ISO
372#undef inline
373#define inline
374#endif
375
376/*
377 * Do we have the capability to support the --passtos option?
378 */
379#if defined(IPPROTO_IP) && defined(IP_TOS)
380#define PASSTOS_CAPABILITY 1
381#else
382#define PASSTOS_CAPABILITY 0
383#endif
384
385/*
386 * Does this platform support linux-style IP_PKTINFO
387 * or bsd-style IP_RECVDSTADDR ?
388 */
389#if ((defined(HAVE_IN_PKTINFO) && defined(IP_PKTINFO)) || defined(IP_RECVDSTADDR)) \
390 && defined(HAVE_MSGHDR) && defined(HAVE_CMSGHDR) && defined(CMSG_FIRSTHDR) \
391 && defined(CMSG_NXTHDR) && defined(HAVE_RECVMSG) && defined(HAVE_SENDMSG)
392#define ENABLE_IP_PKTINFO 1
393#else
394#define ENABLE_IP_PKTINFO 0
395#endif
396
397/*
398 * Does this platform define SOL_IP
399 * or only bsd-style IPPROTO_IP ?
400 */
401#ifndef SOL_IP
402#define SOL_IP IPPROTO_IP
403#endif
404
405/*
406 * Define type sa_family_t if it isn't defined in the socket headers
407 */
408#ifndef HAVE_SA_FAMILY_T
409typedef unsigned short sa_family_t;
410#endif
411
412/*
413 * Do we have a syslog capability?
414 */
415#if defined(HAVE_OPENLOG) && defined(HAVE_SYSLOG)
416#define SYSLOG_CAPABILITY 1
417#else
418#define SYSLOG_CAPABILITY 0
419#endif
420
421/*
422 * Does this OS draw a distinction between binary and ascii files?
423 */
424#ifndef O_BINARY
425#define O_BINARY 0
426#endif
427
428/*
429 * Directory separation char
430 */
431#ifdef _WIN32
432#define PATH_SEPARATOR '\\'
433#define PATH_SEPARATOR_STR "\\"
434#else
435#define PATH_SEPARATOR '/'
436#define PATH_SEPARATOR_STR "/"
437#endif
438
439/*
440 * Our socket descriptor type.
441 */
442#ifdef _WIN32
443#define SOCKET_UNDEFINED (INVALID_SOCKET)
444#define SOCKET_PRINTF "%" PRIxPTR
445typedef SOCKET socket_descriptor_t;
446#else
447#define SOCKET_UNDEFINED (-1)
448#define SOCKET_PRINTF "%d"
449typedef int socket_descriptor_t;
450#endif
451
452static inline int
454{
455 return sd != SOCKET_UNDEFINED;
456}
457
458/*
459 * Should we enable the use of execve() for calling subprocesses,
460 * instead of system()?
461 */
462#if defined(HAVE_EXECVE) && defined(HAVE_FORK)
463#define ENABLE_FEATURE_EXECVE
464#endif
465
466/*
467 * HTTPS port sharing capability
468 */
469#if defined(ENABLE_PORT_SHARE) && defined(SCM_RIGHTS) && defined(HAVE_MSGHDR) \
470 && defined(HAVE_CMSGHDR) && defined(CMSG_FIRSTHDR) && defined(CMSG_NXTHDR) \
471 && defined(HAVE_RECVMSG) && defined(HAVE_SENDMSG)
472#define PORT_SHARE 1
473#else
474#define PORT_SHARE 0
475#endif
476
477#ifdef ENABLE_CRYPTO_MBEDTLS
478#define ENABLE_PREDICTION_RESISTANCE
479#endif /* ENABLE_CRYPTO_MBEDTLS */
480
481/*
482 * Do we support Unix domain sockets?
483 */
484#if defined(PF_UNIX) && !defined(_WIN32)
485#define UNIX_SOCK_SUPPORT 1
486#else
487#define UNIX_SOCK_SUPPORT 0
488#endif
489
490/*
491 * Should we include NTLM proxy functionality
492 */
493#ifdef ENABLE_NTLM
494#define NTLM 1
495#endif
496
497/*
498 * Should we include proxy digest auth functionality
499 */
500#define PROXY_DIGEST_AUTH 1
501
502/*
503 * Do we have CryptoAPI capability?
504 */
505#if defined(_WIN32) && defined(ENABLE_CRYPTO_OPENSSL) && !defined(ENABLE_CRYPTO_WOLFSSL)
506#define ENABLE_CRYPTOAPI
507#endif
508
509/*
510 * Is poll available on this platform?
511 * (Note: on win32 select is faster than poll and we avoid
512 * using poll there)
513 */
514#if defined(HAVE_POLL_H) || !defined(_WIN32)
515#define POLL 1
516#else
517#define POLL 0
518#endif
519
520/*
521 * Is epoll available on this platform?
522 */
523#if defined(HAVE_EPOLL_CREATE) && defined(HAVE_SYS_EPOLL_H)
524#define EPOLL 1
525#else
526#define EPOLL 0
527#endif
528
529/*
530 * Compression support
531 */
532#if defined(ENABLE_LZO) || defined(ENABLE_LZ4) || defined(ENABLE_COMP_STUB)
533#define USE_COMP
534#endif
535
536#ifdef _MSC_VER
537#ifndef PATH_MAX
538#define PATH_MAX MAX_PATH
539#endif
540#endif
541
542#endif /* ifndef SYSHEAD_H */
unsigned short sa_family_t
Definition syshead.h:409
#define SOCKET_UNDEFINED
Definition syshead.h:443
SOCKET socket_descriptor_t
Definition syshead.h:445
uint16_t in_port_t
Definition syshead.h:53
uint32_t in_addr_t
Definition syshead.h:52
static int socket_defined(const socket_descriptor_t sd)
Definition syshead.h:453