Rename yaz++ header files
[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.36 2002-10-09 12:50:26 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_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         !strcmp(m_client->m_last_resultSetId, sr->resultSetName) &&
281         m_client->m_last_databases.match(this_databases))
282     {
283         delete this_query;
284         if (m_client->m_last_resultCount > *sr->smallSetUpperBound &&
285             m_client->m_last_resultCount < *sr->largeSetLowerBound)
286         {
287             // medium Set
288             // send present request (medium size)
289             yaz_log (LOG_LOG, "Optimizing search for medium set");
290             Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
291             Z_PresentRequest *pr = new_apdu->u.presentRequest;
292             pr->referenceId = sr->referenceId;
293             pr->resultSetId = sr->resultSetName;
294             pr->preferredRecordSyntax = sr->preferredRecordSyntax;
295             *pr->numberOfRecordsRequested = *sr->mediumSetPresentNumber;
296             if (sr->mediumSetElementSetNames)
297             {
298                 pr->recordComposition = (Z_RecordComposition *)
299                     odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
300                 pr->recordComposition->which = Z_RecordComp_simple;
301                 pr->recordComposition->u.simple = sr->mediumSetElementSetNames;
302             }
303             m_client->m_sr_transform = 1;
304             return new_apdu;
305         }
306         else if (m_client->m_last_resultCount >= *sr->largeSetLowerBound ||
307             m_client->m_last_resultCount <= 0)
308         {
309             // large set. Return pseudo-search response immediately
310             yaz_log (LOG_LOG, "Optimizing search for large set");
311             Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
312             new_apdu->u.searchResponse->referenceId = sr->referenceId;
313             new_apdu->u.searchResponse->resultCount =
314                 &m_client->m_last_resultCount;
315             send_Z_PDU(new_apdu);
316             return 0;
317         }
318         else
319         {
320             // small set
321             // send a present request (small set)
322             yaz_log (LOG_LOG, "Optimizing search for small set");
323             Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
324             Z_PresentRequest *pr = new_apdu->u.presentRequest;
325             pr->referenceId = sr->referenceId;
326             pr->resultSetId = sr->resultSetName;
327             pr->preferredRecordSyntax = sr->preferredRecordSyntax;
328             *pr->numberOfRecordsRequested = m_client->m_last_resultCount;
329             if (sr->smallSetElementSetNames)
330             {
331                 pr->recordComposition = (Z_RecordComposition *)
332                     odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
333                 pr->recordComposition->which = Z_RecordComp_simple;
334                 pr->recordComposition->u.simple = sr->smallSetElementSetNames;
335             }
336             m_client->m_sr_transform = 1;
337             return new_apdu;
338         }
339     }
340     else
341     {
342         delete m_client->m_last_query;
343         m_client->m_last_query = this_query;
344         m_client->m_last_ok = 0;
345
346         xfree (m_client->m_last_resultSetId);
347         m_client->m_last_resultSetId = xstrdup (sr->resultSetName);
348
349         m_client->m_last_databases.set(sr->num_databaseNames,
350                                        (const char **) sr->databaseNames);
351     }
352     return apdu;
353 }
354
355 static const char *apdu_name(Z_APDU *apdu)
356 {
357     switch (apdu->which)
358     {
359     case Z_APDU_initRequest:
360         return "initRequest";
361     case Z_APDU_initResponse:
362         return "initResponse";
363     case Z_APDU_searchRequest:
364         return "searchRequest";
365     case Z_APDU_searchResponse:
366         return "searchResponse";
367     case Z_APDU_presentRequest:
368         return "presentRequest";
369     case Z_APDU_presentResponse:
370         return "presentResponse";
371     }
372     return "other";
373 }
374
375 void Yaz_Proxy::recv_Z_PDU(Z_APDU *apdu)
376 {
377     yaz_log (LOG_LOG, "Receiving %s from client", apdu_name(apdu));
378     // Determine our client.
379     m_client = get_client(apdu);
380     if (!m_client)
381     {
382         delete this;
383         return;
384     }
385     m_client->m_server = this;
386
387     if (apdu->which == Z_APDU_initRequest)
388     {
389         if (m_client->m_init_flag)
390         {
391             Z_APDU *apdu = m_client->m_initResponse;
392             apdu->u.initResponse->otherInfo = 0;
393             if (m_client->m_cookie)
394                 set_otherInformationString(apdu, VAL_COOKIE, 1,
395                                            m_client->m_cookie);
396             send_Z_PDU(apdu);
397             return;
398         }
399         m_client->m_init_flag = 1;
400     }
401     apdu = result_set_optimize(apdu);
402     if (!apdu)
403         return;
404
405     yaz_log (LOG_LOG, "Sending %s to %s",
406                     apdu_name(apdu), m_client->get_hostname());
407
408     // delete other info part from PDU before sending to target
409     Z_OtherInformation **oi;
410     get_otherInfoAPDU(apdu, &oi);
411     if (oi)
412         *oi = 0;
413
414     if (m_client->send_Z_PDU(apdu) < 0)
415     {
416         delete m_client;
417         m_client = 0;
418         delete this;
419     }
420     else
421         m_client->m_waiting = 1;
422 }
423
424 void Yaz_Proxy::connectNotify()
425 {
426 }
427
428 void Yaz_Proxy::shutdown()
429 {
430     // only keep if keep_alive flag and cookie is set...
431     if (m_keepalive && m_client && m_client->m_cookie[0])
432     {
433         if (m_client->m_waiting == 2)
434             abort();
435         // Tell client (if any) that no server connection is there..
436         m_client->m_server = 0;
437         yaz_log (LOG_LOG, "shutdown (client to proxy) keepalive %s", m_client->get_hostname());
438     }
439     else if (m_client)
440     {
441         if (m_client->m_waiting == 2)
442             abort();
443         yaz_log (LOG_LOG, "shutdown (client to proxy) close %s", m_client->get_hostname());
444         delete m_client;
445     }
446     else if (!m_parent)
447     {
448         yaz_log (LOG_LOG, "shutdown (client to proxy) bad state");
449         abort();
450     }
451     else 
452     {
453         yaz_log (LOG_LOG, "shutdown (client to proxy)");
454     }
455     delete this;
456 }
457
458 void Yaz_ProxyClient::shutdown()
459 {
460     yaz_log (LOG_LOG, "shutdown (proxy to server) %s", get_hostname());
461     delete m_server;
462     delete this;
463 }
464
465 void Yaz_Proxy::failNotify()
466 {
467     yaz_log (LOG_LOG, "Yaz_Proxy connection closed by client");
468     shutdown();
469 }
470
471 void Yaz_ProxyClient::failNotify()
472 {
473     yaz_log (LOG_LOG, "Yaz_ProxyClient connection closed by %s", get_hostname());
474     shutdown();
475 }
476
477 void Yaz_ProxyClient::connectNotify()
478 {
479     yaz_log (LOG_LOG, "Connection accepted by %s", get_hostname());
480     timeout(600);
481 }
482
483 IYaz_PDU_Observer *Yaz_ProxyClient::sessionNotify(IYaz_PDU_Observable
484                                                   *the_PDU_Observable, int fd)
485 {
486     return new Yaz_ProxyClient(the_PDU_Observable);
487 }
488
489 Yaz_ProxyClient::~Yaz_ProxyClient()
490 {
491     if (m_prev)
492         *m_prev = m_next;
493     if (m_next)
494         m_next->m_prev = m_prev;
495     m_waiting = 2;     // for debugging purposes only.
496     odr_destroy(m_init_odr);
497     delete m_last_query;
498     xfree (m_last_resultSetId);
499 }
500
501 void Yaz_Proxy::timeoutNotify()
502 {
503     yaz_log (LOG_LOG, "timeout (client to proxy)");
504     shutdown();
505 }
506
507 void Yaz_ProxyClient::timeoutNotify()
508 {
509     yaz_log (LOG_LOG, "timeout (proxy to target) %s", get_hostname());
510     shutdown();
511 }
512
513 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable) :
514     Yaz_Z_Assoc (the_PDU_Observable)
515 {
516     m_cookie[0] = 0;
517     m_next = 0;
518     m_prev = 0;
519     m_init_flag = 0;
520     m_last_query = 0;
521     m_last_resultSetId = 0;
522     m_last_resultCount = 0;
523     m_last_ok = 0;
524     m_sr_transform = 0;
525     m_waiting = 0;
526     m_init_odr = odr_createmem (ODR_DECODE);
527     m_initResponse = 0;
528 }
529
530 const char *Yaz_Proxy::option(const char *name, const char *value)
531 {
532     if (!strcmp (name, "optimize")) {
533         if (value) {
534             xfree (m_optimize); 
535             m_optimize = xstrdup (value);
536         }
537         return m_optimize;
538     }
539     return 0;
540 }
541
542 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu)
543 {
544     m_waiting = 0;
545     yaz_log (LOG_LOG, "Receiving %s from %s", apdu_name(apdu),
546                      get_hostname());
547     if (apdu->which == Z_APDU_initResponse)
548     {
549         NMEM nmem = odr_extract_mem (odr_decode());
550         odr_reset (m_init_odr);
551         nmem_transfer (m_init_odr->mem, nmem);
552         m_initResponse = apdu;
553         nmem_destroy (nmem);
554     }
555     if (apdu->which == Z_APDU_searchResponse)
556     {
557         m_last_resultCount = *apdu->u.searchResponse->resultCount;
558         int status = *apdu->u.searchResponse->searchStatus;
559         if (status && 
560                 (!apdu->u.searchResponse->records ||
561                  apdu->u.searchResponse->records->which == Z_Records_DBOSD))
562             m_last_ok = 1;
563     }
564     if (apdu->which == Z_APDU_presentResponse && m_sr_transform)
565     {
566         m_sr_transform = 0;
567         Z_PresentResponse *pr = apdu->u.presentResponse;
568         Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
569         Z_SearchResponse *sr = new_apdu->u.searchResponse;
570         sr->referenceId = pr->referenceId;
571         *sr->resultCount = m_last_resultCount;
572         sr->records = pr->records;
573         sr->nextResultSetPosition = pr->nextResultSetPosition;
574         sr->numberOfRecordsReturned = pr->numberOfRecordsReturned;
575         apdu = new_apdu;
576     }
577     if (m_cookie && *m_cookie)
578         set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
579     if (m_server)
580     {
581         yaz_log (LOG_LOG, "Sending %s to client", apdu_name(apdu));
582         m_server->send_Z_PDU(apdu);
583     }
584 }