1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "base/tcpsocket.hpp"
4 #include "base/logger.hpp"
5 #include "base/utility.hpp"
6 #include "base/exception.hpp"
7 #include <boost/exception/errinfo_api_function.hpp>
8 #include <boost/exception/errinfo_errno.hpp>
11 using namespace icinga;
14 * Creates a socket and binds it to the specified service.
16 * @param service The service.
17 * @param family The address family for the socket.
19 void TcpSocket::Bind(const String& service, int family)
21 Bind(String(), service, family);
25 * Creates a socket and binds it to the specified node and service.
27 * @param node The node.
28 * @param service The service.
29 * @param family The address family for the socket.
31 void TcpSocket::Bind(const String& node, const String& service, int family)
38 memset(&hints, 0, sizeof(hints));
39 hints.ai_family = family;
40 hints.ai_socktype = SOCK_STREAM;
41 hints.ai_protocol = IPPROTO_TCP;
42 hints.ai_flags = AI_PASSIVE;
44 int rc = getaddrinfo(node.IsEmpty() ? nullptr : node.CStr(),
45 service.CStr(), &hints, &result);
48 Log(LogCritical, "TcpSocket")
49 << "getaddrinfo() failed with error code " << rc << ", \"" << gai_strerror(rc) << "\"";
51 BOOST_THROW_EXCEPTION(socket_error()
52 << boost::errinfo_api_function("getaddrinfo")
53 << errinfo_getaddrinfo_error(rc));
56 int fd = INVALID_SOCKET;
58 for (addrinfo *info = result; info != nullptr; info = info->ai_next) {
59 fd = socket(info->ai_family, info->ai_socktype, info->ai_protocol);
61 if (fd == INVALID_SOCKET) {
63 error = WSAGetLastError();
72 const int optFalse = 0;
73 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, reinterpret_cast<const char *>(&optFalse), sizeof(optFalse));
75 const int optTrue = 1;
76 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<const char *>(&optTrue), sizeof(optTrue));
78 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, reinterpret_cast<const char *>(&optTrue), sizeof(optTrue));
79 #endif /* SO_REUSEPORT */
81 int rc = bind(fd, info->ai_addr, info->ai_addrlen);
85 error = WSAGetLastError();
101 freeaddrinfo(result);
103 if (GetFD() == INVALID_SOCKET) {
104 Log(LogCritical, "TcpSocket")
105 << "Invalid socket: " << Utility::FormatErrorNumber(error);
108 BOOST_THROW_EXCEPTION(socket_error()
109 << boost::errinfo_api_function(func)
110 << boost::errinfo_errno(error));
112 BOOST_THROW_EXCEPTION(socket_error()
113 << boost::errinfo_api_function(func)
114 << errinfo_win32_error(error));
120 * Creates a socket and connects to the specified node and service.
122 * @param node The node.
123 * @param service The service.
125 void TcpSocket::Connect(const String& node, const String& service)
132 memset(&hints, 0, sizeof(hints));
133 hints.ai_family = AF_UNSPEC;
134 hints.ai_socktype = SOCK_STREAM;
135 hints.ai_protocol = IPPROTO_TCP;
137 int rc = getaddrinfo(node.CStr(), service.CStr(), &hints, &result);
140 Log(LogCritical, "TcpSocket")
141 << "getaddrinfo() failed with error code " << rc << ", \"" << gai_strerror(rc) << "\"";
143 BOOST_THROW_EXCEPTION(socket_error()
144 << boost::errinfo_api_function("getaddrinfo")
145 << errinfo_getaddrinfo_error(rc));
148 SOCKET fd = INVALID_SOCKET;
150 for (addrinfo *info = result; info != nullptr; info = info->ai_next) {
151 fd = socket(info->ai_family, info->ai_socktype, info->ai_protocol);
153 if (fd == INVALID_SOCKET) {
155 error = WSAGetLastError();
164 const int optTrue = 1;
165 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, reinterpret_cast<const char *>(&optTrue), sizeof(optTrue)) != 0) {
167 error = WSAGetLastError();
171 Log(LogWarning, "TcpSocket")
172 << "setsockopt() unable to enable TCP keep-alives with error code " << rc;
175 rc = connect(fd, info->ai_addr, info->ai_addrlen);
179 error = WSAGetLastError();
195 freeaddrinfo(result);
197 if (GetFD() == INVALID_SOCKET) {
198 Log(LogCritical, "TcpSocket")
199 << "Invalid socket: " << Utility::FormatErrorNumber(error);
202 BOOST_THROW_EXCEPTION(socket_error()
203 << boost::errinfo_api_function(func)
204 << boost::errinfo_errno(error));
206 BOOST_THROW_EXCEPTION(socket_error()
207 << boost::errinfo_api_function(func)
208 << errinfo_win32_error(error));