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