pazpar2  1.14.1
connection.c
Go to the documentation of this file.
1 /* This file is part of Pazpar2.
2  Copyright (C) Index Data
3 
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
7 version.
8 
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
12 for more details.
13 
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
17 
18 */
19 
24 #if HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27 
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #if HAVE_SYS_TIME_H
32 #include <sys/time.h>
33 #endif
34 #if HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37 
38 #include <signal.h>
39 #include <assert.h>
40 
41 #include <yaz/log.h>
42 #include <yaz/comstack.h>
43 #include <yaz/tcpip.h>
44 #include "connection.h"
45 #include "session.h"
46 #include "client.h"
47 #include "settings.h"
48 
49 /* connection counting (1) , disable connection counting (0) */
50 #if 1
51 static YAZ_MUTEX g_mutex = 0;
52 static int no_connections = 0;
53 static int total_no_connections = 0;
54 
55 static int connection_use(int delta)
56 {
57  int result;
58  if (!g_mutex)
59  yaz_mutex_create(&g_mutex);
60  yaz_mutex_enter(g_mutex);
61  no_connections += delta;
62  result = no_connections;
63  if (delta > 0)
64  total_no_connections += delta;
65  yaz_mutex_leave(g_mutex);
66  if (delta == 0)
67  return result;
68  yaz_log(YLOG_LOG, "%s connections=%d", delta > 0 ? "INC" : "DEC",
70  return result;
71 }
72 
74 {
75  return connection_use(0);
76 }
77 
78 
79 #else
80 #define connection_use(x)
81 #define connections_count(x) 0
82 #define connections_count_total(x) 0
83 #endif
84 
85 
88 struct connection {
90  ZOOM_connection link;
91  struct client *client;
92  char *zproxy;
93  char *url;
94  enum {
97  Conn_Open
98  } state;
101  struct connection *next; // next for same host or next in free list
102 };
103 
104 static int connection_connect(struct connection *con, iochan_man_t iochan_man);
105 
106 ZOOM_connection connection_get_link(struct connection *co)
107 {
108  return co->link;
109 }
110 
112 {
113  iochan_settimeout(co->iochan, 1);
114 }
115 
116 // Close connection and recycle structure
117 static void connection_destroy(struct connection *co)
118 {
119  if (co->link)
120  ZOOM_connection_destroy(co->link);
121  if (co->iochan)
122  iochan_destroy(co->iochan);
123  yaz_log(YLOG_DEBUG, "%p Connection destroy %s", co, co->url);
124 
125  if (co->client)
126  {
128  }
129 
130  xfree(co->zproxy);
131  xfree(co->url);
132  xfree(co);
133  connection_use(-1);
134 }
135 
136 // Creates a new connection for client, associated with the host of
137 // client's database
138 static struct connection *connection_create(struct client *cl,
139  const char *url,
140  const char *zproxy,
141  int operation_timeout,
142  int session_timeout,
143  iochan_man_t iochan_man)
144 {
145  struct connection *co;
146  int ret;
147 
148  co = xmalloc(sizeof(*co));
149 
150  co->client = cl;
151  co->url = xstrdup(url);
152  co->zproxy = 0;
153  co->iochan = 0;
154  if (zproxy)
155  co->zproxy = xstrdup(zproxy);
156 
157  client_set_connection(cl, co);
158  co->link = 0;
159  co->state = Conn_Closed;
162 
163  ret = connection_connect(co, iochan_man);
164  connection_use(1);
165  if (ret)
166  { /* error */
167  connection_destroy(co);
168  co = 0;
169  }
170  return co;
171 }
172 
173 static void non_block_events(struct connection *co)
174 {
175  int got_records = 0;
176  IOCHAN iochan = co->iochan;
177  ZOOM_connection link = co->link;
178  while (1)
179  {
180  struct client *cl = co->client;
181  int ev;
182  int r = ZOOM_event_nonblock(1, &link);
183  if (!r)
184  break;
185  if (!cl)
186  continue;
187  ev = ZOOM_connection_last_event(link);
188 
189 #if 1
190  yaz_log(YLOG_DEBUG, "%p Connection ZOOM_EVENT_%s", co, ZOOM_get_event_str(ev));
191 #endif
192  switch (ev)
193  {
194  case ZOOM_EVENT_TIMEOUT:
195  break;
196  case ZOOM_EVENT_END:
197  {
198  const char *error, *addinfo;
199  int err;
200  if ((err = ZOOM_connection_error(link, &error, &addinfo)))
201  {
202  struct session *se = client_get_session(cl);
203 
204  session_log(se, YLOG_WARN, "%s: Error %s (%s)",
205  client_get_id(cl), error, addinfo);
206  client_set_diagnostic(cl, err, error, addinfo);
208  }
209  else
210  {
213  }
214  }
215  break;
216  case ZOOM_EVENT_SEND_DATA:
217  break;
218  case ZOOM_EVENT_RECV_DATA:
219  break;
220  case ZOOM_EVENT_UNKNOWN:
221  break;
222  case ZOOM_EVENT_SEND_APDU:
225  break;
226  case ZOOM_EVENT_RECV_APDU:
227  break;
228  case ZOOM_EVENT_CONNECT:
229  co->state = Conn_Open;
230  break;
231  case ZOOM_EVENT_RECV_SEARCH:
233  break;
234  case ZOOM_EVENT_RECV_RECORD:
235  client_record_response(cl, &got_records);
236  break;
237  case ZOOM_EVENT_NONE:
238  break;
239  default:
240  yaz_log(YLOG_LOG, "Unhandled event (%d) from %s",
241  ev, client_get_id(cl));
242  break;
243  }
244  }
245  if (got_records)
246  {
247  struct client *cl = co->client;
248  if (cl)
249  client_got_records(cl);
250  }
251 }
252 
253 static void iochan_update(struct connection *co)
254 {
255  if (co->link)
256  {
257  int m = ZOOM_connection_get_mask(co->link);
258 
259  if (m == 0)
260  m = ZOOM_SELECT_READ;
261  iochan_setflags(co->iochan, m);
262  iochan_setfd(co->iochan, ZOOM_connection_get_socket(co->link));
263  }
264 }
265 
267 {
268  int r = ZOOM_connection_exec_task(co->link);
269  if (!r)
270  {
271  struct client *cl = co->client;
272 
273  client_lock(cl);
274  non_block_events(co);
275  client_unlock(cl);
276  }
277  else
278  iochan_update(co);
279 }
280 
281 static void connection_handler(IOCHAN iochan, int event)
282 {
283  struct connection *co = iochan_getdata(iochan);
284  struct client *cl;
285 
286  cl = co->client;
287  if (!cl)
288  {
289  /* no client associated with it.. We are probably getting
290  a closed connection from the target.. Or, perhaps, an unexpected
291  package.. We will just close the connection */
292  yaz_log(YLOG_LOG, "timeout connection %p event=%d", co, event);
293  connection_destroy(co);
294  }
295  else if (event & EVENT_TIMEOUT)
296  {
297  ZOOM_connection_fire_event_timeout(co->link);
298  client_lock(cl);
299  non_block_events(co);
300  client_unlock(cl);
301 
302  connection_destroy(co);
303  }
304  else
305  {
306  if (ZOOM_connection_is_idle(co->link))
307  {
308  connection_destroy(co);
309  return;
310  }
311  client_lock(cl);
312  non_block_events(co);
313 
314  ZOOM_connection_fire_event_socket(co->link, event);
315 
316  non_block_events(co);
317  client_unlock(cl);
318 
319  iochan_update(co);
320  }
321 }
322 
324 {
325  co->client = 0;
326 }
327 
328 static int connection_connect(struct connection *con, iochan_man_t iochan_man)
329 {
330  ZOOM_options zoptions = ZOOM_options_create();
331  const char *auth;
332  const char *charset;
333  const char *sru;
334  const char *sru_version = 0;
335  const char *value;
336  int r = 0;
337  WRBUF w;
338 
339  struct client *cl = con->client;
340  struct session_database *sdb = client_get_database(cl);
341  const char *apdulog = session_setting_oneval(sdb, PZ_APDULOG);
342  struct session *se = client_get_session(cl);
343  const char *memcached = session_setting_oneval(sdb, PZ_MEMCACHED);
344  const char *redis = session_setting_oneval(sdb, PZ_REDIS);
345  const char *error, *addinfo;
346  int err;
347 
348  assert(con);
349 
350  ZOOM_options_set(zoptions, "async", "1");
351  ZOOM_options_set(zoptions, "implementationName", PACKAGE_NAME);
352  ZOOM_options_set(zoptions, "implementationVersion", VERSION);
353 
354  if ((charset = session_setting_oneval(sdb, PZ_NEGOTIATION_CHARSET)))
355  ZOOM_options_set(zoptions, "charset", charset);
356  if (memcached && *memcached)
357  ZOOM_options_set(zoptions, "memcached", memcached);
358  if (redis && *redis)
359  ZOOM_options_set(zoptions, "redis", redis);
360 
361  if (con->zproxy)
362  {
363  yaz_log(YLOG_LOG, "proxy=%s", con->zproxy);
364  ZOOM_options_set(zoptions, "proxy", con->zproxy);
365  }
366  if (apdulog && *apdulog)
367  ZOOM_options_set(zoptions, "apdulog", apdulog);
368 
369 
370  if ((sru = session_setting_oneval(sdb, PZ_SRU)) && *sru)
371  ZOOM_options_set(zoptions, "sru", sru);
372  if ((sru_version = session_setting_oneval(sdb, PZ_SRU_VERSION))
373  && *sru_version)
374  ZOOM_options_set(zoptions, "sru_version", sru_version);
375 
376  if ((auth = session_setting_oneval(sdb, PZ_AUTHENTICATION)))
377  {
378  /* allow splitting user and reset with a blank always */
379  const char *cp1 = strchr(auth, ' ');
380  if (!cp1 && sru && *sru)
381  cp1 = strchr(auth, '/');
382  if (!cp1)
383  {
384  /* Z39.50 user/password style, or no password for SRU */
385  ZOOM_options_set(zoptions, "user", auth);
386  }
387  else
388  {
389  /* now consider group as well */
390  const char *cp2 = strchr(cp1 + 1, ' ');
391 
392  ZOOM_options_setl(zoptions, "user", auth, cp1 - auth);
393  if (!cp2)
394  ZOOM_options_set(zoptions, "password", cp1 + 1);
395  else
396  {
397  ZOOM_options_setl(zoptions, "group", cp1 + 1, cp2 - cp1 - 1);
398  ZOOM_options_set(zoptions, "password", cp2 + 1);
399  }
400  }
401  }
402 
404  if (value && *value)
405  ZOOM_options_set(zoptions, "authenticationMode", value);
406 
407  if (!(con->link = ZOOM_connection_create(zoptions)))
408  {
409  session_log(se, YLOG_WARN, "%s: ZOOM_connection_create failed",
410  client_get_id(cl));
412  client_set_diagnostic(cl, 2,
413  ZOOM_diag_str(2),
414  "Cannot create connection");
415  ZOOM_options_destroy(zoptions);
416  return -1;
417  }
418 
419  w = wrbuf_alloc();
420  if (sru && *sru && !strstr(con->url, "://"))
421  wrbuf_puts(w, "http://");
422  if (strchr(con->url, '#'))
423  {
424  const char *cp = strchr(con->url, '#');
425  wrbuf_write(w, con->url, cp - con->url);
426  }
427  else
428  wrbuf_puts(w, con->url);
429 
430  ZOOM_connection_connect(con->link, wrbuf_cstr(w), 0);
431 
432  if ((err = ZOOM_connection_error(con->link, &error, &addinfo)))
433  {
434  struct session *se = client_get_session(cl);
435  session_log(se, YLOG_WARN, "%s: Error %s (%s)",
436  client_get_id(cl), error, addinfo);
437  client_set_diagnostic(cl, err, error, addinfo);
439  ZOOM_connection_destroy(con->link);
440  con->link = 0;
441  r = -1;
442  }
443  else
444  {
445  con->iochan = iochan_create(-1, connection_handler, 0,
446  "connection_socket");
447  con->state = Conn_Connecting;
448  iochan_settimeout(con->iochan, con->operation_timeout);
449  iochan_setdata(con->iochan, con);
450  if (iochan_add(iochan_man, con->iochan, 20))
451  {
452  session_log(se, YLOG_WARN, "%s: out of connections",
453  client_get_id(cl));
454  iochan_destroy(con->iochan);
455  con->iochan = 0;
456  ZOOM_connection_destroy(con->link);
457  con->link = 0;
458  r = -1;
459  }
460  else
461  {
463  }
464  }
465  ZOOM_options_destroy(zoptions);
466  wrbuf_destroy(w);
467  return r;
468 }
469 
470 // Ensure that client has a connection associated
472  int operation_timeout, int session_timeout,
473  iochan_man_t iochan_man,
474  const struct timeval *abstime)
475 {
476  struct connection *co;
477  struct session_database *sdb = client_get_database(cl);
478  const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
479  const char *url = session_setting_oneval(sdb, PZ_URL);
480 
481  if (zproxy && zproxy[0] == '\0')
482  zproxy = 0;
483 
484  if (!url || !*url)
485  url = sdb->database->id;
486 
487  yaz_log(YLOG_DEBUG, "client_prep_connection: target=%s url=%s",
488  client_get_id(cl), url);
489 
490  co = client_get_connection(cl);
491  if (co)
492  return 2;
493  if (!co)
494  {
495  co = connection_create(cl, url, zproxy,
496  operation_timeout, session_timeout,
497  iochan_man);
498  }
499 
500  if (co && co->link)
501  return 1;
502  else
503  return 0;
504 }
505 
506 /*
507  * Local variables:
508  * c-basic-offset: 4
509  * c-file-style: "Stroustrup"
510  * indent-tabs-mode: nil
511  * End:
512  * vim: shiftwidth=4 tabstop=8 expandtab
513  */
514 
void client_got_records(struct client *cl)
Definition: client.c:580
void client_unlock(struct client *c)
Definition: client.c:1096
void client_set_connection(struct client *cl, struct connection *con)
Definition: client.c:1144
void client_set_state_nb(struct client *cl, enum client_state st)
Definition: client.c:171
void client_set_diagnostic(struct client *cl, int diagnostic, const char *message, const char *addinfo)
Definition: client.c:1777
void client_search_response(struct client *cl)
Definition: client.c:549
void client_lock(struct client *c)
Definition: client.c:1091
const char * client_get_id(struct client *cl)
Definition: client.c:1833
void client_set_state(struct client *cl, enum client_state st)
Definition: client.c:176
struct connection * client_get_connection(struct client *cl)
Definition: client.c:246
struct session * client_get_session(struct client *cl)
Definition: client.c:256
void client_record_response(struct client *cl, int *got_records)
Definition: client.c:693
struct session_database * client_get_database(struct client *cl)
Definition: client.c:251
void client_disconnect(struct client *cl)
Definition: client.c:1163
Z39.50 client.
@ Client_Working
Definition: client.h:37
@ Client_Idle
Definition: client.h:36
@ Client_Error
Definition: client.h:38
@ Client_Connecting
Definition: client.h:35
#define PACKAGE_NAME
Definition: config.h:53
#define VERSION
Definition: config.h:74
static int total_no_connections
Definition: connection.c:53
static struct connection * connection_create(struct client *cl, const char *url, const char *zproxy, int operation_timeout, int session_timeout, iochan_man_t iochan_man)
Definition: connection.c:138
ZOOM_connection connection_get_link(struct connection *co)
Definition: connection.c:106
void connection_continue(struct connection *co)
Definition: connection.c:266
int client_prep_connection(struct client *cl, int operation_timeout, int session_timeout, iochan_man_t iochan_man, const struct timeval *abstime)
Definition: connection.c:471
static int no_connections
Definition: connection.c:52
static void connection_handler(IOCHAN iochan, int event)
Definition: connection.c:281
static void non_block_events(struct connection *co)
Definition: connection.c:173
static int connection_connect(struct connection *con, iochan_man_t iochan_man)
Definition: connection.c:328
static YAZ_MUTEX g_mutex
Definition: connection.c:51
static void iochan_update(struct connection *co)
Definition: connection.c:253
static int connection_use(int delta)
Definition: connection.c:55
void connection_mark_dead(struct connection *co)
Definition: connection.c:111
void connection_release2(struct connection *co)
Definition: connection.c:323
static void connection_destroy(struct connection *co)
Definition: connection.c:117
int connections_count(void)
Definition: connection.c:73
Z39.50 connection (low-level client)
IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags, const char *name)
Definition: eventl.c:209
void iochan_destroy(IOCHAN chan)
Definition: eventl.c:201
int iochan_add(iochan_man_t man, IOCHAN chan, int slack)
Definition: eventl.c:173
#define iochan_settimeout(i, t)
Definition: eventl.h:68
#define EVENT_TIMEOUT
Definition: eventl.h:38
#define iochan_getdata(i)
Definition: eventl.h:62
#define iochan_setfd(i, d)
Definition: eventl.h:61
#define iochan_setdata(i, d)
Definition: eventl.h:63
#define iochan_setflags(i, d)
Definition: eventl.h:64
static void session_timeout(IOCHAN i, int event)
Definition: http_command.c:128
static void error(struct http_response *rs, enum pazpar2_error_code code, const char *addinfo)
Definition: http_command.c:273
const char * session_setting_oneval(struct session_database *db, int offset)
Definition: session.c:433
void session_log(struct session *s, int level, const char *fmt,...)
Definition: session.c:2509
#define PZ_NEGOTIATION_CHARSET
Definition: settings.h:43
#define PZ_APDULOG
Definition: settings.h:36
#define PZ_REDIS
Definition: settings.h:61
#define PZ_SRU
Definition: settings.h:37
#define PZ_URL
Definition: settings.h:53
#define PZ_AUTHENTICATION_MODE
Definition: settings.h:58
#define PZ_MEMCACHED
Definition: settings.h:60
#define PZ_SRU_VERSION
Definition: settings.h:38
#define PZ_ZPROXY
Definition: settings.h:35
#define PZ_AUTHENTICATION
Definition: settings.h:29
Represents client state for a connection to one search target.
Definition: client.c:99
char * addinfo
Definition: client.c:105
Represents a physical, reusable connection to a remote Z39.50 host.
Definition: connection.c:88
int session_timeout
Definition: connection.c:100
ZOOM_connection link
Definition: connection.c:90
int operation_timeout
Definition: connection.c:99
char * zproxy
Definition: connection.c:92
char * url
Definition: connection.c:93
@ Conn_Connecting
Definition: connection.c:96
IOCHAN iochan
Definition: connection.c:89
struct client * client
Definition: connection.c:91
struct connection * next
Definition: connection.c:101
enum connection::@0 state
char * id
Definition: settings.h:76
Definition: eventl.h:32
struct database * database
Definition: session.h:60