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