Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* 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 | |
| 38 | namespace 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 Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 49 | virtual int getMyPort(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 50 | |
| 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); |
Pierre Ossman | 64069a9 | 2011-11-08 12:10:55 +0000 | [diff] [blame^] | 59 | static bool cork(int sock, bool enable); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 60 | static bool isSocket(int sock); |
| 61 | static bool isConnected(int sock); |
| 62 | static int getSockPort(int sock); |
| 63 | private: |
| 64 | bool closeFd; |
| 65 | }; |
| 66 | |
| 67 | class TcpListener : public SocketListener { |
| 68 | public: |
Adam Tkac | 93ff5db | 2010-02-05 15:54:10 +0000 | [diff] [blame] | 69 | TcpListener(const char *listenaddr, int port, bool localhostOnly=false, |
| 70 | int sock=-1, bool close=true); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 71 | virtual ~TcpListener(); |
| 72 | |
| 73 | virtual void shutdown(); |
| 74 | virtual Socket* accept(); |
| 75 | |
| 76 | void getMyAddresses(std::list<char*>* addrs); |
| 77 | int getMyPort(); |
| 78 | |
| 79 | private: |
| 80 | bool closeFd; |
| 81 | }; |
| 82 | |
| 83 | class TcpFilter : public ConnectionFilter { |
| 84 | public: |
| 85 | TcpFilter(const char* filter); |
| 86 | virtual ~TcpFilter(); |
| 87 | |
| 88 | virtual bool verifyConnection(Socket* s); |
| 89 | |
| 90 | typedef enum {Accept, Reject, Query} Action; |
| 91 | struct Pattern { |
| 92 | Action action; |
| 93 | unsigned long address; |
| 94 | unsigned long mask; |
| 95 | }; |
| 96 | static Pattern parsePattern(const char* s); |
| 97 | static char* patternToStr(const Pattern& p); |
| 98 | protected: |
| 99 | std::list<Pattern> filter; |
| 100 | }; |
| 101 | |
| 102 | } |
| 103 | |
| 104 | #endif // __NETWORK_TCP_SOCKET_H__ |