1 /* This file is part of Metaproxy.
2 Copyright (C) Index Data
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
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
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
21 #include "filter_z3950_client.hpp"
22 #include <metaproxy/package.hpp>
23 #include <metaproxy/util.hpp>
30 #include <boost/thread/mutex.hpp>
31 #include <boost/thread/condition.hpp>
32 #include <boost/thread/xtime.hpp>
36 #include <yaz/otherinfo.h>
37 #include <yaz/diagbib1.h>
38 #include <yaz/oid_db.h>
39 #include <yaz/charneg.h>
41 #include <yazpp/socket-manager.h>
42 #include <yazpp/pdu-assoc.h>
43 #include <yazpp/z-assoc.h>
45 namespace mp = metaproxy_1;
46 namespace yf = mp::filter;
48 namespace metaproxy_1 {
50 class Z3950Client::Assoc : public yazpp_1::Z_Assoc{
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);
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,
68 yazpp_1::SocketManager *m_socket_manager;
69 yazpp_1::IPDU_Observable *m_PDU_Observable;
74 int m_connected; // 0=not connected, 1=init phase, 2=rest
78 int m_connect_time_max;
80 int m_general_time_max;
84 class Z3950Client::Rep {
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;
94 std::string m_charset;
95 std::string m_default_target;
96 std::string m_force_target;
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);
110 yf::Z3950Client::Assoc::Assoc(yazpp_1::SocketManager *socket_manager,
111 yazpp_1::IPDU_Observable *PDU_Observable,
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),
121 m_connect_time_max(connect_timeout),
122 m_init_time_max(init_timeout),
123 m_general_time_max(general_timeout),
126 // std::cout << "create assoc " << this << "\n";
129 yf::Z3950Client::Assoc::~Assoc()
131 // std::cout << "destroy assoc " << this << "\n";
134 void yf::Z3950Client::Assoc::connectNotify()
141 void yf::Z3950Client::Assoc::failNotify()
149 Z_GDU *gdu = m_package->request().get();
151 if (gdu && gdu->which == Z_GDU_Z3950)
154 m_package->response() = odr.create_close(apdu, Z_Close_peerAbort, 0);
155 m_package->session().close();
159 void yf::Z3950Client::Assoc::timeoutNotify()
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))
172 Z_GDU *gdu = m_package->request().get();
174 if (gdu && gdu->which == Z_GDU_Z3950)
177 if (m_connected == 2)
178 m_package->response() =
179 odr.create_close(apdu, Z_Close_lackOfActivity, 0);
181 m_package->response() =
182 odr.create_close(apdu, Z_Close_peerAbort, 0);
184 m_package->session().close();
189 void yf::Z3950Client::Assoc::fixup_nsd(ODR odr, Z_DefaultDiagFormat *nsd)
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)
199 addinfo.assign(nsd->u.v2Addinfo);
202 addinfo += "(backend=" + m_host + ")";
203 nsd->u.v2Addinfo = odr_strdup(odr, addinfo.c_str());
206 void yf::Z3950Client::Assoc::fixup_nsd(ODR odr, Z_Records *records)
208 if (records && records->which == Z_Records_NSD)
210 fixup_nsd(odr, records->u.nonSurrogateDiagnostic);
212 if (records && records->which == Z_Records_multipleNSD)
214 Z_DiagRecs *drecs = records->u.multipleNonSurDiagnostics;
216 for (i = 0; i < drecs->num_diagRecs; i++)
218 Z_DiagRec *dr = drecs->diagRecs[i];
220 if (dr->which == Z_DiagRec_defaultFormat)
221 fixup_nsd(odr, dr->u.defaultFormat);
226 void yf::Z3950Client::Assoc::fixup_init(ODR odr, Z_InitResponse *initrs)
228 Z_External *uif = initrs->userInformationField;
230 if (uif && uif->which == Z_External_userInfo1)
232 Z_OtherInformation *ui = uif->u.userInfo1;
234 for (i = 0; i < ui->num_elements; i++)
236 Z_OtherInformationUnit *unit = ui->list[i];
237 if (unit->which == Z_OtherInfo_externallyDefinedInfo &&
238 unit->information.externallyDefinedInfo &&
239 unit->information.externallyDefinedInfo->which ==
242 Z_DiagnosticFormat *diag =
243 unit->information.externallyDefinedInfo->u.diag1;
245 for (j = 0; j < diag->num; j++)
247 Z_DiagnosticFormat_s *ds = diag->elements[j];
248 if (ds->which == Z_DiagnosticFormat_s_defaultDiagRec)
250 Z_DefaultDiagFormat *r = ds->u.defaultDiagRec;
251 char *oaddinfo = r->u.v2Addinfo;
252 char *naddinfo = (char *) odr_malloc(
254 (oaddinfo ? strlen(oaddinfo) : 0) + 20 +
257 if (oaddinfo && *oaddinfo)
259 strcat(naddinfo, oaddinfo);
260 strcat(naddinfo, " ");
262 strcat(naddinfo, "(backend=");
263 strcat(naddinfo, m_host.c_str());
264 strcat(naddinfo, ")");
266 r->u.v2Addinfo = naddinfo;
274 void yf::Z3950Client::Assoc::recv_GDU(Z_GDU *gdu, int len)
280 mp::odr odr; // must be in scope for response() = assignment
281 if (gdu && gdu->which == Z_GDU_Z3950)
283 Z_APDU *apdu = gdu->u.z3950;
286 case Z_APDU_searchResponse:
287 fixup_nsd(odr, apdu->u.searchResponse->records);
289 case Z_APDU_presentResponse:
290 fixup_nsd(odr, apdu->u.presentResponse->records);
292 case Z_APDU_initResponse:
293 fixup_init(odr, apdu->u.initResponse);
298 m_package->response() = gdu;
302 yazpp_1::IPDU_Observer *yf::Z3950Client::Assoc::sessionNotify(
303 yazpp_1::IPDU_Observable *the_PDU_Observable,
310 yf::Z3950Client::Z3950Client() : m_p(new yf::Z3950Client::Rep)
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;
321 yf::Z3950Client::~Z3950Client() {
324 yf::Z3950Client::Assoc *yf::Z3950Client::Rep::get_assoc(Package &package)
326 // only one thread messes with the clients list at a time
327 boost::mutex::scoped_lock lock(m_mutex);
329 std::map<mp::Session,yf::Z3950Client::Assoc *>::iterator it;
331 Z_GDU *gdu = package.request().get();
333 int max_sockets = package.origin().get_max_sockets();
334 if (max_sockets == 0)
335 max_sockets = m_max_sockets;
337 it = m_clients.find(package.session());
338 if (it != m_clients.end())
340 it->second->m_queue_len++;
344 // double init .. NOT working yet
345 if (gdu && gdu->which == Z_GDU_Z3950 &&
346 gdu->u.z3950->which == Z_APDU_initRequest)
348 yazpp_1::SocketManager *s = it->second->m_socket_manager;
349 delete it->second; // destroy Z_Assoc
350 delete s; // then manager
355 if (!it->second->m_in_use)
357 it->second->m_in_use = true;
360 m_cond_session_ready.wait(lock);
363 if (!gdu || gdu->which != Z_GDU_Z3950)
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)
375 package.response() = odr.create_close(apdu,
376 Z_Close_protocolError,
377 "First PDU was not an "
378 "Initialize Request");
379 package.session().close();
382 std::string target = m_force_target;
383 if (!target.length())
385 target = m_default_target;
386 std::list<std::string> vhosts;
387 mp::util::remove_vhost_otherinfo(&apdu->u.initRequest->otherInfo,
389 size_t no_vhosts = vhosts.size();
392 std::list<std::string>::const_iterator v_it = vhosts.begin();
395 else if (no_vhosts == 0)
397 if (!target.length())
399 // no default target. So we don't know where to connect
401 package.response() = odr.create_initResponse(
403 YAZ_BIB1_INIT_NEGOTIATION_OPTION_REQUIRED,
404 "z3950_client: No vhost given");
406 package.session().close();
410 else if (no_vhosts > 1)
413 package.response() = odr.create_initResponse(
415 YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP,
416 "z3950_client: Can not cope with multiple vhosts");
417 package.session().close();
422 // see if we have reached max number of clients (max-sockets)
426 int no_not_in_use = 0;
428 it = m_clients.begin();
429 for (; it != m_clients.end(); it++)
431 yf::Z3950Client::Assoc *as = it->second;
432 if (!strcmp(as->m_host.c_str(), target.c_str()))
439 yaz_log(YLOG_LOG, "Found %d/%d connections for %s", number, max_sockets,
441 if (number < max_sockets)
443 if (no_not_in_use == 0) // all in use..
447 package.response() = odr.create_initResponse(
448 apdu, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
449 "z3950_client: max sessions");
450 package.session().close();
455 #if BOOST_VERSION >= 105000
463 if (!m_cond_session_ready.timed_wait(lock, xt))
467 package.response() = odr.create_initResponse(
468 apdu, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
469 "z3950_client: max sessions");
470 package.session().close();
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,
481 m_general_timeout_sec);
482 m_clients[package.session()] = as;
486 static void set_charset_proposal(ODR odr, Z_InitRequest *req, const char *charset)
488 Z_OtherInformation **p = &req->otherInfo;
489 Z_OtherInformationUnit *oi;
494 for (i = 0; i < (*p)->num_elements; i++)
496 Z_External *ext = (*p)->list[i]->information.externallyDefinedInfo;
497 if ((*p)->list[i]->which == Z_OtherInfo_externallyDefinedInfo
499 ext->which == Z_External_charSetandLanguageNegotiation)
503 if ((oi = yaz_oi_update(p, odr, 0, 0, 0)))
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, ",",
511 1 /* records included */);
515 void yf::Z3950Client::Rep::send_and_receive(Package &package,
516 yf::Z3950Client::Assoc *c)
521 c->m_package = &package;
523 if (package.session().is_closed() && c->m_connected && !c->m_has_closed
528 package.request() = odr.create_close(
529 0, Z_Close_finished, "z3950_client");
530 c->m_package = 0; // don't inspect response
532 Z_GDU *gdu = package.request().get();
534 if (!gdu || gdu->which != Z_GDU_Z3950)
537 if (gdu->u.z3950->which == Z_APDU_close)
538 c->m_has_closed = true;
540 Z_APDU *apdu = gdu->u.z3950;
543 c->m_time_elapsed = 0;
547 std::string host(c->m_host);
551 std::string bind_host = package.origin().get_bind_address();
552 if (bind_host.length())
555 host.append(bind_host);
558 if (c->client(host.c_str()))
562 odr.create_close(gdu->u.z3950, Z_Close_peerAbort, 0);
563 package.session().close();
566 c->timeout(1); // so timeoutNotify gets called once per second
569 while (!c->m_destroyed && c->m_waiting
570 && c->m_socket_manager->processEvent() > 0)
580 std::string peer_name2 = package.origin().get_address();
581 if (apdu->which == Z_APDU_initRequest && peer_name2.length())
583 Z_OtherInformation **oi = &apdu->u.initRequest->otherInfo;
585 yaz_oi_get_string_oid(oi, yaz_oid_userinfo_client_ip, 1, 1);
589 pcomb.append(peer_name1);
592 pcomb.append(peer_name2);
593 yaz_oi_set_string_oid(&apdu->u.initRequest->otherInfo,
594 odr, yaz_oid_userinfo_client_ip,
598 if (apdu->which == Z_APDU_initRequest && m_charset.length() > 0)
599 set_charset_proposal(odr, apdu->u.initRequest, m_charset.c_str());
602 c->m_time_elapsed = 0;
605 // relay the package ..
607 c->send_GDU(gdu, &len);
609 switch (gdu->u.z3950->which)
611 case Z_APDU_triggerResourceControlRequest:
615 // for the rest: wait for a response PDU
616 while (!c->m_destroyed && c->m_waiting
617 && c->m_socket_manager->processEvent() > 0)
623 void yf::Z3950Client::Rep::release_assoc(Package &package)
625 boost::mutex::scoped_lock lock(m_mutex);
626 std::map<mp::Session,yf::Z3950Client::Assoc *>::iterator it;
628 it = m_clients.find(package.session());
629 if (it != m_clients.end())
631 it->second->m_in_use = false;
632 it->second->m_queue_len--;
634 if (package.session().is_closed())
636 // destroy hint (send_and_receive)
637 it->second->m_destroyed = true;
638 if (it->second->m_queue_len == 0)
640 yazpp_1::SocketManager *s = it->second->m_socket_manager;
641 delete it->second; // destroy Z_Assoc
642 delete s; // then manager
646 m_cond_session_ready.notify_all();
650 void yf::Z3950Client::process(Package &package) const
652 yf::Z3950Client::Assoc *c = m_p->get_assoc(package);
655 m_p->send_and_receive(package, c);
656 m_p->release_assoc(package);
660 void yf::Z3950Client::configure(const xmlNode *ptr, bool test_only,
663 for (ptr = ptr->children; ptr; ptr = ptr->next)
665 if (ptr->type != XML_ELEMENT_NODE)
667 if (!strcmp((const char *) ptr->name, "timeout"))
669 m_p->m_general_timeout_sec = mp::xml::get_int(ptr, 30);
671 else if (!strcmp((const char *) ptr->name, "connect-timeout"))
673 m_p->m_connect_timeout_sec = mp::xml::get_int(ptr, 10);
675 else if (!strcmp((const char *) ptr->name, "init-timeout"))
677 m_p->m_init_timeout_sec = mp::xml::get_int(ptr, 10);
679 else if (!strcmp((const char *) ptr->name, "default_target"))
681 m_p->m_default_target = mp::xml::get_text(ptr);
683 else if (!strcmp((const char *) ptr->name, "force_target"))
685 m_p->m_force_target = mp::xml::get_text(ptr);
687 else if (!strcmp((const char *) ptr->name, "max-sockets"))
689 m_p->m_max_sockets = mp::xml::get_int(ptr, 0);
691 else if (!strcmp((const char *) ptr->name, "force_close"))
693 m_p->m_force_close = mp::xml::get_bool(ptr, 0);
695 else if (!strcmp((const char *) ptr->name, "client_ip"))
697 m_p->m_client_ip = mp::xml::get_bool(ptr, 0);
699 else if (!strcmp((const char *) ptr->name, "charset"))
701 m_p->m_charset = mp::xml::get_text(ptr);
703 else if (!strcmp((const char *) ptr->name, "bind_host"))
705 m_p->m_bind_host = mp::xml::get_bool(ptr, 0);
709 throw mp::filter::FilterException("Bad element "
710 + std::string((const char *)
716 static mp::filter::Base* filter_creator()
718 return new mp::filter::Z3950Client;
722 struct metaproxy_1_filter_struct metaproxy_1_filter_z3950_client = {
732 * c-file-style: "Stroustrup"
733 * indent-tabs-mode: nil
735 * vim: shiftwidth=4 tabstop=8 expandtab