Fixed memory bug.
[yaz-moved-to-github.git] / server / seshigh.c
1 /*
2  * Copyright (C) 1994, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: seshigh.c,v $
7  * Revision 1.23  1995-05-15 13:25:10  quinn
8  * Fixed memory bug.
9  *
10  * Revision 1.22  1995/05/15  11:56:39  quinn
11  * Asynchronous facilities. Restructuring of seshigh code.
12  *
13  * Revision 1.21  1995/05/02  08:53:19  quinn
14  * Trying in vain to fix comm with ISODE
15  *
16  * Revision 1.20  1995/04/20  15:13:00  quinn
17  * Cosmetic
18  *
19  * Revision 1.19  1995/04/18  08:15:34  quinn
20  * Added dynamic memory allocation on encoding (whew). Code is now somewhat
21  * neater. We'll make the same change for decoding one day.
22  *
23  * Revision 1.18  1995/04/17  11:28:25  quinn
24  * Smallish
25  *
26  * Revision 1.17  1995/04/10  10:23:36  quinn
27  * Some work to add scan and other things.
28  *
29  * Revision 1.16  1995/03/31  09:18:55  quinn
30  * Added logging.
31  *
32  * Revision 1.15  1995/03/30  14:03:23  quinn
33  * Added RFC1006 as separate library
34  *
35  * Revision 1.14  1995/03/30  12:18:17  quinn
36  * Fixed bug.
37  *
38  * Revision 1.13  1995/03/30  09:09:24  quinn
39  * Added state-handle and some support for asynchronous activities.
40  *
41  * Revision 1.12  1995/03/29  15:40:16  quinn
42  * Ongoing work. Statserv is now dynamic by default
43  *
44  * Revision 1.11  1995/03/28  09:16:21  quinn
45  * Added record packing to the search request
46  *
47  * Revision 1.10  1995/03/27  08:34:24  quinn
48  * Added dynamic server functionality.
49  * Released bindings to session.c (is now redundant)
50  *
51  * Revision 1.9  1995/03/22  15:01:26  quinn
52  * Adjusting record packing.
53  *
54  * Revision 1.8  1995/03/22  10:13:21  quinn
55  * Working on record packer
56  *
57  * Revision 1.7  1995/03/21  15:53:31  quinn
58  * Little changes.
59  *
60  * Revision 1.6  1995/03/21  12:30:09  quinn
61  * Beginning to add support for record packing.
62  *
63  * Revision 1.5  1995/03/17  10:44:13  quinn
64  * Added catch of null-string in makediagrec
65  *
66  * Revision 1.4  1995/03/17  10:18:08  quinn
67  * Added memory management.
68  *
69  * Revision 1.3  1995/03/16  17:42:39  quinn
70  * Little changes
71  *
72  * Revision 1.2  1995/03/16  13:29:01  quinn
73  * Partitioned server.
74  *
75  * Revision 1.1  1995/03/15  16:02:10  quinn
76  * Modded session.c to seshigh.c
77  *
78  */
79
80 /*
81  * Frontend server logic.
82  *
83  * This code receives incoming APDUs, and handles client requests by means
84  * of the backend API.
85  *
86  * Some of the code is getting quite involved, compared to simpler servers -
87  * primarily because it is asynchronous both in the communication with
88  * the user and the backend. We think the complexity will pay off in
89  * the form of greater flexibility when more asynchronous facilities
90  * are implemented.
91  *
92  * Memory management has become somewhat involved. In the simple case, where
93  * only one PDU is pending at a time, it will simply reuse the same memory,
94  * once it has found its working size. When we enable multiple concurrent
95  * operations, perhaps even with multiple parallel calls to the backend, it
96  * will maintain a pool of buffers for encoding and decoding, trying to
97  * minimize memory allocation/deallocation during normal operation.
98  *
99  * TODOs include (and will be done in order of public interest):
100  * 
101  * Support for EXPLAIN - provide simple meta-database system.
102  * Support for access control.
103  * Support for resource control.
104  * Support for extended services - primarily Item Order.
105  * Rest of Z39.50-1994
106  *
107  */
108
109 #include <stdlib.h>
110 #include <stdio.h>
111 #include <unistd.h>
112 #include <assert.h>
113
114 #include <dmalloc.h>
115 #include <comstack.h>
116 #include <eventl.h>
117 #include <session.h>
118 #include <proto.h>
119 #include <oid.h>
120 #include <log.h>
121 #include <statserv.h>
122
123 #include <backend.h>
124
125 static int process_request(association *assoc);
126 void backend_response(IOCHAN i, int event);
127 static int process_response(association *assoc, request *req, Z_APDU *res);
128 static Z_APDU *process_initRequest(association *assoc, request *reqb);
129 static Z_APDU *process_searchRequest(association *assoc, request *reqb,
130     int *fd);
131 static Z_APDU *response_searchRequest(association *assoc, request *reqb,
132     bend_searchresult *bsrt, int *fd);
133 static Z_APDU *process_presentRequest(association *assoc, request *reqb,
134     int *fd);
135 static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd);
136
137 static FILE *apduf = 0; /* for use in static mode */
138 static statserv_options_block *control_block = 0;
139
140 /*
141  * Create and initialize a new association-handle.
142  *  channel  : iochannel for the current line.
143  *  link     : communications channel.
144  * Returns: 0 or a new association handle.
145  */
146 association *create_association(IOCHAN channel, COMSTACK link)
147 {
148     association *new;
149
150     if (!control_block)
151         control_block = statserv_getcontrol();
152     if (!(new = malloc(sizeof(*new))))
153         return 0;
154     new->client_chan = channel;
155     new->client_link = link;
156     if (!(new->decode = odr_createmem(ODR_DECODE)) ||
157         !(new->encode = odr_createmem(ODR_ENCODE)))
158         return 0;
159     if (*control_block->apdufile)
160     {
161         char filename[256];
162         FILE *f;
163
164         if (!(new->print = odr_createmem(ODR_PRINT)))
165             return 0;
166         if (*control_block->apdufile != '-')
167         {
168             strcpy(filename, control_block->apdufile);
169             if (!control_block->dynamic)
170             {
171                 if (!apduf)
172                 {
173                     if (!(apduf = fopen(filename, "w")))
174                     {
175                         logf(LOG_WARN|LOG_ERRNO, "%s", filename);
176                         return 0;
177                     }
178                     setvbuf(apduf, 0, _IONBF, 0);
179                 }
180                 f = apduf;
181             }
182             else 
183             {
184                 sprintf(filename + strlen(filename), ".%d", getpid());
185                 if (!(f = fopen(filename, "w")))
186                 {
187                     logf(LOG_WARN|LOG_ERRNO, "%s", filename);
188                     return 0;
189                 }
190                 setvbuf(f, 0, _IONBF, 0);
191             }
192             odr_setprint(new->print, f);
193         }
194     }
195     else
196         new->print = 0;
197     new->input_buffer = 0;
198     new->input_buffer_len = 0;
199     new->backend = 0;
200     request_initq(&new->incoming);
201     request_initq(&new->outgoing);
202     if (cs_getproto(link) == CS_Z3950)
203         new->proto = PROTO_Z3950;
204     else
205         new->proto = PROTO_SR;
206     return new;
207 }
208
209 /*
210  * Free association and release resources.
211  */
212 void destroy_association(association *h)
213 {
214     odr_destroy(h->decode);
215     odr_destroy(h->encode);
216     if (h->print)
217         odr_destroy(h->print);
218     if (h->input_buffer)
219         free(h->input_buffer);
220     if (h->backend)
221         bend_close(h->backend);
222     while (request_deq(&h->incoming));
223     while (request_deq(&h->outgoing));
224     free(h);
225 }
226
227 /*
228  * This is where PDUs from the client are read and the further
229  * processing is initiated. Flow of control moves down through the
230  * various process_* functions below, until the encoded result comes back up
231  * to the output handler in here.
232  * 
233  *  h     : the I/O channel that has an outstanding event.
234  *  event : the current outstanding event.
235  */
236 void ir_session(IOCHAN h, int event)
237 {
238     int res;
239     association *assoc = iochan_getdata(h);
240     COMSTACK conn = assoc->client_link;
241     request *req;
242
243     assert(h && conn && assoc);
244     if (event & EVENT_INPUT || event & EVENT_WORK) /* input */
245     {
246         if (event & EVENT_INPUT)
247         {
248             logf(LOG_DEBUG, "ir_session (input)");
249             assert(assoc && conn);
250             if ((res = cs_get(conn, &assoc->input_buffer,
251                 &assoc->input_buffer_len)) <= 0)
252             {
253                 logf(LOG_LOG, "Connection closed by client");
254                 cs_close(conn);
255                 destroy_association(assoc);
256                 iochan_destroy(h);
257                 return;
258             }
259             else if (res == 1) /* incomplete read - wait for more  */
260                 return;
261             if (cs_more(conn)) /* more stuff - call us again later, please */
262                 iochan_setevent(h, EVENT_INPUT);
263                 
264             /* we got a complete PDU. Let's decode it */
265             req = request_get(); /* get a new request structure */
266             odr_reset(assoc->decode);
267             odr_setbuf(assoc->decode, assoc->input_buffer, res, 0);
268             if (!z_APDU(assoc->decode, &req->request, 0))
269             {
270                 logf(LOG_WARN, "ODR error: %s",
271                     odr_errlist[odr_geterror(assoc->decode)]);
272                 cs_close(conn);
273                 destroy_association(assoc);
274                 iochan_destroy(h);
275                 return;
276             }
277             req->request_mem = odr_extract_mem(assoc->decode);
278             if (assoc->print && !z_APDU(assoc->print, &req->request, 0))
279             {
280                 logf(LOG_WARN, "ODR print error: %s", 
281                     odr_errlist[odr_geterror(assoc->print)]);
282                 odr_reset(assoc->print);
283             }
284             request_enq(&assoc->incoming, req);
285         }
286
287         /* can we do something yet? */
288         req = request_head(&assoc->incoming);
289         if (req->state == REQUEST_IDLE)
290             if (process_request(assoc) < 0)
291             {
292                 cs_close(conn);
293                 destroy_association(assoc);
294                 iochan_destroy(h);
295             }
296     }
297     if (event & EVENT_OUTPUT)
298     {
299         request *req = request_head(&assoc->outgoing);
300
301         logf(LOG_DEBUG, "ir_session (output)");
302         req->state = REQUEST_PENDING;
303         switch (res = cs_put(conn, req->response, req->len_response))
304         {
305             case -1:
306                 logf(LOG_LOG, "Connection closed by client");
307                 cs_close(conn);
308                 destroy_association(assoc);
309                 iochan_destroy(h);
310                 break;
311             case 0: /* all sent - release the request structure */
312                 odr_release_mem(req->request_mem);
313                 request_deq(&assoc->outgoing);
314                 request_release(req);
315                 if (!request_head(&assoc->outgoing))
316                     iochan_clearflag(h, EVENT_OUTPUT);
317                 break;
318             /* value of 1 -- partial send -- is simply ignored */
319         }
320     }
321     if (event & EVENT_EXCEPT)
322     {
323         logf(LOG_DEBUG, "ir_session (exception)");
324         cs_close(conn);
325         destroy_association(assoc);
326         iochan_destroy(h);
327     }
328 }
329
330 /*
331  * Initiate request processing.
332  */
333 static int process_request(association *assoc)
334 {
335     request *req = request_head(&assoc->incoming);
336     int fd = -1;
337     Z_APDU *res;
338
339     logf(LOG_DEBUG, "process_request");
340     assert(req && req->state == REQUEST_IDLE);
341     switch (req->request->which)
342     {
343         case Z_APDU_initRequest:
344             res = process_initRequest(assoc, req); break;
345         case Z_APDU_searchRequest:
346             res = process_searchRequest(assoc, req, &fd); break;
347         case Z_APDU_presentRequest:
348             res = process_presentRequest(assoc, req, &fd); break;
349         case Z_APDU_scanRequest:
350             res = process_scanRequest(assoc, req, &fd); break;
351         default:
352             logf(LOG_WARN, "Bad APDU");
353             return -1;
354     }
355     if (res)
356     {
357         logf(LOG_DEBUG, "  result immediately available");
358         return process_response(assoc, req, res);
359     }
360     else if (fd < 0)
361     {
362         logf(LOG_WARN, "   bad result");
363         return -1;
364     }
365     else /* no result yet - one will be provided later */
366     {
367         IOCHAN chan;
368
369         /* Set up an I/O handler for the fd supplied by the backend */
370
371         logf(LOG_DEBUG, "   establishing handler for result");
372         req->state = REQUEST_PENDING;
373         if (!(chan = iochan_create(fd, backend_response, EVENT_INPUT)))
374             abort();
375         iochan_setdata(chan, assoc);
376         return 0;
377     }
378 }
379
380 /*
381  * Handle message from the backend.
382  */
383 void backend_response(IOCHAN i, int event)
384 {
385     association *assoc = iochan_getdata(i);
386     request *req = request_head(&assoc->incoming);
387     Z_APDU *res;
388     int fd;
389
390     logf(LOG_DEBUG, "backend_response");
391     assert(assoc && req && req->state != REQUEST_IDLE);
392     /* determine what it is we're waiting for */
393     switch (req->request->which)
394     {
395         case Z_APDU_searchRequest:
396             res = response_searchRequest(assoc, req, 0, &fd); break;
397 #if 0
398         case Z_APDU_presentRequest:
399             res = response_presentRequest(assoc, req, 0, &fd); break;
400         case Z_APDU_scanRequest:
401             res = response_scanRequest(assoc, req, 0, &fd); break;
402 #endif
403         default:
404             logf(LOG_WARN, "Serious programmer's lapse or bug");
405             abort();
406     }
407     if ((res && process_response(assoc, req, res) < 0) || fd < 0)
408     {
409         logf(LOG_LOG, "Fatal error when talking to backend");
410         cs_close(assoc->client_link);
411         destroy_association(assoc);
412         iochan_destroy(assoc->client_chan);
413         iochan_destroy(i);
414         return;
415     }
416     else if (!res) /* no result yet - try again later */
417     {
418         logf(LOG_DEBUG, "   no result yet");
419         iochan_setfd(i, fd); /* in case fd has changed */
420     }
421 }
422
423 /*
424  * Encode response, and transfer the request structure to the outgoing queue.
425  */
426 static int process_response(association *assoc, request *req, Z_APDU *res)
427 {
428     odr_setbuf(assoc->encode, req->response, req->size_response, 1);
429     if (!z_APDU(assoc->encode, &res, 0))
430     {
431         logf(LOG_WARN, "ODR error when encoding response: %s",
432             odr_errlist[odr_geterror(assoc->decode)]);
433         return -1;
434     }
435     req->response = odr_getbuf(assoc->encode, &req->len_response,
436         &req->size_response);
437     odr_reset(assoc->encode);
438     /* change this when we make the backend reentrant */
439     assert(req == request_head(&assoc->incoming));
440     req->state = REQUEST_IDLE;
441     request_deq(&assoc->incoming);
442     request_enq(&assoc->outgoing, req);
443     /* turn the work over to the ir_session handler */
444     iochan_setflag(assoc->client_chan, EVENT_OUTPUT);
445     /* Is there more work to be done? */
446     if (request_head(&assoc->incoming))
447         iochan_setevent(assoc->client_chan, EVENT_WORK);
448     return 0;
449 }
450
451 static Z_APDU *process_initRequest(association *assoc, request *reqb)
452 {
453     Z_InitRequest *req = reqb->request->u.initRequest;
454     static Z_APDU apdu;
455     static Z_InitResponse resp;
456     static bool_t result = 1;
457     static Odr_bitmask options, protocolVersion;
458     bend_initrequest binitreq;
459     bend_initresult *binitres;
460
461     logf(LOG_LOG, "Got initRequest");
462     if (req->implementationId)
463         logf(LOG_LOG, "Id:        %s", req->implementationId);
464     if (req->implementationName)
465         logf(LOG_LOG, "Name:      %s", req->implementationName);
466     if (req->implementationVersion)
467         logf(LOG_LOG, "Version:   %s", req->implementationVersion);
468
469     binitreq.configname = "default-config";
470     if (!(binitres = bend_init(&binitreq)) || binitres->errcode)
471     {
472         logf(LOG_WARN, "Negative response from backend");
473         return 0;
474     }
475
476     assoc->backend = binitres->handle;
477     apdu.which = Z_APDU_initResponse;
478     apdu.u.initResponse = &resp;
479     resp.referenceId = req->referenceId;
480     /* let's tell the client what we can do */
481     ODR_MASK_ZERO(&options);
482     if (ODR_MASK_GET(req->options, Z_Options_search))
483         ODR_MASK_SET(&options, Z_Options_search);
484     if (ODR_MASK_GET(req->options, Z_Options_present))
485         ODR_MASK_SET(&options, Z_Options_present);
486 #if 0
487     if (ODR_MASK_GET(req->options, Z_Options_delSet))
488         ODR_MASK_SET(&options, Z_Options_delSet);
489 #endif
490     if (ODR_MASK_GET(req->options, Z_Options_namedResultSets))
491         ODR_MASK_SET(&options, Z_Options_namedResultSets);
492     if (ODR_MASK_GET(req->options, Z_Options_scan))
493         ODR_MASK_SET(&options, Z_Options_scan);
494     if (ODR_MASK_GET(req->options, Z_Options_concurrentOperations))
495         ODR_MASK_SET(&options, Z_Options_concurrentOperations);
496     resp.options = &options;
497
498     ODR_MASK_ZERO(&protocolVersion);
499     if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_1))
500         ODR_MASK_SET(&protocolVersion, Z_ProtocolVersion_1);
501     if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_2))
502         ODR_MASK_SET(&protocolVersion, Z_ProtocolVersion_2);
503     resp.protocolVersion = &protocolVersion;
504     assoc->maximumRecordSize = *req->maximumRecordSize;
505     if (assoc->maximumRecordSize > control_block->maxrecordsize)
506         assoc->maximumRecordSize = control_block->maxrecordsize;
507     assoc->preferredMessageSize = *req->preferredMessageSize;
508     if (assoc->preferredMessageSize > assoc->maximumRecordSize)
509         assoc->preferredMessageSize = assoc->maximumRecordSize;
510     resp.preferredMessageSize = &assoc->preferredMessageSize;
511     resp.maximumRecordSize = &assoc->maximumRecordSize;
512     resp.result = &result;
513     resp.implementationId = "YAZ";
514     resp.implementationName = "Index Data/YAZ Generic Frontend Server";
515     resp.implementationVersion = "$Revision: 1.23 $";
516     resp.userInformationField = 0;
517     return &apdu;
518 }
519
520 static Z_Records *diagrec(oid_proto proto, int error, char *addinfo)
521 {
522     static Z_Records rec;
523     oident bib1;
524     static Z_DiagRec dr;
525     static int err;
526
527     bib1.proto = proto;
528     bib1.class = CLASS_DIAGSET;
529     bib1.value = VAL_BIB1;
530
531     logf(LOG_DEBUG, "Diagnostic: %d -- %s", error, addinfo ? addinfo :
532         "NULL");
533     err = error;
534     rec.which = Z_Records_NSD;
535     rec.u.nonSurrogateDiagnostic = &dr;
536     dr.diagnosticSetId = oid_getoidbyent(&bib1);
537     dr.condition = &err;
538     dr.addinfo = addinfo ? addinfo : "";
539     return &rec;
540 }
541
542 static Z_NamePlusRecord *surrogatediagrec(oid_proto proto, char *dbname,
543                                             int error, char *addinfo)
544 {
545     static Z_NamePlusRecord rec;
546     static Z_DiagRec dr;
547     static int err;
548     oident bib1;
549
550     bib1.proto = proto;
551     bib1.class = CLASS_DIAGSET;
552     bib1.value = VAL_BIB1;
553
554     logf(LOG_DEBUG, "SurrogateDiagnotic: %d -- %s", error, addinfo);
555     err = error;
556     rec.databaseName = dbname;
557     rec.which = Z_NamePlusRecord_surrogateDiagnostic;
558     rec.u.surrogateDiagnostic = &dr;
559     dr.diagnosticSetId = oid_getoidbyent(&bib1);
560     dr.condition = &err;
561     dr.addinfo = addinfo ? addinfo : "";
562     return &rec;
563 }
564
565 static Z_DiagRecs *diagrecs(oid_proto proto, int error, char *addinfo)
566 {
567     static Z_DiagRecs recs;
568     static Z_DiagRec *recp[1], rec;
569     static int err;
570     oident bib1;
571
572     logf(LOG_DEBUG, "DiagRecs: %d -- %s", error, addinfo);
573     bib1.proto = proto;
574     bib1.class = CLASS_DIAGSET;
575     bib1.value = VAL_BIB1;
576
577     err = error;
578     recs.num_diagRecs = 1;
579     recs.diagRecs = recp;
580     recp[0] = &rec;
581     rec.diagnosticSetId = oid_getoidbyent(&bib1);
582     rec.condition = &err;
583     rec.addinfo = addinfo ? addinfo : "";
584     return &recs;
585 }
586
587 #define MAX_RECORDS 256
588
589 static Z_Records *pack_records(association *a, char *setname, int start,
590                                 int *num, Z_ElementSetNames *esn,
591                                 int *next, int *pres)
592 {
593     int recno, total_length = 0, toget = *num;
594     static Z_Records records;
595     static Z_NamePlusRecordList reclist;
596     static Z_NamePlusRecord *list[MAX_RECORDS];
597     oident recform;
598     Odr_oid *oid;
599
600     records.which = Z_Records_DBOSD;
601     records.u.databaseOrSurDiagnostics = &reclist;
602     reclist.num_records = 0;
603     reclist.records = list;
604     *pres = Z_PRES_SUCCESS;
605     *num = 0;
606     *next = 0;
607
608     recform.proto = a->proto;
609     recform.class = CLASS_RECSYN;
610     recform.value = VAL_USMARC;
611     if (!(oid = odr_oiddup(a->encode, oid_getoidbyent(&recform))))
612         return 0;
613
614     logf(LOG_DEBUG, "Request to pack %d+%d", start, toget);
615     logf(LOG_DEBUG, "pms=%d, mrs=%d", a->preferredMessageSize,
616         a->maximumRecordSize);
617     for (recno = start; reclist.num_records < toget; recno++)
618     {
619         bend_fetchrequest freq;
620         bend_fetchresult *fres;
621         Z_NamePlusRecord *thisrec;
622         Z_DatabaseRecord *thisext;
623
624         if (reclist.num_records == MAX_RECORDS - 1)
625         {
626             *pres = Z_PRES_PARTIAL_2;
627             break;
628         }
629         freq.setname = setname;
630         freq.number = recno;
631         if (!(fres = bend_fetch(a->backend, &freq, 0)))
632         {
633             *pres = Z_PRES_FAILURE;
634             return diagrec(a->proto, 2, "Backend interface problem");
635         }
636         /* backend should be able to signal whether error is system-wide
637            or only pertaining to current record */
638         if (fres->errcode)
639         {
640             *pres = Z_PRES_FAILURE;
641             return diagrec(a->proto, fres->errcode, fres->errstring);
642         }
643         logf(LOG_DEBUG, "  fetched record, len=%d, total=%d",
644             fres->len, total_length);
645         if (fres->len + total_length > a->preferredMessageSize)
646         {
647             /* record is small enough, really */
648             if (fres->len <= a->preferredMessageSize)
649             {
650                 logf(LOG_DEBUG, "  Dropped last normal-sized record");
651                 *pres = Z_PRES_PARTIAL_2;
652                 break;
653             }
654             /* record can only be fetched by itself */
655             if (fres->len < a->maximumRecordSize)
656             {
657                 logf(LOG_DEBUG, "  Record > prefmsgsz");
658                 if (toget > 1)
659                 {
660                     logf(LOG_DEBUG, "  Dropped it");
661                     reclist.records[reclist.num_records] =
662                          surrogatediagrec(a->proto, fres->basename, 16, 0);
663                     reclist.num_records++;
664                     *pres = Z_PRES_PARTIAL_2;
665                     break;
666                 }
667             }
668             else /* too big entirely */
669             {
670                 logf(LOG_DEBUG, "Record > maxrcdsz");
671                 reclist.records[reclist.num_records] =
672                     surrogatediagrec(a->proto, fres->basename, 17, 0);
673                 reclist.num_records++;
674                 *pres = Z_PRES_PARTIAL_2;
675                 break;
676             }
677         }
678         if (!(thisrec = odr_malloc(a->encode, sizeof(*thisrec))))
679             return 0;
680         if (!(thisrec->databaseName = odr_malloc(a->encode,
681             strlen(fres->basename) + 1)))
682             return 0;
683         strcpy(thisrec->databaseName, fres->basename);
684         thisrec->which = Z_NamePlusRecord_databaseRecord;
685         if (!(thisrec->u.databaseRecord = thisext = odr_malloc(a->encode,
686             sizeof(Z_DatabaseRecord))))
687             return 0;
688         thisext->direct_reference = oid; /* should be OID for current MARC */
689         thisext->indirect_reference = 0;
690         thisext->descriptor = 0;
691         thisext->which = ODR_EXTERNAL_octet;
692         if (!(thisext->u.octet_aligned = odr_malloc(a->encode,
693             sizeof(Odr_oct))))
694             return 0;
695         if (!(thisext->u.octet_aligned->buf = odr_malloc(a->encode, fres->len)))
696             return 0;
697         memcpy(thisext->u.octet_aligned->buf, fres->record, fres->len);
698         thisext->u.octet_aligned->len = thisext->u.octet_aligned->size =
699             fres->len;
700         reclist.records[reclist.num_records] = thisrec;
701         reclist.num_records++;
702         total_length += fres->len;
703         (*num)++;
704         *next = fres->last_in_set ? 0 : recno + 1;
705     }
706     return &records;
707 }
708
709 static Z_APDU *process_searchRequest(association *assoc, request *reqb,
710     int *fd)
711 {
712     Z_SearchRequest *req = reqb->request->u.searchRequest;
713     bend_searchrequest bsrq;
714     bend_searchresult *bsrt;
715
716     logf(LOG_LOG, "Got SearchRequest.");
717
718     bsrq.setname = req->resultSetName;
719     bsrq.replace_set = *req->replaceIndicator;
720     bsrq.num_bases = req->num_databaseNames;
721     bsrq.basenames = req->databaseNames;
722     bsrq.query = req->query;
723
724     if (!(bsrt = bend_search(assoc->backend, &bsrq, fd)))
725         return 0;
726     return response_searchRequest(assoc, reqb, bsrt, fd);
727 }
728
729 bend_searchresult *bend_searchresponse(void *handle) {return 0;}
730
731 /*
732  * Prepare a searchresponse based on the backend results. We probably want
733  * to look at making the fetching of records nonblocking as well, but
734  * so far, we'll keep things simple.
735  * If bsrt is null, that means we're called in response to a communications
736  * event, and we'll have to get the response for ourselves.
737  */
738 static Z_APDU *response_searchRequest(association *assoc, request *reqb,
739     bend_searchresult *bsrt, int *fd)
740 {
741     Z_SearchRequest *req = reqb->request->u.searchRequest;
742     static Z_APDU apdu;
743     static Z_SearchResponse resp;
744     static int nulint = 0;
745     static bool_t sr = 1;
746     static int next = 0;
747     static int none = Z_RES_NONE;
748
749     apdu.which = Z_APDU_searchResponse;
750     apdu.u.searchResponse = &resp;
751     resp.referenceId = req->referenceId;
752     *fd = -1;
753     if (!bsrt && !(bsrt = bend_searchresponse(assoc->backend)))
754     {
755         logf(LOG_FATAL, "Bad result from backend");
756         return 0;
757     }
758     else if (bsrt->errcode)
759     {
760         resp.records = diagrec(assoc->proto, bsrt->errcode,
761             bsrt->errstring);
762         resp.resultCount = &nulint;
763         resp.numberOfRecordsReturned = &nulint;
764         resp.nextResultSetPosition = &nulint;
765         resp.searchStatus = &nulint;
766         resp.resultSetStatus = &none;
767         resp.presentStatus = 0;
768     }
769     else
770     {
771         int toget;
772         Z_ElementSetNames *setnames;
773         int presst = 0;
774
775         resp.records = 0;
776         resp.resultCount = &bsrt->hits;
777
778         /* how many records does the user agent want, then? */
779         if (bsrt->hits <= *req->smallSetUpperBound)
780         {
781             toget = bsrt->hits;
782             setnames = req->smallSetElementSetNames;
783         }
784         else if (bsrt->hits < *req->largeSetLowerBound)
785         {
786             toget = *req->mediumSetPresentNumber;
787             if (toget > bsrt->hits)
788                 toget = bsrt->hits;
789             setnames = req->mediumSetElementSetNames;
790         }
791         else
792             toget = 0;
793
794         if (toget && !resp.records)
795         {
796             resp.records = pack_records(assoc, req->resultSetName, 1,
797                 &toget, setnames, &next, &presst);
798             if (!resp.records)
799                 return 0;
800             resp.numberOfRecordsReturned = &toget;
801             resp.nextResultSetPosition = &next;
802             resp.searchStatus = &sr;
803             resp.resultSetStatus = 0;
804             resp.presentStatus = &presst;
805         }
806         else
807         {
808             resp.numberOfRecordsReturned = &nulint;
809             resp.nextResultSetPosition = &next;
810             resp.searchStatus = &sr;
811             resp.resultSetStatus = 0;
812             resp.presentStatus = 0;
813         }
814     }
815     return &apdu;
816 }
817
818 /*
819  * Maybe we got a little over-friendly when we designed bend_fetch to
820  * get only one record at a time. Some backends can optimise multiple-record
821  * fetches, and at any rate, there is some overhead involved in
822  * all that selecting and hopping around. Problem is, of course, that the
823  * frontend can't know ahead of time how many records it'll need to
824  * fill the negotiated PDU size. Annoying. Segmentation or not, Z/SR
825  * is downright lousy as a bulk data transfer protocol.
826  *
827  * To start with, we'll do the fetching of records from the backend
828  * in one operation: To save some trips in and out of the event-handler,
829  * and to simplify the interface to pack_records. At any rate, asynch
830  * operation is more fun in operations that have an unpredictable execution
831  * speed - which is normally more true for search than for present.
832  */
833 static Z_APDU *process_presentRequest(association *assoc, request *reqb,
834     int *fd)
835 {
836     Z_PresentRequest *req = reqb->request->u.presentRequest;
837     static Z_APDU apdu;
838     static Z_PresentResponse resp;
839     static int presst, next, num;
840
841     logf(LOG_LOG, "Got PresentRequest.");
842     apdu.which = Z_APDU_presentResponse;
843     apdu.u.presentResponse = &resp;
844     resp.referenceId = req->referenceId;
845
846     num = *req->numberOfRecordsRequested;
847     resp.records = pack_records(assoc, req->resultSetId,
848         *req->resultSetStartPoint, &num, req->elementSetNames, &next, &presst);
849     if (!resp.records)
850         return 0;
851     resp.numberOfRecordsReturned = &num;
852     resp.presentStatus = &presst;
853     resp.nextResultSetPosition = &next;
854
855     return &apdu;
856 }
857
858 /*
859  * Scan was implemented rather in a hurry, and with support for only the basic
860  * elements of the service in the backend API. Suggestions are welcome.
861  */
862 static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd)
863 {
864     Z_ScanRequest *req = reqb->request->u.scanRequest;
865     static Z_APDU apdu;
866     static Z_ScanResponse res;
867     static int scanStatus = Z_Scan_failure;
868     static int numberOfEntriesReturned = 0;
869     oident *attent;
870     static Z_ListEntries ents;
871 #define SCAN_MAX_ENTRIES 200
872     static Z_Entry *tab[SCAN_MAX_ENTRIES];
873     bend_scanrequest srq;
874     bend_scanresult *srs;
875
876     apdu.which = Z_APDU_scanResponse;
877     apdu.u.scanResponse = &res;
878     res.referenceId = req->referenceId;
879     res.stepSize = 0;
880     res.scanStatus = &scanStatus;
881     res.numberOfEntriesReturned = &numberOfEntriesReturned;
882     res.positionOfTerm = 0;
883     res.entries = &ents;
884     ents.which = Z_ListEntries_nonSurrogateDiagnostics;
885     res.attributeSet = 0;
886
887     if (req->attributeSet && (!(attent = oid_getentbyoid(req->attributeSet)) ||
888         attent->class != CLASS_ATTSET || attent->value != VAL_BIB1))
889         ents.u.nonSurrogateDiagnostics = diagrecs(assoc->proto, 121, 0);
890     else if (req->stepSize && *req->stepSize > 0)
891         ents.u.nonSurrogateDiagnostics = diagrecs(assoc->proto, 205, 0);
892     else
893     {
894         srq.num_bases = req->num_databaseNames;
895         srq.basenames = req->databaseNames;
896         srq.num_entries = *req->numberOfTermsRequested;
897         srq.term = req->termListAndStartPoint;
898         srq.term_position = req->preferredPositionInResponse ?
899             *req->preferredPositionInResponse : 1;
900         if (!(srs = bend_scan(assoc->backend, &srq, 0)))
901             ents.u.nonSurrogateDiagnostics = diagrecs(assoc->proto, 2, 0);
902         else if (srs->errcode)
903             ents.u.nonSurrogateDiagnostics = diagrecs(assoc->proto,
904                 srs->errcode, srs->errstring);
905         else
906         {
907             int i;
908             static Z_Entries list;
909
910             if (srs->status == BEND_SCAN_PARTIAL)
911                 scanStatus = Z_Scan_partial_5;
912             else
913                 scanStatus = Z_Scan_success;
914             ents.which = Z_ListEntries_entries;
915             ents.u.entries = &list;
916             list.entries = tab;
917             for (i = 0; i < srs->num_entries; i++)
918             {
919                 Z_Entry *e;
920                 Z_TermInfo *t;
921                 Odr_oct *o;
922
923                 if (i >= SCAN_MAX_ENTRIES)
924                 {
925                     scanStatus = Z_Scan_partial_4;
926                     break;
927                 }
928                 list.entries[i] = e = odr_malloc(assoc->encode, sizeof(*e));
929                 e->which = Z_Entry_termInfo;
930                 e->u.termInfo = t = odr_malloc(assoc->encode, sizeof(*t));
931                 t->suggestedAttributes = 0;
932                 t->alternativeTerm = 0;
933                 t->byAttributes = 0;
934                 t->globalOccurrences = &srs->entries[i].occurrences;
935                 t->term = odr_malloc(assoc->encode, sizeof(*t->term));
936                 t->term->which = Z_Term_general;
937                 t->term->u.general = o = odr_malloc(assoc->encode,
938                     sizeof(Odr_oct));
939                 o->buf = odr_malloc(assoc->encode, o->len = o->size =
940                     strlen(srs->entries[i].term));
941                 memcpy(o->buf, srs->entries[i].term, o->len);
942             }
943             list.num_entries = i;
944             res.numberOfEntriesReturned = &list.num_entries;
945             res.positionOfTerm = &srs->term_position;
946         }
947     }
948
949     return &apdu;
950 }