2 * $Id: zoom-c.c,v 1.9 2002-12-03 10:03:27 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/marcdisp.h>
12 #include <yaz/diagbib1.h>
13 #include <yaz/charneg.h>
28 static zoom_ret ZOOM_connection_send_init (ZOOM_connection c);
29 static zoom_ret do_write_ex (ZOOM_connection c, char *buf_out, int len_out);
31 static ZOOM_Event ZOOM_Event_create (int kind)
33 ZOOM_Event event = (ZOOM_Event) xmalloc (sizeof(*event));
40 static void ZOOM_Event_destroy (ZOOM_Event event)
45 static void ZOOM_connection_put_event (ZOOM_connection c, ZOOM_Event event)
49 c->m_queue_back->prev = event;
50 assert (c->m_queue_front);
54 assert (!c->m_queue_front);
55 c->m_queue_front = event;
57 event->next = c->m_queue_back;
59 c->m_queue_back = event;
62 static ZOOM_Event ZOOM_connection_get_event(ZOOM_connection c)
64 ZOOM_Event event = c->m_queue_front;
67 assert (c->m_queue_back);
68 c->m_queue_front = event->prev;
71 assert (c->m_queue_back);
72 c->m_queue_front->next = 0;
76 c->last_event = event->kind;
80 static void clear_error (ZOOM_connection c)
85 case ZOOM_ERROR_CONNECT:
86 case ZOOM_ERROR_MEMORY:
87 case ZOOM_ERROR_DECODE:
88 case ZOOM_ERROR_CONNECTION_LOST:
90 case ZOOM_ERROR_INTERNAL:
93 c->error = ZOOM_ERROR_NONE;
99 ZOOM_task ZOOM_connection_add_task (ZOOM_connection c, int which)
101 ZOOM_task *taskp = &c->tasks;
103 taskp = &(*taskp)->next;
104 *taskp = (ZOOM_task) xmalloc (sizeof(**taskp));
105 (*taskp)->running = 0;
106 (*taskp)->which = which;
112 ZOOM_task ZOOM_connection_insert_task (ZOOM_connection c, int which)
114 ZOOM_task task = (ZOOM_task) xmalloc (sizeof(*task));
116 task->next = c->tasks;
125 void ZOOM_connection_remove_task (ZOOM_connection c)
127 ZOOM_task task = c->tasks;
131 c->tasks = task->next;
134 case ZOOM_TASK_SEARCH:
135 ZOOM_resultset_destroy (task->u.search.resultset);
137 case ZOOM_TASK_RETRIEVE:
138 ZOOM_resultset_destroy (task->u.retrieve.resultset);
140 case ZOOM_TASK_CONNECT:
143 ZOOM_scanset_destroy (task->u.scan.scan);
145 case ZOOM_TASK_PACKAGE:
146 ZOOM_package_destroy (task->u.package);
155 void ZOOM_connection_remove_tasks (ZOOM_connection c)
158 ZOOM_connection_remove_task(c);
161 static ZOOM_record record_cache_lookup (ZOOM_resultset r, int pos);
163 ZOOM_API(ZOOM_connection)
164 ZOOM_connection_create (ZOOM_options options)
166 ZOOM_connection c = (ZOOM_connection) xmalloc (sizeof(*c));
171 c->state = STATE_IDLE;
172 c->error = ZOOM_ERROR_NONE;
180 c->options = ZOOM_options_create_with_parent(options);
185 c->charset = c->lang = 0;
192 c->odr_in = odr_createmem (ODR_DECODE);
193 c->odr_out = odr_createmem (ODR_ENCODE);
196 c->support_named_resultsets = 0;
197 c->last_event = ZOOM_EVENT_NONE;
199 c->m_queue_front = 0;
204 /* set database names. Take local databases (if set); otherwise
205 take databases given in ZURL (if set); otherwise use Default */
206 static char **set_DatabaseNames (ZOOM_connection con, ZOOM_options options,
209 char **databaseNames;
212 const char *cp = ZOOM_options_get (options, "databaseName");
216 if (strncmp (con->host_port, "unix:", 5) == 0)
217 cp = strchr (con->host_port+5, ':');
219 cp = strchr (con->host_port, '/');
226 while ((c = strchr(c, '+')))
234 databaseNames = (char**)
235 odr_malloc (con->odr_out, no * sizeof(*databaseNames));
239 c = strchr (cp, '+');
247 /* cp ptr to first char of db name, c is char
249 databaseNames[no] = (char*) odr_malloc (con->odr_out, 1+c-cp);
250 memcpy (databaseNames[no], cp, c-cp);
251 databaseNames[no++][c-cp] = '\0';
256 databaseNames[no] = NULL;
258 return databaseNames;
261 ZOOM_API(ZOOM_connection)
262 ZOOM_connection_new (const char *host, int portnum)
264 ZOOM_connection c = ZOOM_connection_create (0);
266 ZOOM_connection_connect (c, host, portnum);
271 ZOOM_connection_connect(ZOOM_connection c,
272 const char *host, int portnum)
279 yaz_log (LOG_DEBUG, "reconnect");
283 yaz_log(LOG_DEBUG, "connect");
285 val = ZOOM_options_get (c->options, "proxy");
287 c->proxy = xstrdup (val);
292 val = ZOOM_options_get (c->options, "charset");
294 c->charset = xstrdup (val);
299 val = ZOOM_options_get (c->options, "lang");
301 c->lang = xstrdup (val);
305 xfree (c->host_port);
309 sprintf (hostn, "%.80s:%d", host, portnum);
310 c->host_port = xstrdup(hostn);
313 c->host_port = xstrdup(host);
315 ZOOM_options_set(c->options, "host", c->host_port);
317 val = ZOOM_options_get (c->options, "cookie");
319 c->cookie_out = xstrdup (val);
321 val = ZOOM_options_get (c->options, "clientIP");
323 c->client_IP = xstrdup (val);
325 c->async = ZOOM_options_get_bool (c->options, "async", 0);
327 c->error = ZOOM_ERROR_NONE;
329 task = ZOOM_connection_add_task (c, ZOOM_TASK_CONNECT);
333 while (ZOOM_event (1, &c))
339 ZOOM_query_create(void)
341 ZOOM_query s = (ZOOM_query) xmalloc (sizeof(*s));
346 s->odr = odr_createmem (ODR_ENCODE);
352 ZOOM_query_destroy(ZOOM_query s)
358 yaz_log (LOG_DEBUG, "ZOOM_query_destroy count=%d", s->refcount);
359 if (s->refcount == 0)
361 odr_destroy (s->odr);
367 ZOOM_query_prefix(ZOOM_query s, const char *str)
369 s->query = (Z_Query *) odr_malloc (s->odr, sizeof(*s->query));
370 s->query->which = Z_Query_type_1;
371 s->query->u.type_1 = p_query_rpn(s->odr, PROTO_Z3950, str);
372 if (!s->query->u.type_1)
378 ZOOM_query_sortby(ZOOM_query s, const char *criteria)
380 s->sort_spec = yaz_sort_spec (s->odr, criteria);
386 static zoom_ret do_write(ZOOM_connection c);
389 ZOOM_connection_destroy(ZOOM_connection c)
396 for (r = c->resultsets; r; r = r->next)
401 odr_destroy (c->odr_in);
402 odr_destroy (c->odr_out);
403 ZOOM_options_destroy (c->options);
404 ZOOM_connection_remove_tasks (c);
405 xfree (c->host_port);
409 xfree (c->cookie_out);
410 xfree (c->cookie_in);
411 xfree (c->client_IP);
415 void ZOOM_resultset_addref (ZOOM_resultset r)
420 yaz_log (LOG_DEBUG, "ZOOM_resultset_addref r=%p count=%d",
424 ZOOM_resultset ZOOM_resultset_create ()
426 ZOOM_resultset r = (ZOOM_resultset) xmalloc (sizeof(*r));
428 yaz_log (LOG_DEBUG, "ZOOM_resultset_create r = %p", r);
431 r->odr = odr_createmem (ODR_ENCODE);
445 ZOOM_API(ZOOM_resultset)
446 ZOOM_connection_search_pqf(ZOOM_connection c, const char *q)
449 ZOOM_query s = ZOOM_query_create();
451 ZOOM_query_prefix (s, q);
453 r = ZOOM_connection_search (c, s);
454 ZOOM_query_destroy (s);
458 ZOOM_API(ZOOM_resultset)
459 ZOOM_connection_search(ZOOM_connection c, ZOOM_query q)
461 ZOOM_resultset r = ZOOM_resultset_create ();
465 r->r_sort_spec = q->sort_spec;
466 r->r_query = q->query;
469 r->options = ZOOM_options_create_with_parent(c->options);
471 r->start = ZOOM_options_get_int(r->options, "start", 0);
472 r->count = ZOOM_options_get_int(r->options, "count", 0);
473 r->piggyback = ZOOM_options_get_bool (r->options, "piggyback", 1);
474 cp = ZOOM_options_get (r->options, "setname");
476 r->setname = xstrdup (cp);
480 r->next = c->resultsets;
483 task = ZOOM_connection_add_task (c, ZOOM_TASK_SEARCH);
484 task->u.search.resultset = r;
485 ZOOM_resultset_addref (r);
491 while (ZOOM_event (1, &c))
498 ZOOM_resultset_destroy(ZOOM_resultset r)
503 yaz_log (LOG_DEBUG, "ZOOM_resultset_destroy r = %p count=%d",
505 if (r->refcount == 0)
507 ZOOM_record_cache rc;
509 for (rc = r->record_cache; rc; rc = rc->next)
510 if (rc->rec.wrbuf_marc)
511 wrbuf_free (rc->rec.wrbuf_marc, 1);
514 /* remove ourselves from the resultsets in connection */
515 ZOOM_resultset *rp = &r->connection->resultsets;
518 assert (*rp); /* we must be in this list!! */
520 { /* OK, we're here - take us out of it */
527 ZOOM_query_destroy (r->search);
528 ZOOM_options_destroy (r->options);
529 odr_destroy (r->odr);
536 ZOOM_resultset_size (ZOOM_resultset r)
541 static void do_close (ZOOM_connection c)
547 c->state = STATE_IDLE;
550 static void ZOOM_resultset_retrieve (ZOOM_resultset r,
551 int force_sync, int start, int count)
561 task = ZOOM_connection_add_task (c, ZOOM_TASK_RETRIEVE);
562 task->u.retrieve.resultset = r;
563 task->u.retrieve.start = start;
564 task->u.retrieve.count = count;
566 ZOOM_resultset_addref (r);
568 if (!r->connection->async || force_sync)
569 while (r->connection && ZOOM_event (1, &r->connection))
574 ZOOM_resultset_records (ZOOM_resultset r, ZOOM_record *recs,
575 size_t start, size_t count)
577 int force_present = 0;
583 ZOOM_resultset_retrieve (r, force_present, start, count);
587 for (i = 0; i< count; i++)
588 recs[i] = ZOOM_resultset_record_immediate (r, i+start);
592 static zoom_ret do_connect (ZOOM_connection c)
595 const char *effective_host;
598 effective_host = c->proxy;
600 effective_host = c->host_port;
602 yaz_log (LOG_DEBUG, "do_connect host=%s", effective_host);
605 c->cs = cs_create_host (effective_host, 0, &add);
609 int ret = cs_connect (c->cs, add);
612 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
613 ZOOM_connection_put_event(c, event);
614 ZOOM_connection_send_init(c);
615 c->state = STATE_ESTABLISHED;
620 c->state = STATE_CONNECTING;
621 c->mask = ZOOM_SELECT_EXCEPT;
622 if (c->cs->io_pending & CS_WANT_WRITE)
623 c->mask += ZOOM_SELECT_WRITE;
624 if (c->cs->io_pending & CS_WANT_READ)
625 c->mask += ZOOM_SELECT_READ;
629 c->state = STATE_IDLE;
630 c->error = ZOOM_ERROR_CONNECT;
631 return zoom_complete;
634 int z3950_connection_socket(ZOOM_connection c)
637 return cs_fileno(c->cs);
641 int z3950_connection_mask(ZOOM_connection c)
648 static void otherInfo_attach (ZOOM_connection c, Z_APDU *a, ODR out)
651 for (i = 0; i<200; i++)
654 Z_OtherInformation **oi;
660 sprintf (buf, "otherInfo%d", i);
661 val = ZOOM_options_get (c->options, buf);
664 cp = strchr (val, ':');
668 if (len >= sizeof(buf))
670 memcpy (buf, val, len);
672 oidval = oid_getvalbyname (buf);
673 if (oidval == VAL_NONE)
677 yaz_oi_set_string_oidval(oi, out, oidval, 1, cp+1);
681 static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out)
686 Z_OtherInformation **oi;
688 yaz_oi_set_string_oidval(oi, out, VAL_COOKIE, 1, c->cookie_out);
692 Z_OtherInformation **oi;
694 yaz_oi_set_string_oidval(oi, out, VAL_CLIENT_IP, 1, c->client_IP);
696 otherInfo_attach (c, a, out);
697 if (!z_APDU(out, &a, 0, 0))
699 FILE *outf = fopen("/tmp/apdu.txt", "a");
702 ODR odr_pr = odr_createmem(ODR_PRINT);
703 fprintf (outf, "a=%p\n", a);
704 odr_setprint(odr_pr, outf);
705 z_APDU(odr_pr, &a, 0, 0);
708 yaz_log (LOG_DEBUG, "encoding failed");
709 c->error = ZOOM_ERROR_ENCODE;
717 static zoom_ret send_APDU (ZOOM_connection c, Z_APDU *a)
721 if (encode_APDU(c, a, c->odr_out))
722 return zoom_complete;
723 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
724 event = ZOOM_Event_create (ZOOM_EVENT_SEND_APDU);
725 ZOOM_connection_put_event (c, event);
726 odr_reset(c->odr_out);
730 /* returns 1 if PDU was sent OK (still pending )
731 0 if PDU was not sent OK (nothing to wait for)
734 static zoom_ret ZOOM_connection_send_init (ZOOM_connection c)
737 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest);
738 Z_InitRequest *ireq = apdu->u.initRequest;
739 Z_IdAuthentication *auth = (Z_IdAuthentication *)
740 odr_malloc(c->odr_out, sizeof(*auth));
741 const char *auth_groupId = ZOOM_options_get (c->options, "group");
742 const char *auth_userId = ZOOM_options_get (c->options, "user");
743 const char *auth_password = ZOOM_options_get (c->options, "pass");
745 ODR_MASK_SET(ireq->options, Z_Options_search);
746 ODR_MASK_SET(ireq->options, Z_Options_present);
747 ODR_MASK_SET(ireq->options, Z_Options_scan);
748 ODR_MASK_SET(ireq->options, Z_Options_sort);
749 ODR_MASK_SET(ireq->options, Z_Options_extendedServices);
750 ODR_MASK_SET(ireq->options, Z_Options_namedResultSets);
752 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
753 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
754 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
756 impname = ZOOM_options_get (c->options, "implementationName");
757 ireq->implementationName =
758 (char *) odr_malloc (c->odr_out, 15 + (impname ? strlen(impname) : 0));
759 strcpy (ireq->implementationName, "");
762 strcat (ireq->implementationName, impname);
763 strcat (ireq->implementationName, "/");
765 strcat (ireq->implementationName, "ZOOM-C/YAZ");
767 *ireq->maximumRecordSize =
768 ZOOM_options_get_int (c->options, "maximumRecordSize", 1024*1024);
769 *ireq->preferredMessageSize =
770 ZOOM_options_get_int (c->options, "preferredMessageSize", 1024*1024);
772 if (auth_groupId || auth_password)
774 Z_IdPass *pass = (Z_IdPass *) odr_malloc(c->odr_out, sizeof(*pass));
777 if (auth_groupId && *auth_groupId)
779 pass->groupId = (char *)
780 odr_malloc(c->odr_out, strlen(auth_groupId)+1);
781 strcpy(pass->groupId, auth_groupId);
785 if (auth_userId && *auth_userId)
787 pass->userId = (char *)
788 odr_malloc(c->odr_out, strlen(auth_userId)+1);
789 strcpy(pass->userId, auth_userId);
793 if (auth_password && *auth_password)
795 pass->password = (char *)
796 odr_malloc(c->odr_out, strlen(auth_password)+1);
797 strcpy(pass->password, auth_password);
802 auth->which = Z_IdAuthentication_idPass;
803 auth->u.idPass = pass;
804 ireq->idAuthentication = auth;
807 else if (auth_userId)
809 auth->which = Z_IdAuthentication_open;
810 auth->u.open = (char *)
811 odr_malloc(c->odr_out, strlen(auth_userId)+1);
812 strcpy(auth->u.open, auth_userId);
813 ireq->idAuthentication = auth;
816 yaz_oi_set_string_oidval(&ireq->otherInfo, c->odr_out,
817 VAL_PROXY, 1, c->host_port);
818 if (c->charset||c->lang)
820 Z_OtherInformation **oi;
821 Z_OtherInformationUnit *oi_unit;
823 yaz_oi_APDU(apdu, &oi);
825 if ((oi_unit = yaz_oi_update(oi, c->odr_out, NULL, 0, 0)))
827 ODR_MASK_SET(ireq->options, Z_Options_negotiationModel);
829 oi_unit->which = Z_OtherInfo_externallyDefinedInfo;
830 oi_unit->information.externallyDefinedInfo =
831 yaz_set_proposal_charneg
833 (const char **)&c->charset, (c->charset) ? 1:0,
834 (const char **)&c->lang, (c->lang) ? 1:0, 1);
838 return send_APDU (c, apdu);
841 static zoom_ret ZOOM_connection_send_search (ZOOM_connection c)
844 int lslb, ssub, mspn;
846 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
847 Z_SearchRequest *search_req = apdu->u.searchRequest;
848 const char *elementSetName;
849 const char *smallSetElementSetName;
850 const char *mediumSetElementSetName;
854 assert (c->tasks->which == ZOOM_TASK_SEARCH);
856 r = c->tasks->u.search.resultset;
859 ZOOM_options_get (r->options, "elementSetName");
860 smallSetElementSetName =
861 ZOOM_options_get (r->options, "smallSetElementSetName");
862 mediumSetElementSetName =
863 ZOOM_options_get (r->options, "mediumSetElementSetName");
865 ZOOM_options_get (r->options, "schema");
867 if (!smallSetElementSetName)
868 smallSetElementSetName = elementSetName;
870 if (!mediumSetElementSetName)
871 mediumSetElementSetName = elementSetName;
876 /* prepare query for the search request */
877 search_req->query = r->r_query;
879 search_req->databaseNames =
880 set_DatabaseNames (c, r->options, &search_req->num_databaseNames);
882 /* get syntax (no need to provide unless piggyback is in effect) */
883 syntax = ZOOM_options_get (r->options, "preferredRecordSyntax");
885 lslb = ZOOM_options_get_int (r->options, "largeSetLowerBound", -1);
886 ssub = ZOOM_options_get_int (r->options, "smallSetUpperBound", -1);
887 mspn = ZOOM_options_get_int (r->options, "mediumSetPresentNumber", -1);
888 if (lslb != -1 && ssub != -1 && mspn != -1)
890 /* So're a Z39.50 expert? Let's hope you don't do sort */
891 *search_req->largeSetLowerBound = lslb;
892 *search_req->smallSetUpperBound = ssub;
893 *search_req->mediumSetPresentNumber = mspn;
895 else if (r->start == 0 && r->count > 0
896 && r->piggyback && !r->r_sort_spec && !schema)
898 /* Regular piggyback - do it unless we're going to do sort */
899 *search_req->largeSetLowerBound = 2000000000;
900 *search_req->smallSetUpperBound = r->count;
901 *search_req->mediumSetPresentNumber = r->count;
902 smallSetElementSetName = 0; /* no need to provide this */
906 /* non-piggyback. Need not provide elementsets or syntaxes .. */
907 smallSetElementSetName = 0;
908 mediumSetElementSetName = 0;
911 if (smallSetElementSetName && *smallSetElementSetName)
913 Z_ElementSetNames *esn = (Z_ElementSetNames *)
914 odr_malloc (c->odr_out, sizeof(*esn));
916 esn->which = Z_ElementSetNames_generic;
917 esn->u.generic = odr_strdup (c->odr_out, smallSetElementSetName);
918 search_req->smallSetElementSetNames = esn;
920 if (mediumSetElementSetName && *mediumSetElementSetName)
922 Z_ElementSetNames *esn = (Z_ElementSetNames *)
923 odr_malloc (c->odr_out, sizeof(*esn));
925 esn->which = Z_ElementSetNames_generic;
926 esn->u.generic = odr_strdup (c->odr_out, mediumSetElementSetName);
927 search_req->mediumSetElementSetNames = esn;
930 search_req->preferredRecordSyntax =
931 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
935 if (c->support_named_resultsets)
939 /* find the lowest unused ordinal so that we re-use
940 result sets on the server. */
941 for (ord = 1; ; ord++)
944 sprintf (setname, "%d", ord);
945 for (rp = c->resultsets; rp; rp = rp->next)
946 if (rp->setname && !strcmp (rp->setname, setname))
951 r->setname = xstrdup (setname);
952 yaz_log (LOG_DEBUG, "allocating set %s", r->setname);
955 r->setname = xstrdup ("default");
956 ZOOM_options_set (r->options, "setname", r->setname);
958 search_req->resultSetName = odr_strdup(c->odr_out, r->setname);
959 /* send search request */
960 return send_APDU (c, apdu);
963 static void response_diag (ZOOM_connection c, Z_DiagRec *p)
965 Z_DefaultDiagFormat *r;
970 if (p->which != Z_DiagRec_defaultFormat)
972 c->error = ZOOM_ERROR_DECODE;
975 r = p->u.defaultFormat;
978 case Z_DefaultDiagFormat_v2Addinfo:
979 addinfo = r->u.v2Addinfo;
981 case Z_DefaultDiagFormat_v3Addinfo:
982 addinfo = r->u.v3Addinfo;
986 c->addinfo = xstrdup (addinfo);
987 c->error = *r->condition;
990 ZOOM_API(ZOOM_record)
991 ZOOM_record_clone (ZOOM_record srec)
998 odr_enc = odr_createmem(ODR_ENCODE);
999 if (!z_NamePlusRecord (odr_enc, &srec->npr, 0, 0))
1001 buf = odr_getbuf (odr_enc, &size, 0);
1003 nrec = (ZOOM_record) xmalloc (sizeof(*nrec));
1004 nrec->odr = odr_createmem(ODR_DECODE);
1005 nrec->wrbuf_marc = 0;
1006 odr_setbuf (nrec->odr, buf, size, 0);
1007 z_NamePlusRecord (nrec->odr, &nrec->npr, 0, 0);
1009 odr_destroy (odr_enc);
1013 ZOOM_API(ZOOM_record)
1014 ZOOM_resultset_record_immediate (ZOOM_resultset s,size_t pos)
1016 return record_cache_lookup (s, pos);
1019 ZOOM_API(ZOOM_record)
1020 ZOOM_resultset_record (ZOOM_resultset r, size_t pos)
1022 ZOOM_resultset_retrieve (r, 1, pos, 1);
1023 return ZOOM_resultset_record_immediate (r, pos);
1027 ZOOM_record_destroy (ZOOM_record rec)
1031 if (rec->wrbuf_marc)
1032 wrbuf_free (rec->wrbuf_marc, 1);
1033 odr_destroy (rec->odr);
1037 ZOOM_API(const char *)
1038 ZOOM_record_get (ZOOM_record rec, const char *type, int *len)
1040 Z_NamePlusRecord *npr;
1043 *len = 0; /* default return */
1050 if (!strcmp (type, "database"))
1053 *len = (npr->databaseName ? strlen(npr->databaseName) : 0);
1054 return npr->databaseName;
1056 else if (!strcmp (type, "syntax"))
1058 const char *desc = 0;
1059 if (npr->which == Z_NamePlusRecord_databaseRecord)
1061 Z_External *r = (Z_External *) npr->u.databaseRecord;
1062 oident *ent = oid_getentbyoid(r->direct_reference);
1069 *len = strlen(desc);
1072 else if (!strcmp (type, "render") &&
1073 npr->which == Z_NamePlusRecord_databaseRecord)
1075 Z_External *r = (Z_External *) npr->u.databaseRecord;
1076 oident *ent = oid_getentbyoid(r->direct_reference);
1078 if (r->which == Z_External_sutrs)
1080 if (len) *len = r->u.sutrs->len;
1081 return (const char *) r->u.sutrs->buf;
1083 else if (r->which == Z_External_octet)
1092 case VAL_APPLICATION_XML:
1095 if (!rec->wrbuf_marc)
1096 rec->wrbuf_marc = wrbuf_alloc();
1097 wrbuf_rewind (rec->wrbuf_marc);
1098 if (yaz_marc_decode ((const char *)
1099 r->u.octet_aligned->buf,
1101 r->u.octet_aligned->len,
1104 if (len) *len = wrbuf_len(rec->wrbuf_marc);
1105 return wrbuf_buf(rec->wrbuf_marc);
1108 if (len) *len = r->u.octet_aligned->len;
1109 return (const char *) r->u.octet_aligned->buf;
1111 else if (r->which == Z_External_grs1)
1113 if (!rec->wrbuf_marc)
1114 rec->wrbuf_marc = wrbuf_alloc();
1115 wrbuf_rewind (rec->wrbuf_marc);
1116 yaz_display_grs1(rec->wrbuf_marc, r->u.grs1, 0);
1118 *len = wrbuf_len(rec->wrbuf_marc);
1119 return wrbuf_buf(rec->wrbuf_marc);
1123 else if (npr->which == Z_NamePlusRecord_databaseRecord &&
1124 (!strcmp (type, "xml") || !strcmp(type, "MarcXML")))
1126 Z_External *r = (Z_External *) npr->u.databaseRecord;
1127 oident *ent = oid_getentbyoid(r->direct_reference);
1129 if (r->which == Z_External_sutrs)
1131 if (len) *len = r->u.sutrs->len;
1132 return (const char *) r->u.sutrs->buf;
1134 else if (r->which == Z_External_octet)
1136 int marc_decode_type = YAZ_MARC_OAIMARC;
1138 if (!strcmp(type, "MarcXML"))
1139 marc_decode_type = YAZ_MARC_MARCXML;
1147 case VAL_APPLICATION_XML:
1150 if (!rec->wrbuf_marc)
1151 rec->wrbuf_marc = wrbuf_alloc();
1152 wrbuf_rewind (rec->wrbuf_marc);
1153 if (yaz_marc_decode ((const char *)
1154 r->u.octet_aligned->buf,
1156 r->u.octet_aligned->len,
1157 marc_decode_type) > 0)
1159 if (len) *len = wrbuf_len(rec->wrbuf_marc);
1160 return wrbuf_buf(rec->wrbuf_marc);
1163 if (len) *len = r->u.octet_aligned->len;
1164 return (const char *) r->u.octet_aligned->buf;
1166 else if (r->which == Z_External_grs1)
1173 else if (!strcmp (type, "raw"))
1175 if (npr->which == Z_NamePlusRecord_databaseRecord)
1177 Z_External *r = (Z_External *) npr->u.databaseRecord;
1179 if (r->which == Z_External_sutrs)
1181 if (len) *len = r->u.sutrs->len;
1182 return (const char *) r->u.sutrs->buf;
1184 else if (r->which == Z_External_octet)
1186 if (len) *len = r->u.octet_aligned->len;
1187 return (const char *) r->u.octet_aligned->buf;
1189 else /* grs-1, explain, ... */
1192 return (const char *) npr->u.databaseRecord;
1197 else if (!strcmp (type, "ext"))
1199 if (npr->which == Z_NamePlusRecord_databaseRecord)
1200 return (const char *) npr->u.databaseRecord;
1206 static void record_cache_add (ZOOM_resultset r, Z_NamePlusRecord *npr, int pos)
1208 ZOOM_record_cache rc;
1209 const char *elementSetName =
1210 ZOOM_resultset_option_get (r, "elementSetName");
1211 const char *syntax =
1212 ZOOM_resultset_option_get (r, "preferredRecordSyntax");
1215 for (rc = r->record_cache; rc; rc = rc->next)
1219 if ((!elementSetName && !rc->elementSetName)
1220 || (elementSetName && rc->elementSetName &&
1221 !strcmp (elementSetName, rc->elementSetName)))
1223 if ((!syntax && !rc->syntax)
1224 || (syntax && rc->syntax &&
1225 !strcmp (syntax, rc->syntax)))
1227 /* not destroying rc->npr (it's handled by nmem )*/
1229 /* keeping wrbuf_marc too */
1235 rc = (ZOOM_record_cache) odr_malloc (r->odr, sizeof(*rc));
1238 rc->rec.wrbuf_marc = 0;
1240 rc->elementSetName = odr_strdup (r->odr, elementSetName);
1242 rc->elementSetName = 0;
1245 rc->syntax = odr_strdup (r->odr, syntax);
1250 rc->next = r->record_cache;
1251 r->record_cache = rc;
1254 static ZOOM_record record_cache_lookup (ZOOM_resultset r, int pos)
1256 ZOOM_record_cache rc;
1257 const char *elementSetName =
1258 ZOOM_resultset_option_get (r, "elementSetName");
1259 const char *syntax =
1260 ZOOM_resultset_option_get (r, "preferredRecordSyntax");
1262 for (rc = r->record_cache; rc; rc = rc->next)
1266 if ((!elementSetName && !rc->elementSetName)
1267 || (elementSetName && rc->elementSetName &&
1268 !strcmp (elementSetName, rc->elementSetName)))
1270 if ((!syntax && !rc->syntax)
1271 || (syntax && rc->syntax &&
1272 !strcmp (syntax, rc->syntax)))
1280 static void handle_records (ZOOM_connection c, Z_Records *sr,
1283 ZOOM_resultset resultset;
1287 switch (c->tasks->which)
1289 case ZOOM_TASK_SEARCH:
1290 resultset = c->tasks->u.search.resultset;
1292 case ZOOM_TASK_RETRIEVE:
1293 resultset = c->tasks->u.retrieve.resultset;
1298 if (sr && sr->which == Z_Records_NSD)
1300 Z_DiagRec dr, *dr_p = &dr;
1301 dr.which = Z_DiagRec_defaultFormat;
1302 dr.u.defaultFormat = sr->u.nonSurrogateDiagnostic;
1304 response_diag (c, dr_p);
1306 else if (sr && sr->which == Z_Records_multipleNSD)
1308 if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
1309 response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
1311 c->error = ZOOM_ERROR_DECODE;
1315 if (resultset->count + resultset->start > resultset->size)
1316 resultset->count = resultset->size - resultset->start;
1317 if (resultset->count < 0)
1318 resultset->count = 0;
1319 if (sr && sr->which == Z_Records_DBOSD)
1322 NMEM nmem = odr_extract_mem (c->odr_in);
1323 Z_NamePlusRecordList *p =
1324 sr->u.databaseOrSurDiagnostics;
1325 for (i = 0; i<p->num_records; i++)
1327 record_cache_add (resultset, p->records[i],
1328 i+ resultset->start);
1330 /* transfer our response to search_nmem .. we need it later */
1331 nmem_transfer (resultset->odr->mem, nmem);
1332 nmem_destroy (nmem);
1333 if (present_phase && p->num_records == 0)
1335 /* present response and we didn't get any records! */
1336 c->error = ZOOM_ERROR_DECODE;
1339 else if (present_phase)
1341 /* present response and we didn't get any records! */
1342 c->error = ZOOM_ERROR_DECODE;
1347 static void handle_present_response (ZOOM_connection c, Z_PresentResponse *pr)
1349 handle_records (c, pr->records, 1);
1352 static void handle_search_response (ZOOM_connection c, Z_SearchResponse *sr)
1354 ZOOM_resultset resultset;
1356 yaz_log (LOG_DEBUG, "got search response");
1358 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1361 resultset = c->tasks->u.search.resultset;
1363 resultset->size = *sr->resultCount;
1364 handle_records (c, sr->records, 0);
1367 static void sort_response (ZOOM_connection c, Z_SortResponse *res)
1369 if (res->diagnostics && res->num_diagnostics > 0)
1370 response_diag (c, res->diagnostics[0]);
1373 static int scan_response (ZOOM_connection c, Z_ScanResponse *res)
1375 NMEM nmem = odr_extract_mem (c->odr_in);
1378 if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
1380 scan = c->tasks->u.scan.scan;
1382 if (res->entries && res->entries->nonsurrogateDiagnostics)
1383 response_diag(c, res->entries->nonsurrogateDiagnostics[0]);
1384 scan->scan_response = res;
1385 nmem_transfer (scan->odr->mem, nmem);
1387 ZOOM_options_set_int (scan->options, "stepSize", *res->stepSize);
1388 if (res->positionOfTerm)
1389 ZOOM_options_set_int (scan->options, "position", *res->positionOfTerm);
1390 if (res->scanStatus)
1391 ZOOM_options_set_int (scan->options, "scanStatus", *res->scanStatus);
1392 if (res->numberOfEntriesReturned)
1393 ZOOM_options_set_int (scan->options, "number",
1394 *res->numberOfEntriesReturned);
1395 nmem_destroy (nmem);
1399 static zoom_ret send_sort (ZOOM_connection c)
1401 ZOOM_resultset resultset;
1403 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1404 return zoom_complete;
1406 resultset = c->tasks->u.search.resultset;
1410 resultset->r_sort_spec = 0;
1411 return zoom_complete;
1413 if (resultset->r_sort_spec)
1415 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
1416 Z_SortRequest *req = apdu->u.sortRequest;
1418 req->num_inputResultSetNames = 1;
1419 req->inputResultSetNames = (Z_InternationalString **)
1420 odr_malloc (c->odr_out, sizeof(*req->inputResultSetNames));
1421 req->inputResultSetNames[0] =
1422 odr_strdup (c->odr_out, resultset->setname);
1423 req->sortedResultSetName = odr_strdup (c->odr_out, resultset->setname);
1424 req->sortSequence = resultset->r_sort_spec;
1425 resultset->r_sort_spec = 0;
1426 return send_APDU (c, apdu);
1428 return zoom_complete;
1431 static zoom_ret send_present (ZOOM_connection c)
1434 Z_PresentRequest *req = 0;
1436 const char *syntax = 0;
1437 const char *elementSetName = 0;
1438 const char *schema = 0;
1439 ZOOM_resultset resultset;
1442 return zoom_complete;
1444 switch (c->tasks->which)
1446 case ZOOM_TASK_SEARCH:
1447 resultset = c->tasks->u.search.resultset;
1449 case ZOOM_TASK_RETRIEVE:
1450 resultset = c->tasks->u.retrieve.resultset;
1451 resultset->start = c->tasks->u.retrieve.start;
1452 resultset->count = c->tasks->u.retrieve.count;
1454 if (resultset->start >= resultset->size)
1455 return zoom_complete;
1456 if (resultset->start + resultset->count > resultset->size)
1457 resultset->count = resultset->size - resultset->start;
1460 return zoom_complete;
1463 syntax = ZOOM_resultset_option_get (resultset, "preferredRecordSyntax");
1464 elementSetName = ZOOM_resultset_option_get (resultset, "elementSetName");
1465 schema = ZOOM_resultset_option_get (resultset, "schema");
1467 if (c->error) /* don't continue on error */
1468 return zoom_complete;
1469 if (resultset->start < 0)
1470 return zoom_complete;
1471 for (i = 0; i<resultset->count; i++)
1474 record_cache_lookup (resultset, i + resultset->start);
1478 if (i == resultset->count)
1479 return zoom_complete;
1481 apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
1482 req = apdu->u.presentRequest;
1484 resultset->start += i;
1485 resultset->count -= i;
1486 *req->resultSetStartPoint = resultset->start + 1;
1487 *req->numberOfRecordsRequested = resultset->count;
1488 assert (*req->numberOfRecordsRequested > 0);
1490 if (syntax && *syntax)
1491 req->preferredRecordSyntax =
1492 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
1494 if (schema && *schema)
1496 Z_RecordComposition *compo = (Z_RecordComposition *)
1497 odr_malloc (c->odr_out, sizeof(*compo));
1499 req->recordComposition = compo;
1500 compo->which = Z_RecordComp_complex;
1501 compo->u.complex = (Z_CompSpec *)
1502 odr_malloc(c->odr_out, sizeof(*compo->u.complex));
1503 compo->u.complex->selectAlternativeSyntax = (bool_t *)
1504 odr_malloc(c->odr_out, sizeof(bool_t));
1505 *compo->u.complex->selectAlternativeSyntax = 0;
1507 compo->u.complex->generic = (Z_Specification *)
1508 odr_malloc(c->odr_out, sizeof(*compo->u.complex->generic));
1510 compo->u.complex->generic->schema = (Odr_oid *)
1511 yaz_str_to_z3950oid (c->odr_out, CLASS_SCHEMA, schema);
1513 if (!compo->u.complex->generic->schema)
1515 /* OID wasn't a schema! Try record syntax instead. */
1517 compo->u.complex->generic->schema = (Odr_oid *)
1518 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, schema);
1520 if (elementSetName && *elementSetName)
1522 compo->u.complex->generic->elementSpec = (Z_ElementSpec *)
1523 odr_malloc(c->odr_out, sizeof(Z_ElementSpec));
1524 compo->u.complex->generic->elementSpec->which =
1525 Z_ElementSpec_elementSetName;
1526 compo->u.complex->generic->elementSpec->u.elementSetName =
1527 odr_strdup (c->odr_out, elementSetName);
1530 compo->u.complex->generic->elementSpec = 0;
1531 compo->u.complex->num_dbSpecific = 0;
1532 compo->u.complex->dbSpecific = 0;
1533 compo->u.complex->num_recordSyntax = 0;
1534 compo->u.complex->recordSyntax = 0;
1536 else if (elementSetName && *elementSetName)
1538 Z_ElementSetNames *esn = (Z_ElementSetNames *)
1539 odr_malloc (c->odr_out, sizeof(*esn));
1540 Z_RecordComposition *compo = (Z_RecordComposition *)
1541 odr_malloc (c->odr_out, sizeof(*compo));
1543 esn->which = Z_ElementSetNames_generic;
1544 esn->u.generic = odr_strdup (c->odr_out, elementSetName);
1545 compo->which = Z_RecordComp_simple;
1546 compo->u.simple = esn;
1547 req->recordComposition = compo;
1549 req->resultSetId = odr_strdup(c->odr_out, resultset->setname);
1550 return send_APDU (c, apdu);
1553 ZOOM_API(ZOOM_scanset)
1554 ZOOM_connection_scan (ZOOM_connection c, const char *start)
1556 ZOOM_scanset scan = (ZOOM_scanset) xmalloc (sizeof(*scan));
1558 scan->connection = c;
1559 scan->odr = odr_createmem (ODR_DECODE);
1560 scan->options = ZOOM_options_create_with_parent (c->options);
1562 scan->scan_response = 0;
1564 if ((scan->termListAndStartPoint =
1565 p_query_scan(scan->odr, PROTO_Z3950, &scan->attributeSet,
1568 ZOOM_task task = ZOOM_connection_add_task (c, ZOOM_TASK_SCAN);
1569 task->u.scan.scan = scan;
1574 while (ZOOM_event (1, &c))
1582 ZOOM_scanset_destroy (ZOOM_scanset scan)
1587 if (scan->refcount == 0)
1589 odr_destroy (scan->odr);
1591 ZOOM_options_destroy (scan->options);
1596 static zoom_ret send_package (ZOOM_connection c)
1600 return zoom_complete;
1601 assert (c->tasks->which == ZOOM_TASK_PACKAGE);
1603 event = ZOOM_Event_create (ZOOM_EVENT_SEND_APDU);
1604 ZOOM_connection_put_event (c, event);
1606 return do_write_ex (c, c->tasks->u.package->buf_out,
1607 c->tasks->u.package->len_out);
1610 static zoom_ret send_scan (ZOOM_connection c)
1613 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_scanRequest);
1614 Z_ScanRequest *req = apdu->u.scanRequest;
1616 return zoom_complete;
1617 assert (c->tasks->which == ZOOM_TASK_SCAN);
1618 scan = c->tasks->u.scan.scan;
1620 req->termListAndStartPoint = scan->termListAndStartPoint;
1621 req->attributeSet = scan->attributeSet;
1623 *req->numberOfTermsRequested =
1624 ZOOM_options_get_int(scan->options, "number", 10);
1626 req->preferredPositionInResponse =
1627 odr_intdup (c->odr_out,
1628 ZOOM_options_get_int(scan->options, "position", 1));
1631 odr_intdup (c->odr_out,
1632 ZOOM_options_get_int(scan->options, "stepSize", 0));
1634 req->databaseNames = set_DatabaseNames (c, scan->options,
1635 &req->num_databaseNames);
1637 return send_APDU (c, apdu);
1641 ZOOM_scanset_size (ZOOM_scanset scan)
1643 if (!scan || !scan->scan_response || !scan->scan_response->entries)
1645 return scan->scan_response->entries->num_entries;
1648 ZOOM_API(const char *)
1649 ZOOM_scanset_term (ZOOM_scanset scan, size_t pos,
1652 const char *term = 0;
1653 size_t noent = ZOOM_scanset_size (scan);
1654 Z_ScanResponse *res = scan->scan_response;
1660 if (res->entries->entries[pos]->which == Z_Entry_termInfo)
1662 Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
1664 if (t->term->which == Z_Term_general)
1666 term = (const char *) t->term->u.general->buf;
1667 *len = t->term->u.general->len;
1669 *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
1674 ZOOM_API(const char *)
1675 ZOOM_scanset_option_get (ZOOM_scanset scan, const char *key)
1677 return ZOOM_options_get (scan->options, key);
1681 ZOOM_scanset_option_set (ZOOM_scanset scan, const char *key,
1684 ZOOM_options_set (scan->options, key, val);
1687 static Z_APDU *create_es_package (ZOOM_package p, int type)
1690 Z_APDU *apdu = zget_APDU(p->odr_out, Z_APDU_extendedServicesRequest);
1691 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
1693 *req->function = Z_ExtendedServicesRequest_create;
1695 str = ZOOM_options_get(p->options, "package-name");
1697 req->packageName = nmem_strdup (p->odr_out->mem, str);
1699 str = ZOOM_options_get(p->options, "user-id");
1701 req->userId = nmem_strdup (p->odr_out->mem, str);
1703 req->packageType = yaz_oidval_to_z3950oid(p->odr_out, CLASS_EXTSERV,
1706 str = ZOOM_options_get(p->options, "function");
1709 if (!strcmp (str, "create"))
1711 if (!strcmp (str, "delete"))
1713 if (!strcmp (str, "modify"))
1719 static const char *ill_array_lookup (void *clientData, const char *idx)
1721 ZOOM_package p = (ZOOM_package) clientData;
1722 return ZOOM_options_get (p->options, idx+4);
1725 static Z_External *encode_ill_request (ZOOM_package p)
1727 ODR out = p->odr_out;
1730 struct ill_get_ctl ctl;
1732 ctl.odr = p->odr_out;
1734 ctl.f = ill_array_lookup;
1736 req = ill_get_ILLRequest(&ctl, "ill", 0);
1738 if (!ill_Request (out, &req, 0, 0))
1740 int ill_request_size;
1741 char *ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
1742 if (ill_request_buf)
1743 odr_setbuf (out, ill_request_buf, ill_request_size, 1);
1749 int illRequest_size = 0;
1750 char *illRequest_buf = odr_getbuf (out, &illRequest_size, 0);
1752 oid.proto = PROTO_GENERAL;
1753 oid.oclass = CLASS_GENERAL;
1754 oid.value = VAL_ISO_ILL_1;
1756 r = (Z_External *) odr_malloc (out, sizeof(*r));
1757 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid));
1758 r->indirect_reference = 0;
1760 r->which = Z_External_single;
1762 r->u.single_ASN1_type = (Odr_oct *)
1763 odr_malloc (out, sizeof(*r->u.single_ASN1_type));
1764 r->u.single_ASN1_type->buf = (unsigned char*)
1765 odr_malloc (out, illRequest_size);
1766 r->u.single_ASN1_type->len = illRequest_size;
1767 r->u.single_ASN1_type->size = illRequest_size;
1768 memcpy (r->u.single_ASN1_type->buf, illRequest_buf, illRequest_size);
1773 static Z_ItemOrder *encode_item_order(ZOOM_package p)
1775 Z_ItemOrder *req = (Z_ItemOrder *) odr_malloc (p->odr_out, sizeof(*req));
1778 req->which=Z_IOItemOrder_esRequest;
1779 req->u.esRequest = (Z_IORequest *)
1780 odr_malloc(p->odr_out,sizeof(Z_IORequest));
1782 /* to keep part ... */
1783 req->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
1784 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartToKeep));
1785 req->u.esRequest->toKeep->supplDescription = 0;
1786 req->u.esRequest->toKeep->contact = (Z_IOContact *)
1787 odr_malloc (p->odr_out, sizeof(*req->u.esRequest->toKeep->contact));
1789 str = ZOOM_options_get(p->options, "contact-name");
1790 req->u.esRequest->toKeep->contact->name = str ?
1791 nmem_strdup (p->odr_out->mem, str) : 0;
1793 str = ZOOM_options_get(p->options, "contact-phone");
1794 req->u.esRequest->toKeep->contact->phone = str ?
1795 nmem_strdup (p->odr_out->mem, str) : 0;
1797 str = ZOOM_options_get(p->options, "contact-email");
1798 req->u.esRequest->toKeep->contact->email = str ?
1799 nmem_strdup (p->odr_out->mem, str) : 0;
1801 req->u.esRequest->toKeep->addlBilling = 0;
1803 /* not to keep part ... */
1804 req->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
1805 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartNotToKeep));
1807 req->u.esRequest->notToKeep->resultSetItem = (Z_IOResultSetItem *)
1808 odr_malloc(p->odr_out, sizeof(Z_IOResultSetItem));
1810 str = ZOOM_options_get(p->options, "itemorder-setname");
1813 req->u.esRequest->notToKeep->resultSetItem->resultSetId =
1814 nmem_strdup (p->odr_out->mem, str);
1815 req->u.esRequest->notToKeep->resultSetItem->item =
1816 (int *) odr_malloc(p->odr_out, sizeof(int));
1818 str = ZOOM_options_get(p->options, "itemorder-item");
1819 *req->u.esRequest->notToKeep->resultSetItem->item =
1820 (str ? atoi(str) : 1);
1822 req->u.esRequest->notToKeep->itemRequest = encode_ill_request(p);
1828 ZOOM_package_send (ZOOM_package p, const char *type)
1835 odr_reset (p->odr_out);
1838 if (!strcmp(type, "itemorder"))
1841 apdu = create_es_package (p, VAL_ITEMORDER);
1844 r = (Z_External *) odr_malloc (p->odr_out, sizeof(*r));
1846 r->direct_reference =
1847 yaz_oidval_to_z3950oid(p->odr_out, CLASS_EXTSERV,
1850 r->which = Z_External_itemOrder;
1851 r->indirect_reference = 0;
1852 r->u.itemOrder = encode_item_order (p);
1854 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
1859 if (encode_APDU(p->connection, apdu, p->odr_out) == 0)
1863 ZOOM_task task = ZOOM_connection_add_task (c, ZOOM_TASK_PACKAGE);
1864 task->u.package = p;
1865 buf = odr_getbuf(p->odr_out, &p->len_out, 0);
1866 p->buf_out = (char *) xmalloc (p->len_out);
1867 memcpy (p->buf_out, buf, p->len_out);
1872 while (ZOOM_event (1, &c))
1879 ZOOM_API(ZOOM_package)
1880 ZOOM_connection_package (ZOOM_connection c, ZOOM_options options)
1882 ZOOM_package p = (ZOOM_package) xmalloc (sizeof(*p));
1885 p->odr_out = odr_createmem (ODR_ENCODE);
1886 p->options = ZOOM_options_create_with_parent2 (options, c->options);
1894 ZOOM_package_destroy(ZOOM_package p)
1899 if (p->refcount == 0)
1901 odr_destroy (p->odr_out);
1904 ZOOM_options_destroy (p->options);
1909 ZOOM_API(const char *)
1910 ZOOM_package_option_get (ZOOM_package p, const char *key)
1912 return ZOOM_options_get (p->options, key);
1916 ZOOM_package_option_set (ZOOM_package p, const char *key,
1919 ZOOM_options_set (p->options, key, val);
1922 static int ZOOM_connection_exec_task (ZOOM_connection c)
1924 ZOOM_task task = c->tasks;
1925 zoom_ret ret = zoom_complete;
1929 yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task task=<null>");
1932 yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task type=%d run=%d",
1933 task->which, task->running);
1934 if (c->error != ZOOM_ERROR_NONE ||
1935 (!c->cs && task->which != ZOOM_TASK_CONNECT))
1937 yaz_log (LOG_DEBUG, "remove tasks because of error = %d", c->error);
1938 ZOOM_connection_remove_tasks (c);
1943 yaz_log (LOG_DEBUG, "task already running");
1947 switch (task->which)
1949 case ZOOM_TASK_SEARCH:
1950 ret = ZOOM_connection_send_search (c);
1952 case ZOOM_TASK_RETRIEVE:
1953 ret = send_present (c);
1955 case ZOOM_TASK_CONNECT:
1956 ret = do_connect(c);
1958 case ZOOM_TASK_SCAN:
1961 case ZOOM_TASK_PACKAGE:
1962 ret = send_package(c);
1965 if (ret == zoom_complete)
1967 yaz_log (LOG_DEBUG, "task removed (complete)");
1968 ZOOM_connection_remove_task (c);
1971 yaz_log (LOG_DEBUG, "task pending");
1975 static zoom_ret send_sort_present (ZOOM_connection c)
1977 zoom_ret r = send_sort (c);
1978 if (r == zoom_complete)
1979 r = send_present (c);
1983 static int es_response (ZOOM_connection c,
1984 Z_ExtendedServicesResponse *res)
1986 if (!c->tasks || c->tasks->which != ZOOM_TASK_PACKAGE)
1988 if (res->diagnostics && res->num_diagnostics > 0)
1989 response_diag(c, res->diagnostics[0]);
1990 if (res->taskPackage &&
1991 res->taskPackage->which == Z_External_extendedService)
1993 Z_TaskPackage *taskPackage = res->taskPackage->u.extendedService;
1994 Odr_oct *id = taskPackage->targetReference;
1997 ZOOM_options_setl (c->tasks->u.package->options,
1998 "targetReference", (char*) id->buf, id->len);
2004 static void handle_apdu (ZOOM_connection c, Z_APDU *apdu)
2006 Z_InitResponse *initrs;
2009 yaz_log (LOG_DEBUG, "handle_apdu type=%d", apdu->which);
2012 case Z_APDU_initResponse:
2013 initrs = apdu->u.initResponse;
2014 if (!*initrs->result)
2016 c->error = ZOOM_ERROR_INIT;
2021 yaz_oi_get_string_oidval (&apdu->u.initResponse->otherInfo,
2023 xfree (c->cookie_in);
2026 c->cookie_in = xstrdup(cookie);
2027 if (ODR_MASK_GET(initrs->options, Z_Options_namedResultSets) &&
2028 ODR_MASK_GET(initrs->protocolVersion, Z_ProtocolVersion_3))
2029 c->support_named_resultsets = 1;
2032 assert (c->tasks->which == ZOOM_TASK_CONNECT);
2033 ZOOM_connection_remove_task (c);
2035 ZOOM_connection_exec_task (c);
2037 if (ODR_MASK_GET(initrs->options, Z_Options_negotiationModel))
2039 NMEM tmpmem = nmem_create();
2040 Z_CharSetandLanguageNegotiation *p =
2041 yaz_get_charneg_record(initrs->otherInfo);
2045 char *charset=NULL, *lang=NULL;
2048 yaz_get_response_charneg(tmpmem, p, &charset, &lang, &sel);
2049 yaz_log(LOG_DEBUG, "Target accepted: charset %s, "
2050 "language %s, select %d",
2051 charset ? charset : "none", lang ? lang : "none", sel);
2053 ZOOM_connection_option_set (c, "negotiation-charset",
2056 ZOOM_connection_option_set (c, "negotiation-lang",
2058 nmem_destroy(tmpmem);
2062 case Z_APDU_searchResponse:
2063 handle_search_response (c, apdu->u.searchResponse);
2064 if (send_sort_present (c) == zoom_complete)
2065 ZOOM_connection_remove_task (c);
2067 case Z_APDU_presentResponse:
2068 handle_present_response (c, apdu->u.presentResponse);
2069 if (send_present (c) == zoom_complete)
2070 ZOOM_connection_remove_task (c);
2072 case Z_APDU_sortResponse:
2073 sort_response (c, apdu->u.sortResponse);
2074 if (send_present (c) == zoom_complete)
2075 ZOOM_connection_remove_task (c);
2077 case Z_APDU_scanResponse:
2078 scan_response (c, apdu->u.scanResponse);
2079 ZOOM_connection_remove_task (c);
2081 case Z_APDU_extendedServicesResponse:
2082 es_response (c, apdu->u.extendedServicesResponse);
2083 ZOOM_connection_remove_task (c);
2086 if (c->reconnect_ok)
2089 c->tasks->running = 0;
2090 ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT);
2094 c->error = ZOOM_ERROR_CONNECTION_LOST;
2099 c->error = ZOOM_ERROR_DECODE;
2104 static int do_read (ZOOM_connection c)
2110 event = ZOOM_Event_create (ZOOM_EVENT_RECV_DATA);
2111 ZOOM_connection_put_event (c, event);
2113 yaz_log (LOG_DEBUG, "do_read len=%d", c->len_in);
2115 r = cs_get (c->cs, &c->buf_in, &c->len_in);
2120 if (c->reconnect_ok)
2123 c->reconnect_ok = 0;
2124 yaz_log (LOG_DEBUG, "reconnect read");
2125 c->tasks->running = 0;
2126 ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT);
2130 c->error= ZOOM_ERROR_CONNECTION_LOST;
2137 odr_reset (c->odr_in);
2138 odr_setbuf (c->odr_in, c->buf_in, r, 0);
2139 event = ZOOM_Event_create (ZOOM_EVENT_RECV_APDU);
2140 ZOOM_connection_put_event (c, event);
2141 if (!z_APDU (c->odr_in, &apdu, 0, 0))
2143 c->error = ZOOM_ERROR_DECODE;
2147 handle_apdu (c, apdu);
2148 c->reconnect_ok = 0;
2153 static zoom_ret do_write_ex (ZOOM_connection c, char *buf_out, int len_out)
2158 event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA);
2159 ZOOM_connection_put_event (c, event);
2161 yaz_log (LOG_DEBUG, "do_write_ex len=%d", len_out);
2162 if ((r=cs_put (c->cs, buf_out, len_out)) < 0)
2164 if (c->reconnect_ok)
2167 c->reconnect_ok = 0;
2168 yaz_log (LOG_DEBUG, "reconnect write");
2169 c->tasks->running = 0;
2170 ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT);
2171 return zoom_complete;
2173 if (c->state == STATE_CONNECTING)
2174 c->error = ZOOM_ERROR_CONNECT;
2176 c->error = ZOOM_ERROR_CONNECTION_LOST;
2178 return zoom_complete;
2182 c->mask = ZOOM_SELECT_EXCEPT;
2183 if (c->cs->io_pending & CS_WANT_WRITE)
2184 c->mask += ZOOM_SELECT_WRITE;
2185 if (c->cs->io_pending & CS_WANT_READ)
2186 c->mask += ZOOM_SELECT_READ;
2187 yaz_log (LOG_DEBUG, "do_write_ex 1 mask=%d", c->mask);
2191 c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT;
2192 yaz_log (LOG_DEBUG, "do_write_ex 2 mask=%d", c->mask);
2194 return zoom_pending;
2197 static zoom_ret do_write(ZOOM_connection c)
2199 return do_write_ex (c, c->buf_out, c->len_out);
2203 ZOOM_API(const char *)
2204 ZOOM_connection_option_get (ZOOM_connection c, const char *key)
2206 return ZOOM_options_get (c->options, key);
2210 ZOOM_connection_option_set (ZOOM_connection c, const char *key,
2213 ZOOM_options_set (c->options, key, val);
2216 ZOOM_API(const char *)
2217 ZOOM_resultset_option_get (ZOOM_resultset r, const char *key)
2219 return ZOOM_options_get (r->options, key);
2223 ZOOM_resultset_option_set (ZOOM_resultset r, const char *key,
2226 ZOOM_options_set (r->options, key, val);
2231 ZOOM_connection_errcode (ZOOM_connection c)
2233 return ZOOM_connection_error (c, 0, 0);
2236 ZOOM_API(const char *)
2237 ZOOM_connection_errmsg (ZOOM_connection c)
2240 ZOOM_connection_error (c, &msg, 0);
2244 ZOOM_API(const char *)
2245 ZOOM_connection_addinfo (ZOOM_connection c)
2247 const char *addinfo;
2248 ZOOM_connection_error (c, 0, &addinfo);
2252 ZOOM_API(const char *)
2253 ZOOM_diag_str (int error)
2257 case ZOOM_ERROR_NONE:
2259 case ZOOM_ERROR_CONNECT:
2260 return "Connect failed";
2261 case ZOOM_ERROR_MEMORY:
2262 return "Out of memory";
2263 case ZOOM_ERROR_ENCODE:
2264 return "Encoding failed";
2265 case ZOOM_ERROR_DECODE:
2266 return "Decoding failed";
2267 case ZOOM_ERROR_CONNECTION_LOST:
2268 return "Connection lost";
2269 case ZOOM_ERROR_INIT:
2270 return "Init rejected";
2271 case ZOOM_ERROR_INTERNAL:
2272 return "Internal failure";
2273 case ZOOM_ERROR_TIMEOUT:
2276 return diagbib1_str (error);
2281 ZOOM_connection_error (ZOOM_connection c, const char **cp,
2282 const char **addinfo)
2284 int error = c->error;
2287 *cp = ZOOM_diag_str(error);
2292 *addinfo = c->addinfo;
2299 static int ZOOM_connection_do_io(ZOOM_connection c, int mask)
2301 ZOOM_Event event = 0;
2302 int r = cs_look(c->cs);
2303 yaz_log (LOG_DEBUG, "ZOOM_connection_do_io c=%p mask=%d cs_look=%d",
2308 event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
2309 c->error = ZOOM_ERROR_CONNECT;
2311 ZOOM_connection_put_event (c, event);
2313 else if (r == CS_CONNECT)
2316 event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
2318 ret = cs_rcvconnect (c->cs);
2319 yaz_log (LOG_DEBUG, "cs_rcvconnect returned %d", ret);
2322 c->mask = ZOOM_SELECT_EXCEPT;
2323 if (c->cs->io_pending & CS_WANT_WRITE)
2324 c->mask += ZOOM_SELECT_WRITE;
2325 if (c->cs->io_pending & CS_WANT_READ)
2326 c->mask += ZOOM_SELECT_READ;
2327 ZOOM_connection_put_event (c, event);
2331 ZOOM_connection_put_event (c, event);
2332 ZOOM_connection_send_init (c);
2333 c->state = STATE_ESTABLISHED;
2337 c->error = ZOOM_ERROR_CONNECT;
2339 ZOOM_connection_put_event (c, event);
2344 if (mask & ZOOM_SELECT_READ)
2346 if (c->cs && (mask & ZOOM_SELECT_WRITE))
2353 ZOOM_connection_last_event(ZOOM_connection cs)
2356 return ZOOM_EVENT_NONE;
2357 return cs->last_event;
2361 ZOOM_event (int no, ZOOM_connection *cs)
2365 struct pollfd pollfds[1024];
2366 ZOOM_connection poll_cs[1024];
2369 fd_set input, output, except;
2374 for (i = 0; i<no; i++)
2376 ZOOM_connection c = cs[i];
2378 if (c && (event = ZOOM_connection_get_event(c)))
2380 ZOOM_Event_destroy (event);
2384 for (i = 0; i<no; i++)
2386 ZOOM_connection c = cs[i];
2388 if (c && ZOOM_connection_exec_task (c))
2390 if ((event = ZOOM_connection_get_event(c)))
2392 ZOOM_Event_destroy (event);
2405 for (i = 0; i<no; i++)
2407 ZOOM_connection c = cs[i];
2413 fd = z3950_connection_socket(c);
2414 mask = z3950_connection_mask(c);
2421 this_timeout = ZOOM_options_get_int (c->options, "timeout", -1);
2422 if (this_timeout != -1 && this_timeout < timeout)
2423 timeout = this_timeout;
2427 short poll_events = 0;
2429 if (mask & ZOOM_SELECT_READ)
2430 poll_events += POLLIN;
2431 if (mask & ZOOM_SELECT_WRITE)
2432 poll_events += POLLOUT;
2433 if (mask & ZOOM_SELECT_EXCEPT)
2434 poll_events += POLLERR;
2435 pollfds[nfds].fd = fd;
2436 pollfds[nfds].events = poll_events;
2437 pollfds[nfds].revents = 0;
2442 if (mask & ZOOM_SELECT_READ)
2444 FD_SET (fd, &input);
2447 if (mask & ZOOM_SELECT_WRITE)
2449 FD_SET (fd, &output);
2452 if (mask & ZOOM_SELECT_EXCEPT)
2454 FD_SET (fd, &except);
2459 if (timeout >= 5000)
2466 r = poll (pollfds, nfds, timeout * 1000);
2467 for (i = 0; i<nfds; i++)
2469 ZOOM_connection c = poll_cs[i];
2473 if (pollfds[i].revents & POLLIN)
2474 mask += ZOOM_SELECT_READ;
2475 if (pollfds[i].revents & POLLOUT)
2476 mask += ZOOM_SELECT_WRITE;
2477 if (pollfds[i].revents & POLLERR)
2478 mask += ZOOM_SELECT_EXCEPT;
2480 ZOOM_connection_do_io(c, mask);
2482 else if (r == 0 && c->mask)
2484 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
2485 /* timeout and this connection was waiting */
2486 c->error = ZOOM_ERROR_TIMEOUT;
2488 ZOOM_connection_put_event(c, event);
2492 tv.tv_sec = timeout;
2494 yaz_log (LOG_DEBUG, "select start");
2495 r = select (max_fd+1, &input, &output, &except, &tv);
2496 yaz_log (LOG_DEBUG, "select stop, returned r=%d", r);
2497 for (i = 0; i<no; i++)
2499 ZOOM_connection c = cs[i];
2504 fd = z3950_connection_socket(c);
2508 /* no timeout and real socket */
2509 if (FD_ISSET(fd, &input))
2510 mask += ZOOM_SELECT_READ;
2511 if (FD_ISSET(fd, &output))
2512 mask += ZOOM_SELECT_WRITE;
2513 if (FD_ISSET(fd, &except))
2514 mask += ZOOM_SELECT_EXCEPT;
2516 ZOOM_connection_do_io(c, mask);
2518 if (r == 0 && c->mask)
2520 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
2521 /* timeout and this connection was waiting */
2522 c->error = ZOOM_ERROR_TIMEOUT;
2524 yaz_log (LOG_DEBUG, "timeout");
2525 ZOOM_connection_put_event(c, event);
2529 for (i = 0; i<no; i++)
2531 ZOOM_connection c = cs[i];
2533 if (c && (event = ZOOM_connection_get_event(c)))
2535 ZOOM_Event_destroy (event);