Fix Present request out of range for proxy
[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.38 2003-01-24 20:10:57 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             c->m_seqno = parent->m_seqno;
158             (parent->m_seqno)++;
159             yaz_log (LOG_DEBUG, "get_client 1 %p %p", this, c);
160             return c;
161         }
162     }
163     if (!m_client)
164     {
165         if (apdu->which != Z_APDU_initRequest)
166         {
167             yaz_log (LOG_LOG, "no first INIT!");
168             return 0;
169         }
170         Z_InitRequest *initRequest = apdu->u.initRequest;
171
172         if (!initRequest->idAuthentication)
173         {
174             if (m_proxy_authentication)
175             {
176                 initRequest->idAuthentication =
177                     (Z_IdAuthentication *)
178                     odr_malloc (odr_encode(),
179                                 sizeof(*initRequest->idAuthentication));
180                 initRequest->idAuthentication->which =
181                     Z_IdAuthentication_open;
182                 initRequest->idAuthentication->u.open =
183                     odr_strdup (odr_encode(), m_proxy_authentication);
184             }
185         }
186
187         // go through list of clients - and find the lowest/oldest one.
188         Yaz_ProxyClient *c_min = 0;
189         int min_seq = -1;
190         int no_of_clients = 0;
191         yaz_log (LOG_LOG, "Existing sessions");
192         for (c = parent->m_clientPool; c; c = c->m_next)
193         {
194             yaz_log (LOG_LOG, " Session %-3d wait=%d %s", c->m_seqno,
195                                c->m_waiting, c->get_hostname());
196             no_of_clients++;
197             if (min_seq < 0 || c->m_seqno < min_seq)
198             {
199                 min_seq = c->m_seqno;
200                 c_min = c;
201             }
202         }
203         if (no_of_clients >= parent->m_max_clients)
204         {
205             c = c_min;
206             if (c->m_waiting || strcmp(m_proxyTarget, c->get_hostname()))
207             {
208                 yaz_log (LOG_LOG, "Replace session %d",
209                       c->m_seqno);
210                 if (c->m_server && c->m_server != this)
211                     delete c->m_server;
212                 c->m_server = 0;
213             }
214             else
215             {
216                 yaz_log (LOG_LOG, "Reuse session %d to %d",
217                       c->m_seqno, parent->m_seqno);
218                 if (cookie)
219                     strcpy (c->m_cookie, cookie);
220                 else
221                     c->m_cookie[0] = '\0';
222                 c->m_seqno = parent->m_seqno;
223                 if (c->m_server && c->m_server != this)
224                 {
225                     c->m_server->m_client = 0;
226                     delete c->m_server;
227                 }
228                 (parent->m_seqno)++;
229                 yaz_log (LOG_DEBUG, "get_client 2 %p %p", this, c);
230                 return c;
231             }
232         }
233         else
234         {
235             yaz_log (LOG_LOG, "Making session %d %s", parent->m_seqno,
236                             m_proxyTarget);
237             c = new Yaz_ProxyClient(m_PDU_Observable->clone());
238             c->m_next = parent->m_clientPool;
239             if (c->m_next)
240                 c->m_next->m_prev = &c->m_next;
241             parent->m_clientPool = c;
242             c->m_prev = &parent->m_clientPool;
243         }
244         if (cookie)
245             strcpy (c->m_cookie, cookie);
246         else
247             c->m_cookie[0] = '\0';
248         yaz_log (LOG_LOG, "Connecting to %s", m_proxyTarget);
249         c->m_seqno = parent->m_seqno;
250         c->client(m_proxyTarget);
251         c->m_init_flag = 0;
252         c->m_last_resultCount = 0;
253         c->m_last_ok = 0;
254         c->m_sr_transform = 0;
255         c->m_waiting = 0;
256         c->timeout(20);
257
258         (parent->m_seqno)++;
259     }
260     yaz_log (LOG_DEBUG, "get_client 3 %p %p", this, c);
261     return c;
262 }
263
264 Z_APDU *Yaz_Proxy::result_set_optimize(Z_APDU *apdu)
265 {
266     if (apdu->which != Z_APDU_searchRequest)
267         return apdu;
268     if (*m_parent->m_optimize == '0')
269         return apdu;      // don't optimize result sets..
270     Z_SearchRequest *sr = apdu->u.searchRequest;
271     Yaz_Z_Query *this_query = new Yaz_Z_Query;
272     Yaz_Z_Databases this_databases;
273
274     this_databases.set(sr->num_databaseNames, (const char **)
275                        sr->databaseNames);
276     
277     this_query->set_Z_Query(sr->query);
278     
279     if (m_client->m_last_ok && m_client->m_last_query &&
280         m_client->m_last_query->match(this_query) &&
281         !strcmp(m_client->m_last_resultSetId, sr->resultSetName) &&
282         m_client->m_last_databases.match(this_databases))
283     {
284         delete this_query;
285         if (m_client->m_last_resultCount > *sr->smallSetUpperBound &&
286             m_client->m_last_resultCount < *sr->largeSetLowerBound)
287         {
288             // medium Set
289             // send present request (medium size)
290             yaz_log (LOG_LOG, "Optimizing search for medium set");
291             Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
292             Z_PresentRequest *pr = new_apdu->u.presentRequest;
293             pr->referenceId = sr->referenceId;
294             pr->resultSetId = sr->resultSetName;
295             pr->preferredRecordSyntax = sr->preferredRecordSyntax;
296             if (*sr->mediumSetPresentNumber < m_client->m_last_resultCount)
297                 *pr->numberOfRecordsRequested = *sr->mediumSetPresentNumber;
298             else
299                 *pr->numberOfRecordsRequested = m_client->m_last_resultCount;
300             if (sr->mediumSetElementSetNames)
301             {
302                 pr->recordComposition = (Z_RecordComposition *)
303                     odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
304                 pr->recordComposition->which = Z_RecordComp_simple;
305                 pr->recordComposition->u.simple = sr->mediumSetElementSetNames;
306             }
307             m_client->m_sr_transform = 1;
308             return new_apdu;
309         }
310         else if (m_client->m_last_resultCount >= *sr->largeSetLowerBound ||
311             m_client->m_last_resultCount <= 0)
312         {
313             // large set. Return pseudo-search response immediately
314             yaz_log (LOG_LOG, "Optimizing search for large set");
315             Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
316             new_apdu->u.searchResponse->referenceId = sr->referenceId;
317             new_apdu->u.searchResponse->resultCount =
318                 &m_client->m_last_resultCount;
319             send_Z_PDU(new_apdu);
320             return 0;
321         }
322         else
323         {
324             // small set
325             // send a present request (small set)
326             yaz_log (LOG_LOG, "Optimizing search for small set");
327             Z_APDU *new_apdu = create_Z_PDU(Z_APDU_presentRequest);
328             Z_PresentRequest *pr = new_apdu->u.presentRequest;
329             pr->referenceId = sr->referenceId;
330             pr->resultSetId = sr->resultSetName;
331             pr->preferredRecordSyntax = sr->preferredRecordSyntax;
332             *pr->numberOfRecordsRequested = m_client->m_last_resultCount;
333             if (sr->smallSetElementSetNames)
334             {
335                 pr->recordComposition = (Z_RecordComposition *)
336                     odr_malloc(odr_encode(), sizeof(Z_RecordComposition));
337                 pr->recordComposition->which = Z_RecordComp_simple;
338                 pr->recordComposition->u.simple = sr->smallSetElementSetNames;
339             }
340             m_client->m_sr_transform = 1;
341             return new_apdu;
342         }
343     }
344     else
345     {
346         delete m_client->m_last_query;
347         m_client->m_last_query = this_query;
348         m_client->m_last_ok = 0;
349
350         xfree (m_client->m_last_resultSetId);
351         m_client->m_last_resultSetId = xstrdup (sr->resultSetName);
352
353         m_client->m_last_databases.set(sr->num_databaseNames,
354                                        (const char **) sr->databaseNames);
355     }
356     return apdu;
357 }
358
359 static const char *apdu_name(Z_APDU *apdu)
360 {
361     switch (apdu->which)
362     {
363     case Z_APDU_initRequest:
364         return "initRequest";
365     case Z_APDU_initResponse:
366         return "initResponse";
367     case Z_APDU_searchRequest:
368         return "searchRequest";
369     case Z_APDU_searchResponse:
370         return "searchResponse";
371     case Z_APDU_presentRequest:
372         return "presentRequest";
373     case Z_APDU_presentResponse:
374         return "presentResponse";
375     }
376     return "other";
377 }
378
379 void Yaz_Proxy::recv_Z_PDU(Z_APDU *apdu)
380 {
381     yaz_log (LOG_LOG, "Receiving %s from client", apdu_name(apdu));
382     // Determine our client.
383     m_client = get_client(apdu);
384     if (!m_client)
385     {
386         delete this;
387         return;
388     }
389     m_client->m_server = this;
390
391     if (apdu->which == Z_APDU_initRequest)
392     {
393         if (m_client->m_init_flag)
394         {
395             Z_APDU *apdu = m_client->m_initResponse;
396             apdu->u.initResponse->otherInfo = 0;
397             if (m_client->m_cookie)
398                 set_otherInformationString(apdu, VAL_COOKIE, 1,
399                                            m_client->m_cookie);
400             send_Z_PDU(apdu);
401             return;
402         }
403         m_client->m_init_flag = 1;
404     }
405     apdu = result_set_optimize(apdu);
406     if (!apdu)
407         return;
408
409     yaz_log (LOG_LOG, "Sending %s to %s",
410                     apdu_name(apdu), m_client->get_hostname());
411
412     // delete other info part from PDU before sending to target
413     Z_OtherInformation **oi;
414     get_otherInfoAPDU(apdu, &oi);
415     if (oi)
416         *oi = 0;
417
418     if (m_client->send_Z_PDU(apdu) < 0)
419     {
420         delete m_client;
421         m_client = 0;
422         delete this;
423     }
424     else
425         m_client->m_waiting = 1;
426 }
427
428 void Yaz_Proxy::connectNotify()
429 {
430 }
431
432 void Yaz_Proxy::shutdown()
433 {
434     // only keep if keep_alive flag and cookie is set...
435     if (m_keepalive && m_client && m_client->m_cookie[0])
436     {
437         yaz_log (LOG_LOG, "shutdown (client to proxy) keepalive %s",
438                  m_client->get_hostname());
439         assert (m_client->m_waiting != 2);
440         // Tell client (if any) that no server connection is there..
441         m_client->m_server = 0;
442
443     }
444     else if (m_client)
445     {
446         yaz_log (LOG_LOG, "shutdown (client to proxy) close %s",
447                  m_client->get_hostname());
448         assert (m_client->m_waiting != 2);
449         delete m_client;
450     }
451     else if (!m_parent)
452     {
453         yaz_log (LOG_LOG, "shutdown (client to proxy) bad state");
454         assert (m_parent);
455     }
456     else 
457     {
458         yaz_log (LOG_LOG, "shutdown (client to proxy)");
459     }
460     delete this;
461 }
462
463 void Yaz_ProxyClient::shutdown()
464 {
465     yaz_log (LOG_LOG, "shutdown (proxy to server) %s", get_hostname());
466     delete m_server;
467     delete this;
468 }
469
470 void Yaz_Proxy::failNotify()
471 {
472     yaz_log (LOG_LOG, "Yaz_Proxy connection closed by client");
473     shutdown();
474 }
475
476 void Yaz_ProxyClient::failNotify()
477 {
478     yaz_log (LOG_LOG, "Yaz_ProxyClient connection closed by %s", get_hostname());
479     shutdown();
480 }
481
482 void Yaz_ProxyClient::connectNotify()
483 {
484     yaz_log (LOG_LOG, "Connection accepted by %s", get_hostname());
485     timeout(600);
486 }
487
488 IYaz_PDU_Observer *Yaz_ProxyClient::sessionNotify(IYaz_PDU_Observable
489                                                   *the_PDU_Observable, int fd)
490 {
491     return new Yaz_ProxyClient(the_PDU_Observable);
492 }
493
494 Yaz_ProxyClient::~Yaz_ProxyClient()
495 {
496     if (m_prev)
497         *m_prev = m_next;
498     if (m_next)
499         m_next->m_prev = m_prev;
500     m_waiting = 2;     // for debugging purposes only.
501     odr_destroy(m_init_odr);
502     delete m_last_query;
503     xfree (m_last_resultSetId);
504 }
505
506 void Yaz_Proxy::timeoutNotify()
507 {
508     yaz_log (LOG_LOG, "timeout (client to proxy)");
509     shutdown();
510 }
511
512 void Yaz_ProxyClient::timeoutNotify()
513 {
514     yaz_log (LOG_LOG, "timeout (proxy to target) %s", get_hostname());
515     shutdown();
516 }
517
518 Yaz_ProxyClient::Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable) :
519     Yaz_Z_Assoc (the_PDU_Observable)
520 {
521     m_cookie[0] = 0;
522     m_next = 0;
523     m_prev = 0;
524     m_init_flag = 0;
525     m_last_query = 0;
526     m_last_resultSetId = 0;
527     m_last_resultCount = 0;
528     m_last_ok = 0;
529     m_sr_transform = 0;
530     m_waiting = 0;
531     m_init_odr = odr_createmem (ODR_DECODE);
532     m_initResponse = 0;
533 }
534
535 const char *Yaz_Proxy::option(const char *name, const char *value)
536 {
537     if (!strcmp (name, "optimize")) {
538         if (value) {
539             xfree (m_optimize); 
540             m_optimize = xstrdup (value);
541         }
542         return m_optimize;
543     }
544     return 0;
545 }
546
547 void Yaz_ProxyClient::recv_Z_PDU(Z_APDU *apdu)
548 {
549     m_waiting = 0;
550     yaz_log (LOG_LOG, "Receiving %s from %s", apdu_name(apdu),
551                      get_hostname());
552     if (apdu->which == Z_APDU_initResponse)
553     {
554         NMEM nmem = odr_extract_mem (odr_decode());
555         odr_reset (m_init_odr);
556         nmem_transfer (m_init_odr->mem, nmem);
557         m_initResponse = apdu;
558         nmem_destroy (nmem);
559     }
560     if (apdu->which == Z_APDU_searchResponse)
561     {
562         m_last_resultCount = *apdu->u.searchResponse->resultCount;
563         int status = *apdu->u.searchResponse->searchStatus;
564         if (status && 
565                 (!apdu->u.searchResponse->records ||
566                  apdu->u.searchResponse->records->which == Z_Records_DBOSD))
567             m_last_ok = 1;
568     }
569     if (apdu->which == Z_APDU_presentResponse && m_sr_transform)
570     {
571         m_sr_transform = 0;
572         Z_PresentResponse *pr = apdu->u.presentResponse;
573         Z_APDU *new_apdu = create_Z_PDU(Z_APDU_searchResponse);
574         Z_SearchResponse *sr = new_apdu->u.searchResponse;
575         sr->referenceId = pr->referenceId;
576         *sr->resultCount = m_last_resultCount;
577         sr->records = pr->records;
578         sr->nextResultSetPosition = pr->nextResultSetPosition;
579         sr->numberOfRecordsReturned = pr->numberOfRecordsReturned;
580         apdu = new_apdu;
581     }
582     if (m_cookie && *m_cookie)
583         set_otherInformationString (apdu, VAL_COOKIE, 1, m_cookie);
584     if (m_server)
585     {
586         yaz_log (LOG_LOG, "Sending %s to client", apdu_name(apdu));
587         m_server->send_Z_PDU(apdu);
588     }
589 }