From: "cron2 (Code Review)" In-Reply-To: References: Reply-To: gert@...1296..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="r2zmF8NZK70="; charset=UTF-8 Subject: [Openvpn-devel] [XL] Change in openvpn[master]: socket: Create socket_util with non-socket functions List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Date: Sun, 31 Aug 2025 16:27:49 -0000 To: flichtenheld Cc: plaisthos , openvpn-devel --r2zmF8NZK70= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has submitted this change. ( http://gerrit.openvpn.net/c/openvpn/+/11= 59?usp=3Demail ) Change subject: socket: Create socket_util with non-socket functions ...................................................................... socket: Create socket_util with non-socket functions This extracts a lot of the helper functions that do not actually work on sockets, but instead on addresses or similar. This includes - openvpn_getaddrinfo and related functions - print_* - setenv_* And lots of the inline functions. This move will make it easier to add unit tests for these moved functions. Change-Id: I7393459b975fb9b3e0a42743f58645f769d1be5a Signed-off-by: Frank Lichtenheld Acked-by: Gert Doering Message-Id: <20250831155939.29240-1-gert@...1296...> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg32= 727.html Signed-off-by: Gert Doering --- M CMakeLists.txt M src/openvpn/Makefile.am M src/openvpn/clinat.c M src/openvpn/dhcp.c M src/openvpn/dns.c M src/openvpn/manage.h M src/openvpn/misc.c M src/openvpn/misc.h M src/openvpn/mroute.c M src/openvpn/mss.h M src/openvpn/networking_iproute2.c M src/openvpn/options.c M src/openvpn/options.h M src/openvpn/pool.c M src/openvpn/socket.c M src/openvpn/socket.h A src/openvpn/socket_util.c A src/openvpn/socket_util.h M src/openvpn/ssl_common.h M src/openvpn/tun.c 20 files changed, 1,443 insertions(+), 1,380 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3866e21..45044af 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -555,6 +555,8 @@ src/openvpn/sig.h src/openvpn/socket.c src/openvpn/socket.h + src/openvpn/socket_util.c + src/openvpn/socket_util.h src/openvpn/socks.c src/openvpn/socks.h src/openvpn/ssl.c diff --git a/src/openvpn/Makefile.am b/src/openvpn/Makefile.am index 1e17c3f..217f897 100644 --- a/src/openvpn/Makefile.am +++ b/src/openvpn/Makefile.am @@ -128,6 +128,7 @@ shaper.c shaper.h \ sig.c sig.h \ socket.c socket.h \ + socket_util.c socket_util.h \ socks.c socks.h \ ssl.c ssl.h ssl_backend.h \ ssl_openssl.c ssl_openssl.h \ diff --git a/src/openvpn/clinat.c b/src/openvpn/clinat.c index a303285..48a2057 100644 --- a/src/openvpn/clinat.c +++ b/src/openvpn/clinat.c @@ -28,7 +28,7 @@ #include "clinat.h" #include "proto.h" -#include "socket.h" +#include "socket_util.h" #include "memdbg.h" static bool diff --git a/src/openvpn/dhcp.c b/src/openvpn/dhcp.c index 0a7689f..7abade5 100644 --- a/src/openvpn/dhcp.c +++ b/src/openvpn/dhcp.c @@ -27,7 +27,7 @@ #include "syshead.h" #include "dhcp.h" -#include "socket.h" +#include "socket_util.h" #include "error.h" #include "memdbg.h" diff --git a/src/openvpn/dns.c b/src/openvpn/dns.c index 8e28c2b..8554089 100644 --- a/src/openvpn/dns.c +++ b/src/openvpn/dns.c @@ -27,7 +27,7 @@ #include "syshead.h" #include "dns.h" -#include "socket.h" +#include "socket_util.h" #include "options.h" #include "run_command.h" diff --git a/src/openvpn/manage.h b/src/openvpn/manage.h index 083caf5..b8892c8 100644 --- a/src/openvpn/manage.h +++ b/src/openvpn/manage.h @@ -48,7 +48,7 @@ #include "misc.h" #include "event.h" -#include "socket.h" +#include "socket_util.h" #include "mroute.h" #define MANAGEMENT_VERSION 5 diff --git a/src/openvpn/misc.c b/src/openvpn/misc.c index 17f7706..59bf52b 100644 --- a/src/openvpn/misc.c +++ b/src/openvpn/misc.c @@ -72,27 +72,6 @@ #endif } -/* - * Prepend a random string to hostname to prevent DNS caching. - * For example, foo.bar.gov would be modified to .foo.bar.go= v. - * Of course, this requires explicit support in the DNS server (wildcard). - */ -const char * -hostname_randomize(const char *hostname, struct gc_arena *gc) -{ -#define n_rnd_bytes 6 - - uint8_t rnd_bytes[n_rnd_bytes]; - const char *rnd_str; - struct buffer hname =3D alloc_buf_gc(strlen(hostname) + sizeof(rnd_byt= es) * 2 + 4, gc); - - prng_bytes(rnd_bytes, sizeof(rnd_bytes)); - rnd_str =3D format_hex_ex(rnd_bytes, sizeof(rnd_bytes), 40, 0, NULL, g= c); - buf_printf(&hname, "%s.%s", rnd_str, hostname); - return BSTR(&hname); -#undef n_rnd_bytes -} - #ifdef ENABLE_MANAGEMENT /* Get username/password from the management interface */ static bool diff --git a/src/openvpn/misc.h b/src/openvpn/misc.h index 1b10cd9..e87fd85 100644 --- a/src/openvpn/misc.h +++ b/src/openvpn/misc.h @@ -44,9 +44,6 @@ const char **make_extended_arg_array(char **p, bool is_inline, struct gc_a= rena *gc); -/* prepend a random prefix to hostname */ -const char *hostname_randomize(const char *hostname, struct gc_arena *gc); - /* * Get and store a username/password */ diff --git a/src/openvpn/mroute.c b/src/openvpn/mroute.c index a598037..ab01874 100644 --- a/src/openvpn/mroute.c +++ b/src/openvpn/mroute.c @@ -30,7 +30,7 @@ #include "mroute.h" #include "proto.h" #include "error.h" -#include "socket.h" +#include "socket_util.h" #include "memdbg.h" diff --git a/src/openvpn/mss.h b/src/openvpn/mss.h index 05f12a7..bcbb5a8 100644 --- a/src/openvpn/mss.h +++ b/src/openvpn/mss.h @@ -26,6 +26,7 @@ #include "proto.h" #include "error.h" #include "mtu.h" +#include "socket.h" #include "ssl_common.h" void mss_fixup_ipv4(struct buffer *buf, uint16_t maxmss); diff --git a/src/openvpn/networking_iproute2.c b/src/openvpn/networking_ipr= oute2.c index 0635b5d..e9be3a4 100644 --- a/src/openvpn/networking_iproute2.c +++ b/src/openvpn/networking_iproute2.c @@ -30,7 +30,7 @@ #include "misc.h" #include "openvpn.h" #include "run_command.h" -#include "socket.h" +#include "socket_util.h" #include #include diff --git a/src/openvpn/options.c b/src/openvpn/options.c index 648d526..74946a4 100644 --- a/src/openvpn/options.c +++ b/src/openvpn/options.c @@ -45,7 +45,7 @@ #include "ssl_ncp.h" #include "options.h" #include "misc.h" -#include "socket.h" +#include "socket_util.h" #include "packet_id.h" #include "pkcs11.h" #include "win32.h" diff --git a/src/openvpn/options.h b/src/openvpn/options.h index 4fa6800..44f3fc9 100644 --- a/src/openvpn/options.h +++ b/src/openvpn/options.h @@ -33,7 +33,7 @@ #include "mtu.h" #include "route.h" #include "tun.h" -#include "socket.h" +#include "socket_util.h" #include "plugin.h" #include "manage.h" #include "proxy.h" diff --git a/src/openvpn/pool.c b/src/openvpn/pool.c index a41364a..fde6cea 100644 --- a/src/openvpn/pool.c +++ b/src/openvpn/pool.c @@ -29,7 +29,7 @@ #include "pool.h" #include "buffer.h" #include "error.h" -#include "socket.h" +#include "socket_util.h" #include "otime.h" #include "memdbg.h" diff --git a/src/openvpn/socket.c b/src/openvpn/socket.c index 8fc10bc..306170c 100644 --- a/src/openvpn/socket.c +++ b/src/openvpn/socket.c @@ -405,428 +405,6 @@ throw_signal_soft(SIGHUP, "Preresolving failed"); } -/** - * Small helper function for openvpn_getaddrinfo to print the address - * family when resolving fails - */ -static const char * -getaddrinfo_addr_family_name(int af) -{ - switch (af) - { - case AF_INET: - return "[AF_INET]"; - - case AF_INET6: - return "[AF_INET6]"; - } - return ""; -} - -/* - * Translate IPv4/IPv6 addr or hostname into struct addrinfo - * If resolve error, try again for resolve_retry_seconds seconds. - */ -int -openvpn_getaddrinfo(unsigned int flags, const char *hostname, const char *= servname, - int resolve_retry_seconds, struct signal_info *sig_inf= o, int ai_family, - struct addrinfo **res) -{ - struct addrinfo hints; - int status; - struct signal_info sigrec =3D { 0 }; - int msglevel =3D (flags & GETADDR_FATAL) ? M_FATAL : D_RESOLVE_ERRORS; - struct gc_arena gc =3D gc_new(); - const char *print_hostname; - const char *print_servname; - - ASSERT(res); - - ASSERT(hostname || servname); - ASSERT(!(flags & GETADDR_HOST_ORDER)); - - if (servname) - { - print_servname =3D servname; - } - else - { - print_servname =3D ""; - } - - if (flags & GETADDR_MSG_VIRT_OUT) - { - msglevel |=3D M_MSG_VIRT_OUT; - } - - if ((flags & (GETADDR_FATAL_ON_SIGNAL | GETADDR_WARN_ON_SIGNAL)) && !s= ig_info) - { - sig_info =3D &sigrec; - } - - /* try numeric ip addr first */ - CLEAR(hints); - hints.ai_flags =3D AI_NUMERICHOST; - - if (flags & GETADDR_PASSIVE) - { - hints.ai_flags |=3D AI_PASSIVE; - } - - if (flags & GETADDR_DATAGRAM) - { - hints.ai_socktype =3D SOCK_DGRAM; - } - else - { - hints.ai_socktype =3D SOCK_STREAM; - } - - /* if hostname is not set, we want to bind to 'ANY', with - * the correct address family - v4-only or v6/v6-dual-stack */ - if (!hostname) - { - hints.ai_family =3D ai_family; - } - - status =3D getaddrinfo(hostname, servname, &hints, res); - - if (status !=3D 0) /* parse as numeric address fa= iled? */ - { - const int fail_wait_interval =3D 5; /* seconds */ - /* Add +4 to cause integer division rounding up (1 + 4) =3D 5, (0+= 4)/5=3D0 */ - int resolve_retries =3D - (flags & GETADDR_TRY_ONCE) ? 1 : ((resolve_retry_seconds + 4) = / fail_wait_interval); - const char *fmt; - int level =3D 0; - - /* this is not a numeric IP, therefore force resolution using the - * provided ai_family */ - hints.ai_family =3D ai_family; - - if (hostname && (flags & GETADDR_RANDOMIZE)) - { - hostname =3D hostname_randomize(hostname, &gc); - } - - if (hostname) - { - print_hostname =3D hostname; - } - else - { - print_hostname =3D "undefined"; - } - - fmt =3D "RESOLVE: Cannot resolve host address: %s:%s%s (%s)"; - if ((flags & GETADDR_MENTION_RESOLVE_RETRY) && !resolve_retry_seco= nds) - { - fmt =3D "RESOLVE: Cannot resolve host address: %s:%s%s (%s)" - "(I would have retried this name query if you had " - "specified the --resolv-retry option.)"; - } - - if (!(flags & GETADDR_RESOLVE) || status =3D=3D EAI_FAIL) - { - msg(msglevel, "RESOLVE: Cannot parse IP address: %s:%s (%s)", = print_hostname, - print_servname, gai_strerror(status)); - goto done; - } - -#ifdef ENABLE_MANAGEMENT - if (flags & GETADDR_UPDATE_MANAGEMENT_STATE) - { - if (management) - { - management_set_state(management, OPENVPN_STATE_RESOLVE, NU= LL, NULL, NULL, NULL, - NULL); - } - } -#endif - - /* - * Resolve hostname - */ - while (true) - { -#ifndef _WIN32 - /* force resolv.conf reload */ - res_init(); -#endif - /* try hostname lookup */ - hints.ai_flags &=3D ~AI_NUMERICHOST; - dmsg(D_SOCKET_DEBUG, "GETADDRINFO flags=3D0x%04x ai_family=3D%= d ai_socktype=3D%d", flags, - hints.ai_family, hints.ai_socktype); - status =3D getaddrinfo(hostname, servname, &hints, res); - - if (sig_info) - { - get_signal(&sig_info->signal_received); - if (sig_info->signal_received) /* were we interrupted by a= signal? */ - { - /* why are we overwriting SIGUSR1 ? */ - if (signal_reset(sig_info, SIGUSR1) =3D=3D SIGUSR1) /*= ignore SIGUSR1 */ - { - msg(level, "RESOLVE: Ignored SIGUSR1 signal receiv= ed during " - "DNS resolution attempt"); - } - else - { - /* turn success into failure (interrupted syscall)= */ - if (0 =3D=3D status) - { - ASSERT(res); - freeaddrinfo(*res); - *res =3D NULL; - status =3D EAI_AGAIN; /* =3D temporary failure= */ - errno =3D EINTR; - } - goto done; - } - } - } - - /* success? */ - if (0 =3D=3D status) - { - break; - } - - /* resolve lookup failed, should we - * continue or fail? */ - level =3D msglevel; - if (resolve_retries > 0) - { - level =3D D_RESOLVE_ERRORS; - } - - msg(level, fmt, print_hostname, print_servname, getaddrinfo_ad= dr_family_name(ai_family), - gai_strerror(status)); - - if (--resolve_retries <=3D 0) - { - goto done; - } - - management_sleep(fail_wait_interval); - } - - ASSERT(res); - - /* hostname resolve succeeded */ - - /* - * Do not choose an IP Addresse by random or change the order * - * of IP addresses, doing so will break RFC 3484 address selection= * - */ - } - else - { - /* IP address parse succeeded */ - if (flags & GETADDR_RANDOMIZE) - { - msg(M_WARN, "WARNING: ignoring --remote-random-hostname becaus= e the " - "hostname is an IP address"); - } - } - -done: - if (sig_info && sig_info->signal_received) - { - int level =3D 0; - if (flags & GETADDR_FATAL_ON_SIGNAL) - { - level =3D M_FATAL; - } - else if (flags & GETADDR_WARN_ON_SIGNAL) - { - level =3D M_WARN; - } - msg(level, "RESOLVE: signal received during DNS resolution attempt= "); - } - - gc_free(&gc); - return status; -} - -/* - * We do our own inet_aton because the glibc function - * isn't very good about error checking. - */ -int -openvpn_inet_aton(const char *dotted_quad, struct in_addr *addr) -{ - unsigned int a, b, c, d; - - CLEAR(*addr); - if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) =3D=3D 4) - { - if (a < 256 && b < 256 && c < 256 && d < 256) - { - addr->s_addr =3D htonl(a << 24 | b << 16 | c << 8 | d); - return OIA_IP; /* good dotted quad */ - } - } - if (string_class(dotted_quad, CC_DIGIT | CC_DOT, 0)) - { - return OIA_ERROR; /* probably a badly formatted dotted quad */ - } - else - { - return OIA_HOSTNAME; /* probably a hostname */ - } -} - -bool -ip_addr_dotted_quad_safe(const char *dotted_quad) -{ - /* verify non-NULL */ - if (!dotted_quad) - { - return false; - } - - /* verify length is within limits */ - if (strlen(dotted_quad) > 15) - { - return false; - } - - /* verify that all chars are either numeric or '.' and that no numeric - * substring is greater than 3 chars */ - { - int nnum =3D 0; - const char *p =3D dotted_quad; - int c; - - while ((c =3D *p++)) - { - if (c >=3D '0' && c <=3D '9') - { - ++nnum; - if (nnum > 3) - { - return false; - } - } - else if (c =3D=3D '.') - { - nnum =3D 0; - } - else - { - return false; - } - } - } - - /* verify that string will convert to IP address */ - { - struct in_addr a; - return openvpn_inet_aton(dotted_quad, &a) =3D=3D OIA_IP; - } -} - -bool -ipv6_addr_safe(const char *ipv6_text_addr) -{ - /* verify non-NULL */ - if (!ipv6_text_addr) - { - return false; - } - - /* verify length is within limits */ - if (strlen(ipv6_text_addr) > INET6_ADDRSTRLEN) - { - return false; - } - - /* verify that string will convert to IPv6 address */ - { - struct in6_addr a6; - return inet_pton(AF_INET6, ipv6_text_addr, &a6) =3D=3D 1; - } -} - -static bool -dns_addr_safe(const char *addr) -{ - if (addr) - { - const size_t len =3D strlen(addr); - return len > 0 && len <=3D 255 && string_class(addr, CC_ALNUM | CC= _DASH | CC_DOT, 0); - } - else - { - return false; - } -} - -bool -ip_or_dns_addr_safe(const char *addr, const bool allow_fqdn) -{ - if (ip_addr_dotted_quad_safe(addr)) - { - return true; - } - else if (allow_fqdn) - { - return dns_addr_safe(addr); - } - else - { - return false; - } -} - -bool -mac_addr_safe(const char *mac_addr) -{ - /* verify non-NULL */ - if (!mac_addr) - { - return false; - } - - /* verify length is within limits */ - if (strlen(mac_addr) > 17) - { - return false; - } - - /* verify that all chars are either alphanumeric or ':' and that no - * alphanumeric substring is greater than 2 chars */ - { - int nnum =3D 0; - const char *p =3D mac_addr; - int c; - - while ((c =3D *p++)) - { - if ((c >=3D '0' && c <=3D '9') || (c >=3D 'a' && c <=3D 'f') |= | (c >=3D 'A' && c <=3D 'F')) - { - ++nnum; - if (nnum > 2) - { - return false; - } - } - else if (c =3D=3D ':') - { - nnum =3D 0; - } - else - { - return false; - } - } - } - - /* error-checking is left to script invoked in lladdr.c */ - return true; -} - static int socket_get_sndbuf(socket_descriptor_t sd) { @@ -2670,467 +2248,6 @@ #endif } -/* - * Format IP addresses in ascii - */ - -const char * -print_sockaddr_ex(const struct sockaddr *sa, const char *separator, const = unsigned int flags, - struct gc_arena *gc) -{ - struct buffer out =3D alloc_buf_gc(128, gc); - bool addr_is_defined =3D false; - char hostaddr[NI_MAXHOST] =3D ""; - char servname[NI_MAXSERV] =3D ""; - int status; - - socklen_t salen =3D 0; - switch (sa->sa_family) - { - case AF_INET: - if (!(flags & PS_DONT_SHOW_FAMILY)) - { - buf_puts(&out, "[AF_INET]"); - } - salen =3D sizeof(struct sockaddr_in); - addr_is_defined =3D ((struct sockaddr_in *)sa)->sin_addr.s_add= r !=3D 0; - break; - - case AF_INET6: - if (!(flags & PS_DONT_SHOW_FAMILY)) - { - buf_puts(&out, "[AF_INET6]"); - } - salen =3D sizeof(struct sockaddr_in6); - addr_is_defined =3D !IN6_IS_ADDR_UNSPECIFIED(&((struct sockadd= r_in6 *)sa)->sin6_addr); - break; - - case AF_UNSPEC: - if (!(flags & PS_DONT_SHOW_FAMILY)) - { - return "[AF_UNSPEC]"; - } - else - { - return ""; - } - - default: - ASSERT(0); - } - - status =3D getnameinfo(sa, salen, hostaddr, sizeof(hostaddr), servname= , sizeof(servname), - NI_NUMERICHOST | NI_NUMERICSERV); - - if (status !=3D 0) - { - buf_printf(&out, "[nameinfo() err: %s]", gai_strerror(status)); - return BSTR(&out); - } - - if (!(flags & PS_DONT_SHOW_ADDR)) - { - if (addr_is_defined) - { - buf_puts(&out, hostaddr); - } - else - { - buf_puts(&out, "[undef]"); - } - } - - if ((flags & PS_SHOW_PORT) || (flags & PS_SHOW_PORT_IF_DEFINED)) - { - if (separator) - { - buf_puts(&out, separator); - } - - buf_puts(&out, servname); - } - - return BSTR(&out); -} - -const char * -print_link_socket_actual(const struct link_socket_actual *act, struct gc_a= rena *gc) -{ - return print_link_socket_actual_ex(act, ":", PS_SHOW_PORT | PS_SHOW_PK= TINFO, gc); -} - -#ifndef IF_NAMESIZE -#define IF_NAMESIZE 16 -#endif - -const char * -print_link_socket_actual_ex(const struct link_socket_actual *act, const ch= ar *separator, - const unsigned int flags, struct gc_arena *gc) -{ - if (act) - { - struct buffer out =3D alloc_buf_gc(128, gc); - buf_printf(&out, "%s", print_sockaddr_ex(&act->dest.addr.sa, separ= ator, flags, gc)); -#if ENABLE_IP_PKTINFO - char ifname[IF_NAMESIZE] =3D "[undef]"; - - if ((flags & PS_SHOW_PKTINFO) && addr_defined_ipi(act)) - { - switch (act->dest.addr.sa.sa_family) - { - case AF_INET: - { - struct openvpn_sockaddr sa; - CLEAR(sa); - sa.addr.in4.sin_family =3D AF_INET; -#if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST) - sa.addr.in4.sin_addr =3D act->pi.in4.ipi_spec_dst; - if_indextoname(act->pi.in4.ipi_ifindex, ifname); -#elif defined(IP_RECVDSTADDR) - sa.addr.in4.sin_addr =3D act->pi.in4; - ifname[0] =3D 0; -#else /* if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST) */ -#error ENABLE_IP_PKTINFO is set without IP_PKTINFO xor IP_RECVDSTADDR (fix= syshead.h) -#endif - buf_printf(&out, " (via %s%%%s)", - print_sockaddr_ex(&sa.addr.sa, separator, 0= , gc), ifname); - } - break; - - case AF_INET6: - { - struct sockaddr_in6 sin6; - char buf[INET6_ADDRSTRLEN] =3D "[undef]"; - CLEAR(sin6); - sin6.sin6_family =3D AF_INET6; - sin6.sin6_addr =3D act->pi.in6.ipi6_addr; - if_indextoname(act->pi.in6.ipi6_ifindex, ifname); - if (getnameinfo((struct sockaddr *)&sin6, sizeof(struc= t sockaddr_in6), buf, - sizeof(buf), NULL, 0, NI_NUMERICHOST) - =3D=3D 0) - { - buf_printf(&out, " (via %s%%%s)", buf, ifname); - } - else - { - buf_printf(&out, " (via [getnameinfo() err]%%%s)",= ifname); - } - } - break; - } - } -#endif /* if ENABLE_IP_PKTINFO */ - return BSTR(&out); - } - else - { - return "[NULL]"; - } -} - -/* - * Convert an in_addr_t in host byte order - * to an ascii dotted quad. - */ -const char * -print_in_addr_t(in_addr_t addr, unsigned int flags, struct gc_arena *gc) -{ - struct in_addr ia; - char *out =3D gc_malloc(INET_ADDRSTRLEN, true, gc); - - if (addr || !(flags & IA_EMPTY_IF_UNDEF)) - { - CLEAR(ia); - ia.s_addr =3D (flags & IA_NET_ORDER) ? addr : htonl(addr); - - inet_ntop(AF_INET, &ia, out, INET_ADDRSTRLEN); - } - return out; -} - -/* - * Convert an in6_addr in host byte order - * to an ascii representation of an IPv6 address - */ -const char * -print_in6_addr(struct in6_addr a6, unsigned int flags, struct gc_arena *gc) -{ - char *out =3D gc_malloc(INET6_ADDRSTRLEN, true, gc); - - if (memcmp(&a6, &in6addr_any, sizeof(a6)) !=3D 0 || !(flags & IA_EMPTY= _IF_UNDEF)) - { - inet_ntop(AF_INET6, &a6, out, INET6_ADDRSTRLEN); - } - return out; -} - -/* - * Convert an in_port_t in host byte order to a string - */ -const char * -print_in_port_t(in_port_t port, struct gc_arena *gc) -{ - struct buffer buffer =3D alloc_buf_gc(8, gc); - buf_printf(&buffer, "%hu", port); - return BSTR(&buffer); -} - -/* add some offset to an ipv6 address - * (add in steps of 8 bits, taking overflow into next round) - */ -struct in6_addr -add_in6_addr(struct in6_addr base, uint32_t add) -{ - int i; - - for (i =3D 15; i >=3D 0 && add > 0; i--) - { - register int carry; - register uint32_t h; - - h =3D (unsigned char)base.s6_addr[i]; - base.s6_addr[i] =3D (h + add) & UINT8_MAX; - - /* using explicit carry for the 8-bit additions will catch - * 8-bit and(!) 32-bit overruns nicely - */ - carry =3D ((h & 0xff) + (add & 0xff)) >> 8; - add =3D (add >> 8) + carry; - } - return base; -} - -/* set environmental variables for ip/port in *addr */ -void -setenv_sockaddr(struct env_set *es, const char *name_prefix, const struct = openvpn_sockaddr *addr, - const unsigned int flags) -{ - char name_buf[256]; - - char buf[INET6_ADDRSTRLEN]; - switch (addr->addr.sa.sa_family) - { - case AF_INET: - if (flags & SA_IP_PORT) - { - snprintf(name_buf, sizeof(name_buf), "%s_ip", name_prefix); - } - else - { - snprintf(name_buf, sizeof(name_buf), "%s", name_prefix); - } - - inet_ntop(AF_INET, &addr->addr.in4.sin_addr, buf, sizeof(buf)); - setenv_str(es, name_buf, buf); - - if ((flags & SA_IP_PORT) && addr->addr.in4.sin_port) - { - snprintf(name_buf, sizeof(name_buf), "%s_port", name_prefi= x); - setenv_int(es, name_buf, ntohs(addr->addr.in4.sin_port)); - } - break; - - case AF_INET6: - if (IN6_IS_ADDR_V4MAPPED(&addr->addr.in6.sin6_addr)) - { - struct in_addr ia; - memcpy(&ia.s_addr, &addr->addr.in6.sin6_addr.s6_addr[12], = sizeof(ia.s_addr)); - snprintf(name_buf, sizeof(name_buf), "%s_ip", name_prefix); - inet_ntop(AF_INET, &ia, buf, sizeof(buf)); - } - else - { - snprintf(name_buf, sizeof(name_buf), "%s_ip6", name_prefix= ); - inet_ntop(AF_INET6, &addr->addr.in6.sin6_addr, buf, sizeof= (buf)); - } - setenv_str(es, name_buf, buf); - - if ((flags & SA_IP_PORT) && addr->addr.in6.sin6_port) - { - snprintf(name_buf, sizeof(name_buf), "%s_port", name_prefi= x); - setenv_int(es, name_buf, ntohs(addr->addr.in6.sin6_port)); - } - break; - } -} - -void -setenv_in_addr_t(struct env_set *es, const char *name_prefix, in_addr_t ad= dr, - const unsigned int flags) -{ - if (addr || !(flags & SA_SET_IF_NONZERO)) - { - struct openvpn_sockaddr si; - CLEAR(si); - si.addr.in4.sin_family =3D AF_INET; - si.addr.in4.sin_addr.s_addr =3D htonl(addr); - setenv_sockaddr(es, name_prefix, &si, flags); - } -} - -void -setenv_in6_addr(struct env_set *es, const char *name_prefix, const struct = in6_addr *addr, - const unsigned int flags) -{ - if (!IN6_IS_ADDR_UNSPECIFIED(addr) || !(flags & SA_SET_IF_NONZERO)) - { - struct openvpn_sockaddr si; - CLEAR(si); - si.addr.in6.sin6_family =3D AF_INET6; - si.addr.in6.sin6_addr =3D *addr; - setenv_sockaddr(es, name_prefix, &si, flags); - } -} - -void -setenv_link_socket_actual(struct env_set *es, const char *name_prefix, - const struct link_socket_actual *act, const unsi= gned int flags) -{ - setenv_sockaddr(es, name_prefix, &act->dest, flags); -} - -/* - * Convert protocol names between index and ascii form. - */ - -struct proto_names -{ - const char *short_form; - const char *display_form; - sa_family_t proto_af; - int proto; -}; - -/* Indexed by PROTO_x */ -static const struct proto_names proto_names[] =3D { - { "proto-uninitialized", "proto-NONE", AF_UNSPEC, PROTO_NONE }, - /* try IPv4 and IPv6 (client), bind dual-stack (server) */ - { "udp", "UDP", AF_UNSPEC, PROTO_UDP }, - { "tcp-server", "TCP_SERVER", AF_UNSPEC, PROTO_TCP_SERVER }, - { "tcp-client", "TCP_CLIENT", AF_UNSPEC, PROTO_TCP_CLIENT }, - { "tcp", "TCP", AF_UNSPEC, PROTO_TCP }, - /* force IPv4 */ - { "udp4", "UDPv4", AF_INET, PROTO_UDP }, - { "tcp4-server", "TCPv4_SERVER", AF_INET, PROTO_TCP_SERVER }, - { "tcp4-client", "TCPv4_CLIENT", AF_INET, PROTO_TCP_CLIENT }, - { "tcp4", "TCPv4", AF_INET, PROTO_TCP }, - /* force IPv6 */ - { "udp6", "UDPv6", AF_INET6, PROTO_UDP }, - { "tcp6-server", "TCPv6_SERVER", AF_INET6, PROTO_TCP_SERVER }, - { "tcp6-client", "TCPv6_CLIENT", AF_INET6, PROTO_TCP_CLIENT }, - { "tcp6", "TCPv6", AF_INET6, PROTO_TCP }, -}; - -int -ascii2proto(const char *proto_name) -{ - for (size_t i =3D 0; i < SIZE(proto_names); ++i) - { - if (!strcmp(proto_name, proto_names[i].short_form)) - { - return proto_names[i].proto; - } - } - return -1; -} - -sa_family_t -ascii2af(const char *proto_name) -{ - for (size_t i =3D 0; i < SIZE(proto_names); ++i) - { - if (!strcmp(proto_name, proto_names[i].short_form)) - { - return proto_names[i].proto_af; - } - } - return 0; -} - -const char * -proto2ascii(int proto, sa_family_t af, bool display_form) -{ - for (size_t i =3D 0; i < SIZE(proto_names); ++i) - { - if (proto_names[i].proto_af =3D=3D af && proto_names[i].proto =3D= =3D proto) - { - if (display_form) - { - return proto_names[i].display_form; - } - else - { - return proto_names[i].short_form; - } - } - } - - return "[unknown protocol]"; -} - -const char * -proto2ascii_all(struct gc_arena *gc) -{ - struct buffer out =3D alloc_buf_gc(256, gc); - - for (size_t i =3D 0; i < SIZE(proto_names); ++i) - { - if (i) - { - buf_printf(&out, " "); - } - buf_printf(&out, "[%s]", proto_names[i].short_form); - } - return BSTR(&out); -} - -const char * -addr_family_name(int af) -{ - switch (af) - { - case AF_INET: - return "AF_INET"; - - case AF_INET6: - return "AF_INET6"; - } - return "AF_UNSPEC"; -} - -/* - * Given a local proto, return local proto - * if !remote, or compatible remote proto - * if remote. - * - * This is used for options compatibility - * checking. - * - * IPv6 and IPv4 protocols are comptabile but OpenVPN - * has always sent UDPv4, TCPv4 over the wire. Keep these - * strings for backward compatibility - */ -const char * -proto_remote(int proto, bool remote) -{ - ASSERT(proto >=3D 0 && proto < PROTO_N); - if (proto =3D=3D PROTO_UDP) - { - return "UDPv4"; - } - - if ((remote && proto =3D=3D PROTO_TCP_CLIENT) || (!remote && proto =3D= =3D PROTO_TCP_SERVER)) - { - return "TCPv4_SERVER"; - } - if ((remote && proto =3D=3D PROTO_TCP_SERVER) || (!remote && proto =3D= =3D PROTO_TCP_CLIENT)) - { - return "TCPv4_CLIENT"; - } - - ASSERT(0); - return ""; /* Make the compiler happy */ -} /* * Bad incoming address lengths that differ from what diff --git a/src/openvpn/socket.h b/src/openvpn/socket.h index ed37590..cce9183 100644 --- a/src/openvpn/socket.h +++ b/src/openvpn/socket.h @@ -34,6 +34,7 @@ #include "socks.h" #include "misc.h" #include "tun.h" +#include "socket_util.h" /* * OpenVPN's default port number as assigned by IANA. @@ -60,18 +61,6 @@ /* convert a packet_size_type from network to host order */ #define ntohps(x) ntohs(x) -/* OpenVPN sockaddr struct */ -struct openvpn_sockaddr -{ - /*int dummy;*/ /* add offset to force a bug if sa not explicitly deref= erenced */ - union - { - struct sockaddr sa; - struct sockaddr_in in4; - struct sockaddr_in6 in6; - } addr; -}; - /* struct to hold preresolved host names */ struct cached_dns_entry { @@ -83,25 +72,6 @@ struct cached_dns_entry *next; }; =20 -/* actual address of remote, based on source address of received packets */ -struct link_socket_actual -{ - /*int dummy;*/ /* add offset to force a bug if dest not explicitly der= eferenced */ - - struct openvpn_sockaddr dest; -#if ENABLE_IP_PKTINFO - union - { -#if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST) - struct in_pktinfo in4; -#elif defined(IP_RECVDSTADDR) - struct in_addr in4; -#endif - struct in6_pktinfo in6; - } pi; -#endif -}; - /* IP addresses which are persistent across SIGUSR1s */ struct link_socket_addr { @@ -349,58 +319,6 @@ void sd_close(socket_descriptor_t *sd); -#define PS_SHOW_PORT_IF_DEFINED (1 << 0) -#define PS_SHOW_PORT (1 << 1) -#define PS_SHOW_PKTINFO (1 << 2) -#define PS_DONT_SHOW_ADDR (1 << 3) -#define PS_DONT_SHOW_FAMILY (1 << 4) - -const char *print_sockaddr_ex(const struct sockaddr *addr, const char *sep= arator, - const unsigned int flags, struct gc_arena *g= c); - -static inline const char * -print_openvpn_sockaddr(const struct openvpn_sockaddr *addr, struct gc_aren= a *gc) -{ - return print_sockaddr_ex(&addr->addr.sa, ":", PS_SHOW_PORT, gc); -} - -static inline const char * -print_sockaddr(const struct sockaddr *addr, struct gc_arena *gc) -{ - return print_sockaddr_ex(addr, ":", PS_SHOW_PORT, gc); -} - - -const char *print_link_socket_actual_ex(const struct link_socket_actual *a= ct, const char *separator, - const unsigned int flags, struct g= c_arena *gc); - -const char *print_link_socket_actual(const struct link_socket_actual *act,= struct gc_arena *gc); - - -#define IA_EMPTY_IF_UNDEF (1 << 0) -#define IA_NET_ORDER (1 << 1) -const char *print_in_addr_t(in_addr_t addr, unsigned int flags, struct gc_= arena *gc); - -const char *print_in6_addr(struct in6_addr addr6, unsigned int flags, stru= ct gc_arena *gc); - -const char *print_in_port_t(in_port_t port, struct gc_arena *gc); - -struct in6_addr add_in6_addr(struct in6_addr base, uint32_t add); - -#define SA_IP_PORT (1 << 0) -#define SA_SET_IF_NONZERO (1 << 1) -void setenv_sockaddr(struct env_set *es, const char *name_prefix, - const struct openvpn_sockaddr *addr, const unsigned i= nt flags); - -void setenv_in_addr_t(struct env_set *es, const char *name_prefix, in_addr= _t addr, - const unsigned int flags); - -void setenv_in6_addr(struct env_set *es, const char *name_prefix, const st= ruct in6_addr *addr, - const unsigned int flags); - -void setenv_link_socket_actual(struct env_set *es, const char *name_prefix, - const struct link_socket_actual *act, const= unsigned int flags); - void bad_address_length(int actual, int expected); /* IPV4_INVALID_ADDR: returned by link_socket_current_remote() @@ -432,21 +350,6 @@ * Low-level functions */ -/* return values of openvpn_inet_aton */ -#define OIA_HOSTNAME 0 -#define OIA_IP 1 -#define OIA_ERROR -1 -int openvpn_inet_aton(const char *dotted_quad, struct in_addr *addr); - -/* integrity validation on pulled options */ -bool ip_addr_dotted_quad_safe(const char *dotted_quad); - -bool ip_or_dns_addr_safe(const char *addr, const bool allow_fqdn); - -bool mac_addr_safe(const char *mac_addr); - -bool ipv6_addr_safe(const char *ipv6_text_addr); - socket_descriptor_t create_socket_tcp(struct addrinfo *); socket_descriptor_t socket_do_accept(socket_descriptor_t sd, struct link_s= ocket_actual *act, @@ -472,134 +375,6 @@ #endif /* if UNIX_SOCK_SUPPORT */ -/* - * DNS resolution - */ - -#define GETADDR_RESOLVE (1 << 0) -#define GETADDR_FATAL (1 << 1) -#define GETADDR_HOST_ORDER (1 << 2) -#define GETADDR_MENTION_RESOLVE_RETRY (1 << 3) -#define GETADDR_FATAL_ON_SIGNAL (1 << 4) -#define GETADDR_WARN_ON_SIGNAL (1 << 5) -#define GETADDR_MSG_VIRT_OUT (1 << 6) -#define GETADDR_TRY_ONCE (1 << 7) -#define GETADDR_UPDATE_MANAGEMENT_STATE (1 << 8) -#define GETADDR_RANDOMIZE (1 << 9) -#define GETADDR_PASSIVE (1 << 10) -#define GETADDR_DATAGRAM (1 << 11) - -#define GETADDR_CACHE_MASK (GETADDR_DATAGRAM | GETADDR_PASSIVE) - -/** - * Translate an IPv4 addr or hostname from string form to in_addr_t - * - * In case of resolve error, it will try again for - * resolve_retry_seconds seconds. - */ -in_addr_t getaddr(unsigned int flags, const char *hostname, int resolve_re= try_seconds, - bool *succeeded, struct signal_info *sig_info); - -/** - * Translate an IPv6 addr or hostname from string form to in6_addr - */ -bool get_ipv6_addr(const char *hostname, struct in6_addr *network, unsigne= d int *netbits, - int msglevel); - -int openvpn_getaddrinfo(unsigned int flags, const char *hostname, const ch= ar *servname, - int resolve_retry_seconds, struct signal_info *sig= _info, int ai_family, - struct addrinfo **res); - -/* - * Transport protocol naming and other details. - */ - -/* - * Use enum's instead of #define to allow for easier - * optional proto support - */ -enum proto_num -{ - PROTO_NONE, /* catch for uninitialized */ - PROTO_UDP, - PROTO_TCP, - PROTO_TCP_SERVER, - PROTO_TCP_CLIENT, - PROTO_N -}; - -static inline bool -proto_is_net(int proto) -{ - ASSERT(proto >=3D 0 && proto < PROTO_N); - return proto !=3D PROTO_NONE; -} - -/** - * @brief Returns if the protocol being used is UDP - */ -static inline bool -proto_is_udp(int proto) -{ - ASSERT(proto >=3D 0 && proto < PROTO_N); - return proto =3D=3D PROTO_UDP; -} - -/** - * @brief Return if the protocol is datagram (UDP) - * - */ -static inline bool -proto_is_dgram(int proto) -{ - return proto_is_udp(proto); -} - -/** - * @brief returns if the proto is a TCP variant (tcp-server, tcp-client or= tcp) - */ -static inline bool -proto_is_tcp(int proto) -{ - ASSERT(proto >=3D 0 && proto < PROTO_N); - return proto =3D=3D PROTO_TCP_CLIENT || proto =3D=3D PROTO_TCP_SERVER; -} - - -int ascii2proto(const char *proto_name); - -sa_family_t ascii2af(const char *proto_name); - -const char *proto2ascii(int proto, sa_family_t af, bool display_form); - -const char *proto2ascii_all(struct gc_arena *gc); - -const char *proto_remote(int proto, bool remote); - -const char *addr_family_name(int af); - -/* - * Overhead added to packets by various protocols. - */ -static inline int -datagram_overhead(sa_family_t af, int proto) -{ - int overhead =3D 0; - overhead +=3D (proto =3D=3D PROTO_UDP) ? 8 : 20; - overhead +=3D (af =3D=3D AF_INET) ? 20 : 40; - return overhead; -} - -/* - * Misc inline functions - */ - -static inline bool -link_socket_proto_connection_oriented(int proto) -{ - return !proto_is_dgram(proto); -} - static inline bool link_socket_connection_oriented(const struct link_socket *sock) { @@ -613,242 +388,6 @@ } } -static inline bool -addr_defined(const struct openvpn_sockaddr *addr) -{ - if (!addr) - { - return 0; - } - switch (addr->addr.sa.sa_family) - { - case AF_INET: - return addr->addr.in4.sin_addr.s_addr !=3D 0; - - case AF_INET6: - return !IN6_IS_ADDR_UNSPECIFIED(&addr->addr.in6.sin6_addr); - - default: - return 0; - } -} - -static inline bool -addr_local(const struct sockaddr *addr) -{ - if (!addr) - { - return false; - } - switch (addr->sa_family) - { - case AF_INET: - return ((const struct sockaddr_in *)addr)->sin_addr.s_addr =3D= =3D htonl(INADDR_LOOPBACK); - - case AF_INET6: - return IN6_IS_ADDR_LOOPBACK(&((const struct sockaddr_in6 *)add= r)->sin6_addr); - - default: - return false; - } -} - - -static inline bool -addr_defined_ipi(const struct link_socket_actual *lsa) -{ -#if ENABLE_IP_PKTINFO - if (!lsa) - { - return 0; - } - switch (lsa->dest.addr.sa.sa_family) - { -#if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST) - case AF_INET: - return lsa->pi.in4.ipi_spec_dst.s_addr !=3D 0; - -#elif defined(IP_RECVDSTADDR) - case AF_INET: - return lsa->pi.in4.s_addr !=3D 0; - -#endif - case AF_INET6: - return !IN6_IS_ADDR_UNSPECIFIED(&lsa->pi.in6.ipi6_addr); - - default: - return 0; - } -#else /* if ENABLE_IP_PKTINFO */ - ASSERT(0); -#endif - return false; -} - -static inline bool -link_socket_actual_defined(const struct link_socket_actual *act) -{ - return act && addr_defined(&act->dest); -} - -static inline bool -addr_match(const struct openvpn_sockaddr *a1, const struct openvpn_sockadd= r *a2) -{ - switch (a1->addr.sa.sa_family) - { - case AF_INET: - return a1->addr.in4.sin_addr.s_addr =3D=3D a2->addr.in4.sin_ad= dr.s_addr; - - case AF_INET6: - return IN6_ARE_ADDR_EQUAL(&a1->addr.in6.sin6_addr, &a2->addr.i= n6.sin6_addr); - } - ASSERT(0); - return false; -} - -static inline bool -addrlist_match(const struct openvpn_sockaddr *a1, const struct addrinfo *a= ddrlist) -{ - const struct addrinfo *curele; - for (curele =3D addrlist; curele; curele =3D curele->ai_next) - { - switch (a1->addr.sa.sa_family) - { - case AF_INET: - if (a1->addr.in4.sin_addr.s_addr - =3D=3D ((struct sockaddr_in *)curele->ai_addr)->sin_ad= dr.s_addr) - { - return true; - } - break; - - case AF_INET6: - if (IN6_ARE_ADDR_EQUAL(&a1->addr.in6.sin6_addr, - &((struct sockaddr_in6 *)curele->ai= _addr)->sin6_addr)) - { - return true; - } - break; - - default: - ASSERT(0); - } - } - return false; -} - -static inline bool -addrlist_port_match(const struct openvpn_sockaddr *a1, const struct addrin= fo *a2) -{ - const struct addrinfo *curele; - for (curele =3D a2; curele; curele =3D curele->ai_next) - { - switch (a1->addr.sa.sa_family) - { - case AF_INET: - if (curele->ai_family =3D=3D AF_INET - && a1->addr.in4.sin_addr.s_addr - =3D=3D ((struct sockaddr_in *)curele->ai_addr)-= >sin_addr.s_addr - && a1->addr.in4.sin_port =3D=3D ((struct sockaddr_in *= )curele->ai_addr)->sin_port) - { - return true; - } - break; - - case AF_INET6: - if (curele->ai_family =3D=3D AF_INET6 - && IN6_ARE_ADDR_EQUAL(&a1->addr.in6.sin6_addr, - &((struct sockaddr_in6 *)curele-= >ai_addr)->sin6_addr) - && a1->addr.in6.sin6_port - =3D=3D ((struct sockaddr_in6 *)curele->ai_addr)= ->sin6_port) - { - return true; - } - break; - - default: - ASSERT(0); - } - } - return false; -} - - -static inline bool -addr_port_match(const struct openvpn_sockaddr *a1, const struct openvpn_so= ckaddr *a2) -{ - switch (a1->addr.sa.sa_family) - { - case AF_INET: - return a1->addr.in4.sin_addr.s_addr =3D=3D a2->addr.in4.sin_ad= dr.s_addr - && a1->addr.in4.sin_port =3D=3D a2->addr.in4.sin_port; - - case AF_INET6: - return IN6_ARE_ADDR_EQUAL(&a1->addr.in6.sin6_addr, &a2->addr.i= n6.sin6_addr) - && a1->addr.in6.sin6_port =3D=3D a2->addr.in6.sin6_port; - } - ASSERT(0); - return false; -} - -static inline bool -addr_match_proto(const struct openvpn_sockaddr *a1, const struct openvpn_s= ockaddr *a2, - const int proto) -{ - return link_socket_proto_connection_oriented(proto) ? addr_match(a1, a= 2) - : addr_port_match(= a1, a2); -} - - -static inline bool -addrlist_match_proto(const struct openvpn_sockaddr *a1, struct addrinfo *a= ddr_list, const int proto) -{ - return link_socket_proto_connection_oriented(proto) ? addrlist_match(a= 1, addr_list) - : addrlist_port_ma= tch(a1, addr_list); -} - -static inline void -addr_zero_host(struct openvpn_sockaddr *addr) -{ - switch (addr->addr.sa.sa_family) - { - case AF_INET: - addr->addr.in4.sin_addr.s_addr =3D 0; - break; - - case AF_INET6: - memset(&addr->addr.in6.sin6_addr, 0, sizeof(struct in6_addr)); - break; - } -} - -static inline int -af_addr_size(sa_family_t af) -{ - switch (af) - { - case AF_INET: - return sizeof(struct sockaddr_in); - - case AF_INET6: - return sizeof(struct sockaddr_in6); - - default: -#if 0 - /* could be called from socket_do_accept() with empty addr */ - msg(M_ERR, "Bad address family: %d\n", af); - ASSERT(0); -#endif - return 0; - } -} - -static inline bool -link_socket_actual_match(const struct link_socket_actual *a1, const struct= link_socket_actual *a2) -{ - return addr_port_match(&a1->dest, &a2->dest); -} - #if PORT_SHARE static inline bool diff --git a/src/openvpn/socket_util.c b/src/openvpn/socket_util.c new file mode 100644 index 0000000..13f8c32 --- /dev/null +++ b/src/openvpn/socket_util.c @@ -0,0 +1,936 @@ +/* + * OpenVPN -- An application to securely tunnel IP networks + * over a single TCP/UDP port, with support for SSL/TLS-based + * session authentication and key exchange, + * packet encryption, packet authentication, and + * packet compression. + * + * Copyright (C) 2002-2025 OpenVPN Inc + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, see . + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "syshead.h" + +#include "socket_util.h" +#include "crypto.h" +#include "manage.h" + +/* + * Format IP addresses in ascii + */ + +const char * +print_sockaddr_ex(const struct sockaddr *sa, const char *separator, const = unsigned int flags, + struct gc_arena *gc) +{ + struct buffer out =3D alloc_buf_gc(128, gc); + bool addr_is_defined =3D false; + char hostaddr[NI_MAXHOST] =3D ""; + char servname[NI_MAXSERV] =3D ""; + int status; + + socklen_t salen =3D 0; + switch (sa->sa_family) + { + case AF_INET: + if (!(flags & PS_DONT_SHOW_FAMILY)) + { + buf_puts(&out, "[AF_INET]"); + } + salen =3D sizeof(struct sockaddr_in); + addr_is_defined =3D ((struct sockaddr_in *)sa)->sin_addr.s_add= r !=3D 0; + break; + + case AF_INET6: + if (!(flags & PS_DONT_SHOW_FAMILY)) + { + buf_puts(&out, "[AF_INET6]"); + } + salen =3D sizeof(struct sockaddr_in6); + addr_is_defined =3D !IN6_IS_ADDR_UNSPECIFIED(&((struct sockadd= r_in6 *)sa)->sin6_addr); + break; + + case AF_UNSPEC: + if (!(flags & PS_DONT_SHOW_FAMILY)) + { + return "[AF_UNSPEC]"; + } + else + { + return ""; + } + + default: + ASSERT(0); + } + + status =3D getnameinfo(sa, salen, hostaddr, sizeof(hostaddr), servname= , sizeof(servname), + NI_NUMERICHOST | NI_NUMERICSERV); + + if (status !=3D 0) + { + buf_printf(&out, "[nameinfo() err: %s]", gai_strerror(status)); + return BSTR(&out); + } + + if (!(flags & PS_DONT_SHOW_ADDR)) + { + if (addr_is_defined) + { + buf_puts(&out, hostaddr); + } + else + { + buf_puts(&out, "[undef]"); + } + } + + if ((flags & PS_SHOW_PORT) || (flags & PS_SHOW_PORT_IF_DEFINED)) + { + if (separator) + { + buf_puts(&out, separator); + } + + buf_puts(&out, servname); + } + + return BSTR(&out); +} + +const char * +print_link_socket_actual(const struct link_socket_actual *act, struct gc_a= rena *gc) +{ + return print_link_socket_actual_ex(act, ":", PS_SHOW_PORT | PS_SHOW_PK= TINFO, gc); +} + +#ifndef IF_NAMESIZE +#define IF_NAMESIZE 16 +#endif + +const char * +print_link_socket_actual_ex(const struct link_socket_actual *act, const ch= ar *separator, + const unsigned int flags, struct gc_arena *gc) +{ + if (act) + { + struct buffer out =3D alloc_buf_gc(128, gc); + buf_printf(&out, "%s", print_sockaddr_ex(&act->dest.addr.sa, separ= ator, flags, gc)); +#if ENABLE_IP_PKTINFO + char ifname[IF_NAMESIZE] =3D "[undef]"; + + if ((flags & PS_SHOW_PKTINFO) && addr_defined_ipi(act)) + { + switch (act->dest.addr.sa.sa_family) + { + case AF_INET: + { + struct openvpn_sockaddr sa; + CLEAR(sa); + sa.addr.in4.sin_family =3D AF_INET; +#if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST) + sa.addr.in4.sin_addr =3D act->pi.in4.ipi_spec_dst; + if_indextoname(act->pi.in4.ipi_ifindex, ifname); +#elif defined(IP_RECVDSTADDR) + sa.addr.in4.sin_addr =3D act->pi.in4; + ifname[0] =3D 0; +#else /* if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST) */ +#error ENABLE_IP_PKTINFO is set without IP_PKTINFO xor IP_RECVDSTADDR (fix= syshead.h) +#endif + buf_printf(&out, " (via %s%%%s)", + print_sockaddr_ex(&sa.addr.sa, separator, 0= , gc), ifname); + } + break; + + case AF_INET6: + { + struct sockaddr_in6 sin6; + char buf[INET6_ADDRSTRLEN] =3D "[undef]"; + CLEAR(sin6); + sin6.sin6_family =3D AF_INET6; + sin6.sin6_addr =3D act->pi.in6.ipi6_addr; + if_indextoname(act->pi.in6.ipi6_ifindex, ifname); + if (getnameinfo((struct sockaddr *)&sin6, sizeof(struc= t sockaddr_in6), buf, + sizeof(buf), NULL, 0, NI_NUMERICHOST) + =3D=3D 0) + { + buf_printf(&out, " (via %s%%%s)", buf, ifname); + } + else + { + buf_printf(&out, " (via [getnameinfo() err]%%%s)",= ifname); + } + } + break; + } + } +#endif /* if ENABLE_IP_PKTINFO */ + return BSTR(&out); + } + else + { + return "[NULL]"; + } +} + +/* + * Convert an in_addr_t in host byte order + * to an ascii dotted quad. + */ +const char * +print_in_addr_t(in_addr_t addr, unsigned int flags, struct gc_arena *gc) +{ + struct in_addr ia; + char *out =3D gc_malloc(INET_ADDRSTRLEN, true, gc); + + if (addr || !(flags & IA_EMPTY_IF_UNDEF)) + { + CLEAR(ia); + ia.s_addr =3D (flags & IA_NET_ORDER) ? addr : htonl(addr); + + inet_ntop(AF_INET, &ia, out, INET_ADDRSTRLEN); + } + return out; +} + +/* + * Convert an in6_addr in host byte order + * to an ascii representation of an IPv6 address + */ +const char * +print_in6_addr(struct in6_addr a6, unsigned int flags, struct gc_arena *gc) +{ + char *out =3D gc_malloc(INET6_ADDRSTRLEN, true, gc); + + if (memcmp(&a6, &in6addr_any, sizeof(a6)) !=3D 0 || !(flags & IA_EMPTY= _IF_UNDEF)) + { + inet_ntop(AF_INET6, &a6, out, INET6_ADDRSTRLEN); + } + return out; +} + +/* + * Convert an in_port_t in host byte order to a string + */ +const char * +print_in_port_t(in_port_t port, struct gc_arena *gc) +{ + struct buffer buffer =3D alloc_buf_gc(8, gc); + buf_printf(&buffer, "%hu", port); + return BSTR(&buffer); +} + +/* add some offset to an ipv6 address + * (add in steps of 8 bits, taking overflow into next round) + */ +struct in6_addr +add_in6_addr(struct in6_addr base, uint32_t add) +{ + int i; + + for (i =3D 15; i >=3D 0 && add > 0; i--) + { + register int carry; + register uint32_t h; + + h =3D (unsigned char)base.s6_addr[i]; + base.s6_addr[i] =3D (h + add) & UINT8_MAX; + + /* using explicit carry for the 8-bit additions will catch + * 8-bit and(!) 32-bit overruns nicely + */ + carry =3D ((h & 0xff) + (add & 0xff)) >> 8; + add =3D (add >> 8) + carry; + } + return base; +} + +/* set environmental variables for ip/port in *addr */ +void +setenv_sockaddr(struct env_set *es, const char *name_prefix, const struct = openvpn_sockaddr *addr, + const unsigned int flags) +{ + char name_buf[256]; + + char buf[INET6_ADDRSTRLEN]; + switch (addr->addr.sa.sa_family) + { + case AF_INET: + if (flags & SA_IP_PORT) + { + snprintf(name_buf, sizeof(name_buf), "%s_ip", name_prefix); + } + else + { + snprintf(name_buf, sizeof(name_buf), "%s", name_prefix); + } + + inet_ntop(AF_INET, &addr->addr.in4.sin_addr, buf, sizeof(buf)); + setenv_str(es, name_buf, buf); + + if ((flags & SA_IP_PORT) && addr->addr.in4.sin_port) + { + snprintf(name_buf, sizeof(name_buf), "%s_port", name_prefi= x); + setenv_int(es, name_buf, ntohs(addr->addr.in4.sin_port)); + } + break; + + case AF_INET6: + if (IN6_IS_ADDR_V4MAPPED(&addr->addr.in6.sin6_addr)) + { + struct in_addr ia; + memcpy(&ia.s_addr, &addr->addr.in6.sin6_addr.s6_addr[12], = sizeof(ia.s_addr)); + snprintf(name_buf, sizeof(name_buf), "%s_ip", name_prefix); + inet_ntop(AF_INET, &ia, buf, sizeof(buf)); + } + else + { + snprintf(name_buf, sizeof(name_buf), "%s_ip6", name_prefix= ); + inet_ntop(AF_INET6, &addr->addr.in6.sin6_addr, buf, sizeof= (buf)); + } + setenv_str(es, name_buf, buf); + + if ((flags & SA_IP_PORT) && addr->addr.in6.sin6_port) + { + snprintf(name_buf, sizeof(name_buf), "%s_port", name_prefi= x); + setenv_int(es, name_buf, ntohs(addr->addr.in6.sin6_port)); + } + break; + } +} + +void +setenv_in_addr_t(struct env_set *es, const char *name_prefix, in_addr_t ad= dr, + const unsigned int flags) +{ + if (addr || !(flags & SA_SET_IF_NONZERO)) + { + struct openvpn_sockaddr si; + CLEAR(si); + si.addr.in4.sin_family =3D AF_INET; + si.addr.in4.sin_addr.s_addr =3D htonl(addr); + setenv_sockaddr(es, name_prefix, &si, flags); + } +} + +void +setenv_in6_addr(struct env_set *es, const char *name_prefix, const struct = in6_addr *addr, + const unsigned int flags) +{ + if (!IN6_IS_ADDR_UNSPECIFIED(addr) || !(flags & SA_SET_IF_NONZERO)) + { + struct openvpn_sockaddr si; + CLEAR(si); + si.addr.in6.sin6_family =3D AF_INET6; + si.addr.in6.sin6_addr =3D *addr; + setenv_sockaddr(es, name_prefix, &si, flags); + } +} + +void +setenv_link_socket_actual(struct env_set *es, const char *name_prefix, + const struct link_socket_actual *act, const unsi= gned int flags) +{ + setenv_sockaddr(es, name_prefix, &act->dest, flags); +} + +/* + * Convert protocol names between index and ascii form. + */ + +struct proto_names +{ + const char *short_form; + const char *display_form; + sa_family_t proto_af; + int proto; +}; + +/* Indexed by PROTO_x */ +static const struct proto_names proto_names[] =3D { + { "proto-uninitialized", "proto-NONE", AF_UNSPEC, PROTO_NONE }, + /* try IPv4 and IPv6 (client), bind dual-stack (server) */ + { "udp", "UDP", AF_UNSPEC, PROTO_UDP }, + { "tcp-server", "TCP_SERVER", AF_UNSPEC, PROTO_TCP_SERVER }, + { "tcp-client", "TCP_CLIENT", AF_UNSPEC, PROTO_TCP_CLIENT }, + { "tcp", "TCP", AF_UNSPEC, PROTO_TCP }, + /* force IPv4 */ + { "udp4", "UDPv4", AF_INET, PROTO_UDP }, + { "tcp4-server", "TCPv4_SERVER", AF_INET, PROTO_TCP_SERVER }, + { "tcp4-client", "TCPv4_CLIENT", AF_INET, PROTO_TCP_CLIENT }, + { "tcp4", "TCPv4", AF_INET, PROTO_TCP }, + /* force IPv6 */ + { "udp6", "UDPv6", AF_INET6, PROTO_UDP }, + { "tcp6-server", "TCPv6_SERVER", AF_INET6, PROTO_TCP_SERVER }, + { "tcp6-client", "TCPv6_CLIENT", AF_INET6, PROTO_TCP_CLIENT }, + { "tcp6", "TCPv6", AF_INET6, PROTO_TCP }, +}; + +int +ascii2proto(const char *proto_name) +{ + for (size_t i =3D 0; i < SIZE(proto_names); ++i) + { + if (!strcmp(proto_name, proto_names[i].short_form)) + { + return proto_names[i].proto; + } + } + return -1; +} + +sa_family_t +ascii2af(const char *proto_name) +{ + for (size_t i =3D 0; i < SIZE(proto_names); ++i) + { + if (!strcmp(proto_name, proto_names[i].short_form)) + { + return proto_names[i].proto_af; + } + } + return 0; +} + +const char * +proto2ascii(int proto, sa_family_t af, bool display_form) +{ + for (size_t i =3D 0; i < SIZE(proto_names); ++i) + { + if (proto_names[i].proto_af =3D=3D af && proto_names[i].proto =3D= =3D proto) + { + if (display_form) + { + return proto_names[i].display_form; + } + else + { + return proto_names[i].short_form; + } + } + } + + return "[unknown protocol]"; +} + +const char * +proto2ascii_all(struct gc_arena *gc) +{ + struct buffer out =3D alloc_buf_gc(256, gc); + + for (size_t i =3D 0; i < SIZE(proto_names); ++i) + { + if (i) + { + buf_printf(&out, " "); + } + buf_printf(&out, "[%s]", proto_names[i].short_form); + } + return BSTR(&out); +} + +const char * +addr_family_name(int af) +{ + switch (af) + { + case AF_INET: + return "AF_INET"; + + case AF_INET6: + return "AF_INET6"; + } + return "AF_UNSPEC"; +} + +/* + * Given a local proto, return local proto + * if !remote, or compatible remote proto + * if remote. + * + * This is used for options compatibility + * checking. + * + * IPv6 and IPv4 protocols are comptabile but OpenVPN + * has always sent UDPv4, TCPv4 over the wire. Keep these + * strings for backward compatibility + */ +const char * +proto_remote(int proto, bool remote) +{ + ASSERT(proto >=3D 0 && proto < PROTO_N); + if (proto =3D=3D PROTO_UDP) + { + return "UDPv4"; + } + + if ((remote && proto =3D=3D PROTO_TCP_CLIENT) || (!remote && proto =3D= =3D PROTO_TCP_SERVER)) + { + return "TCPv4_SERVER"; + } + if ((remote && proto =3D=3D PROTO_TCP_SERVER) || (!remote && proto =3D= =3D PROTO_TCP_CLIENT)) + { + return "TCPv4_CLIENT"; + } + + ASSERT(0); + return ""; /* Make the compiler happy */ +} + +/** + * Small helper function for openvpn_getaddrinfo to print the address + * family when resolving fails + */ +static const char * +getaddrinfo_addr_family_name(int af) +{ + switch (af) + { + case AF_INET: + return "[AF_INET]"; + + case AF_INET6: + return "[AF_INET6]"; + } + return ""; +} + +/* + * Prepend a random string to hostname to prevent DNS caching. + * For example, foo.bar.gov would be modified to .foo.bar.go= v. + * Of course, this requires explicit support in the DNS server (wildcard). + */ +static const char * +hostname_randomize(const char *hostname, struct gc_arena *gc) +{ +#define n_rnd_bytes 6 + + uint8_t rnd_bytes[n_rnd_bytes]; + const char *rnd_str; + struct buffer hname =3D alloc_buf_gc(strlen(hostname) + sizeof(rnd_byt= es) * 2 + 4, gc); + + prng_bytes(rnd_bytes, sizeof(rnd_bytes)); + rnd_str =3D format_hex_ex(rnd_bytes, sizeof(rnd_bytes), 40, 0, NULL, g= c); + buf_printf(&hname, "%s.%s", rnd_str, hostname); + return BSTR(&hname); +#undef n_rnd_bytes +} + +/* + * Translate IPv4/IPv6 addr or hostname into struct addrinfo + * If resolve error, try again for resolve_retry_seconds seconds. + */ +int +openvpn_getaddrinfo(unsigned int flags, const char *hostname, const char *= servname, + int resolve_retry_seconds, struct signal_info *sig_inf= o, int ai_family, + struct addrinfo **res) +{ + struct addrinfo hints; + int status; + struct signal_info sigrec =3D { 0 }; + int msglevel =3D (flags & GETADDR_FATAL) ? M_FATAL : D_RESOLVE_ERRORS; + struct gc_arena gc =3D gc_new(); + const char *print_hostname; + const char *print_servname; + + ASSERT(res); + + ASSERT(hostname || servname); + ASSERT(!(flags & GETADDR_HOST_ORDER)); + + if (servname) + { + print_servname =3D servname; + } + else + { + print_servname =3D ""; + } + + if (flags & GETADDR_MSG_VIRT_OUT) + { + msglevel |=3D M_MSG_VIRT_OUT; + } + + if ((flags & (GETADDR_FATAL_ON_SIGNAL | GETADDR_WARN_ON_SIGNAL)) && !s= ig_info) + { + sig_info =3D &sigrec; + } + + /* try numeric ip addr first */ + CLEAR(hints); + hints.ai_flags =3D AI_NUMERICHOST; + + if (flags & GETADDR_PASSIVE) + { + hints.ai_flags |=3D AI_PASSIVE; + } + + if (flags & GETADDR_DATAGRAM) + { + hints.ai_socktype =3D SOCK_DGRAM; + } + else + { + hints.ai_socktype =3D SOCK_STREAM; + } + + /* if hostname is not set, we want to bind to 'ANY', with + * the correct address family - v4-only or v6/v6-dual-stack */ + if (!hostname) + { + hints.ai_family =3D ai_family; + } + + status =3D getaddrinfo(hostname, servname, &hints, res); + + if (status !=3D 0) /* parse as numeric address fa= iled? */ + { + const int fail_wait_interval =3D 5; /* seconds */ + /* Add +4 to cause integer division rounding up (1 + 4) =3D 5, (0+= 4)/5=3D0 */ + int resolve_retries =3D + (flags & GETADDR_TRY_ONCE) ? 1 : ((resolve_retry_seconds + 4) = / fail_wait_interval); + const char *fmt; + int level =3D 0; + + /* this is not a numeric IP, therefore force resolution using the + * provided ai_family */ + hints.ai_family =3D ai_family; + + if (hostname && (flags & GETADDR_RANDOMIZE)) + { + hostname =3D hostname_randomize(hostname, &gc); + } + + if (hostname) + { + print_hostname =3D hostname; + } + else + { + print_hostname =3D "undefined"; + } + + fmt =3D "RESOLVE: Cannot resolve host address: %s:%s%s (%s)"; + if ((flags & GETADDR_MENTION_RESOLVE_RETRY) && !resolve_retry_seco= nds) + { + fmt =3D "RESOLVE: Cannot resolve host address: %s:%s%s (%s)" + "(I would have retried this name query if you had " + "specified the --resolv-retry option.)"; + } + + if (!(flags & GETADDR_RESOLVE) || status =3D=3D EAI_FAIL) + { + msg(msglevel, "RESOLVE: Cannot parse IP address: %s:%s (%s)", = print_hostname, + print_servname, gai_strerror(status)); + goto done; + } + +#ifdef ENABLE_MANAGEMENT + if (flags & GETADDR_UPDATE_MANAGEMENT_STATE) + { + if (management) + { + management_set_state(management, OPENVPN_STATE_RESOLVE, NU= LL, NULL, NULL, NULL, + NULL); + } + } +#endif + + /* + * Resolve hostname + */ + while (true) + { +#ifndef _WIN32 + /* force resolv.conf reload */ + res_init(); +#endif + /* try hostname lookup */ + hints.ai_flags &=3D ~AI_NUMERICHOST; + dmsg(D_SOCKET_DEBUG, "GETADDRINFO flags=3D0x%04x ai_family=3D%= d ai_socktype=3D%d", flags, + hints.ai_family, hints.ai_socktype); + status =3D getaddrinfo(hostname, servname, &hints, res); + + if (sig_info) + { + get_signal(&sig_info->signal_received); + if (sig_info->signal_received) /* were we interrupted by a= signal? */ + { + /* why are we overwriting SIGUSR1 ? */ + if (signal_reset(sig_info, SIGUSR1) =3D=3D SIGUSR1) /*= ignore SIGUSR1 */ + { + msg(level, "RESOLVE: Ignored SIGUSR1 signal receiv= ed during " + "DNS resolution attempt"); + } + else + { + /* turn success into failure (interrupted syscall)= */ + if (0 =3D=3D status) + { + ASSERT(res); + freeaddrinfo(*res); + *res =3D NULL; + status =3D EAI_AGAIN; /* =3D temporary failure= */ + errno =3D EINTR; + } + goto done; + } + } + } + + /* success? */ + if (0 =3D=3D status) + { + break; + } + + /* resolve lookup failed, should we + * continue or fail? */ + level =3D msglevel; + if (resolve_retries > 0) + { + level =3D D_RESOLVE_ERRORS; + } + + msg(level, fmt, print_hostname, print_servname, getaddrinfo_ad= dr_family_name(ai_family), + gai_strerror(status)); + + if (--resolve_retries <=3D 0) + { + goto done; + } + + management_sleep(fail_wait_interval); + } + + ASSERT(res); + + /* hostname resolve succeeded */ + + /* + * Do not choose an IP Addresse by random or change the order * + * of IP addresses, doing so will break RFC 3484 address selection= * + */ + } + else + { + /* IP address parse succeeded */ + if (flags & GETADDR_RANDOMIZE) + { + msg(M_WARN, "WARNING: ignoring --remote-random-hostname becaus= e the " + "hostname is an IP address"); + } + } + +done: + if (sig_info && sig_info->signal_received) + { + int level =3D 0; + if (flags & GETADDR_FATAL_ON_SIGNAL) + { + level =3D M_FATAL; + } + else if (flags & GETADDR_WARN_ON_SIGNAL) + { + level =3D M_WARN; + } + msg(level, "RESOLVE: signal received during DNS resolution attempt= "); + } + + gc_free(&gc); + return status; +} + +/* + * We do our own inet_aton because the glibc function + * isn't very good about error checking. + */ +int +openvpn_inet_aton(const char *dotted_quad, struct in_addr *addr) +{ + unsigned int a, b, c, d; + + CLEAR(*addr); + if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) =3D=3D 4) + { + if (a < 256 && b < 256 && c < 256 && d < 256) + { + addr->s_addr =3D htonl(a << 24 | b << 16 | c << 8 | d); + return OIA_IP; /* good dotted quad */ + } + } + if (string_class(dotted_quad, CC_DIGIT | CC_DOT, 0)) + { + return OIA_ERROR; /* probably a badly formatted dotted quad */ + } + else + { + return OIA_HOSTNAME; /* probably a hostname */ + } +} + +bool +ip_addr_dotted_quad_safe(const char *dotted_quad) +{ + /* verify non-NULL */ + if (!dotted_quad) + { + return false; + } + + /* verify length is within limits */ + if (strlen(dotted_quad) > 15) + { + return false; + } + + /* verify that all chars are either numeric or '.' and that no numeric + * substring is greater than 3 chars */ + { + int nnum =3D 0; + const char *p =3D dotted_quad; + int c; + + while ((c =3D *p++)) + { + if (c >=3D '0' && c <=3D '9') + { + ++nnum; + if (nnum > 3) + { + return false; + } + } + else if (c =3D=3D '.') + { + nnum =3D 0; + } + else + { + return false; + } + } + } + + /* verify that string will convert to IP address */ + { + struct in_addr a; + return openvpn_inet_aton(dotted_quad, &a) =3D=3D OIA_IP; + } +} + +bool +ipv6_addr_safe(const char *ipv6_text_addr) +{ + /* verify non-NULL */ + if (!ipv6_text_addr) + { + return false; + } + + /* verify length is within limits */ + if (strlen(ipv6_text_addr) > INET6_ADDRSTRLEN) + { + return false; + } + + /* verify that string will convert to IPv6 address */ + { + struct in6_addr a6; + return inet_pton(AF_INET6, ipv6_text_addr, &a6) =3D=3D 1; + } +} + +static bool +dns_addr_safe(const char *addr) +{ + if (addr) + { + const size_t len =3D strlen(addr); + return len > 0 && len <=3D 255 && string_class(addr, CC_ALNUM | CC= _DASH | CC_DOT, 0); + } + else + { + return false; + } +} + +bool +ip_or_dns_addr_safe(const char *addr, const bool allow_fqdn) +{ + if (ip_addr_dotted_quad_safe(addr)) + { + return true; + } + else if (allow_fqdn) + { + return dns_addr_safe(addr); + } + else + { + return false; + } +} + +bool +mac_addr_safe(const char *mac_addr) +{ + /* verify non-NULL */ + if (!mac_addr) + { + return false; + } + + /* verify length is within limits */ + if (strlen(mac_addr) > 17) + { + return false; + } + + /* verify that all chars are either alphanumeric or ':' and that no + * alphanumeric substring is greater than 2 chars */ + { + int nnum =3D 0; + const char *p =3D mac_addr; + int c; + + while ((c =3D *p++)) + { + if ((c >=3D '0' && c <=3D '9') || (c >=3D 'a' && c <=3D 'f') |= | (c >=3D 'A' && c <=3D 'F')) + { + ++nnum; + if (nnum > 2) + { + return false; + } + } + else if (c =3D=3D ':') + { + nnum =3D 0; + } + else + { + return false; + } + } + } + + /* error-checking is left to script invoked in lladdr.c */ + return true; +} diff --git a/src/openvpn/socket_util.h b/src/openvpn/socket_util.h new file mode 100644 index 0000000..5ea37dd --- /dev/null +++ b/src/openvpn/socket_util.h @@ -0,0 +1,491 @@ +/* + * OpenVPN -- An application to securely tunnel IP networks + * over a single TCP/UDP port, with support for SSL/TLS-based + * session authentication and key exchange, + * packet encryption, packet authentication, and + * packet compression. + * + * Copyright (C) 2002-2025 OpenVPN Inc + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, see . + */ + +#ifndef SOCKET_UTIL_H +#define SOCKET_UTIL_H + +#include "buffer.h" +#include "env_set.h" +#include "sig.h" + +#define PS_SHOW_PORT_IF_DEFINED (1 << 0) +#define PS_SHOW_PORT (1 << 1) +#define PS_SHOW_PKTINFO (1 << 2) +#define PS_DONT_SHOW_ADDR (1 << 3) +#define PS_DONT_SHOW_FAMILY (1 << 4) + +/* OpenVPN sockaddr struct */ +struct openvpn_sockaddr +{ + /*int dummy;*/ /* add offset to force a bug if sa not explicitly deref= erenced */ + union + { + struct sockaddr sa; + struct sockaddr_in in4; + struct sockaddr_in6 in6; + } addr; +}; + +/* actual address of remote, based on source address of received packets */ +struct link_socket_actual +{ + /*int dummy;*/ /* add offset to force a bug if dest not explicitly der= eferenced */ + + struct openvpn_sockaddr dest; +#if ENABLE_IP_PKTINFO + union + { +#if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST) + struct in_pktinfo in4; +#elif defined(IP_RECVDSTADDR) + struct in_addr in4; +#endif + struct in6_pktinfo in6; + } pi; +#endif +}; + +const char *print_sockaddr_ex(const struct sockaddr *addr, const char *sep= arator, + const unsigned int flags, struct gc_arena *g= c); + +static inline const char * +print_openvpn_sockaddr(const struct openvpn_sockaddr *addr, struct gc_aren= a *gc) +{ + return print_sockaddr_ex(&addr->addr.sa, ":", PS_SHOW_PORT, gc); +} + +static inline const char * +print_sockaddr(const struct sockaddr *addr, struct gc_arena *gc) +{ + return print_sockaddr_ex(addr, ":", PS_SHOW_PORT, gc); +} + + +const char *print_link_socket_actual_ex(const struct link_socket_actual *a= ct, const char *separator, + const unsigned int flags, struct g= c_arena *gc); + +const char *print_link_socket_actual(const struct link_socket_actual *act,= struct gc_arena *gc); + + +#define IA_EMPTY_IF_UNDEF (1 << 0) +#define IA_NET_ORDER (1 << 1) +const char *print_in_addr_t(in_addr_t addr, unsigned int flags, struct gc_= arena *gc); + +const char *print_in6_addr(struct in6_addr addr6, unsigned int flags, stru= ct gc_arena *gc); + +const char *print_in_port_t(in_port_t port, struct gc_arena *gc); + +struct in6_addr add_in6_addr(struct in6_addr base, uint32_t add); + +#define SA_IP_PORT (1 << 0) +#define SA_SET_IF_NONZERO (1 << 1) +void setenv_sockaddr(struct env_set *es, const char *name_prefix, + const struct openvpn_sockaddr *addr, const unsigned i= nt flags); + +void setenv_in_addr_t(struct env_set *es, const char *name_prefix, in_addr= _t addr, + const unsigned int flags); + +void setenv_in6_addr(struct env_set *es, const char *name_prefix, const st= ruct in6_addr *addr, + const unsigned int flags); + +void setenv_link_socket_actual(struct env_set *es, const char *name_prefix, + const struct link_socket_actual *act, const= unsigned int flags); + +/* + * DNS resolution + */ + +#define GETADDR_RESOLVE (1 << 0) +#define GETADDR_FATAL (1 << 1) +#define GETADDR_HOST_ORDER (1 << 2) +#define GETADDR_MENTION_RESOLVE_RETRY (1 << 3) +#define GETADDR_FATAL_ON_SIGNAL (1 << 4) +#define GETADDR_WARN_ON_SIGNAL (1 << 5) +#define GETADDR_MSG_VIRT_OUT (1 << 6) +#define GETADDR_TRY_ONCE (1 << 7) +#define GETADDR_UPDATE_MANAGEMENT_STATE (1 << 8) +#define GETADDR_RANDOMIZE (1 << 9) +#define GETADDR_PASSIVE (1 << 10) +#define GETADDR_DATAGRAM (1 << 11) + +#define GETADDR_CACHE_MASK (GETADDR_DATAGRAM | GETADDR_PASSIVE) + +/** + * Translate an IPv4 addr or hostname from string form to in_addr_t + * + * In case of resolve error, it will try again for + * resolve_retry_seconds seconds. + */ +in_addr_t getaddr(unsigned int flags, const char *hostname, int resolve_re= try_seconds, + bool *succeeded, struct signal_info *sig_info); + +/** + * Translate an IPv6 addr or hostname from string form to in6_addr + */ +bool get_ipv6_addr(const char *hostname, struct in6_addr *network, unsigne= d int *netbits, + int msglevel); + +int openvpn_getaddrinfo(unsigned int flags, const char *hostname, const ch= ar *servname, + int resolve_retry_seconds, struct signal_info *sig= _info, int ai_family, + struct addrinfo **res); + +/* return values of openvpn_inet_aton */ +#define OIA_HOSTNAME 0 +#define OIA_IP 1 +#define OIA_ERROR -1 +int openvpn_inet_aton(const char *dotted_quad, struct in_addr *addr); + +/* integrity validation on pulled options */ +bool ip_addr_dotted_quad_safe(const char *dotted_quad); + +bool ip_or_dns_addr_safe(const char *addr, const bool allow_fqdn); + +bool mac_addr_safe(const char *mac_addr); + +bool ipv6_addr_safe(const char *ipv6_text_addr); + +/* + * Transport protocol naming and other details. + */ + +/* + * Use enum's instead of #define to allow for easier + * optional proto support + */ +enum proto_num +{ + PROTO_NONE, /* catch for uninitialized */ + PROTO_UDP, + PROTO_TCP, + PROTO_TCP_SERVER, + PROTO_TCP_CLIENT, + PROTO_N +}; + +static inline bool +proto_is_net(int proto) +{ + ASSERT(proto >=3D 0 && proto < PROTO_N); + return proto !=3D PROTO_NONE; +} + +/** + * @brief Returns if the protocol being used is UDP + */ +static inline bool +proto_is_udp(int proto) +{ + ASSERT(proto >=3D 0 && proto < PROTO_N); + return proto =3D=3D PROTO_UDP; +} + +/** + * @brief Return if the protocol is datagram (UDP) + * + */ +static inline bool +proto_is_dgram(int proto) +{ + return proto_is_udp(proto); +} + +/** + * @brief returns if the proto is a TCP variant (tcp-server, tcp-client or= tcp) + */ +static inline bool +proto_is_tcp(int proto) +{ + ASSERT(proto >=3D 0 && proto < PROTO_N); + return proto =3D=3D PROTO_TCP_CLIENT || proto =3D=3D PROTO_TCP_SERVER; +} + +int ascii2proto(const char *proto_name); + +sa_family_t ascii2af(const char *proto_name); + +const char *proto2ascii(int proto, sa_family_t af, bool display_form); + +const char *proto2ascii_all(struct gc_arena *gc); + +const char *proto_remote(int proto, bool remote); + +const char *addr_family_name(int af); + +static inline bool +addr_defined(const struct openvpn_sockaddr *addr) +{ + if (!addr) + { + return 0; + } + switch (addr->addr.sa.sa_family) + { + case AF_INET: + return addr->addr.in4.sin_addr.s_addr !=3D 0; + + case AF_INET6: + return !IN6_IS_ADDR_UNSPECIFIED(&addr->addr.in6.sin6_addr); + + default: + return 0; + } +} + +static inline bool +addr_local(const struct sockaddr *addr) +{ + if (!addr) + { + return false; + } + switch (addr->sa_family) + { + case AF_INET: + return ((const struct sockaddr_in *)addr)->sin_addr.s_addr =3D= =3D htonl(INADDR_LOOPBACK); + + case AF_INET6: + return IN6_IS_ADDR_LOOPBACK(&((const struct sockaddr_in6 *)add= r)->sin6_addr); + + default: + return false; + } +} + + +static inline bool +addr_defined_ipi(const struct link_socket_actual *lsa) +{ +#if ENABLE_IP_PKTINFO + if (!lsa) + { + return 0; + } + switch (lsa->dest.addr.sa.sa_family) + { +#if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST) + case AF_INET: + return lsa->pi.in4.ipi_spec_dst.s_addr !=3D 0; + +#elif defined(IP_RECVDSTADDR) + case AF_INET: + return lsa->pi.in4.s_addr !=3D 0; + +#endif + case AF_INET6: + return !IN6_IS_ADDR_UNSPECIFIED(&lsa->pi.in6.ipi6_addr); + + default: + return 0; + } +#else /* if ENABLE_IP_PKTINFO */ + ASSERT(0); +#endif + return false; +} + +/* + * Overhead added to packets by various protocols. + */ +static inline int +datagram_overhead(sa_family_t af, int proto) +{ + int overhead =3D 0; + overhead +=3D (proto =3D=3D PROTO_UDP) ? 8 : 20; + overhead +=3D (af =3D=3D AF_INET) ? 20 : 40; + return overhead; +} + +/* + * Misc inline functions + */ + +static inline bool +link_socket_proto_connection_oriented(int proto) +{ + return !proto_is_dgram(proto); +} + +static inline bool +link_socket_actual_defined(const struct link_socket_actual *act) +{ + return act && addr_defined(&act->dest); +} + +static inline bool +addr_match(const struct openvpn_sockaddr *a1, const struct openvpn_sockadd= r *a2) +{ + switch (a1->addr.sa.sa_family) + { + case AF_INET: + return a1->addr.in4.sin_addr.s_addr =3D=3D a2->addr.in4.sin_ad= dr.s_addr; + + case AF_INET6: + return IN6_ARE_ADDR_EQUAL(&a1->addr.in6.sin6_addr, &a2->addr.i= n6.sin6_addr); + } + ASSERT(0); + return false; +} + +static inline bool +addrlist_match(const struct openvpn_sockaddr *a1, const struct addrinfo *a= ddrlist) +{ + const struct addrinfo *curele; + for (curele =3D addrlist; curele; curele =3D curele->ai_next) + { + switch (a1->addr.sa.sa_family) + { + case AF_INET: + if (a1->addr.in4.sin_addr.s_addr + =3D=3D ((struct sockaddr_in *)curele->ai_addr)->sin_ad= dr.s_addr) + { + return true; + } + break; + + case AF_INET6: + if (IN6_ARE_ADDR_EQUAL(&a1->addr.in6.sin6_addr, + &((struct sockaddr_in6 *)curele->ai= _addr)->sin6_addr)) + { + return true; + } + break; + + default: + ASSERT(0); + } + } + return false; +} + +static inline bool +addrlist_port_match(const struct openvpn_sockaddr *a1, const struct addrin= fo *a2) +{ + const struct addrinfo *curele; + for (curele =3D a2; curele; curele =3D curele->ai_next) + { + switch (a1->addr.sa.sa_family) + { + case AF_INET: + if (curele->ai_family =3D=3D AF_INET + && a1->addr.in4.sin_addr.s_addr + =3D=3D ((struct sockaddr_in *)curele->ai_addr)-= >sin_addr.s_addr + && a1->addr.in4.sin_port =3D=3D ((struct sockaddr_in *= )curele->ai_addr)->sin_port) + { + return true; + } + break; + + case AF_INET6: + if (curele->ai_family =3D=3D AF_INET6 + && IN6_ARE_ADDR_EQUAL(&a1->addr.in6.sin6_addr, + &((struct sockaddr_in6 *)curele-= >ai_addr)->sin6_addr) + && a1->addr.in6.sin6_port + =3D=3D ((struct sockaddr_in6 *)curele->ai_addr)= ->sin6_port) + { + return true; + } + break; + + default: + ASSERT(0); + } + } + return false; +} + + +static inline bool +addr_port_match(const struct openvpn_sockaddr *a1, const struct openvpn_so= ckaddr *a2) +{ + switch (a1->addr.sa.sa_family) + { + case AF_INET: + return a1->addr.in4.sin_addr.s_addr =3D=3D a2->addr.in4.sin_ad= dr.s_addr + && a1->addr.in4.sin_port =3D=3D a2->addr.in4.sin_port; + + case AF_INET6: + return IN6_ARE_ADDR_EQUAL(&a1->addr.in6.sin6_addr, &a2->addr.i= n6.sin6_addr) + && a1->addr.in6.sin6_port =3D=3D a2->addr.in6.sin6_port; + } + ASSERT(0); + return false; +} + +static inline bool +addr_match_proto(const struct openvpn_sockaddr *a1, const struct openvpn_s= ockaddr *a2, + const int proto) +{ + return link_socket_proto_connection_oriented(proto) ? addr_match(a1, a= 2) + : addr_port_match(= a1, a2); +} + + +static inline bool +addrlist_match_proto(const struct openvpn_sockaddr *a1, struct addrinfo *a= ddr_list, const int proto) +{ + return link_socket_proto_connection_oriented(proto) ? addrlist_match(a= 1, addr_list) + : addrlist_port_ma= tch(a1, addr_list); +} + +static inline void +addr_zero_host(struct openvpn_sockaddr *addr) +{ + switch (addr->addr.sa.sa_family) + { + case AF_INET: + addr->addr.in4.sin_addr.s_addr =3D 0; + break; + + case AF_INET6: + memset(&addr->addr.in6.sin6_addr, 0, sizeof(struct in6_addr)); + break; + } +} + +static inline int +af_addr_size(sa_family_t af) +{ + switch (af) + { + case AF_INET: + return sizeof(struct sockaddr_in); + + case AF_INET6: + return sizeof(struct sockaddr_in6); + + default: +#if 0 + /* could be called from socket_do_accept() with empty addr */ + msg(M_ERR, "Bad address family: %d\n", af); + ASSERT(0); +#endif + return 0; + } +} + +static inline bool +link_socket_actual_match(const struct link_socket_actual *a1, const struct= link_socket_actual *a2) +{ + return addr_port_match(&a1->dest, &a2->dest); +} + +#endif diff --git a/src/openvpn/ssl_common.h b/src/openvpn/ssl_common.h index e8fde86..428bf5a 100644 --- a/src/openvpn/ssl_common.h +++ b/src/openvpn/ssl_common.h @@ -30,7 +30,7 @@ #define SSL_COMMON_H_ #include "session_id.h" -#include "socket.h" +#include "socket_util.h" #include "packet_id.h" #include "crypto.h" #include "options.h" diff --git a/src/openvpn/tun.c b/src/openvpn/tun.c index f54b608..6c3096b 100644 --- a/src/openvpn/tun.c +++ b/src/openvpn/tun.c @@ -39,7 +39,7 @@ #include "fdmisc.h" #include "common.h" #include "run_command.h" -#include "socket.h" +#include "socket_util.h" #include "manage.h" #include "route.h" #include "win32.h" -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1159?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: I7393459b975fb9b3e0a42743f58645f769d1be5a Gerrit-Change-Number: 1159 Gerrit-PatchSet: 2 Gerrit-Owner: flichtenheld Gerrit-Reviewer: cron2 Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-MessageType: merged --r2zmF8NZK70= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

cron2 submitted this change.

View Change

socket: Create socket_util with non-socket fu=
nctions

This extracts a lot of the helper functions that do not
a= ctually work on sockets, but instead on addresses or
similar.

Thi= s includes

- openvpn_getaddrinfo and related functions
- print_*<= br>- setenv_*

And lots of the inline functions.

This move wil= l make it easier to add unit tests
for these moved functions.

Cha= nge-Id: I7393459b975fb9b3e0a42743f58645f769d1be5a
Signed-off-by: Frank L= ichtenheld <frank@...2641...>
Acked-by: Gert Doering <gert@...1386...= 296...>
Message-Id: <20250831155939.29240-1-gert@...1296...>URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg3= 2727.html
Signed-off-by: Gert Doering <gert@...1296...>
---
= M CMakeLists.txt
M src/openvpn/Makefile.am
M src/openvpn/clinat.c
= M src/openvpn/dhcp.c
M src/openvpn/dns.c
M src/openvpn/manage.h
M = src/openvpn/misc.c
M src/openvpn/misc.h
M src/openvpn/mroute.c
M s= rc/openvpn/mss.h
M src/openvpn/networking_iproute2.c
M src/openvpn/op= tions.c
M src/openvpn/options.h
M src/openvpn/pool.c
M src/openvpn= /socket.c
M src/openvpn/socket.h
A src/openvpn/socket_util.c
A src= /openvpn/socket_util.h
M src/openvpn/ssl_common.h
M src/openvpn/tun.c=
20 files changed, 1,443 insertions(+), 1,380 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 3866e= 21..45044af 100644
--- a/CMakeLists.txt
+++= b/CMakeLists.txt
@@ -555,6 +555,8 @@
= src/openvpn/sig.h
src/openvpn/socket.c
src/openvpn/socket.h
+ src/openvpn/socket_util.c
+ src/openvpn/socket_util.h
src/ope= nvpn/socks.c
src/openvpn/socks.h
= src/openvpn/ssl.c
diff --git a/src/openvpn/Makefile.am b/sr= c/openvpn/Makefile.am
index 1e17c3f..217f897 100644<= br>--- a/src/openvpn/Makefile.am
+++ b/src/openvpn/Ma= kefile.am
@@ -128,6 +128,7 @@
shaper.c sh= aper.h \
sig.c sig.h \
socket.c socket.= h \
+ socket_util.c s= ocket_util.h \
socks.c socks.h \
ssl.c = ssl.h ssl_backend.h \
ssl_openssl.c ssl_openssl.h \
diff --git a/src/openvpn/clinat.c b/src/openvpn/clinat.c=
index a303285..48a2057 100644
--- a/src/openvpn/c= linat.c
+++ b/src/openvpn/clinat.c
@@ -28,7= +28,7 @@

#include "clinat.h"<= /span>
#include "proto.h"
-#include "socket.h"
+#include "socket_util.h"=
#include "memdbg.h"

= static bool
diff --git a/src/openvpn/dhcp.c b/src/openvpn/= dhcp.c
index 0a7689f..7abade5 100644
--- a/= src/openvpn/dhcp.c
+++ b/src/openvpn/dhcp.c
@@ -27,7 +27,7 @@

#include "syshead.h"
#include "dhcp.h"
-#include "socket.h"
+#include "socket_util.h&quo= t;

#include "error.h"
#include "memdbg.h"
diff --git a/src/ope= nvpn/dns.c b/src/openvpn/dns.c
index 8e28c2b..8554089 10064= 4
--- a/src/openvpn/dns.c
+++ b/src/openvpn= /dns.c
@@ -27,7 +27,7 @@
#include "sy= shead.h"

#include "dns.h"=
-#include "socket= .h"
+#include &q= uot;socket_util.h"
#include "options.h"

#include "run_command.h"
diff --git a/src/openvpn/manage.h b/src/openvpn/manage.h
= index 083caf5..b8892c8 100644
--- a/src/openvpn/manag= e.h
+++ b/src/openvpn/manage.h
@@ -48,7 +48= ,7 @@

#include "misc.h"=
#include "event.h"
-#include "socket.h"
+#include "socket_util.h"
#include "mroute.h"

#defi= ne MANAGEMENT_VERSION 5
diff --git a/src/o= penvpn/misc.c b/src/openvpn/misc.c
index 17f7706..59bf52b 1= 00644
--- a/src/openvpn/misc.c
+++ b/src/op= envpn/misc.c
@@ -72,27 +72,6 @@
#endif
}

