Skip to contentSkip to conversation

Add DNS SRV host discovery support

PATCH Superseded Vladislav Grishenko 2 participants 8 messages Aug 24, 2020 – Sep 9, 2020

v1 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add DNS SRV host discovery support

DNS SRV (rfc2782) support allows to use several OpenVPN servers for a single domain w/o explicit profile enumerating, to move services from host to host with little fuss, and to designate some hosts as primary servers for a service and others as backups.
OpenVPN client ask for a specific service/protocol for a specific domain and get back the names & ports of available servers to connect to, ordered by priority and relative weight. Note, implemented server selection is intended to express static information such as "this server has a faster CPU than that one" or "this server has a much better network connection than that one" at the moment client asks for a records.
Feature has been asked several times already, can be useful in case of for huge ammounts of client & servers deployed.

For example, instead of multiple remotes in profile:
remote server.example.com 1194 udp remote server1.example.com 1195 udp remote server2.example.org 1196 udp remote server.example.com 443 tcp
remote server1.example.tld 8443 tcp

Now it's possible to specify just static domain(s) and enable discovery:
remote server.example.com 1194 udp
remote server.example.com 443 tcp
remote-discovery

When discovery is enabled, OpenVPN will first try to resolve _openvpn._udp.server.example.com SRV records and use resolved targets (see example below) as servers for the subsequent connection attempts. If there's no records or resolve error, OpenVPN will fallback to direct connection using specified server.example.com host and 1194 port. Next connection attempt will be done with next configures connection/remote entry - using _openvpn._tcp.server.example.com records (if any) and server.example.com:443 as last resort.

DNS zone can look like below (arbitrary prio & weight values):
_openvpn._udp.server.example.com IN SRV 10 70 1194 server.example.com _openvpn._udp.server.example.com IN SRV 10 30 1195 server1.example.com _openvpn._udp.server.example.com IN SRV 20 0 1196 server2.example.org _openvpn._tcp.server.example.com IN SRV 10 10 443 server.example.com _openvpn._tcp.server.example.com IN SRV 10 200 443 server.example.com

Currently only "openvpn" service is supported, usage of per-connection service names is up to syntax decision (either intead of fallback port, or as remote-discovery argument, etc), almost all the required mechanics is implemented for that.

References:
https://tools.ietf.org/html/rfc2782
https://en.wikipedia.org/wiki/SRV_record
https://sourceforge.net/p/openvpn/mailman/message/34364911/
https://forums.openvpn.net/viewtopic.php?f=10&t=13660

---
 configure.ac                        |   2 +-
 doc/man-sections/client-options.rst |  26 ++
 src/openvpn/Makefile.am             |   2 +-
 src/openvpn/buffer.h                |   5 -
 src/openvpn/init.c                  |   9 +-
 src/openvpn/manage.c                |   2 +-
 src/openvpn/openvpn.vcxproj         |   8 +-
 src/openvpn/options.c               |  11 +
 src/openvpn/options.h               |   1 +
 src/openvpn/ps.c                    |   2 +-
 src/openvpn/socket.c                | 530 ++++++++++++++++++++++++++--
 src/openvpn/socket.h                |  15 +
 src/openvpn/syshead.h               |   5 +
 13 files changed, 580 insertions(+), 38 deletions(-)
configure.ac +1 -1
index f8279924..67251bed 100644
@@ -477,7 +477,7 @@ SOCKET_INCLUDES="
 "
 
 AC_CHECK_HEADERS(
-	[net/if.h netinet/ip.h resolv.h sys/un.h net/if_utun.h sys/kern_control.h],
+	[net/if.h netinet/ip.h arpa/nameser.h resolv.h sys/un.h net/if_utun.h sys/kern_control.h],
 	,
 	,
 	[[${SOCKET_INCLUDES}]]
doc/man-sections/client-options.rst +26 -0
index ec1e3b11..53e6580e 100644
@@ -299,6 +299,32 @@ configuration.
   specification (4/6 suffix), OpenVPN will try both IPv4 and IPv6
   addresses, in the order getaddrinfo() returns them.
 
+--remote-discovery
+  Perform rfc2782 remote host discovery using specified ``host`` and
+  ``proto`` values.
+  OpenVPN will try to resolve `` _openvpn._proto.host`` name via DNS
+  and use returned DNS SRV target and port records as OpenVPN servers
+  addresses in the order specified by Priority and Weight of that
+  records. If one record resolves to multiple IP addresses, OpenVPN
+  will try them in the order that the system getaddrinfo() presents
+  them. If discovery is failed, usual ``host`` and ``port`` connection
+  will be tried as a fallback.
+
+  Example:
+  ::
+
+     remote example.net 1194 udp
+     remote example.net 443 tcp
+     remote-discovery
+
+  DNS zone:
+  ::
+
+     _openvpn._udp.example.net IN SRV 10 60 1194 server1.example.net
+     _openvpn._udp.example.net IN SRV 10 40 1194 server2.example.net
+     _openvpn._udp.example.net IN SRV 20  0 1194 server3.example.net
+     _openvpn._tcp.example.net IN SRV 10  0  443 server4.example.net
+
 --remote-random
   When multiple ``--remote`` address/ports are specified, or if connection
   profiles are being used, initially randomize the order of the list as a
src/openvpn/Makefile.am +1 -1
index 37b002c6..f7632047 100644
@@ -143,5 +143,5 @@ openvpn_LDADD = \
 	$(OPTIONAL_INOTIFY_LIBS)
 if WIN32
 openvpn_SOURCES += openvpn_win32_resources.rc block_dns.c block_dns.h ring_buffer.h
-openvpn_LDADD += -lgdi32 -lws2_32 -lwininet -lcrypt32 -liphlpapi -lwinmm -lfwpuclnt -lrpcrt4 -lncrypt -lsetupapi
+openvpn_LDADD += -lgdi32 -lws2_32 -lwininet -lcrypt32 -liphlpapi -lwinmm -lfwpuclnt -lrpcrt4 -lncrypt -lsetupapi -ldnsapi
 endif
src/openvpn/buffer.h +0 -5
index 1722ffd5..ae0cf0eb 100644
@@ -198,11 +198,6 @@ bool buf_init_debug(struct buffer *buf, int offset, const char *file, int line);
 
 
 /* inline functions */
-inline static void
-gc_freeaddrinfo_callback(void *addr)
-{
-    freeaddrinfo((struct addrinfo *) addr);
-}
 
 /** Return an empty struct buffer */
 static inline struct buffer
src/openvpn/init.c +7 -2
index a785934a..043f4475 100644
@@ -365,6 +365,7 @@ management_callback_remote_cmd(void *arg, const char **p)
 
                 ce->remote = rhs->host;
                 ce->remote_port = rhs->port;
+                ce->remote_discovery = false;
                 flags = CE_MAN_QUERY_REMOTE_MOD;
                 ret = true;
             }
@@ -458,7 +459,7 @@ clear_remote_addrlist(struct link_socket_addr *lsa, bool free)
 {
     if (lsa->remote_list && free)
     {
-        freeaddrinfo(lsa->remote_list);
+        openvpn_freeaddrinfo(lsa->remote_list);
     }
     lsa->remote_list = NULL;
     lsa->current_remote = NULL;
@@ -3411,6 +3412,10 @@ do_init_socket_1(struct context *c, const int mode)
         sockflags |= SF_PORT_SHARE;
     }
 #endif
