2 * $Id: zoom-c.c,v 1.6 2001-11-15 08:58:29 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>
19 static Z3950_Event Z3950_Event_create (int kind)
21 Z3950_Event event = xmalloc (sizeof(*event));
28 static void Z3950_Event_destroy (Z3950_Event event)
33 static void Z3950_connection_put_event (Z3950_connection c, Z3950_Event event)
35 // put in back of queue
38 c->m_queue_back->prev = event;
39 assert (c->m_queue_front);
43 assert (!c->m_queue_front);
44 c->m_queue_front = event;
46 event->next = c->m_queue_back;
48 c->m_queue_back = event;
51 static Z3950_Event Z3950_connection_get_event(Z3950_connection c)
53 // get from front of queue
54 Z3950_Event event = c->m_queue_front;
57 assert (c->m_queue_back);
58 c->m_queue_front = event->prev;
61 assert (c->m_queue_back);
62 c->m_queue_front->next = 0;
69 static void clear_error (Z3950_connection c)
71 c->error = Z3950_ERROR_NONE;
76 Z3950_task Z3950_connection_add_task (Z3950_connection c, int which)
78 Z3950_task *taskp = &c->tasks;
80 taskp = &(*taskp)->next;
81 *taskp = xmalloc (sizeof(**taskp));
82 (*taskp)->running = 0;
83 (*taskp)->which = which;
84 (*taskp)->u.resultset = 0; /* one null pointer there at least */
90 void Z3950_connection_remove_task (Z3950_connection c)
92 Z3950_task task = c->tasks;
96 c->tasks = task->next;
99 case Z3950_TASK_SEARCH:
100 Z3950_resultset_destroy (task->u.resultset);
102 case Z3950_TASK_RETRIEVE:
103 Z3950_resultset_destroy (task->u.resultset);
105 case Z3950_TASK_CONNECT:
114 void Z3950_connection_remove_tasks (Z3950_connection c)
117 Z3950_connection_remove_task(c);
120 static Z3950_record record_cache_lookup (Z3950_resultset r,
122 const char *elementSetName);
124 Z3950_connection Z3950_connection_create (Z3950_options options)
126 Z3950_connection c = xmalloc (sizeof(*c));
130 c->state = STATE_IDLE;
131 c->error = Z3950_ERROR_NONE;
139 c->options = Z3950_options_create_with_parent(options);
148 c->odr_in = odr_createmem (ODR_DECODE);
149 c->odr_out = odr_createmem (ODR_ENCODE);
153 c->m_queue_front = 0;
158 /* set database names. Take local databases (if set); otherwise
159 take databases given in ZURL (if set); otherwise use Default */
160 static char **set_DatabaseNames (Z3950_connection con, int *num)
162 char **databaseNames;
165 const char *cp = Z3950_options_get (con->options, "databaseName");
169 cp = strchr (con->host_port, '/');
176 while ((c = strchr(c, '+')))
184 databaseNames = odr_malloc (con->odr_out, no * sizeof(*databaseNames));
188 c = strchr (cp, '+');
196 /* cp ptr to first char of db name, c is char
198 databaseNames[no] = odr_malloc (con->odr_out, 1+c-cp);
199 memcpy (databaseNames[no], cp, c-cp);
200 databaseNames[no++][c-cp] = '\0';
205 databaseNames[no] = NULL;
207 return databaseNames;
210 Z3950_connection Z3950_connection_new (const char *host, int portnum)
212 Z3950_connection c = Z3950_connection_create (0);
214 Z3950_connection_connect (c, host, portnum);
218 void Z3950_connection_connect(Z3950_connection c,
219 const char *host, int portnum)
224 val = Z3950_options_get (c->options, "proxy");
226 c->proxy = xstrdup (val);
233 sprintf (hostn, "%.80s:%d", host, portnum);
234 c->host_port = xstrdup(hostn);
237 c->host_port = xstrdup(host);
239 c->async = Z3950_options_get_bool (c->options, "async", 0);
241 task = Z3950_connection_add_task (c, Z3950_TASK_CONNECT);
245 while (Z3950_event (1, &c))
250 Z3950_query Z3950_query_create(void)
252 Z3950_query s = xmalloc (sizeof(*s));
257 s->odr = odr_createmem (ODR_ENCODE);
262 const char *Z3950_connection_host (Z3950_connection c)
267 void Z3950_query_destroy(Z3950_query s)
273 yaz_log (LOG_DEBUG, "Z3950_query_destroy count=%d", s->refcount);
274 if (s->refcount == 0)
276 odr_destroy (s->odr);
281 int Z3950_query_prefix(Z3950_query s, const char *str)
283 s->query = odr_malloc (s->odr, sizeof(*s->query));
284 s->query->which = Z_Query_type_1;
285 s->query->u.type_1 = p_query_rpn(s->odr, PROTO_Z3950, str);
286 if (!s->query->u.type_1)
291 int Z3950_query_sortby(Z3950_query s, const char *criteria)
293 s->sort_spec = yaz_sort_spec (s->odr, criteria);
299 static int do_write(Z3950_connection c);
301 void Z3950_connection_destroy(Z3950_connection c)
308 for (r = c->resultsets; r; r = r->next)
313 odr_destroy (c->odr_in);
314 odr_destroy (c->odr_out);
315 Z3950_options_destroy (c->options);
316 Z3950_connection_remove_tasks (c);
317 xfree (c->host_port);
321 void Z3950_resultset_addref (Z3950_resultset r)
326 Z3950_resultset Z3950_resultset_create ()
328 Z3950_resultset r = xmalloc (sizeof(*r));
332 r->odr = odr_createmem (ODR_ENCODE);
345 Z3950_resultset Z3950_connection_search_pqf(Z3950_connection c, const char *q)
348 Z3950_query s = Z3950_query_create();
350 Z3950_query_prefix (s, q);
352 r = Z3950_connection_search (c, s);
353 Z3950_query_destroy (s);
357 Z3950_resultset Z3950_connection_search(Z3950_connection c, Z3950_query q)
359 Z3950_resultset r = Z3950_resultset_create ();
362 r->r_sort_spec = q->sort_spec;
363 r->r_query = q->query;
366 r->options = Z3950_options_create_with_parent(c->options);
368 r->start = Z3950_options_get_int(r->options, "start", 0);
369 r->count = Z3950_options_get_int(r->options, "count", 0);
370 r->piggyback = Z3950_options_get_bool (r->options, "piggyback", 1);
373 r->next = c->resultsets;
376 task = Z3950_connection_add_task (c, Z3950_TASK_SEARCH);
377 task->u.resultset = r;
378 Z3950_resultset_addref (r);
384 while (Z3950_event (1, &c))
390 void Z3950_resultset_destroy(Z3950_resultset r)
395 yaz_log (LOG_DEBUG, "destroy r = %p count=%d", r, r->refcount);
396 if (r->refcount == 0)
400 /* remove ourselves from the resultsets in connection */
401 Z3950_resultset *rp = &r->connection->resultsets;
404 assert (*rp); /* we must be in this list!! */
406 { /* OK, we're here - take us out of it */
413 Z3950_query_destroy (r->search);
414 Z3950_options_destroy (r->options);
415 odr_destroy (r->odr);
420 size_t Z3950_resultset_size (Z3950_resultset r)
425 static void do_close (Z3950_connection c)
431 c->state = STATE_IDLE;
434 static void Z3950_resultset_retrieve (Z3950_resultset r,
435 int force_sync, int start, int count)
445 task = Z3950_connection_add_task (c, Z3950_TASK_RETRIEVE);
446 task->u.resultset = r;
447 Z3950_resultset_addref (r);
452 if (!r->connection->async || force_sync)
453 while (r->connection && Z3950_event (1, &r->connection))
457 void Z3950_resultset_records (Z3950_resultset r, Z3950_record *recs,
458 size_t start, size_t count)
460 int force_present = 0;
466 Z3950_resultset_retrieve (r, force_present, start, count);
470 for (i = 0; i< count; i++)
471 recs[i] = Z3950_resultset_record_immediate (r, i+start);
475 static int do_connect (Z3950_connection c)
478 const char *effective_host;
481 effective_host = c->proxy;
483 effective_host = c->host_port;
485 yaz_log (LOG_DEBUG, "do_connect host=%s", effective_host);
488 c->cs = cs_create_host (effective_host, 0, &add);
492 int ret = cs_connect (c->cs, add);
493 yaz_log (LOG_DEBUG, "cs_connect returned %d", ret);
496 c->state = STATE_CONNECTING;
497 c->mask = Z3950_SELECT_READ | Z3950_SELECT_WRITE |
502 c->state = STATE_IDLE;
503 c->error = Z3950_ERROR_CONNECT;
507 int z3950_connection_socket(Z3950_connection c)
510 return cs_fileno(c->cs);
514 int z3950_connection_mask(Z3950_connection c)
521 static int encode_APDU(Z3950_connection c, Z_APDU *a, ODR out)
526 sprintf (str, "send_APDU t=%p type=%d", c, a->which);
529 Z_OtherInformation **oi;
531 yaz_oi_set_string_oidval(oi, out, VAL_COOKIE, 1, c->cookie_out);
533 if (!z_APDU(out, &a, 0, 0))
535 FILE *outf = fopen("/tmp/apdu.txt", "w");
538 ODR odr_pr = odr_createmem(ODR_PRINT);
539 fprintf (outf, "a=%p\n", a);
540 odr_setprint(odr_pr, outf);
541 z_APDU(odr_pr, &a, 0, 0);
545 c->error = Z3950_ERROR_ENCODE;
552 static int send_APDU (Z3950_connection c, Z_APDU *a)
555 if (encode_APDU(c, a, c->odr_out))
557 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
558 odr_reset(c->odr_out);
563 static int Z3950_connection_send_init (Z3950_connection c)
566 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest);
567 Z_InitRequest *ireq = apdu->u.initRequest;
568 Z_IdAuthentication *auth = odr_malloc(c->odr_out, sizeof(*auth));
569 const char *auth_groupId = Z3950_options_get (c->options, "group");
570 const char *auth_userId = Z3950_options_get (c->options, "user");
571 const char *auth_password = Z3950_options_get (c->options, "pass");
573 ODR_MASK_SET(ireq->options, Z_Options_search);
574 ODR_MASK_SET(ireq->options, Z_Options_present);
575 ODR_MASK_SET(ireq->options, Z_Options_scan);
576 ODR_MASK_SET(ireq->options, Z_Options_sort);
578 ODR_MASK_SET(ireq->options, Z_Options_extendedServices);
579 ODR_MASK_SET(ireq->options, Z_Options_namedResultSets);
582 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
583 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
584 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
586 impname = Z3950_options_get (c->options, "implementationName");
587 ireq->implementationName =
588 odr_malloc (c->odr_out, 15 + (impname ? strlen(impname) : 0));
589 strcpy (ireq->implementationName, "");
592 strcat (ireq->implementationName, impname);
593 strcat (ireq->implementationName, "/");
595 strcat (ireq->implementationName, "ZOOM-C/YAZ");
597 *ireq->maximumRecordSize =
598 Z3950_options_get_int (c->options, "maximumRecordSize", 1024*1024);
599 *ireq->preferredMessageSize =
600 Z3950_options_get_int (c->options, "preferredMessageSize", 1024*1024);
602 if (auth_groupId || auth_password)
604 Z_IdPass *pass = odr_malloc(c->odr_out, sizeof(*pass));
607 if (auth_groupId && *auth_groupId)
609 pass->groupId = odr_malloc(c->odr_out, strlen(auth_groupId)+1);
610 strcpy(pass->groupId, auth_groupId);
614 if (auth_userId && *auth_userId)
616 pass->userId = odr_malloc(c->odr_out, strlen(auth_userId)+1);
617 strcpy(pass->userId, auth_userId);
621 if (auth_password && *auth_password)
623 pass->password = odr_malloc(c->odr_out, strlen(auth_password)+1);
624 strcpy(pass->password, auth_password);
629 auth->which = Z_IdAuthentication_idPass;
630 auth->u.idPass = pass;
631 ireq->idAuthentication = auth;
634 else if (auth_userId)
636 auth->which = Z_IdAuthentication_open;
637 auth->u.open = odr_malloc(c->odr_out, strlen(auth_userId)+1);
638 strcpy(auth->u.open, auth_userId);
639 ireq->idAuthentication = auth;
642 yaz_oi_set_string_oidval(&ireq->otherInfo, c->odr_out,
643 VAL_PROXY, 1, c->host_port);
650 static int Z3950_connection_send_search (Z3950_connection c)
653 int lslb, ssub, mspn;
655 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
656 Z_SearchRequest *search_req = apdu->u.searchRequest;
657 const char *elementSetName;
658 const char *smallSetElementSetName;
659 const char *mediumSetElementSetName;
662 assert (c->tasks->which == Z3950_TASK_SEARCH);
664 r = c->tasks->u.resultset;
667 Z3950_options_get (r->options, "elementSetName");
668 smallSetElementSetName =
669 Z3950_options_get (r->options, "smallSetElementSetName");
670 mediumSetElementSetName =
671 Z3950_options_get (r->options, "mediumSetElementSetName");
673 if (!smallSetElementSetName)
674 smallSetElementSetName = elementSetName;
676 if (!mediumSetElementSetName)
677 mediumSetElementSetName = elementSetName;
682 /* prepare query for the search request */
683 search_req->query = r->r_query;
685 search_req->databaseNames =
686 set_DatabaseNames (c, &search_req->num_databaseNames);
688 /* get syntax (no need to provide unless piggyback is in effect) */
689 syntax = Z3950_options_get (r->options, "preferredRecordSyntax");
691 lslb = Z3950_options_get_int (r->options, "largeSetLowerBound", -1);
692 ssub = Z3950_options_get_int (r->options, "smallSetUpperBound", -1);
693 mspn = Z3950_options_get_int (r->options, "mediumSetPresentNumber", -1);
694 if (lslb != -1 && ssub != -1 && mspn != -1)
696 /* So're a Z39.50 expert? Let's hope you don't do sort */
697 *search_req->largeSetLowerBound = lslb;
698 *search_req->smallSetUpperBound = ssub;
699 *search_req->mediumSetPresentNumber = mspn;
701 else if (r->start == 0 && r->count > 0
702 && r->piggyback && !r->r_sort_spec)
704 /* Regular piggyback - do it unless we're going to do sort */
705 *search_req->largeSetLowerBound = 2000000000;
706 *search_req->smallSetUpperBound = r->count;
707 *search_req->mediumSetPresentNumber = r->count;
708 smallSetElementSetName = 0; /* no need to provide this */
712 /* non-piggyback. Need not provide elementsets or syntaxes .. */
713 smallSetElementSetName = 0;
714 mediumSetElementSetName = 0;
717 if (smallSetElementSetName && *smallSetElementSetName)
719 Z_ElementSetNames *esn = odr_malloc (c->odr_out, sizeof(*esn));
721 esn->which = Z_ElementSetNames_generic;
722 esn->u.generic = odr_strdup (c->odr_out, smallSetElementSetName);
723 search_req->smallSetElementSetNames = esn;
725 if (mediumSetElementSetName && *mediumSetElementSetName)
727 Z_ElementSetNames *esn = odr_malloc (c->odr_out, sizeof(*esn));
729 esn->which = Z_ElementSetNames_generic;
730 esn->u.generic = odr_strdup (c->odr_out, mediumSetElementSetName);
731 search_req->mediumSetElementSetNames = esn;
734 search_req->preferredRecordSyntax =
735 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
737 /* send search request */
743 static void response_diag (Z3950_connection c, Z_DiagRec *p)
745 Z_DefaultDiagFormat *r;
750 if (p->which != Z_DiagRec_defaultFormat)
752 c->error = Z3950_ERROR_DECODE;
755 r = p->u.defaultFormat;
758 case Z_DefaultDiagFormat_v2Addinfo:
759 addinfo = r->u.v2Addinfo;
761 case Z_DefaultDiagFormat_v3Addinfo:
762 addinfo = r->u.v3Addinfo;
766 c->addinfo = xstrdup (addinfo);
767 c->error = *r->condition;
770 Z3950_record Z3950_record_dup (const Z3950_record srec)
777 odr_enc = odr_createmem(ODR_ENCODE);
778 if (!z_NamePlusRecord (odr_enc, &srec->npr, 0, 0))
780 buf = odr_getbuf (odr_enc, &size, 0);
782 nrec = xmalloc (sizeof(*nrec));
783 nrec->odr = odr_createmem(ODR_DECODE);
784 nrec->wrbuf_marc = 0;
785 odr_setbuf (nrec->odr, buf, size, 0);
786 z_NamePlusRecord (nrec->odr, &nrec->npr, 0, 0);
788 odr_destroy (odr_enc);
792 Z3950_record Z3950_resultset_record_immediate (Z3950_resultset s,size_t pos)
794 return record_cache_lookup (s, pos, 0);
797 Z3950_record Z3950_resultset_record (Z3950_resultset r, size_t pos)
799 Z3950_resultset_retrieve (r, 1, pos, 1);
800 return Z3950_resultset_record_immediate (r, pos);
803 void Z3950_record_destroy (Z3950_record rec)
808 wrbuf_free (rec->wrbuf_marc, 1);
809 odr_destroy (rec->odr);
813 void *Z3950_record_get (Z3950_record rec, const char *type, size_t *len)
815 Z_NamePlusRecord *npr;
821 if (!strcmp (type, "database"))
823 return npr->databaseName;
825 else if (!strcmp (type, "syntax"))
827 if (npr->which == Z_NamePlusRecord_databaseRecord)
829 Z_External *r = (Z_External *) npr->u.databaseRecord;
830 oident *ent = oid_getentbyoid(r->direct_reference);
836 else if (!strcmp (type, "render"))
838 if (npr->which == Z_NamePlusRecord_databaseRecord)
840 Z_External *r = (Z_External *) npr->u.databaseRecord;
841 oident *ent = oid_getentbyoid(r->direct_reference);
843 if (r->which == Z_External_sutrs)
845 *len = r->u.sutrs->len;
846 return r->u.sutrs->buf;
848 else if (r->which == Z_External_octet)
857 case VAL_APPLICATION_XML:
860 if (!rec->wrbuf_marc)
861 rec->wrbuf_marc = wrbuf_alloc();
862 if (marc_display_wrbuf (r->u.octet_aligned->buf,
864 r->u.octet_aligned->len) > 0)
866 *len = wrbuf_len(rec->wrbuf_marc);
867 return wrbuf_buf(rec->wrbuf_marc);
870 *len = r->u.octet_aligned->len;
871 return r->u.octet_aligned->buf;
873 else if (r->which == Z_External_grs1)
881 else if (!strcmp (type, "raw"))
883 if (npr->which == Z_NamePlusRecord_databaseRecord)
886 return (Z_External *) npr->u.databaseRecord;
893 static void record_cache_add (Z3950_resultset r,
894 Z_NamePlusRecord *npr,
896 const char *elementSetName)
898 Z3950_record_cache rc;
900 for (rc = r->record_cache; rc; rc = rc->next)
904 if ((!elementSetName && !rc->elementSetName)
905 || (elementSetName && rc->elementSetName &&
906 !strcmp (elementSetName, rc->elementSetName)))
908 /* not destroying rc->npr (it's handled by nmem )*/
910 /* keeping wrbuf_marc too */
916 rc = odr_malloc (r->odr, sizeof(*rc));
919 rc->rec.wrbuf_marc = 0;
921 rc->elementSetName = odr_strdup (r->odr, elementSetName);
923 rc->elementSetName = 0;
925 rc->next = r->record_cache;
926 r->record_cache = rc;
929 static Z3950_record record_cache_lookup (Z3950_resultset r,
931 const char *elementSetName)
933 Z3950_record_cache rc;
935 for (rc = r->record_cache; rc; rc = rc->next)
939 if ((!elementSetName && !rc->elementSetName)
940 || (elementSetName && rc->elementSetName &&
941 !strcmp (elementSetName, rc->elementSetName)))
948 static void handle_records (Z3950_connection c, Z_Records *sr,
951 Z3950_resultset resultset;
955 if (c->tasks->which != Z3950_TASK_SEARCH &&
956 c->tasks->which != Z3950_TASK_RETRIEVE)
959 resultset = c->tasks->u.resultset;
961 if (sr && sr->which == Z_Records_NSD)
963 Z_DiagRec dr, *dr_p = &dr;
964 dr.which = Z_DiagRec_defaultFormat;
965 dr.u.defaultFormat = sr->u.nonSurrogateDiagnostic;
967 response_diag (c, dr_p);
969 else if (sr && sr->which == Z_Records_multipleNSD)
971 if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
972 response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
974 c->error = Z3950_ERROR_DECODE;
978 if (resultset->count + resultset->start > resultset->size)
979 resultset->count = resultset->size - resultset->start;
980 if (resultset->count < 0)
981 resultset->count = 0;
982 if (sr && sr->which == Z_Records_DBOSD)
985 NMEM nmem = odr_extract_mem (c->odr_in);
986 Z_NamePlusRecordList *p =
987 sr->u.databaseOrSurDiagnostics;
988 for (i = 0; i<p->num_records; i++)
990 record_cache_add (resultset, p->records[i],
991 i+ resultset->start, 0);
993 /* transfer our response to search_nmem .. we need it later */
994 nmem_transfer (resultset->odr->mem, nmem);
996 if (present_phase && p->num_records == 0)
998 /* present response and we didn't get any records! */
999 c->error = Z3950_ERROR_DECODE;
1002 else if (present_phase)
1004 /* present response and we didn't get any records! */
1005 c->error = Z3950_ERROR_DECODE;
1010 static void handle_present_response (Z3950_connection c, Z_PresentResponse *pr)
1012 handle_records (c, pr->records, 1);
1015 static void handle_search_response (Z3950_connection c, Z_SearchResponse *sr)
1017 Z3950_resultset resultset;
1019 yaz_log (LOG_DEBUG, "got search response");
1021 if (!c->tasks || c->tasks->which != Z3950_TASK_SEARCH)
1024 resultset = c->tasks->u.resultset;
1026 resultset->size = *sr->resultCount;
1027 handle_records (c, sr->records, 0);
1030 static void sort_response (Z3950_connection c, Z_SortResponse *res)
1032 if (res->diagnostics && res->num_diagnostics > 0)
1033 response_diag (c, res->diagnostics[0]);
1036 static int send_sort (Z3950_connection c)
1038 Z3950_resultset resultset;
1040 if (!c->tasks || c->tasks->which != Z3950_TASK_SEARCH)
1043 resultset = c->tasks->u.resultset;
1047 resultset->r_sort_spec = 0;
1050 if (resultset->r_sort_spec)
1052 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
1053 Z_SortRequest *req = apdu->u.sortRequest;
1055 req->num_inputResultSetNames = 1;
1056 req->inputResultSetNames = (Z_InternationalString **)
1057 odr_malloc (c->odr_out, sizeof(*req->inputResultSetNames));
1058 req->inputResultSetNames[0] = odr_strdup (c->odr_out, "default");
1059 req->sortedResultSetName = odr_strdup (c->odr_out, "default");
1060 req->sortSequence = resultset->r_sort_spec;
1061 resultset->r_sort_spec = 0;
1062 send_APDU (c, apdu);
1068 static int send_present (Z3950_connection c)
1070 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
1071 Z_PresentRequest *req = apdu->u.presentRequest;
1073 const char *syntax =
1074 Z3950_options_get (c->options, "preferredRecordSyntax");
1075 const char *element =
1076 Z3950_options_get (c->options, "elementSetName");
1077 Z3950_resultset resultset;
1081 if (c->tasks->which != Z3950_TASK_SEARCH &&
1082 c->tasks->which != Z3950_TASK_RETRIEVE)
1085 resultset = c->tasks->u.resultset;
1087 if (c->error) /* don't continue on error */
1089 if (resultset->start < 0)
1091 for (i = 0; i<resultset->count; i++)
1094 record_cache_lookup (resultset, i + resultset->start, 0);
1098 if (i == resultset->count)
1101 resultset->start += i;
1102 resultset->count -= i;
1103 *req->resultSetStartPoint = resultset->start + 1;
1104 *req->numberOfRecordsRequested = resultset->count;
1105 assert (*req->numberOfRecordsRequested > 0);
1107 if (syntax && *syntax)
1108 req->preferredRecordSyntax =
1109 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
1111 if (element && *element)
1113 Z_ElementSetNames *esn = odr_malloc (c->odr_out, sizeof(*esn));
1114 Z_RecordComposition *compo = odr_malloc (c->odr_out, sizeof(*compo));
1116 esn->which = Z_ElementSetNames_generic;
1117 esn->u.generic = odr_strdup (c->odr_out, element);
1118 compo->which = Z_RecordComp_simple;
1119 compo->u.simple = esn;
1120 req->recordComposition = compo;
1122 send_APDU (c, apdu);
1126 static int Z3950_connection_exec_task (Z3950_connection c)
1128 Z3950_task task = c->tasks;
1130 yaz_log (LOG_LOG, "Z3950_connection_exec_task");
1133 if (c->error != Z3950_ERROR_NONE ||
1134 (!c->cs && task->which != Z3950_TASK_CONNECT))
1136 Z3950_connection_remove_tasks (c);
1139 yaz_log (LOG_DEBUG, "Z3950_connection_exec_task type=%d", task->which);
1143 switch (task->which)
1145 case Z3950_TASK_SEARCH:
1146 /* see if search hasn't been sent yet. */
1147 if (Z3950_connection_send_search (c))
1150 case Z3950_TASK_RETRIEVE:
1151 if (send_present (c))
1154 case Z3950_TASK_CONNECT:
1158 Z3950_connection_remove_task (c);
1162 static int send_sort_present (Z3950_connection c)
1164 int r = send_sort (c);
1166 r = send_present (c);
1170 static void handle_apdu (Z3950_connection c, Z_APDU *apdu)
1172 Z_InitResponse *initrs;
1174 yaz_log (LOG_DEBUG, "hande_apdu type=%d", apdu->which);
1178 case Z_APDU_initResponse:
1179 initrs = apdu->u.initResponse;
1180 if (!*initrs->result)
1182 c->error = Z3950_ERROR_INIT;
1187 yaz_oi_get_string_oidval (&apdu->u.initResponse->otherInfo,
1189 xfree (c->cookie_in);
1192 c->cookie_in = xstrdup(cookie);
1195 assert (c->tasks->which == Z3950_TASK_CONNECT);
1196 Z3950_connection_remove_task (c);
1198 Z3950_connection_exec_task (c);
1201 case Z_APDU_searchResponse:
1202 handle_search_response (c, apdu->u.searchResponse);
1203 if (!send_sort_present (c))
1204 Z3950_connection_remove_task (c);
1206 case Z_APDU_presentResponse:
1207 handle_present_response (c, apdu->u.presentResponse);
1208 if (!send_present (c))
1209 Z3950_connection_remove_task (c);
1211 case Z_APDU_sortResponse:
1212 sort_response (c, apdu->u.sortResponse);
1213 if (!send_present (c))
1214 Z3950_connection_remove_task (c);
1218 static int do_read (Z3950_connection c)
1223 r = cs_get (c->cs, &c->buf_in, &c->len_in);
1228 c->error= Z3950_ERROR_CONNECTION_LOST;
1233 odr_reset (c->odr_in);
1234 odr_setbuf (c->odr_in, c->buf_in, r, 0);
1235 if (!z_APDU (c->odr_in, &apdu, 0, 0))
1237 c->error = Z3950_ERROR_DECODE;
1242 handle_apdu (c, apdu);
1248 static int do_write_ex (Z3950_connection c, char *buf_out, int len_out)
1252 if ((r=cs_put (c->cs, buf_out, len_out)) < 0)
1254 if (c->state == STATE_CONNECTING)
1255 c->error = Z3950_ERROR_CONNECT;
1257 c->error = Z3950_ERROR_CONNECTION_LOST;
1263 c->state = STATE_ESTABLISHED;
1264 c->mask = Z3950_SELECT_READ|Z3950_SELECT_WRITE|Z3950_SELECT_EXCEPT;
1268 c->state = STATE_ESTABLISHED;
1269 c->mask = Z3950_SELECT_READ|Z3950_SELECT_EXCEPT;
1274 static int do_write(Z3950_connection c)
1276 return do_write_ex (c, c->buf_out, c->len_out);
1279 const char *Z3950_connection_option (Z3950_connection c, const char *key,
1284 Z3950_options_set (c->options, key, val);
1287 return Z3950_options_get (c->options, key);
1290 const char *Z3950_resultset_option (Z3950_resultset r, const char *key,
1295 Z3950_options_set (r->options, key, val);
1298 return Z3950_options_get (r->options, key);
1302 int Z3950_connection_errcode (Z3950_connection c)
1304 return Z3950_connection_error (c, 0, 0);
1307 const char *Z3950_connection_errmsg (Z3950_connection c)
1310 Z3950_connection_error (c, &msg, 0);
1314 const char *Z3950_connection_addinfo (Z3950_connection c)
1316 const char *addinfo;
1317 Z3950_connection_error (c, 0, &addinfo);
1321 int Z3950_connection_error (Z3950_connection c, const char **cp,
1322 const char **addinfo)
1324 int error = c->error;
1329 case Z3950_ERROR_NONE:
1330 *cp = "No error"; break;
1331 case Z3950_ERROR_CONNECT:
1332 *cp = "Connect failed"; break;
1333 case Z3950_ERROR_MEMORY:
1334 *cp = "Out of memory"; break;
1335 case Z3950_ERROR_ENCODE:
1336 *cp = "Encoding failed"; break;
1337 case Z3950_ERROR_DECODE:
1338 *cp = "Decoding failed"; break;
1339 case Z3950_ERROR_CONNECTION_LOST:
1340 *cp = "Connection lost"; break;
1341 case Z3950_ERROR_INIT:
1342 *cp = "Init rejected"; break;
1343 case Z3950_ERROR_INTERNAL:
1344 *cp = "Internal failure"; break;
1345 case Z3950_ERROR_TIMEOUT:
1346 *cp = "Timeout"; break;
1348 *cp = diagbib1_str (error);
1354 *addinfo = c->addinfo;
1361 int Z3950_connection_do_io(Z3950_connection c, int mask)
1365 int r = cs_look(c->cs);
1366 yaz_log (LOG_LOG, "Z3950_connection_do_io c=%p mask=%d cs_look=%d",
1371 c->error = Z3950_ERROR_CONNECT;
1374 else if (r == CS_CONNECT)
1376 yaz_log (LOG_LOG, "calling rcvconnect");
1377 if (cs_rcvconnect (c->cs) < 0)
1379 c->error = Z3950_ERROR_CONNECT;
1383 Z3950_connection_send_init (c);
1387 if (mask & Z3950_SELECT_READ)
1389 if (c->cs && (mask & Z3950_SELECT_WRITE))
1393 yaz_log (LOG_DEBUG, "Z3950_connection_do_io c=%p mask=%d", c, mask);
1394 if (c->state == STATE_CONNECTING)
1396 if (mask & Z3950_SELECT_WRITE)
1397 Z3950_connection_send_init (c);
1400 c->error = Z3950_ERROR_CONNECT;
1404 else if (c->state == STATE_ESTABLISHED)
1406 if (mask & Z3950_SELECT_READ)
1408 if (c->cs && (mask & Z3950_SELECT_WRITE))
1413 c->error = Z3950_ERROR_INTERNAL;
1417 event = Z3950_Event_create (1);
1418 Z3950_connection_put_event (c, event);
1423 int Z3950_event (int no, Z3950_connection *cs)
1426 struct pollfd pollfds[1024];
1427 Z3950_connection poll_cs[1024];
1430 fd_set input, output, except;
1435 for (i = 0; i<no; i++)
1437 Z3950_connection c = cs[i];
1439 if (c && (event = Z3950_connection_get_event(c)))
1441 Z3950_Event_destroy (event);
1445 for (i = 0; i<no; i++)
1447 Z3950_connection c = cs[i];
1448 if (c && Z3950_connection_exec_task (c))
1462 for (i = 0; i<no; i++)
1464 Z3950_connection c = cs[i];
1469 fd = z3950_connection_socket(c);
1470 mask = z3950_connection_mask(c);
1480 short poll_events = 0;
1482 if (mask & Z3950_SELECT_READ)
1483 poll_events += POLLIN;
1484 if (mask & Z3950_SELECT_WRITE)
1485 poll_events += POLLOUT;
1486 if (mask & Z3950_SELECT_EXCEPT)
1487 poll_events += POLLERR;
1488 pollfds[nfds].fd = fd;
1489 pollfds[nfds].events = poll_events;
1490 pollfds[nfds].revents = 0;
1495 if (mask & Z3950_SELECT_READ)
1497 FD_SET (fd, &input);
1500 if (mask & Z3950_SELECT_WRITE)
1502 FD_SET (fd, &output);
1505 if (mask & Z3950_SELECT_EXCEPT)
1507 FD_SET (fd, &except);
1515 yaz_log (LOG_LOG, "poll start");
1516 r = poll (pollfds, nfds, 15000);
1517 yaz_log (LOG_LOG, "poll stop, returned r=%d", r);
1518 for (i = 0; i<nfds; i++)
1520 Z3950_connection c = poll_cs[i];
1524 if (pollfds[i].revents & POLLIN)
1525 mask += Z3950_SELECT_READ;
1526 if (pollfds[i].revents & POLLOUT)
1527 mask += Z3950_SELECT_WRITE;
1528 if (pollfds[i].revents & POLLERR)
1529 mask += Z3950_SELECT_EXCEPT;
1531 Z3950_connection_do_io(c, mask);
1533 else if (r == 0 && c->mask)
1535 Z3950_Event event = Z3950_Event_create(0);
1536 /* timeout and this connection was waiting */
1537 c->error = Z3950_ERROR_TIMEOUT;
1539 Z3950_connection_put_event(c, event);
1543 yaz_log (LOG_DEBUG, "select start");
1544 r = select (max_fd+1, &input, &output, &except, &tv);
1545 yaz_log (LOG_DEBUG, "select stop, returned r=%d", r);
1546 for (i = 0; i<no; i++)
1548 Z3950_connection c = cs[i];
1553 fd = z3950_connection_socket(c);
1557 /* no timeout and real socket */
1558 if (FD_ISSET(fd, &input))
1559 mask += Z3950_SELECT_READ;
1560 if (FD_ISSET(fd, &output))
1561 mask += Z3950_SELECT_WRITE;
1562 if (FD_ISSET(fd, &except))
1563 mask += Z3950_SELECT_EXCEPT;
1565 Z3950_connection_do_io(c, mask);
1567 if (r == 0 && c->mask)
1569 Z3950_Event event = Z3950_Event_create(0);
1570 /* timeout and this connection was waiting */
1571 c->error = Z3950_ERROR_TIMEOUT;
1573 yaz_log (LOG_LOG, "timeout");
1574 Z3950_connection_put_event(c, event);
1578 for (i = 0; i<no; i++)
1580 Z3950_connection c = cs[i];
1582 if (c && (event = Z3950_connection_get_event(c)))
1584 Z3950_Event_destroy (event);