2 * Copyright (c) 1995-2001, Index Data
3 * See the file LICENSE for details.
5 * $Id: seshigh.c,v 1.122 2001-11-22 09:45:03 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>
49 #include <yaz/backend.h>
51 static int process_request(association *assoc, request *req, char **msg);
52 void backend_response(IOCHAN i, int event);
53 static int process_response(association *assoc, request *req, Z_APDU *res);
54 static Z_APDU *process_initRequest(association *assoc, request *reqb);
55 static Z_APDU *process_searchRequest(association *assoc, request *reqb,
57 static Z_APDU *response_searchRequest(association *assoc, request *reqb,
58 bend_search_rr *bsrr, int *fd);
59 static Z_APDU *process_presentRequest(association *assoc, request *reqb,
61 static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd);
62 static Z_APDU *process_sortRequest(association *assoc, request *reqb, int *fd);
63 static void process_close(association *assoc, request *reqb);
64 void save_referenceId (request *reqb, Z_ReferenceId *refid);
65 static Z_APDU *process_deleteRequest(association *assoc, request *reqb,
67 static Z_APDU *process_segmentRequest (association *assoc, request *reqb);
69 static FILE *apduf = 0; /* for use in static mode */
70 static statserv_options_block *control_block = 0;
72 /* Chas: Added in from DALI */
73 static Z_APDU *process_ESRequest(association *assoc, request *reqb, int *fd);
74 /* Chas: End of addition from DALI */
77 * Create and initialize a new association-handle.
78 * channel : iochannel for the current line.
79 * link : communications channel.
80 * Returns: 0 or a new association handle.
82 association *create_association(IOCHAN channel, COMSTACK link)
87 control_block = statserv_getcontrol();
88 if (!(anew = (association *)xmalloc(sizeof(*anew))))
91 anew->client_chan = channel;
92 anew->client_link = link;
93 anew->cs_get_mask = 0;
94 anew->cs_put_mask = 0;
95 anew->cs_accept_mask = 0;
96 if (!(anew->decode = odr_createmem(ODR_DECODE)) ||
97 !(anew->encode = odr_createmem(ODR_ENCODE)))
99 if (*control_block->apdufile)
104 strcpy(filename, control_block->apdufile);
105 if (!(anew->print = odr_createmem(ODR_PRINT)))
107 if (*control_block->apdufile != '-')
109 strcpy(filename, control_block->apdufile);
110 if (!control_block->dynamic)
114 if (!(apduf = fopen(filename, "w")))
116 yaz_log(LOG_WARN|LOG_ERRNO, "%s", filename);
119 setvbuf(apduf, 0, _IONBF, 0);
125 sprintf(filename + strlen(filename), ".%d", getpid());
126 if (!(f = fopen(filename, "w")))
128 yaz_log(LOG_WARN|LOG_ERRNO, "%s", filename);
131 setvbuf(f, 0, _IONBF, 0);
133 odr_setprint(anew->print, f);
138 anew->input_buffer = 0;
139 anew->input_buffer_len = 0;
141 anew->state = ASSOC_NEW;
142 request_initq(&anew->incoming);
143 request_initq(&anew->outgoing);
144 anew->proto = cs_getproto(link);
149 * Free association and release resources.
151 void destroy_association(association *h)
153 statserv_options_block *cb = statserv_getcontrol();
156 odr_destroy(h->decode);
157 odr_destroy(h->encode);
159 odr_destroy(h->print);
161 xfree(h->input_buffer);
163 (*cb->bend_close)(h->backend);
164 while (request_deq(&h->incoming));
165 while (request_deq(&h->outgoing));
166 request_delq(&h->incoming);
167 request_delq(&h->outgoing);
169 if (control_block && control_block->one_shot)
173 static void do_close_req(association *a, int reason, char *message,
177 Z_Close *cls = zget_Close(a->encode);
179 /* Purge request queue */
180 while (request_deq(&a->incoming));
181 while (request_deq(&a->outgoing));
184 yaz_log(LOG_LOG, "Sending Close PDU, reason=%d, message=%s",
185 reason, message ? message : "none");
186 apdu.which = Z_APDU_close;
188 *cls->closeReason = reason;
189 cls->diagnosticInformation = message;
190 process_response(a, req, &apdu);
191 iochan_settimeout(a->client_chan, 60);
195 yaz_log(LOG_DEBUG, "v2 client. No Close PDU");
196 iochan_setevent(a->client_chan, EVENT_TIMEOUT); /* force imm close */
198 a->state = ASSOC_DEAD;
201 static void do_close(association *a, int reason, char *message)
203 do_close_req (a, reason, message, request_get(&a->outgoing));
207 * This is where PDUs from the client are read and the further
208 * processing is initiated. Flow of control moves down through the
209 * various process_* functions below, until the encoded result comes back up
210 * to the output handler in here.
212 * h : the I/O channel that has an outstanding event.
213 * event : the current outstanding event.
215 void ir_session(IOCHAN h, int event)
218 association *assoc = (association *)iochan_getdata(h);
219 COMSTACK conn = assoc->client_link;
222 assert(h && conn && assoc);
223 if (event == EVENT_TIMEOUT)
225 if (assoc->state != ASSOC_UP)
227 yaz_log(LOG_LOG, "Final timeout - closing connection.");
229 destroy_association(assoc);
234 yaz_log(LOG_LOG, "Session idle too long. Sending close.");
235 do_close(assoc, Z_Close_lackOfActivity, 0);
239 if (event & assoc->cs_accept_mask)
241 yaz_log (LOG_DEBUG, "ir_session (accept)");
242 if (!cs_accept (conn))
244 yaz_log (LOG_LOG, "accept failed");
245 destroy_association(assoc);
248 iochan_clearflag (h, EVENT_OUTPUT|EVENT_OUTPUT);
249 if (conn->io_pending)
250 { /* cs_accept didn't complete */
251 assoc->cs_accept_mask =
252 ((conn->io_pending & CS_WANT_WRITE) ? EVENT_OUTPUT : 0) |
253 ((conn->io_pending & CS_WANT_READ) ? EVENT_INPUT : 0);
255 iochan_setflag (h, assoc->cs_accept_mask);
258 { /* cs_accept completed. Prepare for reading (cs_get) */
259 assoc->cs_accept_mask = 0;
260 assoc->cs_get_mask = EVENT_INPUT;
261 iochan_setflag (h, assoc->cs_get_mask);
265 if ((event & assoc->cs_get_mask) || (event & EVENT_WORK)) /* input */
267 if ((assoc->cs_put_mask & EVENT_INPUT) == 0 && (event & assoc->cs_get_mask))
269 yaz_log(LOG_DEBUG, "ir_session (input)");
270 /* We aren't speaking to this fellow */
271 if (assoc->state == ASSOC_DEAD)
273 yaz_log(LOG_LOG, "Closed connection after reject");
275 destroy_association(assoc);
279 assoc->cs_get_mask = EVENT_INPUT;
280 if ((res = cs_get(conn, &assoc->input_buffer,
281 &assoc->input_buffer_len)) <= 0)
283 yaz_log(LOG_LOG, "Connection closed by client");
285 destroy_association(assoc);
289 else if (res == 1) /* incomplete read - wait for more */
291 if (conn->io_pending & CS_WANT_WRITE)
292 assoc->cs_get_mask |= EVENT_OUTPUT;
293 iochan_setflag(h, assoc->cs_get_mask);
296 if (cs_more(conn)) /* more stuff - call us again later, please */
297 iochan_setevent(h, EVENT_INPUT);
299 /* we got a complete PDU. Let's decode it */
300 yaz_log(LOG_DEBUG, "Got PDU, %d bytes", res);
301 req = request_get(&assoc->incoming); /* get a new request structure */
302 odr_reset(assoc->decode);
303 odr_setbuf(assoc->decode, assoc->input_buffer, res, 0);
304 if (!z_APDU(assoc->decode, &req->apdu_request, 0, 0))
306 yaz_log(LOG_LOG, "ODR error on incoming PDU: %s [near byte %d] ",
307 odr_errmsg(odr_geterror(assoc->decode)),
308 odr_offset(assoc->decode));
309 yaz_log(LOG_LOG, "PDU dump:");
310 odr_dumpBER(yaz_log_file(), assoc->input_buffer, res);
311 do_close(assoc, Z_Close_protocolError, "Malformed package");
314 req->request_mem = odr_extract_mem(assoc->decode);
315 if (assoc->print && !z_APDU(assoc->print, &req->apdu_request, 0, 0))
317 yaz_log(LOG_WARN, "ODR print error: %s",
318 odr_errmsg(odr_geterror(assoc->print)));
319 odr_reset(assoc->print);
321 request_enq(&assoc->incoming, req);
324 /* can we do something yet? */
325 req = request_head(&assoc->incoming);
326 if (req->state == REQUEST_IDLE)
329 request_deq(&assoc->incoming);
330 if (process_request(assoc, req, &msg) < 0)
331 do_close_req(assoc, Z_Close_systemProblem, msg, req);
334 if (event & assoc->cs_put_mask)
336 request *req = request_head(&assoc->outgoing);
338 assoc->cs_put_mask = 0;
339 yaz_log(LOG_DEBUG, "ir_session (output)");
340 req->state = REQUEST_PENDING;
341 switch (res = cs_put(conn, req->response, req->len_response))
344 yaz_log(LOG_LOG, "Connection closed by client");
346 destroy_association(assoc);
349 case 0: /* all sent - release the request structure */
350 yaz_log(LOG_DEBUG, "Wrote PDU, %d bytes", req->len_response);
351 nmem_destroy(req->request_mem);
352 request_deq(&assoc->outgoing);
353 request_release(req);
354 if (!request_head(&assoc->outgoing))
355 { /* restore mask for cs_get operation ... */
356 iochan_clearflag(h, EVENT_OUTPUT|EVENT_INPUT);
357 iochan_setflag(h, assoc->cs_get_mask);
361 if (conn->io_pending & CS_WANT_WRITE)
362 assoc->cs_put_mask |= EVENT_OUTPUT;
363 if (conn->io_pending & CS_WANT_READ)
364 assoc->cs_put_mask |= EVENT_INPUT;
365 iochan_setflag(h, assoc->cs_put_mask);
368 if (event & EVENT_EXCEPT)
370 yaz_log(LOG_LOG, "ir_session (exception)");
372 destroy_association(assoc);
378 * Initiate request processing.
380 static int process_request(association *assoc, request *req, char **msg)
386 *msg = "Unknown Error";
387 assert(req && req->state == REQUEST_IDLE);
388 if (req->apdu_request->which != Z_APDU_initRequest && !assoc->init)
390 *msg = "Missing InitRequest";
393 switch (req->apdu_request->which)
395 case Z_APDU_initRequest:
396 res = process_initRequest(assoc, req); break;
397 case Z_APDU_searchRequest:
398 res = process_searchRequest(assoc, req, &fd); break;
399 case Z_APDU_presentRequest:
400 res = process_presentRequest(assoc, req, &fd); break;
401 case Z_APDU_scanRequest:
402 if (assoc->init->bend_scan)
403 res = process_scanRequest(assoc, req, &fd);
406 *msg = "Cannot handle Scan APDU";
410 case Z_APDU_extendedServicesRequest:
411 if (assoc->init->bend_esrequest)
412 res = process_ESRequest(assoc, req, &fd);
415 *msg = "Cannot handle Extended Services APDU";
419 case Z_APDU_sortRequest:
420 if (assoc->init->bend_sort)
421 res = process_sortRequest(assoc, req, &fd);
424 *msg = "Cannot handle Sort APDU";
429 process_close(assoc, req);
431 case Z_APDU_deleteResultSetRequest:
432 if (assoc->init->bend_delete)
433 res = process_deleteRequest(assoc, req, &fd);
436 *msg = "Cannot handle Delete APDU";
440 case Z_APDU_segmentRequest:
441 if (assoc->init->bend_segment)
443 res = process_segmentRequest (assoc, req);
447 *msg = "Cannot handle Segment APDU";
452 *msg = "Bad APDU received";
457 yaz_log(LOG_DEBUG, " result immediately available");
458 retval = process_response(assoc, req, res);
462 yaz_log(LOG_DEBUG, " result unavailble");
465 else /* no result yet - one will be provided later */
469 /* Set up an I/O handler for the fd supplied by the backend */
471 yaz_log(LOG_DEBUG, " establishing handler for result");
472 req->state = REQUEST_PENDING;
473 if (!(chan = iochan_create(fd, backend_response, EVENT_INPUT)))
475 iochan_setdata(chan, assoc);
482 * Handle message from the backend.
484 void backend_response(IOCHAN i, int event)
486 association *assoc = (association *)iochan_getdata(i);
487 request *req = request_head(&assoc->incoming);
491 yaz_log(LOG_DEBUG, "backend_response");
492 assert(assoc && req && req->state != REQUEST_IDLE);
493 /* determine what it is we're waiting for */
494 switch (req->apdu_request->which)
496 case Z_APDU_searchRequest:
497 res = response_searchRequest(assoc, req, 0, &fd); break;
499 case Z_APDU_presentRequest:
500 res = response_presentRequest(assoc, req, 0, &fd); break;
501 case Z_APDU_scanRequest:
502 res = response_scanRequest(assoc, req, 0, &fd); break;
505 yaz_log(LOG_WARN, "Serious programmer's lapse or bug");
508 if ((res && process_response(assoc, req, res) < 0) || fd < 0)
510 yaz_log(LOG_LOG, "Fatal error when talking to backend");
511 do_close(assoc, Z_Close_systemProblem, 0);
515 else if (!res) /* no result yet - try again later */
517 yaz_log(LOG_DEBUG, " no result yet");
518 iochan_setfd(i, fd); /* in case fd has changed */
523 * Encode response, and transfer the request structure to the outgoing queue.
525 static int process_response(association *assoc, request *req, Z_APDU *res)
527 odr_setbuf(assoc->encode, req->response, req->size_response, 1);
529 if (assoc->print && !z_APDU(assoc->print, &res, 0, 0))
531 yaz_log(LOG_WARN, "ODR print error: %s",
532 odr_errmsg(odr_geterror(assoc->print)));
533 odr_reset(assoc->print);
535 if (!z_APDU(assoc->encode, &res, 0, 0))
537 yaz_log(LOG_WARN, "ODR error when encoding response: %s",
538 odr_errmsg(odr_geterror(assoc->decode)));
541 req->response = odr_getbuf(assoc->encode, &req->len_response,
542 &req->size_response);
543 odr_setbuf(assoc->encode, 0, 0, 0); /* don'txfree if we abort later */
544 odr_reset(assoc->encode);
545 req->state = REQUEST_IDLE;
546 request_enq(&assoc->outgoing, req);
547 /* turn the work over to the ir_session handler */
548 iochan_setflag(assoc->client_chan, EVENT_OUTPUT);
549 assoc->cs_put_mask = EVENT_OUTPUT;
550 /* Is there more work to be done? give that to the input handler too */
552 if (request_head(&assoc->incoming))
554 yaz_log (LOG_DEBUG, "more work to be done");
555 iochan_setevent(assoc->client_chan, EVENT_WORK);
562 * Handle init request.
563 * At the moment, we don't check the options
564 * anywhere else in the code - we just try not to do anything that would
565 * break a naive client. We'll toss 'em into the association block when
566 * we need them there.
568 static Z_APDU *process_initRequest(association *assoc, request *reqb)
570 statserv_options_block *cb = statserv_getcontrol();
571 Z_InitRequest *req = reqb->apdu_request->u.initRequest;
572 Z_APDU *apdu = zget_APDU(assoc->encode, Z_APDU_initResponse);
573 Z_InitResponse *resp = apdu->u.initResponse;
574 bend_initresult *binitres;
578 assoc->init = (bend_initrequest *) xmalloc (sizeof(*assoc->init));
580 yaz_log(LOG_LOG, "Got initRequest");
581 if (req->implementationId)
582 yaz_log(LOG_LOG, "Id: %s", req->implementationId);
583 if (req->implementationName)
584 yaz_log(LOG_LOG, "Name: %s", req->implementationName);
585 if (req->implementationVersion)
586 yaz_log(LOG_LOG, "Version: %s", req->implementationVersion);
588 assoc->init->stream = assoc->encode;
589 assoc->init->print = assoc->print;
590 assoc->init->auth = req->idAuthentication;
591 assoc->init->referenceId = req->referenceId;
592 assoc->init->implementation_version = 0;
593 assoc->init->implementation_name = 0;
594 assoc->init->bend_sort = NULL;
595 assoc->init->bend_search = NULL;
596 assoc->init->bend_present = NULL;
597 assoc->init->bend_esrequest = NULL;
598 assoc->init->bend_delete = NULL;
599 assoc->init->bend_scan = NULL;
600 assoc->init->bend_segment = NULL;
601 assoc->init->bend_fetch = NULL;
603 assoc->init->peer_name =
604 odr_strdup (assoc->encode, cs_addrstr(assoc->client_link));
605 if (!(binitres = (*cb->bend_init)(assoc->init)))
607 yaz_log(LOG_WARN, "Bad response from backend.");
611 assoc->backend = binitres->handle;
612 if ((assoc->init->bend_sort))
613 yaz_log (LOG_DEBUG, "Sort handler installed");
614 if ((assoc->init->bend_search))
615 yaz_log (LOG_DEBUG, "Search handler installed");
616 if ((assoc->init->bend_present))
617 yaz_log (LOG_DEBUG, "Present handler installed");
618 if ((assoc->init->bend_esrequest))
619 yaz_log (LOG_DEBUG, "ESRequest handler installed");
620 if ((assoc->init->bend_delete))
621 yaz_log (LOG_DEBUG, "Delete handler installed");
622 if ((assoc->init->bend_scan))
623 yaz_log (LOG_DEBUG, "Scan handler installed");
624 if ((assoc->init->bend_segment))
625 yaz_log (LOG_DEBUG, "Segment handler installed");
627 resp->referenceId = req->referenceId;
629 /* let's tell the client what we can do */
630 if (ODR_MASK_GET(req->options, Z_Options_search))
632 ODR_MASK_SET(resp->options, Z_Options_search);
633 strcat(options, "srch");
635 if (ODR_MASK_GET(req->options, Z_Options_present))
637 ODR_MASK_SET(resp->options, Z_Options_present);
638 strcat(options, " prst");
640 if (ODR_MASK_GET(req->options, Z_Options_delSet) &&
641 assoc->init->bend_delete)
643 ODR_MASK_SET(resp->options, Z_Options_delSet);
644 strcat(options, " del");
646 if (ODR_MASK_GET(req->options, Z_Options_extendedServices) &&
647 assoc->init->bend_esrequest)
649 ODR_MASK_SET(resp->options, Z_Options_extendedServices);
650 strcat (options, " extendedServices");
652 if (ODR_MASK_GET(req->options, Z_Options_namedResultSets))
654 ODR_MASK_SET(resp->options, Z_Options_namedResultSets);
655 strcat(options, " namedresults");
657 if (ODR_MASK_GET(req->options, Z_Options_scan) && assoc->init->bend_scan)
659 ODR_MASK_SET(resp->options, Z_Options_scan);
660 strcat(options, " scan");
662 if (ODR_MASK_GET(req->options, Z_Options_concurrentOperations))
664 ODR_MASK_SET(resp->options, Z_Options_concurrentOperations);
665 strcat(options, " concurop");
667 if (ODR_MASK_GET(req->options, Z_Options_sort) && assoc->init->bend_sort)
669 ODR_MASK_SET(resp->options, Z_Options_sort);
670 strcat(options, " sort");
672 if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_1))
674 ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_1);
675 assoc->version = 2; /* 1 & 2 are equivalent */
677 if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_2))
679 ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_2);
682 if (ODR_MASK_GET(req->protocolVersion, Z_ProtocolVersion_3))
684 ODR_MASK_SET(resp->protocolVersion, Z_ProtocolVersion_3);
687 yaz_log(LOG_LOG, "Negotiated to v%d: %s", assoc->version, options);
688 assoc->maximumRecordSize = *req->maximumRecordSize;
689 if (assoc->maximumRecordSize > control_block->maxrecordsize)
690 assoc->maximumRecordSize = control_block->maxrecordsize;
691 assoc->preferredMessageSize = *req->preferredMessageSize;
692 if (assoc->preferredMessageSize > assoc->maximumRecordSize)
693 assoc->preferredMessageSize = assoc->maximumRecordSize;
696 assoc->maximumRecordSize = 3000000;
697 assoc->preferredMessageSize = 3000000;
700 resp->preferredMessageSize = &assoc->preferredMessageSize;
701 resp->maximumRecordSize = &assoc->maximumRecordSize;
703 resp->implementationName = "GFS/YAZ";
705 if (assoc->init->implementation_name)
708 odr_malloc (assoc->encode,
709 strlen(assoc->init->implementation_name) + 10 +
710 strlen(resp->implementationName));
711 sprintf (nv, "%s / %s",
712 resp->implementationName, assoc->init->implementation_name);
713 resp->implementationName = nv;
715 if (assoc->init->implementation_version)
718 odr_malloc (assoc->encode,
719 strlen(assoc->init->implementation_version) + 10 +
720 strlen(resp->implementationVersion));
721 sprintf (nv, "YAZ %s / %s",
722 resp->implementationVersion,
723 assoc->init->implementation_version);
724 resp->implementationVersion = nv;
727 if (binitres->errcode)
729 yaz_log(LOG_LOG, "Connection rejected by backend.");
731 assoc->state = ASSOC_DEAD;
734 assoc->state = ASSOC_UP;
739 * These functions should be merged.
742 static void set_addinfo (Z_DefaultDiagFormat *dr, char *addinfo, ODR odr)
744 dr->which = Z_DefaultDiagFormat_v2Addinfo;
745 dr->u.v2Addinfo = odr_strdup (odr, addinfo ? addinfo : "");
749 * nonsurrogate diagnostic record.
751 static Z_Records *diagrec(association *assoc, int error, char *addinfo)
753 Z_Records *rec = (Z_Records *)
754 odr_malloc (assoc->encode, sizeof(*rec));
755 int *err = odr_intdup(assoc->encode, error);
756 Z_DiagRec *drec = (Z_DiagRec *)
757 odr_malloc (assoc->encode, sizeof(*drec));
758 Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
759 odr_malloc (assoc->encode, sizeof(*dr));
761 yaz_log(LOG_DEBUG, "Diagnostic: %d -- %s", error, addinfo ? addinfo :
763 rec->which = Z_Records_NSD;
764 rec->u.nonSurrogateDiagnostic = dr;
765 dr->diagnosticSetId =
766 yaz_oidval_to_z3950oid (assoc->encode, CLASS_DIAGSET, VAL_BIB1);
768 set_addinfo (dr, addinfo, assoc->encode);
773 * surrogate diagnostic.
775 static Z_NamePlusRecord *surrogatediagrec(association *assoc, char *dbname,
776 int error, char *addinfo)
778 Z_NamePlusRecord *rec = (Z_NamePlusRecord *)
779 odr_malloc (assoc->encode, sizeof(*rec));
780 int *err = odr_intdup(assoc->encode, error);
781 Z_DiagRec *drec = (Z_DiagRec *)odr_malloc (assoc->encode, sizeof(*drec));
782 Z_DefaultDiagFormat *dr = (Z_DefaultDiagFormat *)
783 odr_malloc (assoc->encode, sizeof(*dr));
785 yaz_log(LOG_DEBUG, "SurrogateDiagnotic: %d -- %s", error, addinfo);
786 rec->databaseName = dbname;
787 rec->which = Z_NamePlusRecord_surrogateDiagnostic;
788 rec->u.surrogateDiagnostic = drec;
789 drec->which = Z_DiagRec_defaultFormat;
790 drec->u.defaultFormat = dr;
791 dr->diagnosticSetId =
792 yaz_oidval_to_z3950oid (assoc->encode, CLASS_DIAGSET, VAL_BIB1);
794 set_addinfo (dr, addinfo, assoc->encode);
800 * multiple nonsurrogate diagnostics.
802 static Z_DiagRecs *diagrecs(association *assoc, int error, char *addinfo)
804 Z_DiagRecs *recs = (Z_DiagRecs *)odr_malloc (assoc->encode, sizeof(*recs));
805 int *err = odr_intdup(assoc->encode, error);
806 Z_DiagRec **recp = (Z_DiagRec **)odr_malloc (assoc->encode, sizeof(*recp));
807 Z_DiagRec *drec = (Z_DiagRec *)odr_malloc (assoc->encode, sizeof(*drec));
808 Z_DefaultDiagFormat *rec = (Z_DefaultDiagFormat *)
809 odr_malloc (assoc->encode, sizeof(*rec));
811 yaz_log(LOG_DEBUG, "DiagRecs: %d -- %s", error, addinfo ? addinfo : "");
813 recs->num_diagRecs = 1;
814 recs->diagRecs = recp;
816 drec->which = Z_DiagRec_defaultFormat;
817 drec->u.defaultFormat = rec;
819 rec->diagnosticSetId =
820 yaz_oidval_to_z3950oid (assoc->encode, CLASS_DIAGSET, VAL_BIB1);
821 rec->condition = err;
823 rec->which = Z_DefaultDiagFormat_v2Addinfo;
824 rec->u.v2Addinfo = odr_strdup (assoc->encode, addinfo ? addinfo : "");
828 static Z_Records *pack_records(association *a, char *setname, int start,
829 int *num, Z_RecordComposition *comp,
830 int *next, int *pres, oid_value format,
831 Z_ReferenceId *referenceId,
834 int recno, total_length = 0, toget = *num, dumped_records = 0;
836 (Z_Records *) odr_malloc (a->encode, sizeof(*records));
837 Z_NamePlusRecordList *reclist =
838 (Z_NamePlusRecordList *) odr_malloc (a->encode, sizeof(*reclist));
839 Z_NamePlusRecord **list =
840 (Z_NamePlusRecord **) odr_malloc (a->encode, sizeof(*list) * toget);
842 records->which = Z_Records_DBOSD;
843 records->u.databaseOrSurDiagnostics = reclist;
844 reclist->num_records = 0;
845 reclist->records = list;
846 *pres = Z_PRES_SUCCESS;
850 yaz_log(LOG_LOG, "Request to pack %d+%d+%s", start, toget, setname);
851 yaz_log(LOG_DEBUG, "pms=%d, mrs=%d", a->preferredMessageSize,
852 a->maximumRecordSize);
853 for (recno = start; reclist->num_records < toget; recno++)
856 Z_NamePlusRecord *thisrec;
859 * we get the number of bytes allocated on the stream before any
860 * allocation done by the backend - this should give us a reasonable
861 * idea of the total size of the data so far.
863 total_length = odr_total(a->encode) - dumped_records;
869 freq.last_in_set = 0;
870 freq.setname = setname;
871 freq.surrogate_flag = 0;
874 freq.request_format = format;
875 freq.request_format_raw = oid;
876 freq.output_format = format;
877 freq.output_format_raw = 0;
878 freq.stream = a->encode;
879 freq.print = a->print;
880 freq.surrogate_flag = 0;
881 freq.referenceId = referenceId;
882 (*a->init->bend_fetch)(a->backend, &freq);
883 /* backend should be able to signal whether error is system-wide
884 or only pertaining to current record */
887 if (!freq.surrogate_flag)
889 *pres = Z_PRES_FAILURE;
890 return diagrec(a, freq.errcode, freq.errstring);
892 reclist->records[reclist->num_records] =
893 surrogatediagrec(a, freq.basename, freq.errcode,
895 reclist->num_records++;
896 *next = freq.last_in_set ? 0 : recno + 1;
900 this_length = freq.len;
902 this_length = odr_total(a->encode) - total_length;
903 yaz_log(LOG_DEBUG, " fetched record, len=%d, total=%d",
904 this_length, total_length);
905 if (this_length + total_length > a->preferredMessageSize)
907 /* record is small enough, really */
908 if (this_length <= a->preferredMessageSize)
910 yaz_log(LOG_DEBUG, " Dropped last normal-sized record");
911 *pres = Z_PRES_PARTIAL_2;
914 /* record can only be fetched by itself */
915 if (this_length < a->maximumRecordSize)
917 yaz_log(LOG_DEBUG, " Record > prefmsgsz");
920 yaz_log(LOG_DEBUG, " Dropped it");
921 reclist->records[reclist->num_records] =
922 surrogatediagrec(a, freq.basename, 16, 0);
923 reclist->num_records++;
924 *next = freq.last_in_set ? 0 : recno + 1;
925 dumped_records += this_length;
929 else /* too big entirely */
931 yaz_log(LOG_DEBUG, "Record > maxrcdsz");
932 reclist->records[reclist->num_records] =
933 surrogatediagrec(a, freq.basename, 17, 0);
934 reclist->num_records++;
935 *next = freq.last_in_set ? 0 : recno + 1;
936 dumped_records += this_length;
941 if (!(thisrec = (Z_NamePlusRecord *)
942 odr_malloc(a->encode, sizeof(*thisrec))))
944 if (!(thisrec->databaseName = (char *)odr_malloc(a->encode,
945 strlen(freq.basename) + 1)))
947 strcpy(thisrec->databaseName, freq.basename);
948 thisrec->which = Z_NamePlusRecord_databaseRecord;
950 if (freq.output_format_raw)
952 struct oident *ident = oid_getentbyoid(freq.output_format_raw);
953 freq.output_format = ident->value;
955 thisrec->u.databaseRecord = z_ext_record(a->encode, freq.output_format,
956 freq.record, freq.len);
957 if (!thisrec->u.databaseRecord)
959 reclist->records[reclist->num_records] = thisrec;
960 reclist->num_records++;
961 *next = freq.last_in_set ? 0 : recno + 1;
963 *num = reclist->num_records;
967 static Z_APDU *process_searchRequest(association *assoc, request *reqb,
970 Z_SearchRequest *req = reqb->apdu_request->u.searchRequest;
971 bend_search_rr *bsrr =
972 (bend_search_rr *)nmem_malloc (reqb->request_mem, sizeof(*bsrr));
974 yaz_log(LOG_LOG, "Got SearchRequest.");
976 bsrr->request = reqb;
977 bsrr->association = assoc;
978 bsrr->referenceId = req->referenceId;
979 save_referenceId (reqb, bsrr->referenceId);
981 yaz_log (LOG_LOG, "ResultSet '%s'", req->resultSetName);
982 if (req->databaseNames)
985 for (i = 0; i < req->num_databaseNames; i++)
986 yaz_log (LOG_LOG, "Database '%s'", req->databaseNames[i]);
988 switch (req->query->which)
990 case Z_Query_type_1: case Z_Query_type_101:
991 log_rpn_query (req->query->u.type_1);
993 if (assoc->init->bend_search)
995 bsrr->setname = req->resultSetName;
996 bsrr->replace_set = *req->replaceIndicator;
997 bsrr->num_bases = req->num_databaseNames;
998 bsrr->basenames = req->databaseNames;
999 bsrr->query = req->query;
1000 bsrr->stream = assoc->encode;
1001 bsrr->decode = assoc->decode;
1002 bsrr->print = assoc->print;
1005 bsrr->errstring = NULL;
1006 (assoc->init->bend_search)(assoc->backend, bsrr);
1010 return response_searchRequest(assoc, reqb, bsrr, fd);
1013 int bend_searchresponse(void *handle, bend_search_rr *bsrr) {return 0;}
1016 * Prepare a searchresponse based on the backend results. We probably want
1017 * to look at making the fetching of records nonblocking as well, but
1018 * so far, we'll keep things simple.
1019 * If bsrt is null, that means we're called in response to a communications
1020 * event, and we'll have to get the response for ourselves.
1022 static Z_APDU *response_searchRequest(association *assoc, request *reqb,
1023 bend_search_rr *bsrt, int *fd)
1025 Z_SearchRequest *req = reqb->apdu_request->u.searchRequest;
1026 Z_APDU *apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1027 Z_SearchResponse *resp = (Z_SearchResponse *)
1028 odr_malloc (assoc->encode, sizeof(*resp));
1029 int *nulint = odr_intdup (assoc->encode, 0);
1030 bool_t *sr = odr_intdup(assoc->encode, 1);
1031 int *next = odr_intdup(assoc->encode, 0);
1032 int *none = odr_intdup(assoc->encode, Z_RES_NONE);
1034 apdu->which = Z_APDU_searchResponse;
1035 apdu->u.searchResponse = resp;
1036 resp->referenceId = req->referenceId;
1037 resp->additionalSearchInfo = 0;
1038 resp->otherInfo = 0;
1040 if (!bsrt && !bend_searchresponse(assoc->backend, bsrt))
1042 yaz_log(LOG_FATAL, "Bad result from backend");
1045 else if (bsrt->errcode)
1047 resp->records = diagrec(assoc, bsrt->errcode, bsrt->errstring);
1048 resp->resultCount = nulint;
1049 resp->numberOfRecordsReturned = nulint;
1050 resp->nextResultSetPosition = nulint;
1051 resp->searchStatus = nulint;
1052 resp->resultSetStatus = none;
1053 resp->presentStatus = 0;
1057 int *toget = odr_intdup(assoc->encode, 0);
1058 int *presst = odr_intdup(assoc->encode, 0);
1059 Z_RecordComposition comp, *compp = 0;
1061 yaz_log (LOG_LOG, "resultCount: %d", bsrt->hits);
1064 resp->resultCount = &bsrt->hits;
1066 comp.which = Z_RecordComp_simple;
1067 /* how many records does the user agent want, then? */
1068 if (bsrt->hits <= *req->smallSetUpperBound)
1070 *toget = bsrt->hits;
1071 if ((comp.u.simple = req->smallSetElementSetNames))
1074 else if (bsrt->hits < *req->largeSetLowerBound)
1076 *toget = *req->mediumSetPresentNumber;
1077 if (*toget > bsrt->hits)
1078 *toget = bsrt->hits;
1079 if ((comp.u.simple = req->mediumSetElementSetNames))
1085 if (*toget && !resp->records)
1090 if (!(prefformat = oid_getentbyoid(req->preferredRecordSyntax)))
1093 form = prefformat->value;
1094 resp->records = pack_records(assoc, req->resultSetName, 1,
1095 toget, compp, next, presst, form, req->referenceId,
1096 req->preferredRecordSyntax);
1099 resp->numberOfRecordsReturned = toget;
1100 resp->nextResultSetPosition = next;
1101 resp->searchStatus = sr;
1102 resp->resultSetStatus = 0;
1103 resp->presentStatus = presst;
1107 if (*resp->resultCount)
1109 resp->numberOfRecordsReturned = nulint;
1110 resp->nextResultSetPosition = next;
1111 resp->searchStatus = sr;
1112 resp->resultSetStatus = 0;
1113 resp->presentStatus = 0;
1120 * Maybe we got a little over-friendly when we designed bend_fetch to
1121 * get only one record at a time. Some backends can optimise multiple-record
1122 * fetches, and at any rate, there is some overhead involved in
1123 * all that selecting and hopping around. Problem is, of course, that the
1124 * frontend can't know ahead of time how many records it'll need to
1125 * fill the negotiated PDU size. Annoying. Segmentation or not, Z/SR
1126 * is downright lousy as a bulk data transfer protocol.
1128 * To start with, we'll do the fetching of records from the backend
1129 * in one operation: To save some trips in and out of the event-handler,
1130 * and to simplify the interface to pack_records. At any rate, asynch
1131 * operation is more fun in operations that have an unpredictable execution
1132 * speed - which is normally more true for search than for present.
1134 static Z_APDU *process_presentRequest(association *assoc, request *reqb,
1137 Z_PresentRequest *req = reqb->apdu_request->u.presentRequest;
1141 Z_PresentResponse *resp;
1145 yaz_log(LOG_LOG, "Got PresentRequest.");
1147 if (!(prefformat = oid_getentbyoid(req->preferredRecordSyntax)))
1150 form = prefformat->value;
1151 resp = (Z_PresentResponse *)odr_malloc (assoc->encode, sizeof(*resp));
1153 resp->presentStatus = odr_intdup(assoc->encode, 0);
1154 if (assoc->init->bend_present)
1156 bend_present_rr *bprr = (bend_present_rr *)
1157 nmem_malloc (reqb->request_mem, sizeof(*bprr));
1158 bprr->setname = req->resultSetId;
1159 bprr->start = *req->resultSetStartPoint;
1160 bprr->number = *req->numberOfRecordsRequested;
1161 bprr->format = form;
1162 bprr->comp = req->recordComposition;
1163 bprr->referenceId = req->referenceId;
1164 bprr->stream = assoc->encode;
1165 bprr->print = assoc->print;
1166 bprr->request = reqb;
1167 bprr->association = assoc;
1169 bprr->errstring = NULL;
1170 (*assoc->init->bend_present)(assoc->backend, bprr);
1176 resp->records = diagrec(assoc, bprr->errcode, bprr->errstring);
1177 *resp->presentStatus = Z_PRES_FAILURE;
1180 apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1181 next = odr_intdup(assoc->encode, 0);
1182 num = odr_intdup(assoc->encode, 0);
1184 apdu->which = Z_APDU_presentResponse;
1185 apdu->u.presentResponse = resp;
1186 resp->referenceId = req->referenceId;
1187 resp->otherInfo = 0;
1191 *num = *req->numberOfRecordsRequested;
1193 pack_records(assoc, req->resultSetId, *req->resultSetStartPoint,
1194 num, req->recordComposition, next, resp->presentStatus,
1195 form, req->referenceId, req->preferredRecordSyntax);
1199 resp->numberOfRecordsReturned = num;
1200 resp->nextResultSetPosition = next;
1206 * Scan was implemented rather in a hurry, and with support for only the basic
1207 * elements of the service in the backend API. Suggestions are welcome.
1209 static Z_APDU *process_scanRequest(association *assoc, request *reqb, int *fd)
1211 Z_ScanRequest *req = reqb->apdu_request->u.scanRequest;
1212 Z_APDU *apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1213 Z_ScanResponse *res = (Z_ScanResponse *)
1214 odr_malloc (assoc->encode, sizeof(*res));
1215 int *scanStatus = odr_intdup(assoc->encode, Z_Scan_failure);
1216 int *numberOfEntriesReturned = odr_intdup(assoc->encode, 0);
1217 Z_ListEntries *ents = (Z_ListEntries *)
1218 odr_malloc (assoc->encode, sizeof(*ents));
1219 Z_DiagRecs *diagrecs_p = NULL;
1223 yaz_log(LOG_LOG, "Got ScanRequest");
1225 apdu->which = Z_APDU_scanResponse;
1226 apdu->u.scanResponse = res;
1227 res->referenceId = req->referenceId;
1228 res->stepSize = odr_intdup(assoc->encode, 0);
1231 *res->stepSize = *req->stepSize;
1232 res->scanStatus = scanStatus;
1233 res->numberOfEntriesReturned = numberOfEntriesReturned;
1234 res->positionOfTerm = 0;
1235 res->entries = ents;
1236 ents->num_entries = 0;
1237 ents->entries = NULL;
1238 ents->num_nonsurrogateDiagnostics = 0;
1239 ents->nonsurrogateDiagnostics = NULL;
1240 res->attributeSet = 0;
1243 if (req->attributeSet && (!(attent = oid_getentbyoid(req->attributeSet)) ||
1244 attent->oclass != CLASS_ATTSET
1245 || attent->value != VAL_BIB1))
1246 diagrecs_p = diagrecs(assoc, 121, 0);
1247 else if (req->stepSize && *req->stepSize > 0)
1248 diagrecs_p = diagrecs(assoc, 205, 0);
1251 bend_scan_rr *bsrr = (bend_scan_rr *)
1252 odr_malloc (assoc->encode, sizeof(*bsrr));
1253 if (req->databaseNames)
1256 for (i = 0; i < req->num_databaseNames; i++)
1257 yaz_log (LOG_LOG, "Database '%s'", req->databaseNames[i]);
1259 bsrr->num_bases = req->num_databaseNames;
1260 bsrr->basenames = req->databaseNames;
1261 bsrr->num_entries = *req->numberOfTermsRequested;
1262 bsrr->term = req->termListAndStartPoint;
1263 bsrr->referenceId = req->referenceId;
1264 bsrr->stream = assoc->encode;
1265 bsrr->print = assoc->print;
1266 bsrr->step_size = res->stepSize;
1267 if (!(attset = oid_getentbyoid(req->attributeSet)) ||
1268 attset->oclass != CLASS_RECSYN)
1269 bsrr->attributeset = VAL_NONE;
1271 bsrr->attributeset = attset->value;
1272 log_scan_term (req->termListAndStartPoint, bsrr->attributeset);
1273 bsrr->term_position = req->preferredPositionInResponse ?
1274 *req->preferredPositionInResponse : 1;
1275 ((int (*)(void *, bend_scan_rr *))
1276 (*assoc->init->bend_scan))(assoc->backend, bsrr);
1278 diagrecs_p = diagrecs(assoc, bsrr->errcode, bsrr->errstring);
1282 Z_Entry **tab = (Z_Entry **)
1283 odr_malloc (assoc->encode, sizeof(*tab) * bsrr->num_entries);
1285 if (bsrr->status == BEND_SCAN_PARTIAL)
1286 *scanStatus = Z_Scan_partial_5;
1288 *scanStatus = Z_Scan_success;
1289 ents->entries = tab;
1290 ents->num_entries = bsrr->num_entries;
1291 res->numberOfEntriesReturned = &ents->num_entries;
1292 res->positionOfTerm = &bsrr->term_position;
1293 for (i = 0; i < bsrr->num_entries; i++)
1299 tab[i] = e = (Z_Entry *)odr_malloc(assoc->encode, sizeof(*e));
1300 if (bsrr->entries[i].occurrences >= 0)
1302 e->which = Z_Entry_termInfo;
1303 e->u.termInfo = t = (Z_TermInfo *)
1304 odr_malloc(assoc->encode, sizeof(*t));
1305 t->suggestedAttributes = 0;
1307 t->alternativeTerm = 0;
1308 t->byAttributes = 0;
1309 t->otherTermInfo = 0;
1310 t->globalOccurrences = &bsrr->entries[i].occurrences;
1311 t->term = (Z_Term *)
1312 odr_malloc(assoc->encode, sizeof(*t->term));
1313 t->term->which = Z_Term_general;
1314 t->term->u.general = o =
1315 (Odr_oct *)odr_malloc(assoc->encode, sizeof(Odr_oct));
1316 o->buf = (unsigned char *)
1317 odr_malloc(assoc->encode, o->len = o->size =
1318 strlen(bsrr->entries[i].term));
1319 memcpy(o->buf, bsrr->entries[i].term, o->len);
1320 yaz_log(LOG_DEBUG, " term #%d: '%s' (%d)", i,
1321 bsrr->entries[i].term, bsrr->entries[i].occurrences);
1325 Z_DiagRecs *drecs = diagrecs (assoc,
1326 bsrr->entries[i].errcode,
1327 bsrr->entries[i].errstring);
1328 assert (drecs->num_diagRecs == 1);
1329 e->which = Z_Entry_surrogateDiagnostic;
1330 assert (drecs->diagRecs[0]);
1331 e->u.surrogateDiagnostic = drecs->diagRecs[0];
1338 ents->num_nonsurrogateDiagnostics = diagrecs_p->num_diagRecs;
1339 ents->nonsurrogateDiagnostics = diagrecs_p->diagRecs;
1344 static Z_APDU *process_sortRequest(association *assoc, request *reqb,
1347 Z_SortRequest *req = reqb->apdu_request->u.sortRequest;
1348 Z_SortResponse *res = (Z_SortResponse *)
1349 odr_malloc (assoc->encode, sizeof(*res));
1350 bend_sort_rr *bsrr = (bend_sort_rr *)
1351 odr_malloc (assoc->encode, sizeof(*bsrr));
1353 Z_APDU *apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1355 yaz_log(LOG_LOG, "Got SortRequest.");
1357 bsrr->num_input_setnames = req->num_inputResultSetNames;
1358 bsrr->input_setnames = req->inputResultSetNames;
1359 bsrr->referenceId = req->referenceId;
1360 bsrr->output_setname = req->sortedResultSetName;
1361 bsrr->sort_sequence = req->sortSequence;
1362 bsrr->stream = assoc->encode;
1363 bsrr->print = assoc->print;
1365 bsrr->sort_status = Z_SortStatus_failure;
1367 bsrr->errstring = 0;
1369 (*assoc->init->bend_sort)(assoc->backend, bsrr);
1371 res->referenceId = bsrr->referenceId;
1372 res->sortStatus = odr_intdup(assoc->encode, bsrr->sort_status);
1373 res->resultSetStatus = 0;
1376 Z_DiagRecs *dr = diagrecs (assoc, bsrr->errcode, bsrr->errstring);
1377 res->diagnostics = dr->diagRecs;
1378 res->num_diagnostics = dr->num_diagRecs;
1382 res->num_diagnostics = 0;
1383 res->diagnostics = 0;
1387 apdu->which = Z_APDU_sortResponse;
1388 apdu->u.sortResponse = res;
1392 static Z_APDU *process_deleteRequest(association *assoc, request *reqb,
1395 Z_DeleteResultSetRequest *req =
1396 reqb->apdu_request->u.deleteResultSetRequest;
1397 Z_DeleteResultSetResponse *res = (Z_DeleteResultSetResponse *)
1398 odr_malloc (assoc->encode, sizeof(*res));
1399 bend_delete_rr *bdrr = (bend_delete_rr *)
1400 odr_malloc (assoc->encode, sizeof(*bdrr));
1401 Z_APDU *apdu = (Z_APDU *)odr_malloc (assoc->encode, sizeof(*apdu));
1403 yaz_log(LOG_LOG, "Got DeleteRequest.");
1405 bdrr->num_setnames = req->num_resultSetList;
1406 bdrr->setnames = req->resultSetList;
1407 bdrr->stream = assoc->encode;
1408 bdrr->print = assoc->print;
1409 bdrr->function = *req->deleteFunction;
1410 bdrr->referenceId = req->referenceId;
1412 if (bdrr->num_setnames > 0)
1415 bdrr->statuses = (int*)
1416 odr_malloc(assoc->encode, sizeof(*bdrr->statuses) *
1417 bdrr->num_setnames);
1418 for (i = 0; i < bdrr->num_setnames; i++)
1419 bdrr->statuses[i] = 0;
1421 (*assoc->init->bend_delete)(assoc->backend, bdrr);
1423 res->referenceId = req->referenceId;
1425 res->deleteOperationStatus = odr_intdup(assoc->encode,bdrr->delete_status);
1427 res->deleteListStatuses = 0;
1428 if (bdrr->num_setnames > 0)
1431 res->deleteListStatuses = (Z_ListStatuses *)
1432 odr_malloc(assoc->encode, sizeof(*res->deleteListStatuses));
1433 res->deleteListStatuses->num = bdrr->num_setnames;
1434 res->deleteListStatuses->elements =
1436 odr_malloc (assoc->encode,
1437 sizeof(*res->deleteListStatuses->elements) *
1438 bdrr->num_setnames);
1439 for (i = 0; i<bdrr->num_setnames; i++)
1441 res->deleteListStatuses->elements[i] =
1443 odr_malloc (assoc->encode,
1444 sizeof(**res->deleteListStatuses->elements));
1445 res->deleteListStatuses->elements[i]->status = bdrr->statuses+i;
1446 res->deleteListStatuses->elements[i]->id =
1447 odr_strdup (assoc->encode, bdrr->setnames[i]);
1451 res->numberNotDeleted = 0;
1452 res->bulkStatuses = 0;
1453 res->deleteMessage = 0;
1456 apdu->which = Z_APDU_deleteResultSetResponse;
1457 apdu->u.deleteResultSetResponse = res;
1461 static void process_close(association *assoc, request *reqb)
1463 Z_Close *req = reqb->apdu_request->u.close;
1464 static char *reasons[] =
1471 "securityViolation",
1478 yaz_log(LOG_LOG, "Got Close, reason %s, message %s",
1479 reasons[*req->closeReason], req->diagnosticInformation ?
1480 req->diagnosticInformation : "NULL");
1481 if (assoc->version < 3) /* to make do_force respond with close */
1483 do_close_req(assoc, Z_Close_finished,
1484 "Association terminated by client", reqb);
1487 void save_referenceId (request *reqb, Z_ReferenceId *refid)
1491 reqb->len_refid = refid->len;
1492 reqb->refid = (char *)nmem_malloc (reqb->request_mem, refid->len);
1493 memcpy (reqb->refid, refid->buf, refid->len);
1497 reqb->len_refid = 0;
1502 void bend_request_send (bend_association a, bend_request req, Z_APDU *res)
1504 process_response (a, req, res);
1507 bend_request bend_request_mk (bend_association a)
1509 request *nreq = request_get (&a->outgoing);
1510 nreq->request_mem = nmem_create ();
1514 Z_ReferenceId *bend_request_getid (ODR odr, bend_request req)
1519 id = (Odr_oct *)odr_malloc (odr, sizeof(*odr));
1520 id->buf = (unsigned char *)odr_malloc (odr, req->len_refid);
1521 id->len = id->size = req->len_refid;
1522 memcpy (id->buf, req->refid, req->len_refid);
1526 void bend_request_destroy (bend_request *req)
1528 nmem_destroy((*req)->request_mem);
1529 request_release(*req);
1533 int bend_backend_respond (bend_association a, bend_request req)
1537 r = process_request (a, req, &msg);
1539 logf (LOG_WARN, "%s", msg);
1543 void bend_request_setdata(bend_request r, void *p)
1548 void *bend_request_getdata(bend_request r)
1550 return r->clientData;
1553 static Z_APDU *process_segmentRequest (association *assoc, request *reqb)
1555 bend_segment_rr request;
1557 request.segment = reqb->apdu_request->u.segmentRequest;
1558 request.stream = assoc->encode;
1559 request.decode = assoc->decode;
1560 request.print = assoc->print;
1561 request.association = assoc;
1563 (*assoc->init->bend_segment)(assoc->backend, &request);
1568 static Z_APDU *process_ESRequest(association *assoc, request *reqb, int *fd)
1570 bend_esrequest_rr esrequest;
1572 Z_ExtendedServicesRequest *req = reqb->apdu_request->u.extendedServicesRequest;
1573 Z_APDU *apdu = zget_APDU(assoc->encode, Z_APDU_extendedServicesResponse);
1575 Z_ExtendedServicesResponse *resp = apdu->u.extendedServicesResponse;
1577 yaz_log(LOG_DEBUG,"inside Process esRequest");
1579 esrequest.esr = reqb->apdu_request->u.extendedServicesRequest;
1580 esrequest.stream = assoc->encode;
1581 esrequest.decode = assoc->decode;
1582 esrequest.print = assoc->print;
1583 esrequest.errcode = 0;
1584 esrequest.errstring = NULL;
1585 esrequest.request = reqb;
1586 esrequest.association = assoc;
1587 esrequest.taskPackage = 0;
1588 esrequest.referenceId = req->referenceId;
1590 (*assoc->init->bend_esrequest)(assoc->backend, &esrequest);
1592 /* If the response is being delayed, return NULL */
1593 if (esrequest.request == NULL)
1596 resp->referenceId = req->referenceId;
1598 if (esrequest.errcode == -1)
1600 /* Backend service indicates request will be processed */
1601 yaz_log(LOG_DEBUG,"Request could be processed...Accepted !");
1602 *resp->operationStatus = Z_ExtendedServicesResponse_accepted;
1604 else if (esrequest.errcode == 0)
1606 /* Backend service indicates request will be processed */
1607 yaz_log(LOG_DEBUG,"Request could be processed...Done !");
1608 *resp->operationStatus = Z_ExtendedServicesResponse_done;
1612 Z_DiagRecs *diagRecs = diagrecs (assoc, esrequest.errcode,
1613 esrequest.errstring);
1615 /* Backend indicates error, request will not be processed */
1616 yaz_log(LOG_DEBUG,"Request could not be processed...failure !");
1617 *resp->operationStatus = Z_ExtendedServicesResponse_failure;
1618 resp->num_diagnostics = diagRecs->num_diagRecs;
1619 resp->diagnostics = diagRecs->diagRecs;
1621 /* Do something with the members of bend_extendedservice */
1622 if (esrequest.taskPackage)
1623 resp->taskPackage = z_ext_record (assoc->encode, VAL_EXTENDED,
1624 (const char *) esrequest.taskPackage,
1626 yaz_log(LOG_DEBUG,"Send the result apdu");