2 * Copyright (c) 1995, Index Data
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.33 1995-06-06 14:57:05 quinn
10 * Revision 1.32 1995/06/06 08:41:44 quinn
13 * Revision 1.31 1995/06/06 08:15:37 quinn
16 * Revision 1.30 1995/06/05 10:53:32 quinn
17 * Added a better SCAN.
19 * Revision 1.29 1995/06/01 11:25:03 quinn
22 * Revision 1.28 1995/06/01 11:21:01 quinn
23 * Attempting to fix a bug in pack-records. replaced break with continue
24 * for large records, according to standard.
26 * Revision 1.27 1995/05/29 08:12:06 quinn
29 * Revision 1.26 1995/05/18 13:02:12 quinn
32 * Revision 1.25 1995/05/17 08:42:26 quinn
33 * Transfer auth info to backend. Allow backend to reject init gracefully.
35 * Revision 1.24 1995/05/16 08:51:04 quinn
36 * License, documentation, and memory fixes
38 * Revision 1.23 1995/05/15 13:25:10 quinn
41 * Revision 1.22 1995/05/15 11:56:39 quinn
42 * Asynchronous facilities. Restructuring of seshigh code.
44 * Revision 1.21 1995/05/02 08:53:19 quinn
45 * Trying in vain to fix comm with ISODE
47 * Revision 1.20 1995/04/20 15:13:00 quinn
50 * Revision 1.19 1995/04/18 08:15:34 quinn
51 * Added dynamic memory allocation on encoding (whew). Code is now somewhat
52 * neater. We'll make the same change for decoding one day.
54 * Revision 1.18 1995/04/17 11:28:25 quinn
57 * Revision 1.17 1995/04/10 10:23:36 quinn
58 * Some work to add scan and other things.
60 * Revision 1.16 1995/03/31 09:18:55 quinn
63 * Revision 1.15 1995/03/30 14:03:23 quinn
64 * Added RFC1006 as separate library
66 * Revision 1.14 1995/03/30 12:18:17 quinn
69 * Revision 1.13 1995/03/30 09:09:24 quinn
70 * Added state-handle and some support for asynchronous activities.
72 * Revision 1.12 1995/03/29 15:40:16 quinn
73 * Ongoing work. Statserv is now dynamic by default
75 * Revision 1.11 1995/03/28 09:16:21 quinn
76 * Added record packing to the search request
78 * Revision 1.10 1995/03/27 08:34:24 quinn
79 * Added dynamic server functionality.
80 * Released bindings to session.c (is now redundant)
82 * Revision 1.9 1995/03/22 15:01:26 quinn
83 * Adjusting record packing.
85 * Revision 1.8 1995/03/22 10:13:21 quinn
86 * Working on record packer
88 * Revision 1.7 1995/03/21 15:53:31 quinn
91 * Revision 1.6 1995/03/21 12:30:09 quinn
92 * Beginning to add support for record packing.
94 * Revision 1.5 1995/03/17 10:44:13 quinn
95 * Added catch of null-string in makediagrec
97 * Revision 1.4 1995/03/17 10:18:08 quinn
98 * Added memory management.
100 * Revision 1.3 1995/03/16 17:42:39 quinn
103 * Revision 1.2 1995/03/16 13:29:01 quinn
104 * Partitioned server.
106 * Revision 1.1 1995/03/15 16:02:10 quinn
107 * Modded session.c to seshigh.c
112 * Frontend server logic.
114 * This code receives incoming APDUs, and handles client requests by means
115 * of the backend API.
117 * Some of the code is getting quite involved, compared to simpler servers -
118 * primarily because it is asynchronous both in the communication with
119 * the user and the backend. We think the complexity will pay off in
120 * the form of greater flexibility when more asynchronous facilities
123 * Memory management has become somewhat involved. In the simple case, where
124 * only one PDU is pending at a time, it will simply reuse the same memory,
125 * once it has found its working size. When we enable multiple concurrent
126 * operations, perhaps even with multiple parallel calls to the backend, it
127 * will maintain a pool of buffers for encoding and decoding, trying to
128 * minimize memory allocation/deallocation during normal operation.
130 * TODOs include (and will be done in order of public interest):
132 * Support for EXPLAIN - provide simple meta-database system.
133 * Support for access control.
134 * Support for resource control.
135 * Support for extended services - primarily Item Order.
136 * Rest of Z39.50-1994
146 #include <comstack.h>
152 #include <statserv.h>
153 #include "../version.h"
157 static int process_request(association *assoc);
158 void backend_response(IOCHAN i, int event);
159 static int process_response(association *assoc, request *req, Z_APDU *res);
160 static Z_APDU *process_initRequest(association *assoc, request *reqb);
161 static Z_APDU *process_searchRequest(association *assoc, request *reqb,
163 static Z_APDU *response_searchRequest(association *assoc, request *reqb,
164 bend_searchresult *bsrt, int *fd);
165 static Z_APDU *process_presentRequest(association *assoc, request *reqb,
167 static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd);
169 static FILE *apduf = 0; /* for use in static mode */
170 static statserv_options_block *control_block = 0;
173 * Create and initialize a new association-handle.
174 * channel : iochannel for the current line.
175 * link : communications channel.
176 * Returns: 0 or a new association handle.
178 association *create_association(IOCHAN channel, COMSTACK link)
183 control_block = statserv_getcontrol();
184 if (!(new = malloc(sizeof(*new))))
186 new->client_chan = channel;
187 new->client_link = link;
188 if (!(new->decode = odr_createmem(ODR_DECODE)) ||
189 !(new->encode = odr_createmem(ODR_ENCODE)))
191 if (*control_block->apdufile)
196 strcpy(filename, control_block->apdufile);
197 if (!(new->print = odr_createmem(ODR_PRINT)))
199 if (*control_block->apdufile != '-')
201 strcpy(filename, control_block->apdufile);
202 if (!control_block->dynamic)
206 if (!(apduf = fopen(filename, "w")))
208 logf(LOG_WARN|LOG_ERRNO, "%s", filename);
211 setvbuf(apduf, 0, _IONBF, 0);
217 sprintf(filename + strlen(filename), ".%d", getpid());
218 if (!(f = fopen(filename, "w")))
220 logf(LOG_WARN|LOG_ERRNO, "%s", filename);
223 setvbuf(f, 0, _IONBF, 0);
225 odr_setprint(new->print, f);
230 new->input_buffer = 0;
231 new->input_buffer_len = 0;
234 request_initq(&new->incoming);
235 request_initq(&new->outgoing);
236 new->proto = cs_getproto(link);
241 * Free association and release resources.
243 void destroy_association(association *h)
245 odr_destroy(h->decode);
246 odr_destroy(h->encode);
248 odr_destroy(h->print);
250 free(h->input_buffer);
252 bend_close(h->backend);
253 while (request_deq(&h->incoming));
254 while (request_deq(&h->outgoing));
259 * This is where PDUs from the client are read and the further
260 * processing is initiated. Flow of control moves down through the
261 * various process_* functions below, until the encoded result comes back up
262 * to the output handler in here.
264 * h : the I/O channel that has an outstanding event.
265 * event : the current outstanding event.
267 void ir_session(IOCHAN h, int event)
270 association *assoc = iochan_getdata(h);
271 COMSTACK conn = assoc->client_link;
274 assert(h && conn && assoc);
275 if (event & EVENT_INPUT || event & EVENT_WORK) /* input */
277 if (event & EVENT_INPUT)
279 logf(LOG_DEBUG, "ir_session (input)");
280 assert(assoc && conn);
281 /* We aren't speaking to this fellow */
284 logf(LOG_LOG, "Closed connection after reject");
286 destroy_association(assoc);
290 if ((res = cs_get(conn, &assoc->input_buffer,
291 &assoc->input_buffer_len)) <= 0)
293 logf(LOG_LOG, "Connection closed by client");
295 destroy_association(assoc);
299 else if (res == 1) /* incomplete read - wait for more */
301 if (cs_more(conn)) /* more stuff - call us again later, please */
302 iochan_setevent(h, EVENT_INPUT);
304 /* we got a complete PDU. Let's decode it */
305 logf(LOG_DEBUG, "Got PDU, %d bytes", res);
306 req = request_get(); /* get a new request structure */
307 odr_reset(assoc->decode);
308 odr_setbuf(assoc->decode, assoc->input_buffer, res, 0);
309 if (!z_APDU(assoc->decode, &req->request, 0))
311 logf(LOG_WARN, "ODR error: %s",
312 odr_errlist[odr_geterror(assoc->decode)]);
314 destroy_association(assoc);
318 req->request_mem = odr_extract_mem(assoc->decode);
319 if (assoc->print && !z_APDU(assoc->print, &req->request, 0))
321 logf(LOG_WARN, "ODR print error: %s",
322 odr_errlist[odr_geterror(assoc->print)]);
323 odr_reset(assoc->print);
325 request_enq(&assoc->incoming, req);
328 /* can we do something yet? */
329 req = request_head(&assoc->incoming);
330 if (req->state == REQUEST_IDLE)
331 if (process_request(assoc) < 0)
334 destroy_association(assoc);
338 if (event & EVENT_OUTPUT)
340 request *req = request_head(&assoc->outgoing);
342 logf(LOG_DEBUG, "ir_session (output)");
343 req->state = REQUEST_PENDING;
344 switch (res = cs_put(conn, req->response, req->len_response))
347 logf(LOG_LOG, "Connection closed by client");
349 destroy_association(assoc);
352 case 0: /* all sent - release the request structure */
353 logf(LOG_DEBUG, "Wrote PDU, %d bytes", req->len_response);
354 odr_release_mem(req->request_mem);
355 request_deq(&assoc->outgoing);
356 request_release(req);
357 if (!request_head(&assoc->outgoing))
358 iochan_clearflag(h, EVENT_OUTPUT);
360 /* value of 1 -- partial send -- is simply ignored */
363 if (event & EVENT_EXCEPT)
365 logf(LOG_DEBUG, "ir_session (exception)");
367 destroy_association(assoc);
373 * Initiate request processing.
375 static int process_request(association *assoc)
377 request *req = request_head(&assoc->incoming);
381 logf(LOG_DEBUG, "process_request");
382 assert(req && req->state == REQUEST_IDLE);
383 switch (req->request->which)
385 case Z_APDU_initRequest:
386 res = process_initRequest(assoc, req); break;
387 case Z_APDU_searchRequest:
388 res = process_searchRequest(assoc, req, &fd); break;
389 case Z_APDU_presentRequest:
390 res = process_presentRequest(assoc, req, &fd); break;
391 case Z_APDU_scanRequest:
392 res = process_scanRequest(assoc, req, &fd); break;
394 logf(LOG_WARN, "Bad APDU received");
399 logf(LOG_DEBUG, " result immediately available");
400 return process_response(assoc, req, res);
404 logf(LOG_WARN, " bad result");
407 else /* no result yet - one will be provided later */
411 /* Set up an I/O handler for the fd supplied by the backend */
413 logf(LOG_DEBUG, " establishing handler for result");
414 req->state = REQUEST_PENDING;
415 if (!(chan = iochan_create(fd, backend_response, EVENT_INPUT)))
417 iochan_setdata(chan, assoc);
423 * Handle message from the backend.
425 void backend_response(IOCHAN i, int event)
427 association *assoc = iochan_getdata(i);
428 request *req = request_head(&assoc->incoming);
432 logf(LOG_DEBUG, "backend_response");
433 assert(assoc && req && req->state != REQUEST_IDLE);
434 /* determine what it is we're waiting for */
435 switch (req->request->which)
437 case Z_APDU_searchRequest:
438 res = response_searchRequest(assoc, req, 0, &fd); break;
440 case Z_APDU_presentRequest:
441 res = response_presentRequest(assoc, req, 0, &fd); break;
442 case Z_APDU_scanRequest:
443 res = response_scanRequest(assoc, req, 0, &fd); break;
446 logf(LOG_WARN, "Serious programmer's lapse or bug");
449 if ((res && process_response(assoc, req, res) < 0) || fd < 0)
451 logf(LOG_LOG, "Fatal error when talking to backend");
452 cs_close(assoc->client_link);
453 destroy_association(assoc);
454 iochan_destroy(assoc->client_chan);
458 else if (!res) /* no result yet - try again later */
460 logf(LOG_DEBUG, " no result yet");
461 iochan_setfd(i, fd); /* in case fd has changed */
466 * Encode response, and transfer the request structure to the outgoing queue.
468 static int process_response(association *assoc, request *req, Z_APDU *res)
470 odr_setbuf(assoc->encode, req->response, req->size_response, 1);
471 if (!z_APDU(assoc->encode, &res, 0))
473 logf(LOG_WARN, "ODR error when encoding response: %s",
474 odr_errlist[odr_geterror(assoc->decode)]);
477 req->response = odr_getbuf(assoc->encode, &req->len_response,
478 &req->size_response);
479 odr_setbuf(assoc->encode, 0, 0, 0); /* don't free if we abort later */
480 odr_reset(assoc->encode);
481 if (assoc->print && !z_APDU(assoc->print, &res, 0))
483 logf(LOG_WARN, "ODR print error: %s",
484 odr_errlist[odr_geterror(assoc->print)]);
485 odr_reset(assoc->print);
487 /* change this when we make the backend reentrant */
488 assert(req == request_head(&assoc->incoming));
489 req->state = REQUEST_IDLE;
490 request_deq(&assoc->incoming);
491 request_enq(&assoc->outgoing, req);
492 /* turn the work over to the ir_session handler */
493 iochan_setflag(assoc->client_chan, EVENT_OUTPUT);
494 /* Is there more work to be done? give that to the input handler too */
495 if (request_head(&assoc->incoming))
496 iochan_setevent(assoc->client_chan, EVENT_WORK);
501 * Handle init request.
502 * At the moment, we don't check the protocol version or the options
503 * anywhere else in the code - we just try not to do anything that would
504 * break a naive client. We'll toss 'em into the association block when
505 * we need them there.
507 static Z_APDU *process_initRequest(association *assoc, request *reqb)
509 Z_InitRequest *req = reqb->request->u.initRequest;
510 Z_APDU *apdu = zget_APDU(assoc->encode, Z_APDU_initResponse);
511 Z_InitResponse *resp = apdu->u.initResponse;
512 bend_initrequest binitreq;
513 bend_initresult *binitres;
515 logf(LOG_LOG, "Got initRequest");
516 if (req->implementationId)
517 logf(LOG_LOG, "Id: %s", req->implementationId);
518 if (req->implementationName)
519 logf(LOG_LOG, "Name: %s", req->implementationName);
520 if (req->implementationVersion)
521 logf(LOG_LOG, "Version: %s", req->implementationVersion);
523 binitreq.configname = "default-config";
524 binitreq.auth = req->idAuthentication;
525 if (!(binitres = bend_init(&binitreq)))
527 logf(LOG_WARN, "Bad response from backend.");
531 assoc->backend = binitres->handle;
532 resp->referenceId = req->referenceId;
533 /* let's tell the client what we can do */
534 if (ODR_MASK_GET(req->options, Z_Options_search))
535 ODR_MASK_SET(resp->options, Z_Options_search);
536 if (ODR_MASK_GET(req->options, Z_Options_present))
537 ODR_MASK_SET(resp->options, Z_Options_present);
539 if (ODR_MASK_GET(req->options, Z_Options_delSet))
540 ODR_MASK_SET(&options, Z_Options_delSet);
542 if (ODR_MASK_GET(req->options, Z_Options_namedResultSets))
543 ODR_MASK_SET(resp->options, Z_Options_namedResultSets);
544 if (ODR_MASK_GET(req->options, Z_Options_scan))
545 ODR_MASK_SET(resp->options, Z_Options_scan);
546 if (ODR_MASK_GET(req->options, Z_Options_concurrentOperations))
547 ODR_MASK_SET(resp->options, Z_Options_concurrentOperations);
549 if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_1))
550 ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_1);
551 if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_2))
552 ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_2);
553 assoc->maximumRecordSize = *req->maximumRecordSize;
554 if (assoc->maximumRecordSize > control_block->maxrecordsize)
555 assoc->maximumRecordSize = control_block->maxrecordsize;
556 assoc->preferredMessageSize = *req->preferredMessageSize;
557 if (assoc->preferredMessageSize > assoc->maximumRecordSize)
558 assoc->preferredMessageSize = assoc->maximumRecordSize;
559 resp->preferredMessageSize = &assoc->preferredMessageSize;
560 resp->maximumRecordSize = &assoc->maximumRecordSize;
561 resp->implementationName = "Index Data/YAZ Generic Frontend Server";
562 if (binitres->errcode)
564 logf(LOG_LOG, "Connection rejected by backend.");
572 * These functions should be merged.
576 * nonsurrogate diagnostic record.
578 static Z_Records *diagrec(oid_proto proto, int error, char *addinfo)
580 static Z_Records rec;
586 bib1.class = CLASS_DIAGSET;
587 bib1.value = VAL_BIB1;
589 logf(LOG_DEBUG, "Diagnostic: %d -- %s", error, addinfo ? addinfo :
592 rec.which = Z_Records_NSD;
593 rec.u.nonSurrogateDiagnostic = &dr;
594 dr.diagnosticSetId = oid_getoidbyent(&bib1);
596 dr.addinfo = addinfo ? addinfo : "";
601 * surrogate diagnostic.
603 static Z_NamePlusRecord *surrogatediagrec(oid_proto proto, char *dbname,
604 int error, char *addinfo)
606 static Z_NamePlusRecord rec;
612 bib1.class = CLASS_DIAGSET;
613 bib1.value = VAL_BIB1;
615 logf(LOG_DEBUG, "SurrogateDiagnotic: %d -- %s", error, addinfo);
617 rec.databaseName = dbname;
618 rec.which = Z_NamePlusRecord_surrogateDiagnostic;
619 rec.u.surrogateDiagnostic = &dr;
620 dr.diagnosticSetId = oid_getoidbyent(&bib1);
622 dr.addinfo = addinfo ? addinfo : "";
627 * multiple nonsurrogate diagnostics.
629 static Z_DiagRecs *diagrecs(oid_proto proto, int error, char *addinfo)
631 static Z_DiagRecs recs;
632 static Z_DiagRec *recp[1], rec;
636 logf(LOG_DEBUG, "DiagRecs: %d -- %s", error, addinfo);
638 bib1.class = CLASS_DIAGSET;
639 bib1.value = VAL_BIB1;
642 recs.num_diagRecs = 1;
643 recs.diagRecs = recp;
645 rec.diagnosticSetId = oid_getoidbyent(&bib1);
646 rec.condition = &err;
647 rec.addinfo = addinfo ? addinfo : "";
651 #define MAX_RECORDS 256
653 static Z_Records *pack_records(association *a, char *setname, int start,
654 int *num, Z_ElementSetNames *esn,
655 int *next, int *pres)
657 int recno, total_length = 0, toget = *num;
658 static Z_Records records;
659 static Z_NamePlusRecordList reclist;
660 static Z_NamePlusRecord *list[MAX_RECORDS];
664 records.which = Z_Records_DBOSD;
665 records.u.databaseOrSurDiagnostics = &reclist;
666 reclist.num_records = 0;
667 reclist.records = list;
668 *pres = Z_PRES_SUCCESS;
672 recform.proto = a->proto;
673 recform.class = CLASS_RECSYN;
674 recform.value = VAL_USMARC;
675 if (!(oid = odr_oiddup(a->encode, oid_getoidbyent(&recform))))
678 logf(LOG_DEBUG, "Request to pack %d+%d", start, toget);
679 logf(LOG_DEBUG, "pms=%d, mrs=%d", a->preferredMessageSize,
680 a->maximumRecordSize);
681 for (recno = start; reclist.num_records < toget; recno++)
683 bend_fetchrequest freq;
684 bend_fetchresult *fres;
685 Z_NamePlusRecord *thisrec;
686 Z_DatabaseRecord *thisext;
688 if (reclist.num_records == MAX_RECORDS - 1)
690 *pres = Z_PRES_PARTIAL_2;
693 freq.setname = setname;
695 if (!(fres = bend_fetch(a->backend, &freq, 0)))
697 *pres = Z_PRES_FAILURE;
698 return diagrec(a->proto, 2, "Backend interface problem");
700 /* backend should be able to signal whether error is system-wide
701 or only pertaining to current record */
704 *pres = Z_PRES_FAILURE;
705 return diagrec(a->proto, fres->errcode, fres->errstring);
707 logf(LOG_DEBUG, " fetched record, len=%d, total=%d",
708 fres->len, total_length);
709 if (fres->len + total_length > a->preferredMessageSize)
711 /* record is small enough, really */
712 if (fres->len <= a->preferredMessageSize)
714 logf(LOG_DEBUG, " Dropped last normal-sized record");
715 *pres = Z_PRES_PARTIAL_2;
718 /* record can only be fetched by itself */
719 if (fres->len < a->maximumRecordSize)
721 logf(LOG_DEBUG, " Record > prefmsgsz");
724 logf(LOG_DEBUG, " Dropped it");
725 reclist.records[reclist.num_records] =
726 surrogatediagrec(a->proto, fres->basename, 16, 0);
727 reclist.num_records++;
728 total_length += 10; /* totally arbitrary */
732 else /* too big entirely */
734 logf(LOG_DEBUG, "Record > maxrcdsz");
735 reclist.records[reclist.num_records] =
736 surrogatediagrec(a->proto, fres->basename, 17, 0);
737 reclist.num_records++;
738 total_length += 10; /* totally arbitrary */
742 if (!(thisrec = odr_malloc(a->encode, sizeof(*thisrec))))
744 if (!(thisrec->databaseName = odr_malloc(a->encode,
745 strlen(fres->basename) + 1)))
747 strcpy(thisrec->databaseName, fres->basename);
748 thisrec->which = Z_NamePlusRecord_databaseRecord;
749 if (!(thisrec->u.databaseRecord = thisext = odr_malloc(a->encode,
750 sizeof(Z_DatabaseRecord))))
752 thisext->direct_reference = oid; /* should be OID for current MARC */
753 thisext->indirect_reference = 0;
754 thisext->descriptor = 0;
755 thisext->which = ODR_EXTERNAL_octet;
756 if (!(thisext->u.octet_aligned = odr_malloc(a->encode,
759 if (!(thisext->u.octet_aligned->buf = odr_malloc(a->encode, fres->len)))
761 memcpy(thisext->u.octet_aligned->buf, fres->record, fres->len);
762 thisext->u.octet_aligned->len = thisext->u.octet_aligned->size =
764 reclist.records[reclist.num_records] = thisrec;
765 reclist.num_records++;
766 total_length += fres->len;
767 *next = fres->last_in_set ? 0 : recno + 1;
769 *num = reclist.num_records;
773 static Z_APDU *process_searchRequest(association *assoc, request *reqb,
776 Z_SearchRequest *req = reqb->request->u.searchRequest;
777 bend_searchrequest bsrq;
778 bend_searchresult *bsrt;
780 logf(LOG_LOG, "Got SearchRequest.");
782 bsrq.setname = req->resultSetName;
783 bsrq.replace_set = *req->replaceIndicator;
784 bsrq.num_bases = req->num_databaseNames;
785 bsrq.basenames = req->databaseNames;
786 bsrq.query = req->query;
788 if (!(bsrt = bend_search(assoc->backend, &bsrq, fd)))
790 return response_searchRequest(assoc, reqb, bsrt, fd);
793 bend_searchresult *bend_searchresponse(void *handle) {return 0;}
796 * Prepare a searchresponse based on the backend results. We probably want
797 * to look at making the fetching of records nonblocking as well, but
798 * so far, we'll keep things simple.
799 * If bsrt is null, that means we're called in response to a communications
800 * event, and we'll have to get the response for ourselves.
802 static Z_APDU *response_searchRequest(association *assoc, request *reqb,
803 bend_searchresult *bsrt, int *fd)
805 Z_SearchRequest *req = reqb->request->u.searchRequest;
807 static Z_SearchResponse resp;
808 static int nulint = 0;
809 static bool_t sr = 1;
811 static int none = Z_RES_NONE;
813 apdu.which = Z_APDU_searchResponse;
814 apdu.u.searchResponse = &resp;
815 resp.referenceId = req->referenceId;
817 if (!bsrt && !(bsrt = bend_searchresponse(assoc->backend)))
819 logf(LOG_FATAL, "Bad result from backend");
822 else if (bsrt->errcode)
824 resp.records = diagrec(assoc->proto, bsrt->errcode,
826 resp.resultCount = &nulint;
827 resp.numberOfRecordsReturned = &nulint;
828 resp.nextResultSetPosition = &nulint;
829 resp.searchStatus = &nulint;
830 resp.resultSetStatus = &none;
831 resp.presentStatus = 0;
836 Z_ElementSetNames *setnames;
840 resp.resultCount = &bsrt->hits;
842 /* how many records does the user agent want, then? */
843 if (bsrt->hits <= *req->smallSetUpperBound)
846 setnames = req->smallSetElementSetNames;
848 else if (bsrt->hits < *req->largeSetLowerBound)
850 toget = *req->mediumSetPresentNumber;
851 if (toget > bsrt->hits)
853 setnames = req->mediumSetElementSetNames;
858 if (toget && !resp.records)
860 resp.records = pack_records(assoc, req->resultSetName, 1,
861 &toget, setnames, &next, &presst);
864 resp.numberOfRecordsReturned = &toget;
865 resp.nextResultSetPosition = &next;
866 resp.searchStatus = &sr;
867 resp.resultSetStatus = 0;
868 resp.presentStatus = &presst;
872 resp.numberOfRecordsReturned = &nulint;
873 resp.nextResultSetPosition = &next;
874 resp.searchStatus = &sr;
875 resp.resultSetStatus = 0;
876 resp.presentStatus = 0;
883 * Maybe we got a little over-friendly when we designed bend_fetch to
884 * get only one record at a time. Some backends can optimise multiple-record
885 * fetches, and at any rate, there is some overhead involved in
886 * all that selecting and hopping around. Problem is, of course, that the
887 * frontend can't know ahead of time how many records it'll need to
888 * fill the negotiated PDU size. Annoying. Segmentation or not, Z/SR
889 * is downright lousy as a bulk data transfer protocol.
891 * To start with, we'll do the fetching of records from the backend
892 * in one operation: To save some trips in and out of the event-handler,
893 * and to simplify the interface to pack_records. At any rate, asynch
894 * operation is more fun in operations that have an unpredictable execution
895 * speed - which is normally more true for search than for present.
897 static Z_APDU *process_presentRequest(association *assoc, request *reqb,
900 Z_PresentRequest *req = reqb->request->u.presentRequest;
902 static Z_PresentResponse resp;
903 static int presst, next, num;
905 logf(LOG_LOG, "Got PresentRequest.");
906 apdu.which = Z_APDU_presentResponse;
907 apdu.u.presentResponse = &resp;
908 resp.referenceId = req->referenceId;
910 num = *req->numberOfRecordsRequested;
911 resp.records = pack_records(assoc, req->resultSetId,
912 *req->resultSetStartPoint, &num, req->elementSetNames, &next, &presst);
915 resp.numberOfRecordsReturned = #
916 resp.presentStatus = &presst;
917 resp.nextResultSetPosition = &next;
923 * Scan was implemented rather in a hurry, and with support for only the basic
924 * elements of the service in the backend API. Suggestions are welcome.
926 static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd)
928 Z_ScanRequest *req = reqb->request->u.scanRequest;
930 static Z_ScanResponse res;
931 static int scanStatus = Z_Scan_failure;
932 static int numberOfEntriesReturned = 0;
934 static Z_ListEntries ents;
935 #define SCAN_MAX_ENTRIES 200
936 static Z_Entry *tab[SCAN_MAX_ENTRIES];
937 bend_scanrequest srq;
938 bend_scanresult *srs;
940 logf(LOG_LOG, "Got scanrequest");
941 apdu.which = Z_APDU_scanResponse;
942 apdu.u.scanResponse = &res;
943 res.referenceId = req->referenceId;
945 res.scanStatus = &scanStatus;
946 res.numberOfEntriesReturned = &numberOfEntriesReturned;
947 res.positionOfTerm = 0;
949 ents.which = Z_ListEntries_nonSurrogateDiagnostics;
950 res.attributeSet = 0;
952 if (req->attributeSet && (!(attent = oid_getentbyoid(req->attributeSet)) ||
953 attent->class != CLASS_ATTSET || attent->value != VAL_BIB1))
954 ents.u.nonSurrogateDiagnostics = diagrecs(assoc->proto, 121, 0);
955 else if (req->stepSize && *req->stepSize > 0)
956 ents.u.nonSurrogateDiagnostics = diagrecs(assoc->proto, 205, 0);
959 if (req->termListAndStartPoint->term->which == Z_Term_general)
960 logf(LOG_DEBUG, " term: '%.*s'",
961 req->termListAndStartPoint->term->u.general->len,
962 req->termListAndStartPoint->term->u.general->buf);
963 srq.num_bases = req->num_databaseNames;
964 srq.basenames = req->databaseNames;
965 srq.num_entries = *req->numberOfTermsRequested;
966 srq.term = req->termListAndStartPoint;
967 srq.term_position = req->preferredPositionInResponse ?
968 *req->preferredPositionInResponse : 1;
969 if (!(srs = bend_scan(assoc->backend, &srq, 0)))
970 ents.u.nonSurrogateDiagnostics = diagrecs(assoc->proto, 2, 0);
971 else if (srs->errcode)
972 ents.u.nonSurrogateDiagnostics = diagrecs(assoc->proto,
973 srs->errcode, srs->errstring);
977 static Z_Entries list;
979 if (srs->status == BEND_SCAN_PARTIAL)
980 scanStatus = Z_Scan_partial_5;
982 scanStatus = Z_Scan_success;
983 ents.which = Z_ListEntries_entries;
984 ents.u.entries = &list;
986 for (i = 0; i < srs->num_entries; i++)
992 if (i >= SCAN_MAX_ENTRIES)
994 scanStatus = Z_Scan_partial_4;
997 list.entries[i] = e = odr_malloc(assoc->encode, sizeof(*e));
998 e->which = Z_Entry_termInfo;
999 e->u.termInfo = t = odr_malloc(assoc->encode, sizeof(*t));
1000 t->suggestedAttributes = 0;
1001 t->alternativeTerm = 0;
1002 t->byAttributes = 0;
1003 t->globalOccurrences = &srs->entries[i].occurrences;
1004 t->term = odr_malloc(assoc->encode, sizeof(*t->term));
1005 t->term->which = Z_Term_general;
1006 t->term->u.general = o = odr_malloc(assoc->encode,
1008 o->buf = odr_malloc(assoc->encode, o->len = o->size =
1009 strlen(srs->entries[i].term));
1010 memcpy(o->buf, srs->entries[i].term, o->len);
1011 logf(LOG_DEBUG, " term #%d: '%s' (%d)", i,
1012 srs->entries[i].term, srs->entries[i].occurrences);
1014 list.num_entries = i;
1015 res.numberOfEntriesReturned = &list.num_entries;
1016 res.positionOfTerm = &srs->term_position;