2 * $Id: zoom-c.c,v 1.36 2002-07-11 10:39:05 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>
12 #include <yaz/charneg.h>
21 static int ZOOM_connection_send_init (ZOOM_connection c);
22 static int do_write_ex (ZOOM_connection c, char *buf_out, int len_out);
24 static ZOOM_Event ZOOM_Event_create (int kind)
26 ZOOM_Event event = (ZOOM_Event) xmalloc (sizeof(*event));
33 static void ZOOM_Event_destroy (ZOOM_Event event)
38 static void ZOOM_connection_put_event (ZOOM_connection c, ZOOM_Event event)
42 c->m_queue_back->prev = event;
43 assert (c->m_queue_front);
47 assert (!c->m_queue_front);
48 c->m_queue_front = event;
50 event->next = c->m_queue_back;
52 c->m_queue_back = event;
55 static ZOOM_Event ZOOM_connection_get_event(ZOOM_connection c)
57 ZOOM_Event event = c->m_queue_front;
60 assert (c->m_queue_back);
61 c->m_queue_front = event->prev;
64 assert (c->m_queue_back);
65 c->m_queue_front->next = 0;
69 c->last_event = event->kind;
73 static void clear_error (ZOOM_connection c)
78 case ZOOM_ERROR_CONNECT:
79 case ZOOM_ERROR_MEMORY:
80 case ZOOM_ERROR_DECODE:
81 case ZOOM_ERROR_CONNECTION_LOST:
83 case ZOOM_ERROR_INTERNAL:
86 c->error = ZOOM_ERROR_NONE;
92 ZOOM_task ZOOM_connection_add_task (ZOOM_connection c, int which)
94 ZOOM_task *taskp = &c->tasks;
96 taskp = &(*taskp)->next;
97 *taskp = (ZOOM_task) xmalloc (sizeof(**taskp));
98 (*taskp)->running = 0;
99 (*taskp)->which = which;
105 ZOOM_task ZOOM_connection_insert_task (ZOOM_connection c, int which)
107 ZOOM_task task = (ZOOM_task) xmalloc (sizeof(*task));
109 task->next = c->tasks;
118 void ZOOM_connection_remove_task (ZOOM_connection c)
120 ZOOM_task task = c->tasks;
124 c->tasks = task->next;
127 case ZOOM_TASK_SEARCH:
128 ZOOM_resultset_destroy (task->u.search.resultset);
130 case ZOOM_TASK_RETRIEVE:
131 ZOOM_resultset_destroy (task->u.retrieve.resultset);
133 case ZOOM_TASK_CONNECT:
136 ZOOM_scanset_destroy (task->u.scan.scan);
138 case ZOOM_TASK_PACKAGE:
139 ZOOM_package_destroy (task->u.package);
148 void ZOOM_connection_remove_tasks (ZOOM_connection c)
151 ZOOM_connection_remove_task(c);
154 static ZOOM_record record_cache_lookup (ZOOM_resultset r,
156 const char *elementSetName);
158 ZOOM_API(ZOOM_connection)
159 ZOOM_connection_create (ZOOM_options options)
161 ZOOM_connection c = (ZOOM_connection) xmalloc (sizeof(*c));
166 c->state = STATE_IDLE;
167 c->error = ZOOM_ERROR_NONE;
175 c->options = ZOOM_options_create_with_parent(options);
180 c->charset = c->lang = 0;
187 c->odr_in = odr_createmem (ODR_DECODE);
188 c->odr_out = odr_createmem (ODR_ENCODE);
191 c->support_named_resultsets = 0;
192 c->last_event = ZOOM_EVENT_NONE;
194 c->m_queue_front = 0;
199 /* set database names. Take local databases (if set); otherwise
200 take databases given in ZURL (if set); otherwise use Default */
201 static char **set_DatabaseNames (ZOOM_connection con, ZOOM_options options,
204 char **databaseNames;
207 const char *cp = ZOOM_options_get (options, "databaseName");
211 if (strncmp (con->host_port, "unix:", 5) == 0)
212 cp = strchr (con->host_port+5, ':');
214 cp = strchr (con->host_port, '/');
221 while ((c = strchr(c, '+')))
229 databaseNames = (char**)
230 odr_malloc (con->odr_out, no * sizeof(*databaseNames));
234 c = strchr (cp, '+');
242 /* cp ptr to first char of db name, c is char
244 databaseNames[no] = (char*) odr_malloc (con->odr_out, 1+c-cp);
245 memcpy (databaseNames[no], cp, c-cp);
246 databaseNames[no++][c-cp] = '\0';
251 databaseNames[no] = NULL;
253 return databaseNames;
256 ZOOM_API(ZOOM_connection)
257 ZOOM_connection_new (const char *host, int portnum)
259 ZOOM_connection c = ZOOM_connection_create (0);
261 ZOOM_connection_connect (c, host, portnum);
266 ZOOM_connection_connect(ZOOM_connection c,
267 const char *host, int portnum)
274 yaz_log (LOG_DEBUG, "reconnect");
278 yaz_log(LOG_DEBUG, "connect");
280 val = ZOOM_options_get (c->options, "proxy");
282 c->proxy = xstrdup (val);
287 val = ZOOM_options_get (c->options, "charset");
289 c->charset = xstrdup (val);
294 val = ZOOM_options_get (c->options, "lang");
296 c->lang = xstrdup (val);
300 xfree (c->host_port);
304 sprintf (hostn, "%.80s:%d", host, portnum);
305 c->host_port = xstrdup(hostn);
308 c->host_port = xstrdup(host);
310 ZOOM_options_set(c->options, "host", c->host_port);
312 val = ZOOM_options_get (c->options, "cookie");
314 c->cookie_out = xstrdup (val);
316 val = ZOOM_options_get (c->options, "clientIP");
318 c->client_IP = xstrdup (val);
320 c->async = ZOOM_options_get_bool (c->options, "async", 0);
322 c->error = ZOOM_ERROR_NONE;
324 task = ZOOM_connection_add_task (c, ZOOM_TASK_CONNECT);
328 while (ZOOM_event (1, &c))
334 ZOOM_query_create(void)
336 ZOOM_query s = (ZOOM_query) xmalloc (sizeof(*s));
341 s->odr = odr_createmem (ODR_ENCODE);
347 ZOOM_query_destroy(ZOOM_query s)
353 yaz_log (LOG_DEBUG, "ZOOM_query_destroy count=%d", s->refcount);
354 if (s->refcount == 0)
356 odr_destroy (s->odr);
362 ZOOM_query_prefix(ZOOM_query s, const char *str)
364 s->query = (Z_Query *) odr_malloc (s->odr, sizeof(*s->query));
365 s->query->which = Z_Query_type_1;
366 s->query->u.type_1 = p_query_rpn(s->odr, PROTO_Z3950, str);
367 if (!s->query->u.type_1)
373 ZOOM_query_sortby(ZOOM_query s, const char *criteria)
375 s->sort_spec = yaz_sort_spec (s->odr, criteria);
381 static int do_write(ZOOM_connection c);
384 ZOOM_connection_destroy(ZOOM_connection c)
391 for (r = c->resultsets; r; r = r->next)
396 odr_destroy (c->odr_in);
397 odr_destroy (c->odr_out);
398 ZOOM_options_destroy (c->options);
399 ZOOM_connection_remove_tasks (c);
400 xfree (c->host_port);
404 xfree (c->cookie_out);
405 xfree (c->cookie_in);
406 xfree (c->client_IP);
410 void ZOOM_resultset_addref (ZOOM_resultset r)
415 ZOOM_resultset ZOOM_resultset_create ()
417 ZOOM_resultset r = (ZOOM_resultset) xmalloc (sizeof(*r));
421 r->odr = odr_createmem (ODR_ENCODE);
435 ZOOM_API(ZOOM_resultset)
436 ZOOM_connection_search_pqf(ZOOM_connection c, const char *q)
439 ZOOM_query s = ZOOM_query_create();
441 ZOOM_query_prefix (s, q);
443 r = ZOOM_connection_search (c, s);
444 ZOOM_query_destroy (s);
448 ZOOM_API(ZOOM_resultset)
449 ZOOM_connection_search(ZOOM_connection c, ZOOM_query q)
451 ZOOM_resultset r = ZOOM_resultset_create ();
455 r->r_sort_spec = q->sort_spec;
456 r->r_query = q->query;
459 r->options = ZOOM_options_create_with_parent(c->options);
461 r->start = ZOOM_options_get_int(r->options, "start", 0);
462 r->count = ZOOM_options_get_int(r->options, "count", 0);
463 r->piggyback = ZOOM_options_get_bool (r->options, "piggyback", 1);
464 cp = ZOOM_options_get (r->options, "setname");
466 r->setname = xstrdup (cp);
470 r->next = c->resultsets;
473 task = ZOOM_connection_add_task (c, ZOOM_TASK_SEARCH);
474 task->u.search.resultset = r;
475 ZOOM_resultset_addref (r);
481 while (ZOOM_event (1, &c))
488 ZOOM_resultset_destroy(ZOOM_resultset r)
493 yaz_log (LOG_DEBUG, "destroy r = %p count=%d", r, r->refcount);
494 if (r->refcount == 0)
496 ZOOM_record_cache rc;
498 for (rc = r->record_cache; rc; rc = rc->next)
499 if (rc->rec.wrbuf_marc)
500 wrbuf_free (rc->rec.wrbuf_marc, 1);
503 /* remove ourselves from the resultsets in connection */
504 ZOOM_resultset *rp = &r->connection->resultsets;
507 assert (*rp); /* we must be in this list!! */
509 { /* OK, we're here - take us out of it */
516 ZOOM_query_destroy (r->search);
517 ZOOM_options_destroy (r->options);
518 odr_destroy (r->odr);
525 ZOOM_resultset_size (ZOOM_resultset r)
530 static void do_close (ZOOM_connection c)
536 c->state = STATE_IDLE;
539 static void ZOOM_resultset_retrieve (ZOOM_resultset r,
540 int force_sync, int start, int count)
550 task = ZOOM_connection_add_task (c, ZOOM_TASK_RETRIEVE);
551 task->u.retrieve.resultset = r;
552 task->u.retrieve.start = start;
553 task->u.retrieve.count = count;
555 ZOOM_resultset_addref (r);
557 if (!r->connection->async || force_sync)
558 while (r->connection && ZOOM_event (1, &r->connection))
563 ZOOM_resultset_records (ZOOM_resultset r, ZOOM_record *recs,
564 size_t start, size_t count)
566 int force_present = 0;
572 ZOOM_resultset_retrieve (r, force_present, start, count);
576 for (i = 0; i< count; i++)
577 recs[i] = ZOOM_resultset_record_immediate (r, i+start);
581 static int do_connect (ZOOM_connection c)
584 const char *effective_host;
587 effective_host = c->proxy;
589 effective_host = c->host_port;
591 yaz_log (LOG_DEBUG, "do_connect host=%s", effective_host);
594 c->cs = cs_create_host (effective_host, 0, &add);
598 int ret = cs_connect (c->cs, add);
601 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
602 ZOOM_connection_put_event(c, event);
603 ZOOM_connection_send_init(c);
604 c->state = STATE_ESTABLISHED;
609 c->state = STATE_CONNECTING;
610 c->mask = ZOOM_SELECT_EXCEPT;
611 if (c->cs->io_pending & CS_WANT_WRITE)
612 c->mask += ZOOM_SELECT_WRITE;
613 if (c->cs->io_pending & CS_WANT_READ)
614 c->mask += ZOOM_SELECT_READ;
618 c->state = STATE_IDLE;
619 c->error = ZOOM_ERROR_CONNECT;
623 int z3950_connection_socket(ZOOM_connection c)
626 return cs_fileno(c->cs);
630 int z3950_connection_mask(ZOOM_connection c)
637 static void otherInfo_attach (ZOOM_connection c, Z_APDU *a, ODR out)
640 for (i = 0; i<200; i++)
643 Z_OtherInformation **oi;
649 sprintf (buf, "otherInfo%d", i);
650 val = ZOOM_options_get (c->options, buf);
653 cp = strchr (val, ':');
657 if (len >= sizeof(buf))
659 memcpy (buf, val, len);
661 oidval = oid_getvalbyname (buf);
662 if (oidval == VAL_NONE)
666 yaz_oi_set_string_oidval(oi, out, oidval, 1, cp+1);
670 static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out)
675 Z_OtherInformation **oi;
677 yaz_oi_set_string_oidval(oi, out, VAL_COOKIE, 1, c->cookie_out);
681 Z_OtherInformation **oi;
683 yaz_oi_set_string_oidval(oi, out, VAL_CLIENT_IP, 1, c->client_IP);
685 otherInfo_attach (c, a, out);
686 if (!z_APDU(out, &a, 0, 0))
688 FILE *outf = fopen("/tmp/apdu.txt", "a");
691 ODR odr_pr = odr_createmem(ODR_PRINT);
692 fprintf (outf, "a=%p\n", a);
693 odr_setprint(odr_pr, outf);
694 z_APDU(odr_pr, &a, 0, 0);
697 c->error = ZOOM_ERROR_ENCODE;
705 static int send_APDU (ZOOM_connection c, Z_APDU *a)
709 if (encode_APDU(c, a, c->odr_out))
711 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
712 event = ZOOM_Event_create (ZOOM_EVENT_SEND_APDU);
713 ZOOM_connection_put_event (c, event);
714 odr_reset(c->odr_out);
719 static int ZOOM_connection_send_init (ZOOM_connection c)
722 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest);
723 Z_InitRequest *ireq = apdu->u.initRequest;
724 Z_IdAuthentication *auth = (Z_IdAuthentication *)
725 odr_malloc(c->odr_out, sizeof(*auth));
726 const char *auth_groupId = ZOOM_options_get (c->options, "group");
727 const char *auth_userId = ZOOM_options_get (c->options, "user");
728 const char *auth_password = ZOOM_options_get (c->options, "pass");
730 ODR_MASK_SET(ireq->options, Z_Options_search);
731 ODR_MASK_SET(ireq->options, Z_Options_present);
732 ODR_MASK_SET(ireq->options, Z_Options_scan);
733 ODR_MASK_SET(ireq->options, Z_Options_sort);
734 ODR_MASK_SET(ireq->options, Z_Options_extendedServices);
735 ODR_MASK_SET(ireq->options, Z_Options_namedResultSets);
737 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
738 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
739 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
741 impname = ZOOM_options_get (c->options, "implementationName");
742 ireq->implementationName =
743 (char *) odr_malloc (c->odr_out, 15 + (impname ? strlen(impname) : 0));
744 strcpy (ireq->implementationName, "");
747 strcat (ireq->implementationName, impname);
748 strcat (ireq->implementationName, "/");
750 strcat (ireq->implementationName, "ZOOM-C/YAZ");
752 *ireq->maximumRecordSize =
753 ZOOM_options_get_int (c->options, "maximumRecordSize", 1024*1024);
754 *ireq->preferredMessageSize =
755 ZOOM_options_get_int (c->options, "preferredMessageSize", 1024*1024);
757 if (auth_groupId || auth_password)
759 Z_IdPass *pass = (Z_IdPass *) odr_malloc(c->odr_out, sizeof(*pass));
762 if (auth_groupId && *auth_groupId)
764 pass->groupId = (char *)
765 odr_malloc(c->odr_out, strlen(auth_groupId)+1);
766 strcpy(pass->groupId, auth_groupId);
770 if (auth_userId && *auth_userId)
772 pass->userId = (char *)
773 odr_malloc(c->odr_out, strlen(auth_userId)+1);
774 strcpy(pass->userId, auth_userId);
778 if (auth_password && *auth_password)
780 pass->password = (char *)
781 odr_malloc(c->odr_out, strlen(auth_password)+1);
782 strcpy(pass->password, auth_password);
787 auth->which = Z_IdAuthentication_idPass;
788 auth->u.idPass = pass;
789 ireq->idAuthentication = auth;
792 else if (auth_userId)
794 auth->which = Z_IdAuthentication_open;
795 auth->u.open = (char *)
796 odr_malloc(c->odr_out, strlen(auth_userId)+1);
797 strcpy(auth->u.open, auth_userId);
798 ireq->idAuthentication = auth;
801 yaz_oi_set_string_oidval(&ireq->otherInfo, c->odr_out,
802 VAL_PROXY, 1, c->host_port);
803 if (c->charset||c->lang)
805 Z_OtherInformation **oi;
806 Z_OtherInformationUnit *oi_unit;
808 yaz_oi_APDU(apdu, &oi);
810 if ((oi_unit = yaz_oi_update(oi, c->odr_out, NULL, 0, 0)))
812 ODR_MASK_SET(ireq->options, Z_Options_negotiationModel);
814 oi_unit->which = Z_OtherInfo_externallyDefinedInfo;
815 oi_unit->information.externallyDefinedInfo =
816 yaz_set_proposal_charneg
818 (const char **)&c->charset, (c->charset) ? 1:0,
819 (const char **)&c->lang, (c->lang) ? 1:0, 1);
828 static int ZOOM_connection_send_search (ZOOM_connection c)
831 int lslb, ssub, mspn;
833 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
834 Z_SearchRequest *search_req = apdu->u.searchRequest;
835 const char *elementSetName;
836 const char *smallSetElementSetName;
837 const char *mediumSetElementSetName;
841 assert (c->tasks->which == ZOOM_TASK_SEARCH);
843 r = c->tasks->u.search.resultset;
846 ZOOM_options_get (r->options, "elementSetName");
847 smallSetElementSetName =
848 ZOOM_options_get (r->options, "smallSetElementSetName");
849 mediumSetElementSetName =
850 ZOOM_options_get (r->options, "mediumSetElementSetName");
852 ZOOM_options_get (r->options, "schema");
854 if (!smallSetElementSetName)
855 smallSetElementSetName = elementSetName;
857 if (!mediumSetElementSetName)
858 mediumSetElementSetName = elementSetName;
863 /* prepare query for the search request */
864 search_req->query = r->r_query;
866 search_req->databaseNames =
867 set_DatabaseNames (c, r->options, &search_req->num_databaseNames);
869 /* get syntax (no need to provide unless piggyback is in effect) */
870 syntax = ZOOM_options_get (r->options, "preferredRecordSyntax");
872 lslb = ZOOM_options_get_int (r->options, "largeSetLowerBound", -1);
873 ssub = ZOOM_options_get_int (r->options, "smallSetUpperBound", -1);
874 mspn = ZOOM_options_get_int (r->options, "mediumSetPresentNumber", -1);
875 if (lslb != -1 && ssub != -1 && mspn != -1)
877 /* So're a Z39.50 expert? Let's hope you don't do sort */
878 *search_req->largeSetLowerBound = lslb;
879 *search_req->smallSetUpperBound = ssub;
880 *search_req->mediumSetPresentNumber = mspn;
882 else if (r->start == 0 && r->count > 0
883 && r->piggyback && !r->r_sort_spec && !schema)
885 /* Regular piggyback - do it unless we're going to do sort */
886 *search_req->largeSetLowerBound = 2000000000;
887 *search_req->smallSetUpperBound = r->count;
888 *search_req->mediumSetPresentNumber = r->count;
889 smallSetElementSetName = 0; /* no need to provide this */
893 /* non-piggyback. Need not provide elementsets or syntaxes .. */
894 smallSetElementSetName = 0;
895 mediumSetElementSetName = 0;
898 if (smallSetElementSetName && *smallSetElementSetName)
900 Z_ElementSetNames *esn = (Z_ElementSetNames *)
901 odr_malloc (c->odr_out, sizeof(*esn));
903 esn->which = Z_ElementSetNames_generic;
904 esn->u.generic = odr_strdup (c->odr_out, smallSetElementSetName);
905 search_req->smallSetElementSetNames = esn;
907 if (mediumSetElementSetName && *mediumSetElementSetName)
909 Z_ElementSetNames *esn = (Z_ElementSetNames *)
910 odr_malloc (c->odr_out, sizeof(*esn));
912 esn->which = Z_ElementSetNames_generic;
913 esn->u.generic = odr_strdup (c->odr_out, mediumSetElementSetName);
914 search_req->mediumSetElementSetNames = esn;
917 search_req->preferredRecordSyntax =
918 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
922 if (c->support_named_resultsets)
926 /* find the lowest unused ordinal so that we re-use
927 result sets on the server. */
928 for (ord = 1; ; ord++)
931 sprintf (setname, "%d", ord);
932 for (rp = c->resultsets; rp; rp = rp->next)
933 if (rp->setname && !strcmp (rp->setname, setname))
938 r->setname = xstrdup (setname);
939 yaz_log (LOG_DEBUG, "allocating %s", r->setname);
942 r->setname = xstrdup ("default");
943 ZOOM_options_set (r->options, "setname", r->setname);
945 search_req->resultSetName = odr_strdup(c->odr_out, r->setname);
946 /* send search request */
951 static void response_diag (ZOOM_connection c, Z_DiagRec *p)
953 Z_DefaultDiagFormat *r;
958 if (p->which != Z_DiagRec_defaultFormat)
960 c->error = ZOOM_ERROR_DECODE;
963 r = p->u.defaultFormat;
966 case Z_DefaultDiagFormat_v2Addinfo:
967 addinfo = r->u.v2Addinfo;
969 case Z_DefaultDiagFormat_v3Addinfo:
970 addinfo = r->u.v3Addinfo;
974 c->addinfo = xstrdup (addinfo);
975 c->error = *r->condition;
978 ZOOM_API(ZOOM_record)
979 ZOOM_record_clone (ZOOM_record srec)
986 odr_enc = odr_createmem(ODR_ENCODE);
987 if (!z_NamePlusRecord (odr_enc, &srec->npr, 0, 0))
989 buf = odr_getbuf (odr_enc, &size, 0);
991 nrec = (ZOOM_record) xmalloc (sizeof(*nrec));
992 nrec->odr = odr_createmem(ODR_DECODE);
993 nrec->wrbuf_marc = 0;
994 odr_setbuf (nrec->odr, buf, size, 0);
995 z_NamePlusRecord (nrec->odr, &nrec->npr, 0, 0);
997 odr_destroy (odr_enc);
1001 ZOOM_API(ZOOM_record)
1002 ZOOM_resultset_record_immediate (ZOOM_resultset s,size_t pos)
1004 return record_cache_lookup (s, pos, 0);
1007 ZOOM_API(ZOOM_record)
1008 ZOOM_resultset_record (ZOOM_resultset r, size_t pos)
1010 ZOOM_resultset_retrieve (r, 1, pos, 1);
1011 return ZOOM_resultset_record_immediate (r, pos);
1015 ZOOM_record_destroy (ZOOM_record rec)
1019 if (rec->wrbuf_marc)
1020 wrbuf_free (rec->wrbuf_marc, 1);
1021 odr_destroy (rec->odr);
1025 ZOOM_API(const char *)
1026 ZOOM_record_get (ZOOM_record rec, const char *type, int *len)
1028 Z_NamePlusRecord *npr;
1031 *len = 0; /* default return */
1038 if (!strcmp (type, "database"))
1041 *len = strlen(npr->databaseName);
1042 return npr->databaseName;
1044 else if (!strcmp (type, "syntax"))
1046 if (npr->which == Z_NamePlusRecord_databaseRecord)
1048 Z_External *r = (Z_External *) npr->u.databaseRecord;
1049 oident *ent = oid_getentbyoid(r->direct_reference);
1053 *len = strlen(ent->desc);
1059 else if (!strcmp (type, "render") &&
1060 npr->which == Z_NamePlusRecord_databaseRecord)
1062 Z_External *r = (Z_External *) npr->u.databaseRecord;
1063 oident *ent = oid_getentbyoid(r->direct_reference);
1065 if (r->which == Z_External_sutrs)
1067 if (len) *len = r->u.sutrs->len;
1068 return (const char *) r->u.sutrs->buf;
1070 else if (r->which == Z_External_octet)
1079 case VAL_APPLICATION_XML:
1082 if (!rec->wrbuf_marc)
1083 rec->wrbuf_marc = wrbuf_alloc();
1084 wrbuf_rewind (rec->wrbuf_marc);
1085 if (yaz_marc_decode ((const char *)
1086 r->u.octet_aligned->buf,
1088 r->u.octet_aligned->len,
1091 if (len) *len = wrbuf_len(rec->wrbuf_marc);
1092 return wrbuf_buf(rec->wrbuf_marc);
1095 if (len) *len = r->u.octet_aligned->len;
1096 return (const char *) r->u.octet_aligned->buf;
1098 else if (r->which == Z_External_grs1)
1105 else if (!strcmp (type, "xml") &&
1106 npr->which == Z_NamePlusRecord_databaseRecord)
1108 Z_External *r = (Z_External *) npr->u.databaseRecord;
1109 oident *ent = oid_getentbyoid(r->direct_reference);
1111 if (r->which == Z_External_sutrs)
1113 if (len) *len = r->u.sutrs->len;
1114 return (const char *) r->u.sutrs->buf;
1116 else if (r->which == Z_External_octet)
1125 case VAL_APPLICATION_XML:
1128 if (!rec->wrbuf_marc)
1129 rec->wrbuf_marc = wrbuf_alloc();
1130 wrbuf_rewind (rec->wrbuf_marc);
1131 if (yaz_marc_decode ((const char *)
1132 r->u.octet_aligned->buf,
1134 r->u.octet_aligned->len,
1137 if (len) *len = wrbuf_len(rec->wrbuf_marc);
1138 return wrbuf_buf(rec->wrbuf_marc);
1141 if (len) *len = r->u.octet_aligned->len;
1142 return (const char *) r->u.octet_aligned->buf;
1144 else if (r->which == Z_External_grs1)
1151 else if (!strcmp (type, "raw"))
1153 if (npr->which == Z_NamePlusRecord_databaseRecord)
1154 return (const char *) npr->u.databaseRecord;
1160 static void record_cache_add (ZOOM_resultset r,
1161 Z_NamePlusRecord *npr,
1163 const char *elementSetName)
1165 ZOOM_record_cache rc;
1167 for (rc = r->record_cache; rc; rc = rc->next)
1171 if ((!elementSetName && !rc->elementSetName)
1172 || (elementSetName && rc->elementSetName &&
1173 !strcmp (elementSetName, rc->elementSetName)))
1175 /* not destroying rc->npr (it's handled by nmem )*/
1177 /* keeping wrbuf_marc too */
1182 rc = (ZOOM_record_cache) odr_malloc (r->odr, sizeof(*rc));
1185 rc->rec.wrbuf_marc = 0;
1187 rc->elementSetName = odr_strdup (r->odr, elementSetName);
1189 rc->elementSetName = 0;
1191 rc->next = r->record_cache;
1192 r->record_cache = rc;
1195 static ZOOM_record record_cache_lookup (ZOOM_resultset r,
1197 const char *elementSetName)
1199 ZOOM_record_cache rc;
1201 for (rc = r->record_cache; rc; rc = rc->next)
1205 if ((!elementSetName && !rc->elementSetName)
1206 || (elementSetName && rc->elementSetName &&
1207 !strcmp (elementSetName, rc->elementSetName)))
1214 static void handle_records (ZOOM_connection c, Z_Records *sr,
1217 ZOOM_resultset resultset;
1221 switch (c->tasks->which)
1223 case ZOOM_TASK_SEARCH:
1224 resultset = c->tasks->u.search.resultset;
1226 case ZOOM_TASK_RETRIEVE:
1227 resultset = c->tasks->u.retrieve.resultset;
1232 if (sr && sr->which == Z_Records_NSD)
1234 Z_DiagRec dr, *dr_p = &dr;
1235 dr.which = Z_DiagRec_defaultFormat;
1236 dr.u.defaultFormat = sr->u.nonSurrogateDiagnostic;
1238 response_diag (c, dr_p);
1240 else if (sr && sr->which == Z_Records_multipleNSD)
1242 if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
1243 response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
1245 c->error = ZOOM_ERROR_DECODE;
1249 if (resultset->count + resultset->start > resultset->size)
1250 resultset->count = resultset->size - resultset->start;
1251 if (resultset->count < 0)
1252 resultset->count = 0;
1253 if (sr && sr->which == Z_Records_DBOSD)
1256 NMEM nmem = odr_extract_mem (c->odr_in);
1257 Z_NamePlusRecordList *p =
1258 sr->u.databaseOrSurDiagnostics;
1259 for (i = 0; i<p->num_records; i++)
1261 record_cache_add (resultset, p->records[i],
1262 i+ resultset->start, 0);
1264 /* transfer our response to search_nmem .. we need it later */
1265 nmem_transfer (resultset->odr->mem, nmem);
1266 nmem_destroy (nmem);
1267 if (present_phase && p->num_records == 0)
1269 /* present response and we didn't get any records! */
1270 c->error = ZOOM_ERROR_DECODE;
1273 else if (present_phase)
1275 /* present response and we didn't get any records! */
1276 c->error = ZOOM_ERROR_DECODE;
1281 static void handle_present_response (ZOOM_connection c, Z_PresentResponse *pr)
1283 handle_records (c, pr->records, 1);
1286 static void handle_search_response (ZOOM_connection c, Z_SearchResponse *sr)
1288 ZOOM_resultset resultset;
1290 yaz_log (LOG_DEBUG, "got search response");
1292 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1295 resultset = c->tasks->u.search.resultset;
1297 resultset->size = *sr->resultCount;
1298 handle_records (c, sr->records, 0);
1301 static void sort_response (ZOOM_connection c, Z_SortResponse *res)
1303 if (res->diagnostics && res->num_diagnostics > 0)
1304 response_diag (c, res->diagnostics[0]);
1307 static int scan_response (ZOOM_connection c, Z_ScanResponse *res)
1309 NMEM nmem = odr_extract_mem (c->odr_in);
1312 if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
1314 scan = c->tasks->u.scan.scan;
1316 if (res->entries && res->entries->nonsurrogateDiagnostics)
1317 response_diag(c, res->entries->nonsurrogateDiagnostics[0]);
1318 scan->scan_response = res;
1319 nmem_transfer (scan->odr->mem, nmem);
1321 ZOOM_options_set_int (scan->options, "stepSize", *res->stepSize);
1322 if (res->positionOfTerm)
1323 ZOOM_options_set_int (scan->options, "position", *res->positionOfTerm);
1324 if (res->scanStatus)
1325 ZOOM_options_set_int (scan->options, "scanStatus", *res->scanStatus);
1326 if (res->numberOfEntriesReturned)
1327 ZOOM_options_set_int (scan->options, "number",
1328 *res->numberOfEntriesReturned);
1329 nmem_destroy (nmem);
1333 static int send_sort (ZOOM_connection c)
1335 ZOOM_resultset resultset;
1337 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1340 resultset = c->tasks->u.search.resultset;
1344 resultset->r_sort_spec = 0;
1347 if (resultset->r_sort_spec)
1349 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
1350 Z_SortRequest *req = apdu->u.sortRequest;
1352 req->num_inputResultSetNames = 1;
1353 req->inputResultSetNames = (Z_InternationalString **)
1354 odr_malloc (c->odr_out, sizeof(*req->inputResultSetNames));
1355 req->inputResultSetNames[0] =
1356 odr_strdup (c->odr_out, resultset->setname);
1357 req->sortedResultSetName = odr_strdup (c->odr_out, resultset->setname);
1358 req->sortSequence = resultset->r_sort_spec;
1359 resultset->r_sort_spec = 0;
1360 send_APDU (c, apdu);
1366 static int send_present (ZOOM_connection c)
1368 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
1369 Z_PresentRequest *req = apdu->u.presentRequest;
1371 const char *syntax =
1372 ZOOM_options_get (c->options, "preferredRecordSyntax");
1373 const char *element =
1374 ZOOM_options_get (c->options, "elementSetName");
1375 const char *schema =
1376 ZOOM_options_get (c->options, "schema");
1377 ZOOM_resultset resultset;
1382 switch (c->tasks->which)
1384 case ZOOM_TASK_SEARCH:
1385 resultset = c->tasks->u.search.resultset;
1387 case ZOOM_TASK_RETRIEVE:
1388 resultset = c->tasks->u.retrieve.resultset;
1389 resultset->start = c->tasks->u.retrieve.start;
1390 resultset->count = c->tasks->u.retrieve.count;
1392 if (resultset->start >= resultset->size)
1394 if (resultset->start + resultset->count > resultset->size)
1395 resultset->count = resultset->size - resultset->start;
1401 if (c->error) /* don't continue on error */
1403 if (resultset->start < 0)
1405 for (i = 0; i<resultset->count; i++)
1408 record_cache_lookup (resultset, i + resultset->start, 0);
1412 if (i == resultset->count)
1415 resultset->start += i;
1416 resultset->count -= i;
1417 *req->resultSetStartPoint = resultset->start + 1;
1418 *req->numberOfRecordsRequested = resultset->count;
1419 assert (*req->numberOfRecordsRequested > 0);
1421 if (syntax && *syntax)
1422 req->preferredRecordSyntax =
1423 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
1425 if (schema && *schema)
1427 Z_RecordComposition *compo = (Z_RecordComposition *)
1428 odr_malloc (c->odr_out, sizeof(*compo));
1430 req->recordComposition = compo;
1431 compo->which = Z_RecordComp_complex;
1432 compo->u.complex = (Z_CompSpec *)
1433 odr_malloc(c->odr_out, sizeof(*compo->u.complex));
1434 compo->u.complex->selectAlternativeSyntax = (bool_t *)
1435 odr_malloc(c->odr_out, sizeof(bool_t));
1436 *compo->u.complex->selectAlternativeSyntax = 0;
1438 compo->u.complex->generic = (Z_Specification *)
1439 odr_malloc(c->odr_out, sizeof(*compo->u.complex->generic));
1441 compo->u.complex->generic->schema = (Odr_oid *)
1442 yaz_str_to_z3950oid (c->odr_out, CLASS_SCHEMA, schema);
1444 if (!compo->u.complex->generic->schema)
1446 /* OID wasn't a schema! Try record syntax instead. */
1448 compo->u.complex->generic->schema = (Odr_oid *)
1449 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, schema);
1451 if (element && *element)
1453 compo->u.complex->generic->elementSpec = (Z_ElementSpec *)
1454 odr_malloc(c->odr_out, sizeof(Z_ElementSpec));
1455 compo->u.complex->generic->elementSpec->which =
1456 Z_ElementSpec_elementSetName;
1457 compo->u.complex->generic->elementSpec->u.elementSetName =
1458 odr_strdup (c->odr_out, element);
1461 compo->u.complex->generic->elementSpec = 0;
1462 compo->u.complex->num_dbSpecific = 0;
1463 compo->u.complex->dbSpecific = 0;
1464 compo->u.complex->num_recordSyntax = 0;
1465 compo->u.complex->recordSyntax = 0;
1467 else if (element && *element)
1469 Z_ElementSetNames *esn = (Z_ElementSetNames *)
1470 odr_malloc (c->odr_out, sizeof(*esn));
1471 Z_RecordComposition *compo = (Z_RecordComposition *)
1472 odr_malloc (c->odr_out, sizeof(*compo));
1474 esn->which = Z_ElementSetNames_generic;
1475 esn->u.generic = odr_strdup (c->odr_out, element);
1476 compo->which = Z_RecordComp_simple;
1477 compo->u.simple = esn;
1478 req->recordComposition = compo;
1480 req->resultSetId = odr_strdup(c->odr_out, resultset->setname);
1481 send_APDU (c, apdu);
1485 ZOOM_API(ZOOM_scanset)
1486 ZOOM_connection_scan (ZOOM_connection c, const char *start)
1488 ZOOM_scanset scan = (ZOOM_scanset) xmalloc (sizeof(*scan));
1490 scan->connection = c;
1491 scan->odr = odr_createmem (ODR_DECODE);
1492 scan->options = ZOOM_options_create_with_parent (c->options);
1494 scan->scan_response = 0;
1496 if ((scan->termListAndStartPoint =
1497 p_query_scan(scan->odr, PROTO_Z3950, &scan->attributeSet,
1500 ZOOM_task task = ZOOM_connection_add_task (c, ZOOM_TASK_SCAN);
1501 task->u.scan.scan = scan;
1506 while (ZOOM_event (1, &c))
1514 ZOOM_scanset_destroy (ZOOM_scanset scan)
1519 if (scan->refcount == 0)
1521 odr_destroy (scan->odr);
1523 ZOOM_options_destroy (scan->options);
1528 static int send_package (ZOOM_connection c)
1533 assert (c->tasks->which == ZOOM_TASK_PACKAGE);
1535 event = ZOOM_Event_create (ZOOM_EVENT_SEND_APDU);
1536 ZOOM_connection_put_event (c, event);
1538 do_write_ex (c, c->tasks->u.package->buf_out,
1539 c->tasks->u.package->len_out);
1543 static int send_scan (ZOOM_connection c)
1546 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_scanRequest);
1547 Z_ScanRequest *req = apdu->u.scanRequest;
1550 assert (c->tasks->which == ZOOM_TASK_SCAN);
1551 scan = c->tasks->u.scan.scan;
1553 req->termListAndStartPoint = scan->termListAndStartPoint;
1554 req->attributeSet = scan->attributeSet;
1556 *req->numberOfTermsRequested =
1557 ZOOM_options_get_int(scan->options, "number", 10);
1559 req->preferredPositionInResponse =
1560 odr_intdup (c->odr_out,
1561 ZOOM_options_get_int(scan->options, "position", 1));
1564 odr_intdup (c->odr_out,
1565 ZOOM_options_get_int(scan->options, "stepSize", 0));
1567 req->databaseNames = set_DatabaseNames (c, scan->options,
1568 &req->num_databaseNames);
1570 send_APDU (c, apdu);
1576 ZOOM_scanset_size (ZOOM_scanset scan)
1578 if (!scan || !scan->scan_response || !scan->scan_response->entries)
1580 return scan->scan_response->entries->num_entries;
1583 ZOOM_API(const char *)
1584 ZOOM_scanset_term (ZOOM_scanset scan, size_t pos,
1587 const char *term = 0;
1588 size_t noent = ZOOM_scanset_size (scan);
1589 Z_ScanResponse *res = scan->scan_response;
1595 if (res->entries->entries[pos]->which == Z_Entry_termInfo)
1597 Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
1599 if (t->term->which == Z_Term_general)
1601 term = (const char *) t->term->u.general->buf;
1602 *len = t->term->u.general->len;
1604 *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
1609 ZOOM_API(const char *)
1610 ZOOM_scanset_option_get (ZOOM_scanset scan, const char *key)
1612 return ZOOM_options_get (scan->options, key);
1616 ZOOM_scanset_option_set (ZOOM_scanset scan, const char *key,
1619 ZOOM_options_set (scan->options, key, val);
1624 static Z_APDU *create_es_package (ZOOM_package p, int type)
1627 Z_APDU *apdu = zget_APDU(p->odr_out, Z_APDU_extendedServicesRequest);
1628 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
1630 *req->function = Z_ExtendedServicesRequest_create;
1632 str = ZOOM_options_get(p->options, "package-name");
1634 req->packageName = nmem_strdup (p->odr_out->mem, str);
1636 str = ZOOM_options_get(p->options, "user-id");
1638 req->userId = nmem_strdup (p->odr_out->mem, str);
1640 req->packageType = yaz_oidval_to_z3950oid(p->odr_out, CLASS_EXTSERV,
1643 str = ZOOM_options_get(p->options, "function");
1646 if (!strcmp (str, "create"))
1648 if (!strcmp (str, "delete"))
1650 if (!strcmp (str, "modify"))
1656 static const char *ill_array_lookup (void *clientData, const char *idx)
1658 ZOOM_package p = (ZOOM_package) clientData;
1659 return ZOOM_options_get (p->options, idx+4);
1662 static Z_External *encode_ill_request (ZOOM_package p)
1664 ODR out = p->odr_out;
1667 struct ill_get_ctl ctl;
1669 ctl.odr = p->odr_out;
1671 ctl.f = ill_array_lookup;
1673 req = ill_get_ILLRequest(&ctl, "ill", 0);
1675 if (!ill_Request (out, &req, 0, 0))
1677 int ill_request_size;
1678 char *ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
1679 if (ill_request_buf)
1680 odr_setbuf (out, ill_request_buf, ill_request_size, 1);
1686 int illRequest_size = 0;
1687 char *illRequest_buf = odr_getbuf (out, &illRequest_size, 0);
1689 oid.proto = PROTO_GENERAL;
1690 oid.oclass = CLASS_GENERAL;
1691 oid.value = VAL_ISO_ILL_1;
1693 r = (Z_External *) odr_malloc (out, sizeof(*r));
1694 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid));
1695 r->indirect_reference = 0;
1697 r->which = Z_External_single;
1699 r->u.single_ASN1_type = (Odr_oct *)
1700 odr_malloc (out, sizeof(*r->u.single_ASN1_type));
1701 r->u.single_ASN1_type->buf = odr_malloc (out, illRequest_size);
1702 r->u.single_ASN1_type->len = illRequest_size;
1703 r->u.single_ASN1_type->size = illRequest_size;
1704 memcpy (r->u.single_ASN1_type->buf, illRequest_buf, illRequest_size);
1709 static Z_ItemOrder *encode_item_order(ZOOM_package p)
1711 Z_ItemOrder *req = odr_malloc (p->odr_out, sizeof(*req));
1714 req->which=Z_IOItemOrder_esRequest;
1715 req->u.esRequest = (Z_IORequest *)
1716 odr_malloc(p->odr_out,sizeof(Z_IORequest));
1718 /* to keep part ... */
1719 req->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
1720 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartToKeep));
1721 req->u.esRequest->toKeep->supplDescription = 0;
1722 req->u.esRequest->toKeep->contact =
1723 odr_malloc (p->odr_out, sizeof(*req->u.esRequest->toKeep->contact));
1725 str = ZOOM_options_get(p->options, "contact-name");
1726 req->u.esRequest->toKeep->contact->name = str ?
1727 nmem_strdup (p->odr_out->mem, str) : 0;
1729 str = ZOOM_options_get(p->options, "contact-phone");
1730 req->u.esRequest->toKeep->contact->phone = str ?
1731 nmem_strdup (p->odr_out->mem, str) : 0;
1733 str = ZOOM_options_get(p->options, "contact-email");
1734 req->u.esRequest->toKeep->contact->email = str ?
1735 nmem_strdup (p->odr_out->mem, str) : 0;
1737 req->u.esRequest->toKeep->addlBilling = 0;
1739 /* not to keep part ... */
1740 req->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
1741 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartNotToKeep));
1743 req->u.esRequest->notToKeep->resultSetItem = (Z_IOResultSetItem *)
1744 odr_malloc(p->odr_out, sizeof(Z_IOResultSetItem));
1746 str = ZOOM_options_get(p->options, "itemorder-setname");
1749 req->u.esRequest->notToKeep->resultSetItem->resultSetId =
1750 nmem_strdup (p->odr_out->mem, str);
1751 req->u.esRequest->notToKeep->resultSetItem->item =
1752 (int *) odr_malloc(p->odr_out, sizeof(int));
1754 str = ZOOM_options_get(p->options, "itemorder-item");
1755 *req->u.esRequest->notToKeep->resultSetItem->item =
1756 (str ? atoi(str) : 1);
1758 req->u.esRequest->notToKeep->itemRequest = encode_ill_request(p);
1764 ZOOM_package_send (ZOOM_package p, const char *type)
1771 odr_reset (p->odr_out);
1774 if (!strcmp(type, "itemorder"))
1777 apdu = create_es_package (p, VAL_ITEMORDER);
1780 r = odr_malloc (p->odr_out, sizeof(*r));
1782 r->direct_reference =
1783 yaz_oidval_to_z3950oid(p->odr_out, CLASS_EXTSERV,
1786 r->which = Z_External_itemOrder;
1787 r->indirect_reference = 0;
1788 r->u.itemOrder = encode_item_order (p);
1790 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
1795 if (encode_APDU(p->connection, apdu, p->odr_out) == 0)
1799 ZOOM_task task = ZOOM_connection_add_task (c, ZOOM_TASK_PACKAGE);
1800 task->u.package = p;
1801 buf = odr_getbuf(p->odr_out, &p->len_out, 0);
1802 p->buf_out = xmalloc (p->len_out);
1803 memcpy (p->buf_out, buf, p->len_out);
1808 while (ZOOM_event (1, &c))
1815 ZOOM_API(ZOOM_package)
1816 ZOOM_connection_package (ZOOM_connection c, ZOOM_options options)
1818 ZOOM_package p = (ZOOM_package) xmalloc (sizeof(*p));
1821 p->odr_out = odr_createmem (ODR_ENCODE);
1822 p->options = ZOOM_options_create_with_parent2 (options, c->options);
1830 ZOOM_package_destroy(ZOOM_package p)
1835 if (p->refcount == 0)
1837 odr_destroy (p->odr_out);
1840 ZOOM_options_destroy (p->options);
1845 ZOOM_API(const char *)
1846 ZOOM_package_option_get (ZOOM_package p, const char *key)
1848 return ZOOM_options_get (p->options, key);
1852 ZOOM_package_option_set (ZOOM_package p, const char *key,
1855 ZOOM_options_set (p->options, key, val);
1858 static int ZOOM_connection_exec_task (ZOOM_connection c)
1860 ZOOM_task task = c->tasks;
1862 yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task");
1865 if (c->error != ZOOM_ERROR_NONE ||
1866 (!c->cs && task->which != ZOOM_TASK_CONNECT))
1868 ZOOM_connection_remove_tasks (c);
1871 yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task type=%d run=%d",
1872 task->which, task->running);
1876 switch (task->which)
1878 case ZOOM_TASK_SEARCH:
1879 /* see if search hasn't been sent yet. */
1880 if (ZOOM_connection_send_search (c))
1883 case ZOOM_TASK_RETRIEVE:
1884 if (send_present (c))
1887 case ZOOM_TASK_CONNECT:
1891 case ZOOM_TASK_SCAN:
1895 case ZOOM_TASK_PACKAGE:
1896 if (send_package(c))
1899 ZOOM_connection_remove_task (c);
1903 static int send_sort_present (ZOOM_connection c)
1905 int r = send_sort (c);
1907 r = send_present (c);
1911 static int es_response (ZOOM_connection c,
1912 Z_ExtendedServicesResponse *res)
1914 if (!c->tasks || c->tasks->which != ZOOM_TASK_PACKAGE)
1916 if (res->diagnostics && res->num_diagnostics > 0)
1917 response_diag(c, res->diagnostics[0]);
1918 if (res->taskPackage &&
1919 res->taskPackage->which == Z_External_extendedService)
1921 Z_TaskPackage *taskPackage = res->taskPackage->u.extendedService;
1922 Odr_oct *id = taskPackage->targetReference;
1925 ZOOM_options_setl (c->tasks->u.package->options,
1926 "targetReference", id->buf, id->len);
1932 static void handle_apdu (ZOOM_connection c, Z_APDU *apdu)
1934 Z_InitResponse *initrs;
1937 yaz_log (LOG_DEBUG, "hande_apdu type=%d", apdu->which);
1940 case Z_APDU_initResponse:
1941 initrs = apdu->u.initResponse;
1942 if (!*initrs->result)
1944 c->error = ZOOM_ERROR_INIT;
1949 yaz_oi_get_string_oidval (&apdu->u.initResponse->otherInfo,
1951 xfree (c->cookie_in);
1954 c->cookie_in = xstrdup(cookie);
1955 if (ODR_MASK_GET(initrs->options, Z_Options_namedResultSets) &&
1956 ODR_MASK_GET(initrs->protocolVersion, Z_ProtocolVersion_3))
1957 c->support_named_resultsets = 1;
1960 assert (c->tasks->which == ZOOM_TASK_CONNECT);
1961 ZOOM_connection_remove_task (c);
1963 ZOOM_connection_exec_task (c);
1965 if (ODR_MASK_GET(initrs->options, Z_Options_negotiationModel))
1967 NMEM tmpmem = nmem_create();
1968 Z_CharSetandLanguageNegotiation *p =
1969 yaz_get_charneg_record(initrs->otherInfo);
1973 char *charset=NULL, *lang=NULL;
1976 yaz_get_response_charneg(tmpmem, p, &charset, &lang, &selected);
1977 yaz_log(LOG_DEBUG, "Target accepted: charset - %s,"
1978 "language - %s, select - %d",
1979 charset, lang, selected);
1981 nmem_destroy(tmpmem);
1985 case Z_APDU_searchResponse:
1986 handle_search_response (c, apdu->u.searchResponse);
1987 if (!send_sort_present (c))
1988 ZOOM_connection_remove_task (c);
1990 case Z_APDU_presentResponse:
1991 handle_present_response (c, apdu->u.presentResponse);
1992 if (!send_present (c))
1993 ZOOM_connection_remove_task (c);
1995 case Z_APDU_sortResponse:
1996 sort_response (c, apdu->u.sortResponse);
1997 if (!send_present (c))
1998 ZOOM_connection_remove_task (c);
2000 case Z_APDU_scanResponse:
2001 scan_response (c, apdu->u.scanResponse);
2002 ZOOM_connection_remove_task (c);
2004 case Z_APDU_extendedServicesResponse:
2005 es_response (c, apdu->u.extendedServicesResponse);
2006 ZOOM_connection_remove_task (c);
2009 if (c->reconnect_ok)
2012 c->tasks->running = 0;
2013 ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT);
2017 c->error = ZOOM_ERROR_CONNECTION_LOST;
2022 c->error = ZOOM_ERROR_DECODE;
2027 static int do_read (ZOOM_connection c)
2033 event = ZOOM_Event_create (ZOOM_EVENT_RECV_DATA);
2034 ZOOM_connection_put_event (c, event);
2036 yaz_log (LOG_DEBUG, "do_read len=%d", c->len_in);
2038 r = cs_get (c->cs, &c->buf_in, &c->len_in);
2043 if (c->reconnect_ok)
2046 c->reconnect_ok = 0;
2047 yaz_log (LOG_DEBUG, "reconnect read");
2048 c->tasks->running = 0;
2049 ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT);
2053 c->error= ZOOM_ERROR_CONNECTION_LOST;
2060 odr_reset (c->odr_in);
2061 odr_setbuf (c->odr_in, c->buf_in, r, 0);
2062 event = ZOOM_Event_create (ZOOM_EVENT_RECV_APDU);
2063 ZOOM_connection_put_event (c, event);
2064 if (!z_APDU (c->odr_in, &apdu, 0, 0))
2066 c->error = ZOOM_ERROR_DECODE;
2070 handle_apdu (c, apdu);
2071 c->reconnect_ok = 0;
2076 static int do_write_ex (ZOOM_connection c, char *buf_out, int len_out)
2081 event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA);
2082 ZOOM_connection_put_event (c, event);
2084 yaz_log (LOG_DEBUG, "do_write_ex len=%d", len_out);
2085 if ((r=cs_put (c->cs, buf_out, len_out)) < 0)
2087 if (c->reconnect_ok)
2090 c->reconnect_ok = 0;
2091 yaz_log (LOG_DEBUG, "reconnect write");
2092 c->tasks->running = 0;
2093 ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT);
2096 if (c->state == STATE_CONNECTING)
2097 c->error = ZOOM_ERROR_CONNECT;
2099 c->error = ZOOM_ERROR_CONNECTION_LOST;
2105 c->mask = ZOOM_SELECT_EXCEPT;
2106 if (c->cs->io_pending & CS_WANT_WRITE)
2107 c->mask += ZOOM_SELECT_WRITE;
2108 if (c->cs->io_pending & CS_WANT_READ)
2109 c->mask += ZOOM_SELECT_READ;
2110 yaz_log (LOG_DEBUG, "do_write_ex 1 mask=%d", c->mask);
2114 // c->reconnect_ok = 0;
2115 c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT;
2116 yaz_log (LOG_DEBUG, "do_write_ex 2 mask=%d", c->mask);
2121 static int do_write(ZOOM_connection c)
2123 return do_write_ex (c, c->buf_out, c->len_out);
2127 ZOOM_API(const char *)
2128 ZOOM_connection_option_get (ZOOM_connection c, const char *key)
2130 return ZOOM_options_get (c->options, key);
2134 ZOOM_connection_option_set (ZOOM_connection c, const char *key,
2137 ZOOM_options_set (c->options, key, val);
2140 ZOOM_API(const char *)
2141 ZOOM_resultset_option_get (ZOOM_resultset r, const char *key)
2143 return ZOOM_options_get (r->options, key);
2147 ZOOM_resultset_option_set (ZOOM_resultset r, const char *key,
2150 ZOOM_options_set (r->options, key, val);
2155 ZOOM_connection_errcode (ZOOM_connection c)
2157 return ZOOM_connection_error (c, 0, 0);
2160 ZOOM_API(const char *)
2161 ZOOM_connection_errmsg (ZOOM_connection c)
2164 ZOOM_connection_error (c, &msg, 0);
2168 ZOOM_API(const char *)
2169 ZOOM_connection_addinfo (ZOOM_connection c)
2171 const char *addinfo;
2172 ZOOM_connection_error (c, 0, &addinfo);
2177 ZOOM_connection_error (ZOOM_connection c, const char **cp,
2178 const char **addinfo)
2180 int error = c->error;
2185 case ZOOM_ERROR_NONE:
2186 *cp = "No error"; break;
2187 case ZOOM_ERROR_CONNECT:
2188 *cp = "Connect failed"; break;
2189 case ZOOM_ERROR_MEMORY:
2190 *cp = "Out of memory"; break;
2191 case ZOOM_ERROR_ENCODE:
2192 *cp = "Encoding failed"; break;
2193 case ZOOM_ERROR_DECODE:
2194 *cp = "Decoding failed"; break;
2195 case ZOOM_ERROR_CONNECTION_LOST:
2196 *cp = "Connection lost"; break;
2197 case ZOOM_ERROR_INIT:
2198 *cp = "Init rejected"; break;
2199 case ZOOM_ERROR_INTERNAL:
2200 *cp = "Internal failure"; break;
2201 case ZOOM_ERROR_TIMEOUT:
2202 *cp = "Timeout"; break;
2204 *cp = diagbib1_str (error);
2210 *addinfo = c->addinfo;
2217 static int ZOOM_connection_do_io(ZOOM_connection c, int mask)
2219 ZOOM_Event event = 0;
2220 int r = cs_look(c->cs);
2221 yaz_log (LOG_DEBUG, "ZOOM_connection_do_io c=%p mask=%d cs_look=%d",
2226 event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
2227 c->error = ZOOM_ERROR_CONNECT;
2229 ZOOM_connection_put_event (c, event);
2231 else if (r == CS_CONNECT)
2234 event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
2236 ret = cs_rcvconnect (c->cs);
2237 yaz_log (LOG_DEBUG, "cs_rcvconnect returned %d", ret);
2240 c->mask = ZOOM_SELECT_EXCEPT;
2241 if (c->cs->io_pending & CS_WANT_WRITE)
2242 c->mask += ZOOM_SELECT_WRITE;
2243 if (c->cs->io_pending & CS_WANT_READ)
2244 c->mask += ZOOM_SELECT_READ;
2245 ZOOM_connection_put_event (c, event);
2249 ZOOM_connection_put_event (c, event);
2250 ZOOM_connection_send_init (c);
2251 c->state = STATE_ESTABLISHED;
2255 c->error = ZOOM_ERROR_CONNECT;
2257 ZOOM_connection_put_event (c, event);
2262 if (mask & ZOOM_SELECT_READ)
2264 if (c->cs && (mask & ZOOM_SELECT_WRITE))
2271 ZOOM_connection_last_event(ZOOM_connection cs)
2274 return ZOOM_EVENT_NONE;
2275 return cs->last_event;
2279 ZOOM_event (int no, ZOOM_connection *cs)
2282 struct pollfd pollfds[1024];
2283 ZOOM_connection poll_cs[1024];
2286 fd_set input, output, except;
2291 for (i = 0; i<no; i++)
2293 ZOOM_connection c = cs[i];
2295 if (c && (event = ZOOM_connection_get_event(c)))
2297 ZOOM_Event_destroy (event);
2301 for (i = 0; i<no; i++)
2303 ZOOM_connection c = cs[i];
2305 if (c && ZOOM_connection_exec_task (c))
2307 if ((event = ZOOM_connection_get_event(c)))
2309 ZOOM_Event_destroy (event);
2325 for (i = 0; i<no; i++)
2327 ZOOM_connection c = cs[i];
2332 fd = z3950_connection_socket(c);
2333 mask = z3950_connection_mask(c);
2343 short poll_events = 0;
2345 if (mask & ZOOM_SELECT_READ)
2346 poll_events += POLLIN;
2347 if (mask & ZOOM_SELECT_WRITE)
2348 poll_events += POLLOUT;
2349 if (mask & ZOOM_SELECT_EXCEPT)
2350 poll_events += POLLERR;
2351 pollfds[nfds].fd = fd;
2352 pollfds[nfds].events = poll_events;
2353 pollfds[nfds].revents = 0;
2358 if (mask & ZOOM_SELECT_READ)
2360 FD_SET (fd, &input);
2363 if (mask & ZOOM_SELECT_WRITE)
2365 FD_SET (fd, &output);
2368 if (mask & ZOOM_SELECT_EXCEPT)
2370 FD_SET (fd, &except);
2378 r = poll (pollfds, nfds, 15000);
2379 for (i = 0; i<nfds; i++)
2381 ZOOM_connection c = poll_cs[i];
2385 if (pollfds[i].revents & POLLIN)
2386 mask += ZOOM_SELECT_READ;
2387 if (pollfds[i].revents & POLLOUT)
2388 mask += ZOOM_SELECT_WRITE;
2389 if (pollfds[i].revents & POLLERR)
2390 mask += ZOOM_SELECT_EXCEPT;
2392 ZOOM_connection_do_io(c, mask);
2394 else if (r == 0 && c->mask)
2396 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
2397 /* timeout and this connection was waiting */
2398 c->error = ZOOM_ERROR_TIMEOUT;
2400 ZOOM_connection_put_event(c, event);
2404 yaz_log (LOG_DEBUG, "select start");
2405 r = select (max_fd+1, &input, &output, &except, &tv);
2406 yaz_log (LOG_DEBUG, "select stop, returned r=%d", r);
2407 for (i = 0; i<no; i++)
2409 ZOOM_connection c = cs[i];
2414 fd = z3950_connection_socket(c);
2418 /* no timeout and real socket */
2419 if (FD_ISSET(fd, &input))
2420 mask += ZOOM_SELECT_READ;
2421 if (FD_ISSET(fd, &output))
2422 mask += ZOOM_SELECT_WRITE;
2423 if (FD_ISSET(fd, &except))
2424 mask += ZOOM_SELECT_EXCEPT;
2426 ZOOM_connection_do_io(c, mask);
2428 if (r == 0 && c->mask)
2430 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
2431 /* timeout and this connection was waiting */
2432 c->error = ZOOM_ERROR_TIMEOUT;
2434 yaz_log (LOG_DEBUG, "timeout");
2435 ZOOM_connection_put_event(c, event);
2439 for (i = 0; i<no; i++)
2441 ZOOM_connection c = cs[i];
2443 if (c && (event = ZOOM_connection_get_event(c)))
2445 ZOOM_Event_destroy (event);