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