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