From: "flichtenheld (Code Review)" References: Reply-To: frank@...2641..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net Message-ID: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="iDkyn9UOUYE="; 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: Thu, 28 Aug 2025 15:52:50 -0000 To: plaisthos Cc: openvpn-devel --iDkyn9UOUYE= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: plaisthos. Hello plaisthos, I'd like you to do a code review. Please visit http://gerrit.openvpn.net/c/openvpn/+/1159?usp=3Demail to review the following change. 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 --- 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(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/59/1159/1 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" =20 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; }; -/* 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 */ =20 -/* - * 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: 1 Gerrit-Owner: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-MessageType: newchange --iDkyn9UOUYE= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: plaisthos.

flichtenheld would like plaisthos to review this change.

V= iew Change

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
<= br>- openvpn_getaddrinfo and related functions
- print_*
- setenv_*
And lots of the inline functions.

This move will make it easie= r to add unit tests
for these moved functions.

Change-Id: I739345= 9b975fb9b3e0a42743f58645f769d1be5a
Signed-off-by: Frank Lichtenheld <= frank@...2641...>
---
M CMakeLists.txt
M src/openvpn/Makefile.a= m
M src/openvpn/clinat.c
M src/openvpn/dhcp.c
M src/openvpn/dns.c<= br>M src/openvpn/manage.h
M src/openvpn/misc.c
M src/openvpn/misc.hM 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(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn=
 refs/changes/59/1159/1
diff --git a/CMakeLists.txt b/CMakeLists.tx=
t
index 3866e21..45044af 100644
--- a/CMake= Lists.txt
+++ b/CMakeLists.txt
@@ -555,6 +5= 55,8 @@
src/openvpn/sig.h
src/ope= nvpn/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 1e= 17c3f..217f897 100644
--- a/src/openvpn/Makefile.am<= br>+++ b/src/openvpn/Makefile.am
@@ -128,6 +128,7 @@<= /span>
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_o= penssl.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/clin= at.c
@@ -28,7 +28,7 @@

#= include "clinat.h"
#include "proto.h"<= /span>
-#include "socket.= h"
+#include &qu= ot;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/o= penvpn/dhcp.c
@@ -27,7 +27,7 @@
#include &= quot;syshead.h"

#include "dhcp= .h"
-#include &quo= t;socket.h"
+#in= clude "socket_util.h"
#include "error.h&quo= t;

#include "memdbg.h"<= br>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"
#incl= ude "options.h"
#include "run_command.h&quo= t;

diff --git a/src/openvpn/manage.h b/sr= c/openvpn/manage.h
index 083caf5..b8892c8 100644
= --- a/src/openvpn/manage.h
+++ b/src/openvpn/manage.h=
@@ -48,7 +48,7 @@

#incl= ude "misc.h"
#include "event.h"=
-#include "socket.h"= ;
+#include "soc= ket_util.h"
#include "mroute.h"
<= span>

#define MANAGEMENT_VERSION 5
diff --git a/src/openvpn/misc.c b/src/openvpn/misc.c
<= span>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 examp= le, foo.bar.gov would be modified to <random-chars>.foo.bar.gov.
- * Of course, this require= s 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_bytes) * 2 + 4, gc);
-
- prng_b= ytes(rnd_bytes, sizeof(rnd_bytes));
- rnd_str =3D format_hex_ex(rnd_bytes, sizeof(rnd_bytes), 4= 0, 0, NULL, gc);
- b= uf_printf(&hname, "%s.%s", rnd_str, hostname);
- return BSTR(&hname);
<= br>-#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/o= penvpn/misc.h
index 1b10cd9..e87fd85 100644
--- a/src/openvpn/misc.h

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

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

-/* prepe= nd 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"

#in= clude "memdbg.h"

diff --git a/s= rc/openvpn/mss.h b/src/openvpn/mss.h
index 05f12a7..bcbb5a8= 100644
--- a/src/openvpn/mss.h
+++ b/src/o= penvpn/mss.h
@@ -26,6 +26,7 @@
#include &q= uot;proto.h"
#include "error.h"
<= span> #include "mtu.h"

+#include "socket.h"
#include "= ssl_common.h"

void mss_fixup_ipv4(s= truct buffer *buf, uint16_t maxmss);
diff --git a/src/openv= pn/networking_iproute2.c b/src/openvpn/networking_iproute2.c
index 0635b5d..e9be3a4 100644

--- a/src/openvpn/networkin= g_iproute2.c
+++ b/src/openvpn/networking_iproute2.c=
@@ -30,7 +30,7 @@
#include "misc.h"
#include "openvpn.h"
#include &qu= ot;run_command.h"
= -#include "socket.h"
+#include "socket_util.h"

<= span> #include <stdbool.h>

#include <netinet/in.h= >
diff --git a/src/openvpn/options.c b/src/openvpn/optio= ns.c
index 648d526..74946a4 100644
--- a/sr= c/openvpn/options.c
+++ b/src/openvpn/options.c
<= span>@@ -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/opti= ons.h b/src/openvpn/options.h
index 4fa6800..44f3fc9 100644=
--- a/src/openvpn/options.h
+++ b/src/open= vpn/options.h
@@ -33,7 +33,7 @@
#include &= quot;mtu.h"
#include "route.h"
#include "tun.h"

-#include "socket.h"
+#include "socket_util.h"
#in= clude "plugin.h"
#include "manage.h"
#include "proxy.h"
diff --git a/s= rc/openvpn/pool.c b/src/openvpn/pool.c
index a41364a..fde6c= ea 100644
--- a/src/openvpn/pool.c
+++ b/sr= c/openvpn/pool.c
@@ -29,7 +29,7 @@
#includ= e "pool.h"
#include "buffer.h"<= br> #include "error.h"
-#include "socket.h"
+#include "socket_util.h"
#include "otime.h"


#includ= e "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 op= envpn_getaddrinfo to print the address
- * family when resolving fails
- */
-static const char *
-getaddrinfo_addr_family_name(int af)
-{
= - switch (af)
- {=
- case AF_INET:=
- return &q= uot;[AF_INET]";
-<= /span>
- case AF_INET6:=
- return &q= uot;[AF_INET6]";
-= }
- return "= ;";
-}
<= span style=3D"color: hsl(0, 100%, 40%);">-

