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