Remove assignment to variables not in use.
[yaz-moved-to-github.git] / src / tcpip.c
index add2d32..b9466d6 100644 (file)
@@ -1,11 +1,14 @@
 /* This file is part of the YAZ toolkit.
- * Copyright (C) 1995-2008 Index Data
+ * Copyright (C) 1995-2011 Index Data
  * See the file LICENSE for details.
  */
 /**
  * \file tcpip.c
  * \brief Implements TCP/IP + SSL COMSTACK.
  */
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
 
 #include <stdio.h>
 #include <string.h>
@@ -68,9 +71,9 @@
 
 #include <yaz/comstack.h>
 #include <yaz/tcpip.h>
-#include <yaz/nmem.h>
+#include <yaz/errno.h>
 
-static int tcpip_close(COMSTACK h);
+static void tcpip_close(COMSTACK h);
 static int tcpip_put(COMSTACK h, char *buf, int size);
 static int tcpip_get(COMSTACK h, char **buf, int *bufsize);
 static int tcpip_put_connect(COMSTACK h, char *buf, int size);
@@ -90,7 +93,7 @@ static int ssl_put(COMSTACK h, char *buf, int size);
 #endif
 
 static COMSTACK tcpip_accept(COMSTACK h);
-static char *tcpip_addrstr(COMSTACK h);
+static const char *tcpip_addrstr(COMSTACK h);
 static void *tcpip_straddr(COMSTACK h, const char *str);
 
 #if 0
@@ -206,7 +209,6 @@ COMSTACK tcpip_type(int s, int flags, int protocol, void *vp)
     p->state = s < 0 ? CS_ST_UNBND : CS_ST_IDLE; /* state of line */
     p->event = CS_NONE;
     p->cerrno = 0;
-    p->stackerr = 0;
     p->user = 0;
 
 #if HAVE_GNUTLS_H
@@ -364,6 +366,13 @@ struct addrinfo *tcpip_getaddrinfo(const char *str, const char *port)
     if (!strcmp("@", host))
     {
         hints.ai_flags = AI_PASSIVE;
+        hints.ai_family = AF_INET;
+        error = getaddrinfo(0, port, &hints, &res);
+    }
+    else if (!strcmp("@6", host))
+    {
+        hints.ai_flags = AI_PASSIVE;
+        hints.ai_family = AF_INET6;
         error = getaddrinfo(0, port, &hints, &res);
     }
     else
@@ -418,12 +427,12 @@ int tcpip_strtoaddr_ex(const char *str, struct sockaddr_in *add,
     return 1;
 }
 
-
 #if HAVE_GETADDRINFO
 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)
         port = "80";
     if (!tcpip_init())
@@ -435,8 +444,7 @@ 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)
+        for (ai = sp->ai; ai; ai = ai->ai_next)
         {
             s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
             if (s != -1)
@@ -444,12 +452,13 @@ void *tcpip_straddr(COMSTACK h, const char *str)
         }
         if (s == -1)
             return 0;
+        assert(ai);
         h->iofile = s;
         
         if (!tcpip_set_blocking(h, h->flags))
             return 0;
     }
-    return sp->ai;
+    return ai;
 }
 #else
 void *tcpip_straddr(COMSTACK h, const char *str)
@@ -493,15 +502,12 @@ int tcpip_more(COMSTACK h)
 int tcpip_connect(COMSTACK h, void *address)
 {
 #if HAVE_GETADDRINFO
+    struct addrinfo *ai = (struct addrinfo *) address;
     tcpip_state *sp = (tcpip_state *)h->cprivate;
 #else
     struct sockaddr_in *add = (struct sockaddr_in *) address;
 #endif
     int r;
-#ifdef __sun__
-    int recbuflen;
-    YAZ_SOCKLEN_T rbufsize = sizeof(recbuflen);
-#endif
     TRC(fprintf(stderr, "tcpip_connect\n"));
     h->io_pending = 0;
     if (h->state != CS_ST_UNBND)
@@ -510,41 +516,7 @@ int tcpip_connect(COMSTACK h, void *address)
         return -1;
     }
 #if HAVE_GETADDRINFO
