Extended comstack with outgoing IP YAZ-795
[yaz-moved-to-github.git] / src / tcpip.c
index ab9f6c4..6d00525 100644 (file)
@@ -90,6 +90,12 @@ static int ssl_get(COMSTACK h, char **buf, int *bufsize);
 static int ssl_put(COMSTACK h, char *buf, int size);
 #endif
 
+
+#if HAVE_GETADDRINFO
+struct addrinfo *tcpip_getaddrinfo(const char *str, const char *port,
+                                   int *ipv6_only);
+#endif
+
 static COMSTACK tcpip_accept(COMSTACK h);
 static const char *tcpip_addrstr(COMSTACK h);
 static void *tcpip_straddr(COMSTACK h, const char *str);
@@ -125,6 +131,7 @@ typedef struct tcpip_state
     struct addrinfo *ai;
     struct addrinfo *ai_connect;
     int ipv6_only;
+    char *bind_host;
 #if RESOLVER_THREAD
     int pipefd[2];
     char *hoststr;
@@ -179,6 +186,7 @@ static struct tcpip_state *tcpip_state_create(void)
 #if HAVE_GETADDRINFO
     sp->ai = 0;
     sp->ai_connect = 0;
+    sp->bind_host = 0;
 #if RESOLVER_THREAD
     sp->hoststr = 0;
     sp->pipefd[0] = sp->pipefd[1] = -1;
@@ -243,12 +251,18 @@ COMSTACK tcpip_type(int s, int flags, int protocol, void *vp)
     return p;
 }
 
-COMSTACK yaz_tcpip_create(int s, int flags, int protocol,
-                          const char *connect_host)
+COMSTACK yaz_tcpip_create2(int s, int flags, int protocol,
+                           const char *connect_host,
+                           const char *bind_host)
 {
     COMSTACK p = tcpip_type(s, flags, protocol, 0);
     if (!p)
         return 0;
+    if (bind_host)
+    {
+        tcpip_state *sp = (tcpip_state *) p->cprivate;
+        sp->bind_host = xstrdup(bind_host);
+    }
     if (connect_host)
     {
         tcpip_state *sp = (tcpip_state *) p->cprivate;
@@ -263,6 +277,12 @@ COMSTACK yaz_tcpip_create(int s, int flags, int protocol,
     return p;
 }
 
+COMSTACK yaz_tcpip_create(int s, int flags, int protocol,
+                          const char *connect_host)
+{
+    return yaz_tcpip_create2(s, flags, protocol, connect_host, 0);
+}
+
 #if HAVE_GNUTLS_H
 static void tcpip_create_cred(COMSTACK cs)
 {
@@ -456,6 +476,27 @@ static struct addrinfo *create_net_socket(COMSTACK h)
         return 0;
     if (!tcpip_set_blocking(h, h->flags))
         return 0;
+    if (sp->bind_host)
+    {
+        int r;
+        int ipv6_only = 0;
+        struct addrinfo *ai = tcpip_getaddrinfo(sp->bind_host, "210",
+                                                &ipv6_only);
+        if (!ai)
+            return 0;
+        r = bind(h->iofile, ai->ai_addr, ai->ai_addrlen);
+        if (r)
+        {
+            int xerrno = errno;
+            if (xerrno == EINVAL)
+                fprintf(stderr, "bind returned EINVAL\n");
+            fprintf(stderr, "bind failed errno=%d %s\n", xerrno,
+                    strerror(xerrno));
+            freeaddrinfo(ai);
+            return 0;
+        }
+        freeaddrinfo(ai);
+    }
     return ai;
 }
 
@@ -1325,6 +1366,7 @@ void tcpip_close(COMSTACK h)
 
     TRC(fprintf(stderr, "tcpip_close: h=%p pid=%d\n", h, getpid()));
 #if HAVE_GETADDRINFO
+    xfree(sp->bind_host);
 #if RESOLVER_THREAD
     if (sp->pipefd[0] != -1)
     {