blob: 87d8e695d8093da2cc40b0e342f27d58229903ae [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
19// -=- TcpSocket.h - base-class for TCP stream sockets.
20// This header also defines the TcpListener class, used
21// to listen for incoming socket connections over TCP
22//
23// NB: Any file descriptors created by the TcpSocket or
24// TcpListener classes are close-on-exec if the OS supports
25// it. TcpSockets initialised with a caller-supplied fd
26// are NOT set to close-on-exec.
27
28#ifndef __NETWORK_TCP_SOCKET_H__
29#define __NETWORK_TCP_SOCKET_H__
30
31#include <network/Socket.h>
Pierre Ossmana6f97402015-03-17 13:42:06 +010032
33#ifdef WIN32
34#include <winsock2.h>
35#include <ws2tcpip.h>
36#else
Tim Waughc24a64d2015-03-13 16:07:29 +000037#include <sys/socket.h> /* for socklen_t */
38#include <netinet/in.h> /* for struct sockaddr_in */
Pierre Ossmana6f97402015-03-17 13:42:06 +010039#endif
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000040
41#include <list>
42
43/* Tunnelling support. */
44#define TUNNEL_PORT_OFFSET 5500
45
46namespace network {
47
48 /* Tunnelling support. */
49 int findFreeTcpPort (void);
50
51 class TcpSocket : public Socket {
52 public:
Pierre Ossmand7bbbbf2018-05-21 12:06:47 +020053 TcpSocket(int sock);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000054 TcpSocket(const char *name, int port);
55 virtual ~TcpSocket();
56
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000057 virtual char* getPeerAddress();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000058 virtual char* getPeerEndpoint();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000059
60 virtual void shutdown();
Peter Åstrand (astrand)01dc1a62017-10-10 12:56:04 +020061 virtual bool cork(bool enable);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000062
63 static bool enableNagles(int sock, bool enable);
Tristan Schmelchere5afb922011-09-02 15:29:25 -070064 static bool isListening(int sock);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000065 static int getSockPort(int sock);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000066 };
67
68 class TcpListener : public SocketListener {
69 public:
Tim Waugh892d10a2015-03-11 13:12:07 +000070 TcpListener(const struct sockaddr *listenaddr, socklen_t listenaddrlen);
71 TcpListener(int sock);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000072 virtual ~TcpListener();
73
74 virtual void shutdown();
75 virtual Socket* accept();
76
Pierre Ossmane3a2be62018-05-03 14:03:55 +020077 virtual int getMyPort();
78
Pierre Ossman57cab512015-03-17 13:39:39 +010079 static void getMyAddresses(std::list<char*>* result);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000080 };
81
Pierre Ossmane3a2be62018-05-03 14:03:55 +020082 void createLocalTcpListeners(std::list<SocketListener*> *listeners,
Tim Waugh892d10a2015-03-11 13:12:07 +000083 int port);
Pierre Ossmane3a2be62018-05-03 14:03:55 +020084 void createTcpListeners(std::list<SocketListener*> *listeners,
Tim Waugh892d10a2015-03-11 13:12:07 +000085 const char *addr,
86 int port);
Pierre Ossmane3a2be62018-05-03 14:03:55 +020087 void createTcpListeners(std::list<SocketListener*> *listeners,
Pierre Ossmanf7aa3f92015-09-29 15:40:49 +020088 const struct addrinfo *ai);
Tim Waugh892d10a2015-03-11 13:12:07 +000089
Tim Waughc24a64d2015-03-13 16:07:29 +000090 typedef struct vnc_sockaddr {
91 union {
92 sockaddr sa;
93 sockaddr_in sin;
Tim Waughc24a64d2015-03-13 16:07:29 +000094 sockaddr_in6 sin6;
Tim Waughc24a64d2015-03-13 16:07:29 +000095 } u;
96 } vnc_sockaddr_t;
97
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000098 class TcpFilter : public ConnectionFilter {
99 public:
100 TcpFilter(const char* filter);
101 virtual ~TcpFilter();
102
103 virtual bool verifyConnection(Socket* s);
104
105 typedef enum {Accept, Reject, Query} Action;
106 struct Pattern {
107 Action action;
Tim Waughc24a64d2015-03-13 16:07:29 +0000108 vnc_sockaddr_t address;
109 unsigned int prefixlen;
110
111 vnc_sockaddr_t mask; // computed from address and prefix
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000112 };
113 static Pattern parsePattern(const char* s);
114 static char* patternToStr(const Pattern& p);
115 protected:
116 std::list<Pattern> filter;
117 };
118
119}
120
121#endif // __NETWORK_TCP_SOCKET_H__