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.

190 lines
6.2 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/natsocketfactory.h"
  28. #include "talk/base/natserver.h"
  29. #include "talk/base/logging.h"
  30. namespace talk_base {
  31. RouteCmp::RouteCmp(NAT* nat) : symmetric(nat->IsSymmetric()) {
  32. }
  33. size_t RouteCmp::operator()(const SocketAddressPair& r) const {
  34. size_t h = r.source().Hash();
  35. if (symmetric)
  36. h ^= r.destination().Hash();
  37. return h;
  38. }
  39. bool RouteCmp::operator()(
  40. const SocketAddressPair& r1, const SocketAddressPair& r2) const {
  41. if (r1.source() < r2.source())
  42. return true;
  43. if (r2.source() < r1.source())
  44. return false;
  45. if (symmetric && (r1.destination() < r2.destination()))
  46. return true;
  47. if (symmetric && (r2.destination() < r1.destination()))
  48. return false;
  49. return false;
  50. }
  51. AddrCmp::AddrCmp(NAT* nat)
  52. : use_ip(nat->FiltersIP()), use_port(nat->FiltersPort()) {
  53. }
  54. size_t AddrCmp::operator()(const SocketAddress& a) const {
  55. size_t h = 0;
  56. if (use_ip)
  57. h ^= HashIP(a.ipaddr());
  58. if (use_port)
  59. h ^= a.port() | (a.port() << 16);
  60. return h;
  61. }
  62. bool AddrCmp::operator()(
  63. const SocketAddress& a1, const SocketAddress& a2) const {
  64. if (use_ip && (a1.ipaddr() < a2.ipaddr()))
  65. return true;
  66. if (use_ip && (a2.ipaddr() < a1.ipaddr()))
  67. return false;
  68. if (use_port && (a1.port() < a2.port()))
  69. return true;
  70. if (use_port && (a2.port() < a1.port()))
  71. return false;
  72. return false;
  73. }
  74. NATServer::NATServer(
  75. NATType type, SocketFactory* internal, const SocketAddress& internal_addr,
  76. SocketFactory* external, const SocketAddress& external_ip)
  77. : external_(external), external_ip_(external_ip.ipaddr(), 0) {
  78. nat_ = NAT::Create(type);
  79. server_socket_ = AsyncUDPSocket::Create(internal, internal_addr);
  80. server_socket_->SignalReadPacket.connect(this, &NATServer::OnInternalPacket);
  81. int_map_ = new InternalMap(RouteCmp(nat_));
  82. ext_map_ = new ExternalMap();
  83. }
  84. NATServer::~NATServer() {
  85. for (InternalMap::iterator iter = int_map_->begin();
  86. iter != int_map_->end();
  87. iter++)
  88. delete iter->second;
  89. delete nat_;
  90. delete server_socket_;
  91. delete int_map_;
  92. delete ext_map_;
  93. }
  94. void NATServer::OnInternalPacket(
  95. AsyncPacketSocket* socket, const char* buf, size_t size,
  96. const SocketAddress& addr) {
  97. // Read the intended destination from the wire.
  98. SocketAddress dest_addr;
  99. size_t length = UnpackAddressFromNAT(buf, size, &dest_addr);
  100. // Find the translation for these addresses (allocating one if necessary).
  101. SocketAddressPair route(addr, dest_addr);
  102. InternalMap::iterator iter = int_map_->find(route);
  103. if (iter == int_map_->end()) {
  104. Translate(route);
  105. iter = int_map_->find(route);
  106. }
  107. ASSERT(iter != int_map_->end());
  108. // Allow the destination to send packets back to the source.
  109. iter->second->whitelist->insert(dest_addr);
  110. // Send the packet to its intended destination.
  111. iter->second->socket->SendTo(buf + length, size - length, dest_addr);
  112. }
  113. void NATServer::OnExternalPacket(
  114. AsyncPacketSocket* socket, const char* buf, size_t size,
  115. const SocketAddress& remote_addr) {
  116. SocketAddress local_addr = socket->GetLocalAddress();
  117. // Find the translation for this addresses.
  118. ExternalMap::iterator iter = ext_map_->find(local_addr);
  119. ASSERT(iter != ext_map_->end());
  120. // Allow the NAT to reject this packet.
  121. if (Filter(iter->second, remote_addr)) {
  122. LOG(LS_INFO) << "Packet from " << remote_addr.ToString()
  123. << " was filtered out by the NAT.";
  124. return;
  125. }
  126. // Forward this packet to the internal address.
  127. // First prepend the address in a quasi-STUN format.
  128. scoped_array<char> real_buf(new char[size + kNATEncodedIPv6AddressSize]);
  129. size_t addrlength = PackAddressForNAT(real_buf.get(),
  130. size + kNATEncodedIPv6AddressSize,
  131. remote_addr);
  132. // Copy the data part after the address.
  133. std::memcpy(real_buf.get() + addrlength, buf, size);
  134. server_socket_->SendTo(real_buf.get(), size + addrlength,
  135. iter->second->route.source());
  136. }
  137. void NATServer::Translate(const SocketAddressPair& route) {
  138. AsyncUDPSocket* socket = AsyncUDPSocket::Create(external_, external_ip_);
  139. if (!socket) {
  140. LOG(LS_ERROR) << "Couldn't find a free port!";
  141. return;
  142. }
  143. TransEntry* entry = new TransEntry(route, socket, nat_);
  144. (*int_map_)[route] = entry;
  145. (*ext_map_)[socket->GetLocalAddress()] = entry;
  146. socket->SignalReadPacket.connect(this, &NATServer::OnExternalPacket);
  147. }
  148. bool NATServer::Filter(TransEntry* entry, const SocketAddress& ext_addr) {
  149. return entry->whitelist->find(ext_addr) == entry->whitelist->end();
  150. }
  151. NATServer::TransEntry::TransEntry(
  152. const SocketAddressPair& r, AsyncUDPSocket* s, NAT* nat)
  153. : route(r), socket(s) {
  154. whitelist = new AddressSet(AddrCmp(nat));
  155. }
  156. NATServer::TransEntry::~TransEntry() {
  157. delete whitelist;
  158. delete socket;
  159. }
  160. } // namespace talk_base