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.

137 lines
4.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef TALK_BASE_FIREWALLSOCKETSERVER_H_
  28. #define TALK_BASE_FIREWALLSOCKETSERVER_H_
  29. #include <vector>
  30. #include "talk/base/socketserver.h"
  31. #include "talk/base/criticalsection.h"
  32. namespace talk_base {
  33. class FirewallManager;
  34. // This SocketServer shim simulates a rule-based firewall server.
  35. enum FirewallProtocol { FP_UDP, FP_TCP, FP_ANY };
  36. enum FirewallDirection { FD_IN, FD_OUT, FD_ANY };
  37. class FirewallSocketServer : public SocketServer {
  38. public:
  39. FirewallSocketServer(SocketServer * server,
  40. FirewallManager * manager = NULL,
  41. bool should_delete_server = false);
  42. virtual ~FirewallSocketServer();
  43. SocketServer* socketserver() const { return server_; }
  44. void set_socketserver(SocketServer* server) {
  45. if (server_ && should_delete_server_) {
  46. delete server_;
  47. server_ = NULL;
  48. should_delete_server_ = false;
  49. }
  50. server_ = server;
  51. }
  52. // Settings to control whether CreateSocket or Socket::Listen succeed.
  53. void set_udp_sockets_enabled(bool enabled) { udp_sockets_enabled_ = enabled; }
  54. void set_tcp_sockets_enabled(bool enabled) { tcp_sockets_enabled_ = enabled; }
  55. bool tcp_listen_enabled() const { return tcp_listen_enabled_; }
  56. void set_tcp_listen_enabled(bool enabled) { tcp_listen_enabled_ = enabled; }
  57. // Rules govern the behavior of Connect/Accept/Send/Recv attempts.
  58. void AddRule(bool allow, FirewallProtocol p = FP_ANY,
  59. FirewallDirection d = FD_ANY,
  60. const SocketAddress& addr = SocketAddress());
  61. void AddRule(bool allow, FirewallProtocol p,
  62. const SocketAddress& src, const SocketAddress& dst);
  63. void ClearRules();
  64. bool Check(FirewallProtocol p,
  65. const SocketAddress& src, const SocketAddress& dst);
  66. virtual Socket* CreateSocket(int type);
  67. virtual Socket* CreateSocket(int family, int type);
  68. virtual AsyncSocket* CreateAsyncSocket(int type);
  69. virtual AsyncSocket* CreateAsyncSocket(int family, int type);
  70. virtual void SetMessageQueue(MessageQueue* queue) {
  71. server_->SetMessageQueue(queue);
  72. }
  73. virtual bool Wait(int cms, bool process_io) {
  74. return server_->Wait(cms, process_io);
  75. }
  76. virtual void WakeUp() {
  77. return server_->WakeUp();
  78. }
  79. Socket * WrapSocket(Socket * sock, int type);
  80. AsyncSocket * WrapSocket(AsyncSocket * sock, int type);
  81. private:
  82. SocketServer * server_;
  83. FirewallManager * manager_;
  84. CriticalSection crit_;
  85. struct Rule {
  86. bool allow;
  87. FirewallProtocol p;
  88. FirewallDirection d;
  89. SocketAddress src;
  90. SocketAddress dst;
  91. };
  92. std::vector<Rule> rules_;
  93. bool should_delete_server_;
  94. bool udp_sockets_enabled_;
  95. bool tcp_sockets_enabled_;
  96. bool tcp_listen_enabled_;
  97. };
  98. // FirewallManager allows you to manage firewalls in multiple threads together
  99. class FirewallManager {
  100. public:
  101. FirewallManager();
  102. ~FirewallManager();
  103. void AddServer(FirewallSocketServer * server);
  104. void RemoveServer(FirewallSocketServer * server);
  105. void AddRule(bool allow, FirewallProtocol p = FP_ANY,
  106. FirewallDirection d = FD_ANY,
  107. const SocketAddress& addr = SocketAddress());
  108. void ClearRules();
  109. private:
  110. CriticalSection crit_;
  111. std::vector<FirewallSocketServer *> servers_;
  112. };
  113. } // namespace talk_base
  114. #endif // TALK_BASE_FIREWALLSOCKETSERVER_H_