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