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