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