2 * $Id: zoom-c.c,v 1.2 2001-10-24 12:24:43 adam Exp $
4 * ZOOM layer for C, connections, result sets, queries.
7 #include <yaz/xmalloc.h>
8 #include <yaz/otherinfo.h>
10 #include <yaz/pquery.h>
11 #include <yaz/diagbib1.h>
15 static Z3950_record record_cache_lookup (Z3950_resultset r,
17 const char *elementSetName);
19 static void clear_error (Z3950_connection c)
21 c->error = Z3950_ERROR_NONE;
26 Z3950_connection Z3950_connection_create (Z3950_options options)
28 Z3950_connection c = xmalloc (sizeof(*c));
33 c->state = STATE_IDLE;
34 c->error = Z3950_ERROR_NONE;
42 c->options = Z3950_options_create_with_parent(options);
51 c->odr_in = odr_createmem (ODR_DECODE);
52 c->odr_out = odr_createmem (ODR_ENCODE);
58 /* set database names. Take local databases (if set); otherwise
59 take databases given in ZURL (if set); otherwise use Default */
60 static char **set_DatabaseNames (Z3950_connection con, int *num)
65 const char *cp = Z3950_options_get (con->options, "databaseName");
69 cp = strchr (con->host_port, '/');
76 while ((c = strchr(c, '+')))
84 databaseNames = odr_malloc (con->odr_out, no * sizeof(*databaseNames));
96 /* cp ptr to first char of db name, c is char
98 databaseNames[no] = odr_malloc (con->odr_out, 1+c-cp);
99 memcpy (databaseNames[no], cp, c-cp);
100 databaseNames[no++][c-cp] = '\0';
105 databaseNames[no] = NULL;
107 return databaseNames;
110 Z3950_connection Z3950_connection_new (const char *host, int portnum)
112 Z3950_connection c = Z3950_connection_create (0);
114 Z3950_connection_connect (c, host, portnum);
118 void Z3950_connection_connect(Z3950_connection c,
119 const char *host, int portnum)
123 val = Z3950_options_get (c->options, "proxy");
125 c->proxy = xstrdup (val);
132 sprintf (hostn, "%.80s:%d", host, portnum);
133 c->host_port = xstrdup(hostn);
136 c->host_port = xstrdup(host);
138 c->async = Z3950_options_get_bool (c->options, "async", 0);
142 while (Z3950_event (1, &c))
147 Z3950_search Z3950_search_create(void)
149 Z3950_search s = xmalloc (sizeof(*s));
154 s->odr = odr_createmem (ODR_ENCODE);
159 const char *Z3950_connection_host (Z3950_connection c)
164 void Z3950_search_destroy(Z3950_search s)
170 yaz_log (LOG_DEBUG, "Z3950_search_destroy count=%d", s->refcount);
171 if (s->refcount == 0)
173 odr_destroy (s->odr);
178 int Z3950_search_prefix(Z3950_search s, const char *str)
180 s->query = odr_malloc (s->odr, sizeof(*s->query));
181 s->query->which = Z_Query_type_1;
182 s->query->u.type_1 = p_query_rpn(s->odr, PROTO_Z3950, str);
183 if (!s->query->u.type_1)
188 int Z3950_search_sortby(Z3950_search s, const char *criteria)
190 s->sort_spec = yaz_sort_spec (s->odr, criteria);
196 static int do_write(Z3950_connection c);
199 Z3950_task Z3950_connection_add_task (Z3950_connection c, int which)
201 Z3950_task *taskp = &c->tasks;
203 taskp = &(*taskp)->next;
204 *taskp = xmalloc (sizeof(**taskp));
205 (*taskp)->running = 0;
206 (*taskp)->which = which;
207 (*taskp)->u.resultset = 0; /* one null pointer there at least */
213 void Z3950_connection_remove_task (Z3950_connection c)
215 Z3950_task task = c->tasks;
219 c->tasks = task->next;
222 case Z3950_TASK_SEARCH:
223 Z3950_resultset_destroy (task->u.resultset);
225 case Z3950_TASK_RETRIEVE:
226 Z3950_resultset_destroy (task->u.resultset);
235 void Z3950_connection_remove_tasks (Z3950_connection c)
238 Z3950_connection_remove_task(c);
241 void Z3950_connection_destroy(Z3950_connection c)
248 for (r = c->resultsets; r; r = r->next)
253 odr_destroy (c->odr_in);
254 odr_destroy (c->odr_out);
255 Z3950_options_destroy (c->options);
256 Z3950_connection_remove_tasks (c);
257 xfree (c->host_port);
261 void Z3950_resultset_addref (Z3950_resultset r)
266 Z3950_resultset Z3950_resultset_create ()
268 Z3950_resultset r = xmalloc (sizeof(*r));
272 r->odr = odr_createmem (ODR_ENCODE);
285 Z3950_resultset Z3950_connection_search_pqf(Z3950_connection c, const char *q)
288 Z3950_search s = Z3950_search_create();
290 Z3950_search_prefix (s, q);
292 r = Z3950_connection_search (c, s);
293 Z3950_search_destroy (s);
297 Z3950_resultset Z3950_connection_search(Z3950_connection c, Z3950_search q)
299 Z3950_resultset r = Z3950_resultset_create ();
302 r->r_sort_spec = q->sort_spec;
303 r->r_query = q->query;
306 r->options = Z3950_options_create_with_parent(c->options);
308 r->start = Z3950_options_get_int(r->options, "start", 0);
309 r->count = Z3950_options_get_int(r->options, "count", 0);
310 r->piggyback = Z3950_options_get_bool (r->options, "piggyback", 1);
313 r->next = c->resultsets;
316 task = Z3950_connection_add_task (c, Z3950_TASK_SEARCH);
317 task->u.resultset = r;
318 Z3950_resultset_addref (r);
324 while (Z3950_event (1, &c))
330 void Z3950_resultset_destroy(Z3950_resultset r)
335 yaz_log (LOG_DEBUG, "destroy r = %p count=%d", r, r->refcount);
336 if (r->refcount == 0)
340 /* remove ourselves from the resultsets in connection */
341 Z3950_resultset *rp = &r->connection->resultsets;
344 assert (*rp); /* we must be in this list!! */
346 { /* OK, we're here - take us out of it */
353 Z3950_search_destroy (r->search);
354 Z3950_options_destroy (r->options);
355 odr_destroy (r->odr);
360 int Z3950_resultset_size (Z3950_resultset r)
365 static void do_close (Z3950_connection c)
371 c->state = STATE_IDLE;
374 static void Z3950_resultset_retrieve (Z3950_resultset r,
375 int force_sync, int start, int count)
385 task = Z3950_connection_add_task (c, Z3950_TASK_RETRIEVE);
386 task->u.resultset = r;
387 Z3950_resultset_addref (r);
392 if (!r->connection->async || force_sync)
393 while (r->connection && Z3950_event (1, &r->connection))
397 void Z3950_resultset_records (Z3950_resultset r, Z3950_record *recs,
400 int force_present = 0;
405 start = Z3950_options_get_int (r->options, "start", 0);
406 count = Z3950_options_get_int (r->options, "count", 0);
409 Z3950_resultset_retrieve (r, force_present, start, count);
413 for (i = 0; i< *cnt; i++)
414 recs[i] = Z3950_resultset_record_immediate (r, i+start);
418 static void do_connect (Z3950_connection c)
421 const char *effective_host;
424 effective_host = c->proxy;
426 effective_host = c->host_port;
428 yaz_log (LOG_DEBUG, "do_connect host=%s", effective_host);
431 c->cs = cs_create_host (effective_host, 0, &add);
435 int ret = cs_connect (c->cs, add);
436 yaz_log (LOG_DEBUG, "cs_connect returned %d", ret);
439 c->state = STATE_CONNECTING;
440 c->mask = Z3950_SELECT_READ | Z3950_SELECT_WRITE | Z3950_SELECT_EXCEPT;
444 c->event_pending = 1;
445 c->state = STATE_IDLE;
446 c->error = Z3950_ERROR_CONNECT;
449 int z3950_connection_socket(Z3950_connection c)
452 return cs_fileno(c->cs);
456 int z3950_connection_mask(Z3950_connection c)
463 static int encode_APDU(Z3950_connection c, Z_APDU *a, ODR out)
468 sprintf (str, "send_APDU t=%p type=%d", c, a->which);
471 Z_OtherInformation **oi;
473 yaz_oi_set_string_oidval(oi, out, VAL_COOKIE, 1, c->cookie_out);
475 if (!z_APDU(out, &a, 0, 0))
477 FILE *outf = fopen("/tmp/apdu.txt", "w");
480 ODR odr_pr = odr_createmem(ODR_PRINT);
481 fprintf (outf, "a=%p\n", a);
482 odr_setprint(odr_pr, outf);
483 z_APDU(odr_pr, &a, 0, 0);
487 c->error = Z3950_ERROR_ENCODE;
494 static int send_APDU (Z3950_connection c, Z_APDU *a)
497 if (encode_APDU(c, a, c->odr_out))
499 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
500 odr_reset(c->odr_out);
505 static int Z3950_connection_send_init (Z3950_connection c)
508 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest);
509 Z_InitRequest *ireq = apdu->u.initRequest;
510 Z_IdAuthentication *auth = odr_malloc(c->odr_out, sizeof(*auth));
511 const char *auth_groupId = Z3950_options_get (c->options, "group");
512 const char *auth_userId = Z3950_options_get (c->options, "user");
513 const char *auth_password = Z3950_options_get (c->options, "pass");
515 ODR_MASK_SET(ireq->options, Z_Options_search);
516 ODR_MASK_SET(ireq->options, Z_Options_present);
517 ODR_MASK_SET(ireq->options, Z_Options_scan);
518 ODR_MASK_SET(ireq->options, Z_Options_sort);
520 ODR_MASK_SET(ireq->options, Z_Options_extendedServices);
521 ODR_MASK_SET(ireq->options, Z_Options_namedResultSets);
524 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
525 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
526 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
528 impname = Z3950_options_get (c->options, "implementationName");
529 ireq->implementationName =
530 odr_malloc (c->odr_out, 15 + (impname ? strlen(impname) : 0));
531 strcpy (ireq->implementationName, "");
534 strcat (ireq->implementationName, impname);
535 strcat (ireq->implementationName, "/");
537 strcat (ireq->implementationName, "ZOOM-C/YAZ");
539 *ireq->maximumRecordSize =
540 Z3950_options_get_int (c->options, "maximumRecordSize", 1024*1024);
541 *ireq->preferredMessageSize =
542 Z3950_options_get_int (c->options, "preferredMessageSize", 1024*1024);
544 if (auth_groupId || auth_password)
546 Z_IdPass *pass = odr_malloc(c->odr_out, sizeof(*pass));
549 if (auth_groupId && *auth_groupId)
551 pass->groupId = odr_malloc(c->odr_out, strlen(auth_groupId)+1);
552 strcpy(pass->groupId, auth_groupId);
556 if (auth_userId && *auth_userId)
558 pass->userId = odr_malloc(c->odr_out, strlen(auth_userId)+1);
559 strcpy(pass->userId, auth_userId);
563 if (auth_password && *auth_password)
565 pass->password = odr_malloc(c->odr_out, strlen(auth_password)+1);
566 strcpy(pass->password, auth_password);
571 auth->which = Z_IdAuthentication_idPass;
572 auth->u.idPass = pass;
573 ireq->idAuthentication = auth;
576 else if (auth_userId)
578 auth->which = Z_IdAuthentication_open;
579 auth->u.open = odr_malloc(c->odr_out, strlen(auth_userId)+1);
580 strcpy(auth->u.open, auth_userId);
581 ireq->idAuthentication = auth;
584 yaz_oi_set_string_oidval(&ireq->otherInfo, c->odr_out,
585 VAL_PROXY, 1, c->host_port);
592 static int Z3950_connection_send_search (Z3950_connection c)
595 int lslb, ssub, mspn;
597 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
598 Z_SearchRequest *search_req = apdu->u.searchRequest;
599 const char *elementSetName;
600 const char *smallSetElementSetName;
601 const char *mediumSetElementSetName;
604 assert (c->tasks->which == Z3950_TASK_SEARCH);
606 r = c->tasks->u.resultset;
609 Z3950_options_get (r->options, "elementSetName");
610 smallSetElementSetName =
611 Z3950_options_get (r->options, "smallSetElementSetName");
612 mediumSetElementSetName =
613 Z3950_options_get (r->options, "mediumSetElementSetName");
615 if (!smallSetElementSetName)
616 smallSetElementSetName = elementSetName;
618 if (!mediumSetElementSetName)
619 mediumSetElementSetName = elementSetName;
624 /* prepare query for the search request */
625 search_req->query = r->r_query;
627 search_req->databaseNames =
628 set_DatabaseNames (c, &search_req->num_databaseNames);
630 /* get syntax (no need to provide unless piggyback is in effect) */
631 syntax = Z3950_options_get (r->options, "preferredRecordSyntax");
633 lslb = Z3950_options_get_int (r->options, "largeSetLowerBound", -1);
634 ssub = Z3950_options_get_int (r->options, "smallSetUpperBound", -1);
635 mspn = Z3950_options_get_int (r->options, "mediumSetPresentNumber", -1);
636 if (lslb != -1 && ssub != -1 && mspn != -1)
638 /* So're a Z39.50 expert? Let's hope you don't do sort */
639 *search_req->largeSetLowerBound = lslb;
640 *search_req->smallSetUpperBound = ssub;
641 *search_req->mediumSetPresentNumber = mspn;
643 else if (r->start == 0 && r->count > 0
644 && r->piggyback && !r->r_sort_spec)
646 /* Regular piggyback - do it unless we're going to do sort */
647 *search_req->largeSetLowerBound = 2000000000;
648 *search_req->smallSetUpperBound = r->count;
649 *search_req->mediumSetPresentNumber = r->count;
650 smallSetElementSetName = 0; /* no need to provide this */
654 /* non-piggyback. Need not provide elementsets or syntaxes .. */
655 smallSetElementSetName = 0;
656 mediumSetElementSetName = 0;
659 if (smallSetElementSetName && *smallSetElementSetName)
661 Z_ElementSetNames *esn = odr_malloc (c->odr_out, sizeof(*esn));
663 esn->which = Z_ElementSetNames_generic;
664 esn->u.generic = odr_strdup (c->odr_out, smallSetElementSetName);
665 search_req->smallSetElementSetNames = esn;
667 if (mediumSetElementSetName && *mediumSetElementSetName)
669 Z_ElementSetNames *esn = odr_malloc (c->odr_out, sizeof(*esn));
671 esn->which = Z_ElementSetNames_generic;
672 esn->u.generic = odr_strdup (c->odr_out, mediumSetElementSetName);
673 search_req->mediumSetElementSetNames = esn;
676 search_req->preferredRecordSyntax =
677 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
679 /* send search request */
685 static void response_diag (Z3950_connection c, Z_DiagRec *p)
687 Z_DefaultDiagFormat *r;
692 if (p->which != Z_DiagRec_defaultFormat)
694 c->error = Z3950_ERROR_DECODE;
697 r = p->u.defaultFormat;
700 case Z_DefaultDiagFormat_v2Addinfo:
701 addinfo = r->u.v2Addinfo;
703 case Z_DefaultDiagFormat_v3Addinfo:
704 addinfo = r->u.v3Addinfo;
708 c->addinfo = xstrdup (addinfo);
709 c->error = *r->condition;
712 Z3950_record Z3950_record_dup (Z3950_record srec)
719 odr_enc = odr_createmem(ODR_ENCODE);
720 if (!z_NamePlusRecord (odr_enc, &srec->npr, 0, 0))
722 buf = odr_getbuf (odr_enc, &size, 0);
724 nrec = xmalloc (sizeof(*nrec));
725 nrec->odr = odr_createmem(ODR_DECODE);
726 nrec->wrbuf_marc = 0;
727 odr_setbuf (nrec->odr, buf, size, 0);
728 z_NamePlusRecord (nrec->odr, &nrec->npr, 0, 0);
730 odr_destroy (odr_enc);
734 Z3950_record Z3950_resultset_record_immediate (Z3950_resultset s, int pos)
736 Z3950_record rec = record_cache_lookup (s, pos, 0);
739 return Z3950_record_dup (rec);
742 Z3950_record Z3950_resultset_record (Z3950_resultset r, int pos)
744 Z3950_resultset_retrieve (r, 1, pos, 1);
745 return Z3950_resultset_record_immediate (r, pos);
748 void Z3950_record_destroy (Z3950_record rec)
753 wrbuf_free (rec->wrbuf_marc, 1);
754 odr_destroy (rec->odr);
758 void *Z3950_record_get (Z3950_record rec, const char *type, int *len)
760 Z_NamePlusRecord *npr;
766 if (!strcmp (type, "database"))
768 return npr->databaseName;
770 else if (!strcmp (type, "syntax"))
772 if (npr->which == Z_NamePlusRecord_databaseRecord)
774 Z_External *r = (Z_External *) npr->u.databaseRecord;
775 oident *ent = oid_getentbyoid(r->direct_reference);
781 else if (!strcmp (type, "render"))
783 if (npr->which == Z_NamePlusRecord_databaseRecord)
785 Z_External *r = (Z_External *) npr->u.databaseRecord;
786 oident *ent = oid_getentbyoid(r->direct_reference);
788 if (r->which == Z_External_sutrs)
790 *len = r->u.sutrs->len;
791 return r->u.sutrs->buf;
793 else if (r->which == Z_External_octet)
802 case VAL_APPLICATION_XML:
805 if (!rec->wrbuf_marc)
806 rec->wrbuf_marc = wrbuf_alloc();
807 if (marc_display_wrbuf (r->u.octet_aligned->buf,
809 r->u.octet_aligned->len) > 0)
811 *len = wrbuf_len(rec->wrbuf_marc);
812 return wrbuf_buf(rec->wrbuf_marc);
815 *len = r->u.octet_aligned->len;
816 return r->u.octet_aligned->buf;
818 else if (r->which == Z_External_grs1)
829 void *Z3950_resultset_get (Z3950_resultset s, int pos, const char *type,
832 Z3950_record rec = record_cache_lookup (s, pos, 0);
833 return Z3950_record_get (rec, type, len);
836 static void record_cache_add (Z3950_resultset r,
837 Z_NamePlusRecord *npr,
839 const char *elementSetName)
841 Z3950_record_cache rc;
843 for (rc = r->record_cache; rc; rc = rc->next)
847 if ((!elementSetName && !rc->elementSetName)
848 || (elementSetName && rc->elementSetName &&
849 !strcmp (elementSetName, rc->elementSetName)))
851 /* not destroying rc->npr (it's handled by nmem )*/
853 /* keeping wrbuf_marc too */
859 rc = odr_malloc (r->odr, sizeof(*rc));
862 rc->rec.wrbuf_marc = 0;
864 rc->elementSetName = odr_strdup (r->odr, elementSetName);
866 rc->elementSetName = 0;
868 rc->next = r->record_cache;
869 r->record_cache = rc;
872 static Z3950_record record_cache_lookup (Z3950_resultset r,
874 const char *elementSetName)
876 Z3950_record_cache rc;
878 for (rc = r->record_cache; rc; rc = rc->next)
882 if ((!elementSetName && !rc->elementSetName)
883 || (elementSetName && rc->elementSetName &&
884 !strcmp (elementSetName, rc->elementSetName)))
891 static void handle_records (Z3950_connection c, Z_Records *sr,
894 Z3950_resultset resultset;
898 if (c->tasks->which != Z3950_TASK_SEARCH &&
899 c->tasks->which != Z3950_TASK_RETRIEVE)
902 resultset = c->tasks->u.resultset;
904 if (sr && sr->which == Z_Records_NSD)
906 Z_DiagRec dr, *dr_p = &dr;
907 dr.which = Z_DiagRec_defaultFormat;
908 dr.u.defaultFormat = sr->u.nonSurrogateDiagnostic;
910 response_diag (c, dr_p);
912 else if (sr && sr->which == Z_Records_multipleNSD)
914 if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
915 response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
917 c->error = Z3950_ERROR_DECODE;
921 if (resultset->count + resultset->start > resultset->size)
922 resultset->count = resultset->size - resultset->start;
923 if (resultset->count < 0)
924 resultset->count = 0;
925 if (sr && sr->which == Z_Records_DBOSD)
928 NMEM nmem = odr_extract_mem (c->odr_in);
929 Z_NamePlusRecordList *p =
930 sr->u.databaseOrSurDiagnostics;
931 for (i = 0; i<p->num_records; i++)
933 record_cache_add (resultset, p->records[i],
934 i+ resultset->start, 0);
936 /* transfer our response to search_nmem .. we need it later */
937 nmem_transfer (resultset->odr->mem, nmem);
939 if (present_phase && p->num_records == 0)
941 /* present response and we didn't get any records! */
942 c->error = Z3950_ERROR_DECODE;
945 else if (present_phase)
947 /* present response and we didn't get any records! */
948 c->error = Z3950_ERROR_DECODE;
953 static void handle_present_response (Z3950_connection c, Z_PresentResponse *pr)
955 handle_records (c, pr->records, 1);
958 static void handle_search_response (Z3950_connection c, Z_SearchResponse *sr)
960 Z3950_resultset resultset;
962 yaz_log (LOG_DEBUG, "got search response");
964 if (!c->tasks || c->tasks->which != Z3950_TASK_SEARCH)
967 resultset = c->tasks->u.resultset;
969 resultset->size = *sr->resultCount;
970 handle_records (c, sr->records, 0);
973 static void sort_response (Z3950_connection c, Z_SortResponse *res)
975 if (res->diagnostics && res->num_diagnostics > 0)
976 response_diag (c, res->diagnostics[0]);
979 static int send_sort (Z3950_connection c)
981 Z3950_resultset resultset;
983 if (!c->tasks || c->tasks->which != Z3950_TASK_SEARCH)
986 resultset = c->tasks->u.resultset;
990 resultset->r_sort_spec = 0;
993 if (resultset->r_sort_spec)
995 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
996 Z_SortRequest *req = apdu->u.sortRequest;
998 req->num_inputResultSetNames = 1;
999 req->inputResultSetNames = (Z_InternationalString **)
1000 odr_malloc (c->odr_out, sizeof(*req->inputResultSetNames));
1001 req->inputResultSetNames[0] = odr_strdup (c->odr_out, "default");
1002 req->sortedResultSetName = odr_strdup (c->odr_out, "default");
1003 req->sortSequence = resultset->r_sort_spec;
1004 resultset->r_sort_spec = 0;
1005 send_APDU (c, apdu);
1011 static int send_present (Z3950_connection c)
1013 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
1014 Z_PresentRequest *req = apdu->u.presentRequest;
1016 const char *syntax =
1017 Z3950_options_get (c->options, "preferredRecordSyntax");
1018 const char *element =
1019 Z3950_options_get (c->options, "elementSetName");
1020 Z3950_resultset resultset;
1024 if (c->tasks->which != Z3950_TASK_SEARCH &&
1025 c->tasks->which != Z3950_TASK_RETRIEVE)
1028 resultset = c->tasks->u.resultset;
1030 if (c->error) /* don't continue on error */
1032 if (resultset->start < 0)
1034 for (i = 0; i<resultset->count; i++)
1037 record_cache_lookup (resultset, i + resultset->start, 0);
1041 if (i == resultset->count)
1044 resultset->start += i;
1045 resultset->count -= i;
1046 *req->resultSetStartPoint = resultset->start + 1;
1047 *req->numberOfRecordsRequested = resultset->count;
1048 assert (*req->numberOfRecordsRequested > 0);
1050 if (syntax && *syntax)
1051 req->preferredRecordSyntax =
1052 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
1054 if (element && *element)
1056 Z_ElementSetNames *esn = odr_malloc (c->odr_out, sizeof(*esn));
1057 Z_RecordComposition *compo = odr_malloc (c->odr_out, sizeof(*compo));
1059 esn->which = Z_ElementSetNames_generic;
1060 esn->u.generic = odr_strdup (c->odr_out, element);
1061 compo->which = Z_RecordComp_simple;
1062 compo->u.simple = esn;
1063 req->recordComposition = compo;
1065 send_APDU (c, apdu);
1069 static int Z3950_connection_exec_task (Z3950_connection c)
1071 Z3950_task task = c->tasks;
1073 yaz_log (LOG_DEBUG, "Z3950_connection_exec_task");
1076 if (c->error != Z3950_ERROR_NONE || !c->cs)
1078 Z3950_connection_remove_tasks (c);
1081 yaz_log (LOG_DEBUG, "Z3950_connection_exec_task type=%d", task->which);
1085 switch (task->which)
1087 case Z3950_TASK_SEARCH:
1088 /* see if search hasn't been sent yet. */
1089 if (Z3950_connection_send_search (c))
1092 case Z3950_TASK_RETRIEVE:
1093 if (send_present (c))
1097 Z3950_connection_remove_task (c);
1101 static int send_sort_present (Z3950_connection c)
1103 int r = send_sort (c);
1105 r = send_present (c);
1109 static void handle_apdu (Z3950_connection c, Z_APDU *apdu)
1111 Z_InitResponse *initrs;
1113 yaz_log (LOG_DEBUG, "hande_apdu type=%d", apdu->which);
1117 case Z_APDU_initResponse:
1118 initrs = apdu->u.initResponse;
1119 if (!*initrs->result)
1121 c->error = Z3950_ERROR_INIT;
1126 yaz_oi_get_string_oidval (&apdu->u.initResponse->otherInfo,
1128 xfree (c->cookie_in);
1131 c->cookie_in = xstrdup(cookie);
1132 Z3950_connection_exec_task (c);
1135 case Z_APDU_searchResponse:
1136 handle_search_response (c, apdu->u.searchResponse);
1137 if (!send_sort_present (c))
1138 Z3950_connection_remove_task (c);
1140 case Z_APDU_presentResponse:
1141 handle_present_response (c, apdu->u.presentResponse);
1142 if (!send_present (c))
1143 Z3950_connection_remove_task (c);
1145 case Z_APDU_sortResponse:
1146 sort_response (c, apdu->u.sortResponse);
1147 if (!send_present (c))
1148 Z3950_connection_remove_task (c);
1152 static int do_read (Z3950_connection c)
1157 r = cs_get (c->cs, &c->buf_in, &c->len_in);
1162 c->error= Z3950_ERROR_CONNECTION_LOST;
1167 odr_reset (c->odr_in);
1168 odr_setbuf (c->odr_in, c->buf_in, r, 0);
1169 if (!z_APDU (c->odr_in, &apdu, 0, 0))
1171 c->error = Z3950_ERROR_DECODE;
1176 handle_apdu (c, apdu);
1182 static int do_write_ex (Z3950_connection c, char *buf_out, int len_out)
1186 if ((r=cs_put (c->cs, buf_out, len_out)) < 0)
1188 if (c->state == STATE_CONNECTING)
1189 c->error = Z3950_ERROR_CONNECT;
1191 c->error = Z3950_ERROR_CONNECTION_LOST;
1197 c->state = STATE_ESTABLISHED;
1198 c->mask = Z3950_SELECT_READ|Z3950_SELECT_WRITE;
1202 c->state = STATE_ESTABLISHED;
1203 c->mask = Z3950_SELECT_READ;
1208 static int do_write(Z3950_connection c)
1210 return do_write_ex (c, c->buf_out, c->len_out);
1213 const char *Z3950_connection_option (Z3950_connection c, const char *key,
1216 const char *old_val = Z3950_options_get (c->options, key);
1219 Z3950_options_set (c->options, key, val);
1224 const char *Z3950_resultset_option (Z3950_resultset r, const char *key,
1227 const char *old_val = Z3950_options_get (r->options, key);
1230 Z3950_options_set (r->options, key, val);
1236 int Z3950_connection_errcode (Z3950_connection c)
1238 return Z3950_connection_error (c, 0, 0);
1241 const char *Z3950_connection_errmsg (Z3950_connection c)
1244 Z3950_connection_error (c, &msg, 0);
1248 const char *Z3950_connection_addinfo (Z3950_connection c)
1250 const char *addinfo;
1251 Z3950_connection_error (c, 0, &addinfo);
1255 int Z3950_connection_error (Z3950_connection c, const char **cp,
1256 const char **addinfo)
1258 int error = c->error;
1263 case Z3950_ERROR_NONE:
1264 *cp = "No error"; break;
1265 case Z3950_ERROR_CONNECT:
1266 *cp = "Connect failed"; break;
1267 case Z3950_ERROR_MEMORY:
1268 *cp = "Out of memory"; break;
1269 case Z3950_ERROR_ENCODE:
1270 *cp = "Encoding failed"; break;
1271 case Z3950_ERROR_DECODE:
1272 *cp = "Decoding failed"; break;
1273 case Z3950_ERROR_CONNECTION_LOST:
1274 *cp = "Connection lost"; break;
1275 case Z3950_ERROR_INIT:
1276 *cp = "Init rejected"; break;
1277 case Z3950_ERROR_INTERNAL:
1278 *cp = "Internal failure"; break;
1279 case Z3950_ERROR_TIMEOUT:
1280 *cp = "Timeout"; break;
1282 *cp = diagbib1_str (error);
1288 *addinfo = c->addinfo;
1295 int Z3950_connection_do_io(Z3950_connection c, int mask)
1298 int r = cs_look(c->cs);
1299 yaz_log (LOG_DEBUG, "Z3950_connection_do_io c=%p mask=%d cs_look=%d",
1304 c->error = Z3950_ERROR_CONNECT;
1307 else if (r == CS_CONNECT)
1309 yaz_log (LOG_DEBUG, "calling rcvconnect");
1310 if (cs_rcvconnect (c->cs) < 0)
1312 c->error = Z3950_ERROR_CONNECT;
1316 Z3950_connection_send_init (c);
1320 if (mask & Z3950_SELECT_READ)
1322 if (c->cs && (mask & Z3950_SELECT_WRITE))
1326 yaz_log (LOG_DEBUG, "Z3950_connection_do_io c=%p mask=%d", c, mask);
1327 if (c->state == STATE_CONNECTING)
1329 if (mask & Z3950_SELECT_WRITE)
1330 Z3950_connection_send_init (c);
1333 c->error = Z3950_ERROR_CONNECT;
1337 else if (c->state == STATE_ESTABLISHED)
1339 if (mask & Z3950_SELECT_READ)
1341 if (c->cs && (mask & Z3950_SELECT_WRITE))
1346 c->error = Z3950_ERROR_INTERNAL;
1350 c->event_pending = 1;
1354 int Z3950_event (int no, Z3950_connection *cs)
1357 fd_set input, output, except;
1361 for (i = 0; i<no; i++)
1363 Z3950_connection c = cs[i];
1364 if (c && c->event_pending)
1366 c->event_pending = 0;
1378 for (i = 0; i<no; i++)
1380 Z3950_connection c = cs[i];
1385 fd = z3950_connection_socket(c);
1386 mask = z3950_connection_mask(c);
1392 if (mask & Z3950_SELECT_READ)
1394 FD_SET (fd, &input);
1397 if (mask & Z3950_SELECT_WRITE)
1399 FD_SET (fd, &output);
1402 if (mask & Z3950_SELECT_EXCEPT)
1404 FD_SET (fd, &except);
1410 for (i = 0; i<no; i++)
1412 Z3950_connection c = cs[i];
1415 if (!c->cs && c->host_port && c->error == Z3950_ERROR_NONE)
1422 if (Z3950_connection_exec_task (c))
1426 yaz_log (LOG_DEBUG, "no more events");
1429 yaz_log (LOG_DEBUG, "select start");
1430 r = select (max_fd+1, &input, &output, &except, &tv);
1431 yaz_log (LOG_DEBUG, "select stop, returned r=%d", r);
1433 for (i = 0; i<no; i++)
1435 Z3950_connection c = cs[i];
1440 fd = z3950_connection_socket(c);
1444 /* no timeout and real socket */
1445 if (FD_ISSET(fd, &input))
1446 mask += Z3950_SELECT_READ;
1447 if (FD_ISSET(fd, &output))
1448 mask += Z3950_SELECT_WRITE;
1449 if (FD_ISSET(fd, &except))
1450 mask += Z3950_SELECT_EXCEPT;
1452 Z3950_connection_do_io(c, mask);
1454 if (r == 0 && c->mask)
1456 /* timeout and this connection was waiting */
1457 c->error = Z3950_ERROR_TIMEOUT;
1458 c->event_pending = 1;
1461 for (i = 0; i<no; i++)
1463 Z3950_connection c = cs[i];
1466 if (c->event_pending)
1468 c->event_pending = 0;