-/*
- * Translate IPv4/IPv6 addr or hostname into struct addrinfo
- * If resolve error, try again for= resolve_retry_seconds seconds.
- */
-int
-openvpn_getaddrinfo(unsign= ed int flags, const char *hostname, const char *servname,
- int resolve_retry_s= econds, struct signal_info *sig_info, int ai_family,
- struct addrinfo **res)<= /span>
-{
- struct addrinfo hints;
- int status;

- struct signal_info sigrec =3D { 0 };

- int msglevel =3D (fl= ags & GETADDR_FATAL) ? M_FATAL : D_RESOLVE_ERRORS;
- struct gc_arena gc =3D gc_new();
- const char *print_hostn= ame;
- const char *p= rint_servname;
-=
- ASSERT(res);
<= span style=3D"color: hsl(0, 100%, 40%);">-

- ASSERT(hostname || servname);
- ASSERT(!(flags & GETADDR_HOST_ORDE= R));
-
- 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)) && !sig_info)
- {
- sig_info =3D &sigrec;
- }
-
- /* try numer= ic ip addr first */
- = CLEAR(hints);
- hi= nts.ai_flags =3D AI_NUMERICHOST;
-
- if (fl= ags & GETADDR_PASSIVE)
- {
- hin= ts.ai_flags |=3D AI_PASSIVE;
- }
-- if (flags & GETADDR_DA= TAGRAM)
- {- 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 addr= ess family - v4-only or v6/v6-dual-stack */
- if (!hostname)
- {
- }
-
- status =3D ge= taddrinfo(hostname, servname, &hints, res);
-
- if (status !=3D 0) /* parse as numeric addres= s failed? */
- {
- const int fail_wa= it_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_f= amily */
- hints= .ai_family =3D ai_family;
-
- if (hostn= ame && (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)&quo= t;;
- if ((flags= & GETADDR_MENTION_RESOLVE_RETRY) && !resolve_retry_seconds)
- {
- fmt =3D "RESOLVE: C= annot 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(m= sglevel, "RESOLVE: Cannot parse IP address: %s:%s (%s)", print_ho= stname,
- = print_servname, gai_strerror(status));
- goto done;
- }
-
-#ifdef ENABLE= _MANAGEMENT
- if= (flags & GETADDR_UPDATE_MANAGEMENT_STATE)
- {
- if (management)
- {
- management_set_state(management, OPENVPN_STATE_R= ESOLVE, NULL, NULL, NULL, NULL,
- NULL);
- }
- }
-#endif
-
- /*
- * Resolve hostname
<= br>- */
- 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 getaddrin= fo(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 SIGUSR= 1 ? */
- = if (signal_reset(sig_info, SIGUSR1) =3D=3D SIGUSR1) /* ignore SIGUSR1 = */
- = {
- = msg(level, "RESOLVE: Ignored SIGUSR1 signal received during "= ;
- = "DNS resolution attempt");
- }
- else
- {
- /* turn success = into failure (interrupted syscall) */
- if (0 =3D=3D status)
- {

= - ASSE= RT(res);
- = freeaddrinfo(*res);
- *res =3D NULL;
- status =3D EA= I_AGAIN; /* =3D temporary failure */
- errno =3D EINTR;
- }

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

- = }
- }
- }
= -
- /* success? */
- if (0 =3D=3D status)
- {

- break;
- }
-
- /* resolve lookup failed, should we
- * continue or fail? */<= br>- level =3D msgleve= l;
- if (res= olve_retries > 0)
- = {
- = level =3D D_RESOLVE_ERRORS;
- }
-
- msg(= level, fmt, print_hostname, print_servname, getaddrinfo_addr_family_name(ai= _family),
- = gai_strerror(status));
-
- if (= --resolve_retries <=3D 0)
- {
- = goto done;
- }
-
- management_sl= eep(fail_wait_interval);
-- ASSERT(res);-
- /* hostname resolve succeeded */
<= span style=3D"color: hsl(0, 100%, 40%);">-

- /*
- * Do not choose an IP Addresse by random or change the o= rder *
- * of I= P addresses, doing so will break RFC 3484 address selection *
- */
- }
- else
- /* IP= address parse succeeded */
- if (flags & GETADDR_RANDOMIZE)
- {
- msg(M_WARN, "WARNING: ignoring --remote-ran= dom-hostname because the "
- "hostname is an IP address");
- }
- }

-
- if (sig_i= nfo && 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)<= /span>
- {
- level =3D M_WARN;
- }
- msg(level, "RESOLVE: sign= al received during DNS resolution attempt");
- }
-
- gc_fr= ee(&gc);
- retur= n status;
-}
= -
-/*
- * isn't very good about error check= ing.
- */
-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&q= uot;, &a, &b, &c, &d) =3D=3D 4)
- {
- if (a < 256 && b < 256 && c < 25= 6 && d < 256)
- = addr->s_addr =3D htonl(a << 24 | b << 16 | c << 8 |= d);
- retur= n OIA_IP; /* good dotted quad */
- }
- = }
- if (string_cl= ass(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 a= ll 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;
- i= nt 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_ad= dr a;
- return o= penvpn_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;
- }
-
- /* verif= y 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;
- }-}
-
-dns_add= r_safe(const char *addr)
- if (addr)
- {
- const size_t len =3D strlen(addr)= ;
- return len &= gt; 0 && len <=3D 255 && string_class(addr, CC_ALNUM | C= C_DASH | CC_DOT, 0);
- = }
- else- {
- return false;
- }
-}
-=
-bool
-ip_or_dns_addr_safe(const char *addr, const= bool allow_fqdn)
-{

- if (ip_addr_dotted_q= uad_safe(addr))
- {<= /span>
- return true;
- }
- else if (allow_fqdn)
- {

- return dns_addr_safe(addr);
- }
- else
- {
- = return false;
- }
-}
-
-bool
-mac_ad= dr_safe(const char *mac_addr)
-{
- /* verif= y non-NULL */
- if (= !mac_addr)
- {

- return false;
- }
-
- /* verify length is within limits */
- if (strlen(mac_addr) > 17)- {
- return false;
- }
-
- /* ve= rify that all chars are either alphanumeric or ':' and that no
- * alphanumeric substr= ing 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 &= lt;=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 addre= sses 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]&= quot;);
- }<= /span>
- salen =3D = sizeof(struct sockaddr_in);
- addr_is_defined =3D ((struct sockaddr_in *)sa)->sin_ad= dr.s_addr !=3D 0;
- = break;
-=
- case AF_INET6:
- if (!(flags &am= p; PS_DONT_SHOW_FAMILY))
- = buf_puts(&out, "[AF_INET6]");
- }
- salen =3D sizeof(struct sockaddr_in6);=
- addr_is_d= efined =3D !IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)sa)->si= n6_addr);
- = break;
-
- case AF_UNSPEC:

- if (!(flags & PS_D= ONT_SHOW_FAMILY))

- = {
- = return "[AF_UNSPEC]";
- }
- else
= - {
- = return "";
- }
-=
- default:
- ASSERT(0);
- }
-
- status =3D getnameinfo(sa, salen, hostaddr, sizeof(hostadd= r), servname, sizeof(servname),
- NI_NUMERICHOST | NI_NUMERICSERV);=
-
- if (status !=3D 0)
- {
- buf_printf(&out, "[nameinfo() err: %s]", g= ai_strerror(status));
-= return BSTR(&out);
- }
-=
- if (!(flags & PS_DON= T_SHOW_ADDR))
- {
- if (addr_is_defi= ned)
- {<= br>- buf_puts(&out= , hostaddr);
- }=
- else- {
- buf_puts(&out, "[unde= f]");
- }
- }
-
- if ((flags & PS_SHOW_PORT) || (flags & PS_SHOW_P= ORT_IF_DEFINED))
- {=
- if (separator= )
- {
= - buf_puts(&out, s= eparator);
- }
-
- buf_puts(&out, servname);
- }
-
- return BSTR(&out);
-}
-
-const char *
-print_link_socket_actual(const str= uct link_socket_actual *act, struct gc_arena *gc)

-{
- return print_link_socket_actual_ex(act, ":", PS_SHOW_= PORT | PS_SHOW_PKTINFO, gc);
-}
-
-#ifndef IF_NAMESIZE

-#define IF_NAMESIZE 16

-#endif

-
-const char *
-print= _link_socket_actual_ex(const struct link_socket_actual *act, const char *se= parator,
- = const unsigned int flags, struct gc_arena *gc)
-{

- if (act)
- {
- = struct buffer out =3D alloc_buf_gc(128, gc);
- buf_printf(&out, "%s", prin= t_sockaddr_ex(&act->dest.addr.sa, separator, flags, gc));
= -#if ENABLE_IP_PKTINFO
= - char ifname[IF_NAMESIZE]= =3D "[undef]";
-
- if ((flag= s & PS_SHOW_PKTINFO) && addr_defined_ipi(act))
- {
- switch (act->dest.addr.sa.sa_family= )
- {=
- case AF_INET= :
- {
- stru= ct openvpn_sockaddr sa;
- CLEAR(sa);
- sa.addr.in4.sin_family =3D AF_INET;-#if defined(HAVE_IN_PKTINFO) &= amp;& 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);
<= span style=3D"color: hsl(0, 100%, 40%);">-#elif defined(IP_RECVDSTADDR)
- sa.ad= dr.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_RECVDST= ADDR (fix syshead.h)
-#= endif
- = buf_printf(&out, " (via %s%%%s)",
- print_sockad= dr_ex(&sa.addr.sa, separator, 0, gc), ifname);
- }
- break;
-
- case AF_INET6:
- {
- struct sockaddr_in6 sin6;
- char buf[INET6_ADDRST= RLEN] =3D "[undef]";
- CLEAR(sin6);
- sin6.sin6_family =3D AF_INET6;
- sin6.s= in6_addr =3D act->pi.in6.ipi6_addr;
- if_indextoname(act->pi.in6.ipi6_ifi= ndex, ifname);
- = if (getnameinfo((struct sockaddr *)&sin6, sizeof(struct so= ckaddr_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);<= /span>
- }<= /span>
- }
- break;
- }
<= span style=3D"color: hsl(0, 100%, 40%);">- }

-#endif /* if ENABLE_IP_PKTINFO */- return BSTR(&out);<= /span>
- }
- 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, str= uct gc_arena *gc)
-{
- struct in_addr ia;
- char *out =3D gc_ma= lloc(INET_ADDRSTRLEN, true, gc);
-
- if (ad= dr || !(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_AD= DRSTRLEN);
- }
- return out;
<= span style=3D"color: hsl(0, 100%, 40%);">-}

