SSL comstack support. Separate POSIX thread support library.
[yaz-moved-to-github.git] / server / seshigh.c
index 8923b41..18f604b 100644 (file)
@@ -3,7 +3,26 @@
  * See the file LICENSE for details.
  *
  * $Log: seshigh.c,v $
- * Revision 1.105  2000-07-06 10:38:47  adam
+ * Revision 1.111  2000-11-23 10:58:32  adam
+ * SSL comstack support. Separate POSIX thread support library.
+ *
+ * Revision 1.110  2000/10/02 13:05:32  adam
+ * Fixed bug introduced by previous commit.
+ *
+ * Revision 1.109  2000/10/02 11:07:44  adam
+ * Added peer_name member for bend_init handler. Changed the YAZ
+ * client so that tcp: can be avoided in target spec.
+ *
+ * Revision 1.108  2000/09/04 08:58:15  adam
+ * Added prefix yaz_ for most logging utility functions.
+ *
+ * Revision 1.107  2000/08/31 10:20:12  adam
+ * Added member request_format and output_format for backend fetch method.
+ *
+ * Revision 1.106  2000/08/31 09:51:25  adam
+ * Added record_syntax member for fetch method (raw OID).
+ *
+ * Revision 1.105  2000/07/06 10:38:47  adam
  * Enhanced option --enable-tcpd.
  *
  * Revision 1.104  2000/04/05 07:39:55  adam
@@ -419,6 +438,9 @@ association *create_association(IOCHAN channel, COMSTACK link)
     anew->init = 0;
     anew->client_chan = channel;
     anew->client_link = link;
+    anew->cs_get_mask = 0;
+    anew->cs_put_mask = 0;
+    anew->cs_accept_mask = 0;
     if (!(anew->decode = odr_createmem(ODR_DECODE)) ||
        !(anew->encode = odr_createmem(ODR_ENCODE)))
        return 0;
@@ -562,12 +584,37 @@ void ir_session(IOCHAN h, int event)
        }
        return;
     }
-    if (event & EVENT_INPUT || event & EVENT_WORK) /* input */
+    if (event & assoc->cs_accept_mask)
     {
-       if (event & EVENT_INPUT)
+       yaz_log (LOG_DEBUG, "ir_session (accept)");
+       if (!cs_accept (conn))
+       {
+           yaz_log (LOG_LOG, "accept failed");
+           destroy_association(assoc);
+           iochan_destroy(h);
+       }
+       iochan_clearflag (h, EVENT_OUTPUT|EVENT_OUTPUT);
+       if (conn->io_pending) 
+       {   /* cs_accept didn't complete */
+           assoc->cs_accept_mask = 
+               ((conn->io_pending & CS_WANT_WRITE) ? EVENT_OUTPUT : 0) |
+               ((conn->io_pending & CS_WANT_READ) ? EVENT_INPUT : 0);
+
+           iochan_setflag (h, assoc->cs_accept_mask);
+       }
+       else
+       {   /* cs_accept completed. Prepare for reading (cs_get) */
+           assoc->cs_accept_mask = 0;
+           assoc->cs_get_mask = EVENT_INPUT;
+           iochan_setflag (h, assoc->cs_get_mask);
+       }
+       return;
+    }
+    if ((event & assoc->cs_get_mask) || (event & EVENT_WORK)) /* input */
+    {
+       if ((assoc->cs_put_mask & EVENT_INPUT) == 0 && (event & assoc->cs_get_mask))
        {
            yaz_log(LOG_DEBUG, "ir_session (input)");
-           assert(assoc && conn);
            /* We aren't speaking to this fellow */
            if (assoc->state == ASSOC_DEAD)
            {
@@ -577,6 +624,7 @@ void ir_session(IOCHAN h, int event)
                iochan_destroy(h);
                return;
            }
+           assoc->cs_get_mask = EVENT_INPUT;
            if ((res = cs_get(conn, &assoc->input_buffer,
                &assoc->input_buffer_len)) <= 0)
            {
@@ -587,7 +635,12 @@ void ir_session(IOCHAN h, int event)
                return;
            }
            else if (res == 1) /* incomplete read - wait for more  */
+           {
+               if (conn->io_pending & CS_WANT_WRITE)
+                   assoc->cs_get_mask |= EVENT_OUTPUT;
+               iochan_setflag(h, assoc->cs_get_mask);
                return;
+           }
            if (cs_more(conn)) /* more stuff - call us again later, please */
                iochan_setevent(h, EVENT_INPUT);
                
@@ -602,7 +655,7 @@ void ir_session(IOCHAN h, int event)
                    odr_errmsg(odr_geterror(assoc->decode)),
                    odr_offset(assoc->decode));
                yaz_log(LOG_LOG, "PDU dump:");
-               odr_dumpBER(log_file(), assoc->input_buffer, res);
+               odr_dumpBER(yaz_log_file(), assoc->input_buffer, res);
                do_close(assoc, Z_Close_protocolError, "Malformed package");
                return;
            }
