2012-05-10 12:06:41 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2015-01-22 12:00:23 +01:00
|
|
|
* Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
|
2012-05-10 12:06:41 +02:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
2012-05-11 13:33:57 +02:00
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
2012-05-10 12:06:41 +02:00
|
|
|
******************************************************************************/
|
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/tcpsocket.hpp"
|
2014-10-19 14:21:12 +02:00
|
|
|
#include "base/logger.hpp"
|
2014-06-05 14:08:01 +02:00
|
|
|
#include "base/utility.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/exception.hpp"
|
2013-03-18 17:04:22 +01:00
|
|
|
#include <boost/exception/errinfo_api_function.hpp>
|
|
|
|
#include <boost/exception/errinfo_errno.hpp>
|
2014-06-05 14:08:01 +02:00
|
|
|
#include <iostream>
|
2012-03-28 13:24:49 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2012-05-08 15:14:20 +02: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 16:08:02 +02:00
|
|
|
void TcpSocket::Bind(const String& service, int family)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
2012-08-02 09:38:08 +02:00
|
|
|
Bind(String(), service, family);
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* Creates a socket and binds it to the specified node and service.
|
|
|
|
*
|
2012-05-25 11:10:11 +02:00
|
|
|
* @param node The node.
|
2012-05-08 15:14:20 +02:00
|
|
|
* @param service The service.
|
|
|
|
* @param family The address family for the socket.
|
|
|
|
*/
|
2013-04-04 16:08:02 +02:00
|
|
|
void TcpSocket::Bind(const String& node, const String& service, int family)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
2012-04-27 09:54:07 +02:00
|
|
|
addrinfo hints;
|
|
|
|
addrinfo *result;
|
2013-09-12 07:50:09 +02:00
|
|
|
int error;
|
|
|
|
const char *func;
|
2012-03-28 13:24:49 +02:00
|
|
|
|
2012-04-27 09:54:07 +02:00
|
|
|
memset(&hints, 0, sizeof(hints));
|
2012-05-07 13:58:22 +02:00
|
|
|
hints.ai_family = family;
|
2012-04-27 09:54:07 +02:00
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_protocol = IPPROTO_TCP;
|
|
|
|
hints.ai_flags = AI_PASSIVE;
|
2012-04-04 12:22:46 +02:00
|
|
|
|
2013-09-12 07:50:09 +02:00
|
|
|
int rc = getaddrinfo(node.IsEmpty() ? NULL : node.CStr(),
|
|
|
|
service.CStr(), &hints, &result);
|
|
|
|
|
|
|
|
if (rc != 0) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "TcpSocket")
|
|
|
|
<< "getaddrinfo() failed with error code " << rc << ", \"" << gai_strerror(rc) << "\"";
|
2014-06-05 14:08:01 +02:00
|
|
|
|
2013-03-11 14:03:01 +01:00
|
|
|
BOOST_THROW_EXCEPTION(socket_error()
|
2013-03-18 17:04:22 +01:00
|
|
|
<< boost::errinfo_api_function("getaddrinfo")
|
2013-09-12 07:50:09 +02:00
|
|
|
<< errinfo_getaddrinfo_error(rc));
|
2013-03-11 13:45:08 +01:00
|
|
|
}
|
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
|
|
|
|
2013-09-12 07:50:09 +02:00
|
|
|
if (fd == INVALID_SOCKET) {
|
|
|
|
#ifdef _WIN32
|
|
|
|
error = WSAGetLastError();
|
|
|
|
#else /* _WIN32 */
|
|
|
|
error = errno;
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
func = "socket";
|
|
|
|
|
2012-04-27 09:54:07 +02:00
|
|
|
continue;
|
2013-09-12 07:50:09 +02:00
|
|
|
}
|
2012-04-26 16:45:00 +02:00
|
|
|
|
2012-04-27 09:54:07 +02:00
|
|
|
const int optFalse = 0;
|
2012-08-08 08:34:15 +02:00
|
|
|
setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, reinterpret_cast<const 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;
|
2013-07-12 11:03:36 +02:00
|
|
|
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<const char *>(&optTrue), sizeof(optTrue));
|
2012-04-26 16:45:00 +02:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
2012-06-15 19:32:41 +02:00
|
|
|
int rc = bind(fd, info->ai_addr, info->ai_addrlen);
|
2012-04-26 16:45:00 +02:00
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
if (rc < 0) {
|
2013-09-12 07:50:09 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
error = WSAGetLastError();
|
|
|
|
#else /* _WIN32 */
|
|
|
|
error = errno;
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
func = "bind";
|
|
|
|
|
2012-05-08 09:44:58 +02:00
|
|
|
closesocket(fd);
|
|
|
|
|
2012-04-27 09:54:07 +02:00
|
|
|
continue;
|
2012-05-08 09:44:58 +02:00
|
|
|
}
|
2012-04-27 09:54:07 +02:00
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
SetFD(fd);
|
|
|
|
|
2012-04-27 09:54:07 +02:00
|
|
|
break;
|
2012-04-26 16:45:00 +02:00
|
|
|
}
|
|
|
|
|
2012-04-27 09:54:07 +02:00
|
|
|
freeaddrinfo(result);
|
2012-05-26 20:00:03 +02:00
|
|
|
|
2013-09-12 07:50:09 +02:00
|
|
|
if (GetFD() == INVALID_SOCKET) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "TcpSocket")
|
|
|
|
<< "Invalid socket: " << Utility::FormatErrorNumber(error);
|
2014-06-05 14:08:01 +02:00
|
|
|
|
2013-09-12 07:50:09 +02:00
|
|
|
#ifndef _WIN32
|
|
|
|
BOOST_THROW_EXCEPTION(socket_error()
|
|
|
|
<< boost::errinfo_api_function(func)
|
|
|
|
<< boost::errinfo_errno(error));
|
|
|
|
#else /* _WIN32 */
|
|
|
|
BOOST_THROW_EXCEPTION(socket_error()
|
|
|
|
<< boost::errinfo_api_function(func)
|
|
|
|
<< errinfo_win32_error(error));
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
}
|
2012-04-26 16:45:00 +02:00
|
|
|
}
|
2012-11-22 12:04:32 +01: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;
|
2013-09-12 07:50:09 +02:00
|
|
|
int error;
|
|
|
|
const char *func;
|
2012-11-22 12:04:32 +01: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);
|
|
|
|
|
2013-09-12 07:50:09 +02:00
|
|
|
if (rc != 0) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "TcpSocket")
|
|
|
|
<< "getaddrinfo() failed with error code " << rc << ", \"" << gai_strerror(rc) << "\"";
|
2014-06-05 14:08:01 +02:00
|
|
|
|
2013-03-11 13:45:08 +01:00
|
|
|
BOOST_THROW_EXCEPTION(socket_error()
|
2013-03-18 17:04:22 +01:00
|
|
|
<< boost::errinfo_api_function("getaddrinfo")
|
2013-09-12 07:50:09 +02:00
|
|
|
<< errinfo_getaddrinfo_error(rc));
|
2013-03-11 13:45:08 +01:00
|
|
|
}
|
2012-11-22 12:04:32 +01:00
|
|
|
|
|
|
|
int fd = INVALID_SOCKET;
|
|
|
|
|
|
|
|
for (addrinfo *info = result; info != NULL; info = info->ai_next) {
|
|
|
|
fd = socket(info->ai_family, info->ai_socktype, info->ai_protocol);
|
|
|
|
|
2013-09-12 07:50:09 +02:00
|
|
|
if (fd == INVALID_SOCKET) {
|
|
|
|
#ifdef _WIN32
|
|
|
|
error = WSAGetLastError();
|
|
|
|
#else /* _WIN32 */
|
|
|
|
error = errno;
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
func = "socket";
|
|
|
|
|
2012-11-22 12:04:32 +01:00
|
|
|
continue;
|
2013-09-12 07:50:09 +02:00
|
|
|
}
|
2012-11-22 12:04:32 +01:00
|
|
|
|
|
|
|
rc = connect(fd, info->ai_addr, info->ai_addrlen);
|
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
if (rc < 0) {
|
2013-09-12 07:50:09 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
error = WSAGetLastError();
|
|
|
|
#else /* _WIN32 */
|
|
|
|
error = errno;
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
func = "connect";
|
|
|
|
|
2012-11-22 12:04:32 +01:00
|
|
|
closesocket(fd);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-04-04 16:08:02 +02:00
|
|
|
SetFD(fd);
|
2012-11-22 12:04:32 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
freeaddrinfo(result);
|
|
|
|
|
2013-09-12 07:50:09 +02:00
|
|
|
if (GetFD() == INVALID_SOCKET) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "TcpSocket")
|
|
|
|
<< "Invalid socket: " << Utility::FormatErrorNumber(error);
|
2014-06-05 14:08:01 +02:00
|
|
|
|
2013-09-12 07:50:09 +02:00
|
|
|
#ifndef _WIN32
|
|
|
|
BOOST_THROW_EXCEPTION(socket_error()
|
|
|
|
<< boost::errinfo_api_function(func)
|
|
|
|
<< boost::errinfo_errno(error));
|
|
|
|
#else /* _WIN32 */
|
|
|
|
BOOST_THROW_EXCEPTION(socket_error()
|
|
|
|
<< boost::errinfo_api_function(func)
|
|
|
|
<< errinfo_win32_error(error));
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
}
|
2012-11-22 12:04:32 +01:00
|
|
|
}
|