blob: 29c09c69ba99754097fc67bcf9ab9ec248c84052 [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
Tim Waughc24a64d2015-03-13 16:07:29 +000031#ifdef HAVE_CONFIG_H
32#include <config.h> /* for HAVE_GETADDRINFO */
33#endif
34
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000035#include <network/Socket.h>
Tim Waughc24a64d2015-03-13 16:07:29 +000036#include <sys/socket.h> /* for socklen_t */
37#include <netinet/in.h> /* for struct sockaddr_in */
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000038
39#include <list>
40
41/* Tunnelling support. */
42#define TUNNEL_PORT_OFFSET 5500
43
44namespace network {
45
46 /* Tunnelling support. */
47 int findFreeTcpPort (void);
48
49 class TcpSocket : public Socket {
50 public:
51 TcpSocket(int sock, bool close=true);
52 TcpSocket(const char *name, int port);
53 virtual ~TcpSocket();
54
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000055 virtual int getMyPort();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000056
57 virtual char* getPeerAddress();
58 virtual int getPeerPort();
59 virtual char* getPeerEndpoint();
60 virtual bool sameMachine();
61
62 virtual void shutdown();
63
64 static bool enableNagles(int sock, bool enable);
Pierre Ossman64069a92011-11-08 12:10:55 +000065 static bool cork(int sock, bool enable);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000066 static bool isSocket(int sock);
67 static bool isConnected(int sock);
68 static int getSockPort(int sock);
69 private:
70 bool closeFd;
71 };
72
73 class TcpListener : public SocketListener {
74 public:
Tim Waugh892d10a2015-03-11 13:12:07 +000075 TcpListener(const struct sockaddr *listenaddr, socklen_t listenaddrlen);
76 TcpListener(int sock);
77 TcpListener(const TcpListener& other);
78 TcpListener& operator= (const TcpListener& other);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000079 virtual ~TcpListener();
80
81 virtual void shutdown();
82 virtual Socket* accept();
83
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000084 int getMyPort();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000085 };
86
Tim Waugh892d10a2015-03-11 13:12:07 +000087 void createLocalTcpListeners(std::list<TcpListener> *listeners,
88 int port);
89 void createTcpListeners(std::list<TcpListener> *listeners,
90 const char *addr,
91 int port);
92
Tim Waughc24a64d2015-03-13 16:07:29 +000093 typedef struct vnc_sockaddr {
94 union {
95 sockaddr sa;
96 sockaddr_in sin;
97#ifdef HAVE_GETADDRINFO
98 sockaddr_in6 sin6;
99#endif
100 } u;
101 } vnc_sockaddr_t;
102
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000103 class TcpFilter : public ConnectionFilter {
104 public:
105 TcpFilter(const char* filter);
106 virtual ~TcpFilter();
107
108 virtual bool verifyConnection(Socket* s);
109
110 typedef enum {Accept, Reject, Query} Action;
111 struct Pattern {
112 Action action;
Tim Waughc24a64d2015-03-13 16:07:29 +0000113 vnc_sockaddr_t address;
114 unsigned int prefixlen;
115
116 vnc_sockaddr_t mask; // computed from address and prefix
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000117 };
118 static Pattern parsePattern(const char* s);
119 static char* patternToStr(const Pattern& p);
120 protected:
121 std::list<Pattern> filter;
122 };
123
124}
125
126#endif // __NETWORK_TCP_SOCKET_H__