Skip to contentSkip to conversation

getaddrinfo() confusion / way forward?

Openvpn-devel Gert Doering 2 participants 7 messages Apr 6, 2014 – Apr 10, 2014

lore
Share & tools
lore
#0 lore raw

Hiya,

I'm wondering if one of you can share insight here - I've been debugging OpenVPN (-master) on OpenSolaris, which failed in building sockets, due to getaddrinfo() returning "ai_protocol = 0" for everything - while the OpenVPN socket code relies on ai_protocol being IPPROTO_UDP or IPPROTO_TCP (and ASSERT()s out in create_socket() if it's neither).

To understand the issue, I've made a test program, which is appended below - it basically calls getaddrinfo() to resolve two different dual-stack servers, one for SOCK_DGRAM and the other for SOCK_STREAM.

On Linux and NetBSD, the result looks like this:

getaddrinfo('www.google.de', '80', family=24, socktype=1) -> status=0
ai_: flags=8, family=24, socktype=1, protocol=6

     addrlen=28, addr='2a00:1450:4016:801::1018'

getaddrinfo('conn-test-server.openvpn.org', '51194', family=24, socktype=2) -> status=0
ai_: flags=8, family=24, socktype=2, protocol=17

     addrlen=28, addr='2607:fc50:1001:5200::4'

(only the ipv6-only case show, "protocol=6" is TCP, "protocol=17" is UDP)
- which is the expected case.

Same code on Solaris:

getaddrinfo('www.google.de', '80', family=26, socktype=2) -> status=0
ai_: flags=64, family=26, socktype=2, protocol=0

     addrlen=32, addr='2a00:1450:4016:802::101f'

getaddrinfo('conn-test-server.openvpn.org', '51194', family=26, socktype=1) -> status=0
ai_: flags=64, family=26, socktype=1, protocol=0

     addrlen=32, addr='2607:fc50:1001:5200::4'

-> ai_protocol is "0", which is basically "undefined", but on *Solaris*, this seems to be "use what is appropriate", from "man socket":

The protocol parameter is a protocol-family-specific value which specifies a particular protocol to be used with the socket. Normally this value is zero, as commonly only a single protocol exists to support a particular socket type within a given protocol family. However, multiple protocols may exist, in which case a particular protocol may be speci- fied in this manner.

so now I wonder how to tackle this in OpenVPN.

One possible approach would be to add this hack to the end of openvpn_getaddrinfo():

#ifdef TARGET_SOLARIS
/* fixup Solaris' getaddrinfo() returning ai_protocol=0 */ if (status==0 && (*res)->ai_protocol == 0 )

    {
      struct addrinfo *ai = *res;
      msg( M_WARN, "invoking Solaris ai_protocol==0 fixup" );
      while( ai )
        {
          if(flags & GETADDR_DATAGRAM)
              ai->ai_protocol = IPPROTO_UDP;
          else
              ai->ai_protocol = IPPROTO_TCP;
          ai = ai->ai_next;
        }
    }

#endif

... that is, depending on the type of socket we've asked for, fix the return values to always set ai_protocol to the values that create_socket() wants to see.

The other approach would be to change create_socket() to decide upon ai_socktype (SOCK_DGRAM/SOCK_STREAM) whether to call create_socket_udp() or create_socket_tcp(), and change *those* to ignore ai_protocol and always use IPPROTO_UDP / IPPROTO_TCP instead for their socket() calls.

These are the code bits in question:

create_socket()
{
if (addr->ai_protocol == IPPROTO_UDP)

    {
      sock->sd = create_socket_udp (addr, sock->sockflags);
...
    }
  else if (addr->ai_protocol == IPPROTO_TCP)
    {
      sock->sd = create_socket_tcp (addr);
    }
  else
    {
      ASSERT (0);		<<< this is where solaris bombs out
    }
}

create_socket_tcp()
{
...
ASSERT (addrinfo);
ASSERT (addrinfo->ai_socktype == SOCK_STREAM);
ASSERT (addrinfo->ai_protocol == IPPROTO_TCP);

if ((sd = socket (addrinfo->ai_family, addrinfo->ai_socktype, addrinfo->ai_protocol)) < 0)

create_socket_udp()
{
...
ASSERT (addrinfo);
ASSERT (addrinfo->ai_socktype == SOCK_DGRAM);
ASSERT (addrinfo->ai_protocol == IPPROTO_UDP);

if ((sd = socket (addrinfo->ai_family, addrinfo->ai_socktype, addrinfo->ai_protocol)) < 0)

Arne, what do you think?

gert

Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
                                                           //www.muc.de/~gert/
Gert Doering - Munich, Germany                             [email protected]...
fax: +49-89-35655025                        [email protected]...

/* getaddrinfo() sanity test */

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <string.h>

void gai( char * host, char * service, int ai_family, int ai_socktype )

{
    struct addrinfo hints, *ai, *ap;
    int status;
    char print_host[200];

    memset( &hints, 0, sizeof(hints) );
    hints.ai_family = ai_family;
    hints.ai_socktype = ai_socktype;
    /* hints.ai_protocol = IPPROTO_TCP; */  /* test, no effect */
    hints.ai_flags = AI_NUMERICSERV;

    status = getaddrinfo( host, service, &hints, &ai );

    printf( "\ngetaddrinfo('%s', '%s', family=%d, socktype=%d) -> status=%d\n", 
            host, service, ai_family, ai_socktype, status );

    if ( status != 0 ) return;

    for( ap = ai; ap != NULL; ap=ap->ai_next )
    {
	printf( "ai_: flags=%d, family=%d, socktype=%d, protocol=%d\n",
		ap->ai_flags, ap->ai_family, ap->ai_socktype, ap->ai_protocol );
	getnameinfo( ap->ai_addr, ap->ai_addrlen, 
			print_host, sizeof(print_host)-1, 
			NULL, 0, NI_NUMERICHOST );
        printf( "     addrlen=%d, addr='%s'\n",
		ap->ai_addrlen, print_host );
    }
    freeaddrinfo(ai);
}

int main( int argc, char ** argv )

{
    gai( "www.google.de", "80", AF_UNSPEC, SOCK_STREAM );
    gai( "www.google.de", "80", AF_INET6, SOCK_STREAM );
    gai( "conn-test-server.openvpn.org", "51194", AF_UNSPEC, SOCK_DGRAM );
    gai( "conn-test-server.openvpn.org", "51194", AF_INET6, SOCK_DGRAM );
    return 0;
}
#1 lore raw

Hi,

Quoted 2 lines · Gert Doering

One possible approach would be to add this hack to the end of openvpn_getaddrinfo():

A slightly simpler version of this could go to the beginning of create_socket() (--> no ai_next chain walking needed, etc.).

Patch attached. With that patch in place, my OpenSolaris machine passes all my test cases. So functionally it's ok, just "philosophically" one might argue :-)

gert

Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
                                                           //www.muc.de/~gert/
Gert Doering - Munich, Germany                             [email protected]...
fax: +49-89-35655025                        [email protected]...

From 841ea45456e93f973be3f486a30549d35d9cade1 Mon Sep 17 00:00:00 2001
From: Gert Doering <[email protected]...>
Date: Sun, 6 Apr 2014 10:44:38 +0200
Subject: [PATCH] Work around Solaris getaddrinfo() returing ai_protocol=0

Create_socket() and sub-functions assume that the ai_protocol value returned by getaddrinfo() is IPPROTO_UDP or IPPROTO_TCP. On Solaris, it is "0", because Solaris's socket() call will then "select the right protocol" - but it breaks our code. So if ai_protocol is 0, set the IPPROTO_* value according to ai_socktype (SOCK_DGRAM/SOCK_STREAM).

---
 src/openvpn/socket.c |   13 +++++++++++++
 1 files changed, 13 insertions(+), 0 deletions(-)
src/openvpn/socket.c +13 -0
index 7deef0a..75dde7e 100644
@@ -858,6 +858,19 @@ static void bind_local (struct link_socket *sock, const sa_family_t ai_family)
 static void
 create_socket (struct link_socket* sock, struct addrinfo* addr)
 {
+#ifdef TARGET_SOLARIS
+  /* fixup Solaris' getaddrinfo() returning ai_protocol=0 
+   */
+  if (addr->ai_protocol == 0 )
+    {
+      msg( M_WARN, "invoking Solaris ai_protocol==0 fixup" );
+      if( addr->ai_socktype == SOCK_DGRAM )
+          addr->ai_protocol = IPPROTO_UDP;
+      else
+	  addr->ai_protocol = IPPROTO_TCP;
+    }
+#endif
+
   if (addr->ai_protocol == IPPROTO_UDP)
     {
       sock->sd = create_socket_udp (addr, sock->sockflags);
Signature · 1 line
1.5.6.5
#2 lore raw

Hi,

Quoted 2 lines · Gert Doering

A slightly simpler version of this could go to the beginning of create_socket() (--> no ai_next chain walking needed, etc.).

Arne suggested an even more simple version of this - which is convincing, because it takes away lines of code, instead of adding more :-)

Patch attached. (Tested, works!)

gert

Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
                                                           //www.muc.de/~gert/
Gert Doering - Munich, Germany                             [email protected]...
fax: +49-89-35655025                        [email protected]...

From a9b7c4e0fcdf89efb71a910c2295c6158113a732 Mon Sep 17 00:00:00 2001
From: Gert Doering <[email protected]...>
Date: Mon, 7 Apr 2014 22:12:05 +0200
Subject: [PATCH] Work around Solaris getaddrinfo() returing ai_protocol=0

Create_socket() and sub-functions assume that the ai_protocol value returned by getaddrinfo() is IPPROTO_UDP or IPPROTO_TCP. On Solaris, it is "0", because Solaris's socket() call will then "select the right protocol" - but it breaks our code. So remove ASSERT()s on ai_protocol, and also accept properly set ai_socktype (SOCK_DGRAM/SOCK_STREAM) values if ai_protocol is not set.

---
 src/openvpn/socket.c |    7 ++-----
 1 files changed, 2 insertions(+), 5 deletions(-)
src/openvpn/socket.c +2 -5
index 7deef0a..ed4bc6f 100644
@@ -775,7 +775,6 @@ create_socket_tcp (struct addrinfo* addrinfo)
 
   ASSERT (addrinfo);
   ASSERT (addrinfo->ai_socktype == SOCK_STREAM);
-  ASSERT (addrinfo->ai_protocol == IPPROTO_TCP);
 
   if ((sd = socket (addrinfo->ai_family, addrinfo->ai_socktype, addrinfo->ai_protocol)) < 0)
     msg (M_ERR, "Cannot create TCP socket");
@@ -800,7 +799,6 @@ create_socket_udp (struct addrinfo* addrinfo, const unsigned int flags)
 
   ASSERT (addrinfo);
   ASSERT (addrinfo->ai_socktype == SOCK_DGRAM);
-  ASSERT (addrinfo->ai_protocol == IPPROTO_UDP);
 
   if ((sd = socket (addrinfo->ai_family, addrinfo->ai_socktype, addrinfo->ai_protocol)) < 0)
     msg (M_ERR, "UDP: Cannot create UDP/UDP6 socket");
@@ -858,7 +856,7 @@ static void bind_local (struct link_socket *sock, const sa_family_t ai_family)
 static void
 create_socket (struct link_socket* sock, struct addrinfo* addr)
 {
-  if (addr->ai_protocol == IPPROTO_UDP)
+  if (addr->ai_protocol == IPPROTO_UDP || addr->ai_socktype == SOCK_DGRAM)
     {
       sock->sd = create_socket_udp (addr, sock->sockflags);
       sock->sockflags |= SF_GETADDRINFO_DGRAM;
@@ -878,7 +876,7 @@ create_socket (struct link_socket* sock, struct addrinfo* addr)
 	}
 #endif
     }
-  else if (addr->ai_protocol == IPPROTO_TCP)
+  else if (addr->ai_protocol == IPPROTO_TCP || addr->ai_socktype == SOCK_STREAM)
     {
       sock->sd = create_socket_tcp (addr);
     }
@@ -1806,7 +1804,6 @@ phase2_tcp_client (struct link_socket *sock, struct signal_info *sig_info)
   const bool proxy_retry = false;
 #endif
   do {
-    ASSERT (sock->info.lsa->current_remote->ai_protocol == IPPROTO_TCP);
     socket_connect (&sock->sd,
                    sock->info.lsa->current_remote->ai_addr,
                    sock->connect_timeout,
Signature · 1 line
1.5.6.5
#3 lore raw

Am 07.04.14 22:17, schrieb Gert Doering:

Quoted 10 lines

Hi,

Quoted 2 lines · Gert Doering

A slightly simpler version of this could go to the beginning of create_socket() (--> no ai_next chain walking needed, etc.).

Arne suggested an even more simple version of this - which is convincing, because it takes away lines of code, instead of adding more :-)

Patch attached. (Tested, works!)

ACK from me to basically my own patch.

Arne

#4 lore raw

Hi,

Quoted 10 lines · Arne Schwabe

Am 07.04.14 22:17, schrieb Gert Doering:

Quoted 7 lines · Gert Doering
Quoted 2 lines

A slightly simpler version of this could go to the beginning of create_socket() (--> no ai_next chain walking needed, etc.).

Arne suggested an even more simple version of this - which is convincing, because it takes away lines of code, instead of adding more :-)

Patch attached. (Tested, works!)

ACK from me to basically my own patch.

Well, it's an interesting turn of events :-) - I'll commit that with you as an author, and signed-off-by: lines from both of us. OK?

gert

Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
                                                           //www.muc.de/~gert/
Gert Doering - Munich, Germany                             [email protected]...
fax: +49-89-35655025                        [email protected]...
#5 lore raw

Am 08.04.14 21:12, schrieb Gert Doering:

Quoted 7 lines

Hi,

Quoted 4 lines · Arne Schwabe

Am 07.04.14 22:17, schrieb Gert Doering:

Quoted 3 lines · Gert Doering
Quoted 2 lines

A slightly simpler version of this could go to the beginning of create_socket() (--> no ai_next chain walking needed, etc.).

Quoted 9 lines
Quoted 5 lines
Quoted 4 lines

Arne suggested an even more simple version of this - which is convincing, because it takes away lines of code, instead of adding more :-)

Patch attached. (Tested, works!)

ACK from me to basically my own patch.

Well, it's an interesting turn of events :-) - I'll commit that with you as an author, and signed-off-by: lines from both of us. OK?

Okay for me.

Arne

#6 lore raw

Hi,

Quoted 4 lines · Arne Schwabe

Am 08.04.14 21:12, schrieb Gert Doering:

Quoted 2 lines

Well, it's an interesting turn of events :-) - I'll commit that with you as an author, and signed-off-by: lines from both of us. OK?

Okay for me.

Committed, and pushed.

commit 891636cb397e02d6e283da7e1926a5ca6cd11b02

Author: Arne Schwabe <[email protected]...>
Date:   Mon Apr 7 22:12:05 2014 +0200

Work around Solaris getaddrinfo() returing ai_protocol=0
...

    Signed-off-by: Gert Doering <[email protected]...>
    Signed-off-by: Arne Schwabe <[email protected]...>
    Acked-by: Gert Doering <[email protected]...>
    Acked-by: Arne Schwabe <[email protected]...>

"close cooperation" :-)

gert

Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
                                                           //www.muc.de/~gert/
Gert Doering - Munich, Germany                             [email protected]...
fax: +49-89-35655025                        [email protected]...