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/* IP addresses which are persistent across SIGUSR1s */
67{
68 struct addrinfo *bind_local;
69 struct addrinfo *remote_list; /* complete remote list */
70 struct addrinfo *current_remote; /* remote used in the
71 * current connection attempt */
72 struct link_socket_actual actual; /* reply to this address */
73};
74
76{
79 const char *ipchange_command;
80 const struct plugin_list *plugins;
82 uint8_t proto; /* Protocol (PROTO_x defined below) */
83 sa_family_t af; /* Address family like AF_INET, AF_INET6 or AF_UNSPEC*/
85 int mtu_changed; /* Set to true when mtu value is changed */
86};
87
97{
98 /* Buffer to hold the initial buffer that will be used to reset buf */
100
104
107
111
113 struct buffer buf;
114
118 int len;
119
122 bool error;
123#if PORT_SHARE
124#define PS_DISABLED 0
125#define PS_ENABLED 1
126#define PS_FOREIGN 2
127 int port_share_state;
128#endif
129};
130
131/*
132 * Used to set socket buffer sizes
133 */
135{
138};
139
148 bool reduce_size);
149
150/*
151 * This is the main socket structure used by OpenVPN. The SOCKET_
152 * defines try to abstract away our implementation differences between
153 * using sockets on Posix vs. Win32.
154 */
156{
158
163
165 socket_descriptor_t ctrl_sd; /* only used for UDP over Socks */
166
167#ifdef _WIN32
171 struct rw_handle listen_handle; /* For listening on TCP socket in server mode */
172#endif
173
174 /* used for printing status info only */
175 unsigned int rwflags_debug;
176
177 /* used for long-term queueing of pre-accepted socket listen */
179
180 const char *remote_host;
181 const char *remote_port;
182 const char *local_host;
183 const char *local_port;
186
187#define LS_MODE_DEFAULT 0
188#define LS_MODE_TCP_LISTEN 1
189#define LS_MODE_TCP_ACCEPT_FROM 2
190 int mode;
191
194
196
197 int mtu; /* OS discovered MTU, or 0 if unknown */
198
199#define SF_USE_IP_PKTINFO (1 << 0)
200#define SF_TCP_NODELAY (1 << 1) /* unused: flag always enabled */
201#define SF_PORT_SHARE (1 << 2)
202#define SF_HOST_RANDOMIZE (1 << 3)
203#define SF_GETADDRINFO_DGRAM (1 << 4)
204#define SF_DCO_WIN (1 << 5)
205#define SF_PREPEND_SA (1 << 6)
206#define SF_PKTINFO_COPY_IIF (1 << 7)
207 unsigned int sockflags;
208 int mark;
209 const char *bind_dev;
210
211 /* for stream sockets */
215
216 /* HTTP proxy */
218
219 /* Socks proxy */
221 struct link_socket_actual socks_relay; /* Socks UDP relay address */
222
223 /* The OpenVPN server we will use the proxy to connect to */
224 const char *proxy_dest_host;
225 const char *proxy_dest_port;
226
227 /* Pointer to the server-poll to trigger the timeout in function which have
228 * their own loop instead of using the main oop */
230
231#if PASSTOS_CAPABILITY
232 /* used to get/set TOS. */
233#if defined(TARGET_LINUX)
234 uint8_t ptos;
235#else /* all the BSDs, Solaris, MacOS use plain "int" -> see "man ip" there */
236 int ptos;
237#endif
238 bool ptos_defined;
239#endif
240
241#ifdef ENABLE_DEBUG
242 int gremlin; /* --gremlin bits */
243#endif
244};
245
246/*
247 * Some Posix/Win32 differences.
248 */
249
250#ifndef MSG_NOSIGNAL
251#define MSG_NOSIGNAL 0
252#endif
253
254#ifdef _WIN32
255
256#define openvpn_close_socket(s) closesocket(s)
257
258int socket_recv_queue(struct link_socket *sock, int maxsize);
259
260int socket_send_queue(struct link_socket *sock, const struct buffer *buf,
261 const struct link_socket_actual *to);
262
263typedef struct
264{
265 union
266 {
267 SOCKET s;
268 HANDLE h;
269 };
271 bool prepend_sa; /* are incoming packets prepended with sockaddr? */
273
274int sockethandle_finalize(sockethandle_t sh, struct overlapped_io *io, struct buffer *buf,
275 struct link_socket_actual *from);
276
277static inline BOOL
279{
280 return sh.is_handle
281 ? GetOverlappedResult(sh.h, &io->overlapped, &io->size, FALSE)
282 : WSAGetOverlappedResult(sh.s, &io->overlapped, &io->size, FALSE, &io->flags);
283}
284
285static inline int
287{
288 return sh.is_handle ? (int)GetLastError() : WSAGetLastError();
289}
290
291inline static void
293{
294 sh.is_handle ? SetLastError(err) : WSASetLastError(err);
295}
296
297static inline void
299{
300 sh.is_handle ? SetLastError(ERROR_INVALID_FUNCTION) : WSASetLastError(WSAEINVAL);
301}
302
303/* winsock(2).h uses slightly different types so to avoid conversion
304 errors we wrap these functions on Windows */
305
306static inline int
307openvpn_select(socket_descriptor_t nfds, fd_set *readfds, fd_set *writefds,
308 fd_set *exceptfds, struct timeval *timeout)
309{
310 (void)nfds; /* first argument ignored on Windows */
311 return select(0, readfds, writefds, exceptfds, timeout);
312}
313
314static inline ssize_t
315openvpn_send(socket_descriptor_t sockfd, const void *buf, size_t len, int flags)
316{
317 ASSERT(len <= INT_MAX);
318 return send(sockfd, buf, (int)len, flags);
319}
320
321static inline int
322openvpn_bind(socket_descriptor_t sockfd, const struct sockaddr *addr, size_t addrlen)
323{
324 ASSERT(addrlen <= INT_MAX);
325 return bind(sockfd, addr, (int)addrlen);
326}
327
328#else /* ifdef _WIN32 */
329
330#define openvpn_close_socket(s) close(s)
331#define openvpn_select(nfds, readfds, writefds, exceptfds, timeout) \
332 select(nfds, readfds, writefds, exceptfds, timeout)
333#define openvpn_send(sockfd, buf, len, flags) send(sockfd, buf, len, flags)
334#define openvpn_bind(sockfd, addr, addrlen) bind(sockfd, addr, addrlen)
335
336#endif /* ifdef _WIN32 */
337
338struct link_socket *link_socket_new(void);
339
340void socket_bind(socket_descriptor_t sd, const struct addrinfo *local, int af_family,
341 const char *prefix, bool ipv6only);
342
343int openvpn_connect(socket_descriptor_t sd, const struct sockaddr *remote, int connect_timeout,
344 volatile int *signal_received);
345
346
347/*
348 * Initialize link_socket object.
349 */
350void link_socket_init_phase1(struct context *c, int sock_index, int mode);
351
352void link_socket_init_phase2(struct context *c, struct link_socket *sock);
353
354void link_socket_close(struct link_socket *sock);
355
356#ifdef ENABLE_MANAGEMENT
358#endif
359
360void bad_address_length(int actual, int expected);
361
362/* IPV4_INVALID_ADDR: returned by link_socket_current_remote()
363 * to ease redirect-gateway logic for ipv4 tunnels on ipv6 endpoints
364 */
365#define IPV4_INVALID_ADDR 0xffffffff
367
368const struct in6_addr *link_socket_current_remote_ipv6(const struct link_socket_info *info);
369
371 const struct link_socket_actual *addr,
372 const char *common_name, struct env_set *es);
373
374void link_socket_bad_incoming_addr(struct buffer *buf, const struct link_socket_info *info,
375 const struct link_socket_actual *from_addr);
376
377void set_actual_address(struct link_socket_actual *actual, struct addrinfo *ai);
378
380
381void setenv_trusted(struct env_set *es, const struct link_socket_info *info);
382
383void link_socket_update_buffer_sizes(struct link_socket *sock, int rcvbuf, int sndbuf);
384
385/*
386 * Low-level functions
387 */
388
389socket_descriptor_t create_socket_tcp(struct addrinfo *);
390
400 const struct socket_buffer_size *sbs, int mark,
401 const char *bind_dev, struct addrinfo *bind_addr,
402 bool bind_ipv6_only, bool optional);
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 *
449socket_foreign_protocol_head(const struct link_socket *sock)
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
463socket_connection_reset(const struct link_socket *sock, ssize_t status)
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
476 || err == ERROR_CONNECTION_ABORTED;
477#else
478 return err == ECONNRESET;
479#endif
480 }
481 }
482 return false;
483}
484
485static inline bool
486link_socket_verify_incoming_addr(const struct buffer *buf, const struct link_socket_info *info,
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:
495 if (!link_socket_actual_defined(from_addr))
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(const 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(const struct link_socket *sock, const struct buffer *buf,
697 struct link_socket_actual *to);
698
699
700static inline ssize_t
701link_socket_write_udp_posix(const struct link_socket *sock, const 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, CBPTR(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(const struct link_socket *sock, const struct buffer *buf)
718{
719 return send(sock->sd, CBPTR(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 = (const struct openvpn_iphdr *)CBPTR(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(const 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.
static bool buf_write_prepend(struct buffer *dest, const void *src, int size)
Prepend data to a buffer.
Definition buffer.h:1263
#define CBPTR(buf)
Return a const pointer to the start of the buffer content.
Definition buffer.h:141
#define BLEN(buf)
Return the length of the buffer content in bytes.
Definition buffer.h:151
#define BLENZ(buf)
Return the length of the buffer content as a size_t.
Definition buffer.h:153
const struct rw_handle * event_t
Definition event.h:89
#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:221
void link_socket_init_phase1(struct context *c, int sock_index, int mode)
Definition socket.c:979
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:1670
static BOOL SocketHandleGetOverlappedResult(sockethandle_t sh, struct overlapped_io *io)
Definition socket.h:278
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:1334
ssize_t link_socket_write_tcp(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
Definition socket.c:2101
void link_socket_update_buffer_sizes(struct link_socket *sock, int rcvbuf, int sndbuf)
Definition socket.c:161
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:1888
const struct in6_addr * link_socket_current_remote_ipv6(const struct link_socket_info *info)
Definition socket.c:1637
void set_actual_address(struct link_socket_actual *actual, struct addrinfo *ai)
Definition socket.c:701
void socket_bind(socket_descriptor_t sd, const struct addrinfo *local, int af_family, const char *prefix, bool ipv6only)
Definition socket.c:560
static ssize_t link_socket_write(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
Definition socket.h:736
static bool link_socket_verify_incoming_addr(const struct buffer *buf, const struct link_socket_info *info, const struct link_socket_actual *from_addr)
Definition socket.h:486
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:204
void bad_address_length(int actual, int expected)
Definition socket.c:1907
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:292
socket_descriptor_t create_socket_tcp(struct addrinfo *)
Definition socket.c:177
static int SocketHandleGetLastError(sockethandle_t sh)
Definition socket.h:286
static void SocketHandleSetInvalError(sockethandle_t sh)
Definition socket.h:298
ssize_t link_socket_read_tcp(struct link_socket *sock, struct buffer *buf)
Definition socket.c:1919
socket_descriptor_t socket_do_accept(socket_descriptor_t sd, struct link_socket_actual *act, const bool nowait)
Definition socket.c:426
int socket_recv_queue(struct link_socket *sock, int maxsize)
Definition socket.c:2216
void link_socket_close(struct link_socket *sock)
Definition socket.c:1444
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:103
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:1774
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:199
#define MSG_NOSIGNAL
Definition socket.h:251
int socket_send_queue(struct link_socket *sock, const struct buffer *buf, const struct link_socket_actual *to)
Definition socket.c:2316
uint16_t packet_size_type
Definition socket.h:56
void link_socket_bad_outgoing_addr(void)
Definition socket.c:1597
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:2501
in_addr_t link_socket_current_remote(const struct link_socket_info *info)
Definition socket.c:1603
int openvpn_connect(socket_descriptor_t sd, const struct sockaddr *remote, int connect_timeout, volatile int *signal_received)
Definition socket.c:608
#define SF_PREPEND_SA
Definition socket.h:205
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:1570
unsigned int socket_set(struct link_socket *sock, struct event_set *es, unsigned int rwflags, void *arg, unsigned int *persistent)
Definition socket.c:2594
static int openvpn_select(socket_descriptor_t nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
Definition socket.h:307
static ssize_t link_socket_write_udp(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
Definition socket.h:725
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:965
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:1518
bool sockets_read_residual(const struct context *c)
Definition socket.c:46
socket_descriptor_t create_socket_udp_configured(sa_family_t af, unsigned int sockflags, const struct socket_buffer_size *sbs, int mark, const char *bind_dev, struct addrinfo *bind_addr, bool bind_ipv6_only, bool optional)
Create a UDP socket for af set up the way a link socket is: –sndbuf/–rcvbuf, –mark,...
Definition socket.c:309
static int openvpn_bind(socket_descriptor_t sockfd, const struct sockaddr *addr, size_t addrlen)
Definition socket.h:322
void setenv_trusted(struct env_set *es, const struct link_socket_info *info)
Definition socket.c:1496
static ssize_t openvpn_send(socket_descriptor_t sockfd, const void *buf, size_t len, int flags)
Definition socket.h:315
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 addrlist_match_proto(const struct openvpn_sockaddr *a1, const struct addrinfo *addr_list, const int proto)
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)
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 resolve.h:89
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::@051240265251124304325377241125360350250341115171 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:270
bool prepend_sa
Definition socket.h:271
struct used to extract packets encapsulated in streams into a buffer, in this case OpenVPN packets (d...
Definition socket.h:97
struct buffer buf
Holds the data of the current packet.
Definition socket.h:113
bool error
if true, a fatal TCP error has occurred, requiring that connection be restarted
Definition socket.h:122
struct buffer residual
buffer holding the excess bytes that are not part of the packet.
Definition socket.h:103
bool residual_fully_formed
The buffer in buf contains a full packet without a header.
Definition socket.h:110
int maxlen
Maximum length of a packet that we accept.
Definition socket.h:106
int len
-1 if not yet known.
Definition socket.h:118
struct buffer buf_init
Definition socket.h:99
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 gc_arena gc
Definition test_ssl.c:122
long reset_net_event_win32(struct rw_handle *event, socket_descriptor_t sd)
Definition win32.c:356
static bool overlapped_io_active(const struct overlapped_io *o)
Definition win32.h:230