2 * Copyright (c) 1995-1998, Index Data
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.80 1998-09-02 12:41:53 adam
8 * Added decode stream in bend search structures.
10 * Revision 1.79 1998/08/19 16:10:08 adam
11 * Changed som member names of DeleteResultSetRequest/Response.
13 * Revision 1.78 1998/08/03 10:23:55 adam
14 * Fixed bug regarding Options for Sort.
16 * Revision 1.77 1998/07/20 12:38:42 adam
17 * Implemented delete result set service to server API.
19 * Revision 1.76 1998/05/27 16:57:07 adam
20 * Support for surrogate diagnostic records added for bend_fetch.
22 * Revision 1.75 1998/05/18 10:13:07 adam
23 * Fixed call to es_request handler - extra argument was passed.
25 * Revision 1.74 1998/03/31 15:13:20 adam
26 * Development towards compiled ASN.1.
28 * Revision 1.73 1998/03/31 11:07:45 adam
29 * Furhter work on UNIverse resource report.
30 * Added Extended Services handling in frontend server.
32 * Revision 1.72 1998/02/11 11:53:35 adam
33 * Changed code so that it compiles as C++.
35 * Revision 1.71 1998/02/10 11:03:57 adam
36 * Added support for extended handlers in backend server interface.
38 * Revision 1.70 1998/01/29 13:15:35 adam
39 * Implemented sort for the backend interface.
41 * Revision 1.69 1997/09/30 11:48:12 adam
42 * Fixed bug introduced by previous commit.
44 * Revision 1.68 1997/09/29 13:18:59 adam
45 * Added function, oid_ent_to_oid, to replace the function
46 * oid_getoidbyent, which is not thread safe.
48 * Revision 1.67 1997/09/17 12:10:40 adam
51 * Revision 1.66 1997/09/05 15:26:44 adam
52 * Added ODR encode in search and scen bend request structures.
53 * Fixed a few enums that caused trouble with C++.
55 * Revision 1.65 1997/09/01 08:53:01 adam
56 * New windows NT/95 port using MSV5.0. The test server 'ztest' was
57 * moved a separate directory. MSV5.0 project server.dsp created.
58 * As an option, the server can now operate as an NT service.
60 * Revision 1.64 1997/04/30 08:52:11 quinn
63 * Revision 1.63 1996/10/11 11:57:26 quinn
66 * Revision 1.62 1996/07/06 19:58:35 quinn
67 * System headerfiles gathered in yconfig
69 * Revision 1.61 1996/06/10 08:56:16 quinn
72 * Revision 1.60 1996/05/30 11:03:10 quinn
73 * Fixed NextresultSetPosition bug fixed.
75 * Revision 1.59 1996/05/14 09:26:46 quinn
76 * Added attribute set to scan backend
78 * Revision 1.58 1996/02/20 12:53:04 quinn
81 * Revision 1.57 1996/01/02 08:57:47 quinn
82 * Changed enums in the ASN.1 .h files to #defines. Changed oident.class to oclass
84 * Revision 1.56 1995/12/14 11:09:57 quinn
87 * Revision 1.55 1995/11/08 17:41:37 quinn
90 * Revision 1.54 1995/11/08 15:11:29 quinn
91 * Log of close transmit.
93 * Revision 1.53 1995/11/01 13:54:58 quinn
96 * Revision 1.52 1995/11/01 12:19:13 quinn
97 * Second attempt to fix same bug.
99 * Revision 1.50 1995/10/25 16:58:32 quinn
102 * Revision 1.49 1995/10/16 13:51:53 quinn
103 * Changes to provide Especs to the backend.
105 * Revision 1.48 1995/10/06 08:51:20 quinn
106 * Added Write-buffer.
108 * Revision 1.47 1995/08/29 14:24:16 quinn
109 * Added second half of close-handshake
111 * Revision 1.46 1995/08/29 11:17:58 quinn
112 * Added code to receive close
114 * Revision 1.45 1995/08/21 09:11:00 quinn
115 * Smallish fixes to suppport new formats.
117 * Revision 1.44 1995/08/17 12:45:25 quinn
118 * Fixed minor problems with GRS-1. Added support in c&s.
120 * Revision 1.43 1995/08/15 12:00:31 quinn
123 * Revision 1.42 1995/08/15 11:16:50 quinn
125 * Revision 1.41 1995/08/02 10:23:06 quinn
128 * Revision 1.40 1995/07/31 14:34:26 quinn
129 * Fixed bug in process_searchResponse (numberOfRecordsReturned).
131 * Revision 1.39 1995/06/27 13:21:00 quinn
134 * Revision 1.38 1995/06/19 12:39:11 quinn
135 * Fixed bug in timeout code. Added BER dumper.
137 * Revision 1.37 1995/06/16 13:16:14 quinn
138 * Fixed Defaultdiagformat.
140 * Revision 1.36 1995/06/16 10:31:36 quinn
141 * Added session timeout.
143 * Revision 1.35 1995/06/15 07:45:14 quinn
146 * Revision 1.34 1995/06/14 15:26:46 quinn
147 * *** empty log message ***
149 * Revision 1.33 1995/06/06 14:57:05 quinn
150 * Better diagnostics.
152 * Revision 1.32 1995/06/06 08:41:44 quinn
153 * Better diagnostics.
155 * Revision 1.31 1995/06/06 08:15:37 quinn
158 * Revision 1.30 1995/06/05 10:53:32 quinn
159 * Added a better SCAN.
161 * Revision 1.29 1995/06/01 11:25:03 quinn
164 * Revision 1.28 1995/06/01 11:21:01 quinn
165 * Attempting to fix a bug in pack-records. replaced break with continue
166 * for large records, according to standard.
168 * Revision 1.27 1995/05/29 08:12:06 quinn
171 * Revision 1.26 1995/05/18 13:02:12 quinn
174 * Revision 1.25 1995/05/17 08:42:26 quinn
175 * Transfer auth info to backend. Allow backend to reject init gracefully.
177 * Revision 1.24 1995/05/16 08:51:04 quinn
178 * License, documentation, and memory fixes
180 * Revision 1.23 1995/05/15 13:25:10 quinn
183 * Revision 1.22 1995/05/15 11:56:39 quinn
184 * Asynchronous facilities. Restructuring of seshigh code.
186 * Revision 1.21 1995/05/02 08:53:19 quinn
187 * Trying in vain to fix comm with ISODE
189 * Revision 1.20 1995/04/20 15:13:00 quinn
192 * Revision 1.19 1995/04/18 08:15:34 quinn
193 * Added dynamic memory allocation on encoding (whew). Code is now somewhat
194 * neater. We'll make the same change for decoding one day.
196 * Revision 1.18 1995/04/17 11:28:25 quinn
199 * Revision 1.17 1995/04/10 10:23:36 quinn
200 * Some work to add scan and other things.
202 * Revision 1.16 1995/03/31 09:18:55 quinn
205 * Revision 1.15 1995/03/30 14:03:23 quinn
206 * Added RFC1006 as separate library
208 * Revision 1.14 1995/03/30 12:18:17 quinn
211 * Revision 1.13 1995/03/30 09:09:24 quinn
212 * Added state-handle and some support for asynchronous activities.
214 * Revision 1.12 1995/03/29 15:40:16 quinn
215 * Ongoing work. Statserv is now dynamic by default
217 * Revision 1.11 1995/03/28 09:16:21 quinn
218 * Added record packing to the search request
220 * Revision 1.10 1995/03/27 08:34:24 quinn
221 * Added dynamic server functionality.
222 * Released bindings to session.c (is now redundant)
224 * Revision 1.9 1995/03/22 15:01:26 quinn
225 * Adjusting record packing.
227 * Revision 1.8 1995/03/22 10:13:21 quinn
228 * Working on record packer
230 * Revision 1.7 1995/03/21 15:53:31 quinn
233 * Revision 1.6 1995/03/21 12:30:09 quinn
234 * Beginning to add support for record packing.
236 * Revision 1.5 1995/03/17 10:44:13 quinn
237 * Added catch of null-string in makediagrec
239 * Revision 1.4 1995/03/17 10:18:08 quinn
240 * Added memory management.
242 * Revision 1.3 1995/03/16 17:42:39 quinn
245 * Revision 1.2 1995/03/16 13:29:01 quinn
246 * Partitioned server.
248 * Revision 1.1 1995/03/15 16:02:10 quinn
249 * Modded session.c to seshigh.c
254 * Frontend server logic.
256 * This code receives incoming APDUs, and handles client requests by means
257 * of the backend API.
259 * Some of the code is getting quite involved, compared to simpler servers -
260 * primarily because it is asynchronous both in the communication with
261 * the user and the backend. We think the complexity will pay off in
262 * the form of greater flexibility when more asynchronous facilities
265 * Memory management has become somewhat involved. In the simple case, where
266 * only one PDU is pending at a time, it will simply reuse the same memory,
267 * once it has found its working size. When we enable multiple concurrent
268 * operations, perhaps even with multiple parallel calls to the backend, it
269 * will maintain a pool of buffers for encoding and decoding, trying to
270 * minimize memory allocation/deallocation during normal operation.
272 * TODOs include (and will be done in order of public interest):
274 * Support for EXPLAIN - provide simple meta-database system.
275 * Support for access control.
276 * Support for resource control.
277 * Support for extended services - primarily Item Order.
278 * Rest of Z39.50-1994
293 #include <comstack.h>
299 #include <statserv.h>
303 static int process_request(association *assoc, request *req);
304 void backend_response(IOCHAN i, int event);
305 static int process_response(association *assoc, request *req, Z_APDU *res);
306 static Z_APDU *process_initRequest(association *assoc, request *reqb);
307 static Z_APDU *process_searchRequest(association *assoc, request *reqb,
309 static Z_APDU *response_searchRequest(association *assoc, request *reqb,
310 bend_search_rr *bsrr, int *fd);
311 static Z_APDU *process_presentRequest(association *assoc, request *reqb,
313 static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd);
314 static Z_APDU *process_sortRequest(association *assoc, request *reqb, int *fd);
315 static void process_close(association *assoc, request *reqb);
316 void save_referenceId (request *reqb, Z_ReferenceId *refid);
317 static Z_APDU *process_deleteRequest(association *assoc, request *reqb,
320 static FILE *apduf = 0; /* for use in static mode */
321 static statserv_options_block *control_block = 0;
323 /* Chas: Added in from DALI */
324 static Z_APDU *process_ESRequest(association *assoc, request *reqb, int *fd);
325 /* Chas: End of addition from DALI */
328 * Create and initialize a new association-handle.
329 * channel : iochannel for the current line.
330 * link : communications channel.
331 * Returns: 0 or a new association handle.
333 association *create_association(IOCHAN channel, COMSTACK link)
338 control_block = statserv_getcontrol();
339 if (!(anew = (association *)xmalloc(sizeof(*anew))))
341 anew->client_chan = channel;
342 anew->client_link = link;
343 if (!(anew->decode = odr_createmem(ODR_DECODE)) ||
344 !(anew->encode = odr_createmem(ODR_ENCODE)))
346 if (*control_block->apdufile)
351 strcpy(filename, control_block->apdufile);
352 if (!(anew->print = odr_createmem(ODR_PRINT)))
354 if (*control_block->apdufile != '-')
356 strcpy(filename, control_block->apdufile);
357 if (!control_block->dynamic)
361 if (!(apduf = fopen(filename, "w")))
363 logf(LOG_WARN|LOG_ERRNO, "%s", filename);
366 setvbuf(apduf, 0, _IONBF, 0);
372 sprintf(filename + strlen(filename), ".%d", getpid());
373 if (!(f = fopen(filename, "w")))
375 logf(LOG_WARN|LOG_ERRNO, "%s", filename);
378 setvbuf(f, 0, _IONBF, 0);
380 odr_setprint(anew->print, f);
385 anew->input_buffer = 0;
386 anew->input_buffer_len = 0;
388 anew->state = ASSOC_NEW;
389 request_initq(&anew->incoming);
390 request_initq(&anew->outgoing);
391 anew->proto = cs_getproto(link);
396 * Free association and release resources.
398 void destroy_association(association *h)
400 odr_destroy(h->decode);
401 odr_destroy(h->encode);
403 odr_destroy(h->print);
405 xfree(h->input_buffer);
407 bend_close(h->backend);
408 while (request_deq(&h->incoming));
409 while (request_deq(&h->outgoing));
410 request_delq(&h->incoming);
411 request_delq(&h->outgoing);
415 static void do_close_req(association *a, int reason, char *message,
419 Z_Close *cls = zget_Close(a->encode);
421 /* Purge request queue */
422 while (request_deq(&a->incoming));
423 while (request_deq(&a->outgoing));
426 logf(LOG_LOG, "Sending Close PDU, reason=%d, message=%s",
427 reason, message ? message : "none");
428 apdu.which = Z_APDU_close;
430 *cls->closeReason = reason;
431 cls->diagnosticInformation = message;
432 process_response(a, req, &apdu);
433 iochan_settimeout(a->client_chan, 60);
437 logf(LOG_DEBUG, "v2 client. No Close PDU");
438 iochan_setevent(a->client_chan, EVENT_TIMEOUT); /* force imm close */
440 a->state = ASSOC_DEAD;
443 static void do_close(association *a, int reason, char *message)
445 do_close_req (a, reason, message, request_get(&a->outgoing));
449 * This is where PDUs from the client are read and the further
450 * processing is initiated. Flow of control moves down through the
451 * various process_* functions below, until the encoded result comes back up
452 * to the output handler in here.
454 * h : the I/O channel that has an outstanding event.
455 * event : the current outstanding event.
457 void ir_session(IOCHAN h, int event)
460 association *assoc = (association *)iochan_getdata(h);
461 COMSTACK conn = assoc->client_link;
464 assert(h && conn && assoc);
465 if (event == EVENT_TIMEOUT)
467 if (assoc->state != ASSOC_UP)
469 logf(LOG_LOG, "Final timeout - closing connection.");
471 destroy_association(assoc);
476 logf(LOG_LOG, "Session idle too long. Sending close.");
477 do_close(assoc, Z_Close_lackOfActivity, 0);
481 if (event & EVENT_INPUT || event & EVENT_WORK) /* input */
483 if (event & EVENT_INPUT)
485 logf(LOG_DEBUG, "ir_session (input)");
486 assert(assoc && conn);
487 /* We aren't speaking to this fellow */
488 if (assoc->state == ASSOC_DEAD)
490 logf(LOG_LOG, "Closed connection after reject");
492 destroy_association(assoc);
496 if ((res = cs_get(conn, &assoc->input_buffer,
497 &assoc->input_buffer_len)) <= 0)
499 logf(LOG_LOG, "Connection closed by client");
501 destroy_association(assoc);
505 else if (res == 1) /* incomplete read - wait for more */
507 if (cs_more(conn)) /* more stuff - call us again later, please */
508 iochan_setevent(h, EVENT_INPUT);
510 /* we got a complete PDU. Let's decode it */
511 logf(LOG_DEBUG, "Got PDU, %d bytes", res);
512 req = request_get(&assoc->incoming); /* get a new request structure */
513 odr_reset(assoc->decode);
514 odr_setbuf(assoc->decode, assoc->input_buffer, res, 0);
515 if (!z_APDU(assoc->decode, &req->request, 0))
517 logf(LOG_LOG, "ODR error on incoming PDU: %s [near byte %d] ",
518 odr_errmsg(odr_geterror(assoc->decode)),
519 odr_offset(assoc->decode));
520 logf(LOG_LOG, "PDU dump:");
521 odr_dumpBER(log_file(), assoc->input_buffer, res);
522 do_close(assoc, Z_Close_protocolError, "Malformed package");
525 req->request_mem = odr_extract_mem(assoc->decode);
526 if (assoc->print && !z_APDU(assoc->print, &req->request, 0))
528 logf(LOG_WARN, "ODR print error: %s",
529 odr_errmsg(odr_geterror(assoc->print)));
530 odr_reset(assoc->print);
532 request_enq(&assoc->incoming, req);
535 /* can we do something yet? */
536 req = request_head(&assoc->incoming);
537 if (req->state == REQUEST_IDLE)
539 request_deq(&assoc->incoming);
540 if (process_request(assoc, req) < 0)
541 do_close_req(assoc, Z_Close_systemProblem, "Unknown error",
545 if (event & EVENT_OUTPUT)
547 request *req = request_head(&assoc->outgoing);
549 logf(LOG_DEBUG, "ir_session (output)");
550 req->state = REQUEST_PENDING;
551 switch (res = cs_put(conn, req->response, req->len_response))
554 logf(LOG_LOG, "Connection closed by client");
556 destroy_association(assoc);
559 case 0: /* all sent - release the request structure */
560 logf(LOG_DEBUG, "Wrote PDU, %d bytes", req->len_response);
561 nmem_destroy(req->request_mem);
562 request_deq(&assoc->outgoing);
563 request_release(req);
564 if (!request_head(&assoc->outgoing))
565 iochan_clearflag(h, EVENT_OUTPUT);
567 /* value of 1 -- partial send -- is simply ignored */
570 if (event & EVENT_EXCEPT)
572 logf(LOG_DEBUG, "ir_session (exception)");
574 destroy_association(assoc);
580 * Initiate request processing.
582 static int process_request(association *assoc, request *req)
588 assert(req && req->state == REQUEST_IDLE);
589 switch (req->request->which)
591 case Z_APDU_initRequest:
592 res = process_initRequest(assoc, req); break;
593 case Z_APDU_searchRequest:
594 res = process_searchRequest(assoc, req, &fd); break;
595 case Z_APDU_presentRequest:
596 res = process_presentRequest(assoc, req, &fd); break;
597 case Z_APDU_scanRequest:
598 res = process_scanRequest(assoc, req, &fd); break;
599 case Z_APDU_extendedServicesRequest:
600 if (assoc->bend_esrequest)
601 res = process_ESRequest(assoc, req, &fd);
604 logf(LOG_WARN, "Cannot handle EXTENDED SERVICES APDU");
608 case Z_APDU_sortRequest:
609 if (assoc->bend_sort)
610 res = process_sortRequest(assoc, req, &fd);
613 logf(LOG_WARN, "Cannot handle SORT APDU");
618 process_close(assoc, req); return 0;
619 case Z_APDU_deleteResultSetRequest:
620 if (assoc->bend_delete)
621 res = process_deleteRequest(assoc, req, &fd);
624 logf (LOG_WARN, "Cannot handle Delete APDU");
629 logf(LOG_WARN, "Bad APDU received");
634 logf(LOG_DEBUG, " result immediately available");
635 retval = process_response(assoc, req, res);
639 logf(LOG_DEBUG, " result unavailble");
642 else /* no result yet - one will be provided later */
646 /* Set up an I/O handler for the fd supplied by the backend */
648 logf(LOG_DEBUG, " establishing handler for result");
649 req->state = REQUEST_PENDING;
650 if (!(chan = iochan_create(fd, backend_response, EVENT_INPUT)))
652 iochan_setdata(chan, assoc);
659 * Handle message from the backend.
661 void backend_response(IOCHAN i, int event)
663 association *assoc = (association *)iochan_getdata(i);
664 request *req = request_head(&assoc->incoming);
668 logf(LOG_DEBUG, "backend_response");
669 assert(assoc && req && req->state != REQUEST_IDLE);
670 /* determine what it is we're waiting for */
671 switch (req->request->which)
673 case Z_APDU_searchRequest:
674 res = response_searchRequest(assoc, req, 0, &fd); break;
676 case Z_APDU_presentRequest:
677 res = response_presentRequest(assoc, req, 0, &fd); break;
678 case Z_APDU_scanRequest:
679 res = response_scanRequest(assoc, req, 0, &fd); break;
682 logf(LOG_WARN, "Serious programmer's lapse or bug");
685 if ((res && process_response(assoc, req, res) < 0) || fd < 0)
687 logf(LOG_LOG, "Fatal error when talking to backend");
688 do_close(assoc, Z_Close_systemProblem, 0);
692 else if (!res) /* no result yet - try again later */
694 logf(LOG_DEBUG, " no result yet");
695 iochan_setfd(i, fd); /* in case fd has changed */
700 * Encode response, and transfer the request structure to the outgoing queue.
702 static int process_response(association *assoc, request *req, Z_APDU *res)
704 odr_setbuf(assoc->encode, req->response, req->size_response, 1);
705 if (!z_APDU(assoc->encode, &res, 0))
707 logf(LOG_WARN, "ODR error when encoding response: %s",
708 odr_errmsg(odr_geterror(assoc->decode)));
709 odr_reset(assoc->encode);
712 req->response = odr_getbuf(assoc->encode, &req->len_response,
713 &req->size_response);
714 odr_setbuf(assoc->encode, 0, 0, 0); /* don'txfree if we abort later */
715 if (assoc->print && !z_APDU(assoc->print, &res, 0))
717 logf(LOG_WARN, "ODR print error: %s",
718 odr_errmsg(odr_geterror(assoc->print)));
719 odr_reset(assoc->print);
721 odr_reset(assoc->encode);
722 req->state = REQUEST_IDLE;
723 request_enq(&assoc->outgoing, req);
724 /* turn the work over to the ir_session handler */
725 iochan_setflag(assoc->client_chan, EVENT_OUTPUT);
726 /* Is there more work to be done? give that to the input handler too */
728 if (request_head(&assoc->incoming))
730 logf (LOG_DEBUG, "more work to be done");
731 iochan_setevent(assoc->client_chan, EVENT_WORK);
738 * Handle init request.
739 * At the moment, we don't check the options
740 * anywhere else in the code - we just try not to do anything that would
741 * break a naive client. We'll toss 'em into the association block when
742 * we need them there.
744 static Z_APDU *process_initRequest(association *assoc, request *reqb)
746 Z_InitRequest *req = reqb->request->u.initRequest;
747 Z_APDU *apdu = zget_APDU(assoc->encode, Z_APDU_initResponse);
748 Z_InitResponse *resp = apdu->u.initResponse;
749 bend_initrequest binitreq;
750 bend_initresult *binitres;
753 logf(LOG_LOG, "Got initRequest");
754 if (req->implementationId)
755 logf(LOG_LOG, "Id: %s", req->implementationId);
756 if (req->implementationName)
757 logf(LOG_LOG, "Name: %s", req->implementationName);
758 if (req->implementationVersion)
759 logf(LOG_LOG, "Version: %s", req->implementationVersion);
761 binitreq.stream = assoc->encode;
762 binitreq.configname = "default-config";
763 binitreq.auth = req->idAuthentication;
764 binitreq.bend_sort = NULL;
765 binitreq.bend_search = NULL;
766 binitreq.bend_present = NULL;
767 binitreq.bend_esrequest = NULL;
768 binitreq.bend_delete = NULL;
769 if (!(binitres = bend_init(&binitreq)))
771 logf(LOG_WARN, "Bad response from backend.");
775 assoc->backend = binitres->handle;
776 if ((assoc->bend_sort = (int (*)())binitreq.bend_sort))
777 logf (LOG_DEBUG, "Sort handler installed");
778 if ((assoc->bend_search = (int (*)())binitreq.bend_search))
779 logf (LOG_DEBUG, "Search handler installed");
780 if ((assoc->bend_present = (int (*)())binitreq.bend_present))
781 logf (LOG_DEBUG, "Present handler installed");
782 if ((assoc->bend_esrequest = (int (*)())binitreq.bend_esrequest))
783 logf (LOG_DEBUG, "ESRequest handler installed");
784 if ((assoc->bend_delete = (int (*)())binitreq.bend_delete))
785 logf (LOG_DEBUG, "Delete handler installed");
787 resp->referenceId = req->referenceId;
789 /* let's tell the client what we can do */
790 if (ODR_MASK_GET(req->options, Z_Options_search))
792 ODR_MASK_SET(resp->options, Z_Options_search);
793 strcat(options, "srch");
795 if (ODR_MASK_GET(req->options, Z_Options_present))
797 ODR_MASK_SET(resp->options, Z_Options_present);
798 strcat(options, " prst");
800 if (ODR_MASK_GET(req->options, Z_Options_delSet) && binitreq.bend_delete)
802 ODR_MASK_SET(resp->options, Z_Options_delSet);
803 strcat(options, " del");
805 if (ODR_MASK_GET(req->options, Z_Options_extendedServices) &&
806 binitreq.bend_esrequest)
808 ODR_MASK_SET(resp->options, Z_Options_extendedServices);
809 strcat (options, " extendedServices");
811 if (ODR_MASK_GET(req->options, Z_Options_namedResultSets))
813 ODR_MASK_SET(resp->options, Z_Options_namedResultSets);
814 strcat(options, " namedresults");
816 if (ODR_MASK_GET(req->options, Z_Options_scan))
818 ODR_MASK_SET(resp->options, Z_Options_scan);
819 strcat(options, " scan");
821 if (ODR_MASK_GET(req->options, Z_Options_concurrentOperations))
823 ODR_MASK_SET(resp->options, Z_Options_concurrentOperations);
824 strcat(options, " concurop");
826 if (ODR_MASK_GET(req->options, Z_Options_sort) && binitreq.bend_sort)
828 ODR_MASK_SET(resp->options, Z_Options_sort);
829 strcat(options, " sort");
831 if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_1))
833 ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_1);
834 assoc->version = 2; /* 1 & 2 are equivalent */
836 if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_2))
838 ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_2);
841 if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_3))
843 ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_3);
846 logf(LOG_LOG, "Negotiated to v%d: %s", assoc->version, options);
847 assoc->maximumRecordSize = *req->maximumRecordSize;
848 if (assoc->maximumRecordSize > control_block->maxrecordsize)
849 assoc->maximumRecordSize = control_block->maxrecordsize;
850 assoc->preferredMessageSize = *req->preferredMessageSize;
851 if (assoc->preferredMessageSize > assoc->maximumRecordSize)
852 assoc->preferredMessageSize = assoc->maximumRecordSize;
853 resp->preferredMessageSize = &assoc->preferredMessageSize;
854 resp->maximumRecordSize = &assoc->maximumRecordSize;
855 resp->implementationName = "Index Data/YAZ Generic Frontend Server";
856 if (binitres->errcode)
858 logf(LOG_LOG, "Connection rejected by backend.");
860 assoc->state = ASSOC_DEAD;
863 assoc->state = ASSOC_UP;
868 * These functions should be merged.
871 static void set_addinfo (Z_DefaultDiagFormat *dr, char *addinfo)
874 dr->which = Z_DefaultDiagFormat_v2Addinfo;
875 dr->u.v2Addinfo = addinfo ? addinfo : "";
877 dr->which = Z_DiagForm_v2AddInfo;
878 dr->addinfo = addinfo ? addinfo : "";
883 * nonsurrogate diagnostic record.
885 static Z_Records *diagrec(association *assoc, int error, char *addinfo)
888 Z_Records *rec = (Z_Records *)odr_malloc (assoc->encode, sizeof(*rec));
890 int *err = (int *)odr_malloc (assoc->encode, sizeof(*err));
891 Z_DiagRec *drec = (Z_DiagRec *)odr_malloc (assoc->encode, sizeof(*drec));
892 Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)odr_malloc (assoc->encode, sizeof(*dr));
894 bib1.proto = assoc->proto;
895 bib1.oclass = CLASS_DIAGSET;
896 bib1.value = VAL_BIB1;
898 logf(LOG_DEBUG, "Diagnostic: %d -- %s", error, addinfo ? addinfo :
901 rec->which = Z_Records_NSD;
903 rec->u.nonSurrogateDiagnostic = dr;
906 rec->u.nonSurrogateDiagnostic = drec;
907 drec->which = Z_DiagRec_defaultFormat;
908 drec->u.defaultFormat = dr;
910 rec->u.nonSurrogateDiagnostic = dr;
913 dr->diagnosticSetId = odr_oiddup (assoc->encode,
914 oid_ent_to_oid(&bib1, oid));
916 set_addinfo (dr, addinfo);
921 * surrogate diagnostic.
923 static Z_NamePlusRecord *surrogatediagrec(association *assoc, char *dbname,
924 int error, char *addinfo)
927 Z_NamePlusRecord *rec = (Z_NamePlusRecord *)odr_malloc (assoc->encode, sizeof(*rec));
928 int *err = (int *)odr_malloc (assoc->encode, sizeof(*err));
930 Z_DiagRec *drec = (Z_DiagRec *)odr_malloc (assoc->encode, sizeof(*drec));
931 Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)odr_malloc (assoc->encode, sizeof(*dr));
933 bib1.proto = assoc->proto;
934 bib1.oclass = CLASS_DIAGSET;
935 bib1.value = VAL_BIB1;
937 logf(LOG_DEBUG, "SurrogateDiagnotic: %d -- %s", error, addinfo);
939 rec->databaseName = dbname;
940 rec->which = Z_NamePlusRecord_surrogateDiagnostic;
941 rec->u.surrogateDiagnostic = drec;
942 drec->which = Z_DiagRec_defaultFormat;
943 drec->u.defaultFormat = dr;
944 dr->diagnosticSetId = odr_oiddup (assoc->encode,
945 oid_ent_to_oid(&bib1, oid));
947 set_addinfo (dr, addinfo);
953 * multiple nonsurrogate diagnostics.
955 static Z_DiagRecs *diagrecs(association *assoc, int error, char *addinfo)
958 Z_DiagRecs *recs = (Z_DiagRecs *)odr_malloc (assoc->encode, sizeof(*recs));
959 int *err = (int *)odr_malloc (assoc->encode, sizeof(*err));
961 Z_DiagRec **recp = (Z_DiagRec **)odr_malloc (assoc->encode, sizeof(*recp));
962 Z_DiagRec *drec = (Z_DiagRec *)odr_malloc (assoc->encode, sizeof(*drec));
963 Z_DefaultDiagFormat *rec = (Z_DefaultDiagFormat *)odr_malloc (assoc->encode, sizeof(*rec));
965 logf(LOG_DEBUG, "DiagRecs: %d -- %s", error, addinfo);
966 bib1.proto = assoc->proto;
967 bib1.oclass = CLASS_DIAGSET;
968 bib1.value = VAL_BIB1;
971 recs->num_diagRecs = 1;
972 recs->diagRecs = recp;
974 drec->which = Z_DiagRec_defaultFormat;
975 drec->u.defaultFormat = rec;
977 rec->diagnosticSetId = odr_oiddup (assoc->encode,
978 oid_ent_to_oid(&bib1, oid));
979 rec->condition = err;
982 rec->which = Z_DefaultDiagFormat_v2Addinfo;
983 rec->u.v2Addinfo = addinfo ? addinfo : "";
985 rec->which = Z_DiagForm_v2AddInfo;
986 rec->addinfo = addinfo ? addinfo : "";
991 static Z_Records *pack_records(association *a, char *setname, int start,
992 int *num, Z_RecordComposition *comp,
993 int *next, int *pres, oid_value format)
996 int recno, total_length = 0, toget = *num, dumped_records = 0;
997 Z_Records *records = (Z_Records *)odr_malloc (a->encode, sizeof(*records));
998 Z_NamePlusRecordList *reclist = (Z_NamePlusRecordList *)odr_malloc (a->encode, sizeof(*reclist));
999 Z_NamePlusRecord **list = (Z_NamePlusRecord **)odr_malloc (a->encode, sizeof(*list) * toget);
1002 records->which = Z_Records_DBOSD;
1003 records->u.databaseOrSurDiagnostics = reclist;
1004 reclist->num_records = 0;
1005 reclist->records = list;
1006 *pres = Z_PRES_SUCCESS;
1010 logf(LOG_DEBUG, "Request to pack %d+%d", start, toget);
1011 logf(LOG_DEBUG, "pms=%d, mrs=%d", a->preferredMessageSize,
1012 a->maximumRecordSize);
1013 for (recno = start; reclist->num_records < toget; recno++)
1015 bend_fetchrequest freq;
1016 bend_fetchresult *fres;
1017 Z_NamePlusRecord *thisrec;
1018 Z_DatabaseRecord *thisext;
1019 int this_length = 0;
1022 * we get the number of bytes allocated on the stream before any
1023 * allocation done by the backend - this should give us a reasonable
1024 * idea of the total size of the data so far.
1026 total_length = odr_total(a->encode) - dumped_records;
1027 freq.setname = setname;
1028 freq.number = recno;
1030 freq.format = format;
1031 freq.stream = a->encode;
1032 freq.surrogate_flag = 0;
1033 if (!(fres = bend_fetch(a->backend, &freq, 0)))
1035 *pres = Z_PRES_FAILURE;
1036 return diagrec(a, 2, "Backend interface problem");
1038 /* backend should be able to signal whether error is system-wide
1039 or only pertaining to current record */
1042 if (!freq.surrogate_flag)
1044 *pres = Z_PRES_FAILURE;
1045 return diagrec(a, fres->errcode, fres->errstring);
1047 reclist->records[reclist->num_records] =
1048 surrogatediagrec(a, fres->basename, fres->errcode,
1050 reclist->num_records++;
1051 *next = fres->last_in_set ? 0 : recno + 1;
1055 this_length = fres->len;
1057 this_length = odr_total(a->encode) - total_length;
1058 logf(LOG_DEBUG, " fetched record, len=%d, total=%d",
1059 this_length, total_length);
1060 if (this_length + total_length > a->preferredMessageSize)
1062 /* record is small enough, really */
1063 if (this_length <= a->preferredMessageSize)
1065 logf(LOG_DEBUG, " Dropped last normal-sized record");
1066 *pres = Z_PRES_PARTIAL_2;
1069 /* record can only be fetched by itself */
1070 if (this_length < a->maximumRecordSize)
1072 logf(LOG_DEBUG, " Record > prefmsgsz");
1075 logf(LOG_DEBUG, " Dropped it");
1076 reclist->records[reclist->num_records] =
1077 surrogatediagrec(a, fres->basename, 16, 0);
1078 reclist->num_records++;
1079 *next = fres->last_in_set ? 0 : recno + 1;
1080 dumped_records += this_length;
1084 else /* too big entirely */
1086 logf(LOG_DEBUG, "Record > maxrcdsz");
1087 reclist->records[reclist->num_records] =
1088 surrogatediagrec(a, fres->basename, 17, 0);
1089 reclist->num_records++;
1090 *next = fres->last_in_set ? 0 : recno + 1;
1091 dumped_records += this_length;
1096 if (!(thisrec = (Z_NamePlusRecord *)odr_malloc(a->encode, sizeof(*thisrec))))
1098 if (!(thisrec->databaseName = (char *)odr_malloc(a->encode,
1099 strlen(fres->basename) + 1)))
1101 strcpy(thisrec->databaseName, fres->basename);
1102 thisrec->which = Z_NamePlusRecord_databaseRecord;
1103 if (!(thisrec->u.databaseRecord = thisext = (Z_External *)odr_malloc(a->encode,
1104 sizeof(Z_DatabaseRecord))))
1106 recform.proto = a->proto;
1107 recform.oclass = CLASS_RECSYN;
1108 recform.value = fres->format;
1109 thisext->direct_reference = odr_oiddup(a->encode,
1110 oid_ent_to_oid(&recform, oid));
1111 thisext->indirect_reference = 0;
1112 thisext->descriptor = 0;
1113 if (fres->len < 0) /* Structured data */
1115 switch (fres->format)
1117 case VAL_SUTRS: thisext->which = Z_External_sutrs; break;
1118 case VAL_GRS1: thisext->which = Z_External_grs1; break;
1119 case VAL_EXPLAIN: thisext->which = Z_External_explainRecord;
1121 case VAL_SUMMARY: thisext->which = Z_External_summary; break;
1122 case VAL_OPAC: thisext->which = Z_External_OPAC; break;
1125 logf(LOG_FATAL, "Unknown structured format from backend.");
1130 * We cheat on the pointers here. Obviously, the record field
1131 * of the backend-fetch structure should have been a union for
1132 * correctness, but we're stuck with this for backwards
1135 thisext->u.grs1 = (Z_GenericRecord*) fres->record;
1137 else if (fres->format == VAL_SUTRS) /* SUTRS is a single-ASN.1-type */
1140 Z_SUTRS *sutrs = (Z_SUTRS *)odr_malloc(a->encode, 1+fres->len);
1142 memcpy(sutrs, fres->record, fres->len);
1143 sutrs[fres->len] = '\0';
1145 Odr_oct *sutrs = (Odr_oct *)odr_malloc(a->encode, sizeof(*sutrs));
1147 thisext->which = Z_External_sutrs;
1148 thisext->u.sutrs = sutrs;
1149 sutrs->buf = (unsigned char *)odr_malloc(a->encode, fres->len);
1150 sutrs->len = sutrs->size = fres->len;
1151 memcpy(sutrs->buf, fres->record, fres->len);
1154 else /* octet-aligned record. */
1156 thisext->which = Z_External_octet;
1157 if (!(thisext->u.octet_aligned = (Odr_oct *)odr_malloc(a->encode,
1160 if (!(thisext->u.octet_aligned->buf = (unsigned char *)odr_malloc(a->encode,
1163 memcpy(thisext->u.octet_aligned->buf, fres->record, fres->len);
1164 thisext->u.octet_aligned->len = thisext->u.octet_aligned->size =
1167 reclist->records[reclist->num_records] = thisrec;
1168 reclist->num_records++;
1169 *next = fres->last_in_set ? 0 : recno + 1;
1171 *num = reclist->num_records;
1175 static Z_APDU *process_searchRequest(association *assoc, request *reqb,
1178 Z_SearchRequest *req = reqb->request->u.searchRequest;
1179 bend_search_rr *bsrr = (bend_search_rr *)nmem_malloc (reqb->request_mem, sizeof(*bsrr));
1181 logf(LOG_LOG, "Got SearchRequest.");
1182 save_referenceId (reqb, req->referenceId);
1183 /* store ref id in request */
1185 bsrr->request = reqb;
1186 bsrr->association = assoc;
1187 if (assoc->bend_search)
1189 bsrr->setname = req->resultSetName;
1190 bsrr->replace_set = *req->replaceIndicator;
1191 bsrr->num_bases = req->num_databaseNames;
1192 bsrr->basenames = req->databaseNames;
1193 bsrr->query = req->query;
1194 bsrr->stream = assoc->encode;
1195 bsrr->decode = assoc->decode;
1198 bsrr->errstring = NULL;
1199 ((int (*)(void *, bend_search_rr *))(assoc->bend_search))(assoc->backend, bsrr);
1205 bend_searchrequest bsrq;
1206 bend_searchresult *bsrt;
1208 bsrq.setname = req->resultSetName;
1209 bsrq.replace_set = *req->replaceIndicator;
1210 bsrq.num_bases = req->num_databaseNames;
1211 bsrq.basenames = req->databaseNames;
1212 bsrq.query = req->query;
1213 bsrq.stream = assoc->encode;
1214 bsrq.decode = assoc->decode;
1215 if (!(bsrt = bend_search(assoc->backend, &bsrq, fd)))
1217 bsrr->hits = bsrt->hits;
1218 bsrr->errcode = bsrt->errcode;
1219 bsrr->errstring = bsrt->errstring;
1221 return response_searchRequest(assoc, reqb, bsrr, fd);
1224 int bend_searchresponse(void *handle, bend_search_rr *bsrr) {return 0;}
1227 * Prepare a searchresponse based on the backend results. We probably want
1228 * to look at making the fetching of records nonblocking as well, but
1229 * so far, we'll keep things simple.
1230 * If bsrt is null, that means we're called in response to a communications
1231 * event, and we'll have to get the response for ourselves.
1233 static Z_APDU *response_searchRequest(association *assoc, request *reqb,
1234 bend_search_rr *bsrt, int *fd)
1236 Z_SearchRequest *req = reqb->request->u.searchRequest;
1237 Z_APDU *apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1238 Z_SearchResponse *resp = (Z_SearchResponse *)odr_malloc (assoc->encode, sizeof(*resp));
1239 int *nulint = (int *)odr_malloc (assoc->encode, sizeof(*nulint));
1240 bool_t *sr = (bool_t *)odr_malloc (assoc->encode, sizeof(*sr));
1241 int *next = (int *)odr_malloc (assoc->encode, sizeof(*next));
1242 int *none = (int *)odr_malloc (assoc->encode, sizeof(*none));
1249 apdu->which = Z_APDU_searchResponse;
1250 apdu->u.searchResponse = resp;
1251 resp->referenceId = req->referenceId;
1252 resp->additionalSearchInfo = 0;
1253 resp->otherInfo = 0;
1255 if (!bsrt && !bend_searchresponse(assoc->backend, bsrt))
1257 logf(LOG_FATAL, "Bad result from backend");
1260 else if (bsrt->errcode)
1262 resp->records = diagrec(assoc, bsrt->errcode, bsrt->errstring);
1263 resp->resultCount = nulint;
1264 resp->numberOfRecordsReturned = nulint;
1265 resp->nextResultSetPosition = nulint;
1266 resp->searchStatus = nulint;
1267 resp->resultSetStatus = none;
1268 resp->presentStatus = 0;
1272 int *toget = (int *)odr_malloc (assoc->encode, sizeof(*toget));
1273 int *presst = (int *)odr_malloc (assoc->encode, sizeof(*presst));
1274 Z_RecordComposition comp, *compp = 0;
1279 resp->resultCount = &bsrt->hits;
1281 comp.which = Z_RecordComp_simple;
1282 /* how many records does the user agent want, then? */
1283 if (bsrt->hits <= *req->smallSetUpperBound)
1285 *toget = bsrt->hits;
1286 if ((comp.u.simple = req->smallSetElementSetNames))
1289 else if (bsrt->hits < *req->largeSetLowerBound)
1291 *toget = *req->mediumSetPresentNumber;
1292 if (*toget > bsrt->hits)
1293 *toget = bsrt->hits;
1294 if ((comp.u.simple = req->mediumSetElementSetNames))
1300 if (*toget && !resp->records)
1305 if (!(prefformat = oid_getentbyoid(req->preferredRecordSyntax)) ||
1306 prefformat->oclass != CLASS_RECSYN)
1309 form = prefformat->value;
1310 resp->records = pack_records(assoc, req->resultSetName, 1,
1311 toget, compp, next, presst, form);
1314 resp->numberOfRecordsReturned = toget;
1315 resp->nextResultSetPosition = next;
1316 resp->searchStatus = sr;
1317 resp->resultSetStatus = 0;
1318 resp->presentStatus = presst;
1322 if (*resp->resultCount)
1324 resp->numberOfRecordsReturned = nulint;
1325 resp->nextResultSetPosition = next;
1326 resp->searchStatus = sr;
1327 resp->resultSetStatus = 0;
1328 resp->presentStatus = 0;
1335 * Maybe we got a little over-friendly when we designed bend_fetch to
1336 * get only one record at a time. Some backends can optimise multiple-record
1337 * fetches, and at any rate, there is some overhead involved in
1338 * all that selecting and hopping around. Problem is, of course, that the
1339 * frontend can't know ahead of time how many records it'll need to
1340 * fill the negotiated PDU size. Annoying. Segmentation or not, Z/SR
1341 * is downright lousy as a bulk data transfer protocol.
1343 * To start with, we'll do the fetching of records from the backend
1344 * in one operation: To save some trips in and out of the event-handler,
1345 * and to simplify the interface to pack_records. At any rate, asynch
1346 * operation is more fun in operations that have an unpredictable execution
1347 * speed - which is normally more true for search than for present.
1349 static Z_APDU *process_presentRequest(association *assoc, request *reqb,
1352 Z_PresentRequest *req = reqb->request->u.presentRequest;
1356 Z_PresentResponse *resp;
1361 logf(LOG_LOG, "Got PresentRequest.");
1363 if (!(prefformat = oid_getentbyoid(req->preferredRecordSyntax)) ||
1364 prefformat->oclass != CLASS_RECSYN)
1367 form = prefformat->value;
1368 if (assoc->bend_present)
1370 bend_present_rr *bprr = (bend_present_rr *)nmem_malloc (reqb->request_mem, sizeof(*bprr));
1371 bprr->setname = req->resultSetId;
1372 bprr->start = *req->resultSetStartPoint;
1373 bprr->number = *req->numberOfRecordsRequested;
1374 bprr->format = form;
1375 bprr->comp = req->recordComposition;
1376 bprr->stream = assoc->encode;
1377 bprr->request = reqb;
1378 bprr->association = assoc;
1380 bprr->errstring = NULL;
1381 ((int (*)(void *, bend_present_rr *))(*assoc->bend_present))(assoc->backend, bprr);
1386 apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1387 resp = (Z_PresentResponse *)odr_malloc (assoc->encode, sizeof(*resp));
1388 presst = (int *)odr_malloc (assoc->encode, sizeof(*presst));
1389 next = (int *)odr_malloc (assoc->encode, sizeof(*next));
1390 num = (int *)odr_malloc (assoc->encode, sizeof(*num));
1393 *num = *req->numberOfRecordsRequested;
1395 apdu->which = Z_APDU_presentResponse;
1396 apdu->u.presentResponse = resp;
1397 resp->referenceId = req->referenceId;
1398 resp->otherInfo = 0;
1401 pack_records(assoc, req->resultSetId, *req->resultSetStartPoint,
1402 num, req->recordComposition, next, presst, form);
1405 resp->numberOfRecordsReturned = num;
1406 resp->presentStatus = presst;
1407 resp->nextResultSetPosition = next;
1413 * Scan was implemented rather in a hurry, and with support for only the basic
1414 * elements of the service in the backend API. Suggestions are welcome.
1416 static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd)
1418 Z_ScanRequest *req = reqb->request->u.scanRequest;
1419 Z_APDU *apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1420 Z_ScanResponse *res = (Z_ScanResponse *)odr_malloc (assoc->encode, sizeof(*res));
1421 int *scanStatus = (int *)odr_malloc (assoc->encode, sizeof(*scanStatus));
1422 int *numberOfEntriesReturned =
1423 (int *)odr_malloc (assoc->encode, sizeof(*numberOfEntriesReturned));
1424 Z_ListEntries *ents = (Z_ListEntries *)odr_malloc (assoc->encode, sizeof(*ents));
1425 Z_DiagRecs *diagrecs_p = NULL;
1427 bend_scanrequest srq;
1428 bend_scanresult *srs;
1431 logf(LOG_LOG, "Got ScanRequest");
1432 *scanStatus = Z_Scan_failure;
1433 *numberOfEntriesReturned = 0;
1435 apdu->which = Z_APDU_scanResponse;
1436 apdu->u.scanResponse = res;
1437 res->referenceId = req->referenceId;
1439 res->scanStatus = scanStatus;
1440 res->numberOfEntriesReturned = numberOfEntriesReturned;
1441 res->positionOfTerm = 0;
1442 res->entries = ents;
1444 ents->num_entries = 0;
1445 ents->entries = NULL;
1446 ents->num_nonsurrogateDiagnostics = 0;
1447 ents->nonsurrogateDiagnostics = NULL;
1449 ents->which = Z_ListEntries_entries;
1451 res->attributeSet = 0;
1454 if (req->attributeSet && (!(attent = oid_getentbyoid(req->attributeSet)) ||
1455 attent->oclass != CLASS_ATTSET
1456 || attent->value != VAL_BIB1))
1457 diagrecs_p = diagrecs(assoc, 121, 0);
1458 else if (req->stepSize && *req->stepSize > 0)
1459 diagrecs_p = diagrecs(assoc, 205, 0);
1462 if (req->termListAndStartPoint->term->which == Z_Term_general)
1463 logf(LOG_DEBUG, " term: '%.*s'",
1464 req->termListAndStartPoint->term->u.general->len,
1465 req->termListAndStartPoint->term->u.general->buf);
1466 srq.num_bases = req->num_databaseNames;
1467 srq.basenames = req->databaseNames;
1468 srq.num_entries = *req->numberOfTermsRequested;
1469 srq.term = req->termListAndStartPoint;
1470 srq.stream = assoc->encode;
1471 if (!(attset = oid_getentbyoid(req->attributeSet)) ||
1472 attset->oclass != CLASS_RECSYN)
1473 srq.attributeset = VAL_NONE;
1475 srq.attributeset = attset->value;
1476 srq.term_position = req->preferredPositionInResponse ?
1477 *req->preferredPositionInResponse : 1;
1478 if (!(srs = bend_scan(assoc->backend, &srq, 0)))
1479 diagrecs_p = diagrecs(assoc, 2, 0);
1480 else if (srs->errcode)
1481 diagrecs_p = diagrecs(assoc, srs->errcode, srs->errstring);
1487 Z_Entries *list = (Z_Entries *)
1488 odr_malloc (assoc->encode, sizeof(*list));
1490 Z_Entry **tab = (Z_Entry **)
1491 odr_malloc (assoc->encode, sizeof(*tab) * srs->num_entries);
1493 if (srs->status == BEND_SCAN_PARTIAL)
1494 *scanStatus = Z_Scan_partial_5;
1496 *scanStatus = Z_Scan_success;
1498 ents->entries = tab;
1499 ents->num_entries = srs->num_entries;
1500 res->numberOfEntriesReturned = &ents->num_entries;
1502 ents->u.entries = list;
1503 list->entries = tab;
1504 list->num_entries = srs->num_entries;
1505 res->numberOfEntriesReturned = &list->num_entries;
1507 res->positionOfTerm = &srs->term_position;
1508 for (i = 0; i < srs->num_entries; i++)
1514 tab[i] = e = (Z_Entry *)odr_malloc(assoc->encode, sizeof(*e));
1515 e->which = Z_Entry_termInfo;
1516 e->u.termInfo = t = (Z_TermInfo *)odr_malloc(assoc->encode, sizeof(*t));
1517 t->suggestedAttributes = 0;
1519 t->alternativeTerm = 0;
1520 t->byAttributes = 0;
1521 t->otherTermInfo = 0;
1522 t->globalOccurrences = &srs->entries[i].occurrences;
1523 t->term = (Z_Term *)odr_malloc(assoc->encode, sizeof(*t->term));
1524 t->term->which = Z_Term_general;
1525 t->term->u.general = o = (Odr_oct *)odr_malloc(assoc->encode,
1527 o->buf = (unsigned char *)odr_malloc(assoc->encode, o->len = o->size =
1528 strlen(srs->entries[i].term));
1529 memcpy(o->buf, srs->entries[i].term, o->len);
1530 logf(LOG_DEBUG, " term #%d: '%s' (%d)", i,
1531 srs->entries[i].term, srs->entries[i].occurrences);
1538 ents->num_nonsurrogateDiagnostics = diagrecs_p->num_diagRecs;
1539 ents->nonsurrogateDiagnostics = diagrecs_p->diagRecs;
1541 ents->u.nonSurrogateDiagnostics = diagrecs_p;
1542 ents->which = Z_ListEntries_nonSurrogateDiagnostics;
1548 static Z_APDU *process_sortRequest(association *assoc, request *reqb,
1551 Z_SortRequest *req = reqb->request->u.sortRequest;
1552 Z_SortResponse *res = (Z_SortResponse *)odr_malloc (assoc->encode, sizeof(*res));
1553 bend_sort_rr *bsrr = (bend_sort_rr *)odr_malloc (assoc->encode, sizeof(*bsrr));
1555 Z_APDU *apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1557 logf(LOG_LOG, "Got SortRequest.");
1560 bsrr->num_input_setnames = req->num_inputResultSetNames;
1561 bsrr->input_setnames = req->inputResultSetNames;
1563 bsrr->num_input_setnames = req->inputResultSetNames->num_strings;
1564 bsrr->input_setnames = req->inputResultSetNames->strings;
1566 bsrr->output_setname = req->sortedResultSetName;
1567 bsrr->sort_sequence = req->sortSequence;
1568 bsrr->stream = assoc->encode;
1570 bsrr->sort_status = Z_SortStatus_failure;
1572 bsrr->errstring = 0;
1574 ((int (*)(void *, bend_sort_rr *))(*assoc->bend_sort))(assoc->backend, bsrr);
1576 res->referenceId = req->referenceId;
1577 res->sortStatus = (int *)odr_malloc (assoc->encode, sizeof(*res->sortStatus));
1578 *res->sortStatus = bsrr->sort_status;
1579 res->resultSetStatus = 0;
1582 Z_DiagRecs *dr = diagrecs (assoc, bsrr->errcode, bsrr->errstring);
1584 res->diagnostics = dr->diagRecs;
1585 res->num_diagnostics = dr->num_diagRecs;
1587 res->diagnostics = dr;
1593 res->num_diagnostics = 0;
1595 res->diagnostics = 0;
1599 apdu->which = Z_APDU_sortResponse;
1600 apdu->u.sortResponse = res;
1604 static Z_APDU *process_deleteRequest(association *assoc, request *reqb,
1607 Z_DeleteResultSetRequest *req = reqb->request->u.deleteResultSetRequest;
1608 Z_DeleteResultSetResponse *res = (Z_DeleteResultSetResponse *)
1609 odr_malloc (assoc->encode, sizeof(*res));
1610 bend_delete_rr *bdrr = (bend_delete_rr *)
1611 odr_malloc (assoc->encode, sizeof(*bdrr));
1612 Z_APDU *apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1614 logf(LOG_LOG, "Got DeleteRequest.");
1616 bdrr->num_setnames = req->num_resultSetList;
1617 bdrr->setnames = req->resultSetList;
1618 bdrr->stream = assoc->encode;
1619 bdrr->function = *req->deleteFunction;
1621 ((int (*)(void *, bend_delete_rr *))
1622 (*assoc->bend_delete))(assoc->backend, bdrr);
1624 res->referenceId = req->referenceId;
1626 res->deleteOperationStatus = (int *)
1627 odr_malloc (assoc->encode, sizeof(*res->deleteOperationStatus));
1628 *res->deleteOperationStatus = bdrr->delete_status;
1630 res->deleteListStatuses = 0;
1631 res->numberNotDeleted = 0;
1632 res->bulkStatuses = 0;
1633 res->deleteMessage = 0;
1636 apdu->which = Z_APDU_deleteResultSetResponse;
1637 apdu->u.deleteResultSetResponse = res;
1641 static void process_close(association *assoc, request *reqb)
1643 Z_Close *req = reqb->request->u.close;
1644 static char *reasons[] =
1651 "securityViolation",
1658 logf(LOG_LOG, "Got Close, reason %s, message %s",
1659 reasons[*req->closeReason], req->diagnosticInformation ?
1660 req->diagnosticInformation : "NULL");
1661 if (assoc->version < 3) /* to make do_force respond with close */
1663 do_close_req(assoc, Z_Close_finished, "Association terminated by client",
1667 void save_referenceId (request *reqb, Z_ReferenceId *refid)
1671 reqb->len_refid = refid->len;
1672 reqb->refid = (char *)nmem_malloc (reqb->request_mem, refid->len);
1673 memcpy (reqb->refid, refid->buf, refid->len);
1677 reqb->len_refid = 0;
1682 void bend_request_send (bend_association a, bend_request req, Z_APDU *res)
1684 process_response (a, req, res);
1687 bend_request bend_request_mk (bend_association a)
1689 request *nreq = request_get (&a->outgoing);
1690 nreq->request_mem = nmem_create ();
1694 Z_ReferenceId *bend_request_getid (ODR odr, bend_request req)
1699 id = (Odr_oct *)odr_malloc (odr, sizeof(*odr));
1700 id->buf = (unsigned char *)odr_malloc (odr, req->len_refid);
1701 id->len = id->size = req->len_refid;
1702 memcpy (id->buf, req->refid, req->len_refid);
1706 void bend_request_destroy (bend_request *req)
1708 nmem_destroy((*req)->request_mem);
1709 request_release(*req);
1713 int bend_backend_respond (bend_association a, bend_request req)
1715 return process_request (a, req);
1718 void bend_request_setdata(bend_request r, void *p)
1723 void *bend_request_getdata(bend_request r)
1725 return r->clientData;
1728 /* Chas: Added in from DALI */
1729 static Z_APDU *process_ESRequest(association *assoc, request *reqb, int *fd)
1731 bend_esrequest_rr esrequest;
1733 Z_ExtendedServicesRequest *req = reqb->request->u.extendedServicesRequest;
1734 Z_APDU *apdu = zget_APDU(assoc->encode, Z_APDU_extendedServicesResponse);
1736 Z_ExtendedServicesResponse *resp = apdu->u.extendedServicesResponse;
1738 logf(LOG_DEBUG,"inside Process esRequest");
1740 esrequest.esr = reqb->request->u.extendedServicesRequest;
1741 esrequest.stream = assoc->encode;
1742 esrequest.errcode = 0;
1743 esrequest.errstring = NULL;
1744 esrequest.request = reqb;
1745 esrequest.association = assoc;
1747 ((int (*)(void *, bend_esrequest_rr *))(*assoc->bend_esrequest))(assoc->backend,
1750 /* If the response is being delayed, return NULL */
1751 if (esrequest.request == NULL)
1754 resp->referenceId = req->referenceId;
1756 if ( esrequest.errcode == 0 )
1758 /* Backend service indicates request will be processed */
1759 logf(LOG_DEBUG,"Request will be processed...Good !");
1760 *resp->operationStatus = Z_ExtendedServicesResponse_done;
1764 /* Backend indicates error, request will not be processed */
1765 logf(LOG_DEBUG,"Request will not be processed...BAD !");
1766 *resp->operationStatus = Z_ExtendedServicesResponse_failure;
1768 /* Do something with the members of bend_extendedservice */
1770 logf(LOG_DEBUG,"Send the result apdu");
1775 /* Chas: End of addition from DALI */