diff options
Diffstat (limited to 'cpp/src/qpid/sys')
| -rw-r--r-- | cpp/src/qpid/sys/Socket.h | 9 | ||||
| -rw-r--r-- | cpp/src/qpid/sys/posix/Socket.cpp | 39 |
2 files changed, 48 insertions, 0 deletions
diff --git a/cpp/src/qpid/sys/Socket.h b/cpp/src/qpid/sys/Socket.h index 2e9a389133..1594e89aac 100644 --- a/cpp/src/qpid/sys/Socket.h +++ b/cpp/src/qpid/sys/Socket.h @@ -87,7 +87,16 @@ public: * socket */ std::string getPeerAddress() const; + /** + * Returns an address (host and port) for the local end of the + * socket + */ + std::string getLocalAddress() const; + uint getLocalPort() const; + uint getRemotePort() const; + + /** Accept a connection from a socket that is already listening * and has an incoming connection */ diff --git a/cpp/src/qpid/sys/posix/Socket.cpp b/cpp/src/qpid/sys/posix/Socket.cpp index ccb07bdafc..028b45d03d 100644 --- a/cpp/src/qpid/sys/posix/Socket.cpp +++ b/cpp/src/qpid/sys/posix/Socket.cpp @@ -30,6 +30,7 @@ #include <sys/errno.h> #include <netinet/in.h> #include <netdb.h> +#include <cstdlib> #include <boost/format.hpp> @@ -45,6 +46,7 @@ public: int fd; std::string getName(bool local, bool includeService = false) const; + std::string getService(bool local) const; }; std::string SocketPrivate::getName(bool local, bool includeService) const @@ -77,6 +79,28 @@ std::string SocketPrivate::getName(bool local, bool includeService) const } } +std::string SocketPrivate::getService(bool local) const +{ + ::sockaddr_storage name; // big enough for any socket address + ::socklen_t namelen = sizeof(name); + + int result = -1; + if (local) { + result = ::getsockname(fd, (::sockaddr*)&name, &namelen); + } else { + result = ::getpeername(fd, (::sockaddr*)&name, &namelen); + } + + QPID_POSIX_CHECK(result); + + char servName[NI_MAXSERV]; + if (int rc=::getnameinfo((::sockaddr*)&name, namelen, 0, 0, + servName, sizeof(servName), + NI_NUMERICHOST | NI_NUMERICSERV) != 0) + throw QPID_POSIX_ERROR(rc); + return servName; +} + Socket::Socket() : impl(new SocketPrivate) { @@ -231,6 +255,21 @@ std::string Socket::getPeerAddress() const return impl->getName(false, true); } +std::string Socket::getLocalAddress() const +{ + return impl->getName(true, true); +} + +uint Socket::getLocalPort() const +{ + return atoi(impl->getService(true).c_str()); +} + +uint Socket::getRemotePort() const +{ + return atoi(impl->getService(true).c_str()); +} + int Socket::toFd() const { return impl->fd; } |
