]> git.ipfire.org Git - thirdparty/pdns.git/blobdiff - pdns/nproxy.cc
Merge pull request #14200 from rgacogne/auth-enable-leak-detection-unit-tests
[thirdparty/pdns.git] / pdns / nproxy.cc
index d6999e4bc012bf5704d9212a9be9336190041ced..167b15a405b135a53cf1565bb00d0b3a896cba3b 100644 (file)
@@ -25,7 +25,6 @@
 #include <bitset>
 #include "dnsparser.hh"
 #include "iputils.hh"
-#undef L
 #include <boost/program_options.hpp>
 
 #include <boost/format.hpp>
 
 #include "namespaces.hh"
 using namespace ::boost::multi_index;
-#include "namespaces.hh"
 
 namespace po = boost::program_options;
 po::variables_map g_vm;
 
 StatBag S;
 
-SelectFDMultiplexer g_fdm;
+FDMultiplexer* g_fdm;
 int g_pdnssocket;
 bool g_verbose;
 
@@ -68,13 +66,13 @@ struct NotificationInFlight
 typedef map<uint16_t, NotificationInFlight> nifs_t;
 nifs_t g_nifs;
 
-void syslogFmt(const boost::format& fmt)
+static void syslogFmt(const boost::format& fmt)
 {
   cerr<<"nproxy: "<<fmt<<endl;
   syslog(LOG_WARNING, "%s", str(fmt).c_str());
 }
 
-void handleOutsideUDPPacket(int fd, boost::any&)
+static void handleOutsideUDPPacket(int fd, boost::any&)
 try
 {
   char buffer[1500];
@@ -146,7 +144,7 @@ catch(std::exception &e)
 }
 
 
-void handleInsideUDPPacket(int fd, boost::any&)
+static void handleInsideUDPPacket(int fd, boost::any&)
 try
 {
   char buffer[1500];
@@ -192,7 +190,7 @@ catch(std::exception &e)
   syslogFmt(boost::format("Error parsing packet from internal nameserver: %s") % e.what());
 }
 
-void expireOldNotifications()
+static void expireOldNotifications()
 {
   time_t limit = time(0) - 10;
   for(nifs_t::iterator iter = g_nifs.begin(); iter != g_nifs.end(); ) {
@@ -205,9 +203,19 @@ void expireOldNotifications()
   }
 }
 
-void daemonize(int null_fd);
+static void daemonize(int null_fd)
+{
+  if(fork())
+    exit(0); // bye bye
+
+  setsid();
+
+  dup2(null_fd,0); /* stdin */
+  dup2(null_fd,1); /* stderr */
+  dup2(null_fd,2); /* stderr */
+}
 
-void usage(po::options_description &desc) {
+static void usage(po::options_description &desc) {
   cerr<<"nproxy"<<endl;
   cerr<<desc<<endl;
 }
@@ -218,6 +226,11 @@ try
   reportAllTypes();
   openlog("nproxy", LOG_NDELAY | LOG_PID, LOG_DAEMON);
 
+  g_fdm = FDMultiplexer::getMultiplexerSilent();
+  if(!g_fdm) {
+    throw std::runtime_error("Could not enable a multiplexer");
+  }
+  
   po::options_description desc("Allowed options");
   desc.add_options()
     ("help,h", "produce help message")
@@ -273,7 +286,7 @@ try
     if(::bind(sock,(sockaddr*) &local, local.getSocklen()) < 0)
       throw runtime_error("Binding socket for incoming packets to '"+ local.toStringWithPort()+"': "+stringerror());
 
-    g_fdm.addReadFD(sock, handleOutsideUDPPacket); // add to fdmultiplexer for each socket
+    g_fdm->addReadFD(sock, handleOutsideUDPPacket); // add to fdmultiplexer for each socket
     syslogFmt(boost::format("Listening for external notifications on address %s") % local.toStringWithPort());
   }
 
@@ -294,7 +307,7 @@ try
 
   syslogFmt(boost::format("Sending notifications from %s to internal address %s") % originAddress.toString() % pdns.toStringWithPort());
 
-  g_fdm.addReadFD(g_pdnssocket, handleInsideUDPPacket);
+  g_fdm->addReadFD(g_pdnssocket, handleInsideUDPPacket);
 
   int null_fd=open("/dev/null",O_RDWR); /* open stdin */
   if(null_fd < 0)
@@ -332,7 +345,7 @@ try
   struct timeval now;
   for(;;) {
     gettimeofday(&now, 0);
-    g_fdm.run(&now);
+    g_fdm->run(&now);
     // check for notifications that have been outstanding for more than 10 seconds
     expireOldNotifications();
   }
@@ -349,15 +362,3 @@ catch(PDNSException& e)
 {
   syslogFmt(boost::format("Fatal: %s") % e.reason);
 }
-
-void daemonize(int null_fd)
-{
-  if(fork())
-    exit(0); // bye bye
-
-  setsid();
-
-  dup2(null_fd,0); /* stdin */
-  dup2(null_fd,1); /* stderr */
-  dup2(null_fd,2); /* stderr */
-}