]> granicus.if.org Git - icinga2/blob - lib/base/tcpsocket.cpp
c42499211e456f77b062e005b03f7503a9b23548
[icinga2] / lib / base / tcpsocket.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/)        *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
20 #include "i2-base.h"
21
22 using namespace icinga;
23
24 /**
25  * Creates a socket and binds it to the specified service.
26  *
27  * @param service The service.
28  * @param family The address family for the socket.
29  */
30 void TcpSocket::Bind(String service, int family)
31 {
32         Bind(String(), service, family);
33 }
34
35 /**
36  * Creates a socket and binds it to the specified node and service.
37  *
38  * @param node The node.
39  * @param service The service.
40  * @param family The address family for the socket.
41  */
42 void TcpSocket::Bind(String node, String service, int family)
43 {
44         addrinfo hints;
45         addrinfo *result;
46
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;
52
53         if (getaddrinfo(node.IsEmpty() ? NULL : node.CStr(),
54             service.CStr(), &hints, &result) < 0)
55                 throw_exception(SocketException("getaddrinfo() failed", GetLastSocketError()));
56
57         int fd = INVALID_SOCKET;
58
59         for (addrinfo *info = result; info != NULL; info = info->ai_next) {
60                 fd = socket(info->ai_family, info->ai_socktype, info->ai_protocol);
61
62                 if (fd == INVALID_SOCKET)
63                         continue;
64
65                 SetFD(fd);
66
67                 const int optFalse = 0;
68                 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, reinterpret_cast<const char *>(&optFalse), sizeof(optFalse));
69
70 #ifndef _WIN32
71                 const int optTrue = 1;
72                 setsockopt(GetFD(), SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<const char *>(&optTrue), sizeof(optTrue));
73 #endif /* _WIN32 */
74
75                 int rc = bind(fd, info->ai_addr, info->ai_addrlen);
76
77 #ifdef _WIN32
78                 if (rc < 0 && WSAGetLastError() != WSAEWOULDBLOCK) {
79 #else /* _WIN32 */
80                 if (rc < 0 && errno != EINPROGRESS) {
81 #endif /* _WIN32 */
82                         closesocket(fd);
83                         SetFD(INVALID_SOCKET);
84
85                         continue;
86                 }
87
88                 break;
89         }
90
91         freeaddrinfo(result);
92
93         if (fd == INVALID_SOCKET)
94                 throw_exception(runtime_error("Could not create a suitable socket."));
95 }
96
97 /**
98  * Creates a socket and connects to the specified node and service.
99  *
100  * @param node The node.
101  * @param service The service.
102  */
103 void TcpSocket::Connect(const String& node, const String& service)
104 {
105         addrinfo hints;
106         addrinfo *result;
107
108         memset(&hints, 0, sizeof(hints));
109         hints.ai_family = AF_UNSPEC;
110         hints.ai_socktype = SOCK_STREAM;
111         hints.ai_protocol = IPPROTO_TCP;
112
113         int rc = getaddrinfo(node.CStr(), service.CStr(), &hints, &result);
114
115         if (rc < 0)
116                 throw_exception(SocketException("getaddrinfo() failed", GetLastSocketError()));
117
118         int fd = INVALID_SOCKET;
119
120         for (addrinfo *info = result; info != NULL; info = info->ai_next) {
121                 fd = socket(info->ai_family, info->ai_socktype, info->ai_protocol);
122
123                 if (fd == INVALID_SOCKET)
124                         continue;
125
126                 SetFD(fd);
127
128                 rc = connect(fd, info->ai_addr, info->ai_addrlen);
129
130 #ifdef _WIN32
131                 if (rc < 0 && WSAGetLastError() != WSAEWOULDBLOCK) {
132 #else /* _WIN32 */
133                 if (rc < 0 && errno != EINPROGRESS) {
134 #endif /* _WIN32 */
135                         closesocket(fd);
136                         SetFD(INVALID_SOCKET);
137
138                         continue;
139                 }
140
141                 if (rc >= 0) {
142                         SetConnected(true);
143                         OnConnected(GetSelf());
144                 }
145
146                 break;
147         }
148
149         freeaddrinfo(result);
150
151         if (fd == INVALID_SOCKET)
152                 throw_exception(runtime_error("Could not create a suitable socket."));
153 }