blob: 74697829900e43f980e977a039265f43abe3710a [file] [log] [blame]
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +00001/* Copyright (C) 2002-2004 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
Peter Åstrandef07ae82005-02-14 13:11:35 +000035/* Tunnelling support. */
36#define TUNNEL_PORT_OFFSET 5500
37
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000038namespace network {
39
Peter Åstrandef07ae82005-02-14 13:11:35 +000040 /* Tunnelling support. */
41 int findFreeTcpPort (void);
42
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000043 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 void initTcpSockets();
61
62 static bool isSocket(int sock);
63 static bool isConnected(int sock);
64 static int getSockPort(int sock);
65 private:
66 bool closeFd;
67 };
68
69 class TcpListener : public SocketListener {
70 public:
71 TcpListener(int port, bool localhostOnly=false, int sock=-1,
72 bool close=true);
73 virtual ~TcpListener();
74
75 virtual void shutdown();
76 virtual Socket* accept();
77
78 void getMyAddresses(std::list<char*>* addrs);
79 int getMyPort();
80
81 private:
82 bool closeFd;
83 };
84
85 class TcpFilter : public ConnectionFilter {
86 public:
87 TcpFilter(const char* filter);
88 virtual ~TcpFilter();
89
90 virtual bool verifyConnection(Socket* s);
91
92 typedef enum {Accept, Reject, Query} Action;
93 struct Pattern {
94 Action action;
95 unsigned long address;
96 unsigned long mask;
97 };
98 static Pattern parsePattern(const char* s);
99 static char* patternToStr(const Pattern& p);
100 protected:
101 std::list<Pattern> filter;
102 };
103
104}
105
106#endif // __NETWORK_TCP_SOCKET_H__