#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;
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];
}
-void handleInsideUDPPacket(int fd, boost::any&)
+static void handleInsideUDPPacket(int fd, boost::any&)
try
{
char buffer[1500];
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(); ) {
}
}
-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;
}
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")
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());
}
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)
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();
}
{
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 */
-}