blob: e62738900c7f7eb3680baaad36bd3788808e0b05 [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>
Tim Waugh892d10a2015-03-11 13:12:07 +000032#include <sys/socket.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000033
34#include <list>
35
36/* Tunnelling support. */
37#define TUNNEL_PORT_OFFSET 5500
38
39namespace network {
40
41 /* Tunnelling support. */
42 int findFreeTcpPort (void);
43
44 class TcpSocket : public Socket {
45 public:
46 TcpSocket(int sock, bool close=true);
47 TcpSocket(const char *name, int port);
48 virtual ~TcpSocket();
49
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000050 virtual int getMyPort();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000051
52 virtual char* getPeerAddress();
53 virtual int getPeerPort();
54 virtual char* getPeerEndpoint();
55 virtual bool sameMachine();
56
57 virtual void shutdown();
58
59 static bool enableNagles(int sock, bool enable);
Pierre Ossman64069a92011-11-08 12:10:55 +000060 static bool cork(int sock, bool enable);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000061 static bool isSocket(int sock);
62 static bool isConnected(int sock);
63 static int getSockPort(int sock);
64 private:
65 bool closeFd;
66 };
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);
72 TcpListener(const TcpListener& other);
73 TcpListener& operator= (const TcpListener& other);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000074 virtual ~TcpListener();
75
76 virtual void shutdown();
77 virtual Socket* accept();
78
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000079 int getMyPort();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000080 };
81
Tim Waugh892d10a2015-03-11 13:12:07 +000082 void createLocalTcpListeners(std::list<TcpListener> *listeners,
83 int port);
84 void createTcpListeners(std::list<TcpListener> *listeners,
85 const char *addr,
86 int port);
87
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000088 class TcpFilter : public ConnectionFilter {
89 public:
90 TcpFilter(const char* filter);
91 virtual ~TcpFilter();
92
93 virtual bool verifyConnection(Socket* s);
94
95 typedef enum {Accept, Reject, Query} Action;
96 struct Pattern {
97 Action action;
98 unsigned long address;
99 unsigned long mask;
100 };
101 static Pattern parsePattern(const char* s);
102 static char* patternToStr(const Pattern& p);
103 protected:
104 std::list<Pattern> filter;
105 };
106
107}
108
109#endif // __NETWORK_TCP_SOCKET_H__