Fixed serious bug regarding timeouts. Improved logging for proxy.
[yazpp-moved-to-github.git] / src / yaz-proxy.cpp
1 /*
2  * Copyright (c) 1998-1999, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  * 
6  * $Log: yaz-proxy.cpp,v $
7  * Revision 1.13  2000-08-07 14:19:59  adam
8  * Fixed serious bug regarding timeouts. Improved logging for proxy.
9  *
10  * Revision 1.12  2000/07/04 13:48:49  adam
11  * Implemented upper-limit on proxy-to-target sessions.
12  *
13  * Revision 1.11  1999/12/06 13:52:45  adam
14  * Modified for new location of YAZ header files. Experimental threaded
15  * operation.
16  *
17  * Revision 1.10  1999/11/10 10:02:34  adam
18  * Work on proxy.
19  *
20  * Revision 1.9  1999/09/13 12:53:44  adam
21  * Proxy removes OtherInfo Proxy Address and Session ID. Other
22  * Otherinfo remains untouched.
23  *
24  * Revision 1.8  1999/05/04 10:53:00  adam
25  * Changed the way the PROXY behaves when lost cookie is received.
26  *
27  * Revision 1.7  1999/04/28 13:31:17  adam
28  * Better result set optimisation for proxy.
29  *
30  * Revision 1.6  1999/04/27 07:52:13  adam
31  * Improved proxy; added query match for result set re-use.
32  *
33  * Revision 1.5  1999/04/21 12:09:01  adam
34  * Many improvements. Modified to proxy server to work with "sessions"
35  * based on cookies.
36  *
37  * Revision 1.4  1999/04/20 10:30:05  adam
38  * Implemented various stuff for client and proxy. Updated calls
39  * to ODR to reflect new name parameter.
40  *
41  * Revision 1.3  1999/04/09 11:46:57  adam
42  * Added object Yaz_Z_Assoc. Much more functional client.
43  *
44  * Revision 1.2  1999/01/28 13:08:46  adam
45  * Yaz_PDU_Assoc better encapsulated. Memory leak fix in
46  * yaz-socket-manager.cc.
47  *
48  * Revision 1.1.1.1  1999/01/28 09:41:07  adam
49  * First implementation of YAZ++.
50  *
51  */
52
53 #include <assert.h>
54 #include <time.h>
55
56 #include <yaz/log.h>
57 #include <yaz-proxy.h>
58
59 Yaz_Proxy::Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable) :
60     Yaz_Z_Assoc(the_PDU_Observable)
61 {
62     m_PDU_Observable = the_PDU_Observable;
63     m_client = 0;
64     m_parent = 0;
65     m_clientPool = 0;
66     m_seqno = 1;
67     m_keepalive = 1;
68     m_proxyTarget = 0;
69     m_max_clients = 50;
70     m_APDU_fname = 0;
71     m_seed = time(0);
72 }
73
74 Yaz_Proxy::~Yaz_Proxy()
75 {
76     xfree (m_proxyTarget);
77 }
78
79 void Yaz_Proxy::set_proxyTarget(const char *target)
80 {
81     xfree (m_proxyTarget);
82     m_proxyTarget = 0;
83     if (target)
84         m_proxyTarget = (char *) xstrdup (target);
85 }
86
87 IYaz_PDU_Observer *Yaz_Proxy::clone(IYaz_PDU_Observable
88                                     *the_PDU_Observable)
89 {
90     Yaz_Proxy *new_proxy = new Yaz_Proxy(the_PDU_Observable);
91     new_proxy->m_parent = this;
92     new_proxy->timeout(120);
93     new_proxy->set_proxyTarget(m_proxyTarget);
94     new_proxy->set_APDU_log(get_APDU_log());
95     return new_proxy;
96 }
97
98 char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo)
99 {
100     int oid[OID_SIZE];
101     Z_OtherInformationUnit *oi;
102     struct oident ent;
103     ent.proto = PROTO_Z3950;
104     ent.oclass = CLASS_USERINFO;
105     ent.value = (oid_value) VAL_COOKIE;
106     assert (oid_ent_to_oid (&ent, oid));
107
108     if (oid_ent_to_oid (&ent, oid) && 
109         (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
110         oi->which == Z_OtherInfo_characterInfo)
111         return oi->information.characterInfo;
112     return 0;
113 }
114
115 char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
116 {
117     int oid[OID_SIZE];
118     Z_OtherInformationUnit *oi;
119     struct oident ent;
120     ent.proto = PROTO_Z3950;
121     ent.oclass = CLASS_USERINFO;
122     ent.value = (oid_value) VAL_PROXY;
123     if (oid_ent_to_oid (&ent, oid) &&
124         (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
125         oi->which == Z_OtherInfo_characterInfo)
126         return oi->information.characterInfo;
127     return 0;
128 }
129
130 Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu)
131 {
132     assert (m_parent);
133     Yaz_Proxy *parent = m_parent;
134     Z_OtherInformation **oi;
135     Yaz_ProxyClient *c = m_client;
136     
137     get_otherInfoAPDU(apdu, &oi);
138     char *cookie = get_cookie(oi);
139     logf (LOG_LOG, "Yaz_Proxy::get_client cookie=%s", cookie ? cookie :
140           "null");
141     if (cookie)
142     {
143         for (c = parent->m_clientPool; c; c = c->m_next)
144         {
145             assert (c->m_prev);
146             assert (*c->m_prev == c);
147             if (!strcmp(cookie,c->m_cookie))
148             {
149                 logf (LOG_LOG, "Yaz_Proxy::get_client cached %s",
150                       c->get_hostname());
151                 c->m_seqno = parent->m_seqno;
152                 return c;
153             }
154         }       
155     }
156     if (!m_client)
157     {
158         if (apdu->which == Z_APDU_initRequest)
159         {
160             logf (LOG_LOG, "got InitRequest");
161             
162             const char *proxy_host =
163                 get_proxy(&apdu->u.initRequest->otherInfo);
164             if (proxy_host)
165                 set_proxyTarget(proxy_host);
166             logf (LOG_LOG, "proxy_host = %s", m_proxyTarget ?
167                   m_proxyTarget:"none");
168         }
169         else
170         {
171             logf (LOG_LOG, "no first INIT!");
172             return 0;
173         }
174         if (!m_proxyTarget)
175             return 0;
176
177         Yaz_ProxyClient *c_min = 0;
178         int min_seq = -1;
179         int no_of_clients = 0;
180         for (c = parent->m_clientPool; c; c = c->m_next)
181         {
182             no_of_clients++;
183             logf (LOG_LOG, "found seqno = %d", c->m_seqno);
184             if (min_seq < 0 || c->m_seqno < min_seq)
185             {
186                 min_seq = c->m_seqno;
187                 c_min = c;
188             }
189         }
190         if (no_of_clients >= parent->m_max_clients)
191         {
192             c = c_min;
193             logf (LOG_LOG, "Yaz_Proxy::get_client re-using session %d",
194                   c->m_seqno);
195             if (c->m_server)
196                 delete c->m_server;
197             c->m_server = 0;
198         }
199         else
200         {
201             logf (LOG_LOG, "Yaz_Proxy::get_client making session %d",
202                   parent->m_seqno);
203             c = new Yaz_ProxyClient(m_PDU_Observable->clone());
204             c->m_next = parent->m_clientPool;
205             if (c->m_next)
206                 c->m_next->m_prev = &c->m_next;
207             parent->m_clientPool = c;
208             c->m_prev = &parent->m_clientPool;
209         }
210         sprintf (c->m_cookie, "%lx.%d", m_seed, parent->m_seqno);
211         logf (LOG_LOG, "Yaz_Proxy::get_client connect to %s", m_proxyTarget);
212         c->m_seqno = parent->m_seqno;
213         c->client(m_proxyTarget);
214         c->m_init_flag = 0;
215
216         delete c->m_last_query;
217         c->m_last_query = 0;
218         c->m_last_resultCount = 0;
219         c->m_sr_transform = 0;
220
221         c->timeout(600);
222
223         (parent->m_seqno)++;
224     }
225     return c;
226 }
227
228 Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
229 {
230     if (apdu->which != Z_APDU_searchRequest)
231         return apdu;
232     Z_SearchRequest *sr = apdu->u.searchRequest;
233     Yaz_Z_Query *this_query = new Yaz_Z_Query;
234     
235     this_query->set_Z_Query(sr->query);
236     
237     if (m_client->m_last_query &&
238         m_client->m_last_query->match(this_query))
239     {
240         delete this_query;
241         if (m_client->m_last_resultCount > *sr->smallSetUpperBound &&
242             m_client->m_last_resultCount < *sr->largeSetLowerBound)
243         {
244             // medium Set
245             logf (LOG_LOG, "Yaz_Proxy::result_set_optimize medium set");
246             Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
247             Z_PresentRequest *pr = new_apdu->u.presentRequest;
248             pr->referenceId = sr->referenceId;
249             pr->resultSetId = sr->resultSetName;
250             pr->preferredRecordSyntax = sr->preferredRecordSyntax;
251             *pr->numberOfRecordsRequested = *sr->mediumSetPresentNumber;
252             if (sr->mediumSetElementSetNames)
253             {
254                 pr->recordComposition = (Z_RecordComposition *)
255                     odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
256                 pr->recordComposition->which = Z_RecordComp_simple;
257                 pr->recordComposition->u.simple = sr->mediumSetElementSetNames;
258             }
259             m_client->m_sr_transform = 1;
260             return new_apdu;
261         }
262         else if (m_client->m_last_resultCount >= *sr->largeSetLowerBound ||
263             m_client->m_last_resultCount == 0)
264         {
265             // large set
266             logf (LOG_LOG, "Yaz_Proxy::result_set_optimize large set");
267             Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
268             new_apdu->u.searchResponse->referenceId = sr->referenceId;
269             new_apdu->u.searchResponse->resultCount =
270                 &m_client->m_last_resultCount;
271             send_Z_PDU(new_apdu);
272             return 0;
273         }
274         else
275         {
276             // small set
277             logf (LOG_LOG, "Yaz_Proxy::result_set_optimize small set");
278             Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
279             Z_PresentRequest *pr = new_apdu->u.presentRequest;
280             pr->referenceId = sr->referenceId;
281             pr->resultSetId = sr->resultSetName;
282             pr->preferredRecordSyntax = sr->preferredRecordSyntax;
283             *pr->numberOfRecordsRequested = m_client->m_last_resultCount;
284             if (sr->smallSetElementSetNames)
285             {
286                 pr->recordComposition = (Z_RecordComposition *)
287                     odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
288                 pr->recordComposition->which = Z_RecordComp_simple;
289                 pr->recordComposition->u.simple = sr->smallSetElementSetNames;
290             }
291             m_client->m_sr_transform = 1;
292             return new_apdu;
293         }
294     }
295     else
296     {
297         logf (LOG_LOG, "Yaz_Proxy::result_set_optimize new set");
298         delete m_client->m_last_query;
299         m_client->m_last_query = this_query;
300     }
301     return apdu;
302 }
303
304 void Yaz_Proxy::recv_Z_PDU(Z_APDU *apdu)
305 {
306     logf (LOG_LOG, "Yaz_Proxy::recv_Z_PDU");
307     // Determine our client.
308     m_client = get_client(apdu);
309     if (!m_client)
310     {
311         delete this;
312         return;
313     }
314     m_client->m_server = this;
315
316 #if 0
317     Z_OtherInformation **oi;
318     get_otherInfoAPDU(apdu, &oi);
319     *oi = 0;
320 #endif
321
322     if (apdu->which == Z_APDU_initRequest)
323     {
324         if (m_client->m_init_flag)
325         {
326             Z_APDU *apdu = create_Z_PDU(Z_APDU_initResponse);
327             if (m_client->m_cookie)
328                 set_otherInformationString(apdu, VAL_COOKIE, 1,
329                                            m_client->m_cookie);
330             send_Z_PDU(apdu);
331             return;
332         }
333         m_client->m_init_flag = 1;
334     }
335     apdu = result_set_optimize(apdu);
336     if (!apdu)
337         return;
338
339     logf (LOG_LOG, "Yaz_ProxyClient::send_Z_PDU %s", m_client->get_hostname());
340     if (m_client->send_Z_PDU(apdu) < 0)
341     {
342         delete m_client;
343         delete this;
344     }
345 }
346
347 void Yaz_Proxy::connectNotify()
348 {
349 }
350
351 void Yaz_Proxy::shutdown()
352 {
353     logf (LOG_LOG, "shutdown (client to proxy)");
354     if (m_keepalive)
355     {
356         // Tell client (if any) that no server connection is there..
357         if (m_client)
358             m_client->m_server = 0;
359     }
360     else
361     {
362         delete m_client;
363     }
364     delete this;
365 }
366
367 void Yaz_ProxyClient::shutdown()
368 {
369     logf (LOG_LOG, "shutdown (proxy to server) %s", get_hostname());
370     delete m_server;
371     delete this;
372 }
373
374 void Yaz_Proxy::failNotify()
375 {
376     logf (LOG_LOG, "connection closed by client");
377     shutdown();
378 }
379
380 void Yaz_ProxyClient::failNotify()
381 {
382     logf (LOG_LOG, "Yaz_ProxyClient connection closed by %s", get_hostname());
383     shutdown();
384 }
385
386 void Yaz_ProxyClient::connectNotify()
387 {
388     logf (LOG_LOG, "Yaz_ProxyClient connection accept by %s", get_hostname());
389 }
390
391 IYaz_PDU_Observer *Yaz_ProxyClient::clone(IYaz_PDU_Observable
392                                           *the_PDU_Observable)
393 {
394     return new Yaz_ProxyClient(the_PDU_Observable);
395 }
396
397 Yaz_ProxyClient::~Yaz_ProxyClient()
398 {
399     if (m_prev)
400     {
401         *m_prev = m_next;
402         if (m_next)
403             m_next->m_prev = m_prev;
404     }
405     delete m_last_query;
406 }
407
408 void Yaz_Proxy::timeoutNotify()
409 {
410     logf (LOG_LOG, "timeout (client to proxy)");
411     shutdown();
412 }
413
414 void Yaz_ProxyClient::timeoutNotify()
415 {
416     logf (LOG_LOG, "timeout (proxy to target) %s", get_hostname());
417     shutdown();
418 }
419
420 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable) :
421     Yaz_Z_Assoc (the_PDU_Observable)
422 {
423     m_cookie[0] = 0;
424     m_next = 0;
425     m_prev = 0;
426     m_init_flag = 0;
427     m_last_query = 0;
428     m_last_resultCount = 0;
429     m_sr_transform = 0;
430 }
431
432 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu)
433 {
434     logf (LOG_LOG, "Yaz_ProxyClient::recv_Z_PDU %s", get_hostname());
435     if (apdu->which == Z_APDU_searchResponse)
436         m_last_resultCount = *apdu->u.searchResponse->resultCount;
437     if (apdu->which == Z_APDU_presentResponse && m_sr_transform)
438     {
439         m_sr_transform = 0;
440         Z_PresentResponse *pr = apdu->u.presentResponse;
441         Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
442         Z_SearchResponse *sr = new_apdu->u.searchResponse;
443         sr->referenceId = pr->referenceId;
444         *sr->resultCount = m_last_resultCount;
445         sr->records = pr->records;
446         sr->nextResultSetPosition = pr->nextResultSetPosition;
447         sr->numberOfRecordsReturned = pr->numberOfRecordsReturned;
448         apdu = new_apdu;
449     }
450     if (m_cookie)
451         set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
452     if (m_server)
453     {
454         logf (LOG_LOG, "Yaz_Proxy::send_Z_PDU");
455         m_server->send_Z_PDU(apdu);
456     }
457 }