-/*
- * Prep= end a random string to hostname to prevent DNS caching.
- * For example, foo.bar.gov would be modi= fied to <random-chars>.foo.bar.gov.
- * Of course, this requires explicit support in the DNS= server (wildcard).
- *= /
-const char *<= br>-hostname_randomize(const char= *hostname, struct gc_arena *gc)
-{
-#define n= _rnd_bytes 6
-- uint8_t rnd_bytes[n_rnd_by= tes];
- const char *= rnd_str;
- struct bu= ffer hname =3D alloc_buf_gc(strlen(hostname) + sizeof(rnd_bytes) * 2 + 4, g= c);
-
- prng_bytes(rnd_bytes, sizeof(rnd_by= tes));
- rnd_str =3D= format_hex_ex(rnd_bytes, sizeof(rnd_bytes), 40, 0, NULL, gc);
- buf_printf(&hname, "%= s.%s", rnd_str, hostname);

- return BSTR(&hname);
-#undef n_rnd_bytes
-}
-
#ifdef ENABLE_MANAGEMENT
/* Get username/passw= ord from the management interface */
static booldiff --git a/src/openvpn/misc.h b/src/openvpn/misc.h
index 1b10cd9..e87fd85 100644

--- a/src/openvpn/misc.h
+++ b/src/openvpn/misc.h
@@ -44,9 +44,6 @@

