2 * Copyright (c) 1995, Index Data
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.26 1995-05-18 13:02:12 quinn
10 * Revision 1.25 1995/05/17 08:42:26 quinn
11 * Transfer auth info to backend. Allow backend to reject init gracefully.
13 * Revision 1.24 1995/05/16 08:51:04 quinn
14 * License, documentation, and memory fixes
16 * Revision 1.23 1995/05/15 13:25:10 quinn
19 * Revision 1.22 1995/05/15 11:56:39 quinn
20 * Asynchronous facilities. Restructuring of seshigh code.
22 * Revision 1.21 1995/05/02 08:53:19 quinn
23 * Trying in vain to fix comm with ISODE
25 * Revision 1.20 1995/04/20 15:13:00 quinn
28 * Revision 1.19 1995/04/18 08:15:34 quinn
29 * Added dynamic memory allocation on encoding (whew). Code is now somewhat
30 * neater. We'll make the same change for decoding one day.
32 * Revision 1.18 1995/04/17 11:28:25 quinn
35 * Revision 1.17 1995/04/10 10:23:36 quinn
36 * Some work to add scan and other things.
38 * Revision 1.16 1995/03/31 09:18:55 quinn
41 * Revision 1.15 1995/03/30 14:03:23 quinn
42 * Added RFC1006 as separate library
44 * Revision 1.14 1995/03/30 12:18:17 quinn
47 * Revision 1.13 1995/03/30 09:09:24 quinn
48 * Added state-handle and some support for asynchronous activities.
50 * Revision 1.12 1995/03/29 15:40:16 quinn
51 * Ongoing work. Statserv is now dynamic by default
53 * Revision 1.11 1995/03/28 09:16:21 quinn
54 * Added record packing to the search request
56 * Revision 1.10 1995/03/27 08:34:24 quinn
57 * Added dynamic server functionality.
58 * Released bindings to session.c (is now redundant)
60 * Revision 1.9 1995/03/22 15:01:26 quinn
61 * Adjusting record packing.
63 * Revision 1.8 1995/03/22 10:13:21 quinn
64 * Working on record packer
66 * Revision 1.7 1995/03/21 15:53:31 quinn
69 * Revision 1.6 1995/03/21 12:30:09 quinn
70 * Beginning to add support for record packing.
72 * Revision 1.5 1995/03/17 10:44:13 quinn
73 * Added catch of null-string in makediagrec
75 * Revision 1.4 1995/03/17 10:18:08 quinn
76 * Added memory management.
78 * Revision 1.3 1995/03/16 17:42:39 quinn
81 * Revision 1.2 1995/03/16 13:29:01 quinn
84 * Revision 1.1 1995/03/15 16:02:10 quinn
85 * Modded session.c to seshigh.c
90 * Frontend server logic.
92 * This code receives incoming APDUs, and handles client requests by means
95 * Some of the code is getting quite involved, compared to simpler servers -
96 * primarily because it is asynchronous both in the communication with
97 * the user and the backend. We think the complexity will pay off in
98 * the form of greater flexibility when more asynchronous facilities
101 * Memory management has become somewhat involved. In the simple case, where
102 * only one PDU is pending at a time, it will simply reuse the same memory,
103 * once it has found its working size. When we enable multiple concurrent
104 * operations, perhaps even with multiple parallel calls to the backend, it
105 * will maintain a pool of buffers for encoding and decoding, trying to
106 * minimize memory allocation/deallocation during normal operation.
108 * TODOs include (and will be done in order of public interest):
110 * Support for EXPLAIN - provide simple meta-database system.
111 * Support for access control.
112 * Support for resource control.
113 * Support for extended services - primarily Item Order.
114 * Rest of Z39.50-1994
124 #include <comstack.h>
130 #include <statserv.h>
131 #include "../version.h"
135 static int process_request(association *assoc);
136 void backend_response(IOCHAN i, int event);
137 static int process_response(association *assoc, request *req, Z_APDU *res);
138 static Z_APDU *process_initRequest(association *assoc, request *reqb);
139 static Z_APDU *process_searchRequest(association *assoc, request *reqb,
141 static Z_APDU *response_searchRequest(association *assoc, request *reqb,
142 bend_searchresult *bsrt, int *fd);
143 static Z_APDU *process_presentRequest(association *assoc, request *reqb,
145 static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd);
147 static FILE *apduf = 0; /* for use in static mode */
148 static statserv_options_block *control_block = 0;
151 * Create and initialize a new association-handle.
152 * channel : iochannel for the current line.
153 * link : communications channel.
154 * Returns: 0 or a new association handle.
156 association *create_association(IOCHAN channel, COMSTACK link)
161 control_block = statserv_getcontrol();
162 if (!(new = malloc(sizeof(*new))))
164 new->client_chan = channel;
165 new->client_link = link;
166 if (!(new->decode = odr_createmem(ODR_DECODE)) ||
167 !(new->encode = odr_createmem(ODR_ENCODE)))
169 if (*control_block->apdufile)
174 strcpy(filename, control_block->apdufile);
175 if (!(new->print = odr_createmem(ODR_PRINT)))
177 if (*control_block->apdufile != '-')
179 strcpy(filename, control_block->apdufile);
180 if (!control_block->dynamic)
184 if (!(apduf = fopen(filename, "w")))
186 logf(LOG_WARN|LOG_ERRNO, "%s", filename);
189 setvbuf(apduf, 0, _IONBF, 0);
195 sprintf(filename + strlen(filename), ".%d", getpid());
196 if (!(f = fopen(filename, "w")))
198 logf(LOG_WARN|LOG_ERRNO, "%s", filename);
201 setvbuf(f, 0, _IONBF, 0);
203 odr_setprint(new->print, f);
208 new->input_buffer = 0;
209 new->input_buffer_len = 0;
212 request_initq(&new->incoming);
213 request_initq(&new->outgoing);
214 if (cs_getproto(link) == CS_Z3950)
215 new->proto = PROTO_Z3950;
217 new->proto = PROTO_SR;
222 * Free association and release resources.
224 void destroy_association(association *h)
226 odr_destroy(h->decode);
227 odr_destroy(h->encode);
229 odr_destroy(h->print);
231 free(h->input_buffer);
233 bend_close(h->backend);
234 while (request_deq(&h->incoming));
235 while (request_deq(&h->outgoing));
240 * This is where PDUs from the client are read and the further
241 * processing is initiated. Flow of control moves down through the
242 * various process_* functions below, until the encoded result comes back up
243 * to the output handler in here.
245 * h : the I/O channel that has an outstanding event.
246 * event : the current outstanding event.
248 void ir_session(IOCHAN h, int event)
251 association *assoc = iochan_getdata(h);
252 COMSTACK conn = assoc->client_link;
255 assert(h && conn && assoc);
256 if (event & EVENT_INPUT || event & EVENT_WORK) /* input */
258 if (event & EVENT_INPUT)
260 logf(LOG_DEBUG, "ir_session (input)");
261 assert(assoc && conn);
264 logf(LOG_LOG, "Closed connection after reject");
266 destroy_association(assoc);
270 if ((res = cs_get(conn, &assoc->input_buffer,
271 &assoc->input_buffer_len)) <= 0)
273 logf(LOG_LOG, "Connection closed by client");
275 destroy_association(assoc);
279 else if (res == 1) /* incomplete read - wait for more */
281 if (cs_more(conn)) /* more stuff - call us again later, please */
282 iochan_setevent(h, EVENT_INPUT);
284 /* we got a complete PDU. Let's decode it */
285 req = request_get(); /* get a new request structure */
286 odr_reset(assoc->decode);
287 odr_setbuf(assoc->decode, assoc->input_buffer, res, 0);
288 if (!z_APDU(assoc->decode, &req->request, 0))
290 logf(LOG_WARN, "ODR error: %s",
291 odr_errlist[odr_geterror(assoc->decode)]);
293 destroy_association(assoc);
297 req->request_mem = odr_extract_mem(assoc->decode);
298 if (assoc->print && !z_APDU(assoc->print, &req->request, 0))
300 logf(LOG_WARN, "ODR print error: %s",
301 odr_errlist[odr_geterror(assoc->print)]);
302 odr_reset(assoc->print);
304 request_enq(&assoc->incoming, req);
307 /* can we do something yet? */
308 req = request_head(&assoc->incoming);
309 if (req->state == REQUEST_IDLE)
310 if (process_request(assoc) < 0)
313 destroy_association(assoc);
317 if (event & EVENT_OUTPUT)
319 request *req = request_head(&assoc->outgoing);
321 logf(LOG_DEBUG, "ir_session (output)");
322 req->state = REQUEST_PENDING;
323 switch (res = cs_put(conn, req->response, req->len_response))
326 logf(LOG_LOG, "Connection closed by client");
328 destroy_association(assoc);
331 case 0: /* all sent - release the request structure */
332 odr_release_mem(req->request_mem);
333 request_deq(&assoc->outgoing);
334 request_release(req);
335 if (!request_head(&assoc->outgoing))
336 iochan_clearflag(h, EVENT_OUTPUT);
338 /* value of 1 -- partial send -- is simply ignored */
341 if (event & EVENT_EXCEPT)
343 logf(LOG_DEBUG, "ir_session (exception)");
345 destroy_association(assoc);
351 * Initiate request processing.
353 static int process_request(association *assoc)
355 request *req = request_head(&assoc->incoming);
359 logf(LOG_DEBUG, "process_request");
360 assert(req && req->state == REQUEST_IDLE);
361 switch (req->request->which)
363 case Z_APDU_initRequest:
364 res = process_initRequest(assoc, req); break;
365 case Z_APDU_searchRequest:
366 res = process_searchRequest(assoc, req, &fd); break;
367 case Z_APDU_presentRequest:
368 res = process_presentRequest(assoc, req, &fd); break;
369 case Z_APDU_scanRequest:
370 res = process_scanRequest(assoc, req, &fd); break;
372 logf(LOG_WARN, "Bad APDU");
377 logf(LOG_DEBUG, " result immediately available");
378 return process_response(assoc, req, res);
382 logf(LOG_WARN, " bad result");
385 else /* no result yet - one will be provided later */
389 /* Set up an I/O handler for the fd supplied by the backend */
391 logf(LOG_DEBUG, " establishing handler for result");
392 req->state = REQUEST_PENDING;
393 if (!(chan = iochan_create(fd, backend_response, EVENT_INPUT)))
395 iochan_setdata(chan, assoc);
401 * Handle message from the backend.
403 void backend_response(IOCHAN i, int event)
405 association *assoc = iochan_getdata(i);
406 request *req = request_head(&assoc->incoming);
410 logf(LOG_DEBUG, "backend_response");
411 assert(assoc && req && req->state != REQUEST_IDLE);
412 /* determine what it is we're waiting for */
413 switch (req->request->which)
415 case Z_APDU_searchRequest:
416 res = response_searchRequest(assoc, req, 0, &fd); break;
418 case Z_APDU_presentRequest:
419 res = response_presentRequest(assoc, req, 0, &fd); break;
420 case Z_APDU_scanRequest:
421 res = response_scanRequest(assoc, req, 0, &fd); break;
424 logf(LOG_WARN, "Serious programmer's lapse or bug");
427 if ((res && process_response(assoc, req, res) < 0) || fd < 0)
429 logf(LOG_LOG, "Fatal error when talking to backend");
430 cs_close(assoc->client_link);
431 destroy_association(assoc);
432 iochan_destroy(assoc->client_chan);
436 else if (!res) /* no result yet - try again later */
438 logf(LOG_DEBUG, " no result yet");
439 iochan_setfd(i, fd); /* in case fd has changed */
444 * Encode response, and transfer the request structure to the outgoing queue.
446 static int process_response(association *assoc, request *req, Z_APDU *res)
448 odr_setbuf(assoc->encode, req->response, req->size_response, 1);
449 if (!z_APDU(assoc->encode, &res, 0))
451 logf(LOG_WARN, "ODR error when encoding response: %s",
452 odr_errlist[odr_geterror(assoc->decode)]);
455 req->response = odr_getbuf(assoc->encode, &req->len_response,
456 &req->size_response);
457 odr_setbuf(assoc->encode, 0, 0, 0); /* don't free if we abort later */
458 odr_reset(assoc->encode);
459 if (assoc->print && !z_APDU(assoc->print, &res, 0))
461 logf(LOG_WARN, "ODR print error: %s",
462 odr_errlist[odr_geterror(assoc->print)]);
463 odr_reset(assoc->print);
465 /* change this when we make the backend reentrant */
466 assert(req == request_head(&assoc->incoming));
467 req->state = REQUEST_IDLE;
468 request_deq(&assoc->incoming);
469 request_enq(&assoc->outgoing, req);
470 /* turn the work over to the ir_session handler */
471 iochan_setflag(assoc->client_chan, EVENT_OUTPUT);
472 /* Is there more work to be done? give that to the input handler too */
473 if (request_head(&assoc->incoming))
474 iochan_setevent(assoc->client_chan, EVENT_WORK);
479 * Handle init request.
480 * At the moment, we don't check the protocol version or the options
481 * anywhere else in the code - we just don't do anything that would
482 * break a naive client.
484 static Z_APDU *process_initRequest(association *assoc, request *reqb)
486 Z_InitRequest *req = reqb->request->u.initRequest;
488 static Z_InitResponse resp;
489 static bool_t result = 1;
490 static Odr_bitmask options, protocolVersion;
491 bend_initrequest binitreq;
492 bend_initresult *binitres;
494 logf(LOG_LOG, "Got initRequest");
495 if (req->implementationId)
496 logf(LOG_LOG, "Id: %s", req->implementationId);
497 if (req->implementationName)
498 logf(LOG_LOG, "Name: %s", req->implementationName);
499 if (req->implementationVersion)
500 logf(LOG_LOG, "Version: %s", req->implementationVersion);
502 binitreq.configname = "default-config";
503 binitreq.auth = req->idAuthentication;
504 if (!(binitres = bend_init(&binitreq)))
506 logf(LOG_WARN, "Bad response from backend.");
510 assoc->backend = binitres->handle;
511 apdu.which = Z_APDU_initResponse;
512 apdu.u.initResponse = &resp;
513 resp.referenceId = req->referenceId;
514 /* let's tell the client what we can do */
515 ODR_MASK_ZERO(&options);
516 if (ODR_MASK_GET(req->options, Z_Options_search))
517 ODR_MASK_SET(&options, Z_Options_search);
518 if (ODR_MASK_GET(req->options, Z_Options_present))
519 ODR_MASK_SET(&options, Z_Options_present);
521 if (ODR_MASK_GET(req->options, Z_Options_delSet))
522 ODR_MASK_SET(&options, Z_Options_delSet);
524 if (ODR_MASK_GET(req->options, Z_Options_namedResultSets))
525 ODR_MASK_SET(&options, Z_Options_namedResultSets);
526 if (ODR_MASK_GET(req->options, Z_Options_scan))
527 ODR_MASK_SET(&options, Z_Options_scan);
528 if (ODR_MASK_GET(req->options, Z_Options_concurrentOperations))
529 ODR_MASK_SET(&options, Z_Options_concurrentOperations);
530 resp.options = &options;
532 ODR_MASK_ZERO(&protocolVersion);
533 if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_1))
534 ODR_MASK_SET(&protocolVersion, Z_ProtocolVersion_1);
535 if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_2))
536 ODR_MASK_SET(&protocolVersion, Z_ProtocolVersion_2);
537 resp.protocolVersion = &protocolVersion;
538 assoc->maximumRecordSize = *req->maximumRecordSize;
539 if (assoc->maximumRecordSize > control_block->maxrecordsize)
540 assoc->maximumRecordSize = control_block->maxrecordsize;
541 assoc->preferredMessageSize = *req->preferredMessageSize;
542 if (assoc->preferredMessageSize > assoc->maximumRecordSize)
543 assoc->preferredMessageSize = assoc->maximumRecordSize;
544 resp.preferredMessageSize = &assoc->preferredMessageSize;
545 resp.maximumRecordSize = &assoc->maximumRecordSize;
546 resp.result = &result;
547 resp.implementationId = "YAZ";
548 resp.implementationName = "Index Data/YAZ Generic Frontend Server";
549 resp.implementationVersion = YAZ_VERSION;
550 resp.userInformationField = 0;
551 if (binitres->errcode)
553 logf(LOG_LOG, "Connection rejected by backend.");
561 * These functions should be merged.
565 * nonsurrogate diagnostic record.
567 static Z_Records *diagrec(oid_proto proto, int error, char *addinfo)
569 static Z_Records rec;
575 bib1.class = CLASS_DIAGSET;
576 bib1.value = VAL_BIB1;
578 logf(LOG_DEBUG, "Diagnostic: %d -- %s", error, addinfo ? addinfo :
581 rec.which = Z_Records_NSD;
582 rec.u.nonSurrogateDiagnostic = &dr;
583 dr.diagnosticSetId = oid_getoidbyent(&bib1);
585 dr.addinfo = addinfo ? addinfo : "";
590 * surrogate diagnostic.
592 static Z_NamePlusRecord *surrogatediagrec(oid_proto proto, char *dbname,
593 int error, char *addinfo)
595 static Z_NamePlusRecord rec;
601 bib1.class = CLASS_DIAGSET;
602 bib1.value = VAL_BIB1;
604 logf(LOG_DEBUG, "SurrogateDiagnotic: %d -- %s", error, addinfo);
606 rec.databaseName = dbname;
607 rec.which = Z_NamePlusRecord_surrogateDiagnostic;
608 rec.u.surrogateDiagnostic = &dr;
609 dr.diagnosticSetId = oid_getoidbyent(&bib1);
611 dr.addinfo = addinfo ? addinfo : "";
616 * multiple nonsurrogate diagnostics.
618 static Z_DiagRecs *diagrecs(oid_proto proto, int error, char *addinfo)
620 static Z_DiagRecs recs;
621 static Z_DiagRec *recp[1], rec;
625 logf(LOG_DEBUG, "DiagRecs: %d -- %s", error, addinfo);
627 bib1.class = CLASS_DIAGSET;
628 bib1.value = VAL_BIB1;
631 recs.num_diagRecs = 1;
632 recs.diagRecs = recp;
634 rec.diagnosticSetId = oid_getoidbyent(&bib1);
635 rec.condition = &err;
636 rec.addinfo = addinfo ? addinfo : "";
640 #define MAX_RECORDS 256
642 static Z_Records *pack_records(association *a, char *setname, int start,
643 int *num, Z_ElementSetNames *esn,
644 int *next, int *pres)
646 int recno, total_length = 0, toget = *num;
647 static Z_Records records;
648 static Z_NamePlusRecordList reclist;
649 static Z_NamePlusRecord *list[MAX_RECORDS];
653 records.which = Z_Records_DBOSD;
654 records.u.databaseOrSurDiagnostics = &reclist;
655 reclist.num_records = 0;
656 reclist.records = list;
657 *pres = Z_PRES_SUCCESS;
661 recform.proto = a->proto;
662 recform.class = CLASS_RECSYN;
663 recform.value = VAL_USMARC;
664 if (!(oid = odr_oiddup(a->encode, oid_getoidbyent(&recform))))
667 logf(LOG_DEBUG, "Request to pack %d+%d", start, toget);
668 logf(LOG_DEBUG, "pms=%d, mrs=%d", a->preferredMessageSize,
669 a->maximumRecordSize);
670 for (recno = start; reclist.num_records < toget; recno++)
672 bend_fetchrequest freq;
673 bend_fetchresult *fres;
674 Z_NamePlusRecord *thisrec;
675 Z_DatabaseRecord *thisext;
677 if (reclist.num_records == MAX_RECORDS - 1)
679 *pres = Z_PRES_PARTIAL_2;
682 freq.setname = setname;
684 if (!(fres = bend_fetch(a->backend, &freq, 0)))
686 *pres = Z_PRES_FAILURE;
687 return diagrec(a->proto, 2, "Backend interface problem");
689 /* backend should be able to signal whether error is system-wide
690 or only pertaining to current record */
693 *pres = Z_PRES_FAILURE;
694 return diagrec(a->proto, fres->errcode, fres->errstring);
696 logf(LOG_DEBUG, " fetched record, len=%d, total=%d",
697 fres->len, total_length);
698 if (fres->len + total_length > a->preferredMessageSize)
700 /* record is small enough, really */
701 if (fres->len <= a->preferredMessageSize)
703 logf(LOG_DEBUG, " Dropped last normal-sized record");
704 *pres = Z_PRES_PARTIAL_2;
707 /* record can only be fetched by itself */
708 if (fres->len < a->maximumRecordSize)
710 logf(LOG_DEBUG, " Record > prefmsgsz");
713 logf(LOG_DEBUG, " Dropped it");
714 reclist.records[reclist.num_records] =
715 surrogatediagrec(a->proto, fres->basename, 16, 0);
716 reclist.num_records++;
717 *pres = Z_PRES_PARTIAL_2;
721 else /* too big entirely */
723 logf(LOG_DEBUG, "Record > maxrcdsz");
724 reclist.records[reclist.num_records] =
725 surrogatediagrec(a->proto, fres->basename, 17, 0);
726 reclist.num_records++;
727 *pres = Z_PRES_PARTIAL_2;
731 if (!(thisrec = odr_malloc(a->encode, sizeof(*thisrec))))
733 if (!(thisrec->databaseName = odr_malloc(a->encode,
734 strlen(fres->basename) + 1)))
736 strcpy(thisrec->databaseName, fres->basename);
737 thisrec->which = Z_NamePlusRecord_databaseRecord;
738 if (!(thisrec->u.databaseRecord = thisext = odr_malloc(a->encode,
739 sizeof(Z_DatabaseRecord))))
741 thisext->direct_reference = oid; /* should be OID for current MARC */
742 thisext->indirect_reference = 0;
743 thisext->descriptor = 0;
744 thisext->which = ODR_EXTERNAL_octet;
745 if (!(thisext->u.octet_aligned = odr_malloc(a->encode,
748 if (!(thisext->u.octet_aligned->buf = odr_malloc(a->encode, fres->len)))
750 memcpy(thisext->u.octet_aligned->buf, fres->record, fres->len);
751 thisext->u.octet_aligned->len = thisext->u.octet_aligned->size =
753 reclist.records[reclist.num_records] = thisrec;
754 reclist.num_records++;
755 total_length += fres->len;
757 *next = fres->last_in_set ? 0 : recno + 1;
762 static Z_APDU *process_searchRequest(association *assoc, request *reqb,
765 Z_SearchRequest *req = reqb->request->u.searchRequest;
766 bend_searchrequest bsrq;
767 bend_searchresult *bsrt;
769 logf(LOG_LOG, "Got SearchRequest.");
771 bsrq.setname = req->resultSetName;
772 bsrq.replace_set = *req->replaceIndicator;
773 bsrq.num_bases = req->num_databaseNames;
774 bsrq.basenames = req->databaseNames;
775 bsrq.query = req->query;
777 if (!(bsrt = bend_search(assoc->backend, &bsrq, fd)))
779 return response_searchRequest(assoc, reqb, bsrt, fd);
782 bend_searchresult *bend_searchresponse(void *handle) {return 0;}
785 * Prepare a searchresponse based on the backend results. We probably want
786 * to look at making the fetching of records nonblocking as well, but
787 * so far, we'll keep things simple.
788 * If bsrt is null, that means we're called in response to a communications
789 * event, and we'll have to get the response for ourselves.
791 static Z_APDU *response_searchRequest(association *assoc, request *reqb,
792 bend_searchresult *bsrt, int *fd)
794 Z_SearchRequest *req = reqb->request->u.searchRequest;
796 static Z_SearchResponse resp;
797 static int nulint = 0;
798 static bool_t sr = 1;
800 static int none = Z_RES_NONE;
802 apdu.which = Z_APDU_searchResponse;
803 apdu.u.searchResponse = &resp;
804 resp.referenceId = req->referenceId;
806 if (!bsrt && !(bsrt = bend_searchresponse(assoc->backend)))
808 logf(LOG_FATAL, "Bad result from backend");
811 else if (bsrt->errcode)
813 resp.records = diagrec(assoc->proto, bsrt->errcode,
815 resp.resultCount = &nulint;
816 resp.numberOfRecordsReturned = &nulint;
817 resp.nextResultSetPosition = &nulint;
818 resp.searchStatus = &nulint;
819 resp.resultSetStatus = &none;
820 resp.presentStatus = 0;
825 Z_ElementSetNames *setnames;
829 resp.resultCount = &bsrt->hits;
831 /* how many records does the user agent want, then? */
832 if (bsrt->hits <= *req->smallSetUpperBound)
835 setnames = req->smallSetElementSetNames;
837 else if (bsrt->hits < *req->largeSetLowerBound)
839 toget = *req->mediumSetPresentNumber;
840 if (toget > bsrt->hits)
842 setnames = req->mediumSetElementSetNames;
847 if (toget && !resp.records)
849 resp.records = pack_records(assoc, req->resultSetName, 1,
850 &toget, setnames, &next, &presst);
853 resp.numberOfRecordsReturned = &toget;
854 resp.nextResultSetPosition = &next;
855 resp.searchStatus = &sr;
856 resp.resultSetStatus = 0;
857 resp.presentStatus = &presst;
861 resp.numberOfRecordsReturned = &nulint;
862 resp.nextResultSetPosition = &next;
863 resp.searchStatus = &sr;
864 resp.resultSetStatus = 0;
865 resp.presentStatus = 0;
872 * Maybe we got a little over-friendly when we designed bend_fetch to
873 * get only one record at a time. Some backends can optimise multiple-record
874 * fetches, and at any rate, there is some overhead involved in
875 * all that selecting and hopping around. Problem is, of course, that the
876 * frontend can't know ahead of time how many records it'll need to
877 * fill the negotiated PDU size. Annoying. Segmentation or not, Z/SR
878 * is downright lousy as a bulk data transfer protocol.
880 * To start with, we'll do the fetching of records from the backend
881 * in one operation: To save some trips in and out of the event-handler,
882 * and to simplify the interface to pack_records. At any rate, asynch
883 * operation is more fun in operations that have an unpredictable execution
884 * speed - which is normally more true for search than for present.
886 static Z_APDU *process_presentRequest(association *assoc, request *reqb,
889 Z_PresentRequest *req = reqb->request->u.presentRequest;
891 static Z_PresentResponse resp;
892 static int presst, next, num;
894 logf(LOG_LOG, "Got PresentRequest.");
895 apdu.which = Z_APDU_presentResponse;
896 apdu.u.presentResponse = &resp;
897 resp.referenceId = req->referenceId;
899 num = *req->numberOfRecordsRequested;
900 resp.records = pack_records(assoc, req->resultSetId,
901 *req->resultSetStartPoint, &num, req->elementSetNames, &next, &presst);
904 resp.numberOfRecordsReturned = #
905 resp.presentStatus = &presst;
906 resp.nextResultSetPosition = &next;
912 * Scan was implemented rather in a hurry, and with support for only the basic
913 * elements of the service in the backend API. Suggestions are welcome.
915 static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd)
917 Z_ScanRequest *req = reqb->request->u.scanRequest;
919 static Z_ScanResponse res;
920 static int scanStatus = Z_Scan_failure;
921 static int numberOfEntriesReturned = 0;
923 static Z_ListEntries ents;
924 #define SCAN_MAX_ENTRIES 200
925 static Z_Entry *tab[SCAN_MAX_ENTRIES];
926 bend_scanrequest srq;
927 bend_scanresult *srs;
929 apdu.which = Z_APDU_scanResponse;
930 apdu.u.scanResponse = &res;
931 res.referenceId = req->referenceId;
933 res.scanStatus = &scanStatus;
934 res.numberOfEntriesReturned = &numberOfEntriesReturned;
935 res.positionOfTerm = 0;
937 ents.which = Z_ListEntries_nonSurrogateDiagnostics;
938 res.attributeSet = 0;
940 if (req->attributeSet && (!(attent = oid_getentbyoid(req->attributeSet)) ||
941 attent->class != CLASS_ATTSET || attent->value != VAL_BIB1))
942 ents.u.nonSurrogateDiagnostics = diagrecs(assoc->proto, 121, 0);
943 else if (req->stepSize && *req->stepSize > 0)
944 ents.u.nonSurrogateDiagnostics = diagrecs(assoc->proto, 205, 0);
947 srq.num_bases = req->num_databaseNames;
948 srq.basenames = req->databaseNames;
949 srq.num_entries = *req->numberOfTermsRequested;
950 srq.term = req->termListAndStartPoint;
951 srq.term_position = req->preferredPositionInResponse ?
952 *req->preferredPositionInResponse : 1;
953 if (!(srs = bend_scan(assoc->backend, &srq, 0)))
954 ents.u.nonSurrogateDiagnostics = diagrecs(assoc->proto, 2, 0);
955 else if (srs->errcode)
956 ents.u.nonSurrogateDiagnostics = diagrecs(assoc->proto,
957 srs->errcode, srs->errstring);
961 static Z_Entries list;
963 if (srs->status == BEND_SCAN_PARTIAL)
964 scanStatus = Z_Scan_partial_5;
966 scanStatus = Z_Scan_success;
967 ents.which = Z_ListEntries_entries;
968 ents.u.entries = &list;
970 for (i = 0; i < srs->num_entries; i++)
976 if (i >= SCAN_MAX_ENTRIES)
978 scanStatus = Z_Scan_partial_4;
981 list.entries[i] = e = odr_malloc(assoc->encode, sizeof(*e));
982 e->which = Z_Entry_termInfo;
983 e->u.termInfo = t = odr_malloc(assoc->encode, sizeof(*t));
984 t->suggestedAttributes = 0;
985 t->alternativeTerm = 0;
987 t->globalOccurrences = &srs->entries[i].occurrences;
988 t->term = odr_malloc(assoc->encode, sizeof(*t->term));
989 t->term->which = Z_Term_general;
990 t->term->u.general = o = odr_malloc(assoc->encode,
992 o->buf = odr_malloc(assoc->encode, o->len = o->size =
993 strlen(srs->entries[i].term));
994 memcpy(o->buf, srs->entries[i].term, o->len);
996 list.num_entries = i;
997 res.numberOfEntriesReturned = &list.num_entries;
998 res.positionOfTerm = &srs->term_position;