Go to the documentation of this file.
42 #define OPENVPN_PORT "1194"
48 #define RESOLV_RETRY_INFINITE 1000000000
59 #define htonps(x) htons(x)
62 #define ntohps(x) ntohs(x)
70 struct sockaddr_in
in4;
71 struct sockaddr_in6
in6;
93 #if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST)
94 struct in_pktinfo in4;
95 #elif defined(IP_RECVDSTADDR)
98 struct in6_pktinfo in6;
144 #define PS_DISABLED 0
147 int port_share_state;
209 #define LS_MODE_DEFAULT 0
210 #define LS_MODE_TCP_LISTEN 1
211 #define LS_MODE_TCP_ACCEPT_FROM 2
221 #define SF_USE_IP_PKTINFO (1<<0)
222 #define SF_TCP_NODELAY (1<<1)
223 #define SF_PORT_SHARE (1<<2)
224 #define SF_HOST_RANDOMIZE (1<<3)
225 #define SF_GETADDRINFO_DGRAM (1<<4)
226 #define SF_DCO_WIN (1<<5)
227 #define SF_PREPEND_SA (1<<6)
252 #if PASSTOS_CAPABILITY
254 #if defined(TARGET_LINUX)
272 #define MSG_NOSIGNAL 0
277 #define openvpn_close_socket(s) closesocket(s)
310 return sh.
is_handle ? (int)GetLastError() : WSAGetLastError();
316 sh.
is_handle ? SetLastError(err) : WSASetLastError(err);
322 sh.
is_handle ? SetLastError(ERROR_INVALID_FUNCTION) : WSASetLastError(WSAEINVAL);
327 #define openvpn_close_socket(s) close(s)
334 struct addrinfo *local,
340 const struct sockaddr *remote,
342 volatile int *signal_received);
363 #define PS_SHOW_PORT_IF_DEFINED (1<<0)
364 #define PS_SHOW_PORT (1<<1)
365 #define PS_SHOW_PKTINFO (1<<2)
366 #define PS_DONT_SHOW_ADDR (1<<3)
367 #define PS_DONT_SHOW_FAMILY (1<<4)
370 const char *separator,
371 const unsigned int flags,
393 const char *separator,
394 const unsigned int flags,
401 #define IA_EMPTY_IF_UNDEF (1<<0)
402 #define IA_NET_ORDER (1<<1)
409 struct in6_addr
add_in6_addr( struct in6_addr base, uint32_t add );
411 #define SA_IP_PORT (1<<0)
412 #define SA_SET_IF_NONZERO (1<<1)
414 const char *name_prefix,
416 const unsigned int flags);
419 const char *name_prefix,
421 const unsigned int flags);
424 const char *name_prefix,
425 const struct in6_addr *addr,
426 const unsigned int flags);
429 const char *name_prefix,
431 const unsigned int flags);
438 #define IPV4_INVALID_ADDR 0xffffffff
446 const char *common_name,
454 struct addrinfo *ai);
469 #define OIA_HOSTNAME 0
489 #if UNIX_SOCK_SUPPORT
494 struct sockaddr_un *local,
498 struct sockaddr_un *remote);
501 struct sockaddr_un *remote);
503 void sockaddr_unix_init(
struct sockaddr_un *local,
const char *path);
505 const char *sockaddr_unix_name(
const struct sockaddr_un *local,
const char *
null);
507 void socket_delete_unix(
const struct sockaddr_un *local);
517 #define GETADDR_RESOLVE (1<<0)
518 #define GETADDR_FATAL (1<<1)
519 #define GETADDR_HOST_ORDER (1<<2)
520 #define GETADDR_MENTION_RESOLVE_RETRY (1<<3)
521 #define GETADDR_FATAL_ON_SIGNAL (1<<4)
522 #define GETADDR_WARN_ON_SIGNAL (1<<5)
523 #define GETADDR_MSG_VIRT_OUT (1<<6)
524 #define GETADDR_TRY_ONCE (1<<7)
525 #define GETADDR_UPDATE_MANAGEMENT_STATE (1<<8)
526 #define GETADDR_RANDOMIZE (1<<9)
527 #define GETADDR_PASSIVE (1<<10)
528 #define GETADDR_DATAGRAM (1<<11)
530 #define GETADDR_CACHE_MASK (GETADDR_DATAGRAM|GETADDR_PASSIVE)
538 in_addr_t
getaddr(
unsigned int flags,
539 const char *hostname,
540 int resolve_retry_seconds,
547 bool get_ipv6_addr(
const char *hostname,
struct in6_addr *network,
548 unsigned int *netbits,
int msglevel);
551 const char *hostname,
552 const char *servname,
553 int resolve_retry_seconds,
556 struct addrinfo **res);
632 overhead += (proto ==
PROTO_UDP) ? 8 : 20;
633 overhead += (af == AF_INET) ? 20 : 40;
667 switch (addr->
addr.
sa.sa_family)
669 case AF_INET:
return addr->
addr.
in4.sin_addr.s_addr != 0;
671 case AF_INET6:
return !IN6_IS_ADDR_UNSPECIFIED(&addr->
addr.
in6.sin6_addr);
684 switch (addr->sa_family)
687 return ((
const struct sockaddr_in *)addr)->sin_addr.s_addr == htonl(INADDR_LOOPBACK);
690 return IN6_IS_ADDR_LOOPBACK(&((
const struct sockaddr_in6 *)addr)->sin6_addr);
701 #if ENABLE_IP_PKTINFO
708 #if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST)
709 case AF_INET:
return lsa->pi.in4.ipi_spec_dst.s_addr != 0;
711 #elif defined(IP_RECVDSTADDR)
712 case AF_INET:
return lsa->pi.in4.s_addr != 0;
715 case AF_INET6:
return !IN6_IS_ADDR_UNSPECIFIED(&lsa->pi.in6.ipi6_addr);
734 switch (a1->
addr.
sa.sa_family)
737 return a1->
addr.
in4.sin_addr.s_addr == a2->
addr.
in4.sin_addr.s_addr;
749 const struct addrinfo *curele;
750 for (curele = addrlist; curele; curele = curele->ai_next)
752 switch (a1->
addr.
sa.sa_family)
755 if (a1->
addr.
in4.sin_addr.s_addr == ((
struct sockaddr_in *)curele->ai_addr)->sin_addr.s_addr)
778 const struct addrinfo *curele;
779 for (curele = a2; curele; curele = curele->ai_next)
781 switch (a1->
addr.
sa.sa_family)
784 if (curele->ai_family == AF_INET
785 && a1->
addr.
in4.sin_addr.s_addr == ((
struct sockaddr_in *)curele->ai_addr)->sin_addr.s_addr
786 && a1->
addr.
in4.sin_port == ((
struct sockaddr_in *)curele->ai_addr)->sin_port)
793 if (curele->ai_family == AF_INET6
795 && a1->
addr.
in6.sin6_port == ((
struct sockaddr_in6 *) curele->ai_addr)->sin6_port)
813 switch (a1->
addr.
sa.sa_family)
816 return a1->
addr.
in4.sin_addr.s_addr == a2->
addr.
in4.sin_addr.s_addr
840 struct addrinfo *addr_list,
851 switch (addr->
addr.
sa.sa_family)
854 addr->
addr.
in4.sin_addr.s_addr = 0;
858 memset(&addr->
addr.
in6.sin6_addr, 0,
sizeof(
struct in6_addr));
868 case AF_INET:
return sizeof(
struct sockaddr_in);
870 case AF_INET6:
return sizeof(
struct sockaddr_in6);
875 msg(
M_ERR,
"Bad address family: %d\n", af);
891 socket_foreign_protocol_detected(
const struct link_socket *sock)
894 && sock->
stream_buf.port_share_state == PS_FOREIGN;
897 static inline const struct buffer *
898 socket_foreign_protocol_head(
const struct link_socket *sock)
904 socket_foreign_protocol_sd(
const struct link_socket *sock)
924 return err == WSAECONNRESET || err == WSAECONNABORTED
925 || err == ERROR_CONNECTION_ABORTED;
927 return err == ECONNRESET;
986 const char *common_name,
1058 int link_socket_read_udp_posix(
struct link_socket *sock,
1079 res = link_socket_read_udp_posix(sock, buf, from);
1150 ssize_t link_socket_write_udp_posix_sendmsg(
struct link_socket *sock,
1155 static inline ssize_t
1156 link_socket_write_udp_posix(
struct link_socket *sock,
1160 #if ENABLE_IP_PKTINFO
1164 return link_socket_write_udp_posix_sendmsg(sock, buf, to);
1168 return sendto(sock->
sd,
BPTR(buf),
BLEN(buf), 0,
1173 static inline ssize_t
1174 link_socket_write_tcp_posix(
struct link_socket *sock,
1182 static inline ssize_t
1190 return link_socket_write_udp_posix(sock, buf, to);
1195 static inline ssize_t
1216 #if PASSTOS_CAPABILITY
1227 sock->ptos = iph->
tos;
1228 sock->ptos_defined =
true;
1239 if (sock && sock->ptos_defined)
1241 setsockopt(sock->
sd, IPPROTO_IP, IP_TOS, (
const void *)&sock->ptos,
sizeof(sock->ptos));
1273 unsigned int rwflags,
1275 unsigned int *persistent);
static bool overlapped_io_active(struct overlapped_io *o)
void setenv_link_socket_actual(struct env_set *es, const char *name_prefix, const struct link_socket_actual *act, const unsigned int flags)
static int link_socket_read(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *from)
void bad_address_length(int actual, int expected)
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.
#define IN6_ARE_ADDR_EQUAL(a, b)
void link_socket_init_phase1(struct context *c, int sock_index, int mode)
struct overlapped_io reads
const char * print_link_socket_actual_ex(const struct link_socket_actual *act, const char *separator, const unsigned int flags, struct gc_arena *gc)
static const char * print_sockaddr(const struct sockaddr *addr, struct gc_arena *gc)
const char * print_link_socket_actual(const struct link_socket_actual *act, struct gc_arena *gc)
void link_socket_close(struct link_socket *sock)
int len
Length in bytes of the actual content within the allocated memory.
struct event_arg ev_arg
this struct will store a pointer to either mi or link_socket, depending on the event type,...
const char * socket_stat(const struct link_socket *sock, unsigned int rwflags, struct gc_arena *gc)
event_t socket_listen_event_handle(struct link_socket *sock)
ssize_t link_socket_write_tcp(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
Contains all state information for one tunnel.
static bool addrlist_port_match(const struct openvpn_sockaddr *a1, const struct addrinfo *a2)
bool sockets_read_residual(const struct context *c)
struct link_socket_addr * lsa
struct addrinfo * bind_local
static bool link_socket_proto_connection_oriented(int proto)
void setenv_in6_addr(struct env_set *es, const char *name_prefix, const struct in6_addr *addr, const unsigned int flags)
socket_descriptor_t create_socket_tcp(struct addrinfo *)
const char * print_sockaddr_ex(const struct sockaddr *addr, const char *separator, const unsigned int flags, struct gc_arena *gc)
struct addrinfo * current_remote
void link_socket_init_phase2(struct context *c, struct link_socket *sock)
static bool link_socket_verify_incoming_addr(struct buffer *buf, const struct link_socket_info *info, const struct link_socket_actual *from_addr)
void sd_close(socket_descriptor_t *sd)
static bool proto_is_dgram(int proto)
Return if the protocol is datagram (UDP)
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.
socket_descriptor_t ctrl_sd
const char * proto2ascii(int proto, sa_family_t af, bool display_form)
bool link_socket_update_flags(struct link_socket *sock, unsigned int sockflags)
static bool link_socket_connection_oriented(const struct link_socket *sock)
int resolve_retry_seconds
static void addr_zero_host(struct openvpn_sockaddr *addr)
struct rw_handle listen_handle
int link_socket_read_tcp(struct link_socket *sock, struct buffer *buf)
unsigned short sa_family_t
unsigned int rwflags_debug
const char * proxy_dest_host
static int link_socket_read_udp_win32(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *from)
static bool addr_match(const struct openvpn_sockaddr *a1, const struct openvpn_sockaddr *a2)
static int link_socket_write_win32(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
static void event_ctl(struct event_set *es, event_t event, unsigned int rwflags, void *arg)
unsigned int socket_set(struct link_socket *sock, struct event_set *es, unsigned int rwflags, void *arg, unsigned int *persistent)
static bool link_socket_actual_defined(const struct link_socket_actual *act)
bool ip_or_dns_addr_safe(const char *addr, const bool allow_fqdn)
int openvpn_getaddrinfo(unsigned int flags, const char *hostname, const char *servname, int resolve_retry_seconds, struct signal_info *sig_info, int ai_family, struct addrinfo **res)
static bool buf_write_prepend(struct buffer *dest, const void *src, int size)
sa_family_t ascii2af(const char *proto_name)
static event_t socket_event_handle(const struct link_socket *sock)
static bool addrlist_match(const struct openvpn_sockaddr *a1, const struct addrinfo *addrlist)
bool stream_buf_read_setup_dowork(struct link_socket *sock)
void set_actual_address(struct link_socket_actual *actual, struct addrinfo *ai)
bool listen_persistent_queued
const char * print_in_addr_t(in_addr_t addr, unsigned int flags, struct gc_arena *gc)
static int SocketHandleGetLastError(sockethandle_t sh)
in_addr_t getaddr(unsigned int flags, const char *hostname, int resolve_retry_seconds, bool *succeeded, struct signal_info *sig_info)
Translate an IPv4 addr or hostname from string form to in_addr_t.
int sockethandle_finalize(sockethandle_t sh, struct overlapped_io *io, struct buffer *buf, struct link_socket_actual *from)
void link_socket_bad_incoming_addr(struct buffer *buf, const struct link_socket_info *info, const struct link_socket_actual *from_addr)
struct event_timeout * server_poll_timeout
bool residual_fully_formed
struct cached_dns_entry * next
Wrapper structure for dynamically allocated memory.
static bool proto_is_udp(int proto)
Returns if the protocol being used is UDP.
static bool addr_defined_ipi(const struct link_socket_actual *lsa)
const char * print_in6_addr(struct in6_addr addr6, unsigned int flags, struct gc_arena *gc)
struct link_socket_info info
bool ipv6_addr_safe(const char *ipv6_text_addr)
const char * proto2ascii_all(struct gc_arena *gc)
static bool addr_port_match(const struct openvpn_sockaddr *a1, const struct openvpn_sockaddr *a2)
struct link_socket_actual actual
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)
struct socket_buffer_size socket_buffer_sizes
static bool proto_is_net(int proto)
int socket_recv_queue(struct link_socket *sock, int maxsize)
void link_socket_update_buffer_sizes(struct link_socket *sock, int rcvbuf, int sndbuf)
int socket_send_queue(struct link_socket *sock, struct buffer *buf, const struct link_socket_actual *to)
struct http_proxy_info * http_proxy
Garbage collection arena used to keep track of dynamically allocated memory.
struct overlapped_io writes
const char * proto_remote(int proto, bool remote)
static void SocketHandleSetInvalError(sockethandle_t sh)
struct link_socket * link_socket_new(void)
bool ip_addr_dotted_quad_safe(const char *dotted_quad)
bool get_ipv6_addr(const char *hostname, struct in6_addr *network, unsigned int *netbits, int msglevel)
Translate an IPv6 addr or hostname from string form to in6_addr.
void setenv_sockaddr(struct env_set *es, const char *name_prefix, const struct openvpn_sockaddr *addr, const unsigned int flags)
bool mac_addr_safe(const char *mac_addr)
void do_preresolve(struct context *c)
long reset_net_event_win32(struct rw_handle *event, socket_descriptor_t sd)
const char * proxy_dest_port
struct link_socket_actual socks_relay
SOCKET socket_descriptor_t
int openvpn_connect(socket_descriptor_t sd, const struct sockaddr *remote, int connect_timeout, volatile int *signal_received)
const char * addr_family_name(int af)
static void socket_set_listen_persistent(struct link_socket *sock, struct event_set *es, void *arg)
bool connection_established
static bool addr_defined(const struct openvpn_sockaddr *addr)
struct rw_handle rw_handle
uint16_t packet_size_type
int openvpn_inet_aton(const char *dotted_quad, struct in_addr *addr)
static void SocketHandleSetLastError(sockethandle_t sh, DWORD err)
static bool stream_buf_read_setup(struct link_socket *sock)
static SERVICE_STATUS status
struct in6_addr add_in6_addr(struct in6_addr base, uint32_t add)
static bool socket_connection_reset(const struct link_socket *sock, int status)
static bool addr_match_proto(const struct openvpn_sockaddr *a1, const struct openvpn_sockaddr *a2, const int proto)
const char * print_in_port_t(in_port_t port, struct gc_arena *gc)
static bool addrlist_match_proto(const struct openvpn_sockaddr *a1, 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)
in_addr_t link_socket_current_remote(const struct link_socket_info *info)
struct buffer stream_buf_data
void setenv_in_addr_t(struct env_set *es, const char *name_prefix, in_addr_t addr, const unsigned int flags)
static BOOL SocketHandleGetOverlappedResult(sockethandle_t sh, struct overlapped_io *io)
const struct in6_addr * link_socket_current_remote_ipv6(const struct link_socket_info *info)
static ssize_t link_socket_write_udp(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
void setenv_trusted(struct env_set *es, const struct link_socket_info *info)
struct stream_buf stream_buf
static int datagram_overhead(sa_family_t af, int proto)
static ssize_t link_socket_write(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
void link_socket_bad_outgoing_addr(void)
struct openvpn_sockaddr dest
#define SF_USE_IP_PKTINFO
const char * ipchange_command
static const char * print_openvpn_sockaddr(const struct openvpn_sockaddr *addr, struct gc_arena *gc)
static bool addr_local(const struct sockaddr *addr)
static int af_addr_size(sa_family_t af)
const struct plugin_list * plugins
int ascii2proto(const char *proto_name)
static void link_socket_get_outgoing_addr(struct buffer *buf, const struct link_socket_info *info, struct link_socket_actual **act)
socket_descriptor_t socket_do_accept(socket_descriptor_t sd, struct link_socket_actual *act, const bool nowait)
struct socks_proxy_info * socks_proxy
void link_socket_connection_initiated(struct link_socket_info *info, const struct link_socket_actual *addr, const char *common_name, struct env_set *es)
struct cached_dns_entry * dns_cache
struct addrinfo * remote_list
union openvpn_sockaddr::@20 addr
static void socket_reset_listen_persistent(struct link_socket *sock)
void socket_bind(socket_descriptor_t sd, struct addrinfo *local, int af_family, const char *prefix, bool ipv6only)
static bool link_socket_actual_match(const struct link_socket_actual *a1, const struct link_socket_actual *a2)