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 // Close connection and recycle structure
112 static void connection_destroy(struct connection *co)
116 ZOOM_connection_destroy(co->link);
117 iochan_destroy(co->iochan);
119 yaz_log(YLOG_DEBUG, "%p Connection destroy %s", co, co->url);
123 client_disconnect(co->client);
132 // Creates a new connection for client, associated with the host of
134 static struct connection *connection_create(struct client *cl,
137 int operation_timeout,
139 iochan_man_t iochan_man)
141 struct connection *co;
143 co = xmalloc(sizeof(*co));
146 co->url = xstrdup(url);
149 co->zproxy = xstrdup(zproxy);
151 client_set_connection(cl, co);
153 co->state = Conn_Closed;
154 co->operation_timeout = operation_timeout;
155 co->session_timeout = session_timeout;
157 connection_connect(co, iochan_man);
163 static void non_block_events(struct connection *co)
166 IOCHAN iochan = co->iochan;
167 ZOOM_connection link = co->link;
170 struct client *cl = co->client;
172 int r = ZOOM_event_nonblock(1, &link);
177 ev = ZOOM_connection_last_event(link);
180 yaz_log(YLOG_DEBUG, "%p Connection ZOOM_EVENT_%s", co, ZOOM_get_event_str(ev));
184 case ZOOM_EVENT_TIMEOUT:
188 const char *error, *addinfo;
190 if ((err = ZOOM_connection_error(link, &error, &addinfo)))
192 yaz_log(YLOG_LOG, "Error %s from %s",
193 error, client_get_id(cl));
194 client_set_diagnostic(cl, err, error, addinfo);
195 client_set_state(cl, Client_Error);
199 iochan_settimeout(iochan, co->session_timeout);
200 client_set_state(cl, Client_Idle);
204 case ZOOM_EVENT_SEND_DATA:
206 case ZOOM_EVENT_RECV_DATA:
208 case ZOOM_EVENT_UNKNOWN:
210 case ZOOM_EVENT_SEND_APDU:
211 client_set_state(co->client, Client_Working);
212 iochan_settimeout(iochan, co->operation_timeout);
214 case ZOOM_EVENT_RECV_APDU:
216 case ZOOM_EVENT_CONNECT:
217 yaz_log(YLOG_LOG, "Connected to %s", client_get_id(cl));
218 co->state = Conn_Open;
220 case ZOOM_EVENT_RECV_SEARCH:
221 client_search_response(cl);
223 case ZOOM_EVENT_RECV_RECORD:
224 client_record_response(cl, &got_records);
226 case ZOOM_EVENT_NONE:
229 yaz_log(YLOG_LOG, "Unhandled event (%d) from %s",
230 ev, client_get_id(cl));
236 struct client *cl = co->client;
239 client_check_preferred_watch(cl);
240 client_got_records(cl);
245 static void iochan_update(struct connection *co)
249 int m = ZOOM_connection_get_mask(co->link);
252 m = ZOOM_SELECT_READ;
253 iochan_setflags(co->iochan, m);
254 iochan_setfd(co->iochan, ZOOM_connection_get_socket(co->link));
258 void connection_continue(struct connection *co)
260 int r = ZOOM_connection_exec_task(co->link);
263 struct client *cl = co->client;
266 non_block_events(co);
273 static void connection_handler(IOCHAN iochan, int event)
275 struct connection *co = iochan_getdata(iochan);
281 /* no client associated with it.. We are probably getting
282 a closed connection from the target.. Or, perhaps, an unexpected
283 package.. We will just close the connection */
284 yaz_log(YLOG_LOG, "timeout connection %p event=%d", co, event);
285 connection_destroy(co);
287 else if (event & EVENT_TIMEOUT)
289 ZOOM_connection_fire_event_timeout(co->link);
291 non_block_events(co);
294 connection_destroy(co);
298 if (ZOOM_connection_is_idle(co->link))
300 connection_destroy(co);
304 non_block_events(co);
306 ZOOM_connection_fire_event_socket(co->link, event);
308 non_block_events(co);
315 static int connection_connect(struct connection *con, iochan_man_t iochan_man)
317 ZOOM_options zoptions = ZOOM_options_create();
321 const char *sru_version = 0;
325 struct session_database *sdb = client_get_database(con->client);
326 const char *apdulog = session_setting_oneval(sdb, PZ_APDULOG);
327 const char *memcached = session_setting_oneval(sdb, PZ_MEMCACHED);
331 ZOOM_options_set(zoptions, "async", "1");
332 ZOOM_options_set(zoptions, "implementationName", PACKAGE_NAME);
333 ZOOM_options_set(zoptions, "implementationVersion", VERSION);
335 if ((charset = session_setting_oneval(sdb, PZ_NEGOTIATION_CHARSET)))
336 ZOOM_options_set(zoptions, "charset", charset);
337 if (memcached && *memcached)
338 ZOOM_options_set(zoptions, "memcached", memcached);
342 yaz_log(YLOG_LOG, "proxy=%s", con->zproxy);
343 ZOOM_options_set(zoptions, "proxy", con->zproxy);
345 if (apdulog && *apdulog)
346 ZOOM_options_set(zoptions, "apdulog", apdulog);
349 if ((sru = session_setting_oneval(sdb, PZ_SRU)) && *sru)
350 ZOOM_options_set(zoptions, "sru", sru);
351 if ((sru_version = session_setting_oneval(sdb, PZ_SRU_VERSION))
353 ZOOM_options_set(zoptions, "sru_version", sru_version);
355 if ((auth = session_setting_oneval(sdb, PZ_AUTHENTICATION)))
357 /* allow splitting user and reset with a blank always */
358 const char *cp1 = strchr(auth, ' ');
359 if (!cp1 && sru && *sru)
360 cp1 = strchr(auth, '/');
363 /* Z39.50 user/password style, or no password for SRU */
364 ZOOM_options_set(zoptions, "user", auth);
368 /* now consider group as well */
369 const char *cp2 = strchr(cp1 + 1, ' ');
371 ZOOM_options_setl(zoptions, "user", auth, cp1 - auth);
373 ZOOM_options_set(zoptions, "password", cp1 + 1);
376 ZOOM_options_setl(zoptions, "group", cp1 + 1, cp2 - cp1 - 1);
377 ZOOM_options_set(zoptions, "password", cp2 + 1);
382 value = session_setting_oneval(sdb, PZ_AUTHENTICATION_MODE);
384 ZOOM_options_set(zoptions, "authenticationMode", value);
386 if (!(con->link = ZOOM_connection_create(zoptions)))
388 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create ZOOM Connection");
389 ZOOM_options_destroy(zoptions);
394 if (sru && *sru && !strstr(con->url, "://"))
395 wrbuf_puts(w, "http://");
396 if (strchr(con->url, '#'))
398 const char *cp = strchr(con->url, '#');
399 wrbuf_write(w, con->url, cp - con->url);
402 wrbuf_puts(w, con->url);
404 ZOOM_connection_connect(con->link, wrbuf_cstr(w), 0);
406 con->iochan = iochan_create(-1, connection_handler, 0, "connection_socket");
407 con->state = Conn_Connecting;
408 iochan_settimeout(con->iochan, con->operation_timeout);
409 iochan_setdata(con->iochan, con);
410 iochan_add(iochan_man, con->iochan);
412 client_set_state(con->client, Client_Connecting);
413 ZOOM_options_destroy(zoptions);
418 // Ensure that client has a connection associated
419 int client_prep_connection(struct client *cl,
420 int operation_timeout, int session_timeout,
421 iochan_man_t iochan_man,
422 const struct timeval *abstime)
424 struct connection *co;
425 struct session_database *sdb = client_get_database(cl);
426 const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
427 const char *url = session_setting_oneval(sdb, PZ_URL);
429 if (zproxy && zproxy[0] == '\0')
433 url = sdb->database->id;
435 yaz_log(YLOG_DEBUG, "client_prep_connection: target=%s url=%s",
436 client_get_id(cl), url);
438 co = client_get_connection(cl);
441 co = connection_create(cl, url, zproxy,
442 operation_timeout, session_timeout,
455 * c-file-style: "Stroustrup"
456 * indent-tabs-mode: nil
458 * vim: shiftwidth=4 tabstop=8 expandtab