2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.16 1995-03-31 09:18:55 quinn
10 * Revision 1.15 1995/03/30 14:03:23 quinn
11 * Added RFC1006 as separate library
13 * Revision 1.14 1995/03/30 12:18:17 quinn
16 * Revision 1.13 1995/03/30 09:09:24 quinn
17 * Added state-handle and some support for asynchronous activities.
19 * Revision 1.12 1995/03/29 15:40:16 quinn
20 * Ongoing work. Statserv is now dynamic by default
22 * Revision 1.11 1995/03/28 09:16:21 quinn
23 * Added record packing to the search request
25 * Revision 1.10 1995/03/27 08:34:24 quinn
26 * Added dynamic server functionality.
27 * Released bindings to session.c (is now redundant)
29 * Revision 1.9 1995/03/22 15:01:26 quinn
30 * Adjusting record packing.
32 * Revision 1.8 1995/03/22 10:13:21 quinn
33 * Working on record packer
35 * Revision 1.7 1995/03/21 15:53:31 quinn
38 * Revision 1.6 1995/03/21 12:30:09 quinn
39 * Beginning to add support for record packing.
41 * Revision 1.5 1995/03/17 10:44:13 quinn
42 * Added catch of null-string in makediagrec
44 * Revision 1.4 1995/03/17 10:18:08 quinn
45 * Added memory management.
47 * Revision 1.3 1995/03/16 17:42:39 quinn
50 * Revision 1.2 1995/03/16 13:29:01 quinn
53 * Revision 1.1 1995/03/15 16:02:10 quinn
54 * Modded session.c to seshigh.c
71 #define ENCODE_BUFFER_SIZE 10000
73 static int process_apdu(IOCHAN chan);
74 static int process_initRequest(IOCHAN client, Z_InitRequest *req);
75 static int process_searchRequest(IOCHAN client, Z_SearchRequest *req);
76 static int process_presentRequest(IOCHAN client, Z_PresentRequest *req);
78 association *create_association(IOCHAN channel, COMSTACK link)
82 if (!(new = malloc(sizeof(*new))))
84 new->client_chan = channel;
85 new->client_link = link;
86 if (!(new->decode = odr_createmem(ODR_DECODE)) ||
87 !(new->encode = odr_createmem(ODR_ENCODE)))
89 if (!(new->encode_buffer = malloc(ENCODE_BUFFER_SIZE)))
91 odr_setbuf(new->encode, new->encode_buffer, ENCODE_BUFFER_SIZE);
92 new->state = ASSOC_UNINIT;
93 new->input_buffer = 0;
94 new->input_buffer_len = 0;
96 if (cs_getproto(link) == CS_Z3950)
97 new->proto = PROTO_Z3950;
99 new->proto = PROTO_SR;
103 void destroy_association(association *h)
105 odr_destroy(h->decode);
106 odr_destroy(h->encode);
107 free(h->encode_buffer);
109 free(h->input_buffer);
111 bend_close(h->backend);
115 void ir_session(IOCHAN h, int event)
118 association *assoc = iochan_getdata(h);
119 COMSTACK conn = assoc->client_link;
121 if (event == EVENT_INPUT)
123 assert(assoc && conn);
124 res = cs_get(conn, &assoc->input_buffer, &assoc->input_buffer_len);
127 case 0: case -1: /* connection closed by peer */
128 logf(LOG_LOG, "Connection closed by client");
130 destroy_association(assoc);
133 case 1: /* incomplete read */
136 assoc->input_apdu_len = res;
137 if (process_apdu(h) < 0)
140 destroy_association(assoc);
143 else if (cs_more(conn)) /* arrange to be called again */
144 iochan_setevent(h, EVENT_INPUT);
147 else if (event == EVENT_OUTPUT)
149 switch (res = cs_put(conn, assoc->encode_buffer, assoc->encoded_len))
152 logf(LOG_LOG, "Connection closed by client");
154 destroy_association(assoc);
156 case 0: /* all sent */
157 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset */
159 case 1: /* partial send */
160 break; /* we'll get called again */
163 else if (event == EVENT_EXCEPT)
165 logf(LOG_LOG, "Exception on line");
167 destroy_association(assoc);
172 static int process_apdu(IOCHAN chan)
176 association *assoc = iochan_getdata(chan);
178 odr_setbuf(assoc->decode, assoc->input_buffer, assoc->input_apdu_len);
179 if (!z_APDU(assoc->decode, &apdu, 0))
181 logf(LOG_WARN, "ODR error: %s",
182 odr_errlist[odr_geterror(assoc->decode)]);
187 case Z_APDU_initRequest:
188 res = process_initRequest(chan, apdu->u.initRequest); break;
189 case Z_APDU_searchRequest:
190 res = process_searchRequest(chan, apdu->u.searchRequest); break;
191 case Z_APDU_presentRequest:
192 res = process_presentRequest(chan, apdu->u.presentRequest); break;
194 logf(LOG_WARN, "Bad APDU");
197 odr_reset(assoc->decode); /* release incopming APDU */
198 odr_reset(assoc->encode); /* release stuff alloced before encoding */
202 static int process_initRequest(IOCHAN client, Z_InitRequest *req)
207 association *assoc = iochan_getdata(client);
208 bend_initrequest binitreq;
209 bend_initresult *binitres;
210 Odr_bitmask options, protocolVersion;
212 logf(LOG_LOG, "Got initRequest");
213 if (req->implementationId)
214 logf(LOG_LOG, "Id: %s", req->implementationId);
215 if (req->implementationName)
216 logf(LOG_LOG, "Name: %s", req->implementationName);
217 if (req->implementationVersion)
218 logf(LOG_LOG, "Version: %s", req->implementationVersion);
220 binitreq.configname = "default-config";
221 if (!(binitres = bend_init(&binitreq)) || binitres->errcode)
223 logf(LOG_WARN, "Bad response from backend");
227 assoc->backend = binitres->handle;
229 apdu.which = Z_APDU_initResponse;
230 apdu.u.initResponse = &resp;
231 resp.referenceId = req->referenceId;
232 ODR_MASK_ZERO(&options);
233 if (ODR_MASK_GET(req->options, Z_Options_search))
234 ODR_MASK_SET(&options, Z_Options_search);
235 if (ODR_MASK_GET(req->options, Z_Options_present))
236 ODR_MASK_SET(&options, Z_Options_present);
237 if (ODR_MASK_GET(req->options, Z_Options_delSet))
238 ODR_MASK_SET(&options, Z_Options_delSet);
239 if (ODR_MASK_GET(req->options, Z_Options_namedResultSets))
240 ODR_MASK_SET(&options, Z_Options_namedResultSets);
241 resp.options = &options;
242 ODR_MASK_ZERO(&protocolVersion);
243 if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_1))
244 ODR_MASK_SET(&protocolVersion, Z_ProtocolVersion_1);
245 if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_2))
246 ODR_MASK_SET(&protocolVersion, Z_ProtocolVersion_2);
247 resp.protocolVersion = &protocolVersion;
248 assoc->maximumRecordSize = *req->maximumRecordSize;
250 * This is not so hot. The big todo for ODR is dynamic memory allocation
253 if (assoc->maximumRecordSize > ENCODE_BUFFER_SIZE - 1000)
254 assoc->maximumRecordSize = ENCODE_BUFFER_SIZE - 1000;
255 assoc->preferredMessageSize = *req->preferredMessageSize;
256 if (assoc->preferredMessageSize > assoc->maximumRecordSize)
257 assoc->preferredMessageSize = assoc->maximumRecordSize;
258 resp.preferredMessageSize = &assoc->preferredMessageSize;
259 resp.maximumRecordSize = &assoc->maximumRecordSize;
260 resp.result = &result;
261 resp.implementationId = "YAZ";
262 resp.implementationName = "Index Data/YAZ Generic Frontend Server";
263 resp.implementationVersion = "$Revision: 1.16 $";
264 resp.userInformationField = 0;
265 if (!z_APDU(assoc->encode, &apdup, 0))
267 logf(LOG_FATAL, "ODR error encoding initres: %s",
268 odr_errlist[odr_geterror(assoc->encode)]);
271 odr_getbuf(assoc->encode, &assoc->encoded_len);
272 odr_reset(assoc->encode);
273 iochan_setflags(client, EVENT_OUTPUT | EVENT_EXCEPT);
277 static Z_Records *diagrec(oid_proto proto, int error, char *addinfo)
279 static Z_Records rec;
285 bib1.class = CLASS_DIAGSET;
286 bib1.value = VAL_BIB1;
288 logf(LOG_DEBUG, "Diagnostic: %d -- %s", error, addinfo ? addinfo :
291 rec.which = Z_Records_NSD;
292 rec.u.nonSurrogateDiagnostic = &dr;
293 dr.diagnosticSetId = oid_getoidbyent(&bib1);
295 dr.addinfo = addinfo ? addinfo : "";
299 static Z_NamePlusRecord *surrogatediagrec(oid_proto proto, char *dbname,
300 int error, char *addinfo)
302 static Z_NamePlusRecord rec;
308 bib1.class = CLASS_DIAGSET;
309 bib1.value = VAL_BIB1;
311 logf(LOG_DEBUG, "SurrogateDiagnotic: %d -- %s", error, addinfo);
313 rec.databaseName = dbname;
314 rec.which = Z_NamePlusRecord_surrogateDiagnostic;
315 rec.u.surrogateDiagnostic = &dr;
316 dr.diagnosticSetId = oid_getoidbyent(&bib1);
318 dr.addinfo = addinfo ? addinfo : "";
322 #define MAX_RECORDS 256
324 static Z_Records *pack_records(association *a, char *setname, int start,
325 int *num, Z_ElementSetNames *esn,
326 int *next, int *pres)
328 int recno, total_length = 0, toget = *num;
329 static Z_Records records;
330 static Z_NamePlusRecordList reclist;
331 static Z_NamePlusRecord *list[MAX_RECORDS];
335 records.which = Z_Records_DBOSD;
336 records.u.databaseOrSurDiagnostics = &reclist;
337 reclist.num_records = 0;
338 reclist.records = list;
339 *pres = Z_PRES_SUCCESS;
343 recform.proto = a->proto;
344 recform.class = CLASS_RECSYN;
345 recform.value = VAL_USMARC;
346 if (!(oid = odr_oiddup(a->encode, oid_getoidbyent(&recform))))
349 logf(LOG_DEBUG, "Request to pack %d+%d", start, toget);
350 logf(LOG_DEBUG, "pms=%d, mrs=%d", a->preferredMessageSize,
351 a->maximumRecordSize);
352 for (recno = start; reclist.num_records < toget; recno++)
354 bend_fetchrequest freq;
355 bend_fetchresult *fres;
356 Z_NamePlusRecord *thisrec;
357 Z_DatabaseRecord *thisext;
359 if (reclist.num_records == MAX_RECORDS - 1)
361 *pres = Z_PRES_PARTIAL_2;
364 freq.setname = setname;
366 if (!(fres = bend_fetch(a->backend, &freq, 0)))
368 *pres = Z_PRES_FAILURE;
369 return diagrec(a->proto, 2, "Backend interface problem");
371 /* backend should be able to signal whether error is system-wide
372 or only pertaining to current record */
375 *pres = Z_PRES_FAILURE;
376 return diagrec(a->proto, fres->errcode, fres->errstring);
378 logf(LOG_DEBUG, " Got record, len=%d, total=%d",
379 fres->len, total_length);
380 if (fres->len + total_length > a->preferredMessageSize)
382 /* record is small enough, really */
383 if (fres->len <= a->preferredMessageSize)
385 logf(LOG_DEBUG, " Dropped last normal-sized record");
386 *pres = Z_PRES_PARTIAL_2;
389 /* record can only be fetched by itself */
390 if (fres->len < a->maximumRecordSize)
392 logf(LOG_DEBUG, " Record > prefmsgsz");
395 logf(LOG_DEBUG, " Dropped it");
396 reclist.records[reclist.num_records] =
397 surrogatediagrec(a->proto, fres->basename, 16, 0);
398 reclist.num_records++;
399 *pres = Z_PRES_PARTIAL_2;
403 else /* too big entirely */
405 logf(LOG_DEBUG, "Record > maxrcdsz");
406 reclist.records[reclist.num_records] =
407 surrogatediagrec(a->proto, fres->basename, 17, 0);
408 reclist.num_records++;
409 *pres = Z_PRES_PARTIAL_2;
413 if (!(thisrec = odr_malloc(a->encode, sizeof(*thisrec))))
415 if (!(thisrec->databaseName = odr_malloc(a->encode,
416 strlen(fres->basename) + 1)))
418 strcpy(thisrec->databaseName, fres->basename);
419 thisrec->which = Z_NamePlusRecord_databaseRecord;
420 if (!(thisrec->u.databaseRecord = thisext = odr_malloc(a->encode,
421 sizeof(Z_DatabaseRecord))))
423 thisext->direct_reference = oid; /* should be OID for current MARC */
424 thisext->indirect_reference = 0;
425 thisext->descriptor = 0;
426 thisext->which = ODR_EXTERNAL_octet;
427 if (!(thisext->u.octet_aligned = odr_malloc(a->encode,
430 if (!(thisext->u.octet_aligned->buf = odr_malloc(a->encode, fres->len)))
432 memcpy(thisext->u.octet_aligned->buf, fres->record, fres->len);
433 thisext->u.octet_aligned->len = thisext->u.octet_aligned->size =
435 reclist.records[reclist.num_records] = thisrec;
436 reclist.num_records++;
437 total_length += fres->len;
439 *next = fres->last_in_set ? 0 : recno + 1;
444 static int process_searchRequest(IOCHAN client, Z_SearchRequest *req)
447 Z_SearchResponse resp;
448 association *assoc = iochan_getdata(client);
451 bend_searchrequest bsrq;
452 bend_searchresult *bsrt;
455 static int none = Z_RES_NONE;
457 logf(LOG_LOG, "Got SearchRequest.");
459 apdu.which = Z_APDU_searchResponse;
460 apdu.u.searchResponse = &resp;
461 resp.referenceId = req->referenceId;
464 if (req->query->which == Z_Query_type_1)
466 Z_RPNQuery *q = req->query->u.type_1;
468 if (!(oent = oid_getentbyoid(q->attributeSetId)) ||
469 oent->class != CLASS_ATTSET ||
470 oent->value != VAL_BIB1)
472 resp.records = diagrec(assoc->proto, 121, 0);
473 resp.resultCount = &nulint;
474 resp.numberOfRecordsReturned = &nulint;
475 resp.nextResultSetPosition = &nulint;
476 resp.searchStatus = &none;
477 resp.resultSetStatus = 0;
478 resp.presentStatus = 0;
484 Z_ElementSetNames *setnames;
487 bsrq.setname = req->resultSetName;
488 bsrq.replace_set = *req->replaceIndicator;
489 bsrq.num_bases = req->num_databaseNames;
490 bsrq.basenames = req->databaseNames;
491 bsrq.query = req->query;
493 if (!(bsrt = bend_search(assoc->backend, &bsrq, 0)))
495 else if (bsrt->errcode)
498 resp.records = diagrec(assoc->proto, bsrt->errcode,
500 resp.resultCount = &nulint;
501 resp.numberOfRecordsReturned = &nulint;
502 resp.nextResultSetPosition = &nulint;
503 resp.searchStatus = &nulint;
504 resp.resultSetStatus = &none;
505 resp.presentStatus = 0;
511 resp.resultCount = &bsrt->hits;
513 /* how many records does the user agent want, then? */
514 if (bsrt->hits <= *req->smallSetUpperBound)
517 setnames = req->smallSetElementSetNames;
519 else if (bsrt->hits < *req->largeSetLowerBound)
521 toget = *req->mediumSetPresentNumber;
522 setnames = req->mediumSetElementSetNames;
527 if (toget && !resp.records)
529 resp.records = pack_records(assoc, req->resultSetName, 1,
530 &toget, setnames, &next, &presst);
533 resp.numberOfRecordsReturned = &toget;
534 resp.nextResultSetPosition = &next;
535 resp.searchStatus = &sr;
536 resp.resultSetStatus = 0;
537 resp.presentStatus = &presst;
541 resp.numberOfRecordsReturned = &nulint;
542 resp.nextResultSetPosition = &next;
543 resp.searchStatus = &sr;
544 resp.resultSetStatus = 0;
545 resp.presentStatus = 0;
550 if (!z_APDU(assoc->encode, &apdup, 0))
552 logf(LOG_FATAL, "ODR error encoding searchres: %s",
553 odr_errlist[odr_geterror(assoc->encode)]);
556 odr_getbuf(assoc->encode, &assoc->encoded_len);
557 odr_reset(assoc->encode);
558 iochan_setflags(client, EVENT_OUTPUT | EVENT_EXCEPT);
562 static int process_presentRequest(IOCHAN client, Z_PresentRequest *req)
565 Z_PresentResponse resp;
566 association *assoc = iochan_getdata(client);
567 int presst, next, num;
569 logf(LOG_LOG, "Got PresentRequest.");
571 apdu.which = Z_APDU_presentResponse;
572 apdu.u.presentResponse = &resp;
573 resp.referenceId = req->referenceId;
575 num = *req->numberOfRecordsRequested;
576 resp.records = pack_records(assoc, req->resultSetId,
577 *req->resultSetStartPoint, &num, req->elementSetNames, &next, &presst);
580 resp.numberOfRecordsReturned = #
581 resp.presentStatus = &presst;
582 resp.nextResultSetPosition = &next;
584 if (!z_APDU(assoc->encode, &apdup, 0))
586 logf(LOG_FATAL, "ODR error encoding initres: %s",
587 odr_errlist[odr_geterror(assoc->encode)]);
590 odr_getbuf(assoc->encode, &assoc->encoded_len);
591 odr_reset(assoc->encode);
592 iochan_setflags(client, EVENT_OUTPUT | EVENT_EXCEPT);