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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #include <string>
  38. /* Tunnelling support. */
  39. #define TUNNEL_PORT_OFFSET 5500
  40. namespace network {
  41. /* Tunnelling support. */
  42. int findFreeTcpPort (void);
  43. int getSockPort(int sock);
  44. class TcpSocket : public Socket {
  45. public:
  46. TcpSocket(int sock);
  47. TcpSocket(const char *name, int port);
  48. virtual const char* getPeerAddress();
  49. virtual const char* getPeerEndpoint();
  50. protected:
  51. bool enableNagles(bool enable);
  52. };
  53. class TcpListener : public SocketListener {
  54. public:
  55. TcpListener(const struct sockaddr *listenaddr, socklen_t listenaddrlen);
  56. TcpListener(int sock);
  57. virtual int getMyPort();
  58. static std::list<std::string> getMyAddresses();
  59. protected:
  60. virtual Socket* createSocket(int fd);
  61. };
  62. void createLocalTcpListeners(std::list<SocketListener*> *listeners,
  63. int port);
  64. void createTcpListeners(std::list<SocketListener*> *listeners,
  65. const char *addr,
  66. int port);
  67. void createTcpListeners(std::list<SocketListener*> *listeners,
  68. const struct addrinfo *ai);
  69. typedef struct vnc_sockaddr {
  70. union {
  71. sockaddr sa;
  72. sockaddr_in sin;
  73. sockaddr_in6 sin6;
  74. } u;
  75. } vnc_sockaddr_t;
  76. class TcpFilter : public ConnectionFilter {
  77. public:
  78. TcpFilter(const char* filter);
  79. virtual ~TcpFilter();
  80. virtual bool verifyConnection(Socket* s);
  81. typedef enum {Accept, Reject, Query} Action;
  82. struct Pattern {
  83. Action action;
  84. vnc_sockaddr_t address;
  85. unsigned int prefixlen;
  86. vnc_sockaddr_t mask; // computed from address and prefix
  87. };
  88. static Pattern parsePattern(const char* s);
  89. static std::string patternToStr(const Pattern& p);
  90. protected:
  91. std::list<Pattern> filter;
  92. };
  93. }
  94. #endif // __NETWORK_TCP_SOCKET_H__