const char **make_extended_arg_array(char *= *p, bool is_inline, struct gc_arena *gc);

-/* prepend a random prefix to hostnam= e */

-const char *hostn= ame_randomize(const char *hostname, struct gc_arena *gc);
-
/*
= * Get and store a username/password
*/
di= ff --git a/src/openvpn/mroute.c b/src/openvpn/mroute.c
inde= x a598037..ab01874 100644
--- a/src/openvpn/mroute.c=
+++ b/src/openvpn/mroute.c
@@ -30,7 +30,7 @@
#include "mroute.h"
#include "= proto.h"
#include "error.h"
-#include "socket.h"
<= br>+#include "socket_util.= h"

#include "memdbg.h"


diff --git a/src/openvpn/mss.h b/src/openvp= n/mss.h
index 05f12a7..bcbb5a8 100644
--- a= /src/openvpn/mss.h
+++ b/src/openvpn/mss.h
= @@ -26,6 +26,7 @@
#include "proto.h"
<= span> #include "error.h"
#include "mtu.h&qu= ot;
+#include "s= ocket.h"
#include "ssl_common.h"
=
void mss_fixup_ipv4(struct buffer *buf, uint16_t m= axmss);
diff --git a/src/openvpn/networking_iproute2.c b/sr= c/openvpn/networking_iproute2.c
index 0635b5d..e9be3a4 1006= 44
--- a/src/openvpn/networking_iproute2.c
= +++ b/src/openvpn/networking_iproute2.c
@@ -30,7 +30,7 @@
#include "misc.h"
#include "= ;openvpn.h"
#include "run_command.h"=
-#include "socket.h"= ;
+#include "soc= ket_util.h"

