SSL/COMSTACK updates. cs_rcvconnect completes SSL handshake.
authorAdam Dickmeiss <adam@indexdata.dk>
Mon, 21 Jan 2002 21:50:32 +0000 (21:50 +0000)
committerAdam Dickmeiss <adam@indexdata.dk>
Mon, 21 Jan 2002 21:50:32 +0000 (21:50 +0000)
CHANGELOG
comstack/tcpip.c
zoom/zoom-c.c

index 52da69b..ec28a46 100644 (file)
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -2,6 +2,9 @@ Possible compatibility problems with earlier versions marked with '*'.
 
 --- 1.8.5 2002/XX/XX
 
+SSL/COMSTACK updates. cs_rcvconnect completes SSL handshake. ZOOM
+works with SSL.
+
 Configure option --with-openssl=<dir> now works when a directory is
 specified. Patch provided by Morten Bøgeskov.
 
index 22a8ae5..311b6e2 100644 (file)
@@ -1,8 +1,8 @@
 /*
- * Copyright (c) 1995-2001, Index Data
+ * Copyright (c) 1995-2002, Index Data
  * See the file LICENSE for details.
  *
- * $Id: tcpip.c,v 1.44 2001-11-06 17:01:25 adam Exp $
+ * $Id: tcpip.c,v 1.45 2002-01-21 21:50:32 adam Exp $
  */
 
 #include <stdio.h>
