You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TcpSocket.h 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. // -=- TcpSocket.h - base-class for TCP stream sockets.
  19. // This header also defines the TcpListener class, used
  20. // to listen for incoming socket connections over TCP
  21. //
  22. // NB: Any file descriptors created by the TcpSocket or
  23. // TcpListener classes are close-on-exec if the OS supports
  24. // it. TcpSockets initialised with a caller-supplied fd
  25. // are NOT set to close-on-exec.
  26. #ifndef __NETWORK_TCP_SOCKET_H__
  27. #define __NETWORK_TCP_SOCKET_H__
  28. #include <network/Socket.h>
  29. #ifdef WIN32
  30. #include <winsock2.h>
  31. #include <ws2tcpip.h>
  32. #else
  33. #include <sys/socket.h> /* for socklen_t */
  34. #include <netinet/in.h> /* for struct sockaddr_in */
  35. #endif
  36. #include <list>
  37. /* Tunnelling support. */
  38. #define TUNNEL_PORT_OFFSET 5500
  39. namespace network {
  40. /* Tunnelling support. */
  41. int findFreeTcpPort (void);
  42. int getSockPort(int sock);
  43. class TcpSocket : public Socket {
  44. public:
  45. TcpSocket(int sock);
  46. TcpSocket(const char *name, int port);
  47. virtual char* getPeerAddress();
  48. virtual char* getPeerEndpoint();
  49. protected:
  50. bool enableNagles(bool enable);
  51. };
  52. class TcpListener : public SocketListener {
  53. public:
  54. TcpListener(const struct sockaddr *listenaddr, socklen_t listenaddrlen);
  55. TcpListener(int sock);
  56. virtual int getMyPort();
  57. static void getMyAddresses(std::list<char*>* result);
  58. protected:
  59. virtual Socket* createSocket(int fd);
  60. };
  61. void createLocalTcpListeners(std::list<SocketListener*> *listeners,
  62. int port);
  63. void createTcpListeners(std::list<SocketListener*> *listeners,
  64. const char *addr,
  65. int port);
  66. void createTcpListeners(std::list<SocketListener*> *listeners,
  67. const struct addrinfo *ai);
  68. typedef struct vnc_sockaddr {
  69. union {
  70. sockaddr sa;
  71. sockaddr_in sin;
  72. sockaddr_in6 sin6;
  73. } u;
  74. } vnc_sockaddr_t;
  75. class TcpFilter : public ConnectionFilter {
  76. public:
  77. TcpFilter(const char* filter);
  78. virtual ~TcpFilter();
  79. virtual bool verifyConnection(Socket* s);
  80. typedef enum {Accept, Reject, Query} Action;
  81. struct Pattern {
  82. Action action;
  83. vnc_sockaddr_t address;
  84. unsigned int prefixlen;
  85. vnc_sockaddr_t mask; // computed from address and prefix
  86. };
  87. static Pattern parsePattern(const char* s);
  88. static char* patternToStr(const Pattern& p);
  89. protected:
  90. std::list<Pattern> filter;
  91. };
  92. }
  93. #endif // __NETWORK_TCP_SOCKET_H__