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