2 * Copyright (c) 1995-1998, Index Data
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.83 1998-11-03 10:09:36 adam
8 * Fixed bug regarding YC.
10 * Revision 1.82 1998/10/20 14:00:30 quinn
13 * Revision 1.81 1998/10/13 16:12:24 adam
14 * Added support for Surrogate Diagnostics for Scan Term entries.
16 * Revision 1.80 1998/09/02 12:41:53 adam
17 * Added decode stream in bend search structures.
19 * Revision 1.79 1998/08/19 16:10:08 adam
20 * Changed som member names of DeleteResultSetRequest/Response.
22 * Revision 1.78 1998/08/03 10:23:55 adam
23 * Fixed bug regarding Options for Sort.
25 * Revision 1.77 1998/07/20 12:38:42 adam
26 * Implemented delete result set service to server API.
28 * Revision 1.76 1998/05/27 16:57:07 adam
29 * Support for surrogate diagnostic records added for bend_fetch.
31 * Revision 1.75 1998/05/18 10:13:07 adam
32 * Fixed call to es_request handler - extra argument was passed.
34 * Revision 1.74 1998/03/31 15:13:20 adam
35 * Development towards compiled ASN.1.
37 * Revision 1.73 1998/03/31 11:07:45 adam
38 * Furhter work on UNIverse resource report.
39 * Added Extended Services handling in frontend server.
41 * Revision 1.72 1998/02/11 11:53:35 adam
42 * Changed code so that it compiles as C++.
44 * Revision 1.71 1998/02/10 11:03:57 adam
45 * Added support for extended handlers in backend server interface.
47 * Revision 1.70 1998/01/29 13:15:35 adam
48 * Implemented sort for the backend interface.
50 * Revision 1.69 1997/09/30 11:48:12 adam
51 * Fixed bug introduced by previous commit.
53 * Revision 1.68 1997/09/29 13:18:59 adam
54 * Added function, oid_ent_to_oid, to replace the function
55 * oid_getoidbyent, which is not thread safe.
57 * Revision 1.67 1997/09/17 12:10:40 adam
60 * Revision 1.66 1997/09/05 15:26:44 adam
61 * Added ODR encode in search and scen bend request structures.
62 * Fixed a few enums that caused trouble with C++.
64 * Revision 1.65 1997/09/01 08:53:01 adam
65 * New windows NT/95 port using MSV5.0. The test server 'ztest' was
66 * moved a separate directory. MSV5.0 project server.dsp created.
67 * As an option, the server can now operate as an NT service.
69 * Revision 1.64 1997/04/30 08:52:11 quinn
72 * Revision 1.63 1996/10/11 11:57:26 quinn
75 * Revision 1.62 1996/07/06 19:58:35 quinn
76 * System headerfiles gathered in yconfig
78 * Revision 1.61 1996/06/10 08:56:16 quinn
81 * Revision 1.60 1996/05/30 11:03:10 quinn
82 * Fixed NextresultSetPosition bug fixed.
84 * Revision 1.59 1996/05/14 09:26:46 quinn
85 * Added attribute set to scan backend
87 * Revision 1.58 1996/02/20 12:53:04 quinn
90 * Revision 1.57 1996/01/02 08:57:47 quinn
91 * Changed enums in the ASN.1 .h files to #defines. Changed oident.class to oclass
93 * Revision 1.56 1995/12/14 11:09:57 quinn
96 * Revision 1.55 1995/11/08 17:41:37 quinn
99 * Revision 1.54 1995/11/08 15:11:29 quinn
100 * Log of close transmit.
102 * Revision 1.53 1995/11/01 13:54:58 quinn
105 * Revision 1.52 1995/11/01 12:19:13 quinn
106 * Second attempt to fix same bug.
108 * Revision 1.50 1995/10/25 16:58:32 quinn
111 * Revision 1.49 1995/10/16 13:51:53 quinn
112 * Changes to provide Especs to the backend.
114 * Revision 1.48 1995/10/06 08:51:20 quinn
115 * Added Write-buffer.
117 * Revision 1.47 1995/08/29 14:24:16 quinn
118 * Added second half of close-handshake
120 * Revision 1.46 1995/08/29 11:17:58 quinn
121 * Added code to receive close
123 * Revision 1.45 1995/08/21 09:11:00 quinn
124 * Smallish fixes to suppport new formats.
126 * Revision 1.44 1995/08/17 12:45:25 quinn
127 * Fixed minor problems with GRS-1. Added support in c&s.
129 * Revision 1.43 1995/08/15 12:00:31 quinn
132 * Revision 1.42 1995/08/15 11:16:50 quinn
134 * Revision 1.41 1995/08/02 10:23:06 quinn
137 * Revision 1.40 1995/07/31 14:34:26 quinn
138 * Fixed bug in process_searchResponse (numberOfRecordsReturned).
140 * Revision 1.39 1995/06/27 13:21:00 quinn
143 * Revision 1.38 1995/06/19 12:39:11 quinn
144 * Fixed bug in timeout code. Added BER dumper.
146 * Revision 1.37 1995/06/16 13:16:14 quinn
147 * Fixed Defaultdiagformat.
149 * Revision 1.36 1995/06/16 10:31:36 quinn
150 * Added session timeout.
152 * Revision 1.35 1995/06/15 07:45:14 quinn
155 * Revision 1.34 1995/06/14 15:26:46 quinn
156 * *** empty log message ***
158 * Revision 1.33 1995/06/06 14:57:05 quinn
159 * Better diagnostics.
161 * Revision 1.32 1995/06/06 08:41:44 quinn
162 * Better diagnostics.
164 * Revision 1.31 1995/06/06 08:15:37 quinn
167 * Revision 1.30 1995/06/05 10:53:32 quinn
168 * Added a better SCAN.
170 * Revision 1.29 1995/06/01 11:25:03 quinn
173 * Revision 1.28 1995/06/01 11:21:01 quinn
174 * Attempting to fix a bug in pack-records. replaced break with continue
175 * for large records, according to standard.
177 * Revision 1.27 1995/05/29 08:12:06 quinn
180 * Revision 1.26 1995/05/18 13:02:12 quinn
183 * Revision 1.25 1995/05/17 08:42:26 quinn
184 * Transfer auth info to backend. Allow backend to reject init gracefully.
186 * Revision 1.24 1995/05/16 08:51:04 quinn
187 * License, documentation, and memory fixes
189 * Revision 1.23 1995/05/15 13:25:10 quinn
192 * Revision 1.22 1995/05/15 11:56:39 quinn
193 * Asynchronous facilities. Restructuring of seshigh code.
195 * Revision 1.21 1995/05/02 08:53:19 quinn
196 * Trying in vain to fix comm with ISODE
198 * Revision 1.20 1995/04/20 15:13:00 quinn
201 * Revision 1.19 1995/04/18 08:15:34 quinn
202 * Added dynamic memory allocation on encoding (whew). Code is now somewhat
203 * neater. We'll make the same change for decoding one day.
205 * Revision 1.18 1995/04/17 11:28:25 quinn
208 * Revision 1.17 1995/04/10 10:23:36 quinn
209 * Some work to add scan and other things.
211 * Revision 1.16 1995/03/31 09:18:55 quinn
214 * Revision 1.15 1995/03/30 14:03:23 quinn
215 * Added RFC1006 as separate library
217 * Revision 1.14 1995/03/30 12:18:17 quinn
220 * Revision 1.13 1995/03/30 09:09:24 quinn
221 * Added state-handle and some support for asynchronous activities.
223 * Revision 1.12 1995/03/29 15:40:16 quinn
224 * Ongoing work. Statserv is now dynamic by default
226 * Revision 1.11 1995/03/28 09:16:21 quinn
227 * Added record packing to the search request
229 * Revision 1.10 1995/03/27 08:34:24 quinn
230 * Added dynamic server functionality.
231 * Released bindings to session.c (is now redundant)
233 * Revision 1.9 1995/03/22 15:01:26 quinn
234 * Adjusting record packing.
236 * Revision 1.8 1995/03/22 10:13:21 quinn
237 * Working on record packer
239 * Revision 1.7 1995/03/21 15:53:31 quinn
242 * Revision 1.6 1995/03/21 12:30:09 quinn
243 * Beginning to add support for record packing.
245 * Revision 1.5 1995/03/17 10:44:13 quinn
246 * Added catch of null-string in makediagrec
248 * Revision 1.4 1995/03/17 10:18:08 quinn
249 * Added memory management.
251 * Revision 1.3 1995/03/16 17:42:39 quinn
254 * Revision 1.2 1995/03/16 13:29:01 quinn
255 * Partitioned server.
257 * Revision 1.1 1995/03/15 16:02:10 quinn
258 * Modded session.c to seshigh.c
263 * Frontend server logic.
265 * This code receives incoming APDUs, and handles client requests by means
266 * of the backend API.
268 * Some of the code is getting quite involved, compared to simpler servers -
269 * primarily because it is asynchronous both in the communication with
270 * the user and the backend. We think the complexity will pay off in
271 * the form of greater flexibility when more asynchronous facilities
274 * Memory management has become somewhat involved. In the simple case, where
275 * only one PDU is pending at a time, it will simply reuse the same memory,
276 * once it has found its working size. When we enable multiple concurrent
277 * operations, perhaps even with multiple parallel calls to the backend, it
278 * will maintain a pool of buffers for encoding and decoding, trying to
279 * minimize memory allocation/deallocation during normal operation.
281 * TODOs include (and will be done in order of public interest):
283 * Support for EXPLAIN - provide simple meta-database system.
284 * Support for access control.
285 * Support for resource control.
286 * Support for extended services - primarily Item Order.
287 * Rest of Z39.50-1994
302 #include <comstack.h>
308 #include <statserv.h>
312 static int process_request(association *assoc, request *req);
313 void backend_response(IOCHAN i, int event);
314 static int process_response(association *assoc, request *req, Z_APDU *res);
315 static Z_APDU *process_initRequest(association *assoc, request *reqb);
316 static Z_APDU *process_searchRequest(association *assoc, request *reqb,
318 static Z_APDU *response_searchRequest(association *assoc, request *reqb,
319 bend_search_rr *bsrr, int *fd);
320 static Z_APDU *process_presentRequest(association *assoc, request *reqb,
322 static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd);
323 static Z_APDU *process_sortRequest(association *assoc, request *reqb, int *fd);
324 static void process_close(association *assoc, request *reqb);
325 void save_referenceId (request *reqb, Z_ReferenceId *refid);
326 static Z_APDU *process_deleteRequest(association *assoc, request *reqb,
329 static FILE *apduf = 0; /* for use in static mode */
330 static statserv_options_block *control_block = 0;
332 /* Chas: Added in from DALI */
333 static Z_APDU *process_ESRequest(association *assoc, request *reqb, int *fd);
334 /* Chas: End of addition from DALI */
337 * Create and initialize a new association-handle.
338 * channel : iochannel for the current line.
339 * link : communications channel.
340 * Returns: 0 or a new association handle.
342 association *create_association(IOCHAN channel, COMSTACK link)
347 control_block = statserv_getcontrol();
348 if (!(anew = (association *)xmalloc(sizeof(*anew))))
350 anew->client_chan = channel;
351 anew->client_link = link;
352 if (!(anew->decode = odr_createmem(ODR_DECODE)) ||
353 !(anew->encode = odr_createmem(ODR_ENCODE)))
355 if (*control_block->apdufile)
360 strcpy(filename, control_block->apdufile);
361 if (!(anew->print = odr_createmem(ODR_PRINT)))
363 if (*control_block->apdufile != '-')
365 strcpy(filename, control_block->apdufile);
366 if (!control_block->dynamic)
370 if (!(apduf = fopen(filename, "w")))
372 logf(LOG_WARN|LOG_ERRNO, "%s", filename);
375 setvbuf(apduf, 0, _IONBF, 0);
381 sprintf(filename + strlen(filename), ".%d", getpid());
382 if (!(f = fopen(filename, "w")))
384 logf(LOG_WARN|LOG_ERRNO, "%s", filename);
387 setvbuf(f, 0, _IONBF, 0);
389 odr_setprint(anew->print, f);
394 anew->input_buffer = 0;
395 anew->input_buffer_len = 0;
397 anew->state = ASSOC_NEW;
398 request_initq(&anew->incoming);
399 request_initq(&anew->outgoing);
400 anew->proto = cs_getproto(link);
405 * Free association and release resources.
407 void destroy_association(association *h)
409 odr_destroy(h->decode);
410 odr_destroy(h->encode);
412 odr_destroy(h->print);
414 xfree(h->input_buffer);
416 bend_close(h->backend);
417 while (request_deq(&h->incoming));
418 while (request_deq(&h->outgoing));
419 request_delq(&h->incoming);
420 request_delq(&h->outgoing);
424 static void do_close_req(association *a, int reason, char *message,
428 Z_Close *cls = zget_Close(a->encode);
430 /* Purge request queue */
431 while (request_deq(&a->incoming));
432 while (request_deq(&a->outgoing));
435 logf(LOG_LOG, "Sending Close PDU, reason=%d, message=%s",
436 reason, message ? message : "none");
437 apdu.which = Z_APDU_close;
439 *cls->closeReason = reason;
440 cls->diagnosticInformation = message;
441 process_response(a, req, &apdu);
442 iochan_settimeout(a->client_chan, 60);
446 logf(LOG_DEBUG, "v2 client. No Close PDU");
447 iochan_setevent(a->client_chan, EVENT_TIMEOUT); /* force imm close */
449 a->state = ASSOC_DEAD;
452 static void do_close(association *a, int reason, char *message)
454 do_close_req (a, reason, message, request_get(&a->outgoing));
458 * This is where PDUs from the client are read and the further
459 * processing is initiated. Flow of control moves down through the
460 * various process_* functions below, until the encoded result comes back up
461 * to the output handler in here.
463 * h : the I/O channel that has an outstanding event.
464 * event : the current outstanding event.
466 void ir_session(IOCHAN h, int event)
469 association *assoc = (association *)iochan_getdata(h);
470 COMSTACK conn = assoc->client_link;
473 assert(h && conn && assoc);
474 if (event == EVENT_TIMEOUT)
476 if (assoc->state != ASSOC_UP)
478 logf(LOG_LOG, "Final timeout - closing connection.");
480 destroy_association(assoc);
485 logf(LOG_LOG, "Session idle too long. Sending close.");
486 do_close(assoc, Z_Close_lackOfActivity, 0);
490 if (event & EVENT_INPUT || event & EVENT_WORK) /* input */
492 if (event & EVENT_INPUT)
494 logf(LOG_DEBUG, "ir_session (input)");
495 assert(assoc && conn);
496 /* We aren't speaking to this fellow */
497 if (assoc->state == ASSOC_DEAD)
499 logf(LOG_LOG, "Closed connection after reject");
501 destroy_association(assoc);
505 if ((res = cs_get(conn, &assoc->input_buffer,
506 &assoc->input_buffer_len)) <= 0)
508 logf(LOG_LOG, "Connection closed by client");
510 destroy_association(assoc);
514 else if (res == 1) /* incomplete read - wait for more */
516 if (cs_more(conn)) /* more stuff - call us again later, please */
517 iochan_setevent(h, EVENT_INPUT);
519 /* we got a complete PDU. Let's decode it */
520 logf(LOG_DEBUG, "Got PDU, %d bytes", res);
521 req = request_get(&assoc->incoming); /* get a new request structure */
522 odr_reset(assoc->decode);
523 odr_setbuf(assoc->decode, assoc->input_buffer, res, 0);
524 if (!z_APDU(assoc->decode, &req->request, 0))
526 logf(LOG_LOG, "ODR error on incoming PDU: %s [near byte %d] ",
527 odr_errmsg(odr_geterror(assoc->decode)),
528 odr_offset(assoc->decode));
529 logf(LOG_LOG, "PDU dump:");
530 odr_dumpBER(log_file(), assoc->input_buffer, res);
531 do_close(assoc, Z_Close_protocolError, "Malformed package");
534 req->request_mem = odr_extract_mem(assoc->decode);
535 if (assoc->print && !z_APDU(assoc->print, &req->request, 0))
537 logf(LOG_WARN, "ODR print error: %s",
538 odr_errmsg(odr_geterror(assoc->print)));
539 odr_reset(assoc->print);
541 request_enq(&assoc->incoming, req);
544 /* can we do something yet? */
545 req = request_head(&assoc->incoming);
546 if (req->state == REQUEST_IDLE)
548 request_deq(&assoc->incoming);
549 if (process_request(assoc, req) < 0)
550 do_close_req(assoc, Z_Close_systemProblem, "Unknown error",
554 if (event & EVENT_OUTPUT)
556 request *req = request_head(&assoc->outgoing);
558 logf(LOG_DEBUG, "ir_session (output)");
559 req->state = REQUEST_PENDING;
560 switch (res = cs_put(conn, req->response, req->len_response))
563 logf(LOG_LOG, "Connection closed by client");
565 destroy_association(assoc);
568 case 0: /* all sent - release the request structure */
569 logf(LOG_DEBUG, "Wrote PDU, %d bytes", req->len_response);
570 nmem_destroy(req->request_mem);
571 request_deq(&assoc->outgoing);
572 request_release(req);
573 if (!request_head(&assoc->outgoing))
574 iochan_clearflag(h, EVENT_OUTPUT);
576 /* value of 1 -- partial send -- is simply ignored */
579 if (event & EVENT_EXCEPT)
581 logf(LOG_DEBUG, "ir_session (exception)");
583 destroy_association(assoc);
589 * Initiate request processing.
591 static int process_request(association *assoc, request *req)
597 assert(req && req->state == REQUEST_IDLE);
598 switch (req->request->which)
600 case Z_APDU_initRequest:
601 res = process_initRequest(assoc, req); break;
602 case Z_APDU_searchRequest:
603 res = process_searchRequest(assoc, req, &fd); break;
604 case Z_APDU_presentRequest:
605 res = process_presentRequest(assoc, req, &fd); break;
606 case Z_APDU_scanRequest:
607 res = process_scanRequest(assoc, req, &fd); break;
608 case Z_APDU_extendedServicesRequest:
609 if (assoc->bend_esrequest)
610 res = process_ESRequest(assoc, req, &fd);
613 logf(LOG_WARN, "Cannot handle EXTENDED SERVICES APDU");
617 case Z_APDU_sortRequest:
618 if (assoc->bend_sort)
619 res = process_sortRequest(assoc, req, &fd);
622 logf(LOG_WARN, "Cannot handle SORT APDU");
627 process_close(assoc, req); return 0;
628 case Z_APDU_deleteResultSetRequest:
629 if (assoc->bend_delete)
630 res = process_deleteRequest(assoc, req, &fd);
633 logf (LOG_WARN, "Cannot handle Delete APDU");
638 logf(LOG_WARN, "Bad APDU received");
643 logf(LOG_DEBUG, " result immediately available");
644 retval = process_response(assoc, req, res);
648 logf(LOG_DEBUG, " result unavailble");
651 else /* no result yet - one will be provided later */
655 /* Set up an I/O handler for the fd supplied by the backend */
657 logf(LOG_DEBUG, " establishing handler for result");
658 req->state = REQUEST_PENDING;
659 if (!(chan = iochan_create(fd, backend_response, EVENT_INPUT)))
661 iochan_setdata(chan, assoc);
668 * Handle message from the backend.
670 void backend_response(IOCHAN i, int event)
672 association *assoc = (association *)iochan_getdata(i);
673 request *req = request_head(&assoc->incoming);
677 logf(LOG_DEBUG, "backend_response");
678 assert(assoc && req && req->state != REQUEST_IDLE);
679 /* determine what it is we're waiting for */
680 switch (req->request->which)
682 case Z_APDU_searchRequest:
683 res = response_searchRequest(assoc, req, 0, &fd); break;
685 case Z_APDU_presentRequest:
686 res = response_presentRequest(assoc, req, 0, &fd); break;
687 case Z_APDU_scanRequest:
688 res = response_scanRequest(assoc, req, 0, &fd); break;
691 logf(LOG_WARN, "Serious programmer's lapse or bug");
694 if ((res && process_response(assoc, req, res) < 0) || fd < 0)
696 logf(LOG_LOG, "Fatal error when talking to backend");
697 do_close(assoc, Z_Close_systemProblem, 0);
701 else if (!res) /* no result yet - try again later */
703 logf(LOG_DEBUG, " no result yet");
704 iochan_setfd(i, fd); /* in case fd has changed */
709 * Encode response, and transfer the request structure to the outgoing queue.
711 static int process_response(association *assoc, request *req, Z_APDU *res)
713 odr_setbuf(assoc->encode, req->response, req->size_response, 1);
714 if (!z_APDU(assoc->encode, &res, 0))
716 logf(LOG_WARN, "ODR error when encoding response: %s",
717 odr_errmsg(odr_geterror(assoc->decode)));
718 odr_reset(assoc->encode);
721 req->response = odr_getbuf(assoc->encode, &req->len_response,
722 &req->size_response);
723 odr_setbuf(assoc->encode, 0, 0, 0); /* don'txfree if we abort later */
724 if (assoc->print && !z_APDU(assoc->print, &res, 0))
726 logf(LOG_WARN, "ODR print error: %s",
727 odr_errmsg(odr_geterror(assoc->print)));
728 odr_reset(assoc->print);
730 odr_reset(assoc->encode);
731 req->state = REQUEST_IDLE;
732 request_enq(&assoc->outgoing, req);
733 /* turn the work over to the ir_session handler */
734 iochan_setflag(assoc->client_chan, EVENT_OUTPUT);
735 /* Is there more work to be done? give that to the input handler too */
737 if (request_head(&assoc->incoming))
739 logf (LOG_DEBUG, "more work to be done");
740 iochan_setevent(assoc->client_chan, EVENT_WORK);
747 * Handle init request.
748 * At the moment, we don't check the options
749 * anywhere else in the code - we just try not to do anything that would
750 * break a naive client. We'll toss 'em into the association block when
751 * we need them there.
753 static Z_APDU *process_initRequest(association *assoc, request *reqb)
755 Z_InitRequest *req = reqb->request->u.initRequest;
756 Z_APDU *apdu = zget_APDU(assoc->encode, Z_APDU_initResponse);
757 Z_InitResponse *resp = apdu->u.initResponse;
758 bend_initrequest binitreq;
759 bend_initresult *binitres;
762 logf(LOG_LOG, "Got initRequest");
763 if (req->implementationId)
764 logf(LOG_LOG, "Id: %s", req->implementationId);
765 if (req->implementationName)
766 logf(LOG_LOG, "Name: %s", req->implementationName);
767 if (req->implementationVersion)
768 logf(LOG_LOG, "Version: %s", req->implementationVersion);
770 binitreq.stream = assoc->encode;
771 binitreq.configname = "default-config";
772 binitreq.auth = req->idAuthentication;
773 binitreq.bend_sort = NULL;
774 binitreq.bend_search = NULL;
775 binitreq.bend_present = NULL;
776 binitreq.bend_esrequest = NULL;
777 binitreq.bend_delete = NULL;
778 if (!(binitres = bend_init(&binitreq)))
780 logf(LOG_WARN, "Bad response from backend.");
784 assoc->backend = binitres->handle;
785 if ((assoc->bend_sort = (int (*)())binitreq.bend_sort))
786 logf (LOG_DEBUG, "Sort handler installed");
787 if ((assoc->bend_search = (int (*)())binitreq.bend_search))
788 logf (LOG_DEBUG, "Search handler installed");
789 if ((assoc->bend_present = (int (*)())binitreq.bend_present))
790 logf (LOG_DEBUG, "Present handler installed");
791 if ((assoc->bend_esrequest = (int (*)())binitreq.bend_esrequest))
792 logf (LOG_DEBUG, "ESRequest handler installed");
793 if ((assoc->bend_delete = (int (*)())binitreq.bend_delete))
794 logf (LOG_DEBUG, "Delete handler installed");
796 resp->referenceId = req->referenceId;
798 /* let's tell the client what we can do */
799 if (ODR_MASK_GET(req->options, Z_Options_search))
801 ODR_MASK_SET(resp->options, Z_Options_search);
802 strcat(options, "srch");
804 if (ODR_MASK_GET(req->options, Z_Options_present))
806 ODR_MASK_SET(resp->options, Z_Options_present);
807 strcat(options, " prst");
809 if (ODR_MASK_GET(req->options, Z_Options_delSet) && binitreq.bend_delete)
811 ODR_MASK_SET(resp->options, Z_Options_delSet);
812 strcat(options, " del");
814 if (ODR_MASK_GET(req->options, Z_Options_extendedServices) &&
815 binitreq.bend_esrequest)
817 ODR_MASK_SET(resp->options, Z_Options_extendedServices);
818 strcat (options, " extendedServices");
820 if (ODR_MASK_GET(req->options, Z_Options_namedResultSets))
822 ODR_MASK_SET(resp->options, Z_Options_namedResultSets);
823 strcat(options, " namedresults");
825 if (ODR_MASK_GET(req->options, Z_Options_scan))
827 ODR_MASK_SET(resp->options, Z_Options_scan);
828 strcat(options, " scan");
830 if (ODR_MASK_GET(req->options, Z_Options_concurrentOperations))
832 ODR_MASK_SET(resp->options, Z_Options_concurrentOperations);
833 strcat(options, " concurop");
835 if (ODR_MASK_GET(req->options, Z_Options_sort) && binitreq.bend_sort)
837 ODR_MASK_SET(resp->options, Z_Options_sort);
838 strcat(options, " sort");
840 if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_1))
842 ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_1);
843 assoc->version = 2; /* 1 & 2 are equivalent */
845 if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_2))
847 ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_2);
850 if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_3))
852 ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_3);
855 logf(LOG_LOG, "Negotiated to v%d: %s", assoc->version, options);
856 assoc->maximumRecordSize = *req->maximumRecordSize;
857 if (assoc->maximumRecordSize > control_block->maxrecordsize)
858 assoc->maximumRecordSize = control_block->maxrecordsize;
859 assoc->preferredMessageSize = *req->preferredMessageSize;
860 if (assoc->preferredMessageSize > assoc->maximumRecordSize)
861 assoc->preferredMessageSize = assoc->maximumRecordSize;
862 resp->preferredMessageSize = &assoc->preferredMessageSize;
863 resp->maximumRecordSize = &assoc->maximumRecordSize;
864 resp->implementationName = "Index Data/YAZ Generic Frontend Server";
865 if (binitres->errcode)
867 logf(LOG_LOG, "Connection rejected by backend.");
869 assoc->state = ASSOC_DEAD;
872 assoc->state = ASSOC_UP;
877 * These functions should be merged.
880 static void set_addinfo (Z_DefaultDiagFormat *dr, char *addinfo)
883 dr->which = Z_DefaultDiagFormat_v2Addinfo;
884 dr->u.v2Addinfo = addinfo ? addinfo : "";
886 dr->which = Z_DiagForm_v2AddInfo;
887 dr->addinfo = addinfo ? addinfo : "";
892 * nonsurrogate diagnostic record.
894 static Z_Records *diagrec(association *assoc, int error, char *addinfo)
897 Z_Records *rec = (Z_Records *)
898 odr_malloc (assoc->encode, sizeof(*rec));
901 odr_malloc (assoc->encode, sizeof(*err));
902 Z_DiagRec *drec = (Z_DiagRec *)
903 odr_malloc (assoc->encode, sizeof(*drec));
904 Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
905 odr_malloc (assoc->encode, sizeof(*dr));
907 bib1.proto = assoc->proto;
908 bib1.oclass = CLASS_DIAGSET;
909 bib1.value = VAL_BIB1;
911 logf(LOG_DEBUG, "Diagnostic: %d -- %s", error, addinfo ? addinfo :
914 rec->which = Z_Records_NSD;
916 rec->u.nonSurrogateDiagnostic = dr;
919 rec->u.nonSurrogateDiagnostic = drec;
920 drec->which = Z_DiagRec_defaultFormat;
921 drec->u.defaultFormat = dr;
923 rec->u.nonSurrogateDiagnostic = dr;
926 dr->diagnosticSetId = odr_oiddup (assoc->encode,
927 oid_ent_to_oid(&bib1, oid));
929 set_addinfo (dr, addinfo);
934 * surrogate diagnostic.
936 static Z_NamePlusRecord *surrogatediagrec(association *assoc, char *dbname,
937 int error, char *addinfo)
940 Z_NamePlusRecord *rec = (Z_NamePlusRecord *)odr_malloc (assoc->encode, sizeof(*rec));
941 int *err = (int *)odr_malloc (assoc->encode, sizeof(*err));
943 Z_DiagRec *drec = (Z_DiagRec *)odr_malloc (assoc->encode, sizeof(*drec));
944 Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)odr_malloc (assoc->encode, sizeof(*dr));
946 bib1.proto = assoc->proto;
947 bib1.oclass = CLASS_DIAGSET;
948 bib1.value = VAL_BIB1;
950 logf(LOG_DEBUG, "SurrogateDiagnotic: %d -- %s", error, addinfo);
952 rec->databaseName = dbname;
953 rec->which = Z_NamePlusRecord_surrogateDiagnostic;
954 rec->u.surrogateDiagnostic = drec;
955 drec->which = Z_DiagRec_defaultFormat;
956 drec->u.defaultFormat = dr;
957 dr->diagnosticSetId = odr_oiddup (assoc->encode,
958 oid_ent_to_oid(&bib1, oid));
960 set_addinfo (dr, addinfo);
966 * multiple nonsurrogate diagnostics.
968 static Z_DiagRecs *diagrecs(association *assoc, int error, char *addinfo)
971 Z_DiagRecs *recs = (Z_DiagRecs *)odr_malloc (assoc->encode, sizeof(*recs));
972 int *err = (int *)odr_malloc (assoc->encode, sizeof(*err));
974 Z_DiagRec **recp = (Z_DiagRec **)odr_malloc (assoc->encode, sizeof(*recp));
975 Z_DiagRec *drec = (Z_DiagRec *)odr_malloc (assoc->encode, sizeof(*drec));
976 Z_DefaultDiagFormat *rec = (Z_DefaultDiagFormat *)odr_malloc (assoc->encode, sizeof(*rec));
978 logf(LOG_DEBUG, "DiagRecs: %d -- %s", error, addinfo ? addinfo : "");
979 bib1.proto = assoc->proto;
980 bib1.oclass = CLASS_DIAGSET;
981 bib1.value = VAL_BIB1;
984 recs->num_diagRecs = 1;
985 recs->diagRecs = recp;
987 drec->which = Z_DiagRec_defaultFormat;
988 drec->u.defaultFormat = rec;
990 rec->diagnosticSetId = odr_oiddup (assoc->encode,
991 oid_ent_to_oid(&bib1, oid));
992 rec->condition = err;
995 rec->which = Z_DefaultDiagFormat_v2Addinfo;
996 rec->u.v2Addinfo = addinfo ? addinfo : "";
998 rec->which = Z_DiagForm_v2AddInfo;
999 rec->addinfo = addinfo ? addinfo : "";
1004 static Z_Records *pack_records(association *a, char *setname, int start,
1005 int *num, Z_RecordComposition *comp,
1006 int *next, int *pres, oid_value format)
1009 int recno, total_length = 0, toget = *num, dumped_records = 0;
1010 Z_Records *records = (Z_Records *)odr_malloc (a->encode, sizeof(*records));
1011 Z_NamePlusRecordList *reclist = (Z_NamePlusRecordList *)odr_malloc (a->encode, sizeof(*reclist));
1012 Z_NamePlusRecord **list = (Z_NamePlusRecord **)odr_malloc (a->encode, sizeof(*list) * toget);
1015 records->which = Z_Records_DBOSD;
1016 records->u.databaseOrSurDiagnostics = reclist;
1017 reclist->num_records = 0;
1018 reclist->records = list;
1019 *pres = Z_PRES_SUCCESS;
1023 logf(LOG_DEBUG, "Request to pack %d+%d", start, toget);
1024 logf(LOG_DEBUG, "pms=%d, mrs=%d", a->preferredMessageSize,
1025 a->maximumRecordSize);
1026 for (recno = start; reclist->num_records < toget; recno++)
1028 bend_fetchrequest freq;
1029 bend_fetchresult *fres;
1030 Z_NamePlusRecord *thisrec;
1031 Z_DatabaseRecord *thisext;
1032 int this_length = 0;
1035 * we get the number of bytes allocated on the stream before any
1036 * allocation done by the backend - this should give us a reasonable
1037 * idea of the total size of the data so far.
1039 total_length = odr_total(a->encode) - dumped_records;
1040 freq.setname = setname;
1041 freq.number = recno;
1043 freq.format = format;
1044 freq.stream = a->encode;
1045 freq.surrogate_flag = 0;
1046 if (!(fres = bend_fetch(a->backend, &freq, 0)))
1048 *pres = Z_PRES_FAILURE;
1049 return diagrec(a, 2, "Backend interface problem");
1051 /* backend should be able to signal whether error is system-wide
1052 or only pertaining to current record */
1055 if (!freq.surrogate_flag)
1057 *pres = Z_PRES_FAILURE;
1058 return diagrec(a, fres->errcode, fres->errstring);
1060 reclist->records[reclist->num_records] =
1061 surrogatediagrec(a, fres->basename, fres->errcode,
1063 reclist->num_records++;
1064 *next = fres->last_in_set ? 0 : recno + 1;
1068 this_length = fres->len;
1070 this_length = odr_total(a->encode) - total_length;
1071 logf(LOG_DEBUG, " fetched record, len=%d, total=%d",
1072 this_length, total_length);
1073 if (this_length + total_length > a->preferredMessageSize)
1075 /* record is small enough, really */
1076 if (this_length <= a->preferredMessageSize)
1078 logf(LOG_DEBUG, " Dropped last normal-sized record");
1079 *pres = Z_PRES_PARTIAL_2;
1082 /* record can only be fetched by itself */
1083 if (this_length < a->maximumRecordSize)
1085 logf(LOG_DEBUG, " Record > prefmsgsz");
1088 logf(LOG_DEBUG, " Dropped it");
1089 reclist->records[reclist->num_records] =
1090 surrogatediagrec(a, fres->basename, 16, 0);
1091 reclist->num_records++;
1092 *next = fres->last_in_set ? 0 : recno + 1;
1093 dumped_records += this_length;
1097 else /* too big entirely */
1099 logf(LOG_DEBUG, "Record > maxrcdsz");
1100 reclist->records[reclist->num_records] =
1101 surrogatediagrec(a, fres->basename, 17, 0);
1102 reclist->num_records++;
1103 *next = fres->last_in_set ? 0 : recno + 1;
1104 dumped_records += this_length;
1109 if (!(thisrec = (Z_NamePlusRecord *)odr_malloc(a->encode, sizeof(*thisrec))))
1111 if (!(thisrec->databaseName = (char *)odr_malloc(a->encode,
1112 strlen(fres->basename) + 1)))
1114 strcpy(thisrec->databaseName, fres->basename);
1115 thisrec->which = Z_NamePlusRecord_databaseRecord;
1116 if (!(thisrec->u.databaseRecord = thisext = (Z_External *)odr_malloc(a->encode,
1117 sizeof(Z_DatabaseRecord))))
1119 recform.proto = a->proto;
1120 recform.oclass = CLASS_RECSYN;
1121 recform.value = fres->format;
1122 thisext->direct_reference = odr_oiddup(a->encode,
1123 oid_ent_to_oid(&recform, oid));
1124 thisext->indirect_reference = 0;
1125 thisext->descriptor = 0;
1126 if (fres->len < 0) /* Structured data */
1128 switch (fres->format)
1130 case VAL_SUTRS: thisext->which = Z_External_sutrs; break;
1131 case VAL_GRS1: thisext->which = Z_External_grs1; break;
1132 case VAL_EXPLAIN: thisext->which = Z_External_explainRecord;
1134 case VAL_SUMMARY: thisext->which = Z_External_summary; break;
1135 case VAL_OPAC: thisext->which = Z_External_OPAC; break;
1138 logf(LOG_FATAL, "Unknown structured format from backend.");
1143 * We cheat on the pointers here. Obviously, the record field
1144 * of the backend-fetch structure should have been a union for
1145 * correctness, but we're stuck with this for backwards
1148 thisext->u.grs1 = (Z_GenericRecord*) fres->record;
1150 else if (fres->format == VAL_SUTRS) /* SUTRS is a single-ASN.1-type */
1153 Z_SUTRS *sutrs = (Z_SUTRS *)odr_malloc(a->encode, 1+fres->len);
1155 memcpy(sutrs, fres->record, fres->len);
1156 sutrs[fres->len] = '\0';
1158 Odr_oct *sutrs = (Odr_oct *)odr_malloc(a->encode, sizeof(*sutrs));
1160 thisext->which = Z_External_sutrs;
1161 thisext->u.sutrs = sutrs;
1162 sutrs->buf = (unsigned char *)odr_malloc(a->encode, fres->len);
1163 sutrs->len = sutrs->size = fres->len;
1164 memcpy(sutrs->buf, fres->record, fres->len);
1167 else /* octet-aligned record. */
1169 thisext->which = Z_External_octet;
1170 if (!(thisext->u.octet_aligned = (Odr_oct *)odr_malloc(a->encode,
1173 if (!(thisext->u.octet_aligned->buf = (unsigned char *)odr_malloc(a->encode,
1176 memcpy(thisext->u.octet_aligned->buf, fres->record, fres->len);
1177 thisext->u.octet_aligned->len = thisext->u.octet_aligned->size =
1180 reclist->records[reclist->num_records] = thisrec;
1181 reclist->num_records++;
1182 *next = fres->last_in_set ? 0 : recno + 1;
1184 *num = reclist->num_records;
1188 static Z_APDU *process_searchRequest(association *assoc, request *reqb,
1191 Z_SearchRequest *req = reqb->request->u.searchRequest;
1192 bend_search_rr *bsrr = (bend_search_rr *)nmem_malloc (reqb->request_mem, sizeof(*bsrr));
1194 logf(LOG_LOG, "Got SearchRequest.");
1195 save_referenceId (reqb, req->referenceId);
1196 /* store ref id in request */
1198 bsrr->request = reqb;
1199 bsrr->association = assoc;
1200 if (assoc->bend_search)
1202 bsrr->setname = req->resultSetName;
1203 bsrr->replace_set = *req->replaceIndicator;
1204 bsrr->num_bases = req->num_databaseNames;
1205 bsrr->basenames = req->databaseNames;
1206 bsrr->query = req->query;
1207 bsrr->stream = assoc->encode;
1208 bsrr->decode = assoc->decode;
1211 bsrr->errstring = NULL;
1212 ((int (*)(void *, bend_search_rr *))(assoc->bend_search))(assoc->backend, bsrr);
1218 bend_searchrequest bsrq;
1219 bend_searchresult *bsrt;
1221 bsrq.setname = req->resultSetName;
1222 bsrq.replace_set = *req->replaceIndicator;
1223 bsrq.num_bases = req->num_databaseNames;
1224 bsrq.basenames = req->databaseNames;
1225 bsrq.query = req->query;
1226 bsrq.stream = assoc->encode;
1227 bsrq.decode = assoc->decode;
1228 if (!(bsrt = bend_search(assoc->backend, &bsrq, fd)))
1230 bsrr->hits = bsrt->hits;
1231 bsrr->errcode = bsrt->errcode;
1232 bsrr->errstring = bsrt->errstring;
1234 return response_searchRequest(assoc, reqb, bsrr, fd);
1237 int bend_searchresponse(void *handle, bend_search_rr *bsrr) {return 0;}
1240 * Prepare a searchresponse based on the backend results. We probably want
1241 * to look at making the fetching of records nonblocking as well, but
1242 * so far, we'll keep things simple.
1243 * If bsrt is null, that means we're called in response to a communications
1244 * event, and we'll have to get the response for ourselves.
1246 static Z_APDU *response_searchRequest(association *assoc, request *reqb,
1247 bend_search_rr *bsrt, int *fd)
1249 Z_SearchRequest *req = reqb->request->u.searchRequest;
1250 Z_APDU *apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1251 Z_SearchResponse *resp = (Z_SearchResponse *)odr_malloc (assoc->encode, sizeof(*resp));
1252 int *nulint = (int *)odr_malloc (assoc->encode, sizeof(*nulint));
1253 bool_t *sr = (bool_t *)odr_malloc (assoc->encode, sizeof(*sr));
1254 int *next = (int *)odr_malloc (assoc->encode, sizeof(*next));
1255 int *none = (int *)odr_malloc (assoc->encode, sizeof(*none));
1262 apdu->which = Z_APDU_searchResponse;
1263 apdu->u.searchResponse = resp;
1264 resp->referenceId = req->referenceId;
1265 resp->additionalSearchInfo = 0;
1266 resp->otherInfo = 0;
1268 if (!bsrt && !bend_searchresponse(assoc->backend, bsrt))
1270 logf(LOG_FATAL, "Bad result from backend");
1273 else if (bsrt->errcode)
1275 resp->records = diagrec(assoc, bsrt->errcode, bsrt->errstring);
1276 resp->resultCount = nulint;
1277 resp->numberOfRecordsReturned = nulint;
1278 resp->nextResultSetPosition = nulint;
1279 resp->searchStatus = nulint;
1280 resp->resultSetStatus = none;
1281 resp->presentStatus = 0;
1285 int *toget = (int *)odr_malloc (assoc->encode, sizeof(*toget));
1286 int *presst = (int *)odr_malloc (assoc->encode, sizeof(*presst));
1287 Z_RecordComposition comp, *compp = 0;
1292 resp->resultCount = &bsrt->hits;
1294 comp.which = Z_RecordComp_simple;
1295 /* how many records does the user agent want, then? */
1296 if (bsrt->hits <= *req->smallSetUpperBound)
1298 *toget = bsrt->hits;
1299 if ((comp.u.simple = req->smallSetElementSetNames))
1302 else if (bsrt->hits < *req->largeSetLowerBound)
1304 *toget = *req->mediumSetPresentNumber;
1305 if (*toget > bsrt->hits)
1306 *toget = bsrt->hits;
1307 if ((comp.u.simple = req->mediumSetElementSetNames))
1313 if (*toget && !resp->records)
1318 if (!(prefformat = oid_getentbyoid(req->preferredRecordSyntax)) ||
1319 prefformat->oclass != CLASS_RECSYN)
1322 form = prefformat->value;
1323 resp->records = pack_records(assoc, req->resultSetName, 1,
1324 toget, compp, next, presst, form);
1327 resp->numberOfRecordsReturned = toget;
1328 resp->nextResultSetPosition = next;
1329 resp->searchStatus = sr;
1330 resp->resultSetStatus = 0;
1331 resp->presentStatus = presst;
1335 if (*resp->resultCount)
1337 resp->numberOfRecordsReturned = nulint;
1338 resp->nextResultSetPosition = next;
1339 resp->searchStatus = sr;
1340 resp->resultSetStatus = 0;
1341 resp->presentStatus = 0;
1348 * Maybe we got a little over-friendly when we designed bend_fetch to
1349 * get only one record at a time. Some backends can optimise multiple-record
1350 * fetches, and at any rate, there is some overhead involved in
1351 * all that selecting and hopping around. Problem is, of course, that the
1352 * frontend can't know ahead of time how many records it'll need to
1353 * fill the negotiated PDU size. Annoying. Segmentation or not, Z/SR
1354 * is downright lousy as a bulk data transfer protocol.
1356 * To start with, we'll do the fetching of records from the backend
1357 * in one operation: To save some trips in and out of the event-handler,
1358 * and to simplify the interface to pack_records. At any rate, asynch
1359 * operation is more fun in operations that have an unpredictable execution
1360 * speed - which is normally more true for search than for present.
1362 static Z_APDU *process_presentRequest(association *assoc, request *reqb,
1365 Z_PresentRequest *req = reqb->request->u.presentRequest;
1369 Z_PresentResponse *resp;
1374 logf(LOG_LOG, "Got PresentRequest.");
1376 if (!(prefformat = oid_getentbyoid(req->preferredRecordSyntax)) ||
1377 prefformat->oclass != CLASS_RECSYN)
1380 form = prefformat->value;
1381 if (assoc->bend_present)
1383 bend_present_rr *bprr = (bend_present_rr *)nmem_malloc (reqb->request_mem, sizeof(*bprr));
1384 bprr->setname = req->resultSetId;
1385 bprr->start = *req->resultSetStartPoint;
1386 bprr->number = *req->numberOfRecordsRequested;
1387 bprr->format = form;
1388 bprr->comp = req->recordComposition;
1389 bprr->stream = assoc->encode;
1390 bprr->request = reqb;
1391 bprr->association = assoc;
1393 bprr->errstring = NULL;
1394 ((int (*)(void *, bend_present_rr *))(*assoc->bend_present))(assoc->backend, bprr);
1399 apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1400 resp = (Z_PresentResponse *)odr_malloc (assoc->encode, sizeof(*resp));
1401 presst = (int *)odr_malloc (assoc->encode, sizeof(*presst));
1402 next = (int *)odr_malloc (assoc->encode, sizeof(*next));
1403 num = (int *)odr_malloc (assoc->encode, sizeof(*num));
1406 *num = *req->numberOfRecordsRequested;
1408 apdu->which = Z_APDU_presentResponse;
1409 apdu->u.presentResponse = resp;
1410 resp->referenceId = req->referenceId;
1411 resp->otherInfo = 0;
1414 pack_records(assoc, req->resultSetId, *req->resultSetStartPoint,
1415 num, req->recordComposition, next, presst, form);
1418 resp->numberOfRecordsReturned = num;
1419 resp->presentStatus = presst;
1420 resp->nextResultSetPosition = next;
1426 * Scan was implemented rather in a hurry, and with support for only the basic
1427 * elements of the service in the backend API. Suggestions are welcome.
1429 static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd)
1431 Z_ScanRequest *req = reqb->request->u.scanRequest;
1432 Z_APDU *apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1433 Z_ScanResponse *res = (Z_ScanResponse *)odr_malloc (assoc->encode, sizeof(*res));
1434 int *scanStatus = (int *)odr_malloc (assoc->encode, sizeof(*scanStatus));
1435 int *numberOfEntriesReturned =
1436 (int *)odr_malloc (assoc->encode, sizeof(*numberOfEntriesReturned));
1437 Z_ListEntries *ents = (Z_ListEntries *)odr_malloc (assoc->encode, sizeof(*ents));
1438 Z_DiagRecs *diagrecs_p = NULL;
1440 bend_scanrequest srq;
1441 bend_scanresult *srs;
1444 logf(LOG_LOG, "Got ScanRequest");
1445 *scanStatus = Z_Scan_failure;
1446 *numberOfEntriesReturned = 0;
1448 apdu->which = Z_APDU_scanResponse;
1449 apdu->u.scanResponse = res;
1450 res->referenceId = req->referenceId;
1452 res->scanStatus = scanStatus;
1453 res->numberOfEntriesReturned = numberOfEntriesReturned;
1454 res->positionOfTerm = 0;
1455 res->entries = ents;
1456 ents->num_entries = 0;
1457 ents->entries = NULL;
1458 ents->num_nonsurrogateDiagnostics = 0;
1459 ents->nonsurrogateDiagnostics = NULL;
1460 res->attributeSet = 0;
1463 if (req->attributeSet && (!(attent = oid_getentbyoid(req->attributeSet)) ||
1464 attent->oclass != CLASS_ATTSET
1465 || attent->value != VAL_BIB1))
1466 diagrecs_p = diagrecs(assoc, 121, 0);
1467 else if (req->stepSize && *req->stepSize > 0)
1468 diagrecs_p = diagrecs(assoc, 205, 0);
1471 if (req->termListAndStartPoint->term->which == Z_Term_general)
1472 logf(LOG_DEBUG, " term: '%.*s'",
1473 req->termListAndStartPoint->term->u.general->len,
1474 req->termListAndStartPoint->term->u.general->buf);
1475 srq.num_bases = req->num_databaseNames;
1476 srq.basenames = req->databaseNames;
1477 srq.num_entries = *req->numberOfTermsRequested;
1478 srq.term = req->termListAndStartPoint;
1479 srq.stream = assoc->encode;
1480 if (!(attset = oid_getentbyoid(req->attributeSet)) ||
1481 attset->oclass != CLASS_RECSYN)
1482 srq.attributeset = VAL_NONE;
1484 srq.attributeset = attset->value;
1485 srq.term_position = req->preferredPositionInResponse ?
1486 *req->preferredPositionInResponse : 1;
1487 if (!(srs = bend_scan(assoc->backend, &srq, 0)))
1488 diagrecs_p = diagrecs(assoc, 2, 0);
1489 else if (srs->errcode)
1490 diagrecs_p = diagrecs(assoc, srs->errcode, srs->errstring);
1494 Z_Entry **tab = (Z_Entry **)
1495 odr_malloc (assoc->encode, sizeof(*tab) * srs->num_entries);
1497 if (srs->status == BEND_SCAN_PARTIAL)
1498 *scanStatus = Z_Scan_partial_5;
1500 *scanStatus = Z_Scan_success;
1501 ents->entries = tab;
1502 ents->num_entries = srs->num_entries;
1503 res->numberOfEntriesReturned = &ents->num_entries;
1504 res->positionOfTerm = &srs->term_position;
1505 for (i = 0; i < srs->num_entries; i++)
1511 tab[i] = e = (Z_Entry *)odr_malloc(assoc->encode, sizeof(*e));
1512 if (srs->entries[i].occurrences >= 0)
1514 e->which = Z_Entry_termInfo;
1515 e->u.termInfo = t = (Z_TermInfo *)
1516 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 *)
1524 odr_malloc(assoc->encode, sizeof(*t->term));
1525 t->term->which = Z_Term_general;
1526 t->term->u.general = o =
1527 (Odr_oct *)odr_malloc(assoc->encode, sizeof(Odr_oct));
1528 o->buf = (unsigned char *)
1529 odr_malloc(assoc->encode, o->len = o->size =
1530 strlen(srs->entries[i].term));
1531 memcpy(o->buf, srs->entries[i].term, o->len);
1532 logf(LOG_DEBUG, " term #%d: '%s' (%d)", i,
1533 srs->entries[i].term, srs->entries[i].occurrences);
1537 Z_DiagRecs *drecs = diagrecs (assoc,
1538 srs->entries[i].errcode,
1539 srs->entries[i].errstring);
1540 assert (drecs->num_diagRecs == 1);
1541 e->which = Z_Entry_surrogateDiagnostic;
1542 assert (drecs->diagRecs[0]);
1543 e->u.surrogateDiagnostic = drecs->diagRecs[0];
1550 ents->num_nonsurrogateDiagnostics = diagrecs_p->num_diagRecs;
1551 ents->nonsurrogateDiagnostics = diagrecs_p->diagRecs;
1556 static Z_APDU *process_sortRequest(association *assoc, request *reqb,
1559 Z_SortRequest *req = reqb->request->u.sortRequest;
1560 Z_SortResponse *res = (Z_SortResponse *)odr_malloc (assoc->encode, sizeof(*res));
1561 bend_sort_rr *bsrr = (bend_sort_rr *)odr_malloc (assoc->encode, sizeof(*bsrr));
1563 Z_APDU *apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1565 logf(LOG_LOG, "Got SortRequest.");
1568 bsrr->num_input_setnames = req->num_inputResultSetNames;
1569 bsrr->input_setnames = req->inputResultSetNames;
1571 bsrr->num_input_setnames = req->inputResultSetNames->num_strings;
1572 bsrr->input_setnames = req->inputResultSetNames->strings;
1574 bsrr->output_setname = req->sortedResultSetName;
1575 bsrr->sort_sequence = req->sortSequence;
1576 bsrr->stream = assoc->encode;
1578 bsrr->sort_status = Z_SortStatus_failure;
1580 bsrr->errstring = 0;
1582 ((int (*)(void *, bend_sort_rr *))(*assoc->bend_sort))(assoc->backend, bsrr);
1584 res->referenceId = req->referenceId;
1585 res->sortStatus = (int *)odr_malloc (assoc->encode, sizeof(*res->sortStatus));
1586 *res->sortStatus = bsrr->sort_status;
1587 res->resultSetStatus = 0;
1590 Z_DiagRecs *dr = diagrecs (assoc, bsrr->errcode, bsrr->errstring);
1592 res->diagnostics = dr->diagRecs;
1593 res->num_diagnostics = dr->num_diagRecs;
1595 res->diagnostics = dr;
1601 res->num_diagnostics = 0;
1603 res->diagnostics = 0;
1607 apdu->which = Z_APDU_sortResponse;
1608 apdu->u.sortResponse = res;
1612 static Z_APDU *process_deleteRequest(association *assoc, request *reqb,
1615 Z_DeleteResultSetRequest *req = reqb->request->u.deleteResultSetRequest;
1616 Z_DeleteResultSetResponse *res = (Z_DeleteResultSetResponse *)
1617 odr_malloc (assoc->encode, sizeof(*res));
1618 bend_delete_rr *bdrr = (bend_delete_rr *)
1619 odr_malloc (assoc->encode, sizeof(*bdrr));
1620 Z_APDU *apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1622 logf(LOG_LOG, "Got DeleteRequest.");
1624 bdrr->num_setnames = req->num_resultSetList;
1625 bdrr->setnames = req->resultSetList;
1626 bdrr->stream = assoc->encode;
1627 bdrr->function = *req->deleteFunction;
1629 ((int (*)(void *, bend_delete_rr *))
1630 (*assoc->bend_delete))(assoc->backend, bdrr);
1632 res->referenceId = req->referenceId;
1634 res->deleteOperationStatus = (int *)
1635 odr_malloc (assoc->encode, sizeof(*res->deleteOperationStatus));
1636 *res->deleteOperationStatus = bdrr->delete_status;
1638 res->deleteListStatuses = 0;
1639 res->numberNotDeleted = 0;
1640 res->bulkStatuses = 0;
1641 res->deleteMessage = 0;
1644 apdu->which = Z_APDU_deleteResultSetResponse;
1645 apdu->u.deleteResultSetResponse = res;
1649 static void process_close(association *assoc, request *reqb)
1651 Z_Close *req = reqb->request->u.close;
1652 static char *reasons[] =
1659 "securityViolation",
1666 logf(LOG_LOG, "Got Close, reason %s, message %s",
1667 reasons[*req->closeReason], req->diagnosticInformation ?
1668 req->diagnosticInformation : "NULL");
1669 if (assoc->version < 3) /* to make do_force respond with close */
1671 do_close_req(assoc, Z_Close_finished, "Association terminated by client",
1675 void save_referenceId (request *reqb, Z_ReferenceId *refid)
1679 reqb->len_refid = refid->len;
1680 reqb->refid = (char *)nmem_malloc (reqb->request_mem, refid->len);
1681 memcpy (reqb->refid, refid->buf, refid->len);
1685 reqb->len_refid = 0;
1690 void bend_request_send (bend_association a, bend_request req, Z_APDU *res)
1692 process_response (a, req, res);
1695 bend_request bend_request_mk (bend_association a)
1697 request *nreq = request_get (&a->outgoing);
1698 nreq->request_mem = nmem_create ();
1702 Z_ReferenceId *bend_request_getid (ODR odr, bend_request req)
1707 id = (Odr_oct *)odr_malloc (odr, sizeof(*odr));
1708 id->buf = (unsigned char *)odr_malloc (odr, req->len_refid);
1709 id->len = id->size = req->len_refid;
1710 memcpy (id->buf, req->refid, req->len_refid);
1714 void bend_request_destroy (bend_request *req)
1716 nmem_destroy((*req)->request_mem);
1717 request_release(*req);
1721 int bend_backend_respond (bend_association a, bend_request req)
1723 return process_request (a, req);
1726 void bend_request_setdata(bend_request r, void *p)
1731 void *bend_request_getdata(bend_request r)
1733 return r->clientData;
1736 /* Chas: Added in from DALI */
1737 static Z_APDU *process_ESRequest(association *assoc, request *reqb, int *fd)
1739 bend_esrequest_rr esrequest;
1741 Z_ExtendedServicesRequest *req = reqb->request->u.extendedServicesRequest;
1742 Z_APDU *apdu = zget_APDU(assoc->encode, Z_APDU_extendedServicesResponse);
1744 Z_ExtendedServicesResponse *resp = apdu->u.extendedServicesResponse;
1746 logf(LOG_DEBUG,"inside Process esRequest");
1748 esrequest.esr = reqb->request->u.extendedServicesRequest;
1749 esrequest.stream = assoc->encode;
1750 esrequest.errcode = 0;
1751 esrequest.errstring = NULL;
1752 esrequest.request = reqb;
1753 esrequest.association = assoc;
1755 ((int (*)(void *, bend_esrequest_rr *))(*assoc->bend_esrequest))(assoc->backend,
1758 /* If the response is being delayed, return NULL */
1759 if (esrequest.request == NULL)
1762 resp->referenceId = req->referenceId;
1764 if ( esrequest.errcode == 0 )
1766 /* Backend service indicates request will be processed */
1767 logf(LOG_DEBUG,"Request will be processed...Good !");
1768 *resp->operationStatus = Z_ExtendedServicesResponse_done;
1772 /* Backend indicates error, request will not be processed */
1773 logf(LOG_DEBUG,"Request will not be processed...BAD !");
1774 *resp->operationStatus = Z_ExtendedServicesResponse_failure;
1776 /* Do something with the members of bend_extendedservice */
1778 logf(LOG_DEBUG,"Send the result apdu");
1783 /* Chas: End of addition from DALI */