2 * Copyright (c) 1995-1998, Index Data
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.71 1998-02-10 11:03:57 adam
8 * Added support for extended handlers in backend server interface.
10 * Revision 1.70 1998/01/29 13:15:35 adam
11 * Implemented sort for the backend interface.
13 * Revision 1.69 1997/09/30 11:48:12 adam
14 * Fixed bug introduced by previous commit.
16 * Revision 1.68 1997/09/29 13:18:59 adam
17 * Added function, oid_ent_to_oid, to replace the function
18 * oid_getoidbyent, which is not thread safe.
20 * Revision 1.67 1997/09/17 12:10:40 adam
23 * Revision 1.66 1997/09/05 15:26:44 adam
24 * Added ODR encode in search and scen bend request structures.
25 * Fixed a few enums that caused trouble with C++.
27 * Revision 1.65 1997/09/01 08:53:01 adam
28 * New windows NT/95 port using MSV5.0. The test server 'ztest' was
29 * moved a separate directory. MSV5.0 project server.dsp created.
30 * As an option, the server can now operate as an NT service.
32 * Revision 1.64 1997/04/30 08:52:11 quinn
35 * Revision 1.63 1996/10/11 11:57:26 quinn
38 * Revision 1.62 1996/07/06 19:58:35 quinn
39 * System headerfiles gathered in yconfig
41 * Revision 1.61 1996/06/10 08:56:16 quinn
44 * Revision 1.60 1996/05/30 11:03:10 quinn
45 * Fixed NextresultSetPosition bug fixed.
47 * Revision 1.59 1996/05/14 09:26:46 quinn
48 * Added attribute set to scan backend
50 * Revision 1.58 1996/02/20 12:53:04 quinn
53 * Revision 1.57 1996/01/02 08:57:47 quinn
54 * Changed enums in the ASN.1 .h files to #defines. Changed oident.class to oclass
56 * Revision 1.56 1995/12/14 11:09:57 quinn
59 * Revision 1.55 1995/11/08 17:41:37 quinn
62 * Revision 1.54 1995/11/08 15:11:29 quinn
63 * Log of close transmit.
65 * Revision 1.53 1995/11/01 13:54:58 quinn
68 * Revision 1.52 1995/11/01 12:19:13 quinn
69 * Second attempt to fix same bug.
71 * Revision 1.50 1995/10/25 16:58:32 quinn
74 * Revision 1.49 1995/10/16 13:51:53 quinn
75 * Changes to provide Especs to the backend.
77 * Revision 1.48 1995/10/06 08:51:20 quinn
80 * Revision 1.47 1995/08/29 14:24:16 quinn
81 * Added second half of close-handshake
83 * Revision 1.46 1995/08/29 11:17:58 quinn
84 * Added code to receive close
86 * Revision 1.45 1995/08/21 09:11:00 quinn
87 * Smallish fixes to suppport new formats.
89 * Revision 1.44 1995/08/17 12:45:25 quinn
90 * Fixed minor problems with GRS-1. Added support in c&s.
92 * Revision 1.43 1995/08/15 12:00:31 quinn
95 * Revision 1.42 1995/08/15 11:16:50 quinn
97 * Revision 1.41 1995/08/02 10:23:06 quinn
100 * Revision 1.40 1995/07/31 14:34:26 quinn
101 * Fixed bug in process_searchResponse (numberOfRecordsReturned).
103 * Revision 1.39 1995/06/27 13:21:00 quinn
106 * Revision 1.38 1995/06/19 12:39:11 quinn
107 * Fixed bug in timeout code. Added BER dumper.
109 * Revision 1.37 1995/06/16 13:16:14 quinn
110 * Fixed Defaultdiagformat.
112 * Revision 1.36 1995/06/16 10:31:36 quinn
113 * Added session timeout.
115 * Revision 1.35 1995/06/15 07:45:14 quinn
118 * Revision 1.34 1995/06/14 15:26:46 quinn
119 * *** empty log message ***
121 * Revision 1.33 1995/06/06 14:57:05 quinn
122 * Better diagnostics.
124 * Revision 1.32 1995/06/06 08:41:44 quinn
125 * Better diagnostics.
127 * Revision 1.31 1995/06/06 08:15:37 quinn
130 * Revision 1.30 1995/06/05 10:53:32 quinn
131 * Added a better SCAN.
133 * Revision 1.29 1995/06/01 11:25:03 quinn
136 * Revision 1.28 1995/06/01 11:21:01 quinn
137 * Attempting to fix a bug in pack-records. replaced break with continue
138 * for large records, according to standard.
140 * Revision 1.27 1995/05/29 08:12:06 quinn
143 * Revision 1.26 1995/05/18 13:02:12 quinn
146 * Revision 1.25 1995/05/17 08:42:26 quinn
147 * Transfer auth info to backend. Allow backend to reject init gracefully.
149 * Revision 1.24 1995/05/16 08:51:04 quinn
150 * License, documentation, and memory fixes
152 * Revision 1.23 1995/05/15 13:25:10 quinn
155 * Revision 1.22 1995/05/15 11:56:39 quinn
156 * Asynchronous facilities. Restructuring of seshigh code.
158 * Revision 1.21 1995/05/02 08:53:19 quinn
159 * Trying in vain to fix comm with ISODE
161 * Revision 1.20 1995/04/20 15:13:00 quinn
164 * Revision 1.19 1995/04/18 08:15:34 quinn
165 * Added dynamic memory allocation on encoding (whew). Code is now somewhat
166 * neater. We'll make the same change for decoding one day.
168 * Revision 1.18 1995/04/17 11:28:25 quinn
171 * Revision 1.17 1995/04/10 10:23:36 quinn
172 * Some work to add scan and other things.
174 * Revision 1.16 1995/03/31 09:18:55 quinn
177 * Revision 1.15 1995/03/30 14:03:23 quinn
178 * Added RFC1006 as separate library
180 * Revision 1.14 1995/03/30 12:18:17 quinn
183 * Revision 1.13 1995/03/30 09:09:24 quinn
184 * Added state-handle and some support for asynchronous activities.
186 * Revision 1.12 1995/03/29 15:40:16 quinn
187 * Ongoing work. Statserv is now dynamic by default
189 * Revision 1.11 1995/03/28 09:16:21 quinn
190 * Added record packing to the search request
192 * Revision 1.10 1995/03/27 08:34:24 quinn
193 * Added dynamic server functionality.
194 * Released bindings to session.c (is now redundant)
196 * Revision 1.9 1995/03/22 15:01:26 quinn
197 * Adjusting record packing.
199 * Revision 1.8 1995/03/22 10:13:21 quinn
200 * Working on record packer
202 * Revision 1.7 1995/03/21 15:53:31 quinn
205 * Revision 1.6 1995/03/21 12:30:09 quinn
206 * Beginning to add support for record packing.
208 * Revision 1.5 1995/03/17 10:44:13 quinn
209 * Added catch of null-string in makediagrec
211 * Revision 1.4 1995/03/17 10:18:08 quinn
212 * Added memory management.
214 * Revision 1.3 1995/03/16 17:42:39 quinn
217 * Revision 1.2 1995/03/16 13:29:01 quinn
218 * Partitioned server.
220 * Revision 1.1 1995/03/15 16:02:10 quinn
221 * Modded session.c to seshigh.c
226 * Frontend server logic.
228 * This code receives incoming APDUs, and handles client requests by means
229 * of the backend API.
231 * Some of the code is getting quite involved, compared to simpler servers -
232 * primarily because it is asynchronous both in the communication with
233 * the user and the backend. We think the complexity will pay off in
234 * the form of greater flexibility when more asynchronous facilities
237 * Memory management has become somewhat involved. In the simple case, where
238 * only one PDU is pending at a time, it will simply reuse the same memory,
239 * once it has found its working size. When we enable multiple concurrent
240 * operations, perhaps even with multiple parallel calls to the backend, it
241 * will maintain a pool of buffers for encoding and decoding, trying to
242 * minimize memory allocation/deallocation during normal operation.
244 * TODOs include (and will be done in order of public interest):
246 * Support for EXPLAIN - provide simple meta-database system.
247 * Support for access control.
248 * Support for resource control.
249 * Support for extended services - primarily Item Order.
250 * Rest of Z39.50-1994
265 #include <comstack.h>
271 #include <statserv.h>
275 static int process_request(association *assoc, request *req);
276 void backend_response(IOCHAN i, int event);
277 static int process_response(association *assoc, request *req, Z_APDU *res);
278 static Z_APDU *process_initRequest(association *assoc, request *reqb);
279 static Z_APDU *process_searchRequest(association *assoc, request *reqb,
281 static Z_APDU *response_searchRequest(association *assoc, request *reqb,
282 bend_search_rr *bsrr, int *fd);
283 static Z_APDU *process_presentRequest(association *assoc, request *reqb,
285 static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd);
286 static Z_APDU *process_sortRequest(association *assoc, request *reqb, int *fd);
287 static void process_close(association *assoc, request *reqb);
288 void save_referenceId (request *reqb, Z_ReferenceId *refid);
290 static FILE *apduf = 0; /* for use in static mode */
291 static statserv_options_block *control_block = 0;
294 * Create and initialize a new association-handle.
295 * channel : iochannel for the current line.
296 * link : communications channel.
297 * Returns: 0 or a new association handle.
299 association *create_association(IOCHAN channel, COMSTACK link)
304 control_block = statserv_getcontrol();
305 if (!(anew = xmalloc(sizeof(*anew))))
307 anew->client_chan = channel;
308 anew->client_link = link;
309 if (!(anew->decode = odr_createmem(ODR_DECODE)) ||
310 !(anew->encode = odr_createmem(ODR_ENCODE)))
312 if (*control_block->apdufile)
317 strcpy(filename, control_block->apdufile);
318 if (!(anew->print = odr_createmem(ODR_PRINT)))
320 if (*control_block->apdufile != '-')
322 strcpy(filename, control_block->apdufile);
323 if (!control_block->dynamic)
327 if (!(apduf = fopen(filename, "w")))
329 logf(LOG_WARN|LOG_ERRNO, "%s", filename);
332 setvbuf(apduf, 0, _IONBF, 0);
338 sprintf(filename + strlen(filename), ".%d", getpid());
339 if (!(f = fopen(filename, "w")))
341 logf(LOG_WARN|LOG_ERRNO, "%s", filename);
344 setvbuf(f, 0, _IONBF, 0);
346 odr_setprint(anew->print, f);
351 anew->input_buffer = 0;
352 anew->input_buffer_len = 0;
354 anew->state = ASSOC_NEW;
355 request_initq(&anew->incoming);
356 request_initq(&anew->outgoing);
357 anew->proto = cs_getproto(link);
362 * Free association and release resources.
364 void destroy_association(association *h)
366 odr_destroy(h->decode);
367 odr_destroy(h->encode);
369 odr_destroy(h->print);
371 xfree(h->input_buffer);
373 bend_close(h->backend);
374 while (request_deq(&h->incoming));
375 while (request_deq(&h->outgoing));
376 request_delq(&h->incoming);
377 request_delq(&h->outgoing);
381 static void do_close(association *a, int reason, char *message)
384 Z_Close *cls = zget_Close(a->encode);
385 request *req = request_get(&a->outgoing);
387 /* Purge request queue */
388 while (request_deq(&a->incoming));
389 while (request_deq(&a->outgoing));
392 logf(LOG_LOG, "Sending Close PDU, reason=%d, message=%s",
393 reason, message ? message : "none");
394 apdu.which = Z_APDU_close;
396 *cls->closeReason = reason;
397 cls->diagnosticInformation = message;
398 process_response(a, req, &apdu);
399 iochan_settimeout(a->client_chan, 60);
403 logf(LOG_DEBUG, "v2 client. No Close PDU");
404 iochan_setevent(a->client_chan, EVENT_TIMEOUT); /* force imm close */
406 a->state = ASSOC_DEAD;
410 * This is where PDUs from the client are read and the further
411 * processing is initiated. Flow of control moves down through the
412 * various process_* functions below, until the encoded result comes back up
413 * to the output handler in here.
415 * h : the I/O channel that has an outstanding event.
416 * event : the current outstanding event.
418 void ir_session(IOCHAN h, int event)
421 association *assoc = iochan_getdata(h);
422 COMSTACK conn = assoc->client_link;
425 assert(h && conn && assoc);
426 if (event == EVENT_TIMEOUT)
428 if (assoc->state != ASSOC_UP)
430 logf(LOG_LOG, "Final timeout - closing connection.");
432 destroy_association(assoc);
437 logf(LOG_LOG, "Session idle too long. Sending close.");
438 do_close(assoc, Z_Close_lackOfActivity, 0);
442 if (event & EVENT_INPUT || event & EVENT_WORK) /* input */
444 if (event & EVENT_INPUT)
446 logf(LOG_DEBUG, "ir_session (input)");
447 assert(assoc && conn);
448 /* We aren't speaking to this fellow */
449 if (assoc->state == ASSOC_DEAD)
451 logf(LOG_LOG, "Closed connection after reject");
453 destroy_association(assoc);
457 if ((res = cs_get(conn, &assoc->input_buffer,
458 &assoc->input_buffer_len)) <= 0)
460 logf(LOG_LOG, "Connection closed by client");
462 destroy_association(assoc);
466 else if (res == 1) /* incomplete read - wait for more */
468 if (cs_more(conn)) /* more stuff - call us again later, please */
469 iochan_setevent(h, EVENT_INPUT);
471 /* we got a complete PDU. Let's decode it */
472 logf(LOG_DEBUG, "Got PDU, %d bytes", res);
473 req = request_get(&assoc->incoming); /* get a new request structure */
474 odr_reset(assoc->decode);
475 odr_setbuf(assoc->decode, assoc->input_buffer, res, 0);
476 if (!z_APDU(assoc->decode, &req->request, 0))
478 logf(LOG_LOG, "ODR error on incoming PDU: %s [near byte %d] ",
479 odr_errmsg(odr_geterror(assoc->decode)),
480 odr_offset(assoc->decode));
481 logf(LOG_LOG, "PDU dump:");
482 odr_dumpBER(log_file(), assoc->input_buffer, res);
483 do_close(assoc, Z_Close_protocolError, "Malformed package");
486 req->request_mem = odr_extract_mem(assoc->decode);
487 if (assoc->print && !z_APDU(assoc->print, &req->request, 0))
489 logf(LOG_WARN, "ODR print error: %s",
490 odr_errmsg(odr_geterror(assoc->print)));
491 odr_reset(assoc->print);
493 request_enq(&assoc->incoming, req);
496 /* can we do something yet? */
497 req = request_head(&assoc->incoming);
498 if (req->state == REQUEST_IDLE)
500 request_deq(&assoc->incoming);
501 if (process_request(assoc, req) < 0)
502 do_close(assoc, Z_Close_systemProblem, "Unknown error");
505 if (event & EVENT_OUTPUT)
507 request *req = request_head(&assoc->outgoing);
509 logf(LOG_DEBUG, "ir_session (output)");
510 req->state = REQUEST_PENDING;
511 switch (res = cs_put(conn, req->response, req->len_response))
514 logf(LOG_LOG, "Connection closed by client");
516 destroy_association(assoc);
519 case 0: /* all sent - release the request structure */
520 logf(LOG_DEBUG, "Wrote PDU, %d bytes", req->len_response);
521 nmem_destroy(req->request_mem);
522 request_deq(&assoc->outgoing);
523 request_release(req);
524 if (!request_head(&assoc->outgoing))
525 iochan_clearflag(h, EVENT_OUTPUT);
527 /* value of 1 -- partial send -- is simply ignored */
530 if (event & EVENT_EXCEPT)
532 logf(LOG_DEBUG, "ir_session (exception)");
534 destroy_association(assoc);
540 * Initiate request processing.
542 static int process_request(association *assoc, request *req)
548 assert(req && req->state == REQUEST_IDLE);
549 switch (req->request->which)
551 case Z_APDU_initRequest:
552 res = process_initRequest(assoc, req); break;
553 case Z_APDU_searchRequest:
554 res = process_searchRequest(assoc, req, &fd); break;
555 case Z_APDU_presentRequest:
556 res = process_presentRequest(assoc, req, &fd); break;
557 case Z_APDU_scanRequest:
558 res = process_scanRequest(assoc, req, &fd); break;
559 case Z_APDU_sortRequest:
560 if (assoc->bend_sort)
561 res = process_sortRequest(assoc, req, &fd);
564 logf(LOG_WARN, "Cannot handle SORT APDU");
569 process_close(assoc, req); return 0;
571 logf(LOG_WARN, "Bad APDU received");
576 logf(LOG_DEBUG, " result immediately available");
577 retval = process_response(assoc, req, res);
581 logf(LOG_DEBUG, " result unavailble");
584 else /* no result yet - one will be provided later */
588 /* Set up an I/O handler for the fd supplied by the backend */
590 logf(LOG_DEBUG, " establishing handler for result");
591 req->state = REQUEST_PENDING;
592 if (!(chan = iochan_create(fd, backend_response, EVENT_INPUT)))
594 iochan_setdata(chan, assoc);
601 * Handle message from the backend.
603 void backend_response(IOCHAN i, int event)
605 association *assoc = iochan_getdata(i);
606 request *req = request_head(&assoc->incoming);
610 logf(LOG_DEBUG, "backend_response");
611 assert(assoc && req && req->state != REQUEST_IDLE);
612 /* determine what it is we're waiting for */
613 switch (req->request->which)
615 case Z_APDU_searchRequest:
616 res = response_searchRequest(assoc, req, 0, &fd); break;
618 case Z_APDU_presentRequest:
619 res = response_presentRequest(assoc, req, 0, &fd); break;
620 case Z_APDU_scanRequest:
621 res = response_scanRequest(assoc, req, 0, &fd); break;
624 logf(LOG_WARN, "Serious programmer's lapse or bug");
627 if ((res && process_response(assoc, req, res) < 0) || fd < 0)
629 logf(LOG_LOG, "Fatal error when talking to backend");
630 do_close(assoc, Z_Close_systemProblem, 0);
634 else if (!res) /* no result yet - try again later */
636 logf(LOG_DEBUG, " no result yet");
637 iochan_setfd(i, fd); /* in case fd has changed */
642 * Encode response, and transfer the request structure to the outgoing queue.
644 static int process_response(association *assoc, request *req, Z_APDU *res)
646 odr_setbuf(assoc->encode, req->response, req->size_response, 1);
647 if (!z_APDU(assoc->encode, &res, 0))
649 logf(LOG_WARN, "ODR error when encoding response: %s",
650 odr_errmsg(odr_geterror(assoc->decode)));
651 odr_reset(assoc->encode);
654 req->response = odr_getbuf(assoc->encode, &req->len_response,
655 &req->size_response);
656 odr_setbuf(assoc->encode, 0, 0, 0); /* don'txfree if we abort later */
657 odr_reset(assoc->encode);
658 if (assoc->print && !z_APDU(assoc->print, &res, 0))
660 logf(LOG_WARN, "ODR print error: %s",
661 odr_errmsg(odr_geterror(assoc->print)));
662 odr_reset(assoc->print);
664 req->state = REQUEST_IDLE;
665 request_enq(&assoc->outgoing, req);
666 /* turn the work over to the ir_session handler */
667 iochan_setflag(assoc->client_chan, EVENT_OUTPUT);
668 /* Is there more work to be done? give that to the input handler too */
670 if (request_head(&assoc->incoming))
672 logf (LOG_DEBUG, "more work to be done");
673 iochan_setevent(assoc->client_chan, EVENT_WORK);
680 * Handle init request.
681 * At the moment, we don't check the options
682 * anywhere else in the code - we just try not to do anything that would
683 * break a naive client. We'll toss 'em into the association block when
684 * we need them there.
686 static Z_APDU *process_initRequest(association *assoc, request *reqb)
688 Z_InitRequest *req = reqb->request->u.initRequest;
689 Z_APDU *apdu = zget_APDU(assoc->encode, Z_APDU_initResponse);
690 Z_InitResponse *resp = apdu->u.initResponse;
691 bend_initrequest binitreq;
692 bend_initresult *binitres;
695 logf(LOG_LOG, "Got initRequest");
696 if (req->implementationId)
697 logf(LOG_LOG, "Id: %s", req->implementationId);
698 if (req->implementationName)
699 logf(LOG_LOG, "Name: %s", req->implementationName);
700 if (req->implementationVersion)
701 logf(LOG_LOG, "Version: %s", req->implementationVersion);
703 binitreq.stream = assoc->encode;
704 binitreq.configname = "default-config";
705 binitreq.auth = req->idAuthentication;
706 binitreq.bend_sort = NULL;
707 binitreq.bend_search = NULL;
708 binitreq.bend_present = NULL;
709 if (!(binitres = bend_init(&binitreq)))
711 logf(LOG_WARN, "Bad response from backend.");
715 assoc->backend = binitres->handle;
716 if ((assoc->bend_sort = binitreq.bend_sort))
717 logf (LOG_DEBUG, "Sort handler installed");
718 if ((assoc->bend_search = binitreq.bend_search))
719 logf (LOG_DEBUG, "Search handler installed");
720 if ((assoc->bend_present = binitreq.bend_present))
721 logf (LOG_DEBUG, "Present handler installed");
722 resp->referenceId = req->referenceId;
724 /* let's tell the client what we can do */
725 if (ODR_MASK_GET(req->options, Z_Options_search))
727 ODR_MASK_SET(resp->options, Z_Options_search);
728 strcat(options, "srch");
730 if (ODR_MASK_GET(req->options, Z_Options_present))
732 ODR_MASK_SET(resp->options, Z_Options_present);
733 strcat(options, " prst");
736 if (ODR_MASK_GET(req->options, Z_Options_delSet))
738 ODR_MASK_SET(&options, Z_Options_delSet);
739 strcat(options, " del");
742 if (ODR_MASK_GET(req->options, Z_Options_namedResultSets))
744 ODR_MASK_SET(resp->options, Z_Options_namedResultSets);
745 strcat(options, " namedresults");
747 if (ODR_MASK_GET(req->options, Z_Options_scan))
749 ODR_MASK_SET(resp->options, Z_Options_scan);
750 strcat(options, " scan");
752 if (ODR_MASK_GET(req->options, Z_Options_concurrentOperations))
754 ODR_MASK_SET(resp->options, Z_Options_concurrentOperations);
755 strcat(options, " concurop");
757 if (ODR_MASK_GET(req->options, Z_Options_sort && binitreq.bend_sort))
759 ODR_MASK_SET(resp->options, Z_Options_sort);
760 strcat(options, " sort");
762 if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_1))
764 ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_1);
765 assoc->version = 2; /* 1 & 2 are equivalent */
767 if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_2))
769 ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_2);
772 if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_3))
774 ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_3);
777 logf(LOG_LOG, "Negotiated to v%d: %s", assoc->version, options);
778 assoc->maximumRecordSize = *req->maximumRecordSize;
779 if (assoc->maximumRecordSize > control_block->maxrecordsize)
780 assoc->maximumRecordSize = control_block->maxrecordsize;
781 assoc->preferredMessageSize = *req->preferredMessageSize;
782 if (assoc->preferredMessageSize > assoc->maximumRecordSize)
783 assoc->preferredMessageSize = assoc->maximumRecordSize;
784 resp->preferredMessageSize = &assoc->preferredMessageSize;
785 resp->maximumRecordSize = &assoc->maximumRecordSize;
786 resp->implementationName = "Index Data/YAZ Generic Frontend Server";
787 if (binitres->errcode)
789 logf(LOG_LOG, "Connection rejected by backend.");
791 assoc->state = ASSOC_DEAD;
794 assoc->state = ASSOC_UP;
799 * These functions should be merged.
803 * nonsurrogate diagnostic record.
805 static Z_Records *diagrec(association *assoc, int error, char *addinfo)
808 Z_Records *rec = odr_malloc (assoc->encode, sizeof(*rec));
810 int *err = odr_malloc (assoc->encode, sizeof(*err));
811 Z_DiagRec *drec = odr_malloc (assoc->encode, sizeof(*drec));
812 Z_DefaultDiagFormat *dr = odr_malloc (assoc->encode, sizeof(*dr));
814 bib1.proto = assoc->proto;
815 bib1.oclass = CLASS_DIAGSET;
816 bib1.value = VAL_BIB1;
818 logf(LOG_DEBUG, "Diagnostic: %d -- %s", error, addinfo ? addinfo :
821 rec->which = Z_Records_NSD;
823 rec->u.nonSurrogateDiagnostic = drec;
824 drec->which = Z_DiagRec_defaultFormat;
825 drec->u.defaultFormat = dr;
827 rec->u.nonSurrogateDiagnostic = dr;
829 dr->diagnosticSetId = odr_oiddup (assoc->encode,
830 oid_ent_to_oid(&bib1, oid));
832 dr->which = Z_DiagForm_v2AddInfo;
833 dr->addinfo = addinfo ? addinfo : "";
838 * surrogate diagnostic.
840 static Z_NamePlusRecord *surrogatediagrec(association *assoc, char *dbname,
841 int error, char *addinfo)
844 Z_NamePlusRecord *rec = odr_malloc (assoc->encode, sizeof(*rec));
845 int *err = odr_malloc (assoc->encode, sizeof(*err));
847 Z_DiagRec *drec = odr_malloc (assoc->encode, sizeof(*drec));
848 Z_DefaultDiagFormat *dr = odr_malloc (assoc->encode, sizeof(*dr));
850 bib1.proto = assoc->proto;
851 bib1.oclass = CLASS_DIAGSET;
852 bib1.value = VAL_BIB1;
854 logf(LOG_DEBUG, "SurrogateDiagnotic: %d -- %s", error, addinfo);
856 rec->databaseName = dbname;
857 rec->which = Z_NamePlusRecord_surrogateDiagnostic;
858 rec->u.surrogateDiagnostic = drec;
859 drec->which = Z_DiagRec_defaultFormat;
860 drec->u.defaultFormat = dr;
861 dr->diagnosticSetId = odr_oiddup (assoc->encode,
862 oid_ent_to_oid(&bib1, oid));
864 dr->which = Z_DiagForm_v2AddInfo;
865 dr->addinfo = addinfo ? addinfo : "";
870 * multiple nonsurrogate diagnostics.
872 static Z_DiagRecs *diagrecs(association *assoc, int error, char *addinfo)
875 Z_DiagRecs *recs = odr_malloc (assoc->encode, sizeof(*recs));
876 int *err = odr_malloc (assoc->encode, sizeof(*err));
878 Z_DiagRec **recp = odr_malloc (assoc->encode, sizeof(*recp));
879 Z_DiagRec *drec = odr_malloc (assoc->encode, sizeof(*drec));
880 Z_DefaultDiagFormat *rec = odr_malloc (assoc->encode, sizeof(*rec));
882 logf(LOG_DEBUG, "DiagRecs: %d -- %s", error, addinfo);
883 bib1.proto = assoc->proto;
884 bib1.oclass = CLASS_DIAGSET;
885 bib1.value = VAL_BIB1;
888 recs->num_diagRecs = 1;
889 recs->diagRecs = recp;
891 drec->which = Z_DiagRec_defaultFormat;
892 drec->u.defaultFormat = rec;
894 rec->diagnosticSetId = odr_oiddup (assoc->encode,
895 oid_ent_to_oid(&bib1, oid));
896 rec->condition = err;
897 rec->which = Z_DiagForm_v2AddInfo;
898 rec->addinfo = addinfo ? addinfo : "";
902 static Z_Records *pack_records(association *a, char *setname, int start,
903 int *num, Z_RecordComposition *comp,
904 int *next, int *pres, oid_value format)
907 int recno, total_length = 0, toget = *num, dumped_records = 0;
908 Z_Records *records = odr_malloc (a->encode, sizeof(*records));
909 Z_NamePlusRecordList *reclist = odr_malloc (a->encode, sizeof(*reclist));
910 Z_NamePlusRecord **list = odr_malloc (a->encode, sizeof(*list) * toget);
913 records->which = Z_Records_DBOSD;
914 records->u.databaseOrSurDiagnostics = reclist;
915 reclist->num_records = 0;
916 reclist->records = list;
917 *pres = Z_PRES_SUCCESS;
921 logf(LOG_DEBUG, "Request to pack %d+%d", start, toget);
922 logf(LOG_DEBUG, "pms=%d, mrs=%d", a->preferredMessageSize,
923 a->maximumRecordSize);
924 for (recno = start; reclist->num_records < toget; recno++)
926 bend_fetchrequest freq;
927 bend_fetchresult *fres;
928 Z_NamePlusRecord *thisrec;
929 Z_DatabaseRecord *thisext;
933 * we get the number of bytes allocated on the stream before any
934 * allocation done by the backend - this should give us a reasonable
935 * idea of the total size of the data so far.
937 total_length = odr_total(a->encode) - dumped_records;
938 freq.setname = setname;
941 freq.format = format;
942 freq.stream = a->encode;
943 if (!(fres = bend_fetch(a->backend, &freq, 0)))
945 *pres = Z_PRES_FAILURE;
946 return diagrec(a, 2, "Backend interface problem");
948 /* backend should be able to signal whether error is system-wide
949 or only pertaining to current record */
952 *pres = Z_PRES_FAILURE;
953 return diagrec(a, fres->errcode, fres->errstring);
956 this_length = fres->len;
958 this_length = odr_total(a->encode) - total_length;
959 logf(LOG_DEBUG, " fetched record, len=%d, total=%d",
960 this_length, total_length);
961 if (this_length + total_length > a->preferredMessageSize)
963 /* record is small enough, really */
964 if (this_length <= a->preferredMessageSize)
966 logf(LOG_DEBUG, " Dropped last normal-sized record");
967 *pres = Z_PRES_PARTIAL_2;
970 /* record can only be fetched by itself */
971 if (this_length < a->maximumRecordSize)
973 logf(LOG_DEBUG, " Record > prefmsgsz");
976 logf(LOG_DEBUG, " Dropped it");
977 reclist->records[reclist->num_records] =
978 surrogatediagrec(a, fres->basename, 16, 0);
979 reclist->num_records++;
980 *next = fres->last_in_set ? 0 : recno + 1;
981 dumped_records += this_length;
985 else /* too big entirely */
987 logf(LOG_DEBUG, "Record > maxrcdsz");
988 reclist->records[reclist->num_records] =
989 surrogatediagrec(a, fres->basename, 17, 0);
990 reclist->num_records++;
991 *next = fres->last_in_set ? 0 : recno + 1;
992 dumped_records += this_length;
996 if (!(thisrec = odr_malloc(a->encode, sizeof(*thisrec))))
998 if (!(thisrec->databaseName = odr_malloc(a->encode,
999 strlen(fres->basename) + 1)))
1001 strcpy(thisrec->databaseName, fres->basename);
1002 thisrec->which = Z_NamePlusRecord_databaseRecord;
1003 if (!(thisrec->u.databaseRecord = thisext = odr_malloc(a->encode,
1004 sizeof(Z_DatabaseRecord))))
1006 recform.proto = a->proto;
1007 recform.oclass = CLASS_RECSYN;
1008 recform.value = fres->format;
1009 thisext->direct_reference = odr_oiddup(a->encode,
1010 oid_ent_to_oid(&recform, oid));
1011 thisext->indirect_reference = 0;
1012 thisext->descriptor = 0;
1013 if (fres->len < 0) /* Structured data */
1015 switch (fres->format)
1017 case VAL_SUTRS: thisext->which = Z_External_sutrs; break;
1018 case VAL_GRS1: thisext->which = Z_External_grs1; break;
1019 case VAL_EXPLAIN: thisext->which = Z_External_explainRecord;
1021 case VAL_SUMMARY: thisext->which = Z_External_summary; break;
1022 case VAL_OPAC: thisext->which = Z_External_OPAC; break;
1025 logf(LOG_FATAL, "Unknown structured format from backend.");
1030 * We cheat on the pointers here. Obviously, the record field
1031 * of the backend-fetch structure should have been a union for
1032 * correctness, but we're stuck with this for backwards
1035 thisext->u.grs1 = (Z_GenericRecord*) fres->record;
1037 else if (fres->format == VAL_SUTRS) /* SUTRS is a single-ASN.1-type */
1039 Odr_oct *sutrs = odr_malloc(a->encode, sizeof(*sutrs));
1041 thisext->which = Z_External_sutrs;
1042 thisext->u.sutrs = sutrs;
1043 sutrs->buf = odr_malloc(a->encode, fres->len);
1044 sutrs->len = sutrs->size = fres->len;
1045 memcpy(sutrs->buf, fres->record, fres->len);
1047 else /* octet-aligned record. */
1049 thisext->which = Z_External_octet;
1050 if (!(thisext->u.octet_aligned = odr_malloc(a->encode,
1053 if (!(thisext->u.octet_aligned->buf = odr_malloc(a->encode,
1056 memcpy(thisext->u.octet_aligned->buf, fres->record, fres->len);
1057 thisext->u.octet_aligned->len = thisext->u.octet_aligned->size =
1060 reclist->records[reclist->num_records] = thisrec;
1061 reclist->num_records++;
1062 *next = fres->last_in_set ? 0 : recno + 1;
1064 *num = reclist->num_records;
1068 static Z_APDU *process_searchRequest(association *assoc, request *reqb,
1071 Z_SearchRequest *req = reqb->request->u.searchRequest;
1072 bend_search_rr *bsrr = nmem_malloc (reqb->request_mem, sizeof(*bsrr));
1074 logf(LOG_LOG, "Got SearchRequest.");
1076 save_referenceId (reqb, req->referenceId);
1077 /* store ref id in request */
1079 bsrr->request = reqb;
1080 bsrr->association = assoc;
1081 if (assoc->bend_search)
1083 bsrr->setname = req->resultSetName;
1084 bsrr->replace_set = *req->replaceIndicator;
1085 bsrr->num_bases = req->num_databaseNames;
1086 bsrr->basenames = req->databaseNames;
1087 bsrr->query = req->query;
1088 bsrr->stream = assoc->encode;
1091 bsrr->errstring = NULL;
1092 (*assoc->bend_search)(assoc->backend, bsrr);
1098 bend_searchrequest bsrq;
1099 bend_searchresult *bsrt;
1101 bsrq.setname = req->resultSetName;
1102 bsrq.replace_set = *req->replaceIndicator;
1103 bsrq.num_bases = req->num_databaseNames;
1104 bsrq.basenames = req->databaseNames;
1105 bsrq.query = req->query;
1106 bsrq.stream = assoc->encode;
1107 if (!(bsrt = bend_search(assoc->backend, &bsrq, fd)))
1109 bsrr->hits = bsrt->hits;
1110 bsrr->errcode = bsrt->errcode;
1111 bsrr->errstring = bsrt->errstring;
1113 return response_searchRequest(assoc, reqb, bsrr, fd);
1116 int bend_searchresponse(void *handle, bend_search_rr *bsrr) {return 0;}
1119 * Prepare a searchresponse based on the backend results. We probably want
1120 * to look at making the fetching of records nonblocking as well, but
1121 * so far, we'll keep things simple.
1122 * If bsrt is null, that means we're called in response to a communications
1123 * event, and we'll have to get the response for ourselves.
1125 static Z_APDU *response_searchRequest(association *assoc, request *reqb,
1126 bend_search_rr *bsrt, int *fd)
1128 Z_SearchRequest *req = reqb->request->u.searchRequest;
1129 Z_APDU *apdu = odr_malloc (assoc->encode, sizeof(*apdu));
1130 Z_SearchResponse *resp = odr_malloc (assoc->encode, sizeof(*resp));
1131 int *nulint = odr_malloc (assoc->encode, sizeof(*nulint));
1132 bool_t *sr = odr_malloc (assoc->encode, sizeof(*sr));
1133 int *next = odr_malloc (assoc->encode, sizeof(*next));
1134 int *none = odr_malloc (assoc->encode, sizeof(*none));
1141 apdu->which = Z_APDU_searchResponse;
1142 apdu->u.searchResponse = resp;
1143 resp->referenceId = req->referenceId;
1144 resp->additionalSearchInfo = 0;
1145 resp->otherInfo = 0;
1147 if (!bsrt && !bend_searchresponse(assoc->backend, bsrt))
1149 logf(LOG_FATAL, "Bad result from backend");
1152 else if (bsrt->errcode)
1154 resp->records = diagrec(assoc, bsrt->errcode, bsrt->errstring);
1155 resp->resultCount = nulint;
1156 resp->numberOfRecordsReturned = nulint;
1157 resp->nextResultSetPosition = nulint;
1158 resp->searchStatus = nulint;
1159 resp->resultSetStatus = none;
1160 resp->presentStatus = 0;
1164 int *toget = odr_malloc (assoc->encode, sizeof(*toget));
1165 int *presst = odr_malloc (assoc->encode, sizeof(*presst));
1166 Z_RecordComposition comp, *compp = 0;
1171 resp->resultCount = &bsrt->hits;
1173 comp.which = Z_RecordComp_simple;
1174 /* how many records does the user agent want, then? */
1175 if (bsrt->hits <= *req->smallSetUpperBound)
1177 *toget = bsrt->hits;
1178 if ((comp.u.simple = req->smallSetElementSetNames))
1181 else if (bsrt->hits < *req->largeSetLowerBound)
1183 *toget = *req->mediumSetPresentNumber;
1184 if (*toget > bsrt->hits)
1185 *toget = bsrt->hits;
1186 if ((comp.u.simple = req->mediumSetElementSetNames))
1192 if (*toget && !resp->records)
1197 if (!(prefformat = oid_getentbyoid(req->preferredRecordSyntax)) ||
1198 prefformat->oclass != CLASS_RECSYN)
1201 form = prefformat->value;
1202 resp->records = pack_records(assoc, req->resultSetName, 1,
1203 toget, compp, next, presst, form);
1206 resp->numberOfRecordsReturned = toget;
1207 resp->nextResultSetPosition = next;
1208 resp->searchStatus = sr;
1209 resp->resultSetStatus = 0;
1210 resp->presentStatus = presst;
1214 if (*resp->resultCount)
1216 resp->numberOfRecordsReturned = nulint;
1217 resp->nextResultSetPosition = next;
1218 resp->searchStatus = sr;
1219 resp->resultSetStatus = 0;
1220 resp->presentStatus = 0;
1227 * Maybe we got a little over-friendly when we designed bend_fetch to
1228 * get only one record at a time. Some backends can optimise multiple-record
1229 * fetches, and at any rate, there is some overhead involved in
1230 * all that selecting and hopping around. Problem is, of course, that the
1231 * frontend can't know ahead of time how many records it'll need to
1232 * fill the negotiated PDU size. Annoying. Segmentation or not, Z/SR
1233 * is downright lousy as a bulk data transfer protocol.
1235 * To start with, we'll do the fetching of records from the backend
1236 * in one operation: To save some trips in and out of the event-handler,
1237 * and to simplify the interface to pack_records. At any rate, asynch
1238 * operation is more fun in operations that have an unpredictable execution
1239 * speed - which is normally more true for search than for present.
1241 static Z_APDU *process_presentRequest(association *assoc, request *reqb,
1244 Z_PresentRequest *req = reqb->request->u.presentRequest;
1248 Z_PresentResponse *resp;
1253 logf(LOG_LOG, "Got PresentRequest.");
1255 if (!(prefformat = oid_getentbyoid(req->preferredRecordSyntax)) ||
1256 prefformat->oclass != CLASS_RECSYN)
1259 form = prefformat->value;
1260 if (assoc->bend_present)
1262 bend_present_rr *bprr = nmem_malloc (reqb->request_mem, sizeof(*bprr));
1263 bprr->setname = req->resultSetId;
1264 bprr->start = *req->resultSetStartPoint;
1265 bprr->number = *req->numberOfRecordsRequested;
1266 bprr->format = form;
1267 bprr->comp = req->recordComposition;
1268 bprr->stream = assoc->encode;
1269 bprr->request = reqb;
1270 bprr->association = assoc;
1272 bprr->errstring = NULL;
1273 (*assoc->bend_present)(assoc->backend, bprr);
1278 apdu = odr_malloc (assoc->encode, sizeof(*apdu));
1279 resp = odr_malloc (assoc->encode, sizeof(*resp));
1280 presst = odr_malloc (assoc->encode, sizeof(*presst));
1281 next = odr_malloc (assoc->encode, sizeof(*next));
1282 num = odr_malloc (assoc->encode, sizeof(*num));
1285 *num = *req->numberOfRecordsRequested;
1287 apdu->which = Z_APDU_presentResponse;
1288 apdu->u.presentResponse = resp;
1289 resp->referenceId = req->referenceId;
1290 resp->otherInfo = 0;
1293 pack_records(assoc, req->resultSetId, *req->resultSetStartPoint,
1294 num, req->recordComposition, next, presst, form);
1297 resp->numberOfRecordsReturned = num;
1298 resp->presentStatus = presst;
1299 resp->nextResultSetPosition = next;
1305 * Scan was implemented rather in a hurry, and with support for only the basic
1306 * elements of the service in the backend API. Suggestions are welcome.
1308 static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd)
1310 Z_ScanRequest *req = reqb->request->u.scanRequest;
1311 Z_APDU *apdu = odr_malloc (assoc->encode, sizeof(*apdu));
1312 Z_ScanResponse *res = odr_malloc (assoc->encode, sizeof(*res));
1313 int *scanStatus = odr_malloc (assoc->encode, sizeof(*scanStatus));
1314 int *numberOfEntriesReturned =
1315 odr_malloc (assoc->encode, sizeof(*numberOfEntriesReturned));
1316 Z_ListEntries *ents = odr_malloc (assoc->encode, sizeof(*ents));
1318 bend_scanrequest srq;
1319 bend_scanresult *srs;
1322 logf(LOG_LOG, "Got ScanRequest");
1323 *scanStatus = Z_Scan_failure;
1324 *numberOfEntriesReturned = 0;
1326 apdu->which = Z_APDU_scanResponse;
1327 apdu->u.scanResponse = res;
1328 res->referenceId = req->referenceId;
1330 res->scanStatus = scanStatus;
1331 res->numberOfEntriesReturned = numberOfEntriesReturned;
1332 res->positionOfTerm = 0;
1333 res->entries = ents;
1334 ents->which = Z_ListEntries_nonSurrogateDiagnostics;
1335 res->attributeSet = 0;
1338 if (req->attributeSet && (!(attent = oid_getentbyoid(req->attributeSet)) ||
1339 attent->oclass != CLASS_ATTSET || attent->value != VAL_BIB1))
1340 ents->u.nonSurrogateDiagnostics = diagrecs(assoc, 121, 0);
1341 else if (req->stepSize && *req->stepSize > 0)
1342 ents->u.nonSurrogateDiagnostics = diagrecs(assoc, 205, 0);
1345 if (req->termListAndStartPoint->term->which == Z_Term_general)
1346 logf(LOG_DEBUG, " term: '%.*s'",
1347 req->termListAndStartPoint->term->u.general->len,
1348 req->termListAndStartPoint->term->u.general->buf);
1349 srq.num_bases = req->num_databaseNames;
1350 srq.basenames = req->databaseNames;
1351 srq.num_entries = *req->numberOfTermsRequested;
1352 srq.term = req->termListAndStartPoint;
1353 srq.stream = assoc->encode;
1354 if (!(attset = oid_getentbyoid(req->attributeSet)) ||
1355 attset->oclass != CLASS_RECSYN)
1356 srq.attributeset = VAL_NONE;
1358 srq.attributeset = attset->value;
1359 srq.term_position = req->preferredPositionInResponse ?
1360 *req->preferredPositionInResponse : 1;
1361 if (!(srs = bend_scan(assoc->backend, &srq, 0)))
1362 ents->u.nonSurrogateDiagnostics = diagrecs(assoc, 2, 0);
1363 else if (srs->errcode)
1364 ents->u.nonSurrogateDiagnostics = diagrecs(assoc,
1365 srs->errcode, srs->errstring);
1369 Z_Entries *list = odr_malloc (assoc->encode, sizeof(*list));
1370 Z_Entry **tab = odr_malloc (assoc->encode,
1371 sizeof(*tab) * srs->num_entries);
1373 if (srs->status == BEND_SCAN_PARTIAL)
1374 *scanStatus = Z_Scan_partial_5;
1376 *scanStatus = Z_Scan_success;
1377 ents->which = Z_ListEntries_entries;
1378 ents->u.entries = list;
1379 list->entries = tab;
1380 for (i = 0; i < srs->num_entries; i++)
1386 list->entries[i] = e = odr_malloc(assoc->encode, sizeof(*e));
1387 e->which = Z_Entry_termInfo;
1388 e->u.termInfo = t = odr_malloc(assoc->encode, sizeof(*t));
1389 t->suggestedAttributes = 0;
1391 t->alternativeTerm = 0;
1392 t->byAttributes = 0;
1393 t->otherTermInfo = 0;
1394 t->globalOccurrences = &srs->entries[i].occurrences;
1395 t->term = odr_malloc(assoc->encode, sizeof(*t->term));
1396 t->term->which = Z_Term_general;
1397 t->term->u.general = o = odr_malloc(assoc->encode,
1399 o->buf = odr_malloc(assoc->encode, o->len = o->size =
1400 strlen(srs->entries[i].term));
1401 memcpy(o->buf, srs->entries[i].term, o->len);
1402 logf(LOG_DEBUG, " term #%d: '%s' (%d)", i,
1403 srs->entries[i].term, srs->entries[i].occurrences);
1405 list->num_entries = i;
1406 res->numberOfEntriesReturned = &list->num_entries;
1407 res->positionOfTerm = &srs->term_position;
1414 static Z_APDU *process_sortRequest(association *assoc, request *reqb,
1417 Z_SortRequest *req = reqb->request->u.sortRequest;
1418 Z_SortResponse *res = odr_malloc (assoc->encode, sizeof(*res));
1419 bend_sort_rr *bsrr = odr_malloc (assoc->encode, sizeof(*bsrr));
1421 Z_APDU *apdu = odr_malloc (assoc->encode, sizeof(*apdu));
1423 logf(LOG_LOG, "Got SortRequest.");
1425 bsrr->num_input_setnames = req->inputResultSetNames->num_strings;
1426 bsrr->input_setnames = req->inputResultSetNames->strings;
1427 bsrr->output_setname = req->sortedResultSetName;
1428 bsrr->sort_sequence = req->sortSequence;
1429 bsrr->stream = assoc->encode;
1431 bsrr->sort_status = Z_SortStatus_failure;
1433 bsrr->errstring = 0;
1435 (*assoc->bend_sort)(assoc->backend, bsrr);
1437 res->referenceId = req->referenceId;
1438 res->sortStatus = odr_malloc (assoc->encode, sizeof(*res->sortStatus));
1439 *res->sortStatus = bsrr->sort_status;
1440 res->resultSetStatus = 0;
1442 res->diagnostics = diagrecs(assoc, bsrr->errcode, bsrr->errstring);
1444 res->diagnostics = 0;
1447 apdu->which = Z_APDU_sortResponse;
1448 apdu->u.sortResponse = res;
1452 static void process_close(association *assoc, request *reqb)
1454 Z_Close *req = reqb->request->u.close;
1455 static char *reasons[] =
1462 "securityViolation",
1469 logf(LOG_LOG, "Got Close, reason %s, message %s",
1470 reasons[*req->closeReason], req->diagnosticInformation ?
1471 req->diagnosticInformation : "NULL");
1472 if (assoc->version < 3) /* to make do_force respond with close */
1474 do_close(assoc, Z_Close_finished, "Association terminated by client");
1477 void save_referenceId (request *reqb, Z_ReferenceId *refid)
1481 reqb->len_refid = refid->len;
1482 reqb->refid = nmem_malloc (reqb->request_mem, refid->len);
1483 memcpy (reqb->refid, refid->buf, refid->len);
1487 reqb->len_refid = 0;
1492 void bend_request_send (bend_association a, bend_request req, Z_APDU *res)
1494 process_response (a, req, res);
1497 bend_request bend_request_mk (bend_association a)
1499 request *nreq = request_get (&a->outgoing);
1500 nreq->request_mem = nmem_create ();
1504 Z_ReferenceId *bend_request_getid (ODR odr, bend_request req)
1509 id = odr_malloc (odr, sizeof(*odr));
1510 id->buf = odr_malloc (odr, req->len_refid);
1511 id->len = id->size = req->len_refid;
1512 memcpy (id->buf, req->refid, req->len_refid);
1516 void bend_request_destroy (bend_request *req)
1518 nmem_destroy((*req)->request_mem);
1519 request_release(*req);
1523 int bend_backend_respond (bend_association a, bend_request req)
1525 return process_request (a, req);
1528 void bend_request_setdata(bend_request r, void *p)
1533 void *bend_request_getdata(bend_request r)
1535 return r->clientData;