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