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