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 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* Copyright (C) 2002-2004 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. #include <list>
  30. namespace network {
  31. class TcpSocket : public Socket {
  32. public:
  33. TcpSocket(int sock, bool close=true);
  34. TcpSocket(const char *name, int port);
  35. virtual ~TcpSocket();
  36. virtual char* getMyAddress();
  37. virtual int getMyPort();
  38. virtual char* getMyEndpoint();
  39. virtual char* getPeerAddress();
  40. virtual int getPeerPort();
  41. virtual char* getPeerEndpoint();
  42. virtual bool sameMachine();
  43. virtual void shutdown();
  44. static void initTcpSockets();
  45. static bool isSocket(int sock);
  46. static bool isConnected(int sock);
  47. static int getSockPort(int sock);
  48. private:
  49. bool closeFd;
  50. };
  51. class TcpListener : public SocketListener {
  52. public:
  53. TcpListener(int port, bool localhostOnly=false, int sock=-1,
  54. bool close=true);
  55. virtual ~TcpListener();
  56. virtual void shutdown();
  57. virtual Socket* accept();
  58. void getMyAddresses(std::list<char*>* addrs);
  59. int getMyPort();
  60. private:
  61. bool closeFd;
  62. };
  63. class TcpFilter : public ConnectionFilter {
  64. public:
  65. TcpFilter(const char* filter);
  66. virtual ~TcpFilter();
  67. virtual bool verifyConnection(Socket* s);
  68. typedef enum {Accept, Reject, Query} Action;
  69. struct Pattern {
  70. Action action;
  71. unsigned long address;
  72. unsigned long mask;
  73. };
  74. static Pattern parsePattern(const char* s);
  75. static char* patternToStr(const Pattern& p);
  76. protected:
  77. std::list<Pattern> filter;
  78. };
  79. }
  80. #endif // __NETWORK_TCP_SOCKET_H__