1 /* $Id: connection.c,v 1.12 2007-09-20 08:13:27 adam Exp $
2 Copyright (c) 2006-2007, Index Data.
4 This file is part of Pazpar2.
6 Pazpar2 is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with Pazpar2; see the file LICENSE. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 /** \file connection.c
23 \brief Z39.50 connection (low-level client)
31 #include <sys/socket.h>
42 #include <yaz/comstack.h>
43 #include <yaz/tcpip.h>
44 #include "connection.h"
50 #include "parameters.h"
53 /** \brief Represents a physical, reusable connection to a remote Z39.50 host
59 struct client *client;
62 char *authentication; // Empty string or authentication string if set
70 struct connection *next; // next for same host or next in free list
73 static struct connection *connection_freelist = 0;
75 static void remove_connection_from_host(struct connection *con)
77 struct connection **conp = &con->host->connections;
83 *conp = (*conp)->next;
86 conp = &(*conp)->next;
91 // Close connection and recycle structure
92 void connection_destroy(struct connection *co)
97 iochan_destroy(co->iochan);
100 yaz_log(YLOG_DEBUG, "Connection destroy %s", co->host->hostport);
102 remove_connection_from_host(co);
105 client_disconnect(co->client);
109 co->next = connection_freelist;
110 connection_freelist = co;
113 // Creates a new connection for client, associated with the host of
115 struct connection *connection_create(struct client *cl)
117 struct connection *new;
118 struct host *host = client_get_host(cl);
120 if ((new = connection_freelist))
121 connection_freelist = new->next;
124 new = xmalloc(sizeof (struct connection));
129 new->next = new->host->connections;
130 new->host->connections = new;
132 new->authentication = "";
134 client_set_connection(cl, new);
136 new->state = Conn_Resolving;
138 connection_connect(new);
142 static void connection_handler(IOCHAN i, int event)
144 struct connection *co = iochan_getdata(i);
145 struct client *cl = co->client;
146 struct session *se = 0;
149 se = client_get_session(cl);
152 connection_destroy(co);
156 if (event & EVENT_TIMEOUT)
158 if (co->state == Conn_Connecting)
160 yaz_log(YLOG_WARN, "connect timeout %s", client_get_url(cl));
165 yaz_log(YLOG_LOG, "idle timeout %s", client_get_url(cl));
166 connection_destroy(co);
170 if (co->state == Conn_Connecting && event & EVENT_OUTPUT)
173 socklen_t errlen = sizeof(errcode);
175 if (getsockopt(cs_fileno(co->link), SOL_SOCKET, SO_ERROR, &errcode,
176 &errlen) < 0 || errcode != 0)
183 yaz_log(YLOG_DEBUG, "Connect OK");
184 co->state = Conn_Open;
186 client_set_state(cl, Client_Connected);
187 iochan_settimeout(i, global_parameters.z3950_session_timeout);
191 else if (event & EVENT_INPUT)
193 int len = cs_get(co->link, &co->ibuf, &co->ibufsize);
197 yaz_log(YLOG_WARN|YLOG_ERRNO, "Error reading from %s",
199 connection_destroy(co);
204 yaz_log(YLOG_WARN, "EOF reading from %s", client_get_url(cl));
205 connection_destroy(co);
208 else if (len > 1) // We discard input if we have no connection
210 co->state = Conn_Open;
212 if (client_is_our_response(cl))
215 struct session_database *sdb = client_get_database(cl);
216 const char *apdulog = session_setting_oneval(sdb, PZ_APDULOG);
218 odr_reset(global_parameters.odr_in);
219 odr_setbuf(global_parameters.odr_in, co->ibuf, len, 0);
220 if (!z_APDU(global_parameters.odr_in, &a, 0, 0))
226 if (apdulog && *apdulog && *apdulog != '0')
228 ODR p = odr_createmem(ODR_PRINT);
229 yaz_log(YLOG_LOG, "recv APDU %s", client_get_url(cl));
231 odr_setprint(p, yaz_log_file());
233 odr_setprint(p, stderr);
238 case Z_APDU_initResponse:
239 client_init_response(cl, a);
241 case Z_APDU_searchResponse:
242 client_search_response(cl, a);
244 case Z_APDU_presentResponse:
245 client_present_response(cl, a);
248 client_close_response(cl, a);
252 "Unexpected Z39.50 response from %s",
257 // We aren't expecting staggered output from target
258 // if (cs_more(t->link))
259 // iochan_setevent(i, EVENT_INPUT);
261 else // we throw away response and go to idle mode
263 yaz_log(YLOG_DEBUG, "Ignoring result of expired operation");
264 client_set_state(cl, Client_Continue);
267 /* if len==1 we do nothing but wait for more input */
272 // Disassociate connection from client
273 void connection_release(struct connection *co)
275 struct client *cl = co->client;
277 yaz_log(YLOG_DEBUG, "Connection release %s", co->host->hostport);
280 client_set_connection(cl, 0);
284 void connect_resolver_host(struct host *host)
286 struct connection *con = host->connections;
289 if (con->state == Conn_Resolving)
291 if (!host->ipport) /* unresolved */
293 connection_destroy(con);
294 /* start all over .. at some point it will be NULL */
295 con = host->connections;
298 else if (!con->client)
300 connection_destroy(con);
301 /* start all over .. at some point it will be NULL */
302 con = host->connections;
307 connection_connect(con);
312 yaz_log(YLOG_LOG, "connect_resolver_host: state=%d", con->state);
318 int connection_send_apdu(struct connection *co, Z_APDU *a)
323 if (!z_APDU(global_parameters.odr_out, &a, 0, 0))
325 odr_perror(global_parameters.odr_out, "Encoding APDU");
328 buf = odr_getbuf(global_parameters.odr_out, &len, 0);
329 r = cs_put(co->link, buf, len);
332 yaz_log(YLOG_WARN, "cs_put: %s", cs_errmsg(cs_errno(co->link)));
337 fprintf(stderr, "cs_put incomplete (ParaZ does not handle that)\n");
340 odr_reset(global_parameters.odr_out); /* release the APDU structure */
341 co->state = Conn_Waiting;
342 iochan_setflags(co->iochan, EVENT_INPUT);
346 struct host *connection_get_host(struct connection *con)
351 int connection_connect(struct connection *con)
354 struct host *host = connection_get_host(con);
358 struct session_database *sdb = client_get_database(con->client);
359 const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
361 assert(host->ipport);
364 if (!(link = cs_create(tcpip_type, 0, PROTO_Z3950)))
366 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create comstack");
370 if (zproxy && *zproxy)
371 con->zproxy = xstrdup(zproxy);
375 /* no Z39.50 proxy needed - direct connect */
376 yaz_log(YLOG_DEBUG, "Connection create %s", connection_get_url(con));
378 if (!(addr = cs_straddr(link, host->ipport)))
380 yaz_log(YLOG_WARN|YLOG_ERRNO,
381 "Lookup of IP address %s failed", host->ipport);
386 /* Z39.50 proxy connect */
387 yaz_log(YLOG_DEBUG, "Connection create %s proxy %s",
388 connection_get_url(con), con->zproxy);
390 if (!(addr = cs_straddr(link, con->zproxy)))
392 yaz_log(YLOG_WARN|YLOG_ERRNO,
393 "Lookup of ZProxy IP address %s failed",
399 res = cs_connect(link, addr);
402 yaz_log(YLOG_WARN|YLOG_ERRNO, "cs_connect %s",
403 connection_get_url(con));
407 con->state = Conn_Connecting;
408 con->iochan = iochan_create(cs_fileno(link), connection_handler, 0);
409 iochan_settimeout(con->iochan, global_parameters.z3950_connect_timeout);
410 iochan_setdata(con->iochan, con);
411 pazpar2_add_channel(con->iochan);
413 /* this fragment is bad DRY: from client_prep_connection */
414 client_set_state(con->client, Client_Connecting);
415 iochan_setflag(con->iochan, EVENT_OUTPUT);
419 const char *connection_get_url(struct connection *co)
421 return client_get_url(co->client);
424 void connection_set_authentication(struct connection *co, char *auth)
426 co->authentication = auth;
429 // Ensure that client has a connection associated
430 int client_prep_connection(struct client *cl)
432 struct connection *co;
433 struct session *se = client_get_session(cl);
434 struct host *host = client_get_host(cl);
435 struct session_database *sdb = client_get_database(cl);
436 const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
438 if (zproxy && zproxy[0] == '\0')
441 co = client_get_connection(cl);
443 yaz_log(YLOG_DEBUG, "Client prep %s", client_get_url(cl));
447 // See if someone else has an idle connection
448 // We should look at timestamps here to select the longest-idle connection
449 for (co = host->connections; co; co = co->next)
450 if (co->state == Conn_Open &&
451 (!co->client || client_get_session(co->client) != se) &&
452 !strcmp(co->authentication,
453 session_setting_oneval(client_get_database(cl),
456 if (zproxy == 0 && co->zproxy == 0)
458 if (zproxy && co->zproxy && !strcmp(zproxy, co->zproxy))
463 connection_release(co);
464 client_set_connection(cl, co);
468 co = connection_create(cl);
472 if (co->state == Conn_Connecting)
474 client_set_state(cl, Client_Connecting);
475 iochan_setflag(co->iochan, EVENT_OUTPUT);
477 else if (co->state == Conn_Open)
479 if (client_get_state(cl) == Client_Error
480 || client_get_state(cl) == Client_Disconnected
481 || client_get_state(cl) == Client_Idle)
482 client_set_state(cl, Client_Continue);
483 iochan_setflag(co->iochan, EVENT_OUTPUT);
496 * indent-tabs-mode: nil
498 * vim: shiftwidth=4 tabstop=8 expandtab