Prefer IPV6 addresses over IPV4 - should refix bug #2350.
[yaz-moved-to-github.git] / src / tcpip.c
index bab44df..416b707 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of the YAZ toolkit.
- * Copyright (C) 1995-2008 Index Data
+ * Copyright (C) 1995-2009 Index Data
  * See the file LICENSE for details.
  */
 /**
@@ -25,7 +25,6 @@
 #endif
 
 #ifdef WIN32
-
 /* VS 2003 or later has getaddrinfo; older versions do not */
 #include <winsock2.h>
 #if _MSC_VER >= 1300
 #else
 #define HAVE_GETADDRINFO 0
 #endif
+#endif
 
-#else
+#if HAVE_NETINET_IN_H
 #include <netinet/in.h>
+#endif
+#if HAVE_NETDB_H
 #include <netdb.h>
+#endif
+#if HAVE_ARPA_INET_H
 #include <arpa/inet.h>
+#endif
+#if HAVE_NETINET_TCP_H
 #include <netinet/tcp.h>
 #endif
-
 #if HAVE_SYS_SOCKET_H
 #include <sys/socket.h>
 #endif
@@ -63,7 +68,7 @@
 
 #include <yaz/comstack.h>
 #include <yaz/tcpip.h>
-#include <yaz/nmem.h>
+#include <yaz/errno.h>
 
 static int tcpip_close(COMSTACK h);
 static int tcpip_put(COMSTACK h, char *buf, int size);
@@ -260,7 +265,7 @@ COMSTACK yaz_tcpip_create(int s, int flags, int protocol,
 static void tcpip_create_cred(COMSTACK cs)
 {
     tcpip_state *sp = (tcpip_state *) cs->cprivate;
-    sp->cred_ptr = xmalloc(sizeof(*sp->cred_ptr));
+    sp->cred_ptr = (struct tcpip_cred_ptr *) xmalloc(sizeof(*sp->cred_ptr));
     sp->cred_ptr->ref = 1;
     gnutls_certificate_allocate_credentials(&sp->cred_ptr->xcred);
 }
@@ -413,7 +418,6 @@ int tcpip_strtoaddr_ex(const char *str, struct sockaddr_in *add,
     return 1;
 }
 
-
 #if HAVE_GETADDRINFO
 void *tcpip_straddr(COMSTACK h, const char *str)
 {
@@ -430,12 +434,32 @@ void *tcpip_straddr(COMSTACK h, const char *str)
     if (sp->ai && h->state == CS_ST_UNBND)
     {
         int s = -1;
-        struct addrinfo *ai = sp->ai;
-        for (; ai; ai = ai->ai_next)
+        struct addrinfo *ai;
+        /* try to make IPV6 socket first */
+        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)
+                {
+                    sp->ai = ai;
+                    break;
+                }
+            }
+        }
+        if (s == -1)
+        {
+            /* no IPV6 could be made.. Try them all */
+            for (ai = sp->ai; ai; ai = ai->ai_next)
+            {
+                s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
+                if (s != -1)
+                {
+                    sp->ai = ai;
+                    break;
+                }
+            }
         }
         if (s == -1)
             return 0;
@@ -599,12 +623,14 @@ int tcpip_rcvconnect(COMSTACK h)
         tcpip_create_cred(h);
 
         gnutls_init(&sp->session, GNUTLS_CLIENT);
-        gnutls_priority_set_direct(sp->session,  "PERFORMANCE", NULL);
+        gnutls_set_default_priority(sp->session);
         gnutls_credentials_set (sp->session, GNUTLS_CRD_CERTIFICATE,
                                 sp->cred_ptr->xcred);
         
-        gnutls_transport_set_ptr(sp->session, (gnutls_transport_ptr_t) h->iofile);
-        
+        /* cast to intermediate size_t to avoid GCC warning. */
+        gnutls_transport_set_ptr(sp->session, 
+                                 (gnutls_transport_ptr_t) 
+                                 (size_t) h->iofile);
         res = gnutls_handshake(sp->session);
         if (res < 0)
         {
@@ -806,7 +832,11 @@ int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
         h->cerrno = CSOUTSTATE;
         return -1;
     }
