]> granicus.if.org Git - pdns/commitdiff
Auth: replace Socket::connect() implementation with SConnectWithTimeout
authorCharles-Henri Bruyand <charles-henri.bruyand@open-xchange.com>
Thu, 12 Apr 2018 08:20:59 +0000 (10:20 +0200)
committerCharles-Henri Bruyand <charles-henri.bruyand@open-xchange.com>
Fri, 13 Apr 2018 17:49:33 +0000 (19:49 +0200)
pdns/Makefile.am
pdns/iputils.cc
pdns/iputils.hh
pdns/sstuff.hh

index 99f5207799de3918544809f8606f4a1537cfd606..81c575a403b33c1e7831c19510f5c41120722a49 100644 (file)
@@ -578,6 +578,7 @@ saxfr_SOURCES = \
        dnssecinfra.cc \
        dnswriter.cc dnswriter.hh \
        gss_context.cc gss_context.hh \
+       iputils.cc \
        logger.cc \
        misc.cc misc.hh \
        nsecrecords.cc \
@@ -730,6 +731,7 @@ nsec3dig_SOURCES = \
        dnssecinfra.cc \
        dnswriter.cc dnswriter.hh \
        gss_context.cc gss_context.hh \
+       iputils.cc \
        logger.cc \
        misc.cc misc.hh \
        nsec3dig.cc \
index b2d512182a2837c168fe02a7c7061951295b9660..5379bc04e5430e9e019f3327c31a66c1d53a501c 100644 (file)
@@ -32,6 +32,10 @@ static void RuntimeError(const boost::format& fmt)
   throw runtime_error(fmt.str());
 }
 
+static void NetworkErr(const boost::format& fmt)
+{
+  throw NetworkError(fmt.str());
+}
 
 int SSocket(int family, int type, int flags)
 {
@@ -66,26 +70,26 @@ int SConnectWithTimeout(int sockfd, const ComboAddress& remote, int timeout)
           savederrno = 0;
           socklen_t errlen = sizeof(savederrno);
           if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (void *)&savederrno, &errlen) == 0) {
-            RuntimeError(boost::format("connecting to %s failed: %s") % remote.toStringWithPort() % string(strerror(savederrno)));
+            NetworkErr(boost::format("connecting to %s failed: %s") % remote.toStringWithPort() % string(strerror(savederrno)));
           }
           else {
-            RuntimeError(boost::format("connecting to %s failed") % remote.toStringWithPort());
+            NetworkErr(boost::format("connecting to %s failed") % remote.toStringWithPort());
           }
         }
         if (disconnected) {
-          RuntimeError(boost::format("%s closed the connection") % remote.toStringWithPort());
+          NetworkErr(boost::format("%s closed the connection") % remote.toStringWithPort());
         }
         return 0;
       }
       else if (res == 0) {
-        RuntimeError(boost::format("timeout while connecting to %s") % remote.toStringWithPort());
+        NetworkErr(boost::format("timeout while connecting to %s") % remote.toStringWithPort());
       } else if (res < 0) {
         savederrno = errno;
-        RuntimeError(boost::format("waiting to connect to %s: %s") % remote.toStringWithPort() % string(strerror(savederrno)));
+        NetworkErr(boost::format("waiting to connect to %s: %s") % remote.toStringWithPort() % string(strerror(savederrno)));
       }
     }
     else {
-      RuntimeError(boost::format("connecting to %s: %s") % remote.toStringWithPort() % string(strerror(savederrno)));
+      NetworkErr(boost::format("connecting to %s: %s") % remote.toStringWithPort() % string(strerror(savederrno)));
     }
   }
 
index b80aa800f344417d348c2ed39255d7e75c0d72ec..910b40c4cc94df0825721a191ad8843ea87c4596 100644 (file)
@@ -1007,6 +1007,14 @@ struct SComboAddress
   }
 };
 
+class NetworkError : public runtime_error
+{
+public:
+  NetworkError(const string& why="Network Error") : runtime_error(why.c_str())
+  {}
+  NetworkError(const char *why="Network Error") : runtime_error(why)
+  {}
+};
 
 int SSocket(int family, int type, int flags);
 int SConnect(int sockfd, const ComboAddress& remote);
index 0eec38487e91bd993b3497e84dfe43a635e24a55..403611eff9b18b9204f7d50fbcde32eeb4e56bae 100644 (file)
 #include "namespaces.hh"
 
 
-class NetworkError : public runtime_error
-{
-public:
-  NetworkError(const string& why="Network Error") : runtime_error(why.c_str())
-  {}
-  NetworkError(const char *why="Network Error") : runtime_error(why)
-  {}
-};
-
-
 typedef int ProtocolType; //!< Supported protocol types
 
 //! Representation of a Socket and many of the Berkeley functions available
@@ -167,22 +157,7 @@ public:
   //! Connect the socket to a specified endpoint
   void connect(const ComboAddress &ep, int timeout=0)
   {
-    if(::connect(d_socket,(struct sockaddr *)&ep, ep.getSocklen()) < 0) {
-      if(errno == EINPROGRESS) {
-        if (timeout > 0) {
-          /* if a timeout is provided, we wait until the connection has been established */
-          int res = waitForRWData(d_socket, false, timeout, 0);
-          if (res == 0) {
-            throw NetworkError("timeout while connecting to "+ep.toStringWithPort());
-          } else if (res < 0) {
-            throw NetworkError("while waiting to connect to "+ep.toStringWithPort()+": "+string(strerror(errno)));
-          }
-        }
-      }
-      else {
-        throw NetworkError("While connecting to "+ep.toStringWithPort()+": "+string(strerror(errno)));
-      }
-    }
+    SConnectWithTimeout(d_socket, ep, timeout);
   }