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)
29 #include <sys/socket.h>
40 #include <yaz/comstack.h>
41 #include <yaz/tcpip.h>
42 #include "connection.h"
48 #include "parameters.h"
51 /** \brief Represents a physical, reusable connection to a remote Z39.50 host
57 struct client *client;
60 char *authentication; // Empty string or authentication string if set
68 struct connection *next; // next for same host or next in free list
71 static struct connection *connection_freelist = 0;
73 static void remove_connection_from_host(struct connection *con)
75 struct connection **conp = &con->host->connections;
81 *conp = (*conp)->next;
84 conp = &(*conp)->next;
89 // Close connection and recycle structure
90 void connection_destroy(struct connection *co)
95 iochan_destroy(co->iochan);
98 yaz_log(YLOG_DEBUG, "Connection destroy %s", co->host->hostport);
100 remove_connection_from_host(co);
103 client_disconnect(co->client);
107 co->next = connection_freelist;
108 connection_freelist = co;
111 // Creates a new connection for client, associated with the host of
113 struct connection *connection_create(struct client *cl)
115 struct connection *new;
116 struct host *host = client_get_host(cl);
118 if ((new = connection_freelist))
119 connection_freelist = new->next;
122 new = xmalloc(sizeof (struct connection));
127 new->next = new->host->connections;
128 new->host->connections = new;
130 new->authentication = "";
132 client_set_connection(cl, new);
134 new->state = Conn_Resolving;
136 connection_connect(new);
140 static void connection_handler(IOCHAN i, int event)
142 struct connection *co = iochan_getdata(i);
143 struct client *cl = co->client;
144 struct session *se = 0;
147 se = client_get_session(cl);
150 connection_destroy(co);
154 if (event & EVENT_TIMEOUT)
156 if (co->state == Conn_Connecting)
158 yaz_log(YLOG_WARN, "connect timeout %s", client_get_url(cl));
163 yaz_log(YLOG_LOG, "idle timeout %s", client_get_url(cl));
164 connection_destroy(co);
168 if (co->state == Conn_Connecting && event & EVENT_OUTPUT)
171 socklen_t errlen = sizeof(errcode);
173 if (getsockopt(cs_fileno(co->link), SOL_SOCKET, SO_ERROR, &errcode,
174 &errlen) < 0 || errcode != 0)
181 yaz_log(YLOG_DEBUG, "Connect OK");
182 co->state = Conn_Open;
184 client_set_state(cl, Client_Connected);
185 iochan_settimeout(i, global_parameters.z3950_session_timeout);
189 else if (event & EVENT_INPUT)
191 int len = cs_get(co->link, &co->ibuf, &co->ibufsize);
195 yaz_log(YLOG_WARN|YLOG_ERRNO, "Error reading from %s",
197 connection_destroy(co);
202 yaz_log(YLOG_WARN, "EOF reading from %s", client_get_url(cl));
203 connection_destroy(co);
206 else if (len > 1) // We discard input if we have no connection
208 co->state = Conn_Open;
210 if (client_is_our_response(cl))
213 struct session_database *sdb = client_get_database(cl);
214 const char *apdulog = session_setting_oneval(sdb, PZ_APDULOG);
216 odr_reset(global_parameters.odr_in);
217 odr_setbuf(global_parameters.odr_in, co->ibuf, len, 0);
218 if (!z_APDU(global_parameters.odr_in, &a, 0, 0))
224 if (apdulog && *apdulog && *apdulog != '0')
226 ODR p = odr_createmem(ODR_PRINT);
227 yaz_log(YLOG_LOG, "recv APDU %s", client_get_url(cl));
229 odr_setprint(p, yaz_log_file());
231 odr_setprint(p, stderr);
236 case Z_APDU_initResponse:
237 client_init_response(cl, a);
239 case Z_APDU_searchResponse:
240 client_search_response(cl, a);
242 case Z_APDU_presentResponse:
243 client_present_response(cl, a);
246 client_close_response(cl, a);
250 "Unexpected Z39.50 response from %s",
255 // We aren't expecting staggered output from target
256 // if (cs_more(t->link))
257 // iochan_setevent(i, EVENT_INPUT);
259 else // we throw away response and go to idle mode
261 yaz_log(YLOG_DEBUG, "Ignoring result of expired operation");
262 client_set_state(cl, Client_Continue);
265 /* if len==1 we do nothing but wait for more input */
270 // Disassociate connection from client
271 void connection_release(struct connection *co)
273 struct client *cl = co->client;
275 yaz_log(YLOG_DEBUG, "Connection release %s", co->host->hostport);
278 client_set_connection(cl, 0);
282 void connect_resolver_host(struct host *host)
284 struct connection *con = host->connections;
287 if (con->state == Conn_Resolving)
289 if (!host->ipport) /* unresolved */
291 connection_destroy(con);
292 /* start all over .. at some point it will be NULL */
293 con = host->connections;
296 else if (!con->client)
298 connection_destroy(con);
299 /* start all over .. at some point it will be NULL */
300 con = host->connections;
305 connection_connect(con);
310 yaz_log(YLOG_LOG, "connect_resolver_host: state=%d", con->state);
316 int connection_send_apdu(struct connection *co, Z_APDU *a)
321 if (!z_APDU(global_parameters.odr_out, &a, 0, 0))
323 odr_perror(global_parameters.odr_out, "Encoding APDU");
326 buf = odr_getbuf(global_parameters.odr_out, &len, 0);
327 r = cs_put(co->link, buf, len);
330 yaz_log(YLOG_WARN, "cs_put: %s", cs_errmsg(cs_errno(co->link)));
335 fprintf(stderr, "cs_put incomplete (ParaZ does not handle that)\n");
338 odr_reset(global_parameters.odr_out); /* release the APDU structure */
339 co->state = Conn_Waiting;
340 iochan_setflags(co->iochan, EVENT_INPUT);
344 struct host *connection_get_host(struct connection *con)
349 int connection_connect(struct connection *con)
352 struct host *host = connection_get_host(con);
356 struct session_database *sdb = client_get_database(con->client);
357 const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
359 assert(host->ipport);
362 if (!(link = cs_create(tcpip_type, 0, PROTO_Z3950)))
364 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create comstack");
368 if (zproxy && *zproxy)
369 con->zproxy = xstrdup(zproxy);
373 /* no Z39.50 proxy needed - direct connect */
374 yaz_log(YLOG_DEBUG, "Connection create %s", connection_get_url(con));
376 if (!(addr = cs_straddr(link, host->ipport)))
378 yaz_log(YLOG_WARN|YLOG_ERRNO,
379 "Lookup of IP address %s failed", host->ipport);
384 /* Z39.50 proxy connect */
385 yaz_log(YLOG_DEBUG, "Connection create %s proxy %s",
386 connection_get_url(con), con->zproxy);
388 if (!(addr = cs_straddr(link, con->zproxy)))
390 yaz_log(YLOG_WARN|YLOG_ERRNO,
391 "Lookup of ZProxy IP address %s failed",
397 res = cs_connect(link, addr);
400 yaz_log(YLOG_WARN|YLOG_ERRNO, "cs_connect %s",
401 connection_get_url(con));
405 con->state = Conn_Connecting;
406 con->iochan = iochan_create(cs_fileno(link), connection_handler, 0);
407 iochan_settimeout(con->iochan, global_parameters.z3950_connect_timeout);
408 iochan_setdata(con->iochan, con);
409 pazpar2_add_channel(con->iochan);
411 /* this fragment is bad DRY: from client_prep_connection */
412 client_set_state(con->client, Client_Connecting);
413 iochan_setflag(con->iochan, EVENT_OUTPUT);
417 const char *connection_get_url(struct connection *co)
419 return client_get_url(co->client);
422 void connection_set_authentication(struct connection *co, char *auth)
424 co->authentication = auth;
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 (co->state == Conn_Open &&
449 (!co->client || client_get_session(co->client) != se) &&
450 !strcmp(co->authentication,
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);
470 if (co->state == Conn_Connecting)
472 client_set_state(cl, Client_Connecting);
473 iochan_setflag(co->iochan, EVENT_OUTPUT);
475 else if (co->state == Conn_Open)
477 if (client_get_state(cl) == Client_Error
478 || client_get_state(cl) == Client_Disconnected
479 || client_get_state(cl) == Client_Idle)
480 client_set_state(cl, Client_Continue);
481 iochan_setflag(co->iochan, EVENT_OUTPUT);
494 * indent-tabs-mode: nil
496 * vim: shiftwidth=4 tabstop=8 expandtab