OpenVPN
socket.h
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#ifndef SOCKET_H
24#define SOCKET_H
25
26#include "buffer.h"
27#include "common.h"
28#include "error.h"
29#include "proto.h"
30#include "mtu.h"
31#include "win32.h"
32#include "event.h"
33#include "proxy.h"
34#include "socks.h"
35#include "misc.h"
36#include "tun.h"
37#include "socket_util.h"
38
39/*
40 * OpenVPN's default port number as assigned by IANA.
41 */
42#define OPENVPN_PORT "1194"
43
44/*
45 * Number of seconds that "resolv-retry infinite"
46 * represents.
47 */
48#define RESOLV_RETRY_INFINITE 1000000000
49
50/*
51 * packet_size_type is used to communicate packet size
52 * over the wire when stream oriented protocols are
53 * being used
54 */
55
56typedef uint16_t packet_size_type;
57#define PACKET_SIZE_MAX UINT16_MAX
58
59/* convert a packet_size_type from host to network order */
60#define htonps(x) htons(x)
61
62/* convert a packet_size_type from network to host order */
63#define ntohps(x) ntohs(x)
64
65/* struct to hold preresolved host names */
67{
68 const char *hostname;
69 const char *servname;
71 unsigned int flags;
72 struct addrinfo *ai;
74};
75
76/* IP addresses which are persistent across SIGUSR1s */
78{
79 struct addrinfo *bind_local;
80 struct addrinfo *remote_list; /* complete remote list */
81 struct addrinfo *current_remote; /* remote used in the
82 * current connection attempt */
83 struct link_socket_actual actual; /* reply to this address */
84};
85
87{
90 const char *ipchange_command;
91 const struct plugin_list *plugins;
93 uint8_t proto; /* Protocol (PROTO_x defined below) */
94 sa_family_t af; /* Address family like AF_INET, AF_INET6 or AF_UNSPEC*/
96 int mtu_changed; /* Set to true when mtu value is changed */
97};
98
108{
109 /* Buffer to hold the initial buffer that will be used to reset buf */
111
115
118
122
124 struct buffer buf;
125
129 int len;
130
133 bool error;
134#if PORT_SHARE
135#define PS_DISABLED 0
136#define PS_ENABLED 1
137#define PS_FOREIGN 2
139#endif
140};
141
142/*
143 * Used to set socket buffer sizes
144 */
146{
149};
150
159 bool reduce_size);
160
161/*
162 * This is the main socket structure used by OpenVPN. The SOCKET_
163 * defines try to abstract away our implementation differences between
164 * using sockets on Posix vs. Win32.
165 */
167{
169
176 socket_descriptor_t ctrl_sd; /* only used for UDP over Socks */
177
178#ifdef _WIN32
182 struct rw_handle listen_handle; /* For listening on TCP socket in server mode */
183#endif
184
185 /* used for printing status info only */
186 unsigned int rwflags_debug;
187
188 /* used for long-term queueing of pre-accepted socket listen */
190
191 const char *remote_host;
192 const char *remote_port;
193 const char *local_host;
194 const char *local_port;
197
198#define LS_MODE_DEFAULT 0
199#define LS_MODE_TCP_LISTEN 1
200#define LS_MODE_TCP_ACCEPT_FROM 2
201 int mode;
202
205
207
208 int mtu; /* OS discovered MTU, or 0 if unknown */
209
210#define SF_USE_IP_PKTINFO (1 << 0)
211#define SF_TCP_NODELAY (1 << 1) /* unused: flag always enabled */
212#define SF_PORT_SHARE (1 << 2)
213#define SF_HOST_RANDOMIZE (1 << 3)
214#define SF_GETADDRINFO_DGRAM (1 << 4)
215#define SF_DCO_WIN (1 << 5)
216#define SF_PREPEND_SA (1 << 6)
217#define SF_PKTINFO_COPY_IIF (1 << 7)
218 unsigned int sockflags;
219 int mark;
220 const char *bind_dev;
221
222 /* for stream sockets */
226
227 /* HTTP proxy */
229
230 /* Socks proxy */
232 struct link_socket_actual socks_relay; /* Socks UDP relay address */
233
234 /* The OpenVPN server we will use the proxy to connect to */
235 const char *proxy_dest_host;
236 const char *proxy_dest_port;
237
238 /* Pointer to the server-poll to trigger the timeout in function which have
239 * their own loop instead of using the main oop */
241
242#if PASSTOS_CAPABILITY
243 /* used to get/set TOS. */
244#if defined(TARGET_LINUX)
245 uint8_t ptos;
246#else /* all the BSDs, Solaris, MacOS use plain "int" -> see "man ip" there */
247 int ptos;
248#endif
249 bool ptos_defined;
250#endif
251
252#ifdef ENABLE_DEBUG
253 int gremlin; /* --gremlin bits */
254#endif
255};
256
257/*
258 * Some Posix/Win32 differences.
259 */
260
261#ifndef MSG_NOSIGNAL
262#define MSG_NOSIGNAL 0
263#endif
264
265#ifdef _WIN32
266
267#define openvpn_close_socket(s) closesocket(s)
268
269int socket_recv_queue(struct link_socket *sock, int maxsize);
270
271int socket_send_queue(struct link_socket *sock, struct buffer *buf,
272 const struct link_socket_actual *to);
273
274typedef struct
275{
276 union
277 {
278 SOCKET s;
279 HANDLE h;
280 };
282 bool prepend_sa; /* are incoming packets prepended with sockaddr? */
284
285int sockethandle_finalize(sockethandle_t sh, struct overlapped_io *io, struct buffer *buf,
286 struct link_socket_actual *from);
287
288static inline BOOL
290{
291 return sh.is_handle
292 ? GetOverlappedResult(sh.h, &io->overlapped, &io->size, FALSE)
293 : WSAGetOverlappedResult(sh.s, &io->overlapped, &io->size, FALSE, &io->flags);
294}
295
296static inline int
298{
299 return sh.is_handle ? (int)GetLastError() : WSAGetLastError();
300}
301
302inline static void
304{
305 sh.is_handle ? SetLastError(err) : WSASetLastError(err);
306}
307
308static inline void
310{
311 sh.is_handle ? SetLastError(ERROR_INVALID_FUNCTION) : WSASetLastError(WSAEINVAL);
312}
313
314/* winsock(2).h uses slightly different types so to avoid conversion
315 errors we wrap these functions on Windows */
316
317static inline int
318openvpn_select(socket_descriptor_t nfds, fd_set *readfds, fd_set *writefds,
319 fd_set *exceptfds, struct timeval *timeout)
320{
321 (void)nfds; /* first argument ignored on Windows */
322 return select(0, readfds, writefds, exceptfds, timeout);
323}
324
325static inline ssize_t
326openvpn_send(socket_descriptor_t sockfd, const void *buf, size_t len, int flags)
327{
328 ASSERT(len <= INT_MAX);
329 return send(sockfd, buf, (int)len, flags);
330}
331
332static inline int
333openvpn_bind(socket_descriptor_t sockfd, const struct sockaddr *addr, size_t addrlen)
334{
335 ASSERT(addrlen <= INT_MAX);
336 return bind(sockfd, addr, (int)addrlen);
337}
338
339#else /* ifdef _WIN32 */
340
341#define openvpn_close_socket(s) close(s)
342#define openvpn_select(nfds, readfds, writefds, exceptfds, timeout) \
343 select(nfds, readfds, writefds, exceptfds, timeout)
344#define openvpn_send(sockfd, buf, len, flags) send(sockfd, buf, len, flags)
345#define openvpn_bind(sockfd, addr, addrlen) bind(sockfd, addr, addrlen)
346
347#endif /* ifdef _WIN32 */
348
349struct link_socket *link_socket_new(void);
350
351void socket_bind(socket_descriptor_t sd, struct addrinfo *local, int af_family, const char *prefix,
352 bool ipv6only);
353
354int openvpn_connect(socket_descriptor_t sd, const struct sockaddr *remote, int connect_timeout,
355 volatile int *signal_received);
356
357
358/*
359 * Initialize link_socket object.
360 */
361void link_socket_init_phase1(struct context *c, int sock_index, int mode);
362
363void link_socket_init_phase2(struct context *c, struct link_socket *sock);
364
365void do_preresolve(struct context *c);
366
367void link_socket_close(struct link_socket *sock);
368
369#ifdef ENABLE_MANAGEMENT
371#endif
372
373void bad_address_length(int actual, int expected);
374
375/* IPV4_INVALID_ADDR: returned by link_socket_current_remote()
376 * to ease redirect-gateway logic for ipv4 tunnels on ipv6 endpoints
377 */
378#define IPV4_INVALID_ADDR 0xffffffff
380
381const struct in6_addr *link_socket_current_remote_ipv6(const struct link_socket_info *info);
382
384 const struct link_socket_actual *addr,
385 const char *common_name, struct env_set *es);
386
387void link_socket_bad_incoming_addr(struct buffer *buf, const struct link_socket_info *info,
388 const struct link_socket_actual *from_addr);
389
390void set_actual_address(struct link_socket_actual *actual, struct addrinfo *ai);
391
393
394void setenv_trusted(struct env_set *es, const struct link_socket_info *info);
395
396void link_socket_update_buffer_sizes(struct link_socket *sock, int rcvbuf, int sndbuf);
397
398/*
399 * Low-level functions
400 */
401
402socket_descriptor_t create_socket_tcp(struct addrinfo *);
403
405 const bool nowait);
406
407#if UNIX_SOCK_SUPPORT
408
409socket_descriptor_t create_socket_unix(void);
410
411void socket_bind_unix(socket_descriptor_t sd, struct sockaddr_un *local, const char *prefix);
412
413socket_descriptor_t socket_accept_unix(socket_descriptor_t sd, struct sockaddr_un *remote);
414
415int socket_connect_unix(socket_descriptor_t sd, struct sockaddr_un *remote);
416
417void sockaddr_unix_init(struct sockaddr_un *local, const char *path);
418
419const char *sockaddr_unix_name(const struct sockaddr_un *local, const char *null);
420
421void socket_delete_unix(const struct sockaddr_un *local);
422
423bool unix_socket_get_peer_uid_gid(const socket_descriptor_t sd, uid_t *uid, gid_t *gid);
424
425#endif /* if UNIX_SOCK_SUPPORT */
426
427static inline bool
429{
430 if (sock)
431 {
433 }
434 else
435 {
436 return false;
437 }
438}
439
440#if PORT_SHARE
441
442static inline bool
443socket_foreign_protocol_detected(const struct link_socket *sock)
444{
445 return link_socket_connection_oriented(sock) && sock->stream_buf.port_share_state == PS_FOREIGN;
446}
447
448static inline const struct buffer *
450{
451 return &sock->stream_buf.buf;
452}
453
454static inline int
455socket_foreign_protocol_sd(const struct link_socket *sock)
456{
457 return sock->sd;
458}
459
460#endif /* if PORT_SHARE */
461
462static inline bool
464{
466 {
467 if (sock->stream_reset || sock->stream_buf.error)
468 {
469 return true;
470 }
471 else if (status < 0)
472 {
473 const int err = openvpn_errno();
474#ifdef _WIN32
475 return err == WSAECONNRESET || err == WSAECONNABORTED
477#else
478 return err == ECONNRESET;
479#endif
480 }
481 }
482 return false;
483}
484
485static inline bool
487 const struct link_socket_actual *from_addr)
488{
489 if (buf->len > 0)
490 {
491 switch (from_addr->dest.addr.sa.sa_family)
492 {
493 case AF_INET6:
494 case AF_INET:
496 {
497 return false;
498 }
499 if (info->remote_float || (!info->lsa->remote_list))
500 {
501 return true;
502 }
503 if (addrlist_match_proto(&from_addr->dest, info->lsa->remote_list, info->proto))
504 {
505 return true;
506 }
507 }
508 }
509 return false;
510}
511
512static inline void
514 struct link_socket_actual **act)
515{
516 if (buf->len > 0)
517 {
518 struct link_socket_addr *lsa = info->lsa;
520 {
521 *act = &lsa->actual;
522 }
523 else
524 {
526 buf->len = 0;
527 *act = NULL;
528 }
529 }
530}
531
532static inline void
534 const char *common_name, struct env_set *es)
535{
536 struct link_socket_addr *lsa = info->lsa;
537 if (
538 /* new or changed address? */
540 || !addr_match_proto(&act->dest, &lsa->actual.dest, info->proto))
541 &&
542 /* address undef or address == remote or --float */
543 (info->remote_float
544 || (!lsa->remote_list || addrlist_match_proto(&act->dest, lsa->remote_list, info->proto))))
545 {
546 link_socket_connection_initiated(info, act, common_name, es);
547 }
548}
549
561
562static inline bool
564{
566 {
568 }
569 else
570 {
571 return true;
572 }
573}
574
580static inline bool
582{
583 return s->sockflags & SF_DCO_WIN;
584}
585
586/*
587 * Socket Read Routines
588 */
589
590ssize_t link_socket_read_tcp(struct link_socket *sock, struct buffer *buf);
591
592#ifdef _WIN32
593
594static inline int
596 struct link_socket_actual *from)
597{
598 sockethandle_t sh = { .s = sock->sd };
599 if (socket_is_dco_win(sock))
600 {
601 *from = sock->info.lsa->actual;
602 sh.is_handle = true;
604 }
605 return sockethandle_finalize(sh, &sock->reads, buf, from);
606}
607
608#else /* ifdef _WIN32 */
609
610ssize_t link_socket_read_udp_posix(struct link_socket *sock, struct buffer *buf,
611 struct link_socket_actual *from);
612
613#endif /* ifdef _WIN32 */
614
615/* read a TCP or UDP packet from link */
616static inline ssize_t
617link_socket_read(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *from)
618{
619 if (proto_is_udp(sock->info.proto) || socket_is_dco_win(sock))
620 /* unified UDPv4 and UDPv6, for DCO-WIN the kernel
621 * will strip the length header */
622 {
623 ssize_t res;
624
625#ifdef _WIN32
626 res = link_socket_read_udp_win32(sock, buf, from);
627#else
628 res = link_socket_read_udp_posix(sock, buf, from);
629#endif
630 return res;
631 }
632 else if (proto_is_tcp(sock->info.proto)) /* unified TCPv4 and TCPv6 */
633 {
634 /* from address was returned by accept */
635 from->dest = sock->info.lsa->actual.dest;
636 return link_socket_read_tcp(sock, buf);
637 }
638 else
639 {
640 ASSERT(0);
641 return -1; /* NOTREACHED */
642 }
643}
644
645/*
646 * Socket Write routines
647 */
648
649ssize_t link_socket_write_tcp(struct link_socket *sock, struct buffer *buf,
650 struct link_socket_actual *to);
651
652#ifdef _WIN32
653
654static inline int
655link_socket_write_win32(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
656{
657 int err = 0;
658 int status = 0;
659 sockethandle_t sh = { .s = sock->sd, .is_handle = socket_is_dco_win(sock) };
660 if (overlapped_io_active(&sock->writes))
661 {
662 status = sockethandle_finalize(sh, &sock->writes, NULL, NULL);
663 if (status < 0)
664 {
665 err = SocketHandleGetLastError(sh);
666 }
667 }
668
669 /* dco-win mp requires control packets to be prepended with sockaddr */
670 if (sock->sockflags & SF_PREPEND_SA)
671 {
672 if (to->dest.addr.sa.sa_family == AF_INET)
673 {
674 buf_write_prepend(buf, &to->dest.addr.in4, sizeof(struct sockaddr_in));
675 }
676 else
677 {
678 buf_write_prepend(buf, &to->dest.addr.in6, sizeof(struct sockaddr_in6));
679 }
680 }
681
682 socket_send_queue(sock, buf, to);
683 if (status < 0)
684 {
686 return status;
687 }
688 else
689 {
690 return BLEN(buf);
691 }
692}
693
694#else /* ifdef _WIN32 */
695
696ssize_t link_socket_write_udp_posix_sendmsg(struct link_socket *sock, struct buffer *buf,
697 struct link_socket_actual *to);
698
699
700static inline ssize_t
701link_socket_write_udp_posix(struct link_socket *sock, struct buffer *buf,
702 struct link_socket_actual *to)
703{
704#if ENABLE_IP_PKTINFO
705 if (proto_is_udp(sock->info.proto) && (sock->sockflags & SF_USE_IP_PKTINFO)
706 && addr_defined_ipi(to))
707 {
708 return link_socket_write_udp_posix_sendmsg(sock, buf, to);
709 }
710 else
711#endif
712 return sendto(sock->sd, BPTR(buf), BLENZ(buf), 0, (struct sockaddr *)&to->dest.addr.sa,
713 (socklen_t)af_addr_size(to->dest.addr.sa.sa_family));
714}
715
716static inline ssize_t
717link_socket_write_tcp_posix(struct link_socket *sock, struct buffer *buf)
718{
719 return send(sock->sd, BPTR(buf), BLENZ(buf), MSG_NOSIGNAL);
720}
721
722#endif /* ifdef _WIN32 */
723
724static inline ssize_t
725link_socket_write_udp(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
726{
727#ifdef _WIN32
728 return link_socket_write_win32(sock, buf, to);
729#else
730 return link_socket_write_udp_posix(sock, buf, to);
731#endif
732}
733
734/* write a TCP or UDP packet to link */
735static inline ssize_t
736link_socket_write(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
737{
738 if (proto_is_udp(sock->info.proto) || socket_is_dco_win(sock))
739 {
740 /* unified UDPv4, UDPv6 and DCO-WIN (driver adds length header) */
741 return link_socket_write_udp(sock, buf, to);
742 }
743 else if (proto_is_tcp(sock->info.proto)) /* unified TCPv4 and TCPv6 */
744 {
745 return link_socket_write_tcp(sock, buf, to);
746 }
747 else
748 {
749 ASSERT(0);
750 return -1; /* NOTREACHED */
751 }
752}
753
754#if PASSTOS_CAPABILITY
755
756/*
757 * Extract TOS bits. Assumes that ipbuf is a valid IPv4 packet.
758 */
759static inline void
760link_socket_extract_tos(struct link_socket *sock, const struct buffer *ipbuf)
761{
762 if (sock && ipbuf)
763 {
764 const struct openvpn_iphdr *iph = (struct openvpn_iphdr *)BPTR(ipbuf);
765 sock->ptos = iph->tos;
766 sock->ptos_defined = true;
767 }
768}
769
770/*
771 * Set socket properties to reflect TOS bits which were extracted
772 * from tunnel packet.
773 */
774static inline void
775link_socket_set_tos(struct link_socket *sock)
776{
777 if (sock && sock->ptos_defined)
778 {
779 setsockopt(sock->sd, IPPROTO_IP, IP_TOS, (const void *)&sock->ptos, sizeof(sock->ptos));
780 }
781}
782
783#endif /* if PASSTOS_CAPABILITY */
784
785/*
786 * Socket I/O wait functions
787 */
788
789/*
790 * Extends the pre-existing read residual logic
791 * to all initialized sockets, ensuring the complete
792 * packet is read.
793 */
794bool sockets_read_residual(const struct context *c);
795
796static inline event_t
798{
799#ifdef _WIN32
800 return &sock->rw_handle;
801#else
802 return sock->sd;
803#endif
804}
805
807
808unsigned int socket_set(struct link_socket *sock, struct event_set *es, unsigned int rwflags,
809 void *arg, unsigned int *persistent);
810
811static inline void
812socket_set_listen_persistent(struct link_socket *sock, struct event_set *es, void *arg)
813{
814 if (sock && !sock->listen_persistent_queued)
815 {
817 sock->listen_persistent_queued = true;
818 }
819}
820
821static inline void
823{
824#ifdef _WIN32
826#endif
827}
828
829const char *socket_stat(const struct link_socket *sock, unsigned int rwflags, struct gc_arena *gc);
830
831#endif /* SOCKET_H */
Buffer management functions and garbage collection.
#define BPTR(buf)
Return a pointer to the start of the buffer content.
Definition buffer.h:139
static bool buf_write_prepend(struct buffer *dest, const void *src, int size)
Prepend data to a buffer.
Definition buffer.h:1222
#define BLEN(buf)
Return the length of the buffer content in bytes.
Definition buffer.h:145
#define BLENZ(buf)
Return the length of the buffer content as a size_t.
Definition buffer.h:147
#define EVENT_READ
Definition event.h:37
static void event_ctl(struct event_set *es, event_t event, unsigned int rwflags, void *arg)
Definition event.h:180
static SERVICE_STATUS status
Definition interactive.c:52
#define openvpn_errno()
Definition error.h:71
#define ASSERT(x)
Definition error.h:219
void link_socket_init_phase1(struct context *c, int sock_index, int mode)
Definition socket.c:1300
static event_t socket_event_handle(const struct link_socket *sock)
Definition socket.h:797
const char * socket_stat(const struct link_socket *sock, unsigned int rwflags, struct gc_arena *gc)
Definition socket.c:1991
static BOOL SocketHandleGetOverlappedResult(sockethandle_t sh, struct overlapped_io *io)
Definition socket.h:289
static ssize_t link_socket_read(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *from)
Definition socket.h:617
void link_socket_init_phase2(struct context *c, struct link_socket *sock)
Definition socket.c:1655
int socket_send_queue(struct link_socket *sock, struct buffer *buf, const struct link_socket_actual *to)
Definition socket.c:2635
ssize_t link_socket_write_tcp(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
Definition socket.c:2422
void link_socket_update_buffer_sizes(struct link_socket *sock, int rcvbuf, int sndbuf)
Definition socket.c:520
static bool socket_connection_reset(const struct link_socket *sock, ssize_t status)
Definition socket.h:463
event_t socket_listen_event_handle(struct link_socket *sock)
Definition socket.c:2209
const struct in6_addr * link_socket_current_remote_ipv6(const struct link_socket_info *info)
Definition socket.c:1958
void set_actual_address(struct link_socket_actual *actual, struct addrinfo *ai)
Definition socket.c:1022
static ssize_t link_socket_write(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
Definition socket.h:736
static void socket_set_listen_persistent(struct link_socket *sock, struct event_set *es, void *arg)
Definition socket.h:812
#define SF_DCO_WIN
Definition socket.h:215
void bad_address_length(int actual, int expected)
Definition socket.c:2228
static bool link_socket_connection_oriented(const struct link_socket *sock)
Definition socket.h:428
static bool stream_buf_read_setup(struct link_socket *sock)
Definition socket.h:563
void sd_close(socket_descriptor_t *sd)
static void socket_reset_listen_persistent(struct link_socket *sock)
Definition socket.h:822
static int link_socket_read_udp_win32(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *from)
Definition socket.h:595
static void SocketHandleSetLastError(sockethandle_t sh, DWORD err)
Definition socket.h:303
socket_descriptor_t create_socket_tcp(struct addrinfo *)
Definition socket.c:536
static int SocketHandleGetLastError(sockethandle_t sh)
Definition socket.h:297
static void SocketHandleSetInvalError(sockethandle_t sh)
Definition socket.h:309
ssize_t link_socket_read_tcp(struct link_socket *sock, struct buffer *buf)
Definition socket.c:2240
socket_descriptor_t socket_do_accept(socket_descriptor_t sd, struct link_socket_actual *act, const bool nowait)
Definition socket.c:748
int socket_recv_queue(struct link_socket *sock, int maxsize)
Definition socket.c:2535
void link_socket_close(struct link_socket *sock)
Definition socket.c:1765
static bool link_socket_verify_incoming_addr(struct buffer *buf, const struct link_socket_info *info, const struct link_socket_actual *from_addr)
Definition socket.h:486
static void link_socket_set_outgoing_addr(struct link_socket_info *info, const struct link_socket_actual *act, const char *common_name, struct env_set *es)
Definition socket.h:533
void socket_set_buffers(socket_descriptor_t fd, const struct socket_buffer_size *sbs, bool reduce_size)
Sets the receive and send buffer sizes of a socket descriptor.
Definition socket.c:462
bool stream_buf_read_setup_dowork(struct stream_buf *sb)
Will try to check if the buffers in stream form a full packet.
Definition socket.c:2095
static void link_socket_get_outgoing_addr(struct buffer *buf, const struct link_socket_info *info, struct link_socket_actual **act)
Definition socket.h:513
#define SF_USE_IP_PKTINFO
Definition socket.h:210
#define MSG_NOSIGNAL
Definition socket.h:262
uint16_t packet_size_type
Definition socket.h:56
void link_socket_bad_outgoing_addr(void)
Definition socket.c:1918
static bool socket_is_dco_win(const struct link_socket *s)
Returns true if we are on Windows and this link is running on DCO-WIN.
Definition socket.h:581
int sockethandle_finalize(sockethandle_t sh, struct overlapped_io *io, struct buffer *buf, struct link_socket_actual *from)
Definition socket.c:2820
in_addr_t link_socket_current_remote(const struct link_socket_info *info)
Definition socket.c:1924
int openvpn_connect(socket_descriptor_t sd, const struct sockaddr *remote, int connect_timeout, volatile int *signal_received)
Definition socket.c:929
#define SF_PREPEND_SA
Definition socket.h:216
void do_preresolve(struct context *c)
Definition socket.c:323
void link_socket_bad_incoming_addr(struct buffer *buf, const struct link_socket_info *info, const struct link_socket_actual *from_addr)
Definition socket.c:1891
unsigned int socket_set(struct link_socket *sock, struct event_set *es, unsigned int rwflags, void *arg, unsigned int *persistent)
Definition socket.c:2913
static int openvpn_select(socket_descriptor_t nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
Definition socket.h:318
static ssize_t link_socket_write_udp(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
Definition socket.h:725
void socket_bind(socket_descriptor_t sd, struct addrinfo *local, int af_family, const char *prefix, bool ipv6only)
Definition socket.c:881
static int link_socket_write_win32(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
Definition socket.h:655
struct link_socket * link_socket_new(void)
Definition socket.c:1286
void link_socket_connection_initiated(struct link_socket_info *info, const struct link_socket_actual *addr, const char *common_name, struct env_set *es)
Definition socket.c:1839
bool sockets_read_residual(const struct context *c)
Definition socket.c:45
static int openvpn_bind(socket_descriptor_t sockfd, const struct sockaddr *addr, size_t addrlen)
Definition socket.h:333
void setenv_trusted(struct env_set *es, const struct link_socket_info *info)
Definition socket.c:1817
static ssize_t openvpn_send(socket_descriptor_t sockfd, const void *buf, size_t len, int flags)
Definition socket.h:326
static bool link_socket_actual_defined(const struct link_socket_actual *act)
static bool addr_match_proto(const struct openvpn_sockaddr *a1, const struct openvpn_sockaddr *a2, const int proto)
static bool proto_is_udp(int proto)
Returns if the protocol being used is UDP.
static bool proto_is_tcp(int proto)
returns if the proto is a TCP variant (tcp-server, tcp-client or tcp)
static int af_addr_size(sa_family_t af)
static bool link_socket_proto_connection_oriented(int proto)
static bool addrlist_match_proto(const struct openvpn_sockaddr *a1, struct addrinfo *addr_list, const int proto)
Wrapper structure for dynamically allocated memory.
Definition buffer.h:71
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:76
Definition socket.h:67
const char * hostname
Definition socket.h:68
int ai_family
Definition socket.h:70
const char * servname
Definition socket.h:69
unsigned int flags
Definition socket.h:71
struct addrinfo * ai
Definition socket.h:72
struct cached_dns_entry * next
Definition socket.h:73
Contains all state information for one tunnel.
Definition openvpn.h:471
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:127
uint8_t tos
Definition proto.h:95
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
DWORD flags
Definition win32.h:212
DWORD size
Definition win32.h:211
OVERLAPPED overlapped
Definition win32.h:210
bool is_handle
Definition socket.h:281
bool prepend_sa
Definition socket.h:282
struct used to extract packets encapsulated in streams into a buffer, in this case OpenVPN packets (d...
Definition socket.h:108
struct buffer buf
Holds the data of the current packet.
Definition socket.h:124
bool error
if true, a fatal TCP error has occurred, requiring that connection be restarted
Definition socket.h:133
struct buffer residual
buffer holding the excess bytes that are not part of the packet.
Definition socket.h:114
bool residual_fully_formed
The buffer in buf contains a full packet without a header.
Definition socket.h:121
int maxlen
Maximum length of a packet that we accept.
Definition socket.h:117
int len
-1 if not yet known.
Definition socket.h:129
struct buffer buf_init
Definition socket.h:110
unsigned short sa_family_t
Definition syshead.h:409
SOCKET socket_descriptor_t
Definition syshead.h:445
uint32_t in_addr_t
Definition syshead.h:52
struct env_set * es
struct gc_arena gc
Definition test_ssl.c:122
long reset_net_event_win32(struct rw_handle *event, socket_descriptor_t sd)
Definition win32.c:355
static bool overlapped_io_active(struct overlapped_io *o)
Definition win32.h:230