2 * $Id: zoom-c.c,v 1.4 2001-11-11 22:25:25 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>
21 static Z3950_record record_cache_lookup (Z3950_resultset r,
23 const char *elementSetName);
25 static void clear_error (Z3950_connection c)
27 c->error = Z3950_ERROR_NONE;
32 Z3950_connection Z3950_connection_create (Z3950_options options)
34 Z3950_connection c = xmalloc (sizeof(*c));
39 c->state = STATE_IDLE;
40 c->error = Z3950_ERROR_NONE;
48 c->options = Z3950_options_create_with_parent(options);
57 c->odr_in = odr_createmem (ODR_DECODE);
58 c->odr_out = odr_createmem (ODR_ENCODE);
64 /* set database names. Take local databases (if set); otherwise
65 take databases given in ZURL (if set); otherwise use Default */
66 static char **set_DatabaseNames (Z3950_connection con, int *num)
71 const char *cp = Z3950_options_get (con->options, "databaseName");
75 cp = strchr (con->host_port, '/');
82 while ((c = strchr(c, '+')))
90 databaseNames = odr_malloc (con->odr_out, no * sizeof(*databaseNames));
102 /* cp ptr to first char of db name, c is char
104 databaseNames[no] = odr_malloc (con->odr_out, 1+c-cp);
105 memcpy (databaseNames[no], cp, c-cp);
106 databaseNames[no++][c-cp] = '\0';
111 databaseNames[no] = NULL;
113 return databaseNames;
116 Z3950_connection Z3950_connection_new (const char *host, int portnum)
118 Z3950_connection c = Z3950_connection_create (0);
120 Z3950_connection_connect (c, host, portnum);
124 void Z3950_connection_connect(Z3950_connection c,
125 const char *host, int portnum)
129 val = Z3950_options_get (c->options, "proxy");
131 c->proxy = xstrdup (val);
138 sprintf (hostn, "%.80s:%d", host, portnum);
139 c->host_port = xstrdup(hostn);
142 c->host_port = xstrdup(host);
144 c->async = Z3950_options_get_bool (c->options, "async", 0);
148 while (Z3950_event (1, &c))
153 Z3950_query Z3950_query_create(void)
155 Z3950_query s = xmalloc (sizeof(*s));
160 s->odr = odr_createmem (ODR_ENCODE);
165 const char *Z3950_connection_host (Z3950_connection c)
170 void Z3950_query_destroy(Z3950_query s)
176 yaz_log (LOG_DEBUG, "Z3950_query_destroy count=%d", s->refcount);
177 if (s->refcount == 0)
179 odr_destroy (s->odr);
184 int Z3950_query_prefix(Z3950_query s, const char *str)
186 s->query = odr_malloc (s->odr, sizeof(*s->query));
187 s->query->which = Z_Query_type_1;
188 s->query->u.type_1 = p_query_rpn(s->odr, PROTO_Z3950, str);
189 if (!s->query->u.type_1)
194 int Z3950_query_sortby(Z3950_query s, const char *criteria)
196 s->sort_spec = yaz_sort_spec (s->odr, criteria);
202 static int do_write(Z3950_connection c);
205 Z3950_task Z3950_connection_add_task (Z3950_connection c, int which)
207 Z3950_task *taskp = &c->tasks;
209 taskp = &(*taskp)->next;
210 *taskp = xmalloc (sizeof(**taskp));
211 (*taskp)->running = 0;
212 (*taskp)->which = which;
213 (*taskp)->u.resultset = 0; /* one null pointer there at least */
219 void Z3950_connection_remove_task (Z3950_connection c)
221 Z3950_task task = c->tasks;
225 c->tasks = task->next;
228 case Z3950_TASK_SEARCH:
229 Z3950_resultset_destroy (task->u.resultset);
231 case Z3950_TASK_RETRIEVE:
232 Z3950_resultset_destroy (task->u.resultset);
241 void Z3950_connection_remove_tasks (Z3950_connection c)
244 Z3950_connection_remove_task(c);
247 void Z3950_connection_destroy(Z3950_connection c)
254 for (r = c->resultsets; r; r = r->next)
259 odr_destroy (c->odr_in);
260 odr_destroy (c->odr_out);
261 Z3950_options_destroy (c->options);
262 Z3950_connection_remove_tasks (c);
263 xfree (c->host_port);
267 void Z3950_resultset_addref (Z3950_resultset r)
272 Z3950_resultset Z3950_resultset_create ()
274 Z3950_resultset r = xmalloc (sizeof(*r));
278 r->odr = odr_createmem (ODR_ENCODE);
291 Z3950_resultset Z3950_connection_search_pqf(Z3950_connection c, const char *q)
294 Z3950_query s = Z3950_query_create();
296 Z3950_query_prefix (s, q);
298 r = Z3950_connection_search (c, s);
299 Z3950_query_destroy (s);
303 Z3950_resultset Z3950_connection_search(Z3950_connection c, Z3950_query q)
305 Z3950_resultset r = Z3950_resultset_create ();
308 r->r_sort_spec = q->sort_spec;
309 r->r_query = q->query;
312 r->options = Z3950_options_create_with_parent(c->options);
314 r->start = Z3950_options_get_int(r->options, "start", 0);
315 r->count = Z3950_options_get_int(r->options, "count", 0);
316 r->piggyback = Z3950_options_get_bool (r->options, "piggyback", 1);
319 r->next = c->resultsets;
322 task = Z3950_connection_add_task (c, Z3950_TASK_SEARCH);
323 task->u.resultset = r;
324 Z3950_resultset_addref (r);
330 while (Z3950_event (1, &c))
336 void Z3950_resultset_destroy(Z3950_resultset r)
341 yaz_log (LOG_DEBUG, "destroy r = %p count=%d", r, r->refcount);
342 if (r->refcount == 0)
346 /* remove ourselves from the resultsets in connection */
347 Z3950_resultset *rp = &r->connection->resultsets;
350 assert (*rp); /* we must be in this list!! */
352 { /* OK, we're here - take us out of it */
359 Z3950_query_destroy (r->search);
360 Z3950_options_destroy (r->options);
361 odr_destroy (r->odr);
366 int Z3950_resultset_size (Z3950_resultset r)
371 static void do_close (Z3950_connection c)
377 c->state = STATE_IDLE;
380 static void Z3950_resultset_retrieve (Z3950_resultset r,
381 int force_sync, int start, int count)
391 task = Z3950_connection_add_task (c, Z3950_TASK_RETRIEVE);
392 task->u.resultset = r;
393 Z3950_resultset_addref (r);
398 if (!r->connection->async || force_sync)
399 while (r->connection && Z3950_event (1, &r->connection))
403 void Z3950_resultset_records (Z3950_resultset r, Z3950_record *recs,
404 size_t start, size_t count)
406 int force_present = 0;
412 Z3950_resultset_retrieve (r, force_present, start, count);
416 for (i = 0; i< count; i++)
417 recs[i] = Z3950_resultset_record_immediate (r, i+start);
421 static void do_connect (Z3950_connection c)
424 const char *effective_host;
427 effective_host = c->proxy;
429 effective_host = c->host_port;
431 yaz_log (LOG_DEBUG, "do_connect host=%s", effective_host);
434 c->cs = cs_create_host (effective_host, 0, &add);
438 int ret = cs_connect (c->cs, add);
439 yaz_log (LOG_DEBUG, "cs_connect returned %d", ret);
442 c->state = STATE_CONNECTING;
443 c->mask = Z3950_SELECT_READ | Z3950_SELECT_WRITE |
448 c->event_pending = 1;
449 c->state = STATE_IDLE;
450 c->error = Z3950_ERROR_CONNECT;
453 int z3950_connection_socket(Z3950_connection c)
456 return cs_fileno(c->cs);
460 int z3950_connection_mask(Z3950_connection c)
467 static int encode_APDU(Z3950_connection c, Z_APDU *a, ODR out)
472 sprintf (str, "send_APDU t=%p type=%d", c, a->which);
475 Z_OtherInformation **oi;
477 yaz_oi_set_string_oidval(oi, out, VAL_COOKIE, 1, c->cookie_out);
479 if (!z_APDU(out, &a, 0, 0))
481 FILE *outf = fopen("/tmp/apdu.txt", "w");
484 ODR odr_pr = odr_createmem(ODR_PRINT);
485 fprintf (outf, "a=%p\n", a);
486 odr_setprint(odr_pr, outf);
487 z_APDU(odr_pr, &a, 0, 0);
491 c->error = Z3950_ERROR_ENCODE;
498 static int send_APDU (Z3950_connection c, Z_APDU *a)
501 if (encode_APDU(c, a, c->odr_out))
503 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
504 odr_reset(c->odr_out);
509 static int Z3950_connection_send_init (Z3950_connection c)
512 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest);
513 Z_InitRequest *ireq = apdu->u.initRequest;
514 Z_IdAuthentication *auth = odr_malloc(c->odr_out, sizeof(*auth));
515 const char *auth_groupId = Z3950_options_get (c->options, "group");
516 const char *auth_userId = Z3950_options_get (c->options, "user");
517 const char *auth_password = Z3950_options_get (c->options, "pass");
519 ODR_MASK_SET(ireq->options, Z_Options_search);
520 ODR_MASK_SET(ireq->options, Z_Options_present);
521 ODR_MASK_SET(ireq->options, Z_Options_scan);
522 ODR_MASK_SET(ireq->options, Z_Options_sort);
524 ODR_MASK_SET(ireq->options, Z_Options_extendedServices);
525 ODR_MASK_SET(ireq->options, Z_Options_namedResultSets);
528 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
529 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
530 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
532 impname = Z3950_options_get (c->options, "implementationName");
533 ireq->implementationName =
534 odr_malloc (c->odr_out, 15 + (impname ? strlen(impname) : 0));
535 strcpy (ireq->implementationName, "");
538 strcat (ireq->implementationName, impname);
539 strcat (ireq->implementationName, "/");
541 strcat (ireq->implementationName, "ZOOM-C/YAZ");
543 *ireq->maximumRecordSize =
544 Z3950_options_get_int (c->options, "maximumRecordSize", 1024*1024);
545 *ireq->preferredMessageSize =
546 Z3950_options_get_int (c->options, "preferredMessageSize", 1024*1024);
548 if (auth_groupId || auth_password)
550 Z_IdPass *pass = odr_malloc(c->odr_out, sizeof(*pass));
553 if (auth_groupId && *auth_groupId)
555 pass->groupId = odr_malloc(c->odr_out, strlen(auth_groupId)+1);
556 strcpy(pass->groupId, auth_groupId);
560 if (auth_userId && *auth_userId)
562 pass->userId = odr_malloc(c->odr_out, strlen(auth_userId)+1);
563 strcpy(pass->userId, auth_userId);
567 if (auth_password && *auth_password)
569 pass->password = odr_malloc(c->odr_out, strlen(auth_password)+1);
570 strcpy(pass->password, auth_password);
575 auth->which = Z_IdAuthentication_idPass;
576 auth->u.idPass = pass;
577 ireq->idAuthentication = auth;
580 else if (auth_userId)
582 auth->which = Z_IdAuthentication_open;
583 auth->u.open = odr_malloc(c->odr_out, strlen(auth_userId)+1);
584 strcpy(auth->u.open, auth_userId);
585 ireq->idAuthentication = auth;
588 yaz_oi_set_string_oidval(&ireq->otherInfo, c->odr_out,
589 VAL_PROXY, 1, c->host_port);
596 static int Z3950_connection_send_search (Z3950_connection c)
599 int lslb, ssub, mspn;
601 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
602 Z_SearchRequest *search_req = apdu->u.searchRequest;
603 const char *elementSetName;
604 const char *smallSetElementSetName;
605 const char *mediumSetElementSetName;
608 assert (c->tasks->which == Z3950_TASK_SEARCH);
610 r = c->tasks->u.resultset;
613 Z3950_options_get (r->options, "elementSetName");
614 smallSetElementSetName =
615 Z3950_options_get (r->options, "smallSetElementSetName");
616 mediumSetElementSetName =
617 Z3950_options_get (r->options, "mediumSetElementSetName");
619 if (!smallSetElementSetName)
620 smallSetElementSetName = elementSetName;
622 if (!mediumSetElementSetName)
623 mediumSetElementSetName = elementSetName;
628 /* prepare query for the search request */
629 search_req->query = r->r_query;
631 search_req->databaseNames =
632 set_DatabaseNames (c, &search_req->num_databaseNames);
634 /* get syntax (no need to provide unless piggyback is in effect) */
635 syntax = Z3950_options_get (r->options, "preferredRecordSyntax");
637 lslb = Z3950_options_get_int (r->options, "largeSetLowerBound", -1);
638 ssub = Z3950_options_get_int (r->options, "smallSetUpperBound", -1);
639 mspn = Z3950_options_get_int (r->options, "mediumSetPresentNumber", -1);
640 if (lslb != -1 && ssub != -1 && mspn != -1)
642 /* So're a Z39.50 expert? Let's hope you don't do sort */
643 *search_req->largeSetLowerBound = lslb;
644 *search_req->smallSetUpperBound = ssub;
645 *search_req->mediumSetPresentNumber = mspn;
647 else if (r->start == 0 && r->count > 0
648 && r->piggyback && !r->r_sort_spec)
650 /* Regular piggyback - do it unless we're going to do sort */
651 *search_req->largeSetLowerBound = 2000000000;
652 *search_req->smallSetUpperBound = r->count;
653 *search_req->mediumSetPresentNumber = r->count;
654 smallSetElementSetName = 0; /* no need to provide this */
658 /* non-piggyback. Need not provide elementsets or syntaxes .. */
659 smallSetElementSetName = 0;
660 mediumSetElementSetName = 0;
663 if (smallSetElementSetName && *smallSetElementSetName)
665 Z_ElementSetNames *esn = odr_malloc (c->odr_out, sizeof(*esn));
667 esn->which = Z_ElementSetNames_generic;
668 esn->u.generic = odr_strdup (c->odr_out, smallSetElementSetName);
669 search_req->smallSetElementSetNames = esn;
671 if (mediumSetElementSetName && *mediumSetElementSetName)
673 Z_ElementSetNames *esn = odr_malloc (c->odr_out, sizeof(*esn));
675 esn->which = Z_ElementSetNames_generic;
676 esn->u.generic = odr_strdup (c->odr_out, mediumSetElementSetName);
677 search_req->mediumSetElementSetNames = esn;
680 search_req->preferredRecordSyntax =
681 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
683 /* send search request */
689 static void response_diag (Z3950_connection c, Z_DiagRec *p)
691 Z_DefaultDiagFormat *r;
696 if (p->which != Z_DiagRec_defaultFormat)
698 c->error = Z3950_ERROR_DECODE;
701 r = p->u.defaultFormat;
704 case Z_DefaultDiagFormat_v2Addinfo:
705 addinfo = r->u.v2Addinfo;
707 case Z_DefaultDiagFormat_v3Addinfo:
708 addinfo = r->u.v3Addinfo;
712 c->addinfo = xstrdup (addinfo);
713 c->error = *r->condition;
716 Z3950_record Z3950_record_dup (Z3950_record srec)
723 odr_enc = odr_createmem(ODR_ENCODE);
724 if (!z_NamePlusRecord (odr_enc, &srec->npr, 0, 0))
726 buf = odr_getbuf (odr_enc, &size, 0);
728 nrec = xmalloc (sizeof(*nrec));
729 nrec->odr = odr_createmem(ODR_DECODE);
730 nrec->wrbuf_marc = 0;
731 odr_setbuf (nrec->odr, buf, size, 0);
732 z_NamePlusRecord (nrec->odr, &nrec->npr, 0, 0);
734 odr_destroy (odr_enc);
738 Z3950_record Z3950_resultset_record_immediate (Z3950_resultset s,size_t pos)
740 Z3950_record rec = record_cache_lookup (s, pos, 0);
743 return Z3950_record_dup (rec);
746 Z3950_record Z3950_resultset_record (Z3950_resultset r, size_t pos)
748 Z3950_resultset_retrieve (r, 1, pos, 1);
749 return Z3950_resultset_record_immediate (r, pos);
752 void Z3950_record_destroy (Z3950_record rec)
757 wrbuf_free (rec->wrbuf_marc, 1);
758 odr_destroy (rec->odr);
762 void *Z3950_record_get (Z3950_record rec, const char *type, size_t *len)
764 Z_NamePlusRecord *npr;
770 if (!strcmp (type, "database"))
772 return npr->databaseName;
774 else if (!strcmp (type, "syntax"))
776 if (npr->which == Z_NamePlusRecord_databaseRecord)
778 Z_External *r = (Z_External *) npr->u.databaseRecord;
779 oident *ent = oid_getentbyoid(r->direct_reference);
785 else if (!strcmp (type, "render"))
787 if (npr->which == Z_NamePlusRecord_databaseRecord)
789 Z_External *r = (Z_External *) npr->u.databaseRecord;
790 oident *ent = oid_getentbyoid(r->direct_reference);
792 if (r->which == Z_External_sutrs)
794 *len = r->u.sutrs->len;
795 return r->u.sutrs->buf;
797 else if (r->which == Z_External_octet)
806 case VAL_APPLICATION_XML:
809 if (!rec->wrbuf_marc)
810 rec->wrbuf_marc = wrbuf_alloc();
811 if (marc_display_wrbuf (r->u.octet_aligned->buf,
813 r->u.octet_aligned->len) > 0)
815 *len = wrbuf_len(rec->wrbuf_marc);
816 return wrbuf_buf(rec->wrbuf_marc);
819 *len = r->u.octet_aligned->len;
820 return r->u.octet_aligned->buf;
822 else if (r->which == Z_External_grs1)
830 else if (!strcmp (type, "raw"))
832 if (npr->which == Z_NamePlusRecord_databaseRecord)
835 return (Z_External *) npr->u.databaseRecord;
842 void *Z3950_resultset_get (Z3950_resultset s, size_t pos, const char *type,
845 Z3950_record rec = record_cache_lookup (s, pos, 0);
846 return Z3950_record_get (rec, type, len);
849 static void record_cache_add (Z3950_resultset r,
850 Z_NamePlusRecord *npr,
852 const char *elementSetName)
854 Z3950_record_cache rc;
856 for (rc = r->record_cache; rc; rc = rc->next)
860 if ((!elementSetName && !rc->elementSetName)
861 || (elementSetName && rc->elementSetName &&
862 !strcmp (elementSetName, rc->elementSetName)))
864 /* not destroying rc->npr (it's handled by nmem )*/
866 /* keeping wrbuf_marc too */
872 rc = odr_malloc (r->odr, sizeof(*rc));
875 rc->rec.wrbuf_marc = 0;
877 rc->elementSetName = odr_strdup (r->odr, elementSetName);
879 rc->elementSetName = 0;
881 rc->next = r->record_cache;
882 r->record_cache = rc;
885 static Z3950_record record_cache_lookup (Z3950_resultset r,
887 const char *elementSetName)
889 Z3950_record_cache rc;
891 for (rc = r->record_cache; rc; rc = rc->next)
895 if ((!elementSetName && !rc->elementSetName)
896 || (elementSetName && rc->elementSetName &&
897 !strcmp (elementSetName, rc->elementSetName)))
904 static void handle_records (Z3950_connection c, Z_Records *sr,
907 Z3950_resultset resultset;
911 if (c->tasks->which != Z3950_TASK_SEARCH &&
912 c->tasks->which != Z3950_TASK_RETRIEVE)
915 resultset = c->tasks->u.resultset;
917 if (sr && sr->which == Z_Records_NSD)
919 Z_DiagRec dr, *dr_p = &dr;
920 dr.which = Z_DiagRec_defaultFormat;
921 dr.u.defaultFormat = sr->u.nonSurrogateDiagnostic;
923 response_diag (c, dr_p);
925 else if (sr && sr->which == Z_Records_multipleNSD)
927 if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
928 response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
930 c->error = Z3950_ERROR_DECODE;
934 if (resultset->count + resultset->start > resultset->size)
935 resultset->count = resultset->size - resultset->start;
936 if (resultset->count < 0)
937 resultset->count = 0;
938 if (sr && sr->which == Z_Records_DBOSD)
941 NMEM nmem = odr_extract_mem (c->odr_in);
942 Z_NamePlusRecordList *p =
943 sr->u.databaseOrSurDiagnostics;
944 for (i = 0; i<p->num_records; i++)
946 record_cache_add (resultset, p->records[i],
947 i+ resultset->start, 0);
949 /* transfer our response to search_nmem .. we need it later */
950 nmem_transfer (resultset->odr->mem, nmem);
952 if (present_phase && p->num_records == 0)
954 /* present response and we didn't get any records! */
955 c->error = Z3950_ERROR_DECODE;
958 else if (present_phase)
960 /* present response and we didn't get any records! */
961 c->error = Z3950_ERROR_DECODE;
966 static void handle_present_response (Z3950_connection c, Z_PresentResponse *pr)
968 handle_records (c, pr->records, 1);
971 static void handle_search_response (Z3950_connection c, Z_SearchResponse *sr)
973 Z3950_resultset resultset;
975 yaz_log (LOG_DEBUG, "got search response");
977 if (!c->tasks || c->tasks->which != Z3950_TASK_SEARCH)
980 resultset = c->tasks->u.resultset;
982 resultset->size = *sr->resultCount;
983 handle_records (c, sr->records, 0);
986 static void sort_response (Z3950_connection c, Z_SortResponse *res)
988 if (res->diagnostics && res->num_diagnostics > 0)
989 response_diag (c, res->diagnostics[0]);
992 static int send_sort (Z3950_connection c)
994 Z3950_resultset resultset;
996 if (!c->tasks || c->tasks->which != Z3950_TASK_SEARCH)
999 resultset = c->tasks->u.resultset;
1003 resultset->r_sort_spec = 0;
1006 if (resultset->r_sort_spec)
1008 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
1009 Z_SortRequest *req = apdu->u.sortRequest;
1011 req->num_inputResultSetNames = 1;
1012 req->inputResultSetNames = (Z_InternationalString **)
1013 odr_malloc (c->odr_out, sizeof(*req->inputResultSetNames));
1014 req->inputResultSetNames[0] = odr_strdup (c->odr_out, "default");
1015 req->sortedResultSetName = odr_strdup (c->odr_out, "default");
1016 req->sortSequence = resultset->r_sort_spec;
1017 resultset->r_sort_spec = 0;
1018 send_APDU (c, apdu);
1024 static int send_present (Z3950_connection c)
1026 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
1027 Z_PresentRequest *req = apdu->u.presentRequest;
1029 const char *syntax =
1030 Z3950_options_get (c->options, "preferredRecordSyntax");
1031 const char *element =
1032 Z3950_options_get (c->options, "elementSetName");
1033 Z3950_resultset resultset;
1037 if (c->tasks->which != Z3950_TASK_SEARCH &&
1038 c->tasks->which != Z3950_TASK_RETRIEVE)
1041 resultset = c->tasks->u.resultset;
1043 if (c->error) /* don't continue on error */
1045 if (resultset->start < 0)
1047 for (i = 0; i<resultset->count; i++)
1050 record_cache_lookup (resultset, i + resultset->start, 0);
1054 if (i == resultset->count)
1057 resultset->start += i;
1058 resultset->count -= i;
1059 *req->resultSetStartPoint = resultset->start + 1;
1060 *req->numberOfRecordsRequested = resultset->count;
1061 assert (*req->numberOfRecordsRequested > 0);
1063 if (syntax && *syntax)
1064 req->preferredRecordSyntax =
1065 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
1067 if (element && *element)
1069 Z_ElementSetNames *esn = odr_malloc (c->odr_out, sizeof(*esn));
1070 Z_RecordComposition *compo = odr_malloc (c->odr_out, sizeof(*compo));
1072 esn->which = Z_ElementSetNames_generic;
1073 esn->u.generic = odr_strdup (c->odr_out, element);
1074 compo->which = Z_RecordComp_simple;
1075 compo->u.simple = esn;
1076 req->recordComposition = compo;
1078 send_APDU (c, apdu);
1082 static int Z3950_connection_exec_task (Z3950_connection c)
1084 Z3950_task task = c->tasks;
1086 yaz_log (LOG_DEBUG, "Z3950_connection_exec_task");
1089 if (c->error != Z3950_ERROR_NONE || !c->cs)
1091 Z3950_connection_remove_tasks (c);
1094 yaz_log (LOG_DEBUG, "Z3950_connection_exec_task type=%d", task->which);
1098 switch (task->which)
1100 case Z3950_TASK_SEARCH:
1101 /* see if search hasn't been sent yet. */
1102 if (Z3950_connection_send_search (c))
1105 case Z3950_TASK_RETRIEVE:
1106 if (send_present (c))
1110 Z3950_connection_remove_task (c);
1114 static int send_sort_present (Z3950_connection c)
1116 int r = send_sort (c);
1118 r = send_present (c);
1122 static void handle_apdu (Z3950_connection c, Z_APDU *apdu)
1124 Z_InitResponse *initrs;
1126 yaz_log (LOG_DEBUG, "hande_apdu type=%d", apdu->which);
1130 case Z_APDU_initResponse:
1131 initrs = apdu->u.initResponse;
1132 if (!*initrs->result)
1134 c->error = Z3950_ERROR_INIT;
1139 yaz_oi_get_string_oidval (&apdu->u.initResponse->otherInfo,
1141 xfree (c->cookie_in);
1144 c->cookie_in = xstrdup(cookie);
1145 Z3950_connection_exec_task (c);
1148 case Z_APDU_searchResponse:
1149 handle_search_response (c, apdu->u.searchResponse);
1150 if (!send_sort_present (c))
1151 Z3950_connection_remove_task (c);
1153 case Z_APDU_presentResponse:
1154 handle_present_response (c, apdu->u.presentResponse);
1155 if (!send_present (c))
1156 Z3950_connection_remove_task (c);
1158 case Z_APDU_sortResponse:
1159 sort_response (c, apdu->u.sortResponse);
1160 if (!send_present (c))
1161 Z3950_connection_remove_task (c);
1165 static int do_read (Z3950_connection c)
1170 r = cs_get (c->cs, &c->buf_in, &c->len_in);
1175 c->error= Z3950_ERROR_CONNECTION_LOST;
1180 odr_reset (c->odr_in);
1181 odr_setbuf (c->odr_in, c->buf_in, r, 0);
1182 if (!z_APDU (c->odr_in, &apdu, 0, 0))
1184 c->error = Z3950_ERROR_DECODE;
1189 handle_apdu (c, apdu);
1195 static int do_write_ex (Z3950_connection c, char *buf_out, int len_out)
1199 if ((r=cs_put (c->cs, buf_out, len_out)) < 0)
1201 if (c->state == STATE_CONNECTING)
1202 c->error = Z3950_ERROR_CONNECT;
1204 c->error = Z3950_ERROR_CONNECTION_LOST;
1210 c->state = STATE_ESTABLISHED;
1211 c->mask = Z3950_SELECT_READ|Z3950_SELECT_WRITE|Z3950_SELECT_EXCEPT;
1215 c->state = STATE_ESTABLISHED;
1216 c->mask = Z3950_SELECT_READ|Z3950_SELECT_EXCEPT;
1221 static int do_write(Z3950_connection c)
1223 return do_write_ex (c, c->buf_out, c->len_out);
1226 const char *Z3950_connection_option (Z3950_connection c, const char *key,
1229 const char *old_val = Z3950_options_get (c->options, key);
1232 Z3950_options_set (c->options, key, val);
1237 const char *Z3950_resultset_option (Z3950_resultset r, const char *key,
1240 const char *old_val = Z3950_options_get (r->options, key);
1243 Z3950_options_set (r->options, key, val);
1249 int Z3950_connection_errcode (Z3950_connection c)
1251 return Z3950_connection_error (c, 0, 0);
1254 const char *Z3950_connection_errmsg (Z3950_connection c)
1257 Z3950_connection_error (c, &msg, 0);
1261 const char *Z3950_connection_addinfo (Z3950_connection c)
1263 const char *addinfo;
1264 Z3950_connection_error (c, 0, &addinfo);
1268 int Z3950_connection_error (Z3950_connection c, const char **cp,
1269 const char **addinfo)
1271 int error = c->error;
1276 case Z3950_ERROR_NONE:
1277 *cp = "No error"; break;
1278 case Z3950_ERROR_CONNECT:
1279 *cp = "Connect failed"; break;
1280 case Z3950_ERROR_MEMORY:
1281 *cp = "Out of memory"; break;
1282 case Z3950_ERROR_ENCODE:
1283 *cp = "Encoding failed"; break;
1284 case Z3950_ERROR_DECODE:
1285 *cp = "Decoding failed"; break;
1286 case Z3950_ERROR_CONNECTION_LOST:
1287 *cp = "Connection lost"; break;
1288 case Z3950_ERROR_INIT:
1289 *cp = "Init rejected"; break;
1290 case Z3950_ERROR_INTERNAL:
1291 *cp = "Internal failure"; break;
1292 case Z3950_ERROR_TIMEOUT:
1293 *cp = "Timeout"; break;
1295 *cp = diagbib1_str (error);
1301 *addinfo = c->addinfo;
1308 int Z3950_connection_do_io(Z3950_connection c, int mask)
1311 int r = cs_look(c->cs);
1312 yaz_log (LOG_LOG, "Z3950_connection_do_io c=%p mask=%d cs_look=%d",
1317 c->error = Z3950_ERROR_CONNECT;
1320 else if (r == CS_CONNECT)
1322 yaz_log (LOG_LOG, "calling rcvconnect");
1323 if (cs_rcvconnect (c->cs) < 0)
1325 c->error = Z3950_ERROR_CONNECT;
1329 Z3950_connection_send_init (c);
1333 if (mask & Z3950_SELECT_READ)
1335 if (c->cs && (mask & Z3950_SELECT_WRITE))
1339 yaz_log (LOG_DEBUG, "Z3950_connection_do_io c=%p mask=%d", c, mask);
1340 if (c->state == STATE_CONNECTING)
1342 if (mask & Z3950_SELECT_WRITE)
1343 Z3950_connection_send_init (c);
1346 c->error = Z3950_ERROR_CONNECT;
1350 else if (c->state == STATE_ESTABLISHED)
1352 if (mask & Z3950_SELECT_READ)
1354 if (c->cs && (mask & Z3950_SELECT_WRITE))
1359 c->error = Z3950_ERROR_INTERNAL;
1363 c->event_pending = 1;
1367 int Z3950_event (int no, Z3950_connection *cs)
1370 struct pollfd pollfds[1024];
1371 Z3950_connection poll_cs[1024];
1374 fd_set input, output, except;
1379 for (i = 0; i<no; i++)
1381 Z3950_connection c = cs[i];
1382 if (c && c->event_pending)
1384 c->event_pending = 0;
1400 for (i = 0; i<no; i++)
1402 Z3950_connection c = cs[i];
1407 fd = z3950_connection_socket(c);
1408 mask = z3950_connection_mask(c);
1418 short poll_events = 0;
1420 if (mask & Z3950_SELECT_READ)
1421 poll_events += POLLIN;
1422 if (mask & Z3950_SELECT_WRITE)
1423 poll_events += POLLOUT;
1424 if (mask & Z3950_SELECT_EXCEPT)
1425 poll_events += POLLERR;
1426 pollfds[nfds].fd = fd;
1427 pollfds[nfds].events = poll_events;
1428 pollfds[nfds].revents = 0;
1433 if (mask & Z3950_SELECT_READ)
1435 FD_SET (fd, &input);
1438 if (mask & Z3950_SELECT_WRITE)
1440 FD_SET (fd, &output);
1443 if (mask & Z3950_SELECT_EXCEPT)
1445 FD_SET (fd, &except);
1452 for (i = 0; i<no; i++)
1454 Z3950_connection c = cs[i];
1457 if (!c->cs && c->host_port && c->error == Z3950_ERROR_NONE)
1464 if (Z3950_connection_exec_task (c))
1468 yaz_log (LOG_DEBUG, "no more events");
1472 yaz_log (LOG_LOG, "poll start");
1473 r = poll (pollfds, nfds, 15000);
1474 yaz_log (LOG_LOG, "poll stop, returned r=%d", r);
1475 for (i = 0; i<nfds; i++)
1477 Z3950_connection c = poll_cs[i];
1481 if (pollfds[i].revents & POLLIN)
1482 mask += Z3950_SELECT_READ;
1483 if (pollfds[i].revents & POLLOUT)
1484 mask += Z3950_SELECT_WRITE;
1485 if (pollfds[i].revents & POLLERR)
1486 mask += Z3950_SELECT_EXCEPT;
1488 Z3950_connection_do_io(c, mask);
1490 else if (r == 0 && c->mask)
1492 /* timeout and this connection was waiting */
1493 c->error = Z3950_ERROR_TIMEOUT;
1494 c->event_pending = 1;
1498 yaz_log (LOG_DEBUG, "select start");
1499 r = select (max_fd+1, &input, &output, &except, &tv);
1500 yaz_log (LOG_DEBUG, "select stop, returned r=%d", r);
1501 for (i = 0; i<no; i++)
1503 Z3950_connection c = cs[i];
1508 fd = z3950_connection_socket(c);
1512 /* no timeout and real socket */
1513 if (FD_ISSET(fd, &input))
1514 mask += Z3950_SELECT_READ;
1515 if (FD_ISSET(fd, &output))
1516 mask += Z3950_SELECT_WRITE;
1517 if (FD_ISSET(fd, &except))
1518 mask += Z3950_SELECT_EXCEPT;
1520 Z3950_connection_do_io(c, mask);
1522 if (r == 0 && c->mask)
1524 /* timeout and this connection was waiting */
1525 c->error = Z3950_ERROR_TIMEOUT;
1526 c->event_pending = 1;
1531 for (i = 0; i<no; i++)
1533 Z3950_connection c = cs[i];
1536 if (c->event_pending)
1538 c->event_pending = 0;