firewall_socket_server.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright 2004 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef RTC_BASE_FIREWALL_SOCKET_SERVER_H_
  11. #define RTC_BASE_FIREWALL_SOCKET_SERVER_H_
  12. #include <vector>
  13. #include "rtc_base/async_socket.h"
  14. #include "rtc_base/ip_address.h"
  15. #include "rtc_base/socket.h"
  16. #include "rtc_base/socket_address.h"
  17. #include "rtc_base/socket_server.h"
  18. #include "rtc_base/synchronization/mutex.h"
  19. namespace rtc {
  20. class FirewallManager;
  21. // This SocketServer shim simulates a rule-based firewall server.
  22. enum FirewallProtocol { FP_UDP, FP_TCP, FP_ANY };
  23. enum FirewallDirection { FD_IN, FD_OUT, FD_ANY };
  24. class FirewallSocketServer : public SocketServer {
  25. public:
  26. FirewallSocketServer(SocketServer* server,
  27. FirewallManager* manager = nullptr,
  28. bool should_delete_server = false);
  29. ~FirewallSocketServer() override;
  30. SocketServer* socketserver() const { return server_; }
  31. void set_socketserver(SocketServer* server) {
  32. if (server_ && should_delete_server_) {
  33. delete server_;
  34. server_ = nullptr;
  35. should_delete_server_ = false;
  36. }
  37. server_ = server;
  38. }
  39. // Settings to control whether CreateSocket or Socket::Listen succeed.
  40. void set_udp_sockets_enabled(bool enabled) { udp_sockets_enabled_ = enabled; }
  41. void set_tcp_sockets_enabled(bool enabled) { tcp_sockets_enabled_ = enabled; }
  42. bool tcp_listen_enabled() const { return tcp_listen_enabled_; }
  43. void set_tcp_listen_enabled(bool enabled) { tcp_listen_enabled_ = enabled; }
  44. // Rules govern the behavior of Connect/Accept/Send/Recv attempts.
  45. void AddRule(bool allow,
  46. FirewallProtocol p = FP_ANY,
  47. FirewallDirection d = FD_ANY,
  48. const SocketAddress& addr = SocketAddress());
  49. void AddRule(bool allow,
  50. FirewallProtocol p,
  51. const SocketAddress& src,
  52. const SocketAddress& dst);
  53. void ClearRules();
  54. bool Check(FirewallProtocol p,
  55. const SocketAddress& src,
  56. const SocketAddress& dst);
  57. // Set the IP addresses for which Bind will fail. By default this list is
  58. // empty. This can be used to simulate a real OS that refuses to bind to
  59. // addresses under various circumstances.
  60. //
  61. // No matter how many addresses are added (including INADDR_ANY), the server
  62. // will still allow creating outgoing TCP connections, since they don't
  63. // require explicitly binding a socket.
  64. void SetUnbindableIps(const std::vector<rtc::IPAddress>& unbindable_ips);
  65. bool IsBindableIp(const rtc::IPAddress& ip);
  66. Socket* CreateSocket(int family, int type) override;
  67. AsyncSocket* CreateAsyncSocket(int family, int type) override;
  68. void SetMessageQueue(Thread* queue) override;
  69. bool Wait(int cms, bool process_io) override;
  70. void WakeUp() override;
  71. Socket* WrapSocket(Socket* sock, int type);
  72. AsyncSocket* WrapSocket(AsyncSocket* sock, int type);
  73. private:
  74. SocketServer* server_;
  75. FirewallManager* manager_;
  76. webrtc::Mutex mutex_;
  77. struct Rule {
  78. bool allow;
  79. FirewallProtocol p;
  80. FirewallDirection d;
  81. SocketAddress src;
  82. SocketAddress dst;
  83. };
  84. std::vector<Rule> rules_;
  85. std::vector<rtc::IPAddress> unbindable_ips_;
  86. bool should_delete_server_;
  87. bool udp_sockets_enabled_;
  88. bool tcp_sockets_enabled_;
  89. bool tcp_listen_enabled_;
  90. };
  91. // FirewallManager allows you to manage firewalls in multiple threads together
  92. class FirewallManager {
  93. public:
  94. FirewallManager();
  95. ~FirewallManager();
  96. void AddServer(FirewallSocketServer* server);
  97. void RemoveServer(FirewallSocketServer* server);
  98. void AddRule(bool allow,
  99. FirewallProtocol p = FP_ANY,
  100. FirewallDirection d = FD_ANY,
  101. const SocketAddress& addr = SocketAddress());
  102. void ClearRules();
  103. private:
  104. webrtc::Mutex mutex_;
  105. std::vector<FirewallSocketServer*> servers_;
  106. };
  107. } // namespace rtc
  108. #endif // RTC_BASE_FIREWALL_SOCKET_SERVER_H_