2 * Copyright (c) 1995, Index Data
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.53 1995-11-01 13:54:58 quinn
10 * Revision 1.52 1995/11/01 12:19:13 quinn
11 * Second attempt to fix same bug.
13 * Revision 1.50 1995/10/25 16:58:32 quinn
16 * Revision 1.49 1995/10/16 13:51:53 quinn
17 * Changes to provide Especs to the backend.
19 * Revision 1.48 1995/10/06 08:51:20 quinn
22 * Revision 1.47 1995/08/29 14:24:16 quinn
23 * Added second half of close-handshake
25 * Revision 1.46 1995/08/29 11:17:58 quinn
26 * Added code to receive close
28 * Revision 1.45 1995/08/21 09:11:00 quinn
29 * Smallish fixes to suppport new formats.
31 * Revision 1.44 1995/08/17 12:45:25 quinn
32 * Fixed minor problems with GRS-1. Added support in c&s.
34 * Revision 1.43 1995/08/15 12:00:31 quinn
37 * Revision 1.42 1995/08/15 11:16:50 quinn
39 * Revision 1.41 1995/08/02 10:23:06 quinn
42 * Revision 1.40 1995/07/31 14:34:26 quinn
43 * Fixed bug in process_searchResponse (numberOfRecordsReturned).
45 * Revision 1.39 1995/06/27 13:21:00 quinn
48 * Revision 1.38 1995/06/19 12:39:11 quinn
49 * Fixed bug in timeout code. Added BER dumper.
51 * Revision 1.37 1995/06/16 13:16:14 quinn
52 * Fixed Defaultdiagformat.
54 * Revision 1.36 1995/06/16 10:31:36 quinn
55 * Added session timeout.
57 * Revision 1.35 1995/06/15 07:45:14 quinn
60 * Revision 1.34 1995/06/14 15:26:46 quinn
61 * *** empty log message ***
63 * Revision 1.33 1995/06/06 14:57:05 quinn
66 * Revision 1.32 1995/06/06 08:41:44 quinn
69 * Revision 1.31 1995/06/06 08:15:37 quinn
72 * Revision 1.30 1995/06/05 10:53:32 quinn
73 * Added a better SCAN.
75 * Revision 1.29 1995/06/01 11:25:03 quinn
78 * Revision 1.28 1995/06/01 11:21:01 quinn
79 * Attempting to fix a bug in pack-records. replaced break with continue
80 * for large records, according to standard.
82 * Revision 1.27 1995/05/29 08:12:06 quinn
85 * Revision 1.26 1995/05/18 13:02:12 quinn
88 * Revision 1.25 1995/05/17 08:42:26 quinn
89 * Transfer auth info to backend. Allow backend to reject init gracefully.
91 * Revision 1.24 1995/05/16 08:51:04 quinn
92 * License, documentation, and memory fixes
94 * Revision 1.23 1995/05/15 13:25:10 quinn
97 * Revision 1.22 1995/05/15 11:56:39 quinn
98 * Asynchronous facilities. Restructuring of seshigh code.
100 * Revision 1.21 1995/05/02 08:53:19 quinn
101 * Trying in vain to fix comm with ISODE
103 * Revision 1.20 1995/04/20 15:13:00 quinn
106 * Revision 1.19 1995/04/18 08:15:34 quinn
107 * Added dynamic memory allocation on encoding (whew). Code is now somewhat
108 * neater. We'll make the same change for decoding one day.
110 * Revision 1.18 1995/04/17 11:28:25 quinn
113 * Revision 1.17 1995/04/10 10:23:36 quinn
114 * Some work to add scan and other things.
116 * Revision 1.16 1995/03/31 09:18:55 quinn
119 * Revision 1.15 1995/03/30 14:03:23 quinn
120 * Added RFC1006 as separate library
122 * Revision 1.14 1995/03/30 12:18:17 quinn
125 * Revision 1.13 1995/03/30 09:09:24 quinn
126 * Added state-handle and some support for asynchronous activities.
128 * Revision 1.12 1995/03/29 15:40:16 quinn
129 * Ongoing work. Statserv is now dynamic by default
131 * Revision 1.11 1995/03/28 09:16:21 quinn
132 * Added record packing to the search request
134 * Revision 1.10 1995/03/27 08:34:24 quinn
135 * Added dynamic server functionality.
136 * Released bindings to session.c (is now redundant)
138 * Revision 1.9 1995/03/22 15:01:26 quinn
139 * Adjusting record packing.
141 * Revision 1.8 1995/03/22 10:13:21 quinn
142 * Working on record packer
144 * Revision 1.7 1995/03/21 15:53:31 quinn
147 * Revision 1.6 1995/03/21 12:30:09 quinn
148 * Beginning to add support for record packing.
150 * Revision 1.5 1995/03/17 10:44:13 quinn
151 * Added catch of null-string in makediagrec
153 * Revision 1.4 1995/03/17 10:18:08 quinn
154 * Added memory management.
156 * Revision 1.3 1995/03/16 17:42:39 quinn
159 * Revision 1.2 1995/03/16 13:29:01 quinn
160 * Partitioned server.
162 * Revision 1.1 1995/03/15 16:02:10 quinn
163 * Modded session.c to seshigh.c
168 * Frontend server logic.
170 * This code receives incoming APDUs, and handles client requests by means
171 * of the backend API.
173 * Some of the code is getting quite involved, compared to simpler servers -
174 * primarily because it is asynchronous both in the communication with
175 * the user and the backend. We think the complexity will pay off in
176 * the form of greater flexibility when more asynchronous facilities
179 * Memory management has become somewhat involved. In the simple case, where
180 * only one PDU is pending at a time, it will simply reuse the same memory,
181 * once it has found its working size. When we enable multiple concurrent
182 * operations, perhaps even with multiple parallel calls to the backend, it
183 * will maintain a pool of buffers for encoding and decoding, trying to
184 * minimize memory allocation/deallocation during normal operation.
186 * TODOs include (and will be done in order of public interest):
188 * Support for EXPLAIN - provide simple meta-database system.
189 * Support for access control.
190 * Support for resource control.
191 * Support for extended services - primarily Item Order.
192 * Rest of Z39.50-1994
200 #include <sys/time.h>
203 #include <comstack.h>
209 #include <statserv.h>
213 static int process_request(association *assoc);
214 void backend_response(IOCHAN i, int event);
215 static int process_response(association *assoc, request *req, Z_APDU *res);
216 static Z_APDU *process_initRequest(association *assoc, request *reqb);
217 static Z_APDU *process_searchRequest(association *assoc, request *reqb,
219 static Z_APDU *response_searchRequest(association *assoc, request *reqb,
220 bend_searchresult *bsrt, int *fd);
221 static Z_APDU *process_presentRequest(association *assoc, request *reqb,
223 static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd);
224 static void process_close(association *assoc, request *reqb);
226 static FILE *apduf = 0; /* for use in static mode */
227 static statserv_options_block *control_block = 0;
230 * Create and initialize a new association-handle.
231 * channel : iochannel for the current line.
232 * link : communications channel.
233 * Returns: 0 or a new association handle.
235 association *create_association(IOCHAN channel, COMSTACK link)
240 control_block = statserv_getcontrol();
241 if (!(new = xmalloc(sizeof(*new))))
243 new->client_chan = channel;
244 new->client_link = link;
245 if (!(new->decode = odr_createmem(ODR_DECODE)) ||
246 !(new->encode = odr_createmem(ODR_ENCODE)))
248 if (*control_block->apdufile)
253 strcpy(filename, control_block->apdufile);
254 if (!(new->print = odr_createmem(ODR_PRINT)))
256 if (*control_block->apdufile != '-')
258 strcpy(filename, control_block->apdufile);
259 if (!control_block->dynamic)
263 if (!(apduf = fopen(filename, "w")))
265 logf(LOG_WARN|LOG_ERRNO, "%s", filename);
268 setvbuf(apduf, 0, _IONBF, 0);
274 sprintf(filename + strlen(filename), ".%d", getpid());
275 if (!(f = fopen(filename, "w")))
277 logf(LOG_WARN|LOG_ERRNO, "%s", filename);
280 setvbuf(f, 0, _IONBF, 0);
282 odr_setprint(new->print, f);
287 new->input_buffer = 0;
288 new->input_buffer_len = 0;
290 new->state = ASSOC_NEW;
291 request_initq(&new->incoming);
292 request_initq(&new->outgoing);
293 new->proto = cs_getproto(link);
298 * Free association and release resources.
300 void destroy_association(association *h)
302 odr_destroy(h->decode);
303 odr_destroy(h->encode);
305 odr_destroy(h->print);
307 xfree(h->input_buffer);
309 bend_close(h->backend);
310 while (request_deq(&h->incoming));
311 while (request_deq(&h->outgoing));
315 static void do_close(association *a, int reason, char *message)
318 Z_Close *cls = zget_Close(a->encode);
319 request *req = request_get();
321 /* Purge request queue */
322 while (request_deq(&a->incoming));
323 while (request_deq(&a->outgoing));
326 logf(LOG_DEBUG, "Generating Close PDU");
327 apdu.which = Z_APDU_close;
329 *cls->closeReason = reason;
330 cls->diagnosticInformation = message;
331 process_response(a, req, &apdu);
332 iochan_settimeout(a->client_chan, 60);
336 logf(LOG_DEBUG, "v2 client. No Close PDU");
337 iochan_setevent(a->client_chan, EVENT_TIMEOUT); /* force imm close */
339 a->state = ASSOC_DEAD;
343 * This is where PDUs from the client are read and the further
344 * processing is initiated. Flow of control moves down through the
345 * various process_* functions below, until the encoded result comes back up
346 * to the output handler in here.
348 * h : the I/O channel that has an outstanding event.
349 * event : the current outstanding event.
351 void ir_session(IOCHAN h, int event)
354 association *assoc = iochan_getdata(h);
355 COMSTACK conn = assoc->client_link;
358 assert(h && conn && assoc);
359 if (event == EVENT_TIMEOUT)
361 if (assoc->state != ASSOC_UP)
363 logf(LOG_LOG, "Final timeout - closing connection.");
365 destroy_association(assoc);
370 logf(LOG_LOG, "Session idle too long. Sending close.");
371 do_close(assoc, Z_Close_lackOfActivity, 0);
375 if (event & EVENT_INPUT || event & EVENT_WORK) /* input */
377 if (event & EVENT_INPUT)
379 logf(LOG_DEBUG, "ir_session (input)");
380 assert(assoc && conn);
381 /* We aren't speaking to this fellow */
382 if (assoc->state == ASSOC_DEAD)
384 logf(LOG_LOG, "Closed connection after reject");
386 destroy_association(assoc);
390 if ((res = cs_get(conn, &assoc->input_buffer,
391 &assoc->input_buffer_len)) <= 0)
393 logf(LOG_LOG, "Connection closed by client");
395 destroy_association(assoc);
399 else if (res == 1) /* incomplete read - wait for more */
401 if (cs_more(conn)) /* more stuff - call us again later, please */
402 iochan_setevent(h, EVENT_INPUT);
404 /* we got a complete PDU. Let's decode it */
405 logf(LOG_DEBUG, "Got PDU, %d bytes", res);
406 req = request_get(); /* get a new request structure */
407 odr_reset(assoc->decode);
408 odr_setbuf(assoc->decode, assoc->input_buffer, res, 0);
409 if (!z_APDU(assoc->decode, &req->request, 0))
411 logf(LOG_LOG, "ODR error on incoming PDU: %s [near byte %d] ",
412 odr_errlist[odr_geterror(assoc->decode)],
413 odr_offset(assoc->decode));
414 logf(LOG_LOG, "PDU dump:");
415 odr_dumpBER(log_file(), assoc->input_buffer, res);
416 do_close(assoc, Z_Close_protocolError, "Malformed package");
419 req->request_mem = odr_extract_mem(assoc->decode);
420 if (assoc->print && !z_APDU(assoc->print, &req->request, 0))
422 logf(LOG_WARN, "ODR print error: %s",
423 odr_errlist[odr_geterror(assoc->print)]);
424 odr_reset(assoc->print);
426 request_enq(&assoc->incoming, req);
429 /* can we do something yet? */
430 req = request_head(&assoc->incoming);
431 if (req->state == REQUEST_IDLE)
432 if (process_request(assoc) < 0)
433 do_close(assoc, Z_Close_systemProblem, "Unknown error");
435 if (event & EVENT_OUTPUT)
437 request *req = request_head(&assoc->outgoing);
439 logf(LOG_DEBUG, "ir_session (output)");
440 req->state = REQUEST_PENDING;
441 switch (res = cs_put(conn, req->response, req->len_response))
444 logf(LOG_LOG, "Connection closed by client");
446 destroy_association(assoc);
449 case 0: /* all sent - release the request structure */
450 logf(LOG_DEBUG, "Wrote PDU, %d bytes", req->len_response);
451 odr_release_mem(req->request_mem);
452 request_deq(&assoc->outgoing);
453 request_release(req);
454 if (!request_head(&assoc->outgoing))
455 iochan_clearflag(h, EVENT_OUTPUT);
457 /* value of 1 -- partial send -- is simply ignored */
460 if (event & EVENT_EXCEPT)
462 logf(LOG_DEBUG, "ir_session (exception)");
464 destroy_association(assoc);
470 * Initiate request processing.
472 static int process_request(association *assoc)
474 request *req = request_head(&assoc->incoming);
478 logf(LOG_DEBUG, "process_request");
479 assert(req && req->state == REQUEST_IDLE);
480 switch (req->request->which)
482 case Z_APDU_initRequest:
483 res = process_initRequest(assoc, req); break;
484 case Z_APDU_searchRequest:
485 res = process_searchRequest(assoc, req, &fd); break;
486 case Z_APDU_presentRequest:
487 res = process_presentRequest(assoc, req, &fd); break;
488 case Z_APDU_scanRequest:
489 res = process_scanRequest(assoc, req, &fd); break;
491 process_close(assoc, req); return 0;
493 logf(LOG_WARN, "Bad APDU received");
498 logf(LOG_DEBUG, " result immediately available");
499 return process_response(assoc, req, res);
503 logf(LOG_WARN, " bad result");
506 else /* no result yet - one will be provided later */
510 /* Set up an I/O handler for the fd supplied by the backend */
512 logf(LOG_DEBUG, " establishing handler for result");
513 req->state = REQUEST_PENDING;
514 if (!(chan = iochan_create(fd, backend_response, EVENT_INPUT)))
516 iochan_setdata(chan, assoc);
522 * Handle message from the backend.
524 void backend_response(IOCHAN i, int event)
526 association *assoc = iochan_getdata(i);
527 request *req = request_head(&assoc->incoming);
531 logf(LOG_DEBUG, "backend_response");
532 assert(assoc && req && req->state != REQUEST_IDLE);
533 /* determine what it is we're waiting for */
534 switch (req->request->which)
536 case Z_APDU_searchRequest:
537 res = response_searchRequest(assoc, req, 0, &fd); break;
539 case Z_APDU_presentRequest:
540 res = response_presentRequest(assoc, req, 0, &fd); break;
541 case Z_APDU_scanRequest:
542 res = response_scanRequest(assoc, req, 0, &fd); break;
545 logf(LOG_WARN, "Serious programmer's lapse or bug");
548 if ((res && process_response(assoc, req, res) < 0) || fd < 0)
550 logf(LOG_LOG, "Fatal error when talking to backend");
551 do_close(assoc, Z_Close_systemProblem, 0);
555 else if (!res) /* no result yet - try again later */
557 logf(LOG_DEBUG, " no result yet");
558 iochan_setfd(i, fd); /* in case fd has changed */
563 * Encode response, and transfer the request structure to the outgoing queue.
565 static int process_response(association *assoc, request *req, Z_APDU *res)
567 odr_setbuf(assoc->encode, req->response, req->size_response, 1);
568 if (!z_APDU(assoc->encode, &res, 0))
570 logf(LOG_WARN, "ODR error when encoding response: %s",
571 odr_errlist[odr_geterror(assoc->decode)]);
574 req->response = odr_getbuf(assoc->encode, &req->len_response,
575 &req->size_response);
576 odr_setbuf(assoc->encode, 0, 0, 0); /* don'txfree if we abort later */
577 odr_reset(assoc->encode);
578 if (assoc->print && !z_APDU(assoc->print, &res, 0))
580 logf(LOG_WARN, "ODR print error: %s",
581 odr_errlist[odr_geterror(assoc->print)]);
582 odr_reset(assoc->print);
584 /* change this when we make the backend reentrant */
585 if (req == request_head(&assoc->incoming))
587 req->state = REQUEST_IDLE;
588 request_deq(&assoc->incoming);
590 request_enq(&assoc->outgoing, req);
591 /* turn the work over to the ir_session handler */
592 iochan_setflag(assoc->client_chan, EVENT_OUTPUT);
593 /* Is there more work to be done? give that to the input handler too */
594 if (request_head(&assoc->incoming))
595 iochan_setevent(assoc->client_chan, EVENT_WORK);
600 * Handle init request.
601 * At the moment, we don't check the options
602 * anywhere else in the code - we just try not to do anything that would
603 * break a naive client. We'll toss 'em into the association block when
604 * we need them there.
606 static Z_APDU *process_initRequest(association *assoc, request *reqb)
608 Z_InitRequest *req = reqb->request->u.initRequest;
609 Z_APDU *apdu = zget_APDU(assoc->encode, Z_APDU_initResponse);
610 Z_InitResponse *resp = apdu->u.initResponse;
611 bend_initrequest binitreq;
612 bend_initresult *binitres;
615 logf(LOG_LOG, "Got initRequest");
616 if (req->implementationId)
617 logf(LOG_LOG, "Id: %s", req->implementationId);
618 if (req->implementationName)
619 logf(LOG_LOG, "Name: %s", req->implementationName);
620 if (req->implementationVersion)
621 logf(LOG_LOG, "Version: %s", req->implementationVersion);
623 binitreq.configname = "default-config";
624 binitreq.auth = req->idAuthentication;
625 if (!(binitres = bend_init(&binitreq)))
627 logf(LOG_WARN, "Bad response from backend.");
631 assoc->backend = binitres->handle;
632 resp->referenceId = req->referenceId;
634 /* let's tell the client what we can do */
635 if (ODR_MASK_GET(req->options, Z_Options_search))
637 ODR_MASK_SET(resp->options, Z_Options_search);
638 strcat(options, "srch");
640 if (ODR_MASK_GET(req->options, Z_Options_present))
642 ODR_MASK_SET(resp->options, Z_Options_present);
643 strcat(options, " prst");
646 if (ODR_MASK_GET(req->options, Z_Options_delSet))
648 ODR_MASK_SET(&options, Z_Options_delSet);
649 strcat(options, " del");
652 if (ODR_MASK_GET(req->options, Z_Options_namedResultSets))
654 ODR_MASK_SET(resp->options, Z_Options_namedResultSets);
655 strcat(options, " namedresults");
657 if (ODR_MASK_GET(req->options, Z_Options_scan))
659 ODR_MASK_SET(resp->options, Z_Options_scan);
660 strcat(options, " scan");
662 if (ODR_MASK_GET(req->options, Z_Options_concurrentOperations))
664 ODR_MASK_SET(resp->options, Z_Options_concurrentOperations);
665 strcat(options, " concurop");
668 if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_1))
670 ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_1);
671 assoc->version = 2; /* 1 & 2 are equivalent */
673 if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_2))
675 ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_2);
678 if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_3))
680 ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_3);
683 logf(LOG_LOG, "Negotiated to v%d: %s", assoc->version, options);
684 assoc->maximumRecordSize = *req->maximumRecordSize;
685 if (assoc->maximumRecordSize > control_block->maxrecordsize)
686 assoc->maximumRecordSize = control_block->maxrecordsize;
687 assoc->preferredMessageSize = *req->preferredMessageSize;
688 if (assoc->preferredMessageSize > assoc->maximumRecordSize)
689 assoc->preferredMessageSize = assoc->maximumRecordSize;
690 resp->preferredMessageSize = &assoc->preferredMessageSize;
691 resp->maximumRecordSize = &assoc->maximumRecordSize;
692 resp->implementationName = "Index Data/YAZ Generic Frontend Server";
693 if (binitres->errcode)
695 logf(LOG_LOG, "Connection rejected by backend.");
697 assoc->state = ASSOC_DEAD;
700 assoc->state = ASSOC_UP;
705 * These functions should be merged.
709 * nonsurrogate diagnostic record.
711 static Z_Records *diagrec(oid_proto proto, int error, char *addinfo)
713 static Z_Records rec;
717 static Z_DiagRec drec;
718 static Z_DefaultDiagFormat dr;
724 bib1.class = CLASS_DIAGSET;
725 bib1.value = VAL_BIB1;
727 logf(LOG_DEBUG, "Diagnostic: %d -- %s", error, addinfo ? addinfo :
730 rec.which = Z_Records_NSD;
732 rec.u.nonSurrogateDiagnostic = &drec;
733 drec.which = Z_DiagRec_defaultFormat;
734 drec.u.defaultFormat = &dr;
736 rec.u.nonSurrogateDiagnostic = &dr;
738 dr.diagnosticSetId = oid_getoidbyent(&bib1);
740 dr.which = Z_DiagForm_v2AddInfo;
741 dr.addinfo = addinfo ? addinfo : "";
746 * surrogate diagnostic.
748 static Z_NamePlusRecord *surrogatediagrec(oid_proto proto, char *dbname,
749 int error, char *addinfo)
751 static Z_NamePlusRecord rec;
755 static Z_DiagRec drec;
756 static Z_DefaultDiagFormat dr;
762 bib1.class = CLASS_DIAGSET;
763 bib1.value = VAL_BIB1;
765 logf(LOG_DEBUG, "SurrogateDiagnotic: %d -- %s", error, addinfo);
767 rec.databaseName = dbname;
768 rec.which = Z_NamePlusRecord_surrogateDiagnostic;
770 rec.u.surrogateDiagnostic = &drec;
771 drec.which = Z_DiagRec_defaultFormat;
772 drec.u.defaultFormat = &dr;
774 rec.u.surrogateDiagnostic = &dr;
776 dr.diagnosticSetId = oid_getoidbyent(&bib1);
778 dr.which = Z_DiagForm_v2AddInfo;
779 dr.addinfo = addinfo ? addinfo : "";
784 * multiple nonsurrogate diagnostics.
786 static Z_DiagRecs *diagrecs(oid_proto proto, int error, char *addinfo)
788 static Z_DiagRecs recs;
792 static Z_DiagRec *recp[1], drec;
793 static Z_DefaultDiagFormat rec;
795 static Z_DiagRec *recp[1], rec;
798 logf(LOG_DEBUG, "DiagRecs: %d -- %s", error, addinfo);
800 bib1.class = CLASS_DIAGSET;
801 bib1.value = VAL_BIB1;
804 recs.num_diagRecs = 1;
805 recs.diagRecs = recp;
808 drec.which = Z_DiagRec_defaultFormat;
809 drec.u.defaultFormat = &rec;
813 rec.diagnosticSetId = oid_getoidbyent(&bib1);
814 rec.condition = &err;
815 rec.which = Z_DiagForm_v2AddInfo;
816 rec.addinfo = addinfo ? addinfo : "";
820 #define MAX_RECORDS 256
822 static Z_Records *pack_records(association *a, char *setname, int start,
823 int *num, Z_RecordComposition *comp,
824 int *next, int *pres, oid_value format)
826 int recno, total_length = 0, toget = *num;
827 static Z_Records records;
828 static Z_NamePlusRecordList reclist;
829 static Z_NamePlusRecord *list[MAX_RECORDS];
832 records.which = Z_Records_DBOSD;
833 records.u.databaseOrSurDiagnostics = &reclist;
834 reclist.num_records = 0;
835 reclist.records = list;
836 *pres = Z_PRES_SUCCESS;
840 logf(LOG_DEBUG, "Request to pack %d+%d", start, toget);
841 logf(LOG_DEBUG, "pms=%d, mrs=%d", a->preferredMessageSize,
842 a->maximumRecordSize);
843 for (recno = start; reclist.num_records < toget; recno++)
845 bend_fetchrequest freq;
846 bend_fetchresult *fres;
847 Z_NamePlusRecord *thisrec;
848 Z_DatabaseRecord *thisext;
852 * we get the number of bytes allocated on the stream before any
853 * allocation done by the backend - this should give us a reasonable
854 * idea of the total size of the data so far.
856 total_length = odr_total(a->encode);
857 if (reclist.num_records == MAX_RECORDS - 1)
859 *pres = Z_PRES_PARTIAL_2;
862 freq.setname = setname;
865 freq.format = format;
866 freq.stream = a->encode;
867 if (!(fres = bend_fetch(a->backend, &freq, 0)))
869 *pres = Z_PRES_FAILURE;
870 return diagrec(a->proto, 2, "Backend interface problem");
872 /* backend should be able to signal whether error is system-wide
873 or only pertaining to current record */
876 *pres = Z_PRES_FAILURE;
877 return diagrec(a->proto, fres->errcode, fres->errstring);
880 this_length = fres->len;
882 this_length = odr_total(a->encode) - total_length;
883 logf(LOG_DEBUG, " fetched record, len=%d, total=%d",
884 this_length, total_length);
885 if (this_length + total_length > a->preferredMessageSize)
887 /* record is small enough, really */
888 if (this_length <= a->preferredMessageSize)
890 logf(LOG_DEBUG, " Dropped last normal-sized record");
891 *pres = Z_PRES_PARTIAL_2;
894 /* record can only be fetched by itself */
895 if (this_length < a->maximumRecordSize)
897 logf(LOG_DEBUG, " Record > prefmsgsz");
900 logf(LOG_DEBUG, " Dropped it");
901 reclist.records[reclist.num_records] =
902 surrogatediagrec(a->proto, fres->basename, 16, 0);
903 reclist.num_records++;
907 else /* too big entirely */
909 logf(LOG_DEBUG, "Record > maxrcdsz");
910 reclist.records[reclist.num_records] =
911 surrogatediagrec(a->proto, fres->basename, 17, 0);
912 reclist.num_records++;
916 if (!(thisrec = odr_malloc(a->encode, sizeof(*thisrec))))
918 if (!(thisrec->databaseName = odr_malloc(a->encode,
919 strlen(fres->basename) + 1)))
921 strcpy(thisrec->databaseName, fres->basename);
922 thisrec->which = Z_NamePlusRecord_databaseRecord;
923 if (!(thisrec->u.databaseRecord = thisext = odr_malloc(a->encode,
924 sizeof(Z_DatabaseRecord))))
926 recform.proto = a->proto;
927 recform.class = CLASS_RECSYN;
928 recform.value = fres->format;
929 thisext->direct_reference = odr_oiddup(a->encode,
930 oid_getoidbyent(&recform));
931 thisext->indirect_reference = 0;
932 thisext->descriptor = 0;
933 if (fres->len < 0) /* Structured data */
935 switch (fres->format)
937 case VAL_SUTRS: thisext->which = Z_External_sutrs; break;
938 case VAL_GRS1: thisext->which = Z_External_grs1; break;
939 case VAL_EXPLAIN: thisext->which = Z_External_explainRecord;
943 logf(LOG_FATAL, "Unknown structured format from backend.");
948 * We cheat on the pointers here. Obviously, the record field
949 * of the backend-fetch structure should have been a union for
950 * correctness, but we're stuck with this for backwards
953 thisext->u.grs1 = (Z_GenericRecord*) fres->record;
955 else if (fres->format == VAL_SUTRS) /* SUTRS is a single-ASN.1-type */
957 Odr_oct *sutrs = odr_malloc(a->encode, sizeof(*sutrs));
959 thisext->which = Z_External_sutrs;
960 thisext->u.sutrs = sutrs;
961 sutrs->buf = odr_malloc(a->encode, fres->len);
962 sutrs->len = sutrs->size = fres->len;
963 memcpy(sutrs->buf, fres->record, fres->len);
965 else /* octet-aligned record. */
967 thisext->which = Z_External_octet;
968 if (!(thisext->u.octet_aligned = odr_malloc(a->encode,
971 if (!(thisext->u.octet_aligned->buf = odr_malloc(a->encode,
974 memcpy(thisext->u.octet_aligned->buf, fres->record, fres->len);
975 thisext->u.octet_aligned->len = thisext->u.octet_aligned->size =
978 reclist.records[reclist.num_records] = thisrec;
979 reclist.num_records++;
980 *next = fres->last_in_set ? 0 : recno + 1;
982 *num = reclist.num_records;
986 static Z_APDU *process_searchRequest(association *assoc, request *reqb,
989 Z_SearchRequest *req = reqb->request->u.searchRequest;
990 bend_searchrequest bsrq;
991 bend_searchresult *bsrt;
993 logf(LOG_LOG, "Got SearchRequest.");
995 bsrq.setname = req->resultSetName;
996 bsrq.replace_set = *req->replaceIndicator;
997 bsrq.num_bases = req->num_databaseNames;
998 bsrq.basenames = req->databaseNames;
999 bsrq.query = req->query;
1001 if (!(bsrt = bend_search(assoc->backend, &bsrq, fd)))
1003 return response_searchRequest(assoc, reqb, bsrt, fd);
1006 bend_searchresult *bend_searchresponse(void *handle) {return 0;}
1009 * Prepare a searchresponse based on the backend results. We probably want
1010 * to look at making the fetching of records nonblocking as well, but
1011 * so far, we'll keep things simple.
1012 * If bsrt is null, that means we're called in response to a communications
1013 * event, and we'll have to get the response for ourselves.
1015 static Z_APDU *response_searchRequest(association *assoc, request *reqb,
1016 bend_searchresult *bsrt, int *fd)
1018 Z_SearchRequest *req = reqb->request->u.searchRequest;
1020 static Z_SearchResponse resp;
1021 static int nulint = 0;
1022 static bool_t sr = 1;
1023 static int next = 0;
1024 static int none = Z_RES_NONE;
1026 apdu.which = Z_APDU_searchResponse;
1027 apdu.u.searchResponse = &resp;
1028 resp.referenceId = req->referenceId;
1030 resp.additionalSearchInfo = 0;
1034 if (!bsrt && !(bsrt = bend_searchresponse(assoc->backend)))
1036 logf(LOG_FATAL, "Bad result from backend");
1039 else if (bsrt->errcode)
1041 resp.records = diagrec(assoc->proto, bsrt->errcode,
1043 resp.resultCount = &nulint;
1044 resp.numberOfRecordsReturned = &nulint;
1045 resp.nextResultSetPosition = &nulint;
1046 resp.searchStatus = &nulint;
1047 resp.resultSetStatus = &none;
1048 resp.presentStatus = 0;
1053 Z_RecordComposition comp, *compp = 0;
1054 static int presst = 0;
1057 resp.resultCount = &bsrt->hits;
1059 comp.which = Z_RecordComp_simple;
1060 /* how many records does the user agent want, then? */
1061 if (bsrt->hits <= *req->smallSetUpperBound)
1064 if ((comp.u.simple = req->smallSetElementSetNames))
1067 else if (bsrt->hits < *req->largeSetLowerBound)
1069 toget = *req->mediumSetPresentNumber;
1070 if (toget > bsrt->hits)
1072 if ((comp.u.simple = req->mediumSetElementSetNames))
1078 if (toget && !resp.records)
1083 if (!(prefformat = oid_getentbyoid(req->preferredRecordSyntax)) ||
1084 prefformat->class != CLASS_RECSYN)
1087 form = prefformat->value;
1088 resp.records = pack_records(assoc, req->resultSetName, 1,
1089 &toget, compp, &next, &presst, form);
1092 resp.numberOfRecordsReturned = &toget;
1093 resp.nextResultSetPosition = &next;
1094 resp.searchStatus = &sr;
1095 resp.resultSetStatus = 0;
1096 resp.presentStatus = &presst;
1100 if (*resp.resultCount)
1102 resp.numberOfRecordsReturned = &nulint;
1103 resp.nextResultSetPosition = &next;
1104 resp.searchStatus = &sr;
1105 resp.resultSetStatus = 0;
1106 resp.presentStatus = 0;
1113 * Maybe we got a little over-friendly when we designed bend_fetch to
1114 * get only one record at a time. Some backends can optimise multiple-record
1115 * fetches, and at any rate, there is some overhead involved in
1116 * all that selecting and hopping around. Problem is, of course, that the
1117 * frontend can't know ahead of time how many records it'll need to
1118 * fill the negotiated PDU size. Annoying. Segmentation or not, Z/SR
1119 * is downright lousy as a bulk data transfer protocol.
1121 * To start with, we'll do the fetching of records from the backend
1122 * in one operation: To save some trips in and out of the event-handler,
1123 * and to simplify the interface to pack_records. At any rate, asynch
1124 * operation is more fun in operations that have an unpredictable execution
1125 * speed - which is normally more true for search than for present.
1127 static Z_APDU *process_presentRequest(association *assoc, request *reqb,
1130 Z_PresentRequest *req = reqb->request->u.presentRequest;
1132 static Z_PresentResponse resp;
1133 static int presst, next, num;
1138 logf(LOG_LOG, "Got PresentRequest.");
1139 apdu.which = Z_APDU_presentResponse;
1140 apdu.u.presentResponse = &resp;
1141 resp.referenceId = req->referenceId;
1146 if (!(prefformat = oid_getentbyoid(req->preferredRecordSyntax)) ||
1147 prefformat->class != CLASS_RECSYN)
1150 form = prefformat->value;
1151 num = *req->numberOfRecordsRequested;
1152 resp.records = pack_records(assoc, req->resultSetId,
1153 *req->resultSetStartPoint, &num, req->recordComposition, &next,
1157 resp.numberOfRecordsReturned = #
1158 resp.presentStatus = &presst;
1159 resp.nextResultSetPosition = &next;
1165 * Scan was implemented rather in a hurry, and with support for only the basic
1166 * elements of the service in the backend API. Suggestions are welcome.
1168 static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd)
1170 Z_ScanRequest *req = reqb->request->u.scanRequest;
1172 static Z_ScanResponse res;
1173 static int scanStatus = Z_Scan_failure;
1174 static int numberOfEntriesReturned = 0;
1176 static Z_ListEntries ents;
1177 #define SCAN_MAX_ENTRIES 200
1178 static Z_Entry *tab[SCAN_MAX_ENTRIES];
1179 bend_scanrequest srq;
1180 bend_scanresult *srs;
1182 logf(LOG_LOG, "Got scanrequest");
1183 apdu.which = Z_APDU_scanResponse;
1184 apdu.u.scanResponse = &res;
1185 res.referenceId = req->referenceId;
1187 res.scanStatus = &scanStatus;
1188 res.numberOfEntriesReturned = &numberOfEntriesReturned;
1189 res.positionOfTerm = 0;
1190 res.entries = &ents;
1191 ents.which = Z_ListEntries_nonSurrogateDiagnostics;
1192 res.attributeSet = 0;
1197 if (req->attributeSet && (!(attent = oid_getentbyoid(req->attributeSet)) ||
1198 attent->class != CLASS_ATTSET || attent->value != VAL_BIB1))
1199 ents.u.nonSurrogateDiagnostics = diagrecs(assoc->proto, 121, 0);
1200 else if (req->stepSize && *req->stepSize > 0)
1201 ents.u.nonSurrogateDiagnostics = diagrecs(assoc->proto, 205, 0);
1204 if (req->termListAndStartPoint->term->which == Z_Term_general)
1205 logf(LOG_DEBUG, " term: '%.*s'",
1206 req->termListAndStartPoint->term->u.general->len,
1207 req->termListAndStartPoint->term->u.general->buf);
1208 srq.num_bases = req->num_databaseNames;
1209 srq.basenames = req->databaseNames;
1210 srq.num_entries = *req->numberOfTermsRequested;
1211 srq.term = req->termListAndStartPoint;
1212 srq.term_position = req->preferredPositionInResponse ?
1213 *req->preferredPositionInResponse : 1;
1214 if (!(srs = bend_scan(assoc->backend, &srq, 0)))
1215 ents.u.nonSurrogateDiagnostics = diagrecs(assoc->proto, 2, 0);
1216 else if (srs->errcode)
1217 ents.u.nonSurrogateDiagnostics = diagrecs(assoc->proto,
1218 srs->errcode, srs->errstring);
1222 static Z_Entries list;
1224 if (srs->status == BEND_SCAN_PARTIAL)
1225 scanStatus = Z_Scan_partial_5;
1227 scanStatus = Z_Scan_success;
1228 ents.which = Z_ListEntries_entries;
1229 ents.u.entries = &list;
1231 for (i = 0; i < srs->num_entries; i++)
1237 if (i >= SCAN_MAX_ENTRIES)
1239 scanStatus = Z_Scan_partial_4;
1242 list.entries[i] = e = odr_malloc(assoc->encode, sizeof(*e));
1243 e->which = Z_Entry_termInfo;
1244 e->u.termInfo = t = odr_malloc(assoc->encode, sizeof(*t));
1245 t->suggestedAttributes = 0;
1246 t->alternativeTerm = 0;
1247 t->byAttributes = 0;
1248 t->globalOccurrences = &srs->entries[i].occurrences;
1249 t->term = odr_malloc(assoc->encode, sizeof(*t->term));
1250 t->term->which = Z_Term_general;
1251 t->term->u.general = o = odr_malloc(assoc->encode,
1253 o->buf = odr_malloc(assoc->encode, o->len = o->size =
1254 strlen(srs->entries[i].term));
1255 memcpy(o->buf, srs->entries[i].term, o->len);
1256 logf(LOG_DEBUG, " term #%d: '%s' (%d)", i,
1257 srs->entries[i].term, srs->entries[i].occurrences);
1259 list.num_entries = i;
1260 res.numberOfEntriesReturned = &list.num_entries;
1261 res.positionOfTerm = &srs->term_position;
1268 static void process_close(association *assoc, request *reqb)
1270 Z_Close *req = reqb->request->u.close;
1271 static char *reasons[] =
1278 "securityViolation",
1285 logf(LOG_LOG, "Got close, reason %s, message %s",
1286 reasons[*req->closeReason], req->diagnosticInformation ?
1287 req->diagnosticInformation : "NULL");
1288 if (assoc->version < 3) /* to make do_force respond with close */
1290 do_close(assoc, Z_Close_finished, "Association terminated by client");