ZOOM: initial experiments with memcached
[yaz-moved-to-github.git] / src / zoom-c.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) Index Data
3  * See the file LICENSE for details.
4  */
5 /**
6  * \file zoom-c.c
7  * \brief Implements ZOOM C interface.
8  */
9 #if HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12
13 #include <assert.h>
14 #include <string.h>
15 #include <errno.h>
16 #include "zoom-p.h"
17
18 #include <yaz/yaz-util.h>
19 #include <yaz/xmalloc.h>
20 #include <yaz/otherinfo.h>
21 #include <yaz/log.h>
22 #include <yaz/diagbib1.h>
23 #include <yaz/charneg.h>
24 #include <yaz/query-charset.h>
25 #include <yaz/snprintf.h>
26 #include <yaz/facet.h>
27
28 #include <yaz/shptr.h>
29
30 #if SHPTR
31 YAZ_SHPTR_TYPE(WRBUF)
32 #endif
33
34 static int log_api0 = 0;
35 static int log_details0 = 0;
36
37 static void resultset_destroy(ZOOM_resultset r);
38 static zoom_ret do_write_ex(ZOOM_connection c, char *buf_out, int len_out);
39
40 static void initlog(void)
41 {
42     static int log_level_initialized = 0;
43
44     if (!log_level_initialized)
45     {
46         log_api0 = yaz_log_module_level("zoom");
47         log_details0 = yaz_log_module_level("zoomdetails");
48         log_level_initialized = 1;
49     }
50 }
51
52 void ZOOM_connection_remove_tasks(ZOOM_connection c);
53
54 void ZOOM_set_dset_error(ZOOM_connection c, int error,
55                          const char *dset,
56                          const char *addinfo, const char *addinfo2)
57 {
58     char *cp;
59
60     xfree(c->addinfo);
61     c->addinfo = 0;
62     c->error = error;
63     if (!c->diagset || strcmp(dset, c->diagset))
64     {
65         xfree(c->diagset);
66         c->diagset = xstrdup(dset);
67         /* remove integer part from SRW diagset .. */
68         if ((cp = strrchr(c->diagset, '/')))
69             *cp = '\0';
70     }
71     if (addinfo && addinfo2)
72     {
73         c->addinfo = (char*) xmalloc(strlen(addinfo) + strlen(addinfo2) + 3);
74         strcpy(c->addinfo, addinfo);
75         strcat(c->addinfo, ": ");
76         strcat(c->addinfo, addinfo2);
77     }
78     else if (addinfo)
79         c->addinfo = xstrdup(addinfo);
80     if (error != ZOOM_ERROR_NONE)
81     {
82         yaz_log(c->log_api, "%p set_dset_error %s %s:%d %s %s",
83                 c, c->host_port ? c->host_port : "<>", dset, error,
84                 addinfo ? addinfo : "",
85                 addinfo2 ? addinfo2 : "");
86         ZOOM_connection_remove_tasks(c);
87     }
88 }
89
90 int ZOOM_uri_to_code(const char *uri)
91 {
92     int code = 0;
93     const char *cp;
94     if ((cp = strrchr(uri, '/')))
95         code = atoi(cp+1);
96     return code;
97 }
98
99
100
101 void ZOOM_set_error(ZOOM_connection c, int error, const char *addinfo)
102 {
103     ZOOM_set_dset_error(c, error, "ZOOM", addinfo, 0);
104 }
105
106 static void clear_error(ZOOM_connection c)
107 {
108     /*
109      * If an error is tied to an operation then it's ok to clear: for
110      * example, a diagnostic returned from a search is cleared by a
111      * subsequent search.  However, problems such as Connection Lost
112      * or Init Refused are not cleared, because they are not
113      * recoverable: doing another search doesn't help.
114      */
115
116     ZOOM_connection_remove_events(c);
117     switch (c->error)
118     {
119     case ZOOM_ERROR_CONNECT:
120     case ZOOM_ERROR_MEMORY:
121     case ZOOM_ERROR_DECODE:
122     case ZOOM_ERROR_CONNECTION_LOST:
123     case ZOOM_ERROR_INIT:
124     case ZOOM_ERROR_INTERNAL:
125     case ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
126         break;
127     default:
128         ZOOM_set_error(c, ZOOM_ERROR_NONE, 0);
129     }
130 }
131
132 void ZOOM_connection_show_task(ZOOM_task task)
133 {
134     switch(task->which)
135     {
136     case ZOOM_TASK_SEARCH:
137         yaz_log(YLOG_LOG, "search p=%p", task);
138         break;
139     case ZOOM_TASK_RETRIEVE:
140         yaz_log(YLOG_LOG, "retrieve p=%p", task);
141         break;
142     case ZOOM_TASK_CONNECT:
143         yaz_log(YLOG_LOG, "connect p=%p", task);
144         break;
145     case ZOOM_TASK_SCAN:
146         yaz_log(YLOG_LOG, "scan p=%p", task);
147         break;
148     }
149 }
150
151 void ZOOM_connection_show_tasks(ZOOM_connection c)
152 {
153     ZOOM_task task;
154     yaz_log(YLOG_LOG, "connection p=%p tasks", c);
155     for (task = c->tasks; task; task = task->next)
156         ZOOM_connection_show_task(task);
157 }
158
159 ZOOM_task ZOOM_connection_add_task(ZOOM_connection c, int which)
160 {
161     ZOOM_task *taskp = &c->tasks;
162     while (*taskp)
163         taskp = &(*taskp)->next;
164     *taskp = (ZOOM_task) xmalloc(sizeof(**taskp));
165     (*taskp)->running = 0;
166     (*taskp)->which = which;
167     (*taskp)->next = 0;
168     clear_error(c);
169     return *taskp;
170 }
171
172 ZOOM_API(int) ZOOM_connection_is_idle(ZOOM_connection c)
173 {
174     return c->tasks ? 0 : 1;
175 }
176
177 ZOOM_task ZOOM_connection_insert_task(ZOOM_connection c, int which)
178 {
179     ZOOM_task task = (ZOOM_task) xmalloc(sizeof(*task));
180
181     task->next = c->tasks;
182     c->tasks = task;
183
184     task->running = 0;
185     task->which = which;
186     return task;
187 }
188
189 void ZOOM_connection_remove_task(ZOOM_connection c)
190 {
191     ZOOM_task task = c->tasks;
192
193     if (task)
194     {
195         c->tasks = task->next;
196         switch (task->which)
197         {
198         case ZOOM_TASK_SEARCH:
199             resultset_destroy(task->u.search.resultset);
200             xfree(task->u.search.syntax);
201             xfree(task->u.search.elementSetName);
202             xfree(task->u.search.schema);
203             break;
204         case ZOOM_TASK_RETRIEVE:
205             resultset_destroy(task->u.retrieve.resultset);
206             xfree(task->u.retrieve.syntax);
207             xfree(task->u.retrieve.elementSetName);
208             xfree(task->u.retrieve.schema);
209             break;
210         case ZOOM_TASK_CONNECT:
211             break;
212         case ZOOM_TASK_SCAN:
213             ZOOM_scanset_destroy(task->u.scan.scan);
214             break;
215         case ZOOM_TASK_PACKAGE:
216             ZOOM_package_destroy(task->u.package);
217             break;
218         case ZOOM_TASK_SORT:
219             resultset_destroy(task->u.sort.resultset);
220             ZOOM_query_destroy(task->u.sort.q);
221             break;
222         default:
223             assert(0);
224         }
225         xfree(task);
226
227         if (!c->tasks)
228         {
229             ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_END);
230             ZOOM_connection_put_event(c, event);
231         }
232     }
233 }
234
235 void ZOOM_connection_remove_tasks(ZOOM_connection c)
236 {
237     while (c->tasks)
238         ZOOM_connection_remove_task(c);
239 }
240
241 static void odr_wrbuf_write(ODR o, void *handle, int type,
242                             const char *buf, int len)
243 {
244     WRBUF w = (WRBUF) handle;
245     wrbuf_write(w, buf, len);
246 }
247
248 ZOOM_API(ZOOM_connection)
249     ZOOM_connection_create(ZOOM_options options)
250 {
251     ZOOM_connection c = (ZOOM_connection) xmalloc(sizeof(*c));
252
253     initlog();
254
255     c->log_api = log_api0;
256     c->log_details = log_details0;
257
258     yaz_log(c->log_api, "%p ZOOM_connection_create", c);
259
260     c->proto = PROTO_Z3950;
261     c->cs = 0;
262     ZOOM_connection_set_mask(c, 0);
263     c->reconnect_ok = 0;
264     c->state = STATE_IDLE;
265     c->addinfo = 0;
266     c->diagset = 0;
267     ZOOM_set_error(c, ZOOM_ERROR_NONE, 0);
268     c->buf_in = 0;
269     c->len_in = 0;
270     c->buf_out = 0;
271     c->len_out = 0;
272     c->resultsets = 0;
273
274     c->options = ZOOM_options_create_with_parent(options);
275
276     c->host_port = 0;
277     c->proxy = 0;
278     c->tproxy = 0;
279
280     c->charset = c->lang = 0;
281
282     c->cookie_out = 0;
283     c->cookie_in = 0;
284     c->client_IP = 0;
285     c->tasks = 0;
286
287     c->user = 0;
288     c->group = 0;
289     c->password = 0;
290     c->url_authentication = 0;
291
292     c->maximum_record_size = 0;
293     c->preferred_message_size = 0;
294
295     c->odr_in = odr_createmem(ODR_DECODE);
296     c->odr_out = odr_createmem(ODR_ENCODE);
297     c->odr_print = 0;
298     c->odr_save = 0;
299
300     c->async = 0;
301     c->support_named_resultsets = 0;
302     c->last_event = ZOOM_EVENT_NONE;
303
304     c->m_queue_front = 0;
305     c->m_queue_back = 0;
306
307     c->sru_version = 0;
308     c->no_redirects = 0;
309     c->cookies = 0;
310     c->saveAPDU_wrbuf = 0;
311
312 #if HAVE_LIBMEMCACHED_MEMCACHED_H
313     c->mc_st = 0;
314 #endif
315     return c;
316 }
317
318 ZOOM_API(void) ZOOM_connection_save_apdu_wrbuf(ZOOM_connection c, WRBUF w)
319 {
320     if (c->odr_save)
321     {
322         odr_destroy(c->odr_save);
323         c->odr_save = 0;
324     }
325     if (w)
326     {
327         c->odr_save = odr_createmem(ODR_PRINT);
328         odr_set_stream(c->odr_save, w, odr_wrbuf_write, 0);
329     }
330 }
331
332 /* set database names. Take local databases (if set); otherwise
333    take databases given in ZURL (if set); otherwise use Default */
334 char **ZOOM_connection_get_databases(ZOOM_connection con, ZOOM_options options,
335                                      int *num, ODR odr)
336 {
337     char **databaseNames;
338     const char *cp = ZOOM_options_get(options, "databaseName");
339
340     if ((!cp || !*cp) && con->host_port)
341         cs_get_host_args(con->host_port, &cp);
342     if (!cp || !*cp)
343         cp = "Default";
344     nmem_strsplit(odr_getmem(odr), "+", cp,  &databaseNames, num);
345     return databaseNames;
346 }
347
348 ZOOM_API(ZOOM_connection)
349     ZOOM_connection_new(const char *host, int portnum)
350 {
351     ZOOM_connection c = ZOOM_connection_create(0);
352
353     ZOOM_connection_connect(c, host, portnum);
354     return c;
355 }
356
357 static zoom_sru_mode get_sru_mode_from_string(const char *s)
358 {
359     if (!s || !*s)
360         return zoom_sru_soap;
361     if (!yaz_matchstr(s, "soap"))
362         return zoom_sru_soap;
363     else if (!yaz_matchstr(s, "get"))
364         return zoom_sru_get;
365     else if (!yaz_matchstr(s, "post"))
366         return zoom_sru_post;
367     else if (!yaz_matchstr(s, "solr"))
368         return zoom_sru_solr;
369     return zoom_sru_error;
370 }
371
372 ZOOM_API(void)
373     ZOOM_connection_connect(ZOOM_connection c,
374                             const char *host, int portnum)
375 {
376     const char *val;
377
378     initlog();
379
380     yaz_log(c->log_api, "%p ZOOM_connection_connect host=%s portnum=%d",
381             c, host ? host : "null", portnum);
382
383     ZOOM_set_error(c, ZOOM_ERROR_NONE, 0);
384     ZOOM_connection_remove_tasks(c);
385
386     if (c->odr_print)
387     {
388         odr_setprint(c->odr_print, 0); /* prevent destroy from fclose'ing */
389         odr_destroy(c->odr_print);
390     }
391     if (ZOOM_options_get_bool(c->options, "apdulog", 0))
392     {
393         c->odr_print = odr_createmem(ODR_PRINT);
394         odr_setprint(c->odr_print, yaz_log_file());
395     }
396     else
397         c->odr_print = 0;
398
399     if (c->cs)
400     {
401         yaz_log(c->log_details, "%p ZOOM_connection_connect reconnect ok", c);
402         c->reconnect_ok = 1;
403         return;
404     }
405     yaz_log(c->log_details, "%p ZOOM_connection_connect connect", c);
406     xfree(c->proxy);
407     c->proxy = 0;
408     val = ZOOM_options_get(c->options, "proxy");
409     if (val && *val)
410     {
411         yaz_log(c->log_details, "%p ZOOM_connection_connect proxy=%s", c, val);
412         c->proxy = xstrdup(val);
413     }
414
415     xfree(c->tproxy);
416     c->tproxy = 0;
417     val = ZOOM_options_get(c->options, "tproxy");
418     if (val && *val)
419     {
420         yaz_log(c->log_details, "%p ZOOM_connection_connect tproxy=%s", c, val);
421         c->tproxy = xstrdup(val);
422     }
423
424     xfree(c->charset);
425     c->charset = 0;
426     val = ZOOM_options_get(c->options, "charset");
427     if (val && *val)
428     {
429         yaz_log(c->log_details, "%p ZOOM_connection_connect charset=%s", c, val);
430         c->charset = xstrdup(val);
431     }
432
433     xfree(c->lang);
434     val = ZOOM_options_get(c->options, "lang");
435     if (val && *val)
436     {
437         yaz_log(c->log_details, "%p ZOOM_connection_connect lang=%s", c, val);
438         c->lang = xstrdup(val);
439     }
440     else
441         c->lang = 0;
442
443     if (host)
444     {
445         xfree(c->host_port);
446         if (portnum)
447         {
448             char hostn[128];
449             sprintf(hostn, "%.80s:%d", host, portnum);
450             c->host_port = xstrdup(hostn);
451         }
452         else
453             c->host_port = xstrdup(host);
454     }
455
456     {
457         /*
458          * If the "<scheme>:" part of the host string is preceded by one
459          * or more comma-separated <name>=<value> pairs, these are taken
460          * to be options to be set on the connection object.  Among other
461          * applications, this facility can be used to embed authentication
462          * in a host string:
463          *          user=admin,password=secret,tcp:localhost:9999
464          */
465         char *remainder = c->host_port;
466         char *pcolon = strchr(remainder, ':');
467         char *pcomma;
468         char *pequals;
469         while ((pcomma = strchr(remainder, ',')) != 0 &&
470                (pcolon == 0 || pcomma < pcolon))
471         {
472             *pcomma = '\0';
473             if ((pequals = strchr(remainder, '=')) != 0)
474             {
475                 *pequals = '\0';
476                 ZOOM_connection_option_set(c, remainder, pequals+1);
477             }
478             remainder = pcomma+1;
479         }
480
481         if (remainder != c->host_port)
482         {
483             remainder = xstrdup(remainder);
484             xfree(c->host_port);
485             c->host_port = remainder;
486         }
487     }
488
489     val = ZOOM_options_get(c->options, "sru");
490     c->sru_mode = get_sru_mode_from_string(val);
491
492     xfree(c->sru_version);
493     val = ZOOM_options_get(c->options, "sru_version");
494     c->sru_version = xstrdup(val ? val : "1.2");
495
496     ZOOM_options_set(c->options, "host", c->host_port);
497
498     xfree(c->cookie_out);
499     c->cookie_out = 0;
500     val = ZOOM_options_get(c->options, "cookie");
501     if (val && *val)
502     {
503         yaz_log(c->log_details, "%p ZOOM_connection_connect cookie=%s", c, val);
504         c->cookie_out = xstrdup(val);
505     }
506
507     xfree(c->client_IP);
508     c->client_IP = 0;
509     val = ZOOM_options_get(c->options, "clientIP");
510     if (val && *val)
511     {
512         yaz_log(c->log_details, "%p ZOOM_connection_connect clientIP=%s",
513                 c, val);
514         c->client_IP = xstrdup(val);
515     }
516
517     xfree(c->group);
518     c->group = 0;
519     val = ZOOM_options_get(c->options, "group");
520     if (val && *val)
521         c->group = xstrdup(val);
522
523     xfree(c->user);
524     c->user = 0;
525     val = ZOOM_options_get(c->options, "user");
526     if (val && *val)
527         c->user = xstrdup(val);
528
529     xfree(c->password);
530     c->password = 0;
531     val = ZOOM_options_get(c->options, "password");
532     if (!val)
533         val = ZOOM_options_get(c->options, "pass");
534     if (val && *val)
535         c->password = xstrdup(val);
536
537     val = ZOOM_options_get(c->options, "authenticationMode");
538     if (val && !strcmp(val, "url"))
539         c->url_authentication = 1;
540     else
541         c->url_authentication = 0;
542
543     c->maximum_record_size =
544         ZOOM_options_get_int(c->options, "maximumRecordSize", 64*1024*1024);
545     c->preferred_message_size =
546         ZOOM_options_get_int(c->options, "preferredMessageSize", 64*1024*1024);
547
548     c->async = ZOOM_options_get_bool(c->options, "async", 0);
549
550     yaz_cookies_destroy(c->cookies);
551     c->cookies = yaz_cookies_create();
552
553 #if HAVE_LIBMEMCACHED_MEMCACHED_H
554     if (c->mc_st)
555     {
556         memcached_free(c->mc_st);
557         c->mc_st = 0;
558     }
559 #endif
560     val = ZOOM_options_get(c->options, "memcached");
561     if (val && *val)
562     {
563 #if HAVE_LIBMEMCACHED_MEMCACHED_H
564         c->mc_st = memcached(val, strlen(val));
565         if (!c->mc_st)
566         {
567             ZOOM_set_error(c, ZOOM_ERROR_MEMCACHED, val);
568             return;
569         }
570 #else
571         ZOOM_set_error(c, ZOOM_ERROR_MEMCACHED, "not enabled");
572         return;
573 #endif
574     }
575
576     if (c->sru_mode == zoom_sru_error)
577     {
578         ZOOM_set_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, val);
579         ZOOM_connection_remove_tasks(c);
580         return;
581     }
582
583     yaz_log(c->log_details, "%p ZOOM_connection_connect async=%d", c, c->async);
584     ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
585
586     if (!c->async)
587     {
588         while (ZOOM_event(1, &c))
589             ;
590     }
591 }
592
593 ZOOM_API(void) ZOOM_resultset_release(ZOOM_resultset r)
594 {
595     if (r->connection)
596     {
597         /* remove ourselves from the resultsets in connection */
598         ZOOM_resultset *rp = &r->connection->resultsets;
599         while (1)
600         {
601             assert(*rp);   /* we must be in this list!! */
602             if (*rp == r)
603             {   /* OK, we're here - take us out of it */
604                 *rp = (*rp)->next;
605                 break;
606             }
607             rp = &(*rp)->next;
608         }
609         r->connection = 0;
610     }
611 }
612
613 ZOOM_API(void)
614     ZOOM_connection_destroy(ZOOM_connection c)
615 {
616     ZOOM_resultset r;
617     if (!c)
618         return;
619     yaz_log(c->log_api, "%p ZOOM_connection_destroy", c);
620
621 #if HAVE_LIBMEMCACHED_MEMCACHED_H
622     if (c->mc_st)
623         memcached_free(c->mc_st);
624 #endif
625     if (c->cs)
626         cs_close(c->cs);
627
628     for (r = c->resultsets; r; r = r->next)
629         r->connection = 0;
630
631     xfree(c->buf_in);
632     xfree(c->addinfo);
633     xfree(c->diagset);
634     odr_destroy(c->odr_in);
635     odr_destroy(c->odr_out);
636     if (c->odr_save)
637         odr_destroy(c->odr_save);
638     if (c->odr_print)
639     {
640         odr_setprint(c->odr_print, 0); /* prevent destroy from fclose'ing */
641         odr_destroy(c->odr_print);
642     }
643     ZOOM_options_destroy(c->options);
644     ZOOM_connection_remove_tasks(c);
645     ZOOM_connection_remove_events(c);
646     xfree(c->host_port);
647     xfree(c->proxy);
648     xfree(c->tproxy);
649     xfree(c->charset);
650     xfree(c->lang);
651     xfree(c->cookie_out);
652     xfree(c->cookie_in);
653     xfree(c->client_IP);
654     xfree(c->user);
655     xfree(c->group);
656     xfree(c->password);
657     xfree(c->sru_version);
658     yaz_cookies_destroy(c->cookies);
659     wrbuf_destroy(c->saveAPDU_wrbuf);
660     xfree(c);
661 }
662
663 void ZOOM_resultset_addref(ZOOM_resultset r)
664 {
665     if (r)
666     {
667         yaz_mutex_enter(r->mutex);
668         (r->refcount)++;
669         yaz_log(log_details0, "%p ZOOM_resultset_addref count=%d",
670                 r, r->refcount);
671         yaz_mutex_leave(r->mutex);
672     }
673 }
674
675 static int g_resultsets = 0;
676 static YAZ_MUTEX g_resultset_mutex = 0;
677
678 /* TODO We need to initialize this before running threaded:
679  * call resultset_use(0)  */
680
681 static int resultset_use(int delta) {
682     int resultset_count;
683     if (g_resultset_mutex == 0)
684         yaz_mutex_create(&g_resultset_mutex);
685     yaz_mutex_enter(g_resultset_mutex);
686     g_resultsets += delta;
687     resultset_count = g_resultsets;
688     yaz_mutex_leave(g_resultset_mutex);
689     return resultset_count;
690 }
691
692 int resultsets_count(void) {
693     return resultset_use(0);
694 }
695
696 ZOOM_resultset ZOOM_resultset_create(void)
697 {
698     int i;
699     ZOOM_resultset r = (ZOOM_resultset) xmalloc(sizeof(*r));
700
701     initlog();
702
703     yaz_log(log_details0, "%p ZOOM_resultset_create", r);
704     r->refcount = 1;
705     r->size = 0;
706     r->odr = odr_createmem(ODR_ENCODE);
707     r->piggyback = 1;
708     r->setname = 0;
709     r->step = 0;
710     for (i = 0; i<RECORD_HASH_SIZE; i++)
711         r->record_hash[i] = 0;
712     r->r_sort_spec = 0;
713     r->query = 0;
714     r->connection = 0;
715     r->databaseNames = 0;
716     r->num_databaseNames = 0;
717     r->facets = 0;
718     r->num_facets = 0;
719     r->facets_names = 0;
720     r->mutex = 0;
721     yaz_mutex_create(&r->mutex);
722 #if SHPTR
723     {
724         WRBUF w = wrbuf_alloc();
725         YAZ_SHPTR_INIT(r->record_wrbuf, w);
726     }
727 #endif
728     resultset_use(1);
729     r->mc_key = 0;
730     return r;
731 }
732
733 ZOOM_API(ZOOM_resultset)
734     ZOOM_connection_search_pqf(ZOOM_connection c, const char *q)
735 {
736     ZOOM_resultset r;
737     ZOOM_query s = ZOOM_query_create();
738
739     ZOOM_query_prefix(s, q);
740
741     r = ZOOM_connection_search(c, s);
742     ZOOM_query_destroy(s);
743     return r;
744 }
745
746 ZOOM_API(ZOOM_resultset)
747     ZOOM_connection_search(ZOOM_connection c, ZOOM_query q)
748 {
749     ZOOM_resultset r = ZOOM_resultset_create();
750     const char *cp;
751     ZOOM_task task;
752     int start, count;
753     const char *syntax, *elementSetName, *schema;
754     yaz_log(c->log_api, "%p ZOOM_connection_search set %p query %p", c, r, q);
755     r->r_sort_spec = ZOOM_query_get_sortspec(q);
756     r->query = q;
757
758     r->options = ZOOM_options_create_with_parent(c->options);
759
760     start = ZOOM_options_get_int(r->options, "start", 0);
761     count = ZOOM_options_get_int(r->options, "count", 0);
762     {
763         /* If "presentChunk" is defined use that; otherwise "step" */
764         const char *cp = ZOOM_options_get(r->options, "presentChunk");
765         r->step = ZOOM_options_get_int(r->options,
766                                        (cp != 0 ? "presentChunk": "step"), 0);
767     }
768     r->piggyback = ZOOM_options_get_bool(r->options, "piggyback", 1);
769     cp = ZOOM_options_get(r->options, "setname");
770     if (cp)
771         r->setname = xstrdup(cp);
772
773     r->databaseNames = ZOOM_connection_get_databases(c, c->options, &r->num_databaseNames,
774                                          r->odr);
775
776     r->connection = c;
777     r->next = c->resultsets;
778     c->resultsets = r;
779
780 #if HAVE_LIBMEMCACHED_MEMCACHED_H
781     r->mc_key = wrbuf_alloc();
782     wrbuf_puts(r->mc_key, c->host_port);
783     wrbuf_puts(r->mc_key, ";");
784     wrbuf_puts(r->mc_key, ZOOM_query_get_query_string(q));
785     if (c->mc_st)
786     {
787         size_t v_len;
788         uint32_t flags;
789         memcached_return_t rc;
790         char *v = memcached_get(c->mc_st, wrbuf_buf(r->mc_key),
791                                 wrbuf_len(r->mc_key), &v_len, &flags, &rc);
792         if (v)
793         {
794             yaz_log(YLOG_LOG, "For key %s got value %.*s",
795                     wrbuf_cstr(r->mc_key), (int) v_len, v);
796         }
797         else
798         {
799             yaz_log(YLOG_LOG, "For key %s got NO value", wrbuf_cstr(r->mc_key));
800         }
801     }
802 #endif
803
804     if (c->host_port && c->proto == PROTO_HTTP)
805     {
806         if (!c->cs)
807         {
808             yaz_log(c->log_details, "ZOOM_connection_search: no comstack");
809             ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
810         }
811         else
812         {
813             yaz_log(c->log_details, "ZOOM_connection_search: reconnect");
814             c->reconnect_ok = 1;
815         }
816     }
817
818     task = ZOOM_connection_add_task(c, ZOOM_TASK_SEARCH);
819     task->u.search.resultset = r;
820     task->u.search.start = start;
821     task->u.search.count = count;
822     task->u.search.recv_search_fired = 0;
823
824     syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
825     task->u.search.syntax = syntax ? xstrdup(syntax) : 0;
826     elementSetName = ZOOM_options_get(r->options, "elementSetName");
827     task->u.search.elementSetName = elementSetName ?
828         xstrdup(elementSetName) : 0;
829     schema = ZOOM_options_get(r->options, "schema");
830     task->u.search.schema = schema ? xstrdup(schema) : 0;
831
832     ZOOM_resultset_addref(r);
833
834     ZOOM_query_addref(q);
835
836     if (!c->async)
837     {
838         while (ZOOM_event(1, &c))
839             ;
840     }
841     return r;
842 }
843
844 ZOOM_API(void)
845     ZOOM_resultset_sort(ZOOM_resultset r,
846                          const char *sort_type, const char *sort_spec)
847 {
848     (void) ZOOM_resultset_sort1(r, sort_type, sort_spec);
849 }
850
851 ZOOM_API(int)
852     ZOOM_resultset_sort1(ZOOM_resultset r,
853                          const char *sort_type, const char *sort_spec)
854 {
855     ZOOM_connection c = r->connection;
856     ZOOM_task task;
857     ZOOM_query newq;
858
859     newq = ZOOM_query_create();
860     if (ZOOM_query_sortby(newq, sort_spec) < 0)
861         return -1;
862
863     yaz_log(c->log_api, "%p ZOOM_resultset_sort r=%p sort_type=%s sort_spec=%s",
864             r, r, sort_type, sort_spec);
865     if (!c)
866         return 0;
867
868     if (c->host_port && c->proto == PROTO_HTTP)
869     {
870         if (!c->cs)
871         {
872             yaz_log(c->log_details, "%p ZOOM_resultset_sort: no comstack", r);
873             ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
874         }
875         else
876         {
877             yaz_log(c->log_details, "%p ZOOM_resultset_sort: prepare reconnect",
878                     r);
879             c->reconnect_ok = 1;
880         }
881     }
882
883     ZOOM_resultset_cache_reset(r);
884     task = ZOOM_connection_add_task(c, ZOOM_TASK_SORT);
885     task->u.sort.resultset = r;
886     task->u.sort.q = newq;
887
888     ZOOM_resultset_addref(r);
889
890     if (!c->async)
891     {
892         while (ZOOM_event(1, &c))
893             ;
894     }
895
896     return 0;
897 }
898
899 ZOOM_API(void)
900     ZOOM_resultset_destroy(ZOOM_resultset r)
901 {
902     resultset_destroy(r);
903 }
904
905 static void resultset_destroy(ZOOM_resultset r)
906 {
907     if (!r)
908         return;
909     yaz_mutex_enter(r->mutex);
910     (r->refcount)--;
911     yaz_log(log_details0, "%p ZOOM_resultset_destroy r=%p count=%d",
912             r, r, r->refcount);
913     if (r->refcount == 0)
914     {
915         yaz_mutex_leave(r->mutex);
916
917         yaz_log(log_details0, "%p ZOOM_connection resultset_destroy: Deleting resultset (%p) ", r->connection, r);
918         ZOOM_resultset_cache_reset(r);
919         ZOOM_resultset_release(r);
920         ZOOM_query_destroy(r->query);
921         ZOOM_options_destroy(r->options);
922         odr_destroy(r->odr);
923         xfree(r->setname);
924         yaz_mutex_destroy(&r->mutex);
925 #if SHPTR
926         YAZ_SHPTR_DEC(r->record_wrbuf, wrbuf_destroy);
927 #endif
928         wrbuf_destroy(r->mc_key);
929         resultset_use(-1);
930         xfree(r);
931     }
932     else
933         yaz_mutex_leave(r->mutex);
934 }
935
936 ZOOM_API(size_t)
937     ZOOM_resultset_size(ZOOM_resultset r)
938 {
939     return r->size;
940 }
941
942 int ZOOM_test_reconnect(ZOOM_connection c)
943 {
944     ZOOM_Event event;
945
946     if (!c->reconnect_ok)
947         return 0;
948     ZOOM_connection_close(c);
949     c->reconnect_ok = 0;
950     c->tasks->running = 0;
951     ZOOM_connection_insert_task(c, ZOOM_TASK_CONNECT);
952
953     event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
954     ZOOM_connection_put_event(c, event);
955
956     return 1;
957 }
958
959 static void ZOOM_resultset_retrieve(ZOOM_resultset r,
960                                     int force_sync, int start, int count)
961 {
962     ZOOM_task task;
963     ZOOM_connection c;
964     const char *cp;
965     const char *syntax, *elementSetName;
966
967     if (!r)
968         return;
969     yaz_log(log_details0, "%p ZOOM_resultset_retrieve force_sync=%d start=%d"
970             " count=%d", r, force_sync, start, count);
971     c = r->connection;
972     if (!c)
973         return;
974
975     if (c->host_port && c->proto == PROTO_HTTP)
976     {
977         if (!c->cs)
978         {
979             yaz_log(log_details0, "%p ZOOM_resultset_retrieve: no comstack", r);
980             ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
981         }
982         else
983         {
984             yaz_log(log_details0, "%p ZOOM_resultset_retrieve: prepare "
985                     "reconnect", r);
986             c->reconnect_ok = 1;
987         }
988     }
989     task = ZOOM_connection_add_task(c, ZOOM_TASK_RETRIEVE);
990     task->u.retrieve.resultset = r;
991     task->u.retrieve.start = start;
992     task->u.retrieve.count = count;
993
994     syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
995     task->u.retrieve.syntax = syntax ? xstrdup(syntax) : 0;
996     elementSetName = ZOOM_options_get(r->options, "elementSetName");
997     task->u.retrieve.elementSetName = elementSetName
998         ? xstrdup(elementSetName) : 0;
999
1000     cp = ZOOM_options_get(r->options, "schema");
1001     task->u.retrieve.schema = cp ? xstrdup(cp) : 0;
1002
1003     ZOOM_resultset_addref(r);
1004
1005     if (!r->connection->async || force_sync)
1006         while (r->connection && ZOOM_event(1, &r->connection))
1007             ;
1008 }
1009
1010 ZOOM_API(void)
1011     ZOOM_resultset_records(ZOOM_resultset r, ZOOM_record *recs,
1012                            size_t start, size_t count)
1013 {
1014     int force_present = 0;
1015
1016     if (!r)
1017         return ;
1018     yaz_log(log_api0, "%p ZOOM_resultset_records r=%p start=%ld count=%ld",
1019             r, r, (long) start, (long) count);
1020     if (count && recs)
1021         force_present = 1;
1022     ZOOM_resultset_retrieve(r, force_present, start, count);
1023     if (force_present)
1024     {
1025         size_t i;
1026         for (i = 0; i< count; i++)
1027             recs[i] = ZOOM_resultset_record_immediate(r, i+start);
1028     }
1029 }
1030
1031 ZOOM_API(size_t)
1032     ZOOM_resultset_facets_size(ZOOM_resultset r) {
1033     return r->num_facets;
1034 }
1035
1036 ZOOM_API(ZOOM_facet_field)
1037     ZOOM_resultset_get_facet_field(ZOOM_resultset r, const char *name) {
1038     int num = r->num_facets;
1039     ZOOM_facet_field *facets = r->facets;
1040     int index;
1041     for (index = 0; index < num; index++) {
1042         if (!strcmp(facets[index]->facet_name, name)) {
1043             return facets[index];
1044         }
1045     }
1046     return 0;
1047 }
1048
1049 ZOOM_API(ZOOM_facet_field)
1050     ZOOM_resultset_get_facet_field_by_index(ZOOM_resultset r, int index) {
1051     int num = r->num_facets;
1052     ZOOM_facet_field *facets = r->facets;
1053     if (index >= 0 && index < num) {
1054         return facets[index];
1055     }
1056     return 0;
1057 }
1058
1059 ZOOM_API(ZOOM_facet_field *)
1060     ZOOM_resultset_facets(ZOOM_resultset r)
1061 {
1062     return r->facets;
1063 }
1064
1065 ZOOM_API(const char**)
1066     ZOOM_resultset_facets_names(ZOOM_resultset r)
1067 {
1068     return (const char **) r->facets_names;
1069 }
1070
1071 ZOOM_API(const char*)
1072     ZOOM_facet_field_name(ZOOM_facet_field field)
1073 {
1074     return field->facet_name;
1075 }
1076
1077 ZOOM_API(size_t)
1078     ZOOM_facet_field_term_count(ZOOM_facet_field field)
1079 {
1080     return field->num_terms;
1081 }
1082
1083 ZOOM_API(const char*)
1084     ZOOM_facet_field_get_term(ZOOM_facet_field field, size_t idx, int *freq) {
1085     *freq = field->facet_terms[idx].frequency;
1086     return field->facet_terms[idx].term;
1087 }
1088
1089
1090 static void get_cert(ZOOM_connection c)
1091 {
1092     char *cert_buf;
1093     int cert_len;
1094
1095     if (cs_get_peer_certificate_x509(c->cs, &cert_buf, &cert_len))
1096     {
1097         ZOOM_connection_option_setl(c, "sslPeerCert",
1098                                     cert_buf, cert_len);
1099         xfree(cert_buf);
1100     }
1101 }
1102
1103 static zoom_ret do_connect_host(ZOOM_connection c,
1104                                 const char *logical_url);
1105
1106 static zoom_ret do_connect(ZOOM_connection c)
1107 {
1108     return do_connect_host(c, c->host_port);
1109 }
1110
1111 static zoom_ret do_connect_host(ZOOM_connection c, const char *logical_url)
1112 {
1113     void *add;
1114
1115     if (c->cs)
1116         cs_close(c->cs);
1117     c->cs = cs_create_host_proxy(logical_url, 0, &add,
1118                                  c->tproxy ? c->tproxy : c->proxy);
1119
1120     if (c->cs && c->cs->protocol == PROTO_HTTP)
1121     {
1122 #if YAZ_HAVE_XML2
1123         c->proto = PROTO_HTTP;
1124 #else
1125         ZOOM_set_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, "SRW");
1126         ZOOM_connection_close(c);
1127         return zoom_complete;
1128 #endif
1129     }
1130     if (c->cs)
1131     {
1132         int ret = cs_connect(c->cs, add);
1133         if (ret == 0)
1134         {
1135             ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1136             ZOOM_connection_put_event(c, event);
1137             get_cert(c);
1138             if (c->proto == PROTO_Z3950)
1139                 ZOOM_connection_Z3950_send_init(c);
1140             else
1141             {
1142                 /* no init request for SRW .. */
1143                 assert(c->tasks->which == ZOOM_TASK_CONNECT);
1144                 ZOOM_connection_remove_task(c);
1145                 ZOOM_connection_set_mask(c, 0);
1146                 ZOOM_connection_exec_task(c);
1147             }
1148             c->state = STATE_ESTABLISHED;
1149             return zoom_pending;
1150         }
1151         else if (ret > 0)
1152         {
1153             int mask = ZOOM_SELECT_EXCEPT;
1154             if (c->cs->io_pending & CS_WANT_WRITE)
1155                 mask += ZOOM_SELECT_WRITE;
1156             if (c->cs->io_pending & CS_WANT_READ)
1157                 mask += ZOOM_SELECT_READ;
1158             ZOOM_connection_set_mask(c, mask);
1159             c->state = STATE_CONNECTING;
1160             return zoom_pending;
1161         }
1162     }
1163     c->state = STATE_IDLE;
1164     ZOOM_set_error(c, ZOOM_ERROR_CONNECT, logical_url);
1165     return zoom_complete;
1166 }
1167
1168 /* returns 1 if PDU was sent OK (still pending )
1169    0 if PDU was not sent OK (nothing to wait for)
1170 */
1171
1172 ZOOM_API(ZOOM_record)
1173     ZOOM_resultset_record_immediate(ZOOM_resultset s,size_t pos)
1174 {
1175     const char *syntax =
1176         ZOOM_options_get(s->options, "preferredRecordSyntax");
1177     const char *elementSetName =
1178         ZOOM_options_get(s->options, "elementSetName");
1179     const char *schema =
1180         ZOOM_options_get(s->options, "schema");
1181
1182     return ZOOM_record_cache_lookup(s, pos, syntax, elementSetName, schema);
1183 }
1184
1185 ZOOM_API(ZOOM_record)
1186     ZOOM_resultset_record(ZOOM_resultset r, size_t pos)
1187 {
1188     ZOOM_record rec = ZOOM_resultset_record_immediate(r, pos);
1189
1190     if (!rec)
1191     {
1192         /*
1193          * MIKE: I think force_sync should always be zero, but I don't
1194          * want to make this change until I get the go-ahead from
1195          * Adam, in case something depends on the old synchronous
1196          * behaviour.
1197          */
1198         int force_sync = 1;
1199         if (getenv("ZOOM_RECORD_NO_FORCE_SYNC")) force_sync = 0;
1200         ZOOM_resultset_retrieve(r, force_sync, pos, 1);
1201         rec = ZOOM_resultset_record_immediate(r, pos);
1202     }
1203     return rec;
1204 }
1205
1206 ZOOM_API(ZOOM_scanset)
1207     ZOOM_connection_scan(ZOOM_connection c, const char *start)
1208 {
1209     ZOOM_scanset s;
1210     ZOOM_query q = ZOOM_query_create();
1211
1212     ZOOM_query_prefix(q, start);
1213
1214     s = ZOOM_connection_scan1(c, q);
1215     ZOOM_query_destroy(q);
1216     return s;
1217
1218 }
1219
1220 ZOOM_API(ZOOM_scanset)
1221     ZOOM_connection_scan1(ZOOM_connection c, ZOOM_query q)
1222 {
1223     ZOOM_scanset scan = 0;
1224     Z_Query *z_query = ZOOM_query_get_Z_Query(q);
1225
1226     if (!z_query)
1227         return 0;
1228     scan = (ZOOM_scanset) xmalloc(sizeof(*scan));
1229     scan->connection = c;
1230     scan->odr = odr_createmem(ODR_DECODE);
1231     scan->options = ZOOM_options_create_with_parent(c->options);
1232     scan->refcount = 1;
1233     scan->scan_response = 0;
1234     scan->srw_scan_response = 0;
1235
1236     scan->query = q;
1237     ZOOM_query_addref(q);
1238     scan->databaseNames = ZOOM_connection_get_databases(c, c->options,
1239                                             &scan->num_databaseNames,
1240                                             scan->odr);
1241
1242     if (1)
1243     {
1244         ZOOM_task task = ZOOM_connection_add_task(c, ZOOM_TASK_SCAN);
1245         task->u.scan.scan = scan;
1246
1247         (scan->refcount)++;
1248         if (!c->async)
1249         {
1250             while (ZOOM_event(1, &c))
1251                 ;
1252         }
1253     }
1254     return scan;
1255 }
1256
1257 ZOOM_API(void)
1258     ZOOM_scanset_destroy(ZOOM_scanset scan)
1259 {
1260     if (!scan)
1261         return;
1262     (scan->refcount)--;
1263     if (scan->refcount == 0)
1264     {
1265         ZOOM_query_destroy(scan->query);
1266
1267         odr_destroy(scan->odr);
1268
1269         ZOOM_options_destroy(scan->options);
1270         xfree(scan);
1271     }
1272 }
1273
1274 static zoom_ret send_package(ZOOM_connection c)
1275 {
1276     ZOOM_Event event;
1277
1278     yaz_log(c->log_details, "%p send_package", c);
1279     if (!c->tasks)
1280         return zoom_complete;
1281     assert (c->tasks->which == ZOOM_TASK_PACKAGE);
1282
1283     event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
1284     ZOOM_connection_put_event(c, event);
1285
1286     c->buf_out = c->tasks->u.package->buf_out;
1287     c->len_out = c->tasks->u.package->len_out;
1288
1289     return ZOOM_send_buf(c);
1290 }
1291
1292 ZOOM_API(size_t)
1293     ZOOM_scanset_size(ZOOM_scanset scan)
1294 {
1295     if (!scan)
1296         return 0;
1297
1298     if (scan->scan_response && scan->scan_response->entries)
1299         return scan->scan_response->entries->num_entries;
1300     else if (scan->srw_scan_response)
1301         return scan->srw_scan_response->num_terms;
1302     return 0;
1303 }
1304
1305 static void ZOOM_scanset_term_x(ZOOM_scanset scan, size_t pos,
1306                                 size_t *occ,
1307                                 const char **value_term, size_t *value_len,
1308                                 const char **disp_term, size_t *disp_len)
1309 {
1310     size_t noent = ZOOM_scanset_size(scan);
1311
1312     *value_term = 0;
1313     *value_len = 0;
1314
1315     *disp_term = 0;
1316     *disp_len = 0;
1317
1318     *occ = 0;
1319     if (pos >= noent)
1320         return;
1321     if (scan->scan_response)
1322     {
1323         Z_ScanResponse *res = scan->scan_response;
1324         if (res->entries->entries[pos]->which == Z_Entry_termInfo)
1325         {
1326             Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
1327
1328             *value_term = (const char *) t->term->u.general->buf;
1329             *value_len = t->term->u.general->len;
1330             if (t->displayTerm)
1331             {
1332                 *disp_term = t->displayTerm;
1333                 *disp_len = strlen(*disp_term);
1334             }
1335             else if (t->term->which == Z_Term_general)
1336             {
1337                 *disp_term = (const char *) t->term->u.general->buf;
1338                 *disp_len = t->term->u.general->len;
1339             }
1340             *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
1341         }
1342     }
1343     if (scan->srw_scan_response)
1344     {
1345         Z_SRW_scanResponse *res = scan->srw_scan_response;
1346         Z_SRW_scanTerm *t = res->terms + pos;
1347         if (t)
1348         {
1349             *value_term = t->value;
1350             *value_len = strlen(*value_term);
1351
1352             if (t->displayTerm)
1353                 *disp_term = t->displayTerm;
1354             else
1355                 *disp_term = t->value;
1356             *disp_len = strlen(*disp_term);
1357             *occ = t->numberOfRecords ? *t->numberOfRecords : 0;
1358         }
1359     }
1360 }
1361
1362 ZOOM_API(const char *)
1363     ZOOM_scanset_term(ZOOM_scanset scan, size_t pos,
1364                       size_t *occ, size_t *len)
1365 {
1366     const char *value_term = 0;
1367     size_t value_len = 0;
1368     const char *disp_term = 0;
1369     size_t disp_len = 0;
1370
1371     ZOOM_scanset_term_x(scan, pos, occ, &value_term, &value_len,
1372                         &disp_term, &disp_len);
1373
1374     *len = value_len;
1375     return value_term;
1376 }
1377
1378 ZOOM_API(const char *)
1379     ZOOM_scanset_display_term(ZOOM_scanset scan, size_t pos,
1380                               size_t *occ, size_t *len)
1381 {
1382     const char *value_term = 0;
1383     size_t value_len = 0;
1384     const char *disp_term = 0;
1385     size_t disp_len = 0;
1386
1387     ZOOM_scanset_term_x(scan, pos, occ, &value_term, &value_len,
1388                         &disp_term, &disp_len);
1389
1390     *len = disp_len;
1391     return disp_term;
1392 }
1393
1394 ZOOM_API(const char *)
1395     ZOOM_scanset_option_get(ZOOM_scanset scan, const char *key)
1396 {
1397     return ZOOM_options_get(scan->options, key);
1398 }
1399
1400 ZOOM_API(void)
1401     ZOOM_scanset_option_set(ZOOM_scanset scan, const char *key,
1402                             const char *val)
1403 {
1404     ZOOM_options_set(scan->options, key, val);
1405 }
1406
1407
1408 ZOOM_API(ZOOM_package)
1409     ZOOM_connection_package(ZOOM_connection c, ZOOM_options options)
1410 {
1411     ZOOM_package p = (ZOOM_package) xmalloc(sizeof(*p));
1412
1413     p->connection = c;
1414     p->odr_out = odr_createmem(ODR_ENCODE);
1415     p->options = ZOOM_options_create_with_parent2(options, c->options);
1416     p->refcount = 1;
1417     p->buf_out = 0;
1418     p->len_out = 0;
1419     return p;
1420 }
1421
1422 ZOOM_API(void)
1423     ZOOM_package_destroy(ZOOM_package p)
1424 {
1425     if (!p)
1426         return;
1427     (p->refcount)--;
1428     if (p->refcount == 0)
1429     {
1430         odr_destroy(p->odr_out);
1431         xfree(p->buf_out);
1432
1433         ZOOM_options_destroy(p->options);
1434         xfree(p);
1435     }
1436 }
1437
1438 ZOOM_API(const char *)
1439     ZOOM_package_option_get(ZOOM_package p, const char *key)
1440 {
1441     return ZOOM_options_get(p->options, key);
1442 }
1443
1444 ZOOM_API(const char *)
1445     ZOOM_package_option_getl(ZOOM_package p, const char *key, int *lenp)
1446 {
1447     return ZOOM_options_getl(p->options, key, lenp);
1448 }
1449
1450 ZOOM_API(void)
1451     ZOOM_package_option_set(ZOOM_package p, const char *key,
1452                             const char *val)
1453 {
1454     ZOOM_options_set(p->options, key, val);
1455 }
1456
1457 ZOOM_API(void)
1458     ZOOM_package_option_setl(ZOOM_package p, const char *key,
1459                              const char *val, int len)
1460 {
1461     ZOOM_options_setl(p->options, key, val, len);
1462 }
1463
1464 ZOOM_API(int)
1465     ZOOM_connection_exec_task(ZOOM_connection c)
1466 {
1467     ZOOM_task task = c->tasks;
1468     zoom_ret ret = zoom_complete;
1469
1470     if (!task)
1471         return 0;
1472     yaz_log(c->log_details, "%p ZOOM_connection_exec_task type=%d run=%d",
1473             c, task->which, task->running);
1474     if (c->error != ZOOM_ERROR_NONE)
1475     {
1476         yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1477                 "removing tasks because of error = %d", c, c->error);
1478         ZOOM_connection_remove_tasks(c);
1479         return 0;
1480     }
1481     if (task->running)
1482     {
1483         yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1484                 "task already running", c);
1485         return 0;
1486     }
1487     task->running = 1;
1488     ret = zoom_complete;
1489     if (c->cs || task->which == ZOOM_TASK_CONNECT)
1490     {
1491         switch (task->which)
1492         {
1493         case ZOOM_TASK_SEARCH:
1494             if (c->proto == PROTO_HTTP)
1495                 ret = ZOOM_connection_srw_send_search(c);
1496             else
1497                 ret = ZOOM_connection_Z3950_send_search(c);
1498             break;
1499         case ZOOM_TASK_RETRIEVE:
1500             if (c->proto == PROTO_HTTP)
1501                 ret = ZOOM_connection_srw_send_search(c);
1502             else
1503                 ret = send_Z3950_present(c);
1504             break;
1505         case ZOOM_TASK_CONNECT:
1506             ret = do_connect(c);
1507             break;
1508         case ZOOM_TASK_SCAN:
1509             if (c->proto == PROTO_HTTP)
1510                 ret = ZOOM_connection_srw_send_scan(c);
1511             else
1512                 ret = ZOOM_connection_Z3950_send_scan(c);
1513             break;
1514         case ZOOM_TASK_PACKAGE:
1515             ret = send_package(c);
1516             break;
1517         case ZOOM_TASK_SORT:
1518             c->tasks->u.sort.resultset->r_sort_spec =
1519                 ZOOM_query_get_sortspec(c->tasks->u.sort.q);
1520             ret = send_Z3950_sort(c, c->tasks->u.sort.resultset);
1521             break;
1522         }
1523     }
1524     else
1525     {
1526         yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1527                 "remove tasks because no connection exist", c);
1528         ZOOM_connection_remove_tasks(c);
1529     }
1530     if (ret == zoom_complete)
1531     {
1532         yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1533                 "task removed (complete)", c);
1534         ZOOM_connection_remove_task(c);
1535         return 0;
1536     }
1537     yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1538             "task pending", c);
1539     return 1;
1540 }
1541
1542 #if YAZ_HAVE_XML2
1543
1544 static zoom_ret send_HTTP_redirect(ZOOM_connection c, const char *uri)
1545 {
1546     Z_GDU *gdu = z_get_HTTP_Request_uri(c->odr_out, uri, 0, c->proxy ? 1 : 0);
1547
1548     gdu->u.HTTP_Request->method = odr_strdup(c->odr_out, "GET");
1549     z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers, "Accept",
1550                       "text/xml");
1551     yaz_cookies_request(c->cookies, c->odr_out, gdu->u.HTTP_Request);
1552     if (c->user && c->password)
1553     {
1554         z_HTTP_header_add_basic_auth(c->odr_out, &gdu->u.HTTP_Request->headers,
1555                                      c->user, c->password);
1556     }
1557     return ZOOM_send_GDU(c, gdu);
1558 }
1559
1560 zoom_ret ZOOM_send_GDU(ZOOM_connection c, Z_GDU *gdu)
1561 {
1562     ZOOM_Event event;
1563
1564     int r = z_GDU(c->odr_out, &gdu, 0, 0);
1565     if (!r)
1566         return zoom_complete;
1567     if (c->odr_print)
1568         z_GDU(c->odr_print, &gdu, 0, 0);
1569     if (c->odr_save)
1570         z_GDU(c->odr_save, &gdu, 0, 0);
1571     c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
1572     odr_reset(c->odr_out);
1573
1574     event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
1575     ZOOM_connection_put_event(c, event);
1576
1577     return ZOOM_send_buf(c);
1578 }
1579
1580 #if YAZ_HAVE_XML2
1581 void ZOOM_set_HTTP_error(ZOOM_connection c, int error,
1582                          const char *addinfo, const char *addinfo2)
1583 {
1584     ZOOM_set_dset_error(c, error, "HTTP", addinfo, addinfo2);
1585 }
1586 #endif
1587
1588
1589 static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres)
1590 {
1591     zoom_ret cret = zoom_complete;
1592     int ret = -1;
1593     char *addinfo = 0;
1594     const char *connection_head = z_HTTP_header_lookup(hres->headers,
1595                                                        "Connection");
1596     const char *location;
1597
1598     ZOOM_connection_set_mask(c, 0);
1599     yaz_log(c->log_details, "%p handle_http", c);
1600
1601     yaz_cookies_response(c->cookies, hres);
1602     if ((hres->code == 301 || hres->code == 302) && c->sru_mode == zoom_sru_get
1603         && (location = z_HTTP_header_lookup(hres->headers, "Location")))
1604     {
1605         c->no_redirects++;
1606         if (c->no_redirects > 10)
1607         {
1608             ZOOM_set_HTTP_error(c, hres->code, 0, 0);
1609             c->no_redirects = 0;
1610             ZOOM_connection_close(c);
1611         }
1612         else
1613         {
1614             /* since redirect may change host we just reconnect. A smarter
1615                implementation might check whether it's the same server */
1616
1617             int host_change = 0;
1618             location = yaz_check_location(c->odr_in, c->host_port,
1619                                           location, &host_change);
1620             if (do_connect_host(c, location) == zoom_complete)
1621                 return;  /* connect failed.. */
1622             send_HTTP_redirect(c, location);
1623             return;
1624         }
1625     }
1626     else
1627     {
1628         ret = ZOOM_handle_sru(c, hres, &cret, &addinfo);
1629         if (ret == 0)
1630         {
1631             if (c->no_redirects) /* end of redirect. change hosts again */
1632                 ZOOM_connection_close(c);
1633         }
1634         c->no_redirects = 0;
1635     }
1636     if (ret)
1637     {
1638         if (hres->code != 200)
1639             ZOOM_set_HTTP_error(c, hres->code, 0, 0);
1640         else
1641         {
1642             yaz_log(YLOG_LOG, "set error... addinfo=%s", addinfo ?
1643                     addinfo : "NULL");
1644             ZOOM_set_error(c, ZOOM_ERROR_DECODE, addinfo);
1645         }
1646         ZOOM_connection_close(c);
1647     }
1648     if (cret == zoom_complete)
1649     {
1650         yaz_log(c->log_details, "removing tasks in handle_http");
1651         ZOOM_connection_remove_task(c);
1652     }
1653     {
1654         int must_close = 0;
1655         if (!strcmp(hres->version, "1.0"))
1656         {
1657             /* HTTP 1.0: only if Keep-Alive we stay alive.. */
1658             if (!connection_head || strcmp(connection_head, "Keep-Alive"))
1659                 must_close = 1;
1660         }
1661         else
1662         {
1663             /* HTTP 1.1: only if no close we stay alive.. */
1664             if (connection_head && !strcmp(connection_head, "close"))
1665                 must_close = 1;
1666         }
1667         if (must_close)
1668         {
1669             ZOOM_connection_close(c);
1670             if (c->tasks)
1671             {
1672                 c->tasks->running = 0;
1673                 ZOOM_connection_insert_task(c, ZOOM_TASK_CONNECT);
1674                 c->reconnect_ok = 0;
1675             }
1676         }
1677         else
1678             c->reconnect_ok = 1; /* if the server closes anyway */
1679     }
1680 }
1681 #endif
1682
1683 static int do_read(ZOOM_connection c)
1684 {
1685     int r, more;
1686     ZOOM_Event event;
1687
1688     event = ZOOM_Event_create(ZOOM_EVENT_RECV_DATA);
1689     ZOOM_connection_put_event(c, event);
1690
1691     r = cs_get(c->cs, &c->buf_in, &c->len_in);
1692     more = cs_more(c->cs);
1693     yaz_log(c->log_details, "%p do_read len=%d more=%d", c, r, more);
1694     if (r == 1)
1695         return 0;
1696     if (r <= 0)
1697     {
1698         if (!ZOOM_test_reconnect(c))
1699         {
1700             ZOOM_set_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
1701             ZOOM_connection_close(c);
1702         }
1703     }
1704     else
1705     {
1706         Z_GDU *gdu;
1707         ZOOM_Event event;
1708
1709         odr_reset(c->odr_in);
1710         odr_setbuf(c->odr_in, c->buf_in, r, 0);
1711         event = ZOOM_Event_create(ZOOM_EVENT_RECV_APDU);
1712         ZOOM_connection_put_event(c, event);
1713
1714         if (!z_GDU(c->odr_in, &gdu, 0, 0))
1715         {
1716             int x;
1717             int err = odr_geterrorx(c->odr_in, &x);
1718             char msg[100];
1719             const char *element = odr_getelement(c->odr_in);
1720             yaz_snprintf(msg, sizeof(msg),
1721                     "ODR code %d:%d element=%s offset=%d",
1722                     err, x, element ? element : "<unknown>",
1723                     odr_offset(c->odr_in));
1724             ZOOM_set_error(c, ZOOM_ERROR_DECODE, msg);
1725             if (c->log_api)
1726             {
1727                 FILE *ber_file = yaz_log_file();
1728                 if (ber_file)
1729                     odr_dumpBER(ber_file, c->buf_in, r);
1730             }
1731             ZOOM_connection_close(c);
1732         }
1733         else
1734         {
1735             if (c->odr_print)
1736                 z_GDU(c->odr_print, &gdu, 0, 0);
1737             if (c->odr_save)
1738                 z_GDU(c->odr_save, &gdu, 0, 0);
1739             if (gdu->which == Z_GDU_Z3950)
1740                 ZOOM_handle_Z3950_apdu(c, gdu->u.z3950);
1741             else if (gdu->which == Z_GDU_HTTP_Response)
1742             {
1743 #if YAZ_HAVE_XML2
1744                 handle_http(c, gdu->u.HTTP_Response);
1745 #else
1746                 ZOOM_set_error(c, ZOOM_ERROR_DECODE, 0);
1747                 ZOOM_connection_close(c);
1748 #endif
1749             }
1750         }
1751     }
1752     return 1;
1753 }
1754
1755 static zoom_ret do_write_ex(ZOOM_connection c, char *buf_out, int len_out)
1756 {
1757     int r;
1758     ZOOM_Event event;
1759
1760     event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA);
1761     ZOOM_connection_put_event(c, event);
1762
1763     yaz_log(c->log_details, "%p do_write_ex len=%d", c, len_out);
1764     if ((r = cs_put(c->cs, buf_out, len_out)) < 0)
1765     {
1766         yaz_log(c->log_details, "%p do_write_ex write failed", c);
1767         if (ZOOM_test_reconnect(c))
1768         {
1769             return zoom_pending;
1770         }
1771         if (c->state == STATE_CONNECTING)
1772             ZOOM_set_error(c, ZOOM_ERROR_CONNECT, c->host_port);
1773         else
1774             ZOOM_set_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
1775         ZOOM_connection_close(c);
1776         return zoom_complete;
1777     }
1778     else if (r == 1)
1779     {
1780         int mask = ZOOM_SELECT_EXCEPT;
1781         if (c->cs->io_pending & CS_WANT_WRITE)
1782             mask += ZOOM_SELECT_WRITE;
1783         if (c->cs->io_pending & CS_WANT_READ)
1784             mask += ZOOM_SELECT_READ;
1785         ZOOM_connection_set_mask(c, mask);
1786         yaz_log(c->log_details, "%p do_write_ex write incomplete mask=%d",
1787                 c, c->mask);
1788     }
1789     else
1790     {
1791         ZOOM_connection_set_mask(c, ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT);
1792         yaz_log(c->log_details, "%p do_write_ex write complete mask=%d",
1793                 c, c->mask);
1794     }
1795     return zoom_pending;
1796 }
1797
1798 zoom_ret ZOOM_send_buf(ZOOM_connection c)
1799 {
1800     return do_write_ex(c, c->buf_out, c->len_out);
1801 }
1802
1803
1804 ZOOM_API(const char *)
1805     ZOOM_connection_option_get(ZOOM_connection c, const char *key)
1806 {
1807     if (!strcmp(key, "APDU"))
1808     {
1809         return c->saveAPDU_wrbuf ? wrbuf_cstr(c->saveAPDU_wrbuf) : "";
1810     }
1811     else
1812         return ZOOM_options_get(c->options, key);
1813 }
1814
1815 ZOOM_API(const char *)
1816     ZOOM_connection_option_getl(ZOOM_connection c, const char *key, int *lenp)
1817 {
1818     if (!strcmp(key, "APDU"))
1819     {
1820         if (c->saveAPDU_wrbuf)
1821         {
1822             *lenp = wrbuf_len(c->saveAPDU_wrbuf);
1823             return wrbuf_cstr(c->saveAPDU_wrbuf);
1824         }
1825         else
1826         {
1827             *lenp = 0;
1828             return "";
1829         }
1830     }
1831     else
1832         return ZOOM_options_getl(c->options, key, lenp);
1833 }
1834
1835 ZOOM_API(void)
1836     ZOOM_connection_option_set(ZOOM_connection c, const char *key,
1837                                const char *val)
1838 {
1839     if (!strcmp(key, "saveAPDU"))
1840     {
1841         if (val && strcmp(val, "0"))
1842         {
1843             if (!c->saveAPDU_wrbuf)
1844                 c->saveAPDU_wrbuf = wrbuf_alloc();
1845             else
1846                 wrbuf_rewind(c->saveAPDU_wrbuf);
1847         }
1848         else
1849         {
1850             wrbuf_destroy(c->saveAPDU_wrbuf);
1851             c->saveAPDU_wrbuf = 0;
1852         }
1853         ZOOM_connection_save_apdu_wrbuf(c, c->saveAPDU_wrbuf);
1854     }
1855     else
1856         ZOOM_options_set(c->options, key, val);
1857 }
1858
1859 ZOOM_API(void)
1860     ZOOM_connection_option_setl(ZOOM_connection c, const char *key,
1861                                 const char *val, int len)
1862 {
1863     ZOOM_options_setl(c->options, key, val, len);
1864 }
1865
1866 ZOOM_API(const char *)
1867     ZOOM_resultset_option_get(ZOOM_resultset r, const char *key)
1868 {
1869     return ZOOM_options_get(r->options, key);
1870 }
1871
1872 ZOOM_API(void)
1873     ZOOM_resultset_option_set(ZOOM_resultset r, const char *key,
1874                               const char *val)
1875 {
1876     ZOOM_options_set(r->options, key, val);
1877 }
1878
1879
1880 ZOOM_API(int)
1881     ZOOM_connection_errcode(ZOOM_connection c)
1882 {
1883     return ZOOM_connection_error(c, 0, 0);
1884 }
1885
1886 ZOOM_API(const char *)
1887     ZOOM_connection_errmsg(ZOOM_connection c)
1888 {
1889     const char *msg;
1890     ZOOM_connection_error(c, &msg, 0);
1891     return msg;
1892 }
1893
1894 ZOOM_API(const char *)
1895     ZOOM_connection_addinfo(ZOOM_connection c)
1896 {
1897     const char *addinfo;
1898     ZOOM_connection_error(c, 0, &addinfo);
1899     return addinfo;
1900 }
1901
1902 ZOOM_API(const char *)
1903     ZOOM_connection_diagset(ZOOM_connection c)
1904 {
1905     const char *diagset;
1906     ZOOM_connection_error_x(c, 0, 0, &diagset);
1907     return diagset;
1908 }
1909
1910 ZOOM_API(const char *)
1911     ZOOM_diag_str(int error)
1912 {
1913     switch (error)
1914     {
1915     case ZOOM_ERROR_NONE:
1916         return "No error";
1917     case ZOOM_ERROR_CONNECT:
1918         return "Connect failed";
1919     case ZOOM_ERROR_MEMORY:
1920         return "Out of memory";
1921     case ZOOM_ERROR_ENCODE:
1922         return "Encoding failed";
1923     case ZOOM_ERROR_DECODE:
1924         return "Decoding failed";
1925     case ZOOM_ERROR_CONNECTION_LOST:
1926         return "Connection lost";
1927     case ZOOM_ERROR_INIT:
1928         return "Init rejected";
1929     case ZOOM_ERROR_INTERNAL:
1930         return "Internal failure";
1931     case ZOOM_ERROR_TIMEOUT:
1932         return "Timeout";
1933     case ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
1934         return "Unsupported protocol";
1935     case ZOOM_ERROR_UNSUPPORTED_QUERY:
1936         return "Unsupported query type";
1937     case ZOOM_ERROR_INVALID_QUERY:
1938         return "Invalid query";
1939     case ZOOM_ERROR_CQL_PARSE:
1940         return "CQL parsing error";
1941     case ZOOM_ERROR_CQL_TRANSFORM:
1942         return "CQL transformation error";
1943     case ZOOM_ERROR_CCL_CONFIG:
1944         return "CCL configuration error";
1945     case ZOOM_ERROR_CCL_PARSE:
1946         return "CCL parsing error";
1947     case ZOOM_ERROR_ES_INVALID_ACTION:
1948         return "Extended Service. invalid action";
1949     case ZOOM_ERROR_ES_INVALID_VERSION:
1950         return "Extended Service. invalid version";
1951     case ZOOM_ERROR_ES_INVALID_SYNTAX:
1952         return "Extended Service. invalid syntax";
1953     case ZOOM_ERROR_MEMCACHED:
1954         return "Memcached";
1955     default:
1956         return diagbib1_str(error);
1957     }
1958 }
1959
1960 ZOOM_API(int)
1961     ZOOM_connection_error_x(ZOOM_connection c, const char **cp,
1962                             const char **addinfo, const char **diagset)
1963 {
1964     int error = c->error;
1965     if (cp)
1966     {
1967         if (!c->diagset || !strcmp(c->diagset, "ZOOM"))
1968             *cp = ZOOM_diag_str(error);
1969         else if (!strcmp(c->diagset, "HTTP"))
1970             *cp = z_HTTP_errmsg(c->error);
1971         else if (!strcmp(c->diagset, "Bib-1"))
1972             *cp = ZOOM_diag_str(error);
1973         else if (!strcmp(c->diagset, "info:srw/diagnostic/1"))
1974             *cp = yaz_diag_srw_str(c->error);
1975         else
1976             *cp = "Unknown error and diagnostic set";
1977     }
1978     if (addinfo)
1979         *addinfo = c->addinfo ? c->addinfo : "";
1980     if (diagset)
1981         *diagset = c->diagset ? c->diagset : "";
1982     return c->error;
1983 }
1984
1985 ZOOM_API(int)
1986     ZOOM_connection_error(ZOOM_connection c, const char **cp,
1987                           const char **addinfo)
1988 {
1989     return ZOOM_connection_error_x(c, cp, addinfo, 0);
1990 }
1991
1992 static void ZOOM_connection_do_io(ZOOM_connection c, int mask)
1993 {
1994     ZOOM_Event event = 0;
1995     int r = cs_look(c->cs);
1996     yaz_log(c->log_details, "%p ZOOM_connection_do_io mask=%d cs_look=%d",
1997             c, mask, r);
1998
1999     if (r == CS_NONE)
2000     {
2001         event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
2002         ZOOM_set_error(c, ZOOM_ERROR_CONNECT, c->host_port);
2003         ZOOM_connection_close(c);
2004         ZOOM_connection_put_event(c, event);
2005     }
2006     else if (r == CS_CONNECT)
2007     {
2008         int ret = ret = cs_rcvconnect(c->cs);
2009         yaz_log(c->log_details, "%p ZOOM_connection_do_io "
2010                 "cs_rcvconnect returned %d", c, ret);
2011         if (ret == 1)
2012         {
2013             int mask = ZOOM_SELECT_EXCEPT;
2014             if (c->cs->io_pending & CS_WANT_WRITE)
2015                 mask += ZOOM_SELECT_WRITE;
2016             if (c->cs->io_pending & CS_WANT_READ)
2017                 mask += ZOOM_SELECT_READ;
2018             ZOOM_connection_set_mask(c, mask);
2019             event = ZOOM_Event_create(ZOOM_EVENT_NONE);
2020             ZOOM_connection_put_event(c, event);
2021         }
2022         else if (ret == 0)
2023         {
2024             event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
2025             ZOOM_connection_put_event(c, event);
2026             get_cert(c);
2027             if (c->proto == PROTO_Z3950)
2028                 ZOOM_connection_Z3950_send_init(c);
2029             else
2030             {
2031                 /* no init request for SRW .. */
2032                 assert(c->tasks->which == ZOOM_TASK_CONNECT);
2033                 ZOOM_connection_remove_task(c);
2034                 ZOOM_connection_set_mask(c, 0);
2035                 ZOOM_connection_exec_task(c);
2036             }
2037             c->state = STATE_ESTABLISHED;
2038         }
2039         else
2040         {
2041             ZOOM_set_error(c, ZOOM_ERROR_CONNECT, c->host_port);
2042             ZOOM_connection_close(c);
2043         }
2044     }
2045     else
2046     {
2047         if (mask & ZOOM_SELECT_EXCEPT)
2048         {
2049             if (!ZOOM_test_reconnect(c))
2050             {
2051                 ZOOM_set_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
2052                 ZOOM_connection_close(c);
2053             }
2054             return;
2055         }
2056         if (mask & ZOOM_SELECT_READ)
2057             do_read(c);
2058         if (c->cs && (mask & ZOOM_SELECT_WRITE))
2059             ZOOM_send_buf(c);
2060     }
2061 }
2062
2063 ZOOM_API(int)
2064     ZOOM_connection_last_event(ZOOM_connection cs)
2065 {
2066     if (!cs)
2067         return ZOOM_EVENT_NONE;
2068     return cs->last_event;
2069 }
2070
2071
2072 ZOOM_API(int) ZOOM_connection_fire_event_timeout(ZOOM_connection c)
2073 {
2074     if (c->mask)
2075     {
2076         ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
2077         /* timeout and this connection was waiting */
2078         ZOOM_set_error(c, ZOOM_ERROR_TIMEOUT, 0);
2079         ZOOM_connection_close(c);
2080         ZOOM_connection_put_event(c, event);
2081     }
2082     return 0;
2083 }
2084
2085 ZOOM_API(int)
2086     ZOOM_connection_process(ZOOM_connection c)
2087 {
2088     ZOOM_Event event;
2089     if (!c)
2090         return 0;
2091
2092     event = ZOOM_connection_get_event(c);
2093     if (event)
2094     {
2095         ZOOM_Event_destroy(event);
2096         return 1;
2097     }
2098     ZOOM_connection_exec_task(c);
2099     event = ZOOM_connection_get_event(c);
2100     if (event)
2101     {
2102         ZOOM_Event_destroy(event);
2103         return 1;
2104     }
2105     return 0;
2106 }
2107
2108 ZOOM_API(int)
2109     ZOOM_event_nonblock(int no, ZOOM_connection *cs)
2110 {
2111     int i;
2112
2113     yaz_log(log_details0, "ZOOM_process_event(no=%d,cs=%p)", no, cs);
2114
2115     for (i = 0; i<no; i++)
2116     {
2117         ZOOM_connection c = cs[i];
2118
2119         if (c && ZOOM_connection_process(c))
2120             return i+1;
2121     }
2122     return 0;
2123 }
2124
2125 ZOOM_API(int) ZOOM_connection_fire_event_socket(ZOOM_connection c, int mask)
2126 {
2127     if (c->mask && mask)
2128         ZOOM_connection_do_io(c, mask);
2129     return 0;
2130 }
2131
2132 ZOOM_API(int) ZOOM_connection_get_socket(ZOOM_connection c)
2133 {
2134     if (c->cs)
2135         return cs_fileno(c->cs);
2136     return -1;
2137 }
2138
2139 ZOOM_API(int) ZOOM_connection_set_mask(ZOOM_connection c, int mask)
2140 {
2141     c->mask = mask;
2142     if (!c->cs)
2143         return -1;
2144     return 0;
2145 }
2146
2147 ZOOM_API(int) ZOOM_connection_get_mask(ZOOM_connection c)
2148 {
2149     if (c->cs)
2150         return c->mask;
2151     return 0;
2152 }
2153
2154 ZOOM_API(int) ZOOM_connection_get_timeout(ZOOM_connection c)
2155 {
2156     return ZOOM_options_get_int(c->options, "timeout", 30);
2157 }
2158
2159 ZOOM_API(void) ZOOM_connection_close(ZOOM_connection c)
2160 {
2161     if (c->cs)
2162         cs_close(c->cs);
2163     c->cs = 0;
2164     ZOOM_connection_set_mask(c, 0);
2165     c->state = STATE_IDLE;
2166 }
2167
2168 /*
2169  * Local variables:
2170  * c-basic-offset: 4
2171  * c-file-style: "Stroustrup"
2172  * indent-tabs-mode: nil
2173  * End:
2174  * vim: shiftwidth=4 tabstop=8 expandtab
2175  */
2176