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