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.

255 lines
8.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "rtc_base/nat_server.h"
  11. #include <memory>
  12. #include "rtc_base/checks.h"
  13. #include "rtc_base/logging.h"
  14. #include "rtc_base/nat_socket_factory.h"
  15. #include "rtc_base/socket_adapters.h"
  16. namespace rtc {
  17. RouteCmp::RouteCmp(NAT* nat) : symmetric(nat->IsSymmetric()) {}
  18. size_t RouteCmp::operator()(const SocketAddressPair& r) const {
  19. size_t h = r.source().Hash();
  20. if (symmetric)
  21. h ^= r.destination().Hash();
  22. return h;
  23. }
  24. bool RouteCmp::operator()(const SocketAddressPair& r1,
  25. const SocketAddressPair& r2) const {
  26. if (r1.source() < r2.source())
  27. return true;
  28. if (r2.source() < r1.source())
  29. return false;
  30. if (symmetric && (r1.destination() < r2.destination()))
  31. return true;
  32. if (symmetric && (r2.destination() < r1.destination()))
  33. return false;
  34. return false;
  35. }
  36. AddrCmp::AddrCmp(NAT* nat)
  37. : use_ip(nat->FiltersIP()), use_port(nat->FiltersPort()) {}
  38. size_t AddrCmp::operator()(const SocketAddress& a) const {
  39. size_t h = 0;
  40. if (use_ip)
  41. h ^= HashIP(a.ipaddr());
  42. if (use_port)
  43. h ^= a.port() | (a.port() << 16);
  44. return h;
  45. }
  46. bool AddrCmp::operator()(const SocketAddress& a1,
  47. const SocketAddress& a2) const {
  48. if (use_ip && (a1.ipaddr() < a2.ipaddr()))
  49. return true;
  50. if (use_ip && (a2.ipaddr() < a1.ipaddr()))
  51. return false;
  52. if (use_port && (a1.port() < a2.port()))
  53. return true;
  54. if (use_port && (a2.port() < a1.port()))
  55. return false;
  56. return false;
  57. }
  58. // Proxy socket that will capture the external destination address intended for
  59. // a TCP connection to the NAT server.
  60. class NATProxyServerSocket : public AsyncProxyServerSocket {
  61. public:
  62. NATProxyServerSocket(AsyncSocket* socket)
  63. : AsyncProxyServerSocket(socket, kNATEncodedIPv6AddressSize) {
  64. BufferInput(true);
  65. }
  66. void SendConnectResult(int err, const SocketAddress& addr) override {
  67. char code = err ? 1 : 0;
  68. BufferedReadAdapter::DirectSend(&code, sizeof(char));
  69. }
  70. protected:
  71. void ProcessInput(char* data, size_t* len) override {
  72. if (*len < 2) {
  73. return;
  74. }
  75. int family = data[1];
  76. RTC_DCHECK(family == AF_INET || family == AF_INET6);
  77. if ((family == AF_INET && *len < kNATEncodedIPv4AddressSize) ||
  78. (family == AF_INET6 && *len < kNATEncodedIPv6AddressSize)) {
  79. return;
  80. }
  81. SocketAddress dest_addr;
  82. size_t address_length = UnpackAddressFromNAT(data, *len, &dest_addr);
  83. *len -= address_length;
  84. if (*len > 0) {
  85. memmove(data, data + address_length, *len);
  86. }
  87. bool remainder = (*len > 0);
  88. BufferInput(false);
  89. SignalConnectRequest(this, dest_addr);
  90. if (remainder) {
  91. SignalReadEvent(this);
  92. }
  93. }
  94. };
  95. class NATProxyServer : public ProxyServer {
  96. public:
  97. NATProxyServer(SocketFactory* int_factory,
  98. const SocketAddress& int_addr,
  99. SocketFactory* ext_factory,
  100. const SocketAddress& ext_ip)
  101. : ProxyServer(int_factory, int_addr, ext_factory, ext_ip) {}
  102. protected:
  103. AsyncProxyServerSocket* WrapSocket(AsyncSocket* socket) override {
  104. return new NATProxyServerSocket(socket);
  105. }
  106. };
  107. NATServer::NATServer(NATType type,
  108. SocketFactory* internal,
  109. const SocketAddress& internal_udp_addr,
  110. const SocketAddress& internal_tcp_addr,
  111. SocketFactory* external,
  112. const SocketAddress& external_ip)
  113. : external_(external), external_ip_(external_ip.ipaddr(), 0) {
  114. nat_ = NAT::Create(type);
  115. udp_server_socket_ = AsyncUDPSocket::Create(internal, internal_udp_addr);
  116. udp_server_socket_->SignalReadPacket.connect(this,
  117. &NATServer::OnInternalUDPPacket);
  118. tcp_proxy_server_ =
  119. new NATProxyServer(internal, internal_tcp_addr, external, external_ip);
  120. int_map_ = new InternalMap(RouteCmp(nat_));
  121. ext_map_ = new ExternalMap();
  122. }
  123. NATServer::~NATServer() {
  124. for (InternalMap::iterator iter = int_map_->begin(); iter != int_map_->end();
  125. iter++)
  126. delete iter->second;
  127. delete nat_;
  128. delete udp_server_socket_;
  129. delete tcp_proxy_server_;
  130. delete int_map_;
  131. delete ext_map_;
  132. }
  133. void NATServer::OnInternalUDPPacket(AsyncPacketSocket* socket,
  134. const char* buf,
  135. size_t size,
  136. const SocketAddress& addr,
  137. const int64_t& /* packet_time_us */) {
  138. // Read the intended destination from the wire.
  139. SocketAddress dest_addr;
  140. size_t length = UnpackAddressFromNAT(buf, size, &dest_addr);
  141. // Find the translation for these addresses (allocating one if necessary).
  142. SocketAddressPair route(addr, dest_addr);
  143. InternalMap::iterator iter = int_map_->find(route);
  144. if (iter == int_map_->end()) {
  145. Translate(route);
  146. iter = int_map_->find(route);
  147. }
  148. RTC_DCHECK(iter != int_map_->end());
  149. // Allow the destination to send packets back to the source.
  150. iter->second->WhitelistInsert(dest_addr);
  151. // Send the packet to its intended destination.
  152. rtc::PacketOptions options;
  153. iter->second->socket->SendTo(buf + length, size - length, dest_addr, options);
  154. }
  155. void NATServer::OnExternalUDPPacket(AsyncPacketSocket* socket,
  156. const char* buf,
  157. size_t size,
  158. const SocketAddress& remote_addr,
  159. const int64_t& /* packet_time_us */) {
  160. SocketAddress local_addr = socket->GetLocalAddress();
  161. // Find the translation for this addresses.
  162. ExternalMap::iterator iter = ext_map_->find(local_addr);
  163. RTC_DCHECK(iter != ext_map_->end());
  164. // Allow the NAT to reject this packet.
  165. if (ShouldFilterOut(iter->second, remote_addr)) {
  166. RTC_LOG(LS_INFO) << "Packet from " << remote_addr.ToSensitiveString()
  167. << " was filtered out by the NAT.";
  168. return;
  169. }
  170. // Forward this packet to the internal address.
  171. // First prepend the address in a quasi-STUN format.
  172. std::unique_ptr<char[]> real_buf(new char[size + kNATEncodedIPv6AddressSize]);
  173. size_t addrlength = PackAddressForNAT(
  174. real_buf.get(), size + kNATEncodedIPv6AddressSize, remote_addr);
  175. // Copy the data part after the address.
  176. rtc::PacketOptions options;
  177. memcpy(real_buf.get() + addrlength, buf, size);
  178. udp_server_socket_->SendTo(real_buf.get(), size + addrlength,
  179. iter->second->route.source(), options);
  180. }
  181. void NATServer::Translate(const SocketAddressPair& route) {
  182. AsyncUDPSocket* socket = AsyncUDPSocket::Create(external_, external_ip_);
  183. if (!socket) {
  184. RTC_LOG(LS_ERROR) << "Couldn't find a free port!";
  185. return;
  186. }
  187. TransEntry* entry = new TransEntry(route, socket, nat_);
  188. (*int_map_)[route] = entry;
  189. (*ext_map_)[socket->GetLocalAddress()] = entry;
  190. socket->SignalReadPacket.connect(this, &NATServer::OnExternalUDPPacket);
  191. }
  192. bool NATServer::ShouldFilterOut(TransEntry* entry,
  193. const SocketAddress& ext_addr) {
  194. return entry->WhitelistContains(ext_addr);
  195. }
  196. NATServer::TransEntry::TransEntry(const SocketAddressPair& r,
  197. AsyncUDPSocket* s,
  198. NAT* nat)
  199. : route(r), socket(s) {
  200. whitelist = new AddressSet(AddrCmp(nat));
  201. }
  202. NATServer::TransEntry::~TransEntry() {
  203. delete whitelist;
  204. delete socket;
  205. }
  206. void NATServer::TransEntry::WhitelistInsert(const SocketAddress& addr) {
  207. CritScope cs(&crit_);
  208. whitelist->insert(addr);
  209. }
  210. bool NATServer::TransEntry::WhitelistContains(const SocketAddress& ext_addr) {
  211. CritScope cs(&crit_);
  212. return whitelist->find(ext_addr) == whitelist->end();
  213. }
  214. } // namespace rtc