qpid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From astitc...@apache.org
Subject svn commit: r1158934 - in /qpid/trunk/qpid/cpp/src/qpid/sys: SocketAddress.h posix/Socket.cpp posix/SocketAddress.cpp windows/Socket.cpp windows/SocketAddress.cpp
Date Wed, 17 Aug 2011 22:07:37 GMT
Author: astitcher
Date: Wed Aug 17 22:07:36 2011
New Revision: 1158934

URL: http://svn.apache.org/viewvc?rev=1158934&view=rev
Log:
NO-JIRA: Tidy up the Socket/SocketAddress code:
- Move (almost) all knowledge of difference in address types to SocketAddress
- Make the Windows and Posix sockets code more similar with the aim of
  eliminating differences in the future

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/Socket.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=1158934&r1=1158933&r2=1158934&view=diff
==============================================================================
--- qpid/trunk/qpid/cpp/src/qpid/sys/SocketAddress.h (original)
+++ qpid/trunk/qpid/cpp/src/qpid/sys/SocketAddress.h Wed Aug 17 22:07:36 2011
@@ -27,6 +27,7 @@
 #include <string>
 
 struct addrinfo;
+struct sockaddr;
 
 namespace qpid {
 namespace sys {
@@ -45,6 +46,10 @@ public:
     QPID_COMMON_EXTERN std::string asString(bool numeric=true) const;
     QPID_COMMON_EXTERN void setAddrInfoPort(uint16_t port);
 
+    QPID_COMMON_EXTERN static std::string asString(::sockaddr const * const addr, size_t
addrlen);
+    QPID_COMMON_EXTERN static uint16_t getPort(::sockaddr const * const addr);
+    
+
 private:
     std::string host;
     std::string port;

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=1158934&r1=1158933&r2=1158934&view=diff
==============================================================================
--- qpid/trunk/qpid/cpp/src/qpid/sys/posix/Socket.cpp (original)
+++ qpid/trunk/qpid/cpp/src/qpid/sys/posix/Socket.cpp Wed Aug 17 22:07:36 2011
@@ -34,9 +34,6 @@
 #include <netdb.h>
 #include <cstdlib>
 #include <string.h>
-#include <iostream>
-
-#include <boost/format.hpp>
 
 namespace qpid {
 namespace sys {
@@ -44,38 +41,28 @@ namespace sys {
 namespace {
 std::string getName(int fd, bool local)
 {
-    ::sockaddr_storage name; // big enough for any socket address
-    ::socklen_t namelen = sizeof(name);
+    ::sockaddr_storage name_s; // big enough for any socket address
+    ::sockaddr* name = (::sockaddr*)&name_s;
+    ::socklen_t namelen = sizeof(name_s);
 
-    int result = -1;
     if (local) {
-        result = ::getsockname(fd, (::sockaddr*)&name, &namelen);
+        QPID_POSIX_CHECK( ::getsockname(fd, name, &namelen) );
     } else {
-        result = ::getpeername(fd, (::sockaddr*)&name, &namelen);
+        QPID_POSIX_CHECK( ::getpeername(fd, name, &namelen) );
     }
-    QPID_POSIX_CHECK(result);
 
-    char servName[NI_MAXSERV];
-    char dispName[NI_MAXHOST];
-    if (int rc=::getnameinfo((::sockaddr*)&name, namelen, dispName, sizeof(dispName),
-                                servName, sizeof(servName),
-                                NI_NUMERICHOST | NI_NUMERICSERV) != 0)
-        throw QPID_POSIX_ERROR(rc);
-    return std::string(dispName) + ":" + std::string(servName);
+    return SocketAddress::asString(name, namelen);
 }
 
 uint16_t getLocalPort(int fd)
 {
-    ::sockaddr_storage name;
-    ::socklen_t namelen = sizeof(name);
-    if (::getsockname(fd, (::sockaddr*)&name, &namelen) < 0)
-        throw QPID_POSIX_ERROR(errno);
+    ::sockaddr_storage name_s; // big enough for any socket address
+    ::sockaddr* name = (::sockaddr*)&name_s;
+    ::socklen_t namelen = sizeof(name_s);
 
-    switch (name.ss_family) {
-    case AF_INET: return ntohs(((::sockaddr_in&)name).sin_port);
-    case AF_INET6: return ntohs(((::sockaddr_in6&)name).sin6_port);
-    default:throw Exception(QPID_MSG("Unexpected socket type"));
-    }
+    QPID_POSIX_CHECK( ::getsockname(fd, name, &namelen) );
+
+    return SocketAddress::getPort(name);
 }
 }
 

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=1158934&r1=1158933&r2=1158934&view=diff
==============================================================================
--- qpid/trunk/qpid/cpp/src/qpid/sys/posix/SocketAddress.cpp (original)
+++ qpid/trunk/qpid/cpp/src/qpid/sys/posix/SocketAddress.cpp Wed Aug 17 22:07:36 2011
@@ -21,11 +21,13 @@
 
 #include "qpid/sys/SocketAddress.h"
 
-#include "qpid/sys/posix/check.h"
+#include "qpid/Exception.h"
+#include "qpid/Msg.h"
 
 #include <sys/socket.h>
-#include <string.h>
+#include <netinet/in.h>
 #include <netdb.h>
+#include <string.h>
 
 namespace qpid {
 namespace sys {
@@ -59,25 +61,45 @@ SocketAddress::~SocketAddress()
     }
 }
 
-std::string SocketAddress::asString(bool numeric) const
+std::string SocketAddress::asString(::sockaddr const * const addr, size_t addrlen)
 {
-    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)
+    if (int rc=::getnameinfo(addr, addrlen,
+        dispName, sizeof(dispName),
+                             servName, sizeof(servName),
+                             NI_NUMERICHOST | NI_NUMERICSERV) != 0)
         throw qpid::Exception(QPID_MSG(gai_strerror(rc)));
-    std::string s(dispName);
+    std::string s;
+    switch (addr->sa_family) {
+        case AF_INET: s += dispName; break;
+        case AF_INET6: s += "["; s += dispName; s+= "]"; break;
+        default: throw Exception(QPID_MSG("Unexpected socket type"));
+    }
     s += ":";
     s += servName;
     return s;
 }
 
+uint16_t SocketAddress::getPort(::sockaddr const * const addr)
+{
+    switch (addr->sa_family) {
+        case AF_INET: return ntohs(((::sockaddr_in*)addr)->sin_port);
+        case AF_INET6: return ntohs(((::sockaddr_in6*)addr)->sin6_port);
+        default:throw Exception(QPID_MSG("Unexpected socket type"));
+    }
+}
+
+std::string SocketAddress::asString(bool numeric) const
+{
+    if (!numeric)
+        return host + ":" + port;
+    // Canonicalise into numeric id
+    const ::addrinfo& ai = getAddrInfo(*this);
+
+    return asString(ai.ai_addr, ai.ai_addrlen);
+}
+
 bool SocketAddress::nextAddress() {
     bool r = currentAddrInfo->ai_next != 0;
     if (r)

Modified: qpid/trunk/qpid/cpp/src/qpid/sys/windows/Socket.cpp
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/src/qpid/sys/windows/Socket.cpp?rev=1158934&r1=1158933&r2=1158934&view=diff
==============================================================================
--- qpid/trunk/qpid/cpp/src/qpid/sys/windows/Socket.cpp (original)
+++ qpid/trunk/qpid/cpp/src/qpid/sys/windows/Socket.cpp Wed Aug 17 22:07:36 2011
@@ -19,25 +19,19 @@
  *
  */
 
-// Ensure we get all of winsock2.h
-#ifndef _WIN32_WINNT
-#define _WIN32_WINNT 0x0501
-#endif
-
 #include "qpid/sys/Socket.h"
+
 #include "qpid/sys/SocketAddress.h"
-#include "qpid/sys/windows/IoHandlePrivate.h"
 #include "qpid/sys/windows/check.h"
-#include "qpid/sys/Time.h"
+#include "qpid/sys/windows/IoHandlePrivate.h"
 
-#include <cstdlib>
-#include <string.h>
+// Ensure we get all of winsock2.h
+#ifndef _WIN32_WINNT
+#define _WIN32_WINNT 0x0501
+#endif
 
 #include <winsock2.h>
 
-#include <boost/format.hpp>
-#include <boost/lexical_cast.hpp>
-
 // Need to initialize WinSock. Ideally, this would be a singleton or embedded
 // in some one-time initialization function. I tried boost singleton and could
 // not get it to compile (and others located in google had the same problem).
@@ -91,35 +85,28 @@ namespace {
 
 std::string getName(SOCKET fd, bool local)
 {
-    sockaddr_storage name; // big enough for any socket address
-    socklen_t namelen = sizeof(name);
+    ::sockaddr_storage name_s; // big enough for any socket address
+    ::sockaddr* name = (::sockaddr*)&name_s;
+    ::socklen_t namelen = sizeof(name_s);
+
     if (local) {
-        QPID_WINSOCK_CHECK(::getsockname(fd, (sockaddr*)&name, &namelen));
+        QPID_WINSOCK_CHECK(::getsockname(fd, name, &namelen));
     } else {
-        QPID_WINSOCK_CHECK(::getpeername(fd, (sockaddr*)&name, &namelen));
+        QPID_WINSOCK_CHECK(::getpeername(fd, name, &namelen));
     }
 
-    char servName[NI_MAXSERV];
-    char dispName[NI_MAXHOST];
-    if (int rc = ::getnameinfo((sockaddr*)&name, namelen,
-                               dispName, sizeof(dispName), 
-                               servName, sizeof(servName), 
-                               NI_NUMERICHOST | NI_NUMERICSERV) != 0)
-        throw qpid::Exception(QPID_MSG(gai_strerror(rc)));
-    return std::string(dispName) + ":" + std::string(servName);
+    return SocketAddress::asString(name, namelen);
 }
 
 uint16_t getLocalPort(int fd)
 {
-    ::sockaddr_storage name;
-    ::socklen_t namelen = sizeof(name);
-    QPID_WINSOCK_CHECK(::getsockname(fd, (::sockaddr*)&name, &namelen));
-
-    switch (name.ss_family) {
-    case AF_INET: return ntohs(((::sockaddr_in&)name).sin_port);
-    case AF_INET6: return ntohs(((::sockaddr_in6&)name).sin6_port);
-    default:throw Exception(QPID_MSG("Unexpected socket type"));
-    }
+    ::sockaddr_storage name_s; // big enough for any socket address
+    ::sockaddr* name = (::sockaddr*)&name_s;
+    ::socklen_t namelen = sizeof(name_s);
+
+    QPID_WINSOCK_CHECK(::getsockname(fd, name, &namelen));
+
+    return SocketAddress::getPort(name);
 }
 }  // namespace
 
@@ -135,8 +122,7 @@ Socket::Socket(IOHandlePrivate* h) :
     nodelay(false)
 {}
 
-void
-Socket::createSocket(const SocketAddress& sa) const
+void Socket::createSocket(const SocketAddress& sa) const
 {
     SOCKET& socket = impl->fd;
     if (socket != INVALID_SOCKET) Socket::close();
@@ -152,11 +138,11 @@ Socket::createSocket(const SocketAddress
         if (nodelay) setTcpNoDelay();
         if (getAddrInfo(sa).ai_family == AF_INET6) {
             int flag = 1;
-            int result = setsockopt(socket, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&flag,
sizeof(flag));
+            int result = ::setsockopt(socket, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&flag,
sizeof(flag));
             QPID_WINSOCK_CHECK(result);
         }
     } catch (std::exception&) {
-        closesocket(s);
+        ::closesocket(s);
         socket = INVALID_SOCKET;
         throw;
     }
@@ -264,15 +250,17 @@ Socket* Socket::accept() const
 
 std::string Socket::getPeerAddress() const
 {
-    if (peername.empty())
+    if (peername.empty()) {
         peername = getName(impl->fd, false);
+    }
     return peername;
 }
 
 std::string Socket::getLocalAddress() const
 {
-    if (localname.empty())
+    if (localname.empty()) {
         localname = getName(impl->fd, true);
+    }
     return localname;
 }
 

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=1158934&r1=1158933&r2=1158934&view=diff
==============================================================================
--- qpid/trunk/qpid/cpp/src/qpid/sys/windows/SocketAddress.cpp (original)
+++ qpid/trunk/qpid/cpp/src/qpid/sys/windows/SocketAddress.cpp Wed Aug 17 22:07:36 2011
@@ -19,15 +19,16 @@
  *
  */
 
+#include "qpid/sys/SocketAddress.h"
+
+#include "qpid/Exception.h"
+#include "qpid/Msg.h"
+
 // Ensure we get all of winsock2.h
 #ifndef _WIN32_WINNT
 #define _WIN32_WINNT 0x0501
 #endif
 
-#include "qpid/sys/SocketAddress.h"
-
-#include "qpid/sys/windows/check.h"
-
 #include <winsock2.h>
 #include <ws2tcpip.h>
 #include <string.h>
@@ -64,25 +65,45 @@ SocketAddress::~SocketAddress()
     }
 }
 
-std::string SocketAddress::asString(bool numeric) const
+std::string SocketAddress::asString(::sockaddr const * const addr, size_t addrlen)
 {
-    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)
+    if (int rc=::getnameinfo(addr, addrlen,
+        dispName, sizeof(dispName),
+                             servName, sizeof(servName),
+                             NI_NUMERICHOST | NI_NUMERICSERV) != 0)
         throw qpid::Exception(QPID_MSG(gai_strerror(rc)));
-    std::string s(dispName);
+    std::string s;
+    switch (addr->sa_family) {
+        case AF_INET: s += dispName; break;
+        case AF_INET6: s += "["; s += dispName; s+= "]"; break;
+        default: throw Exception(QPID_MSG("Unexpected socket type"));
+    }
     s += ":";
     s += servName;
     return s;
 }
 
+uint16_t SocketAddress::getPort(::sockaddr const * const addr)
+{
+    switch (addr->sa_family) {
+        case AF_INET: return ntohs(((::sockaddr_in*)addr)->sin_port);
+        case AF_INET6: return ntohs(((::sockaddr_in6*)addr)->sin6_port);
+        default:throw Exception(QPID_MSG("Unexpected socket type"));
+    }
+}
+
+std::string SocketAddress::asString(bool numeric) const
+{
+    if (!numeric)
+        return host + ":" + port;
+    // Canonicalise into numeric id
+    const ::addrinfo& ai = getAddrInfo(*this);
+
+    return asString(ai.ai_addr, ai.ai_addrlen);
+}
+
 bool SocketAddress::nextAddress() {
     bool r = currentAddrInfo->ai_next != 0;
     if (r)



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


Mime
View raw message