source: trunk/gsdl/packages/yaz/server/seshigh.c@ 1343

Last change on this file since 1343 was 1343, checked in by johnmcp, 24 years ago

Added the YAZ toolkit source to the packages directory (for z39.50 stuff)

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