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