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"
50 /* connection counting (1) , disable connection counting (0) */
52 static YAZ_MUTEX g_mutex = 0;
53 static int no_connections = 0;
54 static int total_no_connections = 0;
56 static int connection_use(int delta)
60 yaz_mutex_create(&g_mutex);
61 yaz_mutex_enter(g_mutex);
62 no_connections += delta;
63 result = no_connections;
65 total_no_connections += delta;
66 yaz_mutex_leave(g_mutex);
69 yaz_log(YLOG_LOG, "%s connections=%d", delta > 0 ? "INC" : "DEC",
74 int connections_count(void)
76 return connection_use(0);
81 #define connection_use(x)
82 #define connections_count(x) 0
83 #define connections_count_total(x) 0
87 /** \brief Represents a physical, reusable connection to a remote Z39.50 host
93 struct client *client;
101 int operation_timeout;
103 struct connection *next; // next for same host or next in free list
106 static int connection_connect(struct connection *con, iochan_man_t iochan_man);
108 static int connection_is_idle(struct connection *co)
110 ZOOM_connection link = co->link;
113 if (co->state != Conn_Open || !link)
116 if (!ZOOM_connection_is_idle(link))
118 event = ZOOM_connection_peek_event(link);
119 if (event == ZOOM_EVENT_NONE)
125 ZOOM_connection connection_get_link(struct connection *co)
130 static void remove_connection_from_host(struct connection *con)
132 struct connection **conp = &con->host->connections;
138 *conp = (*conp)->next;
141 conp = &(*conp)->next;
143 yaz_cond_broadcast(con->host->cond_ready);
146 // Close connection and recycle structure
147 static void connection_destroy(struct connection *co)
151 ZOOM_connection_destroy(co->link);
152 iochan_destroy(co->iochan);
154 yaz_log(YLOG_DEBUG, "%p Connection destroy %s", co, co->url);
158 client_disconnect(co->client);
167 // Creates a new connection for client, associated with the host of
169 static struct connection *connection_create(struct client *cl,
172 int operation_timeout,
174 iochan_man_t iochan_man)
176 struct connection *co;
178 co = xmalloc(sizeof(*co));
182 co->url = xstrdup(url);
184 client_set_connection(cl, co);
186 co->state = Conn_Closed;
187 co->operation_timeout = operation_timeout;
188 co->session_timeout = session_timeout;
191 connection_connect(co, iochan_man);
193 yaz_mutex_enter(host->mutex);
194 co->next = co->host->connections;
195 co->host->connections = co;
196 yaz_mutex_leave(host->mutex);
202 static void non_block_events(struct connection *co)
205 IOCHAN iochan = co->iochan;
206 ZOOM_connection link = co->link;
209 struct client *cl = co->client;
211 int r = ZOOM_event_nonblock(1, &link);
216 ev = ZOOM_connection_last_event(link);
219 yaz_log(YLOG_DEBUG, "%p Connection ZOOM_EVENT_%s", co, ZOOM_get_event_str(ev));
223 case ZOOM_EVENT_TIMEOUT:
227 const char *error, *addinfo;
229 if ((err = ZOOM_connection_error(link, &error, &addinfo)))
231 struct session *se = client_get_session(cl);
233 session_log(se, YLOG_WARN, "%s: Error %s (%s)",
234 client_get_id(cl), error, addinfo);
235 client_set_diagnostic(cl, err, error, addinfo);
236 client_set_state(cl, Client_Error);
240 iochan_settimeout(iochan, co->session_timeout);
241 client_set_state(cl, Client_Idle);
243 yaz_cond_broadcast(co->host->cond_ready);
246 case ZOOM_EVENT_SEND_DATA:
248 case ZOOM_EVENT_RECV_DATA:
250 case ZOOM_EVENT_UNKNOWN:
252 case ZOOM_EVENT_SEND_APDU:
253 client_set_state(co->client, Client_Working);
254 iochan_settimeout(iochan, co->operation_timeout);
256 case ZOOM_EVENT_RECV_APDU:
258 case ZOOM_EVENT_CONNECT:
259 co->state = Conn_Open;
261 case ZOOM_EVENT_RECV_SEARCH:
262 client_search_response(cl);
264 case ZOOM_EVENT_RECV_RECORD:
265 client_record_response(cl, &got_records);
268 yaz_log(YLOG_LOG, "Unhandled event (%d) from %s",
269 ev, client_get_id(cl));
275 struct client *cl = co->client;
278 client_check_preferred_watch(cl);
279 client_got_records(cl);
284 void connection_continue(struct connection *co)
286 int r = ZOOM_connection_exec_task(co->link);
289 struct client *cl = co->client;
292 non_block_events(co);
297 iochan_setflags(co->iochan, ZOOM_connection_get_mask(co->link));
298 iochan_setfd(co->iochan, ZOOM_connection_get_socket(co->link));
302 static void connection_handler(IOCHAN iochan, int event)
304 struct connection *co = iochan_getdata(iochan);
306 struct host *host = co->host;
308 yaz_mutex_enter(host->mutex);
312 /* no client associated with it.. We are probably getting
313 a closed connection from the target.. Or, perhaps, an unexpected
314 package.. We will just close the connection */
315 yaz_log(YLOG_LOG, "timeout connection %p event=%d", co, event);
316 remove_connection_from_host(co);
317 yaz_mutex_leave(host->mutex);
318 connection_destroy(co);
320 else if (event & EVENT_TIMEOUT)
322 ZOOM_connection_fire_event_timeout(co->link);
324 non_block_events(co);
327 remove_connection_from_host(co);
328 yaz_mutex_leave(host->mutex);
329 connection_destroy(co);
333 yaz_mutex_leave(host->mutex);
336 non_block_events(co);
338 ZOOM_connection_fire_event_socket(co->link, event);
340 non_block_events(co);
345 iochan_setflags(iochan, ZOOM_connection_get_mask(co->link));
346 iochan_setfd(iochan, ZOOM_connection_get_socket(co->link));
352 // Disassociate connection from client
353 static void connection_release(struct connection *co)
355 struct client *cl = co->client;
359 client_set_connection(cl, 0);
363 void connection_release2(struct connection *co)
368 void connect_resolver_host(struct host *host, iochan_man_t iochan_man)
370 struct connection *con;
372 yaz_mutex_enter(host->mutex);
373 con = host->connections;
376 if (con->state == Conn_Closed)
378 struct client *cl = con->client;
379 if (!host->ipport || !cl) /* unresolved or no client */
381 remove_connection_from_host(con);
382 yaz_mutex_leave(host->mutex);
383 connection_destroy(con);
387 struct session_database *sdb = client_get_database(cl);
388 struct session *se = client_get_session(cl);
391 yaz_mutex_leave(host->mutex);
392 client_start_search(cl);
396 remove_connection_from_host(con);
397 yaz_mutex_leave(host->mutex);
398 connection_destroy(con);
401 /* start all over .. at some point it will be NULL */
402 yaz_mutex_enter(host->mutex);
403 con = host->connections;
410 yaz_mutex_leave(host->mutex);
413 static struct host *connection_get_host(struct connection *con)
418 static int connection_connect(struct connection *con, iochan_man_t iochan_man)
420 struct host *host = connection_get_host(con);
421 ZOOM_options zoptions = ZOOM_options_create();
425 const char *sru_version = 0;
429 struct session_database *sdb = client_get_database(con->client);
430 const char *apdulog = session_setting_oneval(sdb, PZ_APDULOG);
431 const char *memcached = session_setting_oneval(sdb, PZ_MEMCACHED);
432 const char *redis = session_setting_oneval(sdb, PZ_REDIS);
436 ZOOM_options_set(zoptions, "async", "1");
437 ZOOM_options_set(zoptions, "implementationName", PACKAGE_NAME);
438 ZOOM_options_set(zoptions, "implementationVersion", VERSION);
440 if ((charset = session_setting_oneval(sdb, PZ_NEGOTIATION_CHARSET)))
441 ZOOM_options_set(zoptions, "charset", charset);
442 if (memcached && *memcached)
443 ZOOM_options_set(zoptions, "memcached", memcached);
445 ZOOM_options_set(zoptions, "redis", redis);
447 assert(host->ipport);
450 yaz_log(YLOG_LOG, "proxy=%s", host->ipport);
451 ZOOM_options_set(zoptions, "proxy", host->ipport);
455 assert(host->tproxy);
456 yaz_log(YLOG_LOG, "tproxy=%s", host->ipport);
457 ZOOM_options_set(zoptions, "tproxy", host->ipport);
460 if (apdulog && *apdulog)
461 ZOOM_options_set(zoptions, "apdulog", apdulog);
464 if ((sru = session_setting_oneval(sdb, PZ_SRU)) && *sru)
465 ZOOM_options_set(zoptions, "sru", sru);
466 if ((sru_version = session_setting_oneval(sdb, PZ_SRU_VERSION))
468 ZOOM_options_set(zoptions, "sru_version", sru_version);
470 if ((auth = session_setting_oneval(sdb, PZ_AUTHENTICATION)))
472 /* allow splitting user and reset with a blank always */
473 const char *cp1 = strchr(auth, ' ');
474 if (!cp1 && sru && *sru)
475 cp1 = strchr(auth, '/');
478 /* Z39.50 user/password style, or no password for SRU */
479 ZOOM_options_set(zoptions, "user", auth);
483 /* now consider group as well */
484 const char *cp2 = strchr(cp1 + 1, ' ');
486 ZOOM_options_setl(zoptions, "user", auth, cp1 - auth);
488 ZOOM_options_set(zoptions, "password", cp1 + 1);
491 ZOOM_options_setl(zoptions, "group", cp1 + 1, cp2 - cp1 - 1);
492 ZOOM_options_set(zoptions, "password", cp2 + 1);
497 value = session_setting_oneval(sdb, PZ_AUTHENTICATION_MODE);
499 ZOOM_options_set(zoptions, "authenticationMode", value);
501 if (!(con->link = ZOOM_connection_create(zoptions)))
503 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create ZOOM Connection");
504 ZOOM_options_destroy(zoptions);
509 if (sru && *sru && !strstr(con->url, "://"))
510 wrbuf_puts(w, "http://");
511 if (strchr(con->url, '#'))
513 const char *cp = strchr(con->url, '#');
514 wrbuf_write(w, con->url, cp - con->url);
517 wrbuf_puts(w, con->url);
519 ZOOM_connection_connect(con->link, wrbuf_cstr(w), 0);
521 con->iochan = iochan_create(-1, connection_handler, 0, "connection_socket");
522 con->state = Conn_Connecting;
523 iochan_settimeout(con->iochan, con->operation_timeout);
524 iochan_setdata(con->iochan, con);
525 iochan_add(iochan_man, con->iochan);
527 client_set_state(con->client, Client_Connecting);
528 ZOOM_options_destroy(zoptions);
533 // Ensure that client has a connection associated
534 int client_prep_connection(struct client *cl,
535 int operation_timeout, int session_timeout,
536 iochan_man_t iochan_man,
537 const struct timeval *abstime)
539 struct connection *co;
540 struct session_database *sdb = client_get_database(cl);
541 const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
542 const char *url = session_setting_oneval(sdb, PZ_URL);
543 const char *sru = session_setting_oneval(sdb, PZ_SRU);
544 struct host *host = 0;
545 int default_port = *sru ? 80 : 210;
547 if (zproxy && zproxy[0] == '\0')
551 url = sdb->database->id;
553 host = find_host(client_get_session(cl)->service->server->database_hosts,
554 url, zproxy, default_port, iochan_man);
556 yaz_log(YLOG_DEBUG, "client_prep_connection: target=%s url=%s",
557 client_get_id(cl), url);
561 co = client_get_connection(cl);
565 if (co->host == host && client_get_state(cl) == Client_Idle)
569 connection_release(co);
574 int max_connections = 0;
575 int reuse_connections = 1;
576 const char *v = session_setting_oneval(client_get_database(cl),
579 max_connections = atoi(v);
581 v = session_setting_oneval(client_get_database(cl),
582 PZ_REUSE_CONNECTIONS);
584 reuse_connections = atoi(v);
586 // See if someone else has an idle connection
587 // We should look at timestamps here to select the longest-idle connection
588 yaz_mutex_enter(host->mutex);
591 int num_connections = 0;
592 for (co = host->connections; co; co = co->next)
594 if (reuse_connections)
596 for (co = host->connections; co; co = co->next)
598 if (connection_is_idle(co) &&
599 !strcmp(url, co->url) &&
600 (!co->client || client_get_state(co->client) == Client_Idle) &&
601 !strcmp(ZOOM_connection_option_get(co->link, "user"),
602 session_setting_oneval(client_get_database(cl),
605 if (zproxy == 0 && co->zproxy == 0)
607 if (zproxy && co->zproxy && !strcmp(zproxy, co->zproxy))
613 yaz_log(YLOG_LOG, "num_connections = %d (reusing)", num_connections);
617 if (max_connections <= 0 || num_connections < max_connections)
619 yaz_log(YLOG_LOG, "num_connections = %d (new); max = %d",
620 num_connections, max_connections);
623 yaz_log(YLOG_LOG, "num_connections = %d (waiting) max = %d",
624 num_connections, max_connections);
625 if (yaz_cond_wait(host->cond_ready, host->mutex, abstime))
627 yaz_log(YLOG_LOG, "out of connections %s", client_get_id(cl));
628 client_set_state(cl, Client_Error);
629 yaz_mutex_leave(host->mutex);
635 yaz_log(YLOG_LOG, "%p Connection reuse. state: %d", co, co->state);
636 connection_release(co);
637 client_set_connection(cl, co);
639 /* ensure that connection is only assigned to this client
640 by marking the client non Idle */
641 client_set_state(cl, Client_Working);
642 yaz_mutex_leave(host->mutex);
643 co->operation_timeout = operation_timeout;
644 co->session_timeout = session_timeout;
645 /* tells ZOOM to reconnect if necessary. Disabled becuase
646 the ZOOM_connection_connect flushes the task queue */
647 ZOOM_connection_connect(co->link, 0, 0);
651 yaz_mutex_leave(host->mutex);
652 co = connection_create(cl, url, host,
653 operation_timeout, session_timeout,
668 * c-file-style: "Stroustrup"
669 * indent-tabs-mode: nil
671 * vim: shiftwidth=4 tabstop=8 expandtab