1 /* $Id: connection.c,v 1.2 2007-04-24 08:03:03 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"
49 #include "parameters.h"
52 /** \brief Represents a physical, reusable connection to a remote Z39.50 host
58 struct client *client;
67 struct connection *next; // next for same host or next in free list
70 static struct connection *connection_freelist = 0;
72 static void remove_connection_from_host(struct connection *con)
74 struct connection **conp = &con->host->connections;
80 *conp = (*conp)->next;
83 conp = &(*conp)->next;
88 // Close connection and recycle structure
89 void connection_destroy(struct connection *co)
94 iochan_destroy(co->iochan);
97 yaz_log(YLOG_DEBUG, "Connection destroy %s", co->host->hostport);
99 remove_connection_from_host(co);
102 client_disconnect(co->client);
104 co->next = connection_freelist;
105 connection_freelist = co;
108 // Creates a new connection for client, associated with the host of
110 struct connection *connection_create(struct client *cl)
112 struct connection *new;
113 struct host *host = client_get_host(cl);
115 if ((new = connection_freelist))
116 connection_freelist = new->next;
119 new = xmalloc(sizeof (struct connection));
124 new->next = new->host->connections;
125 new->host->connections = new;
127 client_set_connection(cl, new);
129 new->state = Conn_Resolving;
131 connection_connect(new);
135 static void connection_handler(IOCHAN i, int event)
137 struct connection *co = iochan_getdata(i);
138 struct client *cl = co->client;
139 struct session *se = 0;
142 se = client_get_session(cl);
145 yaz_log(YLOG_WARN, "Destroying orphan connection");
146 connection_destroy(co);
150 if (co->state == Conn_Connecting && event & EVENT_OUTPUT)
153 socklen_t errlen = sizeof(errcode);
155 if (getsockopt(cs_fileno(co->link), SOL_SOCKET, SO_ERROR, &errcode,
156 &errlen) < 0 || errcode != 0)
163 yaz_log(YLOG_DEBUG, "Connect OK");
164 co->state = Conn_Open;
166 client_set_state(cl, Client_Connected);
170 else if (event & EVENT_INPUT)
172 int len = cs_get(co->link, &co->ibuf, &co->ibufsize);
176 yaz_log(YLOG_WARN|YLOG_ERRNO, "Error reading from %s",
178 connection_destroy(co);
183 yaz_log(YLOG_WARN, "EOF reading from %s", client_get_url(cl));
184 connection_destroy(co);
187 else if (len > 1) // We discard input if we have no connection
189 co->state = Conn_Open;
191 if (client_is_our_response(cl))
195 odr_reset(global_parameters.odr_in);
196 odr_setbuf(global_parameters.odr_in, co->ibuf, len, 0);
197 if (!z_APDU(global_parameters.odr_in, &a, 0, 0))
204 case Z_APDU_initResponse:
205 client_init_response(cl, a);
207 case Z_APDU_searchResponse:
208 client_search_response(cl, a);
210 case Z_APDU_presentResponse:
211 client_present_response(cl, a);
214 client_close_response(cl, a);
218 "Unexpected Z39.50 response from %s",
223 // We aren't expecting staggered output from target
224 // if (cs_more(t->link))
225 // iochan_setevent(i, EVENT_INPUT);
227 else // we throw away response and go to idle mode
229 yaz_log(YLOG_DEBUG, "Ignoring result of expired operation");
230 client_set_state(cl, Client_Idle);
233 /* if len==1 we do nothing but wait for more input */
238 // Disassociate connection from client
239 void connection_release(struct connection *co)
241 struct client *cl = co->client;
243 yaz_log(YLOG_DEBUG, "Connection release %s", co->host->hostport);
246 client_set_connection(cl, 0);
250 void connect_resolver_host(struct host *host)
252 struct connection *con = host->connections;
255 if (con->state == Conn_Resolving)
257 if (!host->ipport) /* unresolved */
259 connection_destroy(con);
260 /* start all over .. at some point it will be NULL */
261 con = host->connections;
263 else if (!con->client)
265 yaz_log(YLOG_WARN, "connect_unresolved_host : ophan client");
266 connection_destroy(con);
267 /* start all over .. at some point it will be NULL */
268 con = host->connections;
272 connection_connect(con);
279 int connection_send_apdu(struct connection *co, Z_APDU *a)
284 if (!z_APDU(global_parameters.odr_out, &a, 0, 0))
286 odr_perror(global_parameters.odr_out, "Encoding APDU");
289 buf = odr_getbuf(global_parameters.odr_out, &len, 0);
290 r = cs_put(co->link, buf, len);
293 yaz_log(YLOG_WARN, "cs_put: %s", cs_errmsg(cs_errno(co->link)));
298 fprintf(stderr, "cs_put incomplete (ParaZ does not handle that)\n");
301 odr_reset(global_parameters.odr_out); /* release the APDU structure */
302 co->state = Conn_Waiting;
303 iochan_setflags(co->iochan, EVENT_INPUT);
307 struct host *connection_get_host(struct connection *con)
312 int connection_connect(struct connection *con)
315 struct host *host = connection_get_host(con);
319 assert(host->ipport);
322 if (!(link = cs_create(tcpip_type, 0, PROTO_Z3950)))
324 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create comstack");
328 if (0 == strlen(global_parameters.zproxy_override)){
329 /* no Z39.50 proxy needed - direct connect */
330 yaz_log(YLOG_DEBUG, "Connection create %s", connection_get_url(con));
332 if (!(addr = cs_straddr(link, host->ipport)))
334 yaz_log(YLOG_WARN|YLOG_ERRNO,
335 "Lookup of IP address %s failed", host->ipport);
340 /* Z39.50 proxy connect */
341 yaz_log(YLOG_DEBUG, "Connection create %s proxy %s",
342 connection_get_url(con), global_parameters.zproxy_override);
344 if (!(addr = cs_straddr(link, global_parameters.zproxy_override)))
346 yaz_log(YLOG_WARN|YLOG_ERRNO,
347 "Lookup of IP address %s failed",
348 global_parameters.zproxy_override);
353 res = cs_connect(link, addr);
356 yaz_log(YLOG_WARN|YLOG_ERRNO, "cs_connect %s",
357 connection_get_url(con));
361 con->state = Conn_Connecting;
362 con->iochan = iochan_create(cs_fileno(link), connection_handler, 0);
363 iochan_setdata(con->iochan, con);
364 pazpar2_add_channel(con->iochan);
366 /* this fragment is bad DRY: from client_prep_connection */
367 client_set_state(con->client, Client_Connecting);
368 iochan_setflag(con->iochan, EVENT_OUTPUT);
372 const char *connection_get_url(struct connection *co)
374 return client_get_url(co->client);
377 // Ensure that client has a connection associated
378 int client_prep_connection(struct client *cl)
380 struct connection *co;
381 struct session *se = client_get_session(cl);
382 struct host *host = client_get_host(cl);
384 co = client_get_connection(cl);
386 yaz_log(YLOG_DEBUG, "Client prep %s", client_get_url(cl));
390 // See if someone else has an idle connection
391 // We should look at timestamps here to select the longest-idle connection
392 for (co = host->connections; co; co = co->next)
393 if (co->state == Conn_Open && (!co->client || client_get_session(co->client) != se))
397 connection_release(co);
398 client_set_connection(cl, co);
402 co = connection_create(cl);
406 if (co->state == Conn_Connecting)
408 client_set_state(cl, Client_Connecting);
409 iochan_setflag(co->iochan, EVENT_OUTPUT);
411 else if (co->state == Conn_Open)
413 if (client_get_state(cl) == Client_Error
414 || client_get_state(cl) == Client_Disconnected)
415 client_set_state(cl, Client_Idle);
416 iochan_setflag(co->iochan, EVENT_OUTPUT);
429 * indent-tabs-mode: nil
431 * vim: shiftwidth=4 tabstop=8 expandtab