2019-02-25 08:48:22 -05:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2012-05-10 06:06:41 -04:00
|
|
|
|
2014-05-25 10:23:35 -04:00
|
|
|
#include "base/tcpsocket.hpp"
|
2014-10-19 08:21:12 -04:00
|
|
|
#include "base/logger.hpp"
|
2014-06-05 08:08:01 -04:00
|
|
|
#include "base/utility.hpp"
|
2014-05-25 10:23:35 -04:00
|
|
|
#include "base/exception.hpp"
|
2013-03-18 12:04:22 -04:00
|
|
|
#include <boost/exception/errinfo_api_function.hpp>
|
|
|
|
|
#include <boost/exception/errinfo_errno.hpp>
|
2014-06-05 08:08:01 -04:00
|
|
|
#include <iostream>
|
2012-03-28 07:24:49 -04:00
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
2012-05-08 09:14:20 -04:00
|
|
|
/**
|
|
|
|
|
* Creates a socket and binds it to the specified service.
|
|
|
|
|
*
|
|
|
|
|
* @param service The service.
|
|
|
|
|
* @param family The address family for the socket.
|
|
|
|
|
*/
|
2013-04-04 10:08:02 -04:00
|
|
|
void TcpSocket::Bind(const String& service, int family)
|
2012-03-28 07:24:49 -04:00
|
|
|
{
|
2012-08-02 03:38:08 -04:00
|
|
|
Bind(String(), service, family);
|
2012-03-28 07:24:49 -04:00
|
|
|
}
|
|
|
|
|
|
2012-05-08 09:14:20 -04:00
|
|
|
/**
|
|
|
|
|
* Creates a socket and binds it to the specified node and service.
|
|
|
|
|
*
|
2012-05-25 05:10:11 -04:00
|
|
|
* @param node The node.
|
2012-05-08 09:14:20 -04:00
|
|
|
* @param service The service.
|
|
|
|
|
* @param family The address family for the socket.
|
|
|
|
|
*/
|
2013-04-04 10:08:02 -04:00
|
|
|
void TcpSocket::Bind(const String& node, const String& service, int family)
|
2012-03-28 07:24:49 -04:00
|
|
|
{
|
2012-04-27 03:54:07 -04:00
|
|
|
addrinfo hints;
|
|
|
|
|
addrinfo *result;
|
2023-03-24 13:07:51 -04:00
|
|
|
int error = 0;
|
|
|
|
|
const char *func = nullptr;
|
2012-03-28 07:24:49 -04:00
|
|
|
|
2012-04-27 03:54:07 -04:00
|
|
|
memset(&hints, 0, sizeof(hints));
|
2012-05-07 07:58:22 -04:00
|
|
|
hints.ai_family = family;
|
2012-04-27 03:54:07 -04:00
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
|
hints.ai_protocol = IPPROTO_TCP;
|
|
|
|
|
hints.ai_flags = AI_PASSIVE;
|
2012-04-04 06:22:46 -04:00
|
|
|
|
2017-12-14 09:37:20 -05:00
|
|
|
int rc = getaddrinfo(node.IsEmpty() ? nullptr : node.CStr(),
|
2017-12-19 09:50:05 -05:00
|
|
|
service.CStr(), &hints, &result);
|
2013-09-12 01:50:09 -04:00
|
|
|
|
|
|
|
|
if (rc != 0) {
|
2014-10-19 11:52:17 -04:00
|
|
|
Log(LogCritical, "TcpSocket")
|
2017-12-19 09:50:05 -05:00
|
|
|
<< "getaddrinfo() failed with error code " << rc << ", \"" << gai_strerror(rc) << "\"";
|
2014-06-05 08:08:01 -04:00
|
|
|
|
2013-03-11 09:03:01 -04:00
|
|
|
BOOST_THROW_EXCEPTION(socket_error()
|
2017-12-19 09:50:05 -05:00
|
|
|
<< boost::errinfo_api_function("getaddrinfo")
|
|
|
|
|
<< errinfo_getaddrinfo_error(rc));
|
2013-03-11 08:45:08 -04:00
|
|
|
}
|
2012-04-26 10:45:00 -04:00
|
|
|
|
2012-04-27 03:54:07 -04:00
|
|
|
int fd = INVALID_SOCKET;
|
2012-04-26 10:45:00 -04:00
|
|
|
|
2017-12-14 09:37:20 -05:00
|
|
|
for (addrinfo *info = result; info != nullptr; info = info->ai_next) {
|
2012-04-27 03:54:07 -04:00
|
|
|
fd = socket(info->ai_family, info->ai_socktype, info->ai_protocol);
|
2012-04-26 10:45:00 -04:00
|
|
|
|
2013-09-12 01:50:09 -04:00
|
|
|
if (fd == INVALID_SOCKET) {
|
2017-12-19 09:50:05 -05:00
|
|
|
#ifdef _WIN32
|
2013-09-12 01:50:09 -04:00
|
|
|
error = WSAGetLastError();
|
|
|
|
|
#else /* _WIN32 */
|
|
|
|
|
error = errno;
|
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
func = "socket";
|
|
|
|
|
|
2012-04-27 03:54:07 -04:00
|
|
|
continue;
|
2013-09-12 01:50:09 -04:00
|
|
|
}
|
2012-04-26 10:45:00 -04:00
|
|
|
|
2012-04-27 03:54:07 -04:00
|
|
|
const int optFalse = 0;
|
2012-08-08 02:34:15 -04:00
|
|
|
setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, reinterpret_cast<const char *>(&optFalse), sizeof(optFalse));
|
2012-04-26 10:45:00 -04:00
|
|
|
|
2012-04-27 03:54:07 -04:00
|
|
|
const int optTrue = 1;
|
2013-07-12 05:03:36 -04:00
|
|
|
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<const char *>(&optTrue), sizeof(optTrue));
|
2019-02-26 05:25:44 -05:00
|
|
|
#ifdef SO_REUSEPORT
|
2019-01-17 06:13:44 -05:00
|
|
|
setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, reinterpret_cast<const char *>(&optTrue), sizeof(optTrue));
|
2019-02-26 05:25:44 -05:00
|
|
|
#endif /* SO_REUSEPORT */
|
2012-04-26 10:45:00 -04:00
|
|
|
|
2012-06-15 13:32:41 -04:00
|
|
|
int rc = bind(fd, info->ai_addr, info->ai_addrlen);
|
2012-04-26 10:45:00 -04:00
|
|
|
|
2013-04-04 10:08:02 -04:00
|
|
|
if (rc < 0) {
|
2013-09-12 01:50:09 -04:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
error = WSAGetLastError();
|
|
|
|
|
#else /* _WIN32 */
|
|
|
|
|
error = errno;
|
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
func = "bind";
|
|
|
|
|
|
2012-05-08 03:44:58 -04:00
|
|
|
closesocket(fd);
|
|
|
|
|
|
2012-04-27 03:54:07 -04:00
|
|
|
continue;
|
2012-05-08 03:44:58 -04:00
|
|
|
}
|
2012-04-27 03:54:07 -04:00
|
|
|
|
2013-04-04 10:08:02 -04:00
|
|
|
SetFD(fd);
|
|
|
|
|
|
2012-04-27 03:54:07 -04:00
|
|
|
break;
|
2012-04-26 10:45:00 -04:00
|
|
|
}
|
|
|
|
|
|
2012-04-27 03:54:07 -04:00
|
|
|
freeaddrinfo(result);
|
2012-05-26 14:00:03 -04:00
|
|
|
|
2013-09-12 01:50:09 -04:00
|
|
|
if (GetFD() == INVALID_SOCKET) {
|
2014-10-19 11:52:17 -04:00
|
|
|
Log(LogCritical, "TcpSocket")
|
2017-12-19 09:50:05 -05:00
|
|
|
<< "Invalid socket: " << Utility::FormatErrorNumber(error);
|
2014-06-05 08:08:01 -04:00
|
|
|
|
2013-09-12 01:50:09 -04:00
|
|
|
#ifndef _WIN32
|
|
|
|
|
BOOST_THROW_EXCEPTION(socket_error()
|
2017-12-19 09:50:05 -05:00
|
|
|
<< boost::errinfo_api_function(func)
|
|
|
|
|
<< boost::errinfo_errno(error));
|
2013-09-12 01:50:09 -04:00
|
|
|
#else /* _WIN32 */
|
|
|
|
|
BOOST_THROW_EXCEPTION(socket_error()
|
2017-12-19 09:50:05 -05:00
|
|
|
<< boost::errinfo_api_function(func)
|
|
|
|
|
<< errinfo_win32_error(error));
|
2013-09-12 01:50:09 -04:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
}
|
2012-04-26 10:45:00 -04:00
|
|
|
}
|
2017-05-23 06:04:08 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a socket and connects to the specified node and service.
|
|
|
|
|
*
|
|
|
|
|
* @param node The node.
|
|
|
|
|
* @param service The service.
|
|
|
|
|
*/
|
|
|
|
|
void TcpSocket::Connect(const String& node, const String& service)
|
|
|
|
|
{
|
|
|
|
|
addrinfo hints;
|
|
|
|
|
addrinfo *result;
|
2023-03-24 13:07:51 -04:00
|
|
|
int error = 0;
|
|
|
|
|
const char *func = nullptr;
|
2017-05-23 06:04:08 -04:00
|
|
|
|
|
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
|
hints.ai_protocol = IPPROTO_TCP;
|
|
|
|
|
|
|
|
|
|
int rc = getaddrinfo(node.CStr(), service.CStr(), &hints, &result);
|
|
|
|
|
|
|
|
|
|
if (rc != 0) {
|
|
|
|
|
Log(LogCritical, "TcpSocket")
|
2017-12-19 09:50:05 -05:00
|
|
|
<< "getaddrinfo() failed with error code " << rc << ", \"" << gai_strerror(rc) << "\"";
|
2017-05-23 06:04:08 -04:00
|
|
|
|
|
|
|
|
BOOST_THROW_EXCEPTION(socket_error()
|
2017-12-19 09:50:05 -05:00
|
|
|
<< boost::errinfo_api_function("getaddrinfo")
|
|
|
|
|
<< errinfo_getaddrinfo_error(rc));
|
2017-05-23 06:04:08 -04:00
|
|
|
}
|
|
|
|
|
|
2017-12-14 02:47:04 -05:00
|
|
|
SOCKET fd = INVALID_SOCKET;
|
2017-05-23 06:04:08 -04:00
|
|
|
|
2017-12-14 09:37:20 -05:00
|
|
|
for (addrinfo *info = result; info != nullptr; info = info->ai_next) {
|
2017-05-23 06:04:08 -04:00
|
|
|
fd = socket(info->ai_family, info->ai_socktype, info->ai_protocol);
|
|
|
|
|
|
|
|
|
|
if (fd == INVALID_SOCKET) {
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
error = WSAGetLastError();
|
|
|
|
|
#else /* _WIN32 */
|
|
|
|
|
error = errno;
|
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
func = "socket";
|
|
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const int optTrue = 1;
|
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, reinterpret_cast<const char *>(&optTrue), sizeof(optTrue)) != 0) {
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
error = WSAGetLastError();
|
|
|
|
|
#else /* _WIN32 */
|
|
|
|
|
error = errno;
|
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
Log(LogWarning, "TcpSocket")
|
2017-12-19 09:50:05 -05:00
|
|
|
<< "setsockopt() unable to enable TCP keep-alives with error code " << rc;
|
2017-05-23 06:04:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rc = connect(fd, info->ai_addr, info->ai_addrlen);
|
|
|
|
|
|
|
|
|
|
if (rc < 0) {
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
error = WSAGetLastError();
|
|
|
|
|
#else /* _WIN32 */
|
|
|
|
|
error = errno;
|
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
func = "connect";
|
|
|
|
|
|
|
|
|
|
closesocket(fd);
|
|
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SetFD(fd);
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
freeaddrinfo(result);
|
|
|
|
|
|
|
|
|
|
if (GetFD() == INVALID_SOCKET) {
|
|
|
|
|
Log(LogCritical, "TcpSocket")
|
2017-12-19 09:50:05 -05:00
|
|
|
<< "Invalid socket: " << Utility::FormatErrorNumber(error);
|
2017-05-23 06:04:08 -04:00
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
BOOST_THROW_EXCEPTION(socket_error()
|
2017-12-19 09:50:05 -05:00
|
|
|
<< boost::errinfo_api_function(func)
|
|
|
|
|
<< boost::errinfo_errno(error));
|
2017-05-23 06:04:08 -04:00
|
|
|
#else /* _WIN32 */
|
|
|
|
|
BOOST_THROW_EXCEPTION(socket_error()
|
2017-12-19 09:50:05 -05:00
|
|
|
<< boost::errinfo_api_function(func)
|
|
|
|
|
<< errinfo_win32_error(error));
|
2017-05-23 06:04:08 -04:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
}
|
|
|
|
|
}
|