]> granicus.if.org Git - pdns/commitdiff
ixfrdist: use our socket convience functions
authorPieter Lexis <pieter.lexis@powerdns.com>
Fri, 19 Jan 2018 16:32:43 +0000 (17:32 +0100)
committerPieter Lexis <pieter.lexis@powerdns.com>
Mon, 29 Jan 2018 08:20:15 +0000 (09:20 +0100)
pdns/Makefile.am
pdns/ixfrdist.cc

index 3022fae78ea33e8fa641889633c165cd7e629863..738d818736d5e0cd4aba2908d1f75185da002fdd 100644 (file)
@@ -601,6 +601,7 @@ ixfrdist_SOURCES = \
        dnssecinfra.cc \
        dnswriter.cc dnswriter.hh \
        gss_context.cc gss_context.hh \
+       iputils.hh iputils.cc \
        ixfr.cc ixfr.hh \
        ixfrdist.cc \
        ixfrutils.cc ixfrutils.hh \
index 2089309f3c5c43507b63101a83411262151a9477..53bc1ea1b8bdac458f3c9d475bf180496ed11ddf 100644 (file)
@@ -468,17 +468,16 @@ void handleUDPRequest(int fd, boost::any&) {
 
 void handleTCPRequest(int fd, boost::any&) {
   ComboAddress saddr;
-  socklen_t socklen = sizeof(saddr);
+  int cfd = 0;
 
-  int cfd = accept(fd, (sockaddr*) &saddr, &socklen);
-
-  if (cfd == -1) {
-    cerr<<"Accepting connection from "<<saddr.toStringWithPort()<<" failed: "<<strerror(errno)<<endl;
+  try {
+    cfd = SAccept(fd, saddr);
+    setBlocking(cfd);
+  } catch(runtime_error &e) {
+    cerr<<"[ERROR] "<<e.what()<<endl;
     return;
   }
 
-  setBlocking(cfd);
-
   if (saddr == ComboAddress("0.0.0.0", 0)) {
     cerr<<"[WARNING] Could not determine source of message"<<endl;
     return;
@@ -666,52 +665,24 @@ int main(int argc, char** argv) {
   }
 
   set<int> allSockets;
-  for (const auto addr : listen_addresses) {
-    // Create UDP socket
-    int s = socket(addr.sin4.sin_family, SOCK_DGRAM, 0);
-    if (s < 0) {
-      cerr<<"[ERROR] Unable to create socket: "<<strerror(errno)<<endl;
-      had_error = true;
-      continue;
-    }
-
-    setNonBlocking(s);
-    setReuseAddr(s);
-
-    if (bind(s, (sockaddr*) &addr, addr.getSocklen()) < 0) {
-      cerr<<"[ERROR] Unable to bind to "<<addr.toStringWithPort()<<": "<<strerror(errno)<<endl;
-      had_error = true;
-      continue;
-    }
-
-    g_fdm.addReadFD(s, handleUDPRequest);
-
-    // Create TCP socket
-    int t = socket(addr.sin4.sin_family, SOCK_STREAM, 0);
-
-    if (t < 0) {
-      cerr<<"[ERROR] Unable to create socket: "<<strerror(errno)<<endl;
-      had_error = true;
-      continue;
-    }
-
-    setNonBlocking(t);
-    setReuseAddr(t);
-
-    if (bind(t, (sockaddr*) &addr, addr.getSocklen()) < 0) {
-      cerr<<"[ERROR] Unable to bind to "<<addr.toStringWithPort()<<": "<<strerror(errno)<<endl;
-      had_error = true;
-    }
-
-    // TODO Make backlog configurable?
-    if (listen(t, 30) < 0) {
-      cerr<<"[ERROR] Unable to listen on "<<addr.toStringWithPort()<<": "<<strerror(errno)<<endl;
-      had_error = true;
-      continue;
+  for (const auto& addr : listen_addresses) {
+    for (const auto& stype : {SOCK_DGRAM, SOCK_STREAM}) {
+      try {
+        int s = SSocket(addr.sin4.sin_family, stype, 0);
+        setNonBlocking(s);
+        setReuseAddr(s);
+        SBind(s, addr);
+        if (stype == SOCK_STREAM) {
+          SListen(s, 30); // TODO make this configurable
+        }
+        g_fdm.addReadFD(s, stype == SOCK_DGRAM ? handleUDPRequest : handleTCPRequest);
+        allSockets.insert(s);
+      } catch(runtime_error &e) {
+        cerr<<"[ERROR] "<<e.what()<<endl;
+        had_error = true;
+        continue;
+      }
     }
-
-    g_fdm.addReadFD(t, handleTCPRequest);
-    allSockets.insert(t);
   }
 
   g_workdir = g_vm["work-dir"].as<string>();