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