metaproxy  1.21.0
filter_frontend_net.cpp
Go to the documentation of this file.
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 #if HAVE_GETRLIMIT
22 #include <sys/resource.h>
23 #endif
24 #include <sstream>
25 #include <iomanip>
26 #include <metaproxy/util.hpp>
27 #include "pipe.hpp"
28 #include <metaproxy/filter.hpp>
29 #include <metaproxy/package.hpp>
30 #include "thread_pool_observer.hpp"
31 #include "filter_frontend_net.hpp"
32 #include <yazpp/z-assoc.h>
33 #include <yazpp/pdu-assoc.h>
34 #include <yazpp/socket-manager.h>
35 #include <yazpp/limit-connect.h>
36 #include <yaz/timing.h>
37 #include <yaz/log.h>
38 #include <yaz/daemon.h>
39 #include <yaz/malloc_info.h>
40 #include "gduutil.hpp"
41 #include <signal.h>
42 
43 #include <iostream>
44 
45 namespace mp = metaproxy_1;
46 namespace yf = metaproxy_1::filter;
47 
48 namespace metaproxy_1 {
49  namespace filter {
51  friend class FrontendNet;
52  PeerStat();
53  ~PeerStat();
54  size_t get(const std::string &peer);
55  size_t add(const std::string &peer);
56  size_t remove(const std::string &peer);
57  class Item {
58  friend class PeerStat;
59  std::string m_peer;
60  size_t cnt;
62  };
64  };
66  friend class Rep;
67  friend class FrontendNet;
68  std::string port;
69  std::string route;
70  std::string cert_fname;
72  };
74  friend class Rep;
75  friend class FrontendNet;
76  std::string pattern;
77  int verbose;
78  int value;
79  };
81  friend class FrontendNet;
82 
86  std::vector<Port> m_ports;
88  std::list<IP_Pattern> session_timeout;
89  std::list<IP_Pattern> connect_max;
90  std::list<IP_Pattern> connect_total;
91  std::list<IP_Pattern> http_req_max;
92  std::string m_msg_config;
93  std::string m_stat_req;
94  yazpp_1::SocketManager mySocketManager;
96  yazpp_1::PDU_Assoc **pdu;
97  int m_duration_freq[22];
98  double m_duration_lim[22];
104  public:
105  Rep();
106  ~Rep();
107  };
108  class FrontendNet::My_Timer_Thread : public yazpp_1::ISocketObserver {
109  private:
110  yazpp_1::ISocketObservable *m_obs;
112  bool m_timeout;
113  public:
114  My_Timer_Thread(yazpp_1::ISocketObservable *obs, int duration);
115  void socketNotify(int event);
116  bool timeout();
117  };
118  class FrontendNet::ZAssocChild : public yazpp_1::Z_Assoc {
119  public:
120  ~ZAssocChild();
121  ZAssocChild(yazpp_1::IPDU_Observable *the_PDU_Observable,
123  const mp::Package *package,
124  Port *port,
125  Rep *rep,
126  yazpp_1::LimitConnect &limit,
127  const char *peername);
130  private:
131  yazpp_1::IPDU_Observer* sessionNotify(
132  yazpp_1::IPDU_Observable *the_PDU_Observable,
133  int fd);
134  void recv_GDU(Z_GDU *apdu, int len);
135  void report(Z_HTTP_Request *hreq);
136  void failNotify();
137  void timeoutNotify();
138  void connectNotify();
139  private:
141  mp::Session m_session;
142  mp::Origin m_origin;
144  const mp::Package *m_package;
146  yazpp_1::LimitConnect &m_limit_http_req;
147  std::string m_peer;
148  };
150  public:
151  ThreadPoolPackage(mp::Package *package,
152  yf::FrontendNet::ZAssocChild *ses,
153  Rep *rep);
156  void result(const char *t_info);
157  bool cleanup(void *info);
158  private:
159  yaz_timing_t timer;
161  mp::Package *m_package;
163  };
164  class FrontendNet::ZAssocServer : public yazpp_1::Z_Assoc {
165  public:
166  ~ZAssocServer();
167  ZAssocServer(yazpp_1::IPDU_Observable *PDU_Observable,
168  FrontendNet::Port *port,
169  Rep *rep);
170  void set_package(const mp::Package *package);
172  private:
173  yazpp_1::IPDU_Observer* sessionNotify(
174  yazpp_1::IPDU_Observable *the_PDU_Observable,
175  int fd);
176  void recv_GDU(Z_GDU *apdu, int len);
177 
178  void failNotify();
179  void timeoutNotify();
180  void connectNotify();
181  private:
183  const mp::Package *m_package;
184  yazpp_1::LimitConnect limit_connect;
185  yazpp_1::LimitConnect limit_http_req;
188  };
189  }
190 }
191 
192 yf::FrontendNet::ThreadPoolPackage::ThreadPoolPackage(mp::Package *package,
193  ZAssocChild *ses,
194  Rep *rep) :
195  m_assoc_child(ses), m_package(package), m_p(rep)
196 {
197  timer = yaz_timing_create();
198 }
199 
200 yf::FrontendNet::ThreadPoolPackage::~ThreadPoolPackage()
201 {
202  yaz_timing_destroy(&timer); // timer may be NULL
203  delete m_package;
204 }
205 
206 bool yf::FrontendNet::ThreadPoolPackage::cleanup(void *info)
207 {
208  mp::Session *ses = (mp::Session *) info;
209  if (*ses == m_package->session())
210  {
211  m_assoc_child->m_no_requests--;
212  return true;
213  }
214  return false;
215 }
216 
217 void yf::FrontendNet::ThreadPoolPackage::result(const char *t_info)
218 {
219  m_assoc_child->m_no_requests--;
220 
221  yazpp_1::GDU *gdu = &m_package->response();
222 
223  if (gdu->get())
224  {
225  int len;
226  m_assoc_child->send_GDU(gdu->get(), &len);
227 
228  yaz_timing_stop(timer);
229  double duration = yaz_timing_get_real(timer);
230 
231  size_t ent = 0;
232  while (m_p->m_duration_lim[ent] != 0.0 && duration > m_p->m_duration_lim[ent])
233  ent++;
234  m_p->m_duration_freq[ent]++;
235 
236  m_p->m_duration_total += duration;
237 
238  if (m_p->m_duration_max < duration)
239  m_p->m_duration_max = duration;
240 
241  if (m_p->m_duration_min == 0.0 || m_p->m_duration_min > duration)
242  m_p->m_duration_min = duration;
243 
244  if (m_p->m_msg_config.length())
245  {
246  Z_GDU *z_gdu = gdu->get();
247 
248  std::ostringstream os;
249  os << m_p->m_msg_config << " "
250  << *m_package << " "
251  << std::fixed << std::setprecision (6) << duration << " ";
252 
253  if (z_gdu)
254  os << *z_gdu;
255  else
256  os << "-";
257 
258  yaz_log(YLOG_LOG, "%s %s", os.str().c_str(), t_info);
259  }
260  }
261  else if (!m_package->session().is_closed())
262  {
263  // no response package and yet the session is still open..
264  // means that request is unhandled..
265  yazpp_1::GDU *gdu_req = &m_package->request();
266  Z_GDU *z_gdu = gdu_req->get();
267  if (z_gdu && z_gdu->which == Z_GDU_Z3950)
268  {
269  // For Z39.50, response with a Close and shutdown
270  mp::odr odr;
271  int len;
272  Z_APDU *apdu_response = odr.create_close(
273  z_gdu->u.z3950, Z_Close_systemProblem,
274  "unhandled Z39.50 request");
275 
276  m_assoc_child->send_Z_PDU(apdu_response, &len);
277  }
278  else if (z_gdu && z_gdu->which == Z_GDU_HTTP_Request)
279  {
280  // For HTTP, respond with Server Error
281  int len;
282  mp::odr odr;
283  Z_GDU *zgdu_res
284  = odr.create_HTTP_Response(m_package->session(),
285  z_gdu->u.HTTP_Request, 500);
286  m_assoc_child->send_GDU(zgdu_res, &len);
287  }
288  m_package->session().close();
289  }
290 
291  if (m_assoc_child->m_no_requests == 0 && m_package->session().is_closed())
292  {
293  m_assoc_child->close();
294  }
295 
296 
297  delete this;
298 }
299 
300 mp::IThreadPoolMsg *yf::FrontendNet::ThreadPoolPackage::handle()
301 {
302  m_package->move(m_assoc_child->m_port->route);
303  return this;
304 }
305 
306 yf::FrontendNet::ZAssocChild::ZAssocChild(
307  yazpp_1::IPDU_Observable *PDU_Observable,
308  mp::ThreadPoolSocketObserver *my_thread_pool,
309  const mp::Package *package,
310  Port *port, Rep *rep,
311  yazpp_1::LimitConnect &limit_http_req,
312  const char *peername)
313  : Z_Assoc(PDU_Observable), m_p(rep), m_limit_http_req(limit_http_req)
314  , m_peer(peername)
315 {
316  m_thread_pool_observer = my_thread_pool;
317  m_no_requests = 0;
318  m_delete_flag = false;
319  m_package = package;
320  m_port = port;
321  std::string addr;
322  addr.append(peername);
323  addr.append(" ");
324  addr.append(port->port);
326  m_origin.set_tcpip_address(addr, m_session.id());
327  int session_timeout = 300; // 5 minutes
328  if (peername) {
329  std::list<IP_Pattern>::const_iterator it = m_p->session_timeout.begin();
330  for (; it != m_p->session_timeout.end(); it++)
331  if (mp::util::match_ip(it->pattern, peername))
332  {
333  if (it->verbose > 1)
334  yaz_log(YLOG_LOG, "timeout pattern=%s ip=%s value=%d",
335  it->pattern.c_str(),
336  peername, it->value);
337  session_timeout = it->value;
338  break;
339  }
340  }
341  timeout(session_timeout);
342 }
343 
344 yazpp_1::IPDU_Observer *yf::FrontendNet::ZAssocChild::sessionNotify(
345  yazpp_1::IPDU_Observable *the_PDU_Observable, int fd)
346 {
347  return 0;
348 }
349 
350 yf::FrontendNet::ZAssocChild::~ZAssocChild()
351 {
352  int d = m_p->m_peerStat.remove(m_peer);
353  if (m_p->m_msg_config.length())
354  {
355  std::ostringstream os;
356  os << m_p->m_msg_config << " "
357  << m_peer << " closing cnt=" << d;
358  yaz_log(YLOG_LOG, "%s", os.str().c_str());
359  }
360 }
361 
362 void yf::FrontendNet::ZAssocChild::report(Z_HTTP_Request *hreq)
363 {
364  mp::odr o;
365 
366  Z_GDU *gdu_res = o.create_HTTP_Response(m_session, hreq, 200);
367 
368  Z_HTTP_Response *hres = gdu_res->u.HTTP_Response;
369 
370  mp::wrbuf w;
371  size_t i;
372  int number_total = 0;
373 
374  for (i = 0; m_p->m_duration_lim[i] != 0.0; i++)
375  number_total += m_p->m_duration_freq[i];
376  number_total += m_p->m_duration_freq[i];
377 
378  wrbuf_puts(w, "<?xml version=\"1.0\"?>\n");
379  wrbuf_puts(w, "<frontend_net>\n");
380  wrbuf_printf(w, " <responses frequency=\"%d\">\n", number_total);
381  for (i = 0; m_p->m_duration_lim[i] != 0.0; i++)
382  {
383  if (m_p->m_duration_freq[i] > 0)
384  wrbuf_printf(
385  w, " <response duration_start=\"%f\" "
386  "duration_end=\"%f\" frequency=\"%d\"/>\n",
387  i > 0 ? m_p->m_duration_lim[i - 1] : 0.0,
388  m_p->m_duration_lim[i], m_p->m_duration_freq[i]);
389  }
390 
391  if (m_p->m_duration_freq[i] > 0)
392  wrbuf_printf(
393  w, " <response duration_start=\"%f\" frequency=\"%d\"/>\n",
394  m_p->m_duration_lim[i - 1], m_p->m_duration_freq[i]);
395 
396  if (m_p->m_duration_max != 0.0)
397  wrbuf_printf(
398  w, " <response duration_max=\"%f\"/>\n",
399  m_p->m_duration_max);
400  if (m_p->m_duration_min != 0.0)
401  wrbuf_printf(
402  w, " <response duration_min=\"%f\"/>\n",
403  m_p->m_duration_min);
404  if (m_p->m_duration_total != 0.0)
405  wrbuf_printf(
406  w, " <response duration_average=\"%f\"/>\n",
407  m_p->m_duration_total / number_total);
408 
409  wrbuf_puts(w, " </responses>\n");
410 
411  int thread_busy;
412  int thread_total;
413  m_thread_pool_observer->get_thread_info(thread_busy, thread_total);
414 
415  wrbuf_printf(w, " <thread_info busy=\"%d\" total=\"%d\"/>\n",
416  thread_busy, thread_total);
417 
418  wrbuf_malloc_info(w);
419 
420  {
421  char buf[200];
422  if (nmem_get_status(buf, sizeof(buf) - 1) == 0)
423  wrbuf_puts(w, buf);
424  }
425  wrbuf_puts(w, "</frontend_net>\n");
426 
427  hres->content_len = w.len();
428  hres->content_buf = (char *) w.buf();
429 
430  int len;
431  send_GDU(gdu_res, &len);
432 }
433 
434 void yf::FrontendNet::ZAssocChild::recv_GDU(Z_GDU *z_pdu, int len)
435 {
436  m_no_requests++;
437 
438  mp::Package *p = new mp::Package(m_session, m_origin);
439 
440  if (z_pdu && z_pdu->which == Z_GDU_HTTP_Request)
441  {
442  Z_HTTP_Request *hreq = z_pdu->u.HTTP_Request;
443 
444  const char *f = z_HTTP_header_lookup(hreq->headers, "X-Forwarded-For");
445  if (f)
446  p->origin().set_tcpip_address(std::string(f), m_session.id());
447 
448  if (m_p->m_stat_req.length()
449  && !strcmp(hreq->path, m_p->m_stat_req.c_str()))
450  {
451  report(hreq);
452  delete p;
453  delete this;
454  return;
455  }
456  }
457 
458  p->copy_route(*m_package);
459  p->request() = yazpp_1::GDU(z_pdu);
460 
461  if (m_p->m_msg_config.length())
462  {
463  if (z_pdu)
464  {
465  std::ostringstream os;
466  os << m_p->m_msg_config << " "
467  << *p << " "
468  << "0.000000" << " "
469  << *z_pdu;
470  yaz_log(YLOG_LOG, "%s", os.str().c_str());
471  }
472  }
473  if (z_pdu && z_pdu->which == Z_GDU_HTTP_Request)
474  {
475  Z_HTTP_Request *hreq = z_pdu->u.HTTP_Request;
476  std::string peername = p->origin().get_address();
477 
478  m_limit_http_req.cleanup(false);
479  int con_sz = m_limit_http_req.get_total(peername.c_str());
480  std::list<IP_Pattern>::const_iterator it = m_p->http_req_max.begin();
481  for (; it != m_p->http_req_max.end(); it++)
482  {
483  if (mp::util::match_ip(it->pattern, peername))
484  {
485  if (it->verbose > 1 ||
486  (it->value && con_sz >= it->value && it->verbose > 0))
487  yaz_log(YLOG_LOG, "http-req-max pattern=%s ip=%s con_sz=%d value=%d", it->pattern.c_str(), peername.c_str(), con_sz, it->value);
488  if (con_sz < it->value)
489  break;
490  mp::odr o;
491  Z_GDU *gdu_res = o.create_HTTP_Response(m_session, hreq, 500);
492  int len;
493  send_GDU(gdu_res, &len);
494  delete p;
495  delete this;
496  return;
497  }
498  }
499  m_limit_http_req.add_connect(peername.c_str());
500  }
501  ThreadPoolPackage *tp = new ThreadPoolPackage(p, this, m_p);
502  m_thread_pool_observer->put(tp);
503 }
504 
505 void yf::FrontendNet::ZAssocChild::failNotify()
506 {
507  // TODO: send Package to signal "close"
508  if (m_session.is_closed())
509  {
510  if (m_no_requests == 0)
511  delete this;
512  return;
513  }
514  m_no_requests++;
515 
516  m_session.close();
517 
518  mp::Package *p = new mp::Package(m_session, m_origin);
519 
520  ThreadPoolPackage *tp = new ThreadPoolPackage(p, this, m_p);
521  p->copy_route(*m_package);
522  m_thread_pool_observer->cleanup(tp, &m_session);
523  m_thread_pool_observer->put(tp);
524 }
525 
526 void yf::FrontendNet::ZAssocChild::timeoutNotify()
527 {
528  failNotify();
529 }
530 
531 void yf::FrontendNet::ZAssocChild::connectNotify()
532 {
533 
534 }
535 
536 yf::FrontendNet::ZAssocServer::ZAssocServer(
537  yazpp_1::IPDU_Observable *PDU_Observable,
538  Port *port,
539  Rep *rep)
540  :
541  Z_Assoc(PDU_Observable), m_port(port), m_p(rep)
542 {
543  m_package = 0;
544 }
545 
546 
547 void yf::FrontendNet::ZAssocServer::set_package(const mp::Package *package)
548 {
549  m_package = package;
550 }
551 
552 void yf::FrontendNet::ZAssocServer::set_thread_pool(
554 {
555  m_thread_pool_observer = observer;
556 }
557 
558 yazpp_1::IPDU_Observer *yf::FrontendNet::ZAssocServer::sessionNotify(
559  yazpp_1::IPDU_Observable *the_PDU_Observable, int fd)
560 {
561 
562  const char *peername = the_PDU_Observable->getpeername();
563  if (!peername)
564  peername = "unknown";
565  else
566  {
567  const char *cp = strchr(peername, ':');
568  if (cp)
569  peername = cp + 1;
570  }
571  if (peername)
572  {
573  int total_sz = m_p->m_peerStat.get(peername);
574  std::list<IP_Pattern>::const_iterator it = m_p->connect_total.begin();
575  for (; it != m_p->connect_total.end(); it++)
576  {
577  if (mp::util::match_ip(it->pattern, peername))
578  {
579  if (it->verbose > 1 ||
580  (it->value && total_sz >= it->value && it->verbose > 0))
581  yaz_log(YLOG_LOG, "connect-total pattern=%s ip=%s con_sz=%d value=%d", it->pattern.c_str(), peername, total_sz, it->value);
582  if (total_sz < it->value)
583  break;
584  return 0;
585  }
586  }
587  limit_connect.cleanup(false);
588  int con_sz = limit_connect.get_total(peername);
589  it = m_p->connect_max.begin();
590  for (; it != m_p->connect_max.end(); it++)
591  {
592  if (mp::util::match_ip(it->pattern, peername))
593  {
594  if (it->verbose > 1 ||
595  (it->value && con_sz >= it->value && it->verbose > 0))
596  yaz_log(YLOG_LOG, "connect-max pattern=%s ip=%s con_sz=%d value=%d", it->pattern.c_str(), peername, con_sz, it->value);
597  if (con_sz < it->value)
598  break;
599  return 0;
600  }
601  }
602  limit_connect.add_connect(peername);
603  }
604  ZAssocChild *my = new ZAssocChild(the_PDU_Observable,
605  m_thread_pool_observer,
606  m_package, m_port, m_p, limit_http_req,
607  peername);
608  return my;
609 }
610 
611 yf::FrontendNet::ZAssocServer::~ZAssocServer()
612 {
613 }
614 
615 void yf::FrontendNet::ZAssocServer::recv_GDU(Z_GDU *apdu, int len)
616 {
617 }
618 
619 void yf::FrontendNet::ZAssocServer::failNotify()
620 {
621 }
622 
623 void yf::FrontendNet::ZAssocServer::timeoutNotify()
624 {
625 }
626 
627 void yf::FrontendNet::ZAssocServer::connectNotify()
628 {
629 }
630 
631 yf::FrontendNet::FrontendNet() : m_p(new Rep)
632 {
633 }
634 
635 yf::FrontendNet::Rep::Rep()
636 {
637  m_max_threads = m_no_threads = 5;
638  m_stack_size = 0;
639  m_listen_duration = 0;
640  az = 0;
641  size_t i;
642  for (i = 0; i < 22; i++)
643  m_duration_freq[i] = 0;
644  m_duration_lim[0] = 0.000001;
645  m_duration_lim[1] = 0.00001;
646  m_duration_lim[2] = 0.0001;
647  m_duration_lim[3] = 0.001;
648  m_duration_lim[4] = 0.01;
649  m_duration_lim[5] = 0.1;
650  m_duration_lim[6] = 0.2;
651  m_duration_lim[7] = 0.3;
652  m_duration_lim[8] = 0.5;
653  m_duration_lim[9] = 1.0;
654  m_duration_lim[10] = 1.5;
655  m_duration_lim[11] = 2.0;
656  m_duration_lim[12] = 3.0;
657  m_duration_lim[13] = 4.0;
658  m_duration_lim[14] = 5.0;
659  m_duration_lim[15] = 6.0;
660  m_duration_lim[16] = 8.0;
661  m_duration_lim[17] = 10.0;
662  m_duration_lim[18] = 15.0;
663  m_duration_lim[19] = 20.0;
664  m_duration_lim[20] = 30.0;
665  m_duration_lim[21] = 0.0;
666  m_duration_max = 0.0;
667  m_duration_min = 0.0;
668  m_duration_total = 0.0;
669  m_stop_signo = 0;
670 }
671 
672 yf::FrontendNet::Rep::~Rep()
673 {
674  if (az)
675  {
676  size_t i;
677  for (i = 0; i < m_ports.size(); i++)
678  delete az[i];
679  delete [] az;
680  delete [] pdu;
681  }
682  az = 0;
683 }
684 
685 yf::FrontendNet::~FrontendNet()
686 {
687 }
688 
689 void yf::FrontendNet::stop(int signo) const
690 {
691  m_p->m_stop_signo = signo;
692 }
693 
694 void yf::FrontendNet::start() const
695 {
696 #if HAVE_GETRLIMIT
697  struct rlimit limit_data;
698  getrlimit(RLIMIT_NOFILE, &limit_data);
699  yaz_log(YLOG_LOG, "getrlimit NOFILE cur=%ld max=%ld",
700  (long) limit_data.rlim_cur, (long) limit_data.rlim_max);
701 #endif
702 }
703 
704 bool yf::FrontendNet::My_Timer_Thread::timeout()
705 {
706  return m_timeout;
707 }
708 
709 yf::FrontendNet::My_Timer_Thread::My_Timer_Thread(
710  yazpp_1::ISocketObservable *obs,
711  int duration) :
712  m_obs(obs), m_pipe(9123), m_timeout(false)
713 {
714  obs->addObserver(m_pipe.read_fd(), this);
715  obs->maskObserver(this, yazpp_1::SOCKET_OBSERVE_READ);
716  obs->timeoutObserver(this, duration);
717 }
718 
719 void yf::FrontendNet::My_Timer_Thread::socketNotify(int event)
720 {
721  m_timeout = true;
722  m_obs->deleteObserver(this);
723 }
724 
725 void yf::FrontendNet::process(mp::Package &package) const
726 {
727  if (m_p->az == 0)
728  return;
729  size_t i;
730  My_Timer_Thread *tt = 0;
731 
732  if (m_p->m_listen_duration)
733  tt = new My_Timer_Thread(&m_p->mySocketManager,
734  m_p->m_listen_duration);
735 
737  new ThreadPoolSocketObserver(&m_p->mySocketManager, m_p->m_no_threads,
738  m_p->m_max_threads,
739  m_p->m_stack_size);
740 
741  for (i = 0; i<m_p->m_ports.size(); i++)
742  {
743  m_p->az[i]->set_package(&package);
744  m_p->az[i]->set_thread_pool(
745  tp);
746  }
747  while (m_p->mySocketManager.processEvent() > 0)
748  {
749  if (m_p->m_stop_signo == SIGTERM)
750  {
751  yaz_log(YLOG_LOG, "metaproxy received SIGTERM");
752  if (m_p->az)
753  {
754  size_t i;
755  for (i = 0; i < m_p->m_ports.size(); i++)
756  {
757  m_p->pdu[i]->shutdown();
758  m_p->az[i]->server("");
759  }
760  yaz_daemon_stop();
761  }
762  return; // do not even attempt to destroy tp or tt
763  }
764 #ifndef WIN32
765  if (m_p->m_stop_signo == SIGUSR1)
766  { /* just stop listeners and cont till all sessions are done*/
767  yaz_log(YLOG_LOG, "metaproxy received SIGUSR1");
768  m_p->m_stop_signo = 0;
769  if (m_p->az)
770  {
771  size_t i;
772  for (i = 0; i < m_p->m_ports.size(); i++)
773  m_p->az[i]->server("");
774  yaz_daemon_stop();
775  }
776  }
777 #endif
778  int no = m_p->mySocketManager.getNumberOfObservers();
779  if (no <= 1)
780  break;
781  if (tt && tt->timeout())
782  break;
783  }
784  delete tp;
785  delete tt;
786 }
787 
788 void yf::FrontendNet::configure(const xmlNode * ptr, bool test_only,
789  const char *path)
790 {
791  if (!ptr || !ptr->children)
792  {
793  throw yf::FilterException("No ports for Frontend");
794  }
795  std::vector<Port> ports;
796  for (ptr = ptr->children; ptr; ptr = ptr->next)
797  {
798  if (ptr->type != XML_ELEMENT_NODE)
799  continue;
800  if (!strcmp((const char *) ptr->name, "port"))
801  {
802  Port port;
803 
804  const char *names[5] = {"route", "max_recv_bytes", "port",
805  "cert_fname", 0};
806  std::string values[4];
807 
808  mp::xml::parse_attr(ptr, names, values);
809  port.route = values[0];
810  if (values[1].length() > 0)
811  port.max_recv_bytes = atoi(values[1].c_str());
812  else
813  port.max_recv_bytes = 0;
814  if (values[2].length() > 0)
815  port.port = values[2];
816  else
817  port.port = mp::xml::get_text(ptr);
818  port.cert_fname = values[3];
819  ports.push_back(port);
820  }
821  else if (!strcmp((const char *) ptr->name, "threads"))
822  {
823  std::string threads_str = mp::xml::get_text(ptr);
824  int threads = atoi(threads_str.c_str());
825  if (threads < 1)
826  throw yf::FilterException("Bad value for threads: "
827  + threads_str);
828  m_p->m_no_threads = threads;
829  }
830  else if (!strcmp((const char *) ptr->name, "max-threads"))
831  {
832  std::string threads_str = mp::xml::get_text(ptr);
833  int threads = atoi(threads_str.c_str());
834  if (threads < 1)
835  throw yf::FilterException("Bad value for max-threads: "
836  + threads_str);
837  m_p->m_max_threads = threads;
838  }
839  else if (!strcmp((const char *) ptr->name, "stack-size"))
840  {
841  std::string sz_str = mp::xml::get_text(ptr);
842  int sz = atoi(sz_str.c_str());
843  if (sz < 0)
844  throw yf::FilterException("Bad value for stack-size: "
845  + sz_str);
846  m_p->m_stack_size = sz * 1024;
847  }
848  else if (!strcmp((const char *) ptr->name, "timeout"))
849  {
850  const char *names[3] = {"ip", "verbose", 0};
851  std::string values[2];
852 
853  mp::xml::parse_attr(ptr, names, values);
854  IP_Pattern m;
855  m.value = mp::xml::get_int(ptr, 0);
856  m.pattern = values[0];
857  m.verbose = values[1].length() ? atoi(values[1].c_str()) : 1;
858  m_p->session_timeout.push_back(m);
859  }
860  else if (!strcmp((const char *) ptr->name, "connect-max"))
861  {
862  const char *names[3] = {"ip", "verbose", 0};
863  std::string values[2];
864 
865  mp::xml::parse_attr(ptr, names, values);
866  IP_Pattern m;
867  m.value = mp::xml::get_int(ptr, INT_MAX);
868  m.pattern = values[0];
869  m.verbose = values[1].length() ? atoi(values[1].c_str()) : 1;
870  m_p->connect_max.push_back(m);
871  }
872  else if (!strcmp((const char *) ptr->name, "connect-total"))
873  {
874  const char *names[3] = {"ip", "verbose", 0};
875  std::string values[2];
876 
877  mp::xml::parse_attr(ptr, names, values);
878  IP_Pattern m;
879  m.value = mp::xml::get_int(ptr, INT_MAX);
880  m.pattern = values[0];
881  m.verbose = values[1].length() ? atoi(values[1].c_str()) : 1;
882  m_p->connect_total.push_back(m);
883  }
884  else if (!strcmp((const char *) ptr->name, "http-req-max"))
885  {
886  const char *names[3] = {"ip", "verbose", 0};
887  std::string values[2];
888 
889  mp::xml::parse_attr(ptr, names, values);
890  IP_Pattern m;
891  m.value = mp::xml::get_int(ptr, INT_MAX);
892  m.pattern = values[0];
893  m.verbose = values[1].length() ? atoi(values[1].c_str()) : 1;
894  m_p->http_req_max.push_back(m);
895  }
896  else if (!strcmp((const char *) ptr->name, "message"))
897  {
898  m_p->m_msg_config = mp::xml::get_text(ptr);
899  }
900  else if (!strcmp((const char *) ptr->name, "stat-req"))
901  {
902  m_p->m_stat_req = mp::xml::get_text(ptr);
903  }
904  else
905  {
906  throw yf::FilterException("Bad element "
907  + std::string((const char *)
908  ptr->name));
909  }
910  }
911  if (m_p->m_msg_config.length() > 0 && m_p->m_stat_req.length() == 0)
912  { // allow stats if message is enabled for filter
913  m_p->m_stat_req = "/fn_stat";
914  }
915  if (test_only)
916  return;
917  set_ports(ports);
918 }
919 
920 void yf::FrontendNet::set_ports(std::vector<std::string> &ports)
921 {
922  std::vector<Port> nports;
923  size_t i;
924 
925  for (i = 0; i < ports.size(); i++)
926  {
927  Port nport;
928 
929  nport.port = ports[i];
930 
931  nports.push_back(nport);
932  }
933  set_ports(nports);
934 }
935 
936 
937 void yf::FrontendNet::set_ports(std::vector<Port> &ports)
938 {
939  m_p->m_ports = ports;
940 
941  m_p->az = new yf::FrontendNet::ZAssocServer *[m_p->m_ports.size()];
942  m_p->pdu = new yazpp_1::PDU_Assoc *[m_p->m_ports.size()];
943 
944  // Create yf::FrontendNet::ZAssocServer for each port
945  size_t i;
946  for (i = 0; i < m_p->m_ports.size(); i++)
947  m_p->az[i] = 0;
948  for (i = 0; i < m_p->m_ports.size(); i++)
949  {
950  // create a PDU assoc object (one per yf::FrontendNet::ZAssocServer)
951  yazpp_1::PDU_Assoc *as = new yazpp_1::PDU_Assoc(&m_p->mySocketManager);
952 
953  if (m_p->m_ports[i].cert_fname.length())
954  as->set_cert_fname(m_p->m_ports[i].cert_fname.c_str());
955  // create ZAssoc with PDU Assoc
956  m_p->pdu[i] = as;
957  m_p->az[i] = new yf::FrontendNet::ZAssocServer(
958  as, &m_p->m_ports[i], m_p.get());
959  if (m_p->az[i]->server(m_p->m_ports[i].port.c_str()))
960  {
961  throw yf::FilterException("Unable to bind to address "
962  + std::string(m_p->m_ports[i].port));
963  }
964  COMSTACK cs = as->get_comstack();
965 
966  if (cs && m_p->m_ports[i].max_recv_bytes)
967  cs_set_max_recv_bytes(cs, m_p->m_ports[i].max_recv_bytes);
968 
969  }
970 }
971 
972 void yf::FrontendNet::set_listen_duration(int d)
973 {
974  m_p->m_listen_duration = d;
975 }
976 
977 
978 yf::FrontendNet::PeerStat::PeerStat()
979 {
980  items = 0;
981 }
982 
983 yf::FrontendNet::PeerStat::~PeerStat()
984 {
985  while (items)
986  {
987  Item *n = items->next;
988  delete items;
989  items = n;
990  }
991 }
992 
993 
994 size_t yf::FrontendNet::PeerStat::add(const std::string &peer)
995 {
996  Item *n = items;
997  for (; n; n = n->next)
998  {
999  if (peer == n->m_peer)
1000  {
1001  n->cnt++;
1002  return n->cnt;
1003  }
1004  }
1005  n = new Item();
1006  n->cnt = 1;
1007  n->m_peer = peer;
1008  n->next = items;
1009  items = n;
1010  return n->cnt;
1011 }
1012 
1013 size_t yf::FrontendNet::PeerStat::get(const std::string &peer)
1014 {
1015  Item *n = items;
1016  for (; n; n = n->next)
1017  {
1018  if (peer == n->m_peer)
1019  return n->cnt;
1020  }
1021  return 0;
1022 }
1023 
1024 size_t yf::FrontendNet::PeerStat::remove(const std::string &peer)
1025 {
1026  Item **np = &items;
1027  for (; *np; np = &(*np)->next)
1028  {
1029  Item *n = *np;
1030  if (peer == n->m_peer)
1031  {
1032  if (--n->cnt == 0)
1033  {
1034  *np = n->next;
1035  delete n;
1036  return 0;
1037  }
1038  return n->cnt;
1039  }
1040  }
1041  return 0;
1042 }
1043 
1044 static yf::Base* filter_creator()
1045 {
1046  return new yf::FrontendNet;
1047 }
1048 
1049 extern "C" {
1050  struct metaproxy_1_filter_struct metaproxy_1_filter_frontend_net = {
1051  0,
1052  "frontend_net",
1054  };
1055 }
1056 
1057 /*
1058  * Local variables:
1059  * c-basic-offset: 4
1060  * c-file-style: "Stroustrup"
1061  * indent-tabs-mode: nil
1062  * End:
1063  * vim: shiftwidth=4 tabstop=8 expandtab
1064  */
1065 
int & read_fd() const
Definition: pipe.cpp:214
My_Timer_Thread(yazpp_1::ISocketObservable *obs, int duration)
ThreadPoolPackage(mp::Package *package, yf::FrontendNet::ZAssocChild *ses, Rep *rep)
yazpp_1::IPDU_Observer * sessionNotify(yazpp_1::IPDU_Observable *the_PDU_Observable, int fd)
ZAssocChild(yazpp_1::IPDU_Observable *the_PDU_Observable, mp::ThreadPoolSocketObserver *m_thread_pool_observer, const mp::Package *package, Port *port, Rep *rep, yazpp_1::LimitConnect &limit, const char *peername)
mp::ThreadPoolSocketObserver * m_thread_pool_observer
void set_thread_pool(ThreadPoolSocketObserver *observer)
ZAssocServer(yazpp_1::IPDU_Observable *PDU_Observable, FrontendNet::Port *port, Rep *rep)
mp::ThreadPoolSocketObserver * m_thread_pool_observer
yazpp_1::IPDU_Observer * sessionNotify(yazpp_1::IPDU_Observable *the_PDU_Observable, int fd)
void set_ports(std::vector< Port > &ports)
set ports
static yf::Base * filter_creator()
struct metaproxy_1_filter_struct metaproxy_1_filter_frontend_net