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 | |
Tim Waugh | c24a64d | 2015-03-13 16:07:29 +0000 | [diff] [blame] | 31 | #ifdef HAVE_CONFIG_H |
| 32 | #include <config.h> /* for HAVE_GETADDRINFO */ |
| 33 | #endif |
| 34 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 35 | #include <network/Socket.h> |
Pierre Ossman | a6f9740 | 2015-03-17 13:42:06 +0100 | [diff] [blame^] | 36 | |
| 37 | #ifdef WIN32 |
| 38 | #include <winsock2.h> |
| 39 | #include <ws2tcpip.h> |
| 40 | #else |
Tim Waugh | c24a64d | 2015-03-13 16:07:29 +0000 | [diff] [blame] | 41 | #include <sys/socket.h> /* for socklen_t */ |
| 42 | #include <netinet/in.h> /* for struct sockaddr_in */ |
Pierre Ossman | a6f9740 | 2015-03-17 13:42:06 +0100 | [diff] [blame^] | 43 | #endif |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 44 | |
| 45 | #include <list> |
| 46 | |
| 47 | /* Tunnelling support. */ |
| 48 | #define TUNNEL_PORT_OFFSET 5500 |
| 49 | |
| 50 | namespace network { |
| 51 | |
| 52 | /* Tunnelling support. */ |
| 53 | int findFreeTcpPort (void); |
| 54 | |
| 55 | class TcpSocket : public Socket { |
| 56 | public: |
| 57 | TcpSocket(int sock, bool close=true); |
| 58 | TcpSocket(const char *name, int port); |
| 59 | virtual ~TcpSocket(); |
| 60 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 61 | virtual int getMyPort(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 62 | |
| 63 | virtual char* getPeerAddress(); |
| 64 | virtual int getPeerPort(); |
| 65 | virtual char* getPeerEndpoint(); |
| 66 | virtual bool sameMachine(); |
| 67 | |
| 68 | virtual void shutdown(); |
| 69 | |
| 70 | static bool enableNagles(int sock, bool enable); |
Pierre Ossman | 64069a9 | 2011-11-08 12:10:55 +0000 | [diff] [blame] | 71 | static bool cork(int sock, bool enable); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 72 | static bool isSocket(int sock); |
| 73 | static bool isConnected(int sock); |
| 74 | static int getSockPort(int sock); |
| 75 | private: |
| 76 | bool closeFd; |
| 77 | }; |
| 78 | |
| 79 | class TcpListener : public SocketListener { |
| 80 | public: |
Tim Waugh | 892d10a | 2015-03-11 13:12:07 +0000 | [diff] [blame] | 81 | TcpListener(const struct sockaddr *listenaddr, socklen_t listenaddrlen); |
| 82 | TcpListener(int sock); |
| 83 | TcpListener(const TcpListener& other); |
| 84 | TcpListener& operator= (const TcpListener& other); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 85 | virtual ~TcpListener(); |
| 86 | |
| 87 | virtual void shutdown(); |
| 88 | virtual Socket* accept(); |
| 89 | |
Pierre Ossman | 57cab51 | 2015-03-17 13:39:39 +0100 | [diff] [blame] | 90 | static void getMyAddresses(std::list<char*>* result); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 91 | int getMyPort(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
Tim Waugh | 892d10a | 2015-03-11 13:12:07 +0000 | [diff] [blame] | 94 | void createLocalTcpListeners(std::list<TcpListener> *listeners, |
| 95 | int port); |
| 96 | void createTcpListeners(std::list<TcpListener> *listeners, |
| 97 | const char *addr, |
| 98 | int port); |
| 99 | |
Tim Waugh | c24a64d | 2015-03-13 16:07:29 +0000 | [diff] [blame] | 100 | typedef struct vnc_sockaddr { |
| 101 | union { |
| 102 | sockaddr sa; |
| 103 | sockaddr_in sin; |
| 104 | #ifdef HAVE_GETADDRINFO |
| 105 | sockaddr_in6 sin6; |
| 106 | #endif |
| 107 | } u; |
| 108 | } vnc_sockaddr_t; |
| 109 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 110 | class TcpFilter : public ConnectionFilter { |
| 111 | public: |
| 112 | TcpFilter(const char* filter); |
| 113 | virtual ~TcpFilter(); |
| 114 | |
| 115 | virtual bool verifyConnection(Socket* s); |
| 116 | |
| 117 | typedef enum {Accept, Reject, Query} Action; |
| 118 | struct Pattern { |
| 119 | Action action; |
Tim Waugh | c24a64d | 2015-03-13 16:07:29 +0000 | [diff] [blame] | 120 | vnc_sockaddr_t address; |
| 121 | unsigned int prefixlen; |
| 122 | |
| 123 | vnc_sockaddr_t mask; // computed from address and prefix |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 124 | }; |
| 125 | static Pattern parsePattern(const char* s); |
| 126 | static char* patternToStr(const Pattern& p); |
| 127 | protected: |
| 128 | std::list<Pattern> filter; |
| 129 | }; |
| 130 | |
| 131 | } |
| 132 | |
| 133 | #endif // __NETWORK_TCP_SOCKET_H__ |