Remove two functions not in use
[pazpar2-moved-to-github.git] / src / connection.c
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
20 /** \file connection.c
21     \brief Z39.50 connection (low-level client)
22 */
23
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",
69             no_connections);
70     return result;
71 }
72
73 int connections_count(void)
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
86 /** \brief Represents a physical, reusable  connection to a remote Z39.50 host
87  */
88 struct connection {
89     IOCHAN iochan;
90     ZOOM_connection link;
91     struct client *client;
92     char *zproxy;
93     char *url;
94     enum {
95         Conn_Closed,
96         Conn_Connecting,
97         Conn_Open
98     } state;
99     int operation_timeout;
100     int session_timeout;
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
111 // Close connection and recycle structure
112 static void connection_destroy(struct connection *co)
113 {
114     if (co->link)
115     {
116         ZOOM_connection_destroy(co->link);
117         iochan_destroy(co->iochan);
118     }
119     yaz_log(YLOG_DEBUG, "%p Connection destroy %s", co, co->url);
120
121     if (co->client)
122     {
123         client_disconnect(co->client);
124     }
125
126     xfree(co->zproxy);
127     xfree(co->url);
128     xfree(co);
129     connection_use(-1);
130 }
131
132 // Creates a new connection for client, associated with the host of
133 // client's database
134 static struct connection *connection_create(struct client *cl,
135                                             const char *url,
136                                             const char *zproxy,
137                                             int operation_timeout,
138                                             int session_timeout,
139                                             iochan_man_t iochan_man)
140 {
141     struct connection *co;
142
143     co = xmalloc(sizeof(*co));
144
145     co->client = cl;
146     co->url = xstrdup(url);
147     co->zproxy = 0;
148     if (zproxy)
149         co->zproxy = xstrdup(zproxy);
150
151     client_set_connection(cl, co);
152     co->link = 0;
153     co->state = Conn_Closed;
154     co->operation_timeout = operation_timeout;
155     co->session_timeout = session_timeout;
156
157     connection_connect(co, iochan_man);
158
159     connection_use(1);
160     return co;
161 }
162
163 static void non_block_events(struct connection *co)
164 {
165     int got_records = 0;
166     IOCHAN iochan = co->iochan;
167     ZOOM_connection link = co->link;
168     while (1)
169     {
170         struct client *cl = co->client;
171         int ev;
172         int r = ZOOM_event_nonblock(1, &link);
173         if (!r)
174             break;
175         if (!cl)
176             continue;
177         ev = ZOOM_connection_last_event(link);
178
179 #if 1
180         yaz_log(YLOG_DEBUG, "%p Connection ZOOM_EVENT_%s", co, ZOOM_get_event_str(ev));
181 #endif
182         switch (ev)
183         {
184         case ZOOM_EVENT_TIMEOUT:
185             break;
186         case ZOOM_EVENT_END:
187             {
188                 const char *error, *addinfo;
189                 int err;
190                 if ((err = ZOOM_connection_error(link, &error, &addinfo)))
191                 {
192                     yaz_log(YLOG_LOG, "Error %s from %s",
193                             error, client_get_id(cl));
194                     client_set_diagnostic(cl, err, error, addinfo);
195                     client_set_state(cl, Client_Error);
196                 }
197                 else
198                 {
199                     iochan_settimeout(iochan, co->session_timeout);
200                     client_set_state(cl, Client_Idle);
201                 }
202             }
203             break;
204         case ZOOM_EVENT_SEND_DATA:
205             break;
206         case ZOOM_EVENT_RECV_DATA:
207             break;
208         case ZOOM_EVENT_UNKNOWN:
209             break;
210         case ZOOM_EVENT_SEND_APDU:
211             client_set_state(co->client, Client_Working);
212             iochan_settimeout(iochan, co->operation_timeout);
213             break;
214         case ZOOM_EVENT_RECV_APDU:
215             break;
216         case ZOOM_EVENT_CONNECT:
217             yaz_log(YLOG_LOG, "Connected to %s", client_get_id(cl));
218             co->state = Conn_Open;
219             break;
220         case ZOOM_EVENT_RECV_SEARCH:
221             client_search_response(cl);
222             break;
223         case ZOOM_EVENT_RECV_RECORD:
224             client_record_response(cl, &got_records);
225             break;
226         case ZOOM_EVENT_NONE:
227             break;
228         default:
229             yaz_log(YLOG_LOG, "Unhandled event (%d) from %s",
230                     ev, client_get_id(cl));
231             break;
232         }
233     }
234     if (got_records)
235     {
236         struct client *cl = co->client;
237         if (cl)
238         {
239             client_check_preferred_watch(cl);
240             client_got_records(cl);
241         }
242     }
243 }
244
245 static void iochan_update(struct connection *co)
246 {
247     if (co->link)
248     {
249         int m = ZOOM_connection_get_mask(co->link);
250
251         if (m == 0)
252             m = ZOOM_SELECT_READ;
253         iochan_setflags(co->iochan, m);
254         iochan_setfd(co->iochan, ZOOM_connection_get_socket(co->link));
255     }
256 }
257
258 void connection_continue(struct connection *co)
259 {
260     int r = ZOOM_connection_exec_task(co->link);
261     if (!r)
262     {
263         struct client *cl = co->client;
264
265         client_lock(cl);
266         non_block_events(co);
267         client_unlock(cl);
268     }
269     else
270         iochan_update(co);
271 }
272
273 static void connection_handler(IOCHAN iochan, int event)
274 {
275     struct connection *co = iochan_getdata(iochan);
276     struct client *cl;
277
278     cl = co->client;
279     if (!cl)
280     {
281         /* no client associated with it.. We are probably getting
282            a closed connection from the target.. Or, perhaps, an unexpected
283            package.. We will just close the connection */
284         yaz_log(YLOG_LOG, "timeout connection %p event=%d", co, event);
285         connection_destroy(co);
286     }
287     else if (event & EVENT_TIMEOUT)
288     {
289         ZOOM_connection_fire_event_timeout(co->link);
290         client_lock(cl);
291         non_block_events(co);
292         client_unlock(cl);
293
294         connection_destroy(co);
295     }
296     else
297     {
298         if (ZOOM_connection_is_idle(co->link))
299         {
300             connection_destroy(co);
301             return;
302         }
303         client_lock(cl);
304         non_block_events(co);
305
306         ZOOM_connection_fire_event_socket(co->link, event);
307
308         non_block_events(co);
309         client_unlock(cl);
310
311         iochan_update(co);
312     }
313 }
314
315 static int connection_connect(struct connection *con, iochan_man_t iochan_man)
316 {
317     ZOOM_options zoptions = ZOOM_options_create();
318     const char *auth;
319     const char *charset;
320     const char *sru;
321     const char *sru_version = 0;
322     const char *value;
323     WRBUF w;
324
325     struct session_database *sdb = client_get_database(con->client);
326     const char *apdulog = session_setting_oneval(sdb, PZ_APDULOG);
327     const char *memcached = session_setting_oneval(sdb, PZ_MEMCACHED);
328
329     assert(con);
330
331     ZOOM_options_set(zoptions, "async", "1");
332     ZOOM_options_set(zoptions, "implementationName", PACKAGE_NAME);
333     ZOOM_options_set(zoptions, "implementationVersion", VERSION);
334
335     if ((charset = session_setting_oneval(sdb, PZ_NEGOTIATION_CHARSET)))
336         ZOOM_options_set(zoptions, "charset", charset);
337     if (memcached && *memcached)
338         ZOOM_options_set(zoptions, "memcached", memcached);
339
340     if (con->zproxy)
341     {
342         yaz_log(YLOG_LOG, "proxy=%s", con->zproxy);
343         ZOOM_options_set(zoptions, "proxy", con->zproxy);
344     }
345     if (apdulog && *apdulog)
346         ZOOM_options_set(zoptions, "apdulog", apdulog);
347
348
349     if ((sru = session_setting_oneval(sdb, PZ_SRU)) && *sru)
350         ZOOM_options_set(zoptions, "sru", sru);
351     if ((sru_version = session_setting_oneval(sdb, PZ_SRU_VERSION))
352         && *sru_version)
353         ZOOM_options_set(zoptions, "sru_version", sru_version);
354
355     if ((auth = session_setting_oneval(sdb, PZ_AUTHENTICATION)))
356     {
357         /* allow splitting user and reset with a blank always */
358         const char *cp1 = strchr(auth, ' ');
359         if (!cp1 && sru && *sru)
360             cp1 =  strchr(auth, '/');
361         if (!cp1)
362         {
363             /* Z39.50 user/password style, or no password for SRU */
364             ZOOM_options_set(zoptions, "user", auth);
365         }
366         else
367         {
368             /* now consider group as well */
369             const char *cp2 = strchr(cp1 + 1, ' ');
370
371             ZOOM_options_setl(zoptions, "user", auth, cp1 - auth);
372             if (!cp2)
373                 ZOOM_options_set(zoptions, "password", cp1 + 1);
374             else
375             {
376                 ZOOM_options_setl(zoptions, "group", cp1 + 1, cp2 - cp1 - 1);
377                 ZOOM_options_set(zoptions, "password", cp2 + 1);
378             }
379         }
380     }
381
382     value = session_setting_oneval(sdb, PZ_AUTHENTICATION_MODE);
383     if (value && *value)
384         ZOOM_options_set(zoptions, "authenticationMode", value);
385
386     if (!(con->link = ZOOM_connection_create(zoptions)))
387     {
388         yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create ZOOM Connection");
389         ZOOM_options_destroy(zoptions);
390         return -1;
391     }
392
393     w = wrbuf_alloc();
394     if (sru && *sru && !strstr(con->url, "://"))
395         wrbuf_puts(w, "http://");
396     if (strchr(con->url, '#'))
397     {
398         const char *cp = strchr(con->url, '#');
399         wrbuf_write(w, con->url, cp - con->url);
400     }
401     else
402         wrbuf_puts(w, con->url);
403
404     ZOOM_connection_connect(con->link, wrbuf_cstr(w), 0);
405
406     con->iochan = iochan_create(-1, connection_handler, 0, "connection_socket");
407     con->state = Conn_Connecting;
408     iochan_settimeout(con->iochan, con->operation_timeout);
409     iochan_setdata(con->iochan, con);
410     iochan_add(iochan_man, con->iochan);
411
412     client_set_state(con->client, Client_Connecting);
413     ZOOM_options_destroy(zoptions);
414     wrbuf_destroy(w);
415     return 0;
416 }
417
418 // Ensure that client has a connection associated
419 int client_prep_connection(struct client *cl,
420                            int operation_timeout, int session_timeout,
421                            iochan_man_t iochan_man,
422                            const struct timeval *abstime)
423 {
424     struct connection *co;
425     struct session_database *sdb = client_get_database(cl);
426     const char *zproxy = session_setting_oneval(sdb, PZ_ZPROXY);
427     const char *url = session_setting_oneval(sdb, PZ_URL);
428
429     if (zproxy && zproxy[0] == '\0')
430         zproxy = 0;
431
432     if (!url || !*url)
433         url = sdb->database->id;
434
435     yaz_log(YLOG_DEBUG, "client_prep_connection: target=%s url=%s",
436             client_get_id(cl), url);
437
438     co = client_get_connection(cl);
439     if (!co)
440     {
441         co = connection_create(cl, url, zproxy,
442                                operation_timeout, session_timeout,
443                                iochan_man);
444     }
445
446     if (co && co->link)
447         return 1;
448     else
449         return 0;
450 }
451
452 /*
453  * Local variables:
454  * c-basic-offset: 4
455  * c-file-style: "Stroustrup"
456  * indent-tabs-mode: nil
457  * End:
458  * vim: shiftwidth=4 tabstop=8 expandtab
459  */
460