2 * $Id: zoom-c.c,v 1.7 2002-11-15 10:38:37 adam Exp $
4 * ZOOM layer for C, connections, result sets, queries.
7 #include <yaz/xmalloc.h>
8 #include <yaz/otherinfo.h>
10 #include <yaz/pquery.h>
11 #include <yaz/diagbib1.h>
12 #include <yaz/charneg.h>
27 static zoom_ret ZOOM_connection_send_init (ZOOM_connection c);
28 static zoom_ret do_write_ex (ZOOM_connection c, char *buf_out, int len_out);
30 static ZOOM_Event ZOOM_Event_create (int kind)
32 ZOOM_Event event = (ZOOM_Event) xmalloc (sizeof(*event));
39 static void ZOOM_Event_destroy (ZOOM_Event event)
44 static void ZOOM_connection_put_event (ZOOM_connection c, ZOOM_Event event)
48 c->m_queue_back->prev = event;
49 assert (c->m_queue_front);
53 assert (!c->m_queue_front);
54 c->m_queue_front = event;
56 event->next = c->m_queue_back;
58 c->m_queue_back = event;
61 static ZOOM_Event ZOOM_connection_get_event(ZOOM_connection c)
63 ZOOM_Event event = c->m_queue_front;
66 assert (c->m_queue_back);
67 c->m_queue_front = event->prev;
70 assert (c->m_queue_back);
71 c->m_queue_front->next = 0;
75 c->last_event = event->kind;
79 static void clear_error (ZOOM_connection c)
84 case ZOOM_ERROR_CONNECT:
85 case ZOOM_ERROR_MEMORY:
86 case ZOOM_ERROR_DECODE:
87 case ZOOM_ERROR_CONNECTION_LOST:
89 case ZOOM_ERROR_INTERNAL:
92 c->error = ZOOM_ERROR_NONE;
98 ZOOM_task ZOOM_connection_add_task (ZOOM_connection c, int which)
100 ZOOM_task *taskp = &c->tasks;
102 taskp = &(*taskp)->next;
103 *taskp = (ZOOM_task) xmalloc (sizeof(**taskp));
104 (*taskp)->running = 0;
105 (*taskp)->which = which;
111 ZOOM_task ZOOM_connection_insert_task (ZOOM_connection c, int which)
113 ZOOM_task task = (ZOOM_task) xmalloc (sizeof(*task));
115 task->next = c->tasks;
124 void ZOOM_connection_remove_task (ZOOM_connection c)
126 ZOOM_task task = c->tasks;
130 c->tasks = task->next;
133 case ZOOM_TASK_SEARCH:
134 ZOOM_resultset_destroy (task->u.search.resultset);
136 case ZOOM_TASK_RETRIEVE:
137 ZOOM_resultset_destroy (task->u.retrieve.resultset);
139 case ZOOM_TASK_CONNECT:
142 ZOOM_scanset_destroy (task->u.scan.scan);
144 case ZOOM_TASK_PACKAGE:
145 ZOOM_package_destroy (task->u.package);
154 void ZOOM_connection_remove_tasks (ZOOM_connection c)
157 ZOOM_connection_remove_task(c);
160 static ZOOM_record record_cache_lookup (ZOOM_resultset r, int pos);
162 ZOOM_API(ZOOM_connection)
163 ZOOM_connection_create (ZOOM_options options)
165 ZOOM_connection c = (ZOOM_connection) xmalloc (sizeof(*c));
170 c->state = STATE_IDLE;
171 c->error = ZOOM_ERROR_NONE;
179 c->options = ZOOM_options_create_with_parent(options);
184 c->charset = c->lang = 0;
191 c->odr_in = odr_createmem (ODR_DECODE);
192 c->odr_out = odr_createmem (ODR_ENCODE);
195 c->support_named_resultsets = 0;
196 c->last_event = ZOOM_EVENT_NONE;
198 c->m_queue_front = 0;
203 /* set database names. Take local databases (if set); otherwise
204 take databases given in ZURL (if set); otherwise use Default */
205 static char **set_DatabaseNames (ZOOM_connection con, ZOOM_options options,
208 char **databaseNames;
211 const char *cp = ZOOM_options_get (options, "databaseName");
215 if (strncmp (con->host_port, "unix:", 5) == 0)
216 cp = strchr (con->host_port+5, ':');
218 cp = strchr (con->host_port, '/');
225 while ((c = strchr(c, '+')))
233 databaseNames = (char**)
234 odr_malloc (con->odr_out, no * sizeof(*databaseNames));
238 c = strchr (cp, '+');
246 /* cp ptr to first char of db name, c is char
248 databaseNames[no] = (char*) odr_malloc (con->odr_out, 1+c-cp);
249 memcpy (databaseNames[no], cp, c-cp);
250 databaseNames[no++][c-cp] = '\0';
255 databaseNames[no] = NULL;
257 return databaseNames;
260 ZOOM_API(ZOOM_connection)
261 ZOOM_connection_new (const char *host, int portnum)
263 ZOOM_connection c = ZOOM_connection_create (0);
265 ZOOM_connection_connect (c, host, portnum);
270 ZOOM_connection_connect(ZOOM_connection c,
271 const char *host, int portnum)
278 yaz_log (LOG_DEBUG, "reconnect");
282 yaz_log(LOG_DEBUG, "connect");
284 val = ZOOM_options_get (c->options, "proxy");
286 c->proxy = xstrdup (val);
291 val = ZOOM_options_get (c->options, "charset");
293 c->charset = xstrdup (val);
298 val = ZOOM_options_get (c->options, "lang");
300 c->lang = xstrdup (val);
304 xfree (c->host_port);
308 sprintf (hostn, "%.80s:%d", host, portnum);
309 c->host_port = xstrdup(hostn);
312 c->host_port = xstrdup(host);
314 ZOOM_options_set(c->options, "host", c->host_port);
316 val = ZOOM_options_get (c->options, "cookie");
318 c->cookie_out = xstrdup (val);
320 val = ZOOM_options_get (c->options, "clientIP");
322 c->client_IP = xstrdup (val);
324 c->async = ZOOM_options_get_bool (c->options, "async", 0);
326 c->error = ZOOM_ERROR_NONE;
328 task = ZOOM_connection_add_task (c, ZOOM_TASK_CONNECT);
332 while (ZOOM_event (1, &c))
338 ZOOM_query_create(void)
340 ZOOM_query s = (ZOOM_query) xmalloc (sizeof(*s));
345 s->odr = odr_createmem (ODR_ENCODE);
351 ZOOM_query_destroy(ZOOM_query s)
357 yaz_log (LOG_DEBUG, "ZOOM_query_destroy count=%d", s->refcount);
358 if (s->refcount == 0)
360 odr_destroy (s->odr);
366 ZOOM_query_prefix(ZOOM_query s, const char *str)
368 s->query = (Z_Query *) odr_malloc (s->odr, sizeof(*s->query));
369 s->query->which = Z_Query_type_1;
370 s->query->u.type_1 = p_query_rpn(s->odr, PROTO_Z3950, str);
371 if (!s->query->u.type_1)
377 ZOOM_query_sortby(ZOOM_query s, const char *criteria)
379 s->sort_spec = yaz_sort_spec (s->odr, criteria);
385 static zoom_ret do_write(ZOOM_connection c);
388 ZOOM_connection_destroy(ZOOM_connection c)
395 for (r = c->resultsets; r; r = r->next)
400 odr_destroy (c->odr_in);
401 odr_destroy (c->odr_out);
402 ZOOM_options_destroy (c->options);
403 ZOOM_connection_remove_tasks (c);
404 xfree (c->host_port);
408 xfree (c->cookie_out);
409 xfree (c->cookie_in);
410 xfree (c->client_IP);
414 void ZOOM_resultset_addref (ZOOM_resultset r)
419 yaz_log (LOG_DEBUG, "ZOOM_resultset_addref r=%p count=%d",
423 ZOOM_resultset ZOOM_resultset_create ()
425 ZOOM_resultset r = (ZOOM_resultset) xmalloc (sizeof(*r));
427 yaz_log (LOG_DEBUG, "ZOOM_resultset_create r = %p", r);
430 r->odr = odr_createmem (ODR_ENCODE);
444 ZOOM_API(ZOOM_resultset)
445 ZOOM_connection_search_pqf(ZOOM_connection c, const char *q)
448 ZOOM_query s = ZOOM_query_create();
450 ZOOM_query_prefix (s, q);
452 r = ZOOM_connection_search (c, s);
453 ZOOM_query_destroy (s);
457 ZOOM_API(ZOOM_resultset)
458 ZOOM_connection_search(ZOOM_connection c, ZOOM_query q)
460 ZOOM_resultset r = ZOOM_resultset_create ();
464 r->r_sort_spec = q->sort_spec;
465 r->r_query = q->query;
468 r->options = ZOOM_options_create_with_parent(c->options);
470 r->start = ZOOM_options_get_int(r->options, "start", 0);
471 r->count = ZOOM_options_get_int(r->options, "count", 0);
472 r->piggyback = ZOOM_options_get_bool (r->options, "piggyback", 1);
473 cp = ZOOM_options_get (r->options, "setname");
475 r->setname = xstrdup (cp);
479 r->next = c->resultsets;
482 task = ZOOM_connection_add_task (c, ZOOM_TASK_SEARCH);
483 task->u.search.resultset = r;
484 ZOOM_resultset_addref (r);
490 while (ZOOM_event (1, &c))
497 ZOOM_resultset_destroy(ZOOM_resultset r)
502 yaz_log (LOG_DEBUG, "ZOOM_resultset_destroy r = %p count=%d",
504 if (r->refcount == 0)
506 ZOOM_record_cache rc;
508 for (rc = r->record_cache; rc; rc = rc->next)
509 if (rc->rec.wrbuf_marc)
510 wrbuf_free (rc->rec.wrbuf_marc, 1);
513 /* remove ourselves from the resultsets in connection */
514 ZOOM_resultset *rp = &r->connection->resultsets;
517 assert (*rp); /* we must be in this list!! */
519 { /* OK, we're here - take us out of it */
526 ZOOM_query_destroy (r->search);
527 ZOOM_options_destroy (r->options);
528 odr_destroy (r->odr);
535 ZOOM_resultset_size (ZOOM_resultset r)
540 static void do_close (ZOOM_connection c)
546 c->state = STATE_IDLE;
549 static void ZOOM_resultset_retrieve (ZOOM_resultset r,
550 int force_sync, int start, int count)
560 task = ZOOM_connection_add_task (c, ZOOM_TASK_RETRIEVE);
561 task->u.retrieve.resultset = r;
562 task->u.retrieve.start = start;
563 task->u.retrieve.count = count;
565 ZOOM_resultset_addref (r);
567 if (!r->connection->async || force_sync)
568 while (r->connection && ZOOM_event (1, &r->connection))
573 ZOOM_resultset_records (ZOOM_resultset r, ZOOM_record *recs,
574 size_t start, size_t count)
576 int force_present = 0;
582 ZOOM_resultset_retrieve (r, force_present, start, count);
586 for (i = 0; i< count; i++)
587 recs[i] = ZOOM_resultset_record_immediate (r, i+start);
591 static zoom_ret do_connect (ZOOM_connection c)
594 const char *effective_host;
597 effective_host = c->proxy;
599 effective_host = c->host_port;
601 yaz_log (LOG_DEBUG, "do_connect host=%s", effective_host);
604 c->cs = cs_create_host (effective_host, 0, &add);
608 int ret = cs_connect (c->cs, add);
611 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
612 ZOOM_connection_put_event(c, event);
613 ZOOM_connection_send_init(c);
614 c->state = STATE_ESTABLISHED;
619 c->state = STATE_CONNECTING;
620 c->mask = ZOOM_SELECT_EXCEPT;
621 if (c->cs->io_pending & CS_WANT_WRITE)
622 c->mask += ZOOM_SELECT_WRITE;
623 if (c->cs->io_pending & CS_WANT_READ)
624 c->mask += ZOOM_SELECT_READ;
628 c->state = STATE_IDLE;
629 c->error = ZOOM_ERROR_CONNECT;
630 return zoom_complete;
633 int z3950_connection_socket(ZOOM_connection c)
636 return cs_fileno(c->cs);
640 int z3950_connection_mask(ZOOM_connection c)
647 static void otherInfo_attach (ZOOM_connection c, Z_APDU *a, ODR out)
650 for (i = 0; i<200; i++)
653 Z_OtherInformation **oi;
659 sprintf (buf, "otherInfo%d", i);
660 val = ZOOM_options_get (c->options, buf);
663 cp = strchr (val, ':');
667 if (len >= sizeof(buf))
669 memcpy (buf, val, len);
671 oidval = oid_getvalbyname (buf);
672 if (oidval == VAL_NONE)
676 yaz_oi_set_string_oidval(oi, out, oidval, 1, cp+1);
680 static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out)
685 Z_OtherInformation **oi;
687 yaz_oi_set_string_oidval(oi, out, VAL_COOKIE, 1, c->cookie_out);
691 Z_OtherInformation **oi;
693 yaz_oi_set_string_oidval(oi, out, VAL_CLIENT_IP, 1, c->client_IP);
695 otherInfo_attach (c, a, out);
696 if (!z_APDU(out, &a, 0, 0))
698 FILE *outf = fopen("/tmp/apdu.txt", "a");
701 ODR odr_pr = odr_createmem(ODR_PRINT);
702 fprintf (outf, "a=%p\n", a);
703 odr_setprint(odr_pr, outf);
704 z_APDU(odr_pr, &a, 0, 0);
707 yaz_log (LOG_DEBUG, "encoding failed");
708 c->error = ZOOM_ERROR_ENCODE;
716 static zoom_ret send_APDU (ZOOM_connection c, Z_APDU *a)
720 if (encode_APDU(c, a, c->odr_out))
721 return zoom_complete;
722 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
723 event = ZOOM_Event_create (ZOOM_EVENT_SEND_APDU);
724 ZOOM_connection_put_event (c, event);
725 odr_reset(c->odr_out);
729 /* returns 1 if PDU was sent OK (still pending )
730 0 if PDU was not sent OK (nothing to wait for)
733 static zoom_ret ZOOM_connection_send_init (ZOOM_connection c)
736 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest);
737 Z_InitRequest *ireq = apdu->u.initRequest;
738 Z_IdAuthentication *auth = (Z_IdAuthentication *)
739 odr_malloc(c->odr_out, sizeof(*auth));
740 const char *auth_groupId = ZOOM_options_get (c->options, "group");
741 const char *auth_userId = ZOOM_options_get (c->options, "user");
742 const char *auth_password = ZOOM_options_get (c->options, "pass");
744 ODR_MASK_SET(ireq->options, Z_Options_search);
745 ODR_MASK_SET(ireq->options, Z_Options_present);
746 ODR_MASK_SET(ireq->options, Z_Options_scan);
747 ODR_MASK_SET(ireq->options, Z_Options_sort);
748 ODR_MASK_SET(ireq->options, Z_Options_extendedServices);
749 ODR_MASK_SET(ireq->options, Z_Options_namedResultSets);
751 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
752 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
753 ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
755 impname = ZOOM_options_get (c->options, "implementationName");
756 ireq->implementationName =
757 (char *) odr_malloc (c->odr_out, 15 + (impname ? strlen(impname) : 0));
758 strcpy (ireq->implementationName, "");
761 strcat (ireq->implementationName, impname);
762 strcat (ireq->implementationName, "/");
764 strcat (ireq->implementationName, "ZOOM-C/YAZ");
766 *ireq->maximumRecordSize =
767 ZOOM_options_get_int (c->options, "maximumRecordSize", 1024*1024);
768 *ireq->preferredMessageSize =
769 ZOOM_options_get_int (c->options, "preferredMessageSize", 1024*1024);
771 if (auth_groupId || auth_password)
773 Z_IdPass *pass = (Z_IdPass *) odr_malloc(c->odr_out, sizeof(*pass));
776 if (auth_groupId && *auth_groupId)
778 pass->groupId = (char *)
779 odr_malloc(c->odr_out, strlen(auth_groupId)+1);
780 strcpy(pass->groupId, auth_groupId);
784 if (auth_userId && *auth_userId)
786 pass->userId = (char *)
787 odr_malloc(c->odr_out, strlen(auth_userId)+1);
788 strcpy(pass->userId, auth_userId);
792 if (auth_password && *auth_password)
794 pass->password = (char *)
795 odr_malloc(c->odr_out, strlen(auth_password)+1);
796 strcpy(pass->password, auth_password);
801 auth->which = Z_IdAuthentication_idPass;
802 auth->u.idPass = pass;
803 ireq->idAuthentication = auth;
806 else if (auth_userId)
808 auth->which = Z_IdAuthentication_open;
809 auth->u.open = (char *)
810 odr_malloc(c->odr_out, strlen(auth_userId)+1);
811 strcpy(auth->u.open, auth_userId);
812 ireq->idAuthentication = auth;
815 yaz_oi_set_string_oidval(&ireq->otherInfo, c->odr_out,
816 VAL_PROXY, 1, c->host_port);
817 if (c->charset||c->lang)
819 Z_OtherInformation **oi;
820 Z_OtherInformationUnit *oi_unit;
822 yaz_oi_APDU(apdu, &oi);
824 if ((oi_unit = yaz_oi_update(oi, c->odr_out, NULL, 0, 0)))
826 ODR_MASK_SET(ireq->options, Z_Options_negotiationModel);
828 oi_unit->which = Z_OtherInfo_externallyDefinedInfo;
829 oi_unit->information.externallyDefinedInfo =
830 yaz_set_proposal_charneg
832 (const char **)&c->charset, (c->charset) ? 1:0,
833 (const char **)&c->lang, (c->lang) ? 1:0, 1);
837 return send_APDU (c, apdu);
840 static zoom_ret ZOOM_connection_send_search (ZOOM_connection c)
843 int lslb, ssub, mspn;
845 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
846 Z_SearchRequest *search_req = apdu->u.searchRequest;
847 const char *elementSetName;
848 const char *smallSetElementSetName;
849 const char *mediumSetElementSetName;
853 assert (c->tasks->which == ZOOM_TASK_SEARCH);
855 r = c->tasks->u.search.resultset;
858 ZOOM_options_get (r->options, "elementSetName");
859 smallSetElementSetName =
860 ZOOM_options_get (r->options, "smallSetElementSetName");
861 mediumSetElementSetName =
862 ZOOM_options_get (r->options, "mediumSetElementSetName");
864 ZOOM_options_get (r->options, "schema");
866 if (!smallSetElementSetName)
867 smallSetElementSetName = elementSetName;
869 if (!mediumSetElementSetName)
870 mediumSetElementSetName = elementSetName;
875 /* prepare query for the search request */
876 search_req->query = r->r_query;
878 search_req->databaseNames =
879 set_DatabaseNames (c, r->options, &search_req->num_databaseNames);
881 /* get syntax (no need to provide unless piggyback is in effect) */
882 syntax = ZOOM_options_get (r->options, "preferredRecordSyntax");
884 lslb = ZOOM_options_get_int (r->options, "largeSetLowerBound", -1);
885 ssub = ZOOM_options_get_int (r->options, "smallSetUpperBound", -1);
886 mspn = ZOOM_options_get_int (r->options, "mediumSetPresentNumber", -1);
887 if (lslb != -1 && ssub != -1 && mspn != -1)
889 /* So're a Z39.50 expert? Let's hope you don't do sort */
890 *search_req->largeSetLowerBound = lslb;
891 *search_req->smallSetUpperBound = ssub;
892 *search_req->mediumSetPresentNumber = mspn;
894 else if (r->start == 0 && r->count > 0
895 && r->piggyback && !r->r_sort_spec && !schema)
897 /* Regular piggyback - do it unless we're going to do sort */
898 *search_req->largeSetLowerBound = 2000000000;
899 *search_req->smallSetUpperBound = r->count;
900 *search_req->mediumSetPresentNumber = r->count;
901 smallSetElementSetName = 0; /* no need to provide this */
905 /* non-piggyback. Need not provide elementsets or syntaxes .. */
906 smallSetElementSetName = 0;
907 mediumSetElementSetName = 0;
910 if (smallSetElementSetName && *smallSetElementSetName)
912 Z_ElementSetNames *esn = (Z_ElementSetNames *)
913 odr_malloc (c->odr_out, sizeof(*esn));
915 esn->which = Z_ElementSetNames_generic;
916 esn->u.generic = odr_strdup (c->odr_out, smallSetElementSetName);
917 search_req->smallSetElementSetNames = esn;
919 if (mediumSetElementSetName && *mediumSetElementSetName)
921 Z_ElementSetNames *esn = (Z_ElementSetNames *)
922 odr_malloc (c->odr_out, sizeof(*esn));
924 esn->which = Z_ElementSetNames_generic;
925 esn->u.generic = odr_strdup (c->odr_out, mediumSetElementSetName);
926 search_req->mediumSetElementSetNames = esn;
929 search_req->preferredRecordSyntax =
930 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
934 if (c->support_named_resultsets)
938 /* find the lowest unused ordinal so that we re-use
939 result sets on the server. */
940 for (ord = 1; ; ord++)
943 sprintf (setname, "%d", ord);
944 for (rp = c->resultsets; rp; rp = rp->next)
945 if (rp->setname && !strcmp (rp->setname, setname))
950 r->setname = xstrdup (setname);
951 yaz_log (LOG_DEBUG, "allocating set %s", r->setname);
954 r->setname = xstrdup ("default");
955 ZOOM_options_set (r->options, "setname", r->setname);
957 search_req->resultSetName = odr_strdup(c->odr_out, r->setname);
958 /* send search request */
959 return send_APDU (c, apdu);
962 static void response_diag (ZOOM_connection c, Z_DiagRec *p)
964 Z_DefaultDiagFormat *r;
969 if (p->which != Z_DiagRec_defaultFormat)
971 c->error = ZOOM_ERROR_DECODE;
974 r = p->u.defaultFormat;
977 case Z_DefaultDiagFormat_v2Addinfo:
978 addinfo = r->u.v2Addinfo;
980 case Z_DefaultDiagFormat_v3Addinfo:
981 addinfo = r->u.v3Addinfo;
985 c->addinfo = xstrdup (addinfo);
986 c->error = *r->condition;
989 ZOOM_API(ZOOM_record)
990 ZOOM_record_clone (ZOOM_record srec)
997 odr_enc = odr_createmem(ODR_ENCODE);
998 if (!z_NamePlusRecord (odr_enc, &srec->npr, 0, 0))
1000 buf = odr_getbuf (odr_enc, &size, 0);
1002 nrec = (ZOOM_record) xmalloc (sizeof(*nrec));
1003 nrec->odr = odr_createmem(ODR_DECODE);
1004 nrec->wrbuf_marc = 0;
1005 odr_setbuf (nrec->odr, buf, size, 0);
1006 z_NamePlusRecord (nrec->odr, &nrec->npr, 0, 0);
1008 odr_destroy (odr_enc);
1012 ZOOM_API(ZOOM_record)
1013 ZOOM_resultset_record_immediate (ZOOM_resultset s,size_t pos)
1015 return record_cache_lookup (s, pos);
1018 ZOOM_API(ZOOM_record)
1019 ZOOM_resultset_record (ZOOM_resultset r, size_t pos)
1021 ZOOM_resultset_retrieve (r, 1, pos, 1);
1022 return ZOOM_resultset_record_immediate (r, pos);
1026 ZOOM_record_destroy (ZOOM_record rec)
1030 if (rec->wrbuf_marc)
1031 wrbuf_free (rec->wrbuf_marc, 1);
1032 odr_destroy (rec->odr);
1036 ZOOM_API(const char *)
1037 ZOOM_record_get (ZOOM_record rec, const char *type, int *len)
1039 Z_NamePlusRecord *npr;
1042 *len = 0; /* default return */
1049 if (!strcmp (type, "database"))
1052 *len = (npr->databaseName ? strlen(npr->databaseName) : 0);
1053 return npr->databaseName;
1055 else if (!strcmp (type, "syntax"))
1057 const char *desc = 0;
1058 if (npr->which == Z_NamePlusRecord_databaseRecord)
1060 Z_External *r = (Z_External *) npr->u.databaseRecord;
1061 oident *ent = oid_getentbyoid(r->direct_reference);
1068 *len = strlen(desc);
1071 else if (!strcmp (type, "render") &&
1072 npr->which == Z_NamePlusRecord_databaseRecord)
1074 Z_External *r = (Z_External *) npr->u.databaseRecord;
1075 oident *ent = oid_getentbyoid(r->direct_reference);
1077 if (r->which == Z_External_sutrs)
1079 if (len) *len = r->u.sutrs->len;
1080 return (const char *) r->u.sutrs->buf;
1082 else if (r->which == Z_External_octet)
1091 case VAL_APPLICATION_XML:
1094 if (!rec->wrbuf_marc)
1095 rec->wrbuf_marc = wrbuf_alloc();
1096 wrbuf_rewind (rec->wrbuf_marc);
1097 if (yaz_marc_decode ((const char *)
1098 r->u.octet_aligned->buf,
1100 r->u.octet_aligned->len,
1103 if (len) *len = wrbuf_len(rec->wrbuf_marc);
1104 return wrbuf_buf(rec->wrbuf_marc);
1107 if (len) *len = r->u.octet_aligned->len;
1108 return (const char *) r->u.octet_aligned->buf;
1110 else if (r->which == Z_External_grs1)
1112 if (!rec->wrbuf_marc)
1113 rec->wrbuf_marc = wrbuf_alloc();
1114 wrbuf_rewind (rec->wrbuf_marc);
1115 yaz_display_grs1(rec->wrbuf_marc, r->u.grs1, 0);
1117 *len = wrbuf_len(rec->wrbuf_marc);
1118 return wrbuf_buf(rec->wrbuf_marc);
1122 else if (!strcmp (type, "xml") &&
1123 npr->which == Z_NamePlusRecord_databaseRecord)
1125 Z_External *r = (Z_External *) npr->u.databaseRecord;
1126 oident *ent = oid_getentbyoid(r->direct_reference);
1128 if (r->which == Z_External_sutrs)
1130 if (len) *len = r->u.sutrs->len;
1131 return (const char *) r->u.sutrs->buf;
1133 else if (r->which == Z_External_octet)
1142 case VAL_APPLICATION_XML:
1145 if (!rec->wrbuf_marc)
1146 rec->wrbuf_marc = wrbuf_alloc();
1147 wrbuf_rewind (rec->wrbuf_marc);
1148 if (yaz_marc_decode ((const char *)
1149 r->u.octet_aligned->buf,
1151 r->u.octet_aligned->len,
1154 if (len) *len = wrbuf_len(rec->wrbuf_marc);
1155 return wrbuf_buf(rec->wrbuf_marc);
1158 if (len) *len = r->u.octet_aligned->len;
1159 return (const char *) r->u.octet_aligned->buf;
1161 else if (r->which == Z_External_grs1)
1168 else if (!strcmp (type, "raw"))
1170 if (npr->which == Z_NamePlusRecord_databaseRecord)
1172 Z_External *r = (Z_External *) npr->u.databaseRecord;
1174 if (r->which == Z_External_sutrs)
1176 if (len) *len = r->u.sutrs->len;
1177 return (const char *) r->u.sutrs->buf;
1179 else if (r->which == Z_External_octet)
1181 if (len) *len = r->u.octet_aligned->len;
1182 return (const char *) r->u.octet_aligned->buf;
1184 else /* grs-1, explain, ... */
1187 return (const char *) npr->u.databaseRecord;
1192 else if (!strcmp (type, "ext"))
1194 if (npr->which == Z_NamePlusRecord_databaseRecord)
1195 return (const char *) npr->u.databaseRecord;
1201 static void record_cache_add (ZOOM_resultset r, Z_NamePlusRecord *npr, int pos)
1203 ZOOM_record_cache rc;
1204 const char *elementSetName =
1205 ZOOM_resultset_option_get (r, "elementSetName");
1206 const char *syntax =
1207 ZOOM_resultset_option_get (r, "preferredRecordSyntax");
1210 for (rc = r->record_cache; rc; rc = rc->next)
1214 if ((!elementSetName && !rc->elementSetName)
1215 || (elementSetName && rc->elementSetName &&
1216 !strcmp (elementSetName, rc->elementSetName)))
1218 if ((!syntax && !rc->syntax)
1219 || (syntax && rc->syntax &&
1220 !strcmp (syntax, rc->syntax)))
1222 /* not destroying rc->npr (it's handled by nmem )*/
1224 /* keeping wrbuf_marc too */
1230 rc = (ZOOM_record_cache) odr_malloc (r->odr, sizeof(*rc));
1233 rc->rec.wrbuf_marc = 0;
1235 rc->elementSetName = odr_strdup (r->odr, elementSetName);
1237 rc->elementSetName = 0;
1240 rc->syntax = odr_strdup (r->odr, syntax);
1245 rc->next = r->record_cache;
1246 r->record_cache = rc;
1249 static ZOOM_record record_cache_lookup (ZOOM_resultset r, int pos)
1251 ZOOM_record_cache rc;
1252 const char *elementSetName =
1253 ZOOM_resultset_option_get (r, "elementSetName");
1254 const char *syntax =
1255 ZOOM_resultset_option_get (r, "preferredRecordSyntax");
1257 for (rc = r->record_cache; rc; rc = rc->next)
1261 if ((!elementSetName && !rc->elementSetName)
1262 || (elementSetName && rc->elementSetName &&
1263 !strcmp (elementSetName, rc->elementSetName)))
1265 if ((!syntax && !rc->syntax)
1266 || (syntax && rc->syntax &&
1267 !strcmp (syntax, rc->syntax)))
1275 static void handle_records (ZOOM_connection c, Z_Records *sr,
1278 ZOOM_resultset resultset;
1282 switch (c->tasks->which)
1284 case ZOOM_TASK_SEARCH:
1285 resultset = c->tasks->u.search.resultset;
1287 case ZOOM_TASK_RETRIEVE:
1288 resultset = c->tasks->u.retrieve.resultset;
1293 if (sr && sr->which == Z_Records_NSD)
1295 Z_DiagRec dr, *dr_p = &dr;
1296 dr.which = Z_DiagRec_defaultFormat;
1297 dr.u.defaultFormat = sr->u.nonSurrogateDiagnostic;
1299 response_diag (c, dr_p);
1301 else if (sr && sr->which == Z_Records_multipleNSD)
1303 if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
1304 response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
1306 c->error = ZOOM_ERROR_DECODE;
1310 if (resultset->count + resultset->start > resultset->size)
1311 resultset->count = resultset->size - resultset->start;
1312 if (resultset->count < 0)
1313 resultset->count = 0;
1314 if (sr && sr->which == Z_Records_DBOSD)
1317 NMEM nmem = odr_extract_mem (c->odr_in);
1318 Z_NamePlusRecordList *p =
1319 sr->u.databaseOrSurDiagnostics;
1320 for (i = 0; i<p->num_records; i++)
1322 record_cache_add (resultset, p->records[i],
1323 i+ resultset->start);
1325 /* transfer our response to search_nmem .. we need it later */
1326 nmem_transfer (resultset->odr->mem, nmem);
1327 nmem_destroy (nmem);
1328 if (present_phase && p->num_records == 0)
1330 /* present response and we didn't get any records! */
1331 c->error = ZOOM_ERROR_DECODE;
1334 else if (present_phase)
1336 /* present response and we didn't get any records! */
1337 c->error = ZOOM_ERROR_DECODE;
1342 static void handle_present_response (ZOOM_connection c, Z_PresentResponse *pr)
1344 handle_records (c, pr->records, 1);
1347 static void handle_search_response (ZOOM_connection c, Z_SearchResponse *sr)
1349 ZOOM_resultset resultset;
1351 yaz_log (LOG_DEBUG, "got search response");
1353 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1356 resultset = c->tasks->u.search.resultset;
1358 resultset->size = *sr->resultCount;
1359 handle_records (c, sr->records, 0);
1362 static void sort_response (ZOOM_connection c, Z_SortResponse *res)
1364 if (res->diagnostics && res->num_diagnostics > 0)
1365 response_diag (c, res->diagnostics[0]);
1368 static int scan_response (ZOOM_connection c, Z_ScanResponse *res)
1370 NMEM nmem = odr_extract_mem (c->odr_in);
1373 if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
1375 scan = c->tasks->u.scan.scan;
1377 if (res->entries && res->entries->nonsurrogateDiagnostics)
1378 response_diag(c, res->entries->nonsurrogateDiagnostics[0]);
1379 scan->scan_response = res;
1380 nmem_transfer (scan->odr->mem, nmem);
1382 ZOOM_options_set_int (scan->options, "stepSize", *res->stepSize);
1383 if (res->positionOfTerm)
1384 ZOOM_options_set_int (scan->options, "position", *res->positionOfTerm);
1385 if (res->scanStatus)
1386 ZOOM_options_set_int (scan->options, "scanStatus", *res->scanStatus);
1387 if (res->numberOfEntriesReturned)
1388 ZOOM_options_set_int (scan->options, "number",
1389 *res->numberOfEntriesReturned);
1390 nmem_destroy (nmem);
1394 static zoom_ret send_sort (ZOOM_connection c)
1396 ZOOM_resultset resultset;
1398 if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1399 return zoom_complete;
1401 resultset = c->tasks->u.search.resultset;
1405 resultset->r_sort_spec = 0;
1406 return zoom_complete;
1408 if (resultset->r_sort_spec)
1410 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
1411 Z_SortRequest *req = apdu->u.sortRequest;
1413 req->num_inputResultSetNames = 1;
1414 req->inputResultSetNames = (Z_InternationalString **)
1415 odr_malloc (c->odr_out, sizeof(*req->inputResultSetNames));
1416 req->inputResultSetNames[0] =
1417 odr_strdup (c->odr_out, resultset->setname);
1418 req->sortedResultSetName = odr_strdup (c->odr_out, resultset->setname);
1419 req->sortSequence = resultset->r_sort_spec;
1420 resultset->r_sort_spec = 0;
1421 return send_APDU (c, apdu);
1423 return zoom_complete;
1426 static zoom_ret send_present (ZOOM_connection c)
1429 Z_PresentRequest *req = 0;
1431 const char *syntax = 0;
1432 const char *elementSetName = 0;
1433 const char *schema = 0;
1434 ZOOM_resultset resultset;
1437 return zoom_complete;
1439 switch (c->tasks->which)
1441 case ZOOM_TASK_SEARCH:
1442 resultset = c->tasks->u.search.resultset;
1444 case ZOOM_TASK_RETRIEVE:
1445 resultset = c->tasks->u.retrieve.resultset;
1446 resultset->start = c->tasks->u.retrieve.start;
1447 resultset->count = c->tasks->u.retrieve.count;
1449 if (resultset->start >= resultset->size)
1450 return zoom_complete;
1451 if (resultset->start + resultset->count > resultset->size)
1452 resultset->count = resultset->size - resultset->start;
1455 return zoom_complete;
1458 syntax = ZOOM_resultset_option_get (resultset, "preferredRecordSyntax");
1459 elementSetName = ZOOM_resultset_option_get (resultset, "elementSetName");
1460 schema = ZOOM_resultset_option_get (resultset, "schema");
1462 if (c->error) /* don't continue on error */
1463 return zoom_complete;
1464 if (resultset->start < 0)
1465 return zoom_complete;
1466 for (i = 0; i<resultset->count; i++)
1469 record_cache_lookup (resultset, i + resultset->start);
1473 if (i == resultset->count)
1474 return zoom_complete;
1476 apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
1477 req = apdu->u.presentRequest;
1479 resultset->start += i;
1480 resultset->count -= i;
1481 *req->resultSetStartPoint = resultset->start + 1;
1482 *req->numberOfRecordsRequested = resultset->count;
1483 assert (*req->numberOfRecordsRequested > 0);
1485 if (syntax && *syntax)
1486 req->preferredRecordSyntax =
1487 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
1489 if (schema && *schema)
1491 Z_RecordComposition *compo = (Z_RecordComposition *)
1492 odr_malloc (c->odr_out, sizeof(*compo));
1494 req->recordComposition = compo;
1495 compo->which = Z_RecordComp_complex;
1496 compo->u.complex = (Z_CompSpec *)
1497 odr_malloc(c->odr_out, sizeof(*compo->u.complex));
1498 compo->u.complex->selectAlternativeSyntax = (bool_t *)
1499 odr_malloc(c->odr_out, sizeof(bool_t));
1500 *compo->u.complex->selectAlternativeSyntax = 0;
1502 compo->u.complex->generic = (Z_Specification *)
1503 odr_malloc(c->odr_out, sizeof(*compo->u.complex->generic));
1505 compo->u.complex->generic->schema = (Odr_oid *)
1506 yaz_str_to_z3950oid (c->odr_out, CLASS_SCHEMA, schema);
1508 if (!compo->u.complex->generic->schema)
1510 /* OID wasn't a schema! Try record syntax instead. */
1512 compo->u.complex->generic->schema = (Odr_oid *)
1513 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, schema);
1515 if (elementSetName && *elementSetName)
1517 compo->u.complex->generic->elementSpec = (Z_ElementSpec *)
1518 odr_malloc(c->odr_out, sizeof(Z_ElementSpec));
1519 compo->u.complex->generic->elementSpec->which =
1520 Z_ElementSpec_elementSetName;
1521 compo->u.complex->generic->elementSpec->u.elementSetName =
1522 odr_strdup (c->odr_out, elementSetName);
1525 compo->u.complex->generic->elementSpec = 0;
1526 compo->u.complex->num_dbSpecific = 0;
1527 compo->u.complex->dbSpecific = 0;
1528 compo->u.complex->num_recordSyntax = 0;
1529 compo->u.complex->recordSyntax = 0;
1531 else if (elementSetName && *elementSetName)
1533 Z_ElementSetNames *esn = (Z_ElementSetNames *)
1534 odr_malloc (c->odr_out, sizeof(*esn));
1535 Z_RecordComposition *compo = (Z_RecordComposition *)
1536 odr_malloc (c->odr_out, sizeof(*compo));
1538 esn->which = Z_ElementSetNames_generic;
1539 esn->u.generic = odr_strdup (c->odr_out, elementSetName);
1540 compo->which = Z_RecordComp_simple;
1541 compo->u.simple = esn;
1542 req->recordComposition = compo;
1544 req->resultSetId = odr_strdup(c->odr_out, resultset->setname);
1545 return send_APDU (c, apdu);
1548 ZOOM_API(ZOOM_scanset)
1549 ZOOM_connection_scan (ZOOM_connection c, const char *start)
1551 ZOOM_scanset scan = (ZOOM_scanset) xmalloc (sizeof(*scan));
1553 scan->connection = c;
1554 scan->odr = odr_createmem (ODR_DECODE);
1555 scan->options = ZOOM_options_create_with_parent (c->options);
1557 scan->scan_response = 0;
1559 if ((scan->termListAndStartPoint =
1560 p_query_scan(scan->odr, PROTO_Z3950, &scan->attributeSet,
1563 ZOOM_task task = ZOOM_connection_add_task (c, ZOOM_TASK_SCAN);
1564 task->u.scan.scan = scan;
1569 while (ZOOM_event (1, &c))
1577 ZOOM_scanset_destroy (ZOOM_scanset scan)
1582 if (scan->refcount == 0)
1584 odr_destroy (scan->odr);
1586 ZOOM_options_destroy (scan->options);
1591 static zoom_ret send_package (ZOOM_connection c)
1595 return zoom_complete;
1596 assert (c->tasks->which == ZOOM_TASK_PACKAGE);
1598 event = ZOOM_Event_create (ZOOM_EVENT_SEND_APDU);
1599 ZOOM_connection_put_event (c, event);
1601 return do_write_ex (c, c->tasks->u.package->buf_out,
1602 c->tasks->u.package->len_out);
1605 static zoom_ret send_scan (ZOOM_connection c)
1608 Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_scanRequest);
1609 Z_ScanRequest *req = apdu->u.scanRequest;
1611 return zoom_complete;
1612 assert (c->tasks->which == ZOOM_TASK_SCAN);
1613 scan = c->tasks->u.scan.scan;
1615 req->termListAndStartPoint = scan->termListAndStartPoint;
1616 req->attributeSet = scan->attributeSet;
1618 *req->numberOfTermsRequested =
1619 ZOOM_options_get_int(scan->options, "number", 10);
1621 req->preferredPositionInResponse =
1622 odr_intdup (c->odr_out,
1623 ZOOM_options_get_int(scan->options, "position", 1));
1626 odr_intdup (c->odr_out,
1627 ZOOM_options_get_int(scan->options, "stepSize", 0));
1629 req->databaseNames = set_DatabaseNames (c, scan->options,
1630 &req->num_databaseNames);
1632 return send_APDU (c, apdu);
1636 ZOOM_scanset_size (ZOOM_scanset scan)
1638 if (!scan || !scan->scan_response || !scan->scan_response->entries)
1640 return scan->scan_response->entries->num_entries;
1643 ZOOM_API(const char *)
1644 ZOOM_scanset_term (ZOOM_scanset scan, size_t pos,
1647 const char *term = 0;
1648 size_t noent = ZOOM_scanset_size (scan);
1649 Z_ScanResponse *res = scan->scan_response;
1655 if (res->entries->entries[pos]->which == Z_Entry_termInfo)
1657 Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
1659 if (t->term->which == Z_Term_general)
1661 term = (const char *) t->term->u.general->buf;
1662 *len = t->term->u.general->len;
1664 *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
1669 ZOOM_API(const char *)
1670 ZOOM_scanset_option_get (ZOOM_scanset scan, const char *key)
1672 return ZOOM_options_get (scan->options, key);
1676 ZOOM_scanset_option_set (ZOOM_scanset scan, const char *key,
1679 ZOOM_options_set (scan->options, key, val);
1682 static Z_APDU *create_es_package (ZOOM_package p, int type)
1685 Z_APDU *apdu = zget_APDU(p->odr_out, Z_APDU_extendedServicesRequest);
1686 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
1688 *req->function = Z_ExtendedServicesRequest_create;
1690 str = ZOOM_options_get(p->options, "package-name");
1692 req->packageName = nmem_strdup (p->odr_out->mem, str);
1694 str = ZOOM_options_get(p->options, "user-id");
1696 req->userId = nmem_strdup (p->odr_out->mem, str);
1698 req->packageType = yaz_oidval_to_z3950oid(p->odr_out, CLASS_EXTSERV,
1701 str = ZOOM_options_get(p->options, "function");
1704 if (!strcmp (str, "create"))
1706 if (!strcmp (str, "delete"))
1708 if (!strcmp (str, "modify"))
1714 static const char *ill_array_lookup (void *clientData, const char *idx)
1716 ZOOM_package p = (ZOOM_package) clientData;
1717 return ZOOM_options_get (p->options, idx+4);
1720 static Z_External *encode_ill_request (ZOOM_package p)
1722 ODR out = p->odr_out;
1725 struct ill_get_ctl ctl;
1727 ctl.odr = p->odr_out;
1729 ctl.f = ill_array_lookup;
1731 req = ill_get_ILLRequest(&ctl, "ill", 0);
1733 if (!ill_Request (out, &req, 0, 0))
1735 int ill_request_size;
1736 char *ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
1737 if (ill_request_buf)
1738 odr_setbuf (out, ill_request_buf, ill_request_size, 1);
1744 int illRequest_size = 0;
1745 char *illRequest_buf = odr_getbuf (out, &illRequest_size, 0);
1747 oid.proto = PROTO_GENERAL;
1748 oid.oclass = CLASS_GENERAL;
1749 oid.value = VAL_ISO_ILL_1;
1751 r = (Z_External *) odr_malloc (out, sizeof(*r));
1752 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid));
1753 r->indirect_reference = 0;
1755 r->which = Z_External_single;
1757 r->u.single_ASN1_type = (Odr_oct *)
1758 odr_malloc (out, sizeof(*r->u.single_ASN1_type));
1759 r->u.single_ASN1_type->buf = (unsigned char*)
1760 odr_malloc (out, illRequest_size);
1761 r->u.single_ASN1_type->len = illRequest_size;
1762 r->u.single_ASN1_type->size = illRequest_size;
1763 memcpy (r->u.single_ASN1_type->buf, illRequest_buf, illRequest_size);
1768 static Z_ItemOrder *encode_item_order(ZOOM_package p)
1770 Z_ItemOrder *req = (Z_ItemOrder *) odr_malloc (p->odr_out, sizeof(*req));
1773 req->which=Z_IOItemOrder_esRequest;
1774 req->u.esRequest = (Z_IORequest *)
1775 odr_malloc(p->odr_out,sizeof(Z_IORequest));
1777 /* to keep part ... */
1778 req->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
1779 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartToKeep));
1780 req->u.esRequest->toKeep->supplDescription = 0;
1781 req->u.esRequest->toKeep->contact = (Z_IOContact *)
1782 odr_malloc (p->odr_out, sizeof(*req->u.esRequest->toKeep->contact));
1784 str = ZOOM_options_get(p->options, "contact-name");
1785 req->u.esRequest->toKeep->contact->name = str ?
1786 nmem_strdup (p->odr_out->mem, str) : 0;
1788 str = ZOOM_options_get(p->options, "contact-phone");
1789 req->u.esRequest->toKeep->contact->phone = str ?
1790 nmem_strdup (p->odr_out->mem, str) : 0;
1792 str = ZOOM_options_get(p->options, "contact-email");
1793 req->u.esRequest->toKeep->contact->email = str ?
1794 nmem_strdup (p->odr_out->mem, str) : 0;
1796 req->u.esRequest->toKeep->addlBilling = 0;
1798 /* not to keep part ... */
1799 req->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
1800 odr_malloc(p->odr_out,sizeof(Z_IOOriginPartNotToKeep));
1802 req->u.esRequest->notToKeep->resultSetItem = (Z_IOResultSetItem *)
1803 odr_malloc(p->odr_out, sizeof(Z_IOResultSetItem));
1805 str = ZOOM_options_get(p->options, "itemorder-setname");
1808 req->u.esRequest->notToKeep->resultSetItem->resultSetId =
1809 nmem_strdup (p->odr_out->mem, str);
1810 req->u.esRequest->notToKeep->resultSetItem->item =
1811 (int *) odr_malloc(p->odr_out, sizeof(int));
1813 str = ZOOM_options_get(p->options, "itemorder-item");
1814 *req->u.esRequest->notToKeep->resultSetItem->item =
1815 (str ? atoi(str) : 1);
1817 req->u.esRequest->notToKeep->itemRequest = encode_ill_request(p);
1823 ZOOM_package_send (ZOOM_package p, const char *type)
1830 odr_reset (p->odr_out);
1833 if (!strcmp(type, "itemorder"))
1836 apdu = create_es_package (p, VAL_ITEMORDER);
1839 r = (Z_External *) odr_malloc (p->odr_out, sizeof(*r));
1841 r->direct_reference =
1842 yaz_oidval_to_z3950oid(p->odr_out, CLASS_EXTSERV,
1845 r->which = Z_External_itemOrder;
1846 r->indirect_reference = 0;
1847 r->u.itemOrder = encode_item_order (p);
1849 apdu->u.extendedServicesRequest->taskSpecificParameters = r;
1854 if (encode_APDU(p->connection, apdu, p->odr_out) == 0)
1858 ZOOM_task task = ZOOM_connection_add_task (c, ZOOM_TASK_PACKAGE);
1859 task->u.package = p;
1860 buf = odr_getbuf(p->odr_out, &p->len_out, 0);
1861 p->buf_out = (char *) xmalloc (p->len_out);
1862 memcpy (p->buf_out, buf, p->len_out);
1867 while (ZOOM_event (1, &c))
1874 ZOOM_API(ZOOM_package)
1875 ZOOM_connection_package (ZOOM_connection c, ZOOM_options options)
1877 ZOOM_package p = (ZOOM_package) xmalloc (sizeof(*p));
1880 p->odr_out = odr_createmem (ODR_ENCODE);
1881 p->options = ZOOM_options_create_with_parent2 (options, c->options);
1889 ZOOM_package_destroy(ZOOM_package p)
1894 if (p->refcount == 0)
1896 odr_destroy (p->odr_out);
1899 ZOOM_options_destroy (p->options);
1904 ZOOM_API(const char *)
1905 ZOOM_package_option_get (ZOOM_package p, const char *key)
1907 return ZOOM_options_get (p->options, key);
1911 ZOOM_package_option_set (ZOOM_package p, const char *key,
1914 ZOOM_options_set (p->options, key, val);
1917 static int ZOOM_connection_exec_task (ZOOM_connection c)
1919 ZOOM_task task = c->tasks;
1920 zoom_ret ret = zoom_complete;
1924 yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task task=<null>");
1927 yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task type=%d run=%d",
1928 task->which, task->running);
1929 if (c->error != ZOOM_ERROR_NONE ||
1930 (!c->cs && task->which != ZOOM_TASK_CONNECT))
1932 yaz_log (LOG_DEBUG, "remove tasks because of error = %d", c->error);
1933 ZOOM_connection_remove_tasks (c);
1938 yaz_log (LOG_DEBUG, "task already running");
1942 switch (task->which)
1944 case ZOOM_TASK_SEARCH:
1945 ret = ZOOM_connection_send_search (c);
1947 case ZOOM_TASK_RETRIEVE:
1948 ret = send_present (c);
1950 case ZOOM_TASK_CONNECT:
1951 ret = do_connect(c);
1953 case ZOOM_TASK_SCAN:
1956 case ZOOM_TASK_PACKAGE:
1957 ret = send_package(c);
1960 if (ret == zoom_complete)
1962 yaz_log (LOG_DEBUG, "task removed (complete)");
1963 ZOOM_connection_remove_task (c);
1966 yaz_log (LOG_DEBUG, "task pending");
1970 static zoom_ret send_sort_present (ZOOM_connection c)
1972 zoom_ret r = send_sort (c);
1973 if (r == zoom_complete)
1974 r = send_present (c);
1978 static int es_response (ZOOM_connection c,
1979 Z_ExtendedServicesResponse *res)
1981 if (!c->tasks || c->tasks->which != ZOOM_TASK_PACKAGE)
1983 if (res->diagnostics && res->num_diagnostics > 0)
1984 response_diag(c, res->diagnostics[0]);
1985 if (res->taskPackage &&
1986 res->taskPackage->which == Z_External_extendedService)
1988 Z_TaskPackage *taskPackage = res->taskPackage->u.extendedService;
1989 Odr_oct *id = taskPackage->targetReference;
1992 ZOOM_options_setl (c->tasks->u.package->options,
1993 "targetReference", (char*) id->buf, id->len);
1999 static void handle_apdu (ZOOM_connection c, Z_APDU *apdu)
2001 Z_InitResponse *initrs;
2004 yaz_log (LOG_DEBUG, "handle_apdu type=%d", apdu->which);
2007 case Z_APDU_initResponse:
2008 initrs = apdu->u.initResponse;
2009 if (!*initrs->result)
2011 c->error = ZOOM_ERROR_INIT;
2016 yaz_oi_get_string_oidval (&apdu->u.initResponse->otherInfo,
2018 xfree (c->cookie_in);
2021 c->cookie_in = xstrdup(cookie);
2022 if (ODR_MASK_GET(initrs->options, Z_Options_namedResultSets) &&
2023 ODR_MASK_GET(initrs->protocolVersion, Z_ProtocolVersion_3))
2024 c->support_named_resultsets = 1;
2027 assert (c->tasks->which == ZOOM_TASK_CONNECT);
2028 ZOOM_connection_remove_task (c);
2030 ZOOM_connection_exec_task (c);
2032 if (ODR_MASK_GET(initrs->options, Z_Options_negotiationModel))
2034 NMEM tmpmem = nmem_create();
2035 Z_CharSetandLanguageNegotiation *p =
2036 yaz_get_charneg_record(initrs->otherInfo);
2040 char *charset=NULL, *lang=NULL;
2043 yaz_get_response_charneg(tmpmem, p, &charset, &lang, &sel);
2044 yaz_log(LOG_DEBUG, "Target accepted: charset %s, "
2045 "language %s, select %d",
2046 charset ? charset : "none", lang ? lang : "none", sel);
2048 ZOOM_connection_option_set (c, "negotiation-charset",
2051 ZOOM_connection_option_set (c, "negotiation-lang",
2053 nmem_destroy(tmpmem);
2057 case Z_APDU_searchResponse:
2058 handle_search_response (c, apdu->u.searchResponse);
2059 if (send_sort_present (c) == zoom_complete)
2060 ZOOM_connection_remove_task (c);
2062 case Z_APDU_presentResponse:
2063 handle_present_response (c, apdu->u.presentResponse);
2064 if (send_present (c) == zoom_complete)
2065 ZOOM_connection_remove_task (c);
2067 case Z_APDU_sortResponse:
2068 sort_response (c, apdu->u.sortResponse);
2069 if (send_present (c) == zoom_complete)
2070 ZOOM_connection_remove_task (c);
2072 case Z_APDU_scanResponse:
2073 scan_response (c, apdu->u.scanResponse);
2074 ZOOM_connection_remove_task (c);
2076 case Z_APDU_extendedServicesResponse:
2077 es_response (c, apdu->u.extendedServicesResponse);
2078 ZOOM_connection_remove_task (c);
2081 if (c->reconnect_ok)
2084 c->tasks->running = 0;
2085 ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT);
2089 c->error = ZOOM_ERROR_CONNECTION_LOST;
2094 c->error = ZOOM_ERROR_DECODE;
2099 static int do_read (ZOOM_connection c)
2105 event = ZOOM_Event_create (ZOOM_EVENT_RECV_DATA);
2106 ZOOM_connection_put_event (c, event);
2108 yaz_log (LOG_DEBUG, "do_read len=%d", c->len_in);
2110 r = cs_get (c->cs, &c->buf_in, &c->len_in);
2115 if (c->reconnect_ok)
2118 c->reconnect_ok = 0;
2119 yaz_log (LOG_DEBUG, "reconnect read");
2120 c->tasks->running = 0;
2121 ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT);
2125 c->error= ZOOM_ERROR_CONNECTION_LOST;
2132 odr_reset (c->odr_in);
2133 odr_setbuf (c->odr_in, c->buf_in, r, 0);
2134 event = ZOOM_Event_create (ZOOM_EVENT_RECV_APDU);
2135 ZOOM_connection_put_event (c, event);
2136 if (!z_APDU (c->odr_in, &apdu, 0, 0))
2138 c->error = ZOOM_ERROR_DECODE;
2142 handle_apdu (c, apdu);
2143 c->reconnect_ok = 0;
2148 static zoom_ret do_write_ex (ZOOM_connection c, char *buf_out, int len_out)
2153 event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA);
2154 ZOOM_connection_put_event (c, event);
2156 yaz_log (LOG_DEBUG, "do_write_ex len=%d", len_out);
2157 if ((r=cs_put (c->cs, buf_out, len_out)) < 0)
2159 if (c->reconnect_ok)
2162 c->reconnect_ok = 0;
2163 yaz_log (LOG_DEBUG, "reconnect write");
2164 c->tasks->running = 0;
2165 ZOOM_connection_insert_task (c, ZOOM_TASK_CONNECT);
2166 return zoom_complete;
2168 if (c->state == STATE_CONNECTING)
2169 c->error = ZOOM_ERROR_CONNECT;
2171 c->error = ZOOM_ERROR_CONNECTION_LOST;
2173 return zoom_complete;
2177 c->mask = ZOOM_SELECT_EXCEPT;
2178 if (c->cs->io_pending & CS_WANT_WRITE)
2179 c->mask += ZOOM_SELECT_WRITE;
2180 if (c->cs->io_pending & CS_WANT_READ)
2181 c->mask += ZOOM_SELECT_READ;
2182 yaz_log (LOG_DEBUG, "do_write_ex 1 mask=%d", c->mask);
2186 c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT;
2187 yaz_log (LOG_DEBUG, "do_write_ex 2 mask=%d", c->mask);
2189 return zoom_pending;
2192 static zoom_ret do_write(ZOOM_connection c)
2194 return do_write_ex (c, c->buf_out, c->len_out);
2198 ZOOM_API(const char *)
2199 ZOOM_connection_option_get (ZOOM_connection c, const char *key)
2201 return ZOOM_options_get (c->options, key);
2205 ZOOM_connection_option_set (ZOOM_connection c, const char *key,
2208 ZOOM_options_set (c->options, key, val);
2211 ZOOM_API(const char *)
2212 ZOOM_resultset_option_get (ZOOM_resultset r, const char *key)
2214 return ZOOM_options_get (r->options, key);
2218 ZOOM_resultset_option_set (ZOOM_resultset r, const char *key,
2221 ZOOM_options_set (r->options, key, val);
2226 ZOOM_connection_errcode (ZOOM_connection c)
2228 return ZOOM_connection_error (c, 0, 0);
2231 ZOOM_API(const char *)
2232 ZOOM_connection_errmsg (ZOOM_connection c)
2235 ZOOM_connection_error (c, &msg, 0);
2239 ZOOM_API(const char *)
2240 ZOOM_connection_addinfo (ZOOM_connection c)
2242 const char *addinfo;
2243 ZOOM_connection_error (c, 0, &addinfo);
2248 ZOOM_connection_error (ZOOM_connection c, const char **cp,
2249 const char **addinfo)
2251 int error = c->error;
2256 case ZOOM_ERROR_NONE:
2257 *cp = "No error"; break;
2258 case ZOOM_ERROR_CONNECT:
2259 *cp = "Connect failed"; break;
2260 case ZOOM_ERROR_MEMORY:
2261 *cp = "Out of memory"; break;
2262 case ZOOM_ERROR_ENCODE:
2263 *cp = "Encoding failed"; break;
2264 case ZOOM_ERROR_DECODE:
2265 *cp = "Decoding failed"; break;
2266 case ZOOM_ERROR_CONNECTION_LOST:
2267 *cp = "Connection lost"; break;
2268 case ZOOM_ERROR_INIT:
2269 *cp = "Init rejected"; break;
2270 case ZOOM_ERROR_INTERNAL:
2271 *cp = "Internal failure"; break;
2272 case ZOOM_ERROR_TIMEOUT:
2273 *cp = "Timeout"; break;
2275 *cp = diagbib1_str (error);
2281 *addinfo = c->addinfo;
2288 static int ZOOM_connection_do_io(ZOOM_connection c, int mask)
2290 ZOOM_Event event = 0;
2291 int r = cs_look(c->cs);
2292 yaz_log (LOG_DEBUG, "ZOOM_connection_do_io c=%p mask=%d cs_look=%d",
2297 event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
2298 c->error = ZOOM_ERROR_CONNECT;
2300 ZOOM_connection_put_event (c, event);
2302 else if (r == CS_CONNECT)
2305 event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
2307 ret = cs_rcvconnect (c->cs);
2308 yaz_log (LOG_DEBUG, "cs_rcvconnect returned %d", ret);
2311 c->mask = ZOOM_SELECT_EXCEPT;
2312 if (c->cs->io_pending & CS_WANT_WRITE)
2313 c->mask += ZOOM_SELECT_WRITE;
2314 if (c->cs->io_pending & CS_WANT_READ)
2315 c->mask += ZOOM_SELECT_READ;
2316 ZOOM_connection_put_event (c, event);
2320 ZOOM_connection_put_event (c, event);
2321 ZOOM_connection_send_init (c);
2322 c->state = STATE_ESTABLISHED;
2326 c->error = ZOOM_ERROR_CONNECT;
2328 ZOOM_connection_put_event (c, event);
2333 if (mask & ZOOM_SELECT_READ)
2335 if (c->cs && (mask & ZOOM_SELECT_WRITE))
2342 ZOOM_connection_last_event(ZOOM_connection cs)
2345 return ZOOM_EVENT_NONE;
2346 return cs->last_event;
2350 ZOOM_event (int no, ZOOM_connection *cs)
2354 struct pollfd pollfds[1024];
2355 ZOOM_connection poll_cs[1024];
2358 fd_set input, output, except;
2363 for (i = 0; i<no; i++)
2365 ZOOM_connection c = cs[i];
2367 if (c && (event = ZOOM_connection_get_event(c)))
2369 ZOOM_Event_destroy (event);
2373 for (i = 0; i<no; i++)
2375 ZOOM_connection c = cs[i];
2377 if (c && ZOOM_connection_exec_task (c))
2379 if ((event = ZOOM_connection_get_event(c)))
2381 ZOOM_Event_destroy (event);
2394 for (i = 0; i<no; i++)
2396 ZOOM_connection c = cs[i];
2402 fd = z3950_connection_socket(c);
2403 mask = z3950_connection_mask(c);
2410 this_timeout = ZOOM_options_get_int (c->options, "timeout", -1);
2411 if (this_timeout != -1 && this_timeout < timeout)
2412 timeout = this_timeout;
2416 short poll_events = 0;
2418 if (mask & ZOOM_SELECT_READ)
2419 poll_events += POLLIN;
2420 if (mask & ZOOM_SELECT_WRITE)
2421 poll_events += POLLOUT;
2422 if (mask & ZOOM_SELECT_EXCEPT)
2423 poll_events += POLLERR;
2424 pollfds[nfds].fd = fd;
2425 pollfds[nfds].events = poll_events;
2426 pollfds[nfds].revents = 0;
2431 if (mask & ZOOM_SELECT_READ)
2433 FD_SET (fd, &input);
2436 if (mask & ZOOM_SELECT_WRITE)
2438 FD_SET (fd, &output);
2441 if (mask & ZOOM_SELECT_EXCEPT)
2443 FD_SET (fd, &except);
2448 if (timeout >= 5000)
2455 r = poll (pollfds, nfds, timeout * 1000);
2456 for (i = 0; i<nfds; i++)
2458 ZOOM_connection c = poll_cs[i];
2462 if (pollfds[i].revents & POLLIN)
2463 mask += ZOOM_SELECT_READ;
2464 if (pollfds[i].revents & POLLOUT)
2465 mask += ZOOM_SELECT_WRITE;
2466 if (pollfds[i].revents & POLLERR)
2467 mask += ZOOM_SELECT_EXCEPT;
2469 ZOOM_connection_do_io(c, mask);
2471 else if (r == 0 && c->mask)
2473 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
2474 /* timeout and this connection was waiting */
2475 c->error = ZOOM_ERROR_TIMEOUT;
2477 ZOOM_connection_put_event(c, event);
2481 tv.tv_sec = timeout;
2483 yaz_log (LOG_DEBUG, "select start");
2484 r = select (max_fd+1, &input, &output, &except, &tv);
2485 yaz_log (LOG_DEBUG, "select stop, returned r=%d", r);
2486 for (i = 0; i<no; i++)
2488 ZOOM_connection c = cs[i];
2493 fd = z3950_connection_socket(c);
2497 /* no timeout and real socket */
2498 if (FD_ISSET(fd, &input))
2499 mask += ZOOM_SELECT_READ;
2500 if (FD_ISSET(fd, &output))
2501 mask += ZOOM_SELECT_WRITE;
2502 if (FD_ISSET(fd, &except))
2503 mask += ZOOM_SELECT_EXCEPT;
2505 ZOOM_connection_do_io(c, mask);
2507 if (r == 0 && c->mask)
2509 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
2510 /* timeout and this connection was waiting */
2511 c->error = ZOOM_ERROR_TIMEOUT;
2513 yaz_log (LOG_DEBUG, "timeout");
2514 ZOOM_connection_put_event(c, event);
2518 for (i = 0; i<no; i++)
2520 ZOOM_connection c = cs[i];
2522 if (c && (event = ZOOM_connection_get_event(c)))
2524 ZOOM_Event_destroy (event);