2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.17 1995-04-10 10:23:36 quinn
8 * Some work to add scan and other things.
10 * Revision 1.16 1995/03/31 09:18:55 quinn
13 * Revision 1.15 1995/03/30 14:03:23 quinn
14 * Added RFC1006 as separate library
16 * Revision 1.14 1995/03/30 12:18:17 quinn
19 * Revision 1.13 1995/03/30 09:09:24 quinn
20 * Added state-handle and some support for asynchronous activities.
22 * Revision 1.12 1995/03/29 15:40:16 quinn
23 * Ongoing work. Statserv is now dynamic by default
25 * Revision 1.11 1995/03/28 09:16:21 quinn
26 * Added record packing to the search request
28 * Revision 1.10 1995/03/27 08:34:24 quinn
29 * Added dynamic server functionality.
30 * Released bindings to session.c (is now redundant)
32 * Revision 1.9 1995/03/22 15:01:26 quinn
33 * Adjusting record packing.
35 * Revision 1.8 1995/03/22 10:13:21 quinn
36 * Working on record packer
38 * Revision 1.7 1995/03/21 15:53:31 quinn
41 * Revision 1.6 1995/03/21 12:30:09 quinn
42 * Beginning to add support for record packing.
44 * Revision 1.5 1995/03/17 10:44:13 quinn
45 * Added catch of null-string in makediagrec
47 * Revision 1.4 1995/03/17 10:18:08 quinn
48 * Added memory management.
50 * Revision 1.3 1995/03/16 17:42:39 quinn
53 * Revision 1.2 1995/03/16 13:29:01 quinn
56 * Revision 1.1 1995/03/15 16:02:10 quinn
57 * Modded session.c to seshigh.c
75 #define ENCODE_BUFFER_SIZE 10000
77 static int process_apdu(IOCHAN chan);
78 static int process_initRequest(IOCHAN client, Z_InitRequest *req);
79 static int process_searchRequest(IOCHAN client, Z_SearchRequest *req);
80 static int process_presentRequest(IOCHAN client, Z_PresentRequest *req);
81 static int process_scanRequest(IOCHAN client, Z_ScanRequest *req);
84 extern char *apdufile;
86 static FILE *apduf = 0; /* for use in static mode */
88 association *create_association(IOCHAN channel, COMSTACK link)
92 if (!(new = malloc(sizeof(*new))))
94 new->client_chan = channel;
95 new->client_link = link;
96 if (!(new->decode = odr_createmem(ODR_DECODE)) ||
97 !(new->encode = odr_createmem(ODR_ENCODE)))
104 if (!(new->print = odr_createmem(ODR_PRINT)))
108 strcpy(filename, apdufile);
113 if (!(apduf = fopen(filename, "w")))
115 logf(LOG_WARN|LOG_ERRNO, "%s", filename);
118 setvbuf(apduf, 0, _IONBF, 0);
124 sprintf(filename + strlen(filename), ".%d", getpid());
125 if (!(f = fopen(filename, "w")))
127 logf(LOG_WARN|LOG_ERRNO, "%s", filename);
130 setvbuf(f, 0, _IONBF, 0);
132 odr_setprint(new->print, f);
137 if (!(new->encode_buffer = malloc(ENCODE_BUFFER_SIZE)))
139 odr_setbuf(new->encode, new->encode_buffer, ENCODE_BUFFER_SIZE);
140 new->state = ASSOC_UNINIT;
141 new->input_buffer = 0;
142 new->input_buffer_len = 0;
144 if (cs_getproto(link) == CS_Z3950)
145 new->proto = PROTO_Z3950;
147 new->proto = PROTO_SR;
151 void destroy_association(association *h)
153 odr_destroy(h->decode);
154 odr_destroy(h->encode);
156 odr_destroy(h->print);
157 free(h->encode_buffer);
159 free(h->input_buffer);
161 bend_close(h->backend);
165 void ir_session(IOCHAN h, int event)
168 association *assoc = iochan_getdata(h);
169 COMSTACK conn = assoc->client_link;
171 if (event == EVENT_INPUT)
173 assert(assoc && conn);
174 res = cs_get(conn, &assoc->input_buffer, &assoc->input_buffer_len);
177 case 0: case -1: /* connection closed by peer */
178 logf(LOG_LOG, "Connection closed by client");
180 destroy_association(assoc);
183 case 1: /* incomplete read */
186 assoc->input_apdu_len = res;
187 if (process_apdu(h) < 0)
190 destroy_association(assoc);
193 else if (cs_more(conn)) /* arrange to be called again */
194 iochan_setevent(h, EVENT_INPUT);
197 else if (event == EVENT_OUTPUT)
199 switch (res = cs_put(conn, assoc->encode_buffer, assoc->encoded_len))
202 logf(LOG_LOG, "Connection closed by client");
204 destroy_association(assoc);
206 case 0: /* all sent */
207 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset */
209 case 1: /* partial send */
210 break; /* we'll get called again */
213 else if (event == EVENT_EXCEPT)
215 logf(LOG_LOG, "Exception on line");
217 destroy_association(assoc);
222 static int process_apdu(IOCHAN chan)
226 association *assoc = iochan_getdata(chan);
228 odr_setbuf(assoc->decode, assoc->input_buffer, assoc->input_apdu_len);
229 if (!z_APDU(assoc->decode, &apdu, 0))
231 logf(LOG_WARN, "ODR error: %s",
232 odr_errlist[odr_geterror(assoc->decode)]);
235 if (assoc->print && !z_APDU(assoc->print, &apdu, 0))
237 logf(LOG_WARN, "ODR print error: %s",
238 odr_errlist[odr_geterror(assoc->print)]);
243 case Z_APDU_initRequest:
244 res = process_initRequest(chan, apdu->u.initRequest); break;
245 case Z_APDU_searchRequest:
246 res = process_searchRequest(chan, apdu->u.searchRequest); break;
247 case Z_APDU_presentRequest:
248 res = process_presentRequest(chan, apdu->u.presentRequest); break;
249 case Z_APDU_scanRequest:
250 res = process_scanRequest(chan, apdu->u.scanRequest); break;
252 logf(LOG_WARN, "Bad APDU");
255 odr_reset(assoc->decode); /* release incopming APDU */
256 odr_reset(assoc->encode); /* release stuff alloced before encoding */
260 static int process_initRequest(IOCHAN client, Z_InitRequest *req)
265 association *assoc = iochan_getdata(client);
266 bend_initrequest binitreq;
267 bend_initresult *binitres;
268 Odr_bitmask options, protocolVersion;
270 logf(LOG_LOG, "Got initRequest");
271 if (req->implementationId)
272 logf(LOG_LOG, "Id: %s", req->implementationId);
273 if (req->implementationName)
274 logf(LOG_LOG, "Name: %s", req->implementationName);
275 if (req->implementationVersion)
276 logf(LOG_LOG, "Version: %s", req->implementationVersion);
278 binitreq.configname = "default-config";
279 if (!(binitres = bend_init(&binitreq)) || binitres->errcode)
281 logf(LOG_WARN, "Bad response from backend");
285 assoc->backend = binitres->handle;
287 apdu.which = Z_APDU_initResponse;
288 apdu.u.initResponse = &resp;
289 resp.referenceId = req->referenceId;
290 ODR_MASK_ZERO(&options);
291 if (ODR_MASK_GET(req->options, Z_Options_search))
292 ODR_MASK_SET(&options, Z_Options_search);
293 if (ODR_MASK_GET(req->options, Z_Options_present))
294 ODR_MASK_SET(&options, Z_Options_present);
295 if (ODR_MASK_GET(req->options, Z_Options_delSet))
296 ODR_MASK_SET(&options, Z_Options_delSet);
297 if (ODR_MASK_GET(req->options, Z_Options_namedResultSets))
298 ODR_MASK_SET(&options, Z_Options_namedResultSets);
299 resp.options = &options;
300 ODR_MASK_ZERO(&protocolVersion);
301 if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_1))
302 ODR_MASK_SET(&protocolVersion, Z_ProtocolVersion_1);
303 if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_2))
304 ODR_MASK_SET(&protocolVersion, Z_ProtocolVersion_2);
305 resp.protocolVersion = &protocolVersion;
306 assoc->maximumRecordSize = *req->maximumRecordSize;
308 * This is not so hot. The big todo for ODR is dynamic memory allocation
311 if (assoc->maximumRecordSize > ENCODE_BUFFER_SIZE - 1000)
312 assoc->maximumRecordSize = ENCODE_BUFFER_SIZE - 1000;
313 assoc->preferredMessageSize = *req->preferredMessageSize;
314 if (assoc->preferredMessageSize > assoc->maximumRecordSize)
315 assoc->preferredMessageSize = assoc->maximumRecordSize;
316 resp.preferredMessageSize = &assoc->preferredMessageSize;
317 resp.maximumRecordSize = &assoc->maximumRecordSize;
318 resp.result = &result;
319 resp.implementationId = "YAZ";
320 resp.implementationName = "Index Data/YAZ Generic Frontend Server";
321 resp.implementationVersion = "$Revision: 1.17 $";
322 resp.userInformationField = 0;
323 if (!z_APDU(assoc->encode, &apdup, 0))
325 logf(LOG_FATAL, "ODR error encoding initres: %s",
326 odr_errlist[odr_geterror(assoc->encode)]);
329 odr_getbuf(assoc->encode, &assoc->encoded_len);
330 odr_reset(assoc->encode);
331 iochan_setflags(client, EVENT_OUTPUT | EVENT_EXCEPT);
335 static Z_Records *diagrec(oid_proto proto, int error, char *addinfo)
337 static Z_Records rec;
343 bib1.class = CLASS_DIAGSET;
344 bib1.value = VAL_BIB1;
346 logf(LOG_DEBUG, "Diagnostic: %d -- %s", error, addinfo ? addinfo :
349 rec.which = Z_Records_NSD;
350 rec.u.nonSurrogateDiagnostic = &dr;
351 dr.diagnosticSetId = oid_getoidbyent(&bib1);
353 dr.addinfo = addinfo ? addinfo : "";
357 static Z_NamePlusRecord *surrogatediagrec(oid_proto proto, char *dbname,
358 int error, char *addinfo)
360 static Z_NamePlusRecord rec;
366 bib1.class = CLASS_DIAGSET;
367 bib1.value = VAL_BIB1;
369 logf(LOG_DEBUG, "SurrogateDiagnotic: %d -- %s", error, addinfo);
371 rec.databaseName = dbname;
372 rec.which = Z_NamePlusRecord_surrogateDiagnostic;
373 rec.u.surrogateDiagnostic = &dr;
374 dr.diagnosticSetId = oid_getoidbyent(&bib1);
376 dr.addinfo = addinfo ? addinfo : "";
380 static Z_DiagRecs *diagrecs(oid_proto proto, int error, char *addinfo)
382 static Z_DiagRecs recs;
383 static Z_DiagRec *recp[1], rec;
387 logf(LOG_DEBUG, "DiagRecs: %d -- %s", error, addinfo);
389 bib1.class = CLASS_DIAGSET;
390 bib1.value = VAL_BIB1;
393 recs.num_diagRecs = 1;
394 recs.diagRecs = recp;
396 rec.diagnosticSetId = oid_getoidbyent(&bib1);
397 rec.condition = &err;
398 rec.addinfo = addinfo ? addinfo : "";
402 #define MAX_RECORDS 256
404 static Z_Records *pack_records(association *a, char *setname, int start,
405 int *num, Z_ElementSetNames *esn,
406 int *next, int *pres)
408 int recno, total_length = 0, toget = *num;
409 static Z_Records records;
410 static Z_NamePlusRecordList reclist;
411 static Z_NamePlusRecord *list[MAX_RECORDS];
415 records.which = Z_Records_DBOSD;
416 records.u.databaseOrSurDiagnostics = &reclist;
417 reclist.num_records = 0;
418 reclist.records = list;
419 *pres = Z_PRES_SUCCESS;
423 recform.proto = a->proto;
424 recform.class = CLASS_RECSYN;
425 recform.value = VAL_USMARC;
426 if (!(oid = odr_oiddup(a->encode, oid_getoidbyent(&recform))))
429 logf(LOG_DEBUG, "Request to pack %d+%d", start, toget);
430 logf(LOG_DEBUG, "pms=%d, mrs=%d", a->preferredMessageSize,
431 a->maximumRecordSize);
432 for (recno = start; reclist.num_records < toget; recno++)
434 bend_fetchrequest freq;
435 bend_fetchresult *fres;
436 Z_NamePlusRecord *thisrec;
437 Z_DatabaseRecord *thisext;
439 if (reclist.num_records == MAX_RECORDS - 1)
441 *pres = Z_PRES_PARTIAL_2;
444 freq.setname = setname;
446 if (!(fres = bend_fetch(a->backend, &freq, 0)))
448 *pres = Z_PRES_FAILURE;
449 return diagrec(a->proto, 2, "Backend interface problem");
451 /* backend should be able to signal whether error is system-wide
452 or only pertaining to current record */
455 *pres = Z_PRES_FAILURE;
456 return diagrec(a->proto, fres->errcode, fres->errstring);
458 logf(LOG_DEBUG, " fetched record, len=%d, total=%d",
459 fres->len, total_length);
460 if (fres->len + total_length > a->preferredMessageSize)
462 /* record is small enough, really */
463 if (fres->len <= a->preferredMessageSize)
465 logf(LOG_DEBUG, " Dropped last normal-sized record");
466 *pres = Z_PRES_PARTIAL_2;
469 /* record can only be fetched by itself */
470 if (fres->len < a->maximumRecordSize)
472 logf(LOG_DEBUG, " Record > prefmsgsz");
475 logf(LOG_DEBUG, " Dropped it");
476 reclist.records[reclist.num_records] =
477 surrogatediagrec(a->proto, fres->basename, 16, 0);
478 reclist.num_records++;
479 *pres = Z_PRES_PARTIAL_2;
483 else /* too big entirely */
485 logf(LOG_DEBUG, "Record > maxrcdsz");
486 reclist.records[reclist.num_records] =
487 surrogatediagrec(a->proto, fres->basename, 17, 0);
488 reclist.num_records++;
489 *pres = Z_PRES_PARTIAL_2;
493 if (!(thisrec = odr_malloc(a->encode, sizeof(*thisrec))))
495 if (!(thisrec->databaseName = odr_malloc(a->encode,
496 strlen(fres->basename) + 1)))
498 strcpy(thisrec->databaseName, fres->basename);
499 thisrec->which = Z_NamePlusRecord_databaseRecord;
500 if (!(thisrec->u.databaseRecord = thisext = odr_malloc(a->encode,
501 sizeof(Z_DatabaseRecord))))
503 thisext->direct_reference = oid; /* should be OID for current MARC */
504 thisext->indirect_reference = 0;
505 thisext->descriptor = 0;
506 thisext->which = ODR_EXTERNAL_octet;
507 if (!(thisext->u.octet_aligned = odr_malloc(a->encode,
510 if (!(thisext->u.octet_aligned->buf = odr_malloc(a->encode, fres->len)))
512 memcpy(thisext->u.octet_aligned->buf, fres->record, fres->len);
513 thisext->u.octet_aligned->len = thisext->u.octet_aligned->size =
515 reclist.records[reclist.num_records] = thisrec;
516 reclist.num_records++;
517 total_length += fres->len;
519 *next = fres->last_in_set ? 0 : recno + 1;
524 static int process_searchRequest(IOCHAN client, Z_SearchRequest *req)
527 Z_SearchResponse resp;
528 association *assoc = iochan_getdata(client);
531 bend_searchrequest bsrq;
532 bend_searchresult *bsrt;
535 static int none = Z_RES_NONE;
537 logf(LOG_LOG, "Got SearchRequest.");
539 apdu.which = Z_APDU_searchResponse;
540 apdu.u.searchResponse = &resp;
541 resp.referenceId = req->referenceId;
544 if (req->query->which == Z_Query_type_1)
546 Z_RPNQuery *q = req->query->u.type_1;
548 if (!(oent = oid_getentbyoid(q->attributeSetId)) ||
549 oent->class != CLASS_ATTSET ||
550 oent->value != VAL_BIB1)
552 resp.records = diagrec(assoc->proto, 121, 0);
553 resp.resultCount = &nulint;
554 resp.numberOfRecordsReturned = &nulint;
555 resp.nextResultSetPosition = &nulint;
556 resp.searchStatus = &none;
557 resp.resultSetStatus = 0;
558 resp.presentStatus = 0;
564 Z_ElementSetNames *setnames;
567 bsrq.setname = req->resultSetName;
568 bsrq.replace_set = *req->replaceIndicator;
569 bsrq.num_bases = req->num_databaseNames;
570 bsrq.basenames = req->databaseNames;
571 bsrq.query = req->query;
573 if (!(bsrt = bend_search(assoc->backend, &bsrq, 0)))
575 else if (bsrt->errcode)
578 resp.records = diagrec(assoc->proto, bsrt->errcode,
580 resp.resultCount = &nulint;
581 resp.numberOfRecordsReturned = &nulint;
582 resp.nextResultSetPosition = &nulint;
583 resp.searchStatus = &nulint;
584 resp.resultSetStatus = &none;
585 resp.presentStatus = 0;
591 resp.resultCount = &bsrt->hits;
593 /* how many records does the user agent want, then? */
594 if (bsrt->hits <= *req->smallSetUpperBound)
597 setnames = req->smallSetElementSetNames;
599 else if (bsrt->hits < *req->largeSetLowerBound)
601 toget = *req->mediumSetPresentNumber;
602 if (toget > bsrt->hits)
604 setnames = req->mediumSetElementSetNames;
609 if (toget && !resp.records)
611 resp.records = pack_records(assoc, req->resultSetName, 1,
612 &toget, setnames, &next, &presst);
615 resp.numberOfRecordsReturned = &toget;
616 resp.nextResultSetPosition = &next;
617 resp.searchStatus = &sr;
618 resp.resultSetStatus = 0;
619 resp.presentStatus = &presst;
623 resp.numberOfRecordsReturned = &nulint;
624 resp.nextResultSetPosition = &next;
625 resp.searchStatus = &sr;
626 resp.resultSetStatus = 0;
627 resp.presentStatus = 0;
632 if (!z_APDU(assoc->encode, &apdup, 0))
634 logf(LOG_FATAL, "ODR error encoding searchres: %s",
635 odr_errlist[odr_geterror(assoc->encode)]);
638 odr_getbuf(assoc->encode, &assoc->encoded_len);
639 odr_reset(assoc->encode);
640 iochan_setflags(client, EVENT_OUTPUT | EVENT_EXCEPT);
644 static int process_presentRequest(IOCHAN client, Z_PresentRequest *req)
647 Z_PresentResponse resp;
648 association *assoc = iochan_getdata(client);
649 int presst, next, num;
651 logf(LOG_LOG, "Got PresentRequest.");
653 apdu.which = Z_APDU_presentResponse;
654 apdu.u.presentResponse = &resp;
655 resp.referenceId = req->referenceId;
657 num = *req->numberOfRecordsRequested;
658 resp.records = pack_records(assoc, req->resultSetId,
659 *req->resultSetStartPoint, &num, req->elementSetNames, &next, &presst);
662 resp.numberOfRecordsReturned = #
663 resp.presentStatus = &presst;
664 resp.nextResultSetPosition = &next;
666 if (!z_APDU(assoc->encode, &apdup, 0))
668 logf(LOG_FATAL, "ODR error encoding initres: %s",
669 odr_errlist[odr_geterror(assoc->encode)]);
672 odr_getbuf(assoc->encode, &assoc->encoded_len);
673 odr_reset(assoc->encode);
674 iochan_setflags(client, EVENT_OUTPUT | EVENT_EXCEPT);
678 static int process_scanRequest(IOCHAN client, Z_ScanRequest *req)
680 association *assoc = iochan_getdata(client);
683 bend_scanrequest srq;
684 bend_scanresult *srs;
685 int scanStatus = Z_Scan_failure;
686 int numberOfEntriesReturned = 0;
689 #define SCAN_MAX_ENTRIES 200
690 Z_Entry *tab[SCAN_MAX_ENTRIES];
693 apdu.which = Z_APDU_scanResponse;
694 apdu.u.scanResponse = &res;
695 res.referenceId = req->referenceId;
697 res.scanStatus = &scanStatus;
698 res.numberOfEntriesReturned = &numberOfEntriesReturned;
699 res.positionOfTerm = 0;
701 ents.which = Z_ListEntries_nonSurrogateDiagnostics;
702 res.attributeSet = 0;
704 if (req->attributeSet && (!(attent = oid_getentbyoid(req->attributeSet)) ||
705 attent->class != CLASS_ATTSET || attent->value != VAL_BIB1))
706 ents.u.nonSurrogateDiagnostics = diagrecs(assoc->proto, 121, 0);
707 else if (req->stepSize && *req->stepSize > 0)
708 ents.u.nonSurrogateDiagnostics = diagrecs(assoc->proto, 205, 0);
711 srq.num_bases = req->num_databaseNames;
712 srq.basenames = req->databaseNames;
713 srq.num_entries = *req->numberOfTermsRequested;
714 srq.term = req->termListAndStartPoint;
715 srq.term_position = req->preferredPositionInResponse ?
716 *req->preferredPositionInResponse : 1;
717 if (!(srs = bend_scan(assoc->backend, &srq, 0)))
718 ents.u.nonSurrogateDiagnostics = diagrecs(assoc->proto, 2, 0);
719 else if (srs->errcode)
720 ents.u.nonSurrogateDiagnostics = diagrecs(assoc->proto,
721 srs->errcode, srs->errstring);
725 static Z_Entries list;
727 if (srs->status == BEND_SCAN_PARTIAL)
728 scanStatus = Z_Scan_partial_5;
730 scanStatus = 1; /* Z_Scan_success; */ /* assumption for now */
731 ents.which = Z_ListEntries_entries;
732 ents.u.entries = &list;
734 for (i = 0; i < srs->num_entries; i++)
740 if (i >= SCAN_MAX_ENTRIES)
742 scanStatus = Z_Scan_partial_4;
745 list.entries[i] = e = odr_malloc(assoc->encode, sizeof(*e));
746 e->which = Z_Entry_termInfo;
747 e->u.termInfo = t = odr_malloc(assoc->encode, sizeof(*t));
748 t->suggestedAttributes = 0;
749 t->alternativeTerm = 0;
751 t->globalOccurrences = &srs->entries[i].occurrences;
752 t->term = odr_malloc(assoc->encode, sizeof(*t->term));
753 t->term->which = Z_Term_general;
754 t->term->u.general = o = odr_malloc(assoc->encode,
756 o->buf = odr_malloc(assoc->encode, o->len = o->size =
757 strlen(srs->entries[i].term));
758 memcpy(o->buf, srs->entries[i].term, o->len);
760 list.num_entries = i;
761 res.numberOfEntriesReturned = &list.num_entries;
762 res.positionOfTerm = &srs->term_position;
765 if (!z_APDU(assoc->encode, &apdup, 0))
767 logf(LOG_FATAL, "ODR error encoding initres: %s",
768 odr_errlist[odr_geterror(assoc->encode)]);
771 odr_getbuf(assoc->encode, &assoc->encoded_len);
772 odr_reset(assoc->encode);
773 iochan_setflags(client, EVENT_OUTPUT | EVENT_EXCEPT);