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