-
= -/*
- * 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 *g= c)
-{
- char *out =3D gc_malloc(INET6_ADDRS= TRLEN, true, gc);
-
- if (memcmp(&a6, &= amp;in6addr_any, sizeof(a6)) !=3D 0 || !(flags & IA_EMPTY_IF_UNDEF))
- {
- inet_ntop(AF_INET6, &a6, out= , INET6_ADDRSTRLEN);
- = }
- return out;
-}
-
-/*
- * Conve= rt an in_port_t in host byte order to a string
- */
-const char *
-pr= int_in_port_t(in_port_t port, struct gc_arena *gc)
-{
- struct buffer buffer =3D alloc_buf_gc(8, gc);
- buf_printf(&buffer, "%hu&= quot;, port);
- retu= rn BSTR(&buffer);
-= }
-
-/* add some offset to an ipv6 address
- * (add in steps of 8 bit= s, taking overflow into next round)
- */
-stru= ct in6_addr
-add_in6_ad= dr(struct in6_addr base, uint32_t add)
-{
- = int i;
-
- for (i =3D 15; i >=3D 0 &= ;& add > 0; i--)

- {
- regist= er int carry;
- = register uint32_t h;
-<= /span>
- h =3D (unsigne= d 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;
<= span style=3D"color: hsl(0, 100%, 40%);">-}

-
= -/* set environmental variables for ip/port in *addr */
-void
-setenv_sockaddr(struct env_set *es, const char *name_prefi= x, 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), &= quot;%s_ip", name_prefix);
- }
- else
- = {
- = snprintf(name_buf, sizeof(name_buf), "%s", name_prefix);
- }
<= span style=3D"color: hsl(0, 100%, 40%);">-

- 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.in= 4.sin_port)
- = {
- sn= printf(name_buf, sizeof(name_buf), "%s_port", name_prefix);
- 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_a= ddr));
- = 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_pre= fix);
- = 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), &qu= ot;%s_port", name_prefix);
- setenv_int(es, name_buf, ntohs(addr->addr.in6.= sin6_port));
- = }
- break= ;
- }
-}

