2 * Copyright (c) 1995-1999, Index Data
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.88 1999-04-20 09:56:48 adam
8 * Added 'name' paramter to encoder/decoder routines (typedef Odr_fun).
9 * Modified all encoders/decoders to reflect this change.
11 * Revision 1.87 1999/03/31 11:18:25 adam
12 * Implemented odr_strdup. Added Reference ID to backend server API.
14 * Revision 1.86 1999/02/02 13:57:38 adam
15 * Uses preprocessor define WIN32 instead of WINDOWS to build code
16 * for Microsoft WIN32.
18 * Revision 1.85 1998/11/17 09:52:59 adam
19 * Fixed minor bug (introduced by previous commit).
21 * Revision 1.84 1998/11/16 16:02:32 adam
22 * Added loggin utilies, log_rpn_query and log_scan_term. These used
23 * to be part of Zebra.
25 * Revision 1.83 1998/11/03 10:09:36 adam
26 * Fixed bug regarding YC.
28 * Revision 1.82 1998/10/20 14:00:30 quinn
31 * Revision 1.81 1998/10/13 16:12:24 adam
32 * Added support for Surrogate Diagnostics for Scan Term entries.
34 * Revision 1.80 1998/09/02 12:41:53 adam
35 * Added decode stream in bend search structures.
37 * Revision 1.79 1998/08/19 16:10:08 adam
38 * Changed som member names of DeleteResultSetRequest/Response.
40 * Revision 1.78 1998/08/03 10:23:55 adam
41 * Fixed bug regarding Options for Sort.
43 * Revision 1.77 1998/07/20 12:38:42 adam
44 * Implemented delete result set service to server API.
46 * Revision 1.76 1998/05/27 16:57:07 adam
47 * Support for surrogate diagnostic records added for bend_fetch.
49 * Revision 1.75 1998/05/18 10:13:07 adam
50 * Fixed call to es_request handler - extra argument was passed.
52 * Revision 1.74 1998/03/31 15:13:20 adam
53 * Development towards compiled ASN.1.
55 * Revision 1.73 1998/03/31 11:07:45 adam
56 * Furhter work on UNIverse resource report.
57 * Added Extended Services handling in frontend server.
59 * Revision 1.72 1998/02/11 11:53:35 adam
60 * Changed code so that it compiles as C++.
62 * Revision 1.71 1998/02/10 11:03:57 adam
63 * Added support for extended handlers in backend server interface.
65 * Revision 1.70 1998/01/29 13:15:35 adam
66 * Implemented sort for the backend interface.
68 * Revision 1.69 1997/09/30 11:48:12 adam
69 * Fixed bug introduced by previous commit.
71 * Revision 1.68 1997/09/29 13:18:59 adam
72 * Added function, oid_ent_to_oid, to replace the function
73 * oid_getoidbyent, which is not thread safe.
75 * Revision 1.67 1997/09/17 12:10:40 adam
78 * Revision 1.66 1997/09/05 15:26:44 adam
79 * Added ODR encode in search and scen bend request structures.
80 * Fixed a few enums that caused trouble with C++.
82 * Revision 1.65 1997/09/01 08:53:01 adam
83 * New windows NT/95 port using MSV5.0. The test server 'ztest' was
84 * moved a separate directory. MSV5.0 project server.dsp created.
85 * As an option, the server can now operate as an NT service.
87 * Revision 1.64 1997/04/30 08:52:11 quinn
90 * Revision 1.63 1996/10/11 11:57:26 quinn
93 * Revision 1.62 1996/07/06 19:58:35 quinn
94 * System headerfiles gathered in yconfig
96 * Revision 1.61 1996/06/10 08:56:16 quinn
99 * Revision 1.60 1996/05/30 11:03:10 quinn
100 * Fixed NextresultSetPosition bug fixed.
102 * Revision 1.59 1996/05/14 09:26:46 quinn
103 * Added attribute set to scan backend
105 * Revision 1.58 1996/02/20 12:53:04 quinn
108 * Revision 1.57 1996/01/02 08:57:47 quinn
109 * Changed enums in the ASN.1 .h files to #defines. Changed oident.class to oclass
111 * Revision 1.56 1995/12/14 11:09:57 quinn
114 * Revision 1.55 1995/11/08 17:41:37 quinn
117 * Revision 1.54 1995/11/08 15:11:29 quinn
118 * Log of close transmit.
120 * Revision 1.53 1995/11/01 13:54:58 quinn
123 * Revision 1.52 1995/11/01 12:19:13 quinn
124 * Second attempt to fix same bug.
126 * Revision 1.50 1995/10/25 16:58:32 quinn
129 * Revision 1.49 1995/10/16 13:51:53 quinn
130 * Changes to provide Especs to the backend.
132 * Revision 1.48 1995/10/06 08:51:20 quinn
133 * Added Write-buffer.
135 * Revision 1.47 1995/08/29 14:24:16 quinn
136 * Added second half of close-handshake
138 * Revision 1.46 1995/08/29 11:17:58 quinn
139 * Added code to receive close
141 * Revision 1.45 1995/08/21 09:11:00 quinn
142 * Smallish fixes to suppport new formats.
144 * Revision 1.44 1995/08/17 12:45:25 quinn
145 * Fixed minor problems with GRS-1. Added support in c&s.
147 * Revision 1.43 1995/08/15 12:00:31 quinn
150 * Revision 1.42 1995/08/15 11:16:50 quinn
152 * Revision 1.41 1995/08/02 10:23:06 quinn
155 * Revision 1.40 1995/07/31 14:34:26 quinn
156 * Fixed bug in process_searchResponse (numberOfRecordsReturned).
158 * Revision 1.39 1995/06/27 13:21:00 quinn
161 * Revision 1.38 1995/06/19 12:39:11 quinn
162 * Fixed bug in timeout code. Added BER dumper.
164 * Revision 1.37 1995/06/16 13:16:14 quinn
165 * Fixed Defaultdiagformat.
167 * Revision 1.36 1995/06/16 10:31:36 quinn
168 * Added session timeout.
170 * Revision 1.35 1995/06/15 07:45:14 quinn
173 * Revision 1.34 1995/06/14 15:26:46 quinn
174 * *** empty log message ***
176 * Revision 1.33 1995/06/06 14:57:05 quinn
177 * Better diagnostics.
179 * Revision 1.32 1995/06/06 08:41:44 quinn
180 * Better diagnostics.
182 * Revision 1.31 1995/06/06 08:15:37 quinn
185 * Revision 1.30 1995/06/05 10:53:32 quinn
186 * Added a better SCAN.
188 * Revision 1.29 1995/06/01 11:25:03 quinn
191 * Revision 1.28 1995/06/01 11:21:01 quinn
192 * Attempting to fix a bug in pack-records. replaced break with continue
193 * for large records, according to standard.
195 * Revision 1.27 1995/05/29 08:12:06 quinn
198 * Revision 1.26 1995/05/18 13:02:12 quinn
201 * Revision 1.25 1995/05/17 08:42:26 quinn
202 * Transfer auth info to backend. Allow backend to reject init gracefully.
204 * Revision 1.24 1995/05/16 08:51:04 quinn
205 * License, documentation, and memory fixes
207 * Revision 1.23 1995/05/15 13:25:10 quinn
210 * Revision 1.22 1995/05/15 11:56:39 quinn
211 * Asynchronous facilities. Restructuring of seshigh code.
213 * Revision 1.21 1995/05/02 08:53:19 quinn
214 * Trying in vain to fix comm with ISODE
216 * Revision 1.20 1995/04/20 15:13:00 quinn
219 * Revision 1.19 1995/04/18 08:15:34 quinn
220 * Added dynamic memory allocation on encoding (whew). Code is now somewhat
221 * neater. We'll make the same change for decoding one day.
223 * Revision 1.18 1995/04/17 11:28:25 quinn
226 * Revision 1.17 1995/04/10 10:23:36 quinn
227 * Some work to add scan and other things.
229 * Revision 1.16 1995/03/31 09:18:55 quinn
232 * Revision 1.15 1995/03/30 14:03:23 quinn
233 * Added RFC1006 as separate library
235 * Revision 1.14 1995/03/30 12:18:17 quinn
238 * Revision 1.13 1995/03/30 09:09:24 quinn
239 * Added state-handle and some support for asynchronous activities.
241 * Revision 1.12 1995/03/29 15:40:16 quinn
242 * Ongoing work. Statserv is now dynamic by default
244 * Revision 1.11 1995/03/28 09:16:21 quinn
245 * Added record packing to the search request
247 * Revision 1.10 1995/03/27 08:34:24 quinn
248 * Added dynamic server functionality.
249 * Released bindings to session.c (is now redundant)
251 * Revision 1.9 1995/03/22 15:01:26 quinn
252 * Adjusting record packing.
254 * Revision 1.8 1995/03/22 10:13:21 quinn
255 * Working on record packer
257 * Revision 1.7 1995/03/21 15:53:31 quinn
260 * Revision 1.6 1995/03/21 12:30:09 quinn
261 * Beginning to add support for record packing.
263 * Revision 1.5 1995/03/17 10:44:13 quinn
264 * Added catch of null-string in makediagrec
266 * Revision 1.4 1995/03/17 10:18:08 quinn
267 * Added memory management.
269 * Revision 1.3 1995/03/16 17:42:39 quinn
272 * Revision 1.2 1995/03/16 13:29:01 quinn
273 * Partitioned server.
275 * Revision 1.1 1995/03/15 16:02:10 quinn
276 * Modded session.c to seshigh.c
281 * Frontend server logic.
283 * This code receives incoming APDUs, and handles client requests by means
284 * of the backend API.
286 * Some of the code is getting quite involved, compared to simpler servers -
287 * primarily because it is asynchronous both in the communication with
288 * the user and the backend. We think the complexity will pay off in
289 * the form of greater flexibility when more asynchronous facilities
292 * Memory management has become somewhat involved. In the simple case, where
293 * only one PDU is pending at a time, it will simply reuse the same memory,
294 * once it has found its working size. When we enable multiple concurrent
295 * operations, perhaps even with multiple parallel calls to the backend, it
296 * will maintain a pool of buffers for encoding and decoding, trying to
297 * minimize memory allocation/deallocation during normal operation.
299 * TODOs include (and will be done in order of public interest):
301 * Support for EXPLAIN - provide simple meta-database system.
302 * Support for access control.
303 * Support for resource control.
304 * Support for extended services - primarily Item Order.
305 * Rest of Z39.50-1994
320 #include <comstack.h>
327 #include <statserv.h>
331 static int process_request(association *assoc, request *req);
332 void backend_response(IOCHAN i, int event);
333 static int process_response(association *assoc, request *req, Z_APDU *res);
334 static Z_APDU *process_initRequest(association *assoc, request *reqb);
335 static Z_APDU *process_searchRequest(association *assoc, request *reqb,
337 static Z_APDU *response_searchRequest(association *assoc, request *reqb,
338 bend_search_rr *bsrr, int *fd);
339 static Z_APDU *process_presentRequest(association *assoc, request *reqb,
341 static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd);
342 static Z_APDU *process_sortRequest(association *assoc, request *reqb, int *fd);
343 static void process_close(association *assoc, request *reqb);
344 void save_referenceId (request *reqb, Z_ReferenceId *refid);
345 static Z_APDU *process_deleteRequest(association *assoc, request *reqb,
348 static FILE *apduf = 0; /* for use in static mode */
349 static statserv_options_block *control_block = 0;
351 /* Chas: Added in from DALI */
352 static Z_APDU *process_ESRequest(association *assoc, request *reqb, int *fd);
353 /* Chas: End of addition from DALI */
356 * Create and initialize a new association-handle.
357 * channel : iochannel for the current line.
358 * link : communications channel.
359 * Returns: 0 or a new association handle.
361 association *create_association(IOCHAN channel, COMSTACK link)
366 control_block = statserv_getcontrol();
367 if (!(anew = (association *)xmalloc(sizeof(*anew))))
369 anew->client_chan = channel;
370 anew->client_link = link;
371 if (!(anew->decode = odr_createmem(ODR_DECODE)) ||
372 !(anew->encode = odr_createmem(ODR_ENCODE)))
374 if (*control_block->apdufile)
379 strcpy(filename, control_block->apdufile);
380 if (!(anew->print = odr_createmem(ODR_PRINT)))
382 if (*control_block->apdufile != '-')
384 strcpy(filename, control_block->apdufile);
385 if (!control_block->dynamic)
389 if (!(apduf = fopen(filename, "w")))
391 logf(LOG_WARN|LOG_ERRNO, "%s", filename);
394 setvbuf(apduf, 0, _IONBF, 0);
400 sprintf(filename + strlen(filename), ".%d", getpid());
401 if (!(f = fopen(filename, "w")))
403 logf(LOG_WARN|LOG_ERRNO, "%s", filename);
406 setvbuf(f, 0, _IONBF, 0);
408 odr_setprint(anew->print, f);
413 anew->input_buffer = 0;
414 anew->input_buffer_len = 0;
416 anew->state = ASSOC_NEW;
417 request_initq(&anew->incoming);
418 request_initq(&anew->outgoing);
419 anew->proto = cs_getproto(link);
424 * Free association and release resources.
426 void destroy_association(association *h)
428 odr_destroy(h->decode);
429 odr_destroy(h->encode);
431 odr_destroy(h->print);
433 xfree(h->input_buffer);
435 bend_close(h->backend);
436 while (request_deq(&h->incoming));
437 while (request_deq(&h->outgoing));
438 request_delq(&h->incoming);
439 request_delq(&h->outgoing);
443 static void do_close_req(association *a, int reason, char *message,
447 Z_Close *cls = zget_Close(a->encode);
449 /* Purge request queue */
450 while (request_deq(&a->incoming));
451 while (request_deq(&a->outgoing));
454 logf(LOG_LOG, "Sending Close PDU, reason=%d, message=%s",
455 reason, message ? message : "none");
456 apdu.which = Z_APDU_close;
458 *cls->closeReason = reason;
459 cls->diagnosticInformation = message;
460 process_response(a, req, &apdu);
461 iochan_settimeout(a->client_chan, 60);
465 logf(LOG_DEBUG, "v2 client. No Close PDU");
466 iochan_setevent(a->client_chan, EVENT_TIMEOUT); /* force imm close */
468 a->state = ASSOC_DEAD;
471 static void do_close(association *a, int reason, char *message)
473 do_close_req (a, reason, message, request_get(&a->outgoing));
477 * This is where PDUs from the client are read and the further
478 * processing is initiated. Flow of control moves down through the
479 * various process_* functions below, until the encoded result comes back up
480 * to the output handler in here.
482 * h : the I/O channel that has an outstanding event.
483 * event : the current outstanding event.
485 void ir_session(IOCHAN h, int event)
488 association *assoc = (association *)iochan_getdata(h);
489 COMSTACK conn = assoc->client_link;
492 assert(h && conn && assoc);
493 if (event == EVENT_TIMEOUT)
495 if (assoc->state != ASSOC_UP)
497 logf(LOG_LOG, "Final timeout - closing connection.");
499 destroy_association(assoc);
504 logf(LOG_LOG, "Session idle too long. Sending close.");
505 do_close(assoc, Z_Close_lackOfActivity, 0);
509 if (event & EVENT_INPUT || event & EVENT_WORK) /* input */
511 if (event & EVENT_INPUT)
513 logf(LOG_DEBUG, "ir_session (input)");
514 assert(assoc && conn);
515 /* We aren't speaking to this fellow */
516 if (assoc->state == ASSOC_DEAD)
518 logf(LOG_LOG, "Closed connection after reject");
520 destroy_association(assoc);
524 if ((res = cs_get(conn, &assoc->input_buffer,
525 &assoc->input_buffer_len)) <= 0)
527 logf(LOG_LOG, "Connection closed by client");
529 destroy_association(assoc);
533 else if (res == 1) /* incomplete read - wait for more */
535 if (cs_more(conn)) /* more stuff - call us again later, please */
536 iochan_setevent(h, EVENT_INPUT);
538 /* we got a complete PDU. Let's decode it */
539 logf(LOG_DEBUG, "Got PDU, %d bytes", res);
540 req = request_get(&assoc->incoming); /* get a new request structure */
541 odr_reset(assoc->decode);
542 odr_setbuf(assoc->decode, assoc->input_buffer, res, 0);
543 if (!z_APDU(assoc->decode, &req->request, 0, 0))
545 logf(LOG_LOG, "ODR error on incoming PDU: %s [near byte %d] ",
546 odr_errmsg(odr_geterror(assoc->decode)),
547 odr_offset(assoc->decode));
548 logf(LOG_LOG, "PDU dump:");
549 odr_dumpBER(log_file(), assoc->input_buffer, res);
550 do_close(assoc, Z_Close_protocolError, "Malformed package");
553 req->request_mem = odr_extract_mem(assoc->decode);
554 if (assoc->print && !z_APDU(assoc->print, &req->request, 0, 0))
556 logf(LOG_WARN, "ODR print error: %s",
557 odr_errmsg(odr_geterror(assoc->print)));
558 odr_reset(assoc->print);
560 request_enq(&assoc->incoming, req);
563 /* can we do something yet? */
564 req = request_head(&assoc->incoming);
565 if (req->state == REQUEST_IDLE)
567 request_deq(&assoc->incoming);
568 if (process_request(assoc, req) < 0)
569 do_close_req(assoc, Z_Close_systemProblem, "Unknown error",
573 if (event & EVENT_OUTPUT)
575 request *req = request_head(&assoc->outgoing);
577 logf(LOG_DEBUG, "ir_session (output)");
578 req->state = REQUEST_PENDING;
579 switch (res = cs_put(conn, req->response, req->len_response))
582 logf(LOG_LOG, "Connection closed by client");
584 destroy_association(assoc);
587 case 0: /* all sent - release the request structure */
588 logf(LOG_DEBUG, "Wrote PDU, %d bytes", req->len_response);
589 nmem_destroy(req->request_mem);
590 request_deq(&assoc->outgoing);
591 request_release(req);
592 if (!request_head(&assoc->outgoing))
593 iochan_clearflag(h, EVENT_OUTPUT);
595 /* value of 1 -- partial send -- is simply ignored */
598 if (event & EVENT_EXCEPT)
600 logf(LOG_DEBUG, "ir_session (exception)");
602 destroy_association(assoc);
608 * Initiate request processing.
610 static int process_request(association *assoc, request *req)
616 assert(req && req->state == REQUEST_IDLE);
617 switch (req->request->which)
619 case Z_APDU_initRequest:
620 res = process_initRequest(assoc, req); break;
621 case Z_APDU_searchRequest:
622 res = process_searchRequest(assoc, req, &fd); break;
623 case Z_APDU_presentRequest:
624 res = process_presentRequest(assoc, req, &fd); break;
625 case Z_APDU_scanRequest:
626 res = process_scanRequest(assoc, req, &fd); break;
627 case Z_APDU_extendedServicesRequest:
628 if (assoc->bend_esrequest)
629 res = process_ESRequest(assoc, req, &fd);
632 logf(LOG_WARN, "Cannot handle EXTENDED SERVICES APDU");
636 case Z_APDU_sortRequest:
637 if (assoc->bend_sort)
638 res = process_sortRequest(assoc, req, &fd);
641 logf(LOG_WARN, "Cannot handle SORT APDU");
646 process_close(assoc, req); return 0;
647 case Z_APDU_deleteResultSetRequest:
648 if (assoc->bend_delete)
649 res = process_deleteRequest(assoc, req, &fd);
652 logf (LOG_WARN, "Cannot handle Delete APDU");
657 logf(LOG_WARN, "Bad APDU received");
662 logf(LOG_DEBUG, " result immediately available");
663 retval = process_response(assoc, req, res);
667 logf(LOG_DEBUG, " result unavailble");
670 else /* no result yet - one will be provided later */
674 /* Set up an I/O handler for the fd supplied by the backend */
676 logf(LOG_DEBUG, " establishing handler for result");
677 req->state = REQUEST_PENDING;
678 if (!(chan = iochan_create(fd, backend_response, EVENT_INPUT)))
680 iochan_setdata(chan, assoc);
687 * Handle message from the backend.
689 void backend_response(IOCHAN i, int event)
691 association *assoc = (association *)iochan_getdata(i);
692 request *req = request_head(&assoc->incoming);
696 logf(LOG_DEBUG, "backend_response");
697 assert(assoc && req && req->state != REQUEST_IDLE);
698 /* determine what it is we're waiting for */
699 switch (req->request->which)
701 case Z_APDU_searchRequest:
702 res = response_searchRequest(assoc, req, 0, &fd); break;
704 case Z_APDU_presentRequest:
705 res = response_presentRequest(assoc, req, 0, &fd); break;
706 case Z_APDU_scanRequest:
707 res = response_scanRequest(assoc, req, 0, &fd); break;
710 logf(LOG_WARN, "Serious programmer's lapse or bug");
713 if ((res && process_response(assoc, req, res) < 0) || fd < 0)
715 logf(LOG_LOG, "Fatal error when talking to backend");
716 do_close(assoc, Z_Close_systemProblem, 0);
720 else if (!res) /* no result yet - try again later */
722 logf(LOG_DEBUG, " no result yet");
723 iochan_setfd(i, fd); /* in case fd has changed */
728 * Encode response, and transfer the request structure to the outgoing queue.
730 static int process_response(association *assoc, request *req, Z_APDU *res)
732 odr_setbuf(assoc->encode, req->response, req->size_response, 1);
733 if (!z_APDU(assoc->encode, &res, 0, 0))
735 logf(LOG_WARN, "ODR error when encoding response: %s",
736 odr_errmsg(odr_geterror(assoc->decode)));
737 odr_reset(assoc->encode);
740 req->response = odr_getbuf(assoc->encode, &req->len_response,
741 &req->size_response);
742 odr_setbuf(assoc->encode, 0, 0, 0); /* don'txfree if we abort later */
743 if (assoc->print && !z_APDU(assoc->print, &res, 0, 0))
745 logf(LOG_WARN, "ODR print error: %s",
746 odr_errmsg(odr_geterror(assoc->print)));
747 odr_reset(assoc->print);
749 odr_reset(assoc->encode);
750 req->state = REQUEST_IDLE;
751 request_enq(&assoc->outgoing, req);
752 /* turn the work over to the ir_session handler */
753 iochan_setflag(assoc->client_chan, EVENT_OUTPUT);
754 /* Is there more work to be done? give that to the input handler too */
756 if (request_head(&assoc->incoming))
758 logf (LOG_DEBUG, "more work to be done");
759 iochan_setevent(assoc->client_chan, EVENT_WORK);
766 * Handle init request.
767 * At the moment, we don't check the options
768 * anywhere else in the code - we just try not to do anything that would
769 * break a naive client. We'll toss 'em into the association block when
770 * we need them there.
772 static Z_APDU *process_initRequest(association *assoc, request *reqb)
774 Z_InitRequest *req = reqb->request->u.initRequest;
775 Z_APDU *apdu = zget_APDU(assoc->encode, Z_APDU_initResponse);
776 Z_InitResponse *resp = apdu->u.initResponse;
777 bend_initrequest binitreq;
778 bend_initresult *binitres;
781 logf(LOG_LOG, "Got initRequest");
782 if (req->implementationId)
783 logf(LOG_LOG, "Id: %s", req->implementationId);
784 if (req->implementationName)
785 logf(LOG_LOG, "Name: %s", req->implementationName);
786 if (req->implementationVersion)
787 logf(LOG_LOG, "Version: %s", req->implementationVersion);
789 binitreq.stream = assoc->encode;
790 binitreq.configname = "default-config";
791 binitreq.auth = req->idAuthentication;
792 binitreq.referenceId = req->referenceId;
793 binitreq.bend_sort = NULL;
794 binitreq.bend_search = NULL;
795 binitreq.bend_present = NULL;
796 binitreq.bend_esrequest = NULL;
797 binitreq.bend_delete = NULL;
798 if (!(binitres = bend_init(&binitreq)))
800 logf(LOG_WARN, "Bad response from backend.");
804 assoc->backend = binitres->handle;
805 if ((assoc->bend_sort = (int (*)())binitreq.bend_sort))
806 logf (LOG_DEBUG, "Sort handler installed");
807 if ((assoc->bend_search = (int (*)())binitreq.bend_search))
808 logf (LOG_DEBUG, "Search handler installed");
809 if ((assoc->bend_present = (int (*)())binitreq.bend_present))
810 logf (LOG_DEBUG, "Present handler installed");
811 if ((assoc->bend_esrequest = (int (*)())binitreq.bend_esrequest))
812 logf (LOG_DEBUG, "ESRequest handler installed");
813 if ((assoc->bend_delete = (int (*)())binitreq.bend_delete))
814 logf (LOG_DEBUG, "Delete handler installed");
816 resp->referenceId = req->referenceId;
818 /* let's tell the client what we can do */
819 if (ODR_MASK_GET(req->options, Z_Options_search))
821 ODR_MASK_SET(resp->options, Z_Options_search);
822 strcat(options, "srch");
824 if (ODR_MASK_GET(req->options, Z_Options_present))
826 ODR_MASK_SET(resp->options, Z_Options_present);
827 strcat(options, " prst");
829 if (ODR_MASK_GET(req->options, Z_Options_delSet) && binitreq.bend_delete)
831 ODR_MASK_SET(resp->options, Z_Options_delSet);
832 strcat(options, " del");
834 if (ODR_MASK_GET(req->options, Z_Options_extendedServices) &&
835 binitreq.bend_esrequest)
837 ODR_MASK_SET(resp->options, Z_Options_extendedServices);
838 strcat (options, " extendedServices");
840 if (ODR_MASK_GET(req->options, Z_Options_namedResultSets))
842 ODR_MASK_SET(resp->options, Z_Options_namedResultSets);
843 strcat(options, " namedresults");
845 if (ODR_MASK_GET(req->options, Z_Options_scan))
847 ODR_MASK_SET(resp->options, Z_Options_scan);
848 strcat(options, " scan");
850 if (ODR_MASK_GET(req->options, Z_Options_concurrentOperations))
852 ODR_MASK_SET(resp->options, Z_Options_concurrentOperations);
853 strcat(options, " concurop");
855 if (ODR_MASK_GET(req->options, Z_Options_sort) && binitreq.bend_sort)
857 ODR_MASK_SET(resp->options, Z_Options_sort);
858 strcat(options, " sort");
860 if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_1))
862 ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_1);
863 assoc->version = 2; /* 1 & 2 are equivalent */
865 if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_2))
867 ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_2);
870 if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_3))
872 ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_3);
875 logf(LOG_LOG, "Negotiated to v%d: %s", assoc->version, options);
876 assoc->maximumRecordSize = *req->maximumRecordSize;
877 if (assoc->maximumRecordSize > control_block->maxrecordsize)
878 assoc->maximumRecordSize = control_block->maxrecordsize;
879 assoc->preferredMessageSize = *req->preferredMessageSize;
880 if (assoc->preferredMessageSize > assoc->maximumRecordSize)
881 assoc->preferredMessageSize = assoc->maximumRecordSize;
882 resp->preferredMessageSize = &assoc->preferredMessageSize;
883 resp->maximumRecordSize = &assoc->maximumRecordSize;
884 resp->implementationName = "Index Data/YAZ Generic Frontend Server";
885 if (binitres->errcode)
887 logf(LOG_LOG, "Connection rejected by backend.");
889 assoc->state = ASSOC_DEAD;
892 assoc->state = ASSOC_UP;
897 * These functions should be merged.
900 static void set_addinfo (Z_DefaultDiagFormat *dr, char *addinfo)
903 dr->which = Z_DefaultDiagFormat_v2Addinfo;
904 dr->u.v2Addinfo = addinfo ? addinfo : "";
906 dr->which = Z_DiagForm_v2AddInfo;
907 dr->addinfo = addinfo ? addinfo : "";
912 * nonsurrogate diagnostic record.
914 static Z_Records *diagrec(association *assoc, int error, char *addinfo)
917 Z_Records *rec = (Z_Records *)
918 odr_malloc (assoc->encode, sizeof(*rec));
921 odr_malloc (assoc->encode, sizeof(*err));
922 Z_DiagRec *drec = (Z_DiagRec *)
923 odr_malloc (assoc->encode, sizeof(*drec));
924 Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
925 odr_malloc (assoc->encode, sizeof(*dr));
927 bib1.proto = assoc->proto;
928 bib1.oclass = CLASS_DIAGSET;
929 bib1.value = VAL_BIB1;
931 logf(LOG_DEBUG, "Diagnostic: %d -- %s", error, addinfo ? addinfo :
934 rec->which = Z_Records_NSD;
936 rec->u.nonSurrogateDiagnostic = dr;
938 rec->u.nonSurrogateDiagnostic = drec;
939 drec->which = Z_DiagRec_defaultFormat;
940 drec->u.defaultFormat = dr;
942 dr->diagnosticSetId =
943 odr_oiddup (assoc->encode, oid_ent_to_oid(&bib1, oid));
945 set_addinfo (dr, addinfo);
950 * surrogate diagnostic.
952 static Z_NamePlusRecord *surrogatediagrec(association *assoc, char *dbname,
953 int error, char *addinfo)
956 Z_NamePlusRecord *rec = (Z_NamePlusRecord *)
957 odr_malloc (assoc->encode, sizeof(*rec));
958 int *err = (int *)odr_malloc (assoc->encode, sizeof(*err));
960 Z_DiagRec *drec = (Z_DiagRec *)odr_malloc (assoc->encode, sizeof(*drec));
961 Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
962 odr_malloc (assoc->encode, sizeof(*dr));
964 bib1.proto = assoc->proto;
965 bib1.oclass = CLASS_DIAGSET;
966 bib1.value = VAL_BIB1;
968 logf(LOG_DEBUG, "SurrogateDiagnotic: %d -- %s", error, addinfo);
970 rec->databaseName = dbname;
971 rec->which = Z_NamePlusRecord_surrogateDiagnostic;
972 rec->u.surrogateDiagnostic = drec;
973 drec->which = Z_DiagRec_defaultFormat;
974 drec->u.defaultFormat = dr;
975 dr->diagnosticSetId = odr_oiddup (assoc->encode,
976 oid_ent_to_oid(&bib1, oid));
978 set_addinfo (dr, addinfo);
984 * multiple nonsurrogate diagnostics.
986 static Z_DiagRecs *diagrecs(association *assoc, int error, char *addinfo)
989 Z_DiagRecs *recs = (Z_DiagRecs *)odr_malloc (assoc->encode, sizeof(*recs));
990 int *err = (int *)odr_malloc (assoc->encode, sizeof(*err));
992 Z_DiagRec **recp = (Z_DiagRec **)odr_malloc (assoc->encode, sizeof(*recp));
993 Z_DiagRec *drec = (Z_DiagRec *)odr_malloc (assoc->encode, sizeof(*drec));
994 Z_DefaultDiagFormat *rec = (Z_DefaultDiagFormat *)odr_malloc (assoc->encode, sizeof(*rec));
996 logf(LOG_DEBUG, "DiagRecs: %d -- %s", error, addinfo ? addinfo : "");
997 bib1.proto = assoc->proto;
998 bib1.oclass = CLASS_DIAGSET;
999 bib1.value = VAL_BIB1;
1002 recs->num_diagRecs = 1;
1003 recs->diagRecs = recp;
1005 drec->which = Z_DiagRec_defaultFormat;
1006 drec->u.defaultFormat = rec;
1008 rec->diagnosticSetId = odr_oiddup (assoc->encode,
1009 oid_ent_to_oid(&bib1, oid));
1010 rec->condition = err;
1013 rec->which = Z_DefaultDiagFormat_v2Addinfo;
1014 rec->u.v2Addinfo = addinfo ? addinfo : "";
1016 rec->which = Z_DiagForm_v2AddInfo;
1017 rec->addinfo = addinfo ? addinfo : "";
1022 static Z_Records *pack_records(association *a, char *setname, int start,
1023 int *num, Z_RecordComposition *comp,
1024 int *next, int *pres, oid_value format,
1025 Z_ReferenceId *referenceId)
1028 int recno, total_length = 0, toget = *num, dumped_records = 0;
1029 Z_Records *records =
1030 (Z_Records *) odr_malloc (a->encode, sizeof(*records));
1031 Z_NamePlusRecordList *reclist =
1032 (Z_NamePlusRecordList *) odr_malloc (a->encode, sizeof(*reclist));
1033 Z_NamePlusRecord **list =
1034 (Z_NamePlusRecord **) odr_malloc (a->encode, sizeof(*list) * toget);
1037 records->which = Z_Records_DBOSD;
1038 records->u.databaseOrSurDiagnostics = reclist;
1039 reclist->num_records = 0;
1040 reclist->records = list;
1041 *pres = Z_PRES_SUCCESS;
1045 logf(LOG_LOG, "Request to pack %d+%d", start, toget);
1046 logf(LOG_DEBUG, "pms=%d, mrs=%d", a->preferredMessageSize,
1047 a->maximumRecordSize);
1048 for (recno = start; reclist->num_records < toget; recno++)
1050 bend_fetchrequest freq;
1051 bend_fetchresult *fres;
1052 Z_NamePlusRecord *thisrec;
1053 Z_DatabaseRecord *thisext;
1054 int this_length = 0;
1057 * we get the number of bytes allocated on the stream before any
1058 * allocation done by the backend - this should give us a reasonable
1059 * idea of the total size of the data so far.
1061 total_length = odr_total(a->encode) - dumped_records;
1062 freq.setname = setname;
1063 freq.number = recno;
1065 freq.format = format;
1066 freq.stream = a->encode;
1067 freq.surrogate_flag = 0;
1068 freq.referenceId = referenceId;
1069 if (!(fres = bend_fetch(a->backend, &freq, 0)))
1071 *pres = Z_PRES_FAILURE;
1072 return diagrec(a, 2, "Backend interface problem");
1074 /* backend should be able to signal whether error is system-wide
1075 or only pertaining to current record */
1078 if (!freq.surrogate_flag)
1080 *pres = Z_PRES_FAILURE;
1081 return diagrec(a, fres->errcode, fres->errstring);
1083 reclist->records[reclist->num_records] =
1084 surrogatediagrec(a, fres->basename, fres->errcode,
1086 reclist->num_records++;
1087 *next = fres->last_in_set ? 0 : recno + 1;
1091 this_length = fres->len;
1093 this_length = odr_total(a->encode) - total_length;
1094 logf(LOG_DEBUG, " fetched record, len=%d, total=%d",
1095 this_length, total_length);
1096 if (this_length + total_length > a->preferredMessageSize)
1098 /* record is small enough, really */
1099 if (this_length <= a->preferredMessageSize)
1101 logf(LOG_DEBUG, " Dropped last normal-sized record");
1102 *pres = Z_PRES_PARTIAL_2;
1105 /* record can only be fetched by itself */
1106 if (this_length < a->maximumRecordSize)
1108 logf(LOG_DEBUG, " Record > prefmsgsz");
1111 logf(LOG_DEBUG, " Dropped it");
1112 reclist->records[reclist->num_records] =
1113 surrogatediagrec(a, fres->basename, 16, 0);
1114 reclist->num_records++;
1115 *next = fres->last_in_set ? 0 : recno + 1;
1116 dumped_records += this_length;
1120 else /* too big entirely */
1122 logf(LOG_DEBUG, "Record > maxrcdsz");
1123 reclist->records[reclist->num_records] =
1124 surrogatediagrec(a, fres->basename, 17, 0);
1125 reclist->num_records++;
1126 *next = fres->last_in_set ? 0 : recno + 1;
1127 dumped_records += this_length;
1132 if (!(thisrec = (Z_NamePlusRecord *)
1133 odr_malloc(a->encode, sizeof(*thisrec))))
1135 if (!(thisrec->databaseName = (char *)odr_malloc(a->encode,
1136 strlen(fres->basename) + 1)))
1138 strcpy(thisrec->databaseName, fres->basename);
1139 thisrec->which = Z_NamePlusRecord_databaseRecord;
1140 if (!(thisrec->u.databaseRecord = thisext = (Z_External *)
1141 odr_malloc(a->encode, sizeof(Z_DatabaseRecord))))
1143 recform.proto = a->proto;
1144 recform.oclass = CLASS_RECSYN;
1145 recform.value = fres->format;
1146 thisext->direct_reference =
1147 odr_oiddup(a->encode, oid_ent_to_oid(&recform, oid));
1148 thisext->indirect_reference = 0;
1149 thisext->descriptor = 0;
1150 if (fres->len < 0) /* Structured data */
1152 switch (fres->format)
1154 case VAL_SUTRS: thisext->which = Z_External_sutrs; break;
1155 case VAL_GRS1: thisext->which = Z_External_grs1; break;
1156 case VAL_EXPLAIN: thisext->which = Z_External_explainRecord;
1158 case VAL_SUMMARY: thisext->which = Z_External_summary; break;
1159 case VAL_OPAC: thisext->which = Z_External_OPAC; break;
1162 logf(LOG_FATAL, "Unknown structured format from backend.");
1167 * We cheat on the pointers here. Obviously, the record field
1168 * of the backend-fetch structure should have been a union for
1169 * correctness, but we're stuck with this for backwards
1172 thisext->u.grs1 = (Z_GenericRecord*) fres->record;
1174 else if (fres->format == VAL_SUTRS) /* SUTRS is a single-ASN.1-type */
1177 Z_SUTRS *sutrs = (Z_SUTRS *)odr_malloc(a->encode, 1+fres->len);
1179 memcpy(sutrs, fres->record, fres->len);
1180 sutrs[fres->len] = '\0';
1182 Odr_oct *sutrs = (Odr_oct *)odr_malloc(a->encode, sizeof(*sutrs));
1184 thisext->which = Z_External_sutrs;
1185 thisext->u.sutrs = sutrs;
1186 sutrs->buf = (unsigned char *)odr_malloc(a->encode, fres->len);
1187 sutrs->len = sutrs->size = fres->len;
1188 memcpy(sutrs->buf, fres->record, fres->len);
1191 else /* octet-aligned record. */
1193 thisext->which = Z_External_octet;
1194 if (!(thisext->u.octet_aligned = (Odr_oct *)odr_malloc(a->encode,
1197 if (!(thisext->u.octet_aligned->buf = (unsigned char *)
1198 odr_malloc(a->encode, fres->len)))
1200 memcpy(thisext->u.octet_aligned->buf, fres->record, fres->len);
1201 thisext->u.octet_aligned->len = thisext->u.octet_aligned->size =
1204 reclist->records[reclist->num_records] = thisrec;
1205 reclist->num_records++;
1206 *next = fres->last_in_set ? 0 : recno + 1;
1208 *num = reclist->num_records;
1212 static Z_APDU *process_searchRequest(association *assoc, request *reqb,
1215 Z_SearchRequest *req = reqb->request->u.searchRequest;
1216 bend_search_rr *bsrr =
1217 (bend_search_rr *)nmem_malloc (reqb->request_mem, sizeof(*bsrr));
1219 logf(LOG_LOG, "Got SearchRequest.");
1221 bsrr->request = reqb;
1222 bsrr->association = assoc;
1223 bsrr->referenceId = req->referenceId;
1224 save_referenceId (reqb, bsrr->referenceId);
1226 logf (LOG_LOG, "ResultSet '%s'", req->resultSetName);
1227 if (req->databaseNames)
1230 for (i = 0; i < req->num_databaseNames; i++)
1231 logf (LOG_LOG, "Database '%s'", req->databaseNames[i]);
1233 switch (req->query->which)
1235 case Z_Query_type_1: case Z_Query_type_101:
1236 log_rpn_query (req->query->u.type_1);
1238 if (assoc->bend_search)
1240 bsrr->setname = req->resultSetName;
1241 bsrr->replace_set = *req->replaceIndicator;
1242 bsrr->num_bases = req->num_databaseNames;
1243 bsrr->basenames = req->databaseNames;
1244 bsrr->query = req->query;
1245 bsrr->stream = assoc->encode;
1246 bsrr->decode = assoc->decode;
1249 bsrr->errstring = NULL;
1250 ((int (*)(void *, bend_search_rr *))(assoc->bend_search))
1251 (assoc->backend, bsrr);
1257 bend_searchrequest bsrq;
1258 bend_searchresult *bsrt;
1260 bsrq.setname = req->resultSetName;
1261 bsrq.replace_set = *req->replaceIndicator;
1262 bsrq.num_bases = req->num_databaseNames;
1263 bsrq.basenames = req->databaseNames;
1264 bsrq.query = req->query;
1265 bsrq.referenceId = req->referenceId;
1266 bsrq.stream = assoc->encode;
1267 bsrq.decode = assoc->decode;
1268 if (!(bsrt = bend_search (assoc->backend, &bsrq, fd)))
1270 bsrr->hits = bsrt->hits;
1271 bsrr->errcode = bsrt->errcode;
1272 bsrr->errstring = bsrt->errstring;
1274 return response_searchRequest(assoc, reqb, bsrr, fd);
1277 int bend_searchresponse(void *handle, bend_search_rr *bsrr) {return 0;}
1280 * Prepare a searchresponse based on the backend results. We probably want
1281 * to look at making the fetching of records nonblocking as well, but
1282 * so far, we'll keep things simple.
1283 * If bsrt is null, that means we're called in response to a communications
1284 * event, and we'll have to get the response for ourselves.
1286 static Z_APDU *response_searchRequest(association *assoc, request *reqb,
1287 bend_search_rr *bsrt, int *fd)
1289 Z_SearchRequest *req = reqb->request->u.searchRequest;
1290 Z_APDU *apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1291 Z_SearchResponse *resp = (Z_SearchResponse *)
1292 odr_malloc (assoc->encode, sizeof(*resp));
1293 int *nulint = (int *)odr_malloc (assoc->encode, sizeof(*nulint));
1294 bool_t *sr = (bool_t *)odr_malloc (assoc->encode, sizeof(*sr));
1295 int *next = (int *)odr_malloc (assoc->encode, sizeof(*next));
1296 int *none = (int *)odr_malloc (assoc->encode, sizeof(*none));
1303 apdu->which = Z_APDU_searchResponse;
1304 apdu->u.searchResponse = resp;
1305 resp->referenceId = req->referenceId;
1306 resp->additionalSearchInfo = 0;
1307 resp->otherInfo = 0;
1309 if (!bsrt && !bend_searchresponse(assoc->backend, bsrt))
1311 logf(LOG_FATAL, "Bad result from backend");
1314 else if (bsrt->errcode)
1316 resp->records = diagrec(assoc, bsrt->errcode, bsrt->errstring);
1317 resp->resultCount = nulint;
1318 resp->numberOfRecordsReturned = nulint;
1319 resp->nextResultSetPosition = nulint;
1320 resp->searchStatus = nulint;
1321 resp->resultSetStatus = none;
1322 resp->presentStatus = 0;
1326 int *toget = (int *)odr_malloc (assoc->encode, sizeof(*toget));
1327 int *presst = (int *)odr_malloc (assoc->encode, sizeof(*presst));
1328 Z_RecordComposition comp, *compp = 0;
1333 resp->resultCount = &bsrt->hits;
1335 comp.which = Z_RecordComp_simple;
1336 /* how many records does the user agent want, then? */
1337 if (bsrt->hits <= *req->smallSetUpperBound)
1339 *toget = bsrt->hits;
1340 if ((comp.u.simple = req->smallSetElementSetNames))
1343 else if (bsrt->hits < *req->largeSetLowerBound)
1345 *toget = *req->mediumSetPresentNumber;
1346 if (*toget > bsrt->hits)
1347 *toget = bsrt->hits;
1348 if ((comp.u.simple = req->mediumSetElementSetNames))
1354 if (*toget && !resp->records)
1359 if (!(prefformat = oid_getentbyoid(req->preferredRecordSyntax)) ||
1360 prefformat->oclass != CLASS_RECSYN)
1363 form = prefformat->value;
1364 resp->records = pack_records(assoc, req->resultSetName, 1,
1365 toget, compp, next, presst, form, req->referenceId);
1368 resp->numberOfRecordsReturned = toget;
1369 resp->nextResultSetPosition = next;
1370 resp->searchStatus = sr;
1371 resp->resultSetStatus = 0;
1372 resp->presentStatus = presst;
1376 if (*resp->resultCount)
1378 resp->numberOfRecordsReturned = nulint;
1379 resp->nextResultSetPosition = next;
1380 resp->searchStatus = sr;
1381 resp->resultSetStatus = 0;
1382 resp->presentStatus = 0;
1389 * Maybe we got a little over-friendly when we designed bend_fetch to
1390 * get only one record at a time. Some backends can optimise multiple-record
1391 * fetches, and at any rate, there is some overhead involved in
1392 * all that selecting and hopping around. Problem is, of course, that the
1393 * frontend can't know ahead of time how many records it'll need to
1394 * fill the negotiated PDU size. Annoying. Segmentation or not, Z/SR
1395 * is downright lousy as a bulk data transfer protocol.
1397 * To start with, we'll do the fetching of records from the backend
1398 * in one operation: To save some trips in and out of the event-handler,
1399 * and to simplify the interface to pack_records. At any rate, asynch
1400 * operation is more fun in operations that have an unpredictable execution
1401 * speed - which is normally more true for search than for present.
1403 static Z_APDU *process_presentRequest(association *assoc, request *reqb,
1406 Z_PresentRequest *req = reqb->request->u.presentRequest;
1410 Z_PresentResponse *resp;
1415 logf(LOG_LOG, "Got PresentRequest.");
1417 if (!(prefformat = oid_getentbyoid(req->preferredRecordSyntax)) ||
1418 prefformat->oclass != CLASS_RECSYN)
1421 form = prefformat->value;
1422 if (assoc->bend_present)
1424 bend_present_rr *bprr = (bend_present_rr *)
1425 nmem_malloc (reqb->request_mem, sizeof(*bprr));
1426 bprr->setname = req->resultSetId;
1427 bprr->start = *req->resultSetStartPoint;
1428 bprr->number = *req->numberOfRecordsRequested;
1429 bprr->format = form;
1430 bprr->comp = req->recordComposition;
1431 bprr->referenceId = req->referenceId;
1432 bprr->stream = assoc->encode;
1433 bprr->request = reqb;
1434 bprr->association = assoc;
1436 bprr->errstring = NULL;
1437 ((int (*)(void *, bend_present_rr *))(*assoc->bend_present))(
1438 assoc->backend, bprr);
1443 apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1444 resp = (Z_PresentResponse *)odr_malloc (assoc->encode, sizeof(*resp));
1445 presst = (int *)odr_malloc (assoc->encode, sizeof(*presst));
1446 next = (int *)odr_malloc (assoc->encode, sizeof(*next));
1447 num = (int *)odr_malloc (assoc->encode, sizeof(*num));
1450 *num = *req->numberOfRecordsRequested;
1452 apdu->which = Z_APDU_presentResponse;
1453 apdu->u.presentResponse = resp;
1454 resp->referenceId = req->referenceId;
1455 resp->otherInfo = 0;
1458 pack_records(assoc, req->resultSetId, *req->resultSetStartPoint,
1459 num, req->recordComposition, next, presst, form,
1463 resp->numberOfRecordsReturned = num;
1464 resp->presentStatus = presst;
1465 resp->nextResultSetPosition = next;
1471 * Scan was implemented rather in a hurry, and with support for only the basic
1472 * elements of the service in the backend API. Suggestions are welcome.
1474 static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd)
1476 Z_ScanRequest *req = reqb->request->u.scanRequest;
1477 Z_APDU *apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1478 Z_ScanResponse *res = (Z_ScanResponse *)
1479 odr_malloc (assoc->encode, sizeof(*res));
1480 int *scanStatus = (int *)
1481 odr_malloc (assoc->encode, sizeof(*scanStatus));
1482 int *numberOfEntriesReturned = (int *)
1483 odr_malloc (assoc->encode, sizeof(*numberOfEntriesReturned));
1484 Z_ListEntries *ents = (Z_ListEntries *)
1485 odr_malloc (assoc->encode, sizeof(*ents));
1486 Z_DiagRecs *diagrecs_p = NULL;
1488 bend_scanrequest srq;
1489 bend_scanresult *srs;
1492 logf(LOG_LOG, "Got ScanRequest");
1493 *scanStatus = Z_Scan_failure;
1494 *numberOfEntriesReturned = 0;
1496 apdu->which = Z_APDU_scanResponse;
1497 apdu->u.scanResponse = res;
1498 res->referenceId = req->referenceId;
1500 res->scanStatus = scanStatus;
1501 res->numberOfEntriesReturned = numberOfEntriesReturned;
1502 res->positionOfTerm = 0;
1503 res->entries = ents;
1504 ents->num_entries = 0;
1505 ents->entries = NULL;
1506 ents->num_nonsurrogateDiagnostics = 0;
1507 ents->nonsurrogateDiagnostics = NULL;
1508 res->attributeSet = 0;
1511 if (req->attributeSet && (!(attent = oid_getentbyoid(req->attributeSet)) ||
1512 attent->oclass != CLASS_ATTSET
1513 || attent->value != VAL_BIB1))
1514 diagrecs_p = diagrecs(assoc, 121, 0);
1515 else if (req->stepSize && *req->stepSize > 0)
1516 diagrecs_p = diagrecs(assoc, 205, 0);
1519 if (req->databaseNames)
1522 for (i = 0; i < req->num_databaseNames; i++)
1523 logf (LOG_LOG, "Database '%s'", req->databaseNames[i]);
1525 srq.num_bases = req->num_databaseNames;
1526 srq.basenames = req->databaseNames;
1527 srq.num_entries = *req->numberOfTermsRequested;
1528 srq.term = req->termListAndStartPoint;
1529 srq.referenceId = req->referenceId;
1530 srq.stream = assoc->encode;
1531 if (!(attset = oid_getentbyoid(req->attributeSet)) ||
1532 attset->oclass != CLASS_RECSYN)
1533 srq.attributeset = VAL_NONE;
1535 srq.attributeset = attset->value;
1536 log_scan_term (req->termListAndStartPoint, attset->value);
1537 srq.term_position = req->preferredPositionInResponse ?
1538 *req->preferredPositionInResponse : 1;
1539 if (!(srs = bend_scan(assoc->backend, &srq, 0)))
1540 diagrecs_p = diagrecs(assoc, 2, 0);
1541 else if (srs->errcode)
1542 diagrecs_p = diagrecs(assoc, srs->errcode, srs->errstring);
1546 Z_Entry **tab = (Z_Entry **)
1547 odr_malloc (assoc->encode, sizeof(*tab) * srs->num_entries);
1549 if (srs->status == BEND_SCAN_PARTIAL)
1550 *scanStatus = Z_Scan_partial_5;
1552 *scanStatus = Z_Scan_success;
1553 ents->entries = tab;
1554 ents->num_entries = srs->num_entries;
1555 res->numberOfEntriesReturned = &ents->num_entries;
1556 res->positionOfTerm = &srs->term_position;
1557 for (i = 0; i < srs->num_entries; i++)
1563 tab[i] = e = (Z_Entry *)odr_malloc(assoc->encode, sizeof(*e));
1564 if (srs->entries[i].occurrences >= 0)
1566 e->which = Z_Entry_termInfo;
1567 e->u.termInfo = t = (Z_TermInfo *)
1568 odr_malloc(assoc->encode, sizeof(*t));
1569 t->suggestedAttributes = 0;
1571 t->alternativeTerm = 0;
1572 t->byAttributes = 0;
1573 t->otherTermInfo = 0;
1574 t->globalOccurrences = &srs->entries[i].occurrences;
1575 t->term = (Z_Term *)
1576 odr_malloc(assoc->encode, sizeof(*t->term));
1577 t->term->which = Z_Term_general;
1578 t->term->u.general = o =
1579 (Odr_oct *)odr_malloc(assoc->encode, sizeof(Odr_oct));
1580 o->buf = (unsigned char *)
1581 odr_malloc(assoc->encode, o->len = o->size =
1582 strlen(srs->entries[i].term));
1583 memcpy(o->buf, srs->entries[i].term, o->len);
1584 logf(LOG_DEBUG, " term #%d: '%s' (%d)", i,
1585 srs->entries[i].term, srs->entries[i].occurrences);
1589 Z_DiagRecs *drecs = diagrecs (assoc,
1590 srs->entries[i].errcode,
1591 srs->entries[i].errstring);
1592 assert (drecs->num_diagRecs == 1);
1593 e->which = Z_Entry_surrogateDiagnostic;
1594 assert (drecs->diagRecs[0]);
1595 e->u.surrogateDiagnostic = drecs->diagRecs[0];
1602 ents->num_nonsurrogateDiagnostics = diagrecs_p->num_diagRecs;
1603 ents->nonsurrogateDiagnostics = diagrecs_p->diagRecs;
1608 static Z_APDU *process_sortRequest(association *assoc, request *reqb,
1611 Z_SortRequest *req = reqb->request->u.sortRequest;
1612 Z_SortResponse *res = (Z_SortResponse *)
1613 odr_malloc (assoc->encode, sizeof(*res));
1614 bend_sort_rr *bsrr = (bend_sort_rr *)
1615 odr_malloc (assoc->encode, sizeof(*bsrr));
1617 Z_APDU *apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1619 logf(LOG_LOG, "Got SortRequest.");
1622 bsrr->num_input_setnames = req->num_inputResultSetNames;
1623 bsrr->input_setnames = req->inputResultSetNames;
1625 bsrr->num_input_setnames = req->inputResultSetNames->num_strings;
1626 bsrr->input_setnames = req->inputResultSetNames->strings;
1628 bsrr->referenceId = req->referenceId;
1629 bsrr->output_setname = req->sortedResultSetName;
1630 bsrr->sort_sequence = req->sortSequence;
1631 bsrr->stream = assoc->encode;
1633 bsrr->sort_status = Z_SortStatus_failure;
1635 bsrr->errstring = 0;
1637 ((int (*)(void *, bend_sort_rr *))(*assoc->bend_sort))(assoc->backend,
1640 res->referenceId = bsrr->referenceId;
1641 res->sortStatus = (int *)
1642 odr_malloc (assoc->encode, sizeof(*res->sortStatus));
1643 *res->sortStatus = bsrr->sort_status;
1644 res->resultSetStatus = 0;
1647 Z_DiagRecs *dr = diagrecs (assoc, bsrr->errcode, bsrr->errstring);
1649 res->diagnostics = dr->diagRecs;
1650 res->num_diagnostics = dr->num_diagRecs;
1652 res->diagnostics = dr;
1658 res->num_diagnostics = 0;
1660 res->diagnostics = 0;
1664 apdu->which = Z_APDU_sortResponse;
1665 apdu->u.sortResponse = res;
1669 static Z_APDU *process_deleteRequest(association *assoc, request *reqb,
1672 Z_DeleteResultSetRequest *req = reqb->request->u.deleteResultSetRequest;
1673 Z_DeleteResultSetResponse *res = (Z_DeleteResultSetResponse *)
1674 odr_malloc (assoc->encode, sizeof(*res));
1675 bend_delete_rr *bdrr = (bend_delete_rr *)
1676 odr_malloc (assoc->encode, sizeof(*bdrr));
1677 Z_APDU *apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1679 logf(LOG_LOG, "Got DeleteRequest.");
1681 bdrr->num_setnames = req->num_resultSetList;
1682 bdrr->setnames = req->resultSetList;
1683 bdrr->stream = assoc->encode;
1684 bdrr->function = *req->deleteFunction;
1685 bdrr->referenceId = req->referenceId;
1687 ((int (*)(void *, bend_delete_rr *))
1688 (*assoc->bend_delete))(assoc->backend, bdrr);
1690 res->referenceId = req->referenceId;
1692 res->deleteOperationStatus = (int *)
1693 odr_malloc (assoc->encode, sizeof(*res->deleteOperationStatus));
1694 *res->deleteOperationStatus = bdrr->delete_status;
1696 res->deleteListStatuses = 0;
1697 res->numberNotDeleted = 0;
1698 res->bulkStatuses = 0;
1699 res->deleteMessage = 0;
1702 apdu->which = Z_APDU_deleteResultSetResponse;
1703 apdu->u.deleteResultSetResponse = res;
1707 static void process_close(association *assoc, request *reqb)
1709 Z_Close *req = reqb->request->u.close;
1710 static char *reasons[] =
1717 "securityViolation",
1724 logf(LOG_LOG, "Got Close, reason %s, message %s",
1725 reasons[*req->closeReason], req->diagnosticInformation ?
1726 req->diagnosticInformation : "NULL");
1727 if (assoc->version < 3) /* to make do_force respond with close */
1729 do_close_req(assoc, Z_Close_finished,
1730 "Association terminated by client", reqb);
1733 void save_referenceId (request *reqb, Z_ReferenceId *refid)
1737 reqb->len_refid = refid->len;
1738 reqb->refid = (char *)nmem_malloc (reqb->request_mem, refid->len);
1739 memcpy (reqb->refid, refid->buf, refid->len);
1743 reqb->len_refid = 0;
1748 void bend_request_send (bend_association a, bend_request req, Z_APDU *res)
1750 process_response (a, req, res);
1753 bend_request bend_request_mk (bend_association a)
1755 request *nreq = request_get (&a->outgoing);
1756 nreq->request_mem = nmem_create ();
1760 Z_ReferenceId *bend_request_getid (ODR odr, bend_request req)
1765 id = (Odr_oct *)odr_malloc (odr, sizeof(*odr));
1766 id->buf = (unsigned char *)odr_malloc (odr, req->len_refid);
1767 id->len = id->size = req->len_refid;
1768 memcpy (id->buf, req->refid, req->len_refid);
1772 void bend_request_destroy (bend_request *req)
1774 nmem_destroy((*req)->request_mem);
1775 request_release(*req);
1779 int bend_backend_respond (bend_association a, bend_request req)
1781 return process_request (a, req);
1784 void bend_request_setdata(bend_request r, void *p)
1789 void *bend_request_getdata(bend_request r)
1791 return r->clientData;
1794 static Z_APDU *process_ESRequest(association *assoc, request *reqb, int *fd)
1796 bend_esrequest_rr esrequest;
1798 Z_ExtendedServicesRequest *req = reqb->request->u.extendedServicesRequest;
1799 Z_APDU *apdu = zget_APDU(assoc->encode, Z_APDU_extendedServicesResponse);
1801 Z_ExtendedServicesResponse *resp = apdu->u.extendedServicesResponse;
1803 logf(LOG_DEBUG,"inside Process esRequest");
1805 esrequest.esr = reqb->request->u.extendedServicesRequest;
1806 esrequest.stream = assoc->encode;
1807 esrequest.errcode = 0;
1808 esrequest.errstring = NULL;
1809 esrequest.request = reqb;
1810 esrequest.association = assoc;
1811 esrequest.referenceId = req->referenceId;
1813 ((int (*)(void *, bend_esrequest_rr *))(*assoc->bend_esrequest))
1814 (assoc->backend, &esrequest);
1816 /* If the response is being delayed, return NULL */
1817 if (esrequest.request == NULL)
1820 resp->referenceId = req->referenceId;
1822 if ( esrequest.errcode == 0 )
1824 /* Backend service indicates request will be processed */
1825 logf(LOG_DEBUG,"Request will be processed...Good !");
1826 *resp->operationStatus = Z_ExtendedServicesResponse_done;
1830 /* Backend indicates error, request will not be processed */
1831 logf(LOG_DEBUG,"Request will not be processed...BAD !");
1832 *resp->operationStatus = Z_ExtendedServicesResponse_failure;
1834 /* Do something with the members of bend_extendedservice */
1836 logf(LOG_DEBUG,"Send the result apdu");