@@ -275,40 +275,56 @@ int tcpip_connect(COMSTACK h, void *address)
 {
     struct sockaddr_in *add = (struct sockaddr_in *)address;
 #if HAVE_OPENSSL_SSL_H
-       tcpip_state *sp = (tcpip_state *)h->cprivate;
+    tcpip_state *sp = (tcpip_state *)h->cprivate;
 #endif
     int r;
 
     TRC(fprintf(stderr, "tcpip_connect\n"));
     h->io_pending = 0;
-    if (h->state == CS_ST_UNBND)
+    if (h->state != CS_ST_UNBND)
+    {
+        h->cerrno = CSOUTSTATE;
+       return -1;
+    }
+    r = connect(h->iofile, (struct sockaddr *) add, sizeof(*add));
+    if (r < 0)
     {
-       r = connect(h->iofile, (struct sockaddr *) add, sizeof(*add));
-       if (r < 0)
-       {
 #ifdef WIN32
-           if (WSAGetLastError() == WSAEWOULDBLOCK)
-           {
-               h->event = CS_CONNECT;
-               h->state = CS_ST_CONNECTING;
-               h->io_pending = CS_WANT_WRITE;
-               return 1;
-           }
+        if (WSAGetLastError() == WSAEWOULDBLOCK)
+        {
+            h->event = CS_CONNECT;
+            h->state = CS_ST_CONNECTING;
+            h->io_pending = CS_WANT_WRITE;
+            return 1;
+        }
 #else
-           if (errno == EINPROGRESS)
-           {
-               h->event = CS_CONNECT;
-               h->state = CS_ST_CONNECTING;
-               h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
-               return 1;
-           }
+        if (errno == EINPROGRESS)
+        {
+            h->event = CS_CONNECT;
+            h->state = CS_ST_CONNECTING;
+            h->io_pending = CS_WANT_WRITE|CS_WANT_READ;
+            return 1;
+        }
 #endif
-           h->cerrno = CSYSERR;
-           return -1;
-       }
-       h->event = CS_CONNECT;
-       h->state = CS_ST_CONNECTING;
+        h->cerrno = CSYSERR;
+        return -1;
     }
+    h->event = CS_CONNECT;
+    h->state = CS_ST_CONNECTING;
+
+    return tcpip_rcvconnect (h);
+}
+
+/*
+ * nop
+ */
+int tcpip_rcvconnect(COMSTACK h)
+{
+    tcpip_state *sp = (tcpip_state *)h->cprivate;
+    TRC(fprintf(stderr, "tcpip_rcvconnect\n"));
+
+    if (h->state == CS_ST_DATAXFER)
+        return 0;
     if (h->state != CS_ST_CONNECTING)
     {
         h->cerrno = CSOUTSTATE;
@@ -330,13 +346,11 @@ int tcpip_connect(COMSTACK h, void *address)
            int err = SSL_get_error(sp->ssl, res);
            if (err == SSL_ERROR_WANT_READ)
            {
-               yaz_log (LOG_LOG, "SSL_connect. want_read");
                h->io_pending = CS_WANT_READ;
                return 1;
            }
            if (err == SSL_ERROR_WANT_WRITE)
            {
-               yaz_log (LOG_LOG, "SSL_connect. want_write");
                h->io_pending = CS_WANT_WRITE;
                return 1;
            }
@@ -350,45 +364,6 @@ int tcpip_connect(COMSTACK h, void *address)
     return 0;
 }
 
-/*
- * nop
- */
-int tcpip_rcvconnect(COMSTACK cs)
-{
-    TRC(fprintf(stderr, "tcpip_rcvconnect\n"));
-
-    if (cs->event == CS_CONNECT)
-    {
-       int fd = cs->iofile;
-       fd_set input, output;
-       struct timeval tv;
-       int r;
-       
-       tv.tv_sec = 0;
-       tv.tv_usec = 1;
-       
-       FD_ZERO(&input);
-       FD_ZERO(&output);
-       FD_SET (fd, &input);
-       FD_SET (fd, &output);
-       
-       r = select (fd+1, &input, &output, 0, &tv);
-       if (r > 0)
-       {
-           if (FD_ISSET(cs->iofile, &output))
-           {
-               cs->event = CS_DATA;
-               return 0;   /* write OK, we're OK */
-           }
-           else
-               return -1;  /* an error, for sure */
-       }
-       else if (r == 0)
-           return 0;  /* timeout - incomplete */
-    }
-    return -1;    /* wrong state or bad select */
-}
-
 #define CERTF "ztest.pem"
 #define KEYF "ztest.pem"
 
@@ -614,13 +589,11 @@ COMSTACK tcpip_accept(COMSTACK h)
                if (err == SSL_ERROR_WANT_READ)
                {
                    h->io_pending = CS_WANT_READ;
-                   yaz_log (LOG_LOG, "SSL_accept. want_read");
                    return h;
                }
                if (err == SSL_ERROR_WANT_WRITE)
                {
                    h->io_pending = CS_WANT_WRITE;
-                   yaz_log (LOG_LOG, "SSL_accept. want_write");
                    return h;
                }
                cs_close (h);
@@ -784,13 +757,11 @@ int ssl_get(COMSTACK h, char **buf, int *bufsize)
            if (ssl_err == SSL_ERROR_WANT_READ)
            {
                h->io_pending = CS_WANT_READ;
-               yaz_log (LOG_LOG, "SSL_read. want_read");
                break;
            }
            if (ssl_err == SSL_ERROR_WANT_WRITE)
            {
                h->io_pending = CS_WANT_WRITE;
-               yaz_log (LOG_LOG, "SSL_read. want_write");
                break;
            }
            if (res == 0)
@@ -925,13 +896,11 @@ int ssl_put(COMSTACK h, char *buf, int size)
            if (ssl_err == SSL_ERROR_WANT_READ)
            {
                h->io_pending = CS_WANT_READ;
-               yaz_log (LOG_LOG, "SSL_write. want_read");
                return 1;
            }
            if (ssl_err == SSL_ERROR_WANT_WRITE)
            {
                h->io_pending = CS_WANT_WRITE;
-               yaz_log (LOG_LOG, "SSL_write. want_write");
                return 1;
            }
            h->cerrno = CSERRORSSL;
index b36d9b2..ddca641 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: zoom-c.c,v 1.20 2002-01-09 12:44:31 adam Exp $
+ * $Id: zoom-c.c,v 1.21 2002-01-21 21:50:32 adam Exp $
  *
  * ZOOM layer for C, connections, result sets, queries.
  */
@@ -32,7 +32,6 @@ static void ZOOM_Event_destroy (ZOOM_Event event)
 
 static void ZOOM_connection_put_event (ZOOM_connection c, ZOOM_Event event)
 {
-    // put in back of queue
     if (c->m_queue_back)
     {
        c->m_queue_back->prev = event;
@@ -50,7 +49,6 @@ static void ZOOM_connection_put_event (ZOOM_connection c, ZOOM_Event event)
 
 static ZOOM_Event ZOOM_connection_get_event(ZOOM_connection c)
 {
-    // get from front of queue
     ZOOM_Event event = c->m_queue_front;
     if (!event)
        return 0;
@@ -518,8 +516,11 @@ static int do_connect (ZOOM_connection c)
        if (ret >= 0)
        {
            c->state = STATE_CONNECTING; 
-           c->mask = ZOOM_SELECT_READ | ZOOM_SELECT_WRITE | 
-                ZOOM_SELECT_EXCEPT;
+            c->mask = ZOOM_SELECT_EXCEPT;
+            if (c->cs->io_pending & CS_WANT_WRITE)
+                c->mask += ZOOM_SELECT_WRITE;
+            if (c->cs->io_pending & CS_WANT_READ)
+                c->mask += ZOOM_SELECT_READ;
            return 1;
        }
     }
@@ -1557,14 +1558,18 @@ static int do_write_ex (ZOOM_connection c, char *buf_out, int len_out)
        return 1;
     }
     else if (r == 1)
-    {
-       c->state = STATE_ESTABLISHED;
-       c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_WRITE|ZOOM_SELECT_EXCEPT;
+    {    
+        c->mask = ZOOM_SELECT_EXCEPT;
+        if (c->cs->io_pending & CS_WANT_WRITE)
+            c->mask += ZOOM_SELECT_WRITE;
+        if (c->cs->io_pending & CS_WANT_READ)
+            c->mask += ZOOM_SELECT_READ;
+        yaz_log (LOG_DEBUG, "do_write_ex 1 mask=%d", c->mask);
     }
     else
     {
-       c->state = STATE_ESTABLISHED;
-       c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT;
+        c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT;
+        yaz_log (LOG_DEBUG, "do_write_ex 2 mask=%d", c->mask);
     }
     return 0;
 }
@@ -1660,73 +1665,53 @@ int ZOOM_connection_error (ZOOM_connection c, const char **cp,
 int ZOOM_connection_do_io(ZOOM_connection c, int mask)
 {
     ZOOM_Event event = 0;
-#if 0
     int r = cs_look(c->cs);
-    yaz_log (LOG_LOG, "ZOOM_connection_do_io c=%p mask=%d cs_look=%d",
+    yaz_log (LOG_DEBUG, "ZOOM_connection_do_io c=%p mask=%d cs_look=%d",
             c, mask, r);
     
     if (r == CS_NONE)
     {
-        event = ZOOM_Event_create (ZOOM_EVENT_IO_CONNECT);
+        event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
        c->error = ZOOM_ERROR_CONNECT;
        do_close (c);
         ZOOM_connection_put_event (c, event);
     }
     else if (r == CS_CONNECT)
     {
-        event = ZOOM_Event_create (ZOOM_EVENT_IO_CONNECT);
-       yaz_log (LOG_LOG, "calling rcvconnect");
-       if (cs_rcvconnect (c->cs) < 0)
-       {
-           c->error = ZOOM_ERROR_CONNECT;
-           do_close (c);
-            ZOOM_connection_put_event (c, event);
-       }
-       else
+        int ret;
+        event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
+
+        ret = cs_rcvconnect (c->cs);
+        yaz_log (LOG_DEBUG, "cs_rcvconnect returned %d", ret);
+        if (ret == 1)
         {
+            c->mask = ZOOM_SELECT_EXCEPT;
+            if (c->cs->io_pending & CS_WANT_WRITE)
+                c->mask += ZOOM_SELECT_WRITE;
+            if (c->cs->io_pending & CS_WANT_READ)
+                c->mask += ZOOM_SELECT_READ;
             ZOOM_connection_put_event (c, event);
-           ZOOM_connection_send_init (c);
         }
-    }
-    else
-    {
-       if (mask & ZOOM_SELECT_READ)
-           do_read (c);
-       if (c->cs && (mask & ZOOM_SELECT_WRITE))
-           do_write (c);
-    }  
-#else
-    yaz_log (LOG_DEBUG, "ZOOM_connection_do_io c=%p mask=%d", c, mask);
-    if (c->state == STATE_CONNECTING)
-    {
-        event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
-       if (mask & ZOOM_SELECT_WRITE)
+        else if (ret == 0)
         {
             ZOOM_connection_put_event (c, event);
-           ZOOM_connection_send_init (c);
+            ZOOM_connection_send_init (c);
+            c->state = STATE_ESTABLISHED;
         }
-       else
-       {
-           c->error = ZOOM_ERROR_CONNECT;
-           do_close (c);
+        else
+        {
+            c->error = ZOOM_ERROR_CONNECT;
+            do_close (c);
             ZOOM_connection_put_event (c, event);
-       }
-    }
-    else if (c->state == STATE_ESTABLISHED)
-    {
-       if (mask & ZOOM_SELECT_READ)
-           do_read (c);
-       if (c->cs && (mask & ZOOM_SELECT_WRITE))
-           do_write (c);
+        }
     }
     else
     {
-        event = ZOOM_Event_create (ZOOM_EVENT_UNKNOWN);
-        ZOOM_connection_put_event (c, event);
-       c->error = ZOOM_ERROR_INTERNAL;
-       do_close (c);
+        if (mask & ZOOM_SELECT_READ)
+            do_read (c);
+        if (c->cs && (mask & ZOOM_SELECT_WRITE))
+            do_write (c);
     }
-#endif
     return 1;
 }
 
@@ -1836,9 +1821,7 @@ int ZOOM_event (int no, ZOOM_connection *cs)
     if (!nfds)
         return 0;
 #if HAVE_SYS_POLL_H
-    yaz_log (LOG_DEBUG, "poll start");
     r = poll (pollfds, nfds, 15000);
-    yaz_log (LOG_DEBUG, "poll stop, returned r=%d", r);
     for (i = 0; i<nfds; i++)
     {
         ZOOM_connection c = poll_cs[i];