Fixed bug in Generic Frontend Server that could cause a server to stop
[yaz-moved-to-github.git] / src / zoom-c.c
index fe4665a..7a702d2 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright (C) 1995-2006, Index Data ApS
  * See the file LICENSE for details.
  *
- * $Id: zoom-c.c,v 1.84 2006-08-16 14:18:59 adam Exp $
+ * $Id: zoom-c.c,v 1.88 2006-09-14 13:47:57 adam Exp $
  */
 /**
  * \file zoom-c.c
@@ -40,6 +40,9 @@
 #include <sys/select.h>
 #endif
 #ifdef WIN32
+#if FD_SETSIZE < 512
+#define FD_SETSIZE 512
+#endif
 #include <winsock.h>
 #endif
 
@@ -371,6 +374,9 @@ ZOOM_API(void)
     yaz_log(log_api, "%p ZOOM_connection_connect host=%s portnum=%d",
             c, host, portnum);
 
+    set_ZOOM_error(c, ZOOM_ERROR_NONE, 0);
+    ZOOM_connection_remove_tasks(c);
+
     if (c->cs)
     {
         yaz_log(log_details, "%p ZOOM_connection_connect reconnect ok", c);
@@ -472,8 +478,6 @@ ZOOM_API(void)
     c->async = ZOOM_options_get_bool(c->options, "async", 0);
     yaz_log(log_details, "%p ZOOM_connection_connect async=%d", c, c->async);
  
-    set_ZOOM_error(c, ZOOM_ERROR_NONE, 0);
-
     task = ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
 
     if (!c->async)
@@ -1187,23 +1191,28 @@ static zoom_ret ZOOM_connection_send_init(ZOOM_connection c)
     ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
     ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
     ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
-
+    
     /* Index Data's Z39.50 Implementor Id is 81 */
-    ireq->implementationId = odr_prepend(c->odr_out,
-                                         ZOOM_options_get(c->options, "implementationId"),
-                                         odr_prepend(c->odr_out, "81", ireq->implementationId));
-
-    ireq->implementationName = odr_prepend(c->odr_out,
-                                           ZOOM_options_get(c->options, "implementationName"),
-                                           odr_prepend(c->odr_out, "ZOOM-C", ireq->implementationName));
-
-    version = odr_strdup(c->odr_out, "$Revision: 1.84 $");
+    ireq->implementationId =
+        odr_prepend(c->odr_out,
+                    ZOOM_options_get(c->options, "implementationId"),
+                    odr_prepend(c->odr_out, "81", ireq->implementationId));
+    
+    ireq->implementationName = 
+        odr_prepend(c->odr_out,
+                    ZOOM_options_get(c->options, "implementationName"),
+                    odr_prepend(c->odr_out, "ZOOM-C",
+                                ireq->implementationName));
+    
+    version = odr_strdup(c->odr_out, "$Revision: 1.88 $");
     if (strlen(version) > 10)   /* check for unexpanded CVS strings */
         version[strlen(version)-2] = '\0';
-    ireq->implementationVersion = odr_prepend(c->odr_out,
-                                              ZOOM_options_get(c->options, "implementationVersion"),
-                                              odr_prepend(c->odr_out, &version[11], ireq->implementationVersion));
-
+    ireq->implementationVersion = 
+        odr_prepend(c->odr_out,
+                    ZOOM_options_get(c->options, "implementationVersion"),
+                    odr_prepend(c->odr_out, &version[11],
+                                ireq->implementationVersion));
+    
     *ireq->maximumRecordSize =
         ZOOM_options_get_int(c->options, "maximumRecordSize", 1024*1024);
     *ireq->preferredMessageSize =
@@ -1313,7 +1322,7 @@ static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
     int *start, *count;
     ZOOM_resultset resultset = 0;
     Z_SRW_PDU *sr = 0;
-    const char *recordPacking = 0;
+    const char *option_val = 0;
 
     if (c->error)                  /* don't continue on error */
         return zoom_complete;
@@ -1381,12 +1390,14 @@ static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
     sr->u.request->maximumRecords = odr_intdup(
         c->odr_out, resultset->step>0 ? resultset->step : *count);
     sr->u.request->recordSchema = resultset->schema;
