From 73ba5999186da82b444472170f7e0ce312ce536b Mon Sep 17 00:00:00 2001 From: Charles-Henri Bruyand Date: Thu, 12 Apr 2018 10:20:59 +0200 Subject: [PATCH] Auth: replace Socket::connect() implementation with SConnectWithTimeout --- pdns/Makefile.am | 2 ++ pdns/iputils.cc | 16 ++++++++++------ pdns/iputils.hh | 8 ++++++++ pdns/sstuff.hh | 27 +-------------------------- 4 files changed, 21 insertions(+), 32 deletions(-) diff --git a/pdns/Makefile.am b/pdns/Makefile.am index 99f520779..81c575a40 100644 --- a/pdns/Makefile.am +++ b/pdns/Makefile.am @@ -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 \ diff --git a/pdns/iputils.cc b/pdns/iputils.cc index b2d512182..5379bc04e 100644 --- a/pdns/iputils.cc +++ b/pdns/iputils.cc @@ -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))); } } diff --git a/pdns/iputils.hh b/pdns/iputils.hh index b80aa800f..910b40c4c 100644 --- a/pdns/iputils.hh +++ b/pdns/iputils.hh @@ -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); diff --git a/pdns/sstuff.hh b/pdns/sstuff.hh index 0eec38487..403611eff 100644 --- a/pdns/sstuff.hh +++ b/pdns/sstuff.hh @@ -42,16 +42,6 @@ #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); } -- 2.40.0