From db25f7f353295a54b438b8ac7d8e89b85d3c0658 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Fri, 27 Apr 2012 11:44:05 +0200 Subject: [PATCH] Moved Get*Address() functions to Socket class. --- base/socket.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ base/socket.h | 5 +++++ base/tcpsocket.cpp | 42 ------------------------------------------ base/tcpsocket.h | 8 -------- 4 files changed, 46 insertions(+), 50 deletions(-) diff --git a/base/socket.cpp b/base/socket.cpp index 7e3770eda..2d0656ac9 100644 --- a/base/socket.cpp +++ b/base/socket.cpp @@ -105,3 +105,44 @@ bool Socket::WantsToWrite(void) const { return false; } + +string Socket::GetAddressFromSockaddr(sockaddr *address, socklen_t len) +{ + char host[NI_MAXHOST]; + char service[NI_MAXSERV]; + + if (getnameinfo(address, len, host, sizeof(host), service, sizeof(service), NI_NUMERICHOST | NI_NUMERICSERV) < 0) + throw InvalidArgumentException(); /* TODO: throw proper exception */ + + stringstream s; + s << "[" << host << "]:" << service; + return s.str(); +} + +string Socket::GetClientAddress(void) +{ + sockaddr_storage sin; + socklen_t len = sizeof(sin); + + if (getsockname(GetFD(), (sockaddr *)&sin, &len) < 0) { + HandleSocketError(); + + return string(); + } + + return GetAddressFromSockaddr((sockaddr *)&sin, len); +} + +string Socket::GetPeerAddress(void) +{ + sockaddr_storage sin; + socklen_t len = sizeof(sin); + + if (getpeername(GetFD(), (sockaddr *)&sin, &len) < 0) { + HandleSocketError(); + + return string(); + } + + return GetAddressFromSockaddr((sockaddr *)&sin, len); +} diff --git a/base/socket.h b/base/socket.h index 1e6aa9881..54d7b1c4a 100644 --- a/base/socket.h +++ b/base/socket.h @@ -19,6 +19,8 @@ private: int ExceptionEventHandler(const EventArgs& ea); + static string GetAddressFromSockaddr(sockaddr *address, socklen_t len); + protected: Socket(void); @@ -52,6 +54,9 @@ public: virtual void Stop(void); void Close(void); + + string GetClientAddress(void); + string GetPeerAddress(void); }; } diff --git a/base/tcpsocket.cpp b/base/tcpsocket.cpp index 86d73afb5..0ab19cc14 100644 --- a/base/tcpsocket.cpp +++ b/base/tcpsocket.cpp @@ -78,45 +78,3 @@ void TCPSocket::Bind(const char *hostname, unsigned short port, int family) freeaddrinfo(result); } - - -string TCPSocket::GetAddressFromSockaddr(sockaddr *address, socklen_t len) -{ - char host[NI_MAXHOST]; - char service[NI_MAXSERV]; - - if (getnameinfo(address, len, host, sizeof(host), service, sizeof(service), NI_NUMERICHOST | NI_NUMERICSERV) < 0) - throw InvalidArgumentException(); /* TODO: throw proper exception */ - - stringstream s; - s << "[" << host << "]:" << service; - return s.str(); -} - -string TCPSocket::GetClientAddress(void) -{ - sockaddr_storage sin; - socklen_t len = sizeof(sin); - - if (getsockname(GetFD(), (sockaddr *)&sin, &len) < 0) { - HandleSocketError(); - - return string(); - } - - return GetAddressFromSockaddr((sockaddr *)&sin, len); -} - -string TCPSocket::GetPeerAddress(void) -{ - sockaddr_storage sin; - socklen_t len = sizeof(sin); - - if (getpeername(GetFD(), (sockaddr *)&sin, &len) < 0) { - HandleSocketError(); - - return string(); - } - - return GetAddressFromSockaddr((sockaddr *)&sin, len); -} diff --git a/base/tcpsocket.h b/base/tcpsocket.h index 1545cc4ce..16ae14851 100644 --- a/base/tcpsocket.h +++ b/base/tcpsocket.h @@ -7,8 +7,6 @@ namespace icinga class I2_BASE_API TCPSocket : public Socket { private: - static string GetAddressFromSockaddr(sockaddr *address, socklen_t len); - void MakeSocket(int family); public: @@ -17,12 +15,6 @@ public: void Bind(unsigned short port, int family); void Bind(const char *hostname, unsigned short port, int family); - - bool GetClientSockaddr(sockaddr_storage *address); - bool GetPeerSockaddr(sockaddr_storage *address); - - string GetClientAddress(void); - string GetPeerAddress(void); }; }