2012-03-28 13:24:49 +02:00
|
|
|
#include "i2-base.h"
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2012-04-27 09:54:07 +02:00
|
|
|
void TCPSocket::MakeSocket(int family)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
2012-03-29 13:15:54 +02:00
|
|
|
assert(GetFD() == INVALID_SOCKET);
|
2012-03-28 13:24:49 +02:00
|
|
|
|
2012-04-27 09:54:07 +02:00
|
|
|
int fd = socket(family, SOCK_STREAM, 0);
|
2012-03-29 13:15:54 +02:00
|
|
|
|
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
|
|
|
}
|
2012-03-29 13:15:54 +02:00
|
|
|
|
|
|
|
SetFD(fd);
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
|
|
|
|
2012-05-07 13:48:17 +02:00
|
|
|
void TCPSocket::Bind(string service, int family)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
2012-05-07 13:48:17 +02:00
|
|
|
Bind(string(), service, family);
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
|
|
|
|
2012-05-07 13:48:17 +02:00
|
|
|
void TCPSocket::Bind(string node, string service, int family)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
2012-04-27 09:54:07 +02:00
|
|
|
addrinfo hints;
|
|
|
|
addrinfo *result;
|
2012-03-28 13:24:49 +02:00
|
|
|
|
2012-04-27 09:54:07 +02:00
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_protocol = IPPROTO_TCP;
|
|
|
|
hints.ai_flags = AI_PASSIVE;
|
2012-04-04 12:22:46 +02:00
|
|
|
|
2012-05-07 13:48:17 +02:00
|
|
|
if (getaddrinfo(node.c_str(), service.c_str(), &hints, &result) < 0) {
|
2012-04-22 16:45:31 +02:00
|
|
|
HandleSocketError();
|
2012-04-26 16:45:00 +02:00
|
|
|
|
2012-04-27 09:54:07 +02:00
|
|
|
return;
|
|
|
|
}
|
2012-04-26 16:45:00 +02:00
|
|
|
|
2012-04-27 09:54:07 +02:00
|
|
|
int fd = INVALID_SOCKET;
|
2012-04-26 16:45:00 +02:00
|
|
|
|
2012-04-27 09:54:07 +02:00
|
|
|
for (addrinfo *info = result; info != NULL; info = info->ai_next) {
|
|
|
|
fd = socket(info->ai_family, info->ai_socktype, info->ai_protocol);
|
2012-04-26 16:45:00 +02:00
|
|
|
|
2012-04-27 09:54:07 +02:00
|
|
|
if (fd == INVALID_SOCKET)
|
|
|
|
continue;
|
2012-04-26 16:45:00 +02:00
|
|
|
|
2012-04-27 09:54:07 +02:00
|
|
|
SetFD(fd);
|
2012-04-26 16:45:00 +02:00
|
|
|
|
2012-04-27 09:54:07 +02:00
|
|
|
const int optFalse = 0;
|
|
|
|
setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&optFalse, sizeof(optFalse));
|
2012-04-26 16:45:00 +02:00
|
|
|
|
2012-04-27 09:54:07 +02:00
|
|
|
#ifndef _WIN32
|
|
|
|
const int optTrue = 1;
|
|
|
|
setsockopt(GetFD(), SOL_SOCKET, SO_REUSEADDR, (char *)&optTrue, sizeof(optTrue));
|
2012-04-26 16:45:00 +02:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
2012-04-27 09:54:07 +02:00
|
|
|
int rc = ::bind(fd, info->ai_addr, info->ai_addrlen);
|
2012-04-26 16:45:00 +02:00
|
|
|
|
2012-04-27 09:54:07 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
if (rc < 0 && WSAGetLastError() != WSAEWOULDBLOCK)
|
|
|
|
#else /* _WIN32 */
|
|
|
|
if (rc < 0 && errno != EINPROGRESS)
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
continue;
|
|
|
|
|
|
|
|
break;
|
2012-04-26 16:45:00 +02:00
|
|
|
}
|
|
|
|
|
2012-04-27 09:54:07 +02:00
|
|
|
if (fd == INVALID_SOCKET)
|
2012-04-26 16:45:00 +02:00
|
|
|
HandleSocketError();
|
2012-04-26 21:33:23 +02:00
|
|
|
|
2012-04-27 09:54:07 +02:00
|
|
|
freeaddrinfo(result);
|
2012-04-26 16:45:00 +02:00
|
|
|
}
|