2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.13 1995-03-30 09:09:24 quinn
8 * Added state-handle and some support for asynchronous activities.
10 * Revision 1.12 1995/03/29 15:40:16 quinn
11 * Ongoing work. Statserv is now dynamic by default
13 * Revision 1.11 1995/03/28 09:16:21 quinn
14 * Added record packing to the search request
16 * Revision 1.10 1995/03/27 08:34:24 quinn
17 * Added dynamic server functionality.
18 * Released bindings to session.c (is now redundant)
20 * Revision 1.9 1995/03/22 15:01:26 quinn
21 * Adjusting record packing.
23 * Revision 1.8 1995/03/22 10:13:21 quinn
24 * Working on record packer
26 * Revision 1.7 1995/03/21 15:53:31 quinn
29 * Revision 1.6 1995/03/21 12:30:09 quinn
30 * Beginning to add support for record packing.
32 * Revision 1.5 1995/03/17 10:44:13 quinn
33 * Added catch of null-string in makediagrec
35 * Revision 1.4 1995/03/17 10:18:08 quinn
36 * Added memory management.
38 * Revision 1.3 1995/03/16 17:42:39 quinn
41 * Revision 1.2 1995/03/16 13:29:01 quinn
44 * Revision 1.1 1995/03/15 16:02:10 quinn
45 * Modded session.c to seshigh.c
62 #define ENCODE_BUFFER_SIZE 10000
64 static int process_apdu(IOCHAN chan);
65 static int process_initRequest(IOCHAN client, Z_InitRequest *req);
66 static int process_searchRequest(IOCHAN client, Z_SearchRequest *req);
67 static int process_presentRequest(IOCHAN client, Z_PresentRequest *req);
69 association *create_association(IOCHAN channel, COMSTACK link)
73 if (!(new = malloc(sizeof(*new))))
75 new->client_chan = channel;
76 new->client_link = link;
77 if (!(new->decode = odr_createmem(ODR_DECODE)) ||
78 !(new->encode = odr_createmem(ODR_ENCODE)))
80 if (!(new->encode_buffer = malloc(ENCODE_BUFFER_SIZE)))
82 odr_setbuf(new->encode, new->encode_buffer, ENCODE_BUFFER_SIZE);
83 new->state = ASSOC_UNINIT;
84 new->input_buffer = 0;
85 new->input_buffer_len = 0;
86 if (cs_getproto(link) == CS_Z3950)
87 new->proto = PROTO_Z3950;
89 new->proto = PROTO_SR;
93 void destroy_association(association *h)
95 odr_destroy(h->decode);
96 odr_destroy(h->encode);
97 free(h->encode_buffer);
99 free(h->input_buffer);
101 bend_close(h->backend);
105 void ir_session(IOCHAN h, int event)
108 association *assoc = iochan_getdata(h);
109 COMSTACK conn = assoc->client_link;
111 if (event == EVENT_INPUT)
113 assert(assoc && conn);
114 res = cs_get(conn, &assoc->input_buffer, &assoc->input_buffer_len);
117 case 0: case -1: /* connection closed by peer */
118 fprintf(stderr, "Closed connection\n");
120 destroy_association(assoc);
123 case 1: /* incomplete read */
126 assoc->input_apdu_len = res;
127 if (process_apdu(h) < 0)
129 fprintf(stderr, "Operation failed\n");
131 destroy_association(assoc);
134 else if (cs_more(conn)) /* arrange to be called again */
135 iochan_setevent(h, EVENT_INPUT);
138 else if (event == EVENT_OUTPUT)
140 switch (res = cs_put(conn, assoc->encode_buffer, assoc->encoded_len))
143 fprintf(stderr, "Closed connection\n");
145 destroy_association(assoc);
147 case 0: /* all sent */
148 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset */
150 case 1: /* partial send */
151 break; /* we'll get called again */
154 else if (event == EVENT_EXCEPT)
156 fprintf(stderr, "Exception on line\n");
158 destroy_association(assoc);
163 static int process_apdu(IOCHAN chan)
167 association *assoc = iochan_getdata(chan);
169 odr_setbuf(assoc->decode, assoc->input_buffer, assoc->input_apdu_len);
170 if (!z_APDU(assoc->decode, &apdu, 0))
172 odr_perror(assoc->decode, "Incoming APDU");
177 case Z_APDU_initRequest:
178 res = process_initRequest(chan, apdu->u.initRequest); break;
179 case Z_APDU_searchRequest:
180 res = process_searchRequest(chan, apdu->u.searchRequest); break;
181 case Z_APDU_presentRequest:
182 res = process_presentRequest(chan, apdu->u.presentRequest); break;
184 fprintf(stderr, "Bad APDU\n");
187 odr_reset(assoc->decode); /* release incopming APDU */
188 odr_reset(assoc->encode); /* release stuff alloced before encoding */
192 static int process_initRequest(IOCHAN client, Z_InitRequest *req)
197 association *assoc = iochan_getdata(client);
198 bend_initrequest binitreq;
199 bend_initresult *binitres;
200 Odr_bitmask options, protocolVersion;
202 fprintf(stderr, "Got initRequest.\n");
203 if (req->implementationId)
204 fprintf(stderr, "Id: %s\n", req->implementationId);
205 if (req->implementationName)
206 fprintf(stderr, "Name: %s\n", req->implementationName);
207 if (req->implementationVersion)
208 fprintf(stderr, "Version: %s\n", req->implementationVersion);
210 binitreq.configname = "default-config";
211 if (!(binitres = bend_init(&binitreq)) || binitres->errcode)
213 fprintf(stderr, "Bad response from backend\n");
217 assoc->backend = binitres->handle;
219 apdu.which = Z_APDU_initResponse;
220 apdu.u.initResponse = &resp;
221 resp.referenceId = req->referenceId;
222 ODR_MASK_ZERO(&options);
223 if (ODR_MASK_GET(req->options, Z_Options_search))
224 ODR_MASK_SET(&options, Z_Options_search);
225 if (ODR_MASK_GET(req->options, Z_Options_present))
226 ODR_MASK_SET(&options, Z_Options_present);
227 if (ODR_MASK_GET(req->options, Z_Options_delSet))
228 ODR_MASK_SET(&options, Z_Options_delSet);
229 if (ODR_MASK_GET(req->options, Z_Options_namedResultSets))
230 ODR_MASK_SET(&options, Z_Options_namedResultSets);
231 resp.options = &options;
232 ODR_MASK_ZERO(&protocolVersion);
233 if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_1))
234 ODR_MASK_SET(&protocolVersion, Z_ProtocolVersion_1);
235 if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_2))
236 ODR_MASK_SET(&protocolVersion, Z_ProtocolVersion_2);
237 resp.protocolVersion = &protocolVersion;
238 assoc->maximumRecordSize = *req->maximumRecordSize;
240 * This is not so hot. The big todo for ODR is dynamic memory allocation
243 if (assoc->maximumRecordSize > ENCODE_BUFFER_SIZE - 1000)
244 assoc->maximumRecordSize = ENCODE_BUFFER_SIZE - 1000;
245 assoc->preferredMessageSize = *req->preferredMessageSize;
246 if (assoc->preferredMessageSize > assoc->maximumRecordSize)
247 assoc->preferredMessageSize = assoc->maximumRecordSize;
248 resp.preferredMessageSize = &assoc->preferredMessageSize;
249 resp.maximumRecordSize = &assoc->maximumRecordSize;
250 resp.result = &result;
251 resp.implementationId = "YAZ";
252 resp.implementationName = "Index Data/YAZ Generic Frontend Server";
253 resp.implementationVersion = "$Revision: 1.13 $";
254 resp.userInformationField = 0;
255 if (!z_APDU(assoc->encode, &apdup, 0))
257 odr_perror(assoc->encode, "Encode initres");
260 odr_getbuf(assoc->encode, &assoc->encoded_len);
261 odr_reset(assoc->encode);
262 iochan_setflags(client, EVENT_OUTPUT | EVENT_EXCEPT);
266 static Z_Records *diagrec(oid_proto proto, int error, char *addinfo)
268 static Z_Records rec;
274 bib1.class = CLASS_DIAGSET;
275 bib1.value = VAL_BIB1;
277 fprintf(stderr, "Diagnostic: %d -- %s\n", error, addinfo ? addinfo :
280 rec.which = Z_Records_NSD;
281 rec.u.nonSurrogateDiagnostic = &dr;
282 dr.diagnosticSetId = oid_getoidbyent(&bib1);
284 dr.addinfo = addinfo ? addinfo : "";
288 static Z_NamePlusRecord *surrogatediagrec(oid_proto proto, char *dbname,
289 int error, char *addinfo)
291 static Z_NamePlusRecord rec;
297 bib1.class = CLASS_DIAGSET;
298 bib1.value = VAL_BIB1;
300 fprintf(stderr, "SurrogateDiagnotic: %d -- %s\n", error, addinfo);
302 rec.databaseName = dbname;
303 rec.which = Z_NamePlusRecord_surrogateDiagnostic;
304 rec.u.surrogateDiagnostic = &dr;
305 dr.diagnosticSetId = oid_getoidbyent(&bib1);
307 dr.addinfo = addinfo ? addinfo : "";
311 #define MAX_RECORDS 256
313 static Z_Records *pack_records(association *a, char *setname, int start,
314 int *num, Z_ElementSetNames *esn,
315 int *next, int *pres)
317 int recno, total_length = 0, toget = *num;
318 static Z_Records records;
319 static Z_NamePlusRecordList reclist;
320 static Z_NamePlusRecord *list[MAX_RECORDS];
324 records.which = Z_Records_DBOSD;
325 records.u.databaseOrSurDiagnostics = &reclist;
326 reclist.num_records = 0;
327 reclist.records = list;
328 *pres = Z_PRES_SUCCESS;
332 recform.proto = a->proto;
333 recform.class = CLASS_RECSYN;
334 recform.value = VAL_USMARC;
335 if (!(oid = odr_oiddup(a->encode, oid_getoidbyent(&recform))))
338 fprintf(stderr, "Request to pack %d+%d\n", start, toget);
339 fprintf(stderr, "pms=%d, mrs=%d\n", a->preferredMessageSize,
340 a->maximumRecordSize);
341 for (recno = start; reclist.num_records < toget; recno++)
343 bend_fetchrequest freq;
344 bend_fetchresult *fres;
345 Z_NamePlusRecord *thisrec;
346 Z_DatabaseRecord *thisext;
348 if (reclist.num_records == MAX_RECORDS - 1)
350 *pres = Z_PRES_PARTIAL_2;
353 freq.setname = setname;
355 if (!(fres = bend_fetch(a->backend, &freq, 0)))
357 *pres = Z_PRES_FAILURE;
358 return diagrec(a->proto, 2, "Backend interface problem");
360 /* backend should be able to signal whether error is system-wide
361 or only pertaining to current record */
364 *pres = Z_PRES_FAILURE;
365 return diagrec(a->proto, fres->errcode, fres->errstring);
367 fprintf(stderr, " Got record, len=%d, total=%d\n",
368 fres->len, total_length);
369 if (fres->len + total_length > a->preferredMessageSize)
371 fprintf(stderr, " In drop-zone\n");
372 /* record is small enough, really */
373 if (fres->len <= a->preferredMessageSize)
375 fprintf(stderr, " Dropped last normal-sized record\n");
376 *pres = Z_PRES_PARTIAL_2;
379 /* record can only be fetched by itself */
380 if (fres->len < a->maximumRecordSize)
382 fprintf(stderr, " Record > prefmsgsz\n");
385 fprintf(stderr, " Dropped it\n");
386 reclist.records[reclist.num_records] =
387 surrogatediagrec(a->proto, fres->basename, 16, 0);
388 reclist.num_records++;
389 *pres = Z_PRES_PARTIAL_2;
393 else /* too big entirely */
395 fprintf(stderr, "Record > maxrcdsz\n");
396 reclist.records[reclist.num_records] =
397 surrogatediagrec(a->proto, fres->basename, 17, 0);
398 reclist.num_records++;
399 *pres = Z_PRES_PARTIAL_2;
403 if (!(thisrec = odr_malloc(a->encode, sizeof(*thisrec))))
405 if (!(thisrec->databaseName = odr_malloc(a->encode,
406 strlen(fres->basename) + 1)))
408 strcpy(thisrec->databaseName, fres->basename);
409 thisrec->which = Z_NamePlusRecord_databaseRecord;
410 if (!(thisrec->u.databaseRecord = thisext = odr_malloc(a->encode,
411 sizeof(Z_DatabaseRecord))))
413 thisext->direct_reference = oid; /* should be OID for current MARC */
414 thisext->indirect_reference = 0;
415 thisext->descriptor = 0;
416 thisext->which = ODR_EXTERNAL_octet;
417 if (!(thisext->u.octet_aligned = odr_malloc(a->encode,
420 if (!(thisext->u.octet_aligned->buf = odr_malloc(a->encode, fres->len)))
422 memcpy(thisext->u.octet_aligned->buf, fres->record, fres->len);
423 thisext->u.octet_aligned->len = thisext->u.octet_aligned->size =
425 reclist.records[reclist.num_records] = thisrec;
426 reclist.num_records++;
427 total_length += fres->len;
429 *next = fres->last_in_set ? 0 : recno + 1;
434 static int process_searchRequest(IOCHAN client, Z_SearchRequest *req)
437 Z_SearchResponse resp;
438 association *assoc = iochan_getdata(client);
441 bend_searchrequest bsrq;
442 bend_searchresult *bsrt;
446 fprintf(stderr, "Got SearchRequest.\n");
448 apdu.which = Z_APDU_searchResponse;
449 apdu.u.searchResponse = &resp;
450 resp.referenceId = req->referenceId;
453 if (req->query->which == Z_Query_type_1)
455 Z_RPNQuery *q = req->query->u.type_1;
457 if (!(oent = oid_getentbyoid(q->attributeSetId)) ||
458 oent->class != CLASS_ATTSET ||
459 oent->value != VAL_BIB1)
460 resp.records = diagrec(assoc->proto, 121, 0);
465 Z_ElementSetNames *setnames;
468 bsrq.setname = req->resultSetName;
469 bsrq.replace_set = *req->replaceIndicator;
470 bsrq.num_bases = req->num_databaseNames;
471 bsrq.basenames = req->databaseNames;
472 bsrq.query = req->query;
474 if (!(bsrt = bend_search(assoc->backend, &bsrq, 0)))
476 else if (bsrt->errcode)
477 resp.records = diagrec(assoc->proto, bsrt->errcode,
482 resp.resultCount = &bsrt->hits;
484 /* how many records does the user agent want, then? */
485 if (bsrt->hits <= *req->smallSetUpperBound)
488 setnames = req->smallSetElementSetNames;
490 else if (bsrt->hits < *req->largeSetLowerBound)
492 toget = *req->mediumSetPresentNumber;
493 setnames = req->mediumSetElementSetNames;
500 resp.records = pack_records(assoc, req->resultSetName, 1, &toget,
501 setnames, &next, &presst);
504 resp.numberOfRecordsReturned = &toget;
505 resp.nextResultSetPosition = &next;
506 resp.searchStatus = &sr;
507 resp.resultSetStatus = &sr;
508 resp.presentStatus = &presst;
513 resp.numberOfRecordsReturned = &nulint;
514 resp.nextResultSetPosition = &nulint;
515 resp.searchStatus = &sr;
516 resp.resultSetStatus = &sr;
517 resp.presentStatus = 0;
522 resp.resultCount = &nulint;
523 resp.numberOfRecordsReturned = &nulint;
524 resp.nextResultSetPosition = &nulint;
525 resp.searchStatus = &nulint;
526 resp.resultSetStatus = 0;
529 if (!z_APDU(assoc->encode, &apdup, 0))
531 odr_perror(assoc->encode, "Encode searchres");
534 odr_getbuf(assoc->encode, &assoc->encoded_len);
535 odr_reset(assoc->encode);
536 iochan_setflags(client, EVENT_OUTPUT | EVENT_EXCEPT);
540 static int process_presentRequest(IOCHAN client, Z_PresentRequest *req)
543 Z_PresentResponse resp;
544 association *assoc = iochan_getdata(client);
545 int presst, next, num;
547 fprintf(stderr, "Got PresentRequest.\n");
549 apdu.which = Z_APDU_presentResponse;
550 apdu.u.presentResponse = &resp;
551 resp.referenceId = req->referenceId;
553 num = *req->numberOfRecordsRequested;
554 resp.records = pack_records(assoc, req->resultSetId,
555 *req->resultSetStartPoint, &num, req->elementSetNames, &next, &presst);
558 resp.numberOfRecordsReturned = #
559 resp.presentStatus = &presst;
560 resp.nextResultSetPosition = &next;
562 if (!z_APDU(assoc->encode, &apdup, 0))
564 odr_perror(assoc->encode, "Encode presentres");
567 odr_getbuf(assoc->encode, &assoc->encoded_len);
568 odr_reset(assoc->encode);
569 iochan_setflags(client, EVENT_OUTPUT | EVENT_EXCEPT);