#include <stdbool.h&= gt;
#include <netinet/in.h>
diff --g= it a/src/openvpn/options.c b/src/openvpn/options.c
index 64= 8d526..74946a4 100644
--- a/src/openvpn/options.c+++ b/src/openvpn/options.c
@@ -45,7 +45,7 @@=
#include "ssl_ncp.h"
#include "o= ptions.h"
#include "misc.h"
-#include "socket.h"
<= br>+#include "socket_util.= h"
#include "packet_id.h"
= #include "pkcs11.h"
#include "win32.h"=
diff --git a/src/openvpn/options.h b/src/openvpn/options.h=
index 4fa6800..44f3fc9 100644
--- a/src/op= envpn/options.h
+++ b/src/openvpn/options.h
@@ -33,7 +33,7 @@
#include "mtu.h"
#include "route.h"
#include "tun.h&quo= t;
-#include "sock= et.h"
+#include = "socket_util.h"
#include "plugin.h"

#include "manage.h"
#include &quo= t;proxy.h"
diff --git a/src/openvpn/pool.c b/src/openv= pn/pool.c
index a41364a..fde6cea 100644
---= a/src/openvpn/pool.c
+++ b/src/openvpn/pool.c
@@ -29,7 +29,7 @@
#include "pool.h" #include "buffer.h"
#include "erro= r.h"
-#include &qu= ot;socket.h"
+#i= nclude "socket_util.h"
#include "otime.h&qu= ot;

