3fe53672dfada8c4ae36e54fcddd5a1ab3306973
[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     m_http_req_max = 0;
538     az = 0;
539     size_t i;
540     for (i = 0; i < 22; i++)
541         m_duration_freq[i] = 0;
542     m_duration_lim[0] = 0.000001;
543     m_duration_lim[1] = 0.00001;
544     m_duration_lim[2] = 0.0001;
545     m_duration_lim[3] = 0.001;
546     m_duration_lim[4] = 0.01;
547     m_duration_lim[5] = 0.1;
548     m_duration_lim[6] = 0.2;
549     m_duration_lim[7] = 0.3;
550     m_duration_lim[8] = 0.5;
551     m_duration_lim[9] = 1.0;
552     m_duration_lim[10] = 1.5;
553     m_duration_lim[11] = 2.0;
554     m_duration_lim[12] = 3.0;
555     m_duration_lim[13] = 4.0;
556     m_duration_lim[14] = 5.0;
557     m_duration_lim[15] = 6.0;
558     m_duration_lim[16] = 8.0;
559     m_duration_lim[17] = 10.0;
560     m_duration_lim[18] = 15.0;
561     m_duration_lim[19] = 20.0;
562     m_duration_lim[20] = 30.0;
563     m_duration_lim[21] = 0.0;
564     m_duration_max = 0.0;
565     m_duration_min = 0.0;
566     m_duration_total = 0.0;
567     m_stop_signo = 0;
568 }
569
570 yf::FrontendNet::Rep::~Rep()
571 {
572     if (az)
573     {
574         size_t i;
575         for (i = 0; i < m_ports.size(); i++)
576             delete az[i];
577         delete [] az;
578         delete [] pdu;
579     }
580     az = 0;
581 }
582
583 yf::FrontendNet::~FrontendNet()
584 {
585 }
586
587 void yf::FrontendNet::stop(int signo) const
588 {
589     m_p->m_stop_signo = signo;
590 }
591
592 void yf::FrontendNet::start() const
593 {
594 #if HAVE_GETRLIMIT
595     struct rlimit limit_data;
596     getrlimit(RLIMIT_NOFILE, &limit_data);
597     yaz_log(YLOG_LOG, "getrlimit NOFILE cur=%ld max=%ld",
598             (long) limit_data.rlim_cur, (long) limit_data.rlim_max);
599 #endif
600 }
601
602 bool yf::FrontendNet::My_Timer_Thread::timeout()
603 {
604     return m_timeout;
605 }
606
607 yf::FrontendNet::My_Timer_Thread::My_Timer_Thread(
608     yazpp_1::ISocketObservable *obs,
609     int duration) :
610     m_obs(obs), m_pipe(9123), m_timeout(false)
611 {
612     obs->addObserver(m_pipe.read_fd(), this);
613     obs->maskObserver(this, yazpp_1::SOCKET_OBSERVE_READ);
614     obs->timeoutObserver(this, duration);
615 }
616
617 void yf::FrontendNet::My_Timer_Thread::socketNotify(int event)
618 {
619     m_timeout = true;
620     m_obs->deleteObserver(this);
621 }
622
623 void yf::FrontendNet::process(mp::Package &package) const
624 {
625     if (m_p->az == 0)
626         return;
627     size_t i;
628     My_Timer_Thread *tt = 0;
629
630     if (m_p->m_listen_duration)
631         tt = new My_Timer_Thread(&m_p->mySocketManager,
632                                  m_p->m_listen_duration);
633
634     ThreadPoolSocketObserver tp(&m_p->mySocketManager, m_p->m_no_threads,
635                                 m_p->m_max_threads,
636                                 m_p->m_stack_size);
637
638     for (i = 0; i<m_p->m_ports.size(); i++)
639     {
640         m_p->az[i]->set_package(&package);
641         m_p->az[i]->set_thread_pool(&tp);
642     }
643     while (m_p->mySocketManager.processEvent() > 0)
644     {
645         if (m_p->m_stop_signo == SIGTERM)
646         {
647             yaz_log(YLOG_LOG, "metaproxy received SIGTERM");
648             if (m_p->az)
649             {
650                 size_t i;
651                 for (i = 0; i < m_p->m_ports.size(); i++)
652                 {
653                     m_p->pdu[i]->shutdown();
654                     m_p->az[i]->server("");
655                 }
656                 yaz_daemon_stop();
657             }
658             break; /* stop right away */
659         }
660 #ifndef WIN32
661         if (m_p->m_stop_signo == SIGUSR1)
662         {    /* just stop listeners and cont till all sessions are done*/
663             yaz_log(YLOG_LOG, "metaproxy received SIGUSR1");
664             m_p->m_stop_signo = 0;
665             if (m_p->az)
666             {
667                 size_t i;
668                 for (i = 0; i < m_p->m_ports.size(); i++)
669                     m_p->az[i]->server("");
670                 yaz_daemon_stop();
671             }
672         }
673 #endif
674         int no = m_p->mySocketManager.getNumberOfObservers();
675         if (no <= 1)
676             break;
677         if (tt && tt->timeout())
678             break;
679     }
680     delete tt;
681 }
682
683 void yf::FrontendNet::configure(const xmlNode * ptr, bool test_only,
684                                 const char *path)
685 {
686     if (!ptr || !ptr->children)
687     {
688         throw yf::FilterException("No ports for Frontend");
689     }
690     std::vector<Port> ports;
691     for (ptr = ptr->children; ptr; ptr = ptr->next)
692     {
693         if (ptr->type != XML_ELEMENT_NODE)
694             continue;
695         if (!strcmp((const char *) ptr->name, "port"))
696         {
697             Port port;
698
699             const char *names[5] = {"route", "max_recv_bytes", "port",
700                                     "cert_fname", 0};
701             std::string values[4];
702
703             mp::xml::parse_attr(ptr, names, values);
704             port.route = values[0];
705             if (values[1].length() > 0)
706                 port.max_recv_bytes = atoi(values[1].c_str());
707             else
708                 port.max_recv_bytes = 0;
709             if (values[2].length() > 0)
710                 port.port = values[2];
711             else
712                 port.port = mp::xml::get_text(ptr);
713             port.cert_fname = values[3];
714             ports.push_back(port);
715         }
716         else if (!strcmp((const char *) ptr->name, "threads"))
717         {
718             std::string threads_str = mp::xml::get_text(ptr);
719             int threads = atoi(threads_str.c_str());
720             if (threads < 1)
721                 throw yf::FilterException("Bad value for threads: "
722                                                    + threads_str);
723             m_p->m_no_threads = threads;
724         }
725         else if (!strcmp((const char *) ptr->name, "max-threads"))
726         {
727             std::string threads_str = mp::xml::get_text(ptr);
728             int threads = atoi(threads_str.c_str());
729             if (threads < 1)
730                 throw yf::FilterException("Bad value for max-threads: "
731                                                    + threads_str);
732             m_p->m_max_threads = threads;
733         }
734         else if (!strcmp((const char *) ptr->name, "stack-size"))
735         {
736             std::string sz_str = mp::xml::get_text(ptr);
737             int sz = atoi(sz_str.c_str());
738             if (sz < 0)
739                 throw yf::FilterException("Bad value for stack-size: "
740                                                    + sz_str);
741             m_p->m_stack_size = sz * 1024;
742         }
743         else if (!strcmp((const char *) ptr->name, "timeout"))
744         {
745             std::string timeout_str = mp::xml::get_text(ptr);
746             int timeout = atoi(timeout_str.c_str());
747             if (timeout < 1)
748                 throw yf::FilterException("Bad value for timeout: "
749                                                    + timeout_str);
750             m_p->m_session_timeout = timeout;
751         }
752         else if (!strcmp((const char *) ptr->name, "connect-max"))
753         {
754             m_p->m_connect_max = mp::xml::get_int(ptr, 0);
755         }
756         else if (!strcmp((const char *) ptr->name, "http-req-max"))
757         {
758             m_p->m_http_req_max = mp::xml::get_int(ptr, 0);
759         }
760         else if (!strcmp((const char *) ptr->name, "message"))
761         {
762             m_p->m_msg_config = mp::xml::get_text(ptr);
763         }
764         else if (!strcmp((const char *) ptr->name, "stat-req"))
765         {
766             m_p->m_stat_req = mp::xml::get_text(ptr);
767         }
768         else
769         {
770             throw yf::FilterException("Bad element "
771                                       + std::string((const char *)
772                                                     ptr->name));
773         }
774     }
775     if (test_only)
776         return;
777     set_ports(ports);
778 }
779
780 void yf::FrontendNet::set_ports(std::vector<std::string> &ports)
781 {
782     std::vector<Port> nports;
783     size_t i;
784
785     for (i = 0; i < ports.size(); i++)
786     {
787         Port nport;
788
789         nport.port = ports[i];
790
791         nports.push_back(nport);
792     }
793     set_ports(nports);
794 }
795
796
797 void yf::FrontendNet::set_ports(std::vector<Port> &ports)
798 {
799     m_p->m_ports = ports;
800
801     m_p->az = new yf::FrontendNet::ZAssocServer *[m_p->m_ports.size()];
802     m_p->pdu = new yazpp_1::PDU_Assoc *[m_p->m_ports.size()];
803
804     // Create yf::FrontendNet::ZAssocServer for each port
805     size_t i;
806     for (i = 0; i < m_p->m_ports.size(); i++)
807         m_p->az[i] = 0;
808     for (i = 0; i < m_p->m_ports.size(); i++)
809     {
810         // create a PDU assoc object (one per yf::FrontendNet::ZAssocServer)
811         yazpp_1::PDU_Assoc *as = new yazpp_1::PDU_Assoc(&m_p->mySocketManager);
812
813         if (m_p->m_ports[i].cert_fname.length())
814             as->set_cert_fname(m_p->m_ports[i].cert_fname.c_str());
815         // create ZAssoc with PDU Assoc
816         m_p->pdu[i] = as;
817         m_p->az[i] = new yf::FrontendNet::ZAssocServer(
818             as, &m_p->m_ports[i], m_p.get());
819         if (m_p->az[i]->server(m_p->m_ports[i].port.c_str()))
820         {
821             throw yf::FilterException("Unable to bind to address "
822                                       + std::string(m_p->m_ports[i].port));
823         }
824         COMSTACK cs = as->get_comstack();
825
826         if (cs && m_p->m_ports[i].max_recv_bytes)
827             cs_set_max_recv_bytes(cs, m_p->m_ports[i].max_recv_bytes);
828
829     }
830 }
831
832 void yf::FrontendNet::set_listen_duration(int d)
833 {
834     m_p->m_listen_duration = d;
835 }
836
837 static yf::Base* filter_creator()
838 {
839     return new yf::FrontendNet;
840 }
841
842 extern "C" {
843     struct metaproxy_1_filter_struct metaproxy_1_filter_frontend_net = {
844         0,
845         "frontend_net",
846         filter_creator
847     };
848 }
849
850 /*
851  * Local variables:
852  * c-basic-offset: 4
853  * c-file-style: "Stroustrup"
854  * indent-tabs-mode: nil
855  * End:
856  * vim: shiftwidth=4 tabstop=8 expandtab
857  */
858