41d6e1cab9e7b9bfa33fd235b3354a3591c42df7
[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, "0;");
782     wrbuf_puts(r->mc_key, c->host_port);
783     wrbuf_puts(r->mc_key, ";");
784     if (c->user)
785         wrbuf_puts(r->mc_key, c->user);
786     wrbuf_puts(r->mc_key, ";");
787     if (c->group)
788         wrbuf_puts(r->mc_key, c->group);
789     wrbuf_puts(r->mc_key, ";");
790     if (c->password)
791         wrbuf_sha1_puts(r->mc_key, c->password, 1);
792     wrbuf_puts(r->mc_key, ";");
793     wrbuf_sha1_puts(r->mc_key, ZOOM_query_get_query_string(q), 1);
794     wrbuf_puts(r->mc_key, ";");
795     /* TODO: add sorting */
796     if (c->mc_st)
797     {
798         size_t v_len;
799         uint32_t flags;
800         memcached_return_t rc;
801         char *v = memcached_get(c->mc_st, wrbuf_buf(r->mc_key),
802                                 wrbuf_len(r->mc_key), &v_len, &flags, &rc);
803         if (v)
804         {
805             ZOOM_Event event;
806             WRBUF w = wrbuf_alloc();
807
808             wrbuf_write(w, v, v_len);
809             free(v);
810             r->size = odr_atoi(wrbuf_cstr(w));
811
812             yaz_log(YLOG_LOG, "For key %s got value %s",
813                     wrbuf_cstr(r->mc_key), wrbuf_cstr(w));
814
815             wrbuf_destroy(w);
816             event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
817             ZOOM_connection_put_event(c, event);
818             return r;
819         }
820         else
821         {
822             yaz_log(YLOG_LOG, "For key %s got NO value", wrbuf_cstr(r->mc_key));
823         }
824     }
825 #endif
826
827     if (c->host_port && c->proto == PROTO_HTTP)
828     {
829         if (!c->cs)
830         {
831             yaz_log(c->log_details, "ZOOM_connection_search: no comstack");
832             ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
833         }
834         else
835         {
836             yaz_log(c->log_details, "ZOOM_connection_search: reconnect");
837             c->reconnect_ok = 1;
838         }
839     }
840
841     task = ZOOM_connection_add_task(c, ZOOM_TASK_SEARCH);
842     task->u.search.resultset = r;
843     task->u.search.start = start;
844     task->u.search.count = count;
845     task->u.search.recv_search_fired = 0;
846
847     syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
848     task->u.search.syntax = syntax ? xstrdup(syntax) : 0;
849     elementSetName = ZOOM_options_get(r->options, "elementSetName");
850     task->u.search.elementSetName = elementSetName ?
851         xstrdup(elementSetName) : 0;
852     schema = ZOOM_options_get(r->options, "schema");
853     task->u.search.schema = schema ? xstrdup(schema) : 0;
854
855     ZOOM_resultset_addref(r);
856
857     if (!c->async)
858     {
859         while (ZOOM_event(1, &c))
860             ;
861     }
862     return r;
863 }
864
865 ZOOM_API(void)
866     ZOOM_resultset_sort(ZOOM_resultset r,
867                          const char *sort_type, const char *sort_spec)
868 {
869     (void) ZOOM_resultset_sort1(r, sort_type, sort_spec);
870 }
871
872 ZOOM_API(int)
873     ZOOM_resultset_sort1(ZOOM_resultset r,
874                          const char *sort_type, const char *sort_spec)
875 {
876     ZOOM_connection c = r->connection;
877     ZOOM_task task;
878     ZOOM_query newq;
879
880     newq = ZOOM_query_create();
881     if (ZOOM_query_sortby(newq, sort_spec) < 0)
882         return -1;
883
884     yaz_log(c->log_api, "%p ZOOM_resultset_sort r=%p sort_type=%s sort_spec=%s",
885             r, r, sort_type, sort_spec);
886     if (!c)
887         return 0;
888
889     if (c->host_port && c->proto == PROTO_HTTP)
890     {
891         if (!c->cs)
892         {
893             yaz_log(c->log_details, "%p ZOOM_resultset_sort: no comstack", r);
894             ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
895         }
896         else
897         {
898             yaz_log(c->log_details, "%p ZOOM_resultset_sort: prepare reconnect",
899                     r);
900             c->reconnect_ok = 1;
901         }
902     }
903
904     ZOOM_resultset_cache_reset(r);
905     task = ZOOM_connection_add_task(c, ZOOM_TASK_SORT);
906     task->u.sort.resultset = r;
907     task->u.sort.q = newq;
908
909     ZOOM_resultset_addref(r);
910
911     if (!c->async)
912     {
913         while (ZOOM_event(1, &c))
914             ;
915     }
916
917     return 0;
918 }
919
920 ZOOM_API(void)
921     ZOOM_resultset_destroy(ZOOM_resultset r)
922 {
923     resultset_destroy(r);
924 }
925
926 static void resultset_destroy(ZOOM_resultset r)
927 {
928     if (!r)
929         return;
930     yaz_mutex_enter(r->mutex);
931     (r->refcount)--;
932     yaz_log(log_details0, "%p ZOOM_resultset_destroy r=%p count=%d",
933             r, r, r->refcount);
934     if (r->refcount == 0)
935     {
936         yaz_mutex_leave(r->mutex);
937
938         yaz_log(log_details0, "%p ZOOM_connection resultset_destroy: Deleting resultset (%p) ", r->connection, r);
939         ZOOM_resultset_cache_reset(r);
940         ZOOM_resultset_release(r);
941         ZOOM_query_destroy(r->query);
942         ZOOM_options_destroy(r->options);
943         odr_destroy(r->odr);
944         xfree(r->setname);
945         yaz_mutex_destroy(&r->mutex);
946 #if SHPTR
947         YAZ_SHPTR_DEC(r->record_wrbuf, wrbuf_destroy);
948 #endif
949         wrbuf_destroy(r->mc_key);
950         resultset_use(-1);
951         xfree(r);
952     }
953     else
954         yaz_mutex_leave(r->mutex);
955 }
956
957 ZOOM_API(size_t)
958     ZOOM_resultset_size(ZOOM_resultset r)
959 {
960     return r->size;
961 }
962
963 int ZOOM_test_reconnect(ZOOM_connection c)
964 {
965     ZOOM_Event event;
966
967     if (!c->reconnect_ok)
968         return 0;
969     ZOOM_connection_close(c);
970     c->reconnect_ok = 0;
971     c->tasks->running = 0;
972     ZOOM_connection_insert_task(c, ZOOM_TASK_CONNECT);
973
974     event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
975     ZOOM_connection_put_event(c, event);
976
977     return 1;
978 }
979
980 static void ZOOM_resultset_retrieve(ZOOM_resultset r,
981                                     int force_sync, int start, int count)
982 {
983     ZOOM_task task;
984     ZOOM_connection c;
985     const char *cp;
986     const char *syntax, *elementSetName;
987
988     if (!r)
989         return;
990     yaz_log(log_details0, "%p ZOOM_resultset_retrieve force_sync=%d start=%d"
991             " count=%d", r, force_sync, start, count);
992     c = r->connection;
993     if (!c)
994         return;
995
996     if (c->host_port && c->proto == PROTO_HTTP)
997     {
998         if (!c->cs)
999         {
1000             yaz_log(log_details0, "%p ZOOM_resultset_retrieve: no comstack", r);
1001             ZOOM_connection_add_task(c, ZOOM_TASK_CONNECT);
1002         }
1003         else
1004         {
1005             yaz_log(log_details0, "%p ZOOM_resultset_retrieve: prepare "
1006                     "reconnect", r);
1007             c->reconnect_ok = 1;
1008         }
1009     }
1010     task = ZOOM_connection_add_task(c, ZOOM_TASK_SEARCH);
1011     task->u.search.resultset = r;
1012     task->u.search.start = start;
1013     task->u.search.count = count;
1014
1015     syntax = ZOOM_options_get(r->options, "preferredRecordSyntax");
1016     task->u.search.syntax = syntax ? xstrdup(syntax) : 0;
1017     elementSetName = ZOOM_options_get(r->options, "elementSetName");
1018     task->u.search.elementSetName = elementSetName
1019         ? xstrdup(elementSetName) : 0;
1020
1021     cp = ZOOM_options_get(r->options, "schema");
1022     task->u.search.schema = cp ? xstrdup(cp) : 0;
1023
1024     ZOOM_resultset_addref(r);
1025
1026     if (!r->connection->async || force_sync)
1027         while (r->connection && ZOOM_event(1, &r->connection))
1028             ;
1029 }
1030
1031 ZOOM_API(void)
1032     ZOOM_resultset_records(ZOOM_resultset r, ZOOM_record *recs,
1033                            size_t start, size_t count)
1034 {
1035     int force_present = 0;
1036
1037     if (!r)
1038         return ;
1039     yaz_log(log_api0, "%p ZOOM_resultset_records r=%p start=%ld count=%ld",
1040             r, r, (long) start, (long) count);
1041     if (count && recs)
1042         force_present = 1;
1043     ZOOM_resultset_retrieve(r, force_present, start, count);
1044     if (force_present)
1045     {
1046         size_t i;
1047         for (i = 0; i< count; i++)
1048             recs[i] = ZOOM_resultset_record_immediate(r, i+start);
1049     }
1050 }
1051
1052 ZOOM_API(size_t)
1053     ZOOM_resultset_facets_size(ZOOM_resultset r) {
1054     return r->num_facets;
1055 }
1056
1057 ZOOM_API(ZOOM_facet_field)
1058     ZOOM_resultset_get_facet_field(ZOOM_resultset r, const char *name) {
1059     int num = r->num_facets;
1060     ZOOM_facet_field *facets = r->facets;
1061     int index;
1062     for (index = 0; index < num; index++) {
1063         if (!strcmp(facets[index]->facet_name, name)) {
1064             return facets[index];
1065         }
1066     }
1067     return 0;
1068 }
1069
1070 ZOOM_API(ZOOM_facet_field)
1071     ZOOM_resultset_get_facet_field_by_index(ZOOM_resultset r, int index) {
1072     int num = r->num_facets;
1073     ZOOM_facet_field *facets = r->facets;
1074     if (index >= 0 && index < num) {
1075         return facets[index];
1076     }
1077     return 0;
1078 }
1079
1080 ZOOM_API(ZOOM_facet_field *)
1081     ZOOM_resultset_facets(ZOOM_resultset r)
1082 {
1083     return r->facets;
1084 }
1085
1086 ZOOM_API(const char**)
1087     ZOOM_resultset_facets_names(ZOOM_resultset r)
1088 {
1089     return (const char **) r->facets_names;
1090 }
1091
1092 ZOOM_API(const char*)
1093     ZOOM_facet_field_name(ZOOM_facet_field field)
1094 {
1095     return field->facet_name;
1096 }
1097
1098 ZOOM_API(size_t)
1099     ZOOM_facet_field_term_count(ZOOM_facet_field field)
1100 {
1101     return field->num_terms;
1102 }
1103
1104 ZOOM_API(const char*)
1105     ZOOM_facet_field_get_term(ZOOM_facet_field field, size_t idx, int *freq) {
1106     *freq = field->facet_terms[idx].frequency;
1107     return field->facet_terms[idx].term;
1108 }
1109
1110
1111 static void get_cert(ZOOM_connection c)
1112 {
1113     char *cert_buf;
1114     int cert_len;
1115
1116     if (cs_get_peer_certificate_x509(c->cs, &cert_buf, &cert_len))
1117     {
1118         ZOOM_connection_option_setl(c, "sslPeerCert",
1119                                     cert_buf, cert_len);
1120         xfree(cert_buf);
1121     }
1122 }
1123
1124 static zoom_ret do_connect_host(ZOOM_connection c,
1125                                 const char *logical_url);
1126
1127 static zoom_ret do_connect(ZOOM_connection c)
1128 {
1129     return do_connect_host(c, c->host_port);
1130 }
1131
1132 static zoom_ret do_connect_host(ZOOM_connection c, const char *logical_url)
1133 {
1134     void *add;
1135
1136     if (c->cs)
1137         cs_close(c->cs);
1138     c->cs = cs_create_host_proxy(logical_url, 0, &add,
1139                                  c->tproxy ? c->tproxy : c->proxy);
1140
1141     if (c->cs && c->cs->protocol == PROTO_HTTP)
1142     {
1143 #if YAZ_HAVE_XML2
1144         c->proto = PROTO_HTTP;
1145 #else
1146         ZOOM_set_error(c, ZOOM_ERROR_UNSUPPORTED_PROTOCOL, "SRW");
1147         ZOOM_connection_close(c);
1148         return zoom_complete;
1149 #endif
1150     }
1151     if (c->cs)
1152     {
1153         int ret = cs_connect(c->cs, add);
1154         if (ret == 0)
1155         {
1156             ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
1157             ZOOM_connection_put_event(c, event);
1158             get_cert(c);
1159             if (c->proto == PROTO_Z3950)
1160                 ZOOM_connection_Z3950_send_init(c);
1161             else
1162             {
1163                 /* no init request for SRW .. */
1164                 assert(c->tasks->which == ZOOM_TASK_CONNECT);
1165                 ZOOM_connection_remove_task(c);
1166                 ZOOM_connection_set_mask(c, 0);
1167                 ZOOM_connection_exec_task(c);
1168             }
1169             c->state = STATE_ESTABLISHED;
1170             return zoom_pending;
1171         }
1172         else if (ret > 0)
1173         {
1174             int mask = ZOOM_SELECT_EXCEPT;
1175             if (c->cs->io_pending & CS_WANT_WRITE)
1176                 mask += ZOOM_SELECT_WRITE;
1177             if (c->cs->io_pending & CS_WANT_READ)
1178                 mask += ZOOM_SELECT_READ;
1179             ZOOM_connection_set_mask(c, mask);
1180             c->state = STATE_CONNECTING;
1181             return zoom_pending;
1182         }
1183     }
1184     c->state = STATE_IDLE;
1185     ZOOM_set_error(c, ZOOM_ERROR_CONNECT, logical_url);
1186     return zoom_complete;
1187 }
1188
1189 /* returns 1 if PDU was sent OK (still pending )
1190    0 if PDU was not sent OK (nothing to wait for)
1191 */
1192
1193 ZOOM_API(ZOOM_record)
1194     ZOOM_resultset_record_immediate(ZOOM_resultset s,size_t pos)
1195 {
1196     const char *syntax =
1197         ZOOM_options_get(s->options, "preferredRecordSyntax");
1198     const char *elementSetName =
1199         ZOOM_options_get(s->options, "elementSetName");
1200     const char *schema =
1201         ZOOM_options_get(s->options, "schema");
1202
1203     return ZOOM_record_cache_lookup(s, pos, syntax, elementSetName, schema);
1204 }
1205
1206 ZOOM_API(ZOOM_record)
1207     ZOOM_resultset_record(ZOOM_resultset r, size_t pos)
1208 {
1209     ZOOM_record rec = ZOOM_resultset_record_immediate(r, pos);
1210
1211     if (!rec)
1212     {
1213         /*
1214          * MIKE: I think force_sync should always be zero, but I don't
1215          * want to make this change until I get the go-ahead from
1216          * Adam, in case something depends on the old synchronous
1217          * behaviour.
1218          */
1219         int force_sync = 1;
1220         if (getenv("ZOOM_RECORD_NO_FORCE_SYNC")) force_sync = 0;
1221         ZOOM_resultset_retrieve(r, force_sync, pos, 1);
1222         rec = ZOOM_resultset_record_immediate(r, pos);
1223     }
1224     return rec;
1225 }
1226
1227 ZOOM_API(ZOOM_scanset)
1228     ZOOM_connection_scan(ZOOM_connection c, const char *start)
1229 {
1230     ZOOM_scanset s;
1231     ZOOM_query q = ZOOM_query_create();
1232
1233     ZOOM_query_prefix(q, start);
1234
1235     s = ZOOM_connection_scan1(c, q);
1236     ZOOM_query_destroy(q);
1237     return s;
1238
1239 }
1240
1241 ZOOM_API(ZOOM_scanset)
1242     ZOOM_connection_scan1(ZOOM_connection c, ZOOM_query q)
1243 {
1244     ZOOM_scanset scan = 0;
1245     Z_Query *z_query = ZOOM_query_get_Z_Query(q);
1246
1247     if (!z_query)
1248         return 0;
1249     scan = (ZOOM_scanset) xmalloc(sizeof(*scan));
1250     scan->connection = c;
1251     scan->odr = odr_createmem(ODR_DECODE);
1252     scan->options = ZOOM_options_create_with_parent(c->options);
1253     scan->refcount = 1;
1254     scan->scan_response = 0;
1255     scan->srw_scan_response = 0;
1256
1257     scan->query = q;
1258     ZOOM_query_addref(q);
1259     scan->databaseNames = ZOOM_connection_get_databases(c, c->options,
1260                                             &scan->num_databaseNames,
1261                                             scan->odr);
1262
1263     if (1)
1264     {
1265         ZOOM_task task = ZOOM_connection_add_task(c, ZOOM_TASK_SCAN);
1266         task->u.scan.scan = scan;
1267
1268         (scan->refcount)++;
1269         if (!c->async)
1270         {
1271             while (ZOOM_event(1, &c))
1272                 ;
1273         }
1274     }
1275     return scan;
1276 }
1277
1278 ZOOM_API(void)
1279     ZOOM_scanset_destroy(ZOOM_scanset scan)
1280 {
1281     if (!scan)
1282         return;
1283     (scan->refcount)--;
1284     if (scan->refcount == 0)
1285     {
1286         ZOOM_query_destroy(scan->query);
1287
1288         odr_destroy(scan->odr);
1289
1290         ZOOM_options_destroy(scan->options);
1291         xfree(scan);
1292     }
1293 }
1294
1295 static zoom_ret send_package(ZOOM_connection c)
1296 {
1297     ZOOM_Event event;
1298
1299     yaz_log(c->log_details, "%p send_package", c);
1300     if (!c->tasks)
1301         return zoom_complete;
1302     assert (c->tasks->which == ZOOM_TASK_PACKAGE);
1303
1304     event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
1305     ZOOM_connection_put_event(c, event);
1306
1307     c->buf_out = c->tasks->u.package->buf_out;
1308     c->len_out = c->tasks->u.package->len_out;
1309
1310     return ZOOM_send_buf(c);
1311 }
1312
1313 ZOOM_API(size_t)
1314     ZOOM_scanset_size(ZOOM_scanset scan)
1315 {
1316     if (!scan)
1317         return 0;
1318
1319     if (scan->scan_response && scan->scan_response->entries)
1320         return scan->scan_response->entries->num_entries;
1321     else if (scan->srw_scan_response)
1322         return scan->srw_scan_response->num_terms;
1323     return 0;
1324 }
1325
1326 static void ZOOM_scanset_term_x(ZOOM_scanset scan, size_t pos,
1327                                 size_t *occ,
1328                                 const char **value_term, size_t *value_len,
1329                                 const char **disp_term, size_t *disp_len)
1330 {
1331     size_t noent = ZOOM_scanset_size(scan);
1332
1333     *value_term = 0;
1334     *value_len = 0;
1335
1336     *disp_term = 0;
1337     *disp_len = 0;
1338
1339     *occ = 0;
1340     if (pos >= noent)
1341         return;
1342     if (scan->scan_response)
1343     {
1344         Z_ScanResponse *res = scan->scan_response;
1345         if (res->entries->entries[pos]->which == Z_Entry_termInfo)
1346         {
1347             Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
1348
1349             *value_term = (const char *) t->term->u.general->buf;
1350             *value_len = t->term->u.general->len;
1351             if (t->displayTerm)
1352             {
1353                 *disp_term = t->displayTerm;
1354                 *disp_len = strlen(*disp_term);
1355             }
1356             else if (t->term->which == Z_Term_general)
1357             {
1358                 *disp_term = (const char *) t->term->u.general->buf;
1359                 *disp_len = t->term->u.general->len;
1360             }
1361             *occ = t->globalOccurrences ? *t->globalOccurrences : 0;
1362         }
1363     }
1364     if (scan->srw_scan_response)
1365     {
1366         Z_SRW_scanResponse *res = scan->srw_scan_response;
1367         Z_SRW_scanTerm *t = res->terms + pos;
1368         if (t)
1369         {
1370             *value_term = t->value;
1371             *value_len = strlen(*value_term);
1372
1373             if (t->displayTerm)
1374                 *disp_term = t->displayTerm;
1375             else
1376                 *disp_term = t->value;
1377             *disp_len = strlen(*disp_term);
1378             *occ = t->numberOfRecords ? *t->numberOfRecords : 0;
1379         }
1380     }
1381 }
1382
1383 ZOOM_API(const char *)
1384     ZOOM_scanset_term(ZOOM_scanset scan, size_t pos,
1385                       size_t *occ, size_t *len)
1386 {
1387     const char *value_term = 0;
1388     size_t value_len = 0;
1389     const char *disp_term = 0;
1390     size_t disp_len = 0;
1391
1392     ZOOM_scanset_term_x(scan, pos, occ, &value_term, &value_len,
1393                         &disp_term, &disp_len);
1394
1395     *len = value_len;
1396     return value_term;
1397 }
1398
1399 ZOOM_API(const char *)
1400     ZOOM_scanset_display_term(ZOOM_scanset scan, size_t pos,
1401                               size_t *occ, size_t *len)
1402 {
1403     const char *value_term = 0;
1404     size_t value_len = 0;
1405     const char *disp_term = 0;
1406     size_t disp_len = 0;
1407
1408     ZOOM_scanset_term_x(scan, pos, occ, &value_term, &value_len,
1409                         &disp_term, &disp_len);
1410
1411     *len = disp_len;
1412     return disp_term;
1413 }
1414
1415 ZOOM_API(const char *)
1416     ZOOM_scanset_option_get(ZOOM_scanset scan, const char *key)
1417 {
1418     return ZOOM_options_get(scan->options, key);
1419 }
1420
1421 ZOOM_API(void)
1422     ZOOM_scanset_option_set(ZOOM_scanset scan, const char *key,
1423                             const char *val)
1424 {
1425     ZOOM_options_set(scan->options, key, val);
1426 }
1427
1428
1429 ZOOM_API(ZOOM_package)
1430     ZOOM_connection_package(ZOOM_connection c, ZOOM_options options)
1431 {
1432     ZOOM_package p = (ZOOM_package) xmalloc(sizeof(*p));
1433
1434     p->connection = c;
1435     p->odr_out = odr_createmem(ODR_ENCODE);
1436     p->options = ZOOM_options_create_with_parent2(options, c->options);
1437     p->refcount = 1;
1438     p->buf_out = 0;
1439     p->len_out = 0;
1440     return p;
1441 }
1442
1443 ZOOM_API(void)
1444     ZOOM_package_destroy(ZOOM_package p)
1445 {
1446     if (!p)
1447         return;
1448     (p->refcount)--;
1449     if (p->refcount == 0)
1450     {
1451         odr_destroy(p->odr_out);
1452         xfree(p->buf_out);
1453
1454         ZOOM_options_destroy(p->options);
1455         xfree(p);
1456     }
1457 }
1458
1459 ZOOM_API(const char *)
1460     ZOOM_package_option_get(ZOOM_package p, const char *key)
1461 {
1462     return ZOOM_options_get(p->options, key);
1463 }
1464
1465 ZOOM_API(const char *)
1466     ZOOM_package_option_getl(ZOOM_package p, const char *key, int *lenp)
1467 {
1468     return ZOOM_options_getl(p->options, key, lenp);
1469 }
1470
1471 ZOOM_API(void)
1472     ZOOM_package_option_set(ZOOM_package p, const char *key,
1473                             const char *val)
1474 {
1475     ZOOM_options_set(p->options, key, val);
1476 }
1477
1478 ZOOM_API(void)
1479     ZOOM_package_option_setl(ZOOM_package p, const char *key,
1480                              const char *val, int len)
1481 {
1482     ZOOM_options_setl(p->options, key, val, len);
1483 }
1484
1485 ZOOM_API(int)
1486     ZOOM_connection_exec_task(ZOOM_connection c)
1487 {
1488     ZOOM_task task = c->tasks;
1489     zoom_ret ret = zoom_complete;
1490
1491     if (!task)
1492         return 0;
1493     yaz_log(c->log_details, "%p ZOOM_connection_exec_task type=%d run=%d",
1494             c, task->which, task->running);
1495     if (c->error != ZOOM_ERROR_NONE)
1496     {
1497         yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1498                 "removing tasks because of error = %d", c, c->error);
1499         ZOOM_connection_remove_tasks(c);
1500         return 0;
1501     }
1502     if (task->running)
1503     {
1504         yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1505                 "task already running", c);
1506         return 0;
1507     }
1508     task->running = 1;
1509     ret = zoom_complete;
1510     if (c->cs || task->which == ZOOM_TASK_CONNECT)
1511     {
1512         switch (task->which)
1513         {
1514         case ZOOM_TASK_SEARCH:
1515             if (c->proto == PROTO_HTTP)
1516                 ret = ZOOM_connection_srw_send_search(c);
1517             else
1518                 ret = ZOOM_connection_Z3950_send_search(c);
1519             break;
1520         case ZOOM_TASK_CONNECT:
1521             ret = do_connect(c);
1522             break;
1523         case ZOOM_TASK_SCAN:
1524             if (c->proto == PROTO_HTTP)
1525                 ret = ZOOM_connection_srw_send_scan(c);
1526             else
1527                 ret = ZOOM_connection_Z3950_send_scan(c);
1528             break;
1529         case ZOOM_TASK_PACKAGE:
1530             ret = send_package(c);
1531             break;
1532         case ZOOM_TASK_SORT:
1533             c->tasks->u.sort.resultset->r_sort_spec =
1534                 ZOOM_query_get_sortspec(c->tasks->u.sort.q);
1535             ret = send_Z3950_sort(c, c->tasks->u.sort.resultset);
1536             break;
1537         }
1538     }
1539     else
1540     {
1541         yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1542                 "remove tasks because no connection exist", c);
1543         ZOOM_connection_remove_tasks(c);
1544     }
1545     if (ret == zoom_complete)
1546     {
1547         yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1548                 "task removed (complete)", c);
1549         ZOOM_connection_remove_task(c);
1550         return 0;
1551     }
1552     yaz_log(c->log_details, "%p ZOOM_connection_exec_task "
1553             "task pending", c);
1554     return 1;
1555 }
1556
1557 #if YAZ_HAVE_XML2
1558
1559 static zoom_ret send_HTTP_redirect(ZOOM_connection c, const char *uri)
1560 {
1561     Z_GDU *gdu = z_get_HTTP_Request_uri(c->odr_out, uri, 0, c->proxy ? 1 : 0);
1562
1563     gdu->u.HTTP_Request->method = odr_strdup(c->odr_out, "GET");
1564     z_HTTP_header_add(c->odr_out, &gdu->u.HTTP_Request->headers, "Accept",
1565                       "text/xml");
1566     yaz_cookies_request(c->cookies, c->odr_out, gdu->u.HTTP_Request);
1567     if (c->user && c->password)
1568     {
1569         z_HTTP_header_add_basic_auth(c->odr_out, &gdu->u.HTTP_Request->headers,
1570                                      c->user, c->password);
1571     }
1572     return ZOOM_send_GDU(c, gdu);
1573 }
1574
1575 zoom_ret ZOOM_send_GDU(ZOOM_connection c, Z_GDU *gdu)
1576 {
1577     ZOOM_Event event;
1578
1579     int r = z_GDU(c->odr_out, &gdu, 0, 0);
1580     if (!r)
1581         return zoom_complete;
1582     if (c->odr_print)
1583         z_GDU(c->odr_print, &gdu, 0, 0);
1584     if (c->odr_save)
1585         z_GDU(c->odr_save, &gdu, 0, 0);
1586     c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
1587     odr_reset(c->odr_out);
1588
1589     event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
1590     ZOOM_connection_put_event(c, event);
1591
1592     return ZOOM_send_buf(c);
1593 }
1594
1595 #if YAZ_HAVE_XML2
1596 void ZOOM_set_HTTP_error(ZOOM_connection c, int error,
1597                          const char *addinfo, const char *addinfo2)
1598 {
1599     ZOOM_set_dset_error(c, error, "HTTP", addinfo, addinfo2);
1600 }
1601 #endif
1602
1603
1604 static void handle_http(ZOOM_connection c, Z_HTTP_Response *hres)
1605 {
1606     zoom_ret cret = zoom_complete;
1607     int ret = -1;
1608     char *addinfo = 0;
1609     const char *connection_head = z_HTTP_header_lookup(hres->headers,
1610                                                        "Connection");
1611     const char *location;
1612
1613     ZOOM_connection_set_mask(c, 0);
1614     yaz_log(c->log_details, "%p handle_http", c);
1615
1616     yaz_cookies_response(c->cookies, hres);
1617     if ((hres->code == 301 || hres->code == 302) && c->sru_mode == zoom_sru_get
1618         && (location = z_HTTP_header_lookup(hres->headers, "Location")))
1619     {
1620         c->no_redirects++;
1621         if (c->no_redirects > 10)
1622         {
1623             ZOOM_set_HTTP_error(c, hres->code, 0, 0);
1624             c->no_redirects = 0;
1625             ZOOM_connection_close(c);
1626         }
1627         else
1628         {
1629             /* since redirect may change host we just reconnect. A smarter
1630                implementation might check whether it's the same server */
1631
1632             int host_change = 0;
1633             location = yaz_check_location(c->odr_in, c->host_port,
1634                                           location, &host_change);
1635             if (do_connect_host(c, location) == zoom_complete)
1636                 return;  /* connect failed.. */
1637             send_HTTP_redirect(c, location);
1638             return;
1639         }
1640     }
1641     else
1642     {
1643         ret = ZOOM_handle_sru(c, hres, &cret, &addinfo);
1644         if (ret == 0)
1645         {
1646             if (c->no_redirects) /* end of redirect. change hosts again */
1647                 ZOOM_connection_close(c);
1648         }
1649         c->no_redirects = 0;
1650     }
1651     if (ret)
1652     {
1653         if (hres->code != 200)
1654             ZOOM_set_HTTP_error(c, hres->code, 0, 0);
1655         else
1656         {
1657             yaz_log(YLOG_LOG, "set error... addinfo=%s", addinfo ?
1658                     addinfo : "NULL");
1659             ZOOM_set_error(c, ZOOM_ERROR_DECODE, addinfo);
1660         }
1661         ZOOM_connection_close(c);
1662     }
1663     if (cret == zoom_complete)
1664     {
1665         yaz_log(c->log_details, "removing tasks in handle_http");
1666         ZOOM_connection_remove_task(c);
1667     }
1668     {
1669         int must_close = 0;
1670         if (!strcmp(hres->version, "1.0"))
1671         {
1672             /* HTTP 1.0: only if Keep-Alive we stay alive.. */
1673             if (!connection_head || strcmp(connection_head, "Keep-Alive"))
1674                 must_close = 1;
1675         }
1676         else
1677         {
1678             /* HTTP 1.1: only if no close we stay alive.. */
1679             if (connection_head && !strcmp(connection_head, "close"))
1680                 must_close = 1;
1681         }
1682         if (must_close)
1683         {
1684             ZOOM_connection_close(c);
1685             if (c->tasks)
1686             {
1687                 c->tasks->running = 0;
1688                 ZOOM_connection_insert_task(c, ZOOM_TASK_CONNECT);
1689                 c->reconnect_ok = 0;
1690             }
1691         }
1692         else
1693             c->reconnect_ok = 1; /* if the server closes anyway */
1694     }
1695 }
1696 #endif
1697
1698 static int do_read(ZOOM_connection c)
1699 {
1700     int r, more;
1701     ZOOM_Event event;
1702
1703     event = ZOOM_Event_create(ZOOM_EVENT_RECV_DATA);
1704     ZOOM_connection_put_event(c, event);
1705
1706     r = cs_get(c->cs, &c->buf_in, &c->len_in);
1707     more = cs_more(c->cs);
1708     yaz_log(c->log_details, "%p do_read len=%d more=%d", c, r, more);
1709     if (r == 1)
1710         return 0;
1711     if (r <= 0)
1712     {
1713         if (!ZOOM_test_reconnect(c))
1714         {
1715             ZOOM_set_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
1716             ZOOM_connection_close(c);
1717         }
1718     }
1719     else
1720     {
1721         Z_GDU *gdu;
1722         ZOOM_Event event;
1723
1724         odr_reset(c->odr_in);
1725         odr_setbuf(c->odr_in, c->buf_in, r, 0);
1726         event = ZOOM_Event_create(ZOOM_EVENT_RECV_APDU);
1727         ZOOM_connection_put_event(c, event);
1728
1729         if (!z_GDU(c->odr_in, &gdu, 0, 0))
1730         {
1731             int x;
1732             int err = odr_geterrorx(c->odr_in, &x);
1733             char msg[100];
1734             const char *element = odr_getelement(c->odr_in);
1735             yaz_snprintf(msg, sizeof(msg),
1736                     "ODR code %d:%d element=%s offset=%d",
1737                     err, x, element ? element : "<unknown>",
1738                     odr_offset(c->odr_in));
1739             ZOOM_set_error(c, ZOOM_ERROR_DECODE, msg);
1740             if (c->log_api)
1741             {
1742                 FILE *ber_file = yaz_log_file();
1743                 if (ber_file)
1744                     odr_dumpBER(ber_file, c->buf_in, r);
1745             }
1746             ZOOM_connection_close(c);
1747         }
1748         else
1749         {
1750             if (c->odr_print)
1751                 z_GDU(c->odr_print, &gdu, 0, 0);
1752             if (c->odr_save)
1753                 z_GDU(c->odr_save, &gdu, 0, 0);
1754             if (gdu->which == Z_GDU_Z3950)
1755                 ZOOM_handle_Z3950_apdu(c, gdu->u.z3950);
1756             else if (gdu->which == Z_GDU_HTTP_Response)
1757             {
1758 #if YAZ_HAVE_XML2
1759                 handle_http(c, gdu->u.HTTP_Response);
1760 #else
1761                 ZOOM_set_error(c, ZOOM_ERROR_DECODE, 0);
1762                 ZOOM_connection_close(c);
1763 #endif
1764             }
1765         }
1766     }
1767     return 1;
1768 }
1769
1770 static zoom_ret do_write_ex(ZOOM_connection c, char *buf_out, int len_out)
1771 {
1772     int r;
1773     ZOOM_Event event;
1774
1775     event = ZOOM_Event_create(ZOOM_EVENT_SEND_DATA);
1776     ZOOM_connection_put_event(c, event);
1777
1778     yaz_log(c->log_details, "%p do_write_ex len=%d", c, len_out);
1779     if ((r = cs_put(c->cs, buf_out, len_out)) < 0)
1780     {
1781         yaz_log(c->log_details, "%p do_write_ex write failed", c);
1782         if (ZOOM_test_reconnect(c))
1783         {
1784             return zoom_pending;
1785         }
1786         if (c->state == STATE_CONNECTING)
1787             ZOOM_set_error(c, ZOOM_ERROR_CONNECT, c->host_port);
1788         else
1789             ZOOM_set_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
1790         ZOOM_connection_close(c);
1791         return zoom_complete;
1792     }
1793     else if (r == 1)
1794     {
1795         int mask = ZOOM_SELECT_EXCEPT;
1796         if (c->cs->io_pending & CS_WANT_WRITE)
1797             mask += ZOOM_SELECT_WRITE;
1798         if (c->cs->io_pending & CS_WANT_READ)
1799             mask += ZOOM_SELECT_READ;
1800         ZOOM_connection_set_mask(c, mask);
1801         yaz_log(c->log_details, "%p do_write_ex write incomplete mask=%d",
1802                 c, c->mask);
1803     }
1804     else
1805     {
1806         ZOOM_connection_set_mask(c, ZOOM_SELECT_READ|ZOOM_SELECT_EXCEPT);
1807         yaz_log(c->log_details, "%p do_write_ex write complete mask=%d",
1808                 c, c->mask);
1809     }
1810     return zoom_pending;
1811 }
1812
1813 zoom_ret ZOOM_send_buf(ZOOM_connection c)
1814 {
1815     return do_write_ex(c, c->buf_out, c->len_out);
1816 }
1817
1818
1819 ZOOM_API(const char *)
1820     ZOOM_connection_option_get(ZOOM_connection c, const char *key)
1821 {
1822     if (!strcmp(key, "APDU"))
1823     {
1824         return c->saveAPDU_wrbuf ? wrbuf_cstr(c->saveAPDU_wrbuf) : "";
1825     }
1826     else
1827         return ZOOM_options_get(c->options, key);
1828 }
1829
1830 ZOOM_API(const char *)
1831     ZOOM_connection_option_getl(ZOOM_connection c, const char *key, int *lenp)
1832 {
1833     if (!strcmp(key, "APDU"))
1834     {
1835         if (c->saveAPDU_wrbuf)
1836         {
1837             *lenp = wrbuf_len(c->saveAPDU_wrbuf);
1838             return wrbuf_cstr(c->saveAPDU_wrbuf);
1839         }
1840         else
1841         {
1842             *lenp = 0;
1843             return "";
1844         }
1845     }
1846     else
1847         return ZOOM_options_getl(c->options, key, lenp);
1848 }
1849
1850 ZOOM_API(void)
1851     ZOOM_connection_option_set(ZOOM_connection c, const char *key,
1852                                const char *val)
1853 {
1854     if (!strcmp(key, "saveAPDU"))
1855     {
1856         if (val && strcmp(val, "0"))
1857         {
1858             if (!c->saveAPDU_wrbuf)
1859                 c->saveAPDU_wrbuf = wrbuf_alloc();
1860             else
1861                 wrbuf_rewind(c->saveAPDU_wrbuf);
1862         }
1863         else
1864         {
1865             wrbuf_destroy(c->saveAPDU_wrbuf);
1866             c->saveAPDU_wrbuf = 0;
1867         }
1868         ZOOM_connection_save_apdu_wrbuf(c, c->saveAPDU_wrbuf);
1869     }
1870     else
1871         ZOOM_options_set(c->options, key, val);
1872 }
1873
1874 ZOOM_API(void)
1875     ZOOM_connection_option_setl(ZOOM_connection c, const char *key,
1876                                 const char *val, int len)
1877 {
1878     ZOOM_options_setl(c->options, key, val, len);
1879 }
1880
1881 ZOOM_API(const char *)
1882     ZOOM_resultset_option_get(ZOOM_resultset r, const char *key)
1883 {
1884     return ZOOM_options_get(r->options, key);
1885 }
1886
1887 ZOOM_API(void)
1888     ZOOM_resultset_option_set(ZOOM_resultset r, const char *key,
1889                               const char *val)
1890 {
1891     ZOOM_options_set(r->options, key, val);
1892 }
1893
1894
1895 ZOOM_API(int)
1896     ZOOM_connection_errcode(ZOOM_connection c)
1897 {
1898     return ZOOM_connection_error(c, 0, 0);
1899 }
1900
1901 ZOOM_API(const char *)
1902     ZOOM_connection_errmsg(ZOOM_connection c)
1903 {
1904     const char *msg;
1905     ZOOM_connection_error(c, &msg, 0);
1906     return msg;
1907 }
1908
1909 ZOOM_API(const char *)
1910     ZOOM_connection_addinfo(ZOOM_connection c)
1911 {
1912     const char *addinfo;
1913     ZOOM_connection_error(c, 0, &addinfo);
1914     return addinfo;
1915 }
1916
1917 ZOOM_API(const char *)
1918     ZOOM_connection_diagset(ZOOM_connection c)
1919 {
1920     const char *diagset;
1921     ZOOM_connection_error_x(c, 0, 0, &diagset);
1922     return diagset;
1923 }
1924
1925 ZOOM_API(const char *)
1926     ZOOM_diag_str(int error)
1927 {
1928     switch (error)
1929     {
1930     case ZOOM_ERROR_NONE:
1931         return "No error";
1932     case ZOOM_ERROR_CONNECT:
1933         return "Connect failed";
1934     case ZOOM_ERROR_MEMORY:
1935         return "Out of memory";
1936     case ZOOM_ERROR_ENCODE:
1937         return "Encoding failed";
1938     case ZOOM_ERROR_DECODE:
1939         return "Decoding failed";
1940     case ZOOM_ERROR_CONNECTION_LOST:
1941         return "Connection lost";
1942     case ZOOM_ERROR_INIT:
1943         return "Init rejected";
1944     case ZOOM_ERROR_INTERNAL:
1945         return "Internal failure";
1946     case ZOOM_ERROR_TIMEOUT:
1947         return "Timeout";
1948     case ZOOM_ERROR_UNSUPPORTED_PROTOCOL:
1949         return "Unsupported protocol";
1950     case ZOOM_ERROR_UNSUPPORTED_QUERY:
1951         return "Unsupported query type";
1952     case ZOOM_ERROR_INVALID_QUERY:
1953         return "Invalid query";
1954     case ZOOM_ERROR_CQL_PARSE:
1955         return "CQL parsing error";
1956     case ZOOM_ERROR_CQL_TRANSFORM:
1957         return "CQL transformation error";
1958     case ZOOM_ERROR_CCL_CONFIG:
1959         return "CCL configuration error";
1960     case ZOOM_ERROR_CCL_PARSE:
1961         return "CCL parsing error";
1962     case ZOOM_ERROR_ES_INVALID_ACTION:
1963         return "Extended Service. invalid action";
1964     case ZOOM_ERROR_ES_INVALID_VERSION:
1965         return "Extended Service. invalid version";
1966     case ZOOM_ERROR_ES_INVALID_SYNTAX:
1967         return "Extended Service. invalid syntax";
1968     case ZOOM_ERROR_MEMCACHED:
1969         return "Memcached";
1970     default:
1971         return diagbib1_str(error);
1972     }
1973 }
1974
1975 ZOOM_API(int)
1976     ZOOM_connection_error_x(ZOOM_connection c, const char **cp,
1977                             const char **addinfo, const char **diagset)
1978 {
1979     int error = c->error;
1980     if (cp)
1981     {
1982         if (!c->diagset || !strcmp(c->diagset, "ZOOM"))
1983             *cp = ZOOM_diag_str(error);
1984         else if (!strcmp(c->diagset, "HTTP"))
1985             *cp = z_HTTP_errmsg(c->error);
1986         else if (!strcmp(c->diagset, "Bib-1"))
1987             *cp = ZOOM_diag_str(error);
1988         else if (!strcmp(c->diagset, "info:srw/diagnostic/1"))
1989             *cp = yaz_diag_srw_str(c->error);
1990         else
1991             *cp = "Unknown error and diagnostic set";
1992     }
1993     if (addinfo)
1994         *addinfo = c->addinfo ? c->addinfo : "";
1995     if (diagset)
1996         *diagset = c->diagset ? c->diagset : "";
1997     return c->error;
1998 }
1999
2000 ZOOM_API(int)
2001     ZOOM_connection_error(ZOOM_connection c, const char **cp,
2002                           const char **addinfo)
2003 {
2004     return ZOOM_connection_error_x(c, cp, addinfo, 0);
2005 }
2006
2007 static void ZOOM_connection_do_io(ZOOM_connection c, int mask)
2008 {
2009     ZOOM_Event event = 0;
2010     int r = cs_look(c->cs);
2011     yaz_log(c->log_details, "%p ZOOM_connection_do_io mask=%d cs_look=%d",
2012             c, mask, r);
2013
2014     if (r == CS_NONE)
2015     {
2016         event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
2017         ZOOM_set_error(c, ZOOM_ERROR_CONNECT, c->host_port);
2018         ZOOM_connection_close(c);
2019         ZOOM_connection_put_event(c, event);
2020     }
2021     else if (r == CS_CONNECT)
2022     {
2023         int ret = ret = cs_rcvconnect(c->cs);
2024         yaz_log(c->log_details, "%p ZOOM_connection_do_io "
2025                 "cs_rcvconnect returned %d", c, ret);
2026         if (ret == 1)
2027         {
2028             int mask = ZOOM_SELECT_EXCEPT;
2029             if (c->cs->io_pending & CS_WANT_WRITE)
2030                 mask += ZOOM_SELECT_WRITE;
2031             if (c->cs->io_pending & CS_WANT_READ)
2032                 mask += ZOOM_SELECT_READ;
2033             ZOOM_connection_set_mask(c, mask);
2034             event = ZOOM_Event_create(ZOOM_EVENT_NONE);
2035             ZOOM_connection_put_event(c, event);
2036         }
2037         else if (ret == 0)
2038         {
2039             event = ZOOM_Event_create(ZOOM_EVENT_CONNECT);
2040             ZOOM_connection_put_event(c, event);
2041             get_cert(c);
2042             if (c->proto == PROTO_Z3950)
2043                 ZOOM_connection_Z3950_send_init(c);
2044             else
2045             {
2046                 /* no init request for SRW .. */
2047                 assert(c->tasks->which == ZOOM_TASK_CONNECT);
2048                 ZOOM_connection_remove_task(c);
2049                 ZOOM_connection_set_mask(c, 0);
2050                 ZOOM_connection_exec_task(c);
2051             }
2052             c->state = STATE_ESTABLISHED;
2053         }
2054         else
2055         {
2056             ZOOM_set_error(c, ZOOM_ERROR_CONNECT, c->host_port);
2057             ZOOM_connection_close(c);
2058         }
2059     }
2060     else
2061     {
2062         if (mask & ZOOM_SELECT_EXCEPT)
2063         {
2064             if (!ZOOM_test_reconnect(c))
2065             {
2066                 ZOOM_set_error(c, ZOOM_ERROR_CONNECTION_LOST, c->host_port);
2067                 ZOOM_connection_close(c);
2068             }
2069             return;
2070         }
2071         if (mask & ZOOM_SELECT_READ)
2072             do_read(c);
2073         if (c->cs && (mask & ZOOM_SELECT_WRITE))
2074             ZOOM_send_buf(c);
2075     }
2076 }
2077
2078 ZOOM_API(int)
2079     ZOOM_connection_last_event(ZOOM_connection cs)
2080 {
2081     if (!cs)
2082         return ZOOM_EVENT_NONE;
2083     return cs->last_event;
2084 }
2085
2086
2087 ZOOM_API(int) ZOOM_connection_fire_event_timeout(ZOOM_connection c)
2088 {
2089     if (c->mask)
2090     {
2091         ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
2092         /* timeout and this connection was waiting */
2093         ZOOM_set_error(c, ZOOM_ERROR_TIMEOUT, 0);
2094         ZOOM_connection_close(c);
2095         ZOOM_connection_put_event(c, event);
2096     }
2097     return 0;
2098 }
2099
2100 ZOOM_API(int)
2101     ZOOM_connection_process(ZOOM_connection c)
2102 {
2103     ZOOM_Event event;
2104     if (!c)
2105         return 0;
2106
2107     event = ZOOM_connection_get_event(c);
2108     if (event)
2109     {
2110         ZOOM_Event_destroy(event);
2111         return 1;
2112     }
2113     ZOOM_connection_exec_task(c);
2114     event = ZOOM_connection_get_event(c);
2115     if (event)
2116     {
2117         ZOOM_Event_destroy(event);
2118         return 1;
2119     }
2120     return 0;
2121 }
2122
2123 ZOOM_API(int)
2124     ZOOM_event_nonblock(int no, ZOOM_connection *cs)
2125 {
2126     int i;
2127
2128     yaz_log(log_details0, "ZOOM_process_event(no=%d,cs=%p)", no, cs);
2129
2130     for (i = 0; i<no; i++)
2131     {
2132         ZOOM_connection c = cs[i];
2133
2134         if (c && ZOOM_connection_process(c))
2135             return i+1;
2136     }
2137     return 0;
2138 }
2139
2140 ZOOM_API(int) ZOOM_connection_fire_event_socket(ZOOM_connection c, int mask)
2141 {
2142     if (c->mask && mask)
2143         ZOOM_connection_do_io(c, mask);
2144     return 0;
2145 }
2146
2147 ZOOM_API(int) ZOOM_connection_get_socket(ZOOM_connection c)
2148 {
2149     if (c->cs)
2150         return cs_fileno(c->cs);
2151     return -1;
2152 }
2153
2154 ZOOM_API(int) ZOOM_connection_set_mask(ZOOM_connection c, int mask)
2155 {
2156     c->mask = mask;
2157     if (!c->cs)
2158         return -1;
2159     return 0;
2160 }
2161
2162 ZOOM_API(int) ZOOM_connection_get_mask(ZOOM_connection c)
2163 {
2164     if (c->cs)
2165         return c->mask;
2166     return 0;
2167 }
2168
2169 ZOOM_API(int) ZOOM_connection_get_timeout(ZOOM_connection c)
2170 {
2171     return ZOOM_options_get_int(c->options, "timeout", 30);
2172 }
2173
2174 ZOOM_API(void) ZOOM_connection_close(ZOOM_connection c)
2175 {
2176     if (c->cs)
2177         cs_close(c->cs);
2178     c->cs = 0;
2179     ZOOM_connection_set_mask(c, 0);
2180     c->state = STATE_IDLE;
2181 }
2182
2183 /*
2184  * Local variables:
2185  * c-basic-offset: 4
2186  * c-file-style: "Stroustrup"
2187  * indent-tabs-mode: nil
2188  * End:
2189  * vim: shiftwidth=4 tabstop=8 expandtab
2190  */
2191