icinga2/base/tcpsocket.cpp

41 lines
783 B
C++

#include "i2-base.h"
using namespace icinga;
void TCPSocket::MakeSocket(void)
{
assert(GetFD() == INVALID_SOCKET);
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd == INVALID_SOCKET) {
HandleSocketError();
return;
}
SetFD(fd);
}
void TCPSocket::Bind(unsigned short port)
{
Bind(NULL, port);
}
void TCPSocket::Bind(const char *hostname, unsigned short port)
{
#ifndef _WIN32
const int optTrue = 1;
setsockopt(GetFD(), SOL_SOCKET, SO_REUSEADDR, (char *)&optTrue, sizeof(optTrue));
#endif /* _WIN32 */
sockaddr_in sin;
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);
if (::bind(GetFD(), (sockaddr *)&sin, sizeof(sin)) < 0)
HandleSocketError();
}