d351988109c6d20d21cb25511f7edcb30fcb751c
[yazpp-moved-to-github.git] / src / yaz-proxy.cpp
1 /*
2  * Copyright (c) 1998-2001, Index Data.
3  * See the file LICENSE for details.
4  * 
5  * $Log: yaz-proxy.cpp,v $
6  * Revision 1.27  2001-11-02 13:06:29  adam
7  * Fixed bug in result_set_optimize where LSLB == resultCount.
8  *
9  * Revision 1.26  2001/08/13 16:39:12  adam
10  * PDU_Assoc keeps track of children. Using yaz_log instead of logf.
11  *
12  * Revision 1.25  2001/04/25 18:59:30  adam
13  * Added referenceId handling for server.
14  *
15  * Revision 1.24  2001/04/10 10:48:08  adam
16  * Fixed problem where proxy could cash bad result sets.
17  *
18  * Revision 1.23  2001/03/26 14:43:49  adam
19  * New threaded PDU association.
20  *
21  * Revision 1.22  2000/11/20 11:27:33  adam
22  * Fixes for connect operation (timeout and notify fix).
23  *
24  * Revision 1.21  2000/11/01 14:22:59  adam
25  * Added fd parameter for method IYaz_PDU_Observer::clone.
26  *
27  * Revision 1.20  2000/10/24 12:29:57  adam
28  * Fixed bug in proxy where a Yaz_ProxyClient could be owned by
29  * two Yaz_Proxy's (fatal).
30  *
31  * Revision 1.19  2000/10/11 11:58:16  adam
32  * Moved header files to include/yaz++. Switched to libtool and automake.
33  * Configure script creates yaz++-config script.
34  *
35  * Revision 1.18  2000/09/08 10:23:42  adam
36  * Added skeleton of yaz-z-server.
37  *
38  * Revision 1.17  2000/09/05 13:57:28  adam
39  * Fixed get_otherInfoAPDU to return otherInfo for extended services.
40  *
41  * Revision 1.16  2000/09/04 08:29:22  adam
42  * Fixed memory leak(s). Added re-use of associations, rather than
43  * re-init, when maximum number of targets are in use.
44  *
45  * Revision 1.15  2000/08/31 14:41:55  adam
46  * Proxy no longer generates cookies (it's up to the client). Proxy
47  * re-opens if target new op is started before previous operation finishes.
48  *
49  * Revision 1.14  2000/08/10 08:42:42  adam
50  * Fixes for {set,get}_APDU_log.
51  *
52  * Revision 1.13  2000/08/07 14:19:59  adam
53  * Fixed serious bug regarding timeouts. Improved logging for proxy.
54  *
55  * Revision 1.12  2000/07/04 13:48:49  adam
56  * Implemented upper-limit on proxy-to-target sessions.
57  *
58  * Revision 1.11  1999/12/06 13:52:45  adam
59  * Modified for new location of YAZ header files. Experimental threaded
60  * operation.
61  *
62  * Revision 1.10  1999/11/10 10:02:34  adam
63  * Work on proxy.
64  *
65  * Revision 1.9  1999/09/13 12:53:44  adam
66  * Proxy removes OtherInfo Proxy Address and Session ID. Other
67  * Otherinfo remains untouched.
68  *
69  * Revision 1.8  1999/05/04 10:53:00  adam
70  * Changed the way the PROXY behaves when lost cookie is received.
71  *
72  * Revision 1.7  1999/04/28 13:31:17  adam
73  * Better result set optimisation for proxy.
74  *
75  * Revision 1.6  1999/04/27 07:52:13  adam
76  * Improved proxy; added query match for result set re-use.
77  *
78  * Revision 1.5  1999/04/21 12:09:01  adam
79  * Many improvements. Modified to proxy server to work with "sessions"
80  * based on cookies.
81  *
82  * Revision 1.4  1999/04/20 10:30:05  adam
83  * Implemented various stuff for client and proxy. Updated calls
84  * to ODR to reflect new name parameter.
85  *
86  * Revision 1.3  1999/04/09 11:46:57  adam
87  * Added object Yaz_Z_Assoc. Much more functional client.
88  *
89  * Revision 1.2  1999/01/28 13:08:46  adam
90  * Yaz_PDU_Assoc better encapsulated. Memory leak fix in
91  * yaz-socket-manager.cc.
92  *
93  * Revision 1.1.1.1  1999/01/28 09:41:07  adam
94  * First implementation of YAZ++.
95  *
96  */
97
98 #include <assert.h>
99 #include <time.h>
100
101 #include <yaz/log.h>
102 #include <yaz++/yaz-proxy.h>
103
104 Yaz_Proxy::Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable) :
105     Yaz_Z_Assoc(the_PDU_Observable)
106 {
107     m_PDU_Observable = the_PDU_Observable;
108     m_client = 0;
109     m_parent = 0;
110     m_clientPool = 0;
111     m_seqno = 1;
112     m_keepalive = 1;
113     m_proxyTarget = 0;
114     m_max_clients = 50;
115     m_seed = time(0);
116 }
117
118 Yaz_Proxy::~Yaz_Proxy()
119 {
120     xfree (m_proxyTarget);
121 }
122
123 void Yaz_Proxy::set_proxyTarget(const char *target)
124 {
125     xfree (m_proxyTarget);
126     m_proxyTarget = 0;
127     if (target)
128         m_proxyTarget = (char *) xstrdup (target);
129 }
130
131 IYaz_PDU_Observer *Yaz_Proxy::sessionNotify(IYaz_PDU_Observable
132                                             *the_PDU_Observable, int fd)
133 {
134     Yaz_Proxy *new_proxy = new Yaz_Proxy(the_PDU_Observable);
135     new_proxy->m_parent = this;
136     new_proxy->timeout(500);
137     new_proxy->set_proxyTarget(m_proxyTarget);
138     new_proxy->set_APDU_log(get_APDU_log());
139     return new_proxy;
140 }
141
142 char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo)
143 {
144     int oid[OID_SIZE];
145     Z_OtherInformationUnit *oi;
146     struct oident ent;
147     ent.proto = PROTO_Z3950;
148     ent.oclass = CLASS_USERINFO;
149     ent.value = (oid_value) VAL_COOKIE;
150     assert (oid_ent_to_oid (&ent, oid));
151
152     if (oid_ent_to_oid (&ent, oid) && 
153         (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
154         oi->which == Z_OtherInfo_characterInfo)
155         return oi->information.characterInfo;
156     return 0;
157 }
158
159 char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
160 {
161     int oid[OID_SIZE];
162     Z_OtherInformationUnit *oi;
163     struct oident ent;
164     ent.proto = PROTO_Z3950;
165     ent.oclass = CLASS_USERINFO;
166     ent.value = (oid_value) VAL_PROXY;
167     if (oid_ent_to_oid (&ent, oid) &&
168         (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
169         oi->which == Z_OtherInfo_characterInfo)
170         return oi->information.characterInfo;
171     return 0;
172 }
173
174 Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu)
175 {
176     assert (m_parent);
177     Yaz_Proxy *parent = m_parent;
178     Z_OtherInformation **oi;
179     Yaz_ProxyClient *c = m_client;
180     
181     get_otherInfoAPDU(apdu, &oi);
182     char *cookie = get_cookie(oi);
183     yaz_log (LOG_LOG, "Yaz_Proxy::get_client cookie=%s", cookie ? cookie :
184           "null");
185
186     const char *proxy_host = get_proxy(oi);
187     if (proxy_host)
188         set_proxyTarget(proxy_host);
189     yaz_log (LOG_LOG, "proxy_host = %s", m_proxyTarget ? m_proxyTarget:"none");
190     
191     // no target specified at all?
192     if (!m_proxyTarget)
193         return 0;
194
195     if (cookie && *cookie)
196     {
197         yaz_log (LOG_LOG, "lookup of clients cookie=%s target=%s",
198               cookie, m_proxyTarget);
199         Yaz_ProxyClient *cc = 0;
200         
201         for (c = parent->m_clientPool; c; c = c->m_next)
202         {
203             yaz_log (LOG_LOG, " found client cookie = %s target=%s seqno=%d",
204                   c->m_cookie, c->get_hostname(), c->m_seqno);
205             assert (c->m_prev);
206             assert (*c->m_prev == c);
207             if (!strcmp(cookie,c->m_cookie) &&
208                 !strcmp(m_proxyTarget, c->get_hostname()))
209             {
210                 yaz_log (LOG_LOG, "found!");
211                 cc = c;
212             }
213         }
214         if (cc)
215         {
216             // found it in cache
217             c = cc;
218             // The following handles "cancel"
219             // If connection is busy (waiting for PDU) and
220             // we have an initRequest we can safely do re-open
221             if (c->m_waiting && apdu->which == Z_APDU_initRequest)
222             {
223                 yaz_log (LOG_LOG, "reopen target=%s", c->get_hostname());
224                 c->close();
225                 c->client(m_proxyTarget);
226                 c->m_init_flag = 0;
227                 
228                 delete c->m_last_query;
229                 c->m_last_query = 0;
230                 c->m_last_resultCount = 0;
231                 c->m_sr_transform = 0;
232                 c->m_waiting = 0;
233                 c->timeout(600); 
234             }
235             c->m_seqno = parent->m_seqno;
236             if (c->m_server && c->m_server != this)
237                 c->m_server->m_client = 0;
238             c->m_server = this;
239             c->m_seqno = parent->m_seqno;
240             (parent->m_seqno)++;
241             yaz_log (LOG_LOG, "get_client 1 %p %p", this, c);
242             return c;
243         }
244     }
245     if (!m_client)
246     {
247         if (apdu->which != Z_APDU_initRequest)
248         {
249             yaz_log (LOG_LOG, "no first INIT!");
250             return 0;
251         }
252         yaz_log (LOG_LOG, "got InitRequest");
253             
254         // go through list of clients - and find the lowest/oldest one.
255         Yaz_ProxyClient *c_min = 0;
256         int min_seq = -1;
257         int no_of_clients = 0;
258         for (c = parent->m_clientPool; c; c = c->m_next)
259         {
260             no_of_clients++;
261             if (min_seq < 0 || c->m_seqno < min_seq)
262             {
263                 min_seq = c->m_seqno;
264                 c_min = c;
265             }
266         }
267         if (no_of_clients >= parent->m_max_clients)
268         {
269             c = c_min;
270             if (c->m_waiting || strcmp(m_proxyTarget, c->get_hostname()))
271             {
272                 yaz_log (LOG_LOG, "Yaz_Proxy::get_client re-init session %d",
273                       c->m_seqno);
274                 if (c->m_server && c->m_server != this)
275                     delete c->m_server;
276                 c->m_server = 0;
277             }
278             else
279             {
280                 yaz_log (LOG_LOG,
281                          "Yaz_Proxy::get_client re-use session %d to %d",
282                       c->m_seqno, parent->m_seqno);
283                 if (cookie)
284                     strcpy (c->m_cookie, cookie);
285                 else
286                     c->m_cookie[0] = '\0';
287                 c->m_seqno = parent->m_seqno;
288                 if (c->m_server && c->m_server != this)
289                 {
290                     c->m_server->m_client = 0;
291                     delete c->m_server;
292                 }
293                 (parent->m_seqno)++;
294                 yaz_log (LOG_LOG, "get_client 2 %p %p", this, c);
295                 return c;
296             }
297         }
298         else
299         {
300             yaz_log (LOG_LOG, "Yaz_Proxy::get_client making session %d",
301                   parent->m_seqno);
302             c = new Yaz_ProxyClient(m_PDU_Observable->clone());
303             c->m_next = parent->m_clientPool;
304             if (c->m_next)
305                 c->m_next->m_prev = &c->m_next;
306             parent->m_clientPool = c;
307             c->m_prev = &parent->m_clientPool;
308         }
309         if (cookie)
310             strcpy (c->m_cookie, cookie);
311         else
312             c->m_cookie[0] = '\0';
313         yaz_log (LOG_LOG, "Yaz_Proxy::get_client connect to %s",
314                  m_proxyTarget);
315         c->m_seqno = parent->m_seqno;
316         c->client(m_proxyTarget);
317         c->m_init_flag = 0;
318
319         delete c->m_last_query;
320         c->m_last_query = 0;
321         c->m_last_resultCount = 0;
322         c->m_sr_transform = 0;
323         c->m_waiting = 0;
324         c->timeout(10);
325
326         (parent->m_seqno)++;
327     }
328     yaz_log (LOG_LOG, "get_client 3 %p %p", this, c);
329     return c;
330 }
331
332 Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
333 {
334     if (apdu->which != Z_APDU_searchRequest)
335         return apdu;
336     Z_SearchRequest *sr = apdu->u.searchRequest;
337     Yaz_Z_Query *this_query = new Yaz_Z_Query;
338     
339     this_query->set_Z_Query(sr->query);
340     
341     if (m_client->m_last_query &&
342         m_client->m_last_query->match(this_query))
343     {
344         delete this_query;
345         if (m_client->m_last_resultCount > *sr->smallSetUpperBound &&
346             m_client->m_last_resultCount < *sr->largeSetLowerBound)
347         {
348             // medium Set
349             yaz_log (LOG_LOG, "Yaz_Proxy::result_set_optimize medium set");
350             Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
351             Z_PresentRequest *pr = new_apdu->u.presentRequest;
352             pr->referenceId = sr->referenceId;
353             pr->resultSetId = sr->resultSetName;
354             pr->preferredRecordSyntax = sr->preferredRecordSyntax;
355             *pr->numberOfRecordsRequested = *sr->mediumSetPresentNumber;
356             if (sr->mediumSetElementSetNames)
357             {
358                 pr->recordComposition = (Z_RecordComposition *)
359                     odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
360                 pr->recordComposition->which = Z_RecordComp_simple;
361                 pr->recordComposition->u.simple = sr->mediumSetElementSetNames;
362             }
363             m_client->m_sr_transform = 1;
364             return new_apdu;
365         }
366         else if (m_client->m_last_resultCount >= *sr->largeSetLowerBound ||
367             m_client->m_last_resultCount == 0)
368         {
369             // large set
370             yaz_log (LOG_LOG, "Yaz_Proxy::result_set_optimize large set");
371             Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
372             new_apdu->u.searchResponse->referenceId = sr->referenceId;
373             new_apdu->u.searchResponse->resultCount =
374                 &m_client->m_last_resultCount;
375             send_Z_PDU(new_apdu);
376             return 0;
377         }
378         else
379         {
380             // small set
381             yaz_log (LOG_LOG, "Yaz_Proxy::result_set_optimize small set");
382             Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
383             Z_PresentRequest *pr = new_apdu->u.presentRequest;
384             pr->referenceId = sr->referenceId;
385             pr->resultSetId = sr->resultSetName;
386             pr->preferredRecordSyntax = sr->preferredRecordSyntax;
387             *pr->numberOfRecordsRequested = m_client->m_last_resultCount;
388             if (sr->smallSetElementSetNames)
389             {
390                 pr->recordComposition = (Z_RecordComposition *)
391                     odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
392                 pr->recordComposition->which = Z_RecordComp_simple;
393                 pr->recordComposition->u.simple = sr->smallSetElementSetNames;
394             }
395             m_client->m_sr_transform = 1;
396             return new_apdu;
397         }
398     }
399     else
400     {
401         yaz_log (LOG_LOG, "Yaz_Proxy::result_set_optimize new set");
402         delete m_client->m_last_query;
403         m_client->m_last_query = this_query;
404     }
405     return apdu;
406 }
407
408 void Yaz_Proxy::recv_Z_PDU(Z_APDU *apdu)
409 {
410     yaz_log (LOG_LOG, "Yaz_Proxy::recv_Z_PDU");
411     // Determine our client.
412     m_client = get_client(apdu);
413     if (!m_client)
414     {
415         delete this;
416         return;
417     }
418     m_client->m_server = this;
419
420     if (apdu->which == Z_APDU_initRequest)
421     {
422         if (m_client->m_init_flag)
423         {
424             Z_APDU *apdu = create_Z_PDU(Z_APDU_initResponse);
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, "Yaz_ProxyClient::send_Z_PDU %s",
438              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     yaz_log (LOG_LOG, "shutdown (client to proxy)");
463     // only keep if keep_alive flag and cookie is set...
464     if (m_keepalive && m_client && m_client->m_cookie[0])
465     {
466         yaz_log (LOG_LOG, "shutdown - keepalive this=%p, m_server=%p",
467             this, m_client->m_server);
468         if (m_client->m_waiting == 2)
469             abort();
470         // Tell client (if any) that no server connection is there..
471         m_client->m_server = 0;
472     }
473     else if (m_client)
474     {
475         yaz_log (LOG_LOG, "deleting %p %p", this, m_client);
476         if (m_client->m_waiting == 2)
477             abort();
478         delete m_client;
479     }
480     else if (!m_parent)
481     {
482         yaz_log (LOG_LOG, "abort %p", this);
483         abort();
484     }
485     delete this;
486 }
487
488 void Yaz_ProxyClient::shutdown()
489 {
490     yaz_log (LOG_LOG, "shutdown (proxy to server) %s", get_hostname());
491     delete m_server;
492     delete this;
493 }
494
495 void Yaz_Proxy::failNotify()
496 {
497     yaz_log (LOG_LOG, "connection closed by client");
498     shutdown();
499 }
500
501 void Yaz_ProxyClient::failNotify()
502 {
503     yaz_log (LOG_LOG, "Yaz_ProxyClient connection closed by %s", get_hostname());
504     shutdown();
505 }
506
507 void Yaz_ProxyClient::connectNotify()
508 {
509     yaz_log (LOG_LOG, "Yaz_ProxyClient connection accepted by %s",
510           get_hostname());
511     timeout(600);
512 }
513
514 IYaz_PDU_Observer *Yaz_ProxyClient::sessionNotify(IYaz_PDU_Observable
515                                                   *the_PDU_Observable, int fd)
516 {
517     return new Yaz_ProxyClient(the_PDU_Observable);
518 }
519
520 Yaz_ProxyClient::~Yaz_ProxyClient()
521 {
522     if (m_prev)
523         *m_prev = m_next;
524     if (m_next)
525         m_next->m_prev = m_prev;
526     m_waiting = 2;     // for debugging purposes only.
527     delete m_last_query;
528 }
529
530 void Yaz_Proxy::timeoutNotify()
531 {
532     yaz_log (LOG_LOG, "timeout (client to proxy)");
533     shutdown();
534 }
535
536 void Yaz_ProxyClient::timeoutNotify()
537 {
538     yaz_log (LOG_LOG, "timeout (proxy to target) %s", get_hostname());
539     shutdown();
540 }
541
542 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable) :
543     Yaz_Z_Assoc (the_PDU_Observable)
544 {
545     m_cookie[0] = 0;
546     m_next = 0;
547     m_prev = 0;
548     m_init_flag = 0;
549     m_last_query = 0;
550     m_last_resultCount = 0;
551     m_sr_transform = 0;
552     m_waiting = 0;
553 }
554
555 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu)
556 {
557     m_waiting = 0;
558     yaz_log (LOG_LOG, "Yaz_ProxyClient::recv_Z_PDU %s", get_hostname());
559     if (apdu->which == Z_APDU_searchResponse)
560     {
561         m_last_resultCount = *apdu->u.searchResponse->resultCount;
562         int status = *apdu->u.searchResponse->searchStatus;
563         if (! status || (
564                 apdu->u.searchResponse->records &&
565                 apdu->u.searchResponse->records->which != Z_Records_DBOSD))
566         {
567             delete m_last_query;
568             m_last_query = 0;
569         }
570     }
571     if (apdu->which == Z_APDU_presentResponse && m_sr_transform)
572     {
573         m_sr_transform = 0;
574         Z_PresentResponse *pr = apdu->u.presentResponse;
575         Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
576         Z_SearchResponse *sr = new_apdu->u.searchResponse;
577         sr->referenceId = pr->referenceId;
578         *sr->resultCount = m_last_resultCount;
579         sr->records = pr->records;
580         sr->nextResultSetPosition = pr->nextResultSetPosition;
581         sr->numberOfRecordsReturned = pr->numberOfRecordsReturned;
582         apdu = new_apdu;
583     }
584     if (m_cookie)
585         set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
586     if (m_server)
587     {
588         yaz_log (LOG_LOG, "Yaz_Proxy::send_Z_PDU");
589         m_server->send_Z_PDU(apdu);
590     }
591 }