1 /* $Id: connection.c,v 1.4 2007-06-06 11:49:48 marc 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
69 struct connection *next; // next for same host or next in free list
72 static struct connection *connection_freelist = 0;
74 static void remove_connection_from_host(struct connection *con)
76 struct connection **conp = &con->host->connections;
82 *conp = (*conp)->next;
85 conp = &(*conp)->next;
90 // Close connection and recycle structure
91 void connection_destroy(struct connection *co)
96 iochan_destroy(co->iochan);
99 yaz_log(YLOG_DEBUG, "Connection destroy %s", co->host->hostport);
101 remove_connection_from_host(co);
104 client_disconnect(co->client);
106 co->next = connection_freelist;
107 connection_freelist = co;
110 // Creates a new connection for client, associated with the host of
112 struct connection *connection_create(struct client *cl)
114 struct connection *new;
115 struct host *host = client_get_host(cl);
117 if ((new = connection_freelist))
118 connection_freelist = new->next;
121 new = xmalloc(sizeof (struct connection));
126 new->next = new->host->connections;
127 new->host->connections = new;
129 new->authentication = "";
130 client_set_connection(cl, new);
132 new->state = Conn_Resolving;
134 connection_connect(new);
138 static void connection_handler(IOCHAN i, int event)
140 struct connection *co = iochan_getdata(i);
141 struct client *cl = co->client;
142 struct session *se = 0;
145 se = client_get_session(cl);
148 yaz_log(YLOG_WARN, "Destroying orphan connection");
149 connection_destroy(co);
153 if (co->state == Conn_Connecting && event & EVENT_OUTPUT)
156 socklen_t errlen = sizeof(errcode);
158 if (getsockopt(cs_fileno(co->link), SOL_SOCKET, SO_ERROR, &errcode,
159 &errlen) < 0 || errcode != 0)
166 yaz_log(YLOG_DEBUG, "Connect OK");
167 co->state = Conn_Open;
169 client_set_state(cl, Client_Connected);
173 else if (event & EVENT_INPUT)
175 int len = cs_get(co->link, &co->ibuf, &co->ibufsize);
179 yaz_log(YLOG_WARN|YLOG_ERRNO, "Error reading from %s",
181 connection_destroy(co);
186 yaz_log(YLOG_WARN, "EOF reading from %s", client_get_url(cl));
187 connection_destroy(co);
190 else if (len > 1) // We discard input if we have no connection
192 co->state = Conn_Open;
194 if (client_is_our_response(cl))
198 odr_reset(global_parameters.odr_in);
199 odr_setbuf(global_parameters.odr_in, co->ibuf, len, 0);
200 if (!z_APDU(global_parameters.odr_in, &a, 0, 0))
207 case Z_APDU_initResponse:
208 client_init_response(cl, a);
210 case Z_APDU_searchResponse:
211 client_search_response(cl, a);
213 case Z_APDU_presentResponse:
214 client_present_response(cl, a);
217 client_close_response(cl, a);
221 "Unexpected Z39.50 response from %s",
226 // We aren't expecting staggered output from target
227 // if (cs_more(t->link))
228 // iochan_setevent(i, EVENT_INPUT);
230 else // we throw away response and go to idle mode
232 yaz_log(YLOG_DEBUG, "Ignoring result of expired operation");
233 client_set_state(cl, Client_Idle);
236 /* if len==1 we do nothing but wait for more input */
241 // Disassociate connection from client
242 void connection_release(struct connection *co)
244 struct client *cl = co->client;
246 yaz_log(YLOG_DEBUG, "Connection release %s", co->host->hostport);
249 client_set_connection(cl, 0);
253 void connect_resolver_host(struct host *host)
255 struct connection *con = host->connections;
258 if (con->state == Conn_Resolving)
260 if (!host->ipport) /* unresolved */
262 connection_destroy(con);
263 /* start all over .. at some point it will be NULL */
264 con = host->connections;
266 else if (!con->client)
268 yaz_log(YLOG_WARN, "connect_unresolved_host : ophan client");
269 connection_destroy(con);
270 /* start all over .. at some point it will be NULL */
271 con = host->connections;
275 connection_connect(con);
282 int connection_send_apdu(struct connection *co, Z_APDU *a)
287 if (!z_APDU(global_parameters.odr_out, &a, 0, 0))
289 odr_perror(global_parameters.odr_out, "Encoding APDU");
292 buf = odr_getbuf(global_parameters.odr_out, &len, 0);
293 r = cs_put(co->link, buf, len);
296 yaz_log(YLOG_WARN, "cs_put: %s", cs_errmsg(cs_errno(co->link)));
301 fprintf(stderr, "cs_put incomplete (ParaZ does not handle that)\n");
304 odr_reset(global_parameters.odr_out); /* release the APDU structure */
305 co->state = Conn_Waiting;
306 iochan_setflags(co->iochan, EVENT_INPUT);
310 struct host *connection_get_host(struct connection *con)
315 int connection_connect(struct connection *con)
318 struct host *host = connection_get_host(con);
322 struct session_database *sdb = client_get_database(con->client);
323 char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
325 assert(host->ipport);
328 if (!(link = cs_create(tcpip_type, 0, PROTO_Z3950)))
330 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create comstack");
334 if (!zproxy || 0 == strlen(zproxy)){
335 /* no Z39.50 proxy needed - direct connect */
336 yaz_log(YLOG_DEBUG, "Connection create %s", connection_get_url(con));
338 if (!(addr = cs_straddr(link, host->ipport)))
340 yaz_log(YLOG_WARN|YLOG_ERRNO,
341 "Lookup of IP address %s failed", host->ipport);
346 /* Z39.50 proxy connect */
347 yaz_log(YLOG_DEBUG, "Connection create %s proxy %s",
348 connection_get_url(con), zproxy);
350 if (!(addr = cs_straddr(link, zproxy)))
352 yaz_log(YLOG_WARN|YLOG_ERRNO,
353 "Lookup of ZProxy IP address %s failed",
359 res = cs_connect(link, addr);
362 yaz_log(YLOG_WARN|YLOG_ERRNO, "cs_connect %s",
363 connection_get_url(con));
367 con->state = Conn_Connecting;
368 con->iochan = iochan_create(cs_fileno(link), connection_handler, 0);
369 iochan_setdata(con->iochan, con);
370 pazpar2_add_channel(con->iochan);
372 /* this fragment is bad DRY: from client_prep_connection */
373 client_set_state(con->client, Client_Connecting);
374 iochan_setflag(con->iochan, EVENT_OUTPUT);
378 const char *connection_get_url(struct connection *co)
380 return client_get_url(co->client);
383 void connection_set_authentication(struct connection *co, char *auth)
385 co->authentication = auth;
388 // Ensure that client has a connection associated
389 int client_prep_connection(struct client *cl)
391 struct connection *co;
392 struct session *se = client_get_session(cl);
393 struct host *host = client_get_host(cl);
395 co = client_get_connection(cl);
397 yaz_log(YLOG_DEBUG, "Client prep %s", client_get_url(cl));
401 // See if someone else has an idle connection
402 // We should look at timestamps here to select the longest-idle connection
403 for (co = host->connections; co; co = co->next)
404 if (co->state == Conn_Open &&
405 (!co->client || client_get_session(co->client) != se) &&
406 !strcmp(co->authentication,
407 session_setting_oneval(client_get_database(cl),
412 connection_release(co);
413 client_set_connection(cl, co);
417 co = connection_create(cl);
421 if (co->state == Conn_Connecting)
423 client_set_state(cl, Client_Connecting);
424 iochan_setflag(co->iochan, EVENT_OUTPUT);
426 else if (co->state == Conn_Open)
428 if (client_get_state(cl) == Client_Error
429 || client_get_state(cl) == Client_Disconnected)
430 client_set_state(cl, Client_Idle);
431 iochan_setflag(co->iochan, EVENT_OUTPUT);
444 * indent-tabs-mode: nil
446 * vim: shiftwidth=4 tabstop=8 expandtab