2 * $Id: zoom-c.c,v 1.5 2001-11-13 22:57:03 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 int 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 (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 Z3950_record rec = record_cache_lookup (s, pos, 0);
797 return Z3950_record_dup (rec);
800 Z3950_record Z3950_resultset_record (Z3950_resultset r, size_t pos)
802 Z3950_resultset_retrieve (r, 1, pos, 1);
803 return Z3950_resultset_record_immediate (r, pos);
806 void Z3950_record_destroy (Z3950_record rec)
811 wrbuf_free (rec->wrbuf_marc, 1);
812 odr_destroy (rec->odr);
816 void *Z3950_record_get (Z3950_record rec, const char *type, size_t *len)
818 Z_NamePlusRecord *npr;
824 if (!strcmp (type, "database"))
826 return npr->databaseName;
828 else if (!strcmp (type, "syntax"))
830 if (npr->which == Z_NamePlusRecord_databaseRecord)
832 Z_External *r = (Z_External *) npr->u.databaseRecord;
833 oident *ent = oid_getentbyoid(r->direct_reference);
839 else if (!strcmp (type, "render"))
841 if (npr->which == Z_NamePlusRecord_databaseRecord)
843 Z_External *r = (Z_External *) npr->u.databaseRecord;
844 oident *ent = oid_getentbyoid(r->direct_reference);
846 if (r->which == Z_External_sutrs)
848 *len = r->u.sutrs->len;
849 return r->u.sutrs->buf;
851 else if (r->which == Z_External_octet)
860 case VAL_APPLICATION_XML:
863 if (!rec->wrbuf_marc)
864 rec->wrbuf_marc = wrbuf_alloc();
865 if (marc_display_wrbuf (r->u.octet_aligned->buf,
867 r->u.octet_aligned->len) > 0)
869 *len = wrbuf_len(rec->wrbuf_marc);
870 return wrbuf_buf(rec->wrbuf_marc);
873 *len = r->u.octet_aligned->len;
874 return r->u.octet_aligned->buf;
876 else if (r->which == Z_External_grs1)
884 else if (!strcmp (type, "raw"))
886 if (npr->which == Z_NamePlusRecord_databaseRecord)
889 return (Z_External *) npr->u.databaseRecord;
896 void *Z3950_resultset_get (Z3950_resultset s, size_t pos, const char *type,
899 Z3950_record rec = record_cache_lookup (s, pos, 0);
900 return Z3950_record_get (rec, type, len);
903 static void record_cache_add (Z3950_resultset r,
904 Z_NamePlusRecord *npr,
906 const char *elementSetName)
908 Z3950_record_cache rc;
910 for (rc = r->record_cache; rc; rc = rc->next)
914 if ((!elementSetName && !rc->elementSetName)
915 || (elementSetName && rc->elementSetName &&
916 !strcmp (elementSetName, rc->elementSetName)))
918 /* not destroying rc->npr (it's handled by nmem )*/
920 /* keeping wrbuf_marc too */
926 rc = odr_malloc (r->odr, sizeof(*rc));
929 rc->rec.wrbuf_marc = 0;
931 rc->elementSetName = odr_strdup (r->odr, elementSetName);
933 rc->elementSetName = 0;
935 rc->next = r->record_cache;
936 r->record_cache = rc;
939 static Z3950_record record_cache_lookup (Z3950_resultset r,
941 const char *elementSetName)
943 Z3950_record_cache rc;
945 for (rc = r->record_cache; rc; rc = rc->next)
949 if ((!elementSetName && !rc->elementSetName)
950 || (elementSetName && rc->elementSetName &&
951 !strcmp (elementSetName, rc->elementSetName)))
958 static void handle_records (Z3950_connection c, Z_Records *sr,
961 Z3950_resultset resultset;
965 if (c->tasks->which != Z3950_TASK_SEARCH &&
966 c->tasks->which != Z3950_TASK_RETRIEVE)
969 resultset = c->tasks->u.resultset;
971 if (sr && sr->which == Z_Records_NSD)
973 Z_DiagRec dr, *dr_p = &dr;
974 dr.which = Z_DiagRec_defaultFormat;
975 dr.u.defaultFormat = sr->u.nonSurrogateDiagnostic;
977 response_diag (c, dr_p);
979 else if (sr && sr->which == Z_Records_multipleNSD)
981 if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
982 response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
984 c->error = Z3950_ERROR_DECODE;
988 if (resultset->count + resultset->start > resultset->size)
989 resultset->count = resultset->size - resultset->start;
990 if (resultset->count < 0)
991 resultset->count = 0;
992 if (sr && sr->which == Z_Records_DBOSD)
995 NMEM nmem = odr_extract_mem (c->odr_in);
996 Z_NamePlusRecordList *p =
997 sr->u.databaseOrSurDiagnostics;
998 for (i = 0; i<p->num_records; i++)
1000 record_cache_add (resultset, p->records[i],
1001 i+ resultset->start, 0);
1003 /* transfer our response to search_nmem .. we need it later */
1004 nmem_transfer (resultset->odr->mem, nmem);
1005 nmem_destroy (nmem);
1006 if (present_phase && p->num_records == 0)
1008 /* present response and we didn't get any records! */
1009 c->error = Z3950_ERROR_DECODE;
1012 else if (present_phase)
1014 /* present response and we didn't get any records! */
1015 c->error = Z3950_ERROR_DECODE;
1020 static void handle_present_response (Z3950_connection c, Z_PresentResponse *pr)
1022 handle_records (c, pr->records, 1);
1025 static void handle_search_response (Z3950_connection c, Z_SearchResponse *sr)
1027 Z3950_resultset resultset;
1029 yaz_log (LOG_DEBUG, "got search response");
1031 if (!c->tasks || c->tasks->which != Z3950_TASK_SEARCH)
1034 resultset = c->tasks->u.resultset;
1036 resultset->size = *sr->resultCount;
1037 handle_records (c, sr->records, 0);
1040 static void sort_response (Z3950_connection c, Z_SortResponse *res)
1042 if (res->diagnostics && res->num_diagnostics > 0)
1043 response_diag (c, res->diagnostics[0]);
1046 static int send_sort (Z3950_connection c)
1048 Z3950_resultset resultset;
1050 if (!c->tasks || c->tasks->which != Z3950_TASK_SEARCH)
1053 resultset = c->tasks->u.resultset;
1057 resultset->r_sort_spec = 0;
1060 if (resultset->r_sort_spec)
1062 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
1063 Z_SortRequest *req = apdu->u.sortRequest;
1065 req->num_inputResultSetNames = 1;
1066 req->inputResultSetNames = (Z_InternationalString **)
1067 odr_malloc (c->odr_out, sizeof(*req->inputResultSetNames));
1068 req->inputResultSetNames[0] = odr_strdup (c->odr_out, "default");
1069 req->sortedResultSetName = odr_strdup (c->odr_out, "default");
1070 req->sortSequence = resultset->r_sort_spec;
1071 resultset->r_sort_spec = 0;
1072 send_APDU (c, apdu);
1078 static int send_present (Z3950_connection c)
1080 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
1081 Z_PresentRequest *req = apdu->u.presentRequest;
1083 const char *syntax =
1084 Z3950_options_get (c->options, "preferredRecordSyntax");
1085 const char *element =
1086 Z3950_options_get (c->options, "elementSetName");
1087 Z3950_resultset resultset;
1091 if (c->tasks->which != Z3950_TASK_SEARCH &&
1092 c->tasks->which != Z3950_TASK_RETRIEVE)
1095 resultset = c->tasks->u.resultset;
1097 if (c->error) /* don't continue on error */
1099 if (resultset->start < 0)
1101 for (i = 0; i<resultset->count; i++)
1104 record_cache_lookup (resultset, i + resultset->start, 0);
1108 if (i == resultset->count)
1111 resultset->start += i;
1112 resultset->count -= i;
1113 *req->resultSetStartPoint = resultset->start + 1;
1114 *req->numberOfRecordsRequested = resultset->count;
1115 assert (*req->numberOfRecordsRequested > 0);
1117 if (syntax && *syntax)
1118 req->preferredRecordSyntax =
1119 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
1121 if (element && *element)
1123 Z_ElementSetNames *esn = odr_malloc (c->odr_out, sizeof(*esn));
1124 Z_RecordComposition *compo = odr_malloc (c->odr_out, sizeof(*compo));
1126 esn->which = Z_ElementSetNames_generic;
1127 esn->u.generic = odr_strdup (c->odr_out, element);
1128 compo->which = Z_RecordComp_simple;
1129 compo->u.simple = esn;
1130 req->recordComposition = compo;
1132 send_APDU (c, apdu);
1136 static int Z3950_connection_exec_task (Z3950_connection c)
1138 Z3950_task task = c->tasks;
1140 yaz_log (LOG_LOG, "Z3950_connection_exec_task");
1143 if (c->error != Z3950_ERROR_NONE ||
1144 (!c->cs && task->which != Z3950_TASK_CONNECT))
1146 Z3950_connection_remove_tasks (c);
1149 yaz_log (LOG_DEBUG, "Z3950_connection_exec_task type=%d", task->which);
1153 switch (task->which)
1155 case Z3950_TASK_SEARCH:
1156 /* see if search hasn't been sent yet. */
1157 if (Z3950_connection_send_search (c))
1160 case Z3950_TASK_RETRIEVE:
1161 if (send_present (c))
1164 case Z3950_TASK_CONNECT:
1168 Z3950_connection_remove_task (c);
1172 static int send_sort_present (Z3950_connection c)
1174 int r = send_sort (c);
1176 r = send_present (c);
1180 static void handle_apdu (Z3950_connection c, Z_APDU *apdu)
1182 Z_InitResponse *initrs;
1184 yaz_log (LOG_DEBUG, "hande_apdu type=%d", apdu->which);
1188 case Z_APDU_initResponse:
1189 initrs = apdu->u.initResponse;
1190 if (!*initrs->result)
1192 c->error = Z3950_ERROR_INIT;
1197 yaz_oi_get_string_oidval (&apdu->u.initResponse->otherInfo,
1199 xfree (c->cookie_in);
1202 c->cookie_in = xstrdup(cookie);
1205 assert (c->tasks->which == Z3950_TASK_CONNECT);
1206 Z3950_connection_remove_task (c);
1208 Z3950_connection_exec_task (c);
1211 case Z_APDU_searchResponse:
1212 handle_search_response (c, apdu->u.searchResponse);
1213 if (!send_sort_present (c))
1214 Z3950_connection_remove_task (c);
1216 case Z_APDU_presentResponse:
1217 handle_present_response (c, apdu->u.presentResponse);
1218 if (!send_present (c))
1219 Z3950_connection_remove_task (c);
1221 case Z_APDU_sortResponse:
1222 sort_response (c, apdu->u.sortResponse);
1223 if (!send_present (c))
1224 Z3950_connection_remove_task (c);
1228 static int do_read (Z3950_connection c)
1233 r = cs_get (c->cs, &c->buf_in, &c->len_in);
1238 c->error= Z3950_ERROR_CONNECTION_LOST;
1243 odr_reset (c->odr_in);
1244 odr_setbuf (c->odr_in, c->buf_in, r, 0);
1245 if (!z_APDU (c->odr_in, &apdu, 0, 0))
1247 c->error = Z3950_ERROR_DECODE;
1252 handle_apdu (c, apdu);
1258 static int do_write_ex (Z3950_connection c, char *buf_out, int len_out)
1262 if ((r=cs_put (c->cs, buf_out, len_out)) < 0)
1264 if (c->state == STATE_CONNECTING)
1265 c->error = Z3950_ERROR_CONNECT;
1267 c->error = Z3950_ERROR_CONNECTION_LOST;
1273 c->state = STATE_ESTABLISHED;
1274 c->mask = Z3950_SELECT_READ|Z3950_SELECT_WRITE|Z3950_SELECT_EXCEPT;
1278 c->state = STATE_ESTABLISHED;
1279 c->mask = Z3950_SELECT_READ|Z3950_SELECT_EXCEPT;
1284 static int do_write(Z3950_connection c)
1286 return do_write_ex (c, c->buf_out, c->len_out);
1289 const char *Z3950_connection_option (Z3950_connection c, const char *key,
1294 Z3950_options_set (c->options, key, val);
1297 return Z3950_options_get (c->options, key);
1300 const char *Z3950_resultset_option (Z3950_resultset r, const char *key,
1305 Z3950_options_set (r->options, key, val);
1308 return Z3950_options_get (r->options, key);
1312 int Z3950_connection_errcode (Z3950_connection c)
1314 return Z3950_connection_error (c, 0, 0);
1317 const char *Z3950_connection_errmsg (Z3950_connection c)
1320 Z3950_connection_error (c, &msg, 0);
1324 const char *Z3950_connection_addinfo (Z3950_connection c)
1326 const char *addinfo;
1327 Z3950_connection_error (c, 0, &addinfo);
1331 int Z3950_connection_error (Z3950_connection c, const char **cp,
1332 const char **addinfo)
1334 int error = c->error;
1339 case Z3950_ERROR_NONE:
1340 *cp = "No error"; break;
1341 case Z3950_ERROR_CONNECT:
1342 *cp = "Connect failed"; break;
1343 case Z3950_ERROR_MEMORY:
1344 *cp = "Out of memory"; break;
1345 case Z3950_ERROR_ENCODE:
1346 *cp = "Encoding failed"; break;
1347 case Z3950_ERROR_DECODE:
1348 *cp = "Decoding failed"; break;
1349 case Z3950_ERROR_CONNECTION_LOST:
1350 *cp = "Connection lost"; break;
1351 case Z3950_ERROR_INIT:
1352 *cp = "Init rejected"; break;
1353 case Z3950_ERROR_INTERNAL:
1354 *cp = "Internal failure"; break;
1355 case Z3950_ERROR_TIMEOUT:
1356 *cp = "Timeout"; break;
1358 *cp = diagbib1_str (error);
1364 *addinfo = c->addinfo;
1371 int Z3950_connection_do_io(Z3950_connection c, int mask)
1375 int r = cs_look(c->cs);
1376 yaz_log (LOG_LOG, "Z3950_connection_do_io c=%p mask=%d cs_look=%d",
1381 c->error = Z3950_ERROR_CONNECT;
1384 else if (r == CS_CONNECT)
1386 yaz_log (LOG_LOG, "calling rcvconnect");
1387 if (cs_rcvconnect (c->cs) < 0)
1389 c->error = Z3950_ERROR_CONNECT;
1393 Z3950_connection_send_init (c);
1397 if (mask & Z3950_SELECT_READ)
1399 if (c->cs && (mask & Z3950_SELECT_WRITE))
1403 yaz_log (LOG_DEBUG, "Z3950_connection_do_io c=%p mask=%d", c, mask);
1404 if (c->state == STATE_CONNECTING)
1406 if (mask & Z3950_SELECT_WRITE)
1407 Z3950_connection_send_init (c);
1410 c->error = Z3950_ERROR_CONNECT;
1414 else if (c->state == STATE_ESTABLISHED)
1416 if (mask & Z3950_SELECT_READ)
1418 if (c->cs && (mask & Z3950_SELECT_WRITE))
1423 c->error = Z3950_ERROR_INTERNAL;
1427 event = Z3950_Event_create (1);
1428 Z3950_connection_put_event (c, event);
1433 int Z3950_event (int no, Z3950_connection *cs)
1436 struct pollfd pollfds[1024];
1437 Z3950_connection poll_cs[1024];
1440 fd_set input, output, except;
1445 for (i = 0; i<no; i++)
1447 Z3950_connection c = cs[i];
1449 if (c && (event = Z3950_connection_get_event(c)))
1451 Z3950_Event_destroy (event);
1455 for (i = 0; i<no; i++)
1457 Z3950_connection c = cs[i];
1458 if (c && Z3950_connection_exec_task (c))
1472 for (i = 0; i<no; i++)
1474 Z3950_connection c = cs[i];
1479 fd = z3950_connection_socket(c);
1480 mask = z3950_connection_mask(c);
1490 short poll_events = 0;
1492 if (mask & Z3950_SELECT_READ)
1493 poll_events += POLLIN;
1494 if (mask & Z3950_SELECT_WRITE)
1495 poll_events += POLLOUT;
1496 if (mask & Z3950_SELECT_EXCEPT)
1497 poll_events += POLLERR;
1498 pollfds[nfds].fd = fd;
1499 pollfds[nfds].events = poll_events;
1500 pollfds[nfds].revents = 0;
1505 if (mask & Z3950_SELECT_READ)
1507 FD_SET (fd, &input);
1510 if (mask & Z3950_SELECT_WRITE)
1512 FD_SET (fd, &output);
1515 if (mask & Z3950_SELECT_EXCEPT)
1517 FD_SET (fd, &except);
1525 yaz_log (LOG_LOG, "poll start");
1526 r = poll (pollfds, nfds, 15000);
1527 yaz_log (LOG_LOG, "poll stop, returned r=%d", r);
1528 for (i = 0; i<nfds; i++)
1530 Z3950_connection c = poll_cs[i];
1534 if (pollfds[i].revents & POLLIN)
1535 mask += Z3950_SELECT_READ;
1536 if (pollfds[i].revents & POLLOUT)
1537 mask += Z3950_SELECT_WRITE;
1538 if (pollfds[i].revents & POLLERR)
1539 mask += Z3950_SELECT_EXCEPT;
1541 Z3950_connection_do_io(c, mask);
1543 else if (r == 0 && c->mask)
1545 Z3950_Event event = Z3950_Event_create(0);
1546 /* timeout and this connection was waiting */
1547 c->error = Z3950_ERROR_TIMEOUT;
1549 Z3950_connection_put_event(c, event);
1553 yaz_log (LOG_DEBUG, "select start");
1554 r = select (max_fd+1, &input, &output, &except, &tv);
1555 yaz_log (LOG_DEBUG, "select stop, returned r=%d", r);
1556 for (i = 0; i<no; i++)
1558 Z3950_connection c = cs[i];
1563 fd = z3950_connection_socket(c);
1567 /* no timeout and real socket */
1568 if (FD_ISSET(fd, &input))
1569 mask += Z3950_SELECT_READ;
1570 if (FD_ISSET(fd, &output))
1571 mask += Z3950_SELECT_WRITE;
1572 if (FD_ISSET(fd, &except))
1573 mask += Z3950_SELECT_EXCEPT;
1575 Z3950_connection_do_io(c, mask);
1577 if (r == 0 && c->mask)
1579 Z3950_Event event = Z3950_Event_create(0);
1580 /* timeout and this connection was waiting */
1581 c->error = Z3950_ERROR_TIMEOUT;
1583 yaz_log (LOG_LOG, "timeout");
1584 Z3950_connection_put_event(c, event);
1588 for (i = 0; i<no; i++)
1590 Z3950_connection c = cs[i];
1592 if (c && (event = Z3950_connection_get_event(c)))
1594 Z3950_Event_destroy (event);