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