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-2023 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 #ifdef HAVE_ARPA_INET_H
170 #include <arpa/inet.h>
171 #endif
172 
173 #ifdef HAVE_NET_IF_H
174 #include <net/if.h>
175 #endif
176 
177 #ifdef TARGET_NETBSD
178 #include <net/if_tap.h>
179 #endif
180 
181 #if defined(TARGET_LINUX) || defined (TARGET_ANDROID)
182 
183 #ifdef HAVE_LINUX_IF_TUN_H
184 #include <linux/if_tun.h>
185 #endif
186 
187 #ifdef HAVE_NETINET_IP_H
188 #include <netinet/ip.h>
189 #endif
190 
191 #ifdef HAVE_LINUX_SOCKIOS_H
192 #include <linux/sockios.h>
193 #endif
194 
195 #ifdef HAVE_LINUX_TYPES_H
196 #include <linux/types.h>
197 #endif
198 
199 #ifdef HAVE_LINUX_ERRQUEUE_H
200 #include <linux/errqueue.h>
201 #endif
202 
203 #ifdef HAVE_NETINET_TCP_H
204 #include <netinet/tcp.h>
205 #endif
206 
207 #endif /* TARGET_LINUX */
208 
209 #ifdef TARGET_SOLARIS
210 
211 #ifdef HAVE_STROPTS_H
212 #include <stropts.h>
213 #undef S_ERROR
214 #endif
215 
216 #ifdef HAVE_NET_IF_TUN_H
217 #include <net/if_tun.h>
218 #endif
219 
220 #ifdef HAVE_SYS_SOCKIO_H
221 #include <sys/sockio.h>
222 #endif
223 
224 #ifdef HAVE_NETINET_IP_H
225 #include <netinet/ip.h>
226 #endif
227 
228 #ifdef HAVE_NETINET_TCP_H
229 #include <netinet/tcp.h>
230 #endif
231 
232 #endif /* TARGET_SOLARIS */
233 
234 #ifdef TARGET_OPENBSD
235 
236 #ifdef HAVE_SYS_UIO_H
237 #include <sys/uio.h>
238 #endif
239 
240 #ifdef HAVE_NETINET_IP_H
241 #include <netinet/ip.h>
242 #endif
243 
244 #ifdef HAVE_NETINET_TCP_H
245 #include <netinet/tcp.h>
246 #endif
247 
248 #ifdef HAVE_NET_IF_TUN_H
249 #include <net/if_tun.h>
250 #endif
251 
252 #endif /* TARGET_OPENBSD */
253 
254 #ifdef TARGET_FREEBSD
255 
256 #ifdef HAVE_SYS_UIO_H
257 #include <sys/uio.h>
258 #endif
259 
260 #ifdef HAVE_NETINET_IP_H
261 #include <netinet/ip.h>
262 #endif
263 
264 #ifdef HAVE_NETINET_TCP_H
265 #include <netinet/tcp.h>
266 #endif
267 
268 #ifdef HAVE_NET_IF_TUN_H
269 #include <net/if_tun.h>
270 #endif
271 
272 #endif /* TARGET_FREEBSD */
273 
274 #ifdef TARGET_NETBSD
275 
276 #ifdef HAVE_NET_IF_TUN_H
277 #include <net/if_tun.h>
278 #endif
279 
280 #ifdef HAVE_NETINET_TCP_H
281 #include <netinet/tcp.h>
282 #endif
283 
284 #endif /* TARGET_NETBSD */
285 
286 #ifdef TARGET_DRAGONFLY
287 
288 #ifdef HAVE_SYS_UIO_H
289 #include <sys/uio.h>
290 #endif
291 
292 #ifdef HAVE_NETINET_IP_H
293 #include <netinet/ip.h>
294 #endif
295 
296 #ifdef HAVE_NET_TUN_IF_TUN_H
297 #include <net/tun/if_tun.h>
298 #endif
299 
300 #endif /* TARGET_DRAGONFLY */
301 
302 #ifdef TARGET_DARWIN
303 
304 #ifdef HAVE_NETINET_TCP_H
305 #include <netinet/tcp.h>
306 #endif
307 
308 #endif /* TARGET_DARWIN */
309 
310 #ifdef _WIN32
311 /* Missing declarations for MinGW 32. */
312 #if defined(__MINGW32__)
313 typedef int MIB_TCP_STATE;
314 #endif
315 #include <naptypes.h>
316 #include <ntddndis.h>
317 #include <iphlpapi.h>
318 #include <wininet.h>
319 #include <shellapi.h>
320 #include <io.h>
321 
322 /* The following two headers are needed of PF_INET6 */
323 #include <winsock2.h>
324 #include <ws2tcpip.h>
325 #endif
326 
327 #ifdef HAVE_SYS_MMAN_H
328 #ifdef TARGET_DARWIN
329 #define _P1003_1B_VISIBLE
330 #endif /* TARGET_DARWIN */
331 #include <sys/mman.h>
332 #endif
333 
334 /*
335  * Pedantic mode is meant to accomplish lint-style program checking,
336  * not to build a working executable.
337  */
338 #ifdef PEDANTIC
339 #undef HAVE_CPP_VARARG_MACRO_GCC
340 #undef HAVE_CPP_VARARG_MACRO_ISO
341 #undef inline
342 #define inline
343 #endif
344 
345 /*
346  * Do we have the capability to support the --passtos option?
347  */
348 #if defined(IPPROTO_IP) && defined(IP_TOS)
349 #define PASSTOS_CAPABILITY 1
350 #else
351 #define PASSTOS_CAPABILITY 0
352 #endif
353 
354 /*
355  * Do we have the capability to report extended socket errors?
356  */
357 #if defined(HAVE_LINUX_TYPES_H) && defined(HAVE_LINUX_ERRQUEUE_H)
358 #define EXTENDED_SOCKET_ERROR_CAPABILITY 1
359 #else
360 #define EXTENDED_SOCKET_ERROR_CAPABILITY 0
361 #endif
362 
363 /*
364  * Does this platform support linux-style IP_PKTINFO
365  * or bsd-style IP_RECVDSTADDR ?
366  */
367 #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)
368 #define ENABLE_IP_PKTINFO 1
369 #else
370 #define ENABLE_IP_PKTINFO 0
371 #endif
372 
373 /*
374  * Does this platform define SOL_IP
375  * or only bsd-style IPPROTO_IP ?
376  */
377 #ifndef SOL_IP
378 #define SOL_IP IPPROTO_IP
379 #endif
380 
381 /*
382  * Define type sa_family_t if it isn't defined in the socket headers
383  */
384 #ifndef HAVE_SA_FAMILY_T
385 typedef unsigned short sa_family_t;
386 #endif
387 
388 /*
389  * Disable ESEC
390  */
391 #if 0
392 #undef EXTENDED_SOCKET_ERROR_CAPABILITY
393 #define EXTENDED_SOCKET_ERROR_CAPABILITY 0
394 #endif
395 
396 /*
397  * Do we have a syslog capability?
398  */
399 #if defined(HAVE_OPENLOG) && defined(HAVE_SYSLOG)
400 #define SYSLOG_CAPABILITY 1
401 #else
402 #define SYSLOG_CAPABILITY 0
403 #endif
404 
405 /*
406  * Does this OS draw a distinction between binary and ascii files?
407  */
408 #ifndef O_BINARY
409 #define O_BINARY 0
410 #endif
411 
412 /*
413  * Directory separation char
414  */
415 #ifdef _WIN32
416 #define PATH_SEPARATOR '\\'
417 #define PATH_SEPARATOR_STR "\\"
418 #else
419 #define PATH_SEPARATOR '/'
420 #define PATH_SEPARATOR_STR "/"
421 #endif
422 
423 /*
424  * Our socket descriptor type.
425  */
426 #ifdef _WIN32
427 #define SOCKET_UNDEFINED (INVALID_SOCKET)
428 #define SOCKET_PRINTF "%" PRIxPTR
429 typedef SOCKET socket_descriptor_t;
430 #else
431 #define SOCKET_UNDEFINED (-1)
432 #define SOCKET_PRINTF "%d"
433 typedef int socket_descriptor_t;
434 #endif
435 
436 static inline int
438 {
439  return sd != SOCKET_UNDEFINED;
440 }
441 
442 /*
443  * Should we enable the use of execve() for calling subprocesses,
444  * instead of system()?
445  */
446 #if defined(HAVE_EXECVE) && defined(HAVE_FORK)
447 #define ENABLE_FEATURE_EXECVE
448 #endif
449 
450 /*
451  * HTTPS port sharing capability
452  */
453 #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)
454 #define PORT_SHARE 1
455 #else
456 #define PORT_SHARE 0
457 #endif
458 
459 #ifdef ENABLE_CRYPTO_MBEDTLS
460 #define ENABLE_PREDICTION_RESISTANCE
461 #endif /* ENABLE_CRYPTO_MBEDTLS */
462 
463 /*
464  * Do we support Unix domain sockets?
465  */
466 #if defined(PF_UNIX) && !defined(_WIN32)
467 #define UNIX_SOCK_SUPPORT 1
468 #else
469 #define UNIX_SOCK_SUPPORT 0
470 #endif
471 
472 /*
473  * Should we include NTLM proxy functionality
474  */
475 #ifdef ENABLE_NTLM
476 #define NTLM 1
477 #endif
478 
479 /*
480  * Should we include proxy digest auth functionality
481  */
482 #define PROXY_DIGEST_AUTH 1
483 
484 /*
485  * Do we have CryptoAPI capability?
486  */
487 #if defined(_WIN32) && defined(ENABLE_CRYPTO_OPENSSL) \
488  && !defined(ENABLE_CRYPTO_WOLFSSL)
489 #define ENABLE_CRYPTOAPI
490 #endif
491 
492 /*
493  * Is poll available on this platform?
494  * (Note: on win32 select is faster than poll and we avoid
495  * using poll there)
496  */
497 #if defined(HAVE_POLL_H) || !defined(_WIN32)
498 #define POLL 1
499 #else
500 #define POLL 0
501 #endif
502 
503 /*
504  * Is epoll available on this platform?
505  */
506 #if defined(HAVE_EPOLL_CREATE) && defined(HAVE_SYS_EPOLL_H)
507 #define EPOLL 1
508 #else
509 #define EPOLL 0
510 #endif
511 
512 /*
513  * Compression support
514  */
515 #if defined(ENABLE_LZO) || defined(ENABLE_LZ4) \
516  || defined(ENABLE_COMP_STUB)
517 #define USE_COMP
518 #endif
519 
520 /*
521  * Enable --memstats option
522  */
523 #ifdef TARGET_LINUX
524 #define ENABLE_MEMSTATS
525 #endif
526 
527 #ifdef _MSC_VER
528 #ifndef PATH_MAX
529 #define PATH_MAX MAX_PATH
530 #endif
531 #endif
532 
533 #endif /* ifndef SYSHEAD_H */
sa_family_t
unsigned short sa_family_t
Definition: syshead.h:385
compat.h
socket_defined
static int socket_defined(const socket_descriptor_t sd)
Definition: syshead.h:437
SOCKET_UNDEFINED
#define SOCKET_UNDEFINED
Definition: syshead.h:427
socket_descriptor_t
SOCKET socket_descriptor_t
Definition: syshead.h:429