Fix-uninit var in new non-blocking getaddrinfo bug
[yaz-moved-to-github.git] / src / tcpip.c
index 87557b2..8df6a40 100644 (file)
@@ -67,7 +67,9 @@
 #include <yaz/tcpip.h>
 #include <yaz/errno.h>
 
+#ifndef WIN32
 #define RESOLVER_THREAD 1
+#endif
 
 static void tcpip_close(COMSTACK h);
 static int tcpip_put(COMSTACK h, char *buf, int size);
@@ -221,6 +223,7 @@ COMSTACK tcpip_type(int s, int flags, int protocol, void *vp)
     sp->port = 0;
 #endif
     sp->ai = 0;
+    sp->ai_connect = 0;
 #endif
     sp->altbuf = 0;
     sp->altsize = sp->altlen = 0;
@@ -478,9 +481,12 @@ static struct addrinfo *wait_resolver_thread(COMSTACK h)
     close(sp->pipefd[0]);
     close(sp->pipefd[1]);
     sp->pipefd[0] = -1;
+    h->iofile = -1;
     return create_net_socket(h);
 }
 
+#endif
+
 void *tcpip_straddr(COMSTACK h, const char *str)
 {
     tcpip_state *sp = (tcpip_state *)h->cprivate;
@@ -496,36 +502,21 @@ void *tcpip_straddr(COMSTACK h, const char *str)
         else
             port = "80";
     }
-
-    if (sp->pipefd[0] != -1)
-        return 0;
-    if (pipe(sp->pipefd) == -1)
-        return 0;
-
-    sp->port = port;
-    xfree(sp->hoststr);
-    sp->hoststr = xstrdup(str);
-    sp->thread_id = yaz_thread_create(resolver_thread, h);
-    return sp->hoststr;
-}
-
-#else
-
-void *tcpip_straddr(COMSTACK h, const char *str)
-{
-    tcpip_state *sp = (tcpip_state *)h->cprivate;
-    const char *port = "210";
-    struct addrinfo *ai = 0;
-    if (h->protocol == PROTO_HTTP)
+#if RESOLVER_THREAD
+    if (h->flags & CS_FLAGS_DNS_NO_BLOCK)
     {
-        if (h->type == ssl_type)
-            port = "443";
-        else
-            port = "80";
-    }
-    if (!tcpip_init())
-        return 0;
+        if (sp->pipefd[0] != -1)
+            return 0;
+        if (pipe(sp->pipefd) == -1)
+            return 0;
 
+        sp->port = port;
+        xfree(sp->hoststr);
+        sp->hoststr = xstrdup(str);
+        sp->thread_id = yaz_thread_create(resolver_thread, h);
+        return sp->hoststr;
+    }
+#endif
     if (sp->ai)
         freeaddrinfo(sp->ai);
     sp->ai = tcpip_getaddrinfo(str, port, &sp->ipv6_only);
@@ -533,11 +524,9 @@ void *tcpip_straddr(COMSTACK h, const char *str)
     {
         return create_net_socket(h);
     }
-    return ai;
+    return sp->ai;
 }
 
-#endif
-
 #else
 void *tcpip_straddr(COMSTACK h, const char *str)
 {
@@ -693,9 +682,7 @@ int tcpip_connect(COMSTACK h, void *address)
  */
 int tcpip_rcvconnect(COMSTACK h)
 {
-#if HAVE_GNUTLS_H
     tcpip_state *sp = (tcpip_state *)h->cprivate;
-#endif
     TRC(fprintf(stderr, "tcpip_rcvconnect\n"));
 
     if (h->state == CS_ST_DATAXFER)
@@ -774,10 +761,7 @@ static int tcpip_bind(COMSTACK h, void *address, int mode)
     if (h->type == ssl_type && !sp->session)
     {
         int res;
-        gnutls_global_init();
-
         tcpip_create_cred(h);
-
         res = gnutls_certificate_set_x509_key_file(sp->cred_ptr->xcred,
                                                    sp->cert_fname,
                                                    sp->cert_fname,
@@ -961,6 +945,11 @@ COMSTACK tcpip_accept(COMSTACK h)
         state->complete = st->complete;
 #if HAVE_GETADDRINFO
         state->ai = 0;
+#if RESOLVER_THREAD
+        state->hoststr = 0;
+        state->pipefd[0] = state->pipefd[1] = -1;
+        state->port = 0;
+#endif
 #endif
         cnew->state = CS_ST_ACCEPT;
         h->state = CS_ST_IDLE;
@@ -1366,6 +1355,17 @@ 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()));
+#if HAVE_GETADDRINFO
+#if RESOLVER_THREAD
+    if (sp->pipefd[0] != -1)
+    {
+        yaz_thread_join(&sp->thread_id, 0);
+        close(sp->pipefd[0]);
+        close(sp->pipefd[1]);
+        h->iofile = -1;
+    }
+#endif
+#endif
     if (h->iofile != -1)
     {
 #if HAVE_GNUTLS_H