From 724affbbb66158e3694f9ebcf2918c37825702b7 Mon Sep 17 00:00:00 2001 From: Andrew Stitcher Date: Wed, 17 Aug 2011 22:07:36 +0000 Subject: 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 git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@1158934 13f79535-47bb-0310-9956-ffa450edef68 --- cpp/src/qpid/sys/windows/SocketAddress.cpp | 49 +++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 14 deletions(-) (limited to 'cpp/src/qpid/sys/windows/SocketAddress.cpp') diff --git a/cpp/src/qpid/sys/windows/SocketAddress.cpp b/cpp/src/qpid/sys/windows/SocketAddress.cpp index 438c31f001..77bbf85810 100644 --- a/cpp/src/qpid/sys/windows/SocketAddress.cpp +++ b/cpp/src/qpid/sys/windows/SocketAddress.cpp @@ -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 #include #include @@ -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) -- cgit v1.2.1