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