blob: 00ccf78a54b3e2f63a6bd712e5125b018e81683c [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
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000049 virtual int getMyPort();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000050
51 virtual char* getPeerAddress();
52 virtual int getPeerPort();
53 virtual char* getPeerEndpoint();
54 virtual bool sameMachine();
55
56 virtual void shutdown();
57
58 static bool enableNagles(int sock, bool enable);
59 static bool isSocket(int sock);
60 static bool isConnected(int sock);
61 static int getSockPort(int sock);
62 private:
63 bool closeFd;
64 };
65
66 class TcpListener : public SocketListener {
67 public:
68 TcpListener(int port, bool localhostOnly=false, int sock=-1,
69 bool close=true);
70 virtual ~TcpListener();
71
72 virtual void shutdown();
73 virtual Socket* accept();
74
75 void getMyAddresses(std::list<char*>* addrs);
76 int getMyPort();
77
78 private:
79 bool closeFd;
80 };
81
82 class TcpFilter : public ConnectionFilter {
83 public:
84 TcpFilter(const char* filter);
85 virtual ~TcpFilter();
86
87 virtual bool verifyConnection(Socket* s);
88
89 typedef enum {Accept, Reject, Query} Action;
90 struct Pattern {
91 Action action;
92 unsigned long address;
93 unsigned long mask;
94 };
95 static Pattern parsePattern(const char* s);
96 static char* patternToStr(const Pattern& p);
97 protected:
98 std::list<Pattern> filter;
99 };
100
101}
102
103#endif // __NETWORK_TCP_SOCKET_H__