-
-vo= id
-setenv_in_addr_t(st= ruct env_set *es, const char *name_prefix, in_addr_t addr,
- const unsigned int fl= ags)
-{
- if (addr || !(flags & SA_SET_= IF_NONZERO))

- {
- struct openvpn_so= ckaddr si;
- CLE= AR(si);
- si.add= r.in4.sin_family =3D AF_INET;
- si.addr.in4.sin_addr.s_addr =3D htonl(addr);
- setenv_sockaddr(es, name_pre= fix, &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;<= br>- 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_a= ctual *act, const unsigned int flags)
-{
- = setenv_sockaddr(es, name_prefix, &act->dest, flags);
-}

-
-/*<= /span>
- * Convert protocol na= mes 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", &quo= t;proto-NONE", AF_UNSPEC, PROTO_NONE },
- /* try IPv4 and IPv6 (client), bind dual-stack (= server) */
- { "= ;udp", "UDP", AF_UNSPEC, PROTO_UDP },
- { "tcp-server", "TCP_SER= VER", AF_UNSPEC, PROTO_TCP_SERVER },
- { "tcp-client", "TCP_CLIENT", A= F_UNSPEC, PROTO_TCP_CLIENT },
- { "tcp", "TCP", AF_UNSPEC, PROTO_TCP },
- /* force IPv4 */
- { "udp4", &q= uot;UDPv4", AF_INET, PROTO_UDP },
- { "tcp4-server", "TCPv4_SERVER", A= F_INET, PROTO_TCP_SERVER },
- { "tcp4-client", "TCPv4_CLIENT", AF_INET, PRO= TO_TCP_CLIENT },
- {= "tcp4", "TCPv4", AF_INET, PROTO_TCP },
- /* force IPv6 */

- { "udp6", "UDPv6&qu= ot;, AF_INET6, PROTO_UDP },
- { "tcp6-server", "TCPv6_SERVER", AF_INET6, PR= OTO_TCP_SERVER },
- = { "tcp6-client", "TCPv6_CLIENT", AF_INET6, PROTO_TCP_CL= IENT },
- { "tc= p6", "TCPv6", AF_INET6, PROTO_TCP },
-};
-
-int
-ascii2proto(const char *pr= oto_name)
-{
= - for (size_t i =3D 0; i < = SIZE(proto_names); ++i)
- {
- if (!s= trcmp(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))
- = {
- retur= n proto_names[i].proto_af;
- }
- }
- return 0;-}
-
-proto2= ascii(int proto, sa_family_t af, bool display_form)
-{
- for (size_t i =3D 0; i < SIZE(proto_names); ++i)<= br>- {
- 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;
- }- }
- }
-
- r= eturn "[unknown protocol]";
-}
-
-const char *
-proto2ascii_all(struct gc_arena *g= c)

-{
- struct buffer out =3D alloc_buf_gc(= 256, gc);
-
<= span style=3D"color: hsl(0, 100%, 40%);">- for (size_t i =3D 0; i < S= IZE(proto_names); ++i)

= - {
- if (i)<= /span>
- {
- buf_printf(&out, &= quot; ");

- = }
- 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, o= r compatible remote proto
- * if remote.
- *
- * This is used for opt= ions compatibility
- * = checking.
- *- * IPv6 and IPv4 protocols are = comptabile but OpenVPN
= - * has always sent UDPv4, TCPv4 over the wire. Keep these
- * strings for backward compatibility<= /span>
- */
-const char *
-proto_remote(int proto, bool remote)
-{

- ASSERT(proto >=3D 0 && proto < PROTO= _N);
- if (proto =3D= =3D PROTO_UDP)
- {
- return "UD= Pv4";
- }
-
- if ((remote && proto =3D=3D PROTO_TC= P_CLIENT) || (!remote && proto =3D=3D PROTO_TCP_SERVER))
= - {
- return "TCPv4_SERVER";<= br>- }
- if ((remote && proto =3D=3D PRO= TO_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/open= vpn/socket.h
+++ b/src/openvpn/socket.h
@@ = -34,6 +34,7 @@
#include "socks.h"
#include "misc.h"

#include "tun.h"<= /span>
+#include "socke= t_util.h"

/*
* Op= enVPN'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 openv= pn_sockaddr
-{- /*int dummy;*/ /* add offs= et to force a bug if sa not explicitly dereferenced */
- union
- {
- struct sockaddr sa;
- struct sockaddr_in in4;
- struct sockaddr_in6 in6;
- } addr;
-};
-struct link_socket= _actual
-{
- /*int dummy;*/ /* add offset t= o force a bug if dest not explicitly dereferenced */

-
- struct openvpn_sockaddr dest;
-#if ENABLE_IP_PKTINFO
- union
- {
-#if d= efined(HAVE_IN_PKTINFO) && defined(HAVE_IPI_SPEC_DST)
- struct in_pktinfo in4;
-#elif defined(IP_RECVDSTAD= DR)
- struct in_= addr in4;
-#endif
- struct in6_pktinfo = in6;
- } pi;<= br>-#endif
-};
-
/* IP addresses which are persistent across = SIGUSR1s */
struct link_socket_addr
{

@@ -349,58 +319,6 @@

void s= d_close(socket_descriptor_t *sd);

-#define PS_SHOW_PORT_IF_DEFINED (1 <<= 0)
-#define PS_SHOW_PO= RT (1 << 1)
-#define PS_SHOW_PKTINFO (1 << 2)
-#define PS_DONT_SHOW_ADDR (1 <<= 3)
-#define PS_DONT_SH= OW_FAMILY (1 << 4)
-
-const char *pr= int_sockaddr_ex(const struct sockaddr *addr, const char *separator,<= br>- = const unsigned int flags, struct gc_arena *gc);
-
-static inline const char *
-print_openvpn_sockaddr(const struct openvpn_sockaddr *addr, stru= ct gc_arena *gc)
-{
- return print_sockaddr= _ex(&addr->addr.sa, ":", PS_SHOW_PORT, gc);
-}

-
-st= atic inline const char *
-{
- return print_sockaddr_ex(addr, ":",= PS_SHOW_PORT, gc);
-}<= /span>
-
-
-const char *print_link_socket_actual_ex(const struct link_socket= _actual *act, const char *separator,
- const unsigned int f= lags, struct gc_arena *gc);
-
-const char *pri= nt_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_i= n_addr_t(in_addr_t addr, unsigned int flags, struct gc_arena *gc);-
-const char *print_in6_addr(struct in6_addr addr6, u= nsigned int flags, struct gc_arena *gc);
-
-co= nst char *print_in_port_t(in_port_t port, struct gc_arena *gc);
<= span style=3D"color: hsl(0, 100%, 40%);">-

-struct in6_addr add_in6_addr(struct in6_addr base, uin= t32_t add);
--#define SA_IP_PORT (1 &l= t;< 0)
-#define SA_S= ET_IF_NONZERO (1 << 1)
-void setenv_sockaddr(struct env_set *es, const char *name_prefix,
- con= st struct openvpn_sockaddr *addr, const unsigned int flags);
-

-void setenv_in_addr_t(struct env_set *es, const char *nam= e_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,
- const unsigned int flags);
-
-void setenv_link_socket_actual(struct env_set *es, const char= *name_prefix,
- = const struct link_socket_actual *act, const unsigne= d int flags);
-<= br> 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(co= nst char *dotted_quad, struct in_addr *addr);
-
-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_add= r_safe(const char *mac_addr);
-
-bool ipv6_add= r_safe(const char *ipv6_text_addr);
-
socket_descriptor_t create_socket_tcp(struc= t addrinfo *);

socket_descriptor_t socke= t_do_accept(socket_descriptor_t sd, struct link_socket_actual *act,<= br>@@ -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_M= ENTION_RESOLVE_RETRY (1 << 3)
-#define GETADDR_FATAL_ON_SIGNAL (1 << 4)
-#define GETADDR_WARN_ON_SI= GNAL (1 << 5)
-#define GETADDR_MSG_VIRT_OUT (1 << 6)
-#define GETADDR_TRY_ONCE = (1 << 7)

-#d= efine GETADDR_UPDATE_MANAGEMENT_STATE (1 << 8)
-#define GETADDR_RANDOMIZE (1 = << 9)
-#define GE= TADDR_PASSIVE (1 << 10)
-#define GETADDR_DATAGRAM (1 <<= 11)
-
-#define GETADDR_CACHE_MASK (GETADDR_DA= TAGRAM | GETADDR_PASSIVE)
-
-/**
- * Translate an IPv4 addr or hostna= me from string form to in_addr_t

- *
- * In ca= se of resolve error, it will try again for
- * resolve_retry_seconds seconds.
- */
-in_addr_t getaddr(unsigned int flags, const char *hostname, = int resolve_retry_seconds,
- bool *succeeded, struct signal_info *sig_info);
-
-/**
- * Translate an IPv6 addr or hostname from string form to in6_add= r
- */
-bool get_ipv6_addr(const char *hostnam= e, struct in6_addr *network, unsigned int *netbits,
- int msglevel);-
-int openvpn_getaddrinfo(unsigned int flags, const ch= ar *hostname, const char *servname,
- int resolve_retry_seconds, struct sig= nal_info *sig_info, int ai_family,
- struct addrinfo **res);
-

-/*
- = * Transport protocol naming and other details.
- */
-
-/*
<= span style=3D"color: hsl(0, 100%, 40%);">- * Use enum's instead of #def= ine to allow for easier

- * optional proto support
- */
-enum proto_n= um
-{
- PROTO_NONE, /* catch for uninitiali= zed */
- PROTO_UDP,<= /span>
- PROTO_TCP,<= br>- PROTO_TCP_SERVER,<= br>- PROTO_TCP_CLIENT,<= br>- PROTO_N
-};
-
-stat= ic inline bool
-proto_i= s_net(int proto)
-{
- ASSERT(proto >=3D = 0 && proto < PROTO_N);
- return proto !=3D PROTO_NONE;
-}
-
-/**
- * @brief Returns if the protocol b= eing used is UDP

- */
-static inline bool
-proto_is_udp(int proto)
-{
- ASSERT(proto >=3D 0 && proto= < PROTO_N);
- re= turn 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 &&a= mp; proto < PROTO_N);

-}
-
-
-int as= cii2proto(const char *proto_name);
-
-sa_famil= y_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 re= mote);
-
-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 inlin= e bool
-link_socket_pro= to_connection_oriented(int proto)
-{
- retu= rn !proto_is_dgram(proto);
-}
-
static inline bool

link_socket_connection_oriented(cons= t struct link_socket *sock)
{
@@ -613,242 = +388,6 @@
}
}

-static inline bool-addr_defined(const struct open= vpn_sockaddr *addr)
-{<= /span>
- if (!addr)<= br>- {
- 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_UNSPECIF= IED(&addr->addr.in6.sin6_addr);
-
- = default:
- = return 0;
- }
-}
-
-static inline bool
-addr_local(const struct sockaddr *addr)
-{
- if (!addr)
- = {
- return fa= lse;
- }
<= span style=3D"color: hsl(0, 100%, 40%);">- switch (addr->sa_family)
- {
- case AF_INET:
- return ((const struct socka= ddr_in *)addr)->sin_addr.s_addr =3D=3D htonl(INADDR_LOOPBACK);-
- case AF_INET6:
- return IN6_IS_ADDR_LOOPBACK(&((cons= t struct sockaddr_in6 *)addr)->sin6_addr);
-
- = return false;
- }
-}
-

-
-s= tatic 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)

- {<= br>-#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.in= 4.s_addr !=3D 0;
-
-#endif
- case AF_INET6:
- return !IN6_IS_ADDR_UNSPECIF= IED(&lsa->pi.in6.ipi6_addr);
-
- = default:
- = return 0;
- }=
-#else /* if ENABLE_IP_PKTINF= O */
- ASSERT(0);
-#endif
- return false;
-}
-
-static in= line 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_soc= kaddr *a2)
-{- switch (a1->addr.sa.sa_f= amily)
- {- 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.in= 6.sin6_addr, &a2->addr.in6.sin6_addr);
- }
- ASSERT(0);
= - return false;
-}
-
-static inline bool
-addrlist_match(const struct openvpn_sockaddr *= a1, const struct addrinfo *addrlist)
-{
- c= onst struct addrinfo *curele;
- for (curele =3D addrlist; curele; curele =3D curele->ai_next= )
- {
- switch (a1->addr.sa.sa_fa= mily)

- {=
- case AF_INET:
- if (a1-&= gt;addr.in4.sin_addr.s_addr
- =3D=3D ((struct sockaddr_in *)curele->ai_addr)= ->sin_addr.s_addr)
-= {
- = return true;
- }
-
- 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_so= ckaddr *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 (curele-&= gt;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
- &&a= mp; a1->addr.in4.sin_port =3D=3D ((struct sockaddr_in *)curele->ai_ad= dr)->sin_port)
- = {
- = return true;
- = break;
-=
- case AF_I= NET6:
- = if (curele->ai_family =3D=3D AF_INET6
- && IN6_ARE_ADDR_EQUAL(&a= 1->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 o= penvpn_sockaddr *a2)
-{=
- switch (a1->ad= dr.sa.sa_family)
- {=
- case AF_INET:=
- return a1= ->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->addr.in= 6.sin6_addr, &a2->addr.in6.sin6_addr)
- && a1->addr.in6.sin6_p= ort =3D=3D a2->addr.in6.sin6_port;
- }
-= ASSERT(0);
- ret= urn false;
-}-
-static inline bool
-addr_match_proto(const struct openvpn_sockaddr *a1, con= st struct openvpn_sockaddr *a2,
- const int proto)
-{
- return link_socket_proto_connection_oriented(proto) ? addr_match(a1, = a2)
- = : addr_port_match(a1, a2);
= -}
-
-
-static inline bool<= /span>
-addrlist_match_proto(c= onst struct openvpn_sockaddr *a1, struct addrinfo *addr_list, const int pro= to)
-{
- return link_socket_proto_connectio= n_oriented(proto) ? addrlist_match(a1, addr_list)
- = : addrlist_port_match(a1, addr_list);
-}
-
-static inline vo= id
-addr_zero_host(stru= ct openvpn_sockaddr *addr)
-{
- switch (add= r->addr.sa.sa_family)
- case = AF_INET:
- a= ddr->addr.in4.sin_addr.s_addr =3D 0;
- break;
-
- = case AF_INET6:
- = memset(&addr->addr.in6.sin6_addr, 0, sizeof(struct in6_addr))= ;
- break;
- }
-}
-
-stati= c inline int
-af_addr_s= ize(sa_family_t af)
-{<= /span>
- switch (af)=
- {
- case AF_INET:
- return sizeof(struct sockaddr_i= n);
-
- case AF_INET6:
- return sizeof(struct socka= ddr_in6);
-
<= span style=3D"color: hsl(0, 100%, 40%);">- default:

-#if 0
- /* could be called from socket_do_accept()= with empty addr */
- = msg(M_ERR, "Bad address family: %d\n", af);
<= span style=3D"color: hsl(0, 100%, 40%);">- ASSERT(0);

= -#endif
- return 0;
- }
-}
-<= br>-static inline bool
= -link_socket_actual_match(const s= truct link_socket_actual *a1, const struct link_socket_actual *a2)-{
- return addr_port_match(&a1->dest, &= a2->dest);
-}=
-
#if PORT_S= HARE

static inline bool
= diff --git a/src/openvpn/socket_util.c b/src/openvpn/socket_util.cnew file mode 100644
index 0000000..13f8c32<= br>--- /dev/null
+++ b/src/openvpn/socket_util.c
@@ -0,0 +1,936 @@
+/*
+ * Ope= nVPN -- 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/>.
+ */

+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "= syshead.h"
+

+#include "socket_u= til.h"
+#include= "crypto.h"
+#include "manage.h"
+
+/*
+ * Format IP addresses i= n ascii
+ */+
+const char *
+print_sockaddr_ex(const struct sockaddr *sa, const cha= r *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_FAM= ILY))
+ {<= /span>
+ 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))
+ {
<= span style=3D"color: hsl(120, 100%, 40%);">+ buf_puts(&o= ut, "[AF_INET6]");

+ }
+ addr_is_defined =3D !IN6_IS_A= DDR_UNSPECIFIED(&((struct sockaddr_in6 *)sa)->sin6_addr);
= + break;
<= span style=3D"color: hsl(120, 100%, 40%);">+

+ case AF_UNSPEC:
+ if (!(flags & PS_DONT_SHOW_FAM= ILY))
+ {<= /span>
+ retu= rn "[AF_UNSPEC]";
+ }
+ else
+ = {
+ = return "";
+ }
+
+ default= :
+ ASSERT= (0);
+ }+
+ status =3D getnameinfo(sa, salen, hostaddr, = sizeof(hostaddr), servname, sizeof(servname),
+ NI_NUMERICHOST | NI_NUME= RICSERV);
++ if (status !=3D 0)=
+ {
+ buf_printf(&out, "[name= info() 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_actu= al(const struct link_socket_actual *act, struct gc_arena *gc)
+{

+ return print_link_socket_actual_ex(act, ":= ", PS_SHOW_PORT | PS_SHOW_PKTINFO, gc);
+}
+
+#ifndef IF_N= AMESIZE
+#define IF_N= AMESIZE 16
+#endif
+
+const char *
+print_link_socket_actual_ex(const struct link_so= cket_actual *act, const char *separator,
+ const unsigned int flags, = struct gc_arena *gc)
= +{
+ if (act)
+ {
+ struct buffer out =3D alloc_bu= f_gc(128, gc);
+ = buf_printf(&out, "%s", print_sockaddr_ex(&act->dest= .addr.sa, separator, 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:
<= span style=3D"color: hsl(120, 100%, 40%);">+ {

+ struct openvp= n_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.i= pi_spec_dst;
+ = if_indextoname(act->pi.in4.ipi_ifindex, ifname);
= +#elif defined(IP_RECVDSTADDR)<= /span>
+ = 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);+ }
<= span style=3D"color: hsl(120, 100%, 40%);">+ break;
+
+ case AF_INET6:
+ {
+ struct sockaddr_in= 6 sin6;
+ = char buf[INET6_ADDRSTRLEN] =3D "[undef]";
+ CLEAR(sin6);
+ si= n6.sin6_family =3D AF_INET6;
+ sin6.sin6_addr =3D act->pi.in6.ipi6_addr;
+ i= f_indextoname(act->pi.in6.ipi6_ifindex, ifname);
+ if (getnameinfo((stru= ct sockaddr *)&sin6, sizeof(struct 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 orde= r
+ * to an ascii dot= ted quad.
+ */=
+const char *
+print_in_addr_t(in_addr_t addr, u= nsigned 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 or= der
+ * to an ascii r= epresentation of an IPv6 address
+ */
+con= st char *
+print_in6_= addr(struct in6_addr a6, unsigned int flags, struct gc_arena *gc)+{
+ char *out =3D gc_malloc(INET6_ADDRSTRLEN, t= rue, gc);
++ if (memcmp(&a6, &= in6addr_any, sizeof(a6)) !=3D 0 || !(flags & IA_EMPTY_IF_UNDEF))=
+ {
+ inet_ntop(AF_INET6, &a6, out= , INET6_ADDRSTRLEN);
= + }
+ return ou= t;
+}
+

+/*
+ * 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 so= me 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 a= dd)
+{
+ int i;

+
+ for (i =3D 15; i >=3D 0 && add > 0; i--)
+ {
+ register int carry;
<= br>+ register uint32_t h= ;
+
+ h =3D (unsigned char)base.s6_= addr[i];
+ bas= e.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 cha= r *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)<= /span>
+ {
+ case AF_INET:

+ if (flags & SA_I= P_PORT)

+ = {
+ sn= printf(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(bu= f));
+ set= env_str(es, name_buf, buf);
+
+ = if ((flags & SA_IP_PORT) && addr->addr.in4.sin_port)=
+ {
+ snprintf(name_bu= f, sizeof(name_buf), "%s_port", name_prefix);

+ 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;=

+ mem= cpy(&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)= ;
+ in= et_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)<= /span>
+ {=
+ snprintf(n= ame_buf, sizeof(name_buf), "%s_port", name_prefix);
+ setenv_int(es, na= me_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))+ {
+ struct openvpn_sockaddr si;
+ CLEAR(si);=
+ si.addr.in4.sin_fa= mily =3D AF_INET;
+ = si.addr.in4.sin_addr.s_addr =3D htonl(addr);
+ setenv_sockaddr(es, name_prefix, &am= p;si, flags);
+ }<= /span>
+}
+
+void
+ = const unsigned int flags)
+{
+ if (!IN6_IS_ADDR_UNSPECIFIED(addr) || !(flags & SA_SET_IF_NONZERO= ))
+ {
<= span style=3D"color: hsl(120, 100%, 40%);">+ struct openvpn_sockaddr= si;

+ CLEAR(s= i);
+ si.addr.= in6.sin6_family =3D AF_INET6;
+ si.addr.in6.sin6_addr =3D *addr;
+ setenv_sockaddr(es, name_prefix, &am= p;si, flags);
+ }<= /span>
+}
+
+void
+ = const struct link_socket_actual *act, const unsigned int flags)=
+{
+ setenv_sockaddr(es, name_prefix, &ac= t->dest, flags);
+= }
+
+/*
+ * Convert protocol names between index and ascii form= .
+ */
+

+struct proto_names
+{
+ const char *display_form;
+ sa_family_t proto_af;
+ int proto;
+};
+
+/* Indexed by = PROTO_x */
+static co= nst struct proto_names proto_names[] =3D {
+ { "proto-uninitialized", "proto-N= ONE", AF_UNSPEC, PROTO_NONE },
+ /* try IPv4 and IPv6 (client), bind dual-stack (server)= */
+ { "udp&= quot;, "UDP", AF_UNSPEC, PROTO_UDP },
+ { "tcp-server", "TCP_SERVER&= quot;, 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&qu= ot;, AF_INET, PROTO_TCP_SERVER },
+ { "tcp4-client", "TCPv4_CLIENT", AF_I= NET, PROTO_TCP_CLIENT },
+ { "tcp4", "TCPv4", AF_INET, PROTO_TCP },
+ /* force IPv6 */
+ { "udp6", = "UDPv6", AF_INET6, PROTO_UDP },
+ { "tcp6-server", "TCPv6_SERVER&quo= t;, AF_INET6, PROTO_TCP_SERVER },
+ { "tcp6-client", "TCPv6_CLIENT", AF_I= NET6, 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_fo= rm))
+ {
+ return proto_= names[i].proto;
+ = }
+ }+ return -1;
+}

+
+sa_family_t
+asc= ii2af(const char *proto_name)
+{
+ for = (size_t i =3D 0; i < SIZE(proto_names); ++i)
+ {
+ if (!strcmp(proto_name, proto_names[i].short_form))
+ {
<= span style=3D"color: hsl(120, 100%, 40%);">+ return proto_names[= i].proto_af;

+ = }
+ }
<= span style=3D"color: hsl(120, 100%, 40%);">+ return 0;

+}
+
= +const char *
+proto2= ascii(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, &qu= ot;[%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
<= span style=3D"color: hsl(120, 100%, 40%);">+ * 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";
<= span style=3D"color: hsl(120, 100%, 40%);">+ }

+ if ((remote && proto =3D=3D PROTO= _TCP_SERVER) || (!remote && proto =3D=3D PROTO_TCP_CLIENT))<= br>+ {
+ return "TCPv4_CLIENT";<= /span>
+ }
+

+ ASSERT(0);
+ return ""; /* Make the compiler happy */
+}
+
+/**
+ = * Small helper function for openvpn_getaddrinfo to print the address=
+ * family when resolving f= ails
+ */
<= span style=3D"color: hsl(120, 100%, 40%);">+static const char *

<= span style=3D"color: hsl(120, 100%, 40%);">+getaddrinfo_addr_family_name(in= t af)

+{
+ switch (af)

+ {
+ case AF_INET:
+ return "[AF_INET]";=
+
+ case AF_INET6:
+ return "[AF_INET6]"= ;
+ }
+ return "";
<= br>+}
+
+/*
+ * Pre= pend a random string to hostname to prevent DNS caching.
+ * For example, foo.bar.gov would be m= odified to <random-chars>.foo.bar.gov.
+ * Of course, this requires explicit support in th= e DNS server (wildcard).
+ */
+static cons= t char *
+hostname_ra= ndomize(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_bytes) * 2 + 4, gc);
+
+ p= rng_bytes(rnd_bytes, sizeof(rnd_bytes));
+ rnd_str =3D format_hex_ex(rnd_bytes, sizeof(rnd_by= tes), 40, 0, NULL, gc);
+ buf_printf(&hname, "%s.%s", rnd_str, hostname);
+ return BSTR(&hnam= e);
+#undef n_rnd_byt= es
+}
+

+/*
+ * Translate IPv4/IPv6 addr or hostname into struct addrinfo
= + * If resolve error, try again= for resolve_retry_seconds seconds.
+ */
+= int
+openvpn_getaddri= nfo(unsigned int flags, const char *hostname, const char *servname,<= br>+ int res= olve_retry_seconds, struct signal_info *sig_info, int ai_family,
= + struct add= rinfo **res)
+{
+ struct addrinfo hints= ;
+ int status;
+ struct signal_info= sigrec =3D { 0 };
+ = int msglevel =3D (flags & GETADDR_FATAL) ? M_FATAL : D_RESOLVE_ERROR= S;
+ struct gc_are= na gc =3D gc_new();
+= const char *print_hostname;
+ const char *print_servname;
+
+ ASSERT(res);
+
+ ASSERT(hostna= me || servname);
+ = ASSERT(!(flags & GETADDR_HOST_ORDER));
+
+ if (servname)
+ pri= nt_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 | GETA= DDR_WARN_ON_SIGNAL)) && !sig_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_so= cktype =3D SOCK_STREAM;
+ }
++ /* if hostname is not set= , we want to bind to 'ANY', with
+ * the correct address family - v4-only or v6/v6-d= ual-stack */
+ if = (!hostname)
+ {
+ hints.ai_famil= y =3D ai_family;
+ = }
+
+ status =3D getaddrinfo(hostname,= servname, &hints, res);
+
+ if (st= atus !=3D 0) /* parse as numeric address failed? */
+ {
+ 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_secon= ds + 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(ho= stname, &gc);
+ = }
+
<= span style=3D"color: hsl(120, 100%, 40%);">+ if (hostname)
+ {
+ print_hostname =3D hostname= ;
+ }+ else
+ {

+ print_hostname =3D "undefine= d";
+ }
+
+ fmt =3D "RESOLVE: Cannot re= solve host address: %s:%s%s (%s)";
+ if ((flags & GETADDR_MENTION_RESOLVE_RETRY)= && !resolve_retry_seconds)
+ {
+ fmt =3D "RESOLVE: Cannot resolve host address: %s:%s= %s (%s)"
+ = "(I would have retried this name query if you had "
+ &qu= ot;specified the --resolv-retry option.)";
+ }
+
+ = if (!(flags & GETADDR_RESOLVE) || status =3D=3D EAI_FAIL)=
+ {
+ msg(msglevel, "RESO= LVE: Cannot parse IP address: %s:%s (%s)", print_hostname,
<= span style=3D"color: hsl(120, 100%, 40%);">+ print_servname,= gai_strerror(status));

+ goto done;
+ }
+
+#ifdef ENABLE_MANAGEME= NT
+ if (flags= & GETADDR_UPDATE_MANAGEMENT_STATE)
+ {
+ if (management)
+ {
+ management_set_state(management, OPENVPN_STATE_= RESOLVE, NULL, NULL, NULL, NULL,
+ NULL);
+ }
+ }
+#endif
+
+ /*<= /span>
+ * Resolve h= ostname
+ */<= /span>
+ while (true)=
+ {+#ifndef _WIN32
+ /* force resolv.conf r= eload */

+ = res_init();
+#endif<= /span>
+ /* try h= ostname lookup */
+ = hints.ai_flags &=3D ~AI_NUMERICHOST;
+ dmsg(D_SOCKET_DEBUG, "GETAD= DRINFO flags=3D0x%04x ai_family=3D%d ai_socktype=3D%d", flags,<= br>+ hints.ai_f= amily, hints.ai_socktype);
+ status =3D getaddrinfo(hostname, servname, &hints, r= es);
+
+ if (sig_info)
+ {
+ get_signal(&si= g_info->signal_received);

+ if (sig_info->signal_received) /* were we inter= rupted by a signal? */
= + /* why are we overwriting SIGUSR1 ? */
+ if (signal_rese= t(sig_info, SIGUSR1) =3D=3D SIGUSR1) /* ignore SIGUSR1 */

+ {
+ msg(level, = "RESOLVE: Ignored SIGUSR1 signal received during "

+ = "DNS resolution attempt");

+ }
+ else
+ {
+ /* turn success into fai= lure (interrupted syscall) */
+ if (0 =3D=3D status)
+ {
+ ASSERT= (res);

+ = freeaddrinfo(*res);
+ *res =3D NULL;
+ status = =3D EAI_AGAIN; /* =3D temporary failure */
+ errno =3D EINTR;<= br>+ }
+ = goto done;
+ = }
+ = }
+ = }
+
<= span style=3D"color: hsl(120, 100%, 40%);">+ /* success? */
+ if (0 =3D=3D= status)
+ = {
+ b= reak;
+ }<= /span>
+
+ /* resolve lookup failed, s= hould we
+ = * continue or fail? */
+ level =3D msglevel;
+ if (resolve_retries > 0)
+ {
+ level =3D D_RESOLVE_ERRORS;<= /span>
+ }=
+
+ msg(level, fmt, print_hostname, p= rint_servname, getaddrinfo_addr_family_name(ai_family),
+ gai_strerror(status));<= /span>
+
+ if (--resolve_retries <= =3D 0)
+ {=
+ got= o done;
+ = }
+
+ management_sleep(fail_wai= t_interval);
+ = }
+
+ ASSERT(res);
+

+ /* hostname resolve succeeded */
+

+ /*
+ * Do not choose an IP Addresse by random or change t= he order *
+ = * of IP addresses, doing so will break RFC 3484 address selection *<= br>+ */
+ }

+ else
+ {
= + /* IP address parse succeeded */
+ if (flags & GETADDR_RANDOMIZE)
= + {
+ msg(M_WARN, "WARNING: i= gnoring --remote-random-hostname because the "
+ "hostname is= an IP address");
+ }
+
+done:
+ if (sig_info && sig_info->signal_receiv= ed)
+ {
= + int level =3D 0;
+ if (flags & G= ETADDR_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);<= /span>
+ return status;
+}
+
+/*
+= * We do our own inet_aton because the glibc function
+ * isn't very good about error checki= ng.
+ */
+int

+openvpn_inet_aton(const char *dotted_quad, struc= t in_addr *addr)
+{
+ unsigned int a, b= , c, d;
+
<= span style=3D"color: hsl(120, 100%, 40%);">+ CLEAR(*addr);

+ if (sscanf(dotted_quad, "= ;%u.%u.%u.%u", &a, &b, &c, &d) =3D=3D 4)

+ {

+ if (a < 256 && b < 256 &a= mp;& c < 256 && d < 256)
+ {
+ addr->s_addr =3D htonl(a << 24 | b <&l= t; 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
+ {<= br>+ 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;
+ }
+
+ /* veri= fy that all chars are either numeric or '.' and that no numeric
+ * substring is gre= ater than 3 chars */
= + {
+ int n= num =3D 0;
+ c= onst char *p =3D dotted_quad;
+ int c;
+
+ while = ((c =3D *p++))
+ = {
+ if = (c >=3D '0' && c <=3D '9')
+ {
+ ++nnum;
+ if (nnum > 3)<= br>+ {+ return fa= lse;
+ = }
+ }
+ 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_i= net_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_ADDRSTR= LEN)
+ {+ return false;<= br>+ }
+
+ /* 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 && l= en <=3D 255 && string_class(addr, CC_ALNUM | CC_DASH | CC_DOT, 0= );
+ }
<= span style=3D"color: hsl(120, 100%, 40%);">+ else

+ {
+ return false;
+ }
+}
++bool
+ip_or_dns_addr_safe(const char *addr, const = bool allow_fqdn)
+{
+ if (ip_addr_dotte= d_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 le= ngth is within limits */
+ if (strlen(mac_addr) > 17)
+ {
+ return false;
+ }
+=
+ /* verify that all cha= rs are either alphanumeric or ':' and that no
+ * alphanumeric substring is greater = than 2 chars */
+ = {
+ int nnum = =3D 0;
+ const= char *p =3D mac_addr;
+
+ 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;
+ }
+ }
+ }
+
+ /* er= ror-checking is left to script invoked in lladdr.c */
+ return true;
+}
diff --git a/src/openvpn/s= ocket_util.h b/src/openvpn/socket_util.h
new file mode 1006= 44
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 authenticat= ion, and
+ * = packet compression.
+ *
+ * Copyright= (C) 2002-2025 OpenVPN Inc <sales@...515...>
+ *
+ * This program is free software; you can redistribute it an= d/or modify
+ * it u= nder the terms of the GNU General Public License version 2
+ * as published by the Free Softwar= e Foundation.
+ *
+ * This program is dis= tributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the imp= lied warranty of
+ * = MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License f= or more details.

+ *<= /span>
+ * You should have = received a copy of the GNU General Public License along
+ * with this program; if not, see <= https://www.gnu.org/licenses/>.
+ */
+<= /span>
+#ifndef SOCKET_UTIL_= H
+#define SOCKET_UTI= L_H
+
+#include "buffer.h"
+#include "env_set.h&= quot;
+#include "= ;sig.h"
+=
+#define PS_SHOW_PORT_IF_DE= FINED (1 << 0)
= +#define PS_SHOW_PORT (1 << 1)
+#define PS_SHOW_PKTINFO (1 << 2)<= /span>
+#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 explic= itly dereferenced */
= + 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 expli= citly dereferenced */
+
+ struct openvp= n_sockaddr dest;
+#if= ENABLE_IP_PKTINFO
+ = union
+ {
+#if defined(HAVE_IN_PKTIN= FO) && defined(HAVE_IPI_SPEC_DST)
+ struct in_pktinfo in4;
+#elif defined(IP_RECVDSTADDR)
<= span style=3D"color: hsl(120, 100%, 40%);">+ struct in_addr in4;
+#endif
+ struct in6_pktinfo in6;
+ } pi;
+#endif

+};
+
+const ch= ar *print_sockaddr_ex(const struct sockaddr *addr, const char *separator,
+ = const unsigned int flags, struct gc_arena *gc);
+
+static inline const char *
+print_openvpn_sockaddr(const struct openvpn_sockad= dr *addr, struct gc_arena *gc)
+{
+ ret= urn print_sockaddr_ex(&addr->addr.sa, ":", PS_SHOW_PORT, g= c);
+}
+

+static inline const char *
+print_sockaddr(const struct sockaddr *addr, st= ruct gc_arena *gc)
+{=
+ return print_so= ckaddr_ex(addr, ":", PS_SHOW_PORT, gc);
+}
+
+=
+const char *print_link_soc= ket_actual_ex(const struct link_socket_actual *act, const char *separator,<= /span>
+ = const unsigned int flags, struct gc_arena *gc);<= br>+
+const char *print_link_socket_actual(const str= uct 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 ad= dr, unsigned int flags, struct gc_arena *gc);
+
+const char *print_in6_addr(struct in6_addr addr6, unsigned int flags= , struct gc_arena *gc);
+
+const char *pri= nt_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,
+ co= nst struct openvpn_sockaddr *addr, const unsigned int 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 *n= ame_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)
= +#define GETADDR_MENTION_RESOLV= E_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)
+#defi= ne GETADDR_UPDATE_MANAGEMENT_STATE (1 << 8)
+#define GETADDR_RANDOMIZE (1 &l= t;< 9)
+#define GE= TADDR_PASSIVE (1 << 10)
+#define GETADDR_DATAGRAM (1 <&l= t; 11)
+
+#define GETADDR_CACHE_MASK (GETA= DDR_DATAGRAM | GETADDR_PASSIVE)

+
+/**
+ * Translate an IPv4 ad= dr 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_retry_seconds,
+ bool *succeeded, struct sign= al_info *sig_info);
+=
+/**
+ * Translate an IPv6 addr or hostna= me from string form to in6_addr

+ */
+bool= get_ipv6_addr(const char *hostname, struct in6_addr *network, unsigned int= *netbits,
+ = int msglevel);
+
+int openvpn_ge= taddrinfo(unsigned int flags, const char *hostname, const char *servname,
+ = int resolve_retry_seconds, struct signal_info *sig_info, int ai_family,<= /span>
+ = struct addrinfo **res);
+
+/* return v= alues 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 i= n_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 all= ow_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<= /span>
+ * optional proto su= pport
+ */
= +enum proto_num
+{

+ PROTO_NONE, /* catch for uninitialized */<= br>+ PROTO_UDP,
+ PROTO_TCP,

+ PROTO_TCP_SERVER,
+ PROTO_TCP_CLIENT,

+ PROTO_N

+};
+
+st= atic inline bool
+pro= to_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
+ */
+s= tatic inline bool
+pr= oto_is_udp(int proto)
+{
+ ASSERT(proto= >=3D 0 && proto < PROTO_N);
+ return proto =3D=3D PROTO_UDP;
+}
+
= +/**
+ * @brief Retur= n if the protocol is datagram (UDP)
+ *
+ = */
+static inline boo= l
+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)
+ */<= br>+static inline bool+proto_is_tcp(int proto)
+{
+ ASSERT(proto >=3D 0 && pro= to < 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);
<= span style=3D"color: hsl(120, 100%, 40%);">+

+const char *proto2ascii(int proto, sa_family_t af,= bool display_form);
= +
+const char *proto2= ascii_all(struct gc_arena *gc);
+
+const c= har *proto_remote(int proto, bool remote);
+
+const char *addr_family_name(int af);
+
+addr_defined(const struct openvpn_sockaddr *addr)
+{
+ if (!addr)
+ {
+ = return 0;
+ }=
+ switch (addr-&g= t;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->add= r.in6.sin6_addr);
+
+ default:
+ return 0;
+ }
+}
+
+static inline bool
= +addr_local(const struct sockaddr *addr)
+{
+ = {
+ return f= alse;
+ }+ switch (addr->sa_fami= ly)
+ {
= + case AF_INET:+ return ((const s= truct sockaddr_in *)addr)->sin_addr.s_addr =3D=3D htonl(INADDR_LOOPBACK)= ;
+
+ case AF_INET6:
+ return IN6_IS_ADDR_LOO= PBACK(&((const struct sockaddr_in6 *)addr)->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)
<= span style=3D"color: hsl(120, 100%, 40%);">+ {

+#if defined(HAVE_IN_PKTINFO) && defin= ed(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_UNSP= ECIFIED(&lsa->pi.in6.ipi6_addr);
+
= + default:
+ = return 0;
+= }
+#else /* if EN= ABLE_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
+{
+ return !proto_is_dgram(proto);
+}
+
+static= inline bool
+link_so= cket_actual_defined(const struct link_socket_actual *act)
+{
+ return act && addr_defined(&act->des= t);
+}
+

+static inline bool
+addr_match(const struct openvpn_sockaddr *a1, const st= ruct 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_ad= dr.s_addr;
++ case AF_INET6:
+ return IN6_AR= E_ADDR_EQUAL(&a1->addr.in6.sin6_addr, &a2->addr.in6.sin6_addr= );
+ }
<= span style=3D"color: hsl(120, 100%, 40%);">+ ASSERT(0);

+ return false;
+}
+
+= static inline bool
+a= ddrlist_match(const struct openvpn_sockaddr *a1, const struct addrinfo *add= rlist)
+{
<= span style=3D"color: hsl(120, 100%, 40%);">+ const struct addrinfo *cure= le;

+ 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_= addr.s_addr)
+ = {
+ = return true;
+ }
+ break;
+
+ ca= se AF_INET6:
+ = if (IN6_ARE_ADDR_EQUAL(&a1->addr.in6.sin6_addr,
<= span style=3D"color: hsl(120, 100%, 40%);">+ = &((struct sockaddr_in6 *)curele->ai_addr)->sin6_addr))
+ {
+ retu= rn true;
+ = }
+ = break;
++ default:+ ASSERT(0);
+ }
<= span style=3D"color: hsl(120, 100%, 40%);">+ }

+ return false;
+}
+
+static in= line bool
+addrlist_p= ort_match(const struct openvpn_sockaddr *a1, const struct addrinfo *a2)
+{
+ const struct addrinfo *curele;=
+ for (curele =3D a2; cu= rele; curele =3D curele->ai_next)
+ {
+ {
+ case AF_INET:
+ if (curele->ai_family =3D=3D AF_INE= T
+ = && a1->addr.in4.sin_addr.s_addr
+ =3D=3D ((struct sockadd= r_in *)curele->ai_addr)->sin_addr.s_addr
+ && a1->addr.in4.s= in_port =3D=3D ((struct sockaddr_in *)curele->ai_addr)->sin_port)
+ {
+ retur= n true;
+ = }
+ = break;
+
= + case AF_INET6:
+ if (cur= ele->ai_family =3D=3D AF_INET6
+ && IN6_ARE_ADDR_EQUAL(&a1->= ;addr.in6.sin6_addr,
= + &((struct sockaddr_in6 *)cur= ele->ai_addr)->sin6_addr)
+ && a1->addr.in6.sin6_port=
+ = =3D=3D ((struct sockaddr_in6 *)curele->ai_addr)->sin6_port)+ {
= + return tru= e;
+ }=
+ bre= ak;
+
+ default:

+ ASSERT(0);
<= br>+ }
+ }
+ return false;
+}
+
+
+static inline bool

+addr_port_match(const struct openvp= n_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
+ && a1->addr.in4.sin_port =3D= =3D a2->addr.in4.sin_port;
+
+ c= ase AF_INET6:
+ = return IN6_ARE_ADDR_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 struct openvpn_sockaddr *a1, const str= uct openvpn_sockaddr *a2,
+ const int proto)
+{
+ return link_socket_proto_connection_oriented(proto) ? addr_match(a1, = a2)
+ = : addr_port_match(a1, a2);+}
+
+
+static in= line bool
+addrlist_m= atch_proto(const struct openvpn_sockaddr *a1, struct addrinfo *addr_list, c= onst int proto)
+{
+ return link_socket= _proto_connection_oriented(proto) ? addrlist_match(a1, addr_list)+ = : 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->ad= dr.in6.sin6_addr, 0, sizeof(struct in6_addr));

+ break;
+ }
+}
+
+static inline int<= br>+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_acc= ept() with empty addr */
+ msg(M_ERR, "Bad address family: %d\n", af);
+ ASSERT(0);
+#endif
+ return 0;

+ }

