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-2018 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 #define sleep(x) Sleep((x)*1000)
43 #define random rand
44 #define srandom srand
45 #endif
46 
47 #ifdef _MSC_VER /* Visual Studio */
48 #define __func__ __FUNCTION__
49 #define __attribute__(x)
50 #include <inttypes.h>
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 #ifdef HAVE_TIME_H
81 #include <time.h>
82 #endif
83 
84 #ifdef HAVE_SYS_SOCKET_H
85 #include <sys/socket.h>
86 #endif
87 
88 #ifdef HAVE_SYS_UN_H
89 #include <sys/un.h>
90 #endif
91 
92 #ifdef HAVE_SYS_IOCTL_H
93 #include <sys/ioctl.h>
94 #endif
95 
96 #ifdef HAVE_SYS_STAT_H
97 #include <sys/stat.h>
98 #endif
99 
100 #ifdef HAVE_FCNTL_H
101 #include <fcntl.h>
102 #endif
103 
104 #ifdef HAVE_DIRECT_H
105 #include <direct.h>
106 #endif
107 
108 #ifdef HAVE_IO_H
109 #include <io.h>
110 #endif
111 
112 #ifdef HAVE_SYS_FILE_H
113 #include <sys/file.h>
114 #endif
115 
116 #ifdef HAVE_STDLIB_H
117 #include <stdlib.h>
118 #endif
119 
120 #ifdef HAVE_INTTYPES_H
121 #include <inttypes.h>
122 #elif defined(HAVE_STDINT_H)
123 #include <stdint.h>
124 #endif
125 
126 #ifdef HAVE_STDARG_H
127 #include <stdarg.h>
128 #endif
129 
130 #ifdef HAVE_UNISTD_H
131 #include <unistd.h>
132 #endif
133 
134 #ifdef HAVE_SIGNAL_H
135 #include <signal.h>
136 #endif
137 
138 #ifdef HAVE_LIMITS_H
139 #include <limits.h>
140 #endif
141 
142 #ifdef HAVE_STDIO_H
143 #include <stdio.h>
144 #endif
145 
146 #ifdef HAVE_CTYPE_H
147 #include <ctype.h>
148 #endif
149 
150 #ifdef HAVE_ERRNO_H
151 #include <errno.h>
152 #endif
153 
154 #ifdef HAVE_ERR_H
155 #include <err.h>
156 #endif
157 
158 #ifdef HAVE_SYSLOG_H
159 #include <syslog.h>
160 #endif
161 
162 #ifdef HAVE_PWD_H
163 #include <pwd.h>
164 #endif
165 
166 #ifdef HAVE_GRP_H
167 #include <grp.h>
168 #endif
169 
170 #ifdef HAVE_NETDB_H
171 #include <netdb.h>
172 #endif
173 
174 #ifdef HAVE_NETINET_IN_H
175 #include <netinet/in.h>
176 #endif
177 
178 #ifdef HAVE_RESOLV_H
179 #include <resolv.h>
180 #endif
181 
182 #ifdef HAVE_SYS_POLL_H
183 #include <sys/poll.h>
184 #endif
185 
186 #ifdef HAVE_SYS_EPOLL_H
187 #include <sys/epoll.h>
188 #endif
189 
190 #ifdef ENABLE_SELINUX
191 #include <selinux/selinux.h>
192 #endif
193 
194 #if defined(HAVE_LIBGEN_H)
195 #include <libgen.h>
196 #endif
197 
198 #ifdef TARGET_SOLARIS
199 #ifdef HAVE_STRINGS_H
200 #include <strings.h>
201 #endif
202 #else
203 #ifdef HAVE_STRING_H
204 #include <string.h>
205 #endif
206 #endif
207 
208 #ifdef HAVE_ARPA_INET_H
209 #include <arpa/inet.h>
210 #endif
211 
212 #ifdef HAVE_NET_IF_H
213 #include <net/if.h>
214 #endif
215 
216 #ifdef TARGET_NETBSD
217 #include <net/if_tap.h>
218 #endif
219 
220 #if defined(TARGET_LINUX) || defined (TARGET_ANDROID)
221 
222 #ifdef HAVE_LINUX_IF_TUN_H
223 #include <linux/if_tun.h>
224 #endif
225 
226 #ifdef HAVE_NETINET_IP_H
227 #include <netinet/ip.h>
228 #endif
229 
230 #ifdef HAVE_LINUX_SOCKIOS_H
231 #include <linux/sockios.h>
232 #endif
233 
234 #ifdef HAVE_LINUX_TYPES_H
235 #include <linux/types.h>
236 #endif
237 
238 #ifdef HAVE_LINUX_ERRQUEUE_H
239 #include <linux/errqueue.h>
240 #endif
241 
242 #ifdef HAVE_NETINET_TCP_H
243 #include <netinet/tcp.h>
244 #endif
245 
246 #endif /* TARGET_LINUX */
247 
248 #ifdef TARGET_SOLARIS
249 
250 #ifdef HAVE_STROPTS_H
251 #include <stropts.h>
252 #undef S_ERROR
253 #endif
254 
255 #ifdef HAVE_NET_IF_TUN_H
256 #include <net/if_tun.h>
257 #endif
258 
259 #ifdef HAVE_SYS_SOCKIO_H
260 #include <sys/sockio.h>
261 #endif
262 
263 #ifdef HAVE_NETINET_IN_SYSTM_H
264 #include <netinet/in_systm.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 #endif /* TARGET_SOLARIS */
276 
277 #ifdef TARGET_OPENBSD
278 
279 #ifdef HAVE_SYS_UIO_H
280 #include <sys/uio.h>
281 #endif
282 
283 #ifdef HAVE_NETINET_IN_SYSTM_H
284 #include <netinet/in_systm.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_OPENBSD */
300 
301 #ifdef TARGET_FREEBSD
302 
303 #ifdef HAVE_SYS_UIO_H
304 #include <sys/uio.h>
305 #endif
306 
307 #ifdef HAVE_NETINET_IN_SYSTM_H
308 #include <netinet/in_systm.h>
309 #endif
310 
311 #ifdef HAVE_NETINET_IP_H
312 #include <netinet/ip.h>
313 #endif
314 
315 #ifdef HAVE_NETINET_TCP_H
316 #include <netinet/tcp.h>
317 #endif
318 
319 #ifdef HAVE_NET_IF_TUN_H
320 #include <net/if_tun.h>
321 #endif
322 
323 #endif /* TARGET_FREEBSD */
324 
325 #ifdef TARGET_NETBSD
326 
327 #ifdef HAVE_NET_IF_TUN_H
328 #include <net/if_tun.h>
329 #endif
330 
331 #ifdef HAVE_NETINET_TCP_H
332 #include <netinet/tcp.h>
333 #endif
334 
335 #endif /* TARGET_NETBSD */
336 
337 #ifdef TARGET_DRAGONFLY
338 
339 #ifdef HAVE_SYS_UIO_H
340 #include <sys/uio.h>
341 #endif
342 
343 #ifdef HAVE_NETINET_IN_SYSTM_H
344 #include <netinet/in_systm.h>
345 #endif
346 
347 #ifdef HAVE_NETINET_IP_H
348 #include <netinet/ip.h>
349 #endif
350 
351 #ifdef HAVE_NET_TUN_IF_TUN_H
352 #include <net/tun/if_tun.h>
353 #endif
354 
355 #endif /* TARGET_DRAGONFLY */
356 
357 #ifdef TARGET_DARWIN
358 
359 #ifdef HAVE_NETINET_TCP_H
360 #include <netinet/tcp.h>
361 #endif
362 
363 #endif /* TARGET_DARWIN */
364 
365 #ifdef _WIN32
366 /* Missing declarations for MinGW 32. */
367 /* #if !defined(__MINGW64_VERSION_MAJOR) || __MINGW64_VERSION_MAJOR < 2 */
368 typedef int MIB_TCP_STATE;
369 /* #endif */
370 #include <naptypes.h>
371 #include <ntddndis.h>
372 #include <iphlpapi.h>
373 #include <wininet.h>
374 #include <shellapi.h>
375 /* The following two headers are needed of PF_INET6 */
376 #include <winsock2.h>
377 #include <ws2tcpip.h>
378 #endif
379 
380 #ifdef HAVE_SYS_MMAN_H
381 #ifdef TARGET_DARWIN
382 #define _P1003_1B_VISIBLE
383 #endif /* TARGET_DARWIN */
384 #include <sys/mman.h>
385 #endif
386 
387 /*
388  * Pedantic mode is meant to accomplish lint-style program checking,
389  * not to build a working executable.
390  */
391 #ifdef PEDANTIC
392 #undef HAVE_CPP_VARARG_MACRO_GCC
393 #undef HAVE_CPP_VARARG_MACRO_ISO
394 #undef EMPTY_ARRAY_SIZE
395 #define EMPTY_ARRAY_SIZE 1
396 #undef inline
397 #define inline
398 #endif
399 
400 /*
401  * Do we have the capability to support the --passtos option?
402  */
403 #if defined(IPPROTO_IP) && defined(IP_TOS) && defined(HAVE_SETSOCKOPT)
404 #define PASSTOS_CAPABILITY 1
405 #else
406 #define PASSTOS_CAPABILITY 0
407 #endif
408 
409 /*
410  * Do we have nanoseconds gettimeofday?
411  */
412 #if defined(HAVE_GETTIMEOFDAY) || defined(_WIN32)
413 #define HAVE_GETTIMEOFDAY_NANOSECONDS 1
414 #endif
415 
416 /*
417  * Do we have the capability to report extended socket errors?
418  */
419 #if defined(HAVE_LINUX_TYPES_H) && defined(HAVE_LINUX_ERRQUEUE_H) && defined(HAVE_SOCK_EXTENDED_ERR) && defined(HAVE_MSGHDR) && defined(HAVE_CMSGHDR) && defined(CMSG_FIRSTHDR) && defined(CMSG_NXTHDR) && defined(IP_RECVERR) && defined(MSG_ERRQUEUE) && defined(SOL_IP) && defined(HAVE_IOVEC)
420 #define EXTENDED_SOCKET_ERROR_CAPABILITY 1
421 #else
422 #define EXTENDED_SOCKET_ERROR_CAPABILITY 0
423 #endif
424 
425 /*
426  * Does this platform support linux-style IP_PKTINFO
427  * or bsd-style IP_RECVDSTADDR ?
428  */
429 #if defined(ENABLE_MULTIHOME) && ((defined(HAVE_IN_PKTINFO) && defined(IP_PKTINFO)) || defined(IP_RECVDSTADDR)) && defined(HAVE_MSGHDR) && defined(HAVE_CMSGHDR) && defined(HAVE_IOVEC) && defined(CMSG_FIRSTHDR) && defined(CMSG_NXTHDR) && defined(HAVE_RECVMSG) && defined(HAVE_SENDMSG)
430 #define ENABLE_IP_PKTINFO 1
431 #else
432 #define ENABLE_IP_PKTINFO 0
433 #endif
434 
435 /*
436  * Does this platform define SOL_IP
437  * or only bsd-style IPPROTO_IP ?
438  */
439 #ifndef SOL_IP
440 #define SOL_IP IPPROTO_IP
441 #endif
442 
443 /*
444  * Define type sa_family_t if it isn't defined in the socket headers
445  */
446 #ifndef HAVE_SA_FAMILY_T
447 typedef unsigned short sa_family_t;
448 #endif
449 
450 /*
451  * Disable ESEC
452  */
453 #if 0
454 #undef EXTENDED_SOCKET_ERROR_CAPABILITY
455 #define EXTENDED_SOCKET_ERROR_CAPABILITY 0
456 #endif
457 
458 /*
459  * Do we have a syslog capability?
460  */
461 #if defined(HAVE_OPENLOG) && defined(HAVE_SYSLOG)
462 #define SYSLOG_CAPABILITY 1
463 #else
464 #define SYSLOG_CAPABILITY 0
465 #endif
466 
467 /*
468  * Does this OS draw a distinction between binary and ascii files?
469  */
470 #ifndef O_BINARY
471 #define O_BINARY 0
472 #endif
473 
474 /*
475  * Directory separation char
476  */
477 #ifdef _WIN32
478 #define OS_SPECIFIC_DIRSEP '\\'
479 #else
480 #define OS_SPECIFIC_DIRSEP '/'
481 #endif
482 
483 /*
484  * Our socket descriptor type.
485  */
486 #ifdef _WIN32
487 #define SOCKET_UNDEFINED (INVALID_SOCKET)
488 typedef SOCKET socket_descriptor_t;
489 #else
490 #define SOCKET_UNDEFINED (-1)
491 typedef int socket_descriptor_t;
492 #endif
493 
494 static inline int
496 {
497  return sd != SOCKET_UNDEFINED;
498 }
499 
500 /*
501  * Should statistics counters be 64 bits?
502  */
503 #define USE_64_BIT_COUNTERS
504 
505 /*
506  * Should we enable the use of execve() for calling subprocesses,
507  * instead of system()?
508  */
509 #if defined(HAVE_EXECVE) && defined(HAVE_FORK)
510 #define ENABLE_FEATURE_EXECVE
511 #endif
512 
513 /*
514  * Do we have point-to-multipoint capability?
515  */
516 
517 #if defined(HAVE_GETTIMEOFDAY_NANOSECONDS)
518 #define P2MP 1
519 #else
520 #define P2MP 0
521 #endif
522 
523 #if P2MP && !defined(ENABLE_CLIENT_ONLY)
524 #define P2MP_SERVER 1
525 #else
526 #define P2MP_SERVER 0
527 #endif
528 
529 /*
530  * HTTPS port sharing capability
531  */
532 #if defined(ENABLE_PORT_SHARE) && P2MP_SERVER && defined(SCM_RIGHTS) && defined(HAVE_MSGHDR) && defined(HAVE_CMSGHDR) && defined(HAVE_IOVEC) && defined(CMSG_FIRSTHDR) && defined(CMSG_NXTHDR) && defined(HAVE_RECVMSG) && defined(HAVE_SENDMSG)
533 #define PORT_SHARE 1
534 #else
535 #define PORT_SHARE 0
536 #endif
537 
538 /*
539  * Enable deferred authentication?
540  */
541 #if defined(ENABLE_DEF_AUTH) && P2MP_SERVER && defined(ENABLE_PLUGIN)
542 #define PLUGIN_DEF_AUTH
543 #endif
544 #if defined(ENABLE_DEF_AUTH) && P2MP_SERVER && defined(ENABLE_MANAGEMENT)
545 #define MANAGEMENT_DEF_AUTH
546 #endif
547 #if !defined(PLUGIN_DEF_AUTH) && !defined(MANAGEMENT_DEF_AUTH)
548 #undef ENABLE_DEF_AUTH
549 #endif
550 
551 /* Enable mbed TLS RNG prediction resistance support */
552 #ifdef ENABLE_CRYPTO_MBEDTLS
553 #define ENABLE_PREDICTION_RESISTANCE
554 #endif /* ENABLE_CRYPTO_MBEDTLS */
555 
556 /*
557  * Enable packet filter?
558  */
559 #if defined(ENABLE_PF) && P2MP_SERVER && defined(ENABLE_PLUGIN) && defined(HAVE_STAT)
560 #define PLUGIN_PF
561 #endif
562 #if defined(ENABLE_PF) && P2MP_SERVER && defined(MANAGEMENT_DEF_AUTH)
563 #define MANAGEMENT_PF
564 #endif
565 #if !defined(PLUGIN_PF) && !defined(MANAGEMENT_PF)
566 #undef ENABLE_PF
567 #endif
568 
569 /*
570  * Do we support Unix domain sockets?
571  */
572 #if defined(PF_UNIX) && !defined(_WIN32)
573 #define UNIX_SOCK_SUPPORT 1
574 #else
575 #define UNIX_SOCK_SUPPORT 0
576 #endif
577 
578 /*
579  * Should we include OCC (options consistency check) code?
580  */
581 #define ENABLE_OCC
582 
583 /*
584  * Should we include NTLM proxy functionality
585  */
586 #define NTLM 1
587 
588 /*
589  * Should we include proxy digest auth functionality
590  */
591 #define PROXY_DIGEST_AUTH 1
592 
593 /*
594  * Do we have CryptoAPI capability?
595  */
596 #if defined(_WIN32) && defined(ENABLE_CRYPTO_OPENSSL)
597 #define ENABLE_CRYPTOAPI
598 #endif
599 
600 /*
601  * Is poll available on this platform?
602  */
603 #if defined(HAVE_POLL) && defined(HAVE_SYS_POLL_H)
604 #define POLL 1
605 #else
606 #define POLL 0
607 #endif
608 
609 /*
610  * Is epoll available on this platform?
611  */
612 #if defined(HAVE_EPOLL_CREATE) && defined(HAVE_SYS_EPOLL_H)
613 #define EPOLL 1
614 #else
615 #define EPOLL 0
616 #endif
617 
618 /* Disable EPOLL */
619 #if 0
620 #undef EPOLL
621 #define EPOLL 0
622 #endif
623 
624 /*
625  * Reduce sensitivity to system clock instability
626  * and backtracks.
627  */
628 #if defined(HAVE_GETTIMEOFDAY_NANOSECONDS)
629 #define TIME_BACKTRACK_PROTECTION 1
630 #endif
631 
632 /*
633  * Enable traffic shaper.
634  */
635 #if defined(HAVE_GETTIMEOFDAY_NANOSECONDS)
636 #define ENABLE_FEATURE_SHAPER 1
637 #endif
638 
639 /*
640  * Is non-blocking connect() supported?
641  */
642 #if defined(HAVE_GETSOCKOPT) && defined(SOL_SOCKET) && defined(SO_ERROR) && defined(EINPROGRESS) && defined(ETIMEDOUT)
643 #define CONNECT_NONBLOCK
644 #endif
645 
646 /*
647  * Compression support
648  */
649 #if defined(ENABLE_LZO) || defined(ENABLE_LZ4) \
650  || defined(ENABLE_COMP_STUB)
651 #define USE_COMP
652 #endif
653 
654 /*
655  * Enable --memstats option
656  */
657 #ifdef TARGET_LINUX
658 #define ENABLE_MEMSTATS
659 #endif
660 
661 #endif /* ifndef SYSHEAD_H */
unsigned short sa_family_t
Definition: syshead.h:447
static int socket_defined(const socket_descriptor_t sd)
Definition: syshead.h:495
int MIB_TCP_STATE
Definition: syshead.h:368
#define SOCKET_UNDEFINED
Definition: syshead.h:487
SOCKET socket_descriptor_t
Definition: syshead.h:488