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