1 /* This file is part of Pazpar2.
2 Copyright (C) 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)
42 #include <yaz/comstack.h>
43 #include <yaz/tcpip.h>
44 #include "connection.h"
49 /* connection counting (1) , disable connection counting (0) */
51 static YAZ_MUTEX g_mutex = 0;
52 static int no_connections = 0;
53 static int total_no_connections = 0;
55 static int connection_use(int delta)
59 yaz_mutex_create(&g_mutex);
60 yaz_mutex_enter(g_mutex);
61 no_connections += delta;
62 result = no_connections;
64 total_no_connections += delta;
65 yaz_mutex_leave(g_mutex);
68 yaz_log(YLOG_LOG, "%s connections=%d", delta > 0 ? "INC" : "DEC",
73 int connections_count(void)
75 return connection_use(0);
80 #define connection_use(x)
81 #define connections_count(x) 0
82 #define connections_count_total(x) 0
86 /** \brief Represents a physical, reusable connection to a remote Z39.50 host
91 struct client *client;
99 int operation_timeout;
101 struct connection *next; // next for same host or next in free list
104 static int connection_connect(struct connection *con, iochan_man_t iochan_man);
106 ZOOM_connection connection_get_link(struct connection *co)
111 void connection_mark_dead(struct connection *co)
113 iochan_settimeout(co->iochan, 1);
116 // Close connection and recycle structure
117 static void connection_destroy(struct connection *co)
121 ZOOM_connection_destroy(co->link);
122 iochan_destroy(co->iochan);
124 yaz_log(YLOG_DEBUG, "%p Connection destroy %s", co, co->url);
128 client_disconnect(co->client);
137 // Creates a new connection for client, associated with the host of
139 static struct connection *connection_create(struct client *cl,
142 int operation_timeout,
144 iochan_man_t iochan_man)
146 struct connection *co;
148 co = xmalloc(sizeof(*co));
151 co->url = xstrdup(url);
154 co->zproxy = xstrdup(zproxy);
156 client_set_connection(cl, co);
158 co->state = Conn_Closed;
159 co->operation_timeout = operation_timeout;
160 co->session_timeout = session_timeout;
162 connection_connect(co, iochan_man);
168 static void non_block_events(struct connection *co)
171 IOCHAN iochan = co->iochan;
172 ZOOM_connection link = co->link;
175 struct client *cl = co->client;
177 int r = ZOOM_event_nonblock(1, &link);
182 ev = ZOOM_connection_last_event(link);
185 yaz_log(YLOG_DEBUG, "%p Connection ZOOM_EVENT_%s", co, ZOOM_get_event_str(ev));
189 case ZOOM_EVENT_TIMEOUT:
193 const char *error, *addinfo;
195 if ((err = ZOOM_connection_error(link, &error, &addinfo)))
197 struct session *se = client_get_session(cl);
199 session_log(se, YLOG_WARN, "%s: Error %s (%s)",
200 client_get_id(cl), error, addinfo);
201 client_set_diagnostic(cl, err, error, addinfo);
202 client_set_state(cl, Client_Error);
206 iochan_settimeout(iochan, co->session_timeout);
207 client_set_state(cl, Client_Idle);
211 case ZOOM_EVENT_SEND_DATA:
213 case ZOOM_EVENT_RECV_DATA:
215 case ZOOM_EVENT_UNKNOWN:
217 case ZOOM_EVENT_SEND_APDU:
218 client_set_state(co->client, Client_Working);
219 iochan_settimeout(iochan, co->operation_timeout);
221 case ZOOM_EVENT_RECV_APDU:
223 case ZOOM_EVENT_CONNECT:
224 co->state = Conn_Open;
226 case ZOOM_EVENT_RECV_SEARCH:
227 client_search_response(cl);
229 case ZOOM_EVENT_RECV_RECORD:
230 client_record_response(cl, &got_records);
232 case ZOOM_EVENT_NONE:
235 yaz_log(YLOG_LOG, "Unhandled event (%d) from %s",
236 ev, client_get_id(cl));
242 struct client *cl = co->client;
245 client_check_preferred_watch(cl);
246 client_got_records(cl);
251 static void iochan_update(struct connection *co)
255 int m = ZOOM_connection_get_mask(co->link);
258 m = ZOOM_SELECT_READ;
259 iochan_setflags(co->iochan, m);
260 iochan_setfd(co->iochan, ZOOM_connection_get_socket(co->link));
264 void connection_continue(struct connection *co)
266 int r = ZOOM_connection_exec_task(co->link);
269 struct client *cl = co->client;
272 non_block_events(co);
279 static void connection_handler(IOCHAN iochan, int event)
281 struct connection *co = iochan_getdata(iochan);
287 /* no client associated with it.. We are probably getting
288 a closed connection from the target.. Or, perhaps, an unexpected
289 package.. We will just close the connection */
290 yaz_log(YLOG_LOG, "timeout connection %p event=%d", co, event);
291 connection_destroy(co);
293 else if (event & EVENT_TIMEOUT)
295 ZOOM_connection_fire_event_timeout(co->link);
297 non_block_events(co);
300 connection_destroy(co);
304 if (ZOOM_connection_is_idle(co->link))
306 connection_destroy(co);
310 non_block_events(co);
312 ZOOM_connection_fire_event_socket(co->link, event);
314 non_block_events(co);
321 static int connection_connect(struct connection *con, iochan_man_t iochan_man)
323 ZOOM_options zoptions = ZOOM_options_create();
327 const char *sru_version = 0;
331 struct session_database *sdb = client_get_database(con->client);
332 const char *apdulog = session_setting_oneval(sdb, PZ_APDULOG);
333 const char *memcached = session_setting_oneval(sdb, PZ_MEMCACHED);
334 const char *redis = session_setting_oneval(sdb, PZ_REDIS);
338 ZOOM_options_set(zoptions, "async", "1");
339 ZOOM_options_set(zoptions, "implementationName", PACKAGE_NAME);
340 ZOOM_options_set(zoptions, "implementationVersion", VERSION);
342 if ((charset = session_setting_oneval(sdb, PZ_NEGOTIATION_CHARSET)))
343 ZOOM_options_set(zoptions, "charset", charset);
344 if (memcached && *memcached)
345 ZOOM_options_set(zoptions, "memcached", memcached);
347 ZOOM_options_set(zoptions, "redis", redis);
351 yaz_log(YLOG_LOG, "proxy=%s", con->zproxy);
352 ZOOM_options_set(zoptions, "proxy", con->zproxy);
354 if (apdulog && *apdulog)
355 ZOOM_options_set(zoptions, "apdulog", apdulog);
358 if ((sru = session_setting_oneval(sdb, PZ_SRU)) && *sru)
359 ZOOM_options_set(zoptions, "sru", sru);
360 if ((sru_version = session_setting_oneval(sdb, PZ_SRU_VERSION))
362 ZOOM_options_set(zoptions, "sru_version", sru_version);
364 if ((auth = session_setting_oneval(sdb, PZ_AUTHENTICATION)))
366 /* allow splitting user and reset with a blank always */
367 const char *cp1 = strchr(auth, ' ');
368 if (!cp1 && sru && *sru)
369 cp1 = strchr(auth, '/');
372 /* Z39.50 user/password style, or no password for SRU */
373 ZOOM_options_set(zoptions, "user", auth);
377 /* now consider group as well */
378 const char *cp2 = strchr(cp1 + 1, ' ');
380 ZOOM_options_setl(zoptions, "user", auth, cp1 - auth);
382 ZOOM_options_set(zoptions, "password", cp1 + 1);
385 ZOOM_options_setl(zoptions, "group", cp1 + 1, cp2 - cp1 - 1);
386 ZOOM_options_set(zoptions, "password", cp2 + 1);
391 value = session_setting_oneval(sdb, PZ_AUTHENTICATION_MODE);
393 ZOOM_options_set(zoptions, "authenticationMode", value);
395 if (!(con->link = ZOOM_connection_create(zoptions)))
397 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create ZOOM Connection");
398 ZOOM_options_destroy(zoptions);
403 if (sru && *sru && !strstr(con->url, "://"))
404 wrbuf_puts(w, "http://");
405 if (strchr(con->url, '#'))
407 const char *cp = strchr(con->url, '#');
408 wrbuf_write(w, con->url, cp - con->url);
411 wrbuf_puts(w, con->url);
413 ZOOM_connection_connect(con->link, wrbuf_cstr(w), 0);
415 con->iochan = iochan_create(-1, connection_handler, 0, "connection_socket");
416 con->state = Conn_Connecting;
417 iochan_settimeout(con->iochan, con->operation_timeout);
418 iochan_setdata(con->iochan, con);
419 iochan_add(iochan_man, con->iochan);
421 client_set_state(con->client, Client_Connecting);
422 ZOOM_options_destroy(zoptions);
427 // Ensure that client has a connection associated
428 int client_prep_connection(struct client *cl,
429 int operation_timeout, int session_timeout,
430 iochan_man_t iochan_man,
431 const struct timeval *abstime)
433 struct connection *co;
434 struct session_database *sdb = client_get_database(cl);
435 const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
436 const char *url = session_setting_oneval(sdb, PZ_URL);
438 if (zproxy && zproxy[0] == '\0')
442 url = sdb->database->id;
444 yaz_log(YLOG_DEBUG, "client_prep_connection: target=%s url=%s",
445 client_get_id(cl), url);
447 co = client_get_connection(cl);
450 co = connection_create(cl, url, zproxy,
451 operation_timeout, session_timeout,
464 * c-file-style: "Stroustrup"
465 * indent-tabs-mode: nil
467 * vim: shiftwidth=4 tabstop=8 expandtab