1 /******************************************************************************
3 * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
22 using namespace icinga;
25 * Creates a socket and binds it to the specified service.
27 * @param service The service.
28 * @param family The address family for the socket.
30 void TcpSocket::Bind(String service, int family)
32 Bind(String(), service, family);
36 * Creates a socket and binds it to the specified node and service.
38 * @param node The node.
39 * @param service The service.
40 * @param family The address family for the socket.
42 void TcpSocket::Bind(String node, String service, int family)
47 memset(&hints, 0, sizeof(hints));
48 hints.ai_family = family;
49 hints.ai_socktype = SOCK_STREAM;
50 hints.ai_protocol = IPPROTO_TCP;
51 hints.ai_flags = AI_PASSIVE;
53 if (getaddrinfo(node.IsEmpty() ? NULL : node.CStr(),
54 service.CStr(), &hints, &result) < 0)
55 throw_exception(SocketException("getaddrinfo() failed", GetLastSocketError()));
57 int fd = INVALID_SOCKET;
59 for (addrinfo *info = result; info != NULL; info = info->ai_next) {
60 fd = socket(info->ai_family, info->ai_socktype, info->ai_protocol);
62 if (fd == INVALID_SOCKET)
67 const int optFalse = 0;
68 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, reinterpret_cast<const char *>(&optFalse), sizeof(optFalse));
71 const int optTrue = 1;
72 setsockopt(GetFD(), SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<const char *>(&optTrue), sizeof(optTrue));
75 int rc = bind(fd, info->ai_addr, info->ai_addrlen);
78 if (rc < 0 && WSAGetLastError() != WSAEWOULDBLOCK) {
80 if (rc < 0 && errno != EINPROGRESS) {
83 SetFD(INVALID_SOCKET);
93 if (fd == INVALID_SOCKET)
94 throw_exception(runtime_error("Could not create a suitable socket."));
98 * Creates a socket and connects to the specified node and service.
100 * @param node The node.
101 * @param service The service.
103 void TcpSocket::Connect(const String& node, const String& service)
108 memset(&hints, 0, sizeof(hints));
109 hints.ai_family = AF_UNSPEC;
110 hints.ai_socktype = SOCK_STREAM;
111 hints.ai_protocol = IPPROTO_TCP;
113 int rc = getaddrinfo(node.CStr(), service.CStr(), &hints, &result);
116 throw_exception(SocketException("getaddrinfo() failed", GetLastSocketError()));
118 int fd = INVALID_SOCKET;
120 for (addrinfo *info = result; info != NULL; info = info->ai_next) {
121 fd = socket(info->ai_family, info->ai_socktype, info->ai_protocol);
123 if (fd == INVALID_SOCKET)
128 rc = connect(fd, info->ai_addr, info->ai_addrlen);
131 if (rc < 0 && WSAGetLastError() != WSAEWOULDBLOCK) {
133 if (rc < 0 && errno != EINPROGRESS) {
136 SetFD(INVALID_SOCKET);
143 OnConnected(GetSelf());
149 freeaddrinfo(result);
151 if (fd == INVALID_SOCKET)
152 throw_exception(runtime_error("Could not create a suitable socket."));