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