OpenVPN
Data Structures | Macros | Typedefs | Functions
socket.h File Reference
#include "buffer.h"
#include "common.h"
#include "error.h"
#include "proto.h"
#include "mtu.h"
#include "win32.h"
#include "event.h"
#include "proxy.h"
#include "socks.h"
#include "misc.h"
#include "tun.h"
#include "socket_util.h"
Include dependency graph for socket.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  cached_dns_entry
 
struct  link_socket_addr
 
struct  link_socket_info
 
struct  stream_buf
 struct used to extract packets encapsulated in streams into a buffer, in this case OpenVPN packets (data or control) embedded in a TCP stream. More...
 
struct  socket_buffer_size
 
struct  link_socket
 
struct  sockethandle_t
 

Macros

#define OPENVPN_PORT   "1194"
 
#define RESOLV_RETRY_INFINITE   1000000000
 
#define htonps(x)   htons(x)
 
#define ntohps(x)   ntohs(x)
 
#define LS_MODE_DEFAULT   0
 
#define LS_MODE_TCP_LISTEN   1
 
#define LS_MODE_TCP_ACCEPT_FROM   2
 
#define SF_USE_IP_PKTINFO   (1 << 0)
 
#define SF_TCP_NODELAY   (1 << 1)
 
#define SF_PORT_SHARE   (1 << 2)
 
#define SF_HOST_RANDOMIZE   (1 << 3)
 
#define SF_GETADDRINFO_DGRAM   (1 << 4)
 
#define SF_DCO_WIN   (1 << 5)
 
#define SF_PREPEND_SA   (1 << 6)
 
#define SF_PKTINFO_COPY_IIF   (1 << 7)
 
#define MSG_NOSIGNAL   0
 
#define openvpn_close_socket(s)   closesocket(s)
 
#define IPV4_INVALID_ADDR   0xffffffff
 

Typedefs

typedef uint16_t packet_size_type
 

Functions

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.
 
int socket_recv_queue (struct link_socket *sock, int maxsize)
 
int socket_send_queue (struct link_socket *sock, struct buffer *buf, const struct link_socket_actual *to)
 
int sockethandle_finalize (sockethandle_t sh, struct overlapped_io *io, struct buffer *buf, struct link_socket_actual *from)
 
static BOOL SocketHandleGetOverlappedResult (sockethandle_t sh, struct overlapped_io *io)
 
static int SocketHandleGetLastError (sockethandle_t sh)
 
static void SocketHandleSetLastError (sockethandle_t sh, DWORD err)
 
static void SocketHandleSetInvalError (sockethandle_t sh)
 
static int openvpn_select (socket_descriptor_t nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
 
static ssize_t openvpn_send (socket_descriptor_t sockfd, const void *buf, size_t len, int flags)
 
static int openvpn_bind (socket_descriptor_t sockfd, const struct sockaddr *addr, size_t addrlen)
 
struct link_socketlink_socket_new (void)
 
void socket_bind (socket_descriptor_t sd, struct addrinfo *local, int af_family, const char *prefix, bool ipv6only)
 
int openvpn_connect (socket_descriptor_t sd, const struct sockaddr *remote, int connect_timeout, volatile int *signal_received)
 
void link_socket_init_phase1 (struct context *c, int sock_index, int mode)
 
void link_socket_init_phase2 (struct context *c, struct link_socket *sock)
 
void do_preresolve (struct context *c)
 
void link_socket_close (struct link_socket *sock)
 
void sd_close (socket_descriptor_t *sd)
 
void bad_address_length (int actual, int expected)
 
in_addr_t link_socket_current_remote (const struct link_socket_info *info)
 
const struct in6_addr * link_socket_current_remote_ipv6 (const struct link_socket_info *info)
 
void link_socket_connection_initiated (struct link_socket_info *info, const struct link_socket_actual *addr, const char *common_name, struct env_set *es)
 
void link_socket_bad_incoming_addr (struct buffer *buf, const struct link_socket_info *info, const struct link_socket_actual *from_addr)
 
void set_actual_address (struct link_socket_actual *actual, struct addrinfo *ai)
 
void link_socket_bad_outgoing_addr (void)
 
void setenv_trusted (struct env_set *es, const struct link_socket_info *info)
 
bool link_socket_update_flags (struct link_socket *sock, unsigned int sockflags)
 
void link_socket_update_buffer_sizes (struct link_socket *sock, int rcvbuf, int sndbuf)
 
socket_descriptor_t create_socket_tcp (struct addrinfo *)
 
socket_descriptor_t socket_do_accept (socket_descriptor_t sd, struct link_socket_actual *act, const bool nowait)
 
static bool link_socket_connection_oriented (const struct link_socket *sock)
 
static bool socket_connection_reset (const struct link_socket *sock, int status)
 
static bool link_socket_verify_incoming_addr (struct buffer *buf, const struct link_socket_info *info, const struct link_socket_actual *from_addr)
 
static void link_socket_get_outgoing_addr (struct buffer *buf, const struct link_socket_info *info, struct link_socket_actual **act)
 
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)
 