-
-    recordPacking = ZOOM_resultset_option_get(resultset, "recordPacking");
-
-    if (recordPacking)
-        sr->u.request->recordPacking = odr_strdup(c->odr_out, recordPacking);
     
+    option_val = ZOOM_resultset_option_get(resultset, "recordPacking");
+    if (option_val)
+        sr->u.request->recordPacking = odr_strdup(c->odr_out, option_val);
+
+    option_val = ZOOM_resultset_option_get(resultset, "extraArgs");
+    if (option_val)
+        sr->extra_args = odr_strdup(c->odr_out, option_val);
     return send_srw(c, sr);
 }
 #else
@@ -3415,6 +3426,7 @@ static void recv_apdu(ZOOM_connection c, Z_APDU *apdu)
         if (c->reconnect_ok)
         {
             do_close(c);
+            c->reconnect_ok = 0;
             c->tasks->running = 0;
             ZOOM_connection_insert_task(c, ZOOM_TASK_CONNECT);
         }
@@ -3600,9 +3612,9 @@ static int do_read(ZOOM_connection c)
     {
         if (c->reconnect_ok)
         {
+            yaz_log(log_details, "%p do_read reconnect read", c);
             do_close(c);
             c->reconnect_ok = 0;
-            yaz_log(log_details, "%p do_read reconnect read", c);
             c->tasks->running = 0;
             ZOOM_connection_insert_task(c, ZOOM_TASK_CONNECT);
         }
@@ -3665,7 +3677,6 @@ static zoom_ret do_write_ex(ZOOM_connection c, char *buf_out, int len_out)
         {
             do_close(c);
             c->reconnect_ok = 0;
-            yaz_log(log_details, "%p do_write_ex reconnect write", c);
             c->tasks->running = 0;
             ZOOM_connection_insert_task(c, ZOOM_TASK_CONNECT);
             return zoom_pending;
@@ -3846,13 +3857,22 @@ ZOOM_API(int)
     return ZOOM_connection_error_x(c, cp, addinfo, 0);
 }
 
-static int ZOOM_connection_do_io(ZOOM_connection c, int mask)
+static void ZOOM_connection_do_io(ZOOM_connection c, int mask)
 {
     ZOOM_Event event = 0;
     int r = cs_look(c->cs);
     yaz_log(log_details, "%p ZOOM_connection_do_io mask=%d cs_look=%d",
             c, mask, r);
     
+    if (mask & ZOOM_SELECT_EXCEPT)
+    {
+        if (r == CS_CONNECT)
+            set_ZOOM_error(c, ZOOM_ERROR_CONNECT, c->host_port);
+        else
+            set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
+        do_close(c);
+        return;
+    }
     if (r == CS_NONE)
     {
         event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
@@ -3862,10 +3882,7 @@ static int ZOOM_connection_do_io(ZOOM_connection c, int mask)
     }
     else if (r == CS_CONNECT)
     {
-        int ret;
-        event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
-
-        ret = cs_rcvconnect(c->cs);
+        int ret = ret = cs_rcvconnect(c->cs);
         yaz_log(log_details, "%p ZOOM_connection_do_io "
                 "cs_rcvconnect returned %d", c, ret);
         if (ret == 1)
@@ -3875,10 +3892,10 @@ static int ZOOM_connection_do_io(ZOOM_connection c, int mask)
                 c->mask += ZOOM_SELECT_WRITE;
             if (c->cs->io_pending & CS_WANT_READ)
                 c->mask += ZOOM_SELECT_READ;
-            ZOOM_connection_put_event(c, event);
         }
         else if (ret == 0)
         {
+            event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
             ZOOM_connection_put_event(c, event);
             get_cert(c);
             if (c->proto == PROTO_Z3950)
@@ -3897,7 +3914,6 @@ static int ZOOM_connection_do_io(ZOOM_connection c, int mask)
         {
             set_ZOOM_error(c, ZOOM_ERROR_CONNECT, c->host_port);
             do_close(c);
-            ZOOM_connection_put_event(c, event);
         }
     }
     else
@@ -3907,7 +3923,6 @@ static int ZOOM_connection_do_io(ZOOM_connection c, int mask)
         if (c->cs && (mask & ZOOM_SELECT_WRITE))
             do_write(c);
     }
-    return 1;
 }
 
 ZOOM_API(int)