#include "memdbg.h"=
diff --git a/src/openvpn/socket.c b/src/openvpn/socket.cindex 8fc10bc..306170c 100644
--- a/src/openvpn/soc= ket.c
+++ b/src/openvpn/socket.c
@@ -405,42= 8 +405,6 @@
throw_signal_soft(SIGHUP, "Preresolvi= ng failed");
}

-/**
- * Small helper function for openvpn_getaddrinfo to print th= e address
- * family wh= en resolving fails
- */=
-static const char *
-getaddrinfo_addr_family= _name(int af)
-{=
- switch (af)
- {

- case AF_INET:
- return "[AF_INET]";-
- case AF_INET6:
- return "[AF_INET6]";<= br>- }
- return "";
-}
-
-/*
- * Translate IPv4/IPv6 ad= dr or hostname into struct addrinfo
- * If resolve error, try again for resolve_retry_seconds seco= nds.
- */
-int

-openvpn_getaddrinfo(unsigned int flags, const char *h= ostname, const char *servname,
- int resolve_retry_seconds, struct signal_info = *sig_info, int ai_family,
- struct addrinfo **res)
-{
- struct addrinfo hints;
- int status;
- struct signal_info sigrec =3D { 0 };
- int msglevel =3D (flags & GETADDR_FATAL) ? M= _FATAL : D_RESOLVE_ERRORS;
- struct gc_arena gc =3D gc_new();
- const char *print_hostname;
- const char *print_servname;
<= span style=3D"color: hsl(0, 100%, 40%);">-
- ASSERT(res);
-
- A= SSERT(hostname || servname);
- ASSERT(!(flags & GETADDR_HOST_ORDER));
-
- if (servname)
- {
- p= rint_servname =3D servname;
- }
- else
- {
- print_servname =3D ""= ;
- }
-

- if (flags & GETADDR_MSG_VIRT_OUT)
- {
- msglevel |=3D M_MSG_VIRT_OUT;
- }
-
= - if ((flags & (GETADDR_FATAL_ON_SIGNAL | GETADDR_WARN_ON_SIGNAL)) &= amp;& !sig_info)
- = {
- sig_info = =3D &sigrec;
- }=
-
- /* try numeric ip addr first */=
- CLEAR(hints);
= - hints.ai_flags =3D AI_NUMERI= CHOST;
-
- if (flags & GETADDR_PASSIVE)=
- {
- hints.ai_flags |=3D AI_PASSIV= E;
- }
-
- if (flags & GETADDR_DATAGRAM)
- {
- hints.ai_socktype =3D SOCK_DGRAM;
- }
- else
- {
- = hints.ai_socktype =3D SOCK_STREAM;
- }
-<= /span>
- /* if hostname is = not set, we want to bind to 'ANY', with
- * the correct address family - v4-only or v6= /v6-dual-stack */
- = if (!hostname)
- {
- hints.ai_family= =3D ai_family;
- }<= /span>
-
- status =3D getaddrinfo(hostname, servna= me, &hints, res);
-=
- if (status !=3D 0= ) /* parse as numeric address failed? */
- {

- const int fail_wait_interval =3D 5; /* seco= nds */
- /* Add = +4 to cause integer division rounding up (1 + 4) =3D 5, (0+4)/5=3D0 */

- int resolve_retrie= s =3D
- (fla= gs & GETADDR_TRY_ONCE) ? 1 : ((resolve_retry_seconds + 4) / fail_wait_i= nterval);
- cons= t char *fmt;
- i= nt level =3D 0;
-

- /* this is not a nu= meric IP, therefore force resolution using the
- * provided ai_family */
- hints.ai_family =3D ai_family;<= /span>
-
- if (hostname && (flags &= ; GETADDR_RANDOMIZE))
-= {
- = hostname =3D hostname_randomize(hostname, &gc);
- }
-
- = if (hostname)
- = {
- p= rint_hostname =3D hostname;
- }
- = else
- {

- print_hostname = =3D "undefined";
- }
-=
- fmt =3D "RESOLV= E: Cannot resolve host address: %s:%s%s (%s)";
- if ((flags & GETADDR_MENTION_RE= SOLVE_RETRY) && !resolve_retry_seconds)
- {
- fmt =3D "RESOLVE: Cannot resolve host addres= s: %s:%s%s (%s)"
-= "(I would have retried this name query if you had &q= uot;
- = "specified the --resolv-retry option.)";
- }
-
- = if (!(flags & GETADDR_RESOLVE) || status =3D=3D EAI_FAIL)=
- {
- msg(msglevel, "RESOLVE:= Cannot parse IP address: %s:%s (%s)", print_hostname,
- print_servname, gai_s= trerror(status));
- = goto done;
- = }
-
-#ifdef ENABLE_MANAGEMENT
<= span style=3D"color: hsl(0, 100%, 40%);">- if (flags & GETADDR_U= PDATE_MANAGEMENT_STATE)
- {
- = if (management)
- = {
- = management_set_state(management, OPENVPN_STATE_RESOLVE, NULL, NULL, NUL= L, NULL,
- = NULL);
- }
= - }
-#endif

-
- /*
- * Resolve hostname
- */
- while (true)
- {
-= #ifndef _WIN32
- = /* force resolv.conf reload */
- res_init();
-#endif
= - /* try hostname lookup */
- hints.ai_flags &=3D ~AI_NUMERICHOST;

- dmsg(D_SOCKET_D= EBUG, "GETADDRINFO flags=3D0x%04x ai_family=3D%d ai_socktype=3D%d"= ;, flags,
- = hints.ai_family, hints.ai_socktype);
- status =3D getaddrinfo(hostname, servname, = &hints, res);
-
- if (sig_info)=
- {<= br>- get_signal(&a= mp;sig_info->signal_received);
- if (sig_info->signal_received) /* were we in= terrupted by a signal? */
- {
-= /* why are we overwriting SIGUSR1 ? */
- if (signal_reset(s= ig_info, SIGUSR1) =3D=3D SIGUSR1) /* ignore SIGUSR1 */
- {
- msg(level, "R= ESOLVE: Ignored SIGUSR1 signal received during "
- "DN= S resolution attempt");
- }
- else
- {
- /* turn success into failure (interrupted= syscall) */
- = if (0 =3D=3D status)
- {
- ASSERT(res);
- freeaddrin= fo(*res);
- = *res =3D NULL;
- status =3D EAI_AGAIN; /* =3D temporary= failure */
- = errno =3D EINTR;
- }
- goto done;
- }
- }
- }
-
- = /* success? */
- if (0 =3D=3D status)
- {
- break;
- }
-=
- /* resolv= e lookup failed, should we
- * continue or fail? */
- level =3D msglevel;
- if (resolve_retries > 0)=
- {
- level =3D D_RESOLVE_= ERRORS;

- }<= /span>
-
- msg(level, fmt, print_hostname,= print_servname, getaddrinfo_addr_family_name(ai_family),
- gai_strerror(status));<= /span>
-
- if (--resolve_retries <=3D 0= )
- {=
- goto done;
- }-
- management_sleep(fail_wait_interval);

- }
-
- ASSERT(res);
-
- = /* hostname resolve succeeded */
-
- = /*
- * Do not= choose an IP Addresse by random or change the order *
- * of IP addresses, doing so will = break RFC 3484 address selection *
- */
- else

- {
- /* IP address parse succeeded */
- if (flags &= GETADDR_RANDOMIZE)
- = {
- ms= g(M_WARN, "WARNING: ignoring --remote-random-hostname because the &quo= t;
- = "hostname is an IP address");
- }
- }
-
-done:
- if (sig_info && sig_info->s= ignal_received)
- {<= /span>
- int level =3D = 0;
- if (flags &= amp; GETADDR_FATAL_ON_SIGNAL)
- {
- = level =3D M_FATAL;
- }
- = else if (flags & GETADDR_WARN_ON_SIGNAL)
- {
- level =3D M_WARN;
- }
- msg(level, "RESOLVE: signal received during DNS resolu= tion attempt");
- = }
-
- gc_free(&gc);
- return status;
-}
-
-/*
- * We do our own inet_aton= because the glibc function
- * isn't very good about error checking.
- */
-int
-openvpn= _inet_aton(const char *dotted_quad, struct in_addr *addr)
-{
- unsigned int a, b, c, d;
-
- CLEAR(*addr);
- = if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, = &d) =3D=3D 4)
- = {
- if (a < 2= 56 && b < 256 && c < 256 && d < 256)

- {
- addr->s_addr =3D htonl(a= << 24 | b << 16 | c << 8 | d);
- return OIA_IP; /* good dotted quad = */
- }- }
- if (string_class(dotted_quad, CC_DIGIT | CC= _DOT, 0))
- {=
- return OIA_ERROR; /*= probably a badly formatted dotted quad */
- }
- else
- {
- return OIA_HOSTN= AME; /* probably a hostname */
- }
-}
-
-bool
-ip_addr_dotted_quad_safe(const char *dotted_quad)
-{

- /* verify non-NULL */
- if (!dotted_quad)
- {
- return false;
- }
-- /* verify length is within = limits */
- if (strl= en(dotted_quad) > 15)
- retur= n false;
- }<= br>-
- /* verify that all chars are either numeric or= '.' and that no numeric
- * substring is greater than 3 chars */
- {
- int nnum =3D 0;
- const char *p =3D dotted_quad;
- int c;
-
- while ((c =3D *p++))
- {
- {- ++nnum;=
- if (nnum >= ; 3)
- {=
- r= eturn false;
- = }
- }=
- else if (= c =3D=3D '.')
-= {
- = nnum =3D 0;
- = }
- = else
- {
- return = false;
- }
- }
- }

-
- {
- struct in_addr a;
- return openvpn_inet_aton(dotted_quad, &= ;a) =3D=3D OIA_IP;
- = }
-}
-
-bool
-ip= v6_addr_safe(const char *ipv6_text_addr)
-{
- = /* verify non-NULL */
- {
- re= turn false;
- }
-
- /* verify length is within limits */=
- if (strlen(ipv6_text_add= r) > INET6_ADDRSTRLEN)
- {
- retu= rn false;
- }=
-
- /* verify that string will convert to IPv6 ad= dress */
- {<= br>- struct in6_addr a6;
- return inet_pto= n(AF_INET6, ipv6_text_addr, &a6) =3D=3D 1;
- }
-}
-
= -static bool
-dns_addr_safe(const char *addr)
= -{
- if (addr)
- {
- = const size_t len =3D strlen(addr);
- return len > 0 && len <=3D 255 &a= mp;& string_class(addr, CC_ALNUM | CC_DASH | CC_DOT, 0);
- }

- else
- {
- = return false;
- }<= /span>
-}
-
-bool
-ip_or= _dns_addr_safe(const char *addr, const bool allow_fqdn)
-{
- if (ip_addr_dotted_quad_safe(addr))
- {
- return true;
- }
- = else if (allow_fqdn)
- {
- return= dns_addr_safe(addr);
-= }
- else<= br>- {
- return false;
- }
-}
-
-bool
-mac_addr_safe(const char *mac_addr)=
-{
- /* verify non-NULL */
- if (!mac_addr)
- {
- return false;
- }
-
- /* verify length is = within limits */
- i= f (strlen(mac_addr) > 17)
- {
- r= eturn false;
- }
-
- /* verify that all chars are either alphan= umeric or ':' and that no
- * alphanumeric substring is greater than 2 chars */=
- {
- int nnum =3D 0;
- const char *p =3D mac_addr;
- int c;
-

- while ((c =3D *p++))
- {
- if ((c >=3D '0' && c <=3D = '9') || (c >=3D 'a' && c <=3D 'f') ||= (c >=3D 'A' && c <=3D 'F'))
- {
- ++nnum;
- if (nnum > 2)
= - {
- return false;
- }=
- }
- else if (c =3D=3D ':= ')

- {
- nnum = =3D 0;
- }
- else=
- {
- return false;
=
- }
- }

- }
-
- /* err= or-checking is left to script invoked in lladdr.c */
- return true;
-}
-
static int
socket_get_sndbuf(sock= et_descriptor_t sd)
{
@@ -2670,467 +2248,6= @@
#endif
}

