From 548abd065f91bc1f238ac98c24edf410edf10356 Mon Sep 17 00:00:00 2001 From: Andrew Stitcher Date: Wed, 27 Jun 2007 21:05:50 +0000 Subject: Fixed missing POSIX implementation of Acceptor::getHost git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@551327 13f79535-47bb-0310-9956-ffa450edef68 --- cpp/src/qpid/sys/posix/EventChannelAcceptor.cpp | 33 ++++++++++++++++--------- cpp/src/qpid/sys/posix/Socket.cpp | 17 +------------ cpp/src/qpid/sys/posix/Socket.h | 2 +- 3 files changed, 24 insertions(+), 28 deletions(-) (limited to 'cpp/src') diff --git a/cpp/src/qpid/sys/posix/EventChannelAcceptor.cpp b/cpp/src/qpid/sys/posix/EventChannelAcceptor.cpp index cbda216cfc..a61a66c577 100644 --- a/cpp/src/qpid/sys/posix/EventChannelAcceptor.cpp +++ b/cpp/src/qpid/sys/posix/EventChannelAcceptor.cpp @@ -18,13 +18,7 @@ * under the License. * */ -#include - -#include -#include -#include -#include -#include +#include "EventChannelConnection.h" #include "qpid/sys/ConnectionOutputHandler.h" #include "qpid/sys/ConnectionInputHandler.h" @@ -35,7 +29,16 @@ #include "qpid/framing/AMQFrame.h" #include "qpid/Exception.h" -#include "EventChannelConnection.h" +#include +#include + +#include +#include +#include +#include +#include + +#include namespace qpid { namespace sys { @@ -100,11 +103,19 @@ EventChannelAcceptor::EventChannelAcceptor( uint16_t EventChannelAcceptor::getPort() const { return port; // Immutable no need for lock. } + +std::string EventChannelAcceptor::getHost() const { + ::sockaddr_storage name; // big enough for any socket address + ::socklen_t namelen = sizeof(name); + if (::getsockname(listener.fd(), (::sockaddr*)&name, &namelen) < 0) + throw QPID_POSIX_ERROR(errno); -uint16_t EventChannelAcceptor::getPort() const { - return port; // Immutable no need for lock. + char dispName[NI_MAXHOST]; + if (int rc=::getnameinfo((::sockaddr*)&name, namelen, dispName, sizeof(dispName), 0, 0, NI_NUMERICHOST) != 0) + throw QPID_POSIX_ERROR(rc); + return dispName; } - + void EventChannelAcceptor::run(ConnectionInputHandlerFactory* f) { { Mutex::ScopedLock l(lock); diff --git a/cpp/src/qpid/sys/posix/Socket.cpp b/cpp/src/qpid/sys/posix/Socket.cpp index 50cbfa7c4d..d46e7943d9 100644 --- a/cpp/src/qpid/sys/posix/Socket.cpp +++ b/cpp/src/qpid/sys/posix/Socket.cpp @@ -111,23 +111,8 @@ int Socket::listen(int port, int backlog) return ntohs(name.sin_port); } - -std::string getHost() const { - // TODO aconway 2007-06-11: Won't work for ip6 - struct sockaddr_in name; - socklen_t namelen = sizeof(name); - if (::getsockname(socket, (struct sockaddr*)&name, &namelen) < 0) - throw QPID_POSIX_ERROR(errno); - uint32_t addr = name.sin_host.s_addr; - ostringstream os; - os << uint8_t(addr >> 24) << '.' - << uint8_t(addr >> 16) << '.' - << uint8_t(addr >> 8) << '.' - << uint8_t(addr); - return os.str(); -} -int Socket::fd() +int Socket::fd() const { return socket; } diff --git a/cpp/src/qpid/sys/posix/Socket.h b/cpp/src/qpid/sys/posix/Socket.h index 614221354f..ca87104471 100644 --- a/cpp/src/qpid/sys/posix/Socket.h +++ b/cpp/src/qpid/sys/posix/Socket.h @@ -63,7 +63,7 @@ class Socket int listen(int port = 0, int backlog = 10); /** Get file descriptor */ - int fd(); + int fd() const; private: void init() const; -- cgit v1.2.1