COMSTACK: @ uses AF_UNSPEC
[yaz-moved-to-github.git] / src / tcpip.c
index 4efc19a..e712c0e 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of the YAZ toolkit.
- * Copyright (C) 1995-2012 Index Data
+ * Copyright (C) 1995-2013 Index Data
  * See the file LICENSE for details.
  */
 /**
@@ -238,7 +238,7 @@ COMSTACK tcpip_type(int s, int flags, int protocol, void *vp)
     sp->connect_response_len = 0;
 
     p->timeout = COMSTACK_DEFAULT_TIMEOUT;
-    TRC(fprintf(stderr, "Created new TCPIP comstack\n"));
+    TRC(fprintf(stderr, "Created new TCPIP comstack h=%p\n", p));
 
     return p;
 }
@@ -338,7 +338,8 @@ static int ssl_check_error(COMSTACK h, tcpip_state *sp, int res)
 
 #if HAVE_GETADDRINFO
 /* resolve using getaddrinfo */
-struct addrinfo *tcpip_getaddrinfo(const char *str, const char *port)
+struct addrinfo *tcpip_getaddrinfo(const char *str, const char *port,
+                                   int *ipv6_only)
 {
     struct addrinfo hints, *res;
     int error;
@@ -366,18 +367,28 @@ struct addrinfo *tcpip_getaddrinfo(const char *str, const char *port)
     if (!strcmp("@", host))
     {
         hints.ai_flags = AI_PASSIVE;
+        hints.ai_family = AF_UNSPEC;
+        error = getaddrinfo(0, port, &hints, &res);
+        *ipv6_only = 0;
+    }
+    else if (!strcmp("@4", host))
+    {
+        hints.ai_flags = AI_PASSIVE;
         hints.ai_family = AF_INET;
         error = getaddrinfo(0, port, &hints, &res);
+        *ipv6_only = -1;
     }
     else if (!strcmp("@6", host))
     {
         hints.ai_flags = AI_PASSIVE;
         hints.ai_family = AF_INET6;
         error = getaddrinfo(0, port, &hints, &res);
+        *ipv6_only = 1;
     }
     else
     {
         error = getaddrinfo(host, port, &hints, &res);
+        *ipv6_only = -1;
     }
     if (error)
         return 0;
@@ -433,6 +444,7 @@ void *tcpip_straddr(COMSTACK h, const char *str)
     tcpip_state *sp = (tcpip_state *)h->cprivate;
     const char *port = "210";
     struct addrinfo *ai = 0;
+    int ipv6_only = 0;
     if (h->protocol == PROTO_HTTP)
     {
         if (h->type == ssl_type)
@@ -445,21 +457,37 @@ void *tcpip_straddr(COMSTACK h, const char *str)
 
     if (sp->ai)
         freeaddrinfo(sp->ai);
-    sp->ai = tcpip_getaddrinfo(str, port);
+    sp->ai = tcpip_getaddrinfo(str, port, &ipv6_only);
     if (sp->ai && h->state == CS_ST_UNBND)
     {
         int s = -1;
         for (ai = sp->ai; ai; ai = ai->ai_next)
         {
-            s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
-            if (s != -1)
-                break;
+            if (ai->ai_family == AF_INET6)
+            {
+                s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
+                if (s != -1)
+                    break;
+            }
+        }
+        if (s == -1)
+        {
+            for (ai = sp->ai; ai; ai = ai->ai_next)
+            {
+                s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
+                if (s != -1)
+                     break;
+            }
         }
         if (s == -1)
             return 0;
         assert(ai);
         h->iofile = s;
-
+        if (ai->ai_family == AF_INET6 && ipv6_only >= 0 &&
+            setsockopt(h->iofile,
+                       IPPROTO_IPV6,
+                       IPV6_V6ONLY, &ipv6_only, sizeof(ipv6_only)))
+                return 0;
         if (!tcpip_set_blocking(h, h->flags))
             return 0;
     }
@@ -518,7 +546,7 @@ int tcpip_connect(COMSTACK h, void *address)
     struct sockaddr_in *add = (struct sockaddr_in *) address;
 #endif
     int r;
-    TRC(fprintf(stderr, "tcpip_connect\n"));
+    TRC(fprintf(stderr, "tcpip_connect h=%p\n", h));
     h->io_pending = 0;
     if (h->state != CS_ST_UNBND)
     {
@@ -1009,7 +1037,7 @@ int tcpip_get(COMSTACK h, char **buf, int *bufsize)
     int tmpi, berlen, rest, req, tomove;
     int hasread = 0, res;
 
-    TRC(fprintf(stderr, "tcpip_get: bufsize=%d\n", *bufsize));
+    TRC(fprintf(stderr, "tcpip_get: h=%p bufsize=%d\n", h, *bufsize));
     if (sp->altlen) /* switch buffers */
     {
         TRC(fprintf(stderr, "  %d bytes in altbuf (%p)\n", sp->altlen,
@@ -1227,7 +1255,7 @@ int tcpip_put(COMSTACK h, char *buf, int size)
     int res;
     struct tcpip_state *state = (struct tcpip_state *)h->cprivate;
 
-    TRC(fprintf(stderr, "tcpip_put: size=%d\n", size));
+    TRC(fprintf(stderr, "tcpip_put: h=%p size=%d\n", h, size));
     h->io_pending = 0;
     h->event = CS_DATA;
     if (state->towrite < 0)
@@ -1345,12 +1373,12 @@ void tcpip_close(COMSTACK h)
 {
     tcpip_state *sp = (struct tcpip_state *)h->cprivate;
 
-    TRC(fprintf(stderr, "tcpip_close h=%p pid=%d\n", h, getpid()));
+    TRC(fprintf(stderr, "tcpip_close: h=%p pid=%d\n", h, getpid()));
     if (h->iofile != -1)
     {
 #if HAVE_GNUTLS_H
         if (sp->session)
-            gnutls_bye(sp->session, GNUTLS_SHUT_RDWR);
+            gnutls_bye(sp->session, GNUTLS_SHUT_WR);
 #elif HAVE_OPENSSL_SSL_H
         if (sp->ssl)
         {