1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2008 Index Data
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
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
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
20 /** \file connection.c
21 \brief Z39.50 connection (low-level client)
38 #include <sys/socket.h>
42 typedef int socklen_t;
53 #include <yaz/comstack.h>
54 #include <yaz/tcpip.h>
55 #include "connection.h"
61 #include "parameters.h"
64 /** \brief Represents a physical, reusable connection to a remote Z39.50 host
69 ZOOM_resultset resultset;
71 struct client *client;
80 struct connection *next; // next for same host or next in free list
83 static struct connection *connection_freelist = 0;
85 static int connection_connect(struct connection *con);
87 static int connection_is_idle(struct connection *co)
89 ZOOM_connection link = co->link;
92 if (co->state != Conn_Open || !link)
95 if (!ZOOM_connection_is_idle(link))
97 event = ZOOM_connection_peek_event(link);
98 if (event == ZOOM_EVENT_NONE ||
99 event == ZOOM_EVENT_END)
105 ZOOM_connection connection_get_link(struct connection *co)
110 ZOOM_resultset connection_get_resultset(struct connection *co)
112 return co->resultset;
115 void connection_set_resultset(struct connection *co, ZOOM_resultset rs)
118 ZOOM_resultset_destroy(co->resultset);
122 static void remove_connection_from_host(struct connection *con)
124 struct connection **conp = &con->host->connections;
130 *conp = (*conp)->next;
133 conp = &(*conp)->next;
138 // Close connection and recycle structure
139 void connection_destroy(struct connection *co)
143 ZOOM_connection_destroy(co->link);
144 iochan_destroy(co->iochan);
147 ZOOM_resultset_destroy(co->resultset);
149 yaz_log(YLOG_DEBUG, "Connection destroy %s", co->host->hostport);
151 remove_connection_from_host(co);
154 client_disconnect(co->client);
158 co->next = connection_freelist;
159 connection_freelist = co;
162 // Creates a new connection for client, associated with the host of
164 static struct connection *connection_create(struct client *cl)
166 struct connection *new;
167 struct host *host = client_get_host(cl);
169 if ((new = connection_freelist))
170 connection_freelist = new->next;
173 new = xmalloc(sizeof (struct connection));
178 new->next = new->host->connections;
179 new->host->connections = new;
182 client_set_connection(cl, new);
185 new->state = Conn_Resolving;
187 connection_connect(new);
191 static void non_block_events(struct connection *co)
193 struct client *cl = co->client;
194 IOCHAN iochan = co->iochan;
195 ZOOM_connection link = co->link;
199 int r = ZOOM_event_nonblock(1, &link);
202 ev = ZOOM_connection_last_event(link);
204 yaz_log(YLOG_LOG, "ZOOM_EVENT_%s", ZOOM_get_event_str(ev));
210 const char *error, *addinfo;
211 if (ZOOM_connection_error(link, &error, &addinfo))
213 yaz_log(YLOG_LOG, "Error %s from %s",
214 error, client_get_url(cl));
216 client_set_state(cl, Client_Idle);
219 case ZOOM_EVENT_SEND_DATA:
221 case ZOOM_EVENT_RECV_DATA:
223 case ZOOM_EVENT_UNKNOWN:
225 case ZOOM_EVENT_SEND_APDU:
226 client_set_state(co->client, Client_Working);
228 case ZOOM_EVENT_RECV_APDU:
230 case ZOOM_EVENT_CONNECT:
231 yaz_log(YLOG_LOG, "Connected to %s", client_get_url(cl));
232 co->state = Conn_Open;
233 iochan_settimeout(iochan, global_parameters.z3950_session_timeout);
235 case ZOOM_EVENT_RECV_SEARCH:
236 client_search_response(cl);
238 case ZOOM_EVENT_RECV_RECORD:
239 client_record_response(cl);
242 yaz_log(YLOG_LOG, "Unhandled event (%d) from %s",
243 ev, client_get_url(cl));
248 void connection_continue(struct connection *co)
250 non_block_events(co);
253 static void connection_handler(IOCHAN iochan, int event)
255 struct connection *co = iochan_getdata(iochan);
256 struct client *cl = co->client;
257 struct session *se = 0;
260 se = client_get_session(cl);
263 connection_destroy(co);
267 if (event & EVENT_TIMEOUT)
269 if (co->state == Conn_Connecting)
271 yaz_log(YLOG_WARN, "connect timeout %s", client_get_url(cl));
276 yaz_log(YLOG_LOG, "idle timeout %s", client_get_url(cl));
277 connection_destroy(co);
282 non_block_events(co);
284 ZOOM_connection_fire_event_socket(co->link, event);
286 non_block_events(co);
291 // Disassociate connection from client
292 void connection_release(struct connection *co)
294 struct client *cl = co->client;
296 yaz_log(YLOG_DEBUG, "Connection release %s", co->host->hostport);
299 client_set_connection(cl, 0);
303 void connect_resolver_host(struct host *host)
305 struct connection *con = host->connections;
308 if (con->state == Conn_Resolving)
310 if (!host->ipport) /* unresolved */
312 connection_destroy(con);
313 /* start all over .. at some point it will be NULL */
314 con = host->connections;
317 else if (!con->client)
319 connection_destroy(con);
320 /* start all over .. at some point it will be NULL */
321 con = host->connections;
326 connection_connect(con);
327 client_start_search(con->client);
332 yaz_log(YLOG_LOG, "connect_resolver_host: state=%d", con->state);
338 static struct host *connection_get_host(struct connection *con)
343 // Callback for use by event loop
344 // We do this because ZOOM connections don't always have (the same) sockets
345 static int socketfun(IOCHAN c)
347 struct connection *co = iochan_getdata(c);
350 return ZOOM_connection_get_socket(co->link);
353 // Because ZOOM always knows what events it is interested in; we may not
354 static int maskfun(IOCHAN c)
356 struct connection *co = iochan_getdata(c);
360 // This is cheating a little, and assuming that eventl mask IDs are always
361 // the same as ZOOM-C's.
362 return ZOOM_connection_get_mask(co->link);
365 static int connection_connect(struct connection *con)
367 ZOOM_connection link = 0;
368 struct host *host = connection_get_host(con);
369 ZOOM_options zoptions = ZOOM_options_create();
372 const char *sru_version = 0;
373 char ipport[512] = "";
375 struct session_database *sdb = client_get_database(con->client);
376 const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
377 const char *apdulog = session_setting_oneval(sdb, PZ_APDULOG);
379 assert(host->ipport);
382 ZOOM_options_set(zoptions, "async", "1");
383 ZOOM_options_set(zoptions, "implementationName",
384 global_parameters.implementationName);
385 ZOOM_options_set(zoptions, "implementationVersion",
386 global_parameters.implementationVersion);
387 if (zproxy && *zproxy)
389 con->zproxy = xstrdup(zproxy);
390 ZOOM_options_set(zoptions, "proxy", zproxy);
392 if (apdulog && *apdulog)
393 ZOOM_options_set(zoptions, "apdulog", apdulog);
395 if ((auth = session_setting_oneval(sdb, PZ_AUTHENTICATION)))
396 ZOOM_options_set(zoptions, "user", auth);
397 if ((sru = session_setting_oneval(sdb, PZ_SRU)) && *sru)
398 ZOOM_options_set(zoptions, "sru", sru);
399 if ((sru_version = session_setting_oneval(sdb, PZ_SRU_VERSION))
401 ZOOM_options_set(zoptions, "sru_version", sru_version);
403 if (!(link = ZOOM_connection_create(zoptions)))
405 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create ZOOM Connection");
406 ZOOM_options_destroy(zoptions);
411 strcpy(ipport, "http://");
412 strcat(ipport, host->ipport);
413 /* deal with SRU path here because databaseName option is not read in
417 if (*sdb->database->databases[0])
420 strcat(ipport, sdb->database->databases[0]);
423 ZOOM_connection_connect(link, ipport, 0);
426 con->iochan = iochan_create(0, connection_handler, 0);
427 con->state = Conn_Connecting;
428 iochan_settimeout(con->iochan, global_parameters.z3950_connect_timeout);
429 iochan_setdata(con->iochan, con);
430 iochan_setsocketfun(con->iochan, socketfun);
431 iochan_setmaskfun(con->iochan, maskfun);
432 pazpar2_add_channel(con->iochan);
434 /* this fragment is bad DRY: from client_prep_connection */
435 client_set_state(con->client, Client_Connecting);
436 ZOOM_options_destroy(zoptions);
437 // This creates the connection
438 ZOOM_connection_process(link);
442 const char *connection_get_url(struct connection *co)
444 return client_get_url(co->client);
447 // Ensure that client has a connection associated
448 int client_prep_connection(struct client *cl)
450 struct connection *co;
451 struct session *se = client_get_session(cl);
452 struct host *host = client_get_host(cl);
453 struct session_database *sdb = client_get_database(cl);
454 const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
456 if (zproxy && zproxy[0] == '\0')
459 co = client_get_connection(cl);
461 yaz_log(YLOG_DEBUG, "Client prep %s", client_get_url(cl));
465 // See if someone else has an idle connection
466 // We should look at timestamps here to select the longest-idle connection
467 for (co = host->connections; co; co = co->next)
468 if (connection_is_idle(co) &&
469 (!co->client || client_get_session(co->client) != se) &&
470 !strcmp(ZOOM_connection_option_get(co->link, "user"),
471 session_setting_oneval(client_get_database(cl),
474 if (zproxy == 0 && co->zproxy == 0)
476 if (zproxy && co->zproxy && !strcmp(zproxy, co->zproxy))
481 connection_release(co);
482 client_set_connection(cl, co);
484 /* tells ZOOM to reconnect if necessary. Disabled becuase
485 the ZOOM_connection_connect flushes the task queue */
486 ZOOM_connection_connect(co->link, 0, 0);
490 co = connection_create(cl);
503 * indent-tabs-mode: nil
505 * vim: shiftwidth=4 tabstop=8 expandtab