Minor adjustments
[yaz-moved-to-github.git] / server / seshigh.c
1 /*
2  * Copyright (c) 1995, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: seshigh.c,v $
7  * Revision 1.53  1995-11-01 13:54:58  quinn
8  * Minor adjustments
9  *
10  * Revision 1.52  1995/11/01  12:19:13  quinn
11  * Second attempt to fix same bug.
12  *
13  * Revision 1.50  1995/10/25  16:58:32  quinn
14  * Simple.
15  *
16  * Revision 1.49  1995/10/16  13:51:53  quinn
17  * Changes to provide Especs to the backend.
18  *
19  * Revision 1.48  1995/10/06  08:51:20  quinn
20  * Added Write-buffer.
21  *
22  * Revision 1.47  1995/08/29  14:24:16  quinn
23  * Added second half of close-handshake
24  *
25  * Revision 1.46  1995/08/29  11:17:58  quinn
26  * Added code to receive close
27  *
28  * Revision 1.45  1995/08/21  09:11:00  quinn
29  * Smallish fixes to suppport new formats.
30  *
31  * Revision 1.44  1995/08/17  12:45:25  quinn
32  * Fixed minor problems with GRS-1. Added support in c&s.
33  *
34  * Revision 1.43  1995/08/15  12:00:31  quinn
35  * Updated External
36  *
37  * Revision 1.42  1995/08/15  11:16:50  quinn
38  *
39  * Revision 1.41  1995/08/02  10:23:06  quinn
40  * Smallish
41  *
42  * Revision 1.40  1995/07/31  14:34:26  quinn
43  * Fixed bug in process_searchResponse (numberOfRecordsReturned).
44  *
45  * Revision 1.39  1995/06/27  13:21:00  quinn
46  * SUTRS support
47  *
48  * Revision 1.38  1995/06/19  12:39:11  quinn
49  * Fixed bug in timeout code. Added BER dumper.
50  *
51  * Revision 1.37  1995/06/16  13:16:14  quinn
52  * Fixed Defaultdiagformat.
53  *
54  * Revision 1.36  1995/06/16  10:31:36  quinn
55  * Added session timeout.
56  *
57  * Revision 1.35  1995/06/15  07:45:14  quinn
58  * Moving to v3.
59  *
60  * Revision 1.34  1995/06/14  15:26:46  quinn
61  * *** empty log message ***
62  *
63  * Revision 1.33  1995/06/06  14:57:05  quinn
64  * Better diagnostics.
65  *
66  * Revision 1.32  1995/06/06  08:41:44  quinn
67  * Better diagnostics.
68  *
69  * Revision 1.31  1995/06/06  08:15:37  quinn
70  * Cosmetic.
71  *
72  * Revision 1.30  1995/06/05  10:53:32  quinn
73  * Added a better SCAN.
74  *
75  * Revision 1.29  1995/06/01  11:25:03  quinn
76  * Smallish.
77  *
78  * Revision 1.28  1995/06/01  11:21:01  quinn
79  * Attempting to fix a bug in pack-records. replaced break with continue
80  * for large records, according to standard.
81  *
82  * Revision 1.27  1995/05/29  08:12:06  quinn
83  * Moved oid to util
84  *
85  * Revision 1.26  1995/05/18  13:02:12  quinn
86  * Smallish.
87  *
88  * Revision 1.25  1995/05/17  08:42:26  quinn
89  * Transfer auth info to backend. Allow backend to reject init gracefully.
90  *
91  * Revision 1.24  1995/05/16  08:51:04  quinn
92  * License, documentation, and memory fixes
93  *
94  * Revision 1.23  1995/05/15  13:25:10  quinn
95  * Fixed memory bug.
96  *
97  * Revision 1.22  1995/05/15  11:56:39  quinn
98  * Asynchronous facilities. Restructuring of seshigh code.
99  *
100  * Revision 1.21  1995/05/02  08:53:19  quinn
101  * Trying in vain to fix comm with ISODE
102  *
103  * Revision 1.20  1995/04/20  15:13:00  quinn
104  * Cosmetic
105  *
106  * Revision 1.19  1995/04/18  08:15:34  quinn
107  * Added dynamic memory allocation on encoding (whew). Code is now somewhat
108  * neater. We'll make the same change for decoding one day.
109  *
110  * Revision 1.18  1995/04/17  11:28:25  quinn
111  * Smallish
112  *
113  * Revision 1.17  1995/04/10  10:23:36  quinn
114  * Some work to add scan and other things.
115  *
116  * Revision 1.16  1995/03/31  09:18:55  quinn
117  * Added logging.
118  *
119  * Revision 1.15  1995/03/30  14:03:23  quinn
120  * Added RFC1006 as separate library
121  *
122  * Revision 1.14  1995/03/30  12:18:17  quinn
123  * Fixed bug.
124  *
125  * Revision 1.13  1995/03/30  09:09:24  quinn
126  * Added state-handle and some support for asynchronous activities.
127  *
128  * Revision 1.12  1995/03/29  15:40:16  quinn
129  * Ongoing work. Statserv is now dynamic by default
130  *
131  * Revision 1.11  1995/03/28  09:16:21  quinn
132  * Added record packing to the search request
133  *
134  * Revision 1.10  1995/03/27  08:34:24  quinn
135  * Added dynamic server functionality.
136  * Released bindings to session.c (is now redundant)
137  *
138  * Revision 1.9  1995/03/22  15:01:26  quinn
139  * Adjusting record packing.
140  *
141  * Revision 1.8  1995/03/22  10:13:21  quinn
142  * Working on record packer
143  *
144  * Revision 1.7  1995/03/21  15:53:31  quinn
145  * Little changes.
146  *
147  * Revision 1.6  1995/03/21  12:30:09  quinn
148  * Beginning to add support for record packing.
149  *
150  * Revision 1.5  1995/03/17  10:44:13  quinn
151  * Added catch of null-string in makediagrec
152  *
153  * Revision 1.4  1995/03/17  10:18:08  quinn
154  * Added memory management.
155  *
156  * Revision 1.3  1995/03/16  17:42:39  quinn
157  * Little changes
158  *
159  * Revision 1.2  1995/03/16  13:29:01  quinn
160  * Partitioned server.
161  *
162  * Revision 1.1  1995/03/15  16:02:10  quinn
163  * Modded session.c to seshigh.c
164  *
165  */
166
167 /*
168  * Frontend server logic.
169  *
170  * This code receives incoming APDUs, and handles client requests by means
171  * of the backend API.
172  *
173  * Some of the code is getting quite involved, compared to simpler servers -
174  * primarily because it is asynchronous both in the communication with
175  * the user and the backend. We think the complexity will pay off in
176  * the form of greater flexibility when more asynchronous facilities
177  * are implemented.
178  *
179  * Memory management has become somewhat involved. In the simple case, where
180  * only one PDU is pending at a time, it will simply reuse the same memory,
181  * once it has found its working size. When we enable multiple concurrent
182  * operations, perhaps even with multiple parallel calls to the backend, it
183  * will maintain a pool of buffers for encoding and decoding, trying to
184  * minimize memory allocation/deallocation during normal operation.
185  *
186  * TODOs include (and will be done in order of public interest):
187  * 
188  * Support for EXPLAIN - provide simple meta-database system.
189  * Support for access control.
190  * Support for resource control.
191  * Support for extended services - primarily Item Order.
192  * Rest of Z39.50-1994
193  *
194  */
195
196 #include <stdlib.h>
197 #include <stdio.h>
198 #include <unistd.h>
199 #include <assert.h>
200 #include <sys/time.h>
201
202 #include <xmalloc.h>
203 #include <comstack.h>
204 #include <eventl.h>
205 #include <session.h>
206 #include <proto.h>
207 #include <oid.h>
208 #include <log.h>
209 #include <statserv.h>
210
211 #include <backend.h>
212
213 static int process_request(association *assoc);
214 void backend_response(IOCHAN i, int event);
215 static int process_response(association *assoc, request *req, Z_APDU *res);
216 static Z_APDU *process_initRequest(association *assoc, request *reqb);
217 static Z_APDU *process_searchRequest(association *assoc, request *reqb,
218     int *fd);
219 static Z_APDU *response_searchRequest(association *assoc, request *reqb,
220     bend_searchresult *bsrt, int *fd);
221 static Z_APDU *process_presentRequest(association *assoc, request *reqb,
222     int *fd);
223 static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd);
224 static void process_close(association *assoc, request *reqb);
225
226 static FILE *apduf = 0; /* for use in static mode */
227 static statserv_options_block *control_block = 0;
228
229 /*
230  * Create and initialize a new association-handle.
231  *  channel  : iochannel for the current line.
232  *  link     : communications channel.
233  * Returns: 0 or a new association handle.
234  */
235 association *create_association(IOCHAN channel, COMSTACK link)
236 {
237     association *new;
238
239     if (!control_block)
240         control_block = statserv_getcontrol();
241     if (!(new = xmalloc(sizeof(*new))))
242         return 0;
243     new->client_chan = channel;
244     new->client_link = link;
245     if (!(new->decode = odr_createmem(ODR_DECODE)) ||
246         !(new->encode = odr_createmem(ODR_ENCODE)))
247         return 0;
248     if (*control_block->apdufile)
249     {
250         char filename[256];
251         FILE *f;
252
253         strcpy(filename, control_block->apdufile);
254         if (!(new->print = odr_createmem(ODR_PRINT)))
255             return 0;
256         if (*control_block->apdufile != '-')
257         {
258             strcpy(filename, control_block->apdufile);
259             if (!control_block->dynamic)
260             {
261                 if (!apduf)
262                 {
263                     if (!(apduf = fopen(filename, "w")))
264                     {
265                         logf(LOG_WARN|LOG_ERRNO, "%s", filename);
266                         return 0;
267                     }
268                     setvbuf(apduf, 0, _IONBF, 0);
269                 }
270                 f = apduf;
271             }
272             else 
273             {
274                 sprintf(filename + strlen(filename), ".%d", getpid());
275                 if (!(f = fopen(filename, "w")))
276                 {
277                     logf(LOG_WARN|LOG_ERRNO, "%s", filename);
278                     return 0;
279                 }
280                 setvbuf(f, 0, _IONBF, 0);
281             }
282             odr_setprint(new->print, f);
283         }
284     }
285     else
286         new->print = 0;
287     new->input_buffer = 0;
288     new->input_buffer_len = 0;
289     new->backend = 0;
290     new->state = ASSOC_NEW;
291     request_initq(&new->incoming);
292     request_initq(&new->outgoing);
293     new->proto = cs_getproto(link);
294     return new;
295 }
296
297 /*
298  * Free association and release resources.
299  */
300 void destroy_association(association *h)
301 {
302     odr_destroy(h->decode);
303     odr_destroy(h->encode);
304     if (h->print)
305         odr_destroy(h->print);
306     if (h->input_buffer)
307     xfree(h->input_buffer);
308     if (h->backend)
309         bend_close(h->backend);
310     while (request_deq(&h->incoming));
311     while (request_deq(&h->outgoing));
312    xfree(h);
313 }
314
315 static void do_close(association *a, int reason, char *message)
316 {
317     Z_APDU apdu;
318     Z_Close *cls = zget_Close(a->encode);
319     request *req = request_get();
320
321     /* Purge request queue */
322     while (request_deq(&a->incoming));
323     while (request_deq(&a->outgoing));
324     if (a->version >= 3)
325     {
326         logf(LOG_DEBUG, "Generating Close PDU");
327         apdu.which = Z_APDU_close;
328         apdu.u.close = cls;
329         *cls->closeReason = reason;
330         cls->diagnosticInformation = message;
331         process_response(a, req, &apdu);
332         iochan_settimeout(a->client_chan, 60);
333     }
334     else
335     {
336         logf(LOG_DEBUG, "v2 client. No Close PDU");
337         iochan_setevent(a->client_chan, EVENT_TIMEOUT); /* force imm close */
338     }
339     a->state = ASSOC_DEAD;
340 }
341
342 /*
343  * This is where PDUs from the client are read and the further
344  * processing is initiated. Flow of control moves down through the
345  * various process_* functions below, until the encoded result comes back up
346  * to the output handler in here.
347  * 
348  *  h     : the I/O channel that has an outstanding event.
349  *  event : the current outstanding event.
350  */
351 void ir_session(IOCHAN h, int event)
352 {
353     int res;
354     association *assoc = iochan_getdata(h);
355     COMSTACK conn = assoc->client_link;
356     request *req;
357
358     assert(h && conn && assoc);
359     if (event == EVENT_TIMEOUT)
360     {
361         if (assoc->state != ASSOC_UP)
362         {
363             logf(LOG_LOG, "Final timeout - closing connection.");
364             cs_close(conn);
365             destroy_association(assoc);
366             iochan_destroy(h);
367         }
368         else
369         {
370             logf(LOG_LOG, "Session idle too long. Sending close.");
371             do_close(assoc, Z_Close_lackOfActivity, 0);
372         }
373         return;
374     }
375     if (event & EVENT_INPUT || event & EVENT_WORK) /* input */
376     {
377         if (event & EVENT_INPUT)
378         {
379             logf(LOG_DEBUG, "ir_session (input)");
380             assert(assoc && conn);
381             /* We aren't speaking to this fellow */
382             if (assoc->state == ASSOC_DEAD)
383             {
384                 logf(LOG_LOG, "Closed connection after reject");
385                 cs_close(conn);
386                 destroy_association(assoc);
387                 iochan_destroy(h);
388                 return;
389             }
390             if ((res = cs_get(conn, &assoc->input_buffer,
391                 &assoc->input_buffer_len)) <= 0)
392             {
393                 logf(LOG_LOG, "Connection closed by client");
394                 cs_close(conn);
395                 destroy_association(assoc);
396                 iochan_destroy(h);
397                 return;
398             }
399             else if (res == 1) /* incomplete read - wait for more  */
400                 return;
401             if (cs_more(conn)) /* more stuff - call us again later, please */
402                 iochan_setevent(h, EVENT_INPUT);
403                 
404             /* we got a complete PDU. Let's decode it */
405             logf(LOG_DEBUG, "Got PDU, %d bytes", res);
406             req = request_get(); /* get a new request structure */
407             odr_reset(assoc->decode);
408             odr_setbuf(assoc->decode, assoc->input_buffer, res, 0);
409             if (!z_APDU(assoc->decode, &req->request, 0))
410             {
411                 logf(LOG_LOG, "ODR error on incoming PDU: %s [near byte %d] ",
412                     odr_errlist[odr_geterror(assoc->decode)],
413                     odr_offset(assoc->decode));
414                 logf(LOG_LOG, "PDU dump:");
415                 odr_dumpBER(log_file(), assoc->input_buffer, res);
416                 do_close(assoc, Z_Close_protocolError, "Malformed package");
417                 return;
418             }
419             req->request_mem = odr_extract_mem(assoc->decode);
420             if (assoc->print && !z_APDU(assoc->print, &req->request, 0))
421             {
422                 logf(LOG_WARN, "ODR print error: %s", 
423                     odr_errlist[odr_geterror(assoc->print)]);
424                 odr_reset(assoc->print);
425             }
426             request_enq(&assoc->incoming, req);
427         }
428
429         /* can we do something yet? */
430         req = request_head(&assoc->incoming);
431         if (req->state == REQUEST_IDLE)
432             if (process_request(assoc) < 0)
433                 do_close(assoc, Z_Close_systemProblem, "Unknown error");
434     }
435     if (event & EVENT_OUTPUT)
436     {
437         request *req = request_head(&assoc->outgoing);
438
439         logf(LOG_DEBUG, "ir_session (output)");
440         req->state = REQUEST_PENDING;
441         switch (res = cs_put(conn, req->response, req->len_response))
442         {
443             case -1:
444                 logf(LOG_LOG, "Connection closed by client");
445                 cs_close(conn);
446                 destroy_association(assoc);
447                 iochan_destroy(h);
448                 break;
449             case 0: /* all sent - release the request structure */
450                 logf(LOG_DEBUG, "Wrote PDU, %d bytes", req->len_response);
451                 odr_release_mem(req->request_mem);
452                 request_deq(&assoc->outgoing);
453                 request_release(req);
454                 if (!request_head(&assoc->outgoing))
455                     iochan_clearflag(h, EVENT_OUTPUT);
456                 break;
457             /* value of 1 -- partial send -- is simply ignored */
458         }
459     }
460     if (event & EVENT_EXCEPT)
461     {
462         logf(LOG_DEBUG, "ir_session (exception)");
463         cs_close(conn);
464         destroy_association(assoc);
465         iochan_destroy(h);
466     }
467 }
468
469 /*
470  * Initiate request processing.
471  */
472 static int process_request(association *assoc)
473 {
474     request *req = request_head(&assoc->incoming);
475     int fd = -1;
476     Z_APDU *res;
477
478     logf(LOG_DEBUG, "process_request");
479     assert(req && req->state == REQUEST_IDLE);
480     switch (req->request->which)
481     {
482         case Z_APDU_initRequest:
483             res = process_initRequest(assoc, req); break;
484         case Z_APDU_searchRequest:
485             res = process_searchRequest(assoc, req, &fd); break;
486         case Z_APDU_presentRequest:
487             res = process_presentRequest(assoc, req, &fd); break;
488         case Z_APDU_scanRequest:
489             res = process_scanRequest(assoc, req, &fd); break;
490         case Z_APDU_close:
491             process_close(assoc, req); return 0;
492         default:
493             logf(LOG_WARN, "Bad APDU received");
494             return -1;
495     }
496     if (res)
497     {
498         logf(LOG_DEBUG, "  result immediately available");
499         return process_response(assoc, req, res);
500     }
501     else if (fd < 0)
502     {
503         logf(LOG_WARN, "   bad result");
504         return -1;
505     }
506     else /* no result yet - one will be provided later */
507     {
508         IOCHAN chan;
509
510         /* Set up an I/O handler for the fd supplied by the backend */
511
512         logf(LOG_DEBUG, "   establishing handler for result");
513         req->state = REQUEST_PENDING;
514         if (!(chan = iochan_create(fd, backend_response, EVENT_INPUT)))
515             abort();
516         iochan_setdata(chan, assoc);
517         return 0;
518     }
519 }
520
521 /*
522  * Handle message from the backend.
523  */
524 void backend_response(IOCHAN i, int event)
525 {
526     association *assoc = iochan_getdata(i);
527     request *req = request_head(&assoc->incoming);
528     Z_APDU *res;
529     int fd;
530
531     logf(LOG_DEBUG, "backend_response");
532     assert(assoc && req && req->state != REQUEST_IDLE);
533     /* determine what it is we're waiting for */
534     switch (req->request->which)
535     {
536         case Z_APDU_searchRequest:
537             res = response_searchRequest(assoc, req, 0, &fd); break;
538 #if 0
539         case Z_APDU_presentRequest:
540             res = response_presentRequest(assoc, req, 0, &fd); break;
541         case Z_APDU_scanRequest:
542             res = response_scanRequest(assoc, req, 0, &fd); break;
543 #endif
544         default:
545             logf(LOG_WARN, "Serious programmer's lapse or bug");
546             abort();
547     }
548     if ((res && process_response(assoc, req, res) < 0) || fd < 0)
549     {
550         logf(LOG_LOG, "Fatal error when talking to backend");
551         do_close(assoc, Z_Close_systemProblem, 0);
552         iochan_destroy(i);
553         return;
554     }
555     else if (!res) /* no result yet - try again later */
556     {
557         logf(LOG_DEBUG, "   no result yet");
558         iochan_setfd(i, fd); /* in case fd has changed */
559     }
560 }
561
562 /*
563  * Encode response, and transfer the request structure to the outgoing queue.
564  */
565 static int process_response(association *assoc, request *req, Z_APDU *res)
566 {
567     odr_setbuf(assoc->encode, req->response, req->size_response, 1);
568     if (!z_APDU(assoc->encode, &res, 0))
569     {
570         logf(LOG_WARN, "ODR error when encoding response: %s",
571             odr_errlist[odr_geterror(assoc->decode)]);
572         return -1;
573     }
574     req->response = odr_getbuf(assoc->encode, &req->len_response,
575         &req->size_response);
576     odr_setbuf(assoc->encode, 0, 0, 0); /* don'txfree if we abort later */
577     odr_reset(assoc->encode);
578     if (assoc->print && !z_APDU(assoc->print, &res, 0))
579     {
580         logf(LOG_WARN, "ODR print error: %s", 
581             odr_errlist[odr_geterror(assoc->print)]);
582         odr_reset(assoc->print);
583     }
584     /* change this when we make the backend reentrant */
585     if (req == request_head(&assoc->incoming))
586     {
587         req->state = REQUEST_IDLE;
588         request_deq(&assoc->incoming);
589     }
590     request_enq(&assoc->outgoing, req);
591     /* turn the work over to the ir_session handler */
592     iochan_setflag(assoc->client_chan, EVENT_OUTPUT);
593     /* Is there more work to be done? give that to the input handler too */
594     if (request_head(&assoc->incoming))
595         iochan_setevent(assoc->client_chan, EVENT_WORK);
596     return 0;
597 }
598
599 /*
600  * Handle init request.
601  * At the moment, we don't check the options
602  * anywhere else in the code - we just try not to do anything that would
603  * break a naive client. We'll toss 'em into the association block when
604  * we need them there.
605  */
606 static Z_APDU *process_initRequest(association *assoc, request *reqb)
607 {
608     Z_InitRequest *req = reqb->request->u.initRequest;
609     Z_APDU *apdu = zget_APDU(assoc->encode, Z_APDU_initResponse);
610     Z_InitResponse *resp = apdu->u.initResponse;
611     bend_initrequest binitreq;
612     bend_initresult *binitres;
613     char options[100];
614
615     logf(LOG_LOG, "Got initRequest");
616     if (req->implementationId)
617         logf(LOG_LOG, "Id:        %s", req->implementationId);
618     if (req->implementationName)
619         logf(LOG_LOG, "Name:      %s", req->implementationName);
620     if (req->implementationVersion)
621         logf(LOG_LOG, "Version:   %s", req->implementationVersion);
622
623     binitreq.configname = "default-config";
624     binitreq.auth = req->idAuthentication;
625     if (!(binitres = bend_init(&binitreq)))
626     {
627         logf(LOG_WARN, "Bad response from backend.");
628         return 0;
629     }
630
631     assoc->backend = binitres->handle;
632     resp->referenceId = req->referenceId;
633     *options = '\0';
634     /* let's tell the client what we can do */
635     if (ODR_MASK_GET(req->options, Z_Options_search))
636     {
637         ODR_MASK_SET(resp->options, Z_Options_search);
638         strcat(options, "srch");
639     }
640     if (ODR_MASK_GET(req->options, Z_Options_present))
641     {
642         ODR_MASK_SET(resp->options, Z_Options_present);
643         strcat(options, " prst");
644     }
645 #if 0
646     if (ODR_MASK_GET(req->options, Z_Options_delSet))
647     {
648         ODR_MASK_SET(&options, Z_Options_delSet);
649         strcat(options, " del");
650     }
651 #endif
652     if (ODR_MASK_GET(req->options, Z_Options_namedResultSets))
653     {
654         ODR_MASK_SET(resp->options, Z_Options_namedResultSets);
655         strcat(options, " namedresults");
656     }
657     if (ODR_MASK_GET(req->options, Z_Options_scan))
658     {
659         ODR_MASK_SET(resp->options, Z_Options_scan);
660         strcat(options, " scan");
661     }
662     if (ODR_MASK_GET(req->options, Z_Options_concurrentOperations))
663     {
664         ODR_MASK_SET(resp->options, Z_Options_concurrentOperations);
665         strcat(options, " concurop");
666     }
667
668     if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_1))
669     {
670         ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_1);
671         assoc->version = 2; /* 1 & 2 are equivalent */
672     }
673     if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_2))
674     {
675         ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_2);
676         assoc->version = 2;
677     }
678     if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_3))
679     {
680         ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_3);
681         assoc->version = 3;
682     }
683     logf(LOG_LOG, "Negotiated to v%d: %s", assoc->version, options);
684     assoc->maximumRecordSize = *req->maximumRecordSize;
685     if (assoc->maximumRecordSize > control_block->maxrecordsize)
686         assoc->maximumRecordSize = control_block->maxrecordsize;
687     assoc->preferredMessageSize = *req->preferredMessageSize;
688     if (assoc->preferredMessageSize > assoc->maximumRecordSize)
689         assoc->preferredMessageSize = assoc->maximumRecordSize;
690     resp->preferredMessageSize = &assoc->preferredMessageSize;
691     resp->maximumRecordSize = &assoc->maximumRecordSize;
692     resp->implementationName = "Index Data/YAZ Generic Frontend Server";
693     if (binitres->errcode)
694     {
695         logf(LOG_LOG, "Connection rejected by backend.");
696         *resp->result = 0;
697         assoc->state = ASSOC_DEAD;
698     }
699     else
700         assoc->state = ASSOC_UP;
701     return apdu;
702 }
703
704 /*
705  * These functions should be merged.
706  */
707
708 /*
709  * nonsurrogate diagnostic record.
710  */
711 static Z_Records *diagrec(oid_proto proto, int error, char *addinfo)
712 {
713     static Z_Records rec;
714     oident bib1;
715     static int err;
716 #ifdef Z_95
717     static Z_DiagRec drec;
718     static Z_DefaultDiagFormat dr;
719 #else
720     static Z_DiagRec dr;
721 #endif
722
723     bib1.proto = proto;
724     bib1.class = CLASS_DIAGSET;
725     bib1.value = VAL_BIB1;
726
727     logf(LOG_DEBUG, "Diagnostic: %d -- %s", error, addinfo ? addinfo :
728         "NULL");
729     err = error;
730     rec.which = Z_Records_NSD;
731 #ifdef Z_95
732     rec.u.nonSurrogateDiagnostic = &drec;
733     drec.which = Z_DiagRec_defaultFormat;
734     drec.u.defaultFormat = &dr;
735 #else
736     rec.u.nonSurrogateDiagnostic = &dr;
737 #endif
738     dr.diagnosticSetId = oid_getoidbyent(&bib1);
739     dr.condition = &err;
740     dr.which = Z_DiagForm_v2AddInfo;
741     dr.addinfo = addinfo ? addinfo : "";
742     return &rec;
743 }
744
745 /*
746  * surrogate diagnostic.
747  */
748 static Z_NamePlusRecord *surrogatediagrec(oid_proto proto, char *dbname,
749                                             int error, char *addinfo)
750 {
751     static Z_NamePlusRecord rec;
752     static int err;
753     oident bib1;
754 #ifdef Z_95
755     static Z_DiagRec drec;
756     static Z_DefaultDiagFormat dr;
757 #else
758     static Z_DiagRec dr;
759 #endif
760
761     bib1.proto = proto;
762     bib1.class = CLASS_DIAGSET;
763     bib1.value = VAL_BIB1;
764
765     logf(LOG_DEBUG, "SurrogateDiagnotic: %d -- %s", error, addinfo);
766     err = error;
767     rec.databaseName = dbname;
768     rec.which = Z_NamePlusRecord_surrogateDiagnostic;
769 #ifdef Z_95
770     rec.u.surrogateDiagnostic = &drec;
771     drec.which = Z_DiagRec_defaultFormat;
772     drec.u.defaultFormat = &dr;
773 #else
774     rec.u.surrogateDiagnostic = &dr;
775 #endif
776     dr.diagnosticSetId = oid_getoidbyent(&bib1);
777     dr.condition = &err;
778     dr.which = Z_DiagForm_v2AddInfo;
779     dr.addinfo = addinfo ? addinfo : "";
780     return &rec;
781 }
782
783 /*
784  * multiple nonsurrogate diagnostics.
785  */
786 static Z_DiagRecs *diagrecs(oid_proto proto, int error, char *addinfo)
787 {
788     static Z_DiagRecs recs;
789     static int err;
790     oident bib1;
791 #ifdef Z_95
792     static Z_DiagRec *recp[1], drec;
793     static Z_DefaultDiagFormat rec;
794 #else
795     static Z_DiagRec *recp[1], rec;
796 #endif
797
798     logf(LOG_DEBUG, "DiagRecs: %d -- %s", error, addinfo);
799     bib1.proto = proto;
800     bib1.class = CLASS_DIAGSET;
801     bib1.value = VAL_BIB1;
802
803     err = error;
804     recs.num_diagRecs = 1;
805     recs.diagRecs = recp;
806 #ifdef Z_95
807     recp[0] = &drec;
808     drec.which = Z_DiagRec_defaultFormat;
809     drec.u.defaultFormat = &rec;
810 #else
811     recp[0] = &rec;
812 #endif
813     rec.diagnosticSetId = oid_getoidbyent(&bib1);
814     rec.condition = &err;
815     rec.which = Z_DiagForm_v2AddInfo;
816     rec.addinfo = addinfo ? addinfo : "";
817     return &recs;
818 }
819
820 #define MAX_RECORDS 256
821
822 static Z_Records *pack_records(association *a, char *setname, int start,
823                                 int *num, Z_RecordComposition *comp,
824                                 int *next, int *pres, oid_value format)
825 {
826     int recno, total_length = 0, toget = *num;
827     static Z_Records records;
828     static Z_NamePlusRecordList reclist;
829     static Z_NamePlusRecord *list[MAX_RECORDS];
830     oident recform;
831
832     records.which = Z_Records_DBOSD;
833     records.u.databaseOrSurDiagnostics = &reclist;
834     reclist.num_records = 0;
835     reclist.records = list;
836     *pres = Z_PRES_SUCCESS;
837     *num = 0;
838     *next = 0;
839
840     logf(LOG_DEBUG, "Request to pack %d+%d", start, toget);
841     logf(LOG_DEBUG, "pms=%d, mrs=%d", a->preferredMessageSize,
842         a->maximumRecordSize);
843     for (recno = start; reclist.num_records < toget; recno++)
844     {
845         bend_fetchrequest freq;
846         bend_fetchresult *fres;
847         Z_NamePlusRecord *thisrec;
848         Z_DatabaseRecord *thisext;
849         int this_length;
850
851         /*
852          * we get the number of bytes allocated on the stream before any
853          * allocation done by the backend - this should give us a reasonable
854          * idea of the total size of the data so far.
855          */
856         total_length = odr_total(a->encode);
857         if (reclist.num_records == MAX_RECORDS - 1)
858         {
859             *pres = Z_PRES_PARTIAL_2;
860             break;
861         }
862         freq.setname = setname;
863         freq.number = recno;
864         freq.comp = comp;
865         freq.format = format;
866         freq.stream = a->encode;
867         if (!(fres = bend_fetch(a->backend, &freq, 0)))
868         {
869             *pres = Z_PRES_FAILURE;
870             return diagrec(a->proto, 2, "Backend interface problem");
871         }
872         /* backend should be able to signal whether error is system-wide
873            or only pertaining to current record */
874         if (fres->errcode)
875         {
876             *pres = Z_PRES_FAILURE;
877             return diagrec(a->proto, fres->errcode, fres->errstring);
878         }
879         if (fres->len >= 0)
880             this_length = fres->len;
881         else
882             this_length = odr_total(a->encode) - total_length;
883         logf(LOG_DEBUG, "  fetched record, len=%d, total=%d",
884             this_length, total_length);
885         if (this_length + total_length > a->preferredMessageSize)
886         {
887             /* record is small enough, really */
888             if (this_length <= a->preferredMessageSize)
889             {
890                 logf(LOG_DEBUG, "  Dropped last normal-sized record");
891                 *pres = Z_PRES_PARTIAL_2;
892                 break;
893             }
894             /* record can only be fetched by itself */
895             if (this_length < a->maximumRecordSize)
896             {
897                 logf(LOG_DEBUG, "  Record > prefmsgsz");
898                 if (toget > 1)
899                 {
900                     logf(LOG_DEBUG, "  Dropped it");
901                     reclist.records[reclist.num_records] =
902                          surrogatediagrec(a->proto, fres->basename, 16, 0);
903                     reclist.num_records++;
904                     continue;
905                 }
906             }
907             else /* too big entirely */
908             {
909                 logf(LOG_DEBUG, "Record > maxrcdsz");
910                 reclist.records[reclist.num_records] =
911                     surrogatediagrec(a->proto, fres->basename, 17, 0);
912                 reclist.num_records++;
913                 continue;
914             }
915         }
916         if (!(thisrec = odr_malloc(a->encode, sizeof(*thisrec))))
917             return 0;
918         if (!(thisrec->databaseName = odr_malloc(a->encode,
919             strlen(fres->basename) + 1)))
920             return 0;
921         strcpy(thisrec->databaseName, fres->basename);
922         thisrec->which = Z_NamePlusRecord_databaseRecord;
923         if (!(thisrec->u.databaseRecord = thisext = odr_malloc(a->encode,
924             sizeof(Z_DatabaseRecord))))
925             return 0;
926         recform.proto = a->proto;
927         recform.class = CLASS_RECSYN;
928         recform.value = fres->format;
929         thisext->direct_reference = odr_oiddup(a->encode,
930             oid_getoidbyent(&recform));
931         thisext->indirect_reference = 0;
932         thisext->descriptor = 0;
933         if (fres->len < 0) /* Structured data */
934         {
935             switch (fres->format)
936             {
937                 case VAL_SUTRS: thisext->which = Z_External_sutrs; break;
938                 case VAL_GRS1: thisext->which = Z_External_grs1; break;
939                 case VAL_EXPLAIN: thisext->which = Z_External_explainRecord;
940                     break;
941
942                 default:
943                     logf(LOG_FATAL, "Unknown structured format from backend.");
944                     return 0;
945             }
946
947             /*
948              * We cheat on the pointers here. Obviously, the record field
949              * of the backend-fetch structure should have been a union for
950              * correctness, but we're stuck with this for backwards
951              * compatibility.
952              */
953             thisext->u.grs1 = (Z_GenericRecord*) fres->record;
954         }
955         else if (fres->format == VAL_SUTRS) /* SUTRS is a single-ASN.1-type */
956         {
957             Odr_oct *sutrs = odr_malloc(a->encode, sizeof(*sutrs));
958
959             thisext->which = Z_External_sutrs;
960             thisext->u.sutrs = sutrs;
961             sutrs->buf = odr_malloc(a->encode, fres->len);
962             sutrs->len = sutrs->size = fres->len;
963             memcpy(sutrs->buf, fres->record, fres->len);
964         }
965         else /* octet-aligned record. */
966         {
967             thisext->which = Z_External_octet;
968             if (!(thisext->u.octet_aligned = odr_malloc(a->encode,
969                 sizeof(Odr_oct))))
970                 return 0;
971             if (!(thisext->u.octet_aligned->buf = odr_malloc(a->encode,
972                 fres->len)))
973                 return 0;
974             memcpy(thisext->u.octet_aligned->buf, fres->record, fres->len);
975             thisext->u.octet_aligned->len = thisext->u.octet_aligned->size =
976                 fres->len;
977         }
978         reclist.records[reclist.num_records] = thisrec;
979         reclist.num_records++;
980         *next = fres->last_in_set ? 0 : recno + 1;
981     }
982     *num = reclist.num_records;
983     return &records;
984 }
985
986 static Z_APDU *process_searchRequest(association *assoc, request *reqb,
987     int *fd)
988 {
989     Z_SearchRequest *req = reqb->request->u.searchRequest;
990     bend_searchrequest bsrq;
991     bend_searchresult *bsrt;
992
993     logf(LOG_LOG, "Got SearchRequest.");
994
995     bsrq.setname = req->resultSetName;
996     bsrq.replace_set = *req->replaceIndicator;
997     bsrq.num_bases = req->num_databaseNames;
998     bsrq.basenames = req->databaseNames;
999     bsrq.query = req->query;
1000
1001     if (!(bsrt = bend_search(assoc->backend, &bsrq, fd)))
1002         return 0;
1003     return response_searchRequest(assoc, reqb, bsrt, fd);
1004 }
1005
1006 bend_searchresult *bend_searchresponse(void *handle) {return 0;}
1007
1008 /*
1009  * Prepare a searchresponse based on the backend results. We probably want
1010  * to look at making the fetching of records nonblocking as well, but
1011  * so far, we'll keep things simple.
1012  * If bsrt is null, that means we're called in response to a communications
1013  * event, and we'll have to get the response for ourselves.
1014  */
1015 static Z_APDU *response_searchRequest(association *assoc, request *reqb,
1016     bend_searchresult *bsrt, int *fd)
1017 {
1018     Z_SearchRequest *req = reqb->request->u.searchRequest;
1019     static Z_APDU apdu;
1020     static Z_SearchResponse resp;
1021     static int nulint = 0;
1022     static bool_t sr = 1;
1023     static int next = 0;
1024     static int none = Z_RES_NONE;
1025
1026     apdu.which = Z_APDU_searchResponse;
1027     apdu.u.searchResponse = &resp;
1028     resp.referenceId = req->referenceId;
1029 #ifdef Z_95
1030     resp.additionalSearchInfo = 0;
1031     resp.otherInfo = 0;
1032 #endif
1033     *fd = -1;
1034     if (!bsrt && !(bsrt = bend_searchresponse(assoc->backend)))
1035     {
1036         logf(LOG_FATAL, "Bad result from backend");
1037         return 0;
1038     }
1039     else if (bsrt->errcode)
1040     {
1041         resp.records = diagrec(assoc->proto, bsrt->errcode,
1042             bsrt->errstring);
1043         resp.resultCount = &nulint;
1044         resp.numberOfRecordsReturned = &nulint;
1045         resp.nextResultSetPosition = &nulint;
1046         resp.searchStatus = &nulint;
1047         resp.resultSetStatus = &none;
1048         resp.presentStatus = 0;
1049     }
1050     else
1051     {
1052         static int toget;
1053         Z_RecordComposition comp, *compp = 0;
1054         static int presst = 0;
1055
1056         resp.records = 0;
1057         resp.resultCount = &bsrt->hits;
1058
1059         comp.which = Z_RecordComp_simple;
1060         /* how many records does the user agent want, then? */
1061         if (bsrt->hits <= *req->smallSetUpperBound)
1062         {
1063             toget = bsrt->hits;
1064             if ((comp.u.simple = req->smallSetElementSetNames))
1065                 compp = &comp;
1066         }
1067         else if (bsrt->hits < *req->largeSetLowerBound)
1068         {
1069             toget = *req->mediumSetPresentNumber;
1070             if (toget > bsrt->hits)
1071                 toget = bsrt->hits;
1072             if ((comp.u.simple = req->mediumSetElementSetNames))
1073                 compp = &comp;
1074         }
1075         else
1076             toget = 0;
1077
1078         if (toget && !resp.records)
1079         {
1080             oident *prefformat;
1081             oid_value form;
1082
1083             if (!(prefformat = oid_getentbyoid(req->preferredRecordSyntax)) ||
1084                 prefformat->class != CLASS_RECSYN)
1085                 form = VAL_NONE;
1086             else
1087                 form = prefformat->value;
1088             resp.records = pack_records(assoc, req->resultSetName, 1,
1089                 &toget, compp, &next, &presst, form);
1090             if (!resp.records)
1091                 return 0;
1092             resp.numberOfRecordsReturned = &toget;
1093             resp.nextResultSetPosition = &next;
1094             resp.searchStatus = &sr;
1095             resp.resultSetStatus = 0;
1096             resp.presentStatus = &presst;
1097         }
1098         else
1099         {
1100             if (*resp.resultCount)
1101                 next = 1;
1102             resp.numberOfRecordsReturned = &nulint;
1103             resp.nextResultSetPosition = &next;
1104             resp.searchStatus = &sr;
1105             resp.resultSetStatus = 0;
1106             resp.presentStatus = 0;
1107         }
1108     }
1109     return &apdu;
1110 }
1111
1112 /*
1113  * Maybe we got a little over-friendly when we designed bend_fetch to
1114  * get only one record at a time. Some backends can optimise multiple-record
1115  * fetches, and at any rate, there is some overhead involved in
1116  * all that selecting and hopping around. Problem is, of course, that the
1117  * frontend can't know ahead of time how many records it'll need to
1118  * fill the negotiated PDU size. Annoying. Segmentation or not, Z/SR
1119  * is downright lousy as a bulk data transfer protocol.
1120  *
1121  * To start with, we'll do the fetching of records from the backend
1122  * in one operation: To save some trips in and out of the event-handler,
1123  * and to simplify the interface to pack_records. At any rate, asynch
1124  * operation is more fun in operations that have an unpredictable execution
1125  * speed - which is normally more true for search than for present.
1126  */
1127 static Z_APDU *process_presentRequest(association *assoc, request *reqb,
1128     int *fd)
1129 {
1130     Z_PresentRequest *req = reqb->request->u.presentRequest;
1131     static Z_APDU apdu;
1132     static Z_PresentResponse resp;
1133     static int presst, next, num;
1134     oident *prefformat;
1135     oid_value form;
1136
1137
1138     logf(LOG_LOG, "Got PresentRequest.");
1139     apdu.which = Z_APDU_presentResponse;
1140     apdu.u.presentResponse = &resp;
1141     resp.referenceId = req->referenceId;
1142 #ifdef Z_95
1143     resp.otherInfo = 0;
1144 #endif
1145
1146     if (!(prefformat = oid_getentbyoid(req->preferredRecordSyntax)) ||
1147         prefformat->class != CLASS_RECSYN)
1148         form = VAL_NONE;
1149     else
1150         form = prefformat->value;
1151     num = *req->numberOfRecordsRequested;
1152     resp.records = pack_records(assoc, req->resultSetId,
1153         *req->resultSetStartPoint, &num, req->recordComposition, &next,
1154         &presst, form);
1155     if (!resp.records)
1156         return 0;
1157     resp.numberOfRecordsReturned = &num;
1158     resp.presentStatus = &presst;
1159     resp.nextResultSetPosition = &next;
1160
1161     return &apdu;
1162 }
1163
1164 /*
1165  * Scan was implemented rather in a hurry, and with support for only the basic
1166  * elements of the service in the backend API. Suggestions are welcome.
1167  */
1168 static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd)
1169 {
1170     Z_ScanRequest *req = reqb->request->u.scanRequest;
1171     static Z_APDU apdu;
1172     static Z_ScanResponse res;
1173     static int scanStatus = Z_Scan_failure;
1174     static int numberOfEntriesReturned = 0;
1175     oident *attent;
1176     static Z_ListEntries ents;
1177 #define SCAN_MAX_ENTRIES 200
1178     static Z_Entry *tab[SCAN_MAX_ENTRIES];
1179     bend_scanrequest srq;
1180     bend_scanresult *srs;
1181
1182     logf(LOG_LOG, "Got scanrequest");
1183     apdu.which = Z_APDU_scanResponse;
1184     apdu.u.scanResponse = &res;
1185     res.referenceId = req->referenceId;
1186     res.stepSize = 0;
1187     res.scanStatus = &scanStatus;
1188     res.numberOfEntriesReturned = &numberOfEntriesReturned;
1189     res.positionOfTerm = 0;
1190     res.entries = &ents;
1191     ents.which = Z_ListEntries_nonSurrogateDiagnostics;
1192     res.attributeSet = 0;
1193 #ifdef Z_95
1194     res.otherInfo = 0;
1195 #endif
1196
1197     if (req->attributeSet && (!(attent = oid_getentbyoid(req->attributeSet)) ||
1198         attent->class != CLASS_ATTSET || attent->value != VAL_BIB1))
1199         ents.u.nonSurrogateDiagnostics = diagrecs(assoc->proto, 121, 0);
1200     else if (req->stepSize && *req->stepSize > 0)
1201         ents.u.nonSurrogateDiagnostics = diagrecs(assoc->proto, 205, 0);
1202     else
1203     {
1204         if (req->termListAndStartPoint->term->which == Z_Term_general)
1205             logf(LOG_DEBUG, " term: '%.*s'",
1206                 req->termListAndStartPoint->term->u.general->len,
1207                 req->termListAndStartPoint->term->u.general->buf);
1208         srq.num_bases = req->num_databaseNames;
1209         srq.basenames = req->databaseNames;
1210         srq.num_entries = *req->numberOfTermsRequested;
1211         srq.term = req->termListAndStartPoint;
1212         srq.term_position = req->preferredPositionInResponse ?
1213             *req->preferredPositionInResponse : 1;
1214         if (!(srs = bend_scan(assoc->backend, &srq, 0)))
1215             ents.u.nonSurrogateDiagnostics = diagrecs(assoc->proto, 2, 0);
1216         else if (srs->errcode)
1217             ents.u.nonSurrogateDiagnostics = diagrecs(assoc->proto,
1218                 srs->errcode, srs->errstring);
1219         else
1220         {
1221             int i;
1222             static Z_Entries list;
1223
1224             if (srs->status == BEND_SCAN_PARTIAL)
1225                 scanStatus = Z_Scan_partial_5;
1226             else
1227                 scanStatus = Z_Scan_success;
1228             ents.which = Z_ListEntries_entries;
1229             ents.u.entries = &list;
1230             list.entries = tab;
1231             for (i = 0; i < srs->num_entries; i++)
1232             {
1233                 Z_Entry *e;
1234                 Z_TermInfo *t;
1235                 Odr_oct *o;
1236
1237                 if (i >= SCAN_MAX_ENTRIES)
1238                 {
1239                     scanStatus = Z_Scan_partial_4;
1240                     break;
1241                 }
1242                 list.entries[i] = e = odr_malloc(assoc->encode, sizeof(*e));
1243                 e->which = Z_Entry_termInfo;
1244                 e->u.termInfo = t = odr_malloc(assoc->encode, sizeof(*t));
1245                 t->suggestedAttributes = 0;
1246                 t->alternativeTerm = 0;
1247                 t->byAttributes = 0;
1248                 t->globalOccurrences = &srs->entries[i].occurrences;
1249                 t->term = odr_malloc(assoc->encode, sizeof(*t->term));
1250                 t->term->which = Z_Term_general;
1251                 t->term->u.general = o = odr_malloc(assoc->encode,
1252                     sizeof(Odr_oct));
1253                 o->buf = odr_malloc(assoc->encode, o->len = o->size =
1254                     strlen(srs->entries[i].term));
1255                 memcpy(o->buf, srs->entries[i].term, o->len);
1256                 logf(LOG_DEBUG, "  term #%d: '%s' (%d)", i,
1257                     srs->entries[i].term, srs->entries[i].occurrences);
1258             }
1259             list.num_entries = i;
1260             res.numberOfEntriesReturned = &list.num_entries;
1261             res.positionOfTerm = &srs->term_position;
1262         }
1263     }
1264
1265     return &apdu;
1266 }
1267
1268 static void process_close(association *assoc, request *reqb)
1269 {
1270     Z_Close *req = reqb->request->u.close;
1271     static char *reasons[] =
1272     {
1273         "finished",
1274         "shutdown",
1275         "systemProblem",
1276         "costLimit",
1277         "resources",
1278         "securityViolation",
1279         "protocolError",
1280         "lackOfActivity",
1281         "peerAbort",
1282         "unspecified"
1283     };
1284
1285     logf(LOG_LOG, "Got close, reason %s, message %s",
1286         reasons[*req->closeReason], req->diagnosticInformation ?
1287         req->diagnosticInformation : "NULL");
1288     if (assoc->version < 3) /* to make do_force respond with close */
1289         assoc->version = 3;
1290     do_close(assoc, Z_Close_finished, "Association terminated by client");
1291 }