Fix bug #2709: TCP connect returns Invalid argument.
[yaz-moved-to-github.git] / src / tcpip.c
index b1ee8e4..97461da 100644 (file)
@@ -68,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);
@@ -419,26 +419,6 @@ int tcpip_strtoaddr_ex(const char *str, struct sockaddr_in *add,
 }
 
 #if HAVE_GETADDRINFO
-/** \brief Creates socket using particular address family (AF_)
-    \param ai getaddrinfo result
-    \param mask family mask
-    \returns socket or -1 if none could be created
-
-*/
-static int create_socket_family(struct addrinfo *ai, unsigned mask)
-{
-    for (; ai; ai = ai->ai_next)
-    {
-        if ((ai->ai_family & mask) == mask)
-        {
-            int s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
-            if (s != -1)
-                return s;
-        }
-    }
-    return -1;
-}
-
 void *tcpip_straddr(COMSTACK h, const char *str)
 {
     tcpip_state *sp = (tcpip_state *)h->cprivate;
@@ -453,13 +433,17 @@ void *tcpip_straddr(COMSTACK h, const char *str)
     sp->ai = tcpip_getaddrinfo(str, port);
     if (sp->ai && h->state == CS_ST_UNBND)
     {
-        /* The getaddrinfo call may return multiple addresses when passive
-           flags are used (AI_PASSIVE). This function picks the IPV6 if a
-           socket can be created for it. Otherwise IPV4 is used.
-           See also bug #2350 */
-        int s = create_socket_family(sp->ai, AF_INET6);
-        if (s == -1)
-            s = create_socket_family(sp->ai, AF_INET);
+        int s = -1;
+        struct addrinfo *ai = sp->ai;
+        for (; 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;
         h->iofile = s;
@@ -829,7 +813,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 (