apdu_name prints more names
[yazpp-moved-to-github.git] / src / yaz-proxy.cpp
1 /*
2  * Copyright (c) 1998-2003, Index Data.
3  * See the file LICENSE for details.
4  * 
5  * $Id: yaz-proxy.cpp,v 1.41 2003-06-11 21:59:07 adam Exp $
6  */
7
8 #include <assert.h>
9 #include <time.h>
10
11 #include <yaz/log.h>
12 #include <yaz++/proxy.h>
13
14 Yaz_Proxy::Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable) :
15     Yaz_Z_Assoc(the_PDU_Observable)
16 {
17     m_PDU_Observable = the_PDU_Observable;
18     m_client = 0;
19     m_parent = 0;
20     m_clientPool = 0;
21     m_seqno = 1;
22     m_keepalive = 1;
23     m_proxyTarget = 0;
24     m_proxy_authentication = 0;
25     m_max_clients = 50;
26     m_seed = time(0);
27     m_idletime = 600;
28     m_optimize = xstrdup ("1");
29 }
30
31 Yaz_Proxy::~Yaz_Proxy()
32 {
33     xfree (m_proxyTarget);
34     xfree (m_proxy_authentication);
35     xfree (m_optimize);
36 }
37
38 void Yaz_Proxy::set_proxy_target(const char *target)
39 {
40     xfree (m_proxyTarget);
41     m_proxyTarget = 0;
42     if (target)
43         m_proxyTarget = (char *) xstrdup (target);
44 }
45
46 void Yaz_Proxy::set_proxy_authentication (const char *auth)
47 {
48     xfree (m_proxy_authentication);
49     m_proxy_authentication = 0;
50     if (auth)
51         m_proxy_authentication = (char *) xstrdup (auth);
52 }
53
54 IYaz_PDU_Observer *Yaz_Proxy::sessionNotify(IYaz_PDU_Observable
55                                             *the_PDU_Observable, int fd)
56 {
57     Yaz_Proxy *new_proxy = new Yaz_Proxy(the_PDU_Observable);
58     new_proxy->m_parent = this;
59     new_proxy->timeout(m_idletime);
60     new_proxy->set_proxy_target(m_proxyTarget);
61     new_proxy->set_APDU_log(get_APDU_log());
62     new_proxy->set_proxy_authentication(m_proxy_authentication);
63     yaz_log (LOG_LOG, "New session p=%p", new_proxy);
64     return new_proxy;
65 }
66
67 char *Yaz_Proxy::get_cookie(Z_OtherInformation **otherInfo)
68 {
69     int oid[OID_SIZE];
70     Z_OtherInformationUnit *oi;
71     struct oident ent;
72     ent.proto = PROTO_Z3950;
73     ent.oclass = CLASS_USERINFO;
74     ent.value = (oid_value) VAL_COOKIE;
75     assert (oid_ent_to_oid (&ent, oid));
76
77     if (oid_ent_to_oid (&ent, oid) && 
78         (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
79         oi->which == Z_OtherInfo_characterInfo)
80         return oi->information.characterInfo;
81     return 0;
82 }
83
84 char *Yaz_Proxy::get_proxy(Z_OtherInformation **otherInfo)
85 {
86     int oid[OID_SIZE];
87     Z_OtherInformationUnit *oi;
88     struct oident ent;
89     ent.proto = PROTO_Z3950;
90     ent.oclass = CLASS_USERINFO;
91     ent.value = (oid_value) VAL_PROXY;
92     if (oid_ent_to_oid (&ent, oid) &&
93         (oi = update_otherInformation(otherInfo, 0, oid, 1, 1)) &&
94         oi->which == Z_OtherInfo_characterInfo)
95         return oi->information.characterInfo;
96     return 0;
97 }
98
99 Yaz_ProxyClient *Yaz_Proxy::get_client(Z_APDU *apdu)
100 {
101     assert (m_parent);
102     Yaz_Proxy *parent = m_parent;
103     Z_OtherInformation **oi;
104     Yaz_ProxyClient *c = m_client;
105     
106     get_otherInfoAPDU(apdu, &oi);
107     char *cookie = get_cookie(oi);
108
109     const char *proxy_host = get_proxy(oi);
110     if (proxy_host)
111         set_proxy_target(proxy_host);
112     
113     // no target specified at all?
114     if (!m_proxyTarget)
115         return 0;
116
117     if (!strcmp(m_proxyTarget, "stop"))
118         exit (0);
119     if (cookie && *cookie)
120     {
121         Yaz_ProxyClient *cc = 0;
122         
123         for (c = parent->m_clientPool; c; c = c->m_next)
124         {
125             assert (c->m_prev);
126             assert (*c->m_prev == c);
127             if (!strcmp(cookie,c->m_cookie) &&
128                 !strcmp(m_proxyTarget, c->get_hostname()))
129             {
130                 cc = c;
131             }
132         }
133         if (cc)
134         {
135             // found it in cache
136             c = cc;
137             // The following handles "cancel"
138             // If connection is busy (waiting for PDU) and
139             // we have an initRequest we can safely do re-open
140             if (c->m_waiting && apdu->which == Z_APDU_initRequest)
141             {
142                 yaz_log (LOG_LOG, "reopen target=%s", c->get_hostname());
143                 c->close();
144                 c->client(m_proxyTarget);
145                 c->m_init_flag = 0;
146
147                 c->m_last_ok = 0;
148                 c->m_last_resultCount = 0;
149                 c->m_sr_transform = 0;
150                 c->m_waiting = 0;
151                 c->timeout(m_idletime); 
152             }
153             c->m_seqno = parent->m_seqno;
154             if (c->m_server && c->m_server != this)
155                 c->m_server->m_client = 0;
156             c->m_server = this;
157             (parent->m_seqno)++;
158             yaz_log (LOG_DEBUG, "get_client 1 %p %p", this, c);
159             return c;
160         }
161     }
162     else if (!c)
163     {
164         Yaz_ProxyClient *cc = 0;
165         
166         for (c = parent->m_clientPool; c; c = c->m_next)
167         {
168             assert (c->m_prev);
169             assert (*c->m_prev == c);
170             if (c->m_server == 0 && c->m_cookie[0] == 0 && 
171                 !strcmp(m_proxyTarget, c->get_hostname()))
172             {
173                 cc = c;
174             }
175         }
176         if (cc)
177         {
178             // found it in cache
179             c = cc;
180
181             yaz_log (LOG_LOG, "Reuse session %d to %d %s",
182                      c->m_seqno, parent->m_seqno, c->get_hostname());
183
184             c->m_seqno = parent->m_seqno;
185             assert(c->m_server == 0);
186             c->m_server = this;
187             
188             (parent->m_seqno)++;
189             return c;
190         }
191     }
192     if (!m_client)
193     {
194         if (apdu->which != Z_APDU_initRequest)
195         {
196             yaz_log (LOG_LOG, "no first INIT!");
197             return 0;
198         }
199         Z_InitRequest *initRequest = apdu->u.initRequest;
200
201         if (!initRequest->idAuthentication)
202         {
203             if (m_proxy_authentication)
204             {
205                 initRequest->idAuthentication =
206                     (Z_IdAuthentication *)
207                     odr_malloc (odr_encode(),
208                                 sizeof(*initRequest->idAuthentication));
209                 initRequest->idAuthentication->which =
210                     Z_IdAuthentication_open;
211                 initRequest->idAuthentication->u.open =
212                     odr_strdup (odr_encode(), m_proxy_authentication);
213             }
214         }
215
216         // go through list of clients - and find the lowest/oldest one.
217         Yaz_ProxyClient *c_min = 0;
218         int min_seq = -1;
219         int no_of_clients = 0;
220         if (parent->m_clientPool)
221             yaz_log (LOG_LOG, "Existing sessions");
222         for (c = parent->m_clientPool; c; c = c->m_next)
223         {
224             yaz_log (LOG_LOG, " Session %-3d wait=%d %s", c->m_seqno,
225                                c->m_waiting, c->get_hostname());
226             no_of_clients++;
227             if (min_seq < 0 || c->m_seqno < min_seq)
228             {
229                 min_seq = c->m_seqno;
230                 c_min = c;
231             }
232         }
233         if (no_of_clients >= parent->m_max_clients)
234         {
235             c = c_min;
236             if (c->m_waiting || strcmp(m_proxyTarget, c->get_hostname()))
237             {
238                 yaz_log (LOG_LOG, "Replace session %d",
239                       c->m_seqno);
240                 if (c->m_server && c->m_server != this)
241                     delete c->m_server;
242                 c->m_server = 0;
243             }
244             else
245             {
246                 yaz_log (LOG_LOG, "Move session %d to %d %s",
247                       c->m_seqno, parent->m_seqno, c->get_hostname());
248                 if (cookie)
249                     strcpy (c->m_cookie, cookie);
250                 else
251                     c->m_cookie[0] = '\0';
252                 c->m_seqno = parent->m_seqno;
253                 if (c->m_server && c->m_server != this)
254                 {
255                     c->m_server->m_client = 0;
256                     delete c->m_server;
257                 }
258                 (parent->m_seqno)++;
259                 yaz_log (LOG_DEBUG, "get_client 2 %p %p", this, c);
260                 return c;
261             }
262         }
263         else
264         {
265             yaz_log (LOG_LOG, "Making session %d %s", parent->m_seqno,
266                             m_proxyTarget);
267             c = new Yaz_ProxyClient(m_PDU_Observable->clone());
268             c->m_next = parent->m_clientPool;
269             if (c->m_next)
270                 c->m_next->m_prev = &c->m_next;
271             parent->m_clientPool = c;
272             c->m_prev = &parent->m_clientPool;
273         }
274         if (cookie)
275             strcpy (c->m_cookie, cookie);
276         else
277             c->m_cookie[0] = '\0';
278         yaz_log (LOG_LOG, "Connecting to %s", m_proxyTarget);
279         c->m_seqno = parent->m_seqno;
280         c->client(m_proxyTarget);
281         c->m_init_flag = 0;
282         c->m_last_resultCount = 0;
283         c->m_last_ok = 0;
284         c->m_sr_transform = 0;
285         c->m_waiting = 0;
286         c->timeout(20);
287
288         (parent->m_seqno)++;
289     }
290     yaz_log (LOG_DEBUG, "get_client 3 %p %p", this, c);
291     return c;
292 }
293
294 Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
295 {
296     if (apdu->which != Z_APDU_searchRequest)
297         return apdu;
298     if (*m_parent->m_optimize == '0')
299         return apdu;      // don't optimize result sets..
300     Z_SearchRequest *sr = apdu->u.searchRequest;
301     Yaz_Z_Query *this_query = new Yaz_Z_Query;
302     Yaz_Z_Databases this_databases;
303
304     this_databases.set(sr->num_databaseNames, (const char **)
305                        sr->databaseNames);
306     
307     this_query->set_Z_Query(sr->query);
308     
309     if (m_client->m_last_ok && m_client->m_last_query &&
310         m_client->m_last_query->match(this_query) &&
311         !strcmp(m_client->m_last_resultSetId, sr->resultSetName) &&
312         m_client->m_last_databases.match(this_databases))
313     {
314         delete this_query;
315         if (m_client->m_last_resultCount > *sr->smallSetUpperBound &&
316             m_client->m_last_resultCount < *sr->largeSetLowerBound)
317         {
318             // medium Set
319             // send present request (medium size)
320             yaz_log (LOG_LOG, "Optimizing search for medium set");
321             Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
322             Z_PresentRequest *pr = new_apdu->u.presentRequest;
323             pr->referenceId = sr->referenceId;
324             pr->resultSetId = sr->resultSetName;
325             pr->preferredRecordSyntax = sr->preferredRecordSyntax;
326             if (*sr->mediumSetPresentNumber < m_client->m_last_resultCount)
327                 *pr->numberOfRecordsRequested = *sr->mediumSetPresentNumber;
328             else
329                 *pr->numberOfRecordsRequested = m_client->m_last_resultCount;
330             if (sr->mediumSetElementSetNames)
331             {
332                 pr->recordComposition = (Z_RecordComposition *)
333                     odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
334                 pr->recordComposition->which = Z_RecordComp_simple;
335                 pr->recordComposition->u.simple = sr->mediumSetElementSetNames;
336             }
337             m_client->m_sr_transform = 1;
338             return new_apdu;
339         }
340         else if (m_client->m_last_resultCount >= *sr->largeSetLowerBound ||
341             m_client->m_last_resultCount <= 0)
342         {
343             // large set. Return pseudo-search response immediately
344             yaz_log (LOG_LOG, "Optimizing search for large set");
345             Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
346             new_apdu->u.searchResponse->referenceId = sr->referenceId;
347             new_apdu->u.searchResponse->resultCount =
348                 &m_client->m_last_resultCount;
349             send_Z_PDU(new_apdu);
350             return 0;
351         }
352         else
353         {
354             // small set
355             // send a present request (small set)
356             yaz_log (LOG_LOG, "Optimizing search for small set");
357             Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
358             Z_PresentRequest *pr = new_apdu->u.presentRequest;
359             pr->referenceId = sr->referenceId;
360             pr->resultSetId = sr->resultSetName;
361             pr->preferredRecordSyntax = sr->preferredRecordSyntax;
362             *pr->numberOfRecordsRequested = m_client->m_last_resultCount;
363             if (sr->smallSetElementSetNames)
364             {
365                 pr->recordComposition = (Z_RecordComposition *)
366                     odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
367                 pr->recordComposition->which = Z_RecordComp_simple;
368                 pr->recordComposition->u.simple = sr->smallSetElementSetNames;
369             }
370             m_client->m_sr_transform = 1;
371             return new_apdu;
372         }
373     }
374     else
375     {
376         delete m_client->m_last_query;
377         m_client->m_last_query = this_query;
378         m_client->m_last_ok = 0;
379
380         xfree (m_client->m_last_resultSetId);
381         m_client->m_last_resultSetId = xstrdup (sr->resultSetName);
382
383         m_client->m_last_databases.set(sr->num_databaseNames,
384                                        (const char **) sr->databaseNames);
385     }
386     return apdu;
387 }
388
389 static const char *apdu_name(Z_APDU *apdu)
390 {
391     switch (apdu->which)
392     {
393     case Z_APDU_initRequest:
394         return "initRequest";
395     case Z_APDU_initResponse:
396         return "initResponse";
397     case Z_APDU_searchRequest:
398         return "searchRequest";
399     case Z_APDU_searchResponse:
400         return "searchResponse";
401     case Z_APDU_presentRequest:
402         return "presentRequest";
403     case Z_APDU_presentResponse:
404         return "presentResponse";
405     case Z_APDU_deleteResultSetRequest:
406         return "deleteResultSetRequest";
407     case Z_APDU_deleteResultSetResponse:
408         return "deleteResultSetResponse";
409     case Z_APDU_scanRequest:
410         return "scanRequest";
411     case Z_APDU_scanResponse:
412         return "scanResponse";
413     case Z_APDU_sortRequest:
414         return "sortRequest";
415     case Z_APDU_sortResponse:
416         return "sortResponse";
417     case Z_APDU_extendedServicesRequest:
418         return "extendedServicesRequest";
419     case Z_APDU_extendedServicesResponse:
420         return "extendedServicesResponse";
421     case Z_APDU_close:
422         return "close";
423     }
424     return "other";
425 }
426
427 void Yaz_Proxy::recv_Z_PDU(Z_APDU *apdu)
428 {
429     yaz_log (LOG_LOG, "Receiving %s from client", apdu_name(apdu));
430     // Determine our client.
431     m_client = get_client(apdu);
432     if (!m_client)
433     {
434         delete this;
435         return;
436     }
437     m_client->m_server = this;
438
439     if (apdu->which == Z_APDU_initRequest)
440     {
441         if (m_client->m_init_flag)
442         {
443             Z_APDU *apdu = m_client->m_initResponse;
444             apdu->u.initResponse->otherInfo = 0;
445             if (m_client->m_cookie)
446                 set_otherInformationString(apdu, VAL_COOKIE, 1,
447                                            m_client->m_cookie);
448             send_Z_PDU(apdu);
449             return;
450         }
451         m_client->m_init_flag = 1;
452     }
453     apdu = result_set_optimize(apdu);
454     if (!apdu)
455         return;
456
457     yaz_log (LOG_LOG, "Sending %s to %s",
458                     apdu_name(apdu), m_client->get_hostname());
459
460     // delete other info part from PDU before sending to target
461     Z_OtherInformation **oi;
462     get_otherInfoAPDU(apdu, &oi);
463     if (oi)
464         *oi = 0;
465
466     if (m_client->send_Z_PDU(apdu) < 0)
467     {
468         delete m_client;
469         m_client = 0;
470         delete this;
471     }
472     else
473         m_client->m_waiting = 1;
474 }
475
476 void Yaz_Proxy::connectNotify()
477 {
478 }
479
480 void Yaz_Proxy::shutdown()
481 {
482     // only keep if keep_alive flag is set...
483     if (m_keepalive && m_client && m_client->m_waiting == 0)
484     {
485         yaz_log (LOG_LOG, "shutdown (client to proxy) keepalive %s",
486                  m_client->get_hostname());
487         assert (m_client->m_waiting != 2);
488         // Tell client (if any) that no server connection is there..
489         m_client->m_server = 0;
490     }
491     else if (m_client)
492     {
493         yaz_log (LOG_LOG, "shutdown (client to proxy) close %s",
494                  m_client->get_hostname());
495         assert (m_client->m_waiting != 2);
496         delete m_client;
497     }
498     else if (!m_parent)
499     {
500         yaz_log (LOG_LOG, "shutdown (client to proxy) bad state");
501         assert (m_parent);
502     }
503     else 
504     {
505         yaz_log (LOG_LOG, "shutdown (client to proxy)");
506     }
507     delete this;
508 }
509
510 void Yaz_ProxyClient::shutdown()
511 {
512     yaz_log (LOG_LOG, "shutdown (proxy to server) %s", get_hostname());
513     delete m_server;
514     delete this;
515 }
516
517 void Yaz_Proxy::failNotify()
518 {
519     yaz_log (LOG_LOG, "Yaz_Proxy connection closed by client");
520     shutdown();
521 }
522
523 void Yaz_ProxyClient::failNotify()
524 {
525     yaz_log (LOG_LOG, "Yaz_ProxyClient connection closed by %s", get_hostname());
526     shutdown();
527 }
528
529 void Yaz_ProxyClient::connectNotify()
530 {
531     yaz_log (LOG_LOG, "Connection accepted by %s", get_hostname());
532     timeout(600);
533 }
534
535 IYaz_PDU_Observer *Yaz_ProxyClient::sessionNotify(IYaz_PDU_Observable
536                                                   *the_PDU_Observable, int fd)
537 {
538     return new Yaz_ProxyClient(the_PDU_Observable);
539 }
540
541 Yaz_ProxyClient::~Yaz_ProxyClient()
542 {
543     if (m_prev)
544         *m_prev = m_next;
545     if (m_next)
546         m_next->m_prev = m_prev;
547     m_waiting = 2;     // for debugging purposes only.
548     odr_destroy(m_init_odr);
549     delete m_last_query;
550     xfree (m_last_resultSetId);
551 }
552
553 void Yaz_Proxy::timeoutNotify()
554 {
555     yaz_log (LOG_LOG, "timeout (client to proxy)");
556     shutdown();
557 }
558
559 void Yaz_ProxyClient::timeoutNotify()
560 {
561     yaz_log (LOG_LOG, "timeout (proxy to target) %s", get_hostname());
562     shutdown();
563 }
564
565 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable) :
566     Yaz_Z_Assoc (the_PDU_Observable)
567 {
568     m_cookie[0] = 0;
569     m_next = 0;
570     m_prev = 0;
571     m_init_flag = 0;
572     m_last_query = 0;
573     m_last_resultSetId = 0;
574     m_last_resultCount = 0;
575     m_last_ok = 0;
576     m_sr_transform = 0;
577     m_waiting = 0;
578     m_init_odr = odr_createmem (ODR_DECODE);
579     m_initResponse = 0;
580 }
581
582 const char *Yaz_Proxy::option(const char *name, const char *value)
583 {
584     if (!strcmp (name, "optimize")) {
585         if (value) {
586             xfree (m_optimize); 
587             m_optimize = xstrdup (value);
588         }
589         return m_optimize;
590     }
591     return 0;
592 }
593
594 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu)
595 {
596     m_waiting = 0;
597     yaz_log (LOG_LOG, "Receiving %s from %s", apdu_name(apdu),
598                      get_hostname());
599     if (apdu->which == Z_APDU_initResponse)
600     {
601         NMEM nmem = odr_extract_mem (odr_decode());
602         odr_reset (m_init_odr);
603         nmem_transfer (m_init_odr->mem, nmem);
604         m_initResponse = apdu;
605         nmem_destroy (nmem);
606     }
607     if (apdu->which == Z_APDU_searchResponse)
608     {
609         m_last_resultCount = *apdu->u.searchResponse->resultCount;
610         int status = *apdu->u.searchResponse->searchStatus;
611         if (status && 
612                 (!apdu->u.searchResponse->records ||
613                  apdu->u.searchResponse->records->which == Z_Records_DBOSD))
614             m_last_ok = 1;
615     }
616     if (apdu->which == Z_APDU_presentResponse && m_sr_transform)
617     {
618         m_sr_transform = 0;
619         Z_PresentResponse *pr = apdu->u.presentResponse;
620         Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
621         Z_SearchResponse *sr = new_apdu->u.searchResponse;
622         sr->referenceId = pr->referenceId;
623         *sr->resultCount = m_last_resultCount;
624         sr->records = pr->records;
625         sr->nextResultSetPosition = pr->nextResultSetPosition;
626         sr->numberOfRecordsReturned = pr->numberOfRecordsReturned;
627         apdu = new_apdu;
628     }
629     if (m_cookie && *m_cookie)
630         set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
631     if (m_server)
632     {
633         yaz_log (LOG_LOG, "Sending %s to client", apdu_name(apdu));
634         m_server->send_Z_PDU(apdu);
635     }
636 }