OpenVPN
socket.c
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single TCP/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-2026 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#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#include "syshead.h"
28
29#include "socket.h"
30#include "fdmisc.h"
31#include "misc.h"
32#include "gremlin.h"
33#include "plugin.h"
34#include "ps.h"
35#include "run_command.h"
36#include "manage.h"
37#include "misc.h"
38#include "manage.h"
39#include "openvpn.h"
40#include "forward.h"
41
42#include "memdbg.h"
43
44bool
46{
47 for (int i = 0; i < c->c1.link_sockets_num; i++)
48 {
50 {
51 return true;
52 }
53 }
54 return false;
55}
56
57/*
58 * Convert sockflags/getaddr_flags into getaddr_flags
59 */
60static unsigned int
61sf2gaf(const unsigned int getaddr_flags, const unsigned int sockflags)
62{
63 if (sockflags & SF_HOST_RANDOMIZE)
64 {
65 return getaddr_flags | GETADDR_RANDOMIZE;
66 }
67 else
68 {
69 return getaddr_flags;
70 }
71}
72
73/*
74 * Functions related to the translation of DNS names to IP addresses.
75 */
76static int
77get_addr_generic(sa_family_t af, unsigned int flags, const char *hostname, void *network,
78 unsigned int *netbits, int resolve_retry_seconds, struct signal_info *sig_info,
79 msglvl_t msglevel)
80{
81 char *endp, *sep, *var_host = NULL;
82 struct addrinfo *ai = NULL;
83 unsigned long bits;
84 uint8_t max_bits;
85 int ret = -1;
86
87 if (!hostname)
88 {
89 msg(M_NONFATAL, "Can't resolve null hostname!");
90 goto out;
91 }
92
93 /* assign family specific default values */
94 switch (af)
95 {
96 case AF_INET:
97 bits = 0;
98 max_bits = sizeof(in_addr_t) * 8;
99 break;
100
101 case AF_INET6:
102 bits = 64;
103 max_bits = sizeof(struct in6_addr) * 8;
104 break;
105
106 default:
107 msg(M_WARN, "Unsupported AF family passed to getaddrinfo for %s (%d)", hostname, af);
108 goto out;
109 }
110
111 /* we need to modify the hostname received as input, but we don't want to
112 * touch it directly as it might be a constant string.
113 *
114 * Therefore, we clone the string here and free it at the end of the
115 * function */
116 var_host = strdup(hostname);
117 if (!var_host)
118 {
119 msg(M_NONFATAL | M_ERRNO, "Can't allocate hostname buffer for getaddrinfo");
120 goto out;
121 }
122
123 /* check if this hostname has a /bits suffix */
124 sep = strchr(var_host, '/');
125 if (sep)
126 {
127 bits = strtoul(sep + 1, &endp, 10);
128 if ((*endp != '\0') || (bits > max_bits))
129 {
130 msg(msglevel, "IP prefix '%s': invalid '/bits' spec (%s)", hostname, sep + 1);
131 goto out;
132 }
133 *sep = '\0';
134 }
135
136 ret = openvpn_getaddrinfo(flags & ~GETADDR_HOST_ORDER, var_host, NULL, resolve_retry_seconds,
137 sig_info, af, &ai);
138 if ((ret == 0) && network)
139 {
140 struct in6_addr *ip6;
141 in_addr_t *ip4;
142
143 if (af != ai->ai_family)
144 {
145 msg(msglevel, "Can't parse %s as IPv%d address", var_host, (af == AF_INET) ? 4 : 6);
146 ret = -1;
147 goto out;
148 }
149
150 switch (af)
151 {
152 case AF_INET:
153 ip4 = network;
154 *ip4 = ((struct sockaddr_in *)ai->ai_addr)->sin_addr.s_addr;
155
156 if (flags & GETADDR_HOST_ORDER)
157 {
158 *ip4 = ntohl(*ip4);
159 }
160 break;
161
162 case AF_INET6:
163 ip6 = network;
164 *ip6 = ((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr;
165 break;
166
167 default:
168 /* can't get here because 'af' was previously checked */
169 msg(M_WARN, "Unsupported AF family for %s (%d)", var_host, af);
170 goto out;
171 }
172 }
173
174 if (netbits)
175 {
176 *netbits = (unsigned int)bits;
177 }
178
179 /* restore '/' separator, if any */
180 if (sep)
181 {
182 *sep = '/';
183 }
184out:
185 if (ai)
186 {
187 freeaddrinfo(ai);
188 }
189 free(var_host);
190
191 return ret;
192}
193
195getaddr(unsigned int flags, const char *hostname, int resolve_retry_seconds, bool *succeeded,
196 struct signal_info *sig_info)
197{
198 in_addr_t addr = { 0 };
199 int status;
200
201 status = get_addr_generic(AF_INET, flags, hostname, &addr, NULL, resolve_retry_seconds,
202 sig_info, M_WARN);
203 if (status == 0)
204 {
205 if (succeeded)
206 {
207 *succeeded = true;
208 }
209 return addr;
210 }
211 else
212 {
213 if (succeeded)
214 {
215 *succeeded = false;
216 }
217 return 0;
218 }
219}
220
221bool
222get_ipv6_addr(const char *hostname, struct in6_addr *network, unsigned int *netbits,
223 msglvl_t msglevel)
224{
225 if (get_addr_generic(AF_INET6, GETADDR_RESOLVE, hostname, network, netbits, 0, NULL, msglevel)
226 < 0)
227 {
228 return false;
229 }
230
231 return true; /* parsing OK, values set */
232}
233
234static inline bool
235streqnull(const char *a, const char *b)
236{
237 if (a == NULL && b == NULL)
238 {
239 return true;
240 }
241 else if (a == NULL || b == NULL)
242 {
243 return false;
244 }
245 else
246 {
247 return streq(a, b);
248 }
249}
250
251/*
252 * get_cached_dns_entry return 0 on success and -1
253 * otherwise. (like getaddrinfo)
254 */
255static int
256get_cached_dns_entry(struct cached_dns_entry *dns_cache, const char *hostname, const char *servname,
257 int ai_family, unsigned int resolve_flags, struct addrinfo **ai)
258{
259 struct cached_dns_entry *ph;
260 unsigned int flags;
261
262 /* Only use flags that are relevant for the structure */
263 flags = resolve_flags & GETADDR_CACHE_MASK;
264
265 for (ph = dns_cache; ph; ph = ph->next)
266 {
268 && ph->ai_family == ai_family && ph->flags == flags)
269 {
270 *ai = ph->ai;
271 return 0;
272 }
273 }
274 return -1;
275}
276
277
278static int
279do_preresolve_host(struct context *c, const char *hostname, const char *servname, const int af,
280 const unsigned int flags)
281{
282 struct addrinfo *ai;
283 int status;
284
285 if (get_cached_dns_entry(c->c1.dns_cache, hostname, servname, af, flags, &ai) == 0)
286 {
287 /* entry already cached, return success */
288 return 0;
289 }
290
291 status = openvpn_getaddrinfo(flags, hostname, servname, c->options.resolve_retry_seconds, NULL,
292 af, &ai);
293 if (status == 0)
294 {
295 struct cached_dns_entry *ph;
296
297 ALLOC_OBJ_CLEAR_GC(ph, struct cached_dns_entry, &c->gc);
298 ph->ai = ai;
299 ph->hostname = hostname;
300 ph->servname = servname;
302
303 if (!c->c1.dns_cache)
304 {
305 c->c1.dns_cache = ph;
306 }
307 else
308 {
309 struct cached_dns_entry *prev = c->c1.dns_cache;
310 while (prev->next)
311 {
312 prev = prev->next;
313 }
314 prev->next = ph;
315 }
316
318 }
319 return status;
320}
321
322void
324{
325 struct connection_list *l = c->options.connection_list;
328
329
330 for (int i = 0; i < l->len; ++i)
331 {
332 int status;
333 const char *remote;
334 unsigned int flags = preresolve_flags;
335
336 struct connection_entry *ce = l->array[i];
337
338 if (proto_is_dgram(ce->proto))
339 {
341 }
342
344 {
346 }
347
348 if (c->options.ip_remote_hint)
349 {
351 }
352 else
353 {
354 remote = ce->remote;
355 }
356
357 /* HTTP remote hostname does not need to be resolved */
358 if (!ce->http_proxy_options)
359 {
361 if (status != 0)
362 {
363 goto err;
364 }
365 }
366
367 /* Preresolve proxy */
368 if (ce->http_proxy_options)
369 {
371 ce->http_proxy_options->port, ce->af, preresolve_flags);
372
373 if (status != 0)
374 {
375 goto err;
376 }
377 }
378
379 if (ce->socks_proxy_server)
380 {
381 status =
383 if (status != 0)
384 {
385 goto err;
386 }
387 }
388
389 if (ce->bind_local)
390 {
392 flags &= ~GETADDR_RANDOMIZE;
393
394 for (int j = 0; j < ce->local_list->len; j++)
395 {
396 const struct local_entry *le = ce->local_list->array[j];
397
398 if (!le->local)
399 {
400 continue;
401 }
402
403 status = do_preresolve_host(c, le->local, le->port, ce->af, flags);
404 if (status != 0)
405 {
406 goto err;
407 }
408 }
409 }
410 }
411 return;
412
413err:
414 throw_signal_soft(SIGHUP, "Preresolving failed");
415}
416
417static int
419{
420 int val;
421 socklen_t len = sizeof(val);
422
423 if (getsockopt(sd, SOL_SOCKET, SO_SNDBUF, (void *)&val, &len) == 0 && len == sizeof(val))
424 {
425 return val;
426 }
427 return 0;
428}
429
430static void
432{
433 if (setsockopt(sd, SOL_SOCKET, SO_SNDBUF, (void *)&size, sizeof(size)) != 0)
434 {
435 msg(M_WARN, "NOTE: setsockopt SO_SNDBUF=%d failed", size);
436 }
437}
438
439static int
441{
442 int val;
443 socklen_t len = sizeof(val);
444
445 if (getsockopt(sd, SOL_SOCKET, SO_RCVBUF, (void *)&val, &len) == 0 && len == sizeof(val))
446 {
447 return val;
448 }
449 return 0;
450}
451
452static void
454{
455 if (setsockopt(sd, SOL_SOCKET, SO_RCVBUF, (void *)&size, sizeof(size)) != 0)
456 {
457 msg(M_WARN, "NOTE: setsockopt SO_RCVBUF=%d failed", size);
458 }
459}
460
461void
462socket_set_buffers(socket_descriptor_t fd, const struct socket_buffer_size *sbs, bool reduce_size)
463{
464 if (sbs)
465 {
466 const int sndbuf_old = socket_get_sndbuf(fd);
467 const int rcvbuf_old = socket_get_rcvbuf(fd);
468
469 if (sbs->sndbuf && (reduce_size || sndbuf_old < sbs->sndbuf))
470 {
471 socket_set_sndbuf(fd, sbs->sndbuf);
472 }
473
474 if (sbs->rcvbuf && (reduce_size || rcvbuf_old < sbs->rcvbuf))
475 {
476 socket_set_rcvbuf(fd, sbs->rcvbuf);
477 }
478
479 msg(D_OSBUF, "Socket Buffers: R=[%d->%d] S=[%d->%d]", rcvbuf_old, socket_get_rcvbuf(fd),
480 sndbuf_old, socket_get_sndbuf(fd));
481 }
482}
483
484/*
485 * Set other socket options
486 */
487
488static bool
490{
491#if defined(_WIN32) || (defined(IPPROTO_TCP) && defined(TCP_NODELAY))
492 if (setsockopt(sd, IPPROTO_TCP, TCP_NODELAY, (void *)&state, sizeof(state)) != 0)
493 {
494 msg(M_WARN, "NOTE: setsockopt TCP_NODELAY=%d failed", state);
495 return false;
496 }
497 else
498 {
499 dmsg(D_OSBUF, "Socket flags: TCP_NODELAY=%d succeeded", state);
500 return true;
501 }
502#else /* if defined(_WIN32) || (defined(IPPROTO_TCP) && defined(TCP_NODELAY)) */
503 msg(M_WARN, "NOTE: setsockopt TCP_NODELAY=%d failed (No kernel support)", state);
504 return false;
505#endif
506}
507
508static inline void
510{
511#if defined(TARGET_LINUX)
512 if (mark && setsockopt(sd, SOL_SOCKET, SO_MARK, (void *)&mark, sizeof(mark)) != 0)
513 {
514 msg(M_WARN, "NOTE: setsockopt SO_MARK=%d failed", mark);
515 }
516#endif
517}
518
519void
520link_socket_update_buffer_sizes(struct link_socket *sock, int rcvbuf, int sndbuf)
521{
522 if (sock && socket_defined(sock->sd))
523 {
524 sock->socket_buffer_sizes.sndbuf = sndbuf;
525 sock->socket_buffer_sizes.rcvbuf = rcvbuf;
526 socket_set_buffers(sock->sd, &sock->socket_buffer_sizes, true);
527 }
528}
529
530/*
531 * SOCKET INITIALIZATION CODE.
532 * Create a TCP/UDP socket
533 */
534
536create_socket_tcp(struct addrinfo *addrinfo)
537{
539
540 ASSERT(addrinfo);
541 ASSERT(addrinfo->ai_socktype == SOCK_STREAM);
542
543 if ((sd = socket(addrinfo->ai_family, addrinfo->ai_socktype, addrinfo->ai_protocol))
545 {
546 msg(M_ERR, "Cannot create TCP socket");
547 }
548
549#ifndef _WIN32 /* using SO_REUSEADDR on Windows will cause bind to succeed on port conflicts! */
550 /* set SO_REUSEADDR on socket */
551 {
552 int on = 1;
553 if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)) < 0)
554 {
555 msg(M_ERR, "TCP: Cannot setsockopt SO_REUSEADDR on TCP socket");
556 }
557 }
558#endif
559
560 /* set socket file descriptor to not pass across execs, so that
561 * scripts don't have access to it */
562 set_cloexec(sd);
563
564 return sd;
565}
566
568create_socket_udp(struct addrinfo *addrinfo, const unsigned int flags)
569{
571
572 ASSERT(addrinfo);
573 ASSERT(addrinfo->ai_socktype == SOCK_DGRAM);
574
575 if ((sd = socket(addrinfo->ai_family, addrinfo->ai_socktype, addrinfo->ai_protocol))
577 {
578 msg(M_ERR, "UDP: Cannot create UDP/UDP6 socket");
579 }
580#if ENABLE_IP_PKTINFO
581 else if (flags & SF_USE_IP_PKTINFO)
582 {
583 int pad = 1;
584 if (addrinfo->ai_family == AF_INET)
585 {
586#if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST)
587 if (setsockopt(sd, SOL_IP, IP_PKTINFO, (void *)&pad, sizeof(pad)) < 0)
588 {
589 msg(M_ERR, "UDP: failed setsockopt for IP_PKTINFO");
590 }
591#elif defined(IP_RECVDSTADDR)
592 if (setsockopt(sd, IPPROTO_IP, IP_RECVDSTADDR, (void *)&pad, sizeof(pad)) < 0)
593 {
594 msg(M_ERR, "UDP: failed setsockopt for IP_RECVDSTADDR");
595 }
596#else /* if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST) */
597#error ENABLE_IP_PKTINFO is set without IP_PKTINFO xor IP_RECVDSTADDR (fix syshead.h)
598#endif
599 }
600 else if (addrinfo->ai_family == AF_INET6)
601 {
602#ifndef IPV6_RECVPKTINFO /* Some older Darwin platforms require this */
603 if (setsockopt(sd, IPPROTO_IPV6, IPV6_PKTINFO, (void *)&pad, sizeof(pad)) < 0)
604#else
605 if (setsockopt(sd, IPPROTO_IPV6, IPV6_RECVPKTINFO, (void *)&pad, sizeof(pad)) < 0)
606#endif
607 {
608 msg(M_ERR, "UDP: failed setsockopt for IPV6_RECVPKTINFO");
609 }
610 }
611 }
612#endif /* if ENABLE_IP_PKTINFO */
613
614 /* set socket file descriptor to not pass across execs, so that
615 * scripts don't have access to it */
616 set_cloexec(sd);
617
618 return sd;
619}
620
621static void
623{
624 /* bind to local address/port */
625 if (sock->bind_local)
626 {
627 if (sock->socks_proxy && sock->info.proto == PROTO_UDP)
628 {
629 socket_bind(sock->ctrl_sd, sock->info.lsa->bind_local, sock->info.af, "SOCKS", false);
630 }
631 else
632 {
633 socket_bind(sock->sd, sock->info.lsa->bind_local, sock->info.af, "TCP/UDP",
634 sock->info.bind_ipv6_only);
635 }
636 }
637}
638
639static void
640create_socket(struct link_socket *sock, struct addrinfo *addr)
641{
642 if (addr->ai_protocol == IPPROTO_UDP || addr->ai_socktype == SOCK_DGRAM)
643 {
644 sock->sd = create_socket_udp(addr, sock->sockflags);
646
647 /* Assume that control socket and data socket to the socks proxy
648 * are using the same IP family */
649 if (sock->socks_proxy)
650 {
651 /* Construct a temporary addrinfo to create the socket,
652 * currently resolve two remote addresses is not supported,
653 * TODO: Rewrite the whole resolve_remote */
654 struct addrinfo addrinfo_tmp = *addr;
655 addrinfo_tmp.ai_socktype = SOCK_STREAM;
656 addrinfo_tmp.ai_protocol = IPPROTO_TCP;
657 sock->ctrl_sd = create_socket_tcp(&addrinfo_tmp);
658 }
659 }
660 else if (addr->ai_protocol == IPPROTO_TCP || addr->ai_socktype == SOCK_STREAM)
661 {
662 sock->sd = create_socket_tcp(addr);
663 }
664 else
665 {
666 ASSERT(0);
667 }
668 /* Set af field of sock->info, so it always reflects the address family
669 * of the created socket */
670 sock->info.af = (sa_family_t)addr->ai_family;
671
672 /* set socket buffers based on --sndbuf and --rcvbuf options */
673 socket_set_buffers(sock->sd, &sock->socket_buffer_sizes, true);
674
675 /* set socket to --mark packets with given value */
676 socket_set_mark(sock->sd, sock->mark);
677
678#if defined(TARGET_LINUX)
679 if (sock->bind_dev)
680 {
681 msg(M_INFO, "Using bind-dev %s", sock->bind_dev);
682 /* Note: We verify strlen of bind_dev in options parsing */
683 if (setsockopt(sock->sd, SOL_SOCKET, SO_BINDTODEVICE, sock->bind_dev,
684 (socklen_t)(strlen(sock->bind_dev) + 1))
685 != 0)
686 {
687 msg(M_WARN | M_ERRNO, "WARN: setsockopt SO_BINDTODEVICE=%s failed", sock->bind_dev);
688 }
689 }
690#endif
691
692 bind_local(sock);
693}
694
695#ifdef TARGET_ANDROID
696static void
697protect_fd_nonlocal(int fd, const struct sockaddr *addr)
698{
699 if (!management)
700 {
701 msg(M_FATAL, "Required management interface not available.");
702 }
703
704 /* pass socket FD to management interface to pass on to VPNService API
705 * as "protected socket" (exempt from being routed into tunnel)
706 */
707 if (addr_local(addr))
708 {
709 msg(D_SOCKET_DEBUG, "Address is local, not protecting socket fd %d", fd);
710 return;
711 }
712
713 msg(D_SOCKET_DEBUG, "Protecting socket fd %d", fd);
714 management->connection.fdtosend = fd;
715 management_android_control(management, "PROTECTFD", __func__);
716}
717#endif
718
719/*
720 * Functions used for establishing a TCP stream connection.
721 */
722static void
723socket_do_listen(socket_descriptor_t sd, const struct addrinfo *local, bool do_listen,
724 bool do_set_nonblock)
725{
726 struct gc_arena gc = gc_new();
727 if (do_listen)
728 {
729 ASSERT(local);
730 msg(M_INFO, "Listening for incoming TCP connection on %s",
731 print_sockaddr(local->ai_addr, &gc));
732 if (listen(sd, 32))
733 {
734 msg(M_ERR, "TCP: listen() failed");
735 }
736 }
737
738 /* set socket to non-blocking mode */
739 if (do_set_nonblock)
740 {
741 set_nonblock(sd);
742 }
743
744 gc_free(&gc);
745}
746
748socket_do_accept(socket_descriptor_t sd, struct link_socket_actual *act, const bool nowait)
749{
750 /* af_addr_size WILL return 0 in this case if AFs other than AF_INET
751 * are compiled because act is empty here.
752 * could use getsockname() to support later remote_len check
753 */
754 socklen_t remote_len_af = af_addr_size(act->dest.addr.sa.sa_family);
755 socklen_t remote_len = sizeof(act->dest.addr);
757
758 CLEAR(*act);
759
760 if (nowait)
761 {
762 new_sd = getpeername(sd, &act->dest.addr.sa, &remote_len);
763
764 if (!socket_defined(new_sd))
765 {
766 msg(D_LINK_ERRORS | M_ERRNO, "TCP: getpeername() failed");
767 }
768 else
769 {
770 new_sd = sd;
771 }
772 }
773 else
774 {
775 new_sd = accept(sd, &act->dest.addr.sa, &remote_len);
776 }
777
778#if 0 /* For debugging only, test the effect of accept() failures */
779 {
780 static int foo = 0;
781 ++foo;
782 if (foo & 1)
783 {
784 new_sd = -1;
785 }
786 }
787#endif
788
789 if (!socket_defined(new_sd))
790 {
791 msg(D_LINK_ERRORS | M_ERRNO, "TCP: accept(%d) failed", (int)sd);
792 }
793 /* only valid if we have remote_len_af!=0 */
794 else if (remote_len_af && remote_len != remote_len_af)
795 {
797 "TCP: Received strange incoming connection with unknown address length=%d", remote_len);
798 openvpn_close_socket(new_sd);
799 new_sd = SOCKET_UNDEFINED;
800 }
801 else
802 {
803 /* set socket file descriptor to not pass across execs, so that
804 * scripts don't have access to it */
805 set_cloexec(new_sd);
806 }
807 return new_sd;
808}
809
810static void
812{
813 struct gc_arena gc = gc_new();
814 msg(M_INFO, "TCP connection established with %s", print_link_socket_actual(act, &gc));
815 gc_free(&gc);
816}
817
820 const struct addrinfo *local, bool do_listen,
821 bool nowait, volatile int *signal_received)
822{
823 struct gc_arena gc = gc_new();
825
826 CLEAR(*act);
827 socket_do_listen(sd, local, do_listen, true);
828
829 while (true)
830 {
831 int status;
832 fd_set reads;
833 struct timeval tv;
834
835 FD_ZERO(&reads);
836 openvpn_fd_set(sd, &reads);
837 tv.tv_sec = 0;
838 tv.tv_usec = 0;
839
840 status = openvpn_select(sd + 1, &reads, NULL, NULL, &tv);
841
842 get_signal(signal_received);
843 if (*signal_received)
844 {
845 gc_free(&gc);
846 return sd;
847 }
848
849 if (status < 0)
850 {
851 msg(D_LINK_ERRORS | M_ERRNO, "TCP: select() failed");
852 }
853
854 if (status <= 0)
855 {
857 continue;
858 }
859
860 new_sd = socket_do_accept(sd, act, nowait);
861
862 if (socket_defined(new_sd))
863 {
864 break;
865 }
867 }
868
869 if (!nowait && openvpn_close_socket(sd))
870 {
871 msg(M_ERR, "TCP: close socket failed (sd)");
872 }
873
875
876 gc_free(&gc);
877 return new_sd;
878}
879
880void
881socket_bind(socket_descriptor_t sd, struct addrinfo *local, int ai_family, const char *prefix,
882 bool ipv6only)
883{
884 struct gc_arena gc = gc_new();
885
886 /* FIXME (schwabe)
887 * getaddrinfo for the bind address might return multiple AF_INET/AF_INET6
888 * entries for the requested protocol.
889 * For example if an address has multiple A records
890 * What is the correct way to deal with it?
891 */
892
893 ASSERT(local);
894
895 /* find the first addrinfo with correct ai_family */
896 const struct addrinfo *cur;
897 for (cur = local; cur; cur = cur->ai_next)
898 {
899 if (cur->ai_family == ai_family)
900 {
901 break;
902 }
903 }
904 if (!cur)
905 {
906 msg(M_FATAL, "%s: Socket bind failed: Addr to bind has no %s record", prefix,
907 addr_family_name(ai_family));
908 }
909
910 if (ai_family == AF_INET6)
911 {
912 int v6only = ipv6only ? 1 : 0; /* setsockopt must have an "int" */
913
914 msg(M_INFO, "setsockopt(IPV6_V6ONLY=%d)", v6only);
915 if (setsockopt(sd, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&v6only, sizeof(v6only)))
916 {
917 msg(M_NONFATAL | M_ERRNO, "Setting IPV6_V6ONLY=%d failed", v6only);
918 }
919 }
920 if (openvpn_bind(sd, cur->ai_addr, cur->ai_addrlen))
921 {
922 msg(M_FATAL | M_ERRNO, "%s: Socket bind failed on local address %s", prefix,
923 print_sockaddr_ex(local->ai_addr, ":", PS_SHOW_PORT, &gc));
924 }
925 gc_free(&gc);
926}
927
928int
929openvpn_connect(socket_descriptor_t sd, const struct sockaddr *remote, int connect_timeout,
930 volatile int *signal_received)
931{
932 int status = 0;
933
934#ifdef TARGET_ANDROID
935 protect_fd_nonlocal(sd, remote);
936#endif
937 set_nonblock(sd);
938 status = connect(sd, remote, af_addr_size(remote->sa_family));
939 if (status)
940 {
942 }
943 if (
944#ifdef _WIN32
945 status == WSAEWOULDBLOCK
946#else
947 status == EINPROGRESS
948#endif
949 )
950 {
951 while (true)
952 {
953#if POLL
954 struct pollfd fds[1];
955 fds[0].fd = sd;
956 fds[0].events = POLLOUT;
957 status = poll(fds, 1, (connect_timeout > 0) ? 1000 : 0);
958#else
959 fd_set writes;
960 struct timeval tv;
961
962 FD_ZERO(&writes);
963 openvpn_fd_set(sd, &writes);
964 tv.tv_sec = (connect_timeout > 0) ? 1 : 0;
965 tv.tv_usec = 0;
966
967 status = openvpn_select(sd + 1, NULL, &writes, NULL, &tv);
968#endif
969 if (signal_received)
970 {
971 get_signal(signal_received);
972 if (*signal_received)
973 {
974 status = 0;
975 break;
976 }
977 }
978 if (status < 0)
979 {
981 break;
982 }
983 if (status <= 0)
984 {
985 if (--connect_timeout < 0)
986 {
987#ifdef _WIN32
988 status = WSAETIMEDOUT;
989#else
990 status = ETIMEDOUT;
991#endif
992 break;
993 }
995 continue;
996 }
997
998 /* got it */
999 {
1000 int val = 0;
1001 socklen_t len;
1002
1003 len = sizeof(val);
1004 if (getsockopt(sd, SOL_SOCKET, SO_ERROR, (void *)&val, &len) == 0
1005 && len == sizeof(val))
1006 {
1007 status = val;
1008 }
1009 else
1010 {
1012 }
1013 break;
1014 }
1015 }
1016 }
1017
1018 return status;
1019}
1020
1021void
1022set_actual_address(struct link_socket_actual *actual, struct addrinfo *ai)
1023{
1024 CLEAR(*actual);
1025 ASSERT(ai);
1026
1027 if (ai->ai_family == AF_INET)
1028 {
1029 actual->dest.addr.in4 = *((struct sockaddr_in *)ai->ai_addr);
1030 }
1031 else if (ai->ai_family == AF_INET6)
1032 {
1033 actual->dest.addr.in6 = *((struct sockaddr_in6 *)ai->ai_addr);
1034 }
1035 else
1036 {
1037 ASSERT(0);
1038 }
1039}
1040
1041static void
1042socket_connect(socket_descriptor_t *sd, const struct sockaddr *dest, const int connect_timeout,
1043 struct signal_info *sig_info)
1044{
1045 struct gc_arena gc = gc_new();
1046 int status;
1047
1048 msg(M_INFO, "Attempting to establish TCP connection with %s", print_sockaddr(dest, &gc));
1049
1050#ifdef ENABLE_MANAGEMENT
1051 if (management)
1052 {
1053 management_set_state(management, OPENVPN_STATE_TCP_CONNECT, NULL, NULL, NULL, NULL, NULL);
1054 }
1055#endif
1056
1057 /* Set the actual address */
1058 status = openvpn_connect(*sd, dest, connect_timeout, &sig_info->signal_received);
1059
1060 get_signal(&sig_info->signal_received);
1061 if (sig_info->signal_received)
1062 {
1063 goto done;
1064 }
1065
1066 if (status)
1067 {
1068 msg(D_LINK_ERRORS, "TCP: connect to %s failed: %s", print_sockaddr(dest, &gc),
1069 strerror(status));
1070
1072 *sd = SOCKET_UNDEFINED;
1073 register_signal(sig_info, SIGUSR1, "connection-failed");
1074 }
1075 else
1076 {
1077 msg(M_INFO, "TCP connection established with %s", print_sockaddr(dest, &gc));
1078 }
1079
1080done:
1081 gc_free(&gc);
1082}
1083
1084/*
1085 * Stream buffer handling prototypes -- stream_buf is a helper class
1086 * to assist in the packetization of stream transport protocols
1087 * such as TCP.
1088 */
1089
1090static void stream_buf_init(struct stream_buf *sb, struct buffer *buf, const unsigned int sockflags,
1091 const int proto);
1092
1093static void stream_buf_close(struct stream_buf *sb);
1094
1095static bool stream_buf_added(struct stream_buf *sb, ssize_t length_added);
1096
1097/* For stream protocols, allocate a buffer to build up packet.
1098 * Called after frame has been finalized. */
1099
1100static void
1101socket_frame_init(const struct frame *frame, struct link_socket *sock)
1102{
1103#ifdef _WIN32
1104 overlapped_io_init(&sock->reads, frame, FALSE);
1105 overlapped_io_init(&sock->writes, frame, TRUE);
1106 sock->rw_handle.read = sock->reads.overlapped.hEvent;
1107 sock->rw_handle.write = sock->writes.overlapped.hEvent;
1108#endif
1109
1111 {
1112#ifdef _WIN32
1113 stream_buf_init(&sock->stream_buf, &sock->reads.buf_init, sock->sockflags,
1114 sock->info.proto);
1115#else
1117
1119 sock->info.proto);
1120#endif
1121 }
1122}
1123
1124static void
1126{
1127 struct gc_arena gc = gc_new();
1128
1129 /* resolve local address if undefined */
1130 if (!sock->info.lsa->bind_local)
1131 {
1133 int status;
1134
1135 if (proto_is_dgram(sock->info.proto))
1136 {
1137 flags |= GETADDR_DATAGRAM;
1138 }
1139
1140 /* will return AF_{INET|INET6}from local_host */
1141 status = get_cached_dns_entry(sock->dns_cache, sock->local_host, sock->local_port, sock->info.af,
1142 flags, &sock->info.lsa->bind_local);
1143
1144 if (status)
1145 {
1146 status = openvpn_getaddrinfo(flags, sock->local_host, sock->local_port, 0, NULL, sock->info.af,
1147 &sock->info.lsa->bind_local);
1148 }
1149
1150 if (status != 0)
1151 {
1152 msg(M_FATAL, "getaddrinfo() failed for local \"%s:%s\": %s", sock->local_host,
1153 sock->local_port, gai_strerror(status));
1154 }
1155
1156 /* the address family returned by openvpn_getaddrinfo() should be
1157 * taken into consideration only if we really passed an hostname
1158 * to resolve. Otherwise its value is not useful to us and may
1159 * actually break our socket, i.e. when it returns AF_INET
1160 * but our remote is v6 only.
1161 */
1162 if (sock->local_host)
1163 {
1164 /* the resolved 'local entry' might have a different family than
1165 * what was globally configured
1166 */
1167 sock->info.af = (sa_family_t)sock->info.lsa->bind_local->ai_family;
1168 }
1169 }
1170
1171 gc_free(&gc);
1172}
1173
1174static void
1175resolve_remote(struct link_socket *sock, int phase, struct signal_info *sig_info)
1176{
1177 volatile int *signal_received = sig_info ? &sig_info->signal_received : NULL;
1178 struct gc_arena gc = gc_new();
1179
1180 /* resolve remote address if undefined */
1181 if (!sock->info.lsa->remote_list)
1182 {
1183 if (sock->remote_host)
1184 {
1185 unsigned int flags =
1187 int retry = 0;
1188 int status = -1;
1189 struct addrinfo *ai;
1190 if (proto_is_dgram(sock->info.proto))
1191 {
1192 flags |= GETADDR_DATAGRAM;
1193 }
1194
1196 {
1197 if (phase == 2)
1198 {
1199 flags |= (GETADDR_TRY_ONCE | GETADDR_FATAL);
1200 }
1201 retry = 0;
1202 }
1203 else if (phase == 1)
1204 {
1205 if (sock->resolve_retry_seconds)
1206 {
1207 retry = 0;
1208 }
1209 else
1210 {
1212 retry = 0;
1213 }
1214 }
1215 else if (phase == 2)
1216 {
1217 if (sock->resolve_retry_seconds)
1218 {
1219 flags |= GETADDR_FATAL;
1220 retry = sock->resolve_retry_seconds;
1221 }
1222 else
1223 {
1224 ASSERT(0);
1225 }
1226 }
1227 else
1228 {
1229 ASSERT(0);
1230 }
1231
1232
1234 sock->info.af, flags, &ai);
1235 if (status)
1236 {
1237 status = openvpn_getaddrinfo(flags, sock->remote_host, sock->remote_port, retry,
1238 sig_info, sock->info.af, &ai);
1239 }
1240
1241 if (status == 0)
1242 {
1243 sock->info.lsa->remote_list = ai;
1244 sock->info.lsa->current_remote = ai;
1245
1246 dmsg(D_SOCKET_DEBUG, "RESOLVE_REMOTE flags=0x%04x phase=%d rrs=%d sig=%d status=%d",
1247 flags, phase, retry, signal_received ? *signal_received : -1, status);
1248 }
1249 if (signal_received && *signal_received)
1250 {
1251 goto done;
1252 }
1253 if (status != 0)
1254 {
1255 if (signal_received)
1256 {
1257 /* potential overwrite of signal */
1258 register_signal(sig_info, SIGUSR1, "socks-resolve-failure");
1259 }
1260 goto done;
1261 }
1262 }
1263 }
1264
1265 /* should we re-use previous active remote address? */
1267 {
1268 msg(M_INFO, "TCP/UDP: Preserving recently used remote address: %s",
1270 }
1271 else
1272 {
1273 CLEAR(sock->info.lsa->actual);
1274 if (sock->info.lsa->current_remote)
1275 {
1277 }
1278 }
1279
1280done:
1281 gc_free(&gc);
1282}
1283
1284
1285struct link_socket *
1287{
1288 struct link_socket *sock;
1289
1290 ALLOC_OBJ_CLEAR(sock, struct link_socket);
1291 sock->sd = SOCKET_UNDEFINED;
1292 sock->ctrl_sd = SOCKET_UNDEFINED;
1294 sock->ev_arg.u.sock = sock;
1295
1296 return sock;
1297}
1298
1299void
1300link_socket_init_phase1(struct context *c, int sock_index, int mode)
1301{
1302 struct link_socket *sock = c->c2.link_sockets[sock_index];
1303 struct options *o = &c->options;
1304 ASSERT(sock);
1305
1306 const char *host = o->ce.local_list->array[sock_index]->local;
1307 const char *port = o->ce.local_list->array[sock_index]->port;
1308 int proto = o->ce.local_list->array[sock_index]->proto;
1309 const char *remote_host = o->ce.remote;
1310 const char *remote_port = o->ce.remote_port;
1311
1312 if (remote_host)
1313 {
1314 proto = o->ce.proto;
1315 }
1316
1317 /* If --lport is specified in a client connection block,
1318 * it takes precedence over the global setting. */
1320 {
1321 port = o->ce.local_port;
1322 }
1323
1324 if (c->mode == CM_CHILD_TCP || c->mode == CM_CHILD_UDP)
1325 {
1326 struct link_socket *tmp_sock = NULL;
1327 if (c->mode == CM_CHILD_TCP)
1328 {
1329 tmp_sock = (struct link_socket *)c->c2.accept_from;
1330 }
1331 else if (c->mode == CM_CHILD_UDP)
1332 {
1333 tmp_sock = c->c2.link_sockets[0];
1334 }
1335
1336 host = tmp_sock->local_host;
1337 port = tmp_sock->local_port;
1338 proto = tmp_sock->info.proto;
1339 }
1340
1341 sock->local_host = host;
1342 sock->local_port = port;
1343 sock->remote_host = remote_host;
1344 sock->remote_port = remote_port;
1345 sock->dns_cache = c->c1.dns_cache;
1346 sock->http_proxy = c->c1.http_proxy;
1347 sock->socks_proxy = c->c1.socks_proxy;
1348 sock->bind_local = o->ce.bind_local;
1351
1352#ifdef ENABLE_DEBUG
1353 sock->gremlin = o->gremlin;
1354#endif
1355
1358
1359 sock->sockflags = o->sockflags;
1360
1361#if PORT_SHARE
1362 if (o->port_share_host && o->port_share_port)
1363 {
1364 sock->sockflags |= SF_PORT_SHARE;
1365 }
1366#endif
1367
1368 sock->mark = o->mark;
1369 sock->bind_dev = o->bind_dev;
1370 ASSERT(proto >= 0 && proto < PROTO_N);
1371 sock->info.proto = (uint8_t)proto;
1372 sock->info.af = o->ce.af;
1373 sock->info.remote_float = o->ce.remote_float;
1374 sock->info.lsa = &c->c1.link_socket_addrs[sock_index];
1376 sock->info.ipchange_command = o->ipchange;
1377 sock->info.plugins = c->plugins;
1379
1380 sock->mode = mode;
1382 {
1383 ASSERT(c->c2.accept_from);
1385 sock->sd = c->c2.accept_from->sd;
1386 /* inherit (possibly guessed) info AF from parent context */
1387 sock->info.af = c->c2.accept_from->info.af;
1388 }
1389
1390 /* are we running in HTTP proxy mode? */
1391 if (sock->http_proxy)
1392 {
1394
1395 /* the proxy server */
1397 sock->remote_port = c->c1.http_proxy->options.port;
1398
1399 /* the OpenVPN server we will use the proxy to connect to */
1402 }
1403 /* or in Socks proxy mode? */
1404 else if (sock->socks_proxy)
1405 {
1406 /* the proxy server */
1407 sock->remote_host = c->c1.socks_proxy->server;
1408 sock->remote_port = c->c1.socks_proxy->port;
1409
1410 /* the OpenVPN server we will use the proxy to connect to */
1413 }
1414 else
1415 {
1416 sock->remote_host = remote_host;
1417 sock->remote_port = remote_port;
1418 }
1419
1420 /* bind behavior for TCP server vs. client */
1421 if (sock->info.proto == PROTO_TCP_SERVER)
1422 {
1423 if (sock->mode == LS_MODE_TCP_ACCEPT_FROM)
1424 {
1425 sock->bind_local = false;
1426 }
1427 else
1428 {
1429 sock->bind_local = true;
1430 }
1431 }
1432
1434 {
1435 if (sock->bind_local)
1436 {
1437 resolve_bind_local(sock);
1438 }
1439 resolve_remote(sock, 1, NULL);
1440 }
1441}
1442
1443static void
1445{
1446 /* TCP_NODELAY is enabled by default on every TCP socket; dco-win is
1447 * skipped as it manages its own socket */
1448 if (proto_is_tcp(sock->info.proto) && !(sock->sockflags & SF_DCO_WIN))
1449 {
1450 socket_set_tcp_nodelay(sock->sd, 1);
1451 }
1452
1453 /* set socket to non-blocking mode */
1454 set_nonblock(sock->sd);
1455
1456 /* set Path MTU discovery options on the socket */
1457 set_mtu_discover_type(sock->sd, sock->mtu_discover_type, sock->info.af);
1458
1459#if EXTENDED_SOCKET_ERROR_CAPABILITY
1460 /* if the OS supports it, enable extended error passing on the socket */
1461 set_sock_extended_error_passing(sock->sd, sock->info.af);
1462#endif
1463}
1464
1465
1466static void
1468{
1469 struct gc_arena gc = gc_new();
1470 const msglvl_t msglevel = (sock->mode == LS_MODE_TCP_ACCEPT_FROM) ? D_INIT_MEDIUM : M_INFO;
1471
1472 /* print local address */
1473 if (sock->bind_local)
1474 {
1475 sa_family_t ai_family = sock->info.lsa->actual.dest.addr.sa.sa_family;
1476 /* Socket is always bound on the first matching address,
1477 * For bound sockets with no remote addr this is the element of
1478 * the list */
1479 const struct addrinfo *cur;
1480 for (cur = sock->info.lsa->bind_local; cur; cur = cur->ai_next)
1481 {
1482 if (!ai_family || ai_family == cur->ai_family)
1483 {
1484 break;
1485 }
1486 }
1487 ASSERT(cur);
1488 msg(msglevel, "%s link local (bound): %s",
1489 proto2ascii(sock->info.proto, sock->info.af, true), print_sockaddr(cur->ai_addr, &gc));
1490 }
1491 else
1492 {
1493 msg(msglevel, "%s link local: (not bound)",
1494 proto2ascii(sock->info.proto, sock->info.af, true));
1495 }
1496
1497 /* print active remote address */
1498 msg(msglevel, "%s link remote: %s", proto2ascii(sock->info.proto, sock->info.af, true),
1500 gc_free(&gc);
1501}
1502
1503static void
1504phase2_tcp_server(struct link_socket *sock, struct signal_info *sig_info)
1505{
1506 ASSERT(sig_info);
1507 volatile int *signal_received = &sig_info->signal_received;
1508 switch (sock->mode)
1509 {
1510 case LS_MODE_DEFAULT:
1511 sock->sd =
1512 socket_listen_accept(sock->sd, &sock->info.lsa->actual,
1513 sock->info.lsa->bind_local, true, false,
1514 signal_received);
1515 break;
1516
1517 case LS_MODE_TCP_LISTEN:
1518 socket_do_listen(sock->sd, sock->info.lsa->bind_local, true, false);
1519 break;
1520
1522 sock->sd = socket_do_accept(sock->sd, &sock->info.lsa->actual, false);
1523 if (!socket_defined(sock->sd))
1524 {
1525 register_signal(sig_info, SIGTERM, "socket-undefined");
1526 return;
1527 }
1529 break;
1530
1531 default:
1532 ASSERT(0);
1533 }
1534}
1535
1536
1537static void
1538phase2_tcp_client(struct link_socket *sock, struct signal_info *sig_info)
1539{
1540 bool proxy_retry = false;
1541 do
1542 {
1543 socket_connect(&sock->sd, sock->info.lsa->current_remote->ai_addr,
1545
1546 if (sig_info->signal_received)
1547 {
1548 return;
1549 }
1550
1551 if (sock->http_proxy)
1552 {
1553 proxy_retry = establish_http_proxy_passthru(
1554 sock->http_proxy, sock->sd, sock->proxy_dest_host, sock->proxy_dest_port,
1555 sock->server_poll_timeout, &sock->stream_buf.residual, sig_info);
1556 }
1557 else if (sock->socks_proxy)
1558 {
1561 sig_info);
1562 }
1563 if (proxy_retry)
1564 {
1565 openvpn_close_socket(sock->sd);
1566 sock->sd = create_socket_tcp(sock->info.lsa->current_remote);
1567 }
1568
1569 } while (proxy_retry);
1570}
1571
1572static void
1573phase2_socks_client(struct link_socket *sock, struct signal_info *sig_info)
1574{
1575 socket_connect(&sock->ctrl_sd, sock->info.lsa->current_remote->ai_addr,
1577
1578 if (sig_info->signal_received)
1579 {
1580 return;
1581 }
1582
1584 sock->server_poll_timeout, sig_info);
1585
1586 if (sig_info->signal_received)
1587 {
1588 return;
1589 }
1590
1591 sock->remote_host = sock->proxy_dest_host;
1592 sock->remote_port = sock->proxy_dest_port;
1593
1595 if (sock->info.lsa->remote_list)
1596 {
1597 freeaddrinfo(sock->info.lsa->remote_list);
1598 sock->info.lsa->current_remote = NULL;
1599 sock->info.lsa->remote_list = NULL;
1600 }
1601
1602 resolve_remote(sock, 1, sig_info);
1603}
1604
1605#if defined(_WIN32)
1606static void
1607create_socket_dco_win(struct context *c, struct link_socket *sock, struct signal_info *sig_info)
1608{
1609 /* in P2P mode we must have remote resolved at this point */
1610 const struct addrinfo *remoteaddr = sock->info.lsa->current_remote;
1611 if ((c->options.mode == MODE_POINT_TO_POINT) && (!remoteaddr))
1612 {
1613 return;
1614 }
1615
1616 if (!c->c1.tuntap)
1617 {
1618 struct tuntap *tt;
1619 ALLOC_OBJ_CLEAR(tt, struct tuntap);
1620
1623
1624 const char *device_guid = NULL; /* not used */
1625 tun_open_device(tt, c->options.dev_node, &device_guid, &c->gc);
1626
1627 /* Ensure we can "safely" cast the handle to a socket */
1628 static_assert(sizeof(sock->sd) == sizeof(tt->hand), "HANDLE and SOCKET size differs");
1629
1630 c->c1.tuntap = tt;
1631 }
1632
1633 if (c->options.mode == MODE_SERVER)
1634 {
1635 dco_mp_start_vpn(c->c1.tuntap->hand, sock);
1636 }
1637 else
1638 {
1639 dco_p2p_new_peer(c->c1.tuntap->hand, &c->c1.tuntap->dco_new_peer_ov, sock, sig_info);
1640 }
1641 sock->sockflags |= SF_DCO_WIN;
1642
1643 if (sig_info->signal_received)
1644 {
1645 return;
1646 }
1647
1648 sock->sd = (SOCKET)c->c1.tuntap->hand;
1649 linksock_print_addr(sock);
1650}
1651#endif /* if defined(_WIN32) */
1652
1653/* finalize socket initialization */
1654void
1656{
1657 const struct frame *frame = &c->c2.frame;
1658 struct signal_info *sig_info = c->sig;
1659
1660 struct signal_info sig_save = { 0 };
1661
1662 ASSERT(sock);
1663 ASSERT(sig_info);
1664
1665 if (sig_info->signal_received)
1666 {
1667 sig_save = *sig_info;
1668 sig_save.signal_received = signal_reset(sig_info, 0);
1669 }
1670
1671 /* initialize buffers */
1672 socket_frame_init(frame, sock);
1673
1674 /* Second chance to resolv/create socket */
1675 resolve_remote(sock, 2, sig_info);
1676
1677 /* If a valid remote has been found, create the socket with its addrinfo */
1678#if defined(_WIN32)
1679 if (dco_enabled(&c->options))
1680 {
1681 create_socket_dco_win(c, sock, sig_info);
1682 goto done;
1683 }
1684#endif
1685 if (sock->info.lsa->current_remote)
1686 {
1687 create_socket(sock, sock->info.lsa->current_remote);
1688 }
1689
1690 /* If socket has not already been created create it now */
1691 if (sock->sd == SOCKET_UNDEFINED)
1692 {
1693 /* If we have no --remote and have still not figured out the
1694 * protocol family to use we will use the first of the bind */
1695
1696 if (sock->bind_local && !sock->remote_host && sock->info.lsa->bind_local)
1697 {
1698 /* Warn if this is because neither v4 or v6 was specified
1699 * and we should not connect a remote */
1700 if (sock->info.af == AF_UNSPEC)
1701 {
1702 sock->info.af = (sa_family_t)sock->info.lsa->bind_local->ai_family;
1703 msg(M_WARN, "Could not determine IPv4/IPv6 protocol. Using %s",
1704 addr_family_name(sock->info.af));
1705 }
1706 create_socket(sock, sock->info.lsa->bind_local);
1707 }
1708 }
1709
1710 /* Socket still undefined, give a warning and abort connection */
1711 if (sock->sd == SOCKET_UNDEFINED)
1712 {
1713 msg(M_WARN, "Could not determine IPv4/IPv6 protocol");
1714 register_signal(sig_info, SIGUSR1, "Could not determine IPv4/IPv6 protocol");
1715 goto done;
1716 }
1717
1718 if (sig_info->signal_received)
1719 {
1720 goto done;
1721 }
1722
1723 if (sock->info.proto == PROTO_TCP_SERVER)
1724 {
1725 phase2_tcp_server(sock, sig_info);
1726 }
1727 else if (sock->info.proto == PROTO_TCP_CLIENT)
1728 {
1729 phase2_tcp_client(sock, sig_info);
1730 }
1731 else if (sock->info.proto == PROTO_UDP && sock->socks_proxy)
1732 {
1733 phase2_socks_client(sock, sig_info);
1734 }
1735#ifdef TARGET_ANDROID
1736 if (sock->sd != -1)
1737 {
1738 protect_fd_nonlocal(sock->sd, &sock->info.lsa->actual.dest.addr.sa);
1739 }
1740#endif
1741 if (sig_info->signal_received)
1742 {
1743 goto done;
1744 }
1745
1747 linksock_print_addr(sock);
1748
1749done:
1750 if (sig_save.signal_received)
1751 {
1752 /* Always restore the saved signal -- register/throw_signal will handle priority */
1753 if (sig_save.source == SIG_SOURCE_HARD && sig_info == &siginfo_static)
1754 {
1755 throw_signal(sig_save.signal_received);
1756 }
1757 else
1758 {
1759 register_signal(sig_info, sig_save.signal_received, sig_save.signal_text);
1760 }
1761 }
1762}
1763
1764void
1766{
1767 if (sock)
1768 {
1769#ifdef ENABLE_DEBUG
1770 const int gremlin = GREMLIN_CONNECTION_FLOOD_LEVEL(sock->gremlin);
1771#else
1772 const int gremlin = 0;
1773#endif
1774
1775 if (socket_defined(sock->sd))
1776 {
1777#ifdef _WIN32
1778 close_net_event_win32(&sock->listen_handle, sock->sd, 0);
1779#endif
1780 if (!gremlin)
1781 {
1782 msg(D_LOW, "TCP/UDP: Closing socket");
1783 if (openvpn_close_socket(sock->sd))
1784 {
1785 msg(M_WARN | M_ERRNO, "TCP/UDP: Close Socket failed");
1786 }
1787 }
1788 sock->sd = SOCKET_UNDEFINED;
1789#ifdef _WIN32
1790 if (!gremlin)
1791 {
1792 overlapped_io_close(&sock->reads);
1794 }
1795#endif
1796 }
1797
1798 if (socket_defined(sock->ctrl_sd))
1799 {
1800 if (openvpn_close_socket(sock->ctrl_sd))
1801 {
1802 msg(M_WARN | M_ERRNO, "TCP/UDP: Close Socket (ctrl_sd) failed");
1803 }
1804 sock->ctrl_sd = SOCKET_UNDEFINED;
1805 }
1806
1808 free_buf(&sock->stream_buf_data);
1809 if (!gremlin)
1810 {
1811 free(sock);
1812 }
1813 }
1814}
1815
1816void
1817setenv_trusted(struct env_set *es, const struct link_socket_info *info)
1818{
1819 setenv_link_socket_actual(es, "trusted", &info->lsa->actual, SA_IP_PORT);
1820}
1821
1822static void
1823ipchange_fmt(const bool include_cmd, struct argv *argv, const struct link_socket_info *info,
1824 struct gc_arena *gc)
1825{
1826 const char *host = print_sockaddr_ex(&info->lsa->actual.dest.addr.sa, " ", PS_SHOW_PORT, gc);
1827 if (include_cmd)
1828 {
1830 argv_printf_cat(argv, "%s", host);
1831 }
1832 else
1833 {
1834 argv_printf(argv, "%s", host);
1835 }
1836}
1837
1838void
1840 const struct link_socket_actual *act, const char *common_name,
1841 struct env_set *es)
1842{
1843 struct gc_arena gc = gc_new();
1844
1845 info->lsa->actual = *act; /* Note: skip this line for --force-dest */
1846 setenv_trusted(es, info);
1847 info->connection_established = true;
1848
1849 /* Print connection initiated message, with common name if available */
1850 {
1851 struct buffer out = alloc_buf_gc(256, &gc);
1852 if (common_name)
1853 {
1854 buf_printf(&out, "[%s] ", common_name);
1855 }
1856 buf_printf(&out, "Peer Connection Initiated with %s",
1858 msg(M_INFO, "%s", BSTR(&out));
1859 }
1860
1861 /* set environmental vars */
1862 setenv_str(es, "common_name", common_name);
1863
1864 /* Process --ipchange plugin */
1866 {
1867 struct argv argv = argv_new();
1868 ipchange_fmt(false, &argv, info, &gc);
1869 if (plugin_call(info->plugins, OPENVPN_PLUGIN_IPCHANGE, &argv, NULL, es)
1870 != OPENVPN_PLUGIN_FUNC_SUCCESS)
1871 {
1872 msg(M_WARN, "WARNING: ipchange plugin call failed");
1873 }
1874 argv_free(&argv);
1875 }
1876
1877 /* Process --ipchange option */
1878 if (info->ipchange_command)
1879 {
1880 struct argv argv = argv_new();
1881 setenv_str(es, "script_type", "ipchange");
1882 ipchange_fmt(true, &argv, info, &gc);
1883 openvpn_run_script(&argv, es, 0, "--ipchange");
1884 argv_free(&argv);
1885 }
1886
1887 gc_free(&gc);
1888}
1889
1890void
1892 const struct link_socket_actual *from_addr)
1893{
1894 struct gc_arena gc = gc_new();
1895 const struct addrinfo *ai;
1896
1897 switch (from_addr->dest.addr.sa.sa_family)
1898 {
1899 case AF_INET:
1900 case AF_INET6:
1902 "TCP/UDP: Incoming packet rejected from %s[%d], expected peer address: %s (allow this incoming source address/port by removing --remote or adding --float)",
1903 print_link_socket_actual(from_addr, &gc), (int)from_addr->dest.addr.sa.sa_family,
1904 print_sockaddr_ex(info->lsa->remote_list->ai_addr, ":", PS_SHOW_PORT, &gc));
1905 /* print additional remote addresses */
1906 for (ai = info->lsa->remote_list->ai_next; ai; ai = ai->ai_next)
1907 {
1908 msg(D_LINK_ERRORS, "or from peer address: %s",
1909 print_sockaddr_ex(ai->ai_addr, ":", PS_SHOW_PORT, &gc));
1910 }
1911 break;
1912 }
1913 buf->len = 0;
1914 gc_free(&gc);
1915}
1916
1917void
1919{
1920 dmsg(D_READ_WRITE, "TCP/UDP: No outgoing address to send packet");
1921}
1922
1925{
1926 const struct link_socket_addr *lsa = info->lsa;
1927
1928 /*
1929 * This logic supports "redirect-gateway" semantic, which
1930 * makes sense only for PF_INET routes over PF_INET endpoints
1931 *
1932 * Maybe in the future consider PF_INET6 endpoints also ...
1933 * by now just ignore it
1934 *
1935 * For --remote entries with multiple addresses this
1936 * only return the actual endpoint we have successfully connected to
1937 */
1938 if (lsa->actual.dest.addr.sa.sa_family != AF_INET)
1939 {
1940 return IPV4_INVALID_ADDR;
1941 }
1942
1944 {
1945 return ntohl(lsa->actual.dest.addr.in4.sin_addr.s_addr);
1946 }
1947 else if (lsa->current_remote)
1948 {
1949 return ntohl(((struct sockaddr_in *)lsa->current_remote->ai_addr)->sin_addr.s_addr);
1950 }
1951 else
1952 {
1953 return 0;
1954 }
1955}
1956
1957const struct in6_addr *
1959{
1960 const struct link_socket_addr *lsa = info->lsa;
1961
1962 /* This logic supports "redirect-gateway" semantic,
1963 * for PF_INET6 routes over PF_INET6 endpoints
1964 *
1965 * For --remote entries with multiple addresses this
1966 * only return the actual endpoint we have successfully connected to
1967 */
1968 if (lsa->actual.dest.addr.sa.sa_family != AF_INET6)
1969 {
1970 return NULL;
1971 }
1972
1974 {
1975 return &(lsa->actual.dest.addr.in6.sin6_addr);
1976 }
1977 else if (lsa->current_remote)
1978 {
1979 return &(((struct sockaddr_in6 *)lsa->current_remote->ai_addr)->sin6_addr);
1980 }
1981 else
1982 {
1983 return NULL;
1984 }
1985}
1986
1987/*
1988 * Return a status string describing socket state.
1989 */
1990const char *
1991socket_stat(const struct link_socket *s, unsigned int rwflags, struct gc_arena *gc)
1992{
1993 struct buffer out = alloc_buf_gc(64, gc);
1994 if (s)
1995 {
1996 if (rwflags & EVENT_READ)
1997 {
1998 buf_printf(&out, "S%s", (s->rwflags_debug & EVENT_READ) ? "R" : "r");
1999#ifdef _WIN32
2000 buf_printf(&out, "%s", overlapped_io_state_ascii(&s->reads));
2001#endif
2002 }
2003 if (rwflags & EVENT_WRITE)
2004 {
2005 buf_printf(&out, "S%s", (s->rwflags_debug & EVENT_WRITE) ? "W" : "w");
2006#ifdef _WIN32
2008#endif
2009 }
2010 }
2011 else
2012 {
2013 buf_printf(&out, "S?");
2014 }
2015 return BSTR(&out);
2016}
2017
2018/*
2019 * Stream buffer functions, used to packetize a TCP
2020 * stream connection.
2021 */
2022
2030static inline void
2032{
2033 dmsg(D_STREAM_DEBUG, "STREAM: RESET");
2034 sb->residual_fully_formed = false;
2035 sb->buf = sb->buf_init;
2036 sb->len = -1;
2037}
2038
2039static void
2040stream_buf_init(struct stream_buf *sb, struct buffer *buf, const unsigned int sockflags,
2041 const int proto)
2042{
2043 sb->buf_init = *buf;
2044 sb->maxlen = sb->buf_init.len;
2045 sb->buf_init.len = 0;
2046 sb->residual = alloc_buf(sb->maxlen);
2047 sb->error = false;
2048#if PORT_SHARE
2049 sb->port_share_state =
2050 ((sockflags & SF_PORT_SHARE) && (proto == PROTO_TCP_SERVER)) ? PS_ENABLED : PS_DISABLED;
2051#endif
2053
2054 dmsg(D_STREAM_DEBUG, "STREAM: INIT maxlen=%d", sb->maxlen);
2055}
2056
2063static inline struct buffer
2065{
2066 /* set up 'next' for next i/o read */
2067 struct buffer next;
2068 next = sb->buf;
2069 next.offset = sb->buf.offset + sb->buf.len;
2070 next.len = (sb->len >= 0 ? sb->len : sb->maxlen) - sb->buf.len;
2071 dmsg(D_STREAM_DEBUG, "STREAM: GET NEXT, buf=[%d,%d] next=[%d,%d] len=%d maxlen=%d",
2072 sb->buf.offset, sb->buf.len, next.offset, next.len, sb->len, sb->maxlen);
2073 ASSERT(next.len > 0);
2074 ASSERT(buf_safe(&sb->buf, next.len));
2075 return next;
2076}
2077
2086static inline void
2088{
2089 dmsg(D_STREAM_DEBUG, "STREAM: GET FINAL len=%d", buf_defined(&sb->buf) ? sb->buf.len : -1);
2090 ASSERT(buf_defined(&sb->buf));
2091 *buf = sb->buf;
2092}
2093
2094bool
2096{
2097 if (sb->residual.len && !sb->residual_fully_formed)
2098 {
2099 ASSERT(buf_copy(&sb->buf, &sb->residual));
2100 ASSERT(buf_init(&sb->residual, 0));
2101 sb->residual_fully_formed = stream_buf_added(sb, 0);
2102 dmsg(D_STREAM_DEBUG, "STREAM: RESIDUAL FULLY FORMED [%s], len=%d",
2103 sb->residual_fully_formed ? "YES" : "NO", sb->residual.len);
2104 }
2105
2106 return !sb->residual_fully_formed;
2107}
2108
2130static bool
2132{
2133 dmsg(D_STREAM_DEBUG, "STREAM: ADD length_added=%zd", length_added);
2134 if (length_added > 0)
2135 {
2136 ASSERT(sb->buf.len + length_added <= INT_MAX);
2137 sb->buf.len += (int)length_added;
2138 }
2139
2140 /* if length unknown, see if we can get the length prefix from
2141 * the head of the buffer */
2142 if (sb->len < 0 && sb->buf.len >= (int)sizeof(packet_size_type))
2143 {
2145
2146#if PORT_SHARE
2147 if (sb->port_share_state == PS_ENABLED)
2148 {
2149 if (!is_openvpn_protocol(&sb->buf))
2150 {
2151 msg(D_PS_PROXY, "Non-OpenVPN client protocol detected");
2152 sb->port_share_state = PS_FOREIGN;
2153 sb->error = true;
2154 return false;
2155 }
2156 else
2157 {
2158 sb->port_share_state = PS_DISABLED;
2159 }
2160 }
2161#endif
2162
2163 ASSERT(buf_read(&sb->buf, &net_size, sizeof(net_size)));
2164 sb->len = ntohps(net_size);
2165
2166 if (sb->len < 1 || sb->len > sb->maxlen)
2167 {
2168 msg(M_WARN,
2169 "WARNING: Bad encapsulated packet length from peer (%d), which must be > 0 and <= %d -- please ensure that --tun-mtu or --link-mtu is equal on both peers -- this condition could also indicate a possible active attack on the TCP link -- [Attempting restart...]",
2170 sb->len, sb->maxlen);
2172 sb->error = true;
2173 return false;
2174 }
2175 }
2176
2177 /* is our incoming packet fully read? */
2178 if (sb->len > 0 && sb->buf.len >= sb->len)
2179 {
2180 /* save any residual data that's part of the next packet */
2181 ASSERT(buf_init(&sb->residual, 0));
2182 if (sb->buf.len > sb->len)
2183 {
2184 ASSERT(buf_copy_excess(&sb->residual, &sb->buf, sb->len));
2185 }
2186 dmsg(D_STREAM_DEBUG, "STREAM: ADD returned TRUE, buf_len=%d, residual_len=%d",
2187 BLEN(&sb->buf), BLEN(&sb->residual));
2188 return true;
2189 }
2190 else
2191 {
2192 dmsg(D_STREAM_DEBUG, "STREAM: ADD returned FALSE (have=%d need=%d)", sb->buf.len, sb->len);
2193 return false;
2194 }
2195}
2196
2197static void
2199{
2200 free_buf(&sb->residual);
2201}
2202
2203/*
2204 * The listen event is a special event whose sole purpose is
2205 * to tell us that there's a new incoming connection on a
2206 * TCP socket, for use in server mode.
2207 */
2208event_t
2210{
2211#ifdef _WIN32
2213 {
2215 }
2216 return &s->listen_handle;
2217#else /* ifdef _WIN32 */
2218 return s->sd;
2219#endif
2220}
2221
2222
2223/*
2224 * Bad incoming address lengths that differ from what
2225 * we expect are considered to be fatal errors.
2226 */
2227void
2229{
2230 msg(M_FATAL,
2231 "ERROR: received strange incoming packet with an address length of %d -- we only accept address lengths of %d.",
2232 actual, expected);
2233}
2234
2235/*
2236 * Socket Read Routines
2237 */
2238
2239ssize_t
2240link_socket_read_tcp(struct link_socket *sock, struct buffer *buf)
2241{
2242 ssize_t len = 0;
2243
2245 {
2246 /* with Linux-DCO, we sometimes try to access a socket that is
2247 * already installed in the kernel and has no valid file descriptor
2248 * anymore. This is a bug.
2249 * Handle by resetting client instance instead of crashing.
2250 */
2251 if (sock->sd == SOCKET_UNDEFINED)
2252 {
2253 msg(M_INFO, "BUG: link_socket_read_tcp(): sock->sd==-1, reset client instance");
2254 sock->stream_reset = true; /* reset client instance */
2255 return buf->len = 0; /* nothing to read */
2256 }
2257
2258#ifdef _WIN32
2259 sockethandle_t sh = { .s = sock->sd };
2260 len = sockethandle_finalize(sh, &sock->reads, buf, NULL);
2261#else
2262 struct buffer frag = stream_buf_get_next(&sock->stream_buf);
2263 len = recv(sock->sd, BPTR(&frag), BLENZ(&frag), MSG_NOSIGNAL);
2264#endif
2265
2266 if (!len)
2267 {
2268 sock->stream_reset = true;
2269 }
2270 if (len <= 0)
2271 {
2272 buf->len = 0;
2273 return len;
2274 }
2275 }
2276
2278 || stream_buf_added(&sock->stream_buf, len)) /* packet complete? */
2279 {
2280 stream_buf_get_final(&sock->stream_buf, buf);
2282 return buf->len;
2283 }
2284 else
2285 {
2286 return buf->len = 0; /* no error, but packet is still incomplete */
2287 }
2288}
2289
2290#ifndef _WIN32
2291
2292#if ENABLE_IP_PKTINFO
2293
2294/* make the buffer large enough to handle ancillary socket data for
2295 * both IPv4 and IPv6 destination addresses, plus padding (see RFC 2292)
2296 */
2297#if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST)
2298#define PKTINFO_BUF_SIZE \
2299 max_int(CMSG_SPACE(sizeof(struct in6_pktinfo)), CMSG_SPACE(sizeof(struct in_pktinfo)))
2300#else
2301#define PKTINFO_BUF_SIZE \
2302 max_int(CMSG_SPACE(sizeof(struct in6_pktinfo)), CMSG_SPACE(sizeof(struct in_addr)))
2303#endif
2304
2305static ssize_t
2306link_socket_read_udp_posix_recvmsg(struct link_socket *sock, struct buffer *buf,
2307 struct link_socket_actual *from, socklen_t *fromlen)
2308{
2309 struct iovec iov;
2310 uint8_t pktinfo_buf[PKTINFO_BUF_SIZE];
2311 struct msghdr mesg = { 0 };
2312 *fromlen = sizeof(from->dest.addr);
2313
2314 ASSERT(sock->sd >= 0); /* can't happen */
2315
2316 iov.iov_base = BPTR(buf);
2317 iov.iov_len = buf_forward_capacity_total(buf);
2318 mesg.msg_iov = &iov;
2319 mesg.msg_iovlen = 1;
2320 mesg.msg_name = &from->dest.addr;
2321 mesg.msg_namelen = *fromlen;
2322 mesg.msg_control = pktinfo_buf;
2323 mesg.msg_controllen = (socklen_t)sizeof(pktinfo_buf);
2324 ssize_t len = recvmsg(sock->sd, &mesg, 0);
2325 if (len < 0)
2326 {
2327 buf->len = 0;
2328 return len;
2329 }
2330 ASSERT(len <= INT_MAX);
2331 buf->len = (int)len;
2332 struct cmsghdr *cmsg;
2333 *fromlen = mesg.msg_namelen;
2334 cmsg = CMSG_FIRSTHDR(&mesg);
2335 if (cmsg != NULL && CMSG_NXTHDR(&mesg, cmsg) == NULL
2336#if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST)
2337 && cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_PKTINFO
2338 && cmsg->cmsg_len >= CMSG_LEN(sizeof(struct in_pktinfo)))
2339#elif defined(IP_RECVDSTADDR)
2340 && cmsg->cmsg_level == IPPROTO_IP && cmsg->cmsg_type == IP_RECVDSTADDR
2341 && cmsg->cmsg_len >= CMSG_LEN(sizeof(struct in_addr)))
2342#else /* if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST) */
2343#error ENABLE_IP_PKTINFO is set without IP_PKTINFO xor IP_RECVDSTADDR (fix syshead.h)
2344#endif
2345 {
2346#if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST)
2347 struct in_pktinfo *pkti = (struct in_pktinfo *)CMSG_DATA(cmsg);
2348 from->pi.in4.ipi_ifindex =
2349 (sock->sockflags & SF_PKTINFO_COPY_IIF) ? pkti->ipi_ifindex : 0;
2350 from->pi.in4.ipi_spec_dst = pkti->ipi_spec_dst;
2351#elif defined(IP_RECVDSTADDR)
2352 from->pi.in4 = *(struct in_addr *)CMSG_DATA(cmsg);
2353#else /* if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST) */
2354#error ENABLE_IP_PKTINFO is set without IP_PKTINFO xor IP_RECVDSTADDR (fix syshead.h)
2355#endif
2356 }
2357 else if (cmsg != NULL && CMSG_NXTHDR(&mesg, cmsg) == NULL
2358 && cmsg->cmsg_level == IPPROTO_IPV6 && cmsg->cmsg_type == IPV6_PKTINFO
2359 && cmsg->cmsg_len >= CMSG_LEN(sizeof(struct in6_pktinfo)))
2360 {
2361 struct in6_pktinfo *pkti6 = (struct in6_pktinfo *)CMSG_DATA(cmsg);
2362 from->pi.in6.ipi6_ifindex =
2363 (sock->sockflags & SF_PKTINFO_COPY_IIF) ? pkti6->ipi6_ifindex : 0;
2364 from->pi.in6.ipi6_addr = pkti6->ipi6_addr;
2365 }
2366 else if (cmsg != NULL)
2367 {
2368 msg(M_WARN,
2369 "CMSG received that cannot be parsed (cmsg_level=%d, cmsg_type=%d, cmsg=len=%zu)",
2370 cmsg->cmsg_level, cmsg->cmsg_type, (size_t)cmsg->cmsg_len);
2371 }
2372
2373 return buf->len;
2374}
2375#endif /* if ENABLE_IP_PKTINFO */
2376
2377ssize_t
2378link_socket_read_udp_posix(struct link_socket *sock, struct buffer *buf,
2379 struct link_socket_actual *from)
2380{
2381 ssize_t recvlen;
2382 socklen_t fromlen = sizeof(from->dest.addr);
2383 socklen_t expectedlen = af_addr_size(sock->info.af);
2384 addr_zero_host(&from->dest);
2385
2386 ASSERT(sock->sd >= 0); /* can't happen */
2387
2388#if ENABLE_IP_PKTINFO
2389 /* Both PROTO_UDPv4 and PROTO_UDPv6 */
2390 if (sock->info.proto == PROTO_UDP && sock->sockflags & SF_USE_IP_PKTINFO)
2391 {
2392 recvlen = link_socket_read_udp_posix_recvmsg(sock, buf, from, &fromlen);
2393 }
2394 else
2395#endif
2396 {
2397 recvlen = recvfrom(sock->sd, BPTR(buf), buf_forward_capacity(buf), 0,
2398 &from->dest.addr.sa, &fromlen);
2399 }
2400 if (recvlen < 0)
2401 {
2402 buf->len = 0;
2403 return recvlen;
2404 }
2405 ASSERT(recvlen <= INT_MAX);
2406 buf->len = (int)recvlen;
2407 /* FIXME: won't do anything when sock->info.af == AF_UNSPEC */
2408 if (expectedlen && fromlen != expectedlen)
2409 {
2410 bad_address_length(fromlen, expectedlen);
2411 }
2412 return buf->len;
2413}
2414
2415#endif /* ifndef _WIN32 */
2416
2417/*
2418 * Socket Write Routines
2419 */
2420
2421ssize_t
2422link_socket_write_tcp(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
2423{
2424 const int blen = BLEN(buf);
2425 ASSERT(blen >= 0 && blen <= PACKET_SIZE_MAX);
2427 dmsg(D_STREAM_DEBUG, "STREAM: WRITE %u offset=%d", len, buf->offset);
2428 ASSERT(len <= sock->stream_buf.maxlen);
2429 len = htonps(len);
2430 ASSERT(buf_write_prepend(buf, &len, sizeof(len)));
2431#ifdef _WIN32
2432 return link_socket_write_win32(sock, buf, to);
2433#else
2434 return link_socket_write_tcp_posix(sock, buf);
2435#endif
2436}
2437
2438#if ENABLE_IP_PKTINFO
2439
2440ssize_t
2441link_socket_write_udp_posix_sendmsg(struct link_socket *sock, struct buffer *buf,
2442 struct link_socket_actual *to)
2443{
2444 struct iovec iov;
2445 struct msghdr mesg;
2446 struct cmsghdr *cmsg;
2447 uint8_t pktinfo_buf[PKTINFO_BUF_SIZE];
2448
2449 iov.iov_base = BPTR(buf);
2450 iov.iov_len = BLENZ(buf);
2451 mesg.msg_iov = &iov;
2452 mesg.msg_iovlen = 1;
2453 switch (to->dest.addr.sa.sa_family)
2454 {
2455 case AF_INET:
2456 {
2457 mesg.msg_name = &to->dest.addr.sa;
2458 mesg.msg_namelen = sizeof(struct sockaddr_in);
2459 mesg.msg_control = pktinfo_buf;
2460 mesg.msg_flags = 0;
2461#if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST)
2462 mesg.msg_controllen = CMSG_SPACE(sizeof(struct in_pktinfo));
2463 cmsg = CMSG_FIRSTHDR(&mesg);
2464 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
2465 cmsg->cmsg_level = SOL_IP;
2466 cmsg->cmsg_type = IP_PKTINFO;
2467 {
2468 struct in_pktinfo *pkti;
2469 pkti = (struct in_pktinfo *)CMSG_DATA(cmsg);
2470 pkti->ipi_ifindex = to->pi.in4.ipi_ifindex;
2471 pkti->ipi_spec_dst = to->pi.in4.ipi_spec_dst;
2472 pkti->ipi_addr.s_addr = 0;
2473 }
2474#elif defined(IP_RECVDSTADDR)
2475 ASSERT(CMSG_SPACE(sizeof(struct in_addr)) <= sizeof(pktinfo_buf));
2476 mesg.msg_controllen = CMSG_SPACE(sizeof(struct in_addr));
2477 cmsg = CMSG_FIRSTHDR(&mesg);
2478 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_addr));
2479 cmsg->cmsg_level = IPPROTO_IP;
2480 cmsg->cmsg_type = IP_RECVDSTADDR;
2481 *(struct in_addr *)CMSG_DATA(cmsg) = to->pi.in4;
2482#else /* if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST) */
2483#error ENABLE_IP_PKTINFO is set without IP_PKTINFO xor IP_RECVDSTADDR (fix syshead.h)
2484#endif /* if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST) */
2485 break;
2486 }
2487
2488 case AF_INET6:
2489 {
2490 struct in6_pktinfo *pkti6;
2491 mesg.msg_name = &to->dest.addr.sa;
2492 mesg.msg_namelen = sizeof(struct sockaddr_in6);
2493
2494 ASSERT(CMSG_SPACE(sizeof(struct in6_pktinfo)) <= sizeof(pktinfo_buf));
2495 mesg.msg_control = pktinfo_buf;
2496 mesg.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
2497 mesg.msg_flags = 0;
2498 cmsg = CMSG_FIRSTHDR(&mesg);
2499 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
2500 cmsg->cmsg_level = IPPROTO_IPV6;
2501 cmsg->cmsg_type = IPV6_PKTINFO;
2502
2503 pkti6 = (struct in6_pktinfo *)CMSG_DATA(cmsg);
2504 pkti6->ipi6_ifindex = to->pi.in6.ipi6_ifindex;
2505 pkti6->ipi6_addr = to->pi.in6.ipi6_addr;
2506 break;
2507 }
2508
2509 default:
2510 ASSERT(0);
2511 }
2512 return sendmsg(sock->sd, &mesg, 0);
2513}
2514
2515#endif /* if ENABLE_IP_PKTINFO */
2516
2517/*
2518 * Win32 overlapped socket I/O functions.
2519 */
2520
2521#ifdef _WIN32
2522
2523static int
2525{
2526 if (socket_is_dco_win(sock))
2527 {
2528 return GetLastError();
2529 }
2530
2531 return WSAGetLastError();
2532}
2533
2534int
2535socket_recv_queue(struct link_socket *sock, int maxsize)
2536{
2537 if (sock->reads.iostate == IOSTATE_INITIAL)
2538 {
2539 WSABUF wsabuf[1];
2540 int status;
2541
2542 /* reset buf to its initial state */
2543 if (proto_is_udp(sock->info.proto))
2544 {
2545 sock->reads.buf = sock->reads.buf_init;
2546 }
2547 else if (proto_is_tcp(sock->info.proto))
2548 {
2549 sock->reads.buf = stream_buf_get_next(&sock->stream_buf);
2550 }
2551 else
2552 {
2553 ASSERT(0);
2554 }
2555
2556 /* Win32 docs say it's okay to allocate the wsabuf on the stack */
2557 wsabuf[0].buf = BSTR(&sock->reads.buf);
2558 /* make sure maxsize is sane */
2559 ASSERT(maxsize <= BLEN(&sock->reads.buf));
2560 wsabuf[0].len = maxsize ? maxsize : BLEN(&sock->reads.buf);
2561
2562 /* the overlapped read will signal this event on I/O completion */
2563 ASSERT(ResetEvent(sock->reads.overlapped.hEvent));
2564 sock->reads.flags = 0;
2565
2566 if (socket_is_dco_win(sock))
2567 {
2568 status = ReadFile((HANDLE)sock->sd, wsabuf[0].buf, wsabuf[0].len, &sock->reads.size,
2569 &sock->reads.overlapped);
2570 /* Readfile status is inverted from WSARecv */
2571 status = !status;
2572 }
2573 else if (proto_is_udp(sock->info.proto))
2574 {
2575 sock->reads.addr_defined = true;
2576 sock->reads.addrlen = sizeof(sock->reads.addr6);
2577 status = WSARecvFrom(sock->sd, wsabuf, 1, &sock->reads.size, &sock->reads.flags,
2578 (struct sockaddr *)&sock->reads.addr, &sock->reads.addrlen,
2579 &sock->reads.overlapped, NULL);
2580 }
2581 else if (proto_is_tcp(sock->info.proto))
2582 {
2583 sock->reads.addr_defined = false;
2584 status = WSARecv(sock->sd, wsabuf, 1, &sock->reads.size, &sock->reads.flags,
2585 &sock->reads.overlapped, NULL);
2586 }
2587 else
2588 {
2589 status = 0;
2590 ASSERT(0);
2591 }
2592
2593 if (!status) /* operation completed immediately? */
2594 {
2595 /* FIXME: won't do anything when sock->info.af == AF_UNSPEC */
2596 int af_len = af_addr_size(sock->info.af);
2597 if (sock->reads.addr_defined && af_len && sock->reads.addrlen != af_len)
2598 {
2599 bad_address_length(sock->reads.addrlen, af_len);
2600 }
2602
2603 /* since we got an immediate return, we must signal the event object ourselves */
2604 ASSERT(SetEvent(sock->reads.overlapped.hEvent));
2605 sock->reads.status = 0;
2606
2607 dmsg(D_WIN32_IO, "WIN32 I/O: Socket Receive immediate return [%d,%d]",
2608 (int)wsabuf[0].len, (int)sock->reads.size);
2609 }
2610 else
2611 {
2613 if (status == WSA_IO_PENDING) /* operation queued? */
2614 {
2616 sock->reads.status = status;
2617 dmsg(D_WIN32_IO, "WIN32 I/O: Socket Receive queued [%d]", (int)wsabuf[0].len);
2618 }
2619 else /* error occurred */
2620 {
2621 struct gc_arena gc = gc_new();
2622 ASSERT(SetEvent(sock->reads.overlapped.hEvent));
2624 sock->reads.status = status;
2625 dmsg(D_WIN32_IO, "WIN32 I/O: Socket Receive error [%d]: %s", (int)wsabuf[0].len,
2627 gc_free(&gc);
2628 }
2629 }
2630 }
2631 return sock->reads.iostate;
2632}
2633
2634int
2635socket_send_queue(struct link_socket *sock, struct buffer *buf, const struct link_socket_actual *to)
2636{
2637 if (sock->writes.iostate == IOSTATE_INITIAL)
2638 {
2639 WSABUF wsabuf[1];
2640 int status;
2641
2642 /* make a private copy of buf */
2643 sock->writes.buf = sock->writes.buf_init;
2644 sock->writes.buf.len = 0;
2645 ASSERT(buf_copy(&sock->writes.buf, buf));
2646
2647 /* Win32 docs say it's okay to allocate the wsabuf on the stack */
2648 wsabuf[0].buf = BSTR(&sock->writes.buf);
2649 wsabuf[0].len = BLEN(&sock->writes.buf);
2650
2651 /* the overlapped write will signal this event on I/O completion */
2652 ASSERT(ResetEvent(sock->writes.overlapped.hEvent));
2653 sock->writes.flags = 0;
2654
2655 if (socket_is_dco_win(sock))
2656 {
2657 status = WriteFile((HANDLE)sock->sd, wsabuf[0].buf, wsabuf[0].len, &sock->writes.size,
2658 &sock->writes.overlapped);
2659
2660 /* WriteFile status is inverted from WSASendTo */
2661 status = !status;
2662 }
2663 else if (proto_is_udp(sock->info.proto))
2664 {
2665 /* set destination address for UDP writes */
2666 sock->writes.addr_defined = true;
2667 if (to->dest.addr.sa.sa_family == AF_INET6)
2668 {
2669 sock->writes.addr6 = to->dest.addr.in6;
2670 sock->writes.addrlen = sizeof(sock->writes.addr6);
2671 }
2672 else
2673 {
2674 sock->writes.addr = to->dest.addr.in4;
2675 sock->writes.addrlen = sizeof(sock->writes.addr);
2676 }
2677
2678 status = WSASendTo(sock->sd, wsabuf, 1, &sock->writes.size, sock->writes.flags,
2679 (struct sockaddr *)&sock->writes.addr, sock->writes.addrlen,
2680 &sock->writes.overlapped, NULL);
2681 }
2682 else if (proto_is_tcp(sock->info.proto))
2683 {
2684 /* destination address for TCP writes was established on connection initiation */
2685 sock->writes.addr_defined = false;
2686
2687 status = WSASend(sock->sd, wsabuf, 1, &sock->writes.size, sock->writes.flags,
2688 &sock->writes.overlapped, NULL);
2689 }
2690 else
2691 {
2692 status = 0;
2693 ASSERT(0);
2694 }
2695
2696 if (!status) /* operation completed immediately? */
2697 {
2699
2700 /* since we got an immediate return, we must signal the event object ourselves */
2701 ASSERT(SetEvent(sock->writes.overlapped.hEvent));
2702
2703 sock->writes.status = 0;
2704
2705 dmsg(D_WIN32_IO, "WIN32 I/O: Socket Send immediate return [%d,%d]", (int)wsabuf[0].len,
2706 (int)sock->writes.size);
2707 }
2708 else
2709 {
2711 /* both status code have the identical value */
2712 if (status == WSA_IO_PENDING || status == ERROR_IO_PENDING) /* operation queued? */
2713 {
2715 sock->writes.status = status;
2716 dmsg(D_WIN32_IO, "WIN32 I/O: Socket Send queued [%d]", (int)wsabuf[0].len);
2717 }
2718 else /* error occurred */
2719 {
2720 struct gc_arena gc = gc_new();
2721 ASSERT(SetEvent(sock->writes.overlapped.hEvent));
2723 sock->writes.status = status;
2724
2725 dmsg(D_WIN32_IO, "WIN32 I/O: Socket Send error [%d]: %s", (int)wsabuf[0].len,
2727
2728 gc_free(&gc);
2729 }
2730 }
2731 }
2732 return sock->writes.iostate;
2733}
2734
2735void
2736read_sockaddr_from_overlapped(struct overlapped_io *io, struct sockaddr *dst, int overlapped_ret)
2737{
2738 if (overlapped_ret >= 0 && io->addr_defined)
2739 {
2740 /* TODO(jjo): streamline this mess */
2741 /* in this func we don't have relevant info about the PF_ of this
2742 * endpoint, as link_socket_actual will be zero for the 1st received packet
2743 *
2744 * Test for inets PF_ possible sizes
2745 */
2746 switch (io->addrlen)
2747 {
2748 case sizeof(struct sockaddr_in):
2749 case sizeof(struct sockaddr_in6):
2750 /* TODO(jjo): for some reason (?) I'm getting 24,28 for AF_INET6
2751 * under _WIN32*/
2752 case sizeof(struct sockaddr_in6) - 4:
2753 break;
2754
2755 default:
2756 bad_address_length(io->addrlen, af_addr_size(io->addr.sin_family));
2757 }
2758
2759 switch (io->addr.sin_family)
2760 {
2761 case AF_INET:
2762 memcpy(dst, &io->addr, sizeof(struct sockaddr_in));
2763 break;
2764
2765 case AF_INET6:
2766 memcpy(dst, &io->addr6, sizeof(struct sockaddr_in6));
2767 break;
2768 }
2769 }
2770 else
2771 {
2772 CLEAR(*dst);
2773 }
2774}
2775
2785static int
2786read_sockaddr_from_packet(struct buffer *buf, struct sockaddr *dst)
2787{
2788 int sa_len = 0;
2789
2790 /* In dco-win multipeer mode the kernel driver always prepends a full
2791 * sockaddr_in or sockaddr_in6 in front of the control-packet payload,
2792 * so the buffer must hold at least sizeof(struct sockaddr_in) bytes
2793 * before we may inspect sa_family. */
2794 ASSERT(buf_len(buf) >= (int)sizeof(struct sockaddr_in));
2795
2796 const struct sockaddr *sa = (const struct sockaddr *)BPTR(buf);
2797 switch (sa->sa_family)
2798 {
2799 case AF_INET:
2800 sa_len = sizeof(struct sockaddr_in);
2801 break;
2802
2803 case AF_INET6:
2804 sa_len = sizeof(struct sockaddr_in6);
2805 ASSERT(buf_len(buf) >= sa_len);
2806 break;
2807
2808 default:
2809 ASSERT(0); /* driver validates the family before writing */
2810 }
2811
2812 memcpy(dst, sa, sa_len);
2813 buf_advance(buf, sa_len);
2814
2815 return sa_len;
2816}
2817
2818/* Returns the number of bytes successfully read */
2819int
2821 struct link_socket_actual *from)
2822{
2823 int ret = -1;
2824 BOOL status;
2825
2826 switch (io->iostate)
2827 {
2828 case IOSTATE_QUEUED:
2830 if (status)
2831 {
2832 /* successful return for a queued operation */
2833 if (buf)
2834 {
2835 *buf = io->buf;
2836 }
2837 ret = io->size;
2839 ASSERT(ResetEvent(io->overlapped.hEvent));
2840
2841 dmsg(D_WIN32_IO, "WIN32 I/O: Completion success [%d]", ret);
2842 }
2843 else
2844 {
2845 /* error during a queued operation */
2846 ret = -1;
2847 if (SocketHandleGetLastError(sh) != ERROR_IO_INCOMPLETE)
2848 {
2849 /* if no error (i.e. just not finished yet), then DON'T execute this code */
2851 ASSERT(ResetEvent(io->overlapped.hEvent));
2852 msg(D_WIN32_IO | M_ERRNO, "WIN32 I/O: Completion error");
2853 }
2854 }
2855 break;
2856
2859 ASSERT(ResetEvent(io->overlapped.hEvent));
2860 if (io->status)
2861 {
2862 /* error return for a non-queued operation */
2864 ret = -1;
2865 msg(D_WIN32_IO | M_ERRNO, "WIN32 I/O: Completion non-queued error");
2866 }
2867 else
2868 {
2869 /* successful return for a non-queued operation */
2870 if (buf)
2871 {
2872 *buf = io->buf;
2873 }
2874 ret = io->size;
2875 dmsg(D_WIN32_IO, "WIN32 I/O: Completion non-queued success [%d]", ret);
2876 }
2877 break;
2878
2879 case IOSTATE_INITIAL: /* were we called without proper queueing? */
2881 ret = -1;
2882 dmsg(D_WIN32_IO, "WIN32 I/O: Completion BAD STATE");
2883 break;
2884
2885 default:
2886 ASSERT(0);
2887 }
2888
2889 if (from && ret > 0 && sh.is_handle && sh.prepend_sa)
2890 {
2891 ret -= read_sockaddr_from_packet(buf, &from->dest.addr.sa);
2892 }
2893
2894 if (!sh.is_handle && from)
2895 {
2896 read_sockaddr_from_overlapped(io, &from->dest.addr.sa, ret);
2897 }
2898
2899 if (buf)
2900 {
2901 buf->len = ret;
2902 }
2903 return ret;
2904}
2905
2906#endif /* _WIN32 */
2907
2908/*
2909 * Socket event notification
2910 */
2911
2912unsigned int
2913socket_set(struct link_socket *s, struct event_set *es, unsigned int rwflags, void *arg,
2914 unsigned int *persistent)
2915{
2916 if (s)
2917 {
2918 if ((rwflags & EVENT_READ) && !stream_buf_read_setup(s))
2919 {
2920 ASSERT(!persistent);
2921 rwflags &= ~EVENT_READ;
2922 }
2923
2924#ifdef _WIN32
2925 if (rwflags & EVENT_READ)
2926 {
2927 socket_recv_queue(s, 0);
2928 }
2929#endif
2930
2931 /* if persistent is defined, call event_ctl only if rwflags has changed since last call */
2932 if (!persistent || *persistent != rwflags)
2933 {
2934 event_ctl(es, socket_event_handle(s), rwflags, arg);
2935 if (persistent)
2936 {
2937 *persistent = rwflags;
2938 }
2939 }
2940
2941 s->rwflags_debug = rwflags;
2942 }
2943 return rwflags;
2944}
2945
2946#if UNIX_SOCK_SUPPORT
2947
2948void
2950{
2951 if (sd && socket_defined(*sd))
2952 {
2954 *sd = SOCKET_UNDEFINED;
2955 }
2956}
2957
2958/*
2959 * code for unix domain sockets
2960 */
2961
2962const char *
2963sockaddr_unix_name(const struct sockaddr_un *local, const char *null)
2964{
2965 if (local && local->sun_family == PF_UNIX)
2966 {
2967 return local->sun_path;
2968 }
2969 else
2970 {
2971 return null;
2972 }
2973}
2974
2976create_socket_unix(void)
2977{
2979
2980 if ((sd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
2981 {
2982 msg(M_ERR, "Cannot create unix domain socket");
2983 }
2984
2985 /* set socket file descriptor to not pass across execs, so that
2986 * scripts don't have access to it */
2987 set_cloexec(sd);
2988
2989 return sd;
2990}
2991
2992void
2993socket_bind_unix(socket_descriptor_t sd, struct sockaddr_un *local, const char *prefix)
2994{
2995 struct gc_arena gc = gc_new();
2996 const mode_t orig_umask = umask(0);
2997
2998 if (bind(sd, (struct sockaddr *)local, sizeof(struct sockaddr_un)))
2999 {
3000 msg(M_FATAL | M_ERRNO, "%s: Socket bind[%d] failed on unix domain socket %s", prefix,
3001 (int)sd, sockaddr_unix_name(local, "NULL"));
3002 }
3003
3004 umask(orig_umask);
3005 gc_free(&gc);
3006}
3007
3009socket_accept_unix(socket_descriptor_t sd, struct sockaddr_un *remote)
3010{
3011 socklen_t remote_len = sizeof(struct sockaddr_un);
3013
3014 CLEAR(*remote);
3015 ret = accept(sd, (struct sockaddr *)remote, &remote_len);
3016 if (ret >= 0)
3017 {
3018 /* set socket file descriptor to not pass across execs, so that
3019 * scripts don't have access to it */
3020 set_cloexec(ret);
3021 }
3022 return ret;
3023}
3024
3025int
3026socket_connect_unix(socket_descriptor_t sd, struct sockaddr_un *remote)
3027{
3028 int status = connect(sd, (struct sockaddr *)remote, sizeof(struct sockaddr_un));
3029 if (status)
3030 {
3032 }
3033 return status;
3034}
3035
3036void
3037sockaddr_unix_init(struct sockaddr_un *local, const char *path)
3038{
3039 local->sun_family = PF_UNIX;
3040 strncpynt(local->sun_path, path, sizeof(local->sun_path));
3041}
3042
3043void
3044socket_delete_unix(const struct sockaddr_un *local)
3045{
3046 const char *name = sockaddr_unix_name(local, NULL);
3047 if (name && strlen(name))
3048 {
3049 unlink(name);
3050 }
3051}
3052
3053bool
3054unix_socket_get_peer_uid_gid(const socket_descriptor_t sd, uid_t *uid, gid_t *gid)
3055{
3056#ifdef HAVE_GETPEEREID
3057 uid_t u;
3058 gid_t g;
3059 if (getpeereid(sd, &u, &g) == -1)
3060 {
3061 return false;
3062 }
3063 if (uid)
3064 {
3065 *uid = u;
3066 }
3067 if (gid)
3068 {
3069 *gid = g;
3070 }
3071 return true;
3072#elif defined(SO_PEERCRED)
3073 struct ucred peercred;
3074 socklen_t so_len = sizeof(peercred);
3075 if (getsockopt(sd, SOL_SOCKET, SO_PEERCRED, &peercred, &so_len) == -1)
3076 {
3077 return false;
3078 }
3079 if (uid)
3080 {
3081 *uid = peercred.uid;
3082 }
3083 if (gid)
3084 {
3085 *gid = peercred.gid;
3086 }
3087 return true;
3088#else /* ifdef HAVE_GETPEEREID */
3089 return false;
3090#endif /* ifdef HAVE_GETPEEREID */
3091}
3092
3093#endif /* if UNIX_SOCK_SUPPORT */
void argv_parse_cmd(struct argv *argres, const char *cmdstr)
Parses a command string, tokenizes it and puts each element into a separate struct argv argument slot...
Definition argv.c:481
void argv_free(struct argv *a)
Frees all memory allocations allocated by the struct argv related functions.
Definition argv.c:101
bool argv_printf(struct argv *argres, const char *format,...)
printf() variant which populates a struct argv.
Definition argv.c:438
bool argv_printf_cat(struct argv *argres, const char *format,...)
printf() inspired argv concatenation.
Definition argv.c:462
struct argv argv_new(void)
Allocates a new struct argv and ensures it is initialised.
Definition argv.c:87
void free_buf(struct buffer *buf)
Free the memory allocated for a buffer.
Definition buffer.c:169
bool buf_printf(struct buffer *buf, const char *format,...)
printf-style append to a buffer with overflow check.
Definition buffer.c:226
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Allocate a buffer of the given size under garbage collection.
Definition buffer.c:77
struct buffer alloc_buf(size_t size)
Allocate a buffer of the given size.
Definition buffer.c:60
void gc_addspecial(void *addr, void(*free_function)(void *), struct gc_arena *a)
Register an address with a custom free function in a garbage collection arena.
Definition buffer.c:411
#define BSTR(buf)
Return the buffer content pointer cast to char *.
Definition buffer.h:151
static bool buf_copy(struct buffer *dest, const struct buffer *src)
Copy the content of one buffer to the end of another.
Definition buffer.h:1301
#define BPTR(buf)
Return a pointer to the start of the buffer content.
Definition buffer.h:139
static bool buf_copy_excess(struct buffer *dest, struct buffer *src, int len)
Truncate src to len bytes and copy any excess to dest.
Definition buffer.h:1376
static bool buf_write_prepend(struct buffer *dest, const void *src, int size)
Prepend data to a buffer.
Definition buffer.h:1222
static bool buf_safe(const struct buffer *buf, size_t len)
Check whether len bytes can be appended to a buffer.
Definition buffer.h:953
static bool buf_read(struct buffer *src, void *dest, int size)
Read bytes from the front of a buffer into a caller-supplied destination.
Definition buffer.h:1410
static int buf_len(const struct buffer *buf)
Return the length of the buffer content.
Definition buffer.h:438
static int buf_forward_capacity(const struct buffer *buf)
Return the number of bytes that can still be appended to the buffer.
Definition buffer.h:997
static bool buf_advance(struct buffer *buf, ssize_t size)
Advance the content start of a buffer, consuming bytes from the front.
Definition buffer.h:1124
#define ALLOC_OBJ_CLEAR_GC(dptr, type, gc)
Allocate and zero-initialise a garbage-collected object of the given type.
Definition buffer.h:2070
#define BLEN(buf)
Return the length of the buffer content in bytes.
Definition buffer.h:145
#define BLENZ(buf)
Return the length of the buffer content as a size_t.
Definition buffer.h:147
static void strncpynt(char *dest, const char *src, size_t maxlen)
Like strncpy() but always null-terminates the destination.
Definition buffer.h:646
static void gc_free(struct gc_arena *a)
Free all allocations in a garbage collection arena.
Definition buffer.h:1912
#define ALLOC_OBJ_CLEAR(dptr, type)
Allocate and zero-initialise memory for a single object of the given type.
Definition buffer.h:1974
static bool buf_defined(const struct buffer *buf)
Return true iff buf has a non-NULL data pointer.
Definition buffer.h:390
#define buf_init(buf, offset)
Definition buffer.h:356
static void gc_freeaddrinfo_callback(void *addr)
Callback to free a struct addrinfo, suitable for use with gc_addspecial().
Definition buffer.h:369
static struct gc_arena gc_new(void)
Allocate and return a new, empty garbage collection arena.
Definition buffer.h:1896
static int buf_forward_capacity_total(const struct buffer *buf)
Return the total number of bytes available from the current offset to the end of the allocated memory...
Definition buffer.h:1026
void dco_mp_start_vpn(HANDLE handle, struct link_socket *sock)
Initializes and binds the kernel UDP transport socket for multipeer mode.
Definition dco_win.c:283
void dco_p2p_new_peer(HANDLE handle, OVERLAPPED *ov, struct link_socket *sock, struct signal_info *sig_info)
Definition dco_win.c:327
void setenv_str(struct env_set *es, const char *name, const char *value)
Definition env_set.c:307
#define D_PS_PROXY
Definition errlevel.h:91
#define D_WIN32_IO
Definition errlevel.h:172
#define D_SOCKET_DEBUG
Definition errlevel.h:139
#define D_STREAM_DEBUG
Definition errlevel.h:171
#define D_INIT_MEDIUM
Definition errlevel.h:103
#define D_READ_WRITE
Definition errlevel.h:166
#define D_OSBUF
Definition errlevel.h:90
#define D_LOW
Definition errlevel.h:96
#define M_INFO
Definition errlevel.h:54
#define D_LINK_ERRORS
Definition errlevel.h:56
#define EVENT_WRITE
Definition event.h:38
#define EVENT_READ
Definition event.h:37
@ EVENT_ARG_LINK_SOCKET
Definition event.h:135
static void event_ctl(struct event_set *es, event_t event, unsigned int rwflags, void *arg)
Definition event.h:180
void set_nonblock(socket_descriptor_t fd)
Definition fdmisc.c:68
void set_cloexec(socket_descriptor_t fd)
Definition fdmisc.c:78
static void openvpn_fd_set(socket_descriptor_t fd, fd_set *setp)
Definition fdmisc.h:39
int get_server_poll_remaining_time(struct event_timeout *server_poll_timeout)
Definition forward.c:504
Interface functions to the internal and external multiplexers.
static SERVICE_STATUS status
Definition interactive.c:52
void management_set_state(struct management *man, const int state, const char *detail, const in_addr_t *tun_local_ip, const struct in6_addr *tun_local_ip6, const struct openvpn_sockaddr *local, const struct openvpn_sockaddr *remote)
Definition manage.c:2882
void management_sleep(const int n)
A sleep function that services the management layer for n seconds rather than doing nothing.
Definition manage.c:4238
#define OPENVPN_STATE_TCP_CONNECT
Definition manage.h:463
void alloc_buf_sock_tun(struct buffer *buf, const struct frame *frame)
Definition mtu.c:42
void set_mtu_discover_type(socket_descriptor_t sd, int mtu_type, sa_family_t proto_af)
Definition mtu.c:226
#define CLEAR(x)
Definition basic.h:32
const char * strerror_win32(DWORD errnum, struct gc_arena *gc)
Definition error.c:777
#define M_FATAL
Definition error.h:90
#define M_NONFATAL
Definition error.h:91
#define dmsg(flags,...)
Definition error.h:172
#define M_ERR
Definition error.h:106
#define openvpn_errno()
Definition error.h:71
#define msg(flags,...)
Definition error.h:152
unsigned int msglvl_t
Definition error.h:77
#define ASSERT(x)
Definition error.h:219
#define M_WARN
Definition error.h:92
#define M_ERRNO
Definition error.h:95
#define CM_CHILD_TCP
Definition openvpn.h:483
#define CM_CHILD_UDP
Definition openvpn.h:482
#define MODE_POINT_TO_POINT
Definition options.h:264
#define MODE_SERVER
Definition options.h:265
#define streq(x, y)
Definition options.h:726
static bool dco_enabled(const struct options *o)
Returns whether the current configuration has dco enabled.
Definition options.h:961
bool plugin_defined(const struct plugin_list *pl, const int type)
Definition plugin.c:904
static int plugin_call(const struct plugin_list *pl, const int type, const struct argv *av, struct plugin_return *pr, struct env_set *es)
Definition plugin.h:195
bool establish_http_proxy_passthru(struct http_proxy_info *p, socket_descriptor_t sd, const char *host, const char *port, struct event_timeout *server_poll_timeout, struct buffer *lookahead, struct signal_info *sig_info)
Definition proxy.c:596
static int openvpn_run_script(const struct argv *a, const struct env_set *es, const unsigned int flags, const char *hook)
Will run a script and return the exit code of the script if between 0 and 255, -1 otherwise.
Definition run_command.h:89
void throw_signal_soft(const int signum, const char *signal_text)
Throw a soft global signal.
Definition sig.c:204
int signal_reset(struct signal_info *si, int signum)
Clear the signal if its current value equals signum.
Definition sig.c:262
void throw_signal(const int signum)
Throw a hard signal.
Definition sig.c:175
struct signal_info siginfo_static
Definition sig.c:44
void register_signal(struct signal_info *si, int signum, const char *signal_text)
Register a soft signal in the signal_info struct si respecting priority.
Definition sig.c:228
#define SIG_SOURCE_HARD
Definition sig.h:30
static void get_signal(volatile int *sig)
Copy the global signal_received (if non-zero) to the passed-in argument sig.
Definition sig.h:109
void link_socket_init_phase1(struct context *c, int sock_index, int mode)
Definition socket.c:1300
static int get_cached_dns_entry(struct cached_dns_entry *dns_cache, const char *hostname, const char *servname, int ai_family, unsigned int resolve_flags, struct addrinfo **ai)
Definition socket.c:256
static int socket_get_sndbuf(socket_descriptor_t sd)
Definition socket.c:418
static void socket_set_sndbuf(socket_descriptor_t sd, int size)
Definition socket.c:431
static void bind_local(struct link_socket *sock)
Definition socket.c:622
void link_socket_init_phase2(struct context *c, struct link_socket *sock)
Definition socket.c:1655
int socket_send_queue(struct link_socket *sock, struct buffer *buf, const struct link_socket_actual *to)
Definition socket.c:2635
static void ipchange_fmt(const bool include_cmd, struct argv *argv, const struct link_socket_info *info, struct gc_arena *gc)
Definition socket.c:1823
static int socket_get_last_error(const struct link_socket *sock)
Definition socket.c:2524
ssize_t link_socket_write_tcp(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
Definition socket.c:2422
void link_socket_update_buffer_sizes(struct link_socket *sock, int rcvbuf, int sndbuf)
Definition socket.c:520
static socket_descriptor_t create_socket_udp(struct addrinfo *addrinfo, const unsigned int flags)
Definition socket.c:568
static void create_socket(struct link_socket *sock, struct addrinfo *addr)
Definition socket.c:640
const struct in6_addr * link_socket_current_remote_ipv6(const struct link_socket_info *info)
Definition socket.c:1958
void set_actual_address(struct link_socket_actual *actual, struct addrinfo *ai)
Definition socket.c:1022
const char * socket_stat(const struct link_socket *s, unsigned int rwflags, struct gc_arena *gc)
Definition socket.c:1991
static int do_preresolve_host(struct context *c, const char *hostname, const char *servname, const int af, const unsigned int flags)
Definition socket.c:279
void bad_address_length(int actual, int expected)
Definition socket.c:2228
event_t socket_listen_event_handle(struct link_socket *s)
Definition socket.c:2209
static void linksock_print_addr(struct link_socket *sock)
Definition socket.c:1467
static socket_descriptor_t socket_listen_accept(socket_descriptor_t sd, struct link_socket_actual *act, const struct addrinfo *local, bool do_listen, bool nowait, volatile int *signal_received)
Definition socket.c:819
static void socket_set_mark(socket_descriptor_t sd, int mark)
Definition socket.c:509
static void socket_set_rcvbuf(socket_descriptor_t sd, int size)
Definition socket.c:453
static void stream_buf_close(struct stream_buf *sb)
Definition socket.c:2198
static void stream_buf_get_final(struct stream_buf *sb, struct buffer *buf)
Sets the parameter buf to the current buffer of sb->buf.
Definition socket.c:2087
static void socket_connect(socket_descriptor_t *sd, const struct sockaddr *dest, const int connect_timeout, struct signal_info *sig_info)
Definition socket.c:1042
ssize_t link_socket_read_tcp(struct link_socket *sock, struct buffer *buf)
Definition socket.c:2240
static void phase2_socks_client(struct link_socket *sock, struct signal_info *sig_info)
Definition socket.c:1573
static bool socket_set_tcp_nodelay(socket_descriptor_t sd, int state)
Definition socket.c:489
static int get_addr_generic(sa_family_t af, unsigned int flags, const char *hostname, void *network, unsigned int *netbits, int resolve_retry_seconds, struct signal_info *sig_info, msglvl_t msglevel)
Definition socket.c:77
socket_descriptor_t socket_do_accept(socket_descriptor_t sd, struct link_socket_actual *act, const bool nowait)
Definition socket.c:748
static void socket_do_listen(socket_descriptor_t sd, const struct addrinfo *local, bool do_listen, bool do_set_nonblock)
Definition socket.c:723
static void phase2_tcp_server(struct link_socket *sock, struct signal_info *sig_info)
Definition socket.c:1504
int socket_recv_queue(struct link_socket *sock, int maxsize)
Definition socket.c:2535
void link_socket_close(struct link_socket *sock)
Definition socket.c:1765
bool get_ipv6_addr(const char *hostname, struct in6_addr *network, unsigned int *netbits, msglvl_t msglevel)
Translate an IPv6 addr or hostname from string form to in6_addr.
Definition socket.c:222
void link_socket_connection_initiated(struct link_socket_info *info, const struct link_socket_actual *act, const char *common_name, struct env_set *es)
Definition socket.c:1839
void socket_set_buffers(socket_descriptor_t fd, const struct socket_buffer_size *sbs, bool reduce_size)
Sets the receive and send buffer sizes of a socket descriptor.
Definition socket.c:462
bool stream_buf_read_setup_dowork(struct stream_buf *sb)
Will try to check if the buffers in stream form a full packet.
Definition socket.c:2095
static bool streqnull(const char *a, const char *b)
Definition socket.c:235
static void phase2_set_socket_flags(struct link_socket *sock)
Definition socket.c:1444
static void resolve_remote(struct link_socket *sock, int phase, struct signal_info *sig_info)
Definition socket.c:1175
void link_socket_bad_outgoing_addr(void)
Definition socket.c:1918
int sockethandle_finalize(sockethandle_t sh, struct overlapped_io *io, struct buffer *buf, struct link_socket_actual *from)
Definition socket.c:2820
in_addr_t link_socket_current_remote(const struct link_socket_info *info)
Definition socket.c:1924
static int socket_get_rcvbuf(socket_descriptor_t sd)
Definition socket.c:440
int openvpn_connect(socket_descriptor_t sd, const struct sockaddr *remote, int connect_timeout, volatile int *signal_received)
Definition socket.c:929
unsigned int socket_set(struct link_socket *s, struct event_set *es, unsigned int rwflags, void *arg, unsigned int *persistent)
Definition socket.c:2913
static struct buffer stream_buf_get_next(struct stream_buf *sb)
Return a buffer that is backed by the same backend as sb->buf that determines where the next read sho...
Definition socket.c:2064
static unsigned int sf2gaf(const unsigned int getaddr_flags, const unsigned int sockflags)
Definition socket.c:61
void do_preresolve(struct context *c)
Definition socket.c:323
void link_socket_bad_incoming_addr(struct buffer *buf, const struct link_socket_info *info, const struct link_socket_actual *from_addr)
Definition socket.c:1891
static void phase2_tcp_client(struct link_socket *sock, struct signal_info *sig_info)
Definition socket.c:1538
void socket_bind(socket_descriptor_t sd, struct addrinfo *local, int ai_family, const char *prefix, bool ipv6only)
Definition socket.c:881
socket_descriptor_t create_socket_tcp(struct addrinfo *addrinfo)
Definition socket.c:536
static void socket_frame_init(const struct frame *frame, struct link_socket *sock)
Definition socket.c:1101
static void stream_buf_reset(struct stream_buf *sb)
resets the stream buffer to be set up for the next round of reassembling a packet
Definition socket.c:2031
static void create_socket_dco_win(struct context *c, struct link_socket *sock, struct signal_info *sig_info)
Definition socket.c:1607
static void resolve_bind_local(struct link_socket *sock)
Definition socket.c:1125
static void tcp_connection_established(const struct link_socket_actual *act)
Definition socket.c:811
struct link_socket * link_socket_new(void)
Definition socket.c:1286
static bool stream_buf_added(struct stream_buf *sb, ssize_t length_added)
This will determine if sb->buf contains a full packet.
Definition socket.c:2131
static int read_sockaddr_from_packet(struct buffer *buf, struct sockaddr *dst)
Extracts a sockaddr from a packet payload.
Definition socket.c:2786
bool sockets_read_residual(const struct context *c)
Definition socket.c:45
in_addr_t getaddr(unsigned int flags, const char *hostname, int resolve_retry_seconds, bool *succeeded, struct signal_info *sig_info)
Translate an IPv4 addr or hostname from string form to in_addr_t.
Definition socket.c:195
void setenv_trusted(struct env_set *es, const struct link_socket_info *info)
Definition socket.c:1817
void read_sockaddr_from_overlapped(struct overlapped_io *io, struct sockaddr *dst, int overlapped_ret)
Definition socket.c:2736
static void stream_buf_init(struct stream_buf *sb, struct buffer *buf, const unsigned int sockflags, const int proto)
Definition socket.c:2040
static event_t socket_event_handle(const struct link_socket *sock)
Definition socket.h:797
#define IPV4_INVALID_ADDR
Definition socket.h:378
static BOOL SocketHandleGetOverlappedResult(sockethandle_t sh, struct overlapped_io *io)
Definition socket.h:289
#define PACKET_SIZE_MAX
Definition socket.h:57
#define LS_MODE_TCP_ACCEPT_FROM
Definition socket.h:200
#define SF_DCO_WIN
Definition socket.h:215
static bool link_socket_connection_oriented(const struct link_socket *sock)
Definition socket.h:428
static bool stream_buf_read_setup(struct link_socket *sock)
Definition socket.h:563
void sd_close(socket_descriptor_t *sd)
static void SocketHandleSetLastError(sockethandle_t sh, DWORD err)
Definition socket.h:303
static int SocketHandleGetLastError(sockethandle_t sh)
Definition socket.h:297
static void SocketHandleSetInvalError(sockethandle_t sh)
Definition socket.h:309
#define RESOLV_RETRY_INFINITE
Definition socket.h:48
#define SF_USE_IP_PKTINFO
Definition socket.h:210
#define LS_MODE_DEFAULT
Definition socket.h:198
#define MSG_NOSIGNAL
Definition socket.h:262
uint16_t packet_size_type
Definition socket.h:56
static bool socket_is_dco_win(const struct link_socket *s)
Returns true if we are on Windows and this link is running on DCO-WIN.
Definition socket.h:581
#define SF_HOST_RANDOMIZE
Definition socket.h:213
#define SF_GETADDRINFO_DGRAM
Definition socket.h:214
#define LS_MODE_TCP_LISTEN
Definition socket.h:199
#define SF_PORT_SHARE
Definition socket.h:212
#define ntohps(x)
Definition socket.h:63
static int openvpn_select(socket_descriptor_t nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
Definition socket.h:318
static int link_socket_write_win32(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
Definition socket.h:655
#define SF_PKTINFO_COPY_IIF
Definition socket.h:217
#define openvpn_close_socket(s)
Definition socket.h:267
#define htonps(x)
Definition socket.h:60
static int openvpn_bind(socket_descriptor_t sockfd, const struct sockaddr *addr, size_t addrlen)
Definition socket.h:333
const char * proto2ascii(int proto, sa_family_t af, bool display_form)
int openvpn_getaddrinfo(unsigned int flags, const char *hostname, const char *servname, int resolve_retry_seconds, struct signal_info *sig_info, int ai_family, struct addrinfo **res)
const char * print_sockaddr_ex(const struct sockaddr *sa, const char *separator, const unsigned int flags, struct gc_arena *gc)
Definition socket_util.c:38
void setenv_link_socket_actual(struct env_set *es, const char *name_prefix, const struct link_socket_actual *act, const unsigned int flags)
const char * print_link_socket_actual(const struct link_socket_actual *act, struct gc_arena *gc)
const char * print_link_socket_actual_ex(const struct link_socket_actual *act, const char *separator, const unsigned int flags, struct gc_arena *gc)
const char * addr_family_name(int af)
static const char * print_sockaddr(const struct sockaddr *addr, struct gc_arena *gc)
Definition socket_util.h:77
#define GETADDR_CACHE_MASK
static bool link_socket_actual_defined(const struct link_socket_actual *act)
#define GETADDR_TRY_ONCE
#define SA_IP_PORT
Definition socket_util.h:99
#define GETADDR_PASSIVE
static bool proto_is_udp(int proto)
Returns if the protocol being used is UDP.
#define GETADDR_FATAL
#define GETADDR_UPDATE_MANAGEMENT_STATE
#define PS_SHOW_PORT
Definition socket_util.h:31
@ PROTO_UDP
@ PROTO_TCP_CLIENT
@ PROTO_N
@ PROTO_TCP_SERVER
#define GETADDR_HOST_ORDER
#define PS_SHOW_PORT_IF_DEFINED
Definition socket_util.h:30
#define GETADDR_RANDOMIZE
#define GETADDR_DATAGRAM
static bool proto_is_tcp(int proto)
returns if the proto is a TCP variant (tcp-server, tcp-client or tcp)
static void addr_zero_host(struct openvpn_sockaddr *addr)
static bool proto_is_dgram(int proto)
Return if the protocol is datagram (UDP)
static int af_addr_size(sa_family_t af)
#define GETADDR_RESOLVE
#define GETADDR_MENTION_RESOLVE_RETRY
#define GETADDR_WARN_ON_SIGNAL
void establish_socks_proxy_passthru(struct socks_proxy_info *p, socket_descriptor_t sd, const char *host, const char *servname, struct event_timeout *server_poll_timeout, struct signal_info *sig_info)
Definition socks.c:337
void establish_socks_proxy_udpassoc(struct socks_proxy_info *p, socket_descriptor_t ctrl_sd, struct openvpn_sockaddr *relay_addr, struct event_timeout *server_poll_timeout, struct signal_info *sig_info)
Definition socks.c:395
Definition argv.h:35
Wrapper structure for dynamically allocated memory.
Definition buffer.h:71
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:76
int offset
Offset in bytes of the actual content within the allocated memory.
Definition buffer.h:74
Definition socket.h:67
const char * hostname
Definition socket.h:68
int ai_family
Definition socket.h:70
const char * servname
Definition socket.h:69
unsigned int flags
Definition socket.h:71
struct addrinfo * ai
Definition socket.h:72
struct cached_dns_entry * next
Definition socket.h:73
Definition options.h:109
struct local_list * local_list
Definition options.h:110
bool bind_local
Definition options.h:120
const char * remote
Definition options.h:116
const char * socks_proxy_port
Definition options.h:126
struct http_proxy_options * http_proxy_options
Definition options.h:124
bool bind_ipv6_only
Definition options.h:119
bool remote_float
Definition options.h:117
const char * remote_port
Definition options.h:115
const char * socks_proxy_server
Definition options.h:125
int mtu_discover_type
Definition options.h:141
int proto
Definition options.h:111
sa_family_t af
Definition options.h:112
bool local_port_defined
Definition options.h:114
const char * local_port
Definition options.h:113
unsigned int flags
Definition options.h:164
struct connection_entry ** array
Definition options.h:206
struct link_socket_addr * link_socket_addrs
Local and remote addresses on the external network.
Definition openvpn.h:160
int link_sockets_num
Definition openvpn.h:159
struct http_proxy_info * http_proxy
Definition openvpn.h:190
struct socks_proxy_info * socks_proxy
Definition openvpn.h:194
struct cached_dns_entry * dns_cache
Definition openvpn.h:168
struct tuntap * tuntap
Tun/tap virtual network interface.
Definition openvpn.h:173
struct event_timeout server_poll_interval
Definition openvpn.h:408
const struct link_socket * accept_from
Definition openvpn.h:243
struct frame frame
Definition openvpn.h:249
struct link_socket ** link_sockets
Definition openvpn.h:238
Contains all state information for one tunnel.
Definition openvpn.h:471
int mode
Role of this context within the OpenVPN process.
Definition openvpn.h:484
struct signal_info * sig
Internal error signaling object.
Definition openvpn.h:500
struct plugin_list * plugins
List of plug-ins.
Definition openvpn.h:502
struct context_2 c2
Level 2 context.
Definition openvpn.h:514
struct options options
Options loaded from command line or configuration file.
Definition openvpn.h:472
struct gc_arena gc
Garbage collection arena for allocations done in the scope of this context structure.
Definition openvpn.h:492
struct context_1 c1
Level 1 context.
Definition openvpn.h:513
struct link_socket * sock
Definition event.h:145
union event_arg::@1 u
event_arg_t type
Definition event.h:141
Packet geometry parameters.
Definition mtu.h:113
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:127
struct http_proxy_options options
Definition proxy.h:70
const char * port
Definition proxy.h:47
const char * server
Definition proxy.h:46
Definition options.h:102
const char * port
Definition options.h:104
int proto
Definition options.h:105
const char * local
Definition options.h:103
struct local_entry ** array
Definition options.h:198
struct man_connection connection
Definition manage.h:337
union openvpn_sockaddr::@27 addr
struct sockaddr sa
Definition socket_util.h:42
struct sockaddr_in in4
Definition socket_util.h:43
struct sockaddr_in6 in6
Definition socket_util.h:44
int resolve_retry_seconds
Definition options.h:367
int rcvbuf
Definition options.h:413
const char * ip_remote_hint
Definition options.h:369
HANDLE msg_channel
Definition options.h:691
struct connection_entry ce
Definition options.h:294
const char * ipchange
Definition options.h:321
int mode
Definition options.h:266
char * bind_dev
Definition options.h:418
int sndbuf
Definition options.h:414
int mark
Definition options.h:417
unsigned int sockflags
Definition options.h:421
const char * dev_node
Definition options.h:324
DWORD flags
Definition win32.h:212
struct buffer buf
Definition win32.h:222
DWORD size
Definition win32.h:211
OVERLAPPED overlapped
Definition win32.h:210
struct buffer buf_init
Definition win32.h:221
int addrlen
Definition win32.h:220
bool addr_defined
Definition win32.h:214
int iostate
Definition win32.h:209
struct sockaddr_in6 addr6
Definition win32.h:218
struct sockaddr_in addr
Definition win32.h:217
HANDLE write
Definition win32.h:83
HANDLE read
Definition win32.h:82
const char * signal_text
Definition sig.h:44
volatile int signal_received
Definition sig.h:42
volatile int source
Definition sig.h:43
bool is_handle
Definition socket.h:281
bool prepend_sa
Definition socket.h:282
char server[128]
Definition socks.h:40
const char * port
Definition socks.h:41
struct used to extract packets encapsulated in streams into a buffer, in this case OpenVPN packets (d...
Definition socket.h:108
struct buffer residual
buffer holding the excess bytes that are not part of the packet.
Definition socket.h:114
bool residual_fully_formed
The buffer in buf contains a full packet without a header.
Definition socket.h:121
int maxlen
Maximum length of a packet that we accept.
Definition socket.h:117
HANDLE msg_channel
Definition tun.h:86
Definition tun.h:181
enum tun_driver_type backend_driver
The backend driver that used for this tun/tap device.
Definition tun.h:191
OVERLAPPED dco_new_peer_ov
Definition tun.h:218
struct tuntap_options options
Definition tun.h:203
HANDLE hand
Definition tun.h:216
#define SIGHUP
Definition syshead.h:55
unsigned short sa_family_t
Definition syshead.h:409
#define SOCKET_UNDEFINED
Definition syshead.h:443
#define SIGTERM
Definition syshead.h:59
#define SOL_IP
Definition syshead.h:402
SOCKET socket_descriptor_t
Definition syshead.h:445
#define SIGUSR1
Definition syshead.h:57
uint32_t in_addr_t
Definition syshead.h:52
static int socket_defined(const socket_descriptor_t sd)
Definition syshead.h:453
#define ENABLE_IP_PKTINFO
Definition syshead.h:394
struct env_set * es
struct gc_arena gc
Definition test_ssl.c:122
void tun_open_device(struct tuntap *tt, const char *dev_node, const char **device_guid, struct gc_arena *gc)
Definition tun.c:5803
@ DRIVER_DCO
Definition tun.h:53
void init_net_event_win32(struct rw_handle *event, long network_events, socket_descriptor_t sd, unsigned int flags)
Definition win32.c:315
void overlapped_io_init(struct overlapped_io *o, const struct frame *frame, BOOL event_state)
Definition win32.c:265
void close_net_event_win32(struct rw_handle *event, socket_descriptor_t sd, unsigned int flags)
Definition win32.c:370
char * overlapped_io_state_ascii(const struct overlapped_io *o)
Definition win32.c:294
void overlapped_io_close(struct overlapped_io *o)
Definition win32.c:281
static bool defined_net_event_win32(const struct rw_handle *event)
Definition win32.h:94
#define IOSTATE_IMMEDIATE_RETURN
Definition win32.h:208
#define IOSTATE_INITIAL
Definition win32.h:206
#define IOSTATE_QUEUED
Definition win32.h:207