blob: 9ac1478ca8c27cf00a648f8a9ca948cd1fcf536c [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
Adam Tkac8aee1a82009-09-04 12:08:56 +000019#ifdef HAVE_CONFIG_H
20#include <config.h>
Adam Tkacad1cbd92008-10-06 14:08:00 +000021#endif
22
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000023#ifdef WIN32
24//#include <io.h>
25#include <winsock2.h>
Pierre Ossman0153e232011-03-08 13:05:27 +000026#include <ws2tcpip.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000027#define errorNumber WSAGetLastError()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000028#else
29#define errorNumber errno
30#define closesocket close
31#include <sys/types.h>
32#include <sys/socket.h>
33#include <arpa/inet.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000034#include <netinet/tcp.h>
35#include <netdb.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000036#include <errno.h>
37#include <string.h>
38#include <signal.h>
39#include <fcntl.h>
40#endif
41
Adam Tkac04b7fd22008-03-19 16:14:48 +000042#include <stdlib.h>
Tim Waugh892d10a2015-03-11 13:12:07 +000043#include <unistd.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000044#include <network/TcpSocket.h>
45#include <rfb/util.h>
46#include <rfb/LogWriter.h>
Pierre Ossman39b3b8f2015-01-29 17:35:45 +010047#include <rfb/Configuration.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000048
Pierre Ossman2f744172015-03-17 13:38:21 +010049#ifdef WIN32
50#include <os/winerrno.h>
51#endif
52
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000053#ifndef INADDR_NONE
54#define INADDR_NONE ((unsigned long)-1)
55#endif
56#ifndef INADDR_LOOPBACK
57#define INADDR_LOOPBACK ((unsigned long)0x7F000001)
58#endif
59
Pierre Ossmancfb21162015-03-17 14:02:11 +010060#ifndef IN6_ARE_ADDR_EQUAL
Pierre Ossman8b6aa202012-12-13 13:55:22 +000061#define IN6_ARE_ADDR_EQUAL(a,b) \
62 (memcmp ((const void*)(a), (const void*)(b), sizeof (struct in6_addr)) == 0)
63#endif
64
Pierre Ossmana6570c52015-03-17 13:38:59 +010065// Missing on older Windows and OS X
66#ifndef AI_NUMERICSERV
67#define AI_NUMERICSERV 0
68#endif
69
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000070using namespace network;
71using namespace rdr;
72
73static rfb::LogWriter vlog("TcpSocket");
74
Pierre Ossman39b3b8f2015-01-29 17:35:45 +010075static rfb::BoolParameter UseIPv4("UseIPv4", "Use IPv4 for incoming and outgoing connections.", true);
Pierre Ossman39b3b8f2015-01-29 17:35:45 +010076static rfb::BoolParameter UseIPv6("UseIPv6", "Use IPv6 for incoming and outgoing connections.", true);
Pierre Ossman39b3b8f2015-01-29 17:35:45 +010077
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000078/* Tunnelling support. */
79int network::findFreeTcpPort (void)
80{
DRCb2618e52011-02-21 13:43:44 +000081 int sock;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000082 struct sockaddr_in addr;
83 memset(&addr, 0, sizeof(addr));
84 addr.sin_family = AF_INET;
85 addr.sin_addr.s_addr = INADDR_ANY;
86
87 if ((sock = socket (AF_INET, SOCK_STREAM, 0)) < 0)
88 throw SocketException ("unable to create socket", errorNumber);
89
DRCb2618e52011-02-21 13:43:44 +000090 addr.sin_port = 0;
91 if (bind (sock, (struct sockaddr *)&addr, sizeof (addr)) < 0)
92 throw SocketException ("unable to find free port", errorNumber);
93
94 socklen_t n = sizeof(addr);
95 if (getsockname (sock, (struct sockaddr *)&addr, &n) < 0)
96 throw SocketException ("unable to get port number", errorNumber);
97
98 closesocket (sock);
99 return ntohs(addr.sin_port);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000100}
101
102
103// -=- Socket initialisation
104static bool socketsInitialised = false;
105static void initSockets() {
106 if (socketsInitialised)
107 return;
108#ifdef WIN32
109 WORD requiredVersion = MAKEWORD(2,0);
110 WSADATA initResult;
111
112 if (WSAStartup(requiredVersion, &initResult) != 0)
113 throw SocketException("unable to initialise Winsock2", errorNumber);
114#else
115 signal(SIGPIPE, SIG_IGN);
116#endif
117 socketsInitialised = true;
118}
119
120
Pierre Ossman3ab5db42015-03-17 17:06:22 +0100121// -=- Socket duplication help for Windows
122static int dupsocket(int fd)
123{
124#ifdef WIN32
125 int ret;
126 WSAPROTOCOL_INFO info;
127 ret = WSADuplicateSocket(fd, GetCurrentProcessId(), &info);
128 if (ret != 0)
129 throw SocketException("unable to duplicate socket", errorNumber);
130 return WSASocket(info.iAddressFamily, info.iSocketType, info.iProtocol,
131 &info, 0, 0);
132#else
133 return dup(fd);
134#endif
135}
136
137
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000138// -=- TcpSocket
139
140TcpSocket::TcpSocket(int sock, bool close)
141 : Socket(new FdInStream(sock), new FdOutStream(sock), true), closeFd(close)
142{
143}
144
145TcpSocket::TcpSocket(const char *host, int port)
146 : closeFd(true)
147{
Pierre Ossman5a126662015-07-30 12:24:36 +0200148 int sock, err, result;
Adam Tkac9cb6a422008-11-14 15:56:15 +0000149 struct addrinfo *ai, *current, hints;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000150
151 // - Create a socket
152 initSockets();
Adam Tkac9cb6a422008-11-14 15:56:15 +0000153
Adam Tkac9cb6a422008-11-14 15:56:15 +0000154 memset(&hints, 0, sizeof(struct addrinfo));
155 hints.ai_family = AF_UNSPEC;
156 hints.ai_socktype = SOCK_STREAM;
157 hints.ai_canonname = NULL;
158 hints.ai_addr = NULL;
159 hints.ai_next = NULL;
160
161 if ((result = getaddrinfo(host, NULL, &hints, &ai)) != 0) {
162 throw Exception("unable to resolve host by name: %s",
Tim Waugha85363d2015-03-11 13:07:48 +0000163 gai_strerror(result));
Adam Tkac9cb6a422008-11-14 15:56:15 +0000164 }
165
Pierre Ossmanf1a35012015-03-03 16:02:42 +0100166 sock = -1;
Pierre Ossman5a126662015-07-30 12:24:36 +0200167 err = 0;
Adam Tkac9cb6a422008-11-14 15:56:15 +0000168 for (current = ai; current != NULL; current = current->ai_next) {
Pierre Ossman5a126662015-07-30 12:24:36 +0200169 int family;
170 vnc_sockaddr_t sa;
171 socklen_t salen;
Pierre Ossmanb7e55742015-07-30 12:25:52 +0200172 char ntop[NI_MAXHOST];
Pierre Ossman5a126662015-07-30 12:24:36 +0200173
Adam Tkac9cb6a422008-11-14 15:56:15 +0000174 family = current->ai_family;
Pierre Ossman39b3b8f2015-01-29 17:35:45 +0100175
176 switch (family) {
177 case AF_INET:
178 if (!UseIPv4)
179 continue;
180 break;
181 case AF_INET6:
182 if (!UseIPv6)
183 continue;
184 break;
185 default:
Adam Tkac9cb6a422008-11-14 15:56:15 +0000186 continue;
Pierre Ossman39b3b8f2015-01-29 17:35:45 +0100187 }
Adam Tkac9cb6a422008-11-14 15:56:15 +0000188
189 salen = current->ai_addrlen;
190 memcpy(&sa, current->ai_addr, salen);
191
192 if (family == AF_INET)
193 sa.u.sin.sin_port = htons(port);
194 else
195 sa.u.sin6.sin6_port = htons(port);
196
Pierre Ossmanb7e55742015-07-30 12:25:52 +0200197 getnameinfo(&sa.u.sa, salen, ntop, sizeof(ntop), NULL, 0, NI_NUMERICHOST);
198 vlog.debug("Connecting to %s [%s] port %d", host, ntop, port);
199
Adam Tkac9cb6a422008-11-14 15:56:15 +0000200 sock = socket (family, SOCK_STREAM, 0);
201 if (sock == -1) {
202 err = errorNumber;
Adam Tkac9cb6a422008-11-14 15:56:15 +0000203 freeaddrinfo(ai);
Adam Tkac9cb6a422008-11-14 15:56:15 +0000204 throw SocketException("unable to create socket", err);
205 }
206
207 /* Attempt to connect to the remote host */
Adam Tkacc9cda3b2009-10-30 11:13:34 +0000208 while ((result = connect(sock, &sa.u.sa, salen)) == -1) {
Adam Tkac9cb6a422008-11-14 15:56:15 +0000209 err = errorNumber;
210#ifndef WIN32
211 if (err == EINTR)
Tim Waugha85363d2015-03-11 13:07:48 +0000212 continue;
Adam Tkac9cb6a422008-11-14 15:56:15 +0000213#endif
Pierre Ossmanb7e55742015-07-30 12:25:52 +0200214 vlog.debug("Failed to connect to address %s port %d: %d",
215 ntop, port, err);
Adam Tkac9cb6a422008-11-14 15:56:15 +0000216 closesocket(sock);
Pierre Ossman5a126662015-07-30 12:24:36 +0200217 sock = -1;
Adam Tkac9cb6a422008-11-14 15:56:15 +0000218 break;
219 }
220
Adam Tkac9cb6a422008-11-14 15:56:15 +0000221 if (result == 0)
222 break;
Adam Tkac9cb6a422008-11-14 15:56:15 +0000223 }
224
225 freeaddrinfo(ai);
Pierre Ossmanda9a38d2015-03-03 16:03:32 +0100226
Pierre Ossman5a126662015-07-30 12:24:36 +0200227 if (sock == -1) {
228 if (err == 0)
229 throw Exception("No useful address for host");
230 else
231 throw SocketException("unable connect to socket", err);
232 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000233
234#ifndef WIN32
235 // - By default, close the socket on exec()
236 fcntl(sock, F_SETFD, FD_CLOEXEC);
237#endif
238
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000239 // Disable Nagle's algorithm, to reduce latency
240 enableNagles(sock, false);
241
242 // Create the input and output streams
243 instream = new FdInStream(sock);
244 outstream = new FdOutStream(sock);
245 ownStreams = true;
246}
247
248TcpSocket::~TcpSocket() {
249 if (closeFd)
250 closesocket(getFd());
251}
252
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000253int TcpSocket::getMyPort() {
254 return getSockPort(getFd());
255}
256
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000257char* TcpSocket::getPeerAddress() {
Tim Waugh6ae42df2014-11-17 17:07:07 +0000258 vnc_sockaddr_t sa;
259 socklen_t sa_size = sizeof(sa);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000260
Tim Waugh6ae42df2014-11-17 17:07:07 +0000261 if (getpeername(getFd(), &sa.u.sa, &sa_size) != 0) {
262 vlog.error("unable to get peer name for socket");
263 return rfb::strDup("");
264 }
265
Pierre Ossman14263e12014-11-19 11:14:49 +0100266 if (sa.u.sa.sa_family == AF_INET6) {
Pierre Ossman07cd2292014-11-19 11:16:04 +0100267 char buffer[INET6_ADDRSTRLEN + 2];
Tim Waugh892d10a2015-03-11 13:12:07 +0000268 int ret;
Tim Waugh6ae42df2014-11-17 17:07:07 +0000269
Pierre Ossman07cd2292014-11-19 11:16:04 +0100270 buffer[0] = '[';
271
Tim Waugh892d10a2015-03-11 13:12:07 +0000272 ret = getnameinfo(&sa.u.sa, sizeof(sa.u.sin6),
273 buffer + 1, sizeof(buffer) - 2, NULL, 0,
274 NI_NUMERICHOST);
275 if (ret != 0) {
Pierre Ossman14263e12014-11-19 11:14:49 +0100276 vlog.error("unable to convert peer name to a string");
277 return rfb::strDup("");
278 }
Tim Waugh6ae42df2014-11-17 17:07:07 +0000279
Pierre Ossman07cd2292014-11-19 11:16:04 +0100280 strcat(buffer, "]");
281
282 return rfb::strDup(buffer);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000283 }
Pierre Ossman14263e12014-11-19 11:14:49 +0100284
285 if (sa.u.sa.sa_family == AF_INET) {
286 char *name;
287
288 name = inet_ntoa(sa.u.sin.sin_addr);
289 if (name == NULL) {
290 vlog.error("unable to convert peer name to a string");
291 return rfb::strDup("");
292 }
293
294 return rfb::strDup(name);
295 }
296
297 vlog.error("unknown address family for socket");
298 return rfb::strDup("");
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000299}
300
301int TcpSocket::getPeerPort() {
Tim Waugh6ae42df2014-11-17 17:07:07 +0000302 vnc_sockaddr_t sa;
303 socklen_t sa_size = sizeof(sa);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000304
Tim Waugh6ae42df2014-11-17 17:07:07 +0000305 getpeername(getFd(), &sa.u.sa, &sa_size);
306
307 switch (sa.u.sa.sa_family) {
Tim Waugh6ae42df2014-11-17 17:07:07 +0000308 case AF_INET6:
309 return ntohs(sa.u.sin6.sin6_port);
Pierre Ossman14263e12014-11-19 11:14:49 +0100310 case AF_INET:
Tim Waugh6ae42df2014-11-17 17:07:07 +0000311 return ntohs(sa.u.sin.sin_port);
Pierre Ossman14263e12014-11-19 11:14:49 +0100312 default:
313 return 0;
Tim Waugh6ae42df2014-11-17 17:07:07 +0000314 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000315}
316
317char* TcpSocket::getPeerEndpoint() {
318 rfb::CharArray address; address.buf = getPeerAddress();
319 int port = getPeerPort();
320
321 int buflen = strlen(address.buf) + 32;
322 char* buffer = new char[buflen];
323 sprintf(buffer, "%s::%d", address.buf, port);
324 return buffer;
325}
326
327bool TcpSocket::sameMachine() {
Adam Tkac897814f2009-11-12 10:32:43 +0000328 vnc_sockaddr_t peeraddr, myaddr;
329 socklen_t addrlen;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000330
Adam Tkac897814f2009-11-12 10:32:43 +0000331 addrlen = sizeof(peeraddr);
332 if (getpeername(getFd(), &peeraddr.u.sa, &addrlen) < 0)
333 throw SocketException ("unable to get peer address", errorNumber);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000334
Adam Tkac897814f2009-11-12 10:32:43 +0000335 addrlen = sizeof(myaddr); /* need to reset, since getpeername overwrote */
336 if (getsockname(getFd(), &myaddr.u.sa, &addrlen) < 0)
337 throw SocketException ("unable to get my address", errorNumber);
338
339 if (peeraddr.u.sa.sa_family != myaddr.u.sa.sa_family)
340 return false;
341
Adam Tkac897814f2009-11-12 10:32:43 +0000342 if (peeraddr.u.sa.sa_family == AF_INET6)
343 return IN6_ARE_ADDR_EQUAL(&peeraddr.u.sin6.sin6_addr,
Tim Waugha85363d2015-03-11 13:07:48 +0000344 &myaddr.u.sin6.sin6_addr);
Pierre Ossman14263e12014-11-19 11:14:49 +0100345 if (peeraddr.u.sa.sa_family == AF_INET)
346 return (peeraddr.u.sin.sin_addr.s_addr == myaddr.u.sin.sin_addr.s_addr);
347
348 // No idea what this is. Assume we're on different machines.
349 return false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000350}
351
352void TcpSocket::shutdown()
353{
354 Socket::shutdown();
355 ::shutdown(getFd(), 2);
356}
357
358bool TcpSocket::enableNagles(int sock, bool enable) {
359 int one = enable ? 0 : 1;
360 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
Tim Waugha85363d2015-03-11 13:07:48 +0000361 (char *)&one, sizeof(one)) < 0) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000362 int e = errorNumber;
363 vlog.error("unable to setsockopt TCP_NODELAY: %d", e);
364 return false;
365 }
366 return true;
367}
368
Pierre Ossman64069a92011-11-08 12:10:55 +0000369bool TcpSocket::cork(int sock, bool enable) {
370#ifndef TCP_CORK
371 return false;
372#else
373 int one = enable ? 1 : 0;
374 if (setsockopt(sock, IPPROTO_TCP, TCP_CORK, (char *)&one, sizeof(one)) < 0)
375 return false;
376 return true;
377#endif
378}
379
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000380bool TcpSocket::isSocket(int sock)
381{
Tim Waugh6ae42df2014-11-17 17:07:07 +0000382 vnc_sockaddr_t sa;
383 socklen_t sa_size = sizeof(sa);
384 return getsockname(sock, &sa.u.sa, &sa_size) >= 0;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000385}
386
387bool TcpSocket::isConnected(int sock)
388{
Tim Waugh6ae42df2014-11-17 17:07:07 +0000389 vnc_sockaddr_t sa;
390 socklen_t sa_size = sizeof(sa);
391 return getpeername(sock, &sa.u.sa, &sa_size) >= 0;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000392}
393
394int TcpSocket::getSockPort(int sock)
395{
Tim Waugh6ae42df2014-11-17 17:07:07 +0000396 vnc_sockaddr_t sa;
397 socklen_t sa_size = sizeof(sa);
398 if (getsockname(sock, &sa.u.sa, &sa_size) < 0)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000399 return 0;
Tim Waugh6ae42df2014-11-17 17:07:07 +0000400
401 switch (sa.u.sa.sa_family) {
Tim Waugh6ae42df2014-11-17 17:07:07 +0000402 case AF_INET6:
403 return ntohs(sa.u.sin6.sin6_port);
Tim Waugh6ae42df2014-11-17 17:07:07 +0000404 default:
405 return ntohs(sa.u.sin.sin_port);
406 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000407}
408
Tim Waugh892d10a2015-03-11 13:12:07 +0000409TcpListener::TcpListener(int sock)
Tim Waughe4d97262014-11-21 16:07:34 +0000410{
Tim Waugh892d10a2015-03-11 13:12:07 +0000411 fd = sock;
Tim Waughe4d97262014-11-21 16:07:34 +0000412}
413
Tim Waugh892d10a2015-03-11 13:12:07 +0000414TcpListener::TcpListener(const TcpListener& other)
Tim Waughe4d97262014-11-21 16:07:34 +0000415{
Pierre Ossman3ab5db42015-03-17 17:06:22 +0100416 fd = dupsocket (other.fd);
Tim Waugh892d10a2015-03-11 13:12:07 +0000417 // Hope TcpListener::shutdown(other) doesn't get called...
Tim Waughe4d97262014-11-21 16:07:34 +0000418}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000419
Tim Waugh892d10a2015-03-11 13:12:07 +0000420TcpListener& TcpListener::operator= (const TcpListener& other)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000421{
Tim Waugh892d10a2015-03-11 13:12:07 +0000422 if (this != &other)
423 {
424 closesocket (fd);
Pierre Ossman3ab5db42015-03-17 17:06:22 +0100425 fd = dupsocket (other.fd);
Tim Waugh892d10a2015-03-11 13:12:07 +0000426 // Hope TcpListener::shutdown(other) doesn't get called...
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000427 }
Tim Waugh892d10a2015-03-11 13:12:07 +0000428 return *this;
429}
430
431TcpListener::TcpListener(const struct sockaddr *listenaddr,
432 socklen_t listenaddrlen)
433{
434 int one = 1;
435 vnc_sockaddr_t sa;
436 int sock;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000437
438 initSockets();
Tim Waugh892d10a2015-03-11 13:12:07 +0000439
440 if ((sock = socket (listenaddr->sa_family, SOCK_STREAM, 0)) < 0)
441 throw SocketException("unable to create listening socket", errorNumber);
442
443 memcpy (&sa, listenaddr, listenaddrlen);
444#ifdef IPV6_V6ONLY
445 if (listenaddr->sa_family == AF_INET6) {
Pierre Ossman467df2a2015-09-29 09:42:03 +0200446 if (setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&one, sizeof(one))) {
447 int e = errorNumber;
448 closesocket(sock);
449 throw SocketException("unable to set IPV6_V6ONLY", e);
450 }
Tim Waugh892d10a2015-03-11 13:12:07 +0000451 }
452#endif /* defined(IPV6_V6ONLY) */
453
Pierre Ossman056c1532015-04-23 11:30:59 +0200454#ifdef FD_CLOEXEC
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000455 // - By default, close the socket on exec()
Tim Waugh892d10a2015-03-11 13:12:07 +0000456 fcntl(sock, F_SETFD, FD_CLOEXEC);
Pierre Ossman056c1532015-04-23 11:30:59 +0200457#endif
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000458
Pierre Ossman056c1532015-04-23 11:30:59 +0200459 // SO_REUSEADDR is broken on Windows. It allows binding to a port
460 // that already has a listening socket on it. SO_EXCLUSIVEADDRUSE
461 // might do what we want, but requires investigation.
462#ifndef WIN32
Tim Waugh892d10a2015-03-11 13:12:07 +0000463 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
464 (char *)&one, sizeof(one)) < 0) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000465 int e = errorNumber;
Tim Waugh892d10a2015-03-11 13:12:07 +0000466 closesocket(sock);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000467 throw SocketException("unable to create listening socket", e);
468 }
469#endif
470
Pierre Ossmanb6536e22015-04-23 11:23:16 +0200471 if (bind(sock, &sa.u.sa, listenaddrlen) == -1) {
472 closesocket(sock);
473 throw SocketException("failed to bind socket", errorNumber);
474 }
475
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000476 // - Set it to be a listening socket
Tim Waugh892d10a2015-03-11 13:12:07 +0000477 if (listen(sock, 5) < 0) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000478 int e = errorNumber;
Tim Waugh892d10a2015-03-11 13:12:07 +0000479 closesocket(sock);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000480 throw SocketException("unable to set socket to listening mode", e);
481 }
Tim Waugh892d10a2015-03-11 13:12:07 +0000482
483 fd = sock;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000484}
485
486TcpListener::~TcpListener() {
Tim Waugh892d10a2015-03-11 13:12:07 +0000487 closesocket(fd);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000488}
489
490void TcpListener::shutdown()
491{
492#ifdef WIN32
493 closesocket(getFd());
494#else
495 ::shutdown(getFd(), 2);
496#endif
497}
498
499
500Socket*
501TcpListener::accept() {
502 int new_sock = -1;
503
504 // Accept an incoming connection
505 if ((new_sock = ::accept(fd, 0, 0)) < 0)
506 throw SocketException("unable to accept new connection", errorNumber);
507
508#ifndef WIN32
509 // - By default, close the socket on exec()
510 fcntl(new_sock, F_SETFD, FD_CLOEXEC);
511#endif
512
513 // Disable Nagle's algorithm, to reduce latency
514 TcpSocket::enableNagles(new_sock, false);
515
516 // Create the socket object & check connection is allowed
517 TcpSocket* s = new TcpSocket(new_sock);
518 if (filter && !filter->verifyConnection(s)) {
519 delete s;
520 return 0;
521 }
522 return s;
523}
524
Pierre Ossman57cab512015-03-17 13:39:39 +0100525void TcpListener::getMyAddresses(std::list<char*>* result) {
Pierre Ossman57cab512015-03-17 13:39:39 +0100526 struct addrinfo *ai, *current, hints;
527
528 initSockets();
529
530 memset(&hints, 0, sizeof(struct addrinfo));
531 hints.ai_flags = AI_PASSIVE | AI_NUMERICSERV;
532 hints.ai_family = AF_UNSPEC;
533 hints.ai_socktype = SOCK_STREAM;
534 hints.ai_canonname = NULL;
535 hints.ai_addr = NULL;
536 hints.ai_next = NULL;
537
538 // Windows doesn't like NULL for service, so specify something
539 if ((getaddrinfo(NULL, "1", &hints, &ai)) != 0)
540 return;
541
542 for (current= ai; current != NULL; current = current->ai_next) {
543 switch (current->ai_family) {
544 case AF_INET:
545 if (!UseIPv4)
546 continue;
547 break;
548 case AF_INET6:
549 if (!UseIPv6)
550 continue;
551 break;
552 default:
553 continue;
554 }
555
556 char *addr = new char[INET6_ADDRSTRLEN];
557
558 getnameinfo(current->ai_addr, current->ai_addrlen, addr, INET6_ADDRSTRLEN,
559 NULL, 0, NI_NUMERICHOST);
560
561 result->push_back(addr);
562 }
563
564 freeaddrinfo(ai);
Pierre Ossman57cab512015-03-17 13:39:39 +0100565}
566
Tim Waugh892d10a2015-03-11 13:12:07 +0000567int TcpListener::getMyPort() {
568 return TcpSocket::getSockPort(getFd());
569}
570
571
572void network::createLocalTcpListeners(std::list<TcpListener> *listeners,
573 int port)
574{
575 std::list<TcpListener> new_listeners;
576 vnc_sockaddr_t sa;
Pierre Ossman9d784402015-03-17 17:10:10 +0100577
578 initSockets();
579
Tim Waugh892d10a2015-03-11 13:12:07 +0000580 if (UseIPv6) {
581 sa.u.sin6.sin6_family = AF_INET6;
582 sa.u.sin6.sin6_port = htons (port);
583 sa.u.sin6.sin6_addr = in6addr_loopback;
584 try {
585 new_listeners.push_back (TcpListener (&sa.u.sa, sizeof (sa.u.sin6)));
586 } catch (SocketException& e) {
587 // Ignore this if it is due to lack of address family support on
588 // the interface or on the system
589 if (e.err != EADDRNOTAVAIL && e.err != EAFNOSUPPORT)
590 // Otherwise, report the error
591 throw;
592 }
593 }
Tim Waugh892d10a2015-03-11 13:12:07 +0000594 if (UseIPv4) {
595 sa.u.sin.sin_family = AF_INET;
596 sa.u.sin.sin_port = htons (port);
597 sa.u.sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
598 try {
599 new_listeners.push_back (TcpListener (&sa.u.sa, sizeof (sa.u.sin)));
600 } catch (SocketException& e) {
601 // Ignore this if it is due to lack of address family support on
602 // the interface or on the system
603 if (e.err != EADDRNOTAVAIL && e.err != EAFNOSUPPORT)
604 // Otherwise, report the error
605 throw;
606 }
607 }
608
609 if (new_listeners.empty ())
610 throw SocketException("createLocalTcpListeners: no addresses available",
611 EADDRNOTAVAIL);
612
613 listeners->splice (listeners->end(), new_listeners);
614}
615
616void network::createTcpListeners(std::list<TcpListener> *listeners,
617 const char *addr,
618 int port)
619{
620 std::list<TcpListener> new_listeners;
621
Tim Waugh6ae42df2014-11-17 17:07:07 +0000622 struct addrinfo *ai, *current, hints;
Tim Waugh892d10a2015-03-11 13:12:07 +0000623 char service[16];
Pierre Ossmanf7d15002015-03-17 17:10:56 +0100624 int result;
Tim Waugh6ae42df2014-11-17 17:07:07 +0000625
Pierre Ossman9d784402015-03-17 17:10:10 +0100626 initSockets();
627
Tim Waugh6ae42df2014-11-17 17:07:07 +0000628 memset(&hints, 0, sizeof(struct addrinfo));
Tim Waugh892d10a2015-03-11 13:12:07 +0000629 hints.ai_flags = AI_PASSIVE | AI_NUMERICSERV;
Tim Waugh6ae42df2014-11-17 17:07:07 +0000630 hints.ai_family = AF_UNSPEC;
631 hints.ai_socktype = SOCK_STREAM;
632 hints.ai_canonname = NULL;
633 hints.ai_addr = NULL;
634 hints.ai_next = NULL;
635
Tim Waugh892d10a2015-03-11 13:12:07 +0000636 snprintf (service, sizeof (service) - 1, "%d", port);
637 service[sizeof (service) - 1] = '\0';
Pierre Ossmanf7d15002015-03-17 17:10:56 +0100638 if ((result = getaddrinfo(addr, service, &hints, &ai)) != 0)
639 throw rdr::Exception("unable to resolve listening address: %s",
640 gai_strerror(result));
Tim Waugh6ae42df2014-11-17 17:07:07 +0000641
Tim Waugh892d10a2015-03-11 13:12:07 +0000642 for (current = ai; current != NULL; current = current->ai_next) {
643 switch (current->ai_family) {
644 case AF_INET:
645 if (!UseIPv4)
646 continue;
647 break;
648
649 case AF_INET6:
650 if (!UseIPv6)
651 continue;
652 break;
653
654 default:
Tim Waugh6ae42df2014-11-17 17:07:07 +0000655 continue;
Tim Waugh892d10a2015-03-11 13:12:07 +0000656 }
Tim Waugh6ae42df2014-11-17 17:07:07 +0000657
Tim Waugh892d10a2015-03-11 13:12:07 +0000658 try {
659 new_listeners.push_back(TcpListener (current->ai_addr,
660 current->ai_addrlen));
661 } catch (SocketException& e) {
662 // Ignore this if it is due to lack of address family support on
663 // the interface or on the system
664 if (e.err != EADDRNOTAVAIL && e.err != EAFNOSUPPORT) {
665 // Otherwise, report the error
666 freeaddrinfo(ai);
667 throw;
668 }
669 }
Tim Waugh6ae42df2014-11-17 17:07:07 +0000670 }
671 freeaddrinfo(ai);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000672
Tim Waugh892d10a2015-03-11 13:12:07 +0000673 if (new_listeners.empty ())
674 throw SocketException("createTcpListeners: no addresses available",
675 EADDRNOTAVAIL);
676
677 listeners->splice (listeners->end(), new_listeners);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000678}
679
680
681TcpFilter::TcpFilter(const char* spec) {
682 rfb::CharArray tmp;
Adam Tkacd36b6262009-09-04 10:57:20 +0000683 tmp.buf = rfb::strDup(spec);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000684 while (tmp.buf) {
685 rfb::CharArray first;
686 rfb::strSplit(tmp.buf, ',', &first.buf, &tmp.buf);
687 if (strlen(first.buf))
688 filter.push_back(parsePattern(first.buf));
689 }
690}
691
692TcpFilter::~TcpFilter() {
693}
694
695
696static bool
Tim Waughc24a64d2015-03-13 16:07:29 +0000697patternMatchIP(const TcpFilter::Pattern& pattern, vnc_sockaddr_t *sa) {
698 switch (pattern.address.u.sa.sa_family) {
699 unsigned long address;
700
701 case AF_INET:
702 if (sa->u.sa.sa_family != AF_INET)
703 return false;
704
705 address = sa->u.sin.sin_addr.s_addr;
706 if (address == htonl (INADDR_NONE)) return false;
707 return ((pattern.address.u.sin.sin_addr.s_addr &
708 pattern.mask.u.sin.sin_addr.s_addr) ==
709 (address & pattern.mask.u.sin.sin_addr.s_addr));
710
711 case AF_INET6:
712 if (sa->u.sa.sa_family != AF_INET6)
713 return false;
714
715 for (unsigned int n = 0; n < 16; n++) {
716 unsigned int bits = (n + 1) * 8;
717 unsigned int mask;
718 if (pattern.prefixlen > bits)
719 mask = 0xff;
720 else {
721 unsigned int lastbits = 0xff;
722 lastbits <<= bits - pattern.prefixlen;
723 mask = lastbits & 0xff;
724 }
725
726 if ((pattern.address.u.sin6.sin6_addr.s6_addr[n] & mask) !=
727 (sa->u.sin6.sin6_addr.s6_addr[n] & mask))
728 return false;
729
730 if (mask < 0xff)
731 break;
732 }
733
734 return true;
735
736 case AF_UNSPEC:
737 // Any address matches
738 return true;
739
740 default:
741 break;
742 }
743
744 return false;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000745}
746
747bool
748TcpFilter::verifyConnection(Socket* s) {
749 rfb::CharArray name;
Tim Waugh6ae42df2014-11-17 17:07:07 +0000750 vnc_sockaddr_t sa;
751 socklen_t sa_size = sizeof(sa);
Tim Waughc24a64d2015-03-13 16:07:29 +0000752
753 if (getpeername(s->getFd(), &sa.u.sa, &sa_size) != 0)
Tim Waugh6ae42df2014-11-17 17:07:07 +0000754 return false;
Tim Waugh6ae42df2014-11-17 17:07:07 +0000755
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000756 name.buf = s->getPeerAddress();
757 std::list<TcpFilter::Pattern>::iterator i;
758 for (i=filter.begin(); i!=filter.end(); i++) {
Tim Waughc24a64d2015-03-13 16:07:29 +0000759 if (patternMatchIP(*i, &sa)) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000760 switch ((*i).action) {
761 case Accept:
762 vlog.debug("ACCEPT %s", name.buf);
763 return true;
764 case Query:
765 vlog.debug("QUERY %s", name.buf);
766 s->setRequiresQuery();
767 return true;
768 case Reject:
769 vlog.debug("REJECT %s", name.buf);
770 return false;
771 }
772 }
773 }
774
775 vlog.debug("[REJECT] %s", name.buf);
776 return false;
777}
778
779
780TcpFilter::Pattern TcpFilter::parsePattern(const char* p) {
781 TcpFilter::Pattern pattern;
782
Tim Waughc24a64d2015-03-13 16:07:29 +0000783 rfb::CharArray addr, pref;
784 bool prefix_specified;
785 int family;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000786
Tim Waughc24a64d2015-03-13 16:07:29 +0000787 prefix_specified = rfb::strSplit(&p[1], '/', &addr.buf, &pref.buf);
788 if (addr.buf[0] == '\0') {
789 // Match any address
790 memset (&pattern.address, 0, sizeof (pattern.address));
791 pattern.address.u.sa.sa_family = AF_UNSPEC;
792 pattern.prefixlen = 0;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000793 } else {
Tim Waughc24a64d2015-03-13 16:07:29 +0000794 struct addrinfo hints;
795 struct addrinfo *ai;
796 char *p = addr.buf;
797 int result;
798 memset (&hints, 0, sizeof (hints));
799 hints.ai_family = AF_UNSPEC;
800 hints.ai_flags = AI_NUMERICHOST;
801
802 // Take out brackets, if present
803 if (*p == '[') {
804 size_t len;
805 p++;
806 len = strlen (p);
807 if (len > 0 && p[len - 1] == ']')
808 p[len - 1] = '\0';
809 }
810
811 if ((result = getaddrinfo (p, NULL, &hints, &ai)) != 0) {
812 throw Exception("unable to resolve host by name: %s",
813 gai_strerror(result));
814 }
815
816 memcpy (&pattern.address.u.sa, ai->ai_addr, ai->ai_addrlen);
817 freeaddrinfo (ai);
Tim Waughc24a64d2015-03-13 16:07:29 +0000818
819 family = pattern.address.u.sa.sa_family;
820
821 if (prefix_specified) {
822 if (family == AF_INET &&
823 rfb::strContains(pref.buf, '.')) {
824 throw Exception("mask no longer supported for filter, "
825 "use prefix instead");
826 }
827
828 pattern.prefixlen = (unsigned int) atoi(pref.buf);
829 } else {
830 switch (family) {
831 case AF_INET:
832 pattern.prefixlen = 32;
833 break;
834 case AF_INET6:
835 pattern.prefixlen = 128;
836 break;
837 default:
838 throw Exception("unknown address family");
839 }
840 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000841 }
842
Pierre Ossmanfdc55e52015-03-17 12:56:31 +0100843 family = pattern.address.u.sa.sa_family;
844
Tim Waughc24a64d2015-03-13 16:07:29 +0000845 if (pattern.prefixlen > (family == AF_INET ? 32: 128))
846 throw Exception("invalid prefix length for filter address: %u",
847 pattern.prefixlen);
848
849 // Compute mask from address and prefix length
850 memset (&pattern.mask, 0, sizeof (pattern.mask));
851 switch (family) {
852 unsigned long mask;
853 case AF_INET:
854 mask = 0;
855 for (unsigned int i=0; i<pattern.prefixlen; i++)
856 mask |= 1<<(31-i);
857 pattern.mask.u.sin.sin_addr.s_addr = htonl(mask);
858 break;
859
860 case AF_INET6:
861 for (unsigned int n = 0; n < 16; n++) {
862 unsigned int bits = (n + 1) * 8;
863 if (pattern.prefixlen > bits)
864 pattern.mask.u.sin6.sin6_addr.s6_addr[n] = 0xff;
865 else {
866 unsigned int lastbits = 0xff;
867 lastbits <<= bits - pattern.prefixlen;
868 pattern.mask.u.sin6.sin6_addr.s6_addr[n] = lastbits & 0xff;
869 break;
870 }
871 }
872 break;
873 case AF_UNSPEC:
874 // No mask to compute
875 break;
876 default:
877 ; /* not reached */
878 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000879
880 switch(p[0]) {
881 case '+': pattern.action = TcpFilter::Accept; break;
882 case '-': pattern.action = TcpFilter::Reject; break;
883 case '?': pattern.action = TcpFilter::Query; break;
884 };
885
886 return pattern;
887}
888
889char* TcpFilter::patternToStr(const TcpFilter::Pattern& p) {
Tim Waughc24a64d2015-03-13 16:07:29 +0000890 rfb::CharArray addr;
Tim Waughc24a64d2015-03-13 16:07:29 +0000891 char buffer[INET6_ADDRSTRLEN + 2];
892
893 if (p.address.u.sa.sa_family == AF_INET) {
894 getnameinfo(&p.address.u.sa, sizeof(p.address.u.sin),
895 buffer, sizeof (buffer), NULL, 0, NI_NUMERICHOST);
896 addr.buf = rfb::strDup(buffer);
897 } else if (p.address.u.sa.sa_family == AF_INET6) {
898 buffer[0] = '[';
899 getnameinfo(&p.address.u.sa, sizeof(p.address.u.sin6),
900 buffer + 1, sizeof (buffer) - 2, NULL, 0, NI_NUMERICHOST);
901 strcat(buffer, "]");
902 addr.buf = rfb::strDup(buffer);
903 } else if (p.address.u.sa.sa_family == AF_UNSPEC)
904 addr.buf = rfb::strDup("");
Tim Waughc24a64d2015-03-13 16:07:29 +0000905
906 char action;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000907 switch (p.action) {
Tim Waughc24a64d2015-03-13 16:07:29 +0000908 case Accept: action = '+'; break;
909 case Reject: action = '-'; break;
910 default:
911 case Query: action = '?'; break;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000912 };
Tim Waughc24a64d2015-03-13 16:07:29 +0000913 size_t resultlen = (1 // action
914 + strlen (addr.buf) // address
915 + 1 // slash
916 + 3 // prefix length, max 128
917 + 1); // terminating nul
918 char* result = new char[resultlen];
919 if (addr.buf[0] == '\0')
920 snprintf(result, resultlen, "%c", action);
921 else
922 snprintf(result, resultlen, "%c%s/%u", action, addr.buf, p.prefixlen);
923
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000924 return result;
925}