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