bool stream_buf_read_setup_dowork (struct stream_buf *sb)
 Will try to check if the buffers in stream form a full packet.
 
static bool stream_buf_read_setup (struct link_socket *sock)
 
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.
 
int link_socket_read_tcp (struct link_socket *sock, struct buffer *buf)
 
static int link_socket_read_udp_win32 (struct link_socket *sock, struct buffer *buf, struct link_socket_actual *from)
 
static int link_socket_read (struct link_socket *sock, struct buffer *buf, struct link_socket_actual *from)
 
ssize_t link_socket_write_tcp (struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
 
static int link_socket_write_win32 (struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
 
static ssize_t link_socket_write_udp (struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
 
static ssize_t link_socket_write (struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
 
bool sockets_read_residual (const struct context *c)
 
static event_t socket_event_handle (const struct link_socket *sock)
 
event_t socket_listen_event_handle (struct link_socket *sock)
 
unsigned int socket_set (struct link_socket *sock, struct event_set *es, unsigned int rwflags, void *arg, unsigned int *persistent)
 
static void socket_set_listen_persistent (struct link_socket *sock, struct event_set *es, void *arg)
 
static void socket_reset_listen_persistent (struct link_socket *sock)
 
const char * socket_stat (const struct link_socket *sock, unsigned int rwflags, struct gc_arena *gc)
 

Macro Definition Documentation

◆ htonps

#define htonps (   x)    htons(x)

Definition at line 59 of file socket.h.

◆ IPV4_INVALID_ADDR

#define IPV4_INVALID_ADDR   0xffffffff

Definition at line 375 of file socket.h.

◆ LS_MODE_DEFAULT

#define LS_MODE_DEFAULT   0

Definition at line 197 of file socket.h.

◆ LS_MODE_TCP_ACCEPT_FROM

#define LS_MODE_TCP_ACCEPT_FROM   2

Definition at line 199 of file socket.h.

◆ LS_MODE_TCP_LISTEN

#define LS_MODE_TCP_LISTEN   1

Definition at line 198 of file socket.h.

◆ MSG_NOSIGNAL

#define MSG_NOSIGNAL   0

Definition at line 261 of file socket.h.

◆ ntohps

#define ntohps (   x)    ntohs(x)

Definition at line 62 of file socket.h.

◆ openvpn_close_socket

#define openvpn_close_socket (   s)    closesocket(s)

Definition at line 266 of file socket.h.

◆ OPENVPN_PORT

#define OPENVPN_PORT   "1194"

Definition at line 42 of file socket.h.

◆ RESOLV_RETRY_INFINITE

#define RESOLV_RETRY_INFINITE   1000000000

Definition at line 48 of file socket.h.

◆ SF_DCO_WIN

#define SF_DCO_WIN   (1 << 5)

Definition at line 214 of file socket.h.

◆ SF_GETADDRINFO_DGRAM

#define SF_GETADDRINFO_DGRAM   (1 << 4)

Definition at line 213 of file socket.h.

◆ SF_HOST_RANDOMIZE

#define SF_HOST_RANDOMIZE   (1 << 3)

Definition at line 212 of file socket.h.

◆ SF_PKTINFO_COPY_IIF

#define SF_PKTINFO_COPY_IIF   (1 << 7)

Definition at line 216 of file socket.h.

◆ SF_PORT_SHARE

#define SF_PORT_SHARE   (1 << 2)

Definition at line 211 of file socket.h.

◆ SF_PREPEND_SA

#define SF_PREPEND_SA   (1 << 6)

Definition at line 215 of file socket.h.

◆ SF_TCP_NODELAY

#define SF_TCP_NODELAY   (1 << 1)

Definition at line 210 of file socket.h.

◆ SF_USE_IP_PKTINFO

#define SF_USE_IP_PKTINFO   (1 << 0)

Definition at line 209 of file socket.h.

Typedef Documentation

◆ packet_size_type

typedef uint16_t packet_size_type

Definition at line 56 of file socket.h.

Function Documentation

◆ bad_address_length()

void bad_address_length ( int  actual,
int  expected 
)

Definition at line 2258 of file socket.c.

References buffer::len, M_FATAL, and msg.

Referenced by read_sockaddr_from_overlapped(), and socket_recv_queue().

◆ create_socket_tcp()

socket_descriptor_t create_socket_tcp ( struct addrinfo *  addrinfo)

Definition at line 564 of file socket.c.

References ASSERT, M_ERR, msg, set_cloexec(), and SOCKET_UNDEFINED.

Referenced by create_socket(), man_connect(), man_listen(), and phase2_tcp_client().

◆ do_preresolve()

void do_preresolve ( struct context c)

◆ link_socket_bad_incoming_addr()

void link_socket_bad_incoming_addr ( struct buffer buf,
const struct link_socket_info info,
const struct link_socket_actual from_addr 
)

◆ link_socket_bad_outgoing_addr()

void link_socket_bad_outgoing_addr ( void  )

Definition at line 1949 of file socket.c.

References D_READ_WRITE, and dmsg.

Referenced by link_socket_get_outgoing_addr().

◆ link_socket_close()

void link_socket_close ( struct link_socket sock)

◆ link_socket_connection_initiated()

void link_socket_connection_initiated ( struct link_socket_info info,
const struct link_socket_actual addr,
const char *  common_name,
struct env_set es 
)

◆ link_socket_connection_oriented()

static bool link_socket_connection_oriented ( const struct link_socket sock)
inlinestatic

◆ link_socket_current_remote()

in_addr_t link_socket_current_remote ( const struct link_socket_info info)

◆ link_socket_current_remote_ipv6()

const struct in6_addr * link_socket_current_remote_ipv6 ( const struct link_socket_info info)

◆ link_socket_get_outgoing_addr()

static void link_socket_get_outgoing_addr ( struct buffer buf,
const struct link_socket_info info,
struct link_socket_actual **  act 
)
inlinestatic

◆ link_socket_init_phase1()

void link_socket_init_phase1 ( struct context c,
int  sock_index,
int  mode 
)

Definition at line 1343 of file socket.c.

References context_2::accept_from, connection_entry::af, link_socket_info::af, local_list::array, ASSERT, options::bind_dev, link_socket::bind_dev, connection_entry::bind_ipv6_only, link_socket_info::bind_ipv6_only, connection_entry::bind_local, link_socket::bind_local, context::c1, context::c2, options::ce, CM_CHILD_TCP, CM_CHILD_UDP, context_1::dns_cache, link_socket::dns_cache, context_1::http_proxy, link_socket::http_proxy, link_socket::info, options::ipchange, link_socket_info::ipchange_command, context_1::link_socket_addrs, context_2::link_sockets, local_entry::local, link_socket::local_host, connection_entry::local_list, link_socket::local_port, LS_MODE_TCP_ACCEPT_FROM, link_socket_info::lsa, options::mark, link_socket::mark, context::mode, link_socket::mode, connection_entry::mtu_discover_type, link_socket::mtu_discover_type, context::options, http_proxy_info::options, context::plugins, link_socket_info::plugins, local_entry::port, http_proxy_options::port, socks_proxy_info::port, local_entry::proto, connection_entry::proto, link_socket_info::proto, PROTO_TCP_CLIENT, PROTO_TCP_SERVER, link_socket::proxy_dest_host, link_socket::proxy_dest_port, options::rcvbuf, socket_buffer_size::rcvbuf, connection_entry::remote, connection_entry::remote_float, link_socket_info::remote_float, link_socket::remote_host, connection_entry::remote_port, link_socket::remote_port, resolve_bind_local(), resolve_remote(), options::resolve_retry_seconds, link_socket::resolve_retry_seconds, link_socket::sd, http_proxy_options::server, socks_proxy_info::server, context_2::server_poll_interval, link_socket::server_poll_timeout, SF_PORT_SHARE, options::sndbuf, socket_buffer_size::sndbuf, link_socket::socket_buffer_sizes, options::sockflags, link_socket::sockflags, context_1::socks_proxy, and link_socket::socks_proxy.

Referenced by do_init_socket_phase1().

◆ link_socket_init_phase2()

void link_socket_init_phase2 ( struct context c,
struct link_socket sock 
)

◆ link_socket_new()

struct link_socket * link_socket_new ( void  )

◆ link_socket_read()

static int link_socket_read ( struct link_socket sock,
struct buffer buf,
struct link_socket_actual from 
)
inlinestatic

◆ link_socket_read_tcp()

int link_socket_read_tcp ( struct link_socket sock,
struct buffer buf 
)

◆ link_socket_read_udp_win32()

static int link_socket_read_udp_win32 ( struct link_socket sock,
struct buffer buf,
struct link_socket_actual from 
)
inlinestatic

◆ link_socket_set_outgoing_addr()

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 
)
inlinestatic

◆ link_socket_update_buffer_sizes()

void link_socket_update_buffer_sizes ( struct link_socket sock,
int  rcvbuf,
int  sndbuf 
)

◆ link_socket_update_flags()

bool link_socket_update_flags ( struct link_socket sock,
unsigned int  sockflags 
)

Definition at line 534 of file socket.c.

References link_socket::sd, socket_defined(), socket_set_flags(), and link_socket::sockflags.

Referenced by do_deferred_options().

◆ link_socket_verify_incoming_addr()

static bool link_socket_verify_incoming_addr ( struct buffer buf,
const struct link_socket_info info,
const struct link_socket_actual from_addr 
)
inlinestatic

◆ link_socket_write()

static ssize_t link_socket_write ( struct link_socket sock,
struct buffer buf,
struct link_socket_actual to 
)
inlinestatic

◆ link_socket_write_tcp()

ssize_t link_socket_write_tcp ( struct link_socket sock,
struct buffer buf,
struct link_socket_actual to 
)

◆ link_socket_write_udp()

static ssize_t link_socket_write_udp ( struct link_socket sock,
struct buffer buf,
struct link_socket_actual to 
)
inlinestatic

Definition at line 724 of file socket.h.

References link_socket_write_win32().

Referenced by link_socket_write().

◆ link_socket_write_win32()

static int link_socket_write_win32 ( struct link_socket sock,
struct buffer buf,
struct link_socket_actual to 
)
inlinestatic

◆ openvpn_bind()

static int openvpn_bind ( socket_descriptor_t  sockfd,
const struct sockaddr *  addr,
size_t  addrlen 
)
inlinestatic

Definition at line 332 of file socket.h.

References ASSERT.

Referenced by socket_bind().

◆ openvpn_connect()

int openvpn_connect ( socket_descriptor_t  sd,
const struct sockaddr *  remote,
int  connect_timeout,
volatile int *  signal_received 
)

◆ openvpn_select()

static int openvpn_select ( socket_descriptor_t  nfds,
fd_set *  readfds,
fd_set *  writefds,
fd_set *  exceptfds,
struct timeval *  timeout 
)
inlinestatic

Definition at line 317 of file socket.h.

Referenced by openvpn_connect(), proxy_recv_char(), and socket_listen_accept().

◆ openvpn_send()

static ssize_t openvpn_send ( socket_descriptor_t  sockfd,
const void *  buf,
size_t  len,
int  flags 
)
inlinestatic

Definition at line 325 of file socket.h.

References ASSERT.

Referenced by proxy_send().

◆ sd_close()

void sd_close ( socket_descriptor_t sd)

Definition at line 2983 of file socket.c.

References openvpn_close_socket, socket_defined(), and SOCKET_UNDEFINED.

Referenced by man_accept(), and man_connect().

◆ set_actual_address()

void set_actual_address ( struct link_socket_actual actual,
struct addrinfo *  ai 
)

◆ setenv_trusted()

void setenv_trusted ( struct env_set es,
const struct link_socket_info info 
)

◆ socket_bind()

void socket_bind ( socket_descriptor_t  sd,
struct addrinfo *  local,
int  af_family,
const char *  prefix,
bool  ipv6only 
)

◆ socket_connection_reset()

static bool socket_connection_reset ( const struct link_socket sock,
int  status 
)
inlinestatic

◆ socket_do_accept()

socket_descriptor_t socket_do_accept ( socket_descriptor_t  sd,
struct link_socket_actual act,
const bool  nowait 
)

◆ socket_event_handle()

static event_t socket_event_handle ( const struct link_socket sock)
inlinestatic

Definition at line 796 of file socket.h.

References link_socket::rw_handle, and link_socket::sd.

Referenced by multi_tcp_dereference_instance(), and socket_set().

◆ socket_is_dco_win()

static bool socket_is_dco_win ( const struct link_socket s)
inlinestatic

Returns true if we are on Windows and this link is running on DCO-WIN.

This helper is used to enable DCO-WIN specific logic that is not relevant to other platforms.

Definition at line 580 of file socket.h.

References SF_DCO_WIN, and link_socket::sockflags.

Referenced by link_socket_read(), link_socket_read_udp_win32(), link_socket_write(), link_socket_write_win32(), socket_get_last_error(), socket_recv_queue(), and socket_send_queue().

◆ socket_listen_event_handle()

event_t socket_listen_event_handle ( struct link_socket sock)

◆ socket_recv_queue()

int socket_recv_queue ( struct link_socket sock,
int  maxsize 
)

◆ socket_reset_listen_persistent()

static void socket_reset_listen_persistent ( struct link_socket sock)
inlinestatic

Definition at line 821 of file socket.h.

References link_socket::listen_handle, reset_net_event_win32(), and link_socket::sd.

Referenced by multi_io_process_io().

◆ socket_send_queue()

int socket_send_queue ( struct link_socket sock,
struct buffer buf,
const struct link_socket_actual to 
)

◆ socket_set()

unsigned int socket_set ( struct link_socket sock,
struct event_set es,
unsigned int  rwflags,
void *  arg,
unsigned int *  persistent 
)

◆ socket_set_buffers()

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.

Parameters
fdThe socket to modify
sbsnew sizes.
reduce_sizeapply the new size even if smaller than current one

Definition at line 462 of file socket.c.

References D_OSBUF, msg, socket_buffer_size::rcvbuf, socket_buffer_size::sndbuf, socket_get_rcvbuf(), socket_get_sndbuf(), socket_set_rcvbuf(), and socket_set_sndbuf().

Referenced by create_socket(), link_socket_update_buffer_sizes(), and open_tun_afunix().

◆ socket_set_listen_persistent()

static void socket_set_listen_persistent ( struct link_socket sock,
struct event_set es,
void *  arg 
)
inlinestatic

◆ socket_stat()

const char * socket_stat ( const struct link_socket sock,
unsigned int  rwflags,
struct gc_arena gc 
)

◆ sockethandle_finalize()

int sockethandle_finalize ( sockethandle_t  sh,
struct overlapped_io io,
struct buffer buf,
struct link_socket_actual from 
)

◆ SocketHandleGetLastError()

static int SocketHandleGetLastError ( sockethandle_t  sh)
inlinestatic

Definition at line 296 of file socket.h.

References sockethandle_t::is_handle.

Referenced by link_socket_write_win32(), and sockethandle_finalize().

◆ SocketHandleGetOverlappedResult()

static BOOL SocketHandleGetOverlappedResult ( sockethandle_t  sh,
struct overlapped_io io 
)
inlinestatic

◆ SocketHandleSetInvalError()

static void SocketHandleSetInvalError ( sockethandle_t  sh)
inlinestatic

Definition at line 308 of file socket.h.

References sockethandle_t::is_handle.

Referenced by sockethandle_finalize().

◆ SocketHandleSetLastError()

static void SocketHandleSetLastError ( sockethandle_t  sh,
DWORD  err 
)
inlinestatic

Definition at line 302 of file socket.h.

References sockethandle_t::is_handle.

Referenced by link_socket_write_win32(), and sockethandle_finalize().

◆ sockets_read_residual()

bool sockets_read_residual ( const struct context c)

◆ stream_buf_read_setup()

static bool stream_buf_read_setup ( struct link_socket sock)
inlinestatic

◆ stream_buf_read_setup_dowork()

bool stream_buf_read_setup_dowork ( struct stream_buf sb)

Will try to check if the buffers in stream form a full packet.

Will return true if further reads are required and false otherwise. (full packet is ready)

With UDP we always return true as there is no reassembly.

Parameters
sbthe stream buffer that should be worked on
Returns
true if more reads are required.

Definition at line 2126 of file socket.c.

References ASSERT, buf_copy(), buf_init, D_STREAM_DEBUG, dmsg, buffer::len, and stream_buf_added().

Referenced by stream_buf_read_setup().