2 * Copyright (c) 1995-2002, Index Data
3 * See the file LICENSE for details.
5 * $Id: seshigh.c,v 1.132 2002-09-25 12:37:07 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>
49 #include <yaz/charneg.h>
50 #include <yaz/otherinfo.h>
52 #include <yaz/backend.h>
54 static int process_request(association *assoc, request *req, char **msg);
55 void backend_response(IOCHAN i, int event);
56 static int process_response(association *assoc, request *req, Z_APDU *res);
57 static Z_APDU *process_initRequest(association *assoc, request *reqb);
58 static Z_APDU *process_searchRequest(association *assoc, request *reqb,
60 static Z_APDU *response_searchRequest(association *assoc, request *reqb,
61 bend_search_rr *bsrr, int *fd);
62 static Z_APDU *process_presentRequest(association *assoc, request *reqb,
64 static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd);
65 static Z_APDU *process_sortRequest(association *assoc, request *reqb, int *fd);
66 static void process_close(association *assoc, request *reqb);
67 void save_referenceId (request *reqb, Z_ReferenceId *refid);
68 static Z_APDU *process_deleteRequest(association *assoc, request *reqb,
70 static Z_APDU *process_segmentRequest (association *assoc, request *reqb);
72 static FILE *apduf = 0; /* for use in static mode */
73 static statserv_options_block *control_block = 0;
75 /* Chas: Added in from DALI */
76 static Z_APDU *process_ESRequest(association *assoc, request *reqb, int *fd);
77 /* Chas: End of addition from DALI */
80 * Create and initialize a new association-handle.
81 * channel : iochannel for the current line.
82 * link : communications channel.
83 * Returns: 0 or a new association handle.
85 association *create_association(IOCHAN channel, COMSTACK link)
90 control_block = statserv_getcontrol();
91 if (!(anew = (association *)xmalloc(sizeof(*anew))))
94 anew->client_chan = channel;
95 anew->client_link = link;
96 anew->cs_get_mask = 0;
97 anew->cs_put_mask = 0;
98 anew->cs_accept_mask = 0;
99 if (!(anew->decode = odr_createmem(ODR_DECODE)) ||
100 !(anew->encode = odr_createmem(ODR_ENCODE)))
102 if (*control_block->apdufile)
107 strcpy(filename, control_block->apdufile);
108 if (!(anew->print = odr_createmem(ODR_PRINT)))
110 if (*control_block->apdufile == '@')
112 odr_setprint(anew->print, yaz_log_file());
114 else if (*control_block->apdufile != '-')
116 strcpy(filename, control_block->apdufile);
117 if (!control_block->dynamic)
121 if (!(apduf = fopen(filename, "w")))
123 yaz_log(LOG_WARN|LOG_ERRNO, "%s", filename);
126 setvbuf(apduf, 0, _IONBF, 0);
132 sprintf(filename + strlen(filename), ".%d", getpid());
133 if (!(f = fopen(filename, "w")))
135 yaz_log(LOG_WARN|LOG_ERRNO, "%s", filename);
138 setvbuf(f, 0, _IONBF, 0);
140 odr_setprint(anew->print, f);
145 anew->input_buffer = 0;
146 anew->input_buffer_len = 0;
148 anew->state = ASSOC_NEW;
149 request_initq(&anew->incoming);
150 request_initq(&anew->outgoing);
151 anew->proto = cs_getproto(link);
156 * Free association and release resources.
158 void destroy_association(association *h)
160 statserv_options_block *cb = statserv_getcontrol();
163 odr_destroy(h->decode);
164 odr_destroy(h->encode);
166 odr_destroy(h->print);
168 xfree(h->input_buffer);
170 (*cb->bend_close)(h->backend);
171 while (request_deq(&h->incoming));
172 while (request_deq(&h->outgoing));
173 request_delq(&h->incoming);
174 request_delq(&h->outgoing);
176 xmalloc_trav("session closed");
177 if (control_block && control_block->one_shot)
181 static void do_close_req(association *a, int reason, char *message,
185 Z_Close *cls = zget_Close(a->encode);
187 /* Purge request queue */
188 while (request_deq(&a->incoming));
189 while (request_deq(&a->outgoing));
192 yaz_log(LOG_LOG, "Sending Close PDU, reason=%d, message=%s",
193 reason, message ? message : "none");
194 apdu.which = Z_APDU_close;
196 *cls->closeReason = reason;
197 cls->diagnosticInformation = message;
198 process_response(a, req, &apdu);
199 iochan_settimeout(a->client_chan, 60);
203 yaz_log(LOG_DEBUG, "v2 client. No Close PDU");
204 iochan_setevent(a->client_chan, EVENT_TIMEOUT); /* force imm close */
206 a->state = ASSOC_DEAD;
209 static void do_close(association *a, int reason, char *message)
211 do_close_req (a, reason, message, request_get(&a->outgoing));
215 * This is where PDUs from the client are read and the further
216 * processing is initiated. Flow of control moves down through the
217 * various process_* functions below, until the encoded result comes back up
218 * to the output handler in here.
220 * h : the I/O channel that has an outstanding event.
221 * event : the current outstanding event.
223 void ir_session(IOCHAN h, int event)
226 association *assoc = (association *)iochan_getdata(h);
227 COMSTACK conn = assoc->client_link;
230 assert(h && conn && assoc);
231 if (event == EVENT_TIMEOUT)
233 if (assoc->state != ASSOC_UP)
235 yaz_log(LOG_LOG, "Final timeout - closing connection.");
237 destroy_association(assoc);
242 yaz_log(LOG_LOG, "Session idle too long. Sending close.");
243 do_close(assoc, Z_Close_lackOfActivity, 0);
247 if (event & assoc->cs_accept_mask)
249 yaz_log (LOG_DEBUG, "ir_session (accept)");
250 if (!cs_accept (conn))
252 yaz_log (LOG_LOG, "accept failed");
253 destroy_association(assoc);
256 iochan_clearflag (h, EVENT_OUTPUT|EVENT_OUTPUT);
257 if (conn->io_pending)
258 { /* cs_accept didn't complete */
259 assoc->cs_accept_mask =
260 ((conn->io_pending & CS_WANT_WRITE) ? EVENT_OUTPUT : 0) |
261 ((conn->io_pending & CS_WANT_READ) ? EVENT_INPUT : 0);
263 iochan_setflag (h, assoc->cs_accept_mask);
266 { /* cs_accept completed. Prepare for reading (cs_get) */
267 assoc->cs_accept_mask = 0;
268 assoc->cs_get_mask = EVENT_INPUT;
269 iochan_setflag (h, assoc->cs_get_mask);
273 if ((event & assoc->cs_get_mask) || (event & EVENT_WORK)) /* input */
275 if ((assoc->cs_put_mask & EVENT_INPUT) == 0 && (event & assoc->cs_get_mask))
277 yaz_log(LOG_DEBUG, "ir_session (input)");
278 /* We aren't speaking to this fellow */
279 if (assoc->state == ASSOC_DEAD)
281 yaz_log(LOG_LOG, "Closed connection after reject");
283 destroy_association(assoc);
287 assoc->cs_get_mask = EVENT_INPUT;
288 if ((res = cs_get(conn, &assoc->input_buffer,
289 &assoc->input_buffer_len)) <= 0)
291 yaz_log(LOG_LOG, "Connection closed by client");
293 destroy_association(assoc);
297 else if (res == 1) /* incomplete read - wait for more */
299 if (conn->io_pending & CS_WANT_WRITE)
300 assoc->cs_get_mask |= EVENT_OUTPUT;
301 iochan_setflag(h, assoc->cs_get_mask);
304 if (cs_more(conn)) /* more stuff - call us again later, please */
305 iochan_setevent(h, EVENT_INPUT);
307 /* we got a complete PDU. Let's decode it */
308 yaz_log(LOG_DEBUG, "Got PDU, %d bytes", res);
309 req = request_get(&assoc->incoming); /* get a new request structure */
310 odr_reset(assoc->decode);
311 odr_setbuf(assoc->decode, assoc->input_buffer, res, 0);
312 if (!z_APDU(assoc->decode, &req->apdu_request, 0, 0))
314 yaz_log(LOG_LOG, "ODR error on incoming PDU: %s [near byte %d] ",
315 odr_errmsg(odr_geterror(assoc->decode)),
316 odr_offset(assoc->decode));
317 yaz_log(LOG_LOG, "PDU dump:");
318 odr_dumpBER(yaz_log_file(), assoc->input_buffer, res);
319 do_close(assoc, Z_Close_protocolError, "Malformed package");
322 req->request_mem = odr_extract_mem(assoc->decode);
323 if (assoc->print && !z_APDU(assoc->print, &req->apdu_request, 0, 0))
325 yaz_log(LOG_WARN, "ODR print error: %s",
326 odr_errmsg(odr_geterror(assoc->print)));
327 odr_reset(assoc->print);
329 request_enq(&assoc->incoming, req);
332 /* can we do something yet? */
333 req = request_head(&assoc->incoming);
334 if (req->state == REQUEST_IDLE)
337 request_deq(&assoc->incoming);
338 if (process_request(assoc, req, &msg) < 0)
339 do_close_req(assoc, Z_Close_systemProblem, msg, req);
342 if (event & assoc->cs_put_mask)
344 request *req = request_head(&assoc->outgoing);
346 assoc->cs_put_mask = 0;
347 yaz_log(LOG_DEBUG, "ir_session (output)");
348 req->state = REQUEST_PENDING;
349 switch (res = cs_put(conn, req->response, req->len_response))
352 yaz_log(LOG_LOG, "Connection closed by client");
354 destroy_association(assoc);
357 case 0: /* all sent - release the request structure */
358 yaz_log(LOG_DEBUG, "Wrote PDU, %d bytes", req->len_response);
359 nmem_destroy(req->request_mem);
360 request_deq(&assoc->outgoing);
361 request_release(req);
362 if (!request_head(&assoc->outgoing))
363 { /* restore mask for cs_get operation ... */
364 iochan_clearflag(h, EVENT_OUTPUT|EVENT_INPUT);
365 iochan_setflag(h, assoc->cs_get_mask);
368 assoc->cs_put_mask = EVENT_OUTPUT;
371 if (conn->io_pending & CS_WANT_WRITE)
372 assoc->cs_put_mask |= EVENT_OUTPUT;
373 if (conn->io_pending & CS_WANT_READ)
374 assoc->cs_put_mask |= EVENT_INPUT;
375 iochan_setflag(h, assoc->cs_put_mask);
378 if (event & EVENT_EXCEPT)
380 yaz_log(LOG_LOG, "ir_session (exception)");
382 destroy_association(assoc);
388 * Initiate request processing.
390 static int process_request(association *assoc, request *req, char **msg)
396 *msg = "Unknown Error";
397 assert(req && req->state == REQUEST_IDLE);
398 if (req->apdu_request->which != Z_APDU_initRequest && !assoc->init)
400 *msg = "Missing InitRequest";
403 switch (req->apdu_request->which)
405 case Z_APDU_initRequest:
406 res = process_initRequest(assoc, req); break;
407 case Z_APDU_searchRequest:
408 res = process_searchRequest(assoc, req, &fd); break;
409 case Z_APDU_presentRequest:
410 res = process_presentRequest(assoc, req, &fd); break;
411 case Z_APDU_scanRequest:
412 if (assoc->init->bend_scan)
413 res = process_scanRequest(assoc, req, &fd);
416 *msg = "Cannot handle Scan APDU";
420 case Z_APDU_extendedServicesRequest:
421 if (assoc->init->bend_esrequest)
422 res = process_ESRequest(assoc, req, &fd);
425 *msg = "Cannot handle Extended Services APDU";
429 case Z_APDU_sortRequest:
430 if (assoc->init->bend_sort)
431 res = process_sortRequest(assoc, req, &fd);
434 *msg = "Cannot handle Sort APDU";
439 process_close(assoc, req);
441 case Z_APDU_deleteResultSetRequest:
442 if (assoc->init->bend_delete)
443 res = process_deleteRequest(assoc, req, &fd);
446 *msg = "Cannot handle Delete APDU";
450 case Z_APDU_segmentRequest:
451 if (assoc->init->bend_segment)
453 res = process_segmentRequest (assoc, req);
457 *msg = "Cannot handle Segment APDU";
462 *msg = "Bad APDU received";
467 yaz_log(LOG_DEBUG, " result immediately available");
468 retval = process_response(assoc, req, res);
472 yaz_log(LOG_DEBUG, " result unavailble");
475 else /* no result yet - one will be provided later */
479 /* Set up an I/O handler for the fd supplied by the backend */
481 yaz_log(LOG_DEBUG, " establishing handler for result");
482 req->state = REQUEST_PENDING;
483 if (!(chan = iochan_create(fd, backend_response, EVENT_INPUT)))
485 iochan_setdata(chan, assoc);
492 * Handle message from the backend.
494 void backend_response(IOCHAN i, int event)
496 association *assoc = (association *)iochan_getdata(i);
497 request *req = request_head(&assoc->incoming);
501 yaz_log(LOG_DEBUG, "backend_response");
502 assert(assoc && req && req->state != REQUEST_IDLE);
503 /* determine what it is we're waiting for */
504 switch (req->apdu_request->which)
506 case Z_APDU_searchRequest:
507 res = response_searchRequest(assoc, req, 0, &fd); break;
509 case Z_APDU_presentRequest:
510 res = response_presentRequest(assoc, req, 0, &fd); break;
511 case Z_APDU_scanRequest:
512 res = response_scanRequest(assoc, req, 0, &fd); break;
515 yaz_log(LOG_WARN, "Serious programmer's lapse or bug");
518 if ((res && process_response(assoc, req, res) < 0) || fd < 0)
520 yaz_log(LOG_LOG, "Fatal error when talking to backend");
521 do_close(assoc, Z_Close_systemProblem, 0);
525 else if (!res) /* no result yet - try again later */
527 yaz_log(LOG_DEBUG, " no result yet");
528 iochan_setfd(i, fd); /* in case fd has changed */
533 * Encode response, and transfer the request structure to the outgoing queue.
535 static int process_response(association *assoc, request *req, Z_APDU *res)
537 odr_setbuf(assoc->encode, req->response, req->size_response, 1);
539 if (assoc->print && !z_APDU(assoc->print, &res, 0, 0))
541 yaz_log(LOG_WARN, "ODR print error: %s",
542 odr_errmsg(odr_geterror(assoc->print)));
543 odr_reset(assoc->print);
545 if (!z_APDU(assoc->encode, &res, 0, 0))
547 yaz_log(LOG_WARN, "ODR error when encoding response: %s",
548 odr_errmsg(odr_geterror(assoc->decode)));
551 req->response = odr_getbuf(assoc->encode, &req->len_response,
552 &req->size_response);
553 odr_setbuf(assoc->encode, 0, 0, 0); /* don'txfree if we abort later */
554 odr_reset(assoc->encode);
555 req->state = REQUEST_IDLE;
556 request_enq(&assoc->outgoing, req);
557 /* turn the work over to the ir_session handler */
558 iochan_setflag(assoc->client_chan, EVENT_OUTPUT);
559 assoc->cs_put_mask = EVENT_OUTPUT;
560 /* Is there more work to be done? give that to the input handler too */
562 if (request_head(&assoc->incoming))
564 yaz_log (LOG_DEBUG, "more work to be done");
565 iochan_setevent(assoc->client_chan, EVENT_WORK);
572 * Handle init request.
573 * At the moment, we don't check the options
574 * anywhere else in the code - we just try not to do anything that would
575 * break a naive client. We'll toss 'em into the association block when
576 * we need them there.
578 static Z_APDU *process_initRequest(association *assoc, request *reqb)
580 statserv_options_block *cb = statserv_getcontrol();
581 Z_InitRequest *req = reqb->apdu_request->u.initRequest;
582 Z_APDU *apdu = zget_APDU(assoc->encode, Z_APDU_initResponse);
583 Z_InitResponse *resp = apdu->u.initResponse;
584 bend_initresult *binitres;
589 assoc->init = (bend_initrequest *) xmalloc (sizeof(*assoc->init));
591 yaz_log(LOG_LOG, "Got initRequest");
592 if (req->implementationId)
593 yaz_log(LOG_LOG, "Id: %s", req->implementationId);
594 if (req->implementationName)
595 yaz_log(LOG_LOG, "Name: %s", req->implementationName);
596 if (req->implementationVersion)
597 yaz_log(LOG_LOG, "Version: %s", req->implementationVersion);
599 assoc->init->stream = assoc->encode;
600 assoc->init->print = assoc->print;
601 assoc->init->auth = req->idAuthentication;
602 assoc->init->referenceId = req->referenceId;
603 assoc->init->implementation_version = 0;
604 assoc->init->implementation_id = 0;
605 assoc->init->implementation_name = 0;
606 assoc->init->bend_sort = NULL;
607 assoc->init->bend_search = NULL;
608 assoc->init->bend_present = NULL;
609 assoc->init->bend_esrequest = NULL;
610 assoc->init->bend_delete = NULL;
611 assoc->init->bend_scan = NULL;
612 assoc->init->bend_segment = NULL;
613 assoc->init->bend_fetch = NULL;
614 assoc->init->charneg_request = NULL;
615 assoc->init->charneg_response = NULL;
616 assoc->init->decode = assoc->decode;
618 if (ODR_MASK_GET(req->options, Z_Options_negotiationModel))
620 Z_CharSetandLanguageNegotiation *negotiation =
621 yaz_get_charneg_record (req->otherInfo);
622 if (negotiation->which == Z_CharSetandLanguageNegotiation_proposal)
623 assoc->init->charneg_request = negotiation;
626 assoc->init->peer_name =
627 odr_strdup (assoc->encode, cs_addrstr(assoc->client_link));
628 if (!(binitres = (*cb->bend_init)(assoc->init)))
630 yaz_log(LOG_WARN, "Bad response from backend.");
634 assoc->backend = binitres->handle;
635 if ((assoc->init->bend_sort))
636 yaz_log (LOG_DEBUG, "Sort handler installed");
637 if ((assoc->init->bend_search))
638 yaz_log (LOG_DEBUG, "Search handler installed");
639 if ((assoc->init->bend_present))
640 yaz_log (LOG_DEBUG, "Present handler installed");
641 if ((assoc->init->bend_esrequest))
642 yaz_log (LOG_DEBUG, "ESRequest handler installed");
643 if ((assoc->init->bend_delete))
644 yaz_log (LOG_DEBUG, "Delete handler installed");
645 if ((assoc->init->bend_scan))
646 yaz_log (LOG_DEBUG, "Scan handler installed");
647 if ((assoc->init->bend_segment))
648 yaz_log (LOG_DEBUG, "Segment handler installed");
650 resp->referenceId = req->referenceId;
652 /* let's tell the client what we can do */
653 if (ODR_MASK_GET(req->options, Z_Options_search))
655 ODR_MASK_SET(resp->options, Z_Options_search);
656 strcat(options, "srch");
658 if (ODR_MASK_GET(req->options, Z_Options_present))
660 ODR_MASK_SET(resp->options, Z_Options_present);
661 strcat(options, " prst");
663 if (ODR_MASK_GET(req->options, Z_Options_delSet) &&
664 assoc->init->bend_delete)
666 ODR_MASK_SET(resp->options, Z_Options_delSet);
667 strcat(options, " del");
669 if (ODR_MASK_GET(req->options, Z_Options_extendedServices) &&
670 assoc->init->bend_esrequest)
672 ODR_MASK_SET(resp->options, Z_Options_extendedServices);
673 strcat (options, " extendedServices");
675 if (ODR_MASK_GET(req->options, Z_Options_namedResultSets))
677 ODR_MASK_SET(resp->options, Z_Options_namedResultSets);
678 strcat(options, " namedresults");
680 if (ODR_MASK_GET(req->options, Z_Options_scan) && assoc->init->bend_scan)
682 ODR_MASK_SET(resp->options, Z_Options_scan);
683 strcat(options, " scan");
685 if (ODR_MASK_GET(req->options, Z_Options_concurrentOperations))
687 ODR_MASK_SET(resp->options, Z_Options_concurrentOperations);
688 strcat(options, " concurrop");
690 if (ODR_MASK_GET(req->options, Z_Options_sort) && assoc->init->bend_sort)
692 ODR_MASK_SET(resp->options, Z_Options_sort);
693 strcat(options, " sort");
696 if (ODR_MASK_GET(req->options, Z_Options_negotiationModel)
697 && assoc->init->charneg_response)
699 Z_OtherInformation **p;
700 Z_OtherInformationUnit *p0;
702 yaz_oi_APDU(apdu, &p);
704 if ((p0=yaz_oi_update(p, assoc->encode, NULL, 0, 0))) {
705 ODR_MASK_SET(resp->options, Z_Options_negotiationModel);
707 p0->which = Z_OtherInfo_externallyDefinedInfo;
708 p0->information.externallyDefinedInfo =
709 assoc->init->charneg_response;
711 ODR_MASK_SET(resp->options, Z_Options_negotiationModel);
712 strcat(options, " negotiation");
715 if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_1))
717 ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_1);
718 assoc->version = 2; /* 1 & 2 are equivalent */
720 if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_2))
722 ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_2);
725 if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_3))
727 ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_3);
731 yaz_log(LOG_LOG, "Negotiated to v%d: %s", assoc->version, options);
732 assoc->maximumRecordSize = *req->maximumRecordSize;
733 if (assoc->maximumRecordSize > control_block->maxrecordsize)
734 assoc->maximumRecordSize = control_block->maxrecordsize;
735 assoc->preferredMessageSize = *req->preferredMessageSize;
736 if (assoc->preferredMessageSize > assoc->maximumRecordSize)
737 assoc->preferredMessageSize = assoc->maximumRecordSize;
740 assoc->maximumRecordSize = 3000000;
741 assoc->preferredMessageSize = 3000000;
744 resp->preferredMessageSize = &assoc->preferredMessageSize;
745 resp->maximumRecordSize = &assoc->maximumRecordSize;
747 resp->implementationName = "GFS/YAZ";
749 if (assoc->init->implementation_id)
752 odr_malloc (assoc->encode,
753 strlen(assoc->init->implementation_id) + 10 +
754 strlen(resp->implementationId));
755 sprintf (nv, "%s / %s",
756 resp->implementationId, assoc->init->implementation_id);
757 resp->implementationId = nv;
759 if (assoc->init->implementation_name)
762 odr_malloc (assoc->encode,
763 strlen(assoc->init->implementation_name) + 10 +
764 strlen(resp->implementationName));
765 sprintf (nv, "%s / %s",
766 resp->implementationName, assoc->init->implementation_name);
767 resp->implementationName = nv;
769 if (assoc->init->implementation_version)
772 odr_malloc (assoc->encode,
773 strlen(assoc->init->implementation_version) + 10 +
774 strlen(resp->implementationVersion));
775 sprintf (nv, "YAZ %s / %s",
776 resp->implementationVersion,
777 assoc->init->implementation_version);
778 resp->implementationVersion = nv;
781 if (binitres->errcode)
783 yaz_log(LOG_LOG, "Connection rejected by backend.");
785 assoc->state = ASSOC_DEAD;
788 assoc->state = ASSOC_UP;
793 * These functions should be merged.
796 static void set_addinfo (Z_DefaultDiagFormat *dr, char *addinfo, ODR odr)
798 dr->which = Z_DefaultDiagFormat_v2Addinfo;
799 dr->u.v2Addinfo = odr_strdup (odr, addinfo ? addinfo : "");
803 * nonsurrogate diagnostic record.
805 static Z_Records *diagrec(association *assoc, int error, char *addinfo)
807 Z_Records *rec = (Z_Records *)
808 odr_malloc (assoc->encode, sizeof(*rec));
809 int *err = odr_intdup(assoc->encode, error);
810 Z_DiagRec *drec = (Z_DiagRec *)
811 odr_malloc (assoc->encode, sizeof(*drec));
812 Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
813 odr_malloc (assoc->encode, sizeof(*dr));
815 yaz_log(LOG_LOG, "[%d] %s %s%s", error, diagbib1_str(error),
816 addinfo ? " -- " : "", addinfo ? addinfo : "");
817 rec->which = Z_Records_NSD;
818 rec->u.nonSurrogateDiagnostic = dr;
819 dr->diagnosticSetId =
820 yaz_oidval_to_z3950oid (assoc->encode, CLASS_DIAGSET, VAL_BIB1);
822 set_addinfo (dr, addinfo, assoc->encode);
827 * surrogate diagnostic.
829 static Z_NamePlusRecord *surrogatediagrec(association *assoc, char *dbname,
830 int error, char *addinfo)
832 Z_NamePlusRecord *rec = (Z_NamePlusRecord *)
833 odr_malloc (assoc->encode, sizeof(*rec));
834 int *err = odr_intdup(assoc->encode, error);
835 Z_DiagRec *drec = (Z_DiagRec *)odr_malloc (assoc->encode, sizeof(*drec));
836 Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
837 odr_malloc (assoc->encode, sizeof(*dr));
839 yaz_log(LOG_DEBUG, "SurrogateDiagnotic: %d -- %s", error, addinfo);
840 rec->databaseName = dbname;
841 rec->which = Z_NamePlusRecord_surrogateDiagnostic;
842 rec->u.surrogateDiagnostic = drec;
843 drec->which = Z_DiagRec_defaultFormat;
844 drec->u.defaultFormat = dr;
845 dr->diagnosticSetId =
846 yaz_oidval_to_z3950oid (assoc->encode, CLASS_DIAGSET, VAL_BIB1);
848 set_addinfo (dr, addinfo, assoc->encode);
854 * multiple nonsurrogate diagnostics.
856 static Z_DiagRecs *diagrecs(association *assoc, int error, char *addinfo)
858 Z_DiagRecs *recs = (Z_DiagRecs *)odr_malloc (assoc->encode, sizeof(*recs));
859 int *err = odr_intdup(assoc->encode, error);
860 Z_DiagRec **recp = (Z_DiagRec **)odr_malloc (assoc->encode, sizeof(*recp));
861 Z_DiagRec *drec = (Z_DiagRec *)odr_malloc (assoc->encode, sizeof(*drec));
862 Z_DefaultDiagFormat *rec = (Z_DefaultDiagFormat *)
863 odr_malloc (assoc->encode, sizeof(*rec));
865 yaz_log(LOG_DEBUG, "DiagRecs: %d -- %s", error, addinfo ? addinfo : "");
867 recs->num_diagRecs = 1;
868 recs->diagRecs = recp;
870 drec->which = Z_DiagRec_defaultFormat;
871 drec->u.defaultFormat = rec;
873 rec->diagnosticSetId =
874 yaz_oidval_to_z3950oid (assoc->encode, CLASS_DIAGSET, VAL_BIB1);
875 rec->condition = err;
877 rec->which = Z_DefaultDiagFormat_v2Addinfo;
878 rec->u.v2Addinfo = odr_strdup (assoc->encode, addinfo ? addinfo : "");
882 static Z_Records *pack_records(association *a, char *setname, int start,
883 int *num, Z_RecordComposition *comp,
884 int *next, int *pres, oid_value format,
885 Z_ReferenceId *referenceId,
888 int recno, total_length = 0, toget = *num, dumped_records = 0;
890 (Z_Records *) odr_malloc (a->encode, sizeof(*records));
891 Z_NamePlusRecordList *reclist =
892 (Z_NamePlusRecordList *) odr_malloc (a->encode, sizeof(*reclist));
893 Z_NamePlusRecord **list =
894 (Z_NamePlusRecord **) odr_malloc (a->encode, sizeof(*list) * toget);
896 records->which = Z_Records_DBOSD;
897 records->u.databaseOrSurDiagnostics = reclist;
898 reclist->num_records = 0;
899 reclist->records = list;
900 *pres = Z_PRES_SUCCESS;
904 yaz_log(LOG_LOG, "Request to pack %d+%d+%s", start, toget, setname);
905 yaz_log(LOG_DEBUG, "pms=%d, mrs=%d", a->preferredMessageSize,
906 a->maximumRecordSize);
907 for (recno = start; reclist->num_records < toget; recno++)
910 Z_NamePlusRecord *thisrec;
913 * we get the number of bytes allocated on the stream before any
914 * allocation done by the backend - this should give us a reasonable
915 * idea of the total size of the data so far.
917 total_length = odr_total(a->encode) - dumped_records;
923 freq.last_in_set = 0;
924 freq.setname = setname;
925 freq.surrogate_flag = 0;
928 freq.request_format = format;
929 freq.request_format_raw = oid;
930 freq.output_format = format;
931 freq.output_format_raw = 0;
932 freq.stream = a->encode;
933 freq.print = a->print;
934 freq.surrogate_flag = 0;
935 freq.referenceId = referenceId;
936 (*a->init->bend_fetch)(a->backend, &freq);
937 /* backend should be able to signal whether error is system-wide
938 or only pertaining to current record */
941 if (!freq.surrogate_flag)
944 *pres = Z_PRES_FAILURE;
945 /* for 'present request out of range',
946 set addinfo to record position if not set */
947 if (freq.errcode == 13 && freq.errstring == 0)
949 sprintf (s, "%d", recno);
952 return diagrec(a, freq.errcode, freq.errstring);
954 reclist->records[reclist->num_records] =
955 surrogatediagrec(a, freq.basename, freq.errcode,
957 reclist->num_records++;
958 *next = freq.last_in_set ? 0 : recno + 1;
962 this_length = freq.len;
964 this_length = odr_total(a->encode) - total_length;
965 yaz_log(LOG_DEBUG, " fetched record, len=%d, total=%d",
966 this_length, total_length);
967 if (this_length + total_length > a->preferredMessageSize)
969 /* record is small enough, really */
970 if (this_length <= a->preferredMessageSize)
972 yaz_log(LOG_DEBUG, " Dropped last normal-sized record");
973 *pres = Z_PRES_PARTIAL_2;
976 /* record can only be fetched by itself */
977 if (this_length < a->maximumRecordSize)
979 yaz_log(LOG_DEBUG, " Record > prefmsgsz");
982 yaz_log(LOG_DEBUG, " Dropped it");
983 reclist->records[reclist->num_records] =
984 surrogatediagrec(a, freq.basename, 16, 0);
985 reclist->num_records++;
986 *next = freq.last_in_set ? 0 : recno + 1;
987 dumped_records += this_length;
991 else /* too big entirely */
993 yaz_log(LOG_LOG, "Record > maxrcdsz this=%d max=%d", this_length, a->maximumRecordSize);
994 reclist->records[reclist->num_records] =
995 surrogatediagrec(a, freq.basename, 17, 0);
996 reclist->num_records++;
997 *next = freq.last_in_set ? 0 : recno + 1;
998 dumped_records += this_length;
1003 if (!(thisrec = (Z_NamePlusRecord *)
1004 odr_malloc(a->encode, sizeof(*thisrec))))
1006 if (!(thisrec->databaseName = (char *)odr_malloc(a->encode,
1007 strlen(freq.basename) + 1)))
1009 strcpy(thisrec->databaseName, freq.basename);
1010 thisrec->which = Z_NamePlusRecord_databaseRecord;
1012 if (freq.output_format_raw)
1014 struct oident *ident = oid_getentbyoid(freq.output_format_raw);
1015 freq.output_format = ident->value;
1017 thisrec->u.databaseRecord = z_ext_record(a->encode, freq.output_format,
1018 freq.record, freq.len);
1019 if (!thisrec->u.databaseRecord)
1021 reclist->records[reclist->num_records] = thisrec;
1022 reclist->num_records++;
1023 *next = freq.last_in_set ? 0 : recno + 1;
1025 *num = reclist->num_records;
1029 static Z_APDU *process_searchRequest(association *assoc, request *reqb,
1032 Z_SearchRequest *req = reqb->apdu_request->u.searchRequest;
1033 bend_search_rr *bsrr =
1034 (bend_search_rr *)nmem_malloc (reqb->request_mem, sizeof(*bsrr));
1036 yaz_log(LOG_LOG, "Got SearchRequest.");
1038 bsrr->request = reqb;
1039 bsrr->association = assoc;
1040 bsrr->referenceId = req->referenceId;
1041 save_referenceId (reqb, bsrr->referenceId);
1043 yaz_log (LOG_LOG, "ResultSet '%s'", req->resultSetName);
1044 if (req->databaseNames)
1047 for (i = 0; i < req->num_databaseNames; i++)
1048 yaz_log (LOG_LOG, "Database '%s'", req->databaseNames[i]);
1050 switch (req->query->which)
1052 case Z_Query_type_1: case Z_Query_type_101:
1053 log_rpn_query (req->query->u.type_1);
1055 if (assoc->init->bend_search)
1057 bsrr->setname = req->resultSetName;
1058 bsrr->replace_set = *req->replaceIndicator;
1059 bsrr->num_bases = req->num_databaseNames;
1060 bsrr->basenames = req->databaseNames;
1061 bsrr->query = req->query;
1062 bsrr->stream = assoc->encode;
1063 bsrr->decode = assoc->decode;
1064 bsrr->print = assoc->print;
1067 bsrr->errstring = NULL;
1068 bsrr->search_info = NULL;
1069 (assoc->init->bend_search)(assoc->backend, bsrr);
1073 return response_searchRequest(assoc, reqb, bsrr, fd);
1076 int bend_searchresponse(void *handle, bend_search_rr *bsrr) {return 0;}
1079 * Prepare a searchresponse based on the backend results. We probably want
1080 * to look at making the fetching of records nonblocking as well, but
1081 * so far, we'll keep things simple.
1082 * If bsrt is null, that means we're called in response to a communications
1083 * event, and we'll have to get the response for ourselves.
1085 static Z_APDU *response_searchRequest(association *assoc, request *reqb,
1086 bend_search_rr *bsrt, int *fd)
1088 Z_SearchRequest *req = reqb->apdu_request->u.searchRequest;
1089 Z_APDU *apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1090 Z_SearchResponse *resp = (Z_SearchResponse *)
1091 odr_malloc (assoc->encode, sizeof(*resp));
1092 int *nulint = odr_intdup (assoc->encode, 0);
1093 bool_t *sr = odr_intdup(assoc->encode, 1);
1094 int *next = odr_intdup(assoc->encode, 0);
1095 int *none = odr_intdup(assoc->encode, Z_RES_NONE);
1097 apdu->which = Z_APDU_searchResponse;
1098 apdu->u.searchResponse = resp;
1099 resp->referenceId = req->referenceId;
1100 resp->additionalSearchInfo = 0;
1101 resp->otherInfo = 0;
1103 if (!bsrt && !bend_searchresponse(assoc->backend, bsrt))
1105 yaz_log(LOG_FATAL, "Bad result from backend");
1108 else if (bsrt->errcode)
1110 resp->records = diagrec(assoc, bsrt->errcode, bsrt->errstring);
1111 resp->resultCount = nulint;
1112 resp->numberOfRecordsReturned = nulint;
1113 resp->nextResultSetPosition = nulint;
1114 resp->searchStatus = nulint;
1115 resp->resultSetStatus = none;
1116 resp->presentStatus = 0;
1120 int *toget = odr_intdup(assoc->encode, 0);
1121 int *presst = odr_intdup(assoc->encode, 0);
1122 Z_RecordComposition comp, *compp = 0;
1124 yaz_log (LOG_LOG, "resultCount: %d", bsrt->hits);
1127 resp->resultCount = &bsrt->hits;
1129 comp.which = Z_RecordComp_simple;
1130 /* how many records does the user agent want, then? */
1131 if (bsrt->hits <= *req->smallSetUpperBound)
1133 *toget = bsrt->hits;
1134 if ((comp.u.simple = req->smallSetElementSetNames))
1137 else if (bsrt->hits < *req->largeSetLowerBound)
1139 *toget = *req->mediumSetPresentNumber;
1140 if (*toget > bsrt->hits)
1141 *toget = bsrt->hits;
1142 if ((comp.u.simple = req->mediumSetElementSetNames))
1148 if (*toget && !resp->records)
1153 if (!(prefformat = oid_getentbyoid(req->preferredRecordSyntax)))
1156 form = prefformat->value;
1157 resp->records = pack_records(assoc, req->resultSetName, 1,
1158 toget, compp, next, presst, form, req->referenceId,
1159 req->preferredRecordSyntax);
1162 resp->numberOfRecordsReturned = toget;
1163 resp->nextResultSetPosition = next;
1164 resp->searchStatus = sr;
1165 resp->resultSetStatus = 0;
1166 resp->presentStatus = presst;
1170 if (*resp->resultCount)
1172 resp->numberOfRecordsReturned = nulint;
1173 resp->nextResultSetPosition = next;
1174 resp->searchStatus = sr;
1175 resp->resultSetStatus = 0;
1176 resp->presentStatus = 0;
1179 resp->additionalSearchInfo = bsrt->search_info;
1184 * Maybe we got a little over-friendly when we designed bend_fetch to
1185 * get only one record at a time. Some backends can optimise multiple-record
1186 * fetches, and at any rate, there is some overhead involved in
1187 * all that selecting and hopping around. Problem is, of course, that the
1188 * frontend can't know ahead of time how many records it'll need to
1189 * fill the negotiated PDU size. Annoying. Segmentation or not, Z/SR
1190 * is downright lousy as a bulk data transfer protocol.
1192 * To start with, we'll do the fetching of records from the backend
1193 * in one operation: To save some trips in and out of the event-handler,
1194 * and to simplify the interface to pack_records. At any rate, asynch
1195 * operation is more fun in operations that have an unpredictable execution
1196 * speed - which is normally more true for search than for present.
1198 static Z_APDU *process_presentRequest(association *assoc, request *reqb,
1201 Z_PresentRequest *req = reqb->apdu_request->u.presentRequest;
1205 Z_PresentResponse *resp;
1209 yaz_log(LOG_LOG, "Got PresentRequest.");
1211 if (!(prefformat = oid_getentbyoid(req->preferredRecordSyntax)))
1214 form = prefformat->value;
1215 resp = (Z_PresentResponse *)odr_malloc (assoc->encode, sizeof(*resp));
1217 resp->presentStatus = odr_intdup(assoc->encode, 0);
1218 if (assoc->init->bend_present)
1220 bend_present_rr *bprr = (bend_present_rr *)
1221 nmem_malloc (reqb->request_mem, sizeof(*bprr));
1222 bprr->setname = req->resultSetId;
1223 bprr->start = *req->resultSetStartPoint;
1224 bprr->number = *req->numberOfRecordsRequested;
1225 bprr->format = form;
1226 bprr->comp = req->recordComposition;
1227 bprr->referenceId = req->referenceId;
1228 bprr->stream = assoc->encode;
1229 bprr->print = assoc->print;
1230 bprr->request = reqb;
1231 bprr->association = assoc;
1233 bprr->errstring = NULL;
1234 (*assoc->init->bend_present)(assoc->backend, bprr);
1240 resp->records = diagrec(assoc, bprr->errcode, bprr->errstring);
1241 *resp->presentStatus = Z_PRES_FAILURE;
1244 apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1245 next = odr_intdup(assoc->encode, 0);
1246 num = odr_intdup(assoc->encode, 0);
1248 apdu->which = Z_APDU_presentResponse;
1249 apdu->u.presentResponse = resp;
1250 resp->referenceId = req->referenceId;
1251 resp->otherInfo = 0;
1255 *num = *req->numberOfRecordsRequested;
1257 pack_records(assoc, req->resultSetId, *req->resultSetStartPoint,
1258 num, req->recordComposition, next, resp->presentStatus,
1259 form, req->referenceId, req->preferredRecordSyntax);
1263 resp->numberOfRecordsReturned = num;
1264 resp->nextResultSetPosition = next;
1270 * Scan was implemented rather in a hurry, and with support for only the basic
1271 * elements of the service in the backend API. Suggestions are welcome.
1273 static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd)
1275 Z_ScanRequest *req = reqb->apdu_request->u.scanRequest;
1276 Z_APDU *apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1277 Z_ScanResponse *res = (Z_ScanResponse *)
1278 odr_malloc (assoc->encode, sizeof(*res));
1279 int *scanStatus = odr_intdup(assoc->encode, Z_Scan_failure);
1280 int *numberOfEntriesReturned = odr_intdup(assoc->encode, 0);
1281 Z_ListEntries *ents = (Z_ListEntries *)
1282 odr_malloc (assoc->encode, sizeof(*ents));
1283 Z_DiagRecs *diagrecs_p = NULL;
1285 bend_scan_rr *bsrr = (bend_scan_rr *)
1286 odr_malloc (assoc->encode, sizeof(*bsrr));
1288 yaz_log(LOG_LOG, "Got ScanRequest");
1290 apdu->which = Z_APDU_scanResponse;
1291 apdu->u.scanResponse = res;
1292 res->referenceId = req->referenceId;
1294 /* if step is absent, set it to 0 */
1295 res->stepSize = odr_intdup(assoc->encode, 0);
1297 *res->stepSize = *req->stepSize;
1299 res->scanStatus = scanStatus;
1300 res->numberOfEntriesReturned = numberOfEntriesReturned;
1301 res->positionOfTerm = 0;
1302 res->entries = ents;
1303 ents->num_entries = 0;
1304 ents->entries = NULL;
1305 ents->num_nonsurrogateDiagnostics = 0;
1306 ents->nonsurrogateDiagnostics = NULL;
1307 res->attributeSet = 0;
1310 if (req->databaseNames)
1313 for (i = 0; i < req->num_databaseNames; i++)
1314 yaz_log (LOG_LOG, "Database '%s'", req->databaseNames[i]);
1316 bsrr->num_bases = req->num_databaseNames;
1317 bsrr->basenames = req->databaseNames;
1318 bsrr->num_entries = *req->numberOfTermsRequested;
1319 bsrr->term = req->termListAndStartPoint;
1320 bsrr->referenceId = req->referenceId;
1321 bsrr->stream = assoc->encode;
1322 bsrr->print = assoc->print;
1323 bsrr->step_size = res->stepSize;
1324 if (req->attributeSet &&
1325 (attset = oid_getentbyoid(req->attributeSet)) &&
1326 (attset->oclass == CLASS_ATTSET || attset->oclass == CLASS_GENERAL))
1327 bsrr->attributeset = attset->value;
1329 bsrr->attributeset = VAL_NONE;
1330 log_scan_term (req->termListAndStartPoint, bsrr->attributeset);
1331 bsrr->term_position = req->preferredPositionInResponse ?
1332 *req->preferredPositionInResponse : 1;
1333 ((int (*)(void *, bend_scan_rr *))
1334 (*assoc->init->bend_scan))(assoc->backend, bsrr);
1336 diagrecs_p = diagrecs(assoc, bsrr->errcode, bsrr->errstring);
1340 Z_Entry **tab = (Z_Entry **)
1341 odr_malloc (assoc->encode, sizeof(*tab) * bsrr->num_entries);
1343 if (bsrr->status == BEND_SCAN_PARTIAL)
1344 *scanStatus = Z_Scan_partial_5;
1346 *scanStatus = Z_Scan_success;
1347 ents->entries = tab;
1348 ents->num_entries = bsrr->num_entries;
1349 res->numberOfEntriesReturned = &ents->num_entries;
1350 res->positionOfTerm = &bsrr->term_position;
1351 for (i = 0; i < bsrr->num_entries; i++)
1357 tab[i] = e = (Z_Entry *)odr_malloc(assoc->encode, sizeof(*e));
1358 if (bsrr->entries[i].occurrences >= 0)
1360 e->which = Z_Entry_termInfo;
1361 e->u.termInfo = t = (Z_TermInfo *)
1362 odr_malloc(assoc->encode, sizeof(*t));
1363 t->suggestedAttributes = 0;
1365 t->alternativeTerm = 0;
1366 t->byAttributes = 0;
1367 t->otherTermInfo = 0;
1368 t->globalOccurrences = &bsrr->entries[i].occurrences;
1369 t->term = (Z_Term *)
1370 odr_malloc(assoc->encode, sizeof(*t->term));
1371 t->term->which = Z_Term_general;
1372 t->term->u.general = o =
1373 (Odr_oct *)odr_malloc(assoc->encode, sizeof(Odr_oct));
1374 o->buf = (unsigned char *)
1375 odr_malloc(assoc->encode, o->len = o->size =
1376 strlen(bsrr->entries[i].term));
1377 memcpy(o->buf, bsrr->entries[i].term, o->len);
1378 yaz_log(LOG_DEBUG, " term #%d: '%s' (%d)", i,
1379 bsrr->entries[i].term, bsrr->entries[i].occurrences);
1383 Z_DiagRecs *drecs = diagrecs (assoc,
1384 bsrr->entries[i].errcode,
1385 bsrr->entries[i].errstring);
1386 assert (drecs->num_diagRecs == 1);
1387 e->which = Z_Entry_surrogateDiagnostic;
1388 assert (drecs->diagRecs[0]);
1389 e->u.surrogateDiagnostic = drecs->diagRecs[0];
1395 ents->num_nonsurrogateDiagnostics = diagrecs_p->num_diagRecs;
1396 ents->nonsurrogateDiagnostics = diagrecs_p->diagRecs;
1401 static Z_APDU *process_sortRequest(association *assoc, request *reqb,
1404 Z_SortRequest *req = reqb->apdu_request->u.sortRequest;
1405 Z_SortResponse *res = (Z_SortResponse *)
1406 odr_malloc (assoc->encode, sizeof(*res));
1407 bend_sort_rr *bsrr = (bend_sort_rr *)
1408 odr_malloc (assoc->encode, sizeof(*bsrr));
1410 Z_APDU *apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1412 yaz_log(LOG_LOG, "Got SortRequest.");
1414 bsrr->num_input_setnames = req->num_inputResultSetNames;
1415 bsrr->input_setnames = req->inputResultSetNames;
1416 bsrr->referenceId = req->referenceId;
1417 bsrr->output_setname = req->sortedResultSetName;
1418 bsrr->sort_sequence = req->sortSequence;
1419 bsrr->stream = assoc->encode;
1420 bsrr->print = assoc->print;
1422 bsrr->sort_status = Z_SortStatus_failure;
1424 bsrr->errstring = 0;
1426 (*assoc->init->bend_sort)(assoc->backend, bsrr);
1428 res->referenceId = bsrr->referenceId;
1429 res->sortStatus = odr_intdup(assoc->encode, bsrr->sort_status);
1430 res->resultSetStatus = 0;
1433 Z_DiagRecs *dr = diagrecs (assoc, bsrr->errcode, bsrr->errstring);
1434 res->diagnostics = dr->diagRecs;
1435 res->num_diagnostics = dr->num_diagRecs;
1439 res->num_diagnostics = 0;
1440 res->diagnostics = 0;
1444 apdu->which = Z_APDU_sortResponse;
1445 apdu->u.sortResponse = res;
1449 static Z_APDU *process_deleteRequest(association *assoc, request *reqb,
1452 Z_DeleteResultSetRequest *req =
1453 reqb->apdu_request->u.deleteResultSetRequest;
1454 Z_DeleteResultSetResponse *res = (Z_DeleteResultSetResponse *)
1455 odr_malloc (assoc->encode, sizeof(*res));
1456 bend_delete_rr *bdrr = (bend_delete_rr *)
1457 odr_malloc (assoc->encode, sizeof(*bdrr));
1458 Z_APDU *apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1460 yaz_log(LOG_LOG, "Got DeleteRequest.");
1462 bdrr->num_setnames = req->num_resultSetList;
1463 bdrr->setnames = req->resultSetList;
1464 bdrr->stream = assoc->encode;
1465 bdrr->print = assoc->print;
1466 bdrr->function = *req->deleteFunction;
1467 bdrr->referenceId = req->referenceId;
1469 if (bdrr->num_setnames > 0)
1472 bdrr->statuses = (int*)
1473 odr_malloc(assoc->encode, sizeof(*bdrr->statuses) *
1474 bdrr->num_setnames);
1475 for (i = 0; i < bdrr->num_setnames; i++)
1476 bdrr->statuses[i] = 0;
1478 (*assoc->init->bend_delete)(assoc->backend, bdrr);
1480 res->referenceId = req->referenceId;
1482 res->deleteOperationStatus = odr_intdup(assoc->encode,bdrr->delete_status);
1484 res->deleteListStatuses = 0;
1485 if (bdrr->num_setnames > 0)
1488 res->deleteListStatuses = (Z_ListStatuses *)
1489 odr_malloc(assoc->encode, sizeof(*res->deleteListStatuses));
1490 res->deleteListStatuses->num = bdrr->num_setnames;
1491 res->deleteListStatuses->elements =
1493 odr_malloc (assoc->encode,
1494 sizeof(*res->deleteListStatuses->elements) *
1495 bdrr->num_setnames);
1496 for (i = 0; i<bdrr->num_setnames; i++)
1498 res->deleteListStatuses->elements[i] =
1500 odr_malloc (assoc->encode,
1501 sizeof(**res->deleteListStatuses->elements));
1502 res->deleteListStatuses->elements[i]->status = bdrr->statuses+i;
1503 res->deleteListStatuses->elements[i]->id =
1504 odr_strdup (assoc->encode, bdrr->setnames[i]);
1508 res->numberNotDeleted = 0;
1509 res->bulkStatuses = 0;
1510 res->deleteMessage = 0;
1513 apdu->which = Z_APDU_deleteResultSetResponse;
1514 apdu->u.deleteResultSetResponse = res;
1518 static void process_close(association *assoc, request *reqb)
1520 Z_Close *req = reqb->apdu_request->u.close;
1521 static char *reasons[] =
1528 "securityViolation",
1535 yaz_log(LOG_LOG, "Got Close, reason %s, message %s",
1536 reasons[*req->closeReason], req->diagnosticInformation ?
1537 req->diagnosticInformation : "NULL");
1538 if (assoc->version < 3) /* to make do_force respond with close */
1540 do_close_req(assoc, Z_Close_finished,
1541 "Association terminated by client", reqb);
1544 void save_referenceId (request *reqb, Z_ReferenceId *refid)
1548 reqb->len_refid = refid->len;
1549 reqb->refid = (char *)nmem_malloc (reqb->request_mem, refid->len);
1550 memcpy (reqb->refid, refid->buf, refid->len);
1554 reqb->len_refid = 0;
1559 void bend_request_send (bend_association a, bend_request req, Z_APDU *res)
1561 process_response (a, req, res);
1564 bend_request bend_request_mk (bend_association a)
1566 request *nreq = request_get (&a->outgoing);
1567 nreq->request_mem = nmem_create ();
1571 Z_ReferenceId *bend_request_getid (ODR odr, bend_request req)
1576 id = (Odr_oct *)odr_malloc (odr, sizeof(*odr));
1577 id->buf = (unsigned char *)odr_malloc (odr, req->len_refid);
1578 id->len = id->size = req->len_refid;
1579 memcpy (id->buf, req->refid, req->len_refid);
1583 void bend_request_destroy (bend_request *req)
1585 nmem_destroy((*req)->request_mem);
1586 request_release(*req);
1590 int bend_backend_respond (bend_association a, bend_request req)
1594 r = process_request (a, req, &msg);
1596 logf (LOG_WARN, "%s", msg);
1600 void bend_request_setdata(bend_request r, void *p)
1605 void *bend_request_getdata(bend_request r)
1607 return r->clientData;
1610 static Z_APDU *process_segmentRequest (association *assoc, request *reqb)
1612 bend_segment_rr req;
1614 req.segment = reqb->apdu_request->u.segmentRequest;
1615 req.stream = assoc->encode;
1616 req.decode = assoc->decode;
1617 req.print = assoc->print;
1618 req.association = assoc;
1620 (*assoc->init->bend_segment)(assoc->backend, &req);
1625 static Z_APDU *process_ESRequest(association *assoc, request *reqb, int *fd)
1627 bend_esrequest_rr esrequest;
1629 Z_ExtendedServicesRequest *req = reqb->apdu_request->u.extendedServicesRequest;
1630 Z_APDU *apdu = zget_APDU(assoc->encode, Z_APDU_extendedServicesResponse);
1632 Z_ExtendedServicesResponse *resp = apdu->u.extendedServicesResponse;
1634 yaz_log(LOG_DEBUG,"inside Process esRequest");
1636 esrequest.esr = reqb->apdu_request->u.extendedServicesRequest;
1637 esrequest.stream = assoc->encode;
1638 esrequest.decode = assoc->decode;
1639 esrequest.print = assoc->print;
1640 esrequest.errcode = 0;
1641 esrequest.errstring = NULL;
1642 esrequest.request = reqb;
1643 esrequest.association = assoc;
1644 esrequest.taskPackage = 0;
1645 esrequest.referenceId = req->referenceId;
1647 (*assoc->init->bend_esrequest)(assoc->backend, &esrequest);
1649 /* If the response is being delayed, return NULL */
1650 if (esrequest.request == NULL)
1653 resp->referenceId = req->referenceId;
1655 if (esrequest.errcode == -1)
1657 /* Backend service indicates request will be processed */
1658 yaz_log(LOG_DEBUG,"Request could be processed...Accepted !");
1659 *resp->operationStatus = Z_ExtendedServicesResponse_accepted;
1661 else if (esrequest.errcode == 0)
1663 /* Backend service indicates request will be processed */
1664 yaz_log(LOG_DEBUG,"Request could be processed...Done !");
1665 *resp->operationStatus = Z_ExtendedServicesResponse_done;
1669 Z_DiagRecs *diagRecs = diagrecs (assoc, esrequest.errcode,
1670 esrequest.errstring);
1672 /* Backend indicates error, request will not be processed */
1673 yaz_log(LOG_DEBUG,"Request could not be processed...failure !");
1674 *resp->operationStatus = Z_ExtendedServicesResponse_failure;
1675 resp->num_diagnostics = diagRecs->num_diagRecs;
1676 resp->diagnostics = diagRecs->diagRecs;
1678 /* Do something with the members of bend_extendedservice */
1679 if (esrequest.taskPackage)
1680 resp->taskPackage = z_ext_record (assoc->encode, VAL_EXTENDED,
1681 (const char *) esrequest.taskPackage,
1683 yaz_log(LOG_DEBUG,"Send the result apdu");