mirror of https://github.com/Icinga/icinga2.git
Moved Get*Address() functions to Socket class.
This commit is contained in:
parent
c1a8576c5c
commit
db25f7f353
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue