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