SSL/COMSTACK updates. cs_rcvconnect completes SSL handshake.
[yaz-moved-to-github.git] / zoom / zoom-c.c
1 /*
2  * $Id: zoom-c.c,v 1.21 2002-01-21 21:50:32 adam Exp $
3  *
4  * ZOOM layer for C, connections, result sets, queries.
5  */
6 #include <assert.h>
7 #include <yaz/xmalloc.h>
8 #include <yaz/otherinfo.h>
9 #include <yaz/log.h>
10 #include <yaz/pquery.h>
11 #include <yaz/diagbib1.h>
12
13 #include "zoom-p.h"
14
15 #if HAVE_SYS_POLL_H
16 #include <sys/poll.h>
17 #endif
18
19 static ZOOM_Event ZOOM_Event_create (int kind)
20 {
21     ZOOM_Event event = xmalloc (sizeof(*event));
22     event->kind = kind;
23     event->next = 0;
24     event->prev = 0;
25     return event;
26 }
27
28 static void ZOOM_Event_destroy (ZOOM_Event event)
29 {
30     xfree (event);
31 }
32
33 static void ZOOM_connection_put_event (ZOOM_connection c, ZOOM_Event event)
34 {
35     if (c->m_queue_back)
36     {
37         c->m_queue_back->prev = event;
38         assert (c->m_queue_front);
39     }
40     else
41     {
42         assert (!c->m_queue_front);
43         c->m_queue_front = event;
44     }
45     event->next = c->m_queue_back;
46     event->prev = 0;
47     c->m_queue_back = event;
48 }
49
50 static ZOOM_Event ZOOM_connection_get_event(ZOOM_connection c)
51 {
52     ZOOM_Event event = c->m_queue_front;
53     if (!event)
54         return 0;
55     assert (c->m_queue_back);
56     c->m_queue_front = event->prev;
57     if (c->m_queue_front)
58     {
59         assert (c->m_queue_back);
60         c->m_queue_front->next = 0;
61     }
62     else
63         c->m_queue_back = 0;
64     c->last_event = event->kind;
65     return event;
66 }
67
68 static void clear_error (ZOOM_connection c)
69 {
70
71     switch (c->error)
72     {
73     case ZOOM_ERROR_CONNECT:
74     case ZOOM_ERROR_MEMORY:
75     case ZOOM_ERROR_DECODE:
76     case ZOOM_ERROR_CONNECTION_LOST:
77     case ZOOM_ERROR_INIT:
78     case ZOOM_ERROR_INTERNAL:
79         break;
80     default:
81         c->error = ZOOM_ERROR_NONE;
82         xfree (c->addinfo);
83         c->addinfo = 0;
84     }
85 }
86
87 ZOOM_task ZOOM_connection_add_task (ZOOM_connection c, int which)
88 {
89     ZOOM_task *taskp = &c->tasks;
90     while (*taskp)
91         taskp = &(*taskp)->next;
92     *taskp = xmalloc (sizeof(**taskp));
93     (*taskp)->running = 0;
94     (*taskp)->which = which;
95     (*taskp)->next = 0;
96     clear_error (c);
97     return *taskp;
98 }
99
100 void ZOOM_connection_remove_task (ZOOM_connection c)
101 {
102     ZOOM_task task = c->tasks;
103
104     if (task)
105     {
106         c->tasks = task->next;
107         switch (task->which)
108         {
109         case ZOOM_TASK_SEARCH:
110             ZOOM_resultset_destroy (task->u.search.resultset);
111             break;
112         case ZOOM_TASK_RETRIEVE:
113             ZOOM_resultset_destroy (task->u.retrieve.resultset);
114             break;
115         case ZOOM_TASK_CONNECT:
116             break;
117         case ZOOM_TASK_SCAN:
118             ZOOM_scanset_destroy (task->u.scan.scan);
119             break;
120         default:
121             assert (0);
122         }
123         xfree (task);
124     }
125 }
126
127 void ZOOM_connection_remove_tasks (ZOOM_connection c)
128 {
129     while (c->tasks)
130         ZOOM_connection_remove_task(c);
131 }
132
133 static ZOOM_record record_cache_lookup (ZOOM_resultset r,
134                                          int pos,
135                                          const char *elementSetName);
136
137 ZOOM_connection ZOOM_connection_create (ZOOM_options options)
138 {
139     ZOOM_connection c = xmalloc (sizeof(*c));
140
141     c->cs = 0;
142     c->mask = 0;
143     c->state = STATE_IDLE;
144     c->error = ZOOM_ERROR_NONE;
145     c->addinfo = 0;
146     c->buf_in = 0;
147     c->len_in = 0;
148     c->buf_out = 0;
149     c->len_out = 0;
150     c->resultsets = 0;
151
152     c->options = ZOOM_options_create_with_parent(options);
153
154     c->host_port = 0;
155     c->proxy = 0;
156
157     c->cookie_out = 0;
158     c->cookie_in = 0;
159     c->tasks = 0;
160
161     c->odr_in = odr_createmem (ODR_DECODE);
162     c->odr_out = odr_createmem (ODR_ENCODE);
163
164     c->async = 0;
165     c->support_named_resultsets = 0;
166     c->last_event = ZOOM_EVENT_NONE;
167
168     c->m_queue_front = 0;
169     c->m_queue_back = 0;
170     return c;
171 }
172
173 /* set database names. Take local databases (if set); otherwise
174    take databases given in ZURL (if set); otherwise use Default */
175 static char **set_DatabaseNames (ZOOM_connection con, ZOOM_options options,
176                                  int *num)
177 {
178     char **databaseNames;
179     const char *c;
180     int no = 2;
181     const char *cp = ZOOM_options_get (options, "databaseName");
182     
183     if (!cp || !*cp)
184     {
185         cp = strchr (con->host_port, '/');
186         if (cp)
187             cp++;
188         }
189     if (cp)
190     {
191         c = cp;
192         while ((c = strchr(c, '+')))
193         {
194             c++;
195             no++;
196         }
197     }
198     else
199         cp = "Default";
200     databaseNames = odr_malloc (con->odr_out, no * sizeof(*databaseNames));
201     no = 0;
202     while (*cp)
203     {
204         c = strchr (cp, '+');
205         if (!c)
206             c = cp + strlen(cp);
207         else if (c == cp)
208         {
209             cp++;
210             continue;
211         }
212         /* cp ptr to first char of db name, c is char
213            following db name */
214         databaseNames[no] = odr_malloc (con->odr_out, 1+c-cp);
215         memcpy (databaseNames[no], cp, c-cp);
216         databaseNames[no++][c-cp] = '\0';
217         cp = c;
218         if (*cp)
219             cp++;
220     }
221     databaseNames[no] = NULL;
222     *num = no;
223     return databaseNames;
224 }
225
226 ZOOM_connection ZOOM_connection_new (const char *host, int portnum)
227 {
228     ZOOM_connection c = ZOOM_connection_create (0);
229
230     ZOOM_connection_connect (c, host, portnum);
231     return c;
232 }
233
234 void ZOOM_connection_connect(ZOOM_connection c,
235                               const char *host, int portnum)
236 {
237     const char *val;
238     ZOOM_task task;
239
240     val = ZOOM_options_get (c->options, "proxy");
241     if (val && *val)
242         c->proxy = xstrdup (val);
243     else
244         c->proxy = 0;
245
246     if (portnum)
247     {
248         char hostn[128];
249         sprintf (hostn, "%.80s:%d", host, portnum);
250         c->host_port = xstrdup(hostn);
251     }
252     else
253         c->host_port = xstrdup(host);
254
255     ZOOM_options_set(c->options, "host", c->host_port);
256
257     c->async = ZOOM_options_get_bool (c->options, "async", 0);
258  
259     c->error = ZOOM_ERROR_NONE;
260
261     task = ZOOM_connection_add_task (c, ZOOM_TASK_CONNECT);
262
263     if (!c->async)
264     {
265         while (ZOOM_event (1, &c))
266             ;
267     }
268 }
269
270 ZOOM_query ZOOM_query_create(void)
271 {
272     ZOOM_query s = xmalloc (sizeof(*s));
273
274     s->refcount = 1;
275     s->query = 0;
276     s->sort_spec = 0;
277     s->odr = odr_createmem (ODR_ENCODE);
278
279     return s;
280 }
281
282 void ZOOM_query_destroy(ZOOM_query s)
283 {
284     if (!s)
285         return;
286
287     (s->refcount)--;
288     yaz_log (LOG_DEBUG, "ZOOM_query_destroy count=%d", s->refcount);
289     if (s->refcount == 0)
290     {
291         odr_destroy (s->odr);
292         xfree (s);
293     }
294 }
295
296 int ZOOM_query_prefix(ZOOM_query s, const char *str)
297 {
298     s->query = odr_malloc (s->odr, sizeof(*s->query));
299     s->query->which = Z_Query_type_1;
300     s->query->u.type_1 =  p_query_rpn(s->odr, PROTO_Z3950, str);
301     if (!s->query->u.type_1)
302         return -1;
303     return 0;
304 }
305
306 int ZOOM_query_sortby(ZOOM_query s, const char *criteria)
307 {
308     s->sort_spec = yaz_sort_spec (s->odr, criteria);
309     if (!s->sort_spec)
310         return -1;
311     return 0;
312 }
313
314 static int do_write(ZOOM_connection c);
315
316 void ZOOM_connection_destroy(ZOOM_connection c)
317 {
318     ZOOM_resultset r;
319     if (!c)
320         return;
321     if (c->cs)
322         cs_close (c->cs);
323     for (r = c->resultsets; r; r = r->next)
324         r->connection = 0;
325
326     xfree (c->buf_in);
327     xfree (c->addinfo);
328     odr_destroy (c->odr_in);
329     odr_destroy (c->odr_out);
330     ZOOM_options_destroy (c->options);
331     ZOOM_connection_remove_tasks (c);
332     xfree (c->host_port);
333     xfree (c);
334 }
335
336 void ZOOM_resultset_addref (ZOOM_resultset r)
337 {
338     if (r)
339         (r->refcount)++;
340 }
341 ZOOM_resultset ZOOM_resultset_create ()
342 {
343     ZOOM_resultset r = xmalloc (sizeof(*r));
344
345     r->refcount = 1;
346     r->size = 0;
347     r->odr = odr_createmem (ODR_ENCODE);
348     r->start = 0;
349     r->piggyback = 1;
350     r->setname = 0;
351     r->count = 0;
352     r->record_cache = 0;
353     r->r_sort_spec = 0;
354     r->r_query = 0;
355     r->search = 0;
356     r->connection = 0;
357     r->next = 0;
358     return r;
359 }
360
361 ZOOM_resultset ZOOM_connection_search_pqf(ZOOM_connection c, const char *q)
362 {
363     ZOOM_resultset r;
364     ZOOM_query s = ZOOM_query_create();
365
366     ZOOM_query_prefix (s, q);
367
368     r = ZOOM_connection_search (c, s);
369     ZOOM_query_destroy (s);
370     return r;
371 }
372
373 ZOOM_resultset ZOOM_connection_search(ZOOM_connection c, ZOOM_query q)
374 {
375     ZOOM_resultset r = ZOOM_resultset_create ();
376     ZOOM_task task;
377     const char *cp;
378
379     r->r_sort_spec = q->sort_spec;
380     r->r_query = q->query;
381     r->search = q;
382
383     r->options = ZOOM_options_create_with_parent(c->options);
384
385     r->start = ZOOM_options_get_int(r->options, "start", 0);
386     r->count = ZOOM_options_get_int(r->options, "count", 0);
387     r->piggyback = ZOOM_options_get_bool (r->options, "piggyback", 1);
388     cp = ZOOM_options_get (r->options, "setname");
389     if (cp)
390         r->setname = xstrdup (cp);
391     
392     r->connection = c;
393
394     r->next = c->resultsets;
395     c->resultsets = r;
396
397     task = ZOOM_connection_add_task (c, ZOOM_TASK_SEARCH);
398     task->u.search.resultset = r;
399     ZOOM_resultset_addref (r);  
400
401     (q->refcount)++;
402
403     if (!c->async)
404     {
405         while (ZOOM_event (1, &c))
406             ;
407     }
408     return r;
409 }
410
411 void ZOOM_resultset_destroy(ZOOM_resultset r)
412 {
413     if (!r)
414         return;
415     (r->refcount)--;
416     yaz_log (LOG_DEBUG, "destroy r = %p count=%d", r, r->refcount);
417     if (r->refcount == 0)
418     {
419         if (r->connection)
420         {
421             /* remove ourselves from the resultsets in connection */
422             ZOOM_resultset *rp = &r->connection->resultsets;
423             while (1)
424             {
425                 assert (*rp);   /* we must be in this list!! */
426                 if (*rp == r)
427                 {   /* OK, we're here - take us out of it */
428                     *rp = (*rp)->next;
429                     break;
430                 }
431                 rp = &(*rp)->next;
432             }
433         }
434         ZOOM_query_destroy (r->search);
435         ZOOM_options_destroy (r->options);
436         odr_destroy (r->odr);
437         xfree (r->setname);
438         xfree (r);
439     }
440 }
441
442 size_t ZOOM_resultset_size (ZOOM_resultset r)
443 {
444     return r->size;
445 }
446
447 static void do_close (ZOOM_connection c)
448 {
449     if (c->cs)
450         cs_close(c->cs);
451     c->cs = 0;
452     c->mask = 0;
453     c->state = STATE_IDLE;
454 }
455
456 static void ZOOM_resultset_retrieve (ZOOM_resultset r,
457                                       int force_sync, int start, int count)
458 {
459     ZOOM_task task;
460     ZOOM_connection c;
461
462     if (!r)
463         return;
464     c = r->connection;
465     if (!c)
466         return;
467     task = ZOOM_connection_add_task (c, ZOOM_TASK_RETRIEVE);
468     task->u.retrieve.resultset = r;
469     task->u.retrieve.start = start;
470     task->u.retrieve.count = count;
471
472     ZOOM_resultset_addref (r);
473
474     if (!r->connection->async || force_sync)
475         while (r->connection && ZOOM_event (1, &r->connection))
476             ;
477 }
478
479 void ZOOM_resultset_records (ZOOM_resultset r, ZOOM_record *recs,
480                               size_t start, size_t count)
481 {
482     int force_present = 0;
483
484     if (!r)
485         return ;
486     if (count && recs)
487         force_present = 1;
488     ZOOM_resultset_retrieve (r, force_present, start, count);
489     if (force_present)
490     {
491         size_t i;
492         for (i = 0; i< count; i++)
493             recs[i] = ZOOM_resultset_record_immediate (r, i+start);
494     }
495 }
496
497 static int do_connect (ZOOM_connection c)
498 {
499     void *add;
500     const char *effective_host;
501
502     if (c->proxy)
503         effective_host = c->proxy;
504     else
505         effective_host = c->host_port;
506
507     yaz_log (LOG_DEBUG, "do_connect host=%s", effective_host);
508
509     assert (!c->cs);
510     c->cs = cs_create_host (effective_host, 0, &add);
511
512     if (c->cs)
513     {
514         int ret = cs_connect (c->cs, add);
515         yaz_log (LOG_DEBUG, "cs_connect returned %d", ret);
516         if (ret >= 0)
517         {
518             c->state = STATE_CONNECTING; 
519             c->mask = ZOOM_SELECT_EXCEPT;
520             if (c->cs->io_pending & CS_WANT_WRITE)
521                 c->mask += ZOOM_SELECT_WRITE;
522             if (c->cs->io_pending & CS_WANT_READ)
523                 c->mask += ZOOM_SELECT_READ;
524             return 1;
525         }
526     }
527     c->state = STATE_IDLE;
528     c->error = ZOOM_ERROR_CONNECT;
529     return 0;
530 }
531
532 int z3950_connection_socket(ZOOM_connection c)
533 {
534     if (c->cs)
535         return cs_fileno(c->cs);
536     return -1;
537 }
538
539 int z3950_connection_mask(ZOOM_connection c)
540 {
541     if (c->cs)
542         return c->mask;
543     return 0;
544 }
545
546 static int encode_APDU(ZOOM_connection c, Z_APDU *a, ODR out)
547 {
548     char str[120];
549
550     assert (a);
551     sprintf (str, "send_APDU t=%p type=%d", c, a->which);
552     if (c->cookie_out)
553     {
554         Z_OtherInformation **oi;
555         yaz_oi_APDU(a, &oi);
556         yaz_oi_set_string_oidval(oi, out, VAL_COOKIE, 1, c->cookie_out);
557     }
558     if (!z_APDU(out, &a, 0, 0))
559     {
560         FILE *outf = fopen("/tmp/apdu.txt", "w");
561         if (outf)
562         {
563             ODR odr_pr = odr_createmem(ODR_PRINT);
564             fprintf (outf, "a=%p\n", a);
565             odr_setprint(odr_pr, outf);
566             z_APDU(odr_pr, &a, 0, 0);
567             odr_destroy(odr_pr);
568             fclose (outf);
569         }
570         c->error = ZOOM_ERROR_ENCODE;
571         do_close (c);
572         return -1;
573     }
574     
575     return 0;
576 }
577
578 static int send_APDU (ZOOM_connection c, Z_APDU *a)
579 {
580     ZOOM_Event event;
581     assert (a);
582     if (encode_APDU(c, a, c->odr_out))
583         return -1;
584     c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
585     event = ZOOM_Event_create (ZOOM_EVENT_SEND_APDU);
586     ZOOM_connection_put_event (c, event);
587     odr_reset(c->odr_out);
588     do_write (c);
589     return 0;   
590 }
591
592 static int ZOOM_connection_send_init (ZOOM_connection c)
593 {
594     const char *impname;
595     Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_initRequest);
596     Z_InitRequest *ireq = apdu->u.initRequest;
597     Z_IdAuthentication *auth = odr_malloc(c->odr_out, sizeof(*auth));
598     const char *auth_groupId = ZOOM_options_get (c->options, "group");
599     const char *auth_userId = ZOOM_options_get (c->options, "user");
600     const char *auth_password = ZOOM_options_get (c->options, "pass");
601     
602     ODR_MASK_SET(ireq->options, Z_Options_search);
603     ODR_MASK_SET(ireq->options, Z_Options_present);
604     ODR_MASK_SET(ireq->options, Z_Options_scan);
605     ODR_MASK_SET(ireq->options, Z_Options_sort);
606     ODR_MASK_SET(ireq->options, Z_Options_extendedServices);
607     ODR_MASK_SET(ireq->options, Z_Options_namedResultSets);
608     
609     ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_1);
610     ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_2);
611     ODR_MASK_SET(ireq->protocolVersion, Z_ProtocolVersion_3);
612     
613     impname = ZOOM_options_get (c->options, "implementationName");
614     ireq->implementationName =
615         odr_malloc (c->odr_out, 15 + (impname ? strlen(impname) : 0));
616     strcpy (ireq->implementationName, "");
617     if (impname)
618     {
619         strcat (ireq->implementationName, impname);
620         strcat (ireq->implementationName, "/");
621     }                                          
622     strcat (ireq->implementationName, "ZOOM-C/YAZ");
623     
624     *ireq->maximumRecordSize =
625         ZOOM_options_get_int (c->options, "maximumRecordSize", 1024*1024);
626     *ireq->preferredMessageSize =
627         ZOOM_options_get_int (c->options, "preferredMessageSize", 1024*1024);
628     
629     if (auth_groupId || auth_password)
630     {
631         Z_IdPass *pass = odr_malloc(c->odr_out, sizeof(*pass));
632         int i = 0;
633         pass->groupId = 0;
634         if (auth_groupId && *auth_groupId)
635         {
636             pass->groupId = odr_malloc(c->odr_out, strlen(auth_groupId)+1);
637             strcpy(pass->groupId, auth_groupId);
638             i++;
639         }
640         pass->userId = 0;
641         if (auth_userId && *auth_userId)
642         {
643             pass->userId = odr_malloc(c->odr_out, strlen(auth_userId)+1);
644             strcpy(pass->userId, auth_userId);
645             i++;
646         }
647         pass->password = 0;
648         if (auth_password && *auth_password)
649         {
650             pass->password = odr_malloc(c->odr_out, strlen(auth_password)+1);
651             strcpy(pass->password, auth_password);
652             i++;
653         }
654         if (i)
655         {
656             auth->which = Z_IdAuthentication_idPass;
657             auth->u.idPass = pass;
658             ireq->idAuthentication = auth;
659         }
660     }
661     else if (auth_userId)
662     {
663         auth->which = Z_IdAuthentication_open;
664         auth->u.open = odr_malloc(c->odr_out, strlen(auth_userId)+1);
665         strcpy(auth->u.open, auth_userId);
666         ireq->idAuthentication = auth;
667     }
668     if (c->proxy)
669         yaz_oi_set_string_oidval(&ireq->otherInfo, c->odr_out,
670                                  VAL_PROXY, 1, c->host_port);
671     assert (apdu);
672     send_APDU (c, apdu);
673     
674     return 0;
675 }
676
677 static int ZOOM_connection_send_search (ZOOM_connection c)
678 {
679     ZOOM_resultset r;
680     int lslb, ssub, mspn;
681     const char *syntax;
682     Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_searchRequest);
683     Z_SearchRequest *search_req = apdu->u.searchRequest;
684     const char *elementSetName;
685     const char *smallSetElementSetName;
686     const char *mediumSetElementSetName;
687     const char *schema;
688
689     assert (c->tasks);
690     assert (c->tasks->which == ZOOM_TASK_SEARCH);
691
692     r = c->tasks->u.search.resultset;
693
694     elementSetName =
695         ZOOM_options_get (r->options, "elementSetName");
696     smallSetElementSetName  =
697         ZOOM_options_get (r->options, "smallSetElementSetName");
698     mediumSetElementSetName =
699         ZOOM_options_get (r->options, "mediumSetElementSetName");
700     schema =
701         ZOOM_options_get (r->options, "schema");
702
703     if (!smallSetElementSetName)
704         smallSetElementSetName = elementSetName;
705
706     if (!mediumSetElementSetName)
707         mediumSetElementSetName = elementSetName;
708
709     assert (r);
710     assert (r->r_query);
711
712     /* prepare query for the search request */
713     search_req->query = r->r_query;
714
715     search_req->databaseNames =
716         set_DatabaseNames (c, r->options, &search_req->num_databaseNames);
717
718     /* get syntax (no need to provide unless piggyback is in effect) */
719     syntax = ZOOM_options_get (r->options, "preferredRecordSyntax");
720
721     lslb = ZOOM_options_get_int (r->options, "largeSetLowerBound", -1);
722     ssub = ZOOM_options_get_int (r->options, "smallSetUpperBound", -1);
723     mspn = ZOOM_options_get_int (r->options, "mediumSetPresentNumber", -1);
724     if (lslb != -1 && ssub != -1 && mspn != -1)
725     {
726         /* So're a Z39.50 expert? Let's hope you don't do sort */
727         *search_req->largeSetLowerBound = lslb;
728         *search_req->smallSetUpperBound = ssub;
729         *search_req->mediumSetPresentNumber = mspn;
730     }
731     else if (r->start == 0 && r->count > 0
732              && r->piggyback && !r->r_sort_spec && !schema)
733     {
734         /* Regular piggyback - do it unless we're going to do sort */
735         *search_req->largeSetLowerBound = 2000000000;
736         *search_req->smallSetUpperBound = r->count;
737         *search_req->mediumSetPresentNumber = r->count;
738         smallSetElementSetName = 0;  /* no need to provide this */
739     }
740     else
741     {
742         /* non-piggyback. Need not provide elementsets or syntaxes .. */
743         smallSetElementSetName = 0;
744         mediumSetElementSetName = 0;
745         syntax = 0;
746     }
747     if (smallSetElementSetName && *smallSetElementSetName)
748     {
749         Z_ElementSetNames *esn = odr_malloc (c->odr_out, sizeof(*esn));
750         
751         esn->which = Z_ElementSetNames_generic;
752         esn->u.generic = odr_strdup (c->odr_out, smallSetElementSetName);
753         search_req->smallSetElementSetNames = esn;
754     }
755     if (mediumSetElementSetName && *mediumSetElementSetName)
756     {
757         Z_ElementSetNames *esn = odr_malloc (c->odr_out, sizeof(*esn));
758         
759         esn->which = Z_ElementSetNames_generic;
760         esn->u.generic = odr_strdup (c->odr_out, mediumSetElementSetName);
761         search_req->mediumSetElementSetNames = esn;
762     }
763     if (syntax)
764         search_req->preferredRecordSyntax =
765             yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
766     
767     if (!r->setname)
768     {
769         if (c->support_named_resultsets)
770         {
771             char setname[14];
772             int ord;
773             /* find the lowest unused ordinal so that we re-use
774                result sets on the server. */
775             for (ord = 1; ; ord++)
776             {
777                 ZOOM_resultset rp;
778                 sprintf (setname, "%d", ord);
779                 for (rp = c->resultsets; rp; rp = rp->next)
780                     if (rp->setname && !strcmp (rp->setname, setname))
781                         break;
782                 if (!rp)
783                     break;
784             }
785             r->setname = xstrdup (setname);
786             yaz_log (LOG_DEBUG, "allocating %s", r->setname);
787         }
788         else
789             r->setname = xstrdup ("default");
790         ZOOM_options_set (r->options, "setname", r->setname);
791     }
792     search_req->resultSetName = odr_strdup(c->odr_out, r->setname);
793     /* send search request */
794     send_APDU (c, apdu);
795     r->r_query = 0;
796     return 1;
797 }
798
799 static void response_diag (ZOOM_connection c, Z_DiagRec *p)
800 {
801     Z_DefaultDiagFormat *r;
802     char *addinfo = 0;
803     
804     xfree (c->addinfo);
805     c->addinfo = 0;
806     if (p->which != Z_DiagRec_defaultFormat)
807     {
808         c->error = ZOOM_ERROR_DECODE;
809         return;
810     }
811     r = p->u.defaultFormat;
812     switch (r->which)
813     {
814     case Z_DefaultDiagFormat_v2Addinfo:
815         addinfo = r->u.v2Addinfo;
816         break;
817     case Z_DefaultDiagFormat_v3Addinfo:
818         addinfo = r->u.v3Addinfo;
819         break;
820     }
821     if (addinfo)
822         c->addinfo = xstrdup (addinfo);
823     c->error = *r->condition;
824 }
825
826 ZOOM_record ZOOM_record_clone (ZOOM_record srec)
827 {
828     char *buf;
829     int size;
830     ODR odr_enc;
831     ZOOM_record nrec;
832
833     odr_enc = odr_createmem(ODR_ENCODE);
834     if (!z_NamePlusRecord (odr_enc, &srec->npr, 0, 0))
835         return 0;
836     buf = odr_getbuf (odr_enc, &size, 0);
837     
838     nrec = xmalloc (sizeof(*nrec));
839     nrec->odr = odr_createmem(ODR_DECODE);
840     nrec->wrbuf_marc = 0;
841     odr_setbuf (nrec->odr, buf, size, 0);
842     z_NamePlusRecord (nrec->odr, &nrec->npr, 0, 0);
843     
844     odr_destroy (odr_enc);
845     return nrec;
846 }
847
848 ZOOM_record ZOOM_resultset_record_immediate (ZOOM_resultset s,size_t pos)
849 {
850     return record_cache_lookup (s, pos, 0);
851 }
852
853 ZOOM_record ZOOM_resultset_record (ZOOM_resultset r, size_t pos)
854 {
855     ZOOM_resultset_retrieve (r, 1, pos, 1);
856     return ZOOM_resultset_record_immediate (r, pos);
857 }
858
859 void ZOOM_record_destroy (ZOOM_record rec)
860 {
861     if (!rec)
862         return;
863     if (rec->wrbuf_marc)
864         wrbuf_free (rec->wrbuf_marc, 1);
865     odr_destroy (rec->odr);
866     xfree (rec);
867 }
868
869 void *ZOOM_record_get (ZOOM_record rec, const char *type, size_t *len)
870 {
871     Z_NamePlusRecord *npr;
872     if (!rec)
873         return 0;
874     npr = rec->npr;
875     if (!npr)
876         return 0;
877     if (!strcmp (type, "database"))
878     {
879         return npr->databaseName;
880     }
881     else if (!strcmp (type, "syntax"))
882     {
883         if (npr->which == Z_NamePlusRecord_databaseRecord)
884         {
885             Z_External *r = (Z_External *) npr->u.databaseRecord;
886             oident *ent = oid_getentbyoid(r->direct_reference);
887             if (ent)
888                 return ent->desc;
889         }
890         return "none";
891     }
892     else if (!strcmp (type, "render"))
893     {
894         if (npr->which == Z_NamePlusRecord_databaseRecord)
895         {
896             Z_External *r = (Z_External *) npr->u.databaseRecord;
897             oident *ent = oid_getentbyoid(r->direct_reference);
898             
899             if (r->which == Z_External_sutrs)
900             {
901                 *len = r->u.sutrs->len;
902                 return r->u.sutrs->buf;
903             }
904             else if (r->which == Z_External_octet)
905             {
906                 switch (ent->value)
907                 {
908                 case VAL_SOIF:
909                 case VAL_HTML:
910                 case VAL_SUTRS:
911                     break;
912                 case VAL_TEXT_XML:
913                 case VAL_APPLICATION_XML:
914                     break;
915                 default:
916                     if (!rec->wrbuf_marc)
917                         rec->wrbuf_marc = wrbuf_alloc();
918                     if (marc_display_wrbuf (r->u.octet_aligned->buf,
919                                         rec->wrbuf_marc, 0,
920                                             r->u.octet_aligned->len) > 0)
921                     {
922                         *len = wrbuf_len(rec->wrbuf_marc);
923                         return wrbuf_buf(rec->wrbuf_marc);
924                     }
925                 }
926                 *len = r->u.octet_aligned->len;
927                 return r->u.octet_aligned->buf;
928             }
929             else if (r->which == Z_External_grs1)
930             {
931                 *len = 5;
932                 return "GRS-1";
933             }
934         }
935         return 0;
936     }
937     else if (!strcmp (type, "raw"))
938     {
939         if (npr->which == Z_NamePlusRecord_databaseRecord)
940         {
941             Z_External *r = (Z_External *) npr->u.databaseRecord;
942             
943             if (r->which == Z_External_sutrs)
944             {
945                 *len = r->u.sutrs->len;
946                 return r->u.sutrs->buf;
947             }
948             else if (r->which == Z_External_octet)
949             {
950                 *len = r->u.octet_aligned->len;
951                 return r->u.octet_aligned->buf;
952             }
953             else /* grs-1, explain, ... */
954             {
955                 *len = -1;
956                 return (Z_External *) npr->u.databaseRecord;
957             }
958         }
959         return 0;
960     }
961     return 0;
962 }
963
964 static void record_cache_add (ZOOM_resultset r,
965                               Z_NamePlusRecord *npr,
966                               int pos,
967                               const char *elementSetName)
968 {
969     ZOOM_record_cache rc;
970
971     for (rc = r->record_cache; rc; rc = rc->next)
972     {
973         if (pos == rc->pos)
974         {
975             if ((!elementSetName && !rc->elementSetName)
976                 || (elementSetName && rc->elementSetName &&
977                     !strcmp (elementSetName, rc->elementSetName)))
978             {
979                 /* not destroying rc->npr (it's handled by nmem )*/
980                 rc->rec.npr = npr;
981                 /* keeping wrbuf_marc too */
982                 return;
983             }
984         }
985
986     }
987     rc = odr_malloc (r->odr, sizeof(*rc));
988     rc->rec.npr = npr; 
989     rc->rec.odr = 0;
990     rc->rec.wrbuf_marc = 0;
991     if (elementSetName)
992         rc->elementSetName = odr_strdup (r->odr, elementSetName);
993     else
994         rc->elementSetName = 0;
995     rc->pos = pos;
996     rc->next = r->record_cache;
997     r->record_cache = rc;
998 }
999
1000 static ZOOM_record record_cache_lookup (ZOOM_resultset r,
1001                                          int pos,
1002                                          const char *elementSetName)
1003 {
1004     ZOOM_record_cache rc;
1005
1006     for (rc = r->record_cache; rc; rc = rc->next)
1007     {
1008         if (pos == rc->pos)
1009         {
1010             if ((!elementSetName && !rc->elementSetName)
1011                 || (elementSetName && rc->elementSetName &&
1012                     !strcmp (elementSetName, rc->elementSetName)))
1013                 return &rc->rec;
1014         }
1015     }
1016     return 0;
1017 }
1018                                              
1019 static void handle_records (ZOOM_connection c, Z_Records *sr,
1020                             int present_phase)
1021 {
1022     ZOOM_resultset resultset;
1023
1024     if (!c->tasks)
1025         return ;
1026     switch (c->tasks->which)
1027     {
1028     case ZOOM_TASK_SEARCH:
1029         resultset = c->tasks->u.search.resultset;
1030         break;
1031     case ZOOM_TASK_RETRIEVE:
1032         resultset = c->tasks->u.retrieve.resultset;        
1033         break;
1034     default:
1035         return;
1036     }
1037     if (sr && sr->which == Z_Records_NSD)
1038     {
1039         Z_DiagRec dr, *dr_p = &dr;
1040         dr.which = Z_DiagRec_defaultFormat;
1041         dr.u.defaultFormat = sr->u.nonSurrogateDiagnostic;
1042         
1043         response_diag (c, dr_p);
1044     }
1045     else if (sr && sr->which == Z_Records_multipleNSD)
1046     {
1047         if (sr->u.multipleNonSurDiagnostics->num_diagRecs >= 1)
1048             response_diag(c, sr->u.multipleNonSurDiagnostics->diagRecs[0]);
1049         else
1050             c->error = ZOOM_ERROR_DECODE;
1051     }
1052     else 
1053     {
1054         if (resultset->count + resultset->start > resultset->size)
1055             resultset->count = resultset->size - resultset->start;
1056         if (resultset->count < 0)
1057             resultset->count = 0;
1058         if (sr && sr->which == Z_Records_DBOSD)
1059         {
1060             int i;
1061             NMEM nmem = odr_extract_mem (c->odr_in);
1062             Z_NamePlusRecordList *p =
1063                 sr->u.databaseOrSurDiagnostics;
1064             for (i = 0; i<p->num_records; i++)
1065             {
1066                 record_cache_add (resultset, p->records[i],
1067                                   i+ resultset->start, 0);
1068             }
1069             /* transfer our response to search_nmem .. we need it later */
1070             nmem_transfer (resultset->odr->mem, nmem);
1071             nmem_destroy (nmem);
1072             if (present_phase && p->num_records == 0)
1073             {
1074                 /* present response and we didn't get any records! */
1075                 c->error = ZOOM_ERROR_DECODE;
1076             }
1077         }
1078         else if (present_phase)
1079         {
1080             /* present response and we didn't get any records! */
1081             c->error = ZOOM_ERROR_DECODE;
1082         }
1083     }
1084 }
1085
1086 static void handle_present_response (ZOOM_connection c, Z_PresentResponse *pr)
1087 {
1088     handle_records (c, pr->records, 1);
1089 }
1090
1091 static void handle_search_response (ZOOM_connection c, Z_SearchResponse *sr)
1092 {
1093     ZOOM_resultset resultset;
1094
1095     yaz_log (LOG_DEBUG, "got search response");
1096
1097     if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1098         return ;
1099
1100     resultset = c->tasks->u.search.resultset;
1101
1102     resultset->size = *sr->resultCount;
1103     handle_records (c, sr->records, 0);
1104 }
1105
1106 static void sort_response (ZOOM_connection c, Z_SortResponse *res)
1107 {
1108     if (res->diagnostics && res->num_diagnostics > 0)
1109         response_diag (c, res->diagnostics[0]);
1110 }
1111
1112 static int scan_response (ZOOM_connection c, Z_ScanResponse *res)
1113 {
1114     NMEM nmem = odr_extract_mem (c->odr_in);
1115     ZOOM_scanset scan;
1116
1117     if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
1118         return 0;
1119     scan = c->tasks->u.scan.scan;
1120
1121     if (res->entries && res->entries->nonsurrogateDiagnostics)
1122         response_diag(c, res->entries->nonsurrogateDiagnostics[0]);
1123     scan->scan_response = res;
1124     nmem_transfer (scan->odr->mem, nmem);
1125     if (res->stepSize)
1126         ZOOM_options_set_int (scan->options, "stepSize", *res->stepSize);
1127     if (res->positionOfTerm)
1128         ZOOM_options_set_int (scan->options, "position", *res->positionOfTerm);
1129     if (res->scanStatus)
1130         ZOOM_options_set_int (scan->options, "scanStatus", *res->scanStatus);
1131     if (res->numberOfEntriesReturned)
1132         ZOOM_options_set_int (scan->options, "number",
1133                               *res->numberOfEntriesReturned);
1134     nmem_destroy (nmem);
1135     return 1;
1136 }
1137
1138 static int send_sort (ZOOM_connection c)
1139 {
1140     ZOOM_resultset  resultset;
1141
1142     if (!c->tasks || c->tasks->which != ZOOM_TASK_SEARCH)
1143         return 0;
1144
1145     resultset = c->tasks->u.search.resultset;
1146
1147     if (c->error)
1148     {
1149         resultset->r_sort_spec = 0;
1150         return 0;
1151     }
1152     if (resultset->r_sort_spec)
1153     {
1154         Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_sortRequest);
1155         Z_SortRequest *req = apdu->u.sortRequest;
1156         
1157         req->num_inputResultSetNames = 1;
1158         req->inputResultSetNames = (Z_InternationalString **)
1159             odr_malloc (c->odr_out, sizeof(*req->inputResultSetNames));
1160         req->inputResultSetNames[0] =
1161             odr_strdup (c->odr_out, resultset->setname);
1162         req->sortedResultSetName = odr_strdup (c->odr_out, resultset->setname);
1163         req->sortSequence = resultset->r_sort_spec;
1164         resultset->r_sort_spec = 0;
1165         send_APDU (c, apdu);
1166         return 1;
1167     }
1168     return 0;
1169 }
1170
1171 static int send_present (ZOOM_connection c)
1172 {
1173     Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_presentRequest);
1174     Z_PresentRequest *req = apdu->u.presentRequest;
1175     int i = 0;
1176     const char *syntax = 
1177         ZOOM_options_get (c->options, "preferredRecordSyntax");
1178     const char *element =
1179         ZOOM_options_get (c->options, "elementSetName");
1180     const char *schema =
1181         ZOOM_options_get (c->options, "schema");
1182     ZOOM_resultset  resultset;
1183
1184     if (!c->tasks)
1185         return 0;
1186
1187     switch (c->tasks->which)
1188     {
1189     case ZOOM_TASK_SEARCH:
1190         resultset = c->tasks->u.search.resultset;
1191         break;
1192     case ZOOM_TASK_RETRIEVE:
1193         resultset = c->tasks->u.retrieve.resultset;
1194         resultset->start = c->tasks->u.retrieve.start;
1195         resultset->count = c->tasks->u.retrieve.count;
1196
1197         if (resultset->start >= resultset->size)
1198             return 0;
1199         if (resultset->start + resultset->count > resultset->size)
1200             resultset->count = resultset->size - resultset->start;
1201         break;
1202     default:
1203         return 0;
1204     }
1205
1206     if (c->error)                  /* don't continue on error */
1207         return 0;
1208     if (resultset->start < 0)
1209         return 0;
1210     for (i = 0; i<resultset->count; i++)
1211     {
1212         ZOOM_record rec =
1213             record_cache_lookup (resultset, i + resultset->start, 0);
1214         if (!rec)
1215             break;
1216     }
1217     if (i == resultset->count)
1218         return 0;
1219
1220     resultset->start += i;
1221     resultset->count -= i;
1222     *req->resultSetStartPoint = resultset->start + 1;
1223     *req->numberOfRecordsRequested = resultset->count;
1224     assert (*req->numberOfRecordsRequested > 0);
1225
1226     if (syntax && *syntax)
1227         req->preferredRecordSyntax =
1228             yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, syntax);
1229
1230     if (schema && *schema)
1231     {
1232         Z_RecordComposition *compo = odr_malloc (c->odr_out, sizeof(*compo));
1233
1234         req->recordComposition = compo;
1235         compo->which = Z_RecordComp_complex;
1236         compo->u.complex = (Z_CompSpec *)
1237             odr_malloc(c->odr_out, sizeof(*compo->u.complex));
1238         compo->u.complex->selectAlternativeSyntax = (bool_t *) 
1239             odr_malloc(c->odr_out, sizeof(bool_t));
1240         *compo->u.complex->selectAlternativeSyntax = 0;
1241
1242         compo->u.complex->generic = (Z_Specification *)
1243             odr_malloc(c->odr_out, sizeof(*compo->u.complex->generic));
1244
1245         compo->u.complex->generic->schema = (Odr_oid *)
1246             yaz_str_to_z3950oid (c->odr_out, CLASS_SCHEMA, schema);
1247
1248         if (!compo->u.complex->generic->schema)
1249         {
1250             /* OID wasn't a schema! Try record syntax instead. */
1251
1252             compo->u.complex->generic->schema = (Odr_oid *)
1253                 yaz_str_to_z3950oid (c->odr_out, CLASS_RECSYN, schema);
1254         }
1255         if (element && *element)
1256         {
1257             compo->u.complex->generic->elementSpec = (Z_ElementSpec *)
1258                 odr_malloc(c->odr_out, sizeof(Z_ElementSpec));
1259             compo->u.complex->generic->elementSpec->which =
1260                 Z_ElementSpec_elementSetName;
1261             compo->u.complex->generic->elementSpec->u.elementSetName =
1262                 odr_strdup (c->odr_out, element);
1263         }
1264         else
1265             compo->u.complex->generic->elementSpec = 0;
1266         compo->u.complex->num_dbSpecific = 0;
1267         compo->u.complex->dbSpecific = 0;
1268         compo->u.complex->num_recordSyntax = 0;
1269         compo->u.complex->recordSyntax = 0;
1270     }
1271     else if (element && *element)
1272     {
1273         Z_ElementSetNames *esn = odr_malloc (c->odr_out, sizeof(*esn));
1274         Z_RecordComposition *compo = odr_malloc (c->odr_out, sizeof(*compo));
1275         
1276         esn->which = Z_ElementSetNames_generic;
1277         esn->u.generic = odr_strdup (c->odr_out, element);
1278         compo->which = Z_RecordComp_simple;
1279         compo->u.simple = esn;
1280         req->recordComposition = compo;
1281     }
1282     req->resultSetId = odr_strdup(c->odr_out, resultset->setname);
1283     send_APDU (c, apdu);
1284     return 1;
1285 }
1286
1287 ZOOM_scanset ZOOM_connection_scan (ZOOM_connection c, const char *start)
1288 {
1289     ZOOM_scanset scan = xmalloc (sizeof(*scan));
1290
1291     scan->connection = c;
1292     scan->odr = odr_createmem (ODR_DECODE);
1293     scan->options = ZOOM_options_create_with_parent (c->options);
1294     scan->refcount = 1;
1295     scan->scan_response = 0;
1296
1297     if ((scan->termListAndStartPoint =
1298          p_query_scan(scan->odr, PROTO_Z3950, &scan->attributeSet,
1299                       start)))
1300     {
1301         ZOOM_task task = ZOOM_connection_add_task (c, ZOOM_TASK_SCAN);
1302         task->u.scan.scan = scan;
1303         
1304         (scan->refcount)++;
1305         if (!c->async)
1306         {
1307             while (ZOOM_event (1, &c))
1308                 ;
1309         }
1310     }
1311     return scan;
1312 }
1313
1314 void ZOOM_scanset_destroy (ZOOM_scanset scan)
1315 {
1316     if (!scan)
1317         return;
1318     (scan->refcount)--;
1319     if (scan->refcount == 0)
1320     {
1321         odr_destroy (scan->odr);
1322         
1323         ZOOM_options_destroy (scan->options);
1324         xfree (scan);
1325     }
1326 }
1327
1328 int send_scan (ZOOM_connection c)
1329 {
1330     ZOOM_scanset scan;
1331     Z_APDU *apdu = zget_APDU(c->odr_out, Z_APDU_scanRequest);
1332     Z_ScanRequest *req = apdu->u.scanRequest;
1333     if (!c->tasks)
1334         return 0;
1335     assert (c->tasks->which == ZOOM_TASK_SCAN);
1336     scan = c->tasks->u.scan.scan;
1337
1338     req->termListAndStartPoint = scan->termListAndStartPoint;
1339     req->attributeSet = scan->attributeSet;
1340
1341     *req->numberOfTermsRequested =
1342         ZOOM_options_get_int(scan->options, "number", 10);
1343
1344     req->preferredPositionInResponse =
1345         odr_intdup (c->odr_out,
1346                     ZOOM_options_get_int(scan->options, "position", 1));
1347
1348     req->stepSize =
1349         odr_intdup (c->odr_out,
1350                     ZOOM_options_get_int(scan->options, "stepSize", 0));
1351     
1352     req->databaseNames = set_DatabaseNames (c, scan->options, 
1353                                             &req->num_databaseNames);
1354
1355     send_APDU (c, apdu);
1356
1357     return 1;
1358 }
1359
1360 size_t ZOOM_scanset_size (ZOOM_scanset scan)
1361 {
1362     if (!scan || !scan->scan_response || !scan->scan_response->entries)
1363         return 0;
1364     return scan->scan_response->entries->num_entries;
1365 }
1366
1367 const char *ZOOM_scanset_term (ZOOM_scanset scan, size_t pos,
1368                                int *occ, size_t *len)
1369 {
1370     const char *term = 0;
1371     size_t noent = ZOOM_scanset_size (scan);
1372     Z_ScanResponse *res = scan->scan_response;
1373     
1374     *len = 0;
1375     *occ = 0;
1376     if (pos >= noent)
1377         return 0;
1378     if (res->entries->entries[pos]->which == Z_Entry_termInfo)
1379     {
1380         Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
1381         
1382         if (t->term->which == Z_Term_general)
1383         {
1384             term = t->term->u.general->buf;
1385             *len = t->term->u.general->len;
1386         }
1387         *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
1388     }
1389     return term;
1390 }
1391
1392 const char *ZOOM_scanset_option_get (ZOOM_scanset scan, const char *key)
1393 {
1394     return ZOOM_options_get (scan->options, key);
1395 }
1396
1397 void ZOOM_scanset_option_set (ZOOM_scanset scan, const char *key,
1398                               const char *val)
1399 {
1400     ZOOM_options_set (scan->options, key, val);
1401 }
1402
1403 static int ZOOM_connection_exec_task (ZOOM_connection c)
1404 {
1405     ZOOM_task task = c->tasks;
1406
1407     yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task");
1408     if (!task)
1409         return 0;
1410     if (c->error != ZOOM_ERROR_NONE ||
1411         (!c->cs && task->which != ZOOM_TASK_CONNECT))
1412     {
1413         ZOOM_connection_remove_tasks (c);
1414         return 0;
1415     }
1416     yaz_log (LOG_DEBUG, "ZOOM_connection_exec_task type=%d", task->which);
1417     if (task->running)
1418         return 0;
1419     task->running = 1;
1420     switch (task->which)
1421     {
1422     case ZOOM_TASK_SEARCH:
1423         /* see if search hasn't been sent yet. */
1424         if (ZOOM_connection_send_search (c))
1425             return 1;
1426         break;
1427     case ZOOM_TASK_RETRIEVE:
1428         if (send_present (c))
1429             return 1;
1430         break;
1431     case ZOOM_TASK_CONNECT:
1432         if (do_connect(c))
1433             return 1;
1434         break;
1435     case ZOOM_TASK_SCAN:
1436         if (send_scan(c))
1437             return 1;
1438     }
1439     ZOOM_connection_remove_task (c);
1440     return 0;
1441 }
1442
1443 static int send_sort_present (ZOOM_connection c)
1444 {
1445     int r = send_sort (c);
1446     if (!r)
1447         r = send_present (c);
1448     return r;
1449 }
1450
1451 static void handle_apdu (ZOOM_connection c, Z_APDU *apdu)
1452 {
1453     Z_InitResponse *initrs;
1454     
1455     yaz_log (LOG_DEBUG, "hande_apdu type=%d", apdu->which);
1456     c->mask = 0;
1457     switch(apdu->which)
1458     {
1459     case Z_APDU_initResponse:
1460         initrs = apdu->u.initResponse;
1461         if (!*initrs->result)
1462         {
1463             c->error = ZOOM_ERROR_INIT;
1464         }
1465         else
1466         {
1467             char *cookie =
1468                 yaz_oi_get_string_oidval (&apdu->u.initResponse->otherInfo,
1469                                           VAL_COOKIE, 1, 0);
1470             xfree (c->cookie_in);
1471             c->cookie_in = 0;
1472             if (cookie)
1473                 c->cookie_in = xstrdup(cookie);
1474             if (ODR_MASK_GET(initrs->options, Z_Options_namedResultSets) &&
1475                 ODR_MASK_GET(initrs->protocolVersion, Z_ProtocolVersion_3))
1476                 c->support_named_resultsets = 1;
1477             if (c->tasks)
1478             {
1479                 assert (c->tasks->which == ZOOM_TASK_CONNECT);
1480                 ZOOM_connection_remove_task (c);
1481             }
1482             ZOOM_connection_exec_task (c);
1483         }
1484         break;
1485     case Z_APDU_searchResponse:
1486         handle_search_response (c, apdu->u.searchResponse);
1487         if (!send_sort_present (c))
1488             ZOOM_connection_remove_task (c);
1489         break;
1490     case Z_APDU_presentResponse:
1491         handle_present_response (c, apdu->u.presentResponse);
1492         if (!send_present (c))
1493             ZOOM_connection_remove_task (c);
1494         break;
1495     case Z_APDU_sortResponse:
1496         sort_response (c, apdu->u.sortResponse);
1497         if (!send_present (c))
1498             ZOOM_connection_remove_task (c);
1499         break;
1500     case Z_APDU_scanResponse:
1501         scan_response (c, apdu->u.scanResponse);
1502         ZOOM_connection_remove_task (c);
1503     }
1504 }
1505
1506 static int do_read (ZOOM_connection c)
1507 {
1508     int r;
1509     Z_APDU *apdu;
1510     ZOOM_Event event;
1511     
1512     event = ZOOM_Event_create (ZOOM_EVENT_RECV_DATA);
1513     ZOOM_connection_put_event (c, event);
1514     
1515     r = cs_get (c->cs, &c->buf_in, &c->len_in);
1516     if (r == 1)
1517         return 0;
1518     if (r <= 0)
1519     {
1520         c->error= ZOOM_ERROR_CONNECTION_LOST;
1521         do_close (c);
1522     }
1523     else
1524     {
1525         ZOOM_Event event;
1526         odr_reset (c->odr_in);
1527         odr_setbuf (c->odr_in, c->buf_in, r, 0);
1528         event = ZOOM_Event_create (ZOOM_EVENT_RECV_APDU);
1529         ZOOM_connection_put_event (c, event);
1530         if (!z_APDU (c->odr_in, &apdu, 0, 0))
1531         {
1532             c->error = ZOOM_ERROR_DECODE;
1533             do_close (c);
1534         }
1535         else
1536         {
1537             handle_apdu (c, apdu);
1538         }
1539     }
1540     return 1;
1541 }
1542
1543 static int do_write_ex (ZOOM_connection c, char *buf_out, int len_out)
1544 {
1545     int r;
1546     ZOOM_Event event;
1547     
1548     event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA);
1549     ZOOM_connection_put_event (c, event);
1550
1551     if ((r=cs_put (c->cs, buf_out, len_out)) < 0)
1552     {
1553         if (c->state == STATE_CONNECTING)
1554             c->error = ZOOM_ERROR_CONNECT;
1555         else
1556             c->error = ZOOM_ERROR_CONNECTION_LOST;
1557         do_close (c);
1558         return 1;
1559     }
1560     else if (r == 1)
1561     {    
1562         c->mask = ZOOM_SELECT_EXCEPT;
1563         if (c->cs->io_pending & CS_WANT_WRITE)
1564             c->mask += ZOOM_SELECT_WRITE;
1565         if (c->cs->io_pending & CS_WANT_READ)
1566             c->mask += ZOOM_SELECT_READ;
1567         yaz_log (LOG_DEBUG, "do_write_ex 1 mask=%d", c->mask);
1568     }
1569     else
1570     {
1571         c->mask = ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT;
1572         yaz_log (LOG_DEBUG, "do_write_ex 2 mask=%d", c->mask);
1573     }
1574     return 0;
1575 }
1576
1577 static int do_write(ZOOM_connection c)
1578 {
1579     return do_write_ex (c, c->buf_out, c->len_out);
1580 }
1581
1582
1583 const char *ZOOM_connection_option_get (ZOOM_connection c, const char *key)
1584 {
1585     return ZOOM_options_get (c->options, key);
1586 }
1587
1588 void ZOOM_connection_option_set (ZOOM_connection c, const char *key,
1589                                   const char *val)
1590 {
1591     ZOOM_options_set (c->options, key, val);
1592 }
1593
1594 const char *ZOOM_resultset_option_get (ZOOM_resultset r, const char *key)
1595 {
1596     return ZOOM_options_get (r->options, key);
1597 }
1598
1599 void ZOOM_resultset_option_set (ZOOM_resultset r, const char *key,
1600                                   const char *val)
1601 {
1602     ZOOM_options_set (r->options, key, val);
1603 }
1604
1605
1606 int ZOOM_connection_errcode (ZOOM_connection c)
1607 {
1608     return ZOOM_connection_error (c, 0, 0);
1609 }
1610
1611 const char *ZOOM_connection_errmsg (ZOOM_connection c)
1612 {
1613     const char *msg;
1614     ZOOM_connection_error (c, &msg, 0);
1615     return msg;
1616 }
1617
1618 const char *ZOOM_connection_addinfo (ZOOM_connection c)
1619 {
1620     const char *addinfo;
1621     ZOOM_connection_error (c, 0, &addinfo);
1622     return addinfo;
1623 }
1624
1625 int ZOOM_connection_error (ZOOM_connection c, const char **cp,
1626                             const char **addinfo)
1627 {
1628     int error = c->error;
1629     if (cp)
1630     {
1631         switch (error)
1632         {
1633         case ZOOM_ERROR_NONE:
1634             *cp = "No error"; break;
1635         case ZOOM_ERROR_CONNECT:
1636             *cp = "Connect failed"; break;
1637         case ZOOM_ERROR_MEMORY:
1638             *cp = "Out of memory"; break;
1639         case ZOOM_ERROR_ENCODE:
1640             *cp = "Encoding failed"; break;
1641         case ZOOM_ERROR_DECODE:
1642             *cp = "Decoding failed"; break;
1643         case ZOOM_ERROR_CONNECTION_LOST:
1644             *cp = "Connection lost"; break;
1645         case ZOOM_ERROR_INIT:
1646             *cp = "Init rejected"; break;
1647         case ZOOM_ERROR_INTERNAL:
1648             *cp = "Internal failure"; break;
1649         case ZOOM_ERROR_TIMEOUT:
1650             *cp = "Timeout"; break;
1651         default:
1652             *cp = diagbib1_str (error);
1653         }
1654     }
1655     if (addinfo)
1656     {
1657         if (c->addinfo)
1658             *addinfo = c->addinfo;
1659         else
1660             *addinfo = "";
1661     }
1662     return c->error;
1663 }
1664
1665 int ZOOM_connection_do_io(ZOOM_connection c, int mask)
1666 {
1667     ZOOM_Event event = 0;
1668     int r = cs_look(c->cs);
1669     yaz_log (LOG_DEBUG, "ZOOM_connection_do_io c=%p mask=%d cs_look=%d",
1670              c, mask, r);
1671     
1672     if (r == CS_NONE)
1673     {
1674         event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
1675         c->error = ZOOM_ERROR_CONNECT;
1676         do_close (c);
1677         ZOOM_connection_put_event (c, event);
1678     }
1679     else if (r == CS_CONNECT)
1680     {
1681         int ret;
1682         event = ZOOM_Event_create (ZOOM_EVENT_CONNECT);
1683
1684         ret = cs_rcvconnect (c->cs);
1685         yaz_log (LOG_DEBUG, "cs_rcvconnect returned %d", ret);
1686         if (ret == 1)
1687         {
1688             c->mask = ZOOM_SELECT_EXCEPT;
1689             if (c->cs->io_pending & CS_WANT_WRITE)
1690                 c->mask += ZOOM_SELECT_WRITE;
1691             if (c->cs->io_pending & CS_WANT_READ)
1692                 c->mask += ZOOM_SELECT_READ;
1693             ZOOM_connection_put_event (c, event);
1694         }
1695         else if (ret == 0)
1696         {
1697             ZOOM_connection_put_event (c, event);
1698             ZOOM_connection_send_init (c);
1699             c->state = STATE_ESTABLISHED;
1700         }
1701         else
1702         {
1703             c->error = ZOOM_ERROR_CONNECT;
1704             do_close (c);
1705             ZOOM_connection_put_event (c, event);
1706         }
1707     }
1708     else
1709     {
1710         if (mask & ZOOM_SELECT_READ)
1711             do_read (c);
1712         if (c->cs && (mask & ZOOM_SELECT_WRITE))
1713             do_write (c);
1714     }
1715     return 1;
1716 }
1717
1718 int ZOOM_connection_last_event(ZOOM_connection cs)
1719 {
1720     if (!cs)
1721         return ZOOM_EVENT_NONE;
1722     return cs->last_event;
1723 }
1724
1725 int ZOOM_event (int no, ZOOM_connection *cs)
1726 {
1727 #if HAVE_SYS_POLL_H
1728     struct pollfd pollfds[1024];
1729     ZOOM_connection poll_cs[1024];
1730 #else
1731     struct timeval tv;
1732     fd_set input, output, except;
1733 #endif
1734     int i, r, nfds;
1735     int max_fd = 0;
1736
1737     for (i = 0; i<no; i++)
1738     {
1739         ZOOM_connection c = cs[i];
1740         ZOOM_Event event;
1741         if (c && (event = ZOOM_connection_get_event(c)))
1742         {
1743             ZOOM_Event_destroy (event);
1744             return i+1;
1745         }
1746     }
1747     for (i = 0; i<no; i++)
1748     {
1749         ZOOM_connection c = cs[i];
1750         ZOOM_Event event;
1751         if (c && ZOOM_connection_exec_task (c))
1752         {
1753             if ((event = ZOOM_connection_get_event(c)))
1754             {
1755                 ZOOM_Event_destroy (event);
1756                 return i+1;
1757             }
1758         }
1759     }
1760 #if HAVE_SYS_POLL_H
1761
1762 #else
1763     tv.tv_sec = 15;
1764     tv.tv_usec = 0;
1765     
1766     FD_ZERO (&input);
1767     FD_ZERO (&output);
1768     FD_ZERO (&except);
1769 #endif
1770     nfds = 0;
1771     for (i = 0; i<no; i++)
1772     {
1773         ZOOM_connection c = cs[i];
1774         int fd, mask;
1775         
1776         if (!c)
1777             continue;
1778         fd = z3950_connection_socket(c);
1779         mask = z3950_connection_mask(c);
1780
1781         if (fd == -1)
1782             continue;
1783         if (max_fd < fd)
1784             max_fd = fd;
1785
1786 #if HAVE_SYS_POLL_H
1787         if (mask)
1788         {
1789             short poll_events = 0;
1790
1791             if (mask & ZOOM_SELECT_READ)
1792                 poll_events += POLLIN;
1793             if (mask & ZOOM_SELECT_WRITE)
1794                 poll_events += POLLOUT;
1795             if (mask & ZOOM_SELECT_EXCEPT)
1796                 poll_events += POLLERR;
1797             pollfds[nfds].fd = fd;
1798             pollfds[nfds].events = poll_events;
1799             pollfds[nfds].revents = 0;
1800             poll_cs[nfds] = c;
1801             nfds++;
1802         }
1803 #else
1804         if (mask & ZOOM_SELECT_READ)
1805         {
1806             FD_SET (fd, &input);
1807             nfds++;
1808         }
1809         if (mask & ZOOM_SELECT_WRITE)
1810         {
1811             FD_SET (fd, &output);
1812             nfds++;
1813         }
1814         if (mask & ZOOM_SELECT_EXCEPT)
1815         {
1816             FD_SET (fd, &except);
1817             nfds++;
1818         }
1819 #endif
1820     }
1821     if (!nfds)
1822         return 0;
1823 #if HAVE_SYS_POLL_H
1824     r = poll (pollfds, nfds, 15000);
1825     for (i = 0; i<nfds; i++)
1826     {
1827         ZOOM_connection c = poll_cs[i];
1828         if (r && c->mask)
1829         {
1830             int mask = 0;
1831             if (pollfds[i].revents & POLLIN)
1832                 mask += ZOOM_SELECT_READ;
1833             if (pollfds[i].revents & POLLOUT)
1834                 mask += ZOOM_SELECT_WRITE;
1835             if (pollfds[i].revents & POLLERR)
1836                 mask += ZOOM_SELECT_EXCEPT;
1837             if (mask)
1838                 ZOOM_connection_do_io(c, mask);
1839         }
1840         else if (r == 0 && c->mask)
1841         {
1842             ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
1843             /* timeout and this connection was waiting */
1844             c->error = ZOOM_ERROR_TIMEOUT;
1845             do_close (c);
1846             ZOOM_connection_put_event(c, event);
1847         }
1848     }
1849 #else
1850     yaz_log (LOG_DEBUG, "select start");
1851     r = select (max_fd+1, &input, &output, &except, &tv);
1852     yaz_log (LOG_DEBUG, "select stop, returned r=%d", r);
1853     for (i = 0; i<no; i++)
1854     {
1855         ZOOM_connection c = cs[i];
1856         int fd, mask;
1857
1858         if (!c)
1859             continue;
1860         fd = z3950_connection_socket(c);
1861         mask = 0;
1862         if (r && c->mask)
1863         {
1864             /* no timeout and real socket */
1865             if (FD_ISSET(fd, &input))
1866                 mask += ZOOM_SELECT_READ;
1867             if (FD_ISSET(fd, &output))
1868                 mask += ZOOM_SELECT_WRITE;
1869             if (FD_ISSET(fd, &except))
1870                 mask += ZOOM_SELECT_EXCEPT;
1871             if (mask)
1872                 ZOOM_connection_do_io(c, mask);
1873         }
1874         if (r == 0 && c->mask)
1875         {
1876             ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
1877             /* timeout and this connection was waiting */
1878             c->error = ZOOM_ERROR_TIMEOUT;
1879             do_close (c);
1880             yaz_log (LOG_DEBUG, "timeout");
1881             ZOOM_connection_put_event(c, event);
1882         }
1883     }
1884 #endif
1885     for (i = 0; i<no; i++)
1886     {
1887         ZOOM_connection c = cs[i];
1888         ZOOM_Event event;
1889         if (c && (event = ZOOM_connection_get_event(c)))
1890         {
1891             ZOOM_Event_destroy (event);
1892             return i+1;
1893         }
1894     }
1895     return 0;
1896 }