= -/*
- * Format IP addresses in ascii
- */
-
-const= char *
-print_sockaddr= _ex(const struct sockaddr *sa, const char *separator, const unsigned int fl= ags,
- = struct gc_arena *gc)
-= {
- struct buffer ou= t =3D alloc_buf_gc(128, gc);
- bool addr_is_defined =3D false;
- char hostaddr[NI_MAXHOST] =3D "";=
- char servname[NI_MAXSERV= ] =3D "";
- = int status;
-<= br>- socklen_t salen =3D 0;
- switch (sa->sa_fa= mily)
- {
= - case AF_INET:
= - if (!(flags & PS= _DONT_SHOW_FAMILY))
- = {
- = buf_puts(&out, "[AF_INET]");
- }
- salen =3D sizeof(struct sockaddr_in);=
- addr_is_defined = =3D ((struct sockaddr_in *)sa)->sin_addr.s_addr !=3D 0;
- break;
-
- case AF_INET6:
- if (!(flags & PS_DONT_SHOW_FAMILY))- {
- buf_puts(&out, &quo= t;[AF_INET6]");
- = }
- = salen =3D sizeof(struct sockaddr_in6);
- addr_is_defined =3D !IN6_IS_ADDR_UNSPECIFIE= D(&((struct sockaddr_in6 *)sa)->sin6_addr);
- break;
-
- case AF_UNSPEC:
- if (!(flags & PS_DONT_SHOW_FAMILY))
- {
- return "[AF_UNSPEC]"= ;
- }=
- else
<= span style=3D"color: hsl(0, 100%, 40%);">- {

- return "";
- }
-

- default:
- ASSERT(0);
- }
-=
- status =3D getnam= einfo(sa, salen, hostaddr, sizeof(hostaddr), servname, sizeof(servname),
- = NI_NUMERICHOST | NI_NUMERICSERV);
-
- if (= status !=3D 0)
- {
- buf_printf(&= ;out, "[nameinfo() err: %s]", gai_strerror(status));
- return BSTR(&out);
- }
-
- if (!(flags & PS_DONT_SHOW_ADDR))
- {
- if (addr_is_defined)
- {
- buf_puts(&out, hostaddr);
- }
- else
- {
- = buf_puts(&out, "[undef]");
- }
- }
-
- if ((flags &a= mp; PS_SHOW_PORT) || (flags & PS_SHOW_PORT_IF_DEFINED))
- {

- if (separator)
- {
- buf_puts(&out, separator);
- }
-
- = buf_puts(&out, servname);
- }
-<= /span>
- return BSTR(&o= ut);
-}
-

-const char *
-print_link_socket_actual(const struct link_socket_actual *act, stru= ct gc_arena *gc)
-{
- return print_link_soc= ket_actual_ex(act, ":", PS_SHOW_PORT | PS_SHOW_PKTINFO, gc);
-}
-
-#ifndef IF_NAMESIZE
-#define IF_NAMESIZE 16
-#endif
-<= br>-const char *
-print_link_socket_actual_ex(const stru= ct link_socket_actual *act, const char *separator,
- const unsigned int= flags, struct gc_arena *gc)
-{
- if (act)<= /span>
- {
- struct buffer out =3D alloc_bu= f_gc(128, gc);
- = buf_printf(&out, "%s", print_sockaddr_ex(&act->dest.a= ddr.sa, separator, flags, gc));
-#if ENABLE_IP_PKTINFO
- char ifname[IF_NAMESIZE] =3D "[undef]";<= br>-
- if ((flags & PS_SHOW_PKTINFO) &&am= p; addr_defined_ipi(act))
- {
- = switch (act->dest.addr.sa.sa_family)
- {
- case AF_INET:
- {
- struct openvpn_sockaddr sa;- CLEAR(sa);<= /span>
- sa= .addr.in4.sin_family =3D AF_INET;
-#if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_D= ST)
- = sa.addr.in4.sin_addr =3D act->pi.in4.ipi_spec_dst;
- if_indextoname(act-&g= t;pi.in4.ipi_ifindex, ifname);
-#elif defined(IP_RECVDSTADDR)
- sa.addr.in4.sin_addr =3D act->pi.in= 4;
- = ifname[0] =3D 0;
-#els= e /* if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST) */
-#error ENABLE_IP_PKTINF= O is set without IP_PKTINFO xor IP_RECVDSTADDR (fix syshead.h)
-#endif

- buf_printf(&out, " (= via %s%%%s)",
- = print_sockaddr_ex(&sa.addr.sa, separator, 0= , gc), ifname);
- = }
- = break;
-- case AF_INET6:=
- {
- struc= t sockaddr_in6 sin6;
- = char buf[INET6_ADDRSTRLEN] =3D "[undef]";
- CLEAR(= sin6);
- = sin6.sin6_family =3D AF_INET6;
- sin6.sin6_addr =3D act->pi.in6.ipi6_ad= dr;
- = if_indextoname(act->pi.in6.ipi6_ifindex, ifname);
- if (getnameinfo((struc= t sockaddr *)&sin6, sizeof(struct sockaddr_in6), buf,
- siz= eof(buf), NULL, 0, NI_NUMERICHOST)
- =3D=3D 0)
- {
- buf_printf(&out, "= ; (via %s%%%s)", buf, ifname);
- }
- else
- {
- buf_printf(&out, " (via [= getnameinfo() err]%%%s)", ifname);
- }
- }
- break;
- }
- }
-#endi= f /* if ENABLE_IP_PKTINFO */
- return BSTR(&out);
- }
-= else
- {<= br>- return "[NULL]&q= uot;;
- }
= -}
-
-/*
- * Convert an in_= addr_t in host byte order
- * to an ascii dotted quad.
- */
-const ch= ar *
-print_in_addr_t(i= n_addr_t addr, unsigned int flags, struct gc_arena *gc)
-{
- struct in_addr ia;
- char *out =3D gc_malloc(INET_ADDRSTRLEN, true, gc);
-
- if (addr || !(flags & IA_EMPTY_IF_UNDEF= ))
- {
- CLEAR(ia);

- ia.s_addr =3D (flags & IA_= NET_ORDER) ? addr : htonl(addr);
-
- in= et_ntop(AF_INET, &ia, out, INET_ADDRSTRLEN);
- }
- return out;
-}
-
-/*

- * Convert an in6_addr in host byte order
- * to an ascii representation of an= IPv6 address

- */
-const char *
-print_in6_addr(struct in6_addr a6, = unsigned int flags, struct gc_arena *gc)

-{
- = char *out =3D gc_malloc(INET6_ADDRSTRLEN, true, gc);
-
- if (memcmp(&a6, &in6addr_any, sizeof(a6)) !=3D 0 |= | !(flags & IA_EMPTY_IF_UNDEF))
- {
- = inet_ntop(AF_INET6, &a6, out, INET6_ADDRSTRLEN);
- }
- return out;
-}
-
-/*
- * Convert an in_port_t in host byte order = to a string
- */=
-const char *
-print_in_port_t(in_port_t port, struc= t gc_arena *gc)

-{
- struct buffer buffer = =3D alloc_buf_gc(8, gc);
- return BSTR(&buffer);
-}

-
-= /* add some offset to an ipv6 address
- * (add in steps of 8 bits, taking overflow into next round= )
- */
-struct in6_addr
-add_in6_addr(struct in6_addr base, uint32_t= add)
-{
- int i;

-
- for (i =3D 15; i >=3D 0 && add > 0; i--)- {
- register int carry;
- register uint32_t h;
-

- h =3D (unsigned char)base.s6_addr[i];- base.s6_addr[i] =3D (h = + add) & UINT8_MAX;
-
- /* using ex= plicit carry for the 8-bit additions will catch
- * 8-bit and(!) 32-bit overruns nicely
- */
- carry =3D ((h & 0xff) = + (add & 0xff)) >> 8;

- add =3D (add >> 8) + carry;
- }
- return base;
-}
--/* set environmental variables= for ip/port in *addr */
-setenv_sockadd= r(struct env_set *es, const char *name_prefix, const struct openvpn_sockadd= r *addr,
- = const unsigned int flags)
-{
- char name= _buf[256];
-
= - char buf[INET6_ADDRSTRLEN];<= /span>
- switch (addr->a= ddr.sa.sa_family)
- = {
- case AF_INET= :
- if (flag= s & SA_IP_PORT)
- = {
- = snprintf(name_buf, sizeof(name_buf), "%s_ip", name_prefix);=
- }<= br>- else
- {

- snprintf(name_buf, sizeof(= name_buf), "%s", name_prefix);
- }
-
- = inet_ntop(AF_INET, &addr->addr.in4.sin_addr, buf, sizeof(buf));
- setenv_str(e= s, name_buf, buf);
-
- if ((flags &= amp; SA_IP_PORT) && addr->addr.in4.sin_port)
- {
- snprintf(name_buf, sizeof(name_bu= f), "%s_port", name_prefix);
- setenv_int(es, name_buf, ntohs(addr->ad= dr.in4.sin_port));
- = }
- = break;
-
- case AF_INET6:

- if (IN6_IS_ADDR_V4MAPP= ED(&addr->addr.in6.sin6_addr))

- {
- struct in_addr ia;
- memcpy(&ia.s_addr, &addr->a= ddr.in6.sin6_addr.s6_addr[12], sizeof(ia.s_addr));
- snprintf(name_buf, sizeof(name= _buf), "%s_ip", name_prefix);
- inet_ntop(AF_INET, &ia, buf, sizeof(b= uf));
- }
- else<= br>- {
- snprintf(name_buf, si= zeof(name_buf), "%s_ip6", name_prefix);

- inet_ntop(AF_INET6, &addr-&= gt;addr.in6.sin6_addr, buf, sizeof(buf));
- }
- setenv_str(es, name_buf, buf);
-
- if ((flags & SA_IP_PORT) && addr->ad= dr.in6.sin6_port)
- = {
- = snprintf(name_buf, sizeof(name_buf), "%s_port", name_prefix);=
- seten= v_int(es, name_buf, ntohs(addr->addr.in6.sin6_port));
- }
- break;
- }
-}
-
<= span style=3D"color: hsl(0, 100%, 40%);">-void

-setenv_in_addr_t(struct env_set *es, const char *n= ame_prefix, in_addr_t addr,
- const unsigned int flags)
-{
- if (addr || !(flags & SA_SET_IF_NONZERO))
- {
- struct openvpn_sockaddr si;
- CLEAR(si);
- si.addr.in4.sin_family =3D AF_INET;=
- si.addr.in4.s= in_addr.s_addr =3D htonl(addr);
- setenv_sockaddr(es, name_prefix, &si, flags);<= br>- }
-}
-
-void
-setenv_in6_addr(struct env= _set *es, const char *name_prefix, const struct in6_addr *addr,
<= span style=3D"color: hsl(0, 100%, 40%);">- const unsigned in= t flags)

-{
<= span style=3D"color: hsl(0, 100%, 40%);">- if (!IN6_IS_ADDR_UNSPECIFIED(= addr) || !(flags & SA_SET_IF_NONZERO))

- {
- struct openvpn_sockaddr si;
- CLEAR(si);
- si.addr.in6.sin6_family =3D AF_INET6;
- si.addr.in6.sin6_addr =3D *= addr;

- setenv_s= ockaddr(es, name_prefix, &si, flags);
- }
-}
-
-void

-setenv_link_socket_actual(struct env_set *es, const ch= ar *name_prefix,
- = const struct link_socket_actual *act, const unsigned i= nt flags)
-{
= - setenv_sockaddr(es, name_pre= fix, &act->dest, flags);
-}
--/*
- * Convert protocol names between index and ascii = form.
- */
-

-struct proto_names
-{
- = const char *short_form;
- sa_family_t proto_af;
- int proto;
-};
-
= -/* Indexed by PROTO_x */<= br>-static const struct proto_nam= es proto_names[] =3D {
= - { "proto-uninitialized", "proto-NONE", AF_UNSPEC, = PROTO_NONE },
- /* t= ry IPv4 and IPv6 (client), bind dual-stack (server) */
- { "udp", "UDP", AF= _UNSPEC, PROTO_UDP },
-= { "tcp-server", "TCP_SERVER", AF_UNSPEC, PROTO_TCP_= SERVER },
- { "= tcp-client", "TCP_CLIENT", AF_UNSPEC, PROTO_TCP_CLIENT },
- { "tcp", &= quot;TCP", AF_UNSPEC, PROTO_TCP },
- /* force IPv4 */
- { "udp4", "UDPv4", AF_INET, PROTO_= UDP },
- { "tcp= 4-server", "TCPv4_SERVER", AF_INET, PROTO_TCP_SERVER },
- { "tcp4-client&qu= ot;, "TCPv4_CLIENT", AF_INET, PROTO_TCP_CLIENT },
- { "tcp4", "TCPv4&q= uot;, AF_INET, PROTO_TCP },

- /* force IPv6 */
- { "udp6", "UDPv6", AF_INET6, PROTO_UDP },
- { "tcp6-server&qu= ot;, "TCPv6_SERVER", AF_INET6, PROTO_TCP_SERVER },
- { "tcp6-client", "= ;TCPv6_CLIENT", AF_INET6, PROTO_TCP_CLIENT },

- { "tcp6", "TCPv6", AF_= INET6, PROTO_TCP },
-};=
-
-int
-ascii2proto(const char *proto_name)
-{
- for (size_t i =3D 0; i < SIZE(proto_names); ++i)
= - {
- if (!strcmp(proto_name, proto_names[i].s= hort_form))
- {<= /span>
- return pro= to_names[i].proto;
- = }
- }- return -1;
-}
-
-sa_f= amily_t
-ascii2af(const= char *proto_name)
-{
- for (size_t i =3D 0= ; i < SIZE(proto_names); ++i)
- {
- = if (!strcmp(proto_name, proto_names[i].short_form))
- {
- return proto_names[i].proto_af;
= - }
- }
- return 0;
-}
-
<= span style=3D"color: hsl(0, 100%, 40%);">-const char *

-proto2ascii(int proto, sa_family_t af, boo= l display_form)
-{
- for (size_t i =3D 0; i= < SIZE(proto_names); ++i)
- {
- = if (proto_names[i].proto_af =3D=3D af && proto_names[i].proto =3D= =3D proto)
- {
- if (display= _form)
- {
- return = proto_names[i].display_form;
- }
- = else
- = {
- = return proto_names[i].short_form;
- }
- }
- }<= /span>
-
- return "[unknown protocol]";<= /span>
-}
-
-const char *
-{
- = struct buffer out =3D alloc_buf_gc(256, gc);
-
- for (size_t i =3D 0; i < SIZE(proto_names); ++i)
- {

- if (i)
- {
- }
- buf_printf(&out, "[%s]", proto_na= mes[i].short_form);
- = }
- return BSTR(&a= mp;out);
-}
<= span style=3D"color: hsl(0, 100%, 40%);">-

-const char *
-addr_family_name(int af)
-{
- s= witch (af)
- {
- case AF_INET:
- return "AF= _INET";
-- case AF_INET6:<= br>- return "AF_I= NET6";
- }
- return "AF_UNSPEC= ";
-}
-

-/*
= - * Given a local proto, return local proto
- * if !remote, or compatible remote proto
<= span style=3D"color: hsl(0, 100%, 40%);">- * if remote.

- *
- * This is used for options compatibility
- * checking.
- *
- * IPv6 and IPv4 protocols are comptabile but OpenVPN
- * has always sent UDPv4, TCPv4 over= the wire. Keep these

-= * strings for backward compatibility
- */
-co= nst char *
-proto_remot= e(int proto, bool remote)
-{
- ASSERT(proto= >=3D 0 && proto < PROTO_N);
- if (proto =3D=3D PROTO_UDP)
- {
- return "UDPv4";
- }
-
- if ((= remote && proto =3D=3D PROTO_TCP_CLIENT) || (!remote && pro= to =3D=3D PROTO_TCP_SERVER))
- {
- r= eturn "TCPv4_SERVER";
- }
- if= ((remote && proto =3D=3D PROTO_TCP_SERVER) || (!remote && = proto =3D=3D PROTO_TCP_CLIENT))
- {
- = return "TCPv4_CLIENT";
- }
-
- ASSERT(0);- return ""; /* Mak= e the compiler happy */
-}

/*
* Bad incoming = address lengths that differ from what
diff --git a/src/open= vpn/socket.h b/src/openvpn/socket.h
index ed37590..cce9183 = 100644
--- a/src/openvpn/socket.h
+++ b/src= /openvpn/socket.h
@@ -34,6 +34,7 @@
#inclu= de "socks.h"
#include "misc.h"<= br> #include "tun.h"
+#include "socket_util.h"

/*
* OpenVPN's default port number as ass= igned by IANA.
@@ -60,18 +61,6 @@
/* conve= rt a packet_size_type from network to host order */
#defin= e ntohps(x) ntohs(x)

-/* OpenVPN sockaddr struct */
-struct openvpn_sockaddr
-{
- /*int dummy;*/ /* add offset to force a bug if sa not explicitly= dereferenced */
- u= nion
- {
<= span style=3D"color: hsl(0, 100%, 40%);">- struct sockaddr sa;
- struct sockaddr_in = in4;
- struct so= ckaddr_in6 in6;
- } = addr;
-};
-

/* struct to hold= preresolved host names */
struct cached_dns_entry<= br> {
@@ -83,25 +72,6 @@
struct = cached_dns_entry *next;
};

-/* actual address of remote, based = on source address of received packets */

-struct link_socket_actual
-{
-=
- struct openvpn_sockaddr = dest;
-#if ENABLE_IP_PK= TINFO
- union=
- {
-#if defined(HAVE_IN_PKTINFO) && def= ined(HAVE_IPI_SPEC_DST)
- struct in_pktinfo in4;
-#elif defined(IP_RECVDSTADDR)
- struct in_addr in4;
-#endif
- struct in6_pktinfo in6;
- } pi;
-#endif
-};
-
/* IP ad= dresses which are persistent across SIGUSR1s */
struct lin= k_socket_addr
{
@@ -349,58 +319,6 @@

void sd_close(socket_descriptor_t *sd);

-#define P= S_SHOW_PORT_IF_DEFINED (1 << 0)
-#define PS_SHOW_PORT (1 << 1)
-#define PS_SHOW_PKTINFO (1= << 2)

-#define P= S_DONT_SHOW_ADDR (1 << 3)
-#define PS_DONT_SHOW_FAMILY (1 << 4)
-

-const char *print_sockaddr_ex(const struct sockaddr *ad= dr, const char *separator,
- const unsigned int flags, struct gc_aren= a *gc);
-
-static inline const char *
-print_openvpn_sockaddr(const s= truct openvpn_sockaddr *addr, struct gc_arena *gc)
-{
- return print_sockaddr_ex(&addr->addr.sa, ":",= PS_SHOW_PORT, gc);
-}<= /span>
-
-static inline const char *
-print_sockaddr(const struct sockaddr *= addr, struct gc_arena *gc)
-{
- return prin= t_sockaddr_ex(addr, ":", PS_SHOW_PORT, 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 *print_link_socket_actual(const struct link_s= ocket_actual *act, struct gc_arena *gc);
-
-
-#define IA_EMPTY_IF_UND= EF (1 << 0)
-#def= ine IA_NET_ORDER (1 << 1)
-const char *print_in_addr_t(in_addr_t addr, unsigned int fla= gs, struct gc_arena *gc);
-
-const char *print= _in6_addr(struct in6_addr addr6, unsigned int flags, struct gc_arena *gc);<= /span>
-
-const char *print_in_port_t(in_port_t port,= struct gc_arena *gc);
= -
-struct in6_addr add_= in6_addr(struct in6_addr base, uint32_t add);
-
-#define SA_SET_IF_NONZERO (1 << 1)
-void setenv_sockaddr(struct env_se= t *es, const char *name_prefix,

- const struct openvpn_sockaddr *addr, const u= nsigned int flags);
-
-void setenv_in_addr_t(s= truct env_set *es, const char *name_prefix, in_addr_t addr,
- const unsigned = int flags);

--void setenv_in6_addr(struct env= _set *es, const char *name_prefix, const struct in6_addr *addr,
<= span style=3D"color: hsl(0, 100%, 40%);">- const unsign= ed int flags);

-=
-void setenv_link_socket_actu= al(struct env_set *es, const char *name_prefix,
- const struct link_= socket_actual *act, const unsigned int flags);
-
void bad_address_length(int actu= al, int expected);

/* IPV4_INVALID_ADDR:= returned by link_socket_current_remote()
@@ -432,21 +350,6= @@
* Low-level functions
*/
=
-/* return valu= es of openvpn_inet_aton */
-#define OIA_HOSTNAME 0
-#define OIA_IP 1
-#define OIA_ERROR -1
-int openvpn_inet_aton(const char *dotted_quad, struct in_addr *add= r);
-
-/* integrity validation on pulled optio= ns */
-bool ip_addr_dot= ted_quad_safe(const char *dotted_quad);
-
-boo= l ip_or_dns_addr_safe(const char *addr, const bool allow_fqdn);
<= span style=3D"color: hsl(0, 100%, 40%);">-

-bool mac_addr_safe(const char *mac_addr);
-

-bool ipv6_addr_safe(const char *ipv6_text_addr);=
-
socket_des= criptor_t create_socket_tcp(struct addrinfo *);
socket_descriptor_t socket_do_accept(socket_descriptor_t sd, struct= link_socket_actual *act,
@@ -472,134 +375,6 @@
<= span>

#endif /* if UNIX_SOCK_SUPPORT */
=
-/*
- * DNS resolution
- */
-
-#define= GETADDR_RESOLVE (1 << 0)
-#define GETADDR_FATAL (1 <&l= t; 1)
-#define GETADDR_= HOST_ORDER (1 << 2)
-#define GETADDR_MENTION_RESOLVE_RETRY (1 << 3)
-#define GETADDR_FATAL_ON_= SIGNAL (1 << 4)
-#define GETADDR_WARN_ON_SIGNAL (1 << 5)
-#define GETADDR_MSG_VIRT_OUT = (1 << 6)

-#= define GETADDR_TRY_ONCE (1 << 7)
-#define GETADDR_UPDATE_MANAGEMENT_STATE (1 = << 8)
-#define GE= TADDR_RANDOMIZE (1 << 9)
-#define GETADDR_PASSIVE (1 << = 10)
-#define GETADDR_DA= TAGRAM (1 << 11)
-
-#defi= ne GETADDR_CACHE_MASK (GETADDR_DATAGRAM | GETADDR_PASSIVE)
-
-/**
- *= Translate an IPv4 addr or hostname from string form to in_addr_t- *
- * In case of resolve error, it will try again for=
- * resolve_retry_seco= nds seconds.
- */
-in_addr_t getaddr(unsigned = int flags, const char *hostname, int resolve_retry_seconds,
- bool *succeeded, st= ruct signal_info *sig_info);

-
-/**
= - * Translate an IPv6 addr or hos= tname from string form to in6_addr
- */
-bool = get_ipv6_addr(const char *hostname, struct in6_addr *network, unsigned int = *netbits,
- = int msglevel);
-=
-int openvpn_getaddrin= fo(unsigned int flags, const char *hostname, const char *servname,- int re= solve_retry_seconds, struct signal_info *sig_info, int ai_family,- struct = addrinfo **res);
-
-/*
- * Transport protocol naming and other deta= ils.
- */
-

-/*
-= * Use enum's instead of #define to allow for easier
- * optional proto support
- */

-enum proto_num
-{
- PR= OTO_NONE, /* catch for uninitialized */
- PROTO_UDP,
- PROTO_TCP,
- PROTO_TCP_SERVER,
- PROTO_TCP_CLIENT,
- PROTO_N
-};
-
-static inline bool
-proto_is_net(int proto)
-{
- ASSERT(proto >=3D 0 && proto < PROTO_N);- return proto !=3D PROTO_NO= NE;
-}
-
-/**
- *= @brief Returns if the protocol being used is UDP
- */
-static inline bool
-proto_is_udp(int proto)
-{
- ASSERT(= proto >=3D 0 && proto < PROTO_N);
- return proto =3D=3D PROTO_UDP;
-}

-
-/*= *
- * @brief Return if = the protocol is datagram (UDP)
- *
- */=
-static inline bool-proto_is_dgram(int proto)
-{
- return proto_is_udp(proto);
-}

-
-/**=
- * @brief returns if = the proto is a TCP variant (tcp-server, tcp-client or tcp)
- */
-static inline bool
-proto_is_tcp(int proto)
-{
- = ASSERT(proto >=3D 0 && proto < PROTO_N);
- return proto =3D=3D PROTO_TCP_CLIENT= || proto =3D=3D PROTO_TCP_SERVER;
-}
-=
-
-int ascii2proto(const char *proto_name);-
-sa_family_t ascii2af(const char *proto_name);
-
-const char *proto2ascii(int proto, sa_family_t a= f, bool display_form);
= -
-const char *proto2as= cii_all(struct gc_arena *gc);
-
-const char *p= roto_remote(int proto, bool remote);
-
-const = char *addr_family_name(int af);
-
-/*- * Overhead added to packets b= y various protocols.
- = */
-static inline int
-datagram_overhead(sa_fa= mily_t af, int proto)
-= {
- int overhead =3D= 0;
- overhead +=3D = (proto =3D=3D PROTO_UDP) ? 8 : 20;
- overhead +=3D (af =3D=3D AF_INET) ? 20 : 40;
- return overhead;

-}

-
-/*=
- * Misc inline functi= ons
- */
-

-static inline bool
-link_socket_proto_connection_oriented(int proto)
<= span style=3D"color: hsl(0, 100%, 40%);">-{

- return !proto_is_dgram(proto);
-}
-
static inline bool
link_s= ocket_connection_oriented(const struct link_socket *sock)
= {
@@ -613,242 +388,6 @@
}
}


-= addr_defined(const struct openvpn_sockaddr *addr)
-{
- if (!addr)
= - {
- return = 0;
- }
- switch (addr->addr.sa.sa_fam= ily)

- {
<= span style=3D"color: hsl(0, 100%, 40%);">- case AF_INET:

<= span style=3D"color: hsl(0, 100%, 40%);">- return addr->addr.= in4.sin_addr.s_addr !=3D 0;

-
- case AF= _INET6:
- re= turn !IN6_IS_ADDR_UNSPECIFIED(&addr->addr.in6.sin6_addr);
= -
- default:
- return 0;
- }
= -}
-
-static inline bool
-addr_local(const struct sockaddr *addr)
-{
- if (!addr)
- {
- return false;
- }
- swi= tch (addr->sa_family)
- case = AF_INET:
- r= eturn ((const struct sockaddr_in *)addr)->sin_addr.s_addr =3D=3D htonl(I= NADDR_LOOPBACK);
-
- case AF_INET6:
- return IN6_IS= _ADDR_LOOPBACK(&((const struct sockaddr_in6 *)addr)->sin6_addr);
-
- default:
- return false;
- }
-}
-<= br>-
-static inline bool
-addr_defined_ipi(const struct link_socket_actual *lsa= )
-{
-#if ENABLE_IP_PKTINFO
- if (!lsa)
- {
- return 0;
- }
- switc= h (lsa->dest.addr.sa.sa_family)
- {
-#if= defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST)
<= span style=3D"color: hsl(0, 100%, 40%);">- case AF_INET:

<= span style=3D"color: hsl(0, 100%, 40%);">- return lsa->pi.in4= .ipi_spec_dst.s_addr !=3D 0;

-
-#elif defined(= IP_RECVDSTADDR)
- = case AF_INET:
- = return lsa->pi.in4.s_addr !=3D 0;
-
-= #endif
- case AF= _INET6:
- re= turn !IN6_IS_ADDR_UNSPECIFIED(&lsa->pi.in6.ipi6_addr);
-

- default:
- return 0;
- }
-#e= lse /* if ENABLE_IP_PKTINFO */
- ASSERT(0);
-#endif
- return fa= lse;
-}
-

-static inline bool
-link_socket_actual_defined(const struct link_socket_actual *a= ct)
-{
- return act && addr_defined= (&act->dest);
-}=
-
-static inline bool
-addr_match(const struct openvpn_sockaddr *a= 1, const struct openvpn_sockaddr *a2)
-{
- = switch (a1->addr.sa.sa_family)
- {
- = case AF_INET:
- = return a1->addr.in4.sin_addr.s_addr =3D=3D a2->addr.in4.sin_a= ddr.s_addr;
-- case AF_INET6:- return IN6_ARE_ADD= R_EQUAL(&a1->addr.in6.sin6_addr, &a2->addr.in6.sin6_addr);
- }
- ASSERT(0);
- return false;
-}
-
-static inline boo= l
-addrlist_match(const= struct openvpn_sockaddr *a1, const struct addrinfo *addrlist)
-{

- const struct addrinfo *curele;
- for (curele =3D addrlist; curele; cur= ele =3D curele->ai_next)
- {
- sw= itch (a1->addr.sa.sa_family)
- {
- = case AF_INET:
- if (a1->addr.in4.sin_addr.s_addr
- =3D=3D ((struct sockadd= r_in *)curele->ai_addr)->sin_addr.s_addr)
- {
- return true;
- }
- break;
-
- case AF_INET6:
- if (IN6_ARE_ADDR_EQUAL(&a1->addr.in6= .sin6_addr,
- = &((struct sockaddr_in6 *)curele->ai_add= r)->sin6_addr))
- = {
- = return true;
- }
-= break;
= -
- default:=
- ASSER= T(0);
- }=
- }
- return false;
-}
-
-static inline= bool
-addrlist_port_ma= tch(const struct openvpn_sockaddr *a1, const struct addrinfo *a2)-{
- const struct addrinfo *curele;
- for (curele =3D a2; curele; curele= =3D curele->ai_next)
- switc= h (a1->addr.sa.sa_family)
- {
- = case AF_INET:
- = if (curele->ai_family =3D=3D AF_INET
- && a1->addr= .in4.sin_addr.s_addr
- = =3D=3D ((struct sockaddr_in *)curele->ai_addr)= ->sin_addr.s_addr
- = && a1->addr.in4.sin_port =3D=3D ((struct sock= addr_in *)curele->ai_addr)->sin_port)
- {
- return true;
- }
- break;
-
- case AF_INET6:
- if (curele->ai_family =3D=3D AF_INET6- &&= IN6_ARE_ADDR_EQUAL(&a1->addr.in6.sin6_addr,
- &= amp;((struct sockaddr_in6 *)curele->ai_addr)->sin6_addr)
- && a1-= >addr.in6.sin6_port
= - =3D=3D ((struct sockaddr_in6 *)curele->ai_ad= dr)->sin6_port)
- = {
- = return true;
- }
-= break;
= -
- default:=
- ASSER= T(0);
- }=
- }
- return false;
-}
-
-
-static inline bool
-addr_port_match(const struct openvpn_= sockaddr *a1, const struct openvpn_sockaddr *a2)
-{
- switch (a1->addr.sa.sa_family)
- {
- case AF_INET:
- return a1->addr.in4.sin_addr.s_addr =3D=3D a2->add= r.in4.sin_addr.s_addr
-= && a1->addr.in4.sin_port =3D=3D a2->addr.i= n4.sin_port;
-- case AF_INET6:<= br>- return IN6_ARE_AD= DR_EQUAL(&a1->addr.in6.sin6_addr, &a2->addr.in6.sin6_addr)

- &= & a1->addr.in6.sin6_port =3D=3D a2->addr.in6.sin6_port;- }
- ASSERT(0);
- return false;
-}
-

-static inline bool
-addr_match_proto(const stru= ct openvpn_sockaddr *a1, const struct openvpn_sockaddr *a2,
- const int proto)
-{
- return link_socket_proto_connection_ori= ented(proto) ? addr_match(a1, a2)
- : addr_= port_match(a1, a2);
-}<= /span>
-
-
-static inline bool
-addrlist_match_proto(const struct openvpn_sockaddr *a1, struct addr= info *addr_list, const int proto)
-{
- retu= rn link_socket_proto_connection_oriented(proto) ? addrlist_match(a1, addr_l= ist)
- = : addrlist_port_match(a1, addr_list)= ;
-}
-
-static inline void
-addr_zero_host(struct openvpn_sockaddr *addr)
-{
- switch (addr->addr.sa.sa_family)
- {
- case AF_INET:
- addr->addr.in4.sin_addr.s_addr =3D 0;

- break;-
- case AF_INET6:
- memset(&addr->addr.in6.sin6_add= r, 0, sizeof(struct in6_addr));
- break;
- }
-}<= br>-
-static inline int
-af_addr_size(sa_family_t af)
-{
- switch (af)
- {
- case A= F_INET:
- re= turn sizeof(struct sockaddr_in);
-
- ca= se AF_INET6:
- = return sizeof(struct sockaddr_in6);
-
- = default:
-#if 0
- /* could be c= alled from socket_do_accept() with empty addr */
- msg(M_ERR, "Bad address family:= %d\n", af);
- = ASSERT(0);
-#en= dif
- return= 0;
- }
-}

-
-= static inline bool
-lin= k_socket_actual_match(const struct link_socket_actual *a1, const struct lin= k_socket_actual *a2)
-{=
- return addr_port_= match(&a1->dest, &a2->dest);
-}
= -
#if PORT_SHARE

static= inline bool
diff --git a/src/openvpn/socket_util.c b/src/o= penvpn/socket_util.c
new file mode 100644
i= ndex 0000000..13f8c32
--- /dev/null
+++ b/s= rc/openvpn/socket_util.c
@@ -0,0 +1,936 @@
+/*
+ * OpenVPN -- An application to securely tunnel IP n= etworks
+ * = over a single TCP/UDP port, with support for SSL/TLS-based
+ * session authentica= tion and key exchange,

+ * packet compressi= on.
+ *
+ * Copyright (C) 2002-2025 OpenV= PN Inc <sales@...515...>
+ *
+ * Th= is program is free software; you can redistribute it and/or modify+ * it under the terms of th= e GNU General Public License version 2
+ * as published by the Free Software Foundation.=
+ *
+ * This program is distributed in the ho= pe that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY o= r FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of= the GNU General Public License along
+ * with this program; if not, see <https://www.gnu.or= g/licenses/>.
+ */=
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"<= br>+#endif
+
+#include "syshead.h"
+
+#include "socket_util.h"
+#include "crypto.h"
+#include "manage.h"
<= span style=3D"color: hsl(120, 100%, 40%);">+

+/*
+ * Format IP addresses in ascii
+ */
+
+const char *
+print_sockaddr_ex(cons= t struct sockaddr *sa, const char *separator, const unsigned int flags,

+ struc= t gc_arena *gc)
+{
+ struct buffer out = =3D alloc_buf_gc(128, gc);
+ bool addr_is_defined =3D false;
+ char hostaddr[NI_MAXHOST] =3D "";
+ char servname[NI_MAX= SERV] =3D "";
+ int status;
+=
+ socklen_t salen= =3D 0;
+ switch (= sa->sa_family)
+ = {
+ case AF_= INET:
+ if= (!(flags & PS_DONT_SHOW_FAMILY))
+ {
+ buf_puts(&out, "[AF_INET]");
+ }
= + salen =3D sizeof(s= truct sockaddr_in);
+= addr_is_defined =3D ((struct sockaddr_in *)sa)->sin_addr.s_a= ddr !=3D 0;
+ = break;
++ case AF_INET6:
+ if (!(flags &= amp; PS_DONT_SHOW_FAMILY))
+ {
= + buf_puts(&out, "[AF_INET6]");
+ }

+ salen =3D sizeof(struct sock= addr_in6);
+ = addr_is_defined =3D !IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 = *)sa)->sin6_addr);
+ break;
= +
+ case AF_UN= SPEC:
+ if= (!(flags & PS_DONT_SHOW_FAMILY))
+ {
+ return "[AF_UNSPEC]";
+ }

+ else
+ {
+ return "";
+ }

+
+ default:
+ ASSERT(0);
+ }
+ status =3D g= etnameinfo(sa, salen, hostaddr, sizeof(hostaddr), servname, sizeof(servname= ),
+ = NI_NUMERICHOST | NI_NUMERICSERV);
+
+ {
+ = buf_printf(&out, "[nameinfo() err: %s]", gai_strerror(status)= );
+ return BS= TR(&out);
+ }<= /span>
+
+ if (!(flags & PS_DONT_SHOW_ADDR= ))
+ {
<= span style=3D"color: hsl(120, 100%, 40%);">+ if (addr_is_defined)
+ {
+ buf_puts(&out, h= ostaddr);

+ }<= /span>
+ else<= br>+ {
+ buf_puts(&out, "= [undef]");
+ = }
+ }+
+ if ((flags & PS_SHOW_PORT) || (flags &a= mp; PS_SHOW_PORT_IF_DEFINED))
+ {
+ = if (separator)
+ = {
+ = buf_puts(&out, separator);
+ }
+
+ buf_puts(= &out, servname);
= + }
+
+ return BSTR(&out);
+}
+
+const char *
+print_link_socket_actual(const struct link_socket_actual *act, stru= ct gc_arena *gc)
+{
+ return print_link= _socket_actual_ex(act, ":", PS_SHOW_PORT | PS_SHOW_PKTINFO, gc);<= /span>
+}
+
+#ifndef IF_NAMESIZE
+#define IF_NAMESIZE 16
+#endif
+
+const char = *
+print_link_socket_= actual_ex(const struct link_socket_actual *act, const char *separator,
+ = const unsigned int flags, struct gc_arena *gc)
+{
+ if (act)
+ {
+ = struct buffer out =3D alloc_buf_gc(128, gc);
+ buf_printf(&out, "%s", print= _sockaddr_ex(&act->dest.addr.sa, separator, flags, gc));
<= span style=3D"color: hsl(120, 100%, 40%);">+#if ENABLE_IP_PKTINFO
+ char ifname[IF_NAMESI= ZE] =3D "[undef]";
+
+ if= ((flags & PS_SHOW_PKTINFO) && addr_defined_ipi(act))+ {
+ switch (act->dest.addr.s= a.sa_family)
+ = {
+ = case AF_INET:
+ = {
+ = struct openvpn_sockaddr sa;
+ CLEAR(sa);
+ sa.addr.in4.sin_famil= y =3D AF_INET;
+#if d= efined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST)
+ sa.addr.in4.s= in_addr =3D act->pi.in4.ipi_spec_dst;

+ if_indextoname(act->pi.in4.ipi_= ifindex, ifname);
+#e= lif defined(IP_RECVDSTADDR)
+ sa.addr.in4.sin_addr =3D act->pi.in4;=
+ ifname= [0] =3D 0;
+#else /* = if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST) */=
+#error ENABLE_IP_PKTINFO i= s set without IP_PKTINFO xor IP_RECVDSTADDR (fix syshead.h)
+#endif

+ buf_printf(&out, " = (via %s%%%s)",
+= print_sockaddr_ex(&sa.addr.sa, separator= , 0, gc), ifname);
+ = }
+ = break;
+=
+ cas= e AF_INET6:
+ = {
+ = struct sockaddr_in6 sin6;
+ char buf[INET6_ADDRSTRLEN] =3D "[u= ndef]";
+ = CLEAR(sin6);
+ sin6.sin6_family =3D AF_INET6;
+ sin6.sin6_addr =3D= act->pi.in6.ipi6_addr;
+ if_indextoname(act->pi.in6.ipi6_ifindex, ifna= me);
+ = if (getnameinfo((struct sockaddr *)&sin6, sizeof(struct sockaddr_i= n6), buf,
+ = sizeof(buf), NULL, 0, NI_NUMERICHOST)
<= span style=3D"color: hsl(120, 100%, 40%);">+ =3D=3D = 0)

+ = {
+ = buf_printf(&out, " (via %s%%%s)", buf, ifname);
+ }
+ el= se
+ = {
+ = buf_printf(&out, " (via [getnameinfo() err]%%%s)", i= fname);
+ = }
+ = }
+ = break;
+ = }
+ }
+#endif /* if ENABLE_IP_PK= TINFO */
+ ret= urn BSTR(&out);
+= }
+ else
+ {
+ return "[NULL]";
+ }
+}
+
+= /*
+ * Convert an in_= addr_t in host byte order
+ * to an ascii dotted quad.
+ */
+co= nst char *
+print_in_= addr_t(in_addr_t addr, unsigned int flags, struct gc_arena *gc)
<= span style=3D"color: hsl(120, 100%, 40%);">+{

+ struct in_addr ia;
+ char *out =3D gc_malloc(INET_ADDRSTRLEN, t= rue, gc);
++ if (addr || !(flags &= IA_EMPTY_IF_UNDEF))
= + {
+ CLEAR= (ia);
+ ia.s_a= ddr =3D (flags & IA_NET_ORDER) ? addr : htonl(addr);
+
+ inet_ntop(AF_INET, &ia, out, INET_ADDRSTRLEN)= ;
+ }
+ return out;

+}
+
+/*
+ * Convert an i= n6_addr in host byte order
+ * to an ascii representation of an IPv6 address
+ */
+const char *
+print_in6_addr(struct in6_addr a6, unsigned int flags, struct = gc_arena *gc)
+{
+ char *out =3D gc_mal= loc(INET6_ADDRSTRLEN, true, gc);
+
+ if= (memcmp(&a6, &in6addr_any, sizeof(a6)) !=3D 0 || !(flags & IA_= EMPTY_IF_UNDEF))
+ = {
+ inet_ntop= (AF_INET6, &a6, out, INET6_ADDRSTRLEN);
+ }
+ return out;
+}
+
<= span style=3D"color: hsl(120, 100%, 40%);">+/*

+ * Convert an in_port_t in host byte order to a = string
+ */+const char *
+print_in_port_t(in_port_t port, stru= ct gc_arena *gc)
+{
+ struct buffer buf= fer =3D alloc_buf_gc(8, gc);
+ buf_printf(&buffer, "%hu", port);
+ return BSTR(&buffer);
+}
+
+/* add some offset to an ipv6 address
+ * (add in steps of 8 bits, taking overfl= ow into next round)
+= */
+struct in6_addr<= /span>
+add_in6_addr(struct = in6_addr base, uint32_t add)
+{
+ int i= ;
+
+ for (i =3D 15; i >=3D 0 &&= amp; add > 0; i--)
+ {
+ regi= ster int carry;
+ = register uint32_t h;
+
+ h =3D = (unsigned char)base.s6_addr[i];
+ base.s6_addr[i] =3D (h + add) & UINT8_MAX;+
+ /* using explicit carry for the 8-bit a= dditions will catch
+= * 8-bit and(!) 32-bit overruns nicely
+ */
+ carry =3D ((h & 0xff) + (add & 0xff)) &g= t;> 8;
+ ad= d =3D (add >> 8) + carry;
+ }
+ = return base;
+}
+
+/* set environmental variables for ip/por= t in *addr */
+void
+setenv_sockaddr(struc= t env_set *es, const char *name_prefix, const struct openvpn_sockaddr *addr= ,
+ co= nst unsigned int flags)
+{
+ char name_= buf[256];
++ char buf[INET6_ADDRSTRLEN= ];
+ switch (addr-= >addr.sa.sa_family)
+ cas= e AF_INET:
+ = if (flags & SA_IP_PORT)
+ {
+ snprintf(name_buf, sizeof(name_buf), "%s_ip"= , name_prefix);
+ = }
+ = else
+ {=
+ snp= rintf(name_buf, sizeof(name_buf), "%s", name_prefix);
<= span style=3D"color: hsl(120, 100%, 40%);">+ }

+
+ inet_ntop(AF_INET, &addr->addr.in4.si= n_addr, buf, sizeof(buf));
+ setenv_str(es, name_buf, buf);
+
+ if ((flags & SA_IP_PORT) && addr->add= r.in4.sin_port)
+ = {
+ = snprintf(name_buf, sizeof(name_buf), "%s_port", name_prefix= );
+ s= etenv_int(es, name_buf, ntohs(addr->addr.in4.sin_port));
+ }

+ break;
+
+ case AF_INET6:
+ if (IN6_IS_ADDR_V4MAPPED(&addr->addr.i= n6.sin6_addr))
+ = {
+ = struct in_addr ia;
+ memcpy(&ia.s_addr, &addr->addr.in6.sin6_addr= .s6_addr[12], sizeof(ia.s_addr));
+ snprintf(name_buf, sizeof(name_buf), "%s= _ip", name_prefix);
+ inet_ntop(AF_INET, &ia, buf, sizeof(buf));<= br>+ }
+ else

+ {
+ snprintf(name_buf, sizeof= (name_buf), "%s_ip6", name_prefix);
+ inet_ntop(AF_INET6, &addr->= ;addr.in6.sin6_addr, buf, sizeof(buf));
+ }
+ setenv_str(es, name_buf, buf);
+
+ if ((flags & SA_IP_PORT) && addr-= >addr.in6.sin6_port)
+ {
+ = snprintf(name_buf, sizeof(name_buf), "%s_port", nam= e_prefix);
+ = setenv_int(es, name_buf, ntohs(addr->addr.in6.sin6_port));=
+ }
+ break;

+ }

+}
+
+void
+setenv_in_addr_t(struct = env_set *es, const char *name_prefix, in_addr_t addr,
+ const unsigned int flags= )
+{
+ if (addr || !(flags & SA_SET= _IF_NONZERO))
+ {<= /span>
+ struct openv= pn_sockaddr si;
+ = CLEAR(si);
+ = si.addr.in4.sin_family =3D AF_INET;
+ si.addr.in4.sin_addr.s_addr =3D htonl(addr);
+ setenv_sockaddr= (es, name_prefix, &si, flags);
+ }
= +}
+
+void
+setenv_in6_addr(struct env_set *es, const char *nam= e_prefix, const struct in6_addr *addr,
+ const unsigned int flags)
+{

+ if (!IN6_IS_ADDR_UNSPECIFIED(addr) || !(flags &a= mp; SA_SET_IF_NONZERO))
+ {
+ st= ruct openvpn_sockaddr si;
+ CLEAR(si);
+ si.addr.in6.sin6_family =3D AF_INET6;
+ si.addr.in6.sin6_addr =3D *addr;
+ setenv_sockaddr= (es, name_prefix, &si, flags);
+ }
= +}
+
+void
+setenv_link_socket_actual(struct env_set *es, const= char *name_prefix,
+= const struct link_socket_actual *act, const unsig= ned int flags)
+{
+ setenv_sockaddr(es,= name_prefix, &act->dest, flags);
+}
+/*
+ * Convert protocol names between= index and ascii form.

+
+struct proto_names

+{

+ const char *short_form;
+ const char *display_form;
<= span style=3D"color: hsl(120, 100%, 40%);">+ sa_family_t proto_af;
+ int proto;
= +};
+
+/* Indexed by PROTO_x */
+static const struct proto_names proto_names[] =3D {+ { "proto-uninitializ= ed", "proto-NONE", AF_UNSPEC, PROTO_NONE },
+ /* try IPv4 and IPv6 (client), b= ind dual-stack (server) */
+ { "udp", "UDP", AF_UNSPEC, PROTO_UDP },
+ { "tcp-server&q= uot;, "TCP_SERVER", AF_UNSPEC, PROTO_TCP_SERVER },
+ { "tcp-client", &quo= t;TCP_CLIENT", AF_UNSPEC, PROTO_TCP_CLIENT },

+ { "tcp", "TCP", AF_U= NSPEC, PROTO_TCP },
+= /* force IPv4 */
= + { "udp4", "UDPv4", AF_INET, PROTO_UDP },+ { "tcp4-server"= , "TCPv4_SERVER", AF_INET, PROTO_TCP_SERVER },
+ { "tcp4-client", "T= CPv4_CLIENT", AF_INET, PROTO_TCP_CLIENT },
+ { "tcp4", "TCPv4", AF_I= NET, PROTO_TCP },
+ = /* force IPv6 */
+ = { "udp6", "UDPv6", AF_INET6, PROTO_UDP },
= + { "tcp6-server",= "TCPv6_SERVER", AF_INET6, PROTO_TCP_SERVER },
+ { "tcp6-client", "T= CPv6_CLIENT", AF_INET6, PROTO_TCP_CLIENT },
+ { "tcp6", "TCPv6", AF_= INET6, PROTO_TCP },
+= };
+
+int
+ascii2proto(const char *proto_name)
+{
+ for (size_t i =3D 0; i < SIZE(proto_names); ++i= )
+ {
+ if (!strcmp(proto_name, = proto_names[i].short_form))

+ {
+ = return proto_names[i].proto;
+ }
+ }
+ r= eturn -1;
+}+
+sa_family_t
+ascii2af(const char *proto_name)
+{
+ for (size_t i =3D 0; i < SIZE(proto_names); ++i)
+ {
+ if (!strcmp(proto_name, proto= _names[i].short_form))
+ = return proto_names[i].proto_af;
+ }
+ }
+ ret= urn 0;
+}
<= span style=3D"color: hsl(120, 100%, 40%);">+

+const char *
+proto2ascii(int proto, sa_family_t af, bool display_form)=
+{
+ for (size_t i =3D 0; i < SIZE(= proto_names); ++i)
+ = {
+ if (pro= to_names[i].proto_af =3D=3D af && proto_names[i].proto =3D=3D proto= )
+ {+ if (display_form= )
+ {
+ return p= roto_names[i].display_form;
+ }
+ else
+ = {
+ = return proto_names[i].short_form;
+ }
+ }
+ }
+=
+ return "[unknown = protocol]";
+}
+
+const char *
+proto2ascii_all(struct gc_arena *gc)
= +{
+ struct buffer out =3D alloc_buf_gc(256, gc);=
+
+ for (size_t i =3D 0; i < SIZE(p= roto_names); ++i)
+ = {
+ if (i)
+ {
<= span style=3D"color: hsl(120, 100%, 40%);">+ buf_printf(&out= , " ");

+ = }
+ buf_= printf(&out, "[%s]", proto_names[i].short_form);
+ }

+ return BSTR(&out);
+}
+
+c= onst char *
+addr_fam= ily_name(int af)
+{
+ switch (af)
+ {
+ case AF_INET:
+ return "AF_INET"= ;;
+
+ case AF_INET6:
+ return "AF_INET6= ";

+ }=
+ return "AF_UNSPEC= ";
+}
= +
+/*
+ * Given a local proto, return local proto
+ * if !remote, or compatible remote proto=
+ * if remote.
+ *
+ * This is used for options compatibility=
+ * checking.=
+ *
+ * IPv6 and IPv4 protocols are comptabile= but OpenVPN
+ * has = always sent UDPv4, TCPv4 over the wire. Keep these
+ * strings for backward compatibility=
+ */
+const char *
+proto_remote(int proto, bool remote)
<= span style=3D"color: hsl(120, 100%, 40%);">+{

+ ASSERT(proto >=3D 0 && proto < = PROTO_N);
+ if (pr= oto =3D=3D PROTO_UDP)
+ {
+ retu= rn "UDPv4";
+ }
+
<= span style=3D"color: hsl(120, 100%, 40%);">+ if ((remote && prot= o =3D=3D PROTO_TCP_CLIENT) || (!remote && proto =3D=3D PROTO_TCP_SE= RVER))

+ {<= br>+ return "TCPv4_= SERVER";
+ }<= /span>
+ if ((remote &= ;& proto =3D=3D PROTO_TCP_SERVER) || (!remote && proto =3D=3D P= ROTO_TCP_CLIENT))
+ = {
+ return &= quot;TCPv4_CLIENT";
+ }
++ ASSERT(0);
+ return ""; /* Make = the compiler happy */

+}
+
+/**

+ * Small helper function for openvpn_getaddrinfo to= print the address
+ = * family when resolving fails
+ */
+static= const char *
+getadd= rinfo_addr_family_name(int af)
+{
+ swi= tch (af)
+ {
+ case AF_INET:
+ return &quo= t;[AF_INET]";
+<= /span>
+ case AF_INET= 6:
+ retur= n "[AF_INET6]";
+ }
+ retu= rn "";
+}
+
+/*
+ * Prepend a random string to hostname to prevent DNS cac= hing.
+ * For example= , foo.bar.gov would be modified to <random-chars>.foo.bar.gov.=
+ * Of course, this require= s explicit support in the DNS server (wildcard).
+ */
+static const char *
+hostname_randomize(const char *hostname, struct gc_arena *gc)<= /span>
+{
+#define n_rnd_bytes 6
+
+ uint8_t rnd_bytes[n_rnd_bytes];
+ const char *rnd_str;
+ struct buffer hname =3D alloc_= buf_gc(strlen(hostname) + sizeof(rnd_bytes) * 2 + 4, gc);

+
+ prng_bytes(rnd_bytes, sizeof(rnd_bytes));
= + rnd_str =3D format_hex_ex(= rnd_bytes, sizeof(rnd_bytes), 40, 0, NULL, gc);
+ buf_printf(&hname, "%s.%s", r= nd_str, hostname);
+ = return BSTR(&hname);
+#undef n_rnd_bytes
+}
+
= +/*
+ * Translate IPv4/IPv6 addr or hostname into st= ruct addrinfo
+ * If = resolve error, try again for resolve_retry_seconds seconds.
+ */

+int
+openvpn_getaddrinfo(unsigned int flags, const char *hostname, const= char *servname,
+ = int resolve_retry_seconds, struct signal_info *sig_info, i= nt ai_family,
+ = struct addrinfo **res)
+{
+ = struct addrinfo hints;
+ int status;
+ struct signal_info sigrec =3D { 0 };
+ int msglevel =3D (flags & GETADDR_FATAL) ? M= _FATAL : D_RESOLVE_ERRORS;
+ struct gc_arena gc =3D gc_new();
+ const char *print_hostname;
+ const char *print_servname;<= br>+
+ ASSERT(res);
+
+ ASSERT(hostname || servname);
+ ASSERT(!(flags & GETADDR_HOST_ORDER));<= br>+
+ if (servname)
+ {
+ print_servname =3D servname;
+ }
+ else
+ {
+ p= rint_servname =3D "";
+ }
+
+ if (flags & G= ETADDR_MSG_VIRT_OUT)
= + {
+ msgle= vel |=3D M_MSG_VIRT_OUT;
+ }
++ if ((flags & (GETADD= R_FATAL_ON_SIGNAL | GETADDR_WARN_ON_SIGNAL)) && !sig_info)+ {
+ sig_info =3D &sigrec;<= br>+ }
+
+ /* try numeric ip addr first */
+ CLEAR(hints);
+ hints.ai_flags =3D AI_NUMERICHOST;<= br>+
+ if (flags & GETADDR_PASSIVE)+ {
+ hints.ai_flags |=3D AI_PASSIVE;
+ }
+

+ if (flags & GETADDR_DATAGRAM)
+ {
+ hints.ai_socktype =3D SOCK_DGRAM;+ }
+ else
+ {
+ hints.ai_socktype =3D SOCK_STREAM;
+ }
+
= + /* if hostname is not set, we want to bind to 'ANY', with
+ * the correct addre= ss family - v4-only or v6/v6-dual-stack */
+ if (!hostname)
+ {
+ hints.ai_family =3D ai_family;
+ }
+
+ st= atus =3D getaddrinfo(hostname, servname, &hints, res);
+
+ if (status !=3D 0) /* parse as= numeric address failed? */
+ {
+ = const int fail_wait_interval =3D 5; /* seconds */
+ /* Add +4 to cause integer divisi= on rounding up (1 + 4) =3D 5, (0+4)/5=3D0 */
+ int resolve_retries =3D
+ (flags & GETADDR_TRY_O= NCE) ? 1 : ((resolve_retry_seconds + 4) / fail_wait_interval);
+ const char *fmt;
<= br>+ int level =3D 0;
+
+ /* this is not a numeric IP, ther= efore force resolution using the
+ * provided ai_family */
+ hints.ai_family =3D ai_family;
= +
+ if (hostname && (flags & GETA= DDR_RANDOMIZE))
+ = {
+ ho= stname =3D hostname_randomize(hostname, &gc);
+ }
+
= + if (hostname)
+ = print_hostname =3D hostname;
+ }
+ else
+ = {
+ p= rint_hostname =3D "undefined";
+ }
+
+ = fmt =3D "RESOLVE: Cannot resolve host address: %s:%s%s (%s)";
+ if ((flags &= ; GETADDR_MENTION_RESOLVE_RETRY) && !resolve_retry_seconds)<= br>+ {
+ fmt =3D "RESOLVE: Ca= nnot resolve host address: %s:%s%s (%s)"
+ "(I would have retried thi= s name query if you had "
+ "specified the --resolv-retry option.)&qu= ot;;
+ }
+
+ if (!(flags & GETADDR_RESOLVE) |= | status =3D=3D EAI_FAIL)
+ {
+ = msg(msglevel, "RESOLVE: Cannot parse IP address: %s:%s (%s)&qu= ot;, print_hostname,
= + print_servname, gai_strerror(status));
+ goto done;
+ }
+
+#ifdef ENABLE_MANAGEMENT
+ if (flags & GETADDR_UPDATE_MANAGEMENT_STATE)
+ {
+ if (management)
+ {
<= span style=3D"color: hsl(120, 100%, 40%);">+ management_set_= state(management, OPENVPN_STATE_RESOLVE, NULL, NULL, NULL, NULL,

= + = NULL);
+ = }
+ }
+#endif
+

+ /*
+ * Resolve hostname
+ */
+ while (true)
+ {
+ = /* force resolv.conf reload */
+ res_init();
+#endif
+ /* try hostname lookup */
+ hints.ai_flags &=3D ~AI_NUMER= ICHOST;
+ = dmsg(D_SOCKET_DEBUG, "GETADDRINFO flags=3D0x%04x ai_family=3D%d ai_soc= ktype=3D%d", flags,
+ hints.ai_family, hints.ai_socktype);
+ status =3D getaddrinfo(h= ostname, servname, &hints, res);
+
+ = if (sig_info)
+ {
+ = get_signal(&sig_info->signal_received);
+ if (sig_info->s= ignal_received) /* were we interrupted by a signal? */

+ {
+ /* why are we overwri= ting SIGUSR1 ? */
+ = if (signal_reset(sig_info, SIGUSR1) =3D=3D SIGUSR1) /* ig= nore SIGUSR1 */
+ = {
+ = msg(level, "RESOLVE: Ignored SIGUSR1 signal rece= ived during "
+ = "DNS resolution attempt");
+ }
+ el= se
+ = {
+ = /* turn success into failure (interrupted syscall) */
+ if (0 = =3D=3D status)

+ = {
= + ASSERT(res);
+ freeaddrinfo(*res);
+ = *res =3D NULL;
+ = status =3D EAI_AGAIN; /* =3D temporary failure */=
+ = errno =3D EINTR;
+ }
+ goto done;
+ }
+ }
+ }
+
+ /* success? */
+ if (0 =3D=3D status)
+ {
+ break;
+ }
+
+ = /* resolve lookup failed, should we
+ * continue or fail? */
+ level =3D msglevel;
+ if (resolve_r= etries > 0)
+ = {
+ = level =3D D_RESOLVE_ERRORS;
+ }
+
+ = msg(level, fmt, print_hostname, print_servname, getaddrinfo_addr_family_nam= e(ai_family),
+ = gai_strerror(status));
+
+ = if (--resolve_retries <=3D 0)
+ {
+ goto done;
+ }
+
+ = management_sleep(fail_wait_interval);
+ }
+
+ = ASSERT(res);
+
+ /* hostname re= solve succeeded */
+<= /span>
+ /*+ * Do not choose an I= P Addresse by random or change the order *
+ * of IP addresses, doing so will break RFC = 3484 address selection *
+ */
+ = }
+ else+ {
+ /* IP address parse succeeded */<= /span>
+ if (flags &a= mp; GETADDR_RANDOMIZE)
+ = msg(M_WARN, "WARNING: ignoring --remote-random-hostname because t= he "
+ = "hostname is an IP address");
+ }
+ }
+
+done:
+ if (sig_info &&= amp; sig_info->signal_received)
+ {
= + int level =3D 0;
+ if (flags & GETADDR_FATAL_ON_SIGNAL)
+ {
+ level =3D M_FATAL;
+ }
+ else if (flags & GETADDR_WARN_ON_SIGN= AL)
+ {=
+ level =3D M_WA= RN;
+ }=
+ msg(level, "R= ESOLVE: signal received during DNS resolution attempt");
+ }

+
+ gc_free(&gc);
+ return status;
+}
+<= br>+/*
+ * We do our own inet_aton because the glibc= function
+ * isn'= ;t very good about error checking.
+ */
+i= nt
+openvpn_inet_aton= (const char *dotted_quad, struct in_addr *addr)
+{
+ unsigned int a, b, c, d;
+
+= CLEAR(*addr);
+ = if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, = &d) =3D=3D 4)
+ = {
+ if (a &l= t; 256 && b < 256 && c < 256 && d < 256)
+ {
<= span style=3D"color: hsl(120, 100%, 40%);">+ addr->s_addr =3D= htonl(a << 24 | b << 16 | c << 8 | d);

+ return OIA_IP; /* good do= tted quad */
+ = }
+ }
<= span style=3D"color: hsl(120, 100%, 40%);">+ if (string_class(dotted_qua= d, CC_DIGIT | CC_DOT, 0))