+}
+
+static i= nline bool
+link_sock= et_actual_match(const struct link_socket_actual *a1, const struct link_sock= et_actual *a2)
+{
+ return addr_port_ma= tch(&a1->dest, &a2->dest);
+}
+#endifdiff --git a/src/openvpn/ssl_common.h b/src/openvpn/ssl_common.h
index e8fde86..428bf5a 100644
--- a/src/openvp= n/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"
#inclu= de "crypto.h"
#include "options.h"
diff --git a/src/openvpn/tun.c b/src/openvpn/tun.c
<= span>index f54b608..6c3096b 100644

--- a/src/openvpn/tun.c<= /span>
+++ b/src/openvpn/tun.c
@@ -39,7 +39,7 @@
#include "fdmisc.h"
#include &qu= ot;common.h"
#include "run_command.h"
-#include "socket.h&quo= t;
+#include "so= cket_util.h"
#include "manage.h"
= #include "route.h"
#include "win32.h= "

To view, visit change 1159. To unsub= scribe, or for help writing mail filters, visit settings.

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I7393459b975fb9b3e0a42743f58= 645f769d1be5a
Gerrit-Change-Number: 1159
Gerrit-PatchSet: 1
Gerrit-Owner: flichtenheld <frank@...2723...= .>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
Gerrit-MessageType: newchange
--iDkyn9UOUYE=--