Allow keep alive for anonymous sessions
[yazpp-moved-to-github.git] / src / yaz-proxy.cpp
1 /*
2  * Copyright (c) 1998-2003, Index Data.
3  * See the file LICENSE for details.
4  * 
5  * $Id: yaz-proxy.cpp,v 1.39 2003-06-09 22:20:39 adam Exp $
6  */
7
8 #include <assert.h>
9 #include <time.h>
10
11 #include <yaz/log.h>
12 #include <yaz++/proxy.h>
13
14 Yaz_Proxy::Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable) :
15     Yaz_Z_Assoc(the_PDU_Observable)
16 {
17     m_PDU_Observable = the_PDU_Observable;
18     m_client = 0;
19     m_parent = 0;
20     m_clientPool = 0;
21     m_seqno = 1;
22     m_keepalive = 1;
23     m_proxyTarget = 0;
24     m_proxy_authentication = 0;
25     m_max_clients = 50;
26     m_seed = time(0);
27     m_idletime = 600;
28     m_optimize = xstrdup ("1");
29 }
30
31 Yaz_Proxy::~Yaz_Proxy()
32 {
33     xfree (m_proxyTarget);
34     xfree (m_proxy_authentication);
35     xfree (m_optimize);
36 }
37
38 void Yaz_Proxy::set_proxy_target(const char *target)
39 {
40     xfree (m_proxyTarget);
41     m_proxyTarget = 0;
42     if (target)
43         m_proxyTarget = (char *) xstrdup (target);
44 }
45
46 void Yaz_Proxy::set_proxy_authentication (const char *auth)
47 {
48     xfree (m_proxy_authentication);
49     m_proxy_authentication = 0;
50     if (auth)
51         m_proxy_authentication = (char *) xstrdup (auth);
52 }
53
54 IYaz_PDU_Observer *Yaz_Proxy::sessionNotify(IYaz_PDU_Observable
55                                             *the_PDU_Observable, int fd)
56 {
57     Yaz_Proxy *new_proxy = new Yaz_Proxy(the_PDU_Observable);
58     new_proxy->m_parent = this;
59     new_proxy->timeout(m_idletime);
60     new_proxy->set_proxy_target(m_proxyTarget);
61     new_proxy->set_APDU_log(get_APDU_log());
62     new_proxy->set_proxy_authentication(m_proxy_authentication);
63     yaz_log (LOG_LOG, "New session p=%p", new_proxy);
64     return new_proxy;
65 }
66
67 char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo)
68 {
69     int oid[OID_SIZE];
70     Z_OtherInformationUnit *oi;
71     struct oident ent;
72     ent.proto = PROTO_Z3950;
73     ent.oclass = CLASS_USERINFO;
74     ent.value = (oid_value) VAL_COOKIE;
75     assert (oid_ent_to_oid (&ent, oid));
76
77     if (oid_ent_to_oid (&ent, oid) && 
78         (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
79         oi->which == Z_OtherInfo_characterInfo)
80         return oi->information.characterInfo;
81     return 0;
82 }
83
84 char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
85 {
86     int oid[OID_SIZE];
87     Z_OtherInformationUnit *oi;
88     struct oident ent;
89     ent.proto = PROTO_Z3950;
90     ent.oclass = CLASS_USERINFO;
91     ent.value = (oid_value) VAL_PROXY;
92     if (oid_ent_to_oid (&ent, oid) &&
93         (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
94         oi->which == Z_OtherInfo_characterInfo)
95         return oi->information.characterInfo;
96     return 0;
97 }
98
99 Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu)
100 {
101     assert (m_parent);
102     Yaz_Proxy *parent = m_parent;
103     Z_OtherInformation **oi;
104     Yaz_ProxyClient *c = m_client;
105     
106     get_otherInfoAPDU(apdu, &oi);
107     char *cookie = get_cookie(oi);
108
109     const char *proxy_host = get_proxy(oi);
110     if (proxy_host)
111         set_proxy_target(proxy_host);
112     
113     // no target specified at all?
114     if (!m_proxyTarget)
115         return 0;
116
117     if (!strcmp(m_proxyTarget, "stop"))
118         exit (0);
119     if (cookie && *cookie)
120     {
121         Yaz_ProxyClient *cc = 0;
122         
123         for (c = parent->m_clientPool; c; c = c->m_next)
124         {
125             assert (c->m_prev);
126             assert (*c->m_prev == c);
127             if (!strcmp(cookie,c->m_cookie) &&
128                 !strcmp(m_proxyTarget, c->get_hostname()))
129             {
130                 cc = c;
131             }
132         }
133         if (cc)
134         {
135             // found it in cache
136             c = cc;
137             // The following handles "cancel"
138             // If connection is busy (waiting for PDU) and
139             // we have an initRequest we can safely do re-open
140             if (c->m_waiting && apdu->which == Z_APDU_initRequest)
141             {
142                 yaz_log (LOG_LOG, "reopen target=%s", c->get_hostname());
143                 c->close();
144                 c->client(m_proxyTarget);
145                 c->m_init_flag = 0;
146
147                 c->m_last_ok = 0;
148                 c->m_last_resultCount = 0;
149                 c->m_sr_transform = 0;
150                 c->m_waiting = 0;
151                 c->timeout(m_idletime); 
152             }
153             c->m_seqno = parent->m_seqno;
154             if (c->m_server && c->m_server != this)
155                 c->m_server->m_client = 0;
156             c->m_server = this;
157             c->m_seqno = parent->m_seqno;
158             (parent->m_seqno)++;
159             yaz_log (LOG_DEBUG, "get_client 1 %p %p", this, c);
160             return c;
161         }
162     }
163     else if (!c)
164     {
165         Yaz_ProxyClient *cc = 0;
166         
167         for (c = parent->m_clientPool; c; c = c->m_next)
168         {
169             assert (c->m_prev);
170             assert (*c->m_prev == c);
171             if (c->m_server == 0 && c->m_cookie[0] == 0 && 
172                 !strcmp(m_proxyTarget, c->get_hostname()))
173             {
174                 cc = c;
175             }
176         }
177         if (cc)
178         {
179             // found it in cache
180             c = cc;
181
182             c->m_seqno = parent->m_seqno;
183             assert(c->m_server == 0);
184             c->m_server = this;
185             c->m_seqno = parent->m_seqno;
186             (parent->m_seqno)++;
187             yaz_log (LOG_LOG, "get_client 10 %p %p", this, c);
188             return c;
189         }
190     }
191     if (!m_client)
192     {
193         if (apdu->which != Z_APDU_initRequest)
194         {
195             yaz_log (LOG_LOG, "no first INIT!");
196             return 0;
197         }
198         Z_InitRequest *initRequest = apdu->u.initRequest;
199
200         if (!initRequest->idAuthentication)
201         {
202             if (m_proxy_authentication)
203             {
204                 initRequest->idAuthentication =
205                     (Z_IdAuthentication *)
206                     odr_malloc (odr_encode(),
207                                 sizeof(*initRequest->idAuthentication));
208                 initRequest->idAuthentication->which =
209                     Z_IdAuthentication_open;
210                 initRequest->idAuthentication->u.open =
211                     odr_strdup (odr_encode(), m_proxy_authentication);
212             }
213         }
214
215         // go through list of clients - and find the lowest/oldest one.
216         Yaz_ProxyClient *c_min = 0;
217         int min_seq = -1;
218         int no_of_clients = 0;
219         yaz_log (LOG_LOG, "Existing sessions");
220         for (c = parent->m_clientPool; c; c = c->m_next)
221         {
222             yaz_log (LOG_LOG, " Session %-3d wait=%d %s", c->m_seqno,
223                                c->m_waiting, c->get_hostname());
224             no_of_clients++;
225             if (min_seq < 0 || c->m_seqno < min_seq)
226             {
227                 min_seq = c->m_seqno;
228                 c_min = c;
229             }
230         }
231         if (no_of_clients >= parent->m_max_clients)
232         {
233             c = c_min;
234             if (c->m_waiting || strcmp(m_proxyTarget, c->get_hostname()))
235             {
236                 yaz_log (LOG_LOG, "Replace session %d",
237                       c->m_seqno);
238                 if (c->m_server && c->m_server != this)
239                     delete c->m_server;
240                 c->m_server = 0;
241             }
242             else
243             {
244                 yaz_log (LOG_LOG, "Reuse session %d to %d",
245                       c->m_seqno, parent->m_seqno);
246                 if (cookie)
247                     strcpy (c->m_cookie, cookie);
248                 else
249                     c->m_cookie[0] = '\0';
250                 c->m_seqno = parent->m_seqno;
251                 if (c->m_server && c->m_server != this)
252                 {
253                     c->m_server->m_client = 0;
254                     delete c->m_server;
255                 }
256                 (parent->m_seqno)++;
257                 yaz_log (LOG_DEBUG, "get_client 2 %p %p", this, c);
258                 return c;
259             }
260         }
261         else
262         {
263             yaz_log (LOG_LOG, "Making session %d %s", parent->m_seqno,
264                             m_proxyTarget);
265             c = new Yaz_ProxyClient(m_PDU_Observable->clone());
266             c->m_next = parent->m_clientPool;
267             if (c->m_next)
268                 c->m_next->m_prev = &c->m_next;
269             parent->m_clientPool = c;
270             c->m_prev = &parent->m_clientPool;
271         }
272         if (cookie)
273             strcpy (c->m_cookie, cookie);
274         else
275             c->m_cookie[0] = '\0';
276         yaz_log (LOG_LOG, "Connecting to %s", m_proxyTarget);
277         c->m_seqno = parent->m_seqno;
278         c->client(m_proxyTarget);
279         c->m_init_flag = 0;
280         c->m_last_resultCount = 0;
281         c->m_last_ok = 0;
282         c->m_sr_transform = 0;
283         c->m_waiting = 0;
284         c->timeout(20);
285
286         (parent->m_seqno)++;
287     }
288     yaz_log (LOG_DEBUG, "get_client 3 %p %p", this, c);
289     return c;
290 }
291
292 Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
293 {
294     if (apdu->which != Z_APDU_searchRequest)
295         return apdu;
296     if (*m_parent->m_optimize == '0')
297         return apdu;      // don't optimize result sets..
298     Z_SearchRequest *sr = apdu->u.searchRequest;
299     Yaz_Z_Query *this_query = new Yaz_Z_Query;
300     Yaz_Z_Databases this_databases;
301
302     this_databases.set(sr->num_databaseNames, (const char **)
303                        sr->databaseNames);
304     
305     this_query->set_Z_Query(sr->query);
306     
307     if (m_client->m_last_ok && m_client->m_last_query &&
308         m_client->m_last_query->match(this_query) &&
309         !strcmp(m_client->m_last_resultSetId, sr->resultSetName) &&
310         m_client->m_last_databases.match(this_databases))
311     {
312         delete this_query;
313         if (m_client->m_last_resultCount > *sr->smallSetUpperBound &&
314             m_client->m_last_resultCount < *sr->largeSetLowerBound)
315         {
316             // medium Set
317             // send present request (medium size)
318             yaz_log (LOG_LOG, "Optimizing search for medium set");
319             Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
320             Z_PresentRequest *pr = new_apdu->u.presentRequest;
321             pr->referenceId = sr->referenceId;
322             pr->resultSetId = sr->resultSetName;
323             pr->preferredRecordSyntax = sr->preferredRecordSyntax;
324             if (*sr->mediumSetPresentNumber < m_client->m_last_resultCount)
325                 *pr->numberOfRecordsRequested = *sr->mediumSetPresentNumber;
326             else
327                 *pr->numberOfRecordsRequested = m_client->m_last_resultCount;
328             if (sr->mediumSetElementSetNames)
329             {
330                 pr->recordComposition = (Z_RecordComposition *)
331                     odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
332                 pr->recordComposition->which = Z_RecordComp_simple;
333                 pr->recordComposition->u.simple = sr->mediumSetElementSetNames;
334             }
335             m_client->m_sr_transform = 1;
336             return new_apdu;
337         }
338         else if (m_client->m_last_resultCount >= *sr->largeSetLowerBound ||
339             m_client->m_last_resultCount <= 0)
340         {
341             // large set. Return pseudo-search response immediately
342             yaz_log (LOG_LOG, "Optimizing search for large set");
343             Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
344             new_apdu->u.searchResponse->referenceId = sr->referenceId;
345             new_apdu->u.searchResponse->resultCount =
346                 &m_client->m_last_resultCount;
347             send_Z_PDU(new_apdu);
348             return 0;
349         }
350         else
351         {
352             // small set
353             // send a present request (small set)
354             yaz_log (LOG_LOG, "Optimizing search for small set");
355             Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
356             Z_PresentRequest *pr = new_apdu->u.presentRequest;
357             pr->referenceId = sr->referenceId;
358             pr->resultSetId = sr->resultSetName;
359             pr->preferredRecordSyntax = sr->preferredRecordSyntax;
360             *pr->numberOfRecordsRequested = m_client->m_last_resultCount;
361             if (sr->smallSetElementSetNames)
362             {
363                 pr->recordComposition = (Z_RecordComposition *)
364                     odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
365                 pr->recordComposition->which = Z_RecordComp_simple;
366                 pr->recordComposition->u.simple = sr->smallSetElementSetNames;
367             }
368             m_client->m_sr_transform = 1;
369             return new_apdu;
370         }
371     }
372     else
373     {
374         delete m_client->m_last_query;
375         m_client->m_last_query = this_query;
376         m_client->m_last_ok = 0;
377
378         xfree (m_client->m_last_resultSetId);
379         m_client->m_last_resultSetId = xstrdup (sr->resultSetName);
380
381         m_client->m_last_databases.set(sr->num_databaseNames,
382                                        (const char **) sr->databaseNames);
383     }
384     return apdu;
385 }
386
387 static const char *apdu_name(Z_APDU *apdu)
388 {
389     switch (apdu->which)
390     {
391     case Z_APDU_initRequest:
392         return "initRequest";
393     case Z_APDU_initResponse:
394         return "initResponse";
395     case Z_APDU_searchRequest:
396         return "searchRequest";
397     case Z_APDU_searchResponse:
398         return "searchResponse";
399     case Z_APDU_presentRequest:
400         return "presentRequest";
401     case Z_APDU_presentResponse:
402         return "presentResponse";
403     }
404     return "other";
405 }
406
407 void Yaz_Proxy::recv_Z_PDU(Z_APDU *apdu)
408 {
409     yaz_log (LOG_LOG, "Receiving %s from client", apdu_name(apdu));
410     // Determine our client.
411     m_client = get_client(apdu);
412     if (!m_client)
413     {
414         delete this;
415         return;
416     }
417     m_client->m_server = this;
418
419     if (apdu->which == Z_APDU_initRequest)
420     {
421         if (m_client->m_init_flag)
422         {
423             Z_APDU *apdu = m_client->m_initResponse;
424             apdu->u.initResponse->otherInfo = 0;
425             if (m_client->m_cookie)
426                 set_otherInformationString(apdu, VAL_COOKIE, 1,
427                                            m_client->m_cookie);
428             send_Z_PDU(apdu);
429             return;
430         }
431         m_client->m_init_flag = 1;
432     }
433     apdu = result_set_optimize(apdu);
434     if (!apdu)
435         return;
436
437     yaz_log (LOG_LOG, "Sending %s to %s",
438                     apdu_name(apdu), m_client->get_hostname());
439
440     // delete other info part from PDU before sending to target
441     Z_OtherInformation **oi;
442     get_otherInfoAPDU(apdu, &oi);
443     if (oi)
444         *oi = 0;
445
446     if (m_client->send_Z_PDU(apdu) < 0)
447     {
448         delete m_client;
449         m_client = 0;
450         delete this;
451     }
452     else
453         m_client->m_waiting = 1;
454 }
455
456 void Yaz_Proxy::connectNotify()
457 {
458 }
459
460 void Yaz_Proxy::shutdown()
461 {
462     // only keep if keep_alive flag is set...
463     if (m_keepalive && m_client)
464     {
465         yaz_log (LOG_LOG, "shutdown (client to proxy) keepalive %s",
466                  m_client->get_hostname());
467         assert (m_client->m_waiting != 2);
468         // Tell client (if any) that no server connection is there..
469         m_client->m_server = 0;
470     }
471     else if (m_client)
472     {
473         yaz_log (LOG_LOG, "shutdown (client to proxy) close %s",
474                  m_client->get_hostname());
475         assert (m_client->m_waiting != 2);
476         delete m_client;
477     }
478     else if (!m_parent)
479     {
480         yaz_log (LOG_LOG, "shutdown (client to proxy) bad state");
481         assert (m_parent);
482     }
483     else 
484     {
485         yaz_log (LOG_LOG, "shutdown (client to proxy)");
486     }
487     delete this;
488 }
489
490 void Yaz_ProxyClient::shutdown()
491 {
492     yaz_log (LOG_LOG, "shutdown (proxy to server) %s", get_hostname());
493     delete m_server;
494     delete this;
495 }
496
497 void Yaz_Proxy::failNotify()
498 {
499     yaz_log (LOG_LOG, "Yaz_Proxy connection closed by client");
500     shutdown();
501 }
502
503 void Yaz_ProxyClient::failNotify()
504 {
505     yaz_log (LOG_LOG, "Yaz_ProxyClient connection closed by %s", get_hostname());
506     shutdown();
507 }
508
509 void Yaz_ProxyClient::connectNotify()
510 {
511     yaz_log (LOG_LOG, "Connection accepted by %s", get_hostname());
512     timeout(600);
513 }
514
515 IYaz_PDU_Observer *Yaz_ProxyClient::sessionNotify(IYaz_PDU_Observable
516                                                   *the_PDU_Observable, int fd)
517 {
518     return new Yaz_ProxyClient(the_PDU_Observable);
519 }
520
521 Yaz_ProxyClient::~Yaz_ProxyClient()
522 {
523     if (m_prev)
524         *m_prev = m_next;
525     if (m_next)
526         m_next->m_prev = m_prev;
527     m_waiting = 2;     // for debugging purposes only.
528     odr_destroy(m_init_odr);
529     delete m_last_query;
530     xfree (m_last_resultSetId);
531 }
532
533 void Yaz_Proxy::timeoutNotify()
534 {
535     yaz_log (LOG_LOG, "timeout (client to proxy)");
536     shutdown();
537 }
538
539 void Yaz_ProxyClient::timeoutNotify()
540 {
541     yaz_log (LOG_LOG, "timeout (proxy to target) %s", get_hostname());
542     shutdown();
543 }
544
545 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable) :
546     Yaz_Z_Assoc (the_PDU_Observable)
547 {
548     m_cookie[0] = 0;
549     m_next = 0;
550     m_prev = 0;
551     m_init_flag = 0;
552     m_last_query = 0;
553     m_last_resultSetId = 0;
554     m_last_resultCount = 0;
555     m_last_ok = 0;
556     m_sr_transform = 0;
557     m_waiting = 0;
558     m_init_odr = odr_createmem (ODR_DECODE);
559     m_initResponse = 0;
560 }
561
562 const char *Yaz_Proxy::option(const char *name, const char *value)
563 {
564     if (!strcmp (name, "optimize")) {
565         if (value) {
566             xfree (m_optimize); 
567             m_optimize = xstrdup (value);
568         }
569         return m_optimize;
570     }
571     return 0;
572 }
573
574 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu)
575 {
576     m_waiting = 0;
577     yaz_log (LOG_LOG, "Receiving %s from %s", apdu_name(apdu),
578                      get_hostname());
579     if (apdu->which == Z_APDU_initResponse)
580     {
581         NMEM nmem = odr_extract_mem (odr_decode());
582         odr_reset (m_init_odr);
583         nmem_transfer (m_init_odr->mem, nmem);
584         m_initResponse = apdu;
585         nmem_destroy (nmem);
586     }
587     if (apdu->which == Z_APDU_searchResponse)
588     {
589         m_last_resultCount = *apdu->u.searchResponse->resultCount;
590         int status = *apdu->u.searchResponse->searchStatus;
591         if (status && 
592                 (!apdu->u.searchResponse->records ||
593                  apdu->u.searchResponse->records->which == Z_Records_DBOSD))
594             m_last_ok = 1;
595     }
596     if (apdu->which == Z_APDU_presentResponse && m_sr_transform)
597     {
598         m_sr_transform = 0;
599         Z_PresentResponse *pr = apdu->u.presentResponse;
600         Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
601         Z_SearchResponse *sr = new_apdu->u.searchResponse;
602         sr->referenceId = pr->referenceId;
603         *sr->resultCount = m_last_resultCount;
604         sr->records = pr->records;
605         sr->nextResultSetPosition = pr->nextResultSetPosition;
606         sr->numberOfRecordsReturned = pr->numberOfRecordsReturned;
607         apdu = new_apdu;
608     }
609     if (m_cookie && *m_cookie)
610         set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
611     if (m_server)
612     {
613         yaz_log (LOG_LOG, "Sending %s to client", apdu_name(apdu));
614         m_server->send_Z_PDU(apdu);
615     }
616 }