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