62 socklen_t len =
sizeof(val);
64 if (getsockopt(sd, SOL_SOCKET, SO_SNDBUF, (
void *)&val, &len) == 0 && len ==
sizeof(val))
74 if (setsockopt(sd, SOL_SOCKET, SO_SNDBUF, (
void *)&size,
sizeof(size)) != 0)
76 msg(
M_WARN,
"NOTE: setsockopt SO_SNDBUF=%d failed", size);
84 socklen_t len =
sizeof(val);
86 if (getsockopt(sd, SOL_SOCKET, SO_RCVBUF, (
void *)&val, &len) == 0 && len ==
sizeof(val))
96 if (setsockopt(sd, SOL_SOCKET, SO_RCVBUF, (
void *)&size,
sizeof(size)) != 0)
98 msg(
M_WARN,
"NOTE: setsockopt SO_RCVBUF=%d failed", size);
110 if (sbs->
sndbuf && (reduce_size || sndbuf_old < sbs->sndbuf))
115 if (sbs->
rcvbuf && (reduce_size || rcvbuf_old < sbs->rcvbuf))
132#if defined(_WIN32) || (defined(IPPROTO_TCP) && defined(TCP_NODELAY))
133 if (setsockopt(sd, IPPROTO_TCP, TCP_NODELAY, (
void *)&state,
sizeof(state)) != 0)
135 msg(
M_WARN,
"NOTE: setsockopt TCP_NODELAY=%d failed", state);
140 dmsg(
D_OSBUF,
"Socket flags: TCP_NODELAY=%d succeeded", state);
144 msg(
M_WARN,
"NOTE: setsockopt TCP_NODELAY=%d failed (No kernel support)", state);
152#if defined(TARGET_LINUX)
153 if (mark && setsockopt(sd, SOL_SOCKET, SO_MARK, (
void *)&mark,
sizeof(mark)) != 0)
155 msg(
M_WARN,
"NOTE: setsockopt SO_MARK=%d failed", mark);
182 ASSERT(addrinfo->ai_socktype == SOCK_STREAM);
184 if ((sd = socket(addrinfo->ai_family, addrinfo->ai_socktype, addrinfo->ai_protocol))
187 msg(
M_ERR,
"Cannot create TCP socket");
194 if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (
void *)&on,
sizeof(on)) < 0)
196 msg(
M_ERR,
"TCP: Cannot setsockopt SO_REUSEADDR on TCP socket");
214 ASSERT(addrinfo->ai_socktype == SOCK_DGRAM);
216 if ((sd = socket(addrinfo->ai_family, addrinfo->ai_socktype, addrinfo->ai_protocol))
226 if (addrinfo->ai_family == AF_INET)
228#if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST)
229 if (setsockopt(sd,
SOL_IP, IP_PKTINFO, (
void *)&pad,
sizeof(pad)) < 0)
231 msg(
M_ERR,
"UDP: failed setsockopt for IP_PKTINFO");
233#elif defined(IP_RECVDSTADDR)
234 if (setsockopt(sd, IPPROTO_IP, IP_RECVDSTADDR, (
void *)&pad,
sizeof(pad)) < 0)
236 msg(
M_ERR,
"UDP: failed setsockopt for IP_RECVDSTADDR");
239#error ENABLE_IP_PKTINFO is set without IP_PKTINFO xor IP_RECVDSTADDR (fix syshead.h)
242 else if (addrinfo->ai_family == AF_INET6)
244#ifndef IPV6_RECVPKTINFO
245 if (setsockopt(sd, IPPROTO_IPV6, IPV6_PKTINFO, (
void *)&pad,
sizeof(pad)) < 0)
247 if (setsockopt(sd, IPPROTO_IPV6, IPV6_RECVPKTINFO, (
void *)&pad,
sizeof(pad)) < 0)
250 msg(
M_ERR,
"UDP: failed setsockopt for IPV6_RECVPKTINFO");
284 const char *bind_dev)
292#if defined(TARGET_LINUX)
295 msg(
M_INFO,
"Using bind-dev %s", bind_dev);
297 if (setsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, bind_dev, (socklen_t)(strlen(bind_dev) + 1))
300 msg(
M_WARN |
M_ERRNO,
"WARN: setsockopt SO_BINDTODEVICE=%s failed", bind_dev);
310 int mark,
const char *bind_dev,
struct addrinfo *bind_addr,
311 bool bind_ipv6_only,
bool optional)
313 struct addrinfo ai = { .ai_family = af, .ai_socktype = SOCK_DGRAM, .ai_protocol = IPPROTO_UDP };
324 socket_bind(sd, bind_addr, af,
"TCP/UDP", bind_ipv6_only);
336 if (addr->ai_protocol == IPPROTO_UDP || addr->ai_socktype == SOCK_DGRAM)
340 struct addrinfo *bind_addr =
354 struct addrinfo addrinfo_tmp = *addr;
355 addrinfo_tmp.ai_socktype = SOCK_STREAM;
356 addrinfo_tmp.ai_protocol = IPPROTO_TCP;
361 else if (addr->ai_protocol == IPPROTO_TCP || addr->ai_socktype == SOCK_STREAM)
375protect_fd_nonlocal(
int fd,
const struct sockaddr *addr)
379 msg(
M_FATAL,
"Required management interface not available.");
385 if (addr_local(addr))
393 management_android_control(
management,
"PROTECTFD", __func__);
402 bool do_set_nonblock)
408 msg(
M_INFO,
"Listening for incoming TCP connection on %s",
433 socklen_t remote_len =
sizeof(act->
dest.
addr);
440 new_sd = getpeername(sd, &act->
dest.
addr.
sa, &remote_len);
453 new_sd = accept(sd, &act->
dest.
addr.
sa, &remote_len);
472 else if (remote_len_af && remote_len != remote_len_af)
475 "TCP: Received strange incoming connection with unknown address length=%u",
476 (
unsigned int)remote_len);
499 const struct addrinfo *local,
bool do_listen,
500 bool nowait,
volatile int *signal_received)
522 if (*signal_received)
550 msg(
M_ERR,
"TCP: close socket failed (sd)");
561 const char *prefix,
bool ipv6only)
575 const struct addrinfo *cur;
576 for (cur = local; cur; cur = cur->ai_next)
578 if (cur->ai_family == ai_family)
585 msg(
M_FATAL,
"%s: Socket bind failed: Addr to bind has no %s record", prefix,
589 if (ai_family == AF_INET6)
591 int v6only = ipv6only ? 1 : 0;
593 msg(
M_INFO,
"setsockopt(IPV6_V6ONLY=%d)", v6only);
594 if (setsockopt(sd, IPPROTO_IPV6, IPV6_V6ONLY, (
void *)&v6only,
sizeof(v6only)))
609 volatile int *signal_received)
614 protect_fd_nonlocal(sd, remote);
633 struct pollfd fds[1];
635 fds[0].events = POLLOUT;
636 status = poll(fds, 1, (connect_timeout > 0) ? 1000 : 0);
643 tv.tv_sec = (connect_timeout > 0) ? 1 : 0;
651 if (*signal_received)
664 if (--connect_timeout < 0)
683 if (getsockopt(sd, SOL_SOCKET, SO_ERROR, (
void *)&val, &len) == 0
684 && len ==
sizeof(val))
706 if (ai->ai_family == AF_INET)
708 actual->
dest.
addr.
in4 = *((
struct sockaddr_in *)ai->ai_addr);
710 else if (ai->ai_family == AF_INET6)
712 actual->
dest.
addr.
in6 = *((
struct sockaddr_in6 *)ai->ai_addr);
729#ifdef ENABLE_MANAGEMENT
770 const unsigned int sockflags,
const int proto);
856 volatile int *signal_received = sig_info ? &sig_info->
signal_received : NULL;
913 sock->
info.
af, flags, &ai);
917 sig_info, sock->
info.
af, &ai);
926 flags, phase, retry, signal_received ? *signal_received : -1,
status);
928 if (signal_received && *signal_received)
947 msg(
M_INFO,
"TCP/UDP: Preserving recently used remote address: %s",
988 const char *remote_host = o->
ce.
remote;
1032 sock->gremlin = o->gremlin;
1041 if (o->port_share_host && o->port_share_port)
1138#if EXTENDED_SOCKET_ERROR_CAPABILITY
1140 set_sock_extended_error_passing(sock->
sd, sock->
info.
af);
1158 const struct addrinfo *cur;
1161 if (!ai_family || ai_family == cur->ai_family)
1167 msg(msglevel,
"%s link local (bound): %s",
1172 msg(msglevel,
"%s link local: (not bound)",
1219 bool proxy_retry =
false;
1248 }
while (proxy_retry);
1303 const char *device_guid = NULL;
1307 static_assert(
sizeof(sock->
sd) ==
sizeof(tt->
hand),
"HANDLE and SOCKET size differs");
1346 sig_save = *sig_info;
1379 if (sock->
info.
af == AF_UNSPEC)
1382 msg(
M_WARN,
"Could not determine IPv4/IPv6 protocol. Using %s",
1392 msg(
M_WARN,
"Could not determine IPv4/IPv6 protocol");
1414#ifdef TARGET_ANDROID
1449 const int gremlin = GREMLIN_CONNECTION_FLOOD_LEVEL(sock->gremlin);
1451 const int gremlin = 0;
1461 msg(
D_LOW,
"TCP/UDP: Closing socket");
1535 buf_printf(&out,
"Peer Connection Initiated with %s",
1549 != OPENVPN_PLUGIN_FUNC_SUCCESS)
1551 msg(
M_WARN,
"WARNING: ipchange plugin call failed");
1574 const struct addrinfo *ai;
1581 "TCP/UDP: Incoming packet rejected from %s[%d], expected peer address: %s (allow this incoming source address/port by removing --remote or adding --float)",
1628 return ntohl(((
struct sockaddr_in *)lsa->
current_remote->ai_addr)->sin_addr.s_addr);
1636const struct in6_addr *
1658 return &(((
struct sockaddr_in6 *)lsa->
current_remote->ai_addr)->sin6_addr);
1728 sb->port_share_state =
1742static inline struct buffer
1748 next.
offset = sb->buf.offset + sb->buf.len;
1749 next.
len = (sb->len >= 0 ? sb->len : sb->maxlen) - sb->buf.len;
1751 sb->buf.offset, sb->buf.len, next.
offset, next.
len, sb->len, sb->maxlen);
1813 if (length_added > 0)
1816 sb->
buf.
len += (int)length_added;
1826 if (sb->port_share_state == PS_ENABLED)
1828 if (!is_openvpn_protocol(&sb->
buf))
1831 sb->port_share_state = PS_FOREIGN;
1837 sb->port_share_state = PS_DISABLED;
1848 "WARNING: Bad encapsulated packet length from peer (%d), which must be > 0 and <= %d -- please ensure that --tun-mtu or --link-mtu is equal on both peers -- this condition could also indicate a possible active attack on the TCP link -- [Attempting restart...]",
1910 "ERROR: received strange incoming packet with an address length of %d -- we only accept address lengths of %d.",
1932 msg(
M_INFO,
"BUG: link_socket_read_tcp(): sock->sd==-1, reset client instance");
1934 return buf->
len = 0;
1965 return buf->
len = 0;
1971#if ENABLE_IP_PKTINFO
1976#if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST)
1977#define PKTINFO_BUF_SIZE \
1978 max_int(CMSG_SPACE(sizeof(struct in6_pktinfo)), CMSG_SPACE(sizeof(struct in_pktinfo)))
1980#define PKTINFO_BUF_SIZE \
1981 max_int(CMSG_SPACE(sizeof(struct in6_pktinfo)), CMSG_SPACE(sizeof(struct in_addr)))
1985link_socket_read_udp_posix_recvmsg(
const struct link_socket *sock,
struct buffer *buf,
1989 uint8_t pktinfo_buf[PKTINFO_BUF_SIZE];
1990 struct msghdr mesg = { 0 };
1991 *fromlen =
sizeof(from->
dest.
addr);
1995 iov.iov_base =
BPTR(buf);
1997 mesg.msg_iov = &iov;
1998 mesg.msg_iovlen = 1;
2000 mesg.msg_namelen = *fromlen;
2001 mesg.msg_control = pktinfo_buf;
2002 mesg.msg_controllen = (socklen_t)
sizeof(pktinfo_buf);
2003 ssize_t len = recvmsg(sock->
sd, &mesg, 0);
2010 buf->
len = (int)len;
2011 struct cmsghdr *cmsg;
2012 *fromlen = mesg.msg_namelen;
2013 cmsg = CMSG_FIRSTHDR(&mesg);
2014 if (cmsg != NULL && CMSG_NXTHDR(&mesg, cmsg) == NULL
2015#
if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST)
2016 && cmsg->cmsg_level ==
SOL_IP && cmsg->cmsg_type == IP_PKTINFO
2017 && cmsg->cmsg_len >= CMSG_LEN(
sizeof(
struct in_pktinfo)))
2018#elif defined(IP_RECVDSTADDR)
2019 && cmsg->cmsg_level == IPPROTO_IP && cmsg->cmsg_type == IP_RECVDSTADDR
2020 && cmsg->cmsg_len >= CMSG_LEN(
sizeof(
struct in_addr)))
2022#error
ENABLE_IP_PKTINFO is set without IP_PKTINFO xor IP_RECVDSTADDR (fix syshead.h)
2025#if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST)
2026 struct in_pktinfo *pkti = (
struct in_pktinfo *)CMSG_DATA(cmsg);
2027 from->pi.in4.ipi_ifindex =
2029 from->pi.in4.ipi_spec_dst = pkti->ipi_spec_dst;
2030#elif defined(IP_RECVDSTADDR)
2031 from->pi.in4 = *(
struct in_addr *)CMSG_DATA(cmsg);
2033#error ENABLE_IP_PKTINFO is set without IP_PKTINFO xor IP_RECVDSTADDR (fix syshead.h)
2036 else if (cmsg != NULL && CMSG_NXTHDR(&mesg, cmsg) == NULL
2037 && cmsg->cmsg_level == IPPROTO_IPV6 && cmsg->cmsg_type == IPV6_PKTINFO
2038 && cmsg->cmsg_len >= CMSG_LEN(
sizeof(
struct in6_pktinfo)))
2040 struct in6_pktinfo *pkti6 = (
struct in6_pktinfo *)CMSG_DATA(cmsg);
2041 from->pi.in6.ipi6_ifindex =
2043 from->pi.in6.ipi6_addr = pkti6->ipi6_addr;
2045 else if (cmsg != NULL)
2048 "CMSG received that cannot be parsed (cmsg_level=%d, cmsg_type=%d, cmsg=len=%zu)",
2049 cmsg->cmsg_level, cmsg->cmsg_type, (
size_t)cmsg->cmsg_len);
2061 socklen_t fromlen =
sizeof(from->
dest.
addr);
2067#if ENABLE_IP_PKTINFO
2071 recvlen = link_socket_read_udp_posix_recvmsg(sock, buf, from, &fromlen);
2084 ASSERT(recvlen <= INT_MAX);
2085 buf->
len = (int)recvlen;
2087 if (expectedlen && fromlen != expectedlen)
2103 const int blen =
BLEN(buf);
2113 return link_socket_write_tcp_posix(sock, buf);
2117#if ENABLE_IP_PKTINFO
2120link_socket_write_udp_posix_sendmsg(
const struct link_socket *sock,
const struct buffer *buf,
2125 struct cmsghdr *cmsg;
2126 uint8_t pktinfo_buf[PKTINFO_BUF_SIZE];
2130 iov.iov_base = (
char *)
CBPTR(buf);
2131 iov.iov_len =
BLENZ(buf);
2132 mesg.msg_iov = &iov;
2133 mesg.msg_iovlen = 1;
2139 mesg.msg_namelen =
sizeof(
struct sockaddr_in);
2140 mesg.msg_control = pktinfo_buf;
2142#if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST)
2143 mesg.msg_controllen = CMSG_SPACE(
sizeof(
struct in_pktinfo));
2144 cmsg = CMSG_FIRSTHDR(&mesg);
2145 cmsg->cmsg_len = CMSG_LEN(
sizeof(
struct in_pktinfo));
2146 cmsg->cmsg_level =
SOL_IP;
2147 cmsg->cmsg_type = IP_PKTINFO;
2149 struct in_pktinfo *pkti;
2150 pkti = (
struct in_pktinfo *)CMSG_DATA(cmsg);
2151 pkti->ipi_ifindex = to->pi.in4.ipi_ifindex;
2152 pkti->ipi_spec_dst = to->pi.in4.ipi_spec_dst;
2153 pkti->ipi_addr.s_addr = 0;
2155#elif defined(IP_RECVDSTADDR)
2156 ASSERT(CMSG_SPACE(
sizeof(
struct in_addr)) <=
sizeof(pktinfo_buf));
2157 mesg.msg_controllen = CMSG_SPACE(
sizeof(
struct in_addr));
2158 cmsg = CMSG_FIRSTHDR(&mesg);
2159 cmsg->cmsg_len = CMSG_LEN(
sizeof(
struct in_addr));
2160 cmsg->cmsg_level = IPPROTO_IP;
2161 cmsg->cmsg_type = IP_RECVDSTADDR;
2162 *(
struct in_addr *)CMSG_DATA(cmsg) = to->pi.in4;
2164#error ENABLE_IP_PKTINFO is set without IP_PKTINFO xor IP_RECVDSTADDR (fix syshead.h)
2171 struct in6_pktinfo *pkti6;
2173 mesg.msg_namelen =
sizeof(
struct sockaddr_in6);
2175 ASSERT(CMSG_SPACE(
sizeof(
struct in6_pktinfo)) <=
sizeof(pktinfo_buf));
2176 mesg.msg_control = pktinfo_buf;
2177 mesg.msg_controllen = CMSG_SPACE(
sizeof(
struct in6_pktinfo));
2179 cmsg = CMSG_FIRSTHDR(&mesg);
2180 cmsg->cmsg_len = CMSG_LEN(
sizeof(
struct in6_pktinfo));
2181 cmsg->cmsg_level = IPPROTO_IPV6;
2182 cmsg->cmsg_type = IPV6_PKTINFO;
2184 pkti6 = (
struct in6_pktinfo *)CMSG_DATA(cmsg);
2185 pkti6->ipi6_ifindex = to->pi.in6.ipi6_ifindex;
2186 pkti6->ipi6_addr = to->pi.in6.ipi6_addr;
2193 return sendmsg(sock->
sd, &mesg, 0);
2209 return GetLastError();
2212 return WSAGetLastError();
2241 wsabuf[0].len = maxsize ? maxsize :
BLEN(&sock->
reads.
buf);
2249 status = ReadFile((HANDLE)sock->
sd, wsabuf[0].buf, wsabuf[0].len, &sock->
reads.
size,
2288 dmsg(
D_WIN32_IO,
"WIN32 I/O: Socket Receive immediate return [%d,%d]",
2289 (
int)wsabuf[0].len, (
int)sock->
reads.
size);
2294 if (
status == WSA_IO_PENDING)
2298 dmsg(
D_WIN32_IO,
"WIN32 I/O: Socket Receive queued [%d]", (
int)wsabuf[0].len);
2306 dmsg(
D_WIN32_IO,
"WIN32 I/O: Socket Receive error [%d]: %s", (
int)wsabuf[0].len,
2338 status = WriteFile((HANDLE)sock->
sd, wsabuf[0].buf, wsabuf[0].len, &sock->
writes.
size,
2386 dmsg(
D_WIN32_IO,
"WIN32 I/O: Socket Send immediate return [%d,%d]", (
int)wsabuf[0].len,
2393 if (
status == WSA_IO_PENDING ||
status == ERROR_IO_PENDING)
2397 dmsg(
D_WIN32_IO,
"WIN32 I/O: Socket Send queued [%d]", (
int)wsabuf[0].len);
2406 dmsg(
D_WIN32_IO,
"WIN32 I/O: Socket Send error [%d]: %s", (
int)wsabuf[0].len,
2429 case sizeof(
struct sockaddr_in):
2430 case sizeof(struct sockaddr_in6):
2433 case sizeof(struct sockaddr_in6) - 4:
2440 switch (io->addr.sin_family)
2443 memcpy(dst, &io->
addr,
sizeof(
struct sockaddr_in));
2447 memcpy(dst, &io->
addr6,
sizeof(
struct sockaddr_in6));
2477 const struct sockaddr *sa = (
const struct sockaddr *)
BPTR(buf);
2478 switch (sa->sa_family)
2481 sa_len =
sizeof(
struct sockaddr_in);
2485 sa_len =
sizeof(
struct sockaddr_in6);
2493 memcpy(dst, sa, sa_len);
2556 dmsg(
D_WIN32_IO,
"WIN32 I/O: Completion non-queued success [%d]", ret);
2595 unsigned int *persistent)
2613 if (!persistent || *persistent != rwflags)
2618 *persistent = rwflags;
2627#if UNIX_SOCK_SUPPORT
2644sockaddr_unix_name(
const struct sockaddr_un *local,
const char *null)
2646 if (local && local->sun_family == PF_UNIX)
2648 return local->sun_path;
2657create_socket_unix(
void)
2661 if ((sd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
2663 msg(
M_ERR,
"Cannot create unix domain socket");
2677 const mode_t orig_umask = umask(0);
2679 if (bind(sd, (
struct sockaddr *)local,
sizeof(
struct sockaddr_un)))
2681 msg(
M_FATAL |
M_ERRNO,
"%s: Socket bind[%d] failed on unix domain socket %s", prefix,
2682 (
int)sd, sockaddr_unix_name(local,
"NULL"));
2692 socklen_t remote_len =
sizeof(
struct sockaddr_un);
2696 ret = accept(sd, (
struct sockaddr *)remote, &remote_len);
2709 int status = connect(sd, (
struct sockaddr *)remote,
sizeof(
struct sockaddr_un));
2718sockaddr_unix_init(
struct sockaddr_un *local,
const char *path)
2720 local->sun_family = PF_UNIX;
2721 strlcpy(local->sun_path, path,
sizeof(local->sun_path));
2725socket_delete_unix(
const struct sockaddr_un *local)
2727 const char *name = sockaddr_unix_name(local, NULL);
2728 if (name && strlen(name))
2737#ifdef HAVE_GETPEEREID
2740 if (getpeereid(sd, &u, &g) == -1)
2753#elif defined(SO_PEERCRED)
2754 struct ucred peercred;
2755 socklen_t so_len =
sizeof(peercred);
2756 if (getsockopt(sd, SOL_SOCKET, SO_PEERCRED, &peercred, &so_len) == -1)
2762 *uid = peercred.uid;
2766 *gid = peercred.gid;
void argv_parse_cmd(struct argv *argres, const char *cmdstr)
Parses a command string, tokenizes it and puts each element into a separate struct argv argument slot...
void argv_free(struct argv *a)
Frees all memory allocations allocated by the struct argv related functions.
bool argv_printf(struct argv *argres, const char *format,...)
printf() variant which populates a struct argv.
bool argv_printf_cat(struct argv *argres, const char *format,...)
printf() inspired argv concatenation.
struct argv argv_new(void)
Allocates a new struct argv and ensures it is initialised.
void free_buf(struct buffer *buf)
Free the memory allocated for a buffer.
bool buf_printf(struct buffer *buf, const char *format,...)
printf-style append to a buffer with overflow check.
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Allocate a buffer of the given size under garbage collection.
struct buffer alloc_buf(size_t size)
Allocate a buffer of the given size.
#define BSTR(buf)
Return the buffer content pointer cast to char *.
static bool buf_copy(struct buffer *dest, const struct buffer *src)
Copy the content of one buffer to the end of another.
#define BPTR(buf)
Return a pointer to the start of the buffer content.
static bool buf_copy_excess(struct buffer *dest, struct buffer *src, int len)
Truncate src to len bytes and copy any excess to dest.
static bool buf_write_prepend(struct buffer *dest, const void *src, int size)
Prepend data to a buffer.
static bool buf_safe(const struct buffer *buf, size_t len)
Check whether len bytes can be appended to a buffer.
static bool buf_read(struct buffer *src, void *dest, int size)
Read bytes from the front of a buffer into a caller-supplied destination.
static int buf_len(const struct buffer *buf)
Return the length of the buffer content.
static int buf_forward_capacity(const struct buffer *buf)
Return the number of bytes that can still be appended to the buffer.
#define CBPTR(buf)
Return a const pointer to the start of the buffer content.
static bool buf_advance(struct buffer *buf, ssize_t size)
Advance the content start of a buffer, consuming bytes from the front.
#define BLEN(buf)
Return the length of the buffer content in bytes.
#define BLENZ(buf)
Return the length of the buffer content as a size_t.
static void gc_free(struct gc_arena *a)
Free all allocations in a garbage collection arena.
#define ALLOC_OBJ_CLEAR(dptr, type)
Allocate and zero-initialise memory for a single object of the given type.
static bool buf_defined(const struct buffer *buf)
Return true iff buf has a non-NULL data pointer.
#define buf_init(buf, offset)
static struct gc_arena gc_new(void)
Allocate and return a new, empty garbage collection arena.
static int buf_forward_capacity_total(const struct buffer *buf)
Return the total number of bytes available from the current offset to the end of the allocated memory...
size_t strlcpy(char *dest, const char *src, size_t maxlen)
void dco_mp_start_vpn(HANDLE handle, struct link_socket *sock)
Initializes and binds the kernel UDP transport socket for multipeer mode.
void dco_p2p_new_peer(HANDLE handle, OVERLAPPED *ov, struct link_socket *sock, struct signal_info *sig_info)
void setenv_str(struct env_set *es, const char *name, const char *value)
const struct rw_handle * event_t
static void event_ctl(struct event_set *es, event_t event, unsigned int rwflags, void *arg)
void set_nonblock(socket_descriptor_t fd)
void set_cloexec(socket_descriptor_t fd)
static void openvpn_fd_set(socket_descriptor_t fd, fd_set *setp)
int get_server_poll_remaining_time(const struct event_timeout *server_poll_timeout)
Interface functions to the internal and external multiplexers.
static SERVICE_STATUS status
void management_set_state(struct management *man, const int state, const char *detail, const in_addr_t *tun_local_ip, const struct in6_addr *tun_local_ip6, const struct openvpn_sockaddr *local, const struct openvpn_sockaddr *remote)
void management_sleep(const int n)
A sleep function that services the management layer for n seconds rather than doing nothing.
#define OPENVPN_STATE_TCP_CONNECT
void alloc_buf_sock_tun(struct buffer *buf, const struct frame *frame)
void set_mtu_discover_type(socket_descriptor_t sd, int mtu_type, sa_family_t proto_af)
const char * strerror_win32(DWORD errnum, struct gc_arena *gc)
#define MODE_POINT_TO_POINT
static bool dco_enabled(const struct options *o)
Returns whether the current configuration has dco enabled.
bool plugin_defined(const struct plugin_list *pl, const int type)
static int plugin_call(const struct plugin_list *pl, const int type, const struct argv *av, struct plugin_return *pr, struct env_set *es)
bool establish_http_proxy_passthru(struct http_proxy_info *p, socket_descriptor_t sd, const char *host, const char *port, const struct event_timeout *server_poll_timeout, struct buffer *lookahead, struct signal_info *sig_info)
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 get_cached_dns_entry(struct cached_dns_entry *dns_cache, const char *hostname, const char *servname, int ai_family, unsigned int resolve_flags, struct addrinfo **ai)
get_cached_dns_entry return 0 on success and -1 otherwise.
unsigned int sf2gaf(const unsigned int getaddr_flags, const unsigned int sockflags)
Convert sockflags/getaddr_flags into getaddr_flags.
#define GETADDR_UPDATE_MANAGEMENT_STATE
#define GETADDR_MENTION_RESOLVE_RETRY
#define GETADDR_WARN_ON_SIGNAL
static int openvpn_run_script(const struct argv *a, const struct env_set *es, const unsigned int flags, const char *hook)
Will run a script and return the exit code of the script if between 0 and 255, -1 otherwise.
int signal_reset(struct signal_info *si, int signum)
Clear the signal if its current value equals signum.
void throw_signal(const int signum)
Throw a hard signal.
struct signal_info siginfo_static
void register_signal(struct signal_info *si, int signum, const char *signal_text)
Register a soft signal in the signal_info struct si respecting priority.
static void get_signal(volatile int *sig)
Copy the global signal_received (if non-zero) to the passed-in argument sig.
void link_socket_init_phase1(struct context *c, int sock_index, int mode)
static int socket_get_sndbuf(socket_descriptor_t sd)
static void socket_set_sndbuf(socket_descriptor_t sd, int size)
void socket_bind(socket_descriptor_t sd, const struct addrinfo *local, int ai_family, const char *prefix, bool ipv6only)
void link_socket_init_phase2(struct context *c, struct link_socket *sock)
static void ipchange_fmt(const bool include_cmd, struct argv *argv, const struct link_socket_info *info, struct gc_arena *gc)
static int socket_get_last_error(const struct link_socket *sock)
static void stream_buf_init(struct stream_buf *sb, const struct buffer *buf, const unsigned int sockflags, const int proto)
ssize_t link_socket_write_tcp(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
void link_socket_update_buffer_sizes(struct link_socket *sock, int rcvbuf, int sndbuf)
static socket_descriptor_t create_socket_udp(struct addrinfo *addrinfo, const unsigned int flags, bool optional)
static void create_socket(struct link_socket *sock, struct addrinfo *addr)
const struct in6_addr * link_socket_current_remote_ipv6(const struct link_socket_info *info)
void set_actual_address(struct link_socket_actual *actual, struct addrinfo *ai)
const char * socket_stat(const struct link_socket *s, unsigned int rwflags, struct gc_arena *gc)
static void socket_apply_options(socket_descriptor_t sd, const struct socket_buffer_size *sbs, int mark, const char *bind_dev)
void bad_address_length(int actual, int expected)
event_t socket_listen_event_handle(struct link_socket *s)
static void linksock_print_addr(struct link_socket *sock)
static socket_descriptor_t socket_listen_accept(socket_descriptor_t sd, struct link_socket_actual *act, const struct addrinfo *local, bool do_listen, bool nowait, volatile int *signal_received)
static void socket_set_mark(socket_descriptor_t sd, int mark)
static void socket_set_rcvbuf(socket_descriptor_t sd, int size)
static void stream_buf_close(struct stream_buf *sb)
static void stream_buf_get_final(struct stream_buf *sb, struct buffer *buf)
Sets the parameter buf to the current buffer of sb->buf.
static void socket_connect(socket_descriptor_t *sd, const struct sockaddr *dest, const int connect_timeout, struct signal_info *sig_info)
ssize_t link_socket_read_tcp(struct link_socket *sock, struct buffer *buf)
static void phase2_socks_client(struct link_socket *sock, struct signal_info *sig_info)
static bool socket_set_tcp_nodelay(socket_descriptor_t sd, int state)
socket_descriptor_t socket_do_accept(socket_descriptor_t sd, struct link_socket_actual *act, const bool nowait)
static void socket_do_listen(socket_descriptor_t sd, const struct addrinfo *local, bool do_listen, bool do_set_nonblock)
static void phase2_tcp_server(struct link_socket *sock, struct signal_info *sig_info)
int socket_recv_queue(struct link_socket *sock, int maxsize)
void link_socket_close(struct link_socket *sock)
void link_socket_connection_initiated(struct link_socket_info *info, const struct link_socket_actual *act, const char *common_name, struct env_set *es)
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.
bool stream_buf_read_setup_dowork(struct stream_buf *sb)
Will try to check if the buffers in stream form a full packet.
static void resolve_remote(struct link_socket *sock, int phase, struct signal_info *sig_info)
static void bind_local(const struct link_socket *sock)
int socket_send_queue(struct link_socket *sock, const struct buffer *buf, const struct link_socket_actual *to)
void link_socket_bad_outgoing_addr(void)
int sockethandle_finalize(sockethandle_t sh, struct overlapped_io *io, struct buffer *buf, struct link_socket_actual *from)
static void phase2_set_socket_flags(const struct link_socket *sock)
in_addr_t link_socket_current_remote(const struct link_socket_info *info)
static int socket_get_rcvbuf(socket_descriptor_t sd)
int openvpn_connect(socket_descriptor_t sd, const struct sockaddr *remote, int connect_timeout, volatile int *signal_received)
unsigned int socket_set(struct link_socket *s, struct event_set *es, unsigned int rwflags, void *arg, unsigned int *persistent)
static struct buffer stream_buf_get_next(struct stream_buf *sb)
Return a buffer that is backed by the same backend as sb->buf that determines where the next read sho...
static void read_sockaddr_from_overlapped(const struct overlapped_io *io, struct sockaddr *dst, int overlapped_ret)
void link_socket_bad_incoming_addr(struct buffer *buf, const struct link_socket_info *info, const struct link_socket_actual *from_addr)
static void phase2_tcp_client(struct link_socket *sock, struct signal_info *sig_info)
socket_descriptor_t create_socket_tcp(struct addrinfo *addrinfo)
static void socket_frame_init(const struct frame *frame, struct link_socket *sock)
static void stream_buf_reset(struct stream_buf *sb)
resets the stream buffer to be set up for the next round of reassembling a packet
static void create_socket_dco_win(struct context *c, struct link_socket *sock, struct signal_info *sig_info)
static void resolve_bind_local(struct link_socket *sock)
static void tcp_connection_established(const struct link_socket_actual *act)
struct link_socket * link_socket_new(void)
static bool stream_buf_added(struct stream_buf *sb, ssize_t length_added)
This will determine if sb->buf contains a full packet.
static int read_sockaddr_from_packet(struct buffer *buf, struct sockaddr *dst)
Extracts a sockaddr from a packet payload.
bool sockets_read_residual(const struct context *c)
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,...
void setenv_trusted(struct env_set *es, const struct link_socket_info *info)
static event_t socket_event_handle(const struct link_socket *sock)
#define IPV4_INVALID_ADDR
static BOOL SocketHandleGetOverlappedResult(sockethandle_t sh, struct overlapped_io *io)
#define LS_MODE_TCP_ACCEPT_FROM
static bool link_socket_connection_oriented(const struct link_socket *sock)
static bool stream_buf_read_setup(struct link_socket *sock)
void sd_close(socket_descriptor_t *sd)
static void SocketHandleSetLastError(sockethandle_t sh, DWORD err)
static int SocketHandleGetLastError(sockethandle_t sh)
static void SocketHandleSetInvalError(sockethandle_t sh)
#define RESOLV_RETRY_INFINITE
#define SF_USE_IP_PKTINFO
uint16_t packet_size_type
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 SF_GETADDRINFO_DGRAM
#define LS_MODE_TCP_LISTEN
static int openvpn_select(socket_descriptor_t nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
static int link_socket_write_win32(struct link_socket *sock, struct buffer *buf, struct link_socket_actual *to)
#define SF_PKTINFO_COPY_IIF
#define openvpn_close_socket(s)
static int openvpn_bind(socket_descriptor_t sockfd, const struct sockaddr *addr, size_t addrlen)
const char * proto2ascii(int proto, sa_family_t af, bool display_form)
const char * print_sockaddr_ex(const struct sockaddr *sa, const char *separator, const unsigned int flags, struct gc_arena *gc)
void setenv_link_socket_actual(struct env_set *es, const char *name_prefix, const struct link_socket_actual *act, const unsigned int flags)
const char * print_link_socket_actual(const struct link_socket_actual *act, struct gc_arena *gc)
const char * print_link_socket_actual_ex(const struct link_socket_actual *act, const char *separator, const unsigned int flags, struct gc_arena *gc)
const char * addr_family_name(int af)
static const char * print_sockaddr(const struct sockaddr *addr, struct gc_arena *gc)
static bool link_socket_actual_defined(const struct link_socket_actual *act)
static bool proto_is_udp(int proto)
Returns if the protocol being used is UDP.
#define PS_SHOW_PORT_IF_DEFINED
static bool proto_is_tcp(int proto)
returns if the proto is a TCP variant (tcp-server, tcp-client or tcp)
static void addr_zero_host(struct openvpn_sockaddr *addr)
static bool proto_is_dgram(int proto)
Return if the protocol is datagram (UDP).
static int af_addr_size(sa_family_t af)
void establish_socks_proxy_passthru(const struct socks_proxy_info *p, socket_descriptor_t sd, const char *host, const char *servname, const struct event_timeout *server_poll_timeout, struct signal_info *sig_info)
void establish_socks_proxy_udpassoc(const struct socks_proxy_info *p, socket_descriptor_t ctrl_sd, struct openvpn_sockaddr *relay_addr, const struct event_timeout *server_poll_timeout, struct signal_info *sig_info)
Wrapper structure for dynamically allocated memory.
int len
Length in bytes of the actual content within the allocated memory.
int offset
Offset in bytes of the actual content within the allocated memory.
struct local_list * local_list
struct link_socket_addr * link_socket_addrs
Local and remote addresses on the external network.
struct http_proxy_info * http_proxy
struct socks_proxy_info * socks_proxy
struct cached_dns_entry * dns_cache
struct tuntap * tuntap
Tun/tap virtual network interface.
struct event_timeout server_poll_interval
const struct link_socket * accept_from
struct link_socket ** link_sockets
Contains all state information for one tunnel.
int mode
Role of this context within the OpenVPN process.
struct signal_info * sig
Internal error signaling object.
struct plugin_list * plugins
List of plug-ins.
struct context_2 c2
Level 2 context.
struct options options
Options loaded from command line or configuration file.
struct gc_arena gc
Garbage collection arena for allocations done in the scope of this context structure.
struct context_1 c1
Level 1 context.
union event_arg::@352313260244035237115140212234220346176163201310 u
struct link_socket * sock
Packet geometry parameters.
Garbage collection arena used to keep track of dynamically allocated memory.
struct http_proxy_options options
struct openvpn_sockaddr dest
struct link_socket_actual actual
struct addrinfo * remote_list
struct addrinfo * bind_local
struct addrinfo * current_remote
bool connection_established
struct link_socket_addr * lsa
const char * ipchange_command
const struct plugin_list * plugins
struct event_timeout * server_poll_timeout
unsigned int rwflags_debug
struct socket_buffer_size socket_buffer_sizes
struct link_socket_actual socks_relay
struct rw_handle listen_handle
struct link_socket_info info
const char * proxy_dest_port
struct rw_handle rw_handle
struct overlapped_io writes
socket_descriptor_t ctrl_sd
struct stream_buf stream_buf
struct socks_proxy_info * socks_proxy
int resolve_retry_seconds
const char * proxy_dest_host
struct http_proxy_info * http_proxy
struct cached_dns_entry * dns_cache
struct buffer stream_buf_data
struct event_arg ev_arg
this struct will store a pointer to either mi or link_socket, depending on the event type,...
struct overlapped_io reads
struct local_entry ** array
struct man_connection connection
union openvpn_sockaddr::@051240265251124304325377241125360350250341115171 addr
int resolve_retry_seconds
struct connection_entry ce
struct sockaddr_in6 addr6
volatile int signal_received
struct used to extract packets encapsulated in streams into a buffer, in this case OpenVPN packets (d...
struct buffer buf
Holds the data of the current packet.
bool error
if true, a fatal TCP error has occurred, requiring that connection be restarted
struct buffer residual
buffer holding the excess bytes that are not part of the packet.
bool residual_fully_formed
The buffer in buf contains a full packet without a header.
int maxlen
Maximum length of a packet that we accept.
int len
-1 if not yet known.
enum tun_driver_type backend_driver
The backend driver that used for this tun/tap device.
OVERLAPPED dco_new_peer_ov
struct tuntap_options options
unsigned short sa_family_t
SOCKET socket_descriptor_t
static int socket_defined(const socket_descriptor_t sd)
#define ENABLE_IP_PKTINFO
void tun_open_device(struct tuntap *tt, const char *dev_node, const char **device_guid, struct gc_arena *gc)
void init_net_event_win32(struct rw_handle *event, long network_events, socket_descriptor_t sd, unsigned int flags)
void overlapped_io_init(struct overlapped_io *o, const struct frame *frame, BOOL event_state)
void close_net_event_win32(struct rw_handle *event, socket_descriptor_t sd, unsigned int flags)
char * overlapped_io_state_ascii(const struct overlapped_io *o)
void overlapped_io_close(struct overlapped_io *o)
static bool defined_net_event_win32(const struct rw_handle *event)
#define IOSTATE_IMMEDIATE_RETURN