2 * Copyright (c) 2000-2003, Index Data
3 * See the file LICENSE for details.
5 * $Id: zoom-c.c,v 1.17 2003-02-12 15:06:44 adam Exp $
7 * ZOOM layer for C, connections, result sets, queries.
13 #include <yaz/xmalloc.h>
14 #include <yaz/otherinfo.h>
16 #include <yaz/pquery.h>
17 #include <yaz/marcdisp.h>
18 #include <yaz/diagbib1.h>
19 #include <yaz/charneg.h>
32 static zoom_ret ZOOM_connection_send_init (ZOOM_connection c);
33 static zoom_ret do_write_ex (ZOOM_connection c, char *buf_out, int len_out);
35 static ZOOM_Event ZOOM_Event_create (int kind)
37 ZOOM_Event event = (ZOOM_Event) xmalloc (sizeof(*event));
44 static void ZOOM_Event_destroy (ZOOM_Event event)
49 static void ZOOM_connection_put_event (ZOOM_connection c, ZOOM_Event event)
53 c->m_queue_back->prev = event;
54 assert (c->m_queue_front);
58 assert (!c->m_queue_front);
59 c->m_queue_front = event;
61 event->next = c->m_queue_back;
63 c->m_queue_back = event;
66 static ZOOM_Event ZOOM_connection_get_event(ZOOM_connection c)
68 ZOOM_Event event = c->m_queue_front;
71 assert (c->m_queue_back);
72 c->m_queue_front = event->prev;
75 assert (c->m_queue_back);
76 c->m_queue_front->next = 0;
80 c->last_event = event->kind;
84 static void set_bib1_error (ZOOM_connection c, int error)
92 static void set_ZOOM_error (ZOOM_connection c, int error,
100 c->addinfo = xstrdup(addinfo);
103 static void clear_error (ZOOM_connection c)
108 case ZOOM_ERROR_CONNECT:
109 case ZOOM_ERROR_MEMORY:
110 case ZOOM_ERROR_DECODE:
111 case ZOOM_ERROR_CONNECTION_LOST:
112 case ZOOM_ERROR_INIT:
113 case ZOOM_ERROR_INTERNAL:
114 case ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
117 set_ZOOM_error(c, ZOOM_ERROR_NONE, 0);
121 ZOOM_task ZOOM_connection_add_task (ZOOM_connection c, int which)
123 ZOOM_task *taskp = &c->tasks;
125 taskp = &(*taskp)->next;
126 *taskp = (ZOOM_task) xmalloc (sizeof(**taskp));
127 (*taskp)->running = 0;
128 (*taskp)->which = which;
134 ZOOM_task ZOOM_connection_insert_task (ZOOM_connection c, int which)
136 ZOOM_task task = (ZOOM_task) xmalloc (sizeof(*task));
138 task->next = c->tasks;
147 void ZOOM_connection_remove_task (ZOOM_connection c)
149 ZOOM_task task = c->tasks;
153 c->tasks = task->next;
156 case ZOOM_TASK_SEARCH:
157 ZOOM_resultset_destroy (task->u.search.resultset);
159 case ZOOM_TASK_RETRIEVE:
160 ZOOM_resultset_destroy (task->u.retrieve.resultset);
162 case ZOOM_TASK_CONNECT:
165 ZOOM_scanset_destroy (task->u.scan.scan);
167 case ZOOM_TASK_PACKAGE:
168 ZOOM_package_destroy (task->u.package);
177 void ZOOM_connection_remove_tasks (ZOOM_connection c)
180 ZOOM_connection_remove_task(c);
183 static ZOOM_record record_cache_lookup (ZOOM_resultset r, int pos);
185 ZOOM_API(ZOOM_connection)
186 ZOOM_connection_create (ZOOM_options options)
188 ZOOM_connection c = (ZOOM_connection) xmalloc (sizeof(*c));
194 c->state = STATE_IDLE;
196 set_ZOOM_error(c, ZOOM_ERROR_NONE, 0);
203 c->options = ZOOM_options_create_with_parent(options);
208 c->charset = c->lang = 0;
215 c->odr_in = odr_createmem (ODR_DECODE);
216 c->odr_out = odr_createmem (ODR_ENCODE);
219 c->support_named_resultsets = 0;
220 c->last_event = ZOOM_EVENT_NONE;
222 c->m_queue_front = 0;
227 /* set database names. Take local databases (if set); otherwise
228 take databases given in ZURL (if set); otherwise use Default */
229 static char **set_DatabaseNames (ZOOM_connection con, ZOOM_options options,
232 char **databaseNames;
235 const char *cp = ZOOM_options_get (options, "databaseName");
239 if (strncmp (con->host_port, "unix:", 5) == 0)
240 cp = strchr (con->host_port+5, ':');
242 cp = strchr (con->host_port, '/');
249 while ((c = strchr(c, '+')))
257 databaseNames = (char**)
258 odr_malloc (con->odr_out, no * sizeof(*databaseNames));
262 c = strchr (cp, '+');
270 /* cp ptr to first char of db name, c is char
272 databaseNames[no] = (char*) odr_malloc (con->odr_out, 1+c-cp);
273 memcpy (databaseNames[no], cp, c-cp);
274 databaseNames[no++][c-cp] = '\0';
279 databaseNames[no] = NULL;
281 return databaseNames;
284 ZOOM_API(ZOOM_connection)
285 ZOOM_connection_new (const char *host, int portnum)
287 ZOOM_connection c = ZOOM_connection_create (0);
289 ZOOM_connection_connect (c, host, portnum);
294 ZOOM_connection_connect(ZOOM_connection c,
295 const char *host, int portnum)
302 yaz_log (LOG_DEBUG, "reconnect");
306 yaz_log(LOG_DEBUG, "connect");
308 val = ZOOM_options_get (c->options, "proxy");
310 c->proxy = xstrdup (val);
315 val = ZOOM_options_get (c->options, "charset");
317 c->charset = xstrdup (val);
322 val = ZOOM_options_get (c->options, "lang");
324 c->lang = xstrdup (val);
328 xfree (c->host_port);
332 sprintf (hostn, "%.80s:%d", host, portnum);
333 c->host_port = xstrdup(hostn);
336 c->host_port = xstrdup(host);
338 ZOOM_options_set(c->options, "host", c->host_port);
340 val = ZOOM_options_get (c->options, "cookie");
342 c->cookie_out = xstrdup (val);
344 val = ZOOM_options_get (c->options, "clientIP");
346 c->client_IP = xstrdup (val);
348 c->async = ZOOM_options_get_bool (c->options, "async", 0);
350 set_ZOOM_error(c, ZOOM_ERROR_NONE, 0);
352 task = ZOOM_connection_add_task (c, ZOOM_TASK_CONNECT);
356 while (ZOOM_event (1, &c))
362 ZOOM_query_create(void)
364 ZOOM_query s = (ZOOM_query) xmalloc (sizeof(*s));
369 s->odr = odr_createmem (ODR_ENCODE);
375 ZOOM_query_destroy(ZOOM_query s)
381 yaz_log (LOG_DEBUG, "ZOOM_query_destroy count=%d", s->refcount);
382 if (s->refcount == 0)
384 odr_destroy (s->odr);
390 ZOOM_query_prefix(ZOOM_query s, const char *str)
392 s->z_query = (Z_Query *) odr_malloc (s->odr, sizeof(*s->z_query));
393 s->z_query->which = Z_Query_type_1;
394 s->z_query->u.type_1 = p_query_rpn(s->odr, PROTO_Z3950, str);
395 if (!s->z_query->u.type_1)
401 ZOOM_query_cql(ZOOM_query s, const char *str)
405 ext = (Z_External *) odr_malloc(s->odr, sizeof(*ext));
406 ext->direct_reference = odr_getoidbystr(s->odr, "1.2.840.10003.16.2");
407 ext->indirect_reference = 0;
409 ext->which = Z_External_CQL;
410 ext->u.cql = odr_strdup(s->odr, str);
412 s->z_query = (Z_Query *) odr_malloc (s->odr, sizeof(*s->z_query));
413 s->z_query->which = Z_Query_type_104;
414 s->z_query->u.type_104 = ext;
419 ZOOM_query_sortby(ZOOM_query s, const char *criteria)
421 s->sort_spec = yaz_sort_spec (s->odr, criteria);
427 static zoom_ret do_write(ZOOM_connection c);
430 ZOOM_connection_destroy(ZOOM_connection c)
441 for (r = c->resultsets; r; r = r->next)
446 odr_destroy (c->odr_in);
447 odr_destroy (c->odr_out);
448 ZOOM_options_destroy (c->options);
449 ZOOM_connection_remove_tasks (c);
450 xfree (c->host_port);
454 xfree (c->cookie_out);
455 xfree (c->cookie_in);
456 xfree (c->client_IP);
460 void ZOOM_resultset_addref (ZOOM_resultset r)
465 yaz_log (LOG_DEBUG, "ZOOM_resultset_addref r=%p count=%d",
469 ZOOM_resultset ZOOM_resultset_create ()
471 ZOOM_resultset r = (ZOOM_resultset) xmalloc (sizeof(*r));
473 yaz_log (LOG_DEBUG, "ZOOM_resultset_create r = %p", r);
476 r->odr = odr_createmem (ODR_ENCODE);
490 ZOOM_API(ZOOM_resultset)
491 ZOOM_connection_search_pqf(ZOOM_connection c, const char *q)
494 ZOOM_query s = ZOOM_query_create();
496 ZOOM_query_prefix (s, q);
498 r = ZOOM_connection_search (c, s);
499 ZOOM_query_destroy (s);
503 ZOOM_API(ZOOM_resultset)
504 ZOOM_connection_search(ZOOM_connection c, ZOOM_query q)
506 ZOOM_resultset r = ZOOM_resultset_create ();
510 r->r_sort_spec = q->sort_spec;
511 r->z_query = q->z_query;
514 r->options = ZOOM_options_create_with_parent(c->options);
516 r->start = ZOOM_options_get_int(r->options, "start", 0);
517 r->count = ZOOM_options_get_int(r->options, "count", 0);
518 r->piggyback = ZOOM_options_get_bool (r->options, "piggyback", 1);
519 cp = ZOOM_options_get (r->options, "setname");
521 r->setname = xstrdup (cp);
525 r->next = c->resultsets;
528 task = ZOOM_connection_add_task (c, ZOOM_TASK_SEARCH);
529 task->u.search.resultset = r;
530 ZOOM_resultset_addref (r);
536 while (ZOOM_event (1, &c))
543 ZOOM_resultset_destroy(ZOOM_resultset r)
548 yaz_log (LOG_DEBUG, "ZOOM_resultset_destroy r = %p count=%d",
550 if (r->refcount == 0)
552 ZOOM_record_cache rc;
554 for (rc = r->record_cache; rc; rc = rc->next)
555 if (rc->rec.wrbuf_marc)
556 wrbuf_free (rc->rec.wrbuf_marc, 1);
559 /* remove ourselves from the resultsets in connection */
560 ZOOM_resultset *rp = &r->connection->resultsets;
563 assert (*rp); /* we must be in this list!! */
565 { /* OK, we're here - take us out of it */
572 ZOOM_query_destroy (r->search);
573 ZOOM_options_destroy (r->options);
574 odr_destroy (r->odr);
581 ZOOM_resultset_size (ZOOM_resultset r)
586 static void do_close (ZOOM_connection c)
592 c->state = STATE_IDLE;
595 static void ZOOM_resultset_retrieve (ZOOM_resultset r,
596 int force_sync, int start, int count)
606 task = ZOOM_connection_add_task (c, ZOOM_TASK_RETRIEVE);
607 task->u.retrieve.resultset = r;
608 task->u.retrieve.start = start;
609 task->u.retrieve.count = count;
611 ZOOM_resultset_addref (r);
613 if (!r->connection->async || force_sync)
614 while (r->connection && ZOOM_event (1, &r->connection))
619 ZOOM_resultset_records (ZOOM_resultset r, ZOOM_record *recs,
620 size_t start, size_t count)
622 int force_present = 0;
628 ZOOM_resultset_retrieve (r, force_present, start, count);
632 for (i = 0; i< count; i++)
633 recs[i] = ZOOM_resultset_record_immediate (r, i+start);
637 static zoom_ret do_connect (ZOOM_connection c)
640 const char *effective_host;
643 effective_host = c->proxy;
645 effective_host = c->host_port;
647 yaz_log (LOG_DEBUG, "do_connect host=%s", effective_host);
651 if (memcmp(c->host_port, "http:", 5) == 0)
654 c->soap = soap_new();
655 c->soap->namespaces = srw_namespaces;
657 c->state = STATE_IDLE;
658 set_ZOOM_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, "SRW");
663 c->cs = cs_create_host (effective_host, 0, &add);
667 int ret = cs_connect (c->cs, add);
670 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
671 ZOOM_connection_put_event(c, event);
672 ZOOM_connection_send_init(c);
673 c->state = STATE_ESTABLISHED;
678 c->state = STATE_CONNECTING;
679 c->mask = ZOOM_SELECT_EXCEPT;
680 if (c->cs->io_pending & CS_WANT_WRITE)
681 c->mask += ZOOM_SELECT_WRITE;
682 if (c->cs->io_pending & CS_WANT_READ)
683 c->mask += ZOOM_SELECT_READ;
687 c->state = STATE_IDLE;
688 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, effective_host);
690 return zoom_complete;
693 int z3950_connection_socket(ZOOM_connection c)
696 return cs_fileno(c->cs);
700 int z3950_connection_mask(ZOOM_connection c)
707 static void otherInfo_attach (ZOOM_connection c, Z_APDU *a, ODR out)
710 for (i = 0; i<200; i++)
713 Z_OtherInformation **oi;
719 sprintf (buf, "otherInfo%d", i);
720 val = ZOOM_options_get (c->options, buf);
723 cp = strchr (val, ':');
727 if (len >= sizeof(buf))
729 memcpy (buf, val, len);
731 oidval = oid_getvalbyname (buf);
732 if (oidval == VAL_NONE)
736 yaz_oi_set_string_oidval(oi, out, oidval, 1, cp+1);
740 static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out)
745 Z_OtherInformation **oi;
747 yaz_oi_set_string_oidval(oi, out, VAL_COOKIE, 1, c->cookie_out);
751 Z_OtherInformation **oi;
753 yaz_oi_set_string_oidval(oi, out, VAL_CLIENT_IP, 1, c->client_IP);
755 otherInfo_attach (c, a, out);
756 if (!z_APDU(out, &a, 0, 0))
758 FILE *outf = fopen("/tmp/apdu.txt", "a");
761 ODR odr_pr = odr_createmem(ODR_PRINT);
762 fprintf (outf, "a=%p\n", a);
763 odr_setprint(odr_pr, outf);
764 z_APDU(odr_pr, &a, 0, 0);
767 yaz_log (LOG_DEBUG, "encoding failed");
768 set_ZOOM_error(c, ZOOM_ERROR_ENCODE, 0);
776 static zoom_ret send_APDU (ZOOM_connection c, Z_APDU *a)
780 if (encode_APDU(c, a, c->odr_out))
781 return zoom_complete;
782 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
783 event = ZOOM_Event_create (ZOOM_EVENT_SEND_APDU);
784 ZOOM_connection_put_event (c, event);
785 odr_reset(c->odr_out);
789 /* returns 1 if PDU was sent OK (still pending )
790 0 if PDU was not sent OK (nothing to wait for)
793 static zoom_ret ZOOM_connection_send_init (ZOOM_connection c)
796 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest);
797 Z_InitRequest *ireq = apdu->u.initRequest;
798 Z_IdAuthentication *auth = (Z_IdAuthentication *)
799 odr_malloc(c->odr_out, sizeof(*auth));
800 const char *auth_groupId = ZOOM_options_get (c->options, "group");
801 const char *auth_userId = ZOOM_options_get (c->options, "user");
802 const char *auth_password = ZOOM_options_get (c->options, "pass");
804 ODR_MASK_SET(ireq->options, Z_Options_search);
805 ODR_MASK_SET(ireq->options, Z_Options_present);
806 ODR_MASK_SET(ireq->options, Z_Options_scan);
807 ODR_MASK_SET(ireq->options, Z_Options_sort);
808 ODR_MASK_SET(ireq->options, Z_Options_extendedServices);
809 ODR_MASK_SET(ireq->options, Z_Options_namedResultSets);
811 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
812 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
813 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
815 impname = ZOOM_options_get (c->options, "implementationName");
816 ireq->implementationName =
817 (char *) odr_malloc (c->odr_out, 15 + (impname ? strlen(impname) : 0));
818 strcpy (ireq->implementationName, "");
821 strcat (ireq->implementationName, impname);
822 strcat (ireq->implementationName, "/");
824 strcat (ireq->implementationName, "ZOOM-C/YAZ");
826 *ireq->maximumRecordSize =
827 ZOOM_options_get_int (c->options, "maximumRecordSize", 1024*1024);
828 *ireq->preferredMessageSize =
829 ZOOM_options_get_int (c->options, "preferredMessageSize", 1024*1024);
831 if (auth_groupId || auth_password)
833 Z_IdPass *pass = (Z_IdPass *) odr_malloc(c->odr_out, sizeof(*pass));
836 if (auth_groupId && *auth_groupId)
838 pass->groupId = (char *)
839 odr_malloc(c->odr_out, strlen(auth_groupId)+1);
840 strcpy(pass->groupId, auth_groupId);
844 if (auth_userId && *auth_userId)
846 pass->userId = (char *)
847 odr_malloc(c->odr_out, strlen(auth_userId)+1);
848 strcpy(pass->userId, auth_userId);
852 if (auth_password && *auth_password)
854 pass->password = (char *)
855 odr_malloc(c->odr_out, strlen(auth_password)+1);
856 strcpy(pass->password, auth_password);
861 auth->which = Z_IdAuthentication_idPass;
862 auth->u.idPass = pass;
863 ireq->idAuthentication = auth;
866 else if (auth_userId)
868 auth->which = Z_IdAuthentication_open;
869 auth->u.open = (char *)
870 odr_malloc(c->odr_out, strlen(auth_userId)+1);
871 strcpy(auth->u.open, auth_userId);
872 ireq->idAuthentication = auth;
875 yaz_oi_set_string_oidval(&ireq->otherInfo, c->odr_out,
876 VAL_PROXY, 1, c->host_port);
877 if (c->charset||c->lang)
879 Z_OtherInformation **oi;
880 Z_OtherInformationUnit *oi_unit;
882 yaz_oi_APDU(apdu, &oi);
884 if ((oi_unit = yaz_oi_update(oi, c->odr_out, NULL, 0, 0)))
886 ODR_MASK_SET(ireq->options, Z_Options_negotiationModel);
888 oi_unit->which = Z_OtherInfo_externallyDefinedInfo;
889 oi_unit->information.externallyDefinedInfo =
890 yaz_set_proposal_charneg
892 (const char **)&c->charset, (c->charset) ? 1:0,
893 (const char **)&c->lang, (c->lang) ? 1:0, 1);
897 return send_APDU (c, apdu);
900 static zoom_ret ZOOM_connection_send_search (ZOOM_connection c)
903 int lslb, ssub, mspn;
905 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
906 Z_SearchRequest *search_req = apdu->u.searchRequest;
907 const char *elementSetName;
908 const char *smallSetElementSetName;
909 const char *mediumSetElementSetName;
913 assert (c->tasks->which == ZOOM_TASK_SEARCH);
915 r = c->tasks->u.search.resultset;
918 ZOOM_options_get (r->options, "elementSetName");
919 smallSetElementSetName =
920 ZOOM_options_get (r->options, "smallSetElementSetName");
921 mediumSetElementSetName =
922 ZOOM_options_get (r->options, "mediumSetElementSetName");
924 ZOOM_options_get (r->options, "schema");
926 if (!smallSetElementSetName)
927 smallSetElementSetName = elementSetName;
929 if (!mediumSetElementSetName)
930 mediumSetElementSetName = elementSetName;
935 /* prepare query for the search request */
936 search_req->query = r->z_query;
938 search_req->databaseNames =
939 set_DatabaseNames (c, r->options, &search_req->num_databaseNames);
941 /* get syntax (no need to provide unless piggyback is in effect) */
942 syntax = ZOOM_options_get (r->options, "preferredRecordSyntax");
944 lslb = ZOOM_options_get_int (r->options, "largeSetLowerBound", -1);
945 ssub = ZOOM_options_get_int (r->options, "smallSetUpperBound", -1);
946 mspn = ZOOM_options_get_int (r->options, "mediumSetPresentNumber", -1);
947 if (lslb != -1 && ssub != -1 && mspn != -1)
949 /* So're a Z39.50 expert? Let's hope you don't do sort */
950 *search_req->largeSetLowerBound = lslb;
951 *search_req->smallSetUpperBound = ssub;
952 *search_req->mediumSetPresentNumber = mspn;
954 else if (r->start == 0 && r->count > 0
955 && r->piggyback && !r->r_sort_spec && !schema)
957 /* Regular piggyback - do it unless we're going to do sort */
958 *search_req->largeSetLowerBound = 2000000000;
959 *search_req->smallSetUpperBound = 0;
960 *search_req->mediumSetPresentNumber = r->count;
961 smallSetElementSetName = 0;
965 /* non-piggyback. Need not provide elementsets or syntaxes .. */
966 smallSetElementSetName = 0;
967 mediumSetElementSetName = 0;
970 if (smallSetElementSetName && *smallSetElementSetName)
972 Z_ElementSetNames *esn = (Z_ElementSetNames *)
973 odr_malloc (c->odr_out, sizeof(*esn));
975 esn->which = Z_ElementSetNames_generic;
976 esn->u.generic = odr_strdup (c->odr_out, smallSetElementSetName);
977 search_req->smallSetElementSetNames = esn;
979 if (mediumSetElementSetName && *mediumSetElementSetName)
981 Z_ElementSetNames *esn = (Z_ElementSetNames *)
982 odr_malloc (c->odr_out, sizeof(*esn));
984 esn->which = Z_ElementSetNames_generic;
985 esn->u.generic = odr_strdup (c->odr_out, mediumSetElementSetName);
986 search_req->mediumSetElementSetNames = esn;
989 search_req->preferredRecordSyntax =
990 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
994 if (c->support_named_resultsets)
998 /* find the lowest unused ordinal so that we re-use
999 result sets on the server. */
1000 for (ord = 1; ; ord++)
1003 sprintf (setname, "%d", ord);
1004 for (rp = c->resultsets; rp; rp = rp->next)
1005 if (rp->setname && !strcmp (rp->setname, setname))
1010 r->setname = xstrdup (setname);
1011 yaz_log (LOG_DEBUG, "allocating set %s", r->setname);
1014 r->setname = xstrdup ("default");
1015 ZOOM_options_set (r->options, "setname", r->setname);
1017 search_req->resultSetName = odr_strdup(c->odr_out, r->setname);
1018 /* send search request */
1019 return send_APDU (c, apdu);
1022 static void response_diag (ZOOM_connection c, Z_DiagRec *p)
1025 Z_DefaultDiagFormat *r;
1030 if (p->which != Z_DiagRec_defaultFormat)
1032 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
1035 r = p->u.defaultFormat;
1036 c->diagset = yaz_z3950oid_to_str(r->diagnosticSetId, &oclass);
1040 case Z_DefaultDiagFormat_v2Addinfo:
1041 addinfo = r->u.v2Addinfo;
1043 case Z_DefaultDiagFormat_v3Addinfo:
1044 addinfo = r->u.v3Addinfo;
1048 c->addinfo = xstrdup (addinfo);
1049 c->error = *r->condition;
1052 ZOOM_API(ZOOM_record)
1053 ZOOM_record_clone (ZOOM_record srec)
1060 odr_enc = odr_createmem(ODR_ENCODE);
1061 if (!z_NamePlusRecord (odr_enc, &srec->npr, 0, 0))
1063 buf = odr_getbuf (odr_enc, &size, 0);
1065 nrec = (ZOOM_record) xmalloc (sizeof(*nrec));
1066 nrec->odr = odr_createmem(ODR_DECODE);
1067 nrec->wrbuf_marc = 0;
1068 odr_setbuf (nrec->odr, buf, size, 0);
1069 z_NamePlusRecord (nrec->odr, &nrec->npr, 0, 0);
1071 odr_destroy (odr_enc);
1075 ZOOM_API(ZOOM_record)
1076 ZOOM_resultset_record_immediate (ZOOM_resultset s,size_t pos)
1078 return record_cache_lookup (s, pos);
1081 ZOOM_API(ZOOM_record)
1082 ZOOM_resultset_record (ZOOM_resultset r, size_t pos)
1084 ZOOM_resultset_retrieve (r, 1, pos, 1);
1085 return ZOOM_resultset_record_immediate (r, pos);
1089 ZOOM_record_destroy (ZOOM_record rec)
1093 if (rec->wrbuf_marc)
1094 wrbuf_free (rec->wrbuf_marc, 1);
1095 odr_destroy (rec->odr);
1099 ZOOM_API(const char *)
1100 ZOOM_record_get (ZOOM_record rec, const char *type, int *len)
1102 Z_NamePlusRecord *npr;
1105 *len = 0; /* default return */
1112 if (!strcmp (type, "database"))
1115 *len = (npr->databaseName ? strlen(npr->databaseName) : 0);
1116 return npr->databaseName;
1118 else if (!strcmp (type, "syntax"))
1120 const char *desc = 0;
1121 if (npr->which == Z_NamePlusRecord_databaseRecord)
1123 Z_External *r = (Z_External *) npr->u.databaseRecord;
1124 oident *ent = oid_getentbyoid(r->direct_reference);
1131 *len = strlen(desc);
1134 else if (!strcmp (type, "render") &&
1135 npr->which == Z_NamePlusRecord_databaseRecord)
1137 Z_External *r = (Z_External *) npr->u.databaseRecord;
1138 oident *ent = oid_getentbyoid(r->direct_reference);
1140 if (r->which == Z_External_sutrs)
1142 if (len) *len = r->u.sutrs->len;
1143 return (const char *) r->u.sutrs->buf;
1145 else if (r->which == Z_External_octet)
1155 case VAL_APPLICATION_XML:
1158 if (!rec->wrbuf_marc)
1159 rec->wrbuf_marc = wrbuf_alloc();
1161 mt = yaz_marc_create();
1162 wrbuf_rewind (rec->wrbuf_marc);
1163 if (yaz_marc_decode_wrbuf (
1164 mt, (const char *) r->u.octet_aligned->buf,
1165 r->u.octet_aligned->len,
1166 rec->wrbuf_marc) > 0)
1169 *len = wrbuf_len(rec->wrbuf_marc);
1170 yaz_marc_destroy(mt);
1171 return wrbuf_buf(rec->wrbuf_marc);
1173 yaz_marc_destroy(mt);
1176 *len = r->u.octet_aligned->len;
1177 return (const char *) r->u.octet_aligned->buf;
1179 else if (r->which == Z_External_grs1)
1181 if (!rec->wrbuf_marc)
1182 rec->wrbuf_marc = wrbuf_alloc();
1183 wrbuf_rewind (rec->wrbuf_marc);
1184 yaz_display_grs1(rec->wrbuf_marc, r->u.grs1, 0);
1186 *len = wrbuf_len(rec->wrbuf_marc);
1187 return wrbuf_buf(rec->wrbuf_marc);
1191 else if (npr->which == Z_NamePlusRecord_databaseRecord &&
1192 (!strcmp (type, "xml") || !strcmp(type, "oai")))
1194 Z_External *r = (Z_External *) npr->u.databaseRecord;
1195 oident *ent = oid_getentbyoid(r->direct_reference);
1197 if (r->which == Z_External_sutrs)
1199 if (len) *len = r->u.sutrs->len;
1200 return (const char *) r->u.sutrs->buf;
1202 else if (r->which == Z_External_octet)
1205 int marc_decode_type = YAZ_MARC_MARCXML;
1207 if (!strcmp(type, "oai"))
1208 marc_decode_type = YAZ_MARC_OAIMARC;
1216 case VAL_APPLICATION_XML:
1219 if (!rec->wrbuf_marc)
1220 rec->wrbuf_marc = wrbuf_alloc();
1221 wrbuf_rewind (rec->wrbuf_marc);
1222 mt = yaz_marc_create();
1224 yaz_marc_xml(mt, YAZ_MARC_MARCXML);
1225 if (yaz_marc_decode_wrbuf (
1226 mt, (const char *) r->u.octet_aligned->buf,
1227 r->u.octet_aligned->len,
1228 rec->wrbuf_marc) > 0)
1231 *len = wrbuf_len(rec->wrbuf_marc);
1232 yaz_marc_destroy(mt);
1233 return wrbuf_buf(rec->wrbuf_marc);
1235 yaz_marc_destroy(mt);
1237 if (len) *len = r->u.octet_aligned->len;
1238 return (const char *) r->u.octet_aligned->buf;
1240 else if (r->which == Z_External_grs1)
1247 else if (!strcmp (type, "raw"))
1249 if (npr->which == Z_NamePlusRecord_databaseRecord)
1251 Z_External *r = (Z_External *) npr->u.databaseRecord;
1253 if (r->which == Z_External_sutrs)
1255 if (len) *len = r->u.sutrs->len;
1256 return (const char *) r->u.sutrs->buf;
1258 else if (r->which == Z_External_octet)
1260 if (len) *len = r->u.octet_aligned->len;
1261 return (const char *) r->u.octet_aligned->buf;
1263 else /* grs-1, explain, ... */
1266 return (const char *) npr->u.databaseRecord;
1271 else if (!strcmp (type, "ext"))
1273 if (npr->which == Z_NamePlusRecord_databaseRecord)
1274 return (const char *) npr->u.databaseRecord;
1280 static void record_cache_add (ZOOM_resultset r, Z_NamePlusRecord *npr, int pos)
1282 ZOOM_record_cache rc;
1283 const char *elementSetName =
1284 ZOOM_resultset_option_get (r, "elementSetName");
1285 const char *syntax =
1286 ZOOM_resultset_option_get (r, "preferredRecordSyntax");
1288 for (rc = r->record_cache; rc; rc = rc->next)
1292 if ((!elementSetName && !rc->elementSetName)
1293 || (elementSetName && rc->elementSetName &&
1294 !strcmp (elementSetName, rc->elementSetName)))
1296 if ((!syntax && !rc->syntax)
1297 || (syntax && rc->syntax &&
1298 !strcmp (syntax, rc->syntax)))
1300 /* not destroying rc->npr (it's handled by nmem )*/
1302 /* keeping wrbuf_marc too */
1308 rc = (ZOOM_record_cache) odr_malloc (r->odr, sizeof(*rc));
1311 rc->rec.wrbuf_marc = 0;
1313 rc->elementSetName = odr_strdup (r->odr, elementSetName);
1315 rc->elementSetName = 0;
1318 rc->syntax = odr_strdup (r->odr, syntax);
1323 rc->next = r->record_cache;
1324 r->record_cache = rc;
1327 static ZOOM_record record_cache_lookup (ZOOM_resultset r, int pos)
1329 ZOOM_record_cache rc;
1330 const char *elementSetName =
1331 ZOOM_resultset_option_get (r, "elementSetName");
1332 const char *syntax =
1333 ZOOM_resultset_option_get (r, "preferredRecordSyntax");
1335 for (rc = r->record_cache; rc; rc = rc->next)
1339 if ((!elementSetName && !rc->elementSetName)
1340 || (elementSetName && rc->elementSetName &&
1341 !strcmp (elementSetName, rc->elementSetName)))
1343 if ((!syntax && !rc->syntax)
1344 || (syntax && rc->syntax &&
1345 !strcmp (syntax, rc->syntax)))
1353 static void handle_records (ZOOM_connection c, Z_Records *sr,
1356 ZOOM_resultset resultset;
1360 switch (c->tasks->which)
1362 case ZOOM_TASK_SEARCH:
1363 resultset = c->tasks->u.search.resultset;
1365 case ZOOM_TASK_RETRIEVE:
1366 resultset = c->tasks->u.retrieve.resultset;
1371 if (sr && sr->which == Z_Records_NSD)
1373 Z_DiagRec dr, *dr_p = &dr;
1374 dr.which = Z_DiagRec_defaultFormat;
1375 dr.u.defaultFormat = sr->u.nonSurrogateDiagnostic;
1377 response_diag (c, dr_p);
1379 else if (sr && sr->which == Z_Records_multipleNSD)
1381 if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
1382 response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
1384 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
1388 if (resultset->count + resultset->start > resultset->size)
1389 resultset->count = resultset->size - resultset->start;
1390 if (resultset->count < 0)
1391 resultset->count = 0;
1392 if (sr && sr->which == Z_Records_DBOSD)
1395 NMEM nmem = odr_extract_mem (c->odr_in);
1396 Z_NamePlusRecordList *p =
1397 sr->u.databaseOrSurDiagnostics;
1398 for (i = 0; i<p->num_records; i++)
1400 record_cache_add (resultset, p->records[i],
1401 i+ resultset->start);
1403 /* transfer our response to search_nmem .. we need it later */
1404 nmem_transfer (resultset->odr->mem, nmem);
1405 nmem_destroy (nmem);
1406 if (present_phase && p->num_records == 0)
1408 /* present response and we didn't get any records! */
1409 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
1412 else if (present_phase)
1414 /* present response and we didn't get any records! */
1415 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
1420 static void handle_present_response (ZOOM_connection c, Z_PresentResponse *pr)
1422 handle_records (c, pr->records, 1);
1425 static void handle_search_response (ZOOM_connection c, Z_SearchResponse *sr)
1427 ZOOM_resultset resultset;
1429 yaz_log (LOG_DEBUG, "got search response");
1431 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1434 resultset = c->tasks->u.search.resultset;
1436 resultset->size = *sr->resultCount;
1437 handle_records (c, sr->records, 0);
1440 static void sort_response (ZOOM_connection c, Z_SortResponse *res)
1442 if (res->diagnostics && res->num_diagnostics > 0)
1443 response_diag (c, res->diagnostics[0]);
1446 static int scan_response (ZOOM_connection c, Z_ScanResponse *res)
1448 NMEM nmem = odr_extract_mem (c->odr_in);
1451 if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
1453 scan = c->tasks->u.scan.scan;
1455 if (res->entries && res->entries->nonsurrogateDiagnostics)
1456 response_diag(c, res->entries->nonsurrogateDiagnostics[0]);
1457 scan->scan_response = res;
1458 nmem_transfer (scan->odr->mem, nmem);
1460 ZOOM_options_set_int (scan->options, "stepSize", *res->stepSize);
1461 if (res->positionOfTerm)
1462 ZOOM_options_set_int (scan->options, "position", *res->positionOfTerm);
1463 if (res->scanStatus)
1464 ZOOM_options_set_int (scan->options, "scanStatus", *res->scanStatus);
1465 if (res->numberOfEntriesReturned)
1466 ZOOM_options_set_int (scan->options, "number",
1467 *res->numberOfEntriesReturned);
1468 nmem_destroy (nmem);
1472 static zoom_ret send_sort (ZOOM_connection c)
1474 ZOOM_resultset resultset;
1476 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1477 return zoom_complete;
1479 resultset = c->tasks->u.search.resultset;
1483 resultset->r_sort_spec = 0;
1484 return zoom_complete;
1486 if (resultset->r_sort_spec)
1488 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
1489 Z_SortRequest *req = apdu->u.sortRequest;
1491 req->num_inputResultSetNames = 1;
1492 req->inputResultSetNames = (Z_InternationalString **)
1493 odr_malloc (c->odr_out, sizeof(*req->inputResultSetNames));
1494 req->inputResultSetNames[0] =
1495 odr_strdup (c->odr_out, resultset->setname);
1496 req->sortedResultSetName = odr_strdup (c->odr_out, resultset->setname);
1497 req->sortSequence = resultset->r_sort_spec;
1498 resultset->r_sort_spec = 0;
1499 return send_APDU (c, apdu);
1501 return zoom_complete;
1504 static zoom_ret send_present (ZOOM_connection c)
1507 Z_PresentRequest *req = 0;
1509 const char *syntax = 0;
1510 const char *elementSetName = 0;
1511 const char *schema = 0;
1512 ZOOM_resultset resultset;
1515 return zoom_complete;
1517 switch (c->tasks->which)
1519 case ZOOM_TASK_SEARCH:
1520 resultset = c->tasks->u.search.resultset;
1522 case ZOOM_TASK_RETRIEVE:
1523 resultset = c->tasks->u.retrieve.resultset;
1524 resultset->start = c->tasks->u.retrieve.start;
1525 resultset->count = c->tasks->u.retrieve.count;
1527 if (resultset->start >= resultset->size)
1528 return zoom_complete;
1529 if (resultset->start + resultset->count > resultset->size)
1530 resultset->count = resultset->size - resultset->start;
1533 return zoom_complete;
1536 syntax = ZOOM_resultset_option_get (resultset, "preferredRecordSyntax");
1537 elementSetName = ZOOM_resultset_option_get (resultset, "elementSetName");
1538 schema = ZOOM_resultset_option_get (resultset, "schema");
1540 if (c->error) /* don't continue on error */
1541 return zoom_complete;
1542 if (resultset->start < 0)
1543 return zoom_complete;
1544 for (i = 0; i<resultset->count; i++)
1547 record_cache_lookup (resultset, i + resultset->start);
1551 if (i == resultset->count)
1552 return zoom_complete;
1554 apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
1555 req = apdu->u.presentRequest;
1557 resultset->start += i;
1558 resultset->count -= i;
1559 *req->resultSetStartPoint = resultset->start + 1;
1560 *req->numberOfRecordsRequested = resultset->count;
1561 assert (*req->numberOfRecordsRequested > 0);
1563 if (syntax && *syntax)
1564 req->preferredRecordSyntax =
1565 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
1567 if (schema && *schema)
1569 Z_RecordComposition *compo = (Z_RecordComposition *)
1570 odr_malloc (c->odr_out, sizeof(*compo));
1572 req->recordComposition = compo;
1573 compo->which = Z_RecordComp_complex;
1574 compo->u.complex = (Z_CompSpec *)
1575 odr_malloc(c->odr_out, sizeof(*compo->u.complex));
1576 compo->u.complex->selectAlternativeSyntax = (bool_t *)
1577 odr_malloc(c->odr_out, sizeof(bool_t));
1578 *compo->u.complex->selectAlternativeSyntax = 0;
1580 compo->u.complex->generic = (Z_Specification *)
1581 odr_malloc(c->odr_out, sizeof(*compo->u.complex->generic));
1583 compo->u.complex->generic->which = Z_Specification_oid;
1584 compo->u.complex->generic->u.oid = (Odr_oid *)
1585 yaz_str_to_z3950oid (c->odr_out, CLASS_SCHEMA, schema);
1587 if (!compo->u.complex->generic->u.oid)
1589 /* OID wasn't a schema! Try record syntax instead. */
1591 compo->u.complex->generic->u.oid = (Odr_oid *)
1592 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, schema);
1594 if (elementSetName && *elementSetName)
1596 compo->u.complex->generic->elementSpec = (Z_ElementSpec *)
1597 odr_malloc(c->odr_out, sizeof(Z_ElementSpec));
1598 compo->u.complex->generic->elementSpec->which =
1599 Z_ElementSpec_elementSetName;
1600 compo->u.complex->generic->elementSpec->u.elementSetName =
1601 odr_strdup (c->odr_out, elementSetName);
1604 compo->u.complex->generic->elementSpec = 0;
1605 compo->u.complex->num_dbSpecific = 0;
1606 compo->u.complex->dbSpecific = 0;
1607 compo->u.complex->num_recordSyntax = 0;
1608 compo->u.complex->recordSyntax = 0;
1610 else if (elementSetName && *elementSetName)
1612 Z_ElementSetNames *esn = (Z_ElementSetNames *)
1613 odr_malloc (c->odr_out, sizeof(*esn));
1614 Z_RecordComposition *compo = (Z_RecordComposition *)
1615 odr_malloc (c->odr_out, sizeof(*compo));
1617 esn->which = Z_ElementSetNames_generic;
1618 esn->u.generic = odr_strdup (c->odr_out, elementSetName);
1619 compo->which = Z_RecordComp_simple;
1620 compo->u.simple = esn;
1621 req->recordComposition = compo;
1623 req->resultSetId = odr_strdup(c->odr_out, resultset->setname);
1624 return send_APDU (c, apdu);
1627 ZOOM_API(ZOOM_scanset)
1628 ZOOM_connection_scan (ZOOM_connection c, const char *start)
1630 ZOOM_scanset scan = (ZOOM_scanset) xmalloc (sizeof(*scan));
1632 scan->connection = c;
1633 scan->odr = odr_createmem (ODR_DECODE);
1634 scan->options = ZOOM_options_create_with_parent (c->options);
1636 scan->scan_response = 0;
1638 if ((scan->termListAndStartPoint =
1639 p_query_scan(scan->odr, PROTO_Z3950, &scan->attributeSet,
1642 ZOOM_task task = ZOOM_connection_add_task (c, ZOOM_TASK_SCAN);
1643 task->u.scan.scan = scan;
1648 while (ZOOM_event (1, &c))
1656 ZOOM_scanset_destroy (ZOOM_scanset scan)
1661 if (scan->refcount == 0)
1663 odr_destroy (scan->odr);
1665 ZOOM_options_destroy (scan->options);
1670 static zoom_ret send_package (ZOOM_connection c)
1674 return zoom_complete;
1675 assert (c->tasks->which == ZOOM_TASK_PACKAGE);
1677 event = ZOOM_Event_create (ZOOM_EVENT_SEND_APDU);
1678 ZOOM_connection_put_event (c, event);
1680 return do_write_ex (c, c->tasks->u.package->buf_out,
1681 c->tasks->u.package->len_out);
1684 static zoom_ret send_scan (ZOOM_connection c)
1687 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_scanRequest);
1688 Z_ScanRequest *req = apdu->u.scanRequest;
1690 return zoom_complete;
1691 assert (c->tasks->which == ZOOM_TASK_SCAN);
1692 scan = c->tasks->u.scan.scan;
1694 req->termListAndStartPoint = scan->termListAndStartPoint;
1695 req->attributeSet = scan->attributeSet;
1697 *req->numberOfTermsRequested =
1698 ZOOM_options_get_int(scan->options, "number", 10);
1700 req->preferredPositionInResponse =
1701 odr_intdup (c->odr_out,
1702 ZOOM_options_get_int(scan->options, "position", 1));
1705 odr_intdup (c->odr_out,
1706 ZOOM_options_get_int(scan->options, "stepSize", 0));
1708 req->databaseNames = set_DatabaseNames (c, scan->options,
1709 &req->num_databaseNames);
1711 return send_APDU (c, apdu);
1715 ZOOM_scanset_size (ZOOM_scanset scan)
1717 if (!scan || !scan->scan_response || !scan->scan_response->entries)
1719 return scan->scan_response->entries->num_entries;
1722 ZOOM_API(const char *)
1723 ZOOM_scanset_term (ZOOM_scanset scan, size_t pos,
1726 const char *term = 0;
1727 size_t noent = ZOOM_scanset_size (scan);
1728 Z_ScanResponse *res = scan->scan_response;
1734 if (res->entries->entries[pos]->which == Z_Entry_termInfo)
1736 Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
1738 if (t->term->which == Z_Term_general)
1740 term = (const char *) t->term->u.general->buf;
1741 *len = t->term->u.general->len;
1743 *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
1748 ZOOM_API(const char *)
1749 ZOOM_scanset_option_get (ZOOM_scanset scan, const char *key)
1751 return ZOOM_options_get (scan->options, key);
1755 ZOOM_scanset_option_set (ZOOM_scanset scan, const char *key,
1758 ZOOM_options_set (scan->options, key, val);
1761 static Z_APDU *create_es_package (ZOOM_package p, int type)
1764 Z_APDU *apdu = zget_APDU(p->odr_out, Z_APDU_extendedServicesRequest);
1765 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
1767 *req->function = Z_ExtendedServicesRequest_create;
1769 str = ZOOM_options_get(p->options, "package-name");
1771 req->packageName = nmem_strdup (p->odr_out->mem, str);
1773 str = ZOOM_options_get(p->options, "user-id");
1775 req->userId = nmem_strdup (p->odr_out->mem, str);
1777 req->packageType = yaz_oidval_to_z3950oid(p->odr_out, CLASS_EXTSERV,
1780 str = ZOOM_options_get(p->options, "function");
1783 if (!strcmp (str, "create"))
1785 if (!strcmp (str, "delete"))
1787 if (!strcmp (str, "modify"))
1793 static const char *ill_array_lookup (void *clientData, const char *idx)
1795 ZOOM_package p = (ZOOM_package) clientData;
1796 return ZOOM_options_get (p->options, idx+4);
1799 static Z_External *encode_ill_request (ZOOM_package p)
1801 ODR out = p->odr_out;
1804 struct ill_get_ctl ctl;
1806 ctl.odr = p->odr_out;
1808 ctl.f = ill_array_lookup;
1810 req = ill_get_ILLRequest(&ctl, "ill", 0);
1812 if (!ill_Request (out, &req, 0, 0))
1814 int ill_request_size;
1815 char *ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
1816 if (ill_request_buf)
1817 odr_setbuf (out, ill_request_buf, ill_request_size, 1);
1823 int illRequest_size = 0;
1824 char *illRequest_buf = odr_getbuf (out, &illRequest_size, 0);
1826 oid.proto = PROTO_GENERAL;
1827 oid.oclass = CLASS_GENERAL;
1828 oid.value = VAL_ISO_ILL_1;
1830 r = (Z_External *) odr_malloc (out, sizeof(*r));
1831 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid));
1832 r->indirect_reference = 0;
1834 r->which = Z_External_single;
1836 r->u.single_ASN1_type = (Odr_oct *)
1837 odr_malloc (out, sizeof(*r->u.single_ASN1_type));
1838 r->u.single_ASN1_type->buf = (unsigned char*)
1839 odr_malloc (out, illRequest_size);
1840 r->u.single_ASN1_type->len = illRequest_size;
1841 r->u.single_ASN1_type->size = illRequest_size;
1842 memcpy (r->u.single_ASN1_type->buf, illRequest_buf, illRequest_size);
1847 static Z_ItemOrder *encode_item_order(ZOOM_package p)
1849 Z_ItemOrder *req = (Z_ItemOrder *) odr_malloc (p->odr_out, sizeof(*req));
1852 req->which=Z_IOItemOrder_esRequest;
1853 req->u.esRequest = (Z_IORequest *)
1854 odr_malloc(p->odr_out,sizeof(Z_IORequest));
1856 /* to keep part ... */
1857 req->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
1858 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartToKeep));
1859 req->u.esRequest->toKeep->supplDescription = 0;
1860 req->u.esRequest->toKeep->contact = (Z_IOContact *)
1861 odr_malloc (p->odr_out, sizeof(*req->u.esRequest->toKeep->contact));
1863 str = ZOOM_options_get(p->options, "contact-name");
1864 req->u.esRequest->toKeep->contact->name = str ?
1865 nmem_strdup (p->odr_out->mem, str) : 0;
1867 str = ZOOM_options_get(p->options, "contact-phone");
1868 req->u.esRequest->toKeep->contact->phone = str ?
1869 nmem_strdup (p->odr_out->mem, str) : 0;
1871 str = ZOOM_options_get(p->options, "contact-email");
1872 req->u.esRequest->toKeep->contact->email = str ?
1873 nmem_strdup (p->odr_out->mem, str) : 0;
1875 req->u.esRequest->toKeep->addlBilling = 0;
1877 /* not to keep part ... */
1878 req->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
1879 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartNotToKeep));
1881 req->u.esRequest->notToKeep->resultSetItem = (Z_IOResultSetItem *)
1882 odr_malloc(p->odr_out, sizeof(Z_IOResultSetItem));
1884 str = ZOOM_options_get(p->options, "itemorder-setname");
1887 req->u.esRequest->notToKeep->resultSetItem->resultSetId =
1888 nmem_strdup (p->odr_out->mem, str);
1889 req->u.esRequest->notToKeep->resultSetItem->item =
1890 (int *) odr_malloc(p->odr_out, sizeof(int));
1892 str = ZOOM_options_get(p->options, "itemorder-item");
1893 *req->u.esRequest->notToKeep->resultSetItem->item =
1894 (str ? atoi(str) : 1);
1896 req->u.esRequest->notToKeep->itemRequest = encode_ill_request(p);
1902 ZOOM_package_send (ZOOM_package p, const char *type)
1909 odr_reset (p->odr_out);
1912 if (!strcmp(type, "itemorder"))
1915 apdu = create_es_package (p, VAL_ITEMORDER);
1918 r = (Z_External *) odr_malloc (p->odr_out, sizeof(*r));
1920 r->direct_reference =
1921 yaz_oidval_to_z3950oid(p->odr_out, CLASS_EXTSERV,
1924 r->which = Z_External_itemOrder;
1925 r->indirect_reference = 0;
1926 r->u.itemOrder = encode_item_order (p);
1928 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
1933 if (encode_APDU(p->connection, apdu, p->odr_out) == 0)
1937 ZOOM_task task = ZOOM_connection_add_task (c, ZOOM_TASK_PACKAGE);
1938 task->u.package = p;
1939 buf = odr_getbuf(p->odr_out, &p->len_out, 0);
1940 p->buf_out = (char *) xmalloc (p->len_out);
1941 memcpy (p->buf_out, buf, p->len_out);
1946 while (ZOOM_event (1, &c))
1953 ZOOM_API(ZOOM_package)
1954 ZOOM_connection_package (ZOOM_connection c, ZOOM_options options)
1956 ZOOM_package p = (ZOOM_package) xmalloc (sizeof(*p));
1959 p->odr_out = odr_createmem (ODR_ENCODE);
1960 p->options = ZOOM_options_create_with_parent2 (options, c->options);
1968 ZOOM_package_destroy(ZOOM_package p)
1973 if (p->refcount == 0)
1975 odr_destroy (p->odr_out);
1978 ZOOM_options_destroy (p->options);
1983 ZOOM_API(const char *)
1984 ZOOM_package_option_get (ZOOM_package p, const char *key)
1986 return ZOOM_options_get (p->options, key);
1990 static zoom_ret ZOOM_srw_search(ZOOM_connection c, ZOOM_resultset r,
1994 struct xcql__operandType *xQuery = 0;
1996 xsd__integer startRecord = r->start + 1;
1997 xsd__integer maximumRecord = r->count;
1998 const char *schema = ZOOM_resultset_option_get (r, "schema");
1999 struct zs__searchRetrieveResponse res;
2000 xsd__string recordPacking = 0;
2004 schema = "http://www.loc.gov/marcxml/";
2006 ret = soap_call_zs__searchRetrieveRequest(c->soap, c->host_port,
2008 &r->z_query->u.type_104->u.cql,
2011 &startRecord, &maximumRecord,
2017 const char **s = soap_faultdetail(c->soap);
2021 c->addinfo = xstrdup(*s);
2022 c->diagset = "SOAP";
2027 if (res.diagnostics.__sizeDiagnostics > 0)
2033 c->error = res.diagnostics.diagnostic[i]->code;
2034 if (res.diagnostics.diagnostic[i]->details)
2036 xstrdup(res.diagnostics.diagnostic[i]->details);
2042 r->size = res.numberOfRecords;
2043 if (res.resultSetId)
2044 r->setname = xstrdup(res.resultSetId);
2045 for (i = 0; i < res.records.__sizeRecords; i++)
2047 char *rdata = res.records.record[i]->recordData;
2050 Z_NamePlusRecord *npr =
2051 odr_malloc(r->odr, sizeof(*npr));
2053 z_ext_record(r->odr, VAL_TEXT_XML,
2054 rdata, strlen(rdata));
2055 npr->databaseName = 0;
2056 npr->which = Z_NamePlusRecord_databaseRecord;
2057 npr->u.databaseRecord = ext;
2058 record_cache_add (r, npr, r->start + i);
2063 return zoom_complete;
2068 ZOOM_package_option_set (ZOOM_package p, const char *key,
2071 ZOOM_options_set (p->options, key, val);
2074 static int ZOOM_connection_exec_task (ZOOM_connection c)
2076 ZOOM_task task = c->tasks;
2077 zoom_ret ret = zoom_complete;
2081 yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task task=<null>");
2084 yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task type=%d run=%d",
2085 task->which, task->running);
2086 if (c->error != ZOOM_ERROR_NONE)
2088 yaz_log (LOG_DEBUG, "remove tasks because of error = %d", c->error);
2089 ZOOM_connection_remove_tasks (c);
2094 yaz_log (LOG_DEBUG, "task already running");
2098 ret = zoom_complete;
2102 ZOOM_resultset resultset;
2103 switch (task->which)
2105 case ZOOM_TASK_SEARCH:
2106 resultset = c->tasks->u.search.resultset;
2107 if (resultset->z_query &&
2108 resultset->z_query->which == Z_Query_type_104
2109 && resultset->z_query->u.type_104->which == Z_External_CQL)
2110 ret = ZOOM_srw_search(c, resultset,
2111 resultset->z_query->u.type_104->u.cql);
2113 case ZOOM_TASK_RETRIEVE:
2114 resultset = c->tasks->u.retrieve.resultset;
2115 resultset->start = c->tasks->u.retrieve.start;
2116 resultset->count = c->tasks->u.retrieve.count;
2118 if (resultset->start >= resultset->size)
2119 return zoom_complete;
2120 if (resultset->start + resultset->count > resultset->size)
2121 resultset->count = resultset->size - resultset->start;
2123 if (resultset->z_query &&
2124 resultset->z_query->which == Z_Query_type_104
2125 && resultset->z_query->u.type_104->which == Z_External_CQL)
2126 ret = ZOOM_srw_search(c, resultset,
2127 resultset->z_query->u.type_104->u.cql);
2134 else if (c->cs || task->which == ZOOM_TASK_CONNECT)
2136 switch (task->which)
2138 case ZOOM_TASK_SEARCH:
2139 ret = ZOOM_connection_send_search(c);
2141 case ZOOM_TASK_RETRIEVE:
2142 ret = send_present (c);
2144 case ZOOM_TASK_CONNECT:
2145 ret = do_connect(c);
2147 case ZOOM_TASK_SCAN:
2150 case ZOOM_TASK_PACKAGE:
2151 ret = send_package(c);
2157 yaz_log (LOG_DEBUG, "remove tasks because no connection exist");
2158 ZOOM_connection_remove_tasks (c);
2160 if (ret == zoom_complete)
2162 yaz_log (LOG_DEBUG, "task removed (complete)");
2163 ZOOM_connection_remove_task (c);
2166 yaz_log (LOG_DEBUG, "task pending");
2170 static zoom_ret send_sort_present (ZOOM_connection c)
2172 zoom_ret r = send_sort (c);
2173 if (r == zoom_complete)
2174 r = send_present (c);
2178 static int es_response (ZOOM_connection c,
2179 Z_ExtendedServicesResponse *res)
2181 if (!c->tasks || c->tasks->which != ZOOM_TASK_PACKAGE)
2183 if (res->diagnostics && res->num_diagnostics > 0)
2184 response_diag(c, res->diagnostics[0]);
2185 if (res->taskPackage &&
2186 res->taskPackage->which == Z_External_extendedService)
2188 Z_TaskPackage *taskPackage = res->taskPackage->u.extendedService;
2189 Odr_oct *id = taskPackage->targetReference;
2192 ZOOM_options_setl (c->tasks->u.package->options,
2193 "targetReference", (char*) id->buf, id->len);
2199 static void handle_apdu (ZOOM_connection c, Z_APDU *apdu)
2201 Z_InitResponse *initrs;
2204 yaz_log (LOG_DEBUG, "handle_apdu type=%d", apdu->which);
2207 case Z_APDU_initResponse:
2208 initrs = apdu->u.initResponse;
2209 ZOOM_connection_option_set(c, "targetImplementationId",
2210 initrs->implementationId ?
2211 initrs->implementationId : "");
2212 ZOOM_connection_option_set(c, "targetImplementationName",
2213 initrs->implementationName ?
2214 initrs->implementationName : "");
2215 ZOOM_connection_option_set(c, "targetImplementationVersion",
2216 initrs->implementationVersion ?
2217 initrs->implementationVersion : "");
2218 if (!*initrs->result)
2220 set_ZOOM_error(c, ZOOM_ERROR_INIT, 0);
2225 yaz_oi_get_string_oidval (&apdu->u.initResponse->otherInfo,
2227 xfree (c->cookie_in);
2230 c->cookie_in = xstrdup(cookie);
2231 if (ODR_MASK_GET(initrs->options, Z_Options_namedResultSets) &&
2232 ODR_MASK_GET(initrs->protocolVersion, Z_ProtocolVersion_3))
2233 c->support_named_resultsets = 1;
2236 assert (c->tasks->which == ZOOM_TASK_CONNECT);
2237 ZOOM_connection_remove_task (c);
2239 ZOOM_connection_exec_task (c);
2241 if (ODR_MASK_GET(initrs->options, Z_Options_negotiationModel))
2243 NMEM tmpmem = nmem_create();
2244 Z_CharSetandLanguageNegotiation *p =
2245 yaz_get_charneg_record(initrs->otherInfo);
2249 char *charset=NULL, *lang=NULL;
2252 yaz_get_response_charneg(tmpmem, p, &charset, &lang, &sel);
2253 yaz_log(LOG_DEBUG, "Target accepted: charset %s, "
2254 "language %s, select %d",
2255 charset ? charset : "none", lang ? lang : "none", sel);
2257 ZOOM_connection_option_set (c, "negotiation-charset",
2260 ZOOM_connection_option_set (c, "negotiation-lang",
2262 nmem_destroy(tmpmem);
2266 case Z_APDU_searchResponse:
2267 handle_search_response (c, apdu->u.searchResponse);
2268 if (send_sort_present (c) == zoom_complete)
2269 ZOOM_connection_remove_task (c);
2271 case Z_APDU_presentResponse:
2272 handle_present_response (c, apdu->u.presentResponse);
2273 if (send_present (c) == zoom_complete)
2274 ZOOM_connection_remove_task (c);
2276 case Z_APDU_sortResponse:
2277 sort_response (c, apdu->u.sortResponse);
2278 if (send_present (c) == zoom_complete)
2279 ZOOM_connection_remove_task (c);
2281 case Z_APDU_scanResponse:
2282 scan_response (c, apdu->u.scanResponse);
2283 ZOOM_connection_remove_task (c);
2285 case Z_APDU_extendedServicesResponse:
2286 es_response (c, apdu->u.extendedServicesResponse);
2287 ZOOM_connection_remove_task (c);
2290 if (c->reconnect_ok)
2293 c->tasks->running = 0;
2294 ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT);
2298 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, 0);
2303 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
2308 static int do_read (ZOOM_connection c)
2314 event = ZOOM_Event_create (ZOOM_EVENT_RECV_DATA);
2315 ZOOM_connection_put_event (c, event);
2317 yaz_log (LOG_DEBUG, "do_read len=%d", c->len_in);
2319 r = cs_get (c->cs, &c->buf_in, &c->len_in);
2324 if (c->reconnect_ok)
2327 c->reconnect_ok = 0;
2328 yaz_log (LOG_DEBUG, "reconnect read");
2329 c->tasks->running = 0;
2330 ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT);
2334 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, 0);
2341 odr_reset (c->odr_in);
2342 odr_setbuf (c->odr_in, c->buf_in, r, 0);
2343 event = ZOOM_Event_create (ZOOM_EVENT_RECV_APDU);
2344 ZOOM_connection_put_event (c, event);
2345 if (!z_APDU (c->odr_in, &apdu, 0, 0))
2347 set_ZOOM_error(c, ZOOM_ERROR_DECODE, 0);
2351 handle_apdu (c, apdu);
2352 c->reconnect_ok = 0;
2357 static zoom_ret do_write_ex (ZOOM_connection c, char *buf_out, int len_out)
2362 event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA);
2363 ZOOM_connection_put_event (c, event);
2365 yaz_log (LOG_DEBUG, "do_write_ex len=%d", len_out);
2366 if ((r=cs_put (c->cs, buf_out, len_out)) < 0)
2368 if (c->reconnect_ok)
2371 c->reconnect_ok = 0;
2372 yaz_log (LOG_DEBUG, "reconnect write");
2373 c->tasks->running = 0;
2374 ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT);
2375 return zoom_complete;
2377 if (c->state == STATE_CONNECTING)
2378 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, 0);
2380 set_ZOOM_error(c, ZOOM_ERROR_CONNECTION_LOST, 0);
2382 return zoom_complete;
2386 c->mask = ZOOM_SELECT_EXCEPT;
2387 if (c->cs->io_pending & CS_WANT_WRITE)
2388 c->mask += ZOOM_SELECT_WRITE;
2389 if (c->cs->io_pending & CS_WANT_READ)
2390 c->mask += ZOOM_SELECT_READ;
2391 yaz_log (LOG_DEBUG, "do_write_ex 1 mask=%d", c->mask);
2395 c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT;
2396 yaz_log (LOG_DEBUG, "do_write_ex 2 mask=%d", c->mask);
2398 return zoom_pending;
2401 static zoom_ret do_write(ZOOM_connection c)
2403 return do_write_ex (c, c->buf_out, c->len_out);
2407 ZOOM_API(const char *)
2408 ZOOM_connection_option_get (ZOOM_connection c, const char *key)
2410 return ZOOM_options_get (c->options, key);
2414 ZOOM_connection_option_set (ZOOM_connection c, const char *key,
2417 ZOOM_options_set (c->options, key, val);
2420 ZOOM_API(const char *)
2421 ZOOM_resultset_option_get (ZOOM_resultset r, const char *key)
2423 return ZOOM_options_get (r->options, key);
2427 ZOOM_resultset_option_set (ZOOM_resultset r, const char *key,
2430 ZOOM_options_set (r->options, key, val);
2435 ZOOM_connection_errcode (ZOOM_connection c)
2437 return ZOOM_connection_error (c, 0, 0);
2440 ZOOM_API(const char *)
2441 ZOOM_connection_errmsg (ZOOM_connection c)
2444 ZOOM_connection_error (c, &msg, 0);
2448 ZOOM_API(const char *)
2449 ZOOM_connection_addinfo (ZOOM_connection c)
2451 const char *addinfo;
2452 ZOOM_connection_error (c, 0, &addinfo);
2456 ZOOM_API(const char *)
2457 ZOOM_diag_str (int error)
2461 case ZOOM_ERROR_NONE:
2463 case ZOOM_ERROR_CONNECT:
2464 return "Connect failed";
2465 case ZOOM_ERROR_MEMORY:
2466 return "Out of memory";
2467 case ZOOM_ERROR_ENCODE:
2468 return "Encoding failed";
2469 case ZOOM_ERROR_DECODE:
2470 return "Decoding failed";
2471 case ZOOM_ERROR_CONNECTION_LOST:
2472 return "Connection lost";
2473 case ZOOM_ERROR_INIT:
2474 return "Init rejected";
2475 case ZOOM_ERROR_INTERNAL:
2476 return "Internal failure";
2477 case ZOOM_ERROR_TIMEOUT:
2479 case ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
2480 return "Unsupported protocol";
2482 return diagbib1_str (error);
2487 ZOOM_connection_error_x (ZOOM_connection c, const char **cp,
2488 const char **addinfo, const char **diagset)
2490 int error = c->error;
2494 *cp = ZOOM_diag_str(error);
2496 else if (!strcmp(c->diagset, "SRW"))
2497 *cp = yaz_srw_diag_str(error);
2498 else if (c->soap && !strcmp(c->diagset, "SOAP"))
2500 const char **s = soap_faultstring(c->soap);
2508 *cp = ZOOM_diag_str(error);
2511 *addinfo = c->addinfo ? c->addinfo : "";
2513 *diagset = c->diagset ? c->diagset : "";
2518 ZOOM_connection_error (ZOOM_connection c, const char **cp,
2519 const char **addinfo)
2521 return ZOOM_connection_error_x(c, cp, addinfo, 0);
2524 static int ZOOM_connection_do_io(ZOOM_connection c, int mask)
2526 ZOOM_Event event = 0;
2527 int r = cs_look(c->cs);
2528 yaz_log (LOG_DEBUG, "ZOOM_connection_do_io c=%p mask=%d cs_look=%d",
2533 event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
2534 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, 0);
2536 ZOOM_connection_put_event (c, event);
2538 else if (r == CS_CONNECT)
2541 event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
2543 ret = cs_rcvconnect (c->cs);
2544 yaz_log (LOG_DEBUG, "cs_rcvconnect returned %d", ret);
2547 c->mask = ZOOM_SELECT_EXCEPT;
2548 if (c->cs->io_pending & CS_WANT_WRITE)
2549 c->mask += ZOOM_SELECT_WRITE;
2550 if (c->cs->io_pending & CS_WANT_READ)
2551 c->mask += ZOOM_SELECT_READ;
2552 ZOOM_connection_put_event (c, event);
2556 ZOOM_connection_put_event (c, event);
2557 ZOOM_connection_send_init (c);
2558 c->state = STATE_ESTABLISHED;
2562 set_ZOOM_error(c, ZOOM_ERROR_CONNECT, 0);
2564 ZOOM_connection_put_event (c, event);
2569 if (mask & ZOOM_SELECT_READ)
2571 if (c->cs && (mask & ZOOM_SELECT_WRITE))
2578 ZOOM_connection_last_event(ZOOM_connection cs)
2581 return ZOOM_EVENT_NONE;
2582 return cs->last_event;
2586 ZOOM_event (int no, ZOOM_connection *cs)
2590 struct pollfd pollfds[1024];
2591 ZOOM_connection poll_cs[1024];
2594 fd_set input, output, except;
2599 for (i = 0; i<no; i++)
2601 ZOOM_connection c = cs[i];
2603 if (c && (event = ZOOM_connection_get_event(c)))
2605 ZOOM_Event_destroy (event);
2609 for (i = 0; i<no; i++)
2611 ZOOM_connection c = cs[i];
2613 if (c && ZOOM_connection_exec_task (c))
2615 if ((event = ZOOM_connection_get_event(c)))
2617 ZOOM_Event_destroy (event);
2630 for (i = 0; i<no; i++)
2632 ZOOM_connection c = cs[i];
2638 fd = z3950_connection_socket(c);
2639 mask = z3950_connection_mask(c);
2646 this_timeout = ZOOM_options_get_int (c->options, "timeout", -1);
2647 if (this_timeout != -1 && this_timeout < timeout)
2648 timeout = this_timeout;
2652 short poll_events = 0;
2654 if (mask & ZOOM_SELECT_READ)
2655 poll_events += POLLIN;
2656 if (mask & ZOOM_SELECT_WRITE)
2657 poll_events += POLLOUT;
2658 if (mask & ZOOM_SELECT_EXCEPT)
2659 poll_events += POLLERR;
2660 pollfds[nfds].fd = fd;
2661 pollfds[nfds].events = poll_events;
2662 pollfds[nfds].revents = 0;
2667 if (mask & ZOOM_SELECT_READ)
2669 FD_SET (fd, &input);
2672 if (mask & ZOOM_SELECT_WRITE)
2674 FD_SET (fd, &output);
2677 if (mask & ZOOM_SELECT_EXCEPT)
2679 FD_SET (fd, &except);
2684 if (timeout >= 5000)
2691 r = poll (pollfds, nfds, timeout * 1000);
2692 for (i = 0; i<nfds; i++)
2694 ZOOM_connection c = poll_cs[i];
2698 if (pollfds[i].revents & POLLIN)
2699 mask += ZOOM_SELECT_READ;
2700 if (pollfds[i].revents & POLLOUT)
2701 mask += ZOOM_SELECT_WRITE;
2702 if (pollfds[i].revents & POLLERR)
2703 mask += ZOOM_SELECT_EXCEPT;
2705 ZOOM_connection_do_io(c, mask);
2707 else if (r == 0 && c->mask)
2709 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
2710 /* timeout and this connection was waiting */
2711 set_ZOOM_error(c, ZOOM_ERROR_TIMEOUT, 0);
2713 ZOOM_connection_put_event(c, event);
2717 tv.tv_sec = timeout;
2719 yaz_log (LOG_DEBUG, "select start");
2720 r = select (max_fd+1, &input, &output, &except, &tv);
2721 yaz_log (LOG_DEBUG, "select stop, returned r=%d", r);
2722 for (i = 0; i<no; i++)
2724 ZOOM_connection c = cs[i];
2729 fd = z3950_connection_socket(c);
2733 /* no timeout and real socket */
2734 if (FD_ISSET(fd, &input))
2735 mask += ZOOM_SELECT_READ;
2736 if (FD_ISSET(fd, &output))
2737 mask += ZOOM_SELECT_WRITE;
2738 if (FD_ISSET(fd, &except))
2739 mask += ZOOM_SELECT_EXCEPT;
2741 ZOOM_connection_do_io(c, mask);
2743 if (r == 0 && c->mask)
2745 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
2746 /* timeout and this connection was waiting */
2747 set_ZOOM_error(c, ZOOM_ERROR_TIMEOUT, 0);
2749 yaz_log (LOG_DEBUG, "timeout");
2750 ZOOM_connection_put_event(c, event);
2754 for (i = 0; i<no; i++)
2756 ZOOM_connection c = cs[i];
2758 if (c && (event = ZOOM_connection_get_event(c)))
2760 ZOOM_Event_destroy (event);