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