1525dfb73d32705fd54073a232f874795e504fe2
[pazpar2-moved-to-github.git] / src / connection.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2010 Index Data
3
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 /** \file connection.c
21     \brief Z39.50 connection (low-level client)
22 */
23
24 #if HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #if HAVE_SYS_TIME_H
32 #include <sys/time.h>
33 #endif
34 #if HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37
38 #include <signal.h>
39 #include <assert.h>
40
41 #include <yaz/log.h>
42 #include <yaz/comstack.h>
43 #include <yaz/tcpip.h>
44 #include "connection.h"
45 #include "session.h"
46 #include "host.h"
47 #include "client.h"
48 #include "settings.h"
49
50
51 /** \brief Represents a physical, reusable  connection to a remote Z39.50 host
52  */
53 struct connection {
54     IOCHAN iochan;
55     ZOOM_connection link;
56     struct host *host;
57     struct client *client;
58     char *zproxy;
59     enum {
60         Conn_Resolving,
61         Conn_Connecting,
62         Conn_Open
63     } state;
64     int operation_timeout;
65     int session_timeout;
66     struct connection *next; // next for same host or next in free list
67 };
68
69 static int connection_connect(struct connection *con, iochan_man_t iochan_man);
70
71 static int connection_is_idle(struct connection *co)
72 {
73     ZOOM_connection link = co->link;
74     int event;
75
76     if (co->state != Conn_Open || !link)
77         return 0;
78
79     if (!ZOOM_connection_is_idle(link))
80         return 0;
81     event = ZOOM_connection_peek_event(link);
82     if (event == ZOOM_EVENT_NONE || event == ZOOM_EVENT_END)
83         return 1;
84     else
85         return 0;
86 }
87
88 ZOOM_connection connection_get_link(struct connection *co)
89 {
90     return co->link;
91 }
92
93 static void remove_connection_from_host(struct connection *con)
94 {
95     struct connection **conp = &con->host->connections;
96     assert(con);
97     yaz_mutex_enter(con->host->mutex);
98     while (*conp)
99     {
100         if (*conp == con)
101         {
102             *conp = (*conp)->next;
103             break;
104         }
105         conp = &(*conp)->next;
106     }
107     yaz_mutex_leave(con->host->mutex);
108 }
109
110 // Close connection and recycle structure
111 void connection_destroy(struct connection *co)
112 {
113     if (co->link)
114     {
115         ZOOM_connection_destroy(co->link);
116         iochan_destroy(co->iochan);
117     }
118     yaz_log(YLOG_DEBUG, "Connection destroy %s", co->host->hostport);
119
120     if (co->client)
121     {
122         client_disconnect(co->client);
123     }
124
125     remove_connection_from_host(co);
126     xfree(co->zproxy);
127     xfree(co);
128 }
129
130 // Creates a new connection for client, associated with the host of 
131 // client's database
132 static struct connection *connection_create(struct client *cl,
133                                             int operation_timeout,
134                                             int session_timeout,
135                                             iochan_man_t iochan_man)
136 {
137     struct connection *new;
138     struct host *host = client_get_host(cl);
139
140     new = xmalloc(sizeof(*new));
141     new->host = host;
142
143     yaz_mutex_enter(host->mutex);
144     new->next = new->host->connections;
145     new->host->connections = new;
146     yaz_mutex_leave(host->mutex);
147
148     new->client = cl;
149     new->zproxy = 0;
150     client_set_connection(cl, new);
151     new->link = 0;
152     new->state = Conn_Resolving;
153     new->operation_timeout = operation_timeout;
154     new->session_timeout = session_timeout;
155     if (host->ipport)
156         connection_connect(new, iochan_man);
157     return new;
158 }
159
160 static void non_block_events(struct connection *co)
161 {
162     int got_records = 0;
163     IOCHAN iochan = co->iochan;
164     ZOOM_connection link = co->link;
165     while (1)
166     {
167         struct client *cl = co->client;
168         int ev;
169         int r = ZOOM_event_nonblock(1, &link);
170         if (!r)
171             break;
172         if (!cl)
173             continue;
174         ev = ZOOM_connection_last_event(link);
175         
176         client_incref(cl);
177 #if 0
178         yaz_log(YLOG_LOG, "ZOOM_EVENT_%s", ZOOM_get_event_str(ev));
179 #endif
180         switch (ev) 
181         {
182         case ZOOM_EVENT_END:
183             {
184                 const char *error, *addinfo;
185                 int err;
186                 if ((err = ZOOM_connection_error(link, &error, &addinfo)))
187                 {
188                     yaz_log(YLOG_LOG, "Error %s from %s",
189                             error, client_get_url(cl));
190                 }
191                 iochan_settimeout(iochan, co->session_timeout);
192                 client_set_diagnostic(cl, err);
193                 client_set_state(cl, Client_Idle);
194             }
195             break;
196         case ZOOM_EVENT_SEND_DATA:
197             break;
198         case ZOOM_EVENT_RECV_DATA:
199             break;
200         case ZOOM_EVENT_UNKNOWN:
201             break;
202         case ZOOM_EVENT_SEND_APDU:
203             client_set_state(co->client, Client_Working);
204             iochan_settimeout(iochan, co->operation_timeout);
205             break;
206         case ZOOM_EVENT_RECV_APDU:
207             break;
208         case ZOOM_EVENT_CONNECT:
209             yaz_log(YLOG_LOG, "Connected to %s", client_get_url(cl));
210             co->state = Conn_Open;
211             break;
212         case ZOOM_EVENT_RECV_SEARCH:
213             client_search_response(cl);
214             break;
215         case ZOOM_EVENT_RECV_RECORD:
216             client_record_response(cl);
217             got_records = 1;
218             break;
219         default:
220             yaz_log(YLOG_LOG, "Unhandled event (%d) from %s",
221                     ev, client_get_url(cl));
222         }
223         client_destroy(cl);
224     }
225     if (got_records)
226     {
227         struct client *cl = co->client;
228         if (cl)
229         {
230             client_incref(cl); 
231             client_got_records(cl);
232             client_destroy(cl);
233         }
234     }
235 }
236
237 void connection_continue(struct connection *co)
238 {
239     non_block_events(co);
240 }
241
242 static void connection_handler(IOCHAN iochan, int event)
243 {
244     struct connection *co = iochan_getdata(iochan);
245     struct client *cl = co->client;
246
247     if (!cl) 
248     {
249         /* no client associated with it.. We are probably getting
250            a closed connection from the target.. Or, perhaps, an unexpected
251            package.. We will just close the connection */
252         connection_destroy(co);
253         return;
254     }
255     if (event & EVENT_TIMEOUT)
256     {
257         if (co->state == Conn_Connecting)
258         {
259             yaz_log(YLOG_WARN,  "connect timeout %s", client_get_url(cl));
260             client_fatal(cl);
261         }
262         else
263         {
264             yaz_log(YLOG_LOG,  "idle timeout %s", client_get_url(cl));
265             connection_destroy(co);
266         }
267     }
268     else
269     {
270         non_block_events(co);
271
272         ZOOM_connection_fire_event_socket(co->link, event);
273         
274         non_block_events(co);
275     }
276 }
277
278
279 // Disassociate connection from client
280 void connection_release(struct connection *co)
281 {
282     struct client *cl = co->client;
283
284     if (!cl)
285         return;
286     client_set_connection(cl, 0);
287     co->client = 0;
288 }
289
290 void connect_resolver_host(struct host *host, iochan_man_t iochan_man)
291 {
292     struct connection *con;
293
294 start:
295     yaz_mutex_enter(host->mutex);
296     con = host->connections;
297     while (con)
298     {
299         if (con->state == Conn_Resolving)
300         {
301             if (!host->ipport) /* unresolved */
302             {
303                 yaz_mutex_leave(host->mutex);
304                 connection_destroy(con);
305                 goto start;
306                 /* start all over .. at some point it will be NULL */
307             }
308             else if (!con->client)
309             {
310                 yaz_mutex_leave(host->mutex);
311                 connection_destroy(con);
312                 /* start all over .. at some point it will be NULL */
313                 goto start;
314             }
315             else
316             {
317                 yaz_mutex_leave(host->mutex);
318                 connection_connect(con, iochan_man);
319                 client_start_search(con->client);
320                 goto start;
321             }
322         }
323         else
324         {
325             yaz_log(YLOG_LOG, "connect_resolver_host: state=%d", con->state);
326             con = con->next;
327         }
328     }
329     yaz_mutex_leave(host->mutex);
330 }
331
332 static struct host *connection_get_host(struct connection *con)
333 {
334     return con->host;
335 }
336
337 // Callback for use by event loop
338 // We do this because ZOOM connections don't always have (the same) sockets
339 static int socketfun(IOCHAN c)
340 {
341     struct connection *co = iochan_getdata(c);
342     if (!co->link)
343         return -1;
344     return ZOOM_connection_get_socket(co->link);
345 }
346
347 // Because ZOOM always knows what events it is interested in; we may not
348 static int maskfun(IOCHAN c)
349 {
350     struct connection *co = iochan_getdata(c);
351     if (!co->link)
352         return 0;
353
354     // This is cheating a little, and assuming that eventl mask IDs are always
355     // the same as ZOOM-C's.
356     return ZOOM_connection_get_mask(co->link);
357 }
358
359 static int connection_connect(struct connection *con, iochan_man_t iochan_man)
360 {
361     ZOOM_connection link = 0;
362     struct host *host = connection_get_host(con);
363     ZOOM_options zoptions = ZOOM_options_create();
364     const char *auth;
365     const char *charset;
366     const char *sru;
367     const char *sru_version = 0;
368
369     struct session_database *sdb = client_get_database(con->client);
370     const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
371     const char *apdulog = session_setting_oneval(sdb, PZ_APDULOG);
372
373     assert(host->ipport);
374     assert(con);
375
376     ZOOM_options_set(zoptions, "async", "1");
377     ZOOM_options_set(zoptions, "implementationName", PACKAGE_NAME);
378     ZOOM_options_set(zoptions, "implementationVersion", VERSION);
379         
380     if ((charset = session_setting_oneval(sdb, PZ_NEGOTIATION_CHARSET)))
381         ZOOM_options_set(zoptions, "charset", charset);
382     
383     if (zproxy && *zproxy)
384     {
385         con->zproxy = xstrdup(zproxy);
386         ZOOM_options_set(zoptions, "proxy", zproxy);
387     }
388     if (apdulog && *apdulog)
389         ZOOM_options_set(zoptions, "apdulog", apdulog);
390
391     if ((auth = session_setting_oneval(sdb, PZ_AUTHENTICATION)))
392         ZOOM_options_set(zoptions, "user", auth);
393     if ((sru = session_setting_oneval(sdb, PZ_SRU)) && *sru)
394         ZOOM_options_set(zoptions, "sru", sru);
395     if ((sru_version = session_setting_oneval(sdb, PZ_SRU_VERSION)) 
396         && *sru_version)
397         ZOOM_options_set(zoptions, "sru_version", sru_version);
398
399     if (!(link = ZOOM_connection_create(zoptions)))
400     {
401         yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create ZOOM Connection");
402         ZOOM_options_destroy(zoptions);
403         return -1;
404     }
405
406     if (sru && *sru)
407     {
408         char http_hostport[512];
409         strcpy(http_hostport, "http://");
410         strcat(http_hostport, host->hostport);
411         ZOOM_connection_connect(link, http_hostport, 0);
412     }
413     else
414         ZOOM_connection_connect(link, host->ipport, 0);
415     
416     con->link = link;
417     con->iochan = iochan_create(0, connection_handler, 0);
418     con->state = Conn_Connecting;
419     iochan_settimeout(con->iochan, con->operation_timeout);
420     iochan_setdata(con->iochan, con);
421     iochan_setsocketfun(con->iochan, socketfun);
422     iochan_setmaskfun(con->iochan, maskfun);
423     iochan_add(iochan_man, con->iochan);
424
425     /* this fragment is bad DRY: from client_prep_connection */
426     client_set_state(con->client, Client_Connecting);
427     ZOOM_options_destroy(zoptions);
428     return 0;
429 }
430
431 const char *connection_get_url(struct connection *co)
432 {
433     return client_get_url(co->client);
434 }
435
436 // Ensure that client has a connection associated
437 int client_prep_connection(struct client *cl,
438                            int operation_timeout, int session_timeout,
439                            iochan_man_t iochan_man)
440 {
441     struct connection *co;
442     struct host *host = client_get_host(cl);
443     struct session_database *sdb = client_get_database(cl);
444     const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
445
446     if (zproxy && zproxy[0] == '\0')
447         zproxy = 0;
448
449     if (!host)
450         return 0;
451
452     co = client_get_connection(cl);
453
454     yaz_log(YLOG_DEBUG, "Client prep %s", client_get_url(cl));
455
456     if (!co)
457     {
458         // See if someone else has an idle connection
459         // We should look at timestamps here to select the longest-idle connection
460         yaz_mutex_enter(host->mutex);
461         for (co = host->connections; co; co = co->next)
462             if (connection_is_idle(co) &&
463                 (!co->client || client_get_state(co->client) == Client_Idle) &&
464                 !strcmp(ZOOM_connection_option_get(co->link, "user"),
465                         session_setting_oneval(client_get_database(cl),
466                                                PZ_AUTHENTICATION)))
467             {
468                 if (zproxy == 0 && co->zproxy == 0)
469                     break;
470                 if (zproxy && co->zproxy && !strcmp(zproxy, co->zproxy))
471                     break;
472             }
473         if (co)
474         {
475             connection_release(co);
476             client_set_connection(cl, co);
477             co->client = cl;
478             yaz_mutex_leave(host->mutex);
479             co->operation_timeout = operation_timeout;
480             co->session_timeout = session_timeout;
481             /* tells ZOOM to reconnect if necessary. Disabled becuase
482                the ZOOM_connection_connect flushes the task queue */
483             ZOOM_connection_connect(co->link, 0, 0);
484         }
485         else
486         {
487             yaz_mutex_leave(host->mutex);
488             co = connection_create(cl, operation_timeout, session_timeout,
489                                    iochan_man);
490         }
491     }
492
493     if (co && co->link)
494         return 1;
495     else
496         return 0;
497 }
498
499 /*
500  * Local variables:
501  * c-basic-offset: 4
502  * c-file-style: "Stroustrup"
503  * indent-tabs-mode: nil
504  * End:
505  * vim: shiftwidth=4 tabstop=8 expandtab
506  */
507