+ {
+ = return OIA_ERROR; /* probably a badly formatted dotted quad */
+ }

+ else
+ {
+ return OIA_HOSTNAME; /* probably a hostname */
+ }
+}
+
+bool<= br>+ip_addr_dotted_quad_safe(co= nst char *dotted_quad)
+ /* verify n= on-NULL */
+ if (!= dotted_quad)
+ {
+ return false;=
+ }
+

+ /* verify length is within limits */
<= span style=3D"color: hsl(120, 100%, 40%);">+ if (strlen(dotted_quad) >= ; 15)

+ {+ return false;=
+ }
+
+ /* verify that all chars are either numeric or '.&#= 39; and that no numeric
+ * substring is greater than 3 chars */
+ {
+ int nnum =3D 0;
+ const char *p =3D dotted_quad;
+ int c;
+
+ while ((c =3D *p++))
+ {
+ if (c >=3D '0' && c <=3D &#= 39;9')
+ = {
+ = ++nnum;
+ = if (nnum > 3)
+ {
+= return false;
+ }
+ }
+ else if (c =3D=3D '.')
+ {
+ nnum =3D 0;
+ }
+ else
+ {
+ return false;
+ }
+ }
+ }
+
+ /* verify that stri= ng will convert to IP address */
+ {
+ = struct in_addr a;
+ return openvpn_inet_aton(dotted_quad, &a) =3D=3D OIA_IP;
+ }
+}