@@ -626,34 +679,43 @@ void ir_session(IOCHAN h, int event)
                do_close_req(assoc, Z_Close_systemProblem, msg, req);
        }
     }
-    if (event & EVENT_OUTPUT)
+    if (event & assoc->cs_put_mask)
     {
        request *req = request_head(&assoc->outgoing);
 
+       assoc->cs_put_mask = 0;
        yaz_log(LOG_DEBUG, "ir_session (output)");
        req->state = REQUEST_PENDING;
        switch (res = cs_put(conn, req->response, req->len_response))
        {
-           case -1:
-               yaz_log(LOG_LOG, "Connection closed by client");
-               cs_close(conn);
-               destroy_association(assoc);
-               iochan_destroy(h);
-               break;
-           case 0: /* all sent - release the request structure */
-               yaz_log(LOG_DEBUG, "Wrote PDU, %d bytes", req->len_response);
-               nmem_destroy(req->request_mem);
-               request_deq(&assoc->outgoing);
-               request_release(req);
-               if (!request_head(&assoc->outgoing))
-                   iochan_clearflag(h, EVENT_OUTPUT);
-               break;
-           /* value of 1 -- partial send -- is simply ignored */
+       case -1:
+           yaz_log(LOG_LOG, "Connection closed by client");
+           cs_close(conn);
+           destroy_association(assoc);
+           iochan_destroy(h);
+           break;
+       case 0: /* all sent - release the request structure */
+           yaz_log(LOG_DEBUG, "Wrote PDU, %d bytes", req->len_response);
+           nmem_destroy(req->request_mem);
+           request_deq(&assoc->outgoing);
+           request_release(req);
+           if (!request_head(&assoc->outgoing))
+           {   /* restore mask for cs_get operation ... */
+               iochan_clearflag(h, EVENT_OUTPUT|EVENT_INPUT);
+               iochan_setflag(h, assoc->cs_get_mask);
+           }
+           break;
+       default:
+           if (conn->io_pending & CS_WANT_WRITE)
+               assoc->cs_put_mask |= EVENT_OUTPUT;
+           if (conn->io_pending & CS_WANT_READ)
+               assoc->cs_put_mask |= EVENT_INPUT;
+           iochan_setflag(h, assoc->cs_put_mask);
        }
     }
     if (event & EVENT_EXCEPT)
     {
-       yaz_log(LOG_DEBUG, "ir_session (exception)");
+       yaz_log(LOG_LOG, "ir_session (exception)");
        cs_close(conn);
        destroy_association(assoc);
        iochan_destroy(h);
@@ -832,6 +894,7 @@ static int process_response(association *assoc, request *req, Z_APDU *res)
     request_enq(&assoc->outgoing, req);
     /* turn the work over to the ir_session handler */
     iochan_setflag(assoc->client_chan, EVENT_OUTPUT);
+    assoc->cs_put_mask = EVENT_OUTPUT;
     /* Is there more work to be done? give that to the input handler too */
 #if 1
     if (request_head(&assoc->incoming))
@@ -884,6 +947,9 @@ static Z_APDU *process_initRequest(association *assoc, request *reqb)
     assoc->init->bend_scan = NULL;
     assoc->init->bend_segment = NULL;
     assoc->init->bend_fetch = NULL;
+    
+    assoc->init->peer_name =
+       odr_strdup (assoc->encode, cs_addrstr(assoc->client_link));
     if (!(binitres = (*cb->bend_init)(assoc->init)))
     {
        yaz_log(LOG_WARN, "Bad response from backend.");
@@ -973,6 +1039,12 @@ static Z_APDU *process_initRequest(association *assoc, request *reqb)
     assoc->preferredMessageSize = *req->preferredMessageSize;
     if (assoc->preferredMessageSize > assoc->maximumRecordSize)
        assoc->preferredMessageSize = assoc->maximumRecordSize;
+
+#if 0
+    assoc->maximumRecordSize = 3000000;
+    assoc->preferredMessageSize = 3000000;
+#endif
+
     resp->preferredMessageSize = &assoc->preferredMessageSize;
     resp->maximumRecordSize = &assoc->maximumRecordSize;
 
@@ -1138,9 +1210,10 @@ static Z_DiagRecs *diagrecs(association *assoc, int error, char *addinfo)
 }
 
 static Z_Records *pack_records(association *a, char *setname, int start,
-                               int *num, Z_RecordComposition *comp,
-                               int *next, int *pres, oid_value format,
-                               Z_ReferenceId *referenceId)
+                              int *num, Z_RecordComposition *comp,
+                              int *next, int *pres, oid_value format,
+                              Z_ReferenceId *referenceId,
+                              int *oid)
 {
     int recno, total_length = 0, toget = *num, dumped_records = 0;
     Z_Records *records =
@@ -1183,7 +1256,9 @@ static Z_Records *pack_records(association *a, char *setname, int start,
        freq.number = recno;
        freq.comp = comp;
        freq.request_format = format;
-       freq.output_format = 0;
+       freq.request_format_raw = oid;
+       freq.output_format = format;
+       freq.output_format_raw = 0;
        freq.stream = a->encode;
        freq.print = a->print;
        freq.surrogate_flag = 0;
@@ -1255,6 +1330,12 @@ static Z_Records *pack_records(association *a, char *setname, int start,
            return 0;
        strcpy(thisrec->databaseName, freq.basename);
        thisrec->which = Z_NamePlusRecord_databaseRecord;
+
+       if (freq.output_format_raw)
+       {
+           struct oident *ident = oid_getentbyoid(freq.output_format_raw);
+           freq.output_format = ident->value;
+       }
        thisrec->u.databaseRecord = z_ext_record(a->encode, freq.output_format,
                                                 freq.record, freq.len);
        if (!thisrec->u.databaseRecord)
@@ -1423,7 +1504,8 @@ static Z_APDU *response_searchRequest(association *assoc, request *reqb,
            else
                form = prefformat->value;
            resp->records = pack_records(assoc, req->resultSetName, 1,
-               toget, compp, next, presst, form, req->referenceId);
+               toget, compp, next, presst, form, req->referenceId,
+                                        req->preferredRecordSyntax);
            if (!resp->records)
                return 0;
            resp->numberOfRecordsReturned = toget;
@@ -1462,7 +1544,7 @@ static Z_APDU *response_searchRequest(association *assoc, request *reqb,
  * speed - which is normally more true for search than for present.
  */
 static Z_APDU *process_presentRequest(association *assoc, request *reqb,
-    int *fd)
+                                     int *fd)
 {
     Z_PresentRequest *req = reqb->request->u.presentRequest;
     oident *prefformat;
@@ -1518,7 +1600,7 @@ static Z_APDU *process_presentRequest(association *assoc, request *reqb,
     resp->records =
        pack_records(assoc, req->resultSetId, *req->resultSetStartPoint,
                     num, req->recordComposition, next, presst, form,
-                     req->referenceId);
+                     req->referenceId, req->preferredRecordSyntax);
     if (!resp->records)
        return 0;
     resp->numberOfRecordsReturned = num;