Windows: use Boost 1.59, msvc 14.0
[metaproxy-moved-to-github.git] / src / filter_z3950_client.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) Index Data
3
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19 #include "config.hpp"
20
21 #include "filter_z3950_client.hpp"
22 #include <metaproxy/package.hpp>
23 #include <metaproxy/util.hpp>
24
25 #include <map>
26 #include <stdexcept>
27 #include <list>
28 #include <iostream>
29
30 #include <boost/thread/mutex.hpp>
31 #include <boost/thread/condition.hpp>
32 #include <boost/thread/xtime.hpp>
33
34 #include <yaz/zgdu.h>
35 #include <yaz/log.h>
36 #include <yaz/otherinfo.h>
37 #include <yaz/diagbib1.h>
38 #include <yaz/oid_db.h>
39 #include <yaz/charneg.h>
40
41 #include <yazpp/socket-manager.h>
42 #include <yazpp/pdu-assoc.h>
43 #include <yazpp/z-assoc.h>
44
45 namespace mp = metaproxy_1;
46 namespace yf = mp::filter;
47
48 namespace metaproxy_1 {
49     namespace filter {
50         class Z3950Client::Assoc : public yazpp_1::Z_Assoc{
51             friend class Rep;
52             Assoc(yazpp_1::SocketManager *socket_manager,
53                   yazpp_1::IPDU_Observable *PDU_Observable,
54                   std::string host, int connect_timeout,
55                   int init_timeout, int general_timeout);
56             ~Assoc();
57             void connectNotify();
58             void failNotify();
59             void timeoutNotify();
60             void recv_GDU(Z_GDU *gdu, int len);
61             void fixup_nsd(ODR odr, Z_Records *records);
62             void fixup_nsd(ODR odr, Z_DefaultDiagFormat *nsd);
63             void fixup_init(ODR odr, Z_InitResponse *initrs);
64             yazpp_1::IPDU_Observer* sessionNotify(
65                 yazpp_1::IPDU_Observable *the_PDU_Observable,
66                 int fd);
67
68             yazpp_1::SocketManager *m_socket_manager;
69             yazpp_1::IPDU_Observable *m_PDU_Observable;
70             Package *m_package;
71             bool m_in_use;
72             bool m_waiting;
73             bool m_destroyed;
74             int m_connected; // 0=not connected, 1=init phase, 2=rest
75             bool m_has_closed;
76             int m_queue_len;
77             int m_time_elapsed;
78             int m_connect_time_max;
79             int m_init_time_max;
80             int m_general_time_max;
81             std::string m_host;
82         };
83
84         class Z3950Client::Rep {
85         public:
86             // number of seconds to wait before we give up request
87             int m_general_timeout_sec;
88             int m_connect_timeout_sec;
89             int m_init_timeout_sec;
90             int m_max_sockets;
91             bool m_force_close;
92             bool m_client_ip;
93             bool m_bind_host;
94             std::string m_charset;
95             std::string m_default_target;
96             std::string m_force_target;
97             boost::mutex m_mutex;
98             boost::condition m_cond_session_ready;
99             std::map<mp::Session,Z3950Client::Assoc *> m_clients;
100             Z3950Client::Assoc *get_assoc(Package &package);
101             void send_and_receive(Package &package,
102                                   yf::Z3950Client::Assoc *c);
103             void release_assoc(Package &package);
104         };
105     }
106 }
107
108 using namespace mp;
109
110 yf::Z3950Client::Assoc::Assoc(yazpp_1::SocketManager *socket_manager,
111                               yazpp_1::IPDU_Observable *PDU_Observable,
112                               std::string host,
113                               int connect_timeout,
114                               int init_timeout, int general_timeout)
115     :  Z_Assoc(PDU_Observable),
116        m_socket_manager(socket_manager), m_PDU_Observable(PDU_Observable),
117        m_package(0), m_in_use(true), m_waiting(false),
118        m_destroyed(false), m_connected(0), m_has_closed(false),
119        m_queue_len(1),
120        m_time_elapsed(0),
121        m_connect_time_max(connect_timeout),
122        m_init_time_max(init_timeout),
123        m_general_time_max(general_timeout),
124        m_host(host)
125 {
126     // std::cout << "create assoc " << this << "\n";
127 }
128
129 yf::Z3950Client::Assoc::~Assoc()
130 {
131     // std::cout << "destroy assoc " << this << "\n";
132 }
133
134 void yf::Z3950Client::Assoc::connectNotify()
135 {
136     m_waiting = false;
137
138     m_connected = 1;
139 }
140
141 void yf::Z3950Client::Assoc::failNotify()
142 {
143     m_waiting = false;
144
145     mp::odr odr;
146
147     if (m_package)
148     {
149         Z_GDU *gdu = m_package->request().get();
150         Z_APDU *apdu = 0;
151         if (gdu && gdu->which == Z_GDU_Z3950)
152             apdu = gdu->u.z3950;
153
154         m_package->response() = odr.create_close(apdu, Z_Close_peerAbort, 0);
155         m_package->session().close();
156     }
157 }
158
159 void yf::Z3950Client::Assoc::timeoutNotify()
160 {
161     m_time_elapsed++;
162     if ((m_connected == 0 && m_time_elapsed >= m_connect_time_max)
163         || (m_connected == 1 && m_time_elapsed >= m_init_time_max)
164         || (m_connected >= 2 && m_time_elapsed >= m_general_time_max))
165     {
166         m_waiting = false;
167
168         mp::odr odr;
169
170         if (m_package)
171         {
172             Z_GDU *gdu = m_package->request().get();
173             Z_APDU *apdu = 0;
174             if (gdu && gdu->which == Z_GDU_Z3950)
175                 apdu = gdu->u.z3950;
176
177             if (m_connected == 2)
178                 m_package->response() =
179                     odr.create_close(apdu, Z_Close_lackOfActivity, 0);
180             else
181                 m_package->response() =
182                     odr.create_close(apdu, Z_Close_peerAbort, 0);
183
184             m_package->session().close();
185         }
186     }
187 }
188
189 void yf::Z3950Client::Assoc::fixup_nsd(ODR odr, Z_DefaultDiagFormat *nsd)
190 {
191     std::string addinfo;
192
193     // should really check for nsd->which.. But union has two members
194     // containing almost same data
195     const char *v2Addinfo = nsd->u.v2Addinfo;
196     //  Z_InternationalString *v3Addinfo;
197     if (v2Addinfo && *v2Addinfo)
198     {
199         addinfo.assign(nsd->u.v2Addinfo);
200         addinfo += " ";
201     }
202     addinfo += "(backend=" + m_host + ")";
203     nsd->u.v2Addinfo = odr_strdup(odr, addinfo.c_str());
204 }
205
206 void yf::Z3950Client::Assoc::fixup_nsd(ODR odr, Z_Records *records)
207 {
208     if (records && records->which == Z_Records_NSD)
209     {
210         fixup_nsd(odr, records->u.nonSurrogateDiagnostic);
211     }
212     if (records && records->which == Z_Records_multipleNSD)
213     {
214         Z_DiagRecs *drecs = records->u.multipleNonSurDiagnostics;
215         int i;
216         for (i = 0; i < drecs->num_diagRecs; i++)
217         {
218             Z_DiagRec *dr = drecs->diagRecs[i];
219
220             if (dr->which == Z_DiagRec_defaultFormat)
221                 fixup_nsd(odr, dr->u.defaultFormat);
222         }
223     }
224 }
225
226 void yf::Z3950Client::Assoc::fixup_init(ODR odr, Z_InitResponse *initrs)
227 {
228     Z_External *uif = initrs->userInformationField;
229
230     if (uif && uif->which == Z_External_userInfo1)
231     {
232         Z_OtherInformation *ui = uif->u.userInfo1;
233         int i;
234         for (i = 0; i < ui->num_elements; i++)
235         {
236             Z_OtherInformationUnit *unit = ui->list[i];
237             if (unit->which == Z_OtherInfo_externallyDefinedInfo &&
238                 unit->information.externallyDefinedInfo &&
239                 unit->information.externallyDefinedInfo->which ==
240                 Z_External_diag1)
241             {
242                 Z_DiagnosticFormat *diag =
243                     unit->information.externallyDefinedInfo->u.diag1;
244                 int j;
245                 for (j = 0; j < diag->num; j++)
246                 {
247                     Z_DiagnosticFormat_s *ds = diag->elements[j];
248                     if (ds->which == Z_DiagnosticFormat_s_defaultDiagRec)
249                     {
250                         Z_DefaultDiagFormat *r = ds->u.defaultDiagRec;
251                         char *oaddinfo = r->u.v2Addinfo;
252                         char *naddinfo = (char *) odr_malloc(
253                             odr,
254                             (oaddinfo ? strlen(oaddinfo) : 0) + 20 +
255                             m_host.length());
256                         *naddinfo = '\0';
257                         if (oaddinfo && *oaddinfo)
258                         {
259                             strcat(naddinfo, oaddinfo);
260                             strcat(naddinfo, " ");
261                         }
262                         strcat(naddinfo, "(backend=");
263                         strcat(naddinfo, m_host.c_str());
264                         strcat(naddinfo, ")");
265
266                         r->u.v2Addinfo = naddinfo;
267                     }
268                 }
269             }
270         }
271     }
272 }
273
274 void yf::Z3950Client::Assoc::recv_GDU(Z_GDU *gdu, int len)
275 {
276     m_waiting = false;
277
278     if (m_package)
279     {
280         mp::odr odr; // must be in scope for response() = assignment
281         if (gdu && gdu->which == Z_GDU_Z3950)
282         {
283             Z_APDU *apdu = gdu->u.z3950;
284             switch (apdu->which)
285             {
286             case Z_APDU_searchResponse:
287                 fixup_nsd(odr, apdu->u.searchResponse->records);
288                 break;
289             case Z_APDU_presentResponse:
290                 fixup_nsd(odr, apdu->u.presentResponse->records);
291                 break;
292             case Z_APDU_initResponse:
293                 fixup_init(odr, apdu->u.initResponse);
294                 m_connected = 2;
295                 break;
296             }
297         }
298         m_package->response() = gdu;
299     }
300 }
301
302 yazpp_1::IPDU_Observer *yf::Z3950Client::Assoc::sessionNotify(
303     yazpp_1::IPDU_Observable *the_PDU_Observable,
304     int fd)
305 {
306     return 0;
307 }
308
309
310 yf::Z3950Client::Z3950Client() :  m_p(new yf::Z3950Client::Rep)
311 {
312     m_p->m_connect_timeout_sec = 10;
313     m_p->m_init_timeout_sec = 10;
314     m_p->m_general_timeout_sec = 30;
315     m_p->m_max_sockets = 0;
316     m_p->m_force_close = false;
317     m_p->m_client_ip = false;
318     m_p->m_bind_host = false;
319 }
320
321 yf::Z3950Client::~Z3950Client() {
322 }
323
324 yf::Z3950Client::Assoc *yf::Z3950Client::Rep::get_assoc(Package &package)
325 {
326     // only one thread messes with the clients list at a time
327     boost::mutex::scoped_lock lock(m_mutex);
328
329     std::map<mp::Session,yf::Z3950Client::Assoc *>::iterator it;
330
331     Z_GDU *gdu = package.request().get();
332
333     int max_sockets = package.origin().get_max_sockets();
334     if (max_sockets == 0)
335         max_sockets = m_max_sockets;
336
337     it = m_clients.find(package.session());
338     if (it != m_clients.end())
339     {
340         it->second->m_queue_len++;
341         while (true)
342         {
343 #if 0
344             // double init .. NOT working yet
345             if (gdu && gdu->which == Z_GDU_Z3950 &&
346                 gdu->u.z3950->which == Z_APDU_initRequest)
347             {
348                 yazpp_1::SocketManager *s = it->second->m_socket_manager;
349                 delete it->second;  // destroy Z_Assoc
350                 delete s;    // then manager
351                 m_clients.erase(it);
352                 break;
353             }
354 #endif
355             if (!it->second->m_in_use)
356             {
357                 it->second->m_in_use = true;
358                 return it->second;
359             }
360             m_cond_session_ready.wait(lock);
361         }
362     }
363     if (!gdu || gdu->which != Z_GDU_Z3950)
364     {
365         package.move();
366         return 0;
367     }
368     // new Z39.50 session ..
369     Z_APDU *apdu = gdu->u.z3950;
370     // check that it is init. If not, close
371     if (apdu->which != Z_APDU_initRequest)
372     {
373         mp::odr odr;
374
375         package.response() = odr.create_close(apdu,
376                                               Z_Close_protocolError,
377                                               "First PDU was not an "
378                                               "Initialize Request");
379         package.session().close();
380         return 0;
381     }
382     std::string target = m_force_target;
383     if (!target.length())
384     {
385         target = m_default_target;
386         std::list<std::string> vhosts;
387         mp::util::remove_vhost_otherinfo(&apdu->u.initRequest->otherInfo,
388                                              vhosts);
389         size_t no_vhosts = vhosts.size();
390         if (no_vhosts == 1)
391         {
392             std::list<std::string>::const_iterator v_it = vhosts.begin();
393             target = *v_it;
394         }
395         else if (no_vhosts == 0)
396         {
397             if (!target.length())
398             {
399                 // no default target. So we don't know where to connect
400                 mp::odr odr;
401                 package.response() = odr.create_initResponse(
402                     apdu,
403                     YAZ_BIB1_INIT_NEGOTIATION_OPTION_REQUIRED,
404                     "z3950_client: No vhost given");
405
406                 package.session().close();
407                 return 0;
408             }
409         }
410         else if (no_vhosts > 1)
411         {
412             mp::odr odr;
413             package.response() = odr.create_initResponse(
414                 apdu,
415                 YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP,
416                 "z3950_client: Can not cope with multiple vhosts");
417             package.session().close();
418             return 0;
419         }
420     }
421
422     // see if we have reached max number of clients (max-sockets)
423
424     while (max_sockets)
425     {
426         int no_not_in_use = 0;
427         int number = 0;
428         it = m_clients.begin();
429         for (; it != m_clients.end(); it++)
430         {
431             yf::Z3950Client::Assoc *as = it->second;
432             if (!strcmp(as->m_host.c_str(), target.c_str()))
433             {
434                 number++;
435                 if (!as->m_in_use)
436                     no_not_in_use++;
437             }
438         }
439         yaz_log(YLOG_LOG, "Found %d/%d connections for %s", number, max_sockets,
440                 target.c_str());
441         if (number < max_sockets)
442             break;
443         if (no_not_in_use == 0) // all in use..
444         {
445             mp::odr odr;
446
447             package.response() = odr.create_initResponse(
448                 apdu, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
449                 "z3950_client: max sessions");
450             package.session().close();
451             return 0;
452         }
453         boost::xtime xt;
454         xtime_get(&xt,
455 #if BOOST_VERSION >= 105000 
456                 boost::TIME_UTC_
457 #else
458                 boost::TIME_UTC
459 #endif 
460                 );
461
462         xt.sec += 15;
463         if (!m_cond_session_ready.timed_wait(lock, xt))
464         {
465             mp::odr odr;
466
467             package.response() = odr.create_initResponse(
468                 apdu, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
469                 "z3950_client: max sessions");
470             package.session().close();
471             return 0;
472         }
473     }
474
475     yazpp_1::SocketManager *sm = new yazpp_1::SocketManager;
476     yazpp_1::PDU_Assoc *pdu_as = new yazpp_1::PDU_Assoc(sm);
477     yf::Z3950Client::Assoc *as =
478         new yf::Z3950Client::Assoc(sm, pdu_as, target.c_str(),
479                                    m_connect_timeout_sec,
480                                    m_init_timeout_sec,
481                                    m_general_timeout_sec);
482     m_clients[package.session()] = as;
483     return as;
484 }
485
486 static void set_charset_proposal(ODR odr, Z_InitRequest *req, const char *charset)
487 {
488     Z_OtherInformation **p = &req->otherInfo;
489     Z_OtherInformationUnit *oi;
490
491     if (*p)
492     {
493         int i;
494         for (i = 0; i < (*p)->num_elements; i++)
495         {
496             Z_External *ext = (*p)->list[i]->information.externallyDefinedInfo;
497             if ((*p)->list[i]->which == Z_OtherInfo_externallyDefinedInfo
498                 && ext &&
499                 ext->which == Z_External_charSetandLanguageNegotiation)
500                 return;
501         }
502     }
503     if ((oi = yaz_oi_update(p, odr, 0, 0, 0)))
504     {
505         ODR_MASK_SET(req->options, Z_Options_negotiationModel);
506         oi->which = Z_OtherInfo_externallyDefinedInfo;
507         oi->information.externallyDefinedInfo =
508             yaz_set_proposal_charneg_list(odr, ",",
509                                           charset,
510                                           0 /* lang */,
511                                           1 /* records included */);
512     }
513 }
514
515 void yf::Z3950Client::Rep::send_and_receive(Package &package,
516                                             yf::Z3950Client::Assoc *c)
517 {
518     if (c->m_destroyed)
519         return;
520
521     c->m_package = &package;
522
523     if (package.session().is_closed() && c->m_connected && !c->m_has_closed
524         && m_force_close)
525     {
526         mp::odr odr;
527
528         package.request() = odr.create_close(
529             0, Z_Close_finished, "z3950_client");
530         c->m_package = 0; // don't inspect response
531     }
532     Z_GDU *gdu = package.request().get();
533
534     if (!gdu || gdu->which != Z_GDU_Z3950)
535         return;
536
537     if (gdu->u.z3950->which == Z_APDU_close)
538         c->m_has_closed = true;
539
540     Z_APDU *apdu = gdu->u.z3950;
541
542     // prepare connect
543     c->m_time_elapsed = 0;
544     c->m_waiting = true;
545     if (!c->m_connected)
546     {
547         std::string host(c->m_host);
548
549         if (m_bind_host)
550         {
551             std::string bind_host = package.origin().get_bind_address();
552             if (bind_host.length())
553             {
554                 host.append(" ");
555                 host.append(bind_host);
556             }
557         }
558         if (c->client(host.c_str()))
559         {
560             mp::odr odr;
561             package.response() =
562                 odr.create_close(gdu->u.z3950, Z_Close_peerAbort, 0);
563             package.session().close();
564             return;
565         }
566         c->timeout(1);  // so timeoutNotify gets called once per second
567
568
569         while (!c->m_destroyed && c->m_waiting
570                && c->m_socket_manager->processEvent() > 0)
571             ;
572     }
573     if (!c->m_connected)
574     {
575         return;
576     }
577     mp::odr odr;
578     if (m_client_ip)
579     {
580         std::string peer_name2 = package.origin().get_address();
581         if (apdu->which == Z_APDU_initRequest && peer_name2.length())
582         {
583             Z_OtherInformation **oi = &apdu->u.initRequest->otherInfo;
584             char *peer_name1 =
585                 yaz_oi_get_string_oid(oi, yaz_oid_userinfo_client_ip, 1, 1);
586             std::string pcomb;
587             if (peer_name1)
588             {
589                 pcomb.append(peer_name1);
590                 pcomb.append(", ");
591             }
592             pcomb.append(peer_name2);
593             yaz_oi_set_string_oid(&apdu->u.initRequest->otherInfo,
594                                   odr, yaz_oid_userinfo_client_ip,
595                                   1, pcomb.c_str());
596         }
597     }
598     if (apdu->which == Z_APDU_initRequest && m_charset.length() > 0)
599         set_charset_proposal(odr, apdu->u.initRequest, m_charset.c_str());
600
601     // prepare response
602     c->m_time_elapsed = 0;
603     c->m_waiting = true;
604
605     // relay the package  ..
606     int len;
607     c->send_GDU(gdu, &len);
608
609     switch (gdu->u.z3950->which)
610     {
611     case Z_APDU_triggerResourceControlRequest:
612         // request only..
613         break;
614     default:
615         // for the rest: wait for a response PDU
616         while (!c->m_destroyed && c->m_waiting
617                && c->m_socket_manager->processEvent() > 0)
618             ;
619         break;
620     }
621 }
622
623 void yf::Z3950Client::Rep::release_assoc(Package &package)
624 {
625     boost::mutex::scoped_lock lock(m_mutex);
626     std::map<mp::Session,yf::Z3950Client::Assoc *>::iterator it;
627
628     it = m_clients.find(package.session());
629     if (it != m_clients.end())
630     {
631         it->second->m_in_use = false;
632         it->second->m_queue_len--;
633
634         if (package.session().is_closed())
635         {
636             // destroy hint (send_and_receive)
637             it->second->m_destroyed = true;
638             if (it->second->m_queue_len == 0)
639             {
640                 yazpp_1::SocketManager *s = it->second->m_socket_manager;
641                 delete it->second;  // destroy Z_Assoc
642                 delete s;    // then manager
643                 m_clients.erase(it);
644             }
645         }
646         m_cond_session_ready.notify_all();
647     }
648 }
649
650 void yf::Z3950Client::process(Package &package) const
651 {
652     yf::Z3950Client::Assoc *c = m_p->get_assoc(package);
653     if (c)
654     {
655         m_p->send_and_receive(package, c);
656         m_p->release_assoc(package);
657     }
658 }
659
660 void yf::Z3950Client::configure(const xmlNode *ptr, bool test_only,
661                                 const char *path)
662 {
663     for (ptr = ptr->children; ptr; ptr = ptr->next)
664     {
665         if (ptr->type != XML_ELEMENT_NODE)
666             continue;
667         if (!strcmp((const char *) ptr->name, "timeout"))
668         {
669             m_p->m_general_timeout_sec = mp::xml::get_int(ptr, 30);
670         }
671         else if (!strcmp((const char *) ptr->name, "connect-timeout"))
672         {
673             m_p->m_connect_timeout_sec = mp::xml::get_int(ptr, 10);
674         }
675         else if (!strcmp((const char *) ptr->name, "init-timeout"))
676         {
677             m_p->m_init_timeout_sec = mp::xml::get_int(ptr, 10);
678         }
679         else if (!strcmp((const char *) ptr->name, "default_target"))
680         {
681             m_p->m_default_target = mp::xml::get_text(ptr);
682         }
683         else if (!strcmp((const char *) ptr->name, "force_target"))
684         {
685             m_p->m_force_target = mp::xml::get_text(ptr);
686         }
687         else if (!strcmp((const char *) ptr->name, "max-sockets"))
688         {
689             m_p->m_max_sockets = mp::xml::get_int(ptr, 0);
690         }
691         else if (!strcmp((const char *) ptr->name, "force_close"))
692         {
693             m_p->m_force_close = mp::xml::get_bool(ptr, 0);
694         }
695         else if (!strcmp((const char *) ptr->name, "client_ip"))
696         {
697             m_p->m_client_ip = mp::xml::get_bool(ptr, 0);
698         }
699         else if (!strcmp((const char *) ptr->name, "charset"))
700         {
701             m_p->m_charset = mp::xml::get_text(ptr);
702         }
703         else if (!strcmp((const char *) ptr->name, "bind_host"))
704         {
705             m_p->m_bind_host = mp::xml::get_bool(ptr, 0);
706         }
707         else
708         {
709             throw mp::filter::FilterException("Bad element "
710                                                + std::string((const char *)
711                                                              ptr->name));
712         }
713     }
714 }
715
716 static mp::filter::Base* filter_creator()
717 {
718     return new mp::filter::Z3950Client;
719 }
720
721 extern "C" {
722     struct metaproxy_1_filter_struct metaproxy_1_filter_z3950_client = {
723         0,
724         "z3950_client",
725         filter_creator
726     };
727 }
728
729 /*
730  * Local variables:
731  * c-basic-offset: 4
732  * c-file-style: "Stroustrup"
733  * indent-tabs-mode: nil
734  * End:
735  * vim: shiftwidth=4 tabstop=8 expandtab
736  */
737