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