+
+ipv6_addr_sa= fe(const char *ipv6_text_addr)
+{
+ /* = verify non-NULL */
+ = if (!ipv6_text_addr)
+ {
+ re= turn false;
+ }
+
+ /* verify length is within limits */<= /span>
+ if (strlen(ipv6_= text_addr) > INET6_ADDRSTRLEN)
+ {
+= return false;
+ }
+
<= span style=3D"color: hsl(120, 100%, 40%);">+ /* verify that string will = convert to IPv6 address */

+ {
+ = struct in6_addr a6;
= + return inet_pton(AF_INET6, ipv6_text_addr, &a6) =3D=3D 1;
+ }
+}
+
+= static bool
+dns_addr= _safe(const char *addr)
+{
+ if (addr)<= /span>
+ {
+ const size_t len =3D strle= n(addr);

+ ret= urn len > 0 && len <=3D 255 && string_class(addr, CC_= ALNUM | CC_DASH | CC_DOT, 0);
+ }
+ = else
+ {+ return false;<= br>+ }
+}
+
+boo= l
+ip_or_dns_addr_saf= e(const char *addr, const bool allow_fqdn)
+{
+ if (ip_addr_dotted_quad_safe(addr))
+ {
+ return true;
+ }
+ = else if (allow_fqdn)
+ ret= urn dns_addr_safe(addr);
+ }
+ else<= /span>
+ {
+ return false;

+ }

+}
+
+bool
+mac_addr_safe(const cha= r *mac_addr)
+{
+ /* verify non-NULL */=
+ if (!mac_addr)<= /span>
+ {
+ return false;

+ }

+
+ /* verify length is within limits */
+ if (strlen(mac_addr) > 17)
= + {
+ return false;
+ }
+
= + /* verify that all chars are either alphanumeric or ':' and th= at no
+ * alphanu= meric substring is greater than 2 chars */
+ {
+ int nnum =3D 0;
+ const char *p =3D mac_addr;
+ int c;
+
+ while ((c =3D *p++))
+ {
+ if ((c >=3D '0' && c <=3D '9'= ) || (c >=3D 'a' && c <=3D 'f') || (c >=3D= 'A' && c <=3D 'F'))
+ {
+ ++nnum;
+ if (nnum > 2)
+ {
+ return false;
+ }=
+ }
+ else if (c =3D=3D &#= 39;:')

+ = {
+ = nnum =3D 0;
+ = }
+ e= lse
+ {
+ return= false;
+ = }
+ }+ }
+
+ /* error-checking is left to script invoked in lladdr.c = */
+ return true;<= /span>
+}
di= ff --git a/src/openvpn/socket_util.h b/src/openvpn/socket_util.h
= new file mode 100644
index 0000000..5ea37dd--- /dev/null
+++ b/src/openvpn/socket_util.h=
@@ -0,0 +1,491 @@
+/*
+ * OpenV= PN -- An application to securely tunnel IP networks
+ * over a single TCP/UDP port= , with support for SSL/TLS-based
+ * session authentication and key exchange,=
+ * packet encr= yption, packet authentication, and
+ * packet compression.
+ *
+ * Copyright (C) 2002-2025 OpenVPN Inc <sales@...515...>= ;
+ *
+ * This program is free software; = you can redistribute it and/or modify

+ * it under the terms of the GNU General Public License = version 2
+ * as pub= lished by the Free Software Foundation.
+ *
+ * but WITHOUT ANY WARR= ANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURP= OSE. See the
+ * GN= U General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License= along
+ * with this= program; if not, see <https://www.gnu.org/licenses/>.
+ */

+
+#ifndef SOCKET_UTIL_H
+#define SOCKET_UTIL_H
+
+#include = "buffer.h"
= +#include "env_set.h"
+#include "sig.h"
+
+= #define PS_SHOW_PORT_IF_DEFINED (1 << 0)
+#define PS_SHOW_PORT (1 << 1)
+#define PS_SHOW_PKTIN= FO (1 << 2)
+#define PS_DONT_SHOW_ADDR (1 << 3)
+#define PS_DONT_SHOW_FAMILY (1 <&l= t; 4)
+
+/* OpenVPN sockaddr struct */
+struct openvpn_sockaddr=
+{
+ /*int dummy;*/ /* add offset to f= orce a bug if sa not explicitly dereferenced */
+ union
+ {
+ struct sockaddr sa;
+ struct sockaddr_in in4;
+ struct sockaddr_in6 in6;
+ } addr;
+};
+
+/* actu= al address of remote, based on source address of received packets */=
+struct link_socket_actual<= /span>
+{
+ /*int dummy;*/ /* add offset to fo= rce a bug if dest not explicitly dereferenced */
+
+ struct openvpn_sockaddr dest;
+#if ENABLE_IP_PKTINFO
+ union
+ {
+#if defined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST)=
+ struct in_pktinfo = in4;
+#elif defined(I= P_RECVDSTADDR)
+ = struct in_addr in4;
+ st= ruct in6_pktinfo in6;
+ } pi;
+#endif
+};
+
+const char *print_sockaddr_ex(const struct sockaddr *addr= , const char *separator,
+ const unsigned int flags, struct gc_aren= a *gc);
+
<= span style=3D"color: hsl(120, 100%, 40%);">+static inline const char *
+print_openvpn_sockaddr(c= onst struct openvpn_sockaddr *addr, struct gc_arena *gc)
+{
+ return print_sockaddr_ex(&addr->addr.sa, &quo= t;:", PS_SHOW_PORT, gc);
+}
+<= br>+static inline const char *<= /span>
+print_sockaddr(const= struct sockaddr *addr, struct gc_arena *gc)
+{
+ return print_sockaddr_ex(addr, ":", PS_SHOW_PORT, gc);=
+}
+
+
+= const char *print_link_socket_actual_ex(const struct link_socket_actual *ac= t, const char *separator,
+ const unsigned int flags, str= uct gc_arena *gc);
+<= /span>
+const char *print_li= nk_socket_actual(const struct link_socket_actual *act, struct gc_arena *gc)= ;
+
+
+#define IA_EMPTY_IF_UNDEF (1 << 0)
+#define IA_NET_ORDER (1 <&l= t; 1)

+const char *pr= int_in_addr_t(in_addr_t addr, unsigned int flags, struct gc_arena *gc);
+
+const char *print_in6_addr(struct in6_add= r addr6, unsigned int flags, struct gc_arena *gc);
+
+const char *print_in_port_t(in_port_t port, struct gc_arena *gc= );
+
+struct in6_addr add_in6_addr(struct = in6_addr base, uint32_t add);
+
+#define S= A_IP_PORT (1 << 0)
+#define SA_SET_IF_NONZERO (1 << 1)
+void setenv_sockaddr(struct env_set *es, = const char *name_prefix,
+ const struct openvpn_sockaddr *addr, const unsign= ed int flags);
+
+void setenv_in_addr_t(st= ruct env_set *es, const char *name_prefix, in_addr_t addr,
+ const unsigned= int flags);
+=
+void setenv_in6_addr(struc= t env_set *es, const char *name_prefix, const struct in6_addr *addr,=
+ const= unsigned int flags);
+
+void setenv_link_= socket_actual(struct env_set *es, const char *name_prefix,
+ const= struct link_socket_actual *act, const unsigned int flags);
+

+/*
+ * DNS resolution
+ */
+
+#define GETADDR_RESOLVE = (1 << 0)

+#define GETADDR_FATAL (1 << 1)
+#define GETADDR_HOST_ORDER = (1 << 2)
+#de= fine GETADDR_MENTION_RESOLVE_RETRY (1 << 3)
+#define GETADDR_FATAL_ON_SIGNAL (= 1 << 4)
+#defin= e GETADDR_WARN_ON_SIGNAL (1 << 5)
+#define GETADDR_MSG_VIRT_OUT (1 <= ;< 6)
+#define GET= ADDR_TRY_ONCE (1 << 7)
+#define GETADDR_UPDATE_MANAGEMENT_STATE (1 <<= 8)
+#define GETADDR_= RANDOMIZE (1 << 9)
+#define GETADDR_PASSIVE (1 << 10)<= /span>
+#define GETADDR_DATA= GRAM (1 << 11)
+
+#de= fine GETADDR_CACHE_MASK (GETADDR_DATAGRAM | GETADDR_PASSIVE)
+

+/**
+ * Translate an IPv4 addr or hostname from string form to in_addr_t
+ *
+ * In case of resolve error, it will t= ry again for
+ * reso= lve_retry_seconds seconds.
+ */
+in_addr_t= getaddr(unsigned int flags, const char *hostname, int resolve_retry_second= s,
+ = bool *succeeded, struct signal_info *sig_info);
+
+/**
+ * Tra= nslate an IPv6 addr or hostname from string form to in6_addr
+ */

+bool get_ipv6_addr(const char *hostname, struct in= 6_addr *network, unsigned int *netbits,
+ int msglevel);
+
+int openvpn_getaddrinfo(unsigned int flags, const char *host= name, const char *servname,
+ int resolve_retry_seconds, struct signal_in= fo *sig_info, int ai_family,
+ struct addrinfo **res);
+
+/* return values of openvpn_inet_aton */
+#define OIA_HOSTNAME 0
+#define OIA_IP 1

= +#define OIA_ERROR -1=
+int openvpn_inet_aton(cons= t char *dotted_quad, struct in_addr *addr);
+
+/* integrity validation on pulled options */
+bool ip_addr_dotted_quad_safe(const char *dot= ted_quad);
++bool ip_or_dns_addr_safe(con= st char *addr, const bool allow_fqdn);
+
+= bool mac_addr_safe(const char *mac_addr);
+
+
+/*
+ *= Transport protocol naming and other details.
+ */
+
+/*=
+ * Use enum's instead = of #define to allow for easier
+ * optional proto support
+ */
= +enum proto_num
+{
+ PROTO_NONE, /* cat= ch for uninitialized */
+ PROTO_UDP,
+ = PROTO_TCP,
+ PR= OTO_TCP_SERVER,
+ = PROTO_TCP_CLIENT,
+ = PROTO_N
+};<= br>+
+static inline bool
+proto_is_net(int proto)
+{
+ ASSERT(proto >=3D 0 && proto < PROTO_N);<= /span>
+ return proto != =3D PROTO_NONE;
+}
+
+/**
+ * @brief Returns if the protocol being used is UDP
+ */
+static inline bool
+proto_is_udp(int proto)
+{
+ ASSERT(proto >=3D 0 && proto < PROTO_= N);
+ return proto= =3D=3D PROTO_UDP;
+}=
+
+/**
+ * @brief Return if the protocol is datagram (UDP)
+ *
+ */
+static inline bool
+proto_is_dgram(int proto)
+{
+ return proto_is_udp(proto);
+}
+=
+/**
+ * @brief returns if the proto is a= TCP variant (tcp-server, tcp-client or tcp)

+ */
+static inline bool
+proto_is_tcp(int proto)
+{
+ AS= SERT(proto >=3D 0 && proto < PROTO_N);
+ return proto =3D=3D PROTO_TCP_CLIENT = || proto =3D=3D PROTO_TCP_SERVER;
+}
+
+int ascii2proto(const c= har *proto_name);
+
+sa_family_t ascii2af(= const char *proto_name);
+
+const char *pr= oto2ascii(int proto, sa_family_t af, bool display_form);
+
+const char *proto2ascii_all(struct gc_arena *gc);<= br>+
+const char *proto_remote(int proto, bool remot= e);
+
+const char *addr_family_name(int af= );

+
+static inline bool
+addr_defined(const struct openvpn_soc= kaddr *addr)
+{
+ if (!addr)
= + {
+ return 0;
+ }
+ switch (addr->addr.sa.sa_family)
+ {
+ case AF_INET:
+ return addr->addr.in4.sin_addr.s_addr= !=3D 0;
+
= + case AF_INET6:<= br>+ return !IN6_IS_= ADDR_UNSPECIFIED(&addr->addr.in6.sin6_addr);
+
+ default:
+ return 0;
+ }
+= }
+
+static inline bool
+addr_local(const struct sockaddr *addr= )
+{
+ if (!addr)
+ {
+ return false;
+ }
+ switch (addr->sa_family)
+ {
+ case AF_INET:
+ return ((const struct sockaddr_in *)addr)->sin_addr.= s_addr =3D=3D htonl(INADDR_LOOPBACK);
+
+ = case AF_INET6:
+ return IN6_IS_ADDR_LOOPBACK(&((const struct sockaddr_in6 = *)addr)->sin6_addr);
+
+ default= :
+ return= false;
+ }=
+}
+
+
+static = inline bool
+addr_def= ined_ipi(const struct link_socket_actual *lsa)
+{
+#if ENABLE_IP_PKTINFO
+ if (!lsa)
+ {
+ r= eturn 0;
+ }
+ switch (lsa->dest.= addr.sa.sa_family)
+ = {
+#if defined(HAV= E_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST)
+ case AF_INET:
+ return lsa->pi.in4.ipi_s= pec_dst.s_addr !=3D 0;
+#elif defined(IP= _RECVDSTADDR)
+ = case AF_INET:
+ = return lsa->pi.in4.s_addr !=3D 0;
+
+#endif
+ = case AF_INET6:
+ = return !IN6_IS_ADDR_UNSPECIFIED(&lsa->pi.in6.ipi6_addr);
+
+ default:
+ return 0;
+ }
+#else /* if ENABLE_IP_PKTINFO */
+ ASSERT(0);
+#endif
+ return false;
+}
+
+/*
+ * Overhead added to packets by various = protocols.
+ */
+static inline int<= br>+datagram_overhead(sa_family= _t af, int proto)
+{<= /span>
+ int overhead =3D= 0;
+ overhead += =3D (proto =3D=3D PROTO_UDP) ? 8 : 20;
+ overhead +=3D (af =3D=3D AF_INET) ? 20 : 40;<= br>+ return overhead;=
+}
+
+/*
+ * Mi= sc inline functions
+= */
+
+static inline bool

+link_socket_proto_connection_oriente= d(int proto)
+{
+ return !proto_is_dgra= m(proto);
+}+
+static inline bool
+link_socket_actual_defined(const struct link_soc= ket_actual *act)
+{
+ return act &&= amp; addr_defined(&act->dest);
+}
+=
+static inline bool<= /span>
+addr_match(const str= uct openvpn_sockaddr *a1, const struct openvpn_sockaddr *a2)
+{

+ switch (a1->addr.sa.sa_family)
+ {

+ case AF_INET:
+ return a1->addr.in4.sin_addr.s_= addr =3D=3D a2->addr.in4.sin_addr.s_addr;
+
+ case AF_INET6:
+ return IN6_ARE_ADDR_EQUAL(&a1->addr.in6.sin6_ad= dr, &a2->addr.in6.sin6_addr);
+ }
+ = return false;
+}
+
+static inline bool
+addrlist_match(const struct openvpn_socka= ddr *a1, const struct addrinfo *addrlist)
+{
+ const struct addrinfo *curele;
+ for (curele =3D addrlist; curele; curele =3D curele-= >ai_next)
+ {
+ switch (a1-&g= t;addr.sa.sa_family)
= + {
+ = case AF_INET:
+ = if (a1->addr.in4.sin_addr.s_addr
+ =3D=3D ((struct sockaddr_i= n *)curele->ai_addr)->sin_addr.s_addr)
+ {
+ return true;
+ }
+ break;
+
+ case AF_INET6:
+ if (IN6_ARE_ADDR_EQUAL(&a1->= ;addr.in6.sin6_addr,
= + &((struct sockaddr_in6 *)curele= ->ai_addr)->sin6_addr))
+ {
+ return true;
+ }
+ break;
+
+ ASSERT(0);
+ }
= + }
+ return fa= lse;
+}
+

+static inline bool
+addrlist_port_match(const struct openvpn_sockaddr *a1= , const struct addrinfo *a2)
+{
+ const= struct addrinfo *curele;
+ for (curele =3D a2; curele; curele =3D curele->ai_next)=
+ {
+ switch (a1->addr.sa.sa_family= )
+ {+ case AF_INET:
+ if (cu= rele->ai_family =3D=3D AF_INET
+ && a1->addr.in4.sin_addr.s_add= r
+ = =3D=3D ((struct sockaddr_in *)curele->ai_addr)->sin_addr.s_a= ddr
+ = && a1->addr.in4.sin_port =3D=3D ((struct sockaddr_in *)curel= e->ai_addr)->sin_port)
+ {
+ return true;
+ }
+ break;
+
+ case AF_INET6:
+ if (curele->ai_family =3D=3D AF_INET6+ &&= ; IN6_ARE_ADDR_EQUAL(&a1->addr.in6.sin6_addr,
+ = &((struct sockaddr_in6 *)curele->ai_addr)->sin6_addr)
= + &&= a1->addr.in6.sin6_port
+ =3D=3D ((struct sockaddr_in6 *)curele->= ;ai_addr)->sin6_port)
+ {
+ }
+ break;
+
+ = default:
+ = ASSERT(0);
+ }
+ = }
+ return false;<= /span>
+}
+
+
+s= tatic inline bool
+ad= dr_port_match(const struct openvpn_sockaddr *a1, const struct openvpn_socka= ddr *a2)
+{+ switch (a1->addr.sa.sa= _family)
+ {
+ case AF_INET:
+ return a1-&= gt;addr.in4.sin_addr.s_addr =3D=3D a2->addr.in4.sin_addr.s_addr+ &&= ; a1->addr.in4.sin_port =3D=3D a2->addr.in4.sin_port;
+

+ case AF_INET6:
+ return IN6_ARE_ADDR_EQUAL(&a1->a= ddr.in6.sin6_addr, &a2->addr.in6.sin6_addr)
+ && a1->addr.in= 6.sin6_port =3D=3D a2->addr.in6.sin6_port;
+ }
+ ASSERT(0);
+ return false;
+}
+
+static inline bool

+addr_match_proto(const struct ope= nvpn_sockaddr *a1, const struct openvpn_sockaddr *a2,

+ const int proto)<= br>+{
+ return link_socket_proto_connection_orien= ted(proto) ? addr_match(a1, a2)
+ : addr_= port_match(a1, a2);
+= }
+
+
+static inline bool
+addrlist_match_proto(const struct openvpn_sockaddr *a1, s= truct addrinfo *addr_list, const int proto)
+{
+ return link_socket_proto_connection_oriented(proto) ? addrlist_ma= tch(a1, addr_list)
+ = : addrlist_port_matc= h(a1, addr_list);
+}<= /span>
+
+static inline void
+addr_zero_host(struct openvpn_sockaddr *= addr)
+{
+ switch (addr->addr.sa.sa_= family)

+ {=
+ case AF_INET:
+ addr->add= r.in4.sin_addr.s_addr =3D 0;
+ break;
+
+ cas= e AF_INET6:
+ = memset(&addr->addr.in6.sin6_addr, 0, sizeof(struct in6_addr));
+ break;
+ }
+}
+
+static inline int
+= af_addr_size(sa_family_t af)
+{
+ switc= h (af)
+ {<= br>+ case AF_INET:
+ return sizeof= (struct sockaddr_in);
+
+ case AF_I= NET6:
+ re= turn sizeof(struct sockaddr_in6);
+
+ = default:
+#if 0
+ /* could b= e called from socket_do_accept() with empty addr */
+ msg(M_ERR, "Bad address = family: %d\n", af);
+ ASSERT(0);
+#endif
+ = return 0;
+ = }
+}
+

+static inline bool
+link_socket_actual_match(const struct link_socket_actua= l *a1, const struct link_socket_actual *a2)
+{
+ return addr_port_match(&a1->dest, &a2->dest);
+}
+
+#endif
diff --git a/src/openvpn/ssl_common.h= b/src/openvpn/ssl_common.h
index e8fde86..428bf5a 100644
--- a/src/openvpn/ssl_common.h
+++ b/src/ope= nvpn/ssl_common.h
@@ -30,7 +30,7 @@
#defin= e SSL_COMMON_H_

#include "session_i= d.h"
-#include &qu= ot;socket.h"
+#i= nclude "socket_util.h"
#include "packet_id.= h"
#include "crypto.h"
#in= clude "options.h"
diff --git a/src/openvpn/tun.c = b/src/openvpn/tun.c
index f54b608..6c3096b 100644--- a/src/openvpn/tun.c
+++ b/src/openvpn/tun.c
@@ -39,7 +39,7 @@
#include "fdmisc.h"= ;
#include "common.h"
#include = "run_command.h"
-#include "socket.h"
+#include "socket_util.h"
#include = "manage.h"
#include "route.h" #include "win32.h"

T= o view, visit change 1159. To unsubscribe, or for help writing mail filters, vi= sit settings.

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I7393459b975fb9b3e0a42743f58= 645f769d1be5a
Gerrit-Change-Number: 1159
Gerrit-PatchSet: 2
Gerrit-Owner: flichtenheld <frank@...2723...= .>
Gerrit-Reviewer: cron2 <gert@...1296...>=
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-MessageType: merged
--r2zmF8NZK70=--