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_is_idle(struct connection *co)
87 ZOOM_connection link = co->link;
90 if (co->state != Conn_Open || !link)
93 event = ZOOM_connection_peek_event(link);
94 if (event == ZOOM_EVENT_NONE ||
95 event == ZOOM_EVENT_END)
101 ZOOM_connection connection_get_link(struct connection *co)
106 ZOOM_resultset connection_get_resultset(struct connection *co)
108 return co->resultset;
111 void connection_set_resultset(struct connection *co, ZOOM_resultset rs)
114 ZOOM_resultset_destroy(co->resultset);
118 static void remove_connection_from_host(struct connection *con)
120 struct connection **conp = &con->host->connections;
126 *conp = (*conp)->next;
129 conp = &(*conp)->next;
134 void connection_continue(struct connection *co)
136 yaz_log(YLOG_LOG, "connection_continue");
137 iochan_setevent(co->iochan, EVENT_OUTPUT);
140 // Close connection and recycle structure
141 void connection_destroy(struct connection *co)
145 ZOOM_connection_destroy(co->link);
146 iochan_destroy(co->iochan);
149 ZOOM_resultset_destroy(co->resultset);
151 yaz_log(YLOG_DEBUG, "Connection destroy %s", co->host->hostport);
153 remove_connection_from_host(co);
156 client_disconnect(co->client);
160 co->next = connection_freelist;
161 connection_freelist = co;
164 // Creates a new connection for client, associated with the host of
166 static struct connection *connection_create(struct client *cl)
168 struct connection *new;
169 struct host *host = client_get_host(cl);
171 if ((new = connection_freelist))
172 connection_freelist = new->next;
175 new = xmalloc(sizeof (struct connection));
180 new->next = new->host->connections;
181 new->host->connections = new;
184 client_set_connection(cl, new);
187 new->state = Conn_Resolving;
189 connection_connect(new);
193 static void connection_handler(IOCHAN i, int event)
195 struct connection *co = iochan_getdata(i);
196 struct client *cl = co->client;
197 struct session *se = 0;
200 se = client_get_session(cl);
203 connection_destroy(co);
207 if (event & EVENT_TIMEOUT)
209 if (co->state == Conn_Connecting)
211 yaz_log(YLOG_WARN, "connect timeout %s", client_get_url(cl));
216 yaz_log(YLOG_LOG, "idle timeout %s", client_get_url(cl));
217 connection_destroy(co);
223 ZOOM_connection link = co->link;
225 if (ZOOM_event(1, &link))
229 int event = ZOOM_connection_last_event(link);
234 case ZOOM_EVENT_SEND_DATA:
236 case ZOOM_EVENT_RECV_DATA:
238 case ZOOM_EVENT_UNKNOWN:
240 case ZOOM_EVENT_SEND_APDU:
241 client_set_state(co->client, Client_Working);
243 case ZOOM_EVENT_RECV_APDU:
244 client_set_state(co->client, Client_Idle);
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);
252 case ZOOM_EVENT_RECV_SEARCH:
253 yaz_log(YLOG_LOG, "Search response from %s", client_get_url(cl));
254 client_search_response(cl);
256 case ZOOM_EVENT_RECV_RECORD:
257 yaz_log(YLOG_LOG, "Record from %s", client_get_url(cl));
258 client_record_response(cl);
261 yaz_log(YLOG_LOG, "Unhandled event (%d) from %s",
262 event, client_get_url(cl));
265 while (ZOOM_event_nonblock(1, &link));
271 // Disassociate connection from client
272 void connection_release(struct connection *co)
274 struct client *cl = co->client;
276 yaz_log(YLOG_DEBUG, "Connection release %s", co->host->hostport);
279 client_set_connection(cl, 0);
283 void connect_resolver_host(struct host *host)
285 struct connection *con = host->connections;
288 if (con->state == Conn_Resolving)
290 if (!host->ipport) /* unresolved */
292 connection_destroy(con);
293 /* start all over .. at some point it will be NULL */
294 con = host->connections;
297 else if (!con->client)
299 connection_destroy(con);
300 /* start all over .. at some point it will be NULL */
301 con = host->connections;
306 connection_connect(con);
307 client_start_search(con->client);
312 yaz_log(YLOG_LOG, "connect_resolver_host: state=%d", con->state);
318 static struct host *connection_get_host(struct connection *con)
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)
327 struct connection *co = iochan_getdata(c);
330 return ZOOM_connection_get_socket(co->link);
333 // Because ZOOM always knows what events it is interested in; we may not
334 static int maskfun(IOCHAN c)
336 struct connection *co = iochan_getdata(c);
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);
345 int connection_connect(struct connection *con)
347 ZOOM_connection link = 0;
348 struct host *host = connection_get_host(con);
349 ZOOM_options zoptions = ZOOM_options_create();
352 const char *sru_version = 0;
353 char ipport[512] = "";
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);
359 assert(host->ipport);
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)
369 con->zproxy = xstrdup(zproxy);
370 ZOOM_options_set(zoptions, "proxy", zproxy);
372 if (apdulog && *apdulog)
373 ZOOM_options_set(zoptions, "apdulog", apdulog);
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))
381 ZOOM_options_set(zoptions, "sru_version", sru_version);
383 if (!(link = ZOOM_connection_create(zoptions)))
385 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create ZOOM Connection");
386 ZOOM_options_destroy(zoptions);
391 strcpy(ipport, "http://");
392 strcat(ipport, host->ipport);
393 /* deal with SRU path here because databaseName option is not read in
397 if (*sdb->database->databases[0])
400 strcat(ipport, sdb->database->databases[0]);
403 ZOOM_connection_connect(link, ipport, 0);
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);
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);
422 const char *connection_get_url(struct connection *co)
424 return client_get_url(co->client);
427 // Ensure that client has a connection associated
428 int client_prep_connection(struct client *cl)
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);
436 if (zproxy && zproxy[0] == '\0')
439 co = client_get_connection(cl);
441 yaz_log(YLOG_DEBUG, "Client prep %s", client_get_url(cl));
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),
454 if (zproxy == 0 && co->zproxy == 0)
456 if (zproxy && co->zproxy && !strcmp(zproxy, co->zproxy))
461 connection_release(co);
462 client_set_connection(cl, co);
466 co = connection_create(cl);
478 * indent-tabs-mode: nil
480 * vim: shiftwidth=4 tabstop=8 expandtab