icinga2/base/tcpsocket.cpp

135 lines
2.5 KiB
C++
Raw Normal View History

2012-03-28 13:24:49 +02:00
#include "i2-base.h"
using namespace icinga;
void TCPSocket::MakeSocket(void)
{
assert(GetFD() == INVALID_SOCKET);
2012-03-28 13:24:49 +02:00
int fd = socket(AF_INET, SOCK_STREAM, 0);
2012-04-04 16:02:19 +02:00
if (fd == INVALID_SOCKET) {
2012-04-22 16:45:31 +02:00
HandleSocketError();
return;
2012-04-04 16:02:19 +02:00
}
SetFD(fd);
2012-03-28 13:24:49 +02:00
}
void TCPSocket::Bind(unsigned short port)
{
Bind(NULL, port);
}
void TCPSocket::Bind(const char *hostname, unsigned short port)
{
2012-04-19 09:18:54 +02:00
#ifndef _WIN32
const int optTrue = 1;
setsockopt(GetFD(), SOL_SOCKET, SO_REUSEADDR, (char *)&optTrue, sizeof(optTrue));
#endif /* _WIN32 */
2012-03-28 13:24:49 +02:00
2012-04-19 09:18:54 +02:00
sockaddr_in sin;
2012-03-28 13:24:49 +02:00
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = hostname ? inet_addr(hostname) : htonl(INADDR_ANY);
sin.sin_port = htons(port);
2012-04-22 16:45:31 +02:00
if (::bind(GetFD(), (sockaddr *)&sin, sizeof(sin)) < 0)
HandleSocketError();
2012-03-28 13:24:49 +02:00
}
string TCPSocket::GetAddressFromSockaddr(sockaddr *address)
{
2012-04-26 16:55:26 +02:00
static char buffer[256];
#ifdef _WIN32
2012-04-26 16:55:26 +02:00
DWORD BufferLength = sizeof(buffer);
socklen_t len;
if (address->sa_family == AF_INET)
len = sizeof(sockaddr_in);
else if (address->sa_family == AF_INET6)
len = sizeof(sockaddr_in6);
else {
assert(0);
return "";
}
2012-04-26 16:55:26 +02:00
if (WSAAddressToString(address, len, NULL, buffer, &BufferLength) != 0)
return string();
#else /* _WIN32 */
void *IpAddress;
2012-04-26 16:55:26 +02:00
if (address->sa_family == AF_INET)
IpAddress = &(((sockaddr_in *)address)->sin_addr);
else
IpAddress = &(((sockaddr_in6 *)address)->sin6_addr);
2012-04-26 21:33:23 +02:00
if (inet_ntop(address->sa_family, IpAddress, buffer, sizeof(buffer)) == NULL)
2012-04-26 16:55:26 +02:00
return string();
#endif /* _WIN32 */
2012-04-26 16:55:26 +02:00
return buffer;
}
unsigned short TCPSocket::GetPortFromSockaddr(sockaddr *address)
{
if (address->sa_family == AF_INET)
return htons(((sockaddr_in *)address)->sin_port);
else if (address->sa_family == AF_INET6)
return htons(((sockaddr_in6 *)address)->sin6_port);
else {
assert(0);
return 0;
}
}
2012-04-26 21:33:23 +02:00
bool TCPSocket::GetClientSockaddr(sockaddr_storage *address)
{
socklen_t len = sizeof(*address);
2012-04-26 21:33:23 +02:00
if (getsockname(GetFD(), (sockaddr *)address, &len) < 0) {
HandleSocketError();
2012-04-26 21:33:23 +02:00
return false;
}
return true;
}
2012-04-26 21:33:23 +02:00
bool TCPSocket::GetPeerSockaddr(sockaddr_storage *address)
{
socklen_t len = sizeof(*address);
2012-04-26 21:33:23 +02:00
if (getpeername(GetFD(), (sockaddr *)address, &len) < 0) {
HandleSocketError();
2012-04-26 21:33:23 +02:00
return false;
}
return true;
}
string TCPSocket::GetClientAddress(void)
{
sockaddr_storage sin;
2012-04-26 21:33:23 +02:00
if (!GetClientSockaddr(&sin))
return "";
return GetAddressFromSockaddr((sockaddr *)&sin);
}
string TCPSocket::GetPeerAddress(void)
{
sockaddr_storage sin;
2012-04-26 21:33:23 +02:00
if (!GetPeerSockaddr(&sin))
return "";
return GetAddressFromSockaddr((sockaddr *)&sin);
}