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