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