+    if (c->options.ce.remote_discovery)
+    {
+        sockflags |= SF_HOST_DISCOVERY;
+    }
 
     link_socket_init_phase1(c->c2.link_socket,
                             c->options.ce.local,
@@ -3652,7 +3657,7 @@ do_close_link_socket(struct context *c)
     {
         if (c->c1.link_socket_addr.bind_local && !c->options.resolve_in_advance)
         {
-            freeaddrinfo(c->c1.link_socket_addr.bind_local);
+            openvpn_freeaddrinfo(c->c1.link_socket_addr.bind_local);
         }
 
         c->c1.link_socket_addr.bind_local = NULL;
src/openvpn/manage.c +1 -1
index 898cb3b3..f0d1e76e 100644
@@ -2556,7 +2556,7 @@ man_settings_close(struct man_settings *ms)
 {
     if (ms->local)
     {
-        freeaddrinfo(ms->local);
+        openvpn_freeaddrinfo(ms->local);
     }
     free(ms->write_peer_info_file);
     CLEAR(*ms);
src/openvpn/openvpn.vcxproj +4 -4
index 5367979d..996c0160 100644
@@ -92,7 +92,7 @@
     </ClCompile>
     <ResourceCompile />
     <Link>
-      <AdditionalDependencies>legacy_stdio_definitions.lib;Ncrypt.lib;libssl.lib;libcrypto.lib;lzo2.lib;pkcs11-helper.dll.lib;gdi32.lib;ws2_32.lib;wininet.lib;crypt32.lib;iphlpapi.lib;winmm.lib;Fwpuclnt.lib;Rpcrt4.lib;%(AdditionalDependencies)</AdditionalDependencies>
+      <AdditionalDependencies>legacy_stdio_definitions.lib;Ncrypt.lib;libssl.lib;libcrypto.lib;lzo2.lib;pkcs11-helper.dll.lib;gdi32.lib;ws2_32.lib;wininet.lib;crypt32.lib;iphlpapi.lib;winmm.lib;Fwpuclnt.lib;Rpcrt4.lib;dnsapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
       <AdditionalLibraryDirectories>$(OPENSSL_HOME)/lib;$(LZO_HOME)/lib;$(PKCS11H_HOME)/lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
       <SubSystem>Console</SubSystem>
     </Link>
@@ -107,7 +107,7 @@
     </ClCompile>
     <ResourceCompile />
     <Link>
-      <AdditionalDependencies>legacy_stdio_definitions.lib;Ncrypt.lib;libssl.lib;libcrypto.lib;lzo2.lib;pkcs11-helper.dll.lib;gdi32.lib;ws2_32.lib;wininet.lib;crypt32.lib;iphlpapi.lib;winmm.lib;Fwpuclnt.lib;Rpcrt4.lib;setupapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
+      <AdditionalDependencies>legacy_stdio_definitions.lib;Ncrypt.lib;libssl.lib;libcrypto.lib;lzo2.lib;pkcs11-helper.dll.lib;gdi32.lib;ws2_32.lib;wininet.lib;crypt32.lib;iphlpapi.lib;winmm.lib;Fwpuclnt.lib;Rpcrt4.lib;setupapi.lib;dnsapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
       <AdditionalLibraryDirectories>$(OPENSSL_HOME)/lib;$(LZO_HOME)/lib;$(PKCS11H_HOME)/lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
       <SubSystem>Console</SubSystem>
     </Link>
@@ -122,7 +122,7 @@
     </ClCompile>
     <ResourceCompile />
     <Link>
-      <AdditionalDependencies>legacy_stdio_definitions.lib;Ncrypt.lib;libssl.lib;libcrypto.lib;lzo2.lib;pkcs11-helper.dll.lib;gdi32.lib;ws2_32.lib;wininet.lib;crypt32.lib;iphlpapi.lib;winmm.lib;Fwpuclnt.lib;Rpcrt4.lib;%(AdditionalDependencies)</AdditionalDependencies>
+      <AdditionalDependencies>legacy_stdio_definitions.lib;Ncrypt.lib;libssl.lib;libcrypto.lib;lzo2.lib;pkcs11-helper.dll.lib;gdi32.lib;ws2_32.lib;wininet.lib;crypt32.lib;iphlpapi.lib;winmm.lib;Fwpuclnt.lib;Rpcrt4.lib;dnsapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
       <AdditionalLibraryDirectories>$(OPENSSL_HOME)/lib;$(LZO_HOME)/lib;$(PKCS11H_HOME)/lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
       <SubSystem>Console</SubSystem>
     </Link>
@@ -137,7 +137,7 @@
     </ClCompile>
     <ResourceCompile />
     <Link>
-      <AdditionalDependencies>legacy_stdio_definitions.lib;Ncrypt.lib;libssl.lib;libcrypto.lib;lzo2.lib;pkcs11-helper.dll.lib;gdi32.lib;ws2_32.lib;wininet.lib;crypt32.lib;iphlpapi.lib;winmm.lib;Fwpuclnt.lib;Rpcrt4.lib;setupapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
+      <AdditionalDependencies>legacy_stdio_definitions.lib;Ncrypt.lib;libssl.lib;libcrypto.lib;lzo2.lib;pkcs11-helper.dll.lib;gdi32.lib;ws2_32.lib;wininet.lib;crypt32.lib;iphlpapi.lib;winmm.lib;Fwpuclnt.lib;Rpcrt4.lib;setupapi.lib;dnsapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
       <AdditionalLibraryDirectories>$(OPENSSL_HOME)/lib;$(LZO_HOME)/lib;$(PKCS11H_HOME)/lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
       <SubSystem>Console</SubSystem>
     </Link>
src/openvpn/options.c +11 -0
index 8bf82c57..79e2a61f 100644
@@ -123,6 +123,7 @@ static const char usage_message[] =
     "--remote host [port] : Remote host name or ip address.\n"
     "--remote-random : If multiple --remote options specified, choose one randomly.\n"
     "--remote-random-hostname : Add a random string to remote DNS name.\n"
+    "--remote-discovery : Perform rfc2782 remote host discovery.\n"
     "--mode m        : Major mode, m = 'p2p' (default, point-to-point) or 'server'.\n"
     "--proto p       : Use protocol p for communicating with peer.\n"
     "                  p = udp (default), tcp-server, or tcp-client\n"
@@ -1446,6 +1447,7 @@ show_connection_entry(const struct connection_entry *o)
     SHOW_STR(local_port);
     SHOW_STR(remote);
     SHOW_STR(remote_port);
+    SHOW_BOOL(remote_discovery);
     SHOW_BOOL(remote_float);
     SHOW_BOOL(bind_defined);
     SHOW_BOOL(bind_local);
@@ -6672,6 +6674,11 @@ add_option(struct options *options,
         VERIFY_PERMISSION(OPT_P_GENERAL);
         options->sockflags |= SF_HOST_RANDOMIZE;
     }
+    else if (streq(p[0], "remote-discovery") && !p[2])
+    {
+        VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_CONNECTION);
+        options->ce.remote_discovery = true;
+    }
     else if (streq(p[0], "setenv") && p[1] && !p[3])
     {
         VERIFY_PERMISSION(OPT_P_GENERAL);
@@ -6679,6 +6686,10 @@ add_option(struct options *options,
         {
             options->sockflags |= SF_HOST_RANDOMIZE;
         }
+        if (streq(p[1], "REMOTE_DISCOVERY"))
+        {
+            options->ce.remote_discovery = true;
+        }
         else if (streq(p[1], "GENERIC_CONFIG"))
         {
             msg(msglevel, "this is a generic configuration and cannot directly be used");
src/openvpn/options.h +1 -0
index 877e9396..26f8e791 100644
@@ -92,6 +92,7 @@ struct connection_entry
     const char *remote_port;
     const char *local;
     const char *remote;
+    bool remote_discovery;
     bool remote_float;
     bool bind_defined;
     bool bind_ipv6_only;
src/openvpn/ps.c +1 -1
index 2089e6b9..40343fe0 100644
@@ -841,7 +841,7 @@ port_share_open(const char *host,
                                  host, port,  0, NULL, AF_INET, &ai);
     ASSERT(status==0);
     hostaddr = *((struct sockaddr_in *) ai->ai_addr);
-    freeaddrinfo(ai);
+    openvpn_freeaddrinfo(ai);
 
     /*
      * Make a socket for foreground and background processes
src/openvpn/socket.c +507 -23

623 lines not on this page · raw message

src/openvpn/socket.h +15 -0
index 7aeae527..7e70c481 100644
@@ -40,6 +40,11 @@
  */
 #define OPENVPN_PORT "1194"
 
+/*
+ * OpenVPN's default service name for rfc2782 discovery.
+ */
+#define OPENVPN_SERVICE "openvpn"
+
 /*
  * Number of seconds that "resolv-retry infinite"
  * represents.
@@ -210,6 +215,7 @@ struct link_socket
 #define SF_PORT_SHARE (1<<2)
 #define SF_HOST_RANDOMIZE (1<<3)
 #define SF_GETADDRINFO_DGRAM (1<<4)
+#define SF_HOST_DISCOVERY (1<<5)
     unsigned int sockflags;
     int mark;
     const char *bind_dev;
@@ -532,6 +538,7 @@ bool unix_socket_get_peer_uid_gid(const socket_descriptor_t sd, int *uid, int *g
 #define GETADDR_RANDOMIZE             (1<<9)
 #define GETADDR_PASSIVE               (1<<10)
 #define GETADDR_DATAGRAM              (1<<11)
+#define GETADDR_DISCOVERY             (1<<12)
 
 #define GETADDR_CACHE_MASK              (GETADDR_DATAGRAM|GETADDR_PASSIVE)
 
@@ -561,6 +568,14 @@ int openvpn_getaddrinfo(unsigned int flags,
                         int ai_family,
                         struct addrinfo **res);
 
+void openvpn_freeaddrinfo(struct addrinfo *res);
+
+inline static void
+gc_freeaddrinfo_callback(void *addr)
+{
+    openvpn_freeaddrinfo((struct addrinfo *) addr);
+}
+
 /*
  * Transport protocol naming and other details.
  */
src/openvpn/syshead.h +5 -0
index 8342eae0..e5852753 100644
@@ -38,6 +38,7 @@
 
 #ifdef _WIN32
 #include <windows.h>
+#include <windns.h>
 #include <winsock2.h>
 #include <tlhelp32.h>
 #define sleep(x) Sleep((x)*1000)
@@ -176,6 +177,10 @@
 #include <netinet/in.h>
 #endif
 
+#ifdef HAVE_ARPA_NAMESER_H
+#include <arpa/nameser.h>
+#endif
+
 #ifdef HAVE_RESOLV_H
 #include <resolv.h>
 #endif
Signature · 1 line
2.17.1
#1 lore raw

Am 25.08.20 um 00:15 schrieb Vladislav Grishenko:

Quoted 4 lines

DNS SRV (rfc2782) support allows to use several OpenVPN servers for a single domain w/o explicit profile enumerating, to move services from host to host with little fuss, and to designate some hosts as primary servers for a service and others as backups.

Quoted 8 lines

OpenVPN client ask for a specific service/protocol for a specific domain and get back the names & ports of available servers to connect to, ordered by priority and relative weight. Note, implemented server selection is intended to express static information such as "this server has a faster CPU than that one" or "this server has a much better network connection than that one" at the moment client asks for a records.
Feature has been asked several times already, can be useful in case of for huge ammounts of client & servers deployed.

Hu? You are talking here about loading balancing by a weight, right?

Quoted 7 lines

For example, instead of multiple remotes in profile:
remote server.example.com 1194 udp remote server1.example.com 1195 udp remote server2.example.org 1196 udp remote server.example.com 443 tcp
remote server1.example.tld 8443 tcp

Quoted 6 lines

Now it's possible to specify just static domain(s) and enable discovery:
remote server.example.com 1194 udp
remote server.example.com 443 tcp
remote-discovery

When discovery is enabled,

I wonder if we should enable this by default. But this still fixes the order of UDP and TCP in the config. Ideally I want just to say:

remote vpn.mycorp.com and then it should resolve to all UDP+TCP options. And it should be possible to point the client to try udp/1194/primary.vpn.corp.com and then 443/tcp/primary.vpn.corp.com and only after that try 1194/backup.vpn.corp.com. With this syntax it looks like that is not possible.

Quoted 10 lines

OpenVPN will first try to resolve _openvpn._udp.server.example.com SRV records and use resolved targets (see example below) as servers for the subsequent connection attempts. If there's no records or resolve error, OpenVPN will fallback to direct connection using specified server.example.com host and 1194 port. Next connection attempt will be done with next configures connection/remote entry - using _openvpn._tcp.server.example.com records (if any) and server.example.com:443 as last resort.

DNS zone can look like below (arbitrary prio & weight values):

What/how are prio and weight used?

Quoted 8 lines

_openvpn._udp.server.example.com IN SRV 10 70 1194 server.example.com _openvpn._udp.server.example.com IN SRV 10 30 1195 server1.example.com _openvpn._udp.server.example.com IN SRV 20 0 1196 server2.example.org _openvpn._tcp.server.example.com IN SRV 10 10 443 server.example.com _openvpn._tcp.server.example.com IN SRV 10 200 443 server.example.com

Currently only "openvpn" service is supported, usage of per-connection service names is up to syntax decision (either intead of fallback port, or

                                                 ^^^^ typo
Quoted 2 lines

as remote-discovery argument, etc), almost all the required mechanics is implemented for that.

Almost all? What is missing?

Quoted 42 lines

References:
https://tools.ietf.org/html/rfc2782
https://en.wikipedia.org/wiki/SRV_record
https://sourceforge.net/p/openvpn/mailman/message/34364911/
https://forums.openvpn.net/viewtopic.php?f=10&t=13660

---
 configure.ac                        |   2 +-
 doc/man-sections/client-options.rst |  26 ++
 src/openvpn/Makefile.am             |   2 +-
 src/openvpn/buffer.h                |   5 -
 src/openvpn/init.c                  |   9 +-
 src/openvpn/manage.c                |   2 +-
 src/openvpn/openvpn.vcxproj         |   8 +-
 src/openvpn/options.c               |  11 +
 src/openvpn/options.h               |   1 +
 src/openvpn/ps.c                    |   2 +-
 src/openvpn/socket.c                | 530 ++++++++++++++++++++++++++--
 src/openvpn/socket.h                |  15 +
 src/openvpn/syshead.h               |   5 +
 13 files changed, 580 insertions(+), 38 deletions(-)
configure.ac +1 -1
index f8279924..67251bed 100644
@@ -477,7 +477,7 @@ SOCKET_INCLUDES="
 "
 
 AC_CHECK_HEADERS(
-	[net/if.h netinet/ip.h resolv.h sys/un.h net/if_utun.h sys/kern_control.h],
+	[net/if.h netinet/ip.h arpa/nameser.h resolv.h sys/un.h net/if_utun.h sys/kern_control.h],
 	,
 	,
 	[[${SOCKET_INCLUDES}]]
doc/man-sections/client-options.rst +0 -0
index ec1e3b11..53e6580e 100644

doc/man-sections/client-options.rst configuration. · line 302

Quoted 4 lines
doc/man-sections/client-options.rst +1 -0
@@ -299,6 +299,32 @@ configuration.
   specification (4/6 suffix), OpenVPN will try both IPv4 and IPv6
   addresses, in the order getaddrinfo() returns them.
 
+--remote-discovery

discovery is a bit ambigious. Maybe rather use --remote-service-discovery?

doc/man-sections/client-options.rst configuration. · line 304

Quoted 2 lines
doc/man-sections/client-options.rst +2 -0
+  Perform rfc2782 remote host discovery using specified ``host`` and
+  ``proto`` values.

Spelling, RFC 2782. But RFC numbers are not user friendly when used alone. I would at least add DNS SRV somewhere in there.

Quoted 13 lines
doc/man-sections/client-options.rst +13 -0
+  OpenVPN will try to resolve `` _openvpn._proto.host`` name via DNS
+  and use returned DNS SRV target and port records as OpenVPN servers
+  addresses in the order specified by Priority and Weight of that
+  records. If one record resolves to multiple IP addresses, OpenVPN
+  will try them in the order that the system getaddrinfo() presents
+  them. If discovery is failed, usual ``host`` and ``port`` connection
+  will be tried as a fallback.
+
+  Example:
+  ::
+
+     remote example.net 1194 udp
+     remote example.net 443 tcp

doc/man-sections/client-options.rst configuration. · line 327

Quoted 10 lines
doc/man-sections/client-options.rst +10 -0
+     remote-discovery
+
+  DNS zone:
+  ::
+
+     _openvpn._udp.example.net IN SRV 10 60 1194 server1.example.net
+     _openvpn._udp.example.net IN SRV 10 40 1194 server2.example.net
+     _openvpn._udp.example.net IN SRV 20  0 1194 server3.example.net
+     _openvpn._tcp.example.net IN SRV 10  0  443 server4.example.net
+

The man page is completely missing how the weight/priority is interpreted by OpenVPN.

doc/man-sections/client-options.rst static const char usage_message[] = · line 126

Quoted 6 lines
doc/man-sections/client-options.rst +2 -0
+++ b/src/openvpn/options.c
@@ -123,6 +123,7 @@ static const char usage_message[] =
     "--remote host [port] : Remote host name or ip address.\n"
     "--remote-random : If multiple --remote options specified, choose one randomly.\n"
     "--remote-random-hostname : Add a random string to remote DNS name.\n"
+    "--remote-discovery : Perform rfc2782 remote host discovery.\n"

See other comment.

doc/man-sections/client-options.rst add_option(struct options *options, · line 6692

Quoted 11 lines
doc/man-sections/client-options.rst +4 -0
     else if (streq(p[0], "setenv") && p[1] && !p[3])
     {
         VERIFY_PERMISSION(OPT_P_GENERAL);
@@ -6679,6 +6686,10 @@ add_option(struct options *options,
         {
             options->sockflags |= SF_HOST_RANDOMIZE;
         }
+        if (streq(p[1], "REMOTE_DISCOVERY"))
+        {
+            options->ce.remote_discovery = true;
+        }

I don't think we should add this. We already have ignore-unknown-options and setenv opt to write backwards compatible configs.

doc/man-sections/client-options.rst do_preresolve(struct context *c) · line 428

Quoted 7 lines
doc/man-sections/client-options.rst +1 -1
@@ -416,7 +424,7 @@ do_preresolve(struct context *c)
         if (ce->bind_local)
         {
             flags |= GETADDR_PASSIVE;
-            flags &= ~GETADDR_RANDOMIZE;
+            flags &= ~(GETADDR_RANDOMIZE|GETADDR_DISCOVERY);
             status = do_preresolve_host(c, ce->local, ce->local_port, ce->af, flags);

Hm does this do a DNS SRV discovery for the local bind address? That seems odd.

Quoted 11 lines
doc/man-sections/client-options.rst +5 -0
             if (status != 0)
             {
@@ -432,6 +440,363 @@ err:
     throw_signal_soft(SIGHUP, "Preresolving failed");
 }
 
+/* Struct to hold resolved srv records */
+struct srvinfo
+{
+    const char *hostname;
+    const char *servname;

doc/man-sections/client-options.rst err: · line 457

Quoted 10 lines
doc/man-sections/client-options.rst +7 -0
+    unsigned short prio;
+    unsigned short weight;
+    int order;
+    struct srvinfo *next;
+};
 
 
 
+static int
+rfc2782_cmp(const void *a, const void *b)

It does compare srvinfo structs and should be named accordingly. Also please add a doxygen comment, say how it compares them.

Quoted 15 lines
doc/man-sections/client-options.rst +15 -0
+{
+    const struct srvinfo *ae = *(struct srvinfo **)a;
+    const struct srvinfo *be = *(struct srvinfo **)b;
+
+    /* lowest-numbered priority first */
+    if (ae->prio != be->prio)
+    {
+        return ae->prio < be->prio ? -1 : 1;
+    }
+
+    /* zero-weighted first */
+    if ((ae->weight == 0 && be->weight)
+        || (ae->weight && be->weight == 0))
+    {
+        return ae->weight < be->weight ? -1 : 1;

doc/man-sections/client-options.rst err:

Quoted 10 lines
doc/man-sections/client-options.rst +8 -0
+    }
+
+    /* keep received order */
+    return ae->order < be->order ? -1 : 1;
+}
 
 
+static struct srvinfo *
+rfc2782_sort(struct srvinfo *list, int count)
+{

Doxygen, sorting by what etc.

Quoted 18 lines
doc/man-sections/client-options.rst +18 -0
+    struct srvinfo head, *tail, **sorted;
+    struct gc_arena gc = gc_new();
+
+    ASSERT(list);
+    ASSERT(count);
+
+    head.next = NULL;
+    tail = &head;
+
+    /* sort records by priority and zero weight */
+    ALLOC_ARRAY_CLEAR_GC(sorted, struct srvinfo *, count, &gc);
+    for (struct srvinfo *e = list; e; e = e->next)
+    {
+        ASSERT(e->order < count);
+        sorted[e->order] = e;
+    }
+    qsort(sorted, count, sizeof(sorted[0]), rfc2782_cmp);
+

doc/man-sections/client-options.rst

Quoted 10 lines
doc/man-sections/client-options.rst +10 -0
+    /* apply weighted selection mechanism */
+    for (int i = 0; i < count;)
+    {
+        struct srvinfo unordered;
+
+        /* compute the sum of the weights of records of the same
+         * priority and put them in the unordered list */
+        unordered.prio = sorted[i]->prio;
+        unordered.weight = 0;
+        unordered.next = NULL;

unordered.weight, unordered.prio seems just used as variables in this algorithm.

Quoted 7 lines
doc/man-sections/client-options.rst +7 -0
+        for (struct srvinfo *prev = &unordered; i < count && sorted[i]->prio == unordered.prio; i++)
+        {
+            struct srvinfo *e = sorted[i];
+
+            unordered.weight += e->weight;
+
+            /* add entry to the tail of unordered list */

doc/man-sections/client-options.rst

Quoted 10 lines
doc/man-sections/client-options.rst +10 -0
+            e->next = NULL;
+            prev->next = e, prev = e;
+        }
+
+        /* process the unordered list */
+        while (unordered.next)
+        {
+            /* choose a uniform random number between 0 and the sum
+             * computed (inclusive) */
+            int weight = unordered.weight ? get_random() % unordered.weight : 0;

This line is hard to parse.

Quoted 15 lines
doc/man-sections/client-options.rst +15 -0
+
+            /* select the entries whose running sum value is the first
+             * in the selected order which is greater than or equal
+             * to the random number selected */
+            for (struct srvinfo *e = unordered.next, *prev = &unordered; e; prev = e, e = e->next)
+            {
+                /* selected entry is the next one to be contacted */
+                if (e->weight >= weight)
+                {
+                    unordered.weight -= e->weight;
+
+                    /* move entry to the ordered list */
+                    prev->next = e->next;
+                    e->next = NULL;
+                    tail->next = e, tail = e;

doc/man-sections/client-options.rst

Quoted 10 lines
doc/man-sections/client-options.rst +10 -0
+
+                    /* in the presence of records containing weights greater
+                     * than 0, records with weight 0 should have a very small
+                     * chance of being selected, so skip them all after the
+                     * last weighted */
+                    if (unordered.weight == 0 && e->weight)
+                    {
+                        unordered.next = NULL;
+                    }
+                    break;

This behaviour needs to be documented in the man page.

Quoted 135 lines
doc/man-sections/client-options.rst +135 -0
+/*
+ * Translated hostname, service and protocol into struct srvinfo.
+ * Multiple records are ordered per rfc2782.
+ */
+static int
+openvpn_getsrvinfo(unsigned int flags,
+                   const char *hostname,
+                   const char *servname,
+                   struct srvinfo **res,
+                   struct gc_arena *gc)
+{
+    struct srvinfo *list = NULL;
+#ifdef _WIN32
+    char qname[DNS_MAX_NAME_BUFFER_LENGTH];
+#else
+    char qname[NS_MAXDNAME];
+#endif
+    int status;
+    int count = 0;
+
+    ASSERT(res);
+
+    ASSERT(hostname && servname);
+    ASSERT(!(flags & GETADDR_HOST_ORDER));
+
+    if (!openvpn_snprintf(qname, sizeof(qname), "_%s._%s.%s", servname,
+                          (flags & GETADDR_DATAGRAM) ? "udp" : "tcp", hostname))
+    {
+        return EAI_MEMORY;
+    }
+
+#ifdef _WIN32
+    PDNS_RECORD pDnsRecord;
+    DNS_STATUS DnsStatus = DnsQuery(qname, DNS_TYPE_SRV, DNS_QUERY_STANDARD, NULL, &pDnsRecord, NULL);
+    dmsg(D_SOCKET_DEBUG, "DNSQUERY type=%d dname=%s result=%d",
+         DNS_TYPE_SRV, qname, DnsStatus);
+    switch (DnsStatus)
+    {
+        case ERROR_SUCCESS:
+            break;
+
+        case DNS_ERROR_RCODE_NAME_ERROR:
+            return EAI_NONAME; /* HOST_NOT_FOUND */
+
+        case DNS_INFO_NO_RECORDS:
+            return EAI_NODATA; /* NO_DATA */
+
+        case DNS_ERROR_NO_DNS_SERVERS:
+        case DNS_ERROR_RCODE_FORMAT_ERROR:
+        case DNS_ERROR_RCODE_NOT_IMPLEMENTED:
+        case DNS_ERROR_RCODE_REFUSED:
+            return EAI_FAIL; /* NO_RECOVERY */
+
+        case ERROR_TIMEOUT:
+        case DNS_ERROR_RCODE_SERVER_FAILURE:
+        case DNS_ERROR_TRY_AGAIN_LATER:
+            return EAI_AGAIN; /* TRY_AGAIN */
+
+        default:
+            return EAI_NODATA;
+    }
+
+    for (PDNS_RECORD rr = pDnsRecord; rr; rr = rr->pNext)
+    {
+        if (rr->wType == DNS_TYPE_SRV)
+        {
+            PDNS_SRV_DATA rdata = &rr->Data.Srv;
+
+            if (rr->wDataLength >= sizeof(DNS_SRV_DATA) && *rdata->pNameTarget)
+            {
+                char port[sizeof("65535")];
+                openvpn_snprintf(port, sizeof(port), "%u", rdata->wPort);
+
+                struct srvinfo *e;
+                ALLOC_OBJ_CLEAR_GC(e, struct srvinfo, gc);
+                e->hostname = string_alloc(rdata->pNameTarget, gc);
+                e->servname = string_alloc(port, gc);
+                e->prio = rdata->wPriority;
+                e->weight = rdata->wWeight;
+                e->order = count++;
+                e->next = list, list = e;
+            }
+        }
+    }
+    DnsRecordListFree(pDnsRecord, DnsFreeParsedMessageFields);
+#else
+    unsigned char answer[NS_MAXMSG];
+    int n = res_query(qname, ns_c_in, ns_t_srv, answer, NS_MAXMSG);
+    dmsg(D_SOCKET_DEBUG, "RES_QUERY class=%d type=%d dname=%s result=%d",
+         ns_c_in, ns_t_srv, qname, n);
+    if (n < 0)
+    {
+        switch (h_errno)
+        {
+            case HOST_NOT_FOUND:
+                return EAI_NONAME;
+
+            case NO_ADDRESS:
+#if NO_ADDRESS != NO_DATA
+            case NO_DATA:
+#endif
+                return EAI_NODATA;
+
+            case NO_RECOVERY:
+                return EAI_FAIL;
+
+            case TRY_AGAIN:
+                return EAI_AGAIN;
+        }
+        return EAI_SYSTEM;
+    }
+
+    ns_msg msg;
+    if (ns_initparse(answer, n, &msg) < 0)
+    {
+        return EAI_FAIL;
+    }
+
+    for (int i = 0; i < ns_msg_count(msg, ns_s_an); i++)
+    {
+        ns_rr rr;
+
+        if (ns_parserr(&msg, ns_s_an, i, &rr) == 0 && ns_rr_type(rr) == ns_t_srv)
+        {
+            const unsigned char *rdata = ns_rr_rdata(rr);
+
+            if (ns_rr_rdlen(rr) > 6
+                && dn_expand(ns_msg_base(msg), ns_msg_end(msg),
+                             rdata + 6, qname, sizeof(qname)) > 0 && *qname)
+            {
+                char port[sizeof("65535")];
+                openvpn_snprintf(port, sizeof(port), "%u", ns_get16(rdata + 4));
+
+                struct srvinfo *e;
+                ALLOC_OBJ_CLEAR_GC(e, struct srvinfo, gc);

doc/man-sections/client-options.rst

Quoted 10 lines
doc/man-sections/client-options.rst +10 -0
+                e->hostname = string_alloc(qname, gc);
+                e->servname = string_alloc(port, gc);
+                e->prio = ns_get16(rdata);
+                e->weight = ns_get16(rdata + 2);
+                e->order = count++;
+                e->next = list, list = e;
+            }
+        }
+    }
+#endif

Can we have the platform specifc part in an extra function? This ifdef makes the function hard to read.

Quoted 4 lines

+/*
+ * Struct to hold chainable copy of struct addinfo.
+ * Must be binary compatible (except size) with original.
+ */

What is binary compatible expect size? The first member needs to be a addrinfo since this might be cast to addrinfo? And if yes, why are we doing a hack like that?

Quoted 6 lines
doc/man-sections/client-options.rst +6 -0
+struct addrinfo_chained
+{
+    struct addrinfo ai;
+    struct openvpn_sockaddr addr;
+    char canonname[0];
+};

doc/man-sections/client-options.rst

Quoted 10 lines
doc/man-sections/client-options.rst +10 -0
+
+/*
+ * System getaddrinfo() returns one or more addrinfo structures.
+ * openvpn_getaddinfo() chains them into one in case DNS SRV
+ * resolve is performed, but unfortunately it can't be safely done
+ * (freed) with musl libc, refer
+ * https://git.musl-libc.org/cgit/musl/commit/?id=d1395c43c019aec6b855cf3c656bf47c8a719e7f
+ * Use own copy of each addrinfo to workaround this and possibly
+ * other getaddrinfo()/freeaddrinfo() implementations.
+ */

This looks like a comment describing the function but it does not desribe the function.

Also wouldn't it be easier to just add a gc special free function for your srvinfo struct that also free the associated addrinfo record instead of doing this strange dance?

Quoted 124 lines
doc/man-sections/client-options.rst +74 -14
+int
+getaddrinfo_chained(const char *node, const char *service,
+                    const struct addrinfo *hints,
+                    struct addrinfo **res)
+{
+    int status = getaddrinfo(node, service, hints, res);
+    if (status == 0 && res && *res)
+    {
+        struct addrinfo_chained head, *tail = &head;
+
+        head.ai.ai_next = NULL;
+        for (struct addrinfo *ai = *res; ai; ai = ai->ai_next)
+        {
+            socklen_t addrlen = ai->ai_addr ? ai->ai_addrlen : 0;
+            size_t namelen = ai->ai_canonname ? strlen(ai->ai_canonname) + 1 : 0;
+
+            /* allocate self-contained struct with enough space for optional data */
+            struct addrinfo_chained *ac = calloc(1, sizeof(*ac) + addrlen + namelen);
+            if (!ac)
+            {
+                openvpn_freeaddrinfo(head.ai.ai_next);
+                head.ai.ai_next = NULL;
+                status = EAI_MEMORY;
+                break;
+            }
+
+            /* deep clone the structure and its members */
+            memcpy(&ac->ai, ai, sizeof(ac->ai));
+            if (ai->ai_addr)
+            {
+                memcpy(&ac->addr.addr.sa, ai->ai_addr, addrlen);
+                ac->ai.ai_addr = &ac->addr.addr.sa;
+            }
+            if (ai->ai_canonname)
+            {
+                memcpy(ac->canonname, ai->ai_canonname, namelen);
+                ac->ai.ai_canonname = ac->canonname;
+            }
+
+            /* add clone to the tail */
+            ac->ai.ai_next = NULL;
+            tail->ai.ai_next = &ac->ai, tail = ac;
+        }
+
+        /* original list is cloned, can be freed now */
+        freeaddrinfo(*res);
+        *res = head.ai.ai_next;
+    }
+    return status;
+}
+
+void
+openvpn_freeaddrinfo(struct addrinfo *res)
+{
+    while (res)
+    {
+        struct addrinfo_chained *ac = (struct addrinfo_chained *)res;
+        res = res->ai_next;
+        free(ac);
+    }
+}
+
 /*
  * Translate IPv4/IPv6 addr or hostname into struct addrinfo
  * If resolve error, try again for resolve_retry_seconds seconds.
@@ -497,7 +862,7 @@ openvpn_getaddrinfo(unsigned int flags,
         hints.ai_socktype = SOCK_STREAM;
     }
 
-    status = getaddrinfo(hostname, servname, &hints, res);
+    status = getaddrinfo_chained(hostname, servname, &hints, res);
 
     if (status != 0) /* parse as numeric address failed? */
     {
@@ -555,16 +920,17 @@ openvpn_getaddrinfo(unsigned int flags,
         /*
          * Resolve hostname
          */
-        while (true)
-        {
+        struct srvinfo *targets = NULL;
 #ifndef _WIN32
-            res_init();
+        res_init();
 #endif
-            /* try hostname lookup */
-            hints.ai_flags &= ~AI_NUMERICHOST;
-            dmsg(D_SOCKET_DEBUG, "GETADDRINFO flags=0x%04x ai_family=%d ai_socktype=%d",
-                 flags, hints.ai_family, hints.ai_socktype);
-            status = getaddrinfo(hostname, servname, &hints, res);
+
+        /* try srv lookup */
+        while (flags & GETADDR_DISCOVERY)
+        {
+            dmsg(D_SOCKET_DEBUG, "GETSRVINFO flags=0x%04x hostname=%s",
+                 flags, np(hostname));
+            status = openvpn_getsrvinfo(flags, hostname, OPENVPN_SERVICE, &targets, &gc);
 
             if (signal_received)
             {
@@ -581,9 +947,6 @@ openvpn_getaddrinfo(unsigned int flags,
                         /* turn success into failure (interrupted syscall) */
                         if (0 == status)
                         {
-                            ASSERT(res);
-                            freeaddrinfo(*res);
-                            *res = NULL;
                             status = EAI_AGAIN; /* = temporary failure */
                             errno = EINTR;
                         }
@@ -592,8 +955,8 @@ openvpn_getaddrinfo(unsigned int flags,
                 }
             }
 
-            /* success? */
-            if (0 == status)
+            /* success and fails are expected, fallback to usual resolve */
+            if (EAI_AGAIN != status)
             {
                 break;
             }
@@ -614,10 +977,127 @@ openvpn_getaddrinfo(unsigned int flags,
 
             if (--resolve_retries <= 0)

doc/man-sections/client-options.rst openvpn_getaddrinfo(unsigned int flags, · line 987

Quoted 10 lines
doc/man-sections/client-options.rst +5 -1
             {
-                goto done;
+                /* can't retry, fallback to usual resolve */
+                break;
             }
 
             management_sleep(fail_wait_interval);
+#ifndef _WIN32
+            res_init();
+#endif

Add a comment why we do that res_init again. It is not obvious for me.

This is not full review. But I feel this emulating addrinfo for remote is a bit hacky. I feel refactoring the current_remote code to be a bit more agnostic and then always use the srvinfo meta struct and iterates through that one to set addrinfo or something in that is a much cleaner solution.

(This might be already in patch) Also if srv records are returned and verb level is high enough, something like 3 or 4, we should log something like whole result of the service discovery.

Arne

#2 lore raw

Hi, Arne
Many thanks the review, please refer comments inline

--
Best Regards, Vladislav Grishenko

Quoted 14 lines

-----Original Message-----
From: Arne Schwabe <[email protected]...>
Sent: Tuesday, August 25, 2020 2:10 PM
Am 25.08.20 um 00:15 schrieb Vladislav Grishenko:

Quoted 10 lines

DNS SRV (rfc2782) support allows to use several OpenVPN servers for a single domain w/o explicit profile enumerating, to move services from host to host with little fuss, and to designate some hosts as primary servers for a service and others as backups.
OpenVPN client ask for a specific service/protocol for a specific domain and get back the names & ports of available servers to connect to, ordered by priority and relative weight. Note, implemented server selection is intended to express static information such as "this server has a faster CPU than that one" or "this server has a much better network connection than that one" at the moment client asks for a

Quoted 5 lines

records.

Quoted 2 lines

Feature has been asked several times already, can be useful in case of for huge ammounts of client & servers deployed.

Hu? You are talking here about loading balancing by a weight, right?

Well, yes at some point, from RFC 2052:
Weight
Load balancing mechanism. When selecting a target host among the those that have the same priority, the chance of trying this one first SHOULD be proportional to its weight. The range of this number is 1-65535. Domain administrators are urged to use Weight 0 when there isn't any load balancing to do, to make the RR easier to read for humans (less noisy).
RFC 2782 has renamed "load balancing" into "server selection":
Weight
A server selection mechanism. The weight field specifies a relative weight for entries with the same priority. Larger weights SHOULD be given a proportionately higher probability of being selected. The range of this number is 0-65535. This is a 16 bit unsigned integer in network byte order. Domain administrators SHOULD use Weight 0 when there isn't any server selection to do, to make the RR easier to read for humans (less noisy). In the presence of records containing weights greater than 0, records with weight 0 should have a very small chance of being selected.

Quoted 15 lines
Quoted 8 lines

For example, instead of multiple remotes in profile:
remote server.example.com 1194 udp remote server1.example.com 1195 udp remote server2.example.org 1196 udp remote server.example.com 443 tcp
remote server1.example.tld 8443 tcp

Quoted 6 lines

Now it's possible to specify just static domain(s) and enable discovery:
remote server.example.com 1194 udp
remote server.example.com 443 tcp
remote-discovery

When discovery is enabled,

Quoted 8 lines

I wonder if we should enable this by default. But this still fixes the order of UDP and TCP in the config. Ideally I want just to say:

remote vpn.mycorp.com and then it should resolve to all UDP+TCP options. And it should be possible to point the client to try udp/1194/primary.vpn.corp.com and then 443/tcp/primary.vpn.corp.com and only after that try 1194/backup.vpn.corp.com. With this syntax it looks like that is not possible.

I'm afraid here we have chicken vs egg issue here.
Service resolution is protocol-based, same as getaddrinfo() uses SOCK_DGRAM/SOCK_STREAM to give correct servname (same service name can have different ports on different proto, afair netbios, that's why originally), so logically we need to know our protocol before asking for service on it. Reflecting it, currently OpenVPN has no "unknown protocol" state for one connection entry and defaults to UDP with no options set to have it in consistent state for subsequent socket/framing initialization and remote resolving.
Same as "remote domain.tld" will be udp-only, service discovery will happen over _udp.domain.tld.

Making OpenVPN service discovery protocol agnostic will mean to allow dynamic protocol change within current connection entry/remote, I'm not sure if openvpn is ready for this. If this can be implemented as looping over udp/tcp if not explicitly set - like looping over remote_list addresses, this can give a base for "full" discovery.

Let's imagine it's done and working - cross-protocol server selection rules are not defined by RFC, ambiguity arises: should tcp and udp records share prio numbers? If yes, should they share relative weight algo or weighted selection should stay within proto scope? Sure, it can be documented in OpenVPN docs, but one who setup that can have general experience with other DNS SRV records (sip, ldap, etc) and be confused breaking the scope rules from other proto domain (_tcp <> _udp). I thought about this issue before implementation, and come only with manual ordering for each proto on profile - in presence of TCP-over-TCP issues, UDP is usually preferred anyway.
How do you think, can it be threaten as limitation for current stage?

Quoted 11 lines
Quoted 11 lines

OpenVPN will first try to resolve _openvpn._udp.server.example.com SRV records and use resolved targets (see example below) as servers for the subsequent connection attempts. If there's no records or resolve error, OpenVPN will fallback to direct connection using specified server.example.com host and 1194 port. Next connection attempt will be done with next configures connection/remote entry - using _openvpn._tcp.server.example.com records (if any) and server.example.com:443 as last resort.

Quoted 3 lines
Quoted 1 line

DNS zone can look like below (arbitrary prio & weight values):

What/how are prio and weight used?

Algorithm is not arbitrary, it's fixed and described in https://tools.ietf.org/html/rfc2782
Implied administrators who are decided to use DNS SRV are familiar with it, same as nginx (for example) documentation has no copy of HTML RFCs, so RFC is mentioned in reference links.
Briefly, service records are ordered by the priority first from lower to higher, and next within same priority randomly picked with taking into account relative weights of the records. Records with zero weight has special meaning if appeared along with non-zero weighted ones. So, it should be still noted in commit messages and docs, right or?

Quoted 9 lines
Quoted 3 lines

_openvpn._udp.server.example.com IN SRV 10 70 1194 server.example.com _openvpn._udp.server.example.com IN SRV 10 30 1195

server1.example.com

Quoted 5 lines

_openvpn._udp.server.example.com IN SRV 20 0 1196 server2.example.org _openvpn._tcp.server.example.com IN SRV 10 10 443 server.example.com _openvpn._tcp.server.example.com IN SRV 10 200 443
server.example.com

Quoted 4 lines
Quoted 3 lines

Currently only "openvpn" service is supported, usage of per-connection service names is up to syntax decision (either intead of fallback port, or

                                                 ^^^^ typo

Oh, thanks.

Quoted 5 lines
Quoted 3 lines

as remote-discovery argument, etc), almost all the required mechanics is implemented for that.

Almost all? What is missing?

The missing thing is absence of syntax that would allow to specify both port (for fallback) and service name (for discovery) for one remote/connection entry.
From tech point of view, openvpn_getaddrinfo() knows nothing about options, contexts, connection entries, etc, so calling api will need to be changed from just hostname:servname into something different. I've decided not to go this way till profile syntax will be clear.

Quoted 49 lines
Quoted 30 lines

References:
https://tools.ietf.org/html/rfc2782
https://en.wikipedia.org/wiki/SRV_record
https://sourceforge.net/p/openvpn/mailman/message/34364911/
https://forums.openvpn.net/viewtopic.php?f=10&t=13660

---
 configure.ac                        |   2 +-
 doc/man-sections/client-options.rst |  26 ++
 src/openvpn/Makefile.am             |   2 +-
 src/openvpn/buffer.h                |   5 -
 src/openvpn/init.c                  |   9 +-
 src/openvpn/manage.c                |   2 +-
 src/openvpn/openvpn.vcxproj         |   8 +-
 src/openvpn/options.c               |  11 +
 src/openvpn/options.h               |   1 +
 src/openvpn/ps.c                    |   2 +-
 src/openvpn/socket.c                | 530 ++++++++++++++++++++++++++--
 src/openvpn/socket.h                |  15 +
 src/openvpn/syshead.h               |   5 +
 13 files changed, 580 insertions(+), 38 deletions(-)
configure.ac index f8279924..67251bed renamed from configure.ac +0 -0

100644

configure.ac SOCKET_INCLUDES=" · line 480

Quoted 4 lines
configure.ac +0 -1
@@ -477,7 +477,7 @@ SOCKET_INCLUDES="
 "
 
 AC_CHECK_HEADERS(
-	[net/if.h netinet/ip.h resolv.h sys/un.h net/if_utun.h

sys/kern_control.h],

Quoted 14 lines
configure.ac +2 -0
+	[net/if.h netinet/ip.h arpa/nameser.h resolv.h sys/un.h
+net/if_utun.h sys/kern_control.h],
 	,
 	,
 	[[${SOCKET_INCLUDES}]]
doc/man-sections/client-options.rst +0 -0
index ec1e3b11..53e6580e 100644
@@ -299,6 +299,32 @@ configuration.
   specification (4/6 suffix), OpenVPN will try both IPv4 and IPv6
   addresses, in the order getaddrinfo() returns them.
 
Quoted 3 lines

doc/man-sections/client-options.rst configuration. · line 302

Quoted 1 line
doc/man-sections/client-options.rst +1 -0
+--remote-discovery

discovery is a bit ambigious. Maybe rather use --remote-service-discovery?

Much better, thank you.
Also, can be related, if go with optional "[service]" syntax for custom non-openvpn service name, what's about "--remote-service [service]"? Default value will be printed by general ce routines as "remote_service openvpn", for example.

Quoted 7 lines

doc/man-sections/client-options.rst configuration. · line 304

Quoted 3 lines
doc/man-sections/client-options.rst +2 -0
+  Perform rfc2782 remote host discovery using specified ``host`` and
+ ``proto`` values.

Spelling, RFC 2782. But RFC numbers are not user friendly when used alone. I would at least add DNS SRV somewhere in there.

Yes, initially there was DNS SRV, and next I thought it was not clear enough reference (w/o algo details) to service selection algorithm, so was changed into RFC reference as any possible ambiguity avoidance. What if use DNS SRV for naming, brief algorithm description in man and RFC reference for exact details?

Quoted 35 lines
Quoted 14 lines
doc/man-sections/client-options.rst +13 -0
+  OpenVPN will try to resolve `` _openvpn._proto.host`` name via DNS
+ and use returned DNS SRV target and port records as OpenVPN servers
+ addresses in the order specified by Priority and Weight of that
+ records. If one record resolves to multiple IP addresses, OpenVPN
+ will try them in the order that the system getaddrinfo() presents
+ them. If discovery is failed, usual ``host`` and ``port`` connection
+ will be tried as a fallback.
+
+  Example:
+  ::
+
+     remote example.net 1194 udp
+     remote example.net 443 tcp

doc/man-sections/client-options.rst configuration. · line 327

Quoted 10 lines
doc/man-sections/client-options.rst +10 -0
+     remote-discovery
+
+  DNS zone:
+  ::
+
+     _openvpn._udp.example.net IN SRV 10 60 1194 server1.example.net
+     _openvpn._udp.example.net IN SRV 10 40 1194 server2.example.net
+     _openvpn._udp.example.net IN SRV 20  0 1194 server3.example.net
+     _openvpn._tcp.example.net IN SRV 10  0  443 server4.example.net
+

The man page is completely missing how the weight/priority is interpreted by OpenVPN.

doc/man-sections/client-options.rst static const char usage_message[] = · line 124

Quoted 4 lines
doc/man-sections/client-options.rst +1 -0
+++ b/src/openvpn/options.c
@@ -123,6 +123,7 @@ static const char usage_message[] =
     "--remote host [port] : Remote host name or ip address.\n"
     "--remote-random : If multiple --remote options specified, choose one

randomly.\n"

Quoted 5 lines

doc/man-sections/client-options.rst static const char usage_message[] = · line 125

Quoted 1 line
doc/man-sections/client-options.rst +0 -0
     "--remote-random-hostname : Add a random string to remote DNS

name.\n"

doc/man-sections/client-options.rst static const char usage_message[] = · line 126

Quoted 1 line
doc/man-sections/client-options.rst +1 -0
+    "--remote-discovery : Perform rfc2782 remote host discovery.\n"

See other comment.

Yep, should be DNS SRV, see above.

Quoted 14 lines

doc/man-sections/client-options.rst

Quoted 13 lines
doc/man-sections/client-options.rst +0 -0
     else if (streq(p[0], "setenv") && p[1] && !p[3])
     {
         VERIFY_PERMISSION(OPT_P_GENERAL); @@ -6679,6 +6686,10 @@

add_option(struct options *options,

         {
             options->sockflags |= SF_HOST_RANDOMIZE;
         }
doc/man-sections/client-options.rst +4 -0
+        if (streq(p[1], "REMOTE_DISCOVERY"))
+        {
+            options->ce.remote_discovery = true;
+        }
Quoted 2 lines

I don't think we should add this. We already have ignore-unknown-options and setenv opt to write backwards compatible configs.

Ok, to be removed.

Quoted 14 lines

doc/man-sections/client-options.rst do_preresolve(struct context *c) · line 429

Quoted 10 lines
doc/man-sections/client-options.rst +1 -1
@@ -416,7 +424,7 @@ do_preresolve(struct context *c)
         if (ce->bind_local)
         {
             flags |= GETADDR_PASSIVE;
-            flags &= ~GETADDR_RANDOMIZE;
+            flags &= ~(GETADDR_RANDOMIZE|GETADDR_DISCOVERY);
             status = do_preresolve_host(c, ce->local, ce->local_port,
 ce->af, flags);

Hm does this do a DNS SRV discovery for the local bind address? That seems odd.

Should not, it's for inverse thing - if service discovery was enabled for remote host (above), it must be disabled for local address resolve just to avoid unnecessary routines.

Quoted 17 lines
Quoted 14 lines
doc/man-sections/client-options.rst +0 -0
             if (status != 0)
             {
doc/man-sections/client-options.rst +8 -0
@@ -432,6 +440,363 @@ err:
     throw_signal_soft(SIGHUP, "Preresolving failed");  }
 
+/* Struct to hold resolved srv records */ struct srvinfo {
+    const char *hostname;
+    const char *servname;
+    unsigned short prio;
+    unsigned short weight;
+    int order;
+    struct srvinfo *next;
+};
Quoted 5 lines

doc/man-sections/client-options.rst err: · line 451

Quoted 2 lines
doc/man-sections/client-options.rst +2 -0
+static int
+rfc2782_cmp(const void *a, const void *b)

It does compare srvinfo structs and should be named accordingly. Also please add a doxygen comment, say how it compares them.

Naming was selected for the same ambiguity avoidance, like glibc does:
https://sourceware.org/git/?p=glibc.git;f=sysdeps/posix/getaddrinfo.c;hb=8b222fa38700422b4da6731806835f0bbf40920d#l1392
https://sourceware.org/git/?p=glibc.git;f=sysdeps/posix/getaddrinfo.c;hb=8b222fa38700422b4da6731806835f0bbf40920d#l2279

Quoted 49 lines
Quoted 14 lines
doc/man-sections/client-options.rst +13 -0
+{
+    const struct srvinfo *ae = *(struct srvinfo **)a;
+    const struct srvinfo *be = *(struct srvinfo **)b;
+
+    /* lowest-numbered priority first */
+    if (ae->prio != be->prio)
+    {
+        return ae->prio < be->prio ? -1 : 1;
+    }
+
+    /* zero-weighted first */
+    if ((ae->weight == 0 && be->weight)
+        || (ae->weight && be->weight == 0))

doc/man-sections/client-options.rst err: · line 474

Quoted 10 lines
doc/man-sections/client-options.rst +8 -0
+    {
+        return ae->weight < be->weight ? -1 : 1;
+    }
+
+    /* keep received order */
+    return ae->order < be->order ? -1 : 1; }
 
 
+static struct srvinfo *
+rfc2782_sort(struct srvinfo *list, int count) {

Doxygen, sorting by what etc.

Quoted 22 lines
doc/man-sections/client-options.rst +22 -0
+    struct srvinfo head, *tail, **sorted;
+    struct gc_arena gc = gc_new();
+
+    ASSERT(list);
+    ASSERT(count);
+
+    head.next = NULL;
+    tail = &head;
+
+    /* sort records by priority and zero weight */
+    ALLOC_ARRAY_CLEAR_GC(sorted, struct srvinfo *, count, &gc);
+    for (struct srvinfo *e = list; e; e = e->next)
+    {
+        ASSERT(e->order < count);
+        sorted[e->order] = e;
+    }
+    qsort(sorted, count, sizeof(sorted[0]), rfc2782_cmp);
+
+    /* apply weighted selection mechanism */
+    for (int i = 0; i < count;)
+    {
+        struct srvinfo unordered;
Quoted 8 lines

doc/man-sections/client-options.rst err: · line 502

Quoted 6 lines
doc/man-sections/client-options.rst +6 -0
+
+        /* compute the sum of the weights of records of the same
+         * priority and put them in the unordered list */
+        unordered.prio = sorted[i]->prio;
+        unordered.weight = 0;
+        unordered.next = NULL;

unordered.weight, unordered.prio seems just used as variables in this algorithm.

Yes, along with next field.
Unordered list has one prio value for all its items and total weight of all of them, used for server selection algo.

Quoted 14 lines

doc/man-sections/client-options.rst err: · line 503

Quoted 2 lines
doc/man-sections/client-options.rst +1 -0
+        for (struct srvinfo *prev = &unordered; i < count && sorted[i]->prio ==

unordered.prio; i++)

Quoted 11 lines
doc/man-sections/client-options.rst +11 -0
+        {
+            struct srvinfo *e = sorted[i];
+
+            unordered.weight += e->weight;
+
+            /* add entry to the tail of unordered list */
+            e->next = NULL;
+            prev->next = e, prev = e;
+        }
+
+        /* process the unordered list */
Quoted 8 lines

doc/man-sections/client-options.rst err: · line 520

Quoted 6 lines
doc/man-sections/client-options.rst +6 -0
+        while (unordered.next)
+        {
+            /* choose a uniform random number between 0 and the sum
+             * computed (inclusive) */
+            int weight = unordered.weight ? get_random() %
+ unordered.weight : 0;

This line is hard to parse.

Thanks, bug is here "inclusive" statement is missed, should be instead:
int weight = get_random() % (unordered.weight + 1);

Quoted 175 lines

doc/man-sections/client-options.rst err: · line 525

Quoted 6 lines
doc/man-sections/client-options.rst +5 -0
+
+            /* select the entries whose running sum value is the first
+             * in the selected order which is greater than or equal
+             * to the random number selected */
+            for (struct srvinfo *e = unordered.next, *prev = &unordered; e; prev =

e, e = e->next)

Quoted 10 lines
doc/man-sections/client-options.rst +10 -0
+            {
+                /* selected entry is the next one to be contacted */
+                if (e->weight >= weight)
+                {
+                    unordered.weight -= e->weight;
+
+                    /* move entry to the ordered list */
+                    prev->next = e->next;
+                    e->next = NULL;
+                    tail->next = e, tail = e;

doc/man-sections/client-options.rst err: · line 545

Quoted 10 lines
doc/man-sections/client-options.rst +10 -0
+
+                    /* in the presence of records containing weights greater
+                     * than 0, records with weight 0 should have a very small
+                     * chance of being selected, so skip them all after the
+                     * last weighted */
+                    if (unordered.weight == 0 && e->weight)
+                    {
+                        unordered.next = NULL;
+                    }
+                    break;

This behaviour needs to be documented in the man page.

Quoted 23 lines
doc/man-sections/client-options.rst +23 -0
+/*
+ * Translated hostname, service and protocol into struct srvinfo.
+ * Multiple records are ordered per rfc2782.
+ */
+static int
+openvpn_getsrvinfo(unsigned int flags,
+                   const char *hostname,
+                   const char *servname,
+                   struct srvinfo **res,
+                   struct gc_arena *gc) {
+    struct srvinfo *list = NULL;
+#ifdef _WIN32
+    char qname[DNS_MAX_NAME_BUFFER_LENGTH];
+#else
+    char qname[NS_MAXDNAME];
+#endif
+    int status;
+    int count = 0;
+
+    ASSERT(res);
+
+    ASSERT(hostname && servname);
+    ASSERT(!(flags & GETADDR_HOST_ORDER));

doc/man-sections/client-options.rst err: · line 578

Quoted 10 lines
doc/man-sections/client-options.rst +10 -0
+
+    if (!openvpn_snprintf(qname, sizeof(qname), "_%s._%s.%s", servname,
+                          (flags & GETADDR_DATAGRAM) ? "udp" : "tcp", hostname))
+    {
+        return EAI_MEMORY;
+    }
+
+#ifdef _WIN32
+    PDNS_RECORD pDnsRecord;
+    DNS_STATUS DnsStatus = DnsQuery(qname, DNS_TYPE_SRV,

DNS_QUERY_STANDARD, NULL, &pDnsRecord, NULL);

Quoted 46 lines
doc/man-sections/client-options.rst +45 -0
+    dmsg(D_SOCKET_DEBUG, "DNSQUERY type=%d dname=%s result=%d",
+         DNS_TYPE_SRV, qname, DnsStatus);
+    switch (DnsStatus)
+    {
+        case ERROR_SUCCESS:
+            break;
+
+        case DNS_ERROR_RCODE_NAME_ERROR:
+            return EAI_NONAME; /* HOST_NOT_FOUND */
+
+        case DNS_INFO_NO_RECORDS:
+            return EAI_NODATA; /* NO_DATA */
+
+        case DNS_ERROR_NO_DNS_SERVERS:
+        case DNS_ERROR_RCODE_FORMAT_ERROR:
+        case DNS_ERROR_RCODE_NOT_IMPLEMENTED:
+        case DNS_ERROR_RCODE_REFUSED:
+            return EAI_FAIL; /* NO_RECOVERY */
+
+        case ERROR_TIMEOUT:
+        case DNS_ERROR_RCODE_SERVER_FAILURE:
+        case DNS_ERROR_TRY_AGAIN_LATER:
+            return EAI_AGAIN; /* TRY_AGAIN */
+
+        default:
+            return EAI_NODATA;
+    }
+
+    for (PDNS_RECORD rr = pDnsRecord; rr; rr = rr->pNext)
+    {
+        if (rr->wType == DNS_TYPE_SRV)
+        {
+            PDNS_SRV_DATA rdata = &rr->Data.Srv;
+
+            if (rr->wDataLength >= sizeof(DNS_SRV_DATA) && *rdata-
 pNameTarget)
+            {
+                char port[sizeof("65535")];
+                openvpn_snprintf(port, sizeof(port), "%u",
+ rdata->wPort);
+
+                struct srvinfo *e;
+                ALLOC_OBJ_CLEAR_GC(e, struct srvinfo, gc);
+                e->hostname = string_alloc(rdata->pNameTarget, gc);
+                e->servname = string_alloc(port, gc);
+                e->prio = rdata->wPriority;

doc/man-sections/client-options.rst err: · line 634

Quoted 10 lines
doc/man-sections/client-options.rst +10 -0
+                e->weight = rdata->wWeight;
+                e->order = count++;
+                e->next = list, list = e;
+            }
+        }
+    }
+    DnsRecordListFree(pDnsRecord, DnsFreeParsedMessageFields); #else
+    unsigned char answer[NS_MAXMSG];
+    int n = res_query(qname, ns_c_in, ns_t_srv, answer, NS_MAXMSG);
+    dmsg(D_SOCKET_DEBUG, "RES_QUERY class=%d type=%d dname=%s

result=%d",

Quoted 52 lines
doc/man-sections/client-options.rst +52 -0
+         ns_c_in, ns_t_srv, qname, n);
+    if (n < 0)
+    {
+        switch (h_errno)
+        {
+            case HOST_NOT_FOUND:
+                return EAI_NONAME;
+
+            case NO_ADDRESS:
+#if NO_ADDRESS != NO_DATA
+            case NO_DATA:
+#endif
+                return EAI_NODATA;
+
+            case NO_RECOVERY:
+                return EAI_FAIL;
+
+            case TRY_AGAIN:
+                return EAI_AGAIN;
+        }
+        return EAI_SYSTEM;
+    }
+
+    ns_msg msg;
+    if (ns_initparse(answer, n, &msg) < 0)
+    {
+        return EAI_FAIL;
+    }
+
+    for (int i = 0; i < ns_msg_count(msg, ns_s_an); i++)
+    {
+        ns_rr rr;
+
+        if (ns_parserr(&msg, ns_s_an, i, &rr) == 0 && ns_rr_type(rr) == ns_t_srv)
+        {
+            const unsigned char *rdata = ns_rr_rdata(rr);
+
+            if (ns_rr_rdlen(rr) > 6
+                && dn_expand(ns_msg_base(msg), ns_msg_end(msg),
+                             rdata + 6, qname, sizeof(qname)) > 0 && *qname)
+            {
+                char port[sizeof("65535")];
+                openvpn_snprintf(port, sizeof(port), "%u",
+ ns_get16(rdata + 4));
+
+                struct srvinfo *e;
+                ALLOC_OBJ_CLEAR_GC(e, struct srvinfo, gc);
+                e->hostname = string_alloc(qname, gc);
+                e->servname = string_alloc(port, gc);
+                e->prio = ns_get16(rdata);
+                e->weight = ns_get16(rdata + 2);
+                e->order = count++;
Quoted 8 lines

doc/man-sections/client-options.rst err: · line 691

Quoted 5 lines
doc/man-sections/client-options.rst +5 -0
+                e->next = list, list = e;
+            }
+        }
+    }
+#endif

Can we have the platform specifc part in an extra function? This ifdef makes the function hard to read.

Yes, functions that will return unordered lists.
About placement, should they better go into compat/win32/etc or it's ok to have it near around in same socket.c?

Quoted 11 lines

doc/man-sections/client-options.rst err: · line 695

Quoted 7 lines
doc/man-sections/client-options.rst +4 -0
+/*
+ * Struct to hold chainable copy of struct addinfo.
+ * Must be binary compatible (except size) with original.
+ */

What is binary compatible expect size? The first member needs to be a addrinfo since this might be cast to addrinfo? And if yes, why are we doing a hack like that?

Yes, first member have to be addrinfo exactly for casting. We're doing it for the reason described right down below.

Quoted 14 lines
Quoted 14 lines
doc/man-sections/client-options.rst +13 -0
+struct addrinfo_chained
+{
+    struct addrinfo ai;
+    struct openvpn_sockaddr addr;
+    char canonname[0];
+};
+
+/*
+ * System getaddrinfo() returns one or more addrinfo structures.
+ * openvpn_getaddinfo() chains them into one in case DNS SRV
+ * resolve is performed, but unfortunately it can't be safely done
+ * (freed) with musl libc, refer
+ *
Quoted 8 lines

doc/man-sections/client-options.rst err: · line 713

Quoted 5 lines
doc/man-sections/client-options.rst +5 -0
+https://git.musl-libc.org/cgit/musl/commit/?id=d1395c43c019aec6b855cf
+3c656bf47c8a719e7f
+ * Use own copy of each addrinfo to workaround this and possibly
+ * other getaddrinfo()/freeaddrinfo() implementations.
+ */

This looks like a comment describing the function but it does not desribe the function.

The hack of cloning each addinfo struct is required to workaround musl's issue - freeaddinfo() can’t (and not going to) free chained addinfo lists. I'll try to come with better wording, along with comment for struct above, if related code parts will survive the review comments down below :)

Quoted 4 lines

Also wouldn't it be easier to just add a gc special free function for your srvinfo struct that also free the associated addrinfo record instead of doing this strange dance?

Not sure if I got the idea right.
Life cycle of srvinfo and addrinfo lists there:
openvpn_getaddrinfo() creates temporary gc
openvpn_getaddrinfo() calls openvpn_getsrvrinfo() passing temporary gc openvpn_getsrvrinfo() allocates srvinfo list using that gc openvpn_getsrvrinfo() returns srvinfo list
openvpn_getaddrinfo() traverses srvinfo list and allocates addrinfo list for each srvinfo openvpn_getaddrinfo() frees temporary gc - srvinfo list is freed now openvpn_getaddrinfo() returns one chained addrinfo list
So, addrinfo list can't be freed at the time srvinfo list is freed.
If only the final result was srvinfo list, not addrinfo - they both should not be freed. What I'm missing?

Quoted 142 lines
Quoted 8 lines
doc/man-sections/client-options.rst +7 -0
+int
+getaddrinfo_chained(const char *node, const char *service,
+                    const struct addrinfo *hints,
+                    struct addrinfo **res) {
+    int status = getaddrinfo(node, service, hints, res);
+    if (status == 0 && res && *res)
+    {

doc/man-sections/client-options.rst err: · line 730

Quoted 10 lines
doc/man-sections/client-options.rst +10 -0
+        struct addrinfo_chained head, *tail = &head;
+
+        head.ai.ai_next = NULL;
+        for (struct addrinfo *ai = *res; ai; ai = ai->ai_next)
+        {
+            socklen_t addrlen = ai->ai_addr ? ai->ai_addrlen : 0;
+            size_t namelen = ai->ai_canonname ?
+ strlen(ai->ai_canonname) + 1 : 0;
+
+            /* allocate self-contained struct with enough space for optional data

*/

doc/man-sections/client-options.rst err: · line 731

Quoted 1 line
doc/man-sections/client-options.rst +1 -0
+            struct addrinfo_chained *ac = calloc(1, sizeof(*ac) + addrlen +

namelen);

Quoted 59 lines
doc/man-sections/client-options.rst +44 -1
+            if (!ac)
+            {
+                openvpn_freeaddrinfo(head.ai.ai_next);
+                head.ai.ai_next = NULL;
+                status = EAI_MEMORY;
+                break;
+            }
+
+            /* deep clone the structure and its members */
+            memcpy(&ac->ai, ai, sizeof(ac->ai));
+            if (ai->ai_addr)
+            {
+                memcpy(&ac->addr.addr.sa, ai->ai_addr, addrlen);
+                ac->ai.ai_addr = &ac->addr.addr.sa;
+            }
+            if (ai->ai_canonname)
+            {
+                memcpy(ac->canonname, ai->ai_canonname, namelen);
+                ac->ai.ai_canonname = ac->canonname;
+            }
+
+            /* add clone to the tail */
+            ac->ai.ai_next = NULL;
+            tail->ai.ai_next = &ac->ai, tail = ac;
+        }
+
+        /* original list is cloned, can be freed now */
+        freeaddrinfo(*res);
+        *res = head.ai.ai_next;
+    }
+    return status;
+}
+
+void
+openvpn_freeaddrinfo(struct addrinfo *res) {
+    while (res)
+    {
+        struct addrinfo_chained *ac = (struct addrinfo_chained *)res;
+        res = res->ai_next;
+        free(ac);
+    }
+}
+
 /*
  * Translate IPv4/IPv6 addr or hostname into struct addrinfo
  * If resolve error, try again for resolve_retry_seconds seconds.
@@ -497,7 +862,7 @@ openvpn_getaddrinfo(unsigned int flags,
         hints.ai_socktype = SOCK_STREAM;
     }
 
-    status = getaddrinfo(hostname, servname, &hints, res);
+    status = getaddrinfo_chained(hostname, servname, &hints, res);
 
     if (status != 0) /* parse as numeric address failed? */
     {
@@ -555,16 +920,17 @@ openvpn_getaddrinfo(unsigned int flags,
         /*
          * Resolve hostname
          */

doc/man-sections/client-options.rst openvpn_getaddrinfo(unsigned int flags, · line 565

Quoted 10 lines
doc/man-sections/client-options.rst +2 -6
-        while (true)
-        {
+        struct srvinfo *targets = NULL;
 #ifndef _WIN32
-            res_init();
+        res_init();
 #endif
-            /* try hostname lookup */
-            hints.ai_flags &= ~AI_NUMERICHOST;
-            dmsg(D_SOCKET_DEBUG, "GETADDRINFO flags=0x%04x ai_family=%d

ai_socktype=%d",

Quoted 37 lines
doc/man-sections/client-options.rst +10 -7
-                 flags, hints.ai_family, hints.ai_socktype);
-            status = getaddrinfo(hostname, servname, &hints, res);
+
+        /* try srv lookup */
+        while (flags & GETADDR_DISCOVERY)
+        {
+            dmsg(D_SOCKET_DEBUG, "GETSRVINFO flags=0x%04x hostname=%s",
+                 flags, np(hostname));
+            status = openvpn_getsrvinfo(flags, hostname,
+ OPENVPN_SERVICE, &targets, &gc);
 
             if (signal_received)
             {
@@ -581,9 +947,6 @@ openvpn_getaddrinfo(unsigned int flags,
                         /* turn success into failure (interrupted syscall) */
                         if (0 == status)
                         {
-                            ASSERT(res);
-                            freeaddrinfo(*res);
-                            *res = NULL;
                             status = EAI_AGAIN; /* = temporary failure */
                             errno = EINTR;
                         }
@@ -592,8 +955,8 @@ openvpn_getaddrinfo(unsigned int flags,
                 }
             }
 
-            /* success? */
-            if (0 == status)
+            /* success and fails are expected, fallback to usual resolve */
+            if (EAI_AGAIN != status)
             {
                 break;
             }
@@ -614,10 +977,127 @@ openvpn_getaddrinfo(unsigned int flags,
 
             if (--resolve_retries <= 0)

doc/man-sections/client-options.rst openvpn_getaddrinfo(unsigned int flags, · line 987

Quoted 10 lines
doc/man-sections/client-options.rst +5 -1
             {
-                goto done;
+                /* can't retry, fallback to usual resolve */
+                break;
             }
 
             management_sleep(fail_wait_interval);
+#ifndef _WIN32
+            res_init();
+#endif

Add a comment why we do that res_init again. It is not obvious for me.

Quoted 4 lines

This is not full review. But I feel this emulating addrinfo for remote is a bit hacky. I feel refactoring the current_remote code to be a bit more agnostic and then always use the srvinfo meta struct and iterates through that one to set addrinfo or something in that is a much cleaner solution.

Right, this will allow to drop reallocation of addrinfo structs and hacky chaining of them. openvpn_getaddrinfo() will need to return servinfo list, with subsequent changes in all the callers.

Quoted 4 lines

(This might be already in patch) Also if srv records are returned and verb level is high enough, something like 3 or 4, we should log something like whole result of the service discovery.

Hostname/service logging is done at debug level so far to be consistent with existing debug messages (for getaddrinfo), I'll add normal logging as intended. And I feel need to add exact hostname:servname resolve logging even for single host, it's absent currently.

Quoted 2 lines

Arne

#3 lore raw

PATCH v2 1/1 Add DNS SRV host discovery support

In reply to Arne Schwabe

DNS SRV host discovery allows to have multiple OpenVPN servers for a single domain w/o explicit profile enumeration, to move services from host to host with little fuss, and to designate hosts as primary servers for a service and others as backups.
Feature has been asked several times already, useful in case of huge ammounts of client & servers deployed.

Patch introduces "--remote-service-discovery [service]" option, which enables discovery and optionally allows to change "openvpn" default service to user specified.
Client will try to resolve ``_service._proto.host`` name via DNS and use returned DNS SRV records as OpenVPN remote server list ordered by server selection mechanism defined in RFC 2782:

A client MUST attempt to contact the target host with the lowest-numbered priority field value it can reach, target hosts with the same priority SHOULD be tried in an order defined by the weight field.
The weight field specifies a relative weight for entries with the same priority. Larger weights SHOULD be given a proportionately higher probability of being selected.
Domain administrators SHOULD use Weight 0 when there isn't any server selection to do. In the presence of records containing weights greater than 0, records with weight 0 SHOULD have a very small chance of being selected.

Example: instead of multiple remotes on order in profile:
remote s1.example.com 1194 udp
remote s2.example.com 1194 udp
remote s3.example.com 1194 udp
remote s4.example.com 1194 udp
remote example.com 1194 udp

now it's possible to specify one remote with discovery:
remote example.net 1194 udp remote-service-discovery

and addconfigure following DNS SRV records, for examle:

     _openvpn._udp.example.net IN SRV 10 60 1194 s1.example.net
     _openvpn._udp.example.net IN SRV 10 40 1194 s2.example.net
     _openvpn._udp.example.net IN SRV 10  0 1194 s3.example.net
     _openvpn._udp.example.net IN SRV 20  0 1194 s4.example.net

Client will try to resolve "_openvpn._udp.example.net".
Next, "s1.example.net", "s2.example.net" and "s3.example.net" will be ordered before "s4.example.net" as their priority 10 is smaller than 20. Next, "s1.example.net", "s2.example.net" and "s3.example.net" will be randomly ordered with weight probability - first will be either "s1.example.net" with probability 60% or "s2.example.net" with 40% or "s3.example.net" with almost zero probability.
Next, if "s1.example.net" is picked as first, second server will be either "s2.example.net" with almost 100% probability or "s3.example.net" instead. Next, if "s2.example.net" is picked as second, "s3.example.net" will be skipped due zero weight.
"s4.example.net" will be the last one as the only server with priority 20. Finally, servers will be resolved and accessed in this order.

If _openvpn._udp.example.net" can't be resolved or no valid records are returned or they can't be resolved, client will try to access "example.net" host at port 1194 as if no discovery is enabled.

v2: use DNS SRV for feature naming
change option name to --remote-service-discovery
add --remote-service-discovery [service] optional parameter support rewrite man, add algo/prio/weight description and examples
fix random weight selection was not implicit
remove pulled REMOTE_DISCOVERY support, not needed
split windows/unix-specific parts into extra functions
rename functions into servinfo scope, add doxygen comments when appropriate remove addrinfo hack, use servinfo containers of addrinfo list instead better proxy support (tcp mode not supported so far)
log discovery attempts and results, if enabled

---
 configure.ac                        |   2 +-
 doc/man-sections/client-options.rst |  63 +++
 src/openvpn/Makefile.am             |   2 +-
 src/openvpn/buffer.h                |   5 -
 src/openvpn/errlevel.h              |   1 +
 src/openvpn/init.c                  |  19 +-
 src/openvpn/openvpn.vcxproj         |   8 +-
 src/openvpn/options.c               |   8 +
 src/openvpn/options.h               |   1 +
 src/openvpn/socket.c                | 773 +++++++++++++++++++++++++---
 src/openvpn/socket.h                |  93 +++-
 src/openvpn/syshead.h               |   5 +
 src/openvpn/tun.c                   |  19 +-
 src/openvpn/tun.h                   |   3 +-
 14 files changed, 904 insertions(+), 98 deletions(-)
configure.ac +1 -1
index f8279924..67251bed 100644
@@ -477,7 +477,7 @@ SOCKET_INCLUDES="
 "
 
 AC_CHECK_HEADERS(
-	[net/if.h netinet/ip.h resolv.h sys/un.h net/if_utun.h sys/kern_control.h],
+	[net/if.h netinet/ip.h arpa/nameser.h resolv.h sys/un.h net/if_utun.h sys/kern_control.h],
 	,
 	,
 	[[${SOCKET_INCLUDES}]]
doc/man-sections/client-options.rst +63 -0

69 lines not on this page · raw message

src/openvpn/Makefile.am +1 -1
index 37b002c6..f7632047 100644
@@ -143,5 +143,5 @@ openvpn_LDADD = \
 	$(OPTIONAL_INOTIFY_LIBS)
 if WIN32
 openvpn_SOURCES += openvpn_win32_resources.rc block_dns.c block_dns.h ring_buffer.h
-openvpn_LDADD += -lgdi32 -lws2_32 -lwininet -lcrypt32 -liphlpapi -lwinmm -lfwpuclnt -lrpcrt4 -lncrypt -lsetupapi
+openvpn_LDADD += -lgdi32 -lws2_32 -lwininet -lcrypt32 -liphlpapi -lwinmm -lfwpuclnt -lrpcrt4 -lncrypt -lsetupapi -ldnsapi
 endif
src/openvpn/buffer.h +0 -5
index 1722ffd5..ae0cf0eb 100644
@@ -198,11 +198,6 @@ bool buf_init_debug(struct buffer *buf, int offset, const char *file, int line);
 
 
 /* inline functions */
-inline static void
-gc_freeaddrinfo_callback(void *addr)
-{
-    freeaddrinfo((struct addrinfo *) addr);
-}
 
 /** Return an empty struct buffer */
 static inline struct buffer
src/openvpn/errlevel.h +1 -0
index e448fc37..c77dc76c 100644
@@ -91,6 +91,7 @@
 #define D_OSBUF              LOGLEV(3, 43, 0)        /* show socket/tun/tap buffer sizes */
 #define D_PS_PROXY           LOGLEV(3, 44, 0)        /* messages related to --port-share option */
 #define D_PF_INFO            LOGLEV(3, 45, 0)        /* packet filter informational messages */
+#define D_RESOLVE_INFO       LOGLEV(3, 46, 0)        /* show hostname resolve info */
 
 #define D_SHOW_PARMS         LOGLEV(4, 50, 0)        /* show all parameters on program initiation */
 #define D_SHOW_OCC           LOGLEV(4, 51, 0)        /* show options compatibility string */
src/openvpn/init.c +17 -2
index a785934a..dafb999e 100644
@@ -365,6 +365,7 @@ management_callback_remote_cmd(void *arg, const char **p)
 
                 ce->remote = rhs->host;
                 ce->remote_port = rhs->port;
+                ce->remote_service = NULL; /* disable discovery */
                 flags = CE_MAN_QUERY_REMOTE_MOD;
                 ret = true;
             }
@@ -458,9 +459,10 @@ clear_remote_addrlist(struct link_socket_addr *lsa, bool free)
 {
     if (lsa->remote_list && free)
     {
-        freeaddrinfo(lsa->remote_list);
+        freeservinfo(lsa->remote_list);
     }
     lsa->remote_list = NULL;
+    lsa->current_service = NULL;
     lsa->current_remote = NULL;
 }
 
@@ -492,6 +494,14 @@ next_connection_entry(struct context *c)
                 c->c1.link_socket_addr.current_remote =
                     c->c1.link_socket_addr.current_remote->ai_next;
             }
+            else if (c->c1.link_socket_addr.current_service
+                && c->c1.link_socket_addr.current_service->next)
+            {
+                c->c1.link_socket_addr.current_service =
+                    c->c1.link_socket_addr.current_service->next;
+                c->c1.link_socket_addr.current_remote =
+                    c->c1.link_socket_addr.current_service->ai;
+            }
             else
             {
                 /* FIXME (schwabe) fix the persist-remote-ip option for real,
@@ -502,12 +512,15 @@ next_connection_entry(struct context *c)
                 {
                     /* close_instance should have cleared the addrinfo objects */
                     ASSERT(c->c1.link_socket_addr.current_remote == NULL);
+                    ASSERT(c->c1.link_socket_addr.current_service == NULL);
                     ASSERT(c->c1.link_socket_addr.remote_list == NULL);
                 }
                 else
                 {
-                    c->c1.link_socket_addr.current_remote =
+                    c->c1.link_socket_addr.current_service =
                         c->c1.link_socket_addr.remote_list;
+                    c->c1.link_socket_addr.current_remote =
+                        c->c1.link_socket_addr.current_service->ai;
                 }
 
                 /*
@@ -3417,6 +3430,7 @@ do_init_socket_1(struct context *c, const int mode)
                             c->options.ce.local_port,
                             c->options.ce.remote,
                             c->options.ce.remote_port,
+                            c->options.ce.remote_service,
                             c->c1.dns_cache,
                             c->options.ce.proto,
                             c->options.ce.af,
@@ -3636,6 +3650,7 @@ do_close_link_socket(struct context *c)
                ||
                ( c->sig->source != SIG_SOURCE_HARD
                  && ((c->c1.link_socket_addr.current_remote && c->c1.link_socket_addr.current_remote->ai_next)
+                     || (c->c1.link_socket_addr.current_service && c->c1.link_socket_addr.current_service->next)
                      || c->options.no_advance))
                )))
     {
src/openvpn/openvpn.vcxproj +4 -4
index 5367979d..996c0160 100644
@@ -92,7 +92,7 @@
     </ClCompile>
     <ResourceCompile />
     <Link>
-      <AdditionalDependencies>legacy_stdio_definitions.lib;Ncrypt.lib;libssl.lib;libcrypto.lib;lzo2.lib;pkcs11-helper.dll.lib;gdi32.lib;ws2_32.lib;wininet.lib;crypt32.lib;iphlpapi.lib;winmm.lib;Fwpuclnt.lib;Rpcrt4.lib;%(AdditionalDependencies)</AdditionalDependencies>
+      <AdditionalDependencies>legacy_stdio_definitions.lib;Ncrypt.lib;libssl.lib;libcrypto.lib;lzo2.lib;pkcs11-helper.dll.lib;gdi32.lib;ws2_32.lib;wininet.lib;crypt32.lib;iphlpapi.lib;winmm.lib;Fwpuclnt.lib;Rpcrt4.lib;dnsapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
       <AdditionalLibraryDirectories>$(OPENSSL_HOME)/lib;$(LZO_HOME)/lib;$(PKCS11H_HOME)/lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
       <SubSystem>Console</SubSystem>
     </Link>
@@ -107,7 +107,7 @@
     </ClCompile>
     <ResourceCompile />
     <Link>
-      <AdditionalDependencies>legacy_stdio_definitions.lib;Ncrypt.lib;libssl.lib;libcrypto.lib;lzo2.lib;pkcs11-helper.dll.lib;gdi32.lib;ws2_32.lib;wininet.lib;crypt32.lib;iphlpapi.lib;winmm.lib;Fwpuclnt.lib;Rpcrt4.lib;setupapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
+      <AdditionalDependencies>legacy_stdio_definitions.lib;Ncrypt.lib;libssl.lib;libcrypto.lib;lzo2.lib;pkcs11-helper.dll.lib;gdi32.lib;ws2_32.lib;wininet.lib;crypt32.lib;iphlpapi.lib;winmm.lib;Fwpuclnt.lib;Rpcrt4.lib;setupapi.lib;dnsapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
       <AdditionalLibraryDirectories>$(OPENSSL_HOME)/lib;$(LZO_HOME)/lib;$(PKCS11H_HOME)/lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
       <SubSystem>Console</SubSystem>
     </Link>
@@ -122,7 +122,7 @@
     </ClCompile>
     <ResourceCompile />
     <Link>
-      <AdditionalDependencies>legacy_stdio_definitions.lib;Ncrypt.lib;libssl.lib;libcrypto.lib;lzo2.lib;pkcs11-helper.dll.lib;gdi32.lib;ws2_32.lib;wininet.lib;crypt32.lib;iphlpapi.lib;winmm.lib;Fwpuclnt.lib;Rpcrt4.lib;%(AdditionalDependencies)</AdditionalDependencies>
+      <AdditionalDependencies>legacy_stdio_definitions.lib;Ncrypt.lib;libssl.lib;libcrypto.lib;lzo2.lib;pkcs11-helper.dll.lib;gdi32.lib;ws2_32.lib;wininet.lib;crypt32.lib;iphlpapi.lib;winmm.lib;Fwpuclnt.lib;Rpcrt4.lib;dnsapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
       <AdditionalLibraryDirectories>$(OPENSSL_HOME)/lib;$(LZO_HOME)/lib;$(PKCS11H_HOME)/lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
       <SubSystem>Console</SubSystem>
     </Link>
@@ -137,7 +137,7 @@
     </ClCompile>
     <ResourceCompile />
     <Link>
-      <AdditionalDependencies>legacy_stdio_definitions.lib;Ncrypt.lib;libssl.lib;libcrypto.lib;lzo2.lib;pkcs11-helper.dll.lib;gdi32.lib;ws2_32.lib;wininet.lib;crypt32.lib;iphlpapi.lib;winmm.lib;Fwpuclnt.lib;Rpcrt4.lib;setupapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
+      <AdditionalDependencies>legacy_stdio_definitions.lib;Ncrypt.lib;libssl.lib;libcrypto.lib;lzo2.lib;pkcs11-helper.dll.lib;gdi32.lib;ws2_32.lib;wininet.lib;crypt32.lib;iphlpapi.lib;winmm.lib;Fwpuclnt.lib;Rpcrt4.lib;setupapi.lib;dnsapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
       <AdditionalLibraryDirectories>$(OPENSSL_HOME)/lib;$(LZO_HOME)/lib;$(PKCS11H_HOME)/lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
       <SubSystem>Console</SubSystem>
     </Link>
src/openvpn/options.c +8 -0
index 8bf82c57..bd893072 100644
@@ -123,6 +123,7 @@ static const char usage_message[] =
     "--remote host [port] : Remote host name or ip address.\n"
     "--remote-random : If multiple --remote options specified, choose one randomly.\n"
     "--remote-random-hostname : Add a random string to remote DNS name.\n"
+    "--remote-service-discovery [service]: Perform DNS SRV remote host discovery.\n"
     "--mode m        : Major mode, m = 'p2p' (default, point-to-point) or 'server'.\n"
     "--proto p       : Use protocol p for communicating with peer.\n"
     "                  p = udp (default), tcp-server, or tcp-client\n"
@@ -809,6 +810,7 @@ init_options(struct options *o, const bool init_gc)
     o->ce.connect_timeout = 120;
     o->connect_retry_max = 0;
     o->ce.local_port = o->ce.remote_port = OPENVPN_PORT;
+    o->ce.remote_service = NULL; /* OPENVPN_SERVICE */
     o->verbosity = 1;
     o->status_file_update_freq = 60;
     o->status_file_version = 1;
@@ -1446,6 +1448,7 @@ show_connection_entry(const struct connection_entry *o)
     SHOW_STR(local_port);
     SHOW_STR(remote);
     SHOW_STR(remote_port);
+    SHOW_STR(remote_service);
     SHOW_BOOL(remote_float);
     SHOW_BOOL(bind_defined);
     SHOW_BOOL(bind_local);
@@ -6672,6 +6675,11 @@ add_option(struct options *options,
         VERIFY_PERMISSION(OPT_P_GENERAL);
         options->sockflags |= SF_HOST_RANDOMIZE;
     }
+    else if (streq(p[0], "remote-service-discovery") && !p[2])
+    {
+        VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_CONNECTION);
+        options->ce.remote_service = p[1] ? p[1] : OPENVPN_SERVICE;
+    }
     else if (streq(p[0], "setenv") && p[1] && !p[3])
     {
         VERIFY_PERMISSION(OPT_P_GENERAL);
src/openvpn/options.h +1 -0
index 877e9396..b6504cd9 100644
@@ -92,6 +92,7 @@ struct connection_entry
     const char *remote_port;
     const char *local;
     const char *remote;
+    const char *remote_service;
     bool remote_float;
     bool bind_defined;
     bool bind_ipv6_only;
src/openvpn/socket.c +702 -71

1,051 lines not on this page · raw message

src/openvpn/socket.h +88 -5

177 lines not on this page · raw message

src/openvpn/syshead.h +5 -0
index 8342eae0..e5852753 100644
@@ -38,6 +38,7 @@
 
 #ifdef _WIN32
 #include <windows.h>
+#include <windns.h>
 #include <winsock2.h>
 #include <tlhelp32.h>
 #define sleep(x) Sleep((x)*1000)
@@ -176,6 +177,10 @@
 #include <netinet/in.h>
 #endif
 
+#ifdef HAVE_ARPA_NAMESER_H
+#include <arpa/nameser.h>
+#endif
+
 #ifdef HAVE_RESOLV_H
 #include <resolv.h>
 #endif
src/openvpn/tun.c +11 -8
index 923131ad..c4072133 100644
@@ -680,7 +680,7 @@ init_tun(const char *dev,        /* --dev option */
          int ifconfig_ipv6_netbits_parm,
          const char *ifconfig_ipv6_remote_parm,     /* --ifconfig parm 2 IPv6 */
          struct addrinfo *local_public,
-         struct addrinfo *remote_public,
+         struct servinfo *remote_public,
          const bool strict_warn,
          struct env_set *es,
          openvpn_net_ctx_t *ctx)
@@ -752,15 +752,18 @@ init_tun(const char *dev,        /* --dev option */
                 }
             }
 
-            for (curele = remote_public; curele; curele = curele->ai_next)
+            for (struct servinfo *si = remote_public; si; si = si->next)
             {
-                if (curele->ai_family == AF_INET)
+                for (curele = si->ai; curele; curele = curele->ai_next)
                 {
-                    check_addr_clash("remote",
-                                     tt->type,
-                                     ((struct sockaddr_in *)curele->ai_addr)->sin_addr.s_addr,
-                                     tt->local,
-                                     tt->remote_netmask);
+                    if (curele->ai_family == AF_INET)
+                    {
+                        check_addr_clash("remote",
+                                         tt->type,
+                                         ((struct sockaddr_in *)curele->ai_addr)->sin_addr.s_addr,
+                                         tt->local,
+                                         tt->remote_netmask);
+                    }
                 }
             }
 
src/openvpn/tun.h +2 -1
index 99826cf7..b969dbc1 100644
@@ -38,6 +38,7 @@
 #include "event.h"
 #include "proto.h"
 #include "misc.h"
+#include "socket.h"
 #include "networking.h"
 #include "ring_buffer.h"
 
@@ -275,7 +276,7 @@ struct tuntap *init_tun(const char *dev,        /* --dev option */
                         int ifconfig_ipv6_netbits_parm,            /* --ifconfig parm 1 / bits */
                         const char *ifconfig_ipv6_remote_parm,     /* --ifconfig parm 2 / IPv6 */
                         struct addrinfo *local_public,
-                        struct addrinfo *remote_public,
+                        struct servinfo *remote_public,
                         const bool strict_warn,
                         struct env_set *es,
                         openvpn_net_ctx_t *ctx);
Signature · 1 line
2.17.1
#4 lore raw

Am 26.08.20 um 18:51 schrieb Vladislav Grishenko:

Quoted 37 lines

DNS SRV host discovery allows to have multiple OpenVPN servers for a single domain w/o explicit profile enumeration, to move services from host to host with little fuss, and to designate hosts as primary servers for a service and others as backups.
Feature has been asked several times already, useful in case of huge ammounts of client & servers deployed.

Patch introduces "--remote-service-discovery [service]" option, which enables discovery and optionally allows to change "openvpn" default service to user specified.
Client will try to resolve ``_service._proto.host`` name via DNS and use returned DNS SRV records as OpenVPN remote server list ordered by server selection mechanism defined in RFC 2782:

A client MUST attempt to contact the target host with the lowest-numbered priority field value it can reach, target hosts with the same priority SHOULD be tried in an order defined by the weight field.
The weight field specifies a relative weight for entries with the same priority. Larger weights SHOULD be given a proportionately higher probability of being selected.
Domain administrators SHOULD use Weight 0 when there isn't any server selection to do. In the presence of records containing weights greater than 0, records with weight 0 SHOULD have a very small chance of being selected.

Example: instead of multiple remotes on order in profile:
remote s1.example.com 1194 udp
remote s2.example.com 1194 udp
remote s3.example.com 1194 udp
remote s4.example.com 1194 udp
remote example.com 1194 udp

now it's possible to specify one remote with discovery:
remote example.net 1194 udp remote-service-discovery

Quoted 5 lines

and addconfigure following DNS SRV records, for examle:

     _openvpn._udp.example.net IN SRV 10 60 1194 s1.example.net
     _openvpn._udp.example.net IN SRV 10 40 1194 s2.example.net
     _openvpn._udp.example.net IN SRV 10  0 1194 s3.example.net
     _openvpn._udp.example.net IN SRV 20  0 1194 s4.example.net

I haven't reviewed this yet but I think we need to agree what we actually want to solve/implement here or what the overall goal is.

Traditionally OpenVPN is configure with a number of remotes in order of
preference: e.g.

server1 udp 1194
server1 tcp 443
server2 udp 1194
server2 tcp 443

DNS SRV discovery is a more modern way to get the actual proto/protocol/IP to connect to. So I feel this should be able to replace a handwritten list of remote in the config. E.g. something like

remote-srv mydomain

However, the current patches forces all remotes to be either UDP or TCP or have a fixed UDP over TCP (or vice versa) priority.

Also reusing --remote for both service discovery and fallback is not great a solution. It suffer from trying to force two very different things into a single command.

I can see that a fallback behaviour if DNS SRV is desirable but I think the normal case is not to fallback to just a single remote but rather a list of --remote. So I would propose to instead have something like remote-dnsserv that does all the discovery and *also* a list of --remotes and then fallback to those if DNS SRV falls. If no fallback is desired, then only remote-dnnsserv is specified.

I am sure that the current implementation is good enough for your use case but I feel the current version is not useful enough as general feature but more only solves a specific use case.

Arne

#5 lore raw

Hi, Arne

Quoted 76 lines

From: Arne Schwabe
Sent: Wednesday, September 9, 2020 4:29 PM
Am 26.08.20 um 18:51 schrieb Vladislav Grishenko:

Quoted 37 lines

DNS SRV host discovery allows to have multiple OpenVPN servers for a single domain w/o explicit profile enumeration, to move services from host to host with little fuss, and to designate hosts as primary servers for a service and others as backups.
Feature has been asked several times already, useful in case of huge ammounts of client & servers deployed.

Patch introduces "--remote-service-discovery [service]" option, which enables discovery and optionally allows to change "openvpn" default service to user specified.
Client will try to resolve ``_service._proto.host`` name via DNS and use returned DNS SRV records as OpenVPN remote server list ordered by server selection mechanism defined in RFC 2782:

A client MUST attempt to contact the target host with the lowest-numbered priority field value it can reach, target hosts with the same priority SHOULD be tried in an order defined by the weight field.
The weight field specifies a relative weight for entries with the same priority. Larger weights SHOULD be given a proportionately higher probability of being selected.
Domain administrators SHOULD use Weight 0 when there isn't any server selection to do. In the presence of records containing weights greater than 0, records with weight 0 SHOULD have a very small chance of being selected.

Example: instead of multiple remotes on order in profile:
remote s1.example.com 1194 udp
remote s2.example.com 1194 udp
remote s3.example.com 1194 udp
remote s4.example.com 1194 udp
remote example.com 1194 udp

now it's possible to specify one remote with discovery:
remote example.net 1194 udp remote-service-discovery

Quoted 5 lines

and addconfigure following DNS SRV records, for examle:

     _openvpn._udp.example.net IN SRV 10 60 1194 s1.example.net
     _openvpn._udp.example.net IN SRV 10 40 1194 s2.example.net
     _openvpn._udp.example.net IN SRV 10  0 1194 s3.example.net
     _openvpn._udp.example.net IN SRV 20  0 1194 s4.example.net

I haven't reviewed this yet but I think we need to agree what we actually want to solve/implement here or what the overall goal is.

Traditionally OpenVPN is configure with a number of remotes in order of
preference: e.g.

server1 udp 1194
server1 tcp 443
server2 udp 1194
server2 tcp 443

DNS SRV discovery is a more modern way to get the actual proto/protocol/IP to connect to. So I feel this should be able to replace a handwritten list of remote in the config. E.g. something like

remote-srv mydomain

However, the current patches forces all remotes to be either UDP or TCP or have a fixed UDP over TCP (or vice versa) priority.

Also reusing --remote for both service discovery and fallback is not great a solution. It suffer from trying to force two very different things into a single command.

I can see that a fallback behaviour if DNS SRV is desirable but I think the normal case is not to fallback to just a single remote but rather a list of --remote. So I would propose to instead have something like remote-dnsserv that does all the discovery and *also* a list of --remotes and then fallback to those if DNS SRV falls. If no fallback is desired, then only remote-dnnsserv is specified.

Quoted 5 lines

I am sure that the current implementation is good enough for your use case but I feel the current version is not useful enough as general feature but more only solves a specific use case.

Arne

I see your point, thank you.
Fallback is done just to conform RFC 2782 suggestion, personally I feel it's hardly be used with controllable client profiles when it's known that domain.tld does dns srv.

Additional --remote-srv indeed has benefits:
* opens a non-conflicting syntax way to have multiple udp/tcp dns srv requests, as you wrote in earlier mails. This will allow to sort them all by prio/weight.
* cleaner syntax for specifying custom dns srv service name, with default "openvpn" and protocol (if need to limit discovery to only udp or tcp) So, in my mind syntax looks like:

	--remote-srv [service] [protocol], where both args are optional, by default service==openvpn, protocol==any

And, important thing, multiple --remote-srv still needs to be supported for:
* custom order of protocol
* different service names
* allow to reuse --remote-random with --random-srv as well to achieve dns-srv-based lb

Is it good enough in this form?

--
Best Regards, Vladislav Grishenko

#6 lore raw
Quoted 2 lines

I see your point, thank you.
Fallback is done just to conform RFC 2782 suggestion, personally I feel it's hardly be used with controllable client profiles when it's known that domain.tld does dns srv.

Yes. And see the point in that RFC. I would rather document that for out fallback the user has to manually specify that because I don't see a good "works for all" fallback.

Quoted 5 lines

Additional --remote-srv indeed has benefits:
* opens a non-conflicting syntax way to have multiple udp/tcp dns srv requests, as you wrote in earlier mails. This will allow to sort them all by prio/weight.
* cleaner syntax for specifying custom dns srv service name, with default "openvpn" and protocol (if need to limit discovery to only udp or tcp) So, in my mind syntax looks like:

	--remote-srv [service] [protocol], where both args are optional, by default service==openvpn, protocol==any

Sounds good to me!

Quoted 6 lines

And, important thing, multiple --remote-srv still needs to be supported for:
* custom order of protocol
* different service names
* allow to reuse --remote-random with --random-srv as well to achieve dns-srv-based lb

Is it good enough in this form?

I don't really see the need for that but it doesn't break the normal case of just one remote-srv, so fine with me.

Arne

#7 lore raw
Quoted 10 lines

From: Arne Schwabe
Sent: Wednesday, September 9, 2020 5:15 PM

Quoted 2 lines

I see your point, thank you.
Fallback is done just to conform RFC 2782 suggestion, personally I feel it's

hardly be used with controllable client profiles when it's known that domain.tld does dns srv.

Yes. And see the point in that RFC. I would rather document that for out fallback the user has to manually specify that because I don't see a good "works for all" fallback.

Sure, I saw. RFC point of fallback was pretty valid for combined --remote for seamless migration dns->srv dns.
With new --remote-srv this will be not necessary anymore, no migration here. Moreover, facing the coexisting with usual --remote, fallback becomes pretty useless additional complexity.

Quoted 5 lines
Quoted 3 lines

Additional --remote-srv indeed has benefits:
* opens a non-conflicting syntax way to have multiple udp/tcp dns srv

requests, as you wrote in earlier mails. This will allow to sort them all by prio/weight.

Quoted 7 lines
Quoted 2 lines

* cleaner syntax for specifying custom dns srv service name, with default "openvpn" and protocol (if need to limit discovery to only udp or tcp)

So, in my mind syntax looks like:

Quoted 2 lines

--remote-srv [service] [protocol], where both args are optional, by
default service==openvpn, protocol==any

Sounds good to me!

Great, doing this way.

Quoted 7 lines
Quoted 7 lines

And, important thing, multiple --remote-srv still needs to be supported for:
* custom order of protocol
* different service names
* allow to reuse --remote-random with --random-srv as well to achieve dns-srv-based lb

Quoted 7 lines
Quoted 1 line

Is it good enough in this form?

I don't really see the need for that but it doesn't break the normal case of just one remote-srv, so fine with me.

Arne

--
Best Regards, Vladislav Grishenko