+#ifdef WIN32
+    h->newfd = accept(h->iofile, 0, 0);
+#else
     h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
+#endif
     if (h->newfd < 0)
     {
         if (
@@ -938,8 +968,7 @@ COMSTACK tcpip_accept(COMSTACK h)
                 xfree(state);
                 return 0;
             }
-            res = gnutls_priority_set_direct(state->session,  
-                                             "PERFORMANCE", NULL);
+            res = gnutls_set_default_priority(state->session);
             if (res != GNUTLS_E_SUCCESS)
             {
                 xfree(cnew);
@@ -955,8 +984,10 @@ COMSTACK tcpip_accept(COMSTACK h)
                 xfree(state);
                 return 0;
             }
+            /* cast to intermediate size_t to avoid GCC warning. */
             gnutls_transport_set_ptr(state->session, 
-                                     (gnutls_transport_ptr_t) cnew->iofile);
+                                     (gnutls_transport_ptr_t)
+                                     (size_t) cnew->iofile);
         }
 #elif HAVE_OPENSSL_SSL_H
         state->ctx = st->ctx;
@@ -1040,8 +1071,8 @@ int tcpip_get(COMSTACK h, char **buf, int *bufsize)
     TRC(fprintf(stderr, "tcpip_get: bufsize=%d\n", *bufsize));
     if (sp->altlen) /* switch buffers */
     {
-        TRC(fprintf(stderr, "  %d bytes in altbuf (0x%x)\n", sp->altlen,
-            (unsigned) sp->altbuf));
+        TRC(fprintf(stderr, "  %d bytes in altbuf (%p)\n", sp->altlen,
+                    sp->altbuf));
         tmpc = *buf;
         tmpi = *bufsize;
         *buf = sp->altbuf;
@@ -1146,8 +1177,8 @@ int tcpip_get(COMSTACK h, char **buf, int *bufsize)
                 h->cerrno = CSYSERR;
                 return -1;
             }
-        TRC(fprintf(stderr, "  Moving %d bytes to altbuf(0x%x)\n", tomove,
-            (unsigned) sp->altbuf));
+        TRC(fprintf(stderr, "  Moving %d bytes to altbuf(%p)\n", tomove,
+                    sp->altbuf));
         memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
     }
     if (berlen < CS_TCPIP_BUFCHUNK - 1)
@@ -1171,8 +1202,8 @@ int ssl_get(COMSTACK h, char **buf, int *bufsize)
     TRC(fprintf(stderr, "ssl_get: bufsize=%d\n", *bufsize));
     if (sp->altlen) /* switch buffers */
     {
-        TRC(fprintf(stderr, "  %d bytes in altbuf (0x%x)\n", sp->altlen,
-            (unsigned) sp->altbuf));
+        TRC(fprintf(stderr, "  %d bytes in altbuf (%p)\n", sp->altlen,
+                    sp->altbuf));
         tmpc = *buf;
         tmpi = *bufsize;
         *buf = sp->altbuf;
@@ -1230,8 +1261,8 @@ int ssl_get(COMSTACK h, char **buf, int *bufsize)
         } else if (sp->altsize < req)
             if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
                 return -1;
-        TRC(fprintf(stderr, "  Moving %d bytes to altbuf(0x%x)\n", tomove,
-            (unsigned) sp->altbuf));
+        TRC(fprintf(stderr, "  Moving %d bytes to altbuf(%p)\n", tomove,
+                    sp->altbuf));
         memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
     }
     if (berlen < CS_TCPIP_BUFCHUNK - 1)
@@ -1663,6 +1694,7 @@ static int tcpip_get_connect(COMSTACK h, char **buf, int *bufsize)
 /*
  * Local variables:
  * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
  * indent-tabs-mode: nil
  * End:
  * vim: shiftwidth=4 tabstop=8 expandtab