2 * Copyright (c) 1995-1999, Index Data
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.97 1999-11-29 14:36:28 adam
8 * Implementation_name and implementation_version copied verbatim.
10 * Revision 1.96 1999/11/04 14:58:44 adam
11 * Added status elements for backend delete result set handler.
12 * Updated delete result result set command for client.
14 * Revision 1.95 1999/10/11 10:01:24 adam
15 * Implemented bend_sort_rr handler for frontend server.
17 * Revision 1.94 1999/08/27 09:40:32 adam
18 * Renamed logf function to yaz_log. Removed VC++ project files.
20 * Revision 1.93 1999/07/06 12:17:15 adam
21 * Added option -1 that runs server once (for profiling purposes).
23 * Revision 1.92 1999/06/17 10:54:45 adam
24 * Added facility to specify implementation version - and name
27 * Revision 1.91 1999/06/01 14:29:12 adam
28 * Work on Extended Services.
30 * Revision 1.90 1999/05/27 13:02:20 adam
31 * Assigned OID for old DB Update (VAL_DBUPDATE0).
33 * Revision 1.89 1999/05/26 15:24:26 adam
34 * Fixed minor bugs regarding DB Update (introduced by previous commit).
36 * Revision 1.88 1999/04/20 09:56:48 adam
37 * Added 'name' paramter to encoder/decoder routines (typedef Odr_fun).
38 * Modified all encoders/decoders to reflect this change.
40 * Revision 1.87 1999/03/31 11:18:25 adam
41 * Implemented odr_strdup. Added Reference ID to backend server API.
43 * Revision 1.86 1999/02/02 13:57:38 adam
44 * Uses preprocessor define WIN32 instead of WINDOWS to build code
45 * for Microsoft WIN32.
47 * Revision 1.85 1998/11/17 09:52:59 adam
48 * Fixed minor bug (introduced by previous commit).
50 * Revision 1.84 1998/11/16 16:02:32 adam
51 * Added loggin utilies, log_rpn_query and log_scan_term. These used
52 * to be part of Zebra.
54 * Revision 1.83 1998/11/03 10:09:36 adam
55 * Fixed bug regarding YC.
57 * Revision 1.82 1998/10/20 14:00:30 quinn
60 * Revision 1.81 1998/10/13 16:12:24 adam
61 * Added support for Surrogate Diagnostics for Scan Term entries.
63 * Revision 1.80 1998/09/02 12:41:53 adam
64 * Added decode stream in bend search structures.
66 * Revision 1.79 1998/08/19 16:10:08 adam
67 * Changed som member names of DeleteResultSetRequest/Response.
69 * Revision 1.78 1998/08/03 10:23:55 adam
70 * Fixed bug regarding Options for Sort.
72 * Revision 1.77 1998/07/20 12:38:42 adam
73 * Implemented delete result set service to server API.
75 * Revision 1.76 1998/05/27 16:57:07 adam
76 * Support for surrogate diagnostic records added for bend_fetch.
78 * Revision 1.75 1998/05/18 10:13:07 adam
79 * Fixed call to es_request handler - extra argument was passed.
81 * Revision 1.74 1998/03/31 15:13:20 adam
82 * Development towards compiled ASN.1.
84 * Revision 1.73 1998/03/31 11:07:45 adam
85 * Furhter work on UNIverse resource report.
86 * Added Extended Services handling in frontend server.
88 * Revision 1.72 1998/02/11 11:53:35 adam
89 * Changed code so that it compiles as C++.
91 * Revision 1.71 1998/02/10 11:03:57 adam
92 * Added support for extended handlers in backend server interface.
94 * Revision 1.70 1998/01/29 13:15:35 adam
95 * Implemented sort for the backend interface.
97 * Revision 1.69 1997/09/30 11:48:12 adam
98 * Fixed bug introduced by previous commit.
100 * Revision 1.68 1997/09/29 13:18:59 adam
101 * Added function, oid_ent_to_oid, to replace the function
102 * oid_getoidbyent, which is not thread safe.
104 * Revision 1.67 1997/09/17 12:10:40 adam
107 * Revision 1.66 1997/09/05 15:26:44 adam
108 * Added ODR encode in search and scen bend request structures.
109 * Fixed a few enums that caused trouble with C++.
111 * Revision 1.65 1997/09/01 08:53:01 adam
112 * New windows NT/95 port using MSV5.0. The test server 'ztest' was
113 * moved a separate directory. MSV5.0 project server.dsp created.
114 * As an option, the server can now operate as an NT service.
116 * Revision 1.64 1997/04/30 08:52:11 quinn
119 * Revision 1.63 1996/10/11 11:57:26 quinn
122 * Revision 1.62 1996/07/06 19:58:35 quinn
123 * System headerfiles gathered in yconfig
125 * Revision 1.61 1996/06/10 08:56:16 quinn
128 * Revision 1.60 1996/05/30 11:03:10 quinn
129 * Fixed NextresultSetPosition bug fixed.
131 * Revision 1.59 1996/05/14 09:26:46 quinn
132 * Added attribute set to scan backend
134 * Revision 1.58 1996/02/20 12:53:04 quinn
137 * Revision 1.57 1996/01/02 08:57:47 quinn
138 * Changed enums in the ASN.1 .h files to #defines. Changed oident.class to oclass
140 * Revision 1.56 1995/12/14 11:09:57 quinn
143 * Revision 1.55 1995/11/08 17:41:37 quinn
146 * Revision 1.54 1995/11/08 15:11:29 quinn
147 * Log of close transmit.
149 * Revision 1.53 1995/11/01 13:54:58 quinn
152 * Revision 1.52 1995/11/01 12:19:13 quinn
153 * Second attempt to fix same bug.
155 * Revision 1.50 1995/10/25 16:58:32 quinn
158 * Revision 1.49 1995/10/16 13:51:53 quinn
159 * Changes to provide Especs to the backend.
161 * Revision 1.48 1995/10/06 08:51:20 quinn
162 * Added Write-buffer.
164 * Revision 1.47 1995/08/29 14:24:16 quinn
165 * Added second half of close-handshake
167 * Revision 1.46 1995/08/29 11:17:58 quinn
168 * Added code to receive close
170 * Revision 1.45 1995/08/21 09:11:00 quinn
171 * Smallish fixes to suppport new formats.
173 * Revision 1.44 1995/08/17 12:45:25 quinn
174 * Fixed minor problems with GRS-1. Added support in c&s.
176 * Revision 1.43 1995/08/15 12:00:31 quinn
179 * Revision 1.42 1995/08/15 11:16:50 quinn
181 * Revision 1.41 1995/08/02 10:23:06 quinn
184 * Revision 1.40 1995/07/31 14:34:26 quinn
185 * Fixed bug in process_searchResponse (numberOfRecordsReturned).
187 * Revision 1.39 1995/06/27 13:21:00 quinn
190 * Revision 1.38 1995/06/19 12:39:11 quinn
191 * Fixed bug in timeout code. Added BER dumper.
193 * Revision 1.37 1995/06/16 13:16:14 quinn
194 * Fixed Defaultdiagformat.
196 * Revision 1.36 1995/06/16 10:31:36 quinn
197 * Added session timeout.
199 * Revision 1.35 1995/06/15 07:45:14 quinn
202 * Revision 1.34 1995/06/14 15:26:46 quinn
203 * *** empty log message ***
205 * Revision 1.33 1995/06/06 14:57:05 quinn
206 * Better diagnostics.
208 * Revision 1.32 1995/06/06 08:41:44 quinn
209 * Better diagnostics.
211 * Revision 1.31 1995/06/06 08:15:37 quinn
214 * Revision 1.30 1995/06/05 10:53:32 quinn
215 * Added a better SCAN.
217 * Revision 1.29 1995/06/01 11:25:03 quinn
220 * Revision 1.28 1995/06/01 11:21:01 quinn
221 * Attempting to fix a bug in pack-records. replaced break with continue
222 * for large records, according to standard.
224 * Revision 1.27 1995/05/29 08:12:06 quinn
227 * Revision 1.26 1995/05/18 13:02:12 quinn
230 * Revision 1.25 1995/05/17 08:42:26 quinn
231 * Transfer auth info to backend. Allow backend to reject init gracefully.
233 * Revision 1.24 1995/05/16 08:51:04 quinn
234 * License, documentation, and memory fixes
236 * Revision 1.23 1995/05/15 13:25:10 quinn
239 * Revision 1.22 1995/05/15 11:56:39 quinn
240 * Asynchronous facilities. Restructuring of seshigh code.
242 * Revision 1.21 1995/05/02 08:53:19 quinn
243 * Trying in vain to fix comm with ISODE
245 * Revision 1.20 1995/04/20 15:13:00 quinn
248 * Revision 1.19 1995/04/18 08:15:34 quinn
249 * Added dynamic memory allocation on encoding (whew). Code is now somewhat
250 * neater. We'll make the same change for decoding one day.
252 * Revision 1.18 1995/04/17 11:28:25 quinn
255 * Revision 1.17 1995/04/10 10:23:36 quinn
256 * Some work to add scan and other things.
258 * Revision 1.16 1995/03/31 09:18:55 quinn
261 * Revision 1.15 1995/03/30 14:03:23 quinn
262 * Added RFC1006 as separate library
264 * Revision 1.14 1995/03/30 12:18:17 quinn
267 * Revision 1.13 1995/03/30 09:09:24 quinn
268 * Added state-handle and some support for asynchronous activities.
270 * Revision 1.12 1995/03/29 15:40:16 quinn
271 * Ongoing work. Statserv is now dynamic by default
273 * Revision 1.11 1995/03/28 09:16:21 quinn
274 * Added record packing to the search request
276 * Revision 1.10 1995/03/27 08:34:24 quinn
277 * Added dynamic server functionality.
278 * Released bindings to session.c (is now redundant)
280 * Revision 1.9 1995/03/22 15:01:26 quinn
281 * Adjusting record packing.
283 * Revision 1.8 1995/03/22 10:13:21 quinn
284 * Working on record packer
286 * Revision 1.7 1995/03/21 15:53:31 quinn
289 * Revision 1.6 1995/03/21 12:30:09 quinn
290 * Beginning to add support for record packing.
292 * Revision 1.5 1995/03/17 10:44:13 quinn
293 * Added catch of null-string in makediagrec
295 * Revision 1.4 1995/03/17 10:18:08 quinn
296 * Added memory management.
298 * Revision 1.3 1995/03/16 17:42:39 quinn
301 * Revision 1.2 1995/03/16 13:29:01 quinn
302 * Partitioned server.
304 * Revision 1.1 1995/03/15 16:02:10 quinn
305 * Modded session.c to seshigh.c
310 * Frontend server logic.
312 * This code receives incoming APDUs, and handles client requests by means
313 * of the backend API.
315 * Some of the code is getting quite involved, compared to simpler servers -
316 * primarily because it is asynchronous both in the communication with
317 * the user and the backend. We think the complexity will pay off in
318 * the form of greater flexibility when more asynchronous facilities
321 * Memory management has become somewhat involved. In the simple case, where
322 * only one PDU is pending at a time, it will simply reuse the same memory,
323 * once it has found its working size. When we enable multiple concurrent
324 * operations, perhaps even with multiple parallel calls to the backend, it
325 * will maintain a pool of buffers for encoding and decoding, trying to
326 * minimize memory allocation/deallocation during normal operation.
328 * TODOs include (and will be done in order of public interest):
330 * Support for EXPLAIN - provide simple meta-database system.
331 * Support for access control.
332 * Support for resource control.
333 * Support for extended services - primarily Item Order.
334 * Rest of Z39.50-1994
349 #include <comstack.h>
356 #include <statserv.h>
360 static int process_request(association *assoc, request *req, char **msg);
361 void backend_response(IOCHAN i, int event);
362 static int process_response(association *assoc, request *req, Z_APDU *res);
363 static Z_APDU *process_initRequest(association *assoc, request *reqb);
364 static Z_APDU *process_searchRequest(association *assoc, request *reqb,
366 static Z_APDU *response_searchRequest(association *assoc, request *reqb,
367 bend_search_rr *bsrr, int *fd);
368 static Z_APDU *process_presentRequest(association *assoc, request *reqb,
370 static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd);
371 static Z_APDU *process_sortRequest(association *assoc, request *reqb, int *fd);
372 static void process_close(association *assoc, request *reqb);
373 void save_referenceId (request *reqb, Z_ReferenceId *refid);
374 static Z_APDU *process_deleteRequest(association *assoc, request *reqb,
376 static int bend_default_scan (void *handle, bend_scan_rr *rr);
378 static FILE *apduf = 0; /* for use in static mode */
379 static statserv_options_block *control_block = 0;
381 /* Chas: Added in from DALI */
382 static Z_APDU *process_ESRequest(association *assoc, request *reqb, int *fd);
383 /* Chas: End of addition from DALI */
386 * Create and initialize a new association-handle.
387 * channel : iochannel for the current line.
388 * link : communications channel.
389 * Returns: 0 or a new association handle.
391 association *create_association(IOCHAN channel, COMSTACK link)
396 control_block = statserv_getcontrol();
397 if (!(anew = (association *)xmalloc(sizeof(*anew))))
399 anew->client_chan = channel;
400 anew->client_link = link;
401 if (!(anew->decode = odr_createmem(ODR_DECODE)) ||
402 !(anew->encode = odr_createmem(ODR_ENCODE)))
404 if (*control_block->apdufile)
409 strcpy(filename, control_block->apdufile);
410 if (!(anew->print = odr_createmem(ODR_PRINT)))
412 if (*control_block->apdufile != '-')
414 strcpy(filename, control_block->apdufile);
415 if (!control_block->dynamic)
419 if (!(apduf = fopen(filename, "w")))
421 yaz_log(LOG_WARN|LOG_ERRNO, "%s", filename);
424 setvbuf(apduf, 0, _IONBF, 0);
430 sprintf(filename + strlen(filename), ".%d", getpid());
431 if (!(f = fopen(filename, "w")))
433 yaz_log(LOG_WARN|LOG_ERRNO, "%s", filename);
436 setvbuf(f, 0, _IONBF, 0);
438 odr_setprint(anew->print, f);
443 anew->input_buffer = 0;
444 anew->input_buffer_len = 0;
446 anew->state = ASSOC_NEW;
447 request_initq(&anew->incoming);
448 request_initq(&anew->outgoing);
449 anew->proto = cs_getproto(link);
454 * Free association and release resources.
456 void destroy_association(association *h)
458 odr_destroy(h->decode);
459 odr_destroy(h->encode);
461 odr_destroy(h->print);
463 xfree(h->input_buffer);
465 bend_close(h->backend);
466 while (request_deq(&h->incoming));
467 while (request_deq(&h->outgoing));
468 request_delq(&h->incoming);
469 request_delq(&h->outgoing);
471 if (control_block && control_block->one_shot)
475 static void do_close_req(association *a, int reason, char *message,
479 Z_Close *cls = zget_Close(a->encode);
481 /* Purge request queue */
482 while (request_deq(&a->incoming));
483 while (request_deq(&a->outgoing));
486 yaz_log(LOG_LOG, "Sending Close PDU, reason=%d, message=%s",
487 reason, message ? message : "none");
488 apdu.which = Z_APDU_close;
490 *cls->closeReason = reason;
491 cls->diagnosticInformation = message;
492 process_response(a, req, &apdu);
493 iochan_settimeout(a->client_chan, 60);
497 yaz_log(LOG_DEBUG, "v2 client. No Close PDU");
498 iochan_setevent(a->client_chan, EVENT_TIMEOUT); /* force imm close */
500 a->state = ASSOC_DEAD;
503 static void do_close(association *a, int reason, char *message)
505 do_close_req (a, reason, message, request_get(&a->outgoing));
509 * This is where PDUs from the client are read and the further
510 * processing is initiated. Flow of control moves down through the
511 * various process_* functions below, until the encoded result comes back up
512 * to the output handler in here.
514 * h : the I/O channel that has an outstanding event.
515 * event : the current outstanding event.
517 void ir_session(IOCHAN h, int event)
520 association *assoc = (association *)iochan_getdata(h);
521 COMSTACK conn = assoc->client_link;
524 assert(h && conn && assoc);
525 if (event == EVENT_TIMEOUT)
527 if (assoc->state != ASSOC_UP)
529 yaz_log(LOG_LOG, "Final timeout - closing connection.");
531 destroy_association(assoc);
536 yaz_log(LOG_LOG, "Session idle too long. Sending close.");
537 do_close(assoc, Z_Close_lackOfActivity, 0);
541 if (event & EVENT_INPUT || event & EVENT_WORK) /* input */
543 if (event & EVENT_INPUT)
545 yaz_log(LOG_DEBUG, "ir_session (input)");
546 assert(assoc && conn);
547 /* We aren't speaking to this fellow */
548 if (assoc->state == ASSOC_DEAD)
550 yaz_log(LOG_LOG, "Closed connection after reject");
552 destroy_association(assoc);
556 if ((res = cs_get(conn, &assoc->input_buffer,
557 &assoc->input_buffer_len)) <= 0)
559 yaz_log(LOG_LOG, "Connection closed by client");
561 destroy_association(assoc);
565 else if (res == 1) /* incomplete read - wait for more */
567 if (cs_more(conn)) /* more stuff - call us again later, please */
568 iochan_setevent(h, EVENT_INPUT);
570 /* we got a complete PDU. Let's decode it */
571 yaz_log(LOG_DEBUG, "Got PDU, %d bytes", res);
572 req = request_get(&assoc->incoming); /* get a new request structure */
573 odr_reset(assoc->decode);
574 odr_setbuf(assoc->decode, assoc->input_buffer, res, 0);
575 if (!z_APDU(assoc->decode, &req->request, 0, 0))
577 yaz_log(LOG_LOG, "ODR error on incoming PDU: %s [near byte %d] ",
578 odr_errmsg(odr_geterror(assoc->decode)),
579 odr_offset(assoc->decode));
580 yaz_log(LOG_LOG, "PDU dump:");
581 odr_dumpBER(log_file(), assoc->input_buffer, res);
582 do_close(assoc, Z_Close_protocolError, "Malformed package");
585 req->request_mem = odr_extract_mem(assoc->decode);
586 if (assoc->print && !z_APDU(assoc->print, &req->request, 0, 0))
588 yaz_log(LOG_WARN, "ODR print error: %s",
589 odr_errmsg(odr_geterror(assoc->print)));
590 odr_reset(assoc->print);
592 request_enq(&assoc->incoming, req);
595 /* can we do something yet? */
596 req = request_head(&assoc->incoming);
597 if (req->state == REQUEST_IDLE)
600 request_deq(&assoc->incoming);
601 if (process_request(assoc, req, &msg) < 0)
602 do_close_req(assoc, Z_Close_systemProblem, msg, req);
605 if (event & EVENT_OUTPUT)
607 request *req = request_head(&assoc->outgoing);
609 yaz_log(LOG_DEBUG, "ir_session (output)");
610 req->state = REQUEST_PENDING;
611 switch (res = cs_put(conn, req->response, req->len_response))
614 yaz_log(LOG_LOG, "Connection closed by client");
616 destroy_association(assoc);
619 case 0: /* all sent - release the request structure */
620 yaz_log(LOG_DEBUG, "Wrote PDU, %d bytes", req->len_response);
621 nmem_destroy(req->request_mem);
622 request_deq(&assoc->outgoing);
623 request_release(req);
624 if (!request_head(&assoc->outgoing))
625 iochan_clearflag(h, EVENT_OUTPUT);
627 /* value of 1 -- partial send -- is simply ignored */
630 if (event & EVENT_EXCEPT)
632 yaz_log(LOG_DEBUG, "ir_session (exception)");
634 destroy_association(assoc);
640 * Initiate request processing.
642 static int process_request(association *assoc, request *req, char **msg)
648 *msg = "Unknown Error";
649 assert(req && req->state == REQUEST_IDLE);
650 switch (req->request->which)
652 case Z_APDU_initRequest:
653 res = process_initRequest(assoc, req); break;
654 case Z_APDU_searchRequest:
655 res = process_searchRequest(assoc, req, &fd); break;
656 case Z_APDU_presentRequest:
657 res = process_presentRequest(assoc, req, &fd); break;
658 case Z_APDU_scanRequest:
659 if (assoc->bend_scan)
660 res = process_scanRequest(assoc, req, &fd);
663 *msg = "Cannot handle Scan APDU";
667 case Z_APDU_extendedServicesRequest:
668 if (assoc->bend_esrequest)
669 res = process_ESRequest(assoc, req, &fd);
672 *msg = "Cannot handle Extended Services APDU";
676 case Z_APDU_sortRequest:
677 if (assoc->bend_sort)
678 res = process_sortRequest(assoc, req, &fd);
681 *msg = "Cannot handle Sort APDU";
686 process_close(assoc, req);
688 case Z_APDU_deleteResultSetRequest:
689 if (assoc->bend_delete)
690 res = process_deleteRequest(assoc, req, &fd);
693 *msg = "Cannot handle Delete APDU";
698 *msg = "Bad APDU received";
703 yaz_log(LOG_DEBUG, " result immediately available");
704 retval = process_response(assoc, req, res);
708 yaz_log(LOG_DEBUG, " result unavailble");
711 else /* no result yet - one will be provided later */
715 /* Set up an I/O handler for the fd supplied by the backend */
717 yaz_log(LOG_DEBUG, " establishing handler for result");
718 req->state = REQUEST_PENDING;
719 if (!(chan = iochan_create(fd, backend_response, EVENT_INPUT)))
721 iochan_setdata(chan, assoc);
728 * Handle message from the backend.
730 void backend_response(IOCHAN i, int event)
732 association *assoc = (association *)iochan_getdata(i);
733 request *req = request_head(&assoc->incoming);
737 yaz_log(LOG_DEBUG, "backend_response");
738 assert(assoc && req && req->state != REQUEST_IDLE);
739 /* determine what it is we're waiting for */
740 switch (req->request->which)
742 case Z_APDU_searchRequest:
743 res = response_searchRequest(assoc, req, 0, &fd); break;
745 case Z_APDU_presentRequest:
746 res = response_presentRequest(assoc, req, 0, &fd); break;
747 case Z_APDU_scanRequest:
748 res = response_scanRequest(assoc, req, 0, &fd); break;
751 yaz_log(LOG_WARN, "Serious programmer's lapse or bug");
754 if ((res && process_response(assoc, req, res) < 0) || fd < 0)
756 yaz_log(LOG_LOG, "Fatal error when talking to backend");
757 do_close(assoc, Z_Close_systemProblem, 0);
761 else if (!res) /* no result yet - try again later */
763 yaz_log(LOG_DEBUG, " no result yet");
764 iochan_setfd(i, fd); /* in case fd has changed */
769 * Encode response, and transfer the request structure to the outgoing queue.
771 static int process_response(association *assoc, request *req, Z_APDU *res)
773 odr_setbuf(assoc->encode, req->response, req->size_response, 1);
774 if (!z_APDU(assoc->encode, &res, 0, 0))
776 yaz_log(LOG_WARN, "ODR error when encoding response: %s",
777 odr_errmsg(odr_geterror(assoc->decode)));
778 odr_reset(assoc->encode);
781 req->response = odr_getbuf(assoc->encode, &req->len_response,
782 &req->size_response);
783 odr_setbuf(assoc->encode, 0, 0, 0); /* don'txfree if we abort later */
784 if (assoc->print && !z_APDU(assoc->print, &res, 0, 0))
786 yaz_log(LOG_WARN, "ODR print error: %s",
787 odr_errmsg(odr_geterror(assoc->print)));
788 odr_reset(assoc->print);
790 odr_reset(assoc->encode);
791 req->state = REQUEST_IDLE;
792 request_enq(&assoc->outgoing, req);
793 /* turn the work over to the ir_session handler */
794 iochan_setflag(assoc->client_chan, EVENT_OUTPUT);
795 /* Is there more work to be done? give that to the input handler too */
797 if (request_head(&assoc->incoming))
799 yaz_log (LOG_DEBUG, "more work to be done");
800 iochan_setevent(assoc->client_chan, EVENT_WORK);
807 * Handle init request.
808 * At the moment, we don't check the options
809 * anywhere else in the code - we just try not to do anything that would
810 * break a naive client. We'll toss 'em into the association block when
811 * we need them there.
813 static Z_APDU *process_initRequest(association *assoc, request *reqb)
815 Z_InitRequest *req = reqb->request->u.initRequest;
816 Z_APDU *apdu = zget_APDU(assoc->encode, Z_APDU_initResponse);
817 Z_InitResponse *resp = apdu->u.initResponse;
818 bend_initrequest binitreq;
819 bend_initresult *binitres;
822 yaz_log(LOG_LOG, "Got initRequest");
823 if (req->implementationId)
824 yaz_log(LOG_LOG, "Id: %s", req->implementationId);
825 if (req->implementationName)
826 yaz_log(LOG_LOG, "Name: %s", req->implementationName);
827 if (req->implementationVersion)
828 yaz_log(LOG_LOG, "Version: %s", req->implementationVersion);
830 binitreq.stream = assoc->encode;
831 binitreq.configname = "default-config";
832 binitreq.auth = req->idAuthentication;
833 binitreq.referenceId = req->referenceId;
834 binitreq.implementation_version = 0;
835 binitreq.implementation_name = 0;
836 binitreq.bend_sort = NULL;
837 binitreq.bend_search = NULL;
838 binitreq.bend_present = NULL;
839 binitreq.bend_esrequest = NULL;
840 binitreq.bend_delete = NULL;
841 binitreq.bend_scan = bend_default_scan;
842 if (!(binitres = bend_init(&binitreq)))
844 yaz_log(LOG_WARN, "Bad response from backend.");
848 assoc->backend = binitres->handle;
849 if ((assoc->bend_sort = (int (*)())binitreq.bend_sort))
850 yaz_log (LOG_DEBUG, "Sort handler installed");
851 if ((assoc->bend_search = (int (*)())binitreq.bend_search))
852 yaz_log (LOG_DEBUG, "Search handler installed");
853 if ((assoc->bend_present = (int (*)())binitreq.bend_present))
854 yaz_log (LOG_DEBUG, "Present handler installed");
855 if ((assoc->bend_esrequest = (int (*)())binitreq.bend_esrequest))
856 yaz_log (LOG_DEBUG, "ESRequest handler installed");
857 if ((assoc->bend_delete = (int (*)())binitreq.bend_delete))
858 yaz_log (LOG_DEBUG, "Delete handler installed");
859 if ((assoc->bend_scan = (int (*)())binitreq.bend_scan))
860 yaz_log (LOG_DEBUG, "Scan handler installed");
862 resp->referenceId = req->referenceId;
864 /* let's tell the client what we can do */
865 if (ODR_MASK_GET(req->options, Z_Options_search))
867 ODR_MASK_SET(resp->options, Z_Options_search);
868 strcat(options, "srch");
870 if (ODR_MASK_GET(req->options, Z_Options_present))
872 ODR_MASK_SET(resp->options, Z_Options_present);
873 strcat(options, " prst");
875 if (ODR_MASK_GET(req->options, Z_Options_delSet) && binitreq.bend_delete)
877 ODR_MASK_SET(resp->options, Z_Options_delSet);
878 strcat(options, " del");
880 if (ODR_MASK_GET(req->options, Z_Options_extendedServices) &&
881 binitreq.bend_esrequest)
883 ODR_MASK_SET(resp->options, Z_Options_extendedServices);
884 strcat (options, " extendedServices");
886 if (ODR_MASK_GET(req->options, Z_Options_namedResultSets))
888 ODR_MASK_SET(resp->options, Z_Options_namedResultSets);
889 strcat(options, " namedresults");
891 if (ODR_MASK_GET(req->options, Z_Options_scan) && binitreq.bend_scan)
893 ODR_MASK_SET(resp->options, Z_Options_scan);
894 strcat(options, " scan");
896 if (ODR_MASK_GET(req->options, Z_Options_concurrentOperations))
898 ODR_MASK_SET(resp->options, Z_Options_concurrentOperations);
899 strcat(options, " concurop");
901 if (ODR_MASK_GET(req->options, Z_Options_sort) && binitreq.bend_sort)
903 ODR_MASK_SET(resp->options, Z_Options_sort);
904 strcat(options, " sort");
906 if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_1))
908 ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_1);
909 assoc->version = 2; /* 1 & 2 are equivalent */
911 if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_2))
913 ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_2);
916 if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_3))
918 ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_3);
921 yaz_log(LOG_LOG, "Negotiated to v%d: %s", assoc->version, options);
922 assoc->maximumRecordSize = *req->maximumRecordSize;
923 if (assoc->maximumRecordSize > control_block->maxrecordsize)
924 assoc->maximumRecordSize = control_block->maxrecordsize;
925 assoc->preferredMessageSize = *req->preferredMessageSize;
926 if (assoc->preferredMessageSize > assoc->maximumRecordSize)
927 assoc->preferredMessageSize = assoc->maximumRecordSize;
928 resp->preferredMessageSize = &assoc->preferredMessageSize;
929 resp->maximumRecordSize = &assoc->maximumRecordSize;
931 resp->implementationName = "Index Data/YAZ Generic Frontend Server";
933 if (binitreq.implementation_name)
934 resp->implementationName = binitreq.implementation_name;
935 if (binitreq.implementation_version)
936 resp->implementationVersion = binitreq.implementation_version;
938 if (binitres->errcode)
940 yaz_log(LOG_LOG, "Connection rejected by backend.");
942 assoc->state = ASSOC_DEAD;
945 assoc->state = ASSOC_UP;
950 * These functions should be merged.
953 static void set_addinfo (Z_DefaultDiagFormat *dr, char *addinfo)
956 dr->which = Z_DefaultDiagFormat_v2Addinfo;
957 dr->u.v2Addinfo = addinfo ? addinfo : "";
959 dr->which = Z_DiagForm_v2AddInfo;
960 dr->addinfo = addinfo ? addinfo : "";
965 * nonsurrogate diagnostic record.
967 static Z_Records *diagrec(association *assoc, int error, char *addinfo)
970 Z_Records *rec = (Z_Records *)
971 odr_malloc (assoc->encode, sizeof(*rec));
974 odr_malloc (assoc->encode, sizeof(*err));
975 Z_DiagRec *drec = (Z_DiagRec *)
976 odr_malloc (assoc->encode, sizeof(*drec));
977 Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
978 odr_malloc (assoc->encode, sizeof(*dr));
980 bib1.proto = assoc->proto;
981 bib1.oclass = CLASS_DIAGSET;
982 bib1.value = VAL_BIB1;
984 yaz_log(LOG_DEBUG, "Diagnostic: %d -- %s", error, addinfo ? addinfo :
987 rec->which = Z_Records_NSD;
989 rec->u.nonSurrogateDiagnostic = dr;
991 rec->u.nonSurrogateDiagnostic = drec;
992 drec->which = Z_DiagRec_defaultFormat;
993 drec->u.defaultFormat = dr;
995 dr->diagnosticSetId =
996 odr_oiddup (assoc->encode, oid_ent_to_oid(&bib1, oid));
998 set_addinfo (dr, addinfo);
1003 * surrogate diagnostic.
1005 static Z_NamePlusRecord *surrogatediagrec(association *assoc, char *dbname,
1006 int error, char *addinfo)
1009 Z_NamePlusRecord *rec = (Z_NamePlusRecord *)
1010 odr_malloc (assoc->encode, sizeof(*rec));
1011 int *err = (int *)odr_malloc (assoc->encode, sizeof(*err));
1013 Z_DiagRec *drec = (Z_DiagRec *)odr_malloc (assoc->encode, sizeof(*drec));
1014 Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
1015 odr_malloc (assoc->encode, sizeof(*dr));
1017 bib1.proto = assoc->proto;
1018 bib1.oclass = CLASS_DIAGSET;
1019 bib1.value = VAL_BIB1;
1021 yaz_log(LOG_DEBUG, "SurrogateDiagnotic: %d -- %s", error, addinfo);
1023 rec->databaseName = dbname;
1024 rec->which = Z_NamePlusRecord_surrogateDiagnostic;
1025 rec->u.surrogateDiagnostic = drec;
1026 drec->which = Z_DiagRec_defaultFormat;
1027 drec->u.defaultFormat = dr;
1028 dr->diagnosticSetId = odr_oiddup (assoc->encode,
1029 oid_ent_to_oid(&bib1, oid));
1030 dr->condition = err;
1031 set_addinfo (dr, addinfo);
1037 * multiple nonsurrogate diagnostics.
1039 static Z_DiagRecs *diagrecs(association *assoc, int error, char *addinfo)
1042 Z_DiagRecs *recs = (Z_DiagRecs *)odr_malloc (assoc->encode, sizeof(*recs));
1043 int *err = (int *)odr_malloc (assoc->encode, sizeof(*err));
1045 Z_DiagRec **recp = (Z_DiagRec **)odr_malloc (assoc->encode, sizeof(*recp));
1046 Z_DiagRec *drec = (Z_DiagRec *)odr_malloc (assoc->encode, sizeof(*drec));
1047 Z_DefaultDiagFormat *rec = (Z_DefaultDiagFormat *)odr_malloc (assoc->encode, sizeof(*rec));
1049 yaz_log(LOG_DEBUG, "DiagRecs: %d -- %s", error, addinfo ? addinfo : "");
1050 bib1.proto = assoc->proto;
1051 bib1.oclass = CLASS_DIAGSET;
1052 bib1.value = VAL_BIB1;
1055 recs->num_diagRecs = 1;
1056 recs->diagRecs = recp;
1058 drec->which = Z_DiagRec_defaultFormat;
1059 drec->u.defaultFormat = rec;
1061 rec->diagnosticSetId = odr_oiddup (assoc->encode,
1062 oid_ent_to_oid(&bib1, oid));
1063 rec->condition = err;
1066 rec->which = Z_DefaultDiagFormat_v2Addinfo;
1067 rec->u.v2Addinfo = addinfo ? addinfo : "";
1069 rec->which = Z_DiagForm_v2AddInfo;
1070 rec->addinfo = addinfo ? addinfo : "";
1075 static Z_Records *pack_records(association *a, char *setname, int start,
1076 int *num, Z_RecordComposition *comp,
1077 int *next, int *pres, oid_value format,
1078 Z_ReferenceId *referenceId)
1080 int recno, total_length = 0, toget = *num, dumped_records = 0;
1081 Z_Records *records =
1082 (Z_Records *) odr_malloc (a->encode, sizeof(*records));
1083 Z_NamePlusRecordList *reclist =
1084 (Z_NamePlusRecordList *) odr_malloc (a->encode, sizeof(*reclist));
1085 Z_NamePlusRecord **list =
1086 (Z_NamePlusRecord **) odr_malloc (a->encode, sizeof(*list) * toget);
1088 records->which = Z_Records_DBOSD;
1089 records->u.databaseOrSurDiagnostics = reclist;
1090 reclist->num_records = 0;
1091 reclist->records = list;
1092 *pres = Z_PRES_SUCCESS;
1096 yaz_log(LOG_LOG, "Request to pack %d+%d", start, toget);
1097 yaz_log(LOG_DEBUG, "pms=%d, mrs=%d", a->preferredMessageSize,
1098 a->maximumRecordSize);
1099 for (recno = start; reclist->num_records < toget; recno++)
1101 bend_fetchrequest freq;
1102 bend_fetchresult *fres;
1103 Z_NamePlusRecord *thisrec;
1104 int this_length = 0;
1106 * we get the number of bytes allocated on the stream before any
1107 * allocation done by the backend - this should give us a reasonable
1108 * idea of the total size of the data so far.
1110 total_length = odr_total(a->encode) - dumped_records;
1111 freq.setname = setname;
1112 freq.number = recno;
1114 freq.format = format;
1115 freq.stream = a->encode;
1116 freq.surrogate_flag = 0;
1117 freq.referenceId = referenceId;
1118 if (!(fres = bend_fetch(a->backend, &freq, 0)))
1120 *pres = Z_PRES_FAILURE;
1121 return diagrec(a, 2, "Backend interface problem");
1123 /* backend should be able to signal whether error is system-wide
1124 or only pertaining to current record */
1127 if (!freq.surrogate_flag)
1129 *pres = Z_PRES_FAILURE;
1130 return diagrec(a, fres->errcode, fres->errstring);
1132 reclist->records[reclist->num_records] =
1133 surrogatediagrec(a, fres->basename, fres->errcode,
1135 reclist->num_records++;
1136 *next = fres->last_in_set ? 0 : recno + 1;
1140 this_length = fres->len;
1142 this_length = odr_total(a->encode) - total_length;
1143 yaz_log(LOG_DEBUG, " fetched record, len=%d, total=%d",
1144 this_length, total_length);
1145 if (this_length + total_length > a->preferredMessageSize)
1147 /* record is small enough, really */
1148 if (this_length <= a->preferredMessageSize)
1150 yaz_log(LOG_DEBUG, " Dropped last normal-sized record");
1151 *pres = Z_PRES_PARTIAL_2;
1154 /* record can only be fetched by itself */
1155 if (this_length < a->maximumRecordSize)
1157 yaz_log(LOG_DEBUG, " Record > prefmsgsz");
1160 yaz_log(LOG_DEBUG, " Dropped it");
1161 reclist->records[reclist->num_records] =
1162 surrogatediagrec(a, fres->basename, 16, 0);
1163 reclist->num_records++;
1164 *next = fres->last_in_set ? 0 : recno + 1;
1165 dumped_records += this_length;
1169 else /* too big entirely */
1171 yaz_log(LOG_DEBUG, "Record > maxrcdsz");
1172 reclist->records[reclist->num_records] =
1173 surrogatediagrec(a, fres->basename, 17, 0);
1174 reclist->num_records++;
1175 *next = fres->last_in_set ? 0 : recno + 1;
1176 dumped_records += this_length;
1181 if (!(thisrec = (Z_NamePlusRecord *)
1182 odr_malloc(a->encode, sizeof(*thisrec))))
1184 if (!(thisrec->databaseName = (char *)odr_malloc(a->encode,
1185 strlen(fres->basename) + 1)))
1187 strcpy(thisrec->databaseName, fres->basename);
1188 thisrec->which = Z_NamePlusRecord_databaseRecord;
1189 thisrec->u.databaseRecord = z_ext_record(a->encode, fres->format,
1190 fres->record, fres->len);
1191 if (!thisrec->u.databaseRecord)
1193 reclist->records[reclist->num_records] = thisrec;
1194 reclist->num_records++;
1195 *next = fres->last_in_set ? 0 : recno + 1;
1197 *num = reclist->num_records;
1201 static Z_APDU *process_searchRequest(association *assoc, request *reqb,
1204 Z_SearchRequest *req = reqb->request->u.searchRequest;
1205 bend_search_rr *bsrr =
1206 (bend_search_rr *)nmem_malloc (reqb->request_mem, sizeof(*bsrr));
1208 yaz_log(LOG_LOG, "Got SearchRequest.");
1210 bsrr->request = reqb;
1211 bsrr->association = assoc;
1212 bsrr->referenceId = req->referenceId;
1213 save_referenceId (reqb, bsrr->referenceId);
1215 yaz_log (LOG_LOG, "ResultSet '%s'", req->resultSetName);
1216 if (req->databaseNames)
1219 for (i = 0; i < req->num_databaseNames; i++)
1220 yaz_log (LOG_LOG, "Database '%s'", req->databaseNames[i]);
1222 switch (req->query->which)
1224 case Z_Query_type_1: case Z_Query_type_101:
1225 log_rpn_query (req->query->u.type_1);
1227 if (assoc->bend_search)
1229 bsrr->setname = req->resultSetName;
1230 bsrr->replace_set = *req->replaceIndicator;
1231 bsrr->num_bases = req->num_databaseNames;
1232 bsrr->basenames = req->databaseNames;
1233 bsrr->query = req->query;
1234 bsrr->stream = assoc->encode;
1235 bsrr->decode = assoc->decode;
1238 bsrr->errstring = NULL;
1239 ((int (*)(void *, bend_search_rr *))(assoc->bend_search))
1240 (assoc->backend, bsrr);
1246 bend_searchrequest bsrq;
1247 bend_searchresult *bsrt;
1249 bsrq.setname = req->resultSetName;
1250 bsrq.replace_set = *req->replaceIndicator;
1251 bsrq.num_bases = req->num_databaseNames;
1252 bsrq.basenames = req->databaseNames;
1253 bsrq.query = req->query;
1254 bsrq.referenceId = req->referenceId;
1255 bsrq.stream = assoc->encode;
1256 bsrq.decode = assoc->decode;
1257 if (!(bsrt = bend_search (assoc->backend, &bsrq, fd)))
1259 bsrr->hits = bsrt->hits;
1260 bsrr->errcode = bsrt->errcode;
1261 bsrr->errstring = bsrt->errstring;
1263 return response_searchRequest(assoc, reqb, bsrr, fd);
1266 int bend_searchresponse(void *handle, bend_search_rr *bsrr) {return 0;}
1269 * Prepare a searchresponse based on the backend results. We probably want
1270 * to look at making the fetching of records nonblocking as well, but
1271 * so far, we'll keep things simple.
1272 * If bsrt is null, that means we're called in response to a communications
1273 * event, and we'll have to get the response for ourselves.
1275 static Z_APDU *response_searchRequest(association *assoc, request *reqb,
1276 bend_search_rr *bsrt, int *fd)
1278 Z_SearchRequest *req = reqb->request->u.searchRequest;
1279 Z_APDU *apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1280 Z_SearchResponse *resp = (Z_SearchResponse *)
1281 odr_malloc (assoc->encode, sizeof(*resp));
1282 int *nulint = (int *)odr_malloc (assoc->encode, sizeof(*nulint));
1283 bool_t *sr = (bool_t *)odr_malloc (assoc->encode, sizeof(*sr));
1284 int *next = (int *)odr_malloc (assoc->encode, sizeof(*next));
1285 int *none = (int *)odr_malloc (assoc->encode, sizeof(*none));
1292 apdu->which = Z_APDU_searchResponse;
1293 apdu->u.searchResponse = resp;
1294 resp->referenceId = req->referenceId;
1295 resp->additionalSearchInfo = 0;
1296 resp->otherInfo = 0;
1298 if (!bsrt && !bend_searchresponse(assoc->backend, bsrt))
1300 yaz_log(LOG_FATAL, "Bad result from backend");
1303 else if (bsrt->errcode)
1305 resp->records = diagrec(assoc, bsrt->errcode, bsrt->errstring);
1306 resp->resultCount = nulint;
1307 resp->numberOfRecordsReturned = nulint;
1308 resp->nextResultSetPosition = nulint;
1309 resp->searchStatus = nulint;
1310 resp->resultSetStatus = none;
1311 resp->presentStatus = 0;
1315 int *toget = (int *)odr_malloc (assoc->encode, sizeof(*toget));
1316 int *presst = (int *)odr_malloc (assoc->encode, sizeof(*presst));
1317 Z_RecordComposition comp, *compp = 0;
1322 resp->resultCount = &bsrt->hits;
1324 comp.which = Z_RecordComp_simple;
1325 /* how many records does the user agent want, then? */
1326 if (bsrt->hits <= *req->smallSetUpperBound)
1328 *toget = bsrt->hits;
1329 if ((comp.u.simple = req->smallSetElementSetNames))
1332 else if (bsrt->hits < *req->largeSetLowerBound)
1334 *toget = *req->mediumSetPresentNumber;
1335 if (*toget > bsrt->hits)
1336 *toget = bsrt->hits;
1337 if ((comp.u.simple = req->mediumSetElementSetNames))
1343 if (*toget && !resp->records)
1348 if (!(prefformat = oid_getentbyoid(req->preferredRecordSyntax)) ||
1349 prefformat->oclass != CLASS_RECSYN)
1352 form = prefformat->value;
1353 resp->records = pack_records(assoc, req->resultSetName, 1,
1354 toget, compp, next, presst, form, req->referenceId);
1357 resp->numberOfRecordsReturned = toget;
1358 resp->nextResultSetPosition = next;
1359 resp->searchStatus = sr;
1360 resp->resultSetStatus = 0;
1361 resp->presentStatus = presst;
1365 if (*resp->resultCount)
1367 resp->numberOfRecordsReturned = nulint;
1368 resp->nextResultSetPosition = next;
1369 resp->searchStatus = sr;
1370 resp->resultSetStatus = 0;
1371 resp->presentStatus = 0;
1378 * Maybe we got a little over-friendly when we designed bend_fetch to
1379 * get only one record at a time. Some backends can optimise multiple-record
1380 * fetches, and at any rate, there is some overhead involved in
1381 * all that selecting and hopping around. Problem is, of course, that the
1382 * frontend can't know ahead of time how many records it'll need to
1383 * fill the negotiated PDU size. Annoying. Segmentation or not, Z/SR
1384 * is downright lousy as a bulk data transfer protocol.
1386 * To start with, we'll do the fetching of records from the backend
1387 * in one operation: To save some trips in and out of the event-handler,
1388 * and to simplify the interface to pack_records. At any rate, asynch
1389 * operation is more fun in operations that have an unpredictable execution
1390 * speed - which is normally more true for search than for present.
1392 static Z_APDU *process_presentRequest(association *assoc, request *reqb,
1395 Z_PresentRequest *req = reqb->request->u.presentRequest;
1399 Z_PresentResponse *resp;
1404 yaz_log(LOG_LOG, "Got PresentRequest.");
1406 if (!(prefformat = oid_getentbyoid(req->preferredRecordSyntax)) ||
1407 prefformat->oclass != CLASS_RECSYN)
1410 form = prefformat->value;
1411 if (assoc->bend_present)
1413 bend_present_rr *bprr = (bend_present_rr *)
1414 nmem_malloc (reqb->request_mem, sizeof(*bprr));
1415 bprr->setname = req->resultSetId;
1416 bprr->start = *req->resultSetStartPoint;
1417 bprr->number = *req->numberOfRecordsRequested;
1418 bprr->format = form;
1419 bprr->comp = req->recordComposition;
1420 bprr->referenceId = req->referenceId;
1421 bprr->stream = assoc->encode;
1422 bprr->request = reqb;
1423 bprr->association = assoc;
1425 bprr->errstring = NULL;
1426 ((int (*)(void *, bend_present_rr *))(*assoc->bend_present))(
1427 assoc->backend, bprr);
1432 apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1433 resp = (Z_PresentResponse *)odr_malloc (assoc->encode, sizeof(*resp));
1434 presst = (int *)odr_malloc (assoc->encode, sizeof(*presst));
1435 next = (int *)odr_malloc (assoc->encode, sizeof(*next));
1436 num = (int *)odr_malloc (assoc->encode, sizeof(*num));
1439 *num = *req->numberOfRecordsRequested;
1441 apdu->which = Z_APDU_presentResponse;
1442 apdu->u.presentResponse = resp;
1443 resp->referenceId = req->referenceId;
1444 resp->otherInfo = 0;
1447 pack_records(assoc, req->resultSetId, *req->resultSetStartPoint,
1448 num, req->recordComposition, next, presst, form,
1452 resp->numberOfRecordsReturned = num;
1453 resp->presentStatus = presst;
1454 resp->nextResultSetPosition = next;
1459 static int bend_default_scan (void *handle, bend_scan_rr *rr)
1461 bend_scanrequest srq;
1462 bend_scanresult *srs;
1464 srq.num_bases = rr->num_bases;
1465 srq.basenames = rr->basenames;
1466 srq.attributeset = rr->attributeset;
1467 srq.referenceId = rr->referenceId;
1468 srq.term = rr->term;
1469 srq.term_position = rr->term_position;
1470 srq.num_entries = rr->num_entries;
1471 srq.stream = rr->stream;
1473 srs = bend_scan(handle, &srq, 0);
1475 rr->term_position = srs->term_position;
1476 rr->num_entries = srs->num_entries;
1477 rr->entries = srs->entries;
1478 rr->status = srs->status;
1479 rr->errcode = srs->errcode;
1480 rr->errstring = srs->errstring;
1485 * Scan was implemented rather in a hurry, and with support for only the basic
1486 * elements of the service in the backend API. Suggestions are welcome.
1488 static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd)
1490 Z_ScanRequest *req = reqb->request->u.scanRequest;
1491 Z_APDU *apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1492 Z_ScanResponse *res = (Z_ScanResponse *)
1493 odr_malloc (assoc->encode, sizeof(*res));
1494 int *scanStatus = (int *)
1495 odr_malloc (assoc->encode, sizeof(*scanStatus));
1496 int *numberOfEntriesReturned = (int *)
1497 odr_malloc (assoc->encode, sizeof(*numberOfEntriesReturned));
1498 Z_ListEntries *ents = (Z_ListEntries *)
1499 odr_malloc (assoc->encode, sizeof(*ents));
1500 Z_DiagRecs *diagrecs_p = NULL;
1504 yaz_log(LOG_LOG, "Got ScanRequest");
1505 *scanStatus = Z_Scan_failure;
1506 *numberOfEntriesReturned = 0;
1508 apdu->which = Z_APDU_scanResponse;
1509 apdu->u.scanResponse = res;
1510 res->referenceId = req->referenceId;
1512 res->scanStatus = scanStatus;
1513 res->numberOfEntriesReturned = numberOfEntriesReturned;
1514 res->positionOfTerm = 0;
1515 res->entries = ents;
1516 ents->num_entries = 0;
1517 ents->entries = NULL;
1518 ents->num_nonsurrogateDiagnostics = 0;
1519 ents->nonsurrogateDiagnostics = NULL;
1520 res->attributeSet = 0;
1523 if (req->attributeSet && (!(attent = oid_getentbyoid(req->attributeSet)) ||
1524 attent->oclass != CLASS_ATTSET
1525 || attent->value != VAL_BIB1))
1526 diagrecs_p = diagrecs(assoc, 121, 0);
1527 else if (req->stepSize && *req->stepSize > 0)
1528 diagrecs_p = diagrecs(assoc, 205, 0);
1531 bend_scan_rr *bsrr = (bend_scan_rr *)
1532 odr_malloc (assoc->encode, sizeof(*bsrr));
1533 if (req->databaseNames)
1536 for (i = 0; i < req->num_databaseNames; i++)
1537 yaz_log (LOG_LOG, "Database '%s'", req->databaseNames[i]);
1539 bsrr->num_bases = req->num_databaseNames;
1540 bsrr->basenames = req->databaseNames;
1541 bsrr->num_entries = *req->numberOfTermsRequested;
1542 bsrr->term = req->termListAndStartPoint;
1543 bsrr->referenceId = req->referenceId;
1544 bsrr->stream = assoc->encode;
1545 if (!(attset = oid_getentbyoid(req->attributeSet)) ||
1546 attset->oclass != CLASS_RECSYN)
1547 bsrr->attributeset = VAL_NONE;
1549 bsrr->attributeset = attset->value;
1550 log_scan_term (req->termListAndStartPoint, attset->value);
1551 bsrr->term_position = req->preferredPositionInResponse ?
1552 *req->preferredPositionInResponse : 1;
1553 ((int (*)(void *, bend_scan_rr *))(*assoc->bend_scan))(assoc->backend,
1556 diagrecs_p = diagrecs(assoc, bsrr->errcode, bsrr->errstring);
1560 Z_Entry **tab = (Z_Entry **)
1561 odr_malloc (assoc->encode, sizeof(*tab) * bsrr->num_entries);
1563 if (bsrr->status == BEND_SCAN_PARTIAL)
1564 *scanStatus = Z_Scan_partial_5;
1566 *scanStatus = Z_Scan_success;
1567 ents->entries = tab;
1568 ents->num_entries = bsrr->num_entries;
1569 res->numberOfEntriesReturned = &ents->num_entries;
1570 res->positionOfTerm = &bsrr->term_position;
1571 for (i = 0; i < bsrr->num_entries; i++)
1577 tab[i] = e = (Z_Entry *)odr_malloc(assoc->encode, sizeof(*e));
1578 if (bsrr->entries[i].occurrences >= 0)
1580 e->which = Z_Entry_termInfo;
1581 e->u.termInfo = t = (Z_TermInfo *)
1582 odr_malloc(assoc->encode, sizeof(*t));
1583 t->suggestedAttributes = 0;
1585 t->alternativeTerm = 0;
1586 t->byAttributes = 0;
1587 t->otherTermInfo = 0;
1588 t->globalOccurrences = &bsrr->entries[i].occurrences;
1589 t->term = (Z_Term *)
1590 odr_malloc(assoc->encode, sizeof(*t->term));
1591 t->term->which = Z_Term_general;
1592 t->term->u.general = o =
1593 (Odr_oct *)odr_malloc(assoc->encode, sizeof(Odr_oct));
1594 o->buf = (unsigned char *)
1595 odr_malloc(assoc->encode, o->len = o->size =
1596 strlen(bsrr->entries[i].term));
1597 memcpy(o->buf, bsrr->entries[i].term, o->len);
1598 yaz_log(LOG_DEBUG, " term #%d: '%s' (%d)", i,
1599 bsrr->entries[i].term, bsrr->entries[i].occurrences);
1603 Z_DiagRecs *drecs = diagrecs (assoc,
1604 bsrr->entries[i].errcode,
1605 bsrr->entries[i].errstring);
1606 assert (drecs->num_diagRecs == 1);
1607 e->which = Z_Entry_surrogateDiagnostic;
1608 assert (drecs->diagRecs[0]);
1609 e->u.surrogateDiagnostic = drecs->diagRecs[0];
1616 ents->num_nonsurrogateDiagnostics = diagrecs_p->num_diagRecs;
1617 ents->nonsurrogateDiagnostics = diagrecs_p->diagRecs;
1622 static Z_APDU *process_sortRequest(association *assoc, request *reqb,
1625 Z_SortRequest *req = reqb->request->u.sortRequest;
1626 Z_SortResponse *res = (Z_SortResponse *)
1627 odr_malloc (assoc->encode, sizeof(*res));
1628 bend_sort_rr *bsrr = (bend_sort_rr *)
1629 odr_malloc (assoc->encode, sizeof(*bsrr));
1631 Z_APDU *apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1633 yaz_log(LOG_LOG, "Got SortRequest.");
1636 bsrr->num_input_setnames = req->num_inputResultSetNames;
1637 bsrr->input_setnames = req->inputResultSetNames;
1639 bsrr->num_input_setnames = req->inputResultSetNames->num_strings;
1640 bsrr->input_setnames = req->inputResultSetNames->strings;
1642 bsrr->referenceId = req->referenceId;
1643 bsrr->output_setname = req->sortedResultSetName;
1644 bsrr->sort_sequence = req->sortSequence;
1645 bsrr->stream = assoc->encode;
1647 bsrr->sort_status = Z_SortStatus_failure;
1649 bsrr->errstring = 0;
1651 ((int (*)(void *, bend_sort_rr *))(*assoc->bend_sort))(assoc->backend,
1654 res->referenceId = bsrr->referenceId;
1655 res->sortStatus = (int *)
1656 odr_malloc (assoc->encode, sizeof(*res->sortStatus));
1657 *res->sortStatus = bsrr->sort_status;
1658 res->resultSetStatus = 0;
1661 Z_DiagRecs *dr = diagrecs (assoc, bsrr->errcode, bsrr->errstring);
1663 res->diagnostics = dr->diagRecs;
1664 res->num_diagnostics = dr->num_diagRecs;
1666 res->diagnostics = dr;
1672 res->num_diagnostics = 0;
1674 res->diagnostics = 0;
1678 apdu->which = Z_APDU_sortResponse;
1679 apdu->u.sortResponse = res;
1683 static Z_APDU *process_deleteRequest(association *assoc, request *reqb,
1686 Z_DeleteResultSetRequest *req = reqb->request->u.deleteResultSetRequest;
1687 Z_DeleteResultSetResponse *res = (Z_DeleteResultSetResponse *)
1688 odr_malloc (assoc->encode, sizeof(*res));
1689 bend_delete_rr *bdrr = (bend_delete_rr *)
1690 odr_malloc (assoc->encode, sizeof(*bdrr));
1691 Z_APDU *apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1693 yaz_log(LOG_LOG, "Got DeleteRequest.");
1695 bdrr->num_setnames = req->num_resultSetList;
1696 bdrr->setnames = req->resultSetList;
1697 bdrr->stream = assoc->encode;
1698 bdrr->function = *req->deleteFunction;
1699 bdrr->referenceId = req->referenceId;
1701 if (bdrr->num_setnames > 0)
1704 bdrr->statuses = odr_malloc(assoc->encode, sizeof(*bdrr->statuses) *
1705 bdrr->num_setnames);
1706 for (i = 0; i < bdrr->num_setnames; i++)
1707 bdrr->statuses[i] = 0;
1709 ((int (*)(void *, bend_delete_rr *))
1710 (*assoc->bend_delete))(assoc->backend, bdrr);
1712 res->referenceId = req->referenceId;
1714 res->deleteOperationStatus = (int *)
1715 odr_malloc (assoc->encode, sizeof(*res->deleteOperationStatus));
1716 *res->deleteOperationStatus = bdrr->delete_status;
1718 res->deleteListStatuses = 0;
1719 if (bdrr->num_setnames > 0)
1722 res->deleteListStatuses = odr_malloc(assoc->encode,
1723 sizeof(*res->deleteListStatuses));
1724 res->deleteListStatuses->num = bdrr->num_setnames;
1725 res->deleteListStatuses->elements =
1726 odr_malloc (assoc->encode,
1727 sizeof(*res->deleteListStatuses->elements) *
1728 bdrr->num_setnames);
1729 for (i = 0; i<bdrr->num_setnames; i++)
1731 res->deleteListStatuses->elements[i] =
1732 odr_malloc (assoc->encode,
1733 sizeof(**res->deleteListStatuses->elements));
1734 res->deleteListStatuses->elements[i]->status = bdrr->statuses+i;
1735 res->deleteListStatuses->elements[i]->id =
1736 odr_strdup (assoc->encode, bdrr->setnames[i]);
1740 res->numberNotDeleted = 0;
1741 res->bulkStatuses = 0;
1742 res->deleteMessage = 0;
1745 apdu->which = Z_APDU_deleteResultSetResponse;
1746 apdu->u.deleteResultSetResponse = res;
1750 static void process_close(association *assoc, request *reqb)
1752 Z_Close *req = reqb->request->u.close;
1753 static char *reasons[] =
1760 "securityViolation",
1767 yaz_log(LOG_LOG, "Got Close, reason %s, message %s",
1768 reasons[*req->closeReason], req->diagnosticInformation ?
1769 req->diagnosticInformation : "NULL");
1770 if (assoc->version < 3) /* to make do_force respond with close */
1772 do_close_req(assoc, Z_Close_finished,
1773 "Association terminated by client", reqb);
1776 void save_referenceId (request *reqb, Z_ReferenceId *refid)
1780 reqb->len_refid = refid->len;
1781 reqb->refid = (char *)nmem_malloc (reqb->request_mem, refid->len);
1782 memcpy (reqb->refid, refid->buf, refid->len);
1786 reqb->len_refid = 0;
1791 void bend_request_send (bend_association a, bend_request req, Z_APDU *res)
1793 process_response (a, req, res);
1796 bend_request bend_request_mk (bend_association a)
1798 request *nreq = request_get (&a->outgoing);
1799 nreq->request_mem = nmem_create ();
1803 Z_ReferenceId *bend_request_getid (ODR odr, bend_request req)
1808 id = (Odr_oct *)odr_malloc (odr, sizeof(*odr));
1809 id->buf = (unsigned char *)odr_malloc (odr, req->len_refid);
1810 id->len = id->size = req->len_refid;
1811 memcpy (id->buf, req->refid, req->len_refid);
1815 void bend_request_destroy (bend_request *req)
1817 nmem_destroy((*req)->request_mem);
1818 request_release(*req);
1822 int bend_backend_respond (bend_association a, bend_request req)
1826 r = process_request (a, req, &msg);
1828 logf (LOG_WARN, "%s", msg);
1832 void bend_request_setdata(bend_request r, void *p)
1837 void *bend_request_getdata(bend_request r)
1839 return r->clientData;
1842 static Z_APDU *process_ESRequest(association *assoc, request *reqb, int *fd)
1844 bend_esrequest_rr esrequest;
1846 Z_ExtendedServicesRequest *req = reqb->request->u.extendedServicesRequest;
1847 Z_APDU *apdu = zget_APDU(assoc->encode, Z_APDU_extendedServicesResponse);
1849 Z_ExtendedServicesResponse *resp = apdu->u.extendedServicesResponse;
1851 yaz_log(LOG_DEBUG,"inside Process esRequest");
1853 esrequest.esr = reqb->request->u.extendedServicesRequest;
1854 esrequest.stream = assoc->encode;
1855 esrequest.errcode = 0;
1856 esrequest.errstring = NULL;
1857 esrequest.request = reqb;
1858 esrequest.association = assoc;
1859 esrequest.referenceId = req->referenceId;
1861 ((int (*)(void *, bend_esrequest_rr *))(*assoc->bend_esrequest))
1862 (assoc->backend, &esrequest);
1864 /* If the response is being delayed, return NULL */
1865 if (esrequest.request == NULL)
1868 resp->referenceId = req->referenceId;
1870 if (esrequest.errcode == -1)
1872 /* Backend service indicates request will be processed */
1873 yaz_log(LOG_DEBUG,"Request could be processed...Accepted !");
1874 *resp->operationStatus = Z_ExtendedServicesResponse_accepted;
1876 else if (esrequest.errcode == 0)
1878 /* Backend service indicates request will be processed */
1879 yaz_log(LOG_DEBUG,"Request could be processed...Done !");
1880 *resp->operationStatus = Z_ExtendedServicesResponse_done;
1884 Z_DiagRecs *diagRecs = diagrecs (assoc, esrequest.errcode,
1885 esrequest.errstring);
1887 /* Backend indicates error, request will not be processed */
1888 yaz_log(LOG_DEBUG,"Request could not be processed...failure !");
1889 *resp->operationStatus = Z_ExtendedServicesResponse_failure;
1890 resp->num_diagnostics = diagRecs->num_diagRecs;
1891 resp->diagnostics = diagRecs->diagRecs;
1893 /* Do something with the members of bend_extendedservice */
1895 yaz_log(LOG_DEBUG,"Send the result apdu");