blob: 774c6e135b7e7a89967933d9178b966ac7198233 [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>
32
33#include <list>
34
35/* Tunnelling support. */
36#define TUNNEL_PORT_OFFSET 5500
37
38namespace network {
39
40 /* Tunnelling support. */
41 int findFreeTcpPort (void);
42
43 class TcpSocket : public Socket {
44 public:
45 TcpSocket(int sock, bool close=true);
46 TcpSocket(const char *name, int port);
47 virtual ~TcpSocket();
48
49 virtual char* getMyAddress();
50 virtual int getMyPort();
51 virtual char* getMyEndpoint();
52
53 virtual char* getPeerAddress();
54 virtual int getPeerPort();
55 virtual char* getPeerEndpoint();
56 virtual bool sameMachine();
57
58 virtual void shutdown();
59
60 static bool enableNagles(int sock, bool enable);
61 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:
70 TcpListener(int port, bool localhostOnly=false, int sock=-1,
71 bool close=true);
72 virtual ~TcpListener();
73
74 virtual void shutdown();
75 virtual Socket* accept();
76
77 void getMyAddresses(std::list<char*>* addrs);
78 int getMyPort();
79
80 private:
81 bool closeFd;
82 };
83
84 class TcpFilter : public ConnectionFilter {
85 public:
86 TcpFilter(const char* filter);
87 virtual ~TcpFilter();
88
89 virtual bool verifyConnection(Socket* s);
90
91 typedef enum {Accept, Reject, Query} Action;
92 struct Pattern {
93 Action action;
94 unsigned long address;
95 unsigned long mask;
96 };
97 static Pattern parsePattern(const char* s);
98 static char* patternToStr(const Pattern& p);
99 protected:
100 std::list<Pattern> filter;
101 };
102
103}
104
105#endif // __NETWORK_TCP_SOCKET_H__