-    if (sp->ai != (struct addrinfo *) address)
-    {
-        h->cerrno = CSOUTSTATE;
-        return -1;
-    }
-#endif
-#ifdef __sun__
-    /* On Suns, you must set a bigger Receive Buffer BEFORE a call to connect
-     * This gives the connect a chance to negotiate with the other side
-     * (see 'man tcp') 
-     */
-    if (getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) < 0 )
-    {
-        h->cerrno = CSYSERR;
-        return -1;
-    }
-    TRC(fprintf( stderr, "Current Size of TCP Receive Buffer= %d\n",
-                 recbuflen ));
-    recbuflen *= 10; /* lets be optimistic */
-    if (setsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, rbufsize ) < 0 )
-    {
-        h->cerrno = CSYSERR;
-        return -1;
-    }
-    if (getsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, &rbufsize ) )
-    {
-        h->cerrno = CSYSERR;
-        return -1;
-    }
-    TRC(fprintf(stderr, "New Size of TCP Receive Buffer = %d\n",
-                recbuflen ));
-#endif
-
-#if HAVE_GETADDRINFO
-    r = connect(h->iofile, sp->ai->ai_addr, sp->ai->ai_addrlen);
+    r = connect(h->iofile, ai->ai_addr, ai->ai_addrlen);
     freeaddrinfo(sp->ai);
     sp->ai = 0;
 #else
@@ -608,8 +580,10 @@ int tcpip_rcvconnect(COMSTACK h)
         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)
         {
@@ -657,32 +631,12 @@ int tcpip_rcvconnect(COMSTACK h)
 #define CERTF "ztest.pem"
 #define KEYF "ztest.pem"
 
-static void tcpip_setsockopt(int fd)
-{
-#if 0
-    int len = 4096;
-    int set = 1;
-    
-    if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char*)&set, sizeof(int)))
-    {
-        yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt TCP_NODELAY");
-    }
-    if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char*)&len, sizeof(int)))
-    {
-        yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt SNDBUF");
-    }
-    if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char*)&len, sizeof(int)))
-    {
-        yaz_log(LOG_WARN|LOG_ERRNO, "setsockopt RCVBUF");
-    }
-#endif
-}
-
 static int tcpip_bind(COMSTACK h, void *address, int mode)
 {
     int r;
     tcpip_state *sp = (tcpip_state *)h->cprivate;
-#if HAVE_GETADDRINFO
+#if HAVE_GETADDRINFO 
+    struct addrinfo *ai = (struct addrinfo *) address;   
 #else
     struct sockaddr *addr = (struct sockaddr *)address;
 #endif
@@ -692,14 +646,6 @@ static int tcpip_bind(COMSTACK h, void *address, int mode)
     int one = 1;
 #endif
 
-#if HAVE_GETADDRINFO
-    if (sp->ai != (struct addrinfo *) address)
-    {
-        h->cerrno = CSOUTSTATE;
-        return -1;
-    }
-#endif
-
 #if HAVE_GNUTLS_H
     if (h->type == ssl_type && !sp->session)
     {
@@ -774,9 +720,8 @@ static int tcpip_bind(COMSTACK h, void *address, int mode)
         return -1;
     }
 #endif
-    tcpip_setsockopt(h->iofile);
 #if HAVE_GETADDRINFO
-    r = bind(h->iofile, sp->ai->ai_addr, sp->ai->ai_addrlen);
+    r = bind(h->iofile, ai->ai_addr, ai->ai_addrlen);
     freeaddrinfo(sp->ai);
     sp->ai = 0;
 #else
@@ -802,8 +747,12 @@ int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
                  int (*check_ip)(void *cd, const char *a, int len, int t),
                  void *cd)
 {
+#ifdef WIN32
+    /* we don't get peer address on Windows (via accept) */
+#else
     struct sockaddr_in addr;
     YAZ_SOCKLEN_T len = sizeof(addr);
+#endif
 
     TRC(fprintf(stderr, "tcpip_listen pid=%d\n", getpid()));
     if (h->state != CS_ST_IDLE)
@@ -811,7 +760,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 (
@@ -839,6 +792,10 @@ int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
         }
         return -1;
     }
