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