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
370
371void bad_address_length(int actual, int expected);
372
373/* IPV4_INVALID_ADDR: returned by link_socket_current_remote()
374 * to ease redirect-gateway logic for ipv4 tunnels on ipv6 endpoints
375 */
376#define IPV4_INVALID_ADDR 0xffffffff
378
379const struct in6_addr *link_socket_current_remote_ipv6(const struct link_socket_info *info);
380
382 const struct link_socket_actual *addr,
383 const char *common_name, struct env_set *es);
384
385void link_socket_bad_incoming_addr(struct buffer *buf, const struct link_socket_info *info,
386 const struct link_socket_actual *from_addr);
387
388void set_actual_address(struct link_socket_actual *actual, struct addrinfo *ai);
389
391
392void setenv_trusted(struct env_set *es, const struct link_socket_info *info);
393
394void link_socket_update_buffer_sizes(struct link_socket *sock, int rcvbuf, int sndbuf);
395
396/*
397 * Low-level functions
398 */
399
400socket_descriptor_t create_socket_tcp(struct addrinfo *);
401
403 const bool nowait);
404
405#if UNIX_SOCK_SUPPORT
406
407socket_descriptor_t create_socket_unix(void);
408
409void socket_bind_unix(socket_descriptor_t sd, struct sockaddr_un *local, const char *prefix);
410
411socket_descriptor_t socket_accept_unix(socket_descriptor_t sd, struct sockaddr_un *remote);
412
413int socket_connect_unix(socket_descriptor_t sd, struct sockaddr_un *remote);
414
415void sockaddr_unix_init(struct sockaddr_un *local, const char *path);
416
417const char *sockaddr_unix_name(const struct sockaddr_un *local, const char *null);
418
419void socket_delete_unix(const struct sockaddr_un *local);
420
421bool unix_socket_get_peer_uid_gid(const socket_descriptor_t sd, uid_t *uid, gid_t *gid);
422
423#endif /* if UNIX_SOCK_SUPPORT */
424
425static inline bool
427{
428 if (sock)
429 {
431 }
432 else
433 {
434 return false;
435 }
436}
437
438#if PORT_SHARE
439
440static inline bool
441socket_foreign_protocol_detected(const struct link_socket *sock)
442{
443 return link_socket_connection_oriented(sock) && sock->stream_buf.port_share_state == PS_FOREIGN;
444}
445
446static inline const struct buffer *
448{
449 return &sock->stream_buf.buf;
450}
451
452static inline int
453socket_foreign_protocol_sd(const struct link_socket *sock)
454{
455 return sock->sd;
456}
457
458#endif /* if PORT_SHARE */
459
460static inline bool
462{
464 {
465 if (sock->stream_reset || sock->stream_buf.error)
466 {
467 return true;
468 }
469 else if (status < 0)
470 {
471 const int err = openvpn_errno();
472#ifdef _WIN32
473 return err == WSAECONNRESET || err == WSAECONNABORTED
475#else
476 return err == ECONNRESET;
477#endif
478 }
479 }
480 return false;
481}
482
483static inline bool
485 const struct link_socket_actual *from_addr)
486{
487 if (buf->len > 0)
488 {
489 switch (from_addr->dest.addr.sa.sa_family)
490 {
491 case AF_INET6:
492 case AF_INET:
494 {
495 return false;
496 }
497 if (info->remote_float || (!info->lsa->remote_list))
498 {
499 return true;
500 }
501 if (addrlist_match_proto(&from_addr->dest, info->lsa->remote_list, info->proto))
502 {
503 return true;
504 }
505 }
506 }
507 return false;
508}
509
510static inline void
512 struct link_socket_actual **act)
513{
514 if (buf->len > 0)
515 {
516 struct link_socket_addr *lsa = info->lsa;
518 {
519 *act = &lsa->actual;
520 }
521 else
522 {
524 buf->len = 0;
525 *act = NULL;
526 }
527 }
528}
529
530static inline void
532 const char *common_name, struct env_set *es)
533{
534 struct link_socket_addr *lsa = info->lsa;
535 if (
536 /* new or changed address? */
538 || !addr_match_proto(&act->dest, &lsa->actual.dest, info->proto))
539 &&
540 /* address undef or address == remote or --float */
541 (info->remote_float
542 || (!lsa->remote_list || addrlist_match_proto(&act->dest, lsa->remote_list, info->proto))))
543 {
544 link_socket_connection_initiated(info, act, common_name, es);
545 }
546}
547
559
560static inline bool
562{
564 {
566 }
567 else
568 {
569 return true;
570 }
571}
572
578static inline bool
580{
581 return s->sockflags & SF_DCO_WIN;
582}
583
584/*
585 * Socket Read Routines
586 */
587
588ssize_t link_socket_read_tcp(struct link_socket *sock, struct buffer *buf);
589
590#ifdef _WIN32
591
592static inline int
594 struct link_socket_actual *from)
595{
596 sockethandle_t sh = { .s = sock->sd };
597 if (socket_is_dco_win(sock))
598 {
599 *from = sock->info.lsa->actual;
600 sh.is_handle = true;
602 }
603 return sockethandle_finalize(sh, &sock->reads, buf, from);
604}
605
606#else /* ifdef _WIN32 */
607
608ssize_t link_socket_read_udp_posix(struct link_socket *sock, struct buffer *buf,
609 struct link_socket_actual *from);
610
611#endif /* ifdef _WIN32 */
612
613/* read a TCP or UDP packet from link */
614static inline ssize_t
615link_socket_read(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *from)
616{
617 if (proto_is_udp(sock->info.proto) || socket_is_dco_win(sock))
618 /* unified UDPv4 and UDPv6, for DCO-WIN the kernel
619 * will strip the length header */
620 {
621 ssize_t res;
622
623#ifdef _WIN32
624 res = link_socket_read_udp_win32(sock, buf, from);
625#else
626 res = link_socket_read_udp_posix(sock, buf, from);
627#endif
628 return res;
629 }
630 else if (proto_is_tcp(sock->info.proto)) /* unified TCPv4 and TCPv6 */
631 {
632 /* from address was returned by accept */
633 from->dest = sock->info.lsa->actual.dest;
634 return link_socket_read_tcp(sock, buf);
635 }
636 else
637 {
638 ASSERT(0);
639 return -1; /* NOTREACHED */
640 }
641}
642
643/*
644 * Socket Write routines
645 */
646
647ssize_t link_socket_write_tcp(struct link_socket *sock, struct buffer *buf,
648 struct link_socket_actual *to);
649
650#ifdef _WIN32
651
652static inline int
653link_socket_write_win32(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
654{
655 int err = 0;
656 int status = 0;
657 sockethandle_t sh = { .s = sock->sd, .is_handle = socket_is_dco_win(sock) };
658 if (overlapped_io_active(&sock->writes))
659 {
660 status = sockethandle_finalize(sh, &sock->writes, NULL, NULL);
661 if (status < 0)
662 {
663 err = SocketHandleGetLastError(sh);
664 }
665 }
666
667 /* dco-win mp requires control packets to be prepended with sockaddr */
668 if (sock->sockflags & SF_PREPEND_SA)
669 {
670 if (to->dest.addr.sa.sa_family == AF_INET)
671 {
672 buf_write_prepend(buf, &to->dest.addr.in4, sizeof(struct sockaddr_in));
673 }
674 else
675 {
676 buf_write_prepend(buf, &to->dest.addr.in6, sizeof(struct sockaddr_in6));
677 }
678 }
679
680 socket_send_queue(sock, buf, to);
681 if (status < 0)
682 {
684 return status;
685 }
686 else
687 {
688 return BLEN(buf);
689 }
690}
691
692#else /* ifdef _WIN32 */
693
694ssize_t link_socket_write_udp_posix_sendmsg(struct link_socket *sock, struct buffer *buf,
695 struct link_socket_actual *to);
696
697
698static inline ssize_t
699link_socket_write_udp_posix(struct link_socket *sock, struct buffer *buf,
700 struct link_socket_actual *to)
701{
702#if ENABLE_IP_PKTINFO
703 if (proto_is_udp(sock->info.proto) && (sock->sockflags & SF_USE_IP_PKTINFO)
704 && addr_defined_ipi(to))
705 {
706 return link_socket_write_udp_posix_sendmsg(sock, buf, to);
707 }
708 else
709#endif
710 return sendto(sock->sd, BPTR(buf), BLENZ(buf), 0, (struct sockaddr *)&to->dest.addr.sa,
711 (socklen_t)af_addr_size(to->dest.addr.sa.sa_family));
712}
713
714static inline ssize_t
715link_socket_write_tcp_posix(struct link_socket *sock, struct buffer *buf)
716{
717 return send(sock->sd, BPTR(buf), BLENZ(buf), MSG_NOSIGNAL);
718}
719
720#endif /* ifdef _WIN32 */
721
722static inline ssize_t
723link_socket_write_udp(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
724{
725#ifdef _WIN32
726 return link_socket_write_win32(sock, buf, to);
727#else
728 return link_socket_write_udp_posix(sock, buf, to);
729#endif
730}
731
732/* write a TCP or UDP packet to link */
733static inline ssize_t
734link_socket_write(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
735{
736 if (proto_is_udp(sock->info.proto) || socket_is_dco_win(sock))
737 {
738 /* unified UDPv4, UDPv6 and DCO-WIN (driver adds length header) */
739 return link_socket_write_udp(sock, buf, to);
740 }
741 else if (proto_is_tcp(sock->info.proto)) /* unified TCPv4 and TCPv6 */
742 {
743 return link_socket_write_tcp(sock, buf, to);
744 }
745 else
746 {
747 ASSERT(0);
748 return -1; /* NOTREACHED */
749 }
750}
751
752#if PASSTOS_CAPABILITY
753
754/*
755 * Extract TOS bits. Assumes that ipbuf is a valid IPv4 packet.
756 */
757static inline void
758link_socket_extract_tos(struct link_socket *sock, const struct buffer *ipbuf)
759{
760 if (sock && ipbuf)
761 {
762 struct openvpn_iphdr *iph = (struct openvpn_iphdr *)BPTR(ipbuf);
763 sock->ptos = iph->tos;
764 sock->ptos_defined = true;
765 }
766}
767
768/*
769 * Set socket properties to reflect TOS bits which were extracted
770 * from tunnel packet.
771 */
772static inline void
773link_socket_set_tos(struct link_socket *sock)
774{
775 if (sock && sock->ptos_defined)
776 {
777 setsockopt(sock->sd, IPPROTO_IP, IP_TOS, (const void *)&sock->ptos, sizeof(sock->ptos));
778 }
779}
780
781#endif /* if PASSTOS_CAPABILITY */
782
783/*
784 * Socket I/O wait functions
785 */
786
787/*
788 * Extends the pre-existing read residual logic
789 * to all initialized sockets, ensuring the complete
790 * packet is read.
791 */
792bool sockets_read_residual(const struct context *c);
793
794static inline event_t
796{
797#ifdef _WIN32
798 return &sock->rw_handle;
799#else
800 return sock->sd;
801#endif
802}
803
805
806unsigned int socket_set(struct link_socket *sock, struct event_set *es, unsigned int rwflags,
807 void *arg, unsigned int *persistent);
808
809static inline void
810socket_set_listen_persistent(struct link_socket *sock, struct event_set *es, void *arg)
811{
812 if (sock && !sock->listen_persistent_queued)
813 {
815 sock->listen_persistent_queued = true;
816 }
817}
818
819static inline void
821{
822#ifdef _WIN32
824#endif
825}
826
827const char *socket_stat(const struct link_socket *sock, unsigned int rwflags, struct gc_arena *gc);
828
829#endif /* SOCKET_H */
#define BPTR(buf)
Definition buffer.h:124
static bool buf_write_prepend(struct buffer *dest, const void *src, int size)
Definition buffer.h:674
#define BLEN(buf)
Definition buffer.h:127
#define BLENZ(buf)
Definition buffer.h:128
#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:1315
static event_t socket_event_handle(const struct link_socket *sock)
Definition socket.h:795
const char * socket_stat(const struct link_socket *sock, unsigned int rwflags, struct gc_arena *gc)
Definition socket.c:2009
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:615
void link_socket_init_phase2(struct context *c, struct link_socket *sock)
Definition socket.c:1669
int socket_send_queue(struct link_socket *sock, struct buffer *buf, const struct link_socket_actual *to)
Definition socket.c:2653
ssize_t link_socket_write_tcp(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
Definition socket.c:2440
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:461
event_t socket_listen_event_handle(struct link_socket *sock)
Definition socket.c:2227
const struct in6_addr * link_socket_current_remote_ipv6(const struct link_socket_info *info)
Definition socket.c:1976
void set_actual_address(struct link_socket_actual *actual, struct addrinfo *ai)
Definition socket.c:1032
static ssize_t link_socket_write(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
Definition socket.h:734
static void socket_set_listen_persistent(struct link_socket *sock, struct event_set *es, void *arg)
Definition socket.h:810
#define SF_DCO_WIN
Definition socket.h:215
void bad_address_length(int actual, int expected)
Definition socket.c:2246
static bool link_socket_connection_oriented(const struct link_socket *sock)
Definition socket.h:426
static bool stream_buf_read_setup(struct link_socket *sock)
Definition socket.h:561
void sd_close(socket_descriptor_t *sd)
Definition socket.c:2965
static void socket_reset_listen_persistent(struct link_socket *sock)
Definition socket.h:820
static int link_socket_read_udp_win32(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *from)
Definition socket.h:593
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:2258
socket_descriptor_t socket_do_accept(socket_descriptor_t sd, struct link_socket_actual *act, const bool nowait)
Definition socket.c:756
int socket_recv_queue(struct link_socket *sock, int maxsize)
Definition socket.c:2553
void link_socket_close(struct link_socket *sock)
Definition socket.c:1783
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:484
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:531
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:2113
static void link_socket_get_outgoing_addr(struct buffer *buf, const struct link_socket_info *info, struct link_socket_actual **act)
Definition socket.h:511
#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:1936
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:579
int sockethandle_finalize(sockethandle_t sh, struct overlapped_io *io, struct buffer *buf, struct link_socket_actual *from)
Definition socket.c:2838
in_addr_t link_socket_current_remote(const struct link_socket_info *info)
Definition socket.c:1942
int openvpn_connect(socket_descriptor_t sd, const struct sockaddr *remote, int connect_timeout, volatile int *signal_received)
Definition socket.c:939
#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:1909
unsigned int socket_set(struct link_socket *sock, struct event_set *es, unsigned int rwflags, void *arg, unsigned int *persistent)
Definition socket.c:2931
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:723
void socket_bind(socket_descriptor_t sd, struct addrinfo *local, int af_family, const char *prefix, bool ipv6only)
Definition socket.c:889
static int link_socket_write_win32(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
Definition socket.h:653
struct link_socket * link_socket_new(void)
Definition socket.c:1301
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:1857
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:1835
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 bool addr_defined_ipi(const struct link_socket_actual *lsa)
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:61
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:66
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:117
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:211
DWORD size
Definition win32.h:210
OVERLAPPED overlapped
Definition win32.h:209
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:133
long reset_net_event_win32(struct rw_handle *event, socket_descriptor_t sd)
Definition win32.c:259
static bool overlapped_io_active(struct overlapped_io *o)
Definition win32.h:229