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
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
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
291#if defined(__GNUC__) || defined(__clang__)
292#pragma GCC diagnostic push
293#pragma GCC diagnostic ignored "-Wsign-conversion"
294#endif
295
296inline static void
298{
299 sh.is_handle ? SetLastError(err) : WSASetLastError(err);
300}
301
302#if defined(__GNUC__) || defined(__clang__)
303#pragma GCC diagnostic pop
304#endif
305
306static inline void
308{
309 sh.is_handle ? SetLastError(ERROR_INVALID_FUNCTION) : WSASetLastError(WSAEINVAL);
310}
311
312/* winsock(2).h uses slightly different types so to avoid conversion
313 errors we wrap these functions on Windows */
314
315static inline int
316openvpn_select(socket_descriptor_t nfds, fd_set *readfds, fd_set *writefds,
317 fd_set *exceptfds, struct timeval *timeout)
318{
319 (void)nfds; /* first argument ignored on Windows */
320 return select(0, readfds, writefds, exceptfds, timeout);
321}
322
323static inline ssize_t
324openvpn_send(socket_descriptor_t sockfd, const void *buf, size_t len, int flags)
325{
326 ASSERT(len <= INT_MAX);
327 return send(sockfd, buf, (int)len, flags);
328}
329
330static inline int
331openvpn_bind(socket_descriptor_t sockfd, const struct sockaddr *addr, size_t addrlen)
332{
333 ASSERT(addrlen <= INT_MAX);
334 return bind(sockfd, addr, (int)addrlen);
335}
336
337#else /* ifdef _WIN32 */
338
339#define openvpn_close_socket(s) close(s)
340#define openvpn_select(nfds, readfds, writefds, exceptfds, timeout) \
341 select(nfds, readfds, writefds, exceptfds, timeout)
342#define openvpn_send(sockfd, buf, len, flags) send(sockfd, buf, len, flags)
343#define openvpn_bind(sockfd, addr, addrlen) bind(sockfd, addr, addrlen)
344
345#endif /* ifdef _WIN32 */
346
347struct link_socket *link_socket_new(void);
348
349void socket_bind(socket_descriptor_t sd, const struct addrinfo *local, int af_family,
350 const char *prefix, bool ipv6only);
351
352int openvpn_connect(socket_descriptor_t sd, const struct sockaddr *remote, int connect_timeout,
353 volatile int *signal_received);
354
355
356/*
357 * Initialize link_socket object.
358 */
359void link_socket_init_phase1(struct context *c, int sock_index, int mode);
360
361void link_socket_init_phase2(struct context *c, struct link_socket *sock);
362
363void link_socket_close(struct link_socket *sock);
364
365#ifdef ENABLE_MANAGEMENT
367#endif
368
369void bad_address_length(int actual, int expected);
370
371/* IPV4_INVALID_ADDR: returned by link_socket_current_remote()
372 * to ease redirect-gateway logic for ipv4 tunnels on ipv6 endpoints
373 */
374#define IPV4_INVALID_ADDR 0xffffffff
376
377const struct in6_addr *link_socket_current_remote_ipv6(const struct link_socket_info *info);
378
380 const struct link_socket_actual *addr,
381 const char *common_name, struct env_set *es);
382
383void link_socket_bad_incoming_addr(struct buffer *buf, const struct link_socket_info *info,
384 const struct link_socket_actual *from_addr);
385
386void set_actual_address(struct link_socket_actual *actual, struct addrinfo *ai);
387
389
390void setenv_trusted(struct env_set *es, const struct link_socket_info *info);
391
392void link_socket_update_buffer_sizes(struct link_socket *sock, int rcvbuf, int sndbuf);
393
394/*
395 * Low-level functions
396 */
397
398socket_descriptor_t create_socket_tcp(struct addrinfo *);
399
409 const struct socket_buffer_size *sbs, int mark,
410 const char *bind_dev, struct addrinfo *bind_addr,
411 bool bind_ipv6_only, bool optional);
412
414 const bool nowait);
415
416#if UNIX_SOCK_SUPPORT
417
418socket_descriptor_t create_socket_unix(void);
419
420void socket_bind_unix(socket_descriptor_t sd, struct sockaddr_un *local, const char *prefix);
421
422socket_descriptor_t socket_accept_unix(socket_descriptor_t sd, struct sockaddr_un *remote);
423
424int socket_connect_unix(socket_descriptor_t sd, struct sockaddr_un *remote);
425
426void sockaddr_unix_init(struct sockaddr_un *local, const char *path);
427
428const char *sockaddr_unix_name(const struct sockaddr_un *local, const char *null);
429
430void socket_delete_unix(const struct sockaddr_un *local);
431
432bool unix_socket_get_peer_uid_gid(const socket_descriptor_t sd, uid_t *uid, gid_t *gid);
433
434#endif /* if UNIX_SOCK_SUPPORT */
435
436static inline bool
438{
439 if (sock)
440 {
442 }
443 else
444 {
445 return false;
446 }
447}
448
449#if PORT_SHARE
450
451static inline bool
452socket_foreign_protocol_detected(const struct link_socket *sock)
453{
454 return link_socket_connection_oriented(sock) && sock->stream_buf.port_share_state == PS_FOREIGN;
455}
456
457static inline const struct buffer *
459{
460 return &sock->stream_buf.buf;
461}
462
463static inline int
464socket_foreign_protocol_sd(const struct link_socket *sock)
465{
466 return sock->sd;
467}
468
469#endif /* if PORT_SHARE */
470
471#if defined(__GNUC__) || defined(__clang__)
472#pragma GCC diagnostic push
473#pragma GCC diagnostic ignored "-Wsign-conversion"
474#endif
475
476static inline bool
478{
480 {
481 if (sock->stream_reset || sock->stream_buf.error)
482 {
483 return true;
484 }
485 else if (status < 0)
486 {
487 const int err = openvpn_errno();
488#ifdef _WIN32
489 return err == WSAECONNRESET || err == WSAECONNABORTED
491#else
492 return err == ECONNRESET;
493#endif
494 }
495 }
496 return false;
497}
498
499#if defined(__GNUC__) || defined(__clang__)
500#pragma GCC diagnostic pop
501#endif
502
503static inline bool
504link_socket_verify_incoming_addr(const struct buffer *buf, const struct link_socket_info *info,
505 const struct link_socket_actual *from_addr)
506{
507 if (buf->len > 0)
508 {
509 switch (from_addr->dest.addr.sa.sa_family)
510 {
511 case AF_INET6:
512 case AF_INET:
514 {
515 return false;
516 }
517 if (info->remote_float || (!info->lsa->remote_list))
518 {
519 return true;
520 }
521 if (addrlist_match_proto(&from_addr->dest, info->lsa->remote_list, info->proto))
522 {
523 return true;
524 }
525 }
526 }
527 return false;
528}
529
530static inline void
532 struct link_socket_actual **act)
533{
534 if (buf->len > 0)
535 {
536 struct link_socket_addr *lsa = info->lsa;
538 {
539 *act = &lsa->actual;
540 }
541 else
542 {
544 buf->len = 0;
545 *act = NULL;
546 }
547 }
548}
549
550static inline void
552 const char *common_name, struct env_set *es)
553{
554 struct link_socket_addr *lsa = info->lsa;
555 if (
556 /* new or changed address? */
558 || !addr_match_proto(&act->dest, &lsa->actual.dest, info->proto))
559 &&
560 /* address undef or address == remote or --float */
561 (info->remote_float
562 || (!lsa->remote_list || addrlist_match_proto(&act->dest, lsa->remote_list, info->proto))))
563 {
564 link_socket_connection_initiated(info, act, common_name, es);
565 }
566}
567
579
580static inline bool
582{
584 {
586 }
587 else
588 {
589 return true;
590 }
591}
592
598static inline bool
600{
601 return s->sockflags & SF_DCO_WIN;
602}
603
604/*
605 * Socket Read Routines
606 */
607
608ssize_t link_socket_read_tcp(struct link_socket *sock, struct buffer *buf);
609
610#ifdef _WIN32
611
612static inline int
614 struct link_socket_actual *from)
615{
616 sockethandle_t sh = { .s = sock->sd };
617 if (socket_is_dco_win(sock))
618 {
619 *from = sock->info.lsa->actual;
620 sh.is_handle = true;
622 }
623 return sockethandle_finalize(sh, &sock->reads, buf, from);
624}
625
626#else /* ifdef _WIN32 */
627
628ssize_t link_socket_read_udp_posix(const struct link_socket *sock, struct buffer *buf,
629 struct link_socket_actual *from);
630
631#endif /* ifdef _WIN32 */
632
633/* read a TCP or UDP packet from link */
634static inline ssize_t
635link_socket_read(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *from)
636{
637 if (proto_is_udp(sock->info.proto) || socket_is_dco_win(sock))
638 /* unified UDPv4 and UDPv6, for DCO-WIN the kernel
639 * will strip the length header */
640 {
641 ssize_t res;
642
643#ifdef _WIN32
644 res = link_socket_read_udp_win32(sock, buf, from);
645#else
646 res = link_socket_read_udp_posix(sock, buf, from);
647#endif
648 return res;
649 }
650 else if (proto_is_tcp(sock->info.proto)) /* unified TCPv4 and TCPv6 */
651 {
652 /* from address was returned by accept */
653 from->dest = sock->info.lsa->actual.dest;
654 return link_socket_read_tcp(sock, buf);
655 }
656 else
657 {
658 ASSERT(0);
659 return -1; /* NOTREACHED */
660 }
661}
662
663/*
664 * Socket Write routines
665 */
666
667ssize_t link_socket_write_tcp(struct link_socket *sock, struct buffer *buf,
668 struct link_socket_actual *to);
669
670#if defined(__GNUC__) || defined(__clang__)
671#pragma GCC diagnostic push
672#pragma GCC diagnostic ignored "-Wsign-conversion"
673#endif
674
675#ifdef _WIN32
676
677static inline int
678link_socket_write_win32(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
679{
680 int err = 0;
681 int status = 0;
682 sockethandle_t sh = { .s = sock->sd, .is_handle = socket_is_dco_win(sock) };
683 if (overlapped_io_active(&sock->writes))
684 {
685 status = sockethandle_finalize(sh, &sock->writes, NULL, NULL);
686 if (status < 0)
687 {
688 err = SocketHandleGetLastError(sh);
689 }
690 }
691
692 /* dco-win mp requires control packets to be prepended with sockaddr */
693 if (sock->sockflags & SF_PREPEND_SA)
694 {
695 if (to->dest.addr.sa.sa_family == AF_INET)
696 {
697 buf_write_prepend(buf, &to->dest.addr.in4, sizeof(struct sockaddr_in));
698 }
699 else
700 {
701 buf_write_prepend(buf, &to->dest.addr.in6, sizeof(struct sockaddr_in6));
702 }
703 }
704
705 socket_send_queue(sock, buf, to);
706 if (status < 0)
707 {
709 return status;
710 }
711 else
712 {
713 return BLEN(buf);
714 }
715}
716
717#else /* ifdef _WIN32 */
718
719ssize_t link_socket_write_udp_posix_sendmsg(const struct link_socket *sock, const struct buffer *buf,
720 struct link_socket_actual *to);
721
722
723static inline ssize_t
724link_socket_write_udp_posix(const struct link_socket *sock, const struct buffer *buf,
725 struct link_socket_actual *to)
726{
727#if ENABLE_IP_PKTINFO
728 if (proto_is_udp(sock->info.proto) && (sock->sockflags & SF_USE_IP_PKTINFO)
729 && addr_defined_ipi(to))
730 {
731 return link_socket_write_udp_posix_sendmsg(sock, buf, to);
732 }
733 else
734#endif
735 return sendto(sock->sd, CBPTR(buf), BLENZ(buf), 0, (struct sockaddr *)&to->dest.addr.sa,
736 (socklen_t)af_addr_size(to->dest.addr.sa.sa_family));
737}
738
739static inline ssize_t
740link_socket_write_tcp_posix(const struct link_socket *sock, const struct buffer *buf)
741{
742 return send(sock->sd, CBPTR(buf), BLENZ(buf), MSG_NOSIGNAL);
743}
744
745#endif /* ifdef _WIN32 */
746
747#if defined(__GNUC__) || defined(__clang__)
748#pragma GCC diagnostic pop
749#endif
750
751static inline ssize_t
752link_socket_write_udp(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
753{
754#ifdef _WIN32
755 return link_socket_write_win32(sock, buf, to);
756#else
757 return link_socket_write_udp_posix(sock, buf, to);
758#endif
759}
760
761/* write a TCP or UDP packet to link */
762static inline ssize_t
763link_socket_write(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
764{
765 if (proto_is_udp(sock->info.proto) || socket_is_dco_win(sock))
766 {
767 /* unified UDPv4, UDPv6 and DCO-WIN (driver adds length header) */
768 return link_socket_write_udp(sock, buf, to);
769 }
770 else if (proto_is_tcp(sock->info.proto)) /* unified TCPv4 and TCPv6 */
771 {
772 return link_socket_write_tcp(sock, buf, to);
773 }
774 else
775 {
776 ASSERT(0);
777 return -1; /* NOTREACHED */
778 }
779}
780
781#if PASSTOS_CAPABILITY
782
783/*
784 * Extract TOS bits. Assumes that ipbuf is a valid IPv4 packet.
785 */
786static inline void
787link_socket_extract_tos(struct link_socket *sock, const struct buffer *ipbuf)
788{
789 if (sock && ipbuf)
790 {
791 const struct openvpn_iphdr *iph = (const struct openvpn_iphdr *)CBPTR(ipbuf);
792 sock->ptos = iph->tos;
793 sock->ptos_defined = true;
794 }
795}
796
797/*
798 * Set socket properties to reflect TOS bits which were extracted
799 * from tunnel packet.
800 */
801static inline void
802link_socket_set_tos(const struct link_socket *sock)
803{
804 if (sock && sock->ptos_defined)
805 {
806 setsockopt(sock->sd, IPPROTO_IP, IP_TOS, (const void *)&sock->ptos, sizeof(sock->ptos));
807 }
808}
809
810#endif /* if PASSTOS_CAPABILITY */
811
812/*
813 * Socket I/O wait functions
814 */
815
816/*
817 * Extends the pre-existing read residual logic
818 * to all initialized sockets, ensuring the complete
819 * packet is read.
820 */
821bool sockets_read_residual(const struct context *c);
822
823static inline event_t
825{
826#ifdef _WIN32
827 return &sock->rw_handle;
828#else
829 return sock->sd;
830#endif
831}
832
834
835unsigned int socket_set(struct link_socket *sock, struct event_set *es, unsigned int rwflags,
836 void *arg, unsigned int *persistent);
837
838static inline void
839socket_set_listen_persistent(struct link_socket *sock, struct event_set *es, void *arg)
840{
841 if (sock && !sock->listen_persistent_queued)
842 {
844 sock->listen_persistent_queued = true;
845 }
846}
847
848static inline void
850{
851#ifdef _WIN32
853#endif
854}
855
856const char *socket_stat(const struct link_socket *sock, unsigned int rwflags, struct gc_arena *gc);
857
858#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:1268
#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
#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:977
static event_t socket_event_handle(const struct link_socket *sock)
Definition socket.h:824
const char * socket_stat(const struct link_socket *sock, unsigned int rwflags, struct gc_arena *gc)
Definition socket.c:1668
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:635
void link_socket_init_phase2(struct context *c, struct link_socket *sock)
Definition socket.c:1332
ssize_t link_socket_write_tcp(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
Definition socket.c:2103
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:477
event_t socket_listen_event_handle(struct link_socket *sock)
Definition socket.c:1886
const struct in6_addr * link_socket_current_remote_ipv6(const struct link_socket_info *info)
Definition socket.c:1635
void set_actual_address(struct link_socket_actual *actual, struct addrinfo *ai)
Definition socket.c:703
void socket_bind(socket_descriptor_t sd, const struct addrinfo *local, int af_family, const char *prefix, bool ipv6only)
Definition socket.c:562
static ssize_t link_socket_write(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
Definition socket.h:763
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:504
static void socket_set_listen_persistent(struct link_socket *sock, struct event_set *es, void *arg)
Definition socket.h:839
#define SF_DCO_WIN
Definition socket.h:204
void bad_address_length(int actual, int expected)
Definition socket.c:1905
static bool link_socket_connection_oriented(const struct link_socket *sock)
Definition socket.h:437
static bool stream_buf_read_setup(struct link_socket *sock)
Definition socket.h:581
void sd_close(socket_descriptor_t *sd)
static void socket_reset_listen_persistent(struct link_socket *sock)
Definition socket.h:849
static int link_socket_read_udp_win32(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *from)
Definition socket.h:613
static void SocketHandleSetLastError(sockethandle_t sh, DWORD err)
Definition socket.h:297
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:307
ssize_t link_socket_read_tcp(struct link_socket *sock, struct buffer *buf)
Definition socket.c:1917
socket_descriptor_t socket_do_accept(socket_descriptor_t sd, struct link_socket_actual *act, const bool nowait)
Definition socket.c:431
int socket_recv_queue(struct link_socket *sock, int maxsize)
Definition socket.c:2218
void link_socket_close(struct link_socket *sock)
Definition socket.c:1442
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:551
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:1772
static void link_socket_get_outgoing_addr(struct buffer *buf, const struct link_socket_info *info, struct link_socket_actual **act)
Definition socket.h:531
#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:2318
uint16_t packet_size_type
Definition socket.h:56
void link_socket_bad_outgoing_addr(void)
Definition socket.c:1595
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:599
int sockethandle_finalize(sockethandle_t sh, struct overlapped_io *io, struct buffer *buf, struct link_socket_actual *from)
Definition socket.c:2503
in_addr_t link_socket_current_remote(const struct link_socket_info *info)
Definition socket.c:1601
int openvpn_connect(socket_descriptor_t sd, const struct sockaddr *remote, int connect_timeout, volatile int *signal_received)
Definition socket.c:610
#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:1568
unsigned int socket_set(struct link_socket *sock, struct event_set *es, unsigned int rwflags, void *arg, unsigned int *persistent)
Definition socket.c:2596
static int openvpn_select(socket_descriptor_t nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
Definition socket.h:316
static ssize_t link_socket_write_udp(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
Definition socket.h:752
static int link_socket_write_win32(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
Definition socket.h:678
struct link_socket * link_socket_new(void)
Definition socket.c:963
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:1516
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:331
void setenv_trusted(struct env_set *es, const struct link_socket_info *info)
Definition socket.c:1494
static ssize_t openvpn_send(socket_descriptor_t sockfd, const void *buf, size_t len, int flags)
Definition socket.h:324
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:445
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: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:405
SOCKET socket_descriptor_t
Definition syshead.h:441
uint32_t in_addr_t
Definition syshead.h:52
struct gc_arena gc
Definition test_ssl.c:138
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