2 * Copyright (c) 1995-2002, Index Data
3 * See the file LICENSE for details.
5 * $Id: seshigh.c,v 1.129 2002-04-18 13:18:47 adam Exp $
9 * Frontend server logic.
11 * This code receives incoming APDUs, and handles client requests by means
14 * Some of the code is getting quite involved, compared to simpler servers -
15 * primarily because it is asynchronous both in the communication with
16 * the user and the backend. We think the complexity will pay off in
17 * the form of greater flexibility when more asynchronous facilities
20 * Memory management has become somewhat involved. In the simple case, where
21 * only one PDU is pending at a time, it will simply reuse the same memory,
22 * once it has found its working size. When we enable multiple concurrent
23 * operations, perhaps even with multiple parallel calls to the backend, it
24 * will maintain a pool of buffers for encoding and decoding, trying to
25 * minimize memory allocation/deallocation during normal operation.
38 #include <yaz/yconfig.h>
39 #include <yaz/xmalloc.h>
40 #include <yaz/comstack.h>
43 #include <yaz/proto.h>
46 #include <yaz/logrpn.h>
47 #include <yaz/statserv.h>
48 #include <yaz/diagbib1.h>
50 #include <yaz/backend.h>
52 static int process_request(association *assoc, request *req, char **msg);
53 void backend_response(IOCHAN i, int event);
54 static int process_response(association *assoc, request *req, Z_APDU *res);
55 static Z_APDU *process_initRequest(association *assoc, request *reqb);
56 static Z_APDU *process_searchRequest(association *assoc, request *reqb,
58 static Z_APDU *response_searchRequest(association *assoc, request *reqb,
59 bend_search_rr *bsrr, int *fd);
60 static Z_APDU *process_presentRequest(association *assoc, request *reqb,
62 static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd);
63 static Z_APDU *process_sortRequest(association *assoc, request *reqb, int *fd);
64 static void process_close(association *assoc, request *reqb);
65 void save_referenceId (request *reqb, Z_ReferenceId *refid);
66 static Z_APDU *process_deleteRequest(association *assoc, request *reqb,
68 static Z_APDU *process_segmentRequest (association *assoc, request *reqb);
70 static FILE *apduf = 0; /* for use in static mode */
71 static statserv_options_block *control_block = 0;
73 /* Chas: Added in from DALI */
74 static Z_APDU *process_ESRequest(association *assoc, request *reqb, int *fd);
75 /* Chas: End of addition from DALI */
78 * Create and initialize a new association-handle.
79 * channel : iochannel for the current line.
80 * link : communications channel.
81 * Returns: 0 or a new association handle.
83 association *create_association(IOCHAN channel, COMSTACK link)
88 control_block = statserv_getcontrol();
89 if (!(anew = (association *)xmalloc(sizeof(*anew))))
92 anew->client_chan = channel;
93 anew->client_link = link;
94 anew->cs_get_mask = 0;
95 anew->cs_put_mask = 0;
96 anew->cs_accept_mask = 0;
97 if (!(anew->decode = odr_createmem(ODR_DECODE)) ||
98 !(anew->encode = odr_createmem(ODR_ENCODE)))
100 if (*control_block->apdufile)
105 strcpy(filename, control_block->apdufile);
106 if (!(anew->print = odr_createmem(ODR_PRINT)))
108 if (*control_block->apdufile != '-')
110 strcpy(filename, control_block->apdufile);
111 if (!control_block->dynamic)
115 if (!(apduf = fopen(filename, "w")))
117 yaz_log(LOG_WARN|LOG_ERRNO, "%s", filename);
120 setvbuf(apduf, 0, _IONBF, 0);
126 sprintf(filename + strlen(filename), ".%d", getpid());
127 if (!(f = fopen(filename, "w")))
129 yaz_log(LOG_WARN|LOG_ERRNO, "%s", filename);
132 setvbuf(f, 0, _IONBF, 0);
134 odr_setprint(anew->print, f);
139 anew->input_buffer = 0;
140 anew->input_buffer_len = 0;
142 anew->state = ASSOC_NEW;
143 request_initq(&anew->incoming);
144 request_initq(&anew->outgoing);
145 anew->proto = cs_getproto(link);
150 * Free association and release resources.
152 void destroy_association(association *h)
154 statserv_options_block *cb = statserv_getcontrol();
157 odr_destroy(h->decode);
158 odr_destroy(h->encode);
160 odr_destroy(h->print);
162 xfree(h->input_buffer);
164 (*cb->bend_close)(h->backend);
165 while (request_deq(&h->incoming));
166 while (request_deq(&h->outgoing));
167 request_delq(&h->incoming);
168 request_delq(&h->outgoing);
170 if (control_block && control_block->one_shot)
174 static void do_close_req(association *a, int reason, char *message,
178 Z_Close *cls = zget_Close(a->encode);
180 /* Purge request queue */
181 while (request_deq(&a->incoming));
182 while (request_deq(&a->outgoing));
185 yaz_log(LOG_LOG, "Sending Close PDU, reason=%d, message=%s",
186 reason, message ? message : "none");
187 apdu.which = Z_APDU_close;
189 *cls->closeReason = reason;
190 cls->diagnosticInformation = message;
191 process_response(a, req, &apdu);
192 iochan_settimeout(a->client_chan, 60);
196 yaz_log(LOG_DEBUG, "v2 client. No Close PDU");
197 iochan_setevent(a->client_chan, EVENT_TIMEOUT); /* force imm close */
199 a->state = ASSOC_DEAD;
202 static void do_close(association *a, int reason, char *message)
204 do_close_req (a, reason, message, request_get(&a->outgoing));
208 * This is where PDUs from the client are read and the further
209 * processing is initiated. Flow of control moves down through the
210 * various process_* functions below, until the encoded result comes back up
211 * to the output handler in here.
213 * h : the I/O channel that has an outstanding event.
214 * event : the current outstanding event.
216 void ir_session(IOCHAN h, int event)
219 association *assoc = (association *)iochan_getdata(h);
220 COMSTACK conn = assoc->client_link;
223 assert(h && conn && assoc);
224 if (event == EVENT_TIMEOUT)
226 if (assoc->state != ASSOC_UP)
228 yaz_log(LOG_LOG, "Final timeout - closing connection.");
230 destroy_association(assoc);
235 yaz_log(LOG_LOG, "Session idle too long. Sending close.");
236 do_close(assoc, Z_Close_lackOfActivity, 0);
240 if (event & assoc->cs_accept_mask)
242 yaz_log (LOG_DEBUG, "ir_session (accept)");
243 if (!cs_accept (conn))
245 yaz_log (LOG_LOG, "accept failed");
246 destroy_association(assoc);
249 iochan_clearflag (h, EVENT_OUTPUT|EVENT_OUTPUT);
250 if (conn->io_pending)
251 { /* cs_accept didn't complete */
252 assoc->cs_accept_mask =
253 ((conn->io_pending & CS_WANT_WRITE) ? EVENT_OUTPUT : 0) |
254 ((conn->io_pending & CS_WANT_READ) ? EVENT_INPUT : 0);
256 iochan_setflag (h, assoc->cs_accept_mask);
259 { /* cs_accept completed. Prepare for reading (cs_get) */
260 assoc->cs_accept_mask = 0;
261 assoc->cs_get_mask = EVENT_INPUT;
262 iochan_setflag (h, assoc->cs_get_mask);
266 if ((event & assoc->cs_get_mask) || (event & EVENT_WORK)) /* input */
268 if ((assoc->cs_put_mask & EVENT_INPUT) == 0 && (event & assoc->cs_get_mask))
270 yaz_log(LOG_DEBUG, "ir_session (input)");
271 /* We aren't speaking to this fellow */
272 if (assoc->state == ASSOC_DEAD)
274 yaz_log(LOG_LOG, "Closed connection after reject");
276 destroy_association(assoc);
280 assoc->cs_get_mask = EVENT_INPUT;
281 if ((res = cs_get(conn, &assoc->input_buffer,
282 &assoc->input_buffer_len)) <= 0)
284 yaz_log(LOG_LOG, "Connection closed by client");
286 destroy_association(assoc);
290 else if (res == 1) /* incomplete read - wait for more */
292 if (conn->io_pending & CS_WANT_WRITE)
293 assoc->cs_get_mask |= EVENT_OUTPUT;
294 iochan_setflag(h, assoc->cs_get_mask);
297 if (cs_more(conn)) /* more stuff - call us again later, please */
298 iochan_setevent(h, EVENT_INPUT);
300 /* we got a complete PDU. Let's decode it */
301 yaz_log(LOG_DEBUG, "Got PDU, %d bytes", res);
302 req = request_get(&assoc->incoming); /* get a new request structure */
303 odr_reset(assoc->decode);
304 odr_setbuf(assoc->decode, assoc->input_buffer, res, 0);
305 if (!z_APDU(assoc->decode, &req->apdu_request, 0, 0))
307 yaz_log(LOG_LOG, "ODR error on incoming PDU: %s [near byte %d] ",
308 odr_errmsg(odr_geterror(assoc->decode)),
309 odr_offset(assoc->decode));
310 yaz_log(LOG_LOG, "PDU dump:");
311 odr_dumpBER(yaz_log_file(), assoc->input_buffer, res);
312 do_close(assoc, Z_Close_protocolError, "Malformed package");
315 req->request_mem = odr_extract_mem(assoc->decode);
316 if (assoc->print && !z_APDU(assoc->print, &req->apdu_request, 0, 0))
318 yaz_log(LOG_WARN, "ODR print error: %s",
319 odr_errmsg(odr_geterror(assoc->print)));
320 odr_reset(assoc->print);
322 request_enq(&assoc->incoming, req);
325 /* can we do something yet? */
326 req = request_head(&assoc->incoming);
327 if (req->state == REQUEST_IDLE)
330 request_deq(&assoc->incoming);
331 if (process_request(assoc, req, &msg) < 0)
332 do_close_req(assoc, Z_Close_systemProblem, msg, req);
335 if (event & assoc->cs_put_mask)
337 request *req = request_head(&assoc->outgoing);
339 assoc->cs_put_mask = 0;
340 yaz_log(LOG_DEBUG, "ir_session (output)");
341 req->state = REQUEST_PENDING;
342 switch (res = cs_put(conn, req->response, req->len_response))
345 yaz_log(LOG_LOG, "Connection closed by client");
347 destroy_association(assoc);
350 case 0: /* all sent - release the request structure */
351 yaz_log(LOG_DEBUG, "Wrote PDU, %d bytes", req->len_response);
352 nmem_destroy(req->request_mem);
353 request_deq(&assoc->outgoing);
354 request_release(req);
355 if (!request_head(&assoc->outgoing))
356 { /* restore mask for cs_get operation ... */
357 iochan_clearflag(h, EVENT_OUTPUT|EVENT_INPUT);
358 iochan_setflag(h, assoc->cs_get_mask);
362 if (conn->io_pending & CS_WANT_WRITE)
363 assoc->cs_put_mask |= EVENT_OUTPUT;
364 if (conn->io_pending & CS_WANT_READ)
365 assoc->cs_put_mask |= EVENT_INPUT;
366 iochan_setflag(h, assoc->cs_put_mask);
369 if (event & EVENT_EXCEPT)
371 yaz_log(LOG_LOG, "ir_session (exception)");
373 destroy_association(assoc);
379 * Initiate request processing.
381 static int process_request(association *assoc, request *req, char **msg)
387 *msg = "Unknown Error";
388 assert(req && req->state == REQUEST_IDLE);
389 if (req->apdu_request->which != Z_APDU_initRequest && !assoc->init)
391 *msg = "Missing InitRequest";
394 switch (req->apdu_request->which)
396 case Z_APDU_initRequest:
397 res = process_initRequest(assoc, req); break;
398 case Z_APDU_searchRequest:
399 res = process_searchRequest(assoc, req, &fd); break;
400 case Z_APDU_presentRequest:
401 res = process_presentRequest(assoc, req, &fd); break;
402 case Z_APDU_scanRequest:
403 if (assoc->init->bend_scan)
404 res = process_scanRequest(assoc, req, &fd);
407 *msg = "Cannot handle Scan APDU";
411 case Z_APDU_extendedServicesRequest:
412 if (assoc->init->bend_esrequest)
413 res = process_ESRequest(assoc, req, &fd);
416 *msg = "Cannot handle Extended Services APDU";
420 case Z_APDU_sortRequest:
421 if (assoc->init->bend_sort)
422 res = process_sortRequest(assoc, req, &fd);
425 *msg = "Cannot handle Sort APDU";
430 process_close(assoc, req);
432 case Z_APDU_deleteResultSetRequest:
433 if (assoc->init->bend_delete)
434 res = process_deleteRequest(assoc, req, &fd);
437 *msg = "Cannot handle Delete APDU";
441 case Z_APDU_segmentRequest:
442 if (assoc->init->bend_segment)
444 res = process_segmentRequest (assoc, req);
448 *msg = "Cannot handle Segment APDU";
453 *msg = "Bad APDU received";
458 yaz_log(LOG_DEBUG, " result immediately available");
459 retval = process_response(assoc, req, res);
463 yaz_log(LOG_DEBUG, " result unavailble");
466 else /* no result yet - one will be provided later */
470 /* Set up an I/O handler for the fd supplied by the backend */
472 yaz_log(LOG_DEBUG, " establishing handler for result");
473 req->state = REQUEST_PENDING;
474 if (!(chan = iochan_create(fd, backend_response, EVENT_INPUT)))
476 iochan_setdata(chan, assoc);
483 * Handle message from the backend.
485 void backend_response(IOCHAN i, int event)
487 association *assoc = (association *)iochan_getdata(i);
488 request *req = request_head(&assoc->incoming);
492 yaz_log(LOG_DEBUG, "backend_response");
493 assert(assoc && req && req->state != REQUEST_IDLE);
494 /* determine what it is we're waiting for */
495 switch (req->apdu_request->which)
497 case Z_APDU_searchRequest:
498 res = response_searchRequest(assoc, req, 0, &fd); break;
500 case Z_APDU_presentRequest:
501 res = response_presentRequest(assoc, req, 0, &fd); break;
502 case Z_APDU_scanRequest:
503 res = response_scanRequest(assoc, req, 0, &fd); break;
506 yaz_log(LOG_WARN, "Serious programmer's lapse or bug");
509 if ((res && process_response(assoc, req, res) < 0) || fd < 0)
511 yaz_log(LOG_LOG, "Fatal error when talking to backend");
512 do_close(assoc, Z_Close_systemProblem, 0);
516 else if (!res) /* no result yet - try again later */
518 yaz_log(LOG_DEBUG, " no result yet");
519 iochan_setfd(i, fd); /* in case fd has changed */
524 * Encode response, and transfer the request structure to the outgoing queue.
526 static int process_response(association *assoc, request *req, Z_APDU *res)
528 odr_setbuf(assoc->encode, req->response, req->size_response, 1);
530 if (assoc->print && !z_APDU(assoc->print, &res, 0, 0))
532 yaz_log(LOG_WARN, "ODR print error: %s",
533 odr_errmsg(odr_geterror(assoc->print)));
534 odr_reset(assoc->print);
536 if (!z_APDU(assoc->encode, &res, 0, 0))
538 yaz_log(LOG_WARN, "ODR error when encoding response: %s",
539 odr_errmsg(odr_geterror(assoc->decode)));
542 req->response = odr_getbuf(assoc->encode, &req->len_response,
543 &req->size_response);
544 odr_setbuf(assoc->encode, 0, 0, 0); /* don'txfree if we abort later */
545 odr_reset(assoc->encode);
546 req->state = REQUEST_IDLE;
547 request_enq(&assoc->outgoing, req);
548 /* turn the work over to the ir_session handler */
549 iochan_setflag(assoc->client_chan, EVENT_OUTPUT);
550 assoc->cs_put_mask = EVENT_OUTPUT;
551 /* Is there more work to be done? give that to the input handler too */
553 if (request_head(&assoc->incoming))
555 yaz_log (LOG_DEBUG, "more work to be done");
556 iochan_setevent(assoc->client_chan, EVENT_WORK);
563 * Handle init request.
564 * At the moment, we don't check the options
565 * anywhere else in the code - we just try not to do anything that would
566 * break a naive client. We'll toss 'em into the association block when
567 * we need them there.
569 static Z_APDU *process_initRequest(association *assoc, request *reqb)
571 statserv_options_block *cb = statserv_getcontrol();
572 Z_InitRequest *req = reqb->apdu_request->u.initRequest;
573 Z_APDU *apdu = zget_APDU(assoc->encode, Z_APDU_initResponse);
574 Z_InitResponse *resp = apdu->u.initResponse;
575 bend_initresult *binitres;
579 assoc->init = (bend_initrequest *) xmalloc (sizeof(*assoc->init));
581 yaz_log(LOG_LOG, "Got initRequest");
582 if (req->implementationId)
583 yaz_log(LOG_LOG, "Id: %s", req->implementationId);
584 if (req->implementationName)
585 yaz_log(LOG_LOG, "Name: %s", req->implementationName);
586 if (req->implementationVersion)
587 yaz_log(LOG_LOG, "Version: %s", req->implementationVersion);
589 assoc->init->stream = assoc->encode;
590 assoc->init->print = assoc->print;
591 assoc->init->auth = req->idAuthentication;
592 assoc->init->referenceId = req->referenceId;
593 assoc->init->implementation_version = 0;
594 assoc->init->implementation_id = 0;
595 assoc->init->implementation_name = 0;
596 assoc->init->bend_sort = NULL;
597 assoc->init->bend_search = NULL;
598 assoc->init->bend_present = NULL;
599 assoc->init->bend_esrequest = NULL;
600 assoc->init->bend_delete = NULL;
601 assoc->init->bend_scan = NULL;
602 assoc->init->bend_segment = NULL;
603 assoc->init->bend_fetch = NULL;
605 assoc->init->peer_name =
606 odr_strdup (assoc->encode, cs_addrstr(assoc->client_link));
607 if (!(binitres = (*cb->bend_init)(assoc->init)))
609 yaz_log(LOG_WARN, "Bad response from backend.");
613 assoc->backend = binitres->handle;
614 if ((assoc->init->bend_sort))
615 yaz_log (LOG_DEBUG, "Sort handler installed");
616 if ((assoc->init->bend_search))
617 yaz_log (LOG_DEBUG, "Search handler installed");
618 if ((assoc->init->bend_present))
619 yaz_log (LOG_DEBUG, "Present handler installed");
620 if ((assoc->init->bend_esrequest))
621 yaz_log (LOG_DEBUG, "ESRequest handler installed");
622 if ((assoc->init->bend_delete))
623 yaz_log (LOG_DEBUG, "Delete handler installed");
624 if ((assoc->init->bend_scan))
625 yaz_log (LOG_DEBUG, "Scan handler installed");
626 if ((assoc->init->bend_segment))
627 yaz_log (LOG_DEBUG, "Segment handler installed");
629 resp->referenceId = req->referenceId;
631 /* let's tell the client what we can do */
632 if (ODR_MASK_GET(req->options, Z_Options_search))
634 ODR_MASK_SET(resp->options, Z_Options_search);
635 strcat(options, "srch");
637 if (ODR_MASK_GET(req->options, Z_Options_present))
639 ODR_MASK_SET(resp->options, Z_Options_present);
640 strcat(options, " prst");
642 if (ODR_MASK_GET(req->options, Z_Options_delSet) &&
643 assoc->init->bend_delete)
645 ODR_MASK_SET(resp->options, Z_Options_delSet);
646 strcat(options, " del");
648 if (ODR_MASK_GET(req->options, Z_Options_extendedServices) &&
649 assoc->init->bend_esrequest)
651 ODR_MASK_SET(resp->options, Z_Options_extendedServices);
652 strcat (options, " extendedServices");
654 if (ODR_MASK_GET(req->options, Z_Options_namedResultSets))
656 ODR_MASK_SET(resp->options, Z_Options_namedResultSets);
657 strcat(options, " namedresults");
659 if (ODR_MASK_GET(req->options, Z_Options_scan) && assoc->init->bend_scan)
661 ODR_MASK_SET(resp->options, Z_Options_scan);
662 strcat(options, " scan");
664 if (ODR_MASK_GET(req->options, Z_Options_concurrentOperations))
666 ODR_MASK_SET(resp->options, Z_Options_concurrentOperations);
667 strcat(options, " concurop");
669 if (ODR_MASK_GET(req->options, Z_Options_sort) && assoc->init->bend_sort)
671 ODR_MASK_SET(resp->options, Z_Options_sort);
672 strcat(options, " sort");
674 if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_1))
676 ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_1);
677 assoc->version = 2; /* 1 & 2 are equivalent */
679 if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_2))
681 ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_2);
684 if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_3))
686 ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_3);
689 yaz_log(LOG_LOG, "Negotiated to v%d: %s", assoc->version, options);
690 assoc->maximumRecordSize = *req->maximumRecordSize;
691 if (assoc->maximumRecordSize > control_block->maxrecordsize)
692 assoc->maximumRecordSize = control_block->maxrecordsize;
693 assoc->preferredMessageSize = *req->preferredMessageSize;
694 if (assoc->preferredMessageSize > assoc->maximumRecordSize)
695 assoc->preferredMessageSize = assoc->maximumRecordSize;
698 assoc->maximumRecordSize = 3000000;
699 assoc->preferredMessageSize = 3000000;
702 resp->preferredMessageSize = &assoc->preferredMessageSize;
703 resp->maximumRecordSize = &assoc->maximumRecordSize;
705 resp->implementationName = "GFS/YAZ";
707 if (assoc->init->implementation_id)
710 odr_malloc (assoc->encode,
711 strlen(assoc->init->implementation_id) + 10 +
712 strlen(resp->implementationId));
713 sprintf (nv, "%s / %s",
714 resp->implementationId, assoc->init->implementation_id);
715 resp->implementationId = nv;
717 if (assoc->init->implementation_name)
720 odr_malloc (assoc->encode,
721 strlen(assoc->init->implementation_name) + 10 +
722 strlen(resp->implementationName));
723 sprintf (nv, "%s / %s",
724 resp->implementationName, assoc->init->implementation_name);
725 resp->implementationName = nv;
727 if (assoc->init->implementation_version)
730 odr_malloc (assoc->encode,
731 strlen(assoc->init->implementation_version) + 10 +
732 strlen(resp->implementationVersion));
733 sprintf (nv, "YAZ %s / %s",
734 resp->implementationVersion,
735 assoc->init->implementation_version);
736 resp->implementationVersion = nv;
739 if (binitres->errcode)
741 yaz_log(LOG_LOG, "Connection rejected by backend.");
743 assoc->state = ASSOC_DEAD;
746 assoc->state = ASSOC_UP;
751 * These functions should be merged.
754 static void set_addinfo (Z_DefaultDiagFormat *dr, char *addinfo, ODR odr)
756 dr->which = Z_DefaultDiagFormat_v2Addinfo;
757 dr->u.v2Addinfo = odr_strdup (odr, addinfo ? addinfo : "");
761 * nonsurrogate diagnostic record.
763 static Z_Records *diagrec(association *assoc, int error, char *addinfo)
765 Z_Records *rec = (Z_Records *)
766 odr_malloc (assoc->encode, sizeof(*rec));
767 int *err = odr_intdup(assoc->encode, error);
768 Z_DiagRec *drec = (Z_DiagRec *)
769 odr_malloc (assoc->encode, sizeof(*drec));
770 Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
771 odr_malloc (assoc->encode, sizeof(*dr));
773 yaz_log(LOG_LOG, "[%d] %s %s%s", error, diagbib1_str(error),
774 addinfo ? " -- " : "", addinfo ? addinfo : "");
775 rec->which = Z_Records_NSD;
776 rec->u.nonSurrogateDiagnostic = dr;
777 dr->diagnosticSetId =
778 yaz_oidval_to_z3950oid (assoc->encode, CLASS_DIAGSET, VAL_BIB1);
780 set_addinfo (dr, addinfo, assoc->encode);
785 * surrogate diagnostic.
787 static Z_NamePlusRecord *surrogatediagrec(association *assoc, char *dbname,
788 int error, char *addinfo)
790 Z_NamePlusRecord *rec = (Z_NamePlusRecord *)
791 odr_malloc (assoc->encode, sizeof(*rec));
792 int *err = odr_intdup(assoc->encode, error);
793 Z_DiagRec *drec = (Z_DiagRec *)odr_malloc (assoc->encode, sizeof(*drec));
794 Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
795 odr_malloc (assoc->encode, sizeof(*dr));
797 yaz_log(LOG_DEBUG, "SurrogateDiagnotic: %d -- %s", error, addinfo);
798 rec->databaseName = dbname;
799 rec->which = Z_NamePlusRecord_surrogateDiagnostic;
800 rec->u.surrogateDiagnostic = drec;
801 drec->which = Z_DiagRec_defaultFormat;
802 drec->u.defaultFormat = dr;
803 dr->diagnosticSetId =
804 yaz_oidval_to_z3950oid (assoc->encode, CLASS_DIAGSET, VAL_BIB1);
806 set_addinfo (dr, addinfo, assoc->encode);
812 * multiple nonsurrogate diagnostics.
814 static Z_DiagRecs *diagrecs(association *assoc, int error, char *addinfo)
816 Z_DiagRecs *recs = (Z_DiagRecs *)odr_malloc (assoc->encode, sizeof(*recs));
817 int *err = odr_intdup(assoc->encode, error);
818 Z_DiagRec **recp = (Z_DiagRec **)odr_malloc (assoc->encode, sizeof(*recp));
819 Z_DiagRec *drec = (Z_DiagRec *)odr_malloc (assoc->encode, sizeof(*drec));
820 Z_DefaultDiagFormat *rec = (Z_DefaultDiagFormat *)
821 odr_malloc (assoc->encode, sizeof(*rec));
823 yaz_log(LOG_DEBUG, "DiagRecs: %d -- %s", error, addinfo ? addinfo : "");
825 recs->num_diagRecs = 1;
826 recs->diagRecs = recp;
828 drec->which = Z_DiagRec_defaultFormat;
829 drec->u.defaultFormat = rec;
831 rec->diagnosticSetId =
832 yaz_oidval_to_z3950oid (assoc->encode, CLASS_DIAGSET, VAL_BIB1);
833 rec->condition = err;
835 rec->which = Z_DefaultDiagFormat_v2Addinfo;
836 rec->u.v2Addinfo = odr_strdup (assoc->encode, addinfo ? addinfo : "");
840 static Z_Records *pack_records(association *a, char *setname, int start,
841 int *num, Z_RecordComposition *comp,
842 int *next, int *pres, oid_value format,
843 Z_ReferenceId *referenceId,
846 int recno, total_length = 0, toget = *num, dumped_records = 0;
848 (Z_Records *) odr_malloc (a->encode, sizeof(*records));
849 Z_NamePlusRecordList *reclist =
850 (Z_NamePlusRecordList *) odr_malloc (a->encode, sizeof(*reclist));
851 Z_NamePlusRecord **list =
852 (Z_NamePlusRecord **) odr_malloc (a->encode, sizeof(*list) * toget);
854 records->which = Z_Records_DBOSD;
855 records->u.databaseOrSurDiagnostics = reclist;
856 reclist->num_records = 0;
857 reclist->records = list;
858 *pres = Z_PRES_SUCCESS;
862 yaz_log(LOG_LOG, "Request to pack %d+%d+%s", start, toget, setname);
863 yaz_log(LOG_DEBUG, "pms=%d, mrs=%d", a->preferredMessageSize,
864 a->maximumRecordSize);
865 for (recno = start; reclist->num_records < toget; recno++)
868 Z_NamePlusRecord *thisrec;
871 * we get the number of bytes allocated on the stream before any
872 * allocation done by the backend - this should give us a reasonable
873 * idea of the total size of the data so far.
875 total_length = odr_total(a->encode) - dumped_records;
881 freq.last_in_set = 0;
882 freq.setname = setname;
883 freq.surrogate_flag = 0;
886 freq.request_format = format;
887 freq.request_format_raw = oid;
888 freq.output_format = format;
889 freq.output_format_raw = 0;
890 freq.stream = a->encode;
891 freq.print = a->print;
892 freq.surrogate_flag = 0;
893 freq.referenceId = referenceId;
894 (*a->init->bend_fetch)(a->backend, &freq);
895 /* backend should be able to signal whether error is system-wide
896 or only pertaining to current record */
899 if (!freq.surrogate_flag)
902 *pres = Z_PRES_FAILURE;
903 /* for 'present request out of range',
904 set addinfo to record position if not set */
905 if (freq.errcode == 13 && freq.errstring == 0)
907 sprintf (s, "%d", recno);
910 return diagrec(a, freq.errcode, freq.errstring);
912 reclist->records[reclist->num_records] =
913 surrogatediagrec(a, freq.basename, freq.errcode,
915 reclist->num_records++;
916 *next = freq.last_in_set ? 0 : recno + 1;
920 this_length = freq.len;
922 this_length = odr_total(a->encode) - total_length;
923 yaz_log(LOG_DEBUG, " fetched record, len=%d, total=%d",
924 this_length, total_length);
925 if (this_length + total_length > a->preferredMessageSize)
927 /* record is small enough, really */
928 if (this_length <= a->preferredMessageSize)
930 yaz_log(LOG_DEBUG, " Dropped last normal-sized record");
931 *pres = Z_PRES_PARTIAL_2;
934 /* record can only be fetched by itself */
935 if (this_length < a->maximumRecordSize)
937 yaz_log(LOG_DEBUG, " Record > prefmsgsz");
940 yaz_log(LOG_DEBUG, " Dropped it");
941 reclist->records[reclist->num_records] =
942 surrogatediagrec(a, freq.basename, 16, 0);
943 reclist->num_records++;
944 *next = freq.last_in_set ? 0 : recno + 1;
945 dumped_records += this_length;
949 else /* too big entirely */
951 yaz_log(LOG_LOG, "Record > maxrcdsz this=%d max=%d", this_length, a->maximumRecordSize);
952 reclist->records[reclist->num_records] =
953 surrogatediagrec(a, freq.basename, 17, 0);
954 reclist->num_records++;
955 *next = freq.last_in_set ? 0 : recno + 1;
956 dumped_records += this_length;
961 if (!(thisrec = (Z_NamePlusRecord *)
962 odr_malloc(a->encode, sizeof(*thisrec))))
964 if (!(thisrec->databaseName = (char *)odr_malloc(a->encode,
965 strlen(freq.basename) + 1)))
967 strcpy(thisrec->databaseName, freq.basename);
968 thisrec->which = Z_NamePlusRecord_databaseRecord;
970 if (freq.output_format_raw)
972 struct oident *ident = oid_getentbyoid(freq.output_format_raw);
973 freq.output_format = ident->value;
975 thisrec->u.databaseRecord = z_ext_record(a->encode, freq.output_format,
976 freq.record, freq.len);
977 if (!thisrec->u.databaseRecord)
979 reclist->records[reclist->num_records] = thisrec;
980 reclist->num_records++;
981 *next = freq.last_in_set ? 0 : recno + 1;
983 *num = reclist->num_records;
987 static Z_APDU *process_searchRequest(association *assoc, request *reqb,
990 Z_SearchRequest *req = reqb->apdu_request->u.searchRequest;
991 bend_search_rr *bsrr =
992 (bend_search_rr *)nmem_malloc (reqb->request_mem, sizeof(*bsrr));
994 yaz_log(LOG_LOG, "Got SearchRequest.");
996 bsrr->request = reqb;
997 bsrr->association = assoc;
998 bsrr->referenceId = req->referenceId;
999 save_referenceId (reqb, bsrr->referenceId);
1001 yaz_log (LOG_LOG, "ResultSet '%s'", req->resultSetName);
1002 if (req->databaseNames)
1005 for (i = 0; i < req->num_databaseNames; i++)
1006 yaz_log (LOG_LOG, "Database '%s'", req->databaseNames[i]);
1008 switch (req->query->which)
1010 case Z_Query_type_1: case Z_Query_type_101:
1011 log_rpn_query (req->query->u.type_1);
1013 if (assoc->init->bend_search)
1015 bsrr->setname = req->resultSetName;
1016 bsrr->replace_set = *req->replaceIndicator;
1017 bsrr->num_bases = req->num_databaseNames;
1018 bsrr->basenames = req->databaseNames;
1019 bsrr->query = req->query;
1020 bsrr->stream = assoc->encode;
1021 bsrr->decode = assoc->decode;
1022 bsrr->print = assoc->print;
1025 bsrr->errstring = NULL;
1026 bsrr->search_info = NULL;
1027 (assoc->init->bend_search)(assoc->backend, bsrr);
1031 return response_searchRequest(assoc, reqb, bsrr, fd);
1034 int bend_searchresponse(void *handle, bend_search_rr *bsrr) {return 0;}
1037 * Prepare a searchresponse based on the backend results. We probably want
1038 * to look at making the fetching of records nonblocking as well, but
1039 * so far, we'll keep things simple.
1040 * If bsrt is null, that means we're called in response to a communications
1041 * event, and we'll have to get the response for ourselves.
1043 static Z_APDU *response_searchRequest(association *assoc, request *reqb,
1044 bend_search_rr *bsrt, int *fd)
1046 Z_SearchRequest *req = reqb->apdu_request->u.searchRequest;
1047 Z_APDU *apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1048 Z_SearchResponse *resp = (Z_SearchResponse *)
1049 odr_malloc (assoc->encode, sizeof(*resp));
1050 int *nulint = odr_intdup (assoc->encode, 0);
1051 bool_t *sr = odr_intdup(assoc->encode, 1);
1052 int *next = odr_intdup(assoc->encode, 0);
1053 int *none = odr_intdup(assoc->encode, Z_RES_NONE);
1055 apdu->which = Z_APDU_searchResponse;
1056 apdu->u.searchResponse = resp;
1057 resp->referenceId = req->referenceId;
1058 resp->additionalSearchInfo = 0;
1059 resp->otherInfo = 0;
1061 if (!bsrt && !bend_searchresponse(assoc->backend, bsrt))
1063 yaz_log(LOG_FATAL, "Bad result from backend");
1066 else if (bsrt->errcode)
1068 resp->records = diagrec(assoc, bsrt->errcode, bsrt->errstring);
1069 resp->resultCount = nulint;
1070 resp->numberOfRecordsReturned = nulint;
1071 resp->nextResultSetPosition = nulint;
1072 resp->searchStatus = nulint;
1073 resp->resultSetStatus = none;
1074 resp->presentStatus = 0;
1078 int *toget = odr_intdup(assoc->encode, 0);
1079 int *presst = odr_intdup(assoc->encode, 0);
1080 Z_RecordComposition comp, *compp = 0;
1082 yaz_log (LOG_LOG, "resultCount: %d", bsrt->hits);
1085 resp->resultCount = &bsrt->hits;
1087 comp.which = Z_RecordComp_simple;
1088 /* how many records does the user agent want, then? */
1089 if (bsrt->hits <= *req->smallSetUpperBound)
1091 *toget = bsrt->hits;
1092 if ((comp.u.simple = req->smallSetElementSetNames))
1095 else if (bsrt->hits < *req->largeSetLowerBound)
1097 *toget = *req->mediumSetPresentNumber;
1098 if (*toget > bsrt->hits)
1099 *toget = bsrt->hits;
1100 if ((comp.u.simple = req->mediumSetElementSetNames))
1106 if (*toget && !resp->records)
1111 if (!(prefformat = oid_getentbyoid(req->preferredRecordSyntax)))
1114 form = prefformat->value;
1115 resp->records = pack_records(assoc, req->resultSetName, 1,
1116 toget, compp, next, presst, form, req->referenceId,
1117 req->preferredRecordSyntax);
1120 resp->numberOfRecordsReturned = toget;
1121 resp->nextResultSetPosition = next;
1122 resp->searchStatus = sr;
1123 resp->resultSetStatus = 0;
1124 resp->presentStatus = presst;
1128 if (*resp->resultCount)
1130 resp->numberOfRecordsReturned = nulint;
1131 resp->nextResultSetPosition = next;
1132 resp->searchStatus = sr;
1133 resp->resultSetStatus = 0;
1134 resp->presentStatus = 0;
1137 resp->additionalSearchInfo = bsrt->search_info;
1142 * Maybe we got a little over-friendly when we designed bend_fetch to
1143 * get only one record at a time. Some backends can optimise multiple-record
1144 * fetches, and at any rate, there is some overhead involved in
1145 * all that selecting and hopping around. Problem is, of course, that the
1146 * frontend can't know ahead of time how many records it'll need to
1147 * fill the negotiated PDU size. Annoying. Segmentation or not, Z/SR
1148 * is downright lousy as a bulk data transfer protocol.
1150 * To start with, we'll do the fetching of records from the backend
1151 * in one operation: To save some trips in and out of the event-handler,
1152 * and to simplify the interface to pack_records. At any rate, asynch
1153 * operation is more fun in operations that have an unpredictable execution
1154 * speed - which is normally more true for search than for present.
1156 static Z_APDU *process_presentRequest(association *assoc, request *reqb,
1159 Z_PresentRequest *req = reqb->apdu_request->u.presentRequest;
1163 Z_PresentResponse *resp;
1167 yaz_log(LOG_LOG, "Got PresentRequest.");
1169 if (!(prefformat = oid_getentbyoid(req->preferredRecordSyntax)))
1172 form = prefformat->value;
1173 resp = (Z_PresentResponse *)odr_malloc (assoc->encode, sizeof(*resp));
1175 resp->presentStatus = odr_intdup(assoc->encode, 0);
1176 if (assoc->init->bend_present)
1178 bend_present_rr *bprr = (bend_present_rr *)
1179 nmem_malloc (reqb->request_mem, sizeof(*bprr));
1180 bprr->setname = req->resultSetId;
1181 bprr->start = *req->resultSetStartPoint;
1182 bprr->number = *req->numberOfRecordsRequested;
1183 bprr->format = form;
1184 bprr->comp = req->recordComposition;
1185 bprr->referenceId = req->referenceId;
1186 bprr->stream = assoc->encode;
1187 bprr->print = assoc->print;
1188 bprr->request = reqb;
1189 bprr->association = assoc;
1191 bprr->errstring = NULL;
1192 (*assoc->init->bend_present)(assoc->backend, bprr);
1198 resp->records = diagrec(assoc, bprr->errcode, bprr->errstring);
1199 *resp->presentStatus = Z_PRES_FAILURE;
1202 apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1203 next = odr_intdup(assoc->encode, 0);
1204 num = odr_intdup(assoc->encode, 0);
1206 apdu->which = Z_APDU_presentResponse;
1207 apdu->u.presentResponse = resp;
1208 resp->referenceId = req->referenceId;
1209 resp->otherInfo = 0;
1213 *num = *req->numberOfRecordsRequested;
1215 pack_records(assoc, req->resultSetId, *req->resultSetStartPoint,
1216 num, req->recordComposition, next, resp->presentStatus,
1217 form, req->referenceId, req->preferredRecordSyntax);
1221 resp->numberOfRecordsReturned = num;
1222 resp->nextResultSetPosition = next;
1228 * Scan was implemented rather in a hurry, and with support for only the basic
1229 * elements of the service in the backend API. Suggestions are welcome.
1231 static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd)
1233 Z_ScanRequest *req = reqb->apdu_request->u.scanRequest;
1234 Z_APDU *apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1235 Z_ScanResponse *res = (Z_ScanResponse *)
1236 odr_malloc (assoc->encode, sizeof(*res));
1237 int *scanStatus = odr_intdup(assoc->encode, Z_Scan_failure);
1238 int *numberOfEntriesReturned = odr_intdup(assoc->encode, 0);
1239 Z_ListEntries *ents = (Z_ListEntries *)
1240 odr_malloc (assoc->encode, sizeof(*ents));
1241 Z_DiagRecs *diagrecs_p = NULL;
1243 bend_scan_rr *bsrr = (bend_scan_rr *)
1244 odr_malloc (assoc->encode, sizeof(*bsrr));
1246 yaz_log(LOG_LOG, "Got ScanRequest");
1248 apdu->which = Z_APDU_scanResponse;
1249 apdu->u.scanResponse = res;
1250 res->referenceId = req->referenceId;
1252 /* if step is absent, set it to 0 */
1253 res->stepSize = odr_intdup(assoc->encode, 0);
1255 *res->stepSize = *req->stepSize;
1257 res->scanStatus = scanStatus;
1258 res->numberOfEntriesReturned = numberOfEntriesReturned;
1259 res->positionOfTerm = 0;
1260 res->entries = ents;
1261 ents->num_entries = 0;
1262 ents->entries = NULL;
1263 ents->num_nonsurrogateDiagnostics = 0;
1264 ents->nonsurrogateDiagnostics = NULL;
1265 res->attributeSet = 0;
1268 if (req->databaseNames)
1271 for (i = 0; i < req->num_databaseNames; i++)
1272 yaz_log (LOG_LOG, "Database '%s'", req->databaseNames[i]);
1274 bsrr->num_bases = req->num_databaseNames;
1275 bsrr->basenames = req->databaseNames;
1276 bsrr->num_entries = *req->numberOfTermsRequested;
1277 bsrr->term = req->termListAndStartPoint;
1278 bsrr->referenceId = req->referenceId;
1279 bsrr->stream = assoc->encode;
1280 bsrr->print = assoc->print;
1281 bsrr->step_size = res->stepSize;
1282 if (req->attributeSet &&
1283 (attset = oid_getentbyoid(req->attributeSet)) &&
1284 (attset->oclass == CLASS_ATTSET || attset->oclass == CLASS_GENERAL))
1285 bsrr->attributeset = attset->value;
1287 bsrr->attributeset = VAL_NONE;
1288 log_scan_term (req->termListAndStartPoint, bsrr->attributeset);
1289 bsrr->term_position = req->preferredPositionInResponse ?
1290 *req->preferredPositionInResponse : 1;
1291 ((int (*)(void *, bend_scan_rr *))
1292 (*assoc->init->bend_scan))(assoc->backend, bsrr);
1294 diagrecs_p = diagrecs(assoc, bsrr->errcode, bsrr->errstring);
1298 Z_Entry **tab = (Z_Entry **)
1299 odr_malloc (assoc->encode, sizeof(*tab) * bsrr->num_entries);
1301 if (bsrr->status == BEND_SCAN_PARTIAL)
1302 *scanStatus = Z_Scan_partial_5;
1304 *scanStatus = Z_Scan_success;
1305 ents->entries = tab;
1306 ents->num_entries = bsrr->num_entries;
1307 res->numberOfEntriesReturned = &ents->num_entries;
1308 res->positionOfTerm = &bsrr->term_position;
1309 for (i = 0; i < bsrr->num_entries; i++)
1315 tab[i] = e = (Z_Entry *)odr_malloc(assoc->encode, sizeof(*e));
1316 if (bsrr->entries[i].occurrences >= 0)
1318 e->which = Z_Entry_termInfo;
1319 e->u.termInfo = t = (Z_TermInfo *)
1320 odr_malloc(assoc->encode, sizeof(*t));
1321 t->suggestedAttributes = 0;
1323 t->alternativeTerm = 0;
1324 t->byAttributes = 0;
1325 t->otherTermInfo = 0;
1326 t->globalOccurrences = &bsrr->entries[i].occurrences;
1327 t->term = (Z_Term *)
1328 odr_malloc(assoc->encode, sizeof(*t->term));
1329 t->term->which = Z_Term_general;
1330 t->term->u.general = o =
1331 (Odr_oct *)odr_malloc(assoc->encode, sizeof(Odr_oct));
1332 o->buf = (unsigned char *)
1333 odr_malloc(assoc->encode, o->len = o->size =
1334 strlen(bsrr->entries[i].term));
1335 memcpy(o->buf, bsrr->entries[i].term, o->len);
1336 yaz_log(LOG_DEBUG, " term #%d: '%s' (%d)", i,
1337 bsrr->entries[i].term, bsrr->entries[i].occurrences);
1341 Z_DiagRecs *drecs = diagrecs (assoc,
1342 bsrr->entries[i].errcode,
1343 bsrr->entries[i].errstring);
1344 assert (drecs->num_diagRecs == 1);
1345 e->which = Z_Entry_surrogateDiagnostic;
1346 assert (drecs->diagRecs[0]);
1347 e->u.surrogateDiagnostic = drecs->diagRecs[0];
1353 ents->num_nonsurrogateDiagnostics = diagrecs_p->num_diagRecs;
1354 ents->nonsurrogateDiagnostics = diagrecs_p->diagRecs;
1359 static Z_APDU *process_sortRequest(association *assoc, request *reqb,
1362 Z_SortRequest *req = reqb->apdu_request->u.sortRequest;
1363 Z_SortResponse *res = (Z_SortResponse *)
1364 odr_malloc (assoc->encode, sizeof(*res));
1365 bend_sort_rr *bsrr = (bend_sort_rr *)
1366 odr_malloc (assoc->encode, sizeof(*bsrr));
1368 Z_APDU *apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1370 yaz_log(LOG_LOG, "Got SortRequest.");
1372 bsrr->num_input_setnames = req->num_inputResultSetNames;
1373 bsrr->input_setnames = req->inputResultSetNames;
1374 bsrr->referenceId = req->referenceId;
1375 bsrr->output_setname = req->sortedResultSetName;
1376 bsrr->sort_sequence = req->sortSequence;
1377 bsrr->stream = assoc->encode;
1378 bsrr->print = assoc->print;
1380 bsrr->sort_status = Z_SortStatus_failure;
1382 bsrr->errstring = 0;
1384 (*assoc->init->bend_sort)(assoc->backend, bsrr);
1386 res->referenceId = bsrr->referenceId;
1387 res->sortStatus = odr_intdup(assoc->encode, bsrr->sort_status);
1388 res->resultSetStatus = 0;
1391 Z_DiagRecs *dr = diagrecs (assoc, bsrr->errcode, bsrr->errstring);
1392 res->diagnostics = dr->diagRecs;
1393 res->num_diagnostics = dr->num_diagRecs;
1397 res->num_diagnostics = 0;
1398 res->diagnostics = 0;
1402 apdu->which = Z_APDU_sortResponse;
1403 apdu->u.sortResponse = res;
1407 static Z_APDU *process_deleteRequest(association *assoc, request *reqb,
1410 Z_DeleteResultSetRequest *req =
1411 reqb->apdu_request->u.deleteResultSetRequest;
1412 Z_DeleteResultSetResponse *res = (Z_DeleteResultSetResponse *)
1413 odr_malloc (assoc->encode, sizeof(*res));
1414 bend_delete_rr *bdrr = (bend_delete_rr *)
1415 odr_malloc (assoc->encode, sizeof(*bdrr));
1416 Z_APDU *apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1418 yaz_log(LOG_LOG, "Got DeleteRequest.");
1420 bdrr->num_setnames = req->num_resultSetList;
1421 bdrr->setnames = req->resultSetList;
1422 bdrr->stream = assoc->encode;
1423 bdrr->print = assoc->print;
1424 bdrr->function = *req->deleteFunction;
1425 bdrr->referenceId = req->referenceId;
1427 if (bdrr->num_setnames > 0)
1430 bdrr->statuses = (int*)
1431 odr_malloc(assoc->encode, sizeof(*bdrr->statuses) *
1432 bdrr->num_setnames);
1433 for (i = 0; i < bdrr->num_setnames; i++)
1434 bdrr->statuses[i] = 0;
1436 (*assoc->init->bend_delete)(assoc->backend, bdrr);
1438 res->referenceId = req->referenceId;
1440 res->deleteOperationStatus = odr_intdup(assoc->encode,bdrr->delete_status);
1442 res->deleteListStatuses = 0;
1443 if (bdrr->num_setnames > 0)
1446 res->deleteListStatuses = (Z_ListStatuses *)
1447 odr_malloc(assoc->encode, sizeof(*res->deleteListStatuses));
1448 res->deleteListStatuses->num = bdrr->num_setnames;
1449 res->deleteListStatuses->elements =
1451 odr_malloc (assoc->encode,
1452 sizeof(*res->deleteListStatuses->elements) *
1453 bdrr->num_setnames);
1454 for (i = 0; i<bdrr->num_setnames; i++)
1456 res->deleteListStatuses->elements[i] =
1458 odr_malloc (assoc->encode,
1459 sizeof(**res->deleteListStatuses->elements));
1460 res->deleteListStatuses->elements[i]->status = bdrr->statuses+i;
1461 res->deleteListStatuses->elements[i]->id =
1462 odr_strdup (assoc->encode, bdrr->setnames[i]);
1466 res->numberNotDeleted = 0;
1467 res->bulkStatuses = 0;
1468 res->deleteMessage = 0;
1471 apdu->which = Z_APDU_deleteResultSetResponse;
1472 apdu->u.deleteResultSetResponse = res;
1476 static void process_close(association *assoc, request *reqb)
1478 Z_Close *req = reqb->apdu_request->u.close;
1479 static char *reasons[] =
1486 "securityViolation",
1493 yaz_log(LOG_LOG, "Got Close, reason %s, message %s",
1494 reasons[*req->closeReason], req->diagnosticInformation ?
1495 req->diagnosticInformation : "NULL");
1496 if (assoc->version < 3) /* to make do_force respond with close */
1498 do_close_req(assoc, Z_Close_finished,
1499 "Association terminated by client", reqb);
1502 void save_referenceId (request *reqb, Z_ReferenceId *refid)
1506 reqb->len_refid = refid->len;
1507 reqb->refid = (char *)nmem_malloc (reqb->request_mem, refid->len);
1508 memcpy (reqb->refid, refid->buf, refid->len);
1512 reqb->len_refid = 0;
1517 void bend_request_send (bend_association a, bend_request req, Z_APDU *res)
1519 process_response (a, req, res);
1522 bend_request bend_request_mk (bend_association a)
1524 request *nreq = request_get (&a->outgoing);
1525 nreq->request_mem = nmem_create ();
1529 Z_ReferenceId *bend_request_getid (ODR odr, bend_request req)
1534 id = (Odr_oct *)odr_malloc (odr, sizeof(*odr));
1535 id->buf = (unsigned char *)odr_malloc (odr, req->len_refid);
1536 id->len = id->size = req->len_refid;
1537 memcpy (id->buf, req->refid, req->len_refid);
1541 void bend_request_destroy (bend_request *req)
1543 nmem_destroy((*req)->request_mem);
1544 request_release(*req);
1548 int bend_backend_respond (bend_association a, bend_request req)
1552 r = process_request (a, req, &msg);
1554 logf (LOG_WARN, "%s", msg);
1558 void bend_request_setdata(bend_request r, void *p)
1563 void *bend_request_getdata(bend_request r)
1565 return r->clientData;
1568 static Z_APDU *process_segmentRequest (association *assoc, request *reqb)
1570 bend_segment_rr req;
1572 req.segment = reqb->apdu_request->u.segmentRequest;
1573 req.stream = assoc->encode;
1574 req.decode = assoc->decode;
1575 req.print = assoc->print;
1576 req.association = assoc;
1578 (*assoc->init->bend_segment)(assoc->backend, &req);
1583 static Z_APDU *process_ESRequest(association *assoc, request *reqb, int *fd)
1585 bend_esrequest_rr esrequest;
1587 Z_ExtendedServicesRequest *req = reqb->apdu_request->u.extendedServicesRequest;
1588 Z_APDU *apdu = zget_APDU(assoc->encode, Z_APDU_extendedServicesResponse);
1590 Z_ExtendedServicesResponse *resp = apdu->u.extendedServicesResponse;
1592 yaz_log(LOG_DEBUG,"inside Process esRequest");
1594 esrequest.esr = reqb->apdu_request->u.extendedServicesRequest;
1595 esrequest.stream = assoc->encode;
1596 esrequest.decode = assoc->decode;
1597 esrequest.print = assoc->print;
1598 esrequest.errcode = 0;
1599 esrequest.errstring = NULL;
1600 esrequest.request = reqb;
1601 esrequest.association = assoc;
1602 esrequest.taskPackage = 0;
1603 esrequest.referenceId = req->referenceId;
1605 (*assoc->init->bend_esrequest)(assoc->backend, &esrequest);
1607 /* If the response is being delayed, return NULL */
1608 if (esrequest.request == NULL)
1611 resp->referenceId = req->referenceId;
1613 if (esrequest.errcode == -1)
1615 /* Backend service indicates request will be processed */
1616 yaz_log(LOG_DEBUG,"Request could be processed...Accepted !");
1617 *resp->operationStatus = Z_ExtendedServicesResponse_accepted;
1619 else if (esrequest.errcode == 0)
1621 /* Backend service indicates request will be processed */
1622 yaz_log(LOG_DEBUG,"Request could be processed...Done !");
1623 *resp->operationStatus = Z_ExtendedServicesResponse_done;
1627 Z_DiagRecs *diagRecs = diagrecs (assoc, esrequest.errcode,
1628 esrequest.errstring);
1630 /* Backend indicates error, request will not be processed */
1631 yaz_log(LOG_DEBUG,"Request could not be processed...failure !");
1632 *resp->operationStatus = Z_ExtendedServicesResponse_failure;
1633 resp->num_diagnostics = diagRecs->num_diagRecs;
1634 resp->diagnostics = diagRecs->diagRecs;
1636 /* Do something with the members of bend_extendedservice */
1637 if (esrequest.taskPackage)
1638 resp->taskPackage = z_ext_record (assoc->encode, VAL_EXTENDED,
1639 (const char *) esrequest.taskPackage,
1641 yaz_log(LOG_DEBUG,"Send the result apdu");