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