+#ifdef WIN32
+    if (addrlen)
+        *addrlen = 0;
+#else
     if (addrlen && (size_t) (*addrlen) >= sizeof(struct sockaddr_in))
         memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in));
     else if (addrlen)
@@ -855,8 +812,8 @@ int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
         h->newfd = -1;
         return -1;
     }
+#endif
     h->state = CS_ST_INCON;
-    tcpip_setsockopt(h->newfd);
     return 0;
 }
 
@@ -959,8 +916,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;
@@ -1044,8 +1003,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;
@@ -1150,8 +1109,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)
@@ -1175,8 +1134,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;
@@ -1234,8 +1193,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)
@@ -1368,7 +1327,7 @@ int ssl_put(COMSTACK h, char *buf, int size)
 }
 #endif
 
-int tcpip_close(COMSTACK h)
+void tcpip_close(COMSTACK h)
 {
     tcpip_state *sp = (struct tcpip_state *)h->cprivate;
 
@@ -1428,10 +1387,9 @@ int tcpip_close(COMSTACK h)
     xfree(sp->connect_response_buf);
     xfree(sp);
     xfree(h);
-    return 0;
 }
 
-char *tcpip_addrstr(COMSTACK h)
+const char *tcpip_addrstr(COMSTACK h)
 {
     tcpip_state *sp = (struct tcpip_state *)h->cprivate;
     char *r = 0, *buf = sp->buf;
@@ -1566,47 +1524,44 @@ void cs_print_session_info(COMSTACK cs)
 void *cs_get_ssl(COMSTACK cs)
 {
 #if HAVE_OPENSSL_SSL_H
-    struct tcpip_state *sp;
-    if (!cs || cs->type != ssl_type)
-        return 0;
-    sp = (struct tcpip_state *) cs->cprivate;
-    return sp->ssl;  
-#else
-    return 0;
+    if (cs && cs->type == ssl_type)
+    {
+        struct tcpip_state *sp = (struct tcpip_state *) cs->cprivate;
+        return sp->ssl;  
+    }
 #endif
+    return 0;
 }
 
 int cs_set_ssl_ctx(COMSTACK cs, void *ctx)
 {
 #if ENABLE_SSL
-    struct tcpip_state *sp;
-    if (!cs || cs->type != ssl_type)
-        return 0;
-    sp = (struct tcpip_state *) cs->cprivate;
+    if (cs && cs->type == ssl_type)
+    {
 #if HAVE_OPENSSL_SSL_H
-    if (sp->ctx_alloc)
-        return 0;
-    sp->ctx = (SSL_CTX *) ctx;
+        struct tcpip_state *sp = (struct tcpip_state *) cs->cprivate;
+        if (sp->ctx_alloc)
+            return 0;
+        sp->ctx = (SSL_CTX *) ctx;
 #endif
-    return 1;
-#else
-    return 0;
+        return 1;
+    }
 #endif
+    return 0;
 }
 
 int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
 {
 #if ENABLE_SSL
-    struct tcpip_state *sp;
-    if (!cs || cs->type != ssl_type)
-        return 0;
-    sp = (struct tcpip_state *) cs->cprivate;
-    strncpy(sp->cert_fname, fname, sizeof(sp->cert_fname)-1);
-    sp->cert_fname[sizeof(sp->cert_fname)-1] = '\0';
-    return 1;
-#else
-    return 0;
+    if (cs && cs->type == ssl_type)
+    {
+        struct tcpip_state *sp = (struct tcpip_state *) cs->cprivate;
+        strncpy(sp->cert_fname, fname, sizeof(sp->cert_fname)-1);
+        sp->cert_fname[sizeof(sp->cert_fname)-1] = '\0';
+        return 1;
+    }
 #endif
+    return 0;
 }
 
 int cs_get_peer_certificate_x509(COMSTACK cs, char **buf, int *len)
@@ -1667,6 +1622,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