qpid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From astitc...@apache.org
Subject svn commit: r1128068 - in /qpid/trunk/qpid/cpp/src/qpid/sys: SocketAddress.h posix/Socket.cpp posix/SocketAddress.cpp windows/SocketAddress.cpp
Date Thu, 26 May 2011 20:38:52 GMT
Author: astitcher
Date: Thu May 26 20:38:52 2011
New Revision: 1128068

URL: http://svn.apache.org/viewvc?rev=1128068&view=rev
Log:
QPID-3282: Potential bug in circular connection detection:
- make sure that we compare numeric addresses of both local and remote ends
  of the connection (previously the remote end could be an unresolved hostname)

Modified:
    qpid/trunk/qpid/cpp/src/qpid/sys/SocketAddress.h
    qpid/trunk/qpid/cpp/src/qpid/sys/posix/Socket.cpp
    qpid/trunk/qpid/cpp/src/qpid/sys/posix/SocketAddress.cpp
    qpid/trunk/qpid/cpp/src/qpid/sys/windows/SocketAddress.cpp

Modified: qpid/trunk/qpid/cpp/src/qpid/sys/SocketAddress.h
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/src/qpid/sys/SocketAddress.h?rev=1128068&r1=1128067&r2=1128068&view=diff
==============================================================================
--- qpid/trunk/qpid/cpp/src/qpid/sys/SocketAddress.h (original)
+++ qpid/trunk/qpid/cpp/src/qpid/sys/SocketAddress.h Thu May 26 20:38:52 2011
@@ -41,7 +41,7 @@ public:
     QPID_COMMON_EXTERN SocketAddress& operator=(const SocketAddress&);
     QPID_COMMON_EXTERN ~SocketAddress();
 
-    std::string asString() const;
+    std::string asString(bool numeric=true) const;
 
 private:
     std::string host;

Modified: qpid/trunk/qpid/cpp/src/qpid/sys/posix/Socket.cpp
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/src/qpid/sys/posix/Socket.cpp?rev=1128068&r1=1128067&r2=1128068&view=diff
==============================================================================
--- qpid/trunk/qpid/cpp/src/qpid/sys/posix/Socket.cpp (original)
+++ qpid/trunk/qpid/cpp/src/qpid/sys/posix/Socket.cpp Thu May 26 20:38:52 2011
@@ -122,7 +122,14 @@ void Socket::connect(const std::string& 
 
 void Socket::connect(const SocketAddress& addr) const
 {
-    peername = addr.asString();
+    // The display name for an outbound connection needs to be the name that was specified
+    // for the address rather than a resolved IP address as we don't know which of
+    // the IP addresses is actually the one that will be connected to.
+    peername = addr.asString(false);
+
+    // However the string we compare with the local port must be numeric or it might not
+    // match when it should as getLocalAddress() will always be numeric
+    std::string connectname = addr.asString();
 
     createSocket(addr);
 
@@ -145,7 +152,7 @@ void Socket::connect(const SocketAddress
     // Raise an error if we see such a connection, since we know there is
     // no listener on the peer address.
     //
-    if (getLocalAddress() == getPeerAddress()) {
+    if (getLocalAddress() == connectname) {
         close();
         throw Exception(QPID_MSG("Connection refused: " << peername));
     }

Modified: qpid/trunk/qpid/cpp/src/qpid/sys/posix/SocketAddress.cpp
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/src/qpid/sys/posix/SocketAddress.cpp?rev=1128068&r1=1128067&r2=1128068&view=diff
==============================================================================
--- qpid/trunk/qpid/cpp/src/qpid/sys/posix/SocketAddress.cpp (original)
+++ qpid/trunk/qpid/cpp/src/qpid/sys/posix/SocketAddress.cpp Thu May 26 20:38:52 2011
@@ -61,9 +61,23 @@ SocketAddress::~SocketAddress()
     }
 }
 
-std::string SocketAddress::asString() const
+std::string SocketAddress::asString(bool numeric) const
 {
-    return host + ":" + port;
+    if (!numeric)
+        return host + ":" + port;
+    // Canonicalise into numeric id
+    const ::addrinfo& ai = getAddrInfo(*this);
+    char servName[NI_MAXSERV];
+    char dispName[NI_MAXHOST];
+    if (int rc=::getnameinfo(ai.ai_addr, ai.ai_addrlen,
+                            dispName, sizeof(dispName),
+                            servName, sizeof(servName),
+                            NI_NUMERICHOST | NI_NUMERICSERV) != 0)
+        throw QPID_POSIX_ERROR(rc);
+    std::string s(dispName);
+    s += ":";
+    s += servName;
+    return s;
 }
 
 const ::addrinfo& getAddrInfo(const SocketAddress& sa)
@@ -84,7 +98,7 @@ const ::addrinfo& getAddrInfo(const Sock
 
         int n = ::getaddrinfo(node, service, &hints, &sa.addrInfo);
         if (n != 0)
-            throw Exception(QPID_MSG("Cannot resolve " << sa.asString() << ":
" << ::gai_strerror(n)));
+            throw Exception(QPID_MSG("Cannot resolve " << sa.asString(false) <<
": " << ::gai_strerror(n)));
     }
 
     return *sa.addrInfo;

Modified: qpid/trunk/qpid/cpp/src/qpid/sys/windows/SocketAddress.cpp
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/src/qpid/sys/windows/SocketAddress.cpp?rev=1128068&r1=1128067&r2=1128068&view=diff
==============================================================================
--- qpid/trunk/qpid/cpp/src/qpid/sys/windows/SocketAddress.cpp (original)
+++ qpid/trunk/qpid/cpp/src/qpid/sys/windows/SocketAddress.cpp Thu May 26 20:38:52 2011
@@ -63,7 +63,7 @@ SocketAddress::~SocketAddress()
     ::freeaddrinfo(addrInfo);
 }
 
-std::string SocketAddress::asString() const
+std::string SocketAddress::asString(bool) const
 {
     return host + ":" + port;
 }



---------------------------------------------------------------------
Apache Qpid - AMQP Messaging Implementation
Project:      http://qpid.apache.org
Use/Interact: mailto:commits-subscribe@qpid.apache.org


Mime
View raw message