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;
192 #define LS_MODE_DEFAULT 0
193 #define LS_MODE_TCP_LISTEN 1
194 #define LS_MODE_TCP_ACCEPT_FROM 2
204 #define SF_USE_IP_PKTINFO (1<<0)
205 #define SF_TCP_NODELAY (1<<1)
206 #define SF_PORT_SHARE (1<<2)
207 #define SF_HOST_RANDOMIZE (1<<3)
208 #define SF_GETADDRINFO_DGRAM (1<<4)
209 #define SF_DCO_WIN (1<<5)
234 #if PASSTOS_CAPABILITY
236 #if defined(TARGET_LINUX)
254 #define MSG_NOSIGNAL 0
259 #define openvpn_close_socket(s) closesocket(s)
291 return sh.
is_handle ? (int)GetLastError() : WSAGetLastError();
297 sh.
is_handle ? SetLastError(err) : WSASetLastError(err);
303 sh.
is_handle ? SetLastError(ERROR_INVALID_FUNCTION) : WSASetLastError(WSAEINVAL);
308 #define openvpn_close_socket(s) close(s)
315 struct addrinfo *local,
321 const struct sockaddr *remote,
323 volatile int *signal_received);
340 #define PS_SHOW_PORT_IF_DEFINED (1<<0)
341 #define PS_SHOW_PORT (1<<1)
342 #define PS_SHOW_PKTINFO (1<<2)
343 #define PS_DONT_SHOW_ADDR (1<<3)
344 #define PS_DONT_SHOW_FAMILY (1<<4)
347 const char *separator,
348 const unsigned int flags,
354 const char *separator,
355 const unsigned int flags,
380 const char *separator,
381 const unsigned int flags,
388 #define IA_EMPTY_IF_UNDEF (1<<0)
389 #define IA_NET_ORDER (1<<1)
396 struct in6_addr
add_in6_addr( struct in6_addr base, uint32_t add );
398 #define SA_IP_PORT (1<<0)
399 #define SA_SET_IF_NONZERO (1<<1)
401 const char *name_prefix,
403 const unsigned int flags);
406 const char *name_prefix,
408 const unsigned int flags);
411 const char *name_prefix,
412 const struct in6_addr *addr,
413 const unsigned int flags);
416 const char *name_prefix,
418 const unsigned int flags);
425 #define IPV4_INVALID_ADDR 0xffffffff
433 const char *common_name,
441 struct addrinfo *ai);
456 #define OIA_HOSTNAME 0
476 #if UNIX_SOCK_SUPPORT
481 struct sockaddr_un *local,
485 struct sockaddr_un *remote);
488 struct sockaddr_un *remote);
490 void sockaddr_unix_init(
struct sockaddr_un *local,
const char *path);
492 const char *sockaddr_unix_name(
const struct sockaddr_un *local,
const char *
null);
494 void socket_delete_unix(
const struct sockaddr_un *local);
504 #define GETADDR_RESOLVE (1<<0)
505 #define GETADDR_FATAL (1<<1)
506 #define GETADDR_HOST_ORDER (1<<2)
507 #define GETADDR_MENTION_RESOLVE_RETRY (1<<3)
508 #define GETADDR_FATAL_ON_SIGNAL (1<<4)
509 #define GETADDR_WARN_ON_SIGNAL (1<<5)
510 #define GETADDR_MSG_VIRT_OUT (1<<6)
511 #define GETADDR_TRY_ONCE (1<<7)
512 #define GETADDR_UPDATE_MANAGEMENT_STATE (1<<8)
513 #define GETADDR_RANDOMIZE (1<<9)
514 #define GETADDR_PASSIVE (1<<10)
515 #define GETADDR_DATAGRAM (1<<11)
517 #define GETADDR_CACHE_MASK (GETADDR_DATAGRAM|GETADDR_PASSIVE)
525 in_addr_t
getaddr(
unsigned int flags,
526 const char *hostname,
527 int resolve_retry_seconds,
534 bool get_ipv6_addr(
const char *hostname,
struct in6_addr *network,
535 unsigned int *netbits,
int msglevel);
538 const char *hostname,
539 const char *servname,
540 int resolve_retry_seconds,
543 struct addrinfo **res);
619 overhead += (proto ==
PROTO_UDP) ? 8 : 20;
620 overhead += (af == AF_INET) ? 20 : 40;
654 switch (addr->
addr.
sa.sa_family)
656 case AF_INET:
return addr->
addr.
in4.sin_addr.s_addr != 0;
658 case AF_INET6:
return !IN6_IS_ADDR_UNSPECIFIED(&addr->
addr.
in6.sin6_addr);
671 switch (addr->sa_family)
674 return ((
const struct sockaddr_in *)addr)->sin_addr.s_addr == htonl(INADDR_LOOPBACK);
677 return IN6_IS_ADDR_LOOPBACK(&((
const struct sockaddr_in6 *)addr)->sin6_addr);
688 #if ENABLE_IP_PKTINFO
695 #if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST)
696 case AF_INET:
return lsa->pi.in4.ipi_spec_dst.s_addr != 0;
698 #elif defined(IP_RECVDSTADDR)
699 case AF_INET:
return lsa->pi.in4.s_addr != 0;
702 case AF_INET6:
return !IN6_IS_ADDR_UNSPECIFIED(&lsa->pi.in6.ipi6_addr);
721 switch (a1->
addr.
sa.sa_family)
724 return a1->
addr.
in4.sin_addr.s_addr == a2->
addr.
in4.sin_addr.s_addr;
736 const struct addrinfo *curele;
737 for (curele = addrlist; curele; curele = curele->ai_next)
739 switch (a1->
addr.
sa.sa_family)
742 if (a1->
addr.
in4.sin_addr.s_addr == ((
struct sockaddr_in *)curele->ai_addr)->sin_addr.s_addr)
762 static inline in_addr_t
770 if (addr->
addr.
sa.sa_family != AF_INET)
774 return ntohl(addr->
addr.
in4.sin_addr.s_addr);
781 const struct addrinfo *curele;
782 for (curele = a2; curele; curele = curele->ai_next)
784 switch (a1->
addr.
sa.sa_family)
787 if (curele->ai_family == AF_INET
788 && a1->
addr.
in4.sin_addr.s_addr == ((
struct sockaddr_in *)curele->ai_addr)->sin_addr.s_addr
789 && a1->
addr.
in4.sin_port == ((
struct sockaddr_in *)curele->ai_addr)->sin_port)
796 if (curele->ai_family == AF_INET6
798 && a1->
addr.
in6.sin6_port == ((
struct sockaddr_in6 *) curele->ai_addr)->sin6_port)
816 switch (a1->
addr.
sa.sa_family)
819 return a1->
addr.
in4.sin_addr.s_addr == a2->
addr.
in4.sin_addr.s_addr
843 struct addrinfo *addr_list,
854 switch (addr->
addr.
sa.sa_family)
857 addr->
addr.
in4.sin_addr.s_addr = 0;
861 memset(&addr->
addr.
in6.sin6_addr, 0,
sizeof(
struct in6_addr));
871 case AF_INET:
return sizeof(
struct sockaddr_in);
873 case AF_INET6:
return sizeof(
struct sockaddr_in6);
878 msg(
M_ERR,
"Bad address family: %d\n", af);
894 socket_foreign_protocol_detected(
const struct link_socket *sock)
897 && sock->
stream_buf.port_share_state == PS_FOREIGN;
900 static inline const struct buffer *
901 socket_foreign_protocol_head(
const struct link_socket *sock)
907 socket_foreign_protocol_sd(
const struct link_socket *sock)
927 return err == WSAECONNRESET || err == WSAECONNABORTED
928 || err == ERROR_CONNECTION_ABORTED;
930 return err == ECONNRESET;
989 const char *common_name,
1060 int link_socket_read_udp_posix(
struct link_socket *sock,
1081 res = link_socket_read_udp_posix(sock, buf, from);
1138 size_t link_socket_write_udp_posix_sendmsg(
struct link_socket *sock,
1143 static inline size_t
1144 link_socket_write_udp_posix(
struct link_socket *sock,
1148 #if ENABLE_IP_PKTINFO
1152 return link_socket_write_udp_posix_sendmsg(sock, buf, to);
1156 return sendto(sock->
sd,
BPTR(buf),
BLEN(buf), 0,
1161 static inline size_t
1162 link_socket_write_tcp_posix(
struct link_socket *sock,
1171 static inline size_t
1179 return link_socket_write_udp_posix(sock, buf, to);
1205 #if PASSTOS_CAPABILITY
1216 ls->ptos = iph->
tos;
1217 ls->ptos_defined =
true;
1228 if (ls && ls->ptos_defined)
1230 setsockopt(ls->
sd, IPPROTO_IP, IP_TOS, (
const void *)&ls->ptos,
sizeof(ls->ptos));
1261 unsigned int rwflags,
1263 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)
union openvpn_sockaddr::@14 addr
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)
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.
bool link_socket_update_flags(struct link_socket *ls, unsigned int sockflags)
Contains all state information for one tunnel.
static bool addrlist_port_match(const struct openvpn_sockaddr *a1, const struct addrinfo *a2)
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
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)
unsigned int socket_set(struct link_socket *s, struct event_set *es, unsigned int rwflags, void *arg, unsigned int *persistent)
socket_descriptor_t ctrl_sd
const char * proto2ascii(int proto, sa_family_t af, bool display_form)
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)
static in_addr_t addr_host(const struct openvpn_sockaddr *addr)
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)
static event_t socket_event_handle(const struct link_socket *s)
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)
void link_socket_update_buffer_sizes(struct link_socket *ls, int rcvbuf, int sndbuf)
static size_t link_socket_write_udp(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
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)
int link_socket_write_tcp(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
sa_family_t ascii2af(const char *proto_name)
static bool addrlist_match(const struct openvpn_sockaddr *a1, const struct addrinfo *addrlist)
void link_socket_init_phase2(struct context *c)
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
event_t socket_listen_event_handle(struct link_socket *s)
static int link_socket_write(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
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)
void link_socket_init_phase1(struct context *c, int mode)
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)
static void socket_reset_listen_persistent(struct link_socket *s)
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)
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)
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)
static void socket_set_listen_persistent(struct link_socket *s, struct event_set *es, void *arg)
static bool socket_read_residual(const struct link_socket *s)
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)
static const char * print_openvpn_sockaddr_ex(const struct openvpn_sockaddr *addr, const char *separator, const unsigned int flags, struct gc_arena *gc)
const struct in6_addr * link_socket_current_remote_ipv6(const struct link_socket_info *info)
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)
void link_socket_bad_outgoing_addr(void)
struct openvpn_sockaddr dest
#define SF_USE_IP_PKTINFO
const char * ipchange_command
const char * socket_stat(const struct link_socket *s, unsigned int rwflags, struct gc_arena *gc)
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
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)