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