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