Remove more semi-unused globals
[pazpar2-moved-to-github.git] / src / connection.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2009 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 "eventl.h"
46 #include "pazpar2.h"
47 #include "host.h"
48 #include "client.h"
49 #include "settings.h"
50 #include "parameters.h"
51
52
53 /** \brief Represents a physical, reusable  connection to a remote Z39.50 host
54  */
55 struct connection {
56     IOCHAN iochan;
57     ZOOM_connection link;
58     struct host *host;
59     struct client *client;
60     char *ibuf;
61     int ibufsize;
62     char *zproxy;
63     enum {
64         Conn_Resolving,
65         Conn_Connecting,
66         Conn_Open
67     } state;
68     struct connection *next; // next for same host or next in free list
69 };
70
71 static struct connection *connection_freelist = 0;
72
73 static int connection_connect(struct connection *con);
74
75 static int connection_is_idle(struct connection *co)
76 {
77     ZOOM_connection link = co->link;
78     int event;
79
80     if (co->state != Conn_Open || !link)
81         return 0;
82
83     if (!ZOOM_connection_is_idle(link))
84         return 0;
85     event = ZOOM_connection_peek_event(link);
86     if (event == ZOOM_EVENT_NONE || event == ZOOM_EVENT_END)
87         return 1;
88     else
89         return 0;
90 }
91
92 ZOOM_connection connection_get_link(struct connection *co)
93 {
94     return co->link;
95 }
96
97 static void remove_connection_from_host(struct connection *con)
98 {
99     struct connection **conp = &con->host->connections;
100     assert(con);
101     while (*conp)
102     {
103         if (*conp == con)
104         {
105             *conp = (*conp)->next;
106             return;
107         }
108         conp = &(*conp)->next;
109     }
110     assert(*conp == 0);
111 }
112
113 // Close connection and recycle structure
114 void connection_destroy(struct connection *co)
115 {
116     if (co->link)
117     {
118         ZOOM_connection_destroy(co->link);
119         iochan_destroy(co->iochan);
120     }
121     yaz_log(YLOG_DEBUG, "Connection destroy %s", co->host->hostport);
122
123     remove_connection_from_host(co);
124     if (co->client)
125     {
126         client_disconnect(co->client);
127     }
128     xfree(co->zproxy);
129     co->zproxy = 0;
130     co->next = connection_freelist;
131     connection_freelist = co;
132 }
133
134 // Creates a new connection for client, associated with the host of 
135 // client's database
136 static struct connection *connection_create(struct client *cl)
137 {
138     struct connection *new;
139     struct host *host = client_get_host(cl);
140
141     if ((new = connection_freelist))
142         connection_freelist = new->next;
143     else
144     {
145         new = xmalloc(sizeof (struct connection));
146         new->ibuf = 0;
147         new->ibufsize = 0;
148     }
149     new->host = host;
150     new->next = new->host->connections;
151     new->host->connections = new;
152     new->client = cl;
153     new->zproxy = 0;
154     client_set_connection(cl, new);
155     new->link = 0;
156     new->state = Conn_Resolving;
157     if (host->ipport)
158         connection_connect(new);
159     return new;
160 }
161
162 static void non_block_events(struct connection *co)
163 {
164     struct client *cl = co->client;
165     IOCHAN iochan = co->iochan;
166     ZOOM_connection link = co->link;
167     while (1)
168     {
169         int ev;
170         int r = ZOOM_event_nonblock(1, &link);
171         if (!r)
172             break;
173         ev = ZOOM_connection_last_event(link);
174 #if 0
175         yaz_log(YLOG_LOG, "ZOOM_EVENT_%s", ZOOM_get_event_str(ev));
176 #endif
177         switch (ev) 
178         {
179         case ZOOM_EVENT_END:
180             {
181                 const char *error, *addinfo;
182                 if (ZOOM_connection_error(link, &error, &addinfo))
183                 {
184                     yaz_log(YLOG_LOG, "Error %s from %s",
185                             error, client_get_url(cl));
186                 }
187                 client_set_state(cl, Client_Idle);
188             }
189             break;
190         case ZOOM_EVENT_SEND_DATA:
191             break;
192         case ZOOM_EVENT_RECV_DATA:
193             break;
194         case ZOOM_EVENT_UNKNOWN:
195             break;
196         case ZOOM_EVENT_SEND_APDU:
197             client_set_state(co->client, Client_Working);
198             break;
199         case ZOOM_EVENT_RECV_APDU:
200             break;
201         case ZOOM_EVENT_CONNECT:
202             yaz_log(YLOG_LOG, "Connected to %s", client_get_url(cl));
203             co->state = Conn_Open;
204             iochan_settimeout(iochan, global_parameters.z3950_session_timeout);
205             break;
206         case ZOOM_EVENT_RECV_SEARCH:
207             client_search_response(cl);
208             break;
209         case ZOOM_EVENT_RECV_RECORD:
210             client_record_response(cl);
211             break;
212         default:
213             yaz_log(YLOG_LOG, "Unhandled event (%d) from %s",
214                     ev, client_get_url(cl));
215         }
216     }
217 }
218
219 void connection_continue(struct connection *co)
220 {
221     non_block_events(co);
222 }
223
224 static void connection_handler(IOCHAN iochan, int event)
225 {
226     struct connection *co = iochan_getdata(iochan);
227     struct client *cl = co->client;
228     struct session *se = 0;
229
230     if (cl)
231         se = client_get_session(cl);
232     else
233     {
234         connection_destroy(co);
235         return;
236     }
237
238     if (event & EVENT_TIMEOUT)
239     {
240         if (co->state == Conn_Connecting)
241         {
242             yaz_log(YLOG_WARN,  "connect timeout %s", client_get_url(cl));
243             client_fatal(cl);
244         }
245         else
246         {
247             yaz_log(YLOG_LOG,  "idle timeout %s", client_get_url(cl));
248             connection_destroy(co);
249         }
250     }
251     else
252     {
253         non_block_events(co);
254
255         ZOOM_connection_fire_event_socket(co->link, event);
256         
257         non_block_events(co);
258     }
259 }
260
261
262 // Disassociate connection from client
263 void connection_release(struct connection *co)
264 {
265     struct client *cl = co->client;
266
267     yaz_log(YLOG_DEBUG, "Connection release %s", co->host->hostport);
268     if (!cl)
269         return;
270     client_set_connection(cl, 0);
271     co->client = 0;
272 }
273
274 void connect_resolver_host(struct host *host)
275 {
276     struct connection *con = host->connections;
277     while (con)
278     {
279         if (con->state == Conn_Resolving)
280         {
281             if (!host->ipport) /* unresolved */
282             {
283                 connection_destroy(con);
284                 /* start all over .. at some point it will be NULL */
285                 con = host->connections;
286                 continue;
287             }
288             else if (!con->client)
289             {
290                 connection_destroy(con);
291                 /* start all over .. at some point it will be NULL */
292                 con = host->connections;
293                 continue;
294             }
295             else
296             {
297                 connection_connect(con);
298                 client_start_search(con->client);
299             }
300         }
301         else
302         {
303             yaz_log(YLOG_LOG, "connect_resolver_host: state=%d", con->state);
304         }
305         con = con->next;
306     }
307 }
308
309 static struct host *connection_get_host(struct connection *con)
310 {
311     return con->host;
312 }
313
314 // Callback for use by event loop
315 // We do this because ZOOM connections don't always have (the same) sockets
316 static int socketfun(IOCHAN c)
317 {
318     struct connection *co = iochan_getdata(c);
319     if (!co->link)
320         return -1;
321     return ZOOM_connection_get_socket(co->link);
322 }
323
324 // Because ZOOM always knows what events it is interested in; we may not
325 static int maskfun(IOCHAN c)
326 {
327     struct connection *co = iochan_getdata(c);
328     if (!co->link)
329         return 0;
330
331     // This is cheating a little, and assuming that eventl mask IDs are always
332     // the same as ZOOM-C's.
333     return ZOOM_connection_get_mask(co->link);
334 }
335
336 static int connection_connect(struct connection *con)
337 {
338     ZOOM_connection link = 0;
339     struct host *host = connection_get_host(con);
340     ZOOM_options zoptions = ZOOM_options_create();
341     const char *auth;
342     const char *sru;
343     const char *sru_version = 0;
344     char ipport[512] = "";
345
346     struct session_database *sdb = client_get_database(con->client);
347     const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
348     const char *apdulog = session_setting_oneval(sdb, PZ_APDULOG);
349
350     assert(host->ipport);
351     assert(con);
352
353     ZOOM_options_set(zoptions, "async", "1");
354     ZOOM_options_set(zoptions, "implementationName", PACKAGE_NAME);
355     ZOOM_options_set(zoptions, "implementationVersion", VERSION);
356     if (zproxy && *zproxy)
357     {
358         con->zproxy = xstrdup(zproxy);
359         ZOOM_options_set(zoptions, "proxy", zproxy);
360     }
361     if (apdulog && *apdulog)
362         ZOOM_options_set(zoptions, "apdulog", apdulog);
363
364     if ((auth = session_setting_oneval(sdb, PZ_AUTHENTICATION)))
365         ZOOM_options_set(zoptions, "user", auth);
366     if ((sru = session_setting_oneval(sdb, PZ_SRU)) && *sru)
367         ZOOM_options_set(zoptions, "sru", sru);
368     if ((sru_version = session_setting_oneval(sdb, PZ_SRU_VERSION)) 
369         && *sru_version)
370         ZOOM_options_set(zoptions, "sru_version", sru_version);
371
372     if (!(link = ZOOM_connection_create(zoptions)))
373     {
374         yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create ZOOM Connection");
375         ZOOM_options_destroy(zoptions);
376         return -1;
377     }
378
379     if (sru && *sru)
380         strcpy(ipport, "http://");
381     strcat(ipport, host->ipport);
382
383     ZOOM_connection_connect(link, ipport, 0);
384     
385     con->link = link;
386     con->iochan = iochan_create(0, connection_handler, 0);
387     con->state = Conn_Connecting;
388     iochan_settimeout(con->iochan, global_parameters.z3950_connect_timeout);
389     iochan_setdata(con->iochan, con);
390     iochan_setsocketfun(con->iochan, socketfun);
391     iochan_setmaskfun(con->iochan, maskfun);
392     pazpar2_add_channel(con->iochan);
393
394     /* this fragment is bad DRY: from client_prep_connection */
395     client_set_state(con->client, Client_Connecting);
396     ZOOM_options_destroy(zoptions);
397     // This creates the connection
398     ZOOM_connection_process(link);
399     return 0;
400 }
401
402 const char *connection_get_url(struct connection *co)
403 {
404     return client_get_url(co->client);
405 }
406
407 // Ensure that client has a connection associated
408 int client_prep_connection(struct client *cl)
409 {
410     struct connection *co;
411     struct session *se = client_get_session(cl);
412     struct host *host = client_get_host(cl);
413     struct session_database *sdb = client_get_database(cl);
414     const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
415
416     if (zproxy && zproxy[0] == '\0')
417         zproxy = 0;
418
419     co = client_get_connection(cl);
420
421     yaz_log(YLOG_DEBUG, "Client prep %s", client_get_url(cl));
422
423     if (!co)
424     {
425         // See if someone else has an idle connection
426         // We should look at timestamps here to select the longest-idle connection
427         for (co = host->connections; co; co = co->next)
428             if (connection_is_idle(co) &&
429                 (!co->client || client_get_session(co->client) != se) &&
430                 !strcmp(ZOOM_connection_option_get(co->link, "user"),
431                         session_setting_oneval(client_get_database(cl),
432                                                PZ_AUTHENTICATION)))
433             {
434                 if (zproxy == 0 && co->zproxy == 0)
435                     break;
436                 if (zproxy && co->zproxy && !strcmp(zproxy, co->zproxy))
437                     break;
438             }
439         if (co)
440         {
441             connection_release(co);
442             client_set_connection(cl, co);
443             co->client = cl;
444             /* tells ZOOM to reconnect if necessary. Disabled becuase
445                the ZOOM_connection_connect flushes the task queue */
446             ZOOM_connection_connect(co->link, 0, 0);
447         }
448         else
449         {
450             co = connection_create(cl);
451         }
452     }
453
454     if (co && co->link)
455         return 1;
456     else
457         return 0;
458 }
459
460 /*
461  * Local variables:
462  * c-basic-offset: 4
463  * c-file-style: "Stroustrup"
464  * indent-tabs-mode: nil
465  * End:
466  * vim: shiftwidth=4 tabstop=8 expandtab
467  */
468