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