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.

254 lines
8.1 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. #include "talk/base/firewallsocketserver.h"
  28. #include <cassert>
  29. #include <algorithm>
  30. #include "talk/base/asyncsocket.h"
  31. #include "talk/base/logging.h"
  32. namespace talk_base {
  33. class FirewallSocket : public AsyncSocketAdapter {
  34. public:
  35. FirewallSocket(FirewallSocketServer* server, AsyncSocket* socket, int type)
  36. : AsyncSocketAdapter(socket), server_(server), type_(type) {
  37. }
  38. virtual int Connect(const SocketAddress& addr) {
  39. if (type_ == SOCK_STREAM) {
  40. if (!server_->Check(FP_TCP, GetLocalAddress(), addr)) {
  41. LOG(LS_VERBOSE) << "FirewallSocket outbound TCP connection from "
  42. << GetLocalAddress().ToString() << " to "
  43. << addr.ToString() << " denied";
  44. // TODO: Handle this asynchronously.
  45. SetError(EHOSTUNREACH);
  46. return SOCKET_ERROR;
  47. }
  48. }
  49. return AsyncSocketAdapter::Connect(addr);
  50. }
  51. virtual int Send(const void* pv, size_t cb) {
  52. return SendTo(pv, cb, GetRemoteAddress());
  53. }
  54. virtual int SendTo(const void* pv, size_t cb, const SocketAddress& addr) {
  55. if (type_ == SOCK_DGRAM) {
  56. if (!server_->Check(FP_UDP, GetLocalAddress(), addr)) {
  57. LOG(LS_VERBOSE) << "FirewallSocket outbound UDP packet from "
  58. << GetLocalAddress().ToString() << " to "
  59. << addr.ToString() << " dropped";
  60. return static_cast<int>(cb);
  61. }
  62. }
  63. return AsyncSocketAdapter::SendTo(pv, cb, addr);
  64. }
  65. virtual int Recv(void* pv, size_t cb) {
  66. SocketAddress addr;
  67. return RecvFrom(pv, cb, &addr);
  68. }
  69. virtual int RecvFrom(void* pv, size_t cb, SocketAddress* paddr) {
  70. if (type_ == SOCK_DGRAM) {
  71. while (true) {
  72. int res = AsyncSocketAdapter::RecvFrom(pv, cb, paddr);
  73. if (res <= 0)
  74. return res;
  75. if (server_->Check(FP_UDP, *paddr, GetLocalAddress()))
  76. return res;
  77. LOG(LS_VERBOSE) << "FirewallSocket inbound UDP packet from "
  78. << paddr->ToString() << " to "
  79. << GetLocalAddress().ToString() << " dropped";
  80. }
  81. }
  82. return AsyncSocketAdapter::RecvFrom(pv, cb, paddr);
  83. }
  84. virtual int Listen(int backlog) {
  85. if (!server_->tcp_listen_enabled()) {
  86. LOG(LS_VERBOSE) << "FirewallSocket listen attempt denied";
  87. return -1;
  88. }
  89. return AsyncSocketAdapter::Listen(backlog);
  90. }
  91. virtual AsyncSocket* Accept(SocketAddress* paddr) {
  92. SocketAddress addr;
  93. while (AsyncSocket* sock = AsyncSocketAdapter::Accept(&addr)) {
  94. if (server_->Check(FP_TCP, addr, GetLocalAddress())) {
  95. if (paddr)
  96. *paddr = addr;
  97. return sock;
  98. }
  99. sock->Close();
  100. delete sock;
  101. LOG(LS_VERBOSE) << "FirewallSocket inbound TCP connection from "
  102. << addr.ToString() << " to "
  103. << GetLocalAddress().ToString() << " denied";
  104. }
  105. return 0;
  106. }
  107. private:
  108. FirewallSocketServer* server_;
  109. int type_;
  110. };
  111. FirewallSocketServer::FirewallSocketServer(SocketServer* server,
  112. FirewallManager* manager,
  113. bool should_delete_server)
  114. : server_(server), manager_(manager),
  115. should_delete_server_(should_delete_server),
  116. udp_sockets_enabled_(true), tcp_sockets_enabled_(true),
  117. tcp_listen_enabled_(true) {
  118. if (manager_)
  119. manager_->AddServer(this);
  120. }
  121. FirewallSocketServer::~FirewallSocketServer() {
  122. if (manager_)
  123. manager_->RemoveServer(this);
  124. if (server_ && should_delete_server_) {
  125. delete server_;
  126. server_ = NULL;
  127. }
  128. }
  129. void FirewallSocketServer::AddRule(bool allow, FirewallProtocol p,
  130. FirewallDirection d,
  131. const SocketAddress& addr) {
  132. SocketAddress src, dst;
  133. if (d == FD_IN) {
  134. dst = addr;
  135. } else {
  136. src = addr;
  137. }
  138. AddRule(allow, p, src, dst);
  139. }
  140. void FirewallSocketServer::AddRule(bool allow, FirewallProtocol p,
  141. const SocketAddress& src,
  142. const SocketAddress& dst) {
  143. Rule r;
  144. r.allow = allow;
  145. r.p = p;
  146. r.src = src;
  147. r.dst = dst;
  148. CritScope scope(&crit_);
  149. rules_.push_back(r);
  150. }
  151. void FirewallSocketServer::ClearRules() {
  152. CritScope scope(&crit_);
  153. rules_.clear();
  154. }
  155. bool FirewallSocketServer::Check(FirewallProtocol p,
  156. const SocketAddress& src,
  157. const SocketAddress& dst) {
  158. CritScope scope(&crit_);
  159. for (size_t i = 0; i < rules_.size(); ++i) {
  160. const Rule& r = rules_[i];
  161. if ((r.p != p) && (r.p != FP_ANY))
  162. continue;
  163. if ((r.src.ipaddr() != src.ipaddr()) && !r.src.IsNil())
  164. continue;
  165. if ((r.src.port() != src.port()) && (r.src.port() != 0))
  166. continue;
  167. if ((r.dst.ipaddr() != dst.ipaddr()) && !r.dst.IsNil())
  168. continue;
  169. if ((r.dst.port() != dst.port()) && (r.dst.port() != 0))
  170. continue;
  171. return r.allow;
  172. }
  173. return true;
  174. }
  175. Socket* FirewallSocketServer::CreateSocket(int type) {
  176. return CreateSocket(AF_INET, type);
  177. }
  178. Socket* FirewallSocketServer::CreateSocket(int family, int type) {
  179. return WrapSocket(server_->CreateAsyncSocket(family, type), type);
  180. }
  181. AsyncSocket* FirewallSocketServer::CreateAsyncSocket(int type) {
  182. return CreateAsyncSocket(AF_INET, type);
  183. }
  184. AsyncSocket* FirewallSocketServer::CreateAsyncSocket(int family, int type) {
  185. return WrapSocket(server_->CreateAsyncSocket(family, type), type);
  186. }
  187. AsyncSocket* FirewallSocketServer::WrapSocket(AsyncSocket* sock, int type) {
  188. if (!sock ||
  189. (type == SOCK_STREAM && !tcp_sockets_enabled_) ||
  190. (type == SOCK_DGRAM && !udp_sockets_enabled_)) {
  191. LOG(LS_VERBOSE) << "FirewallSocketServer socket creation denied";
  192. return NULL;
  193. }
  194. return new FirewallSocket(this, sock, type);
  195. }
  196. FirewallManager::FirewallManager() {
  197. }
  198. FirewallManager::~FirewallManager() {
  199. assert(servers_.empty());
  200. }
  201. void FirewallManager::AddServer(FirewallSocketServer* server) {
  202. CritScope scope(&crit_);
  203. servers_.push_back(server);
  204. }
  205. void FirewallManager::RemoveServer(FirewallSocketServer* server) {
  206. CritScope scope(&crit_);
  207. servers_.erase(std::remove(servers_.begin(), servers_.end(), server),
  208. servers_.end());
  209. }
  210. void FirewallManager::AddRule(bool allow, FirewallProtocol p,
  211. FirewallDirection d, const SocketAddress& addr) {
  212. CritScope scope(&crit_);
  213. for (std::vector<FirewallSocketServer*>::const_iterator it =
  214. servers_.begin(); it != servers_.end(); ++it) {
  215. (*it)->AddRule(allow, p, d, addr);
  216. }
  217. }
  218. void FirewallManager::ClearRules() {
  219. CritScope scope(&crit_);
  220. for (std::vector<FirewallSocketServer*>::const_iterator it =
  221. servers_.begin(); it != servers_.end(); ++it) {
  222. (*it)->ClearRules();
  223. }
  224. }
  225. } // namespace talk_base