henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +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 | |
| 11 | #include "webrtc/p2p/base/stunport.h" |
| 12 | |
| 13 | #include "webrtc/p2p/base/common.h" |
| 14 | #include "webrtc/p2p/base/portallocator.h" |
| 15 | #include "webrtc/p2p/base/stun.h" |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 16 | #include "webrtc/base/checks.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 17 | #include "webrtc/base/common.h" |
| 18 | #include "webrtc/base/helpers.h" |
Guo-wei Shieh | fe3bc9d | 2015-08-20 08:48:20 -0700 | [diff] [blame] | 19 | #include "webrtc/base/ipaddress.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 20 | #include "webrtc/base/logging.h" |
| 21 | #include "webrtc/base/nethelpers.h" |
| 22 | |
| 23 | namespace cricket { |
| 24 | |
| 25 | // TODO: Move these to a common place (used in relayport too) |
| 26 | const int KEEPALIVE_DELAY = 10 * 1000; // 10 seconds - sort timeouts |
honghaiz | e2af9ef | 2016-03-03 08:27:47 -0800 | [diff] [blame] | 27 | const int RETRY_TIMEOUT = 50 * 1000; // 50 seconds |
| 28 | // Lifetime chosen for STUN ports on low-cost networks. |
| 29 | const int INFINITE_LIFETIME = -1; |
| 30 | // Lifetime for STUN ports on high-cost networks: 2 minutes |
| 31 | const int HIGH_COST_PORT_KEEPALIVE_LIFETIME = 2 * 60 * 1000; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 32 | |
| 33 | // Handles a binding request sent to the STUN server. |
| 34 | class StunBindingRequest : public StunRequest { |
| 35 | public: |
honghaiz | 45b0efd | 2015-12-04 08:57:26 -0800 | [diff] [blame] | 36 | StunBindingRequest(UDPPort* port, |
| 37 | const rtc::SocketAddress& addr, |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 38 | int64_t start_time, |
honghaiz | e2af9ef | 2016-03-03 08:27:47 -0800 | [diff] [blame] | 39 | int lifetime) |
| 40 | : port_(port), |
| 41 | server_addr_(addr), |
| 42 | start_time_(start_time), |
| 43 | lifetime_(lifetime) {} |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 44 | |
| 45 | virtual ~StunBindingRequest() { |
| 46 | } |
| 47 | |
| 48 | const rtc::SocketAddress& server_addr() const { return server_addr_; } |
| 49 | |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 50 | virtual void Prepare(StunMessage* request) override { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 51 | request->SetType(STUN_BINDING_REQUEST); |
| 52 | } |
| 53 | |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 54 | virtual void OnResponse(StunMessage* response) override { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 55 | const StunAddressAttribute* addr_attr = |
| 56 | response->GetAddress(STUN_ATTR_MAPPED_ADDRESS); |
| 57 | if (!addr_attr) { |
| 58 | LOG(LS_ERROR) << "Binding response missing mapped address."; |
| 59 | } else if (addr_attr->family() != STUN_ADDRESS_IPV4 && |
| 60 | addr_attr->family() != STUN_ADDRESS_IPV6) { |
| 61 | LOG(LS_ERROR) << "Binding address has bad family"; |
| 62 | } else { |
| 63 | rtc::SocketAddress addr(addr_attr->ipaddr(), addr_attr->port()); |
| 64 | port_->OnStunBindingRequestSucceeded(server_addr_, addr); |
| 65 | } |
| 66 | |
honghaiz | e2af9ef | 2016-03-03 08:27:47 -0800 | [diff] [blame] | 67 | // The keep-alive requests will be stopped after its lifetime has passed. |
nisse | 1bffc1d | 2016-05-02 08:18:55 -0700 | [diff] [blame] | 68 | if (WithinLifetime(rtc::TimeMillis())) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 69 | port_->requests_.SendDelayed( |
honghaiz | e2af9ef | 2016-03-03 08:27:47 -0800 | [diff] [blame] | 70 | new StunBindingRequest(port_, server_addr_, start_time_, lifetime_), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 71 | port_->stun_keepalive_delay()); |
| 72 | } |
| 73 | } |
| 74 | |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 75 | virtual void OnErrorResponse(StunMessage* response) override { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 76 | const StunErrorCodeAttribute* attr = response->GetErrorCode(); |
| 77 | if (!attr) { |
| 78 | LOG(LS_ERROR) << "Bad allocate response error code"; |
| 79 | } else { |
| 80 | LOG(LS_ERROR) << "Binding error response:" |
honghaiz | e2af9ef | 2016-03-03 08:27:47 -0800 | [diff] [blame] | 81 | << " class=" << attr->eclass() |
| 82 | << " number=" << attr->number() << " reason='" |
| 83 | << attr->reason() << "'"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | port_->OnStunBindingOrResolveRequestFailed(server_addr_); |
| 87 | |
nisse | 1bffc1d | 2016-05-02 08:18:55 -0700 | [diff] [blame] | 88 | int64_t now = rtc::TimeMillis(); |
honghaiz | e2af9ef | 2016-03-03 08:27:47 -0800 | [diff] [blame] | 89 | if (WithinLifetime(now) && |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame^] | 90 | rtc::TimeDiff(now, start_time_) < RETRY_TIMEOUT) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 91 | port_->requests_.SendDelayed( |
honghaiz | e2af9ef | 2016-03-03 08:27:47 -0800 | [diff] [blame] | 92 | new StunBindingRequest(port_, server_addr_, start_time_, lifetime_), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 93 | port_->stun_keepalive_delay()); |
| 94 | } |
| 95 | } |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 96 | virtual void OnTimeout() override { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 97 | LOG(LS_ERROR) << "Binding request timed out from " |
honghaiz | e2af9ef | 2016-03-03 08:27:47 -0800 | [diff] [blame] | 98 | << port_->GetLocalAddress().ToSensitiveString() << " (" |
| 99 | << port_->Network()->name() << ")"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 100 | |
| 101 | port_->OnStunBindingOrResolveRequestFailed(server_addr_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | private: |
honghaiz | e2af9ef | 2016-03-03 08:27:47 -0800 | [diff] [blame] | 105 | // Returns true if |now| is within the lifetime of the request (a negative |
| 106 | // lifetime means infinite). |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 107 | bool WithinLifetime(int64_t now) const { |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame^] | 108 | return lifetime_ < 0 || rtc::TimeDiff(now, start_time_) <= lifetime_; |
honghaiz | e2af9ef | 2016-03-03 08:27:47 -0800 | [diff] [blame] | 109 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 110 | UDPPort* port_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 111 | const rtc::SocketAddress server_addr_; |
honghaiz | e2af9ef | 2016-03-03 08:27:47 -0800 | [diff] [blame] | 112 | |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 113 | int64_t start_time_; |
honghaiz | e2af9ef | 2016-03-03 08:27:47 -0800 | [diff] [blame] | 114 | // The time duration for which this request will be rescheduled. |
| 115 | int lifetime_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | UDPPort::AddressResolver::AddressResolver( |
| 119 | rtc::PacketSocketFactory* factory) |
| 120 | : socket_factory_(factory) {} |
| 121 | |
| 122 | UDPPort::AddressResolver::~AddressResolver() { |
| 123 | for (ResolverMap::iterator it = resolvers_.begin(); |
| 124 | it != resolvers_.end(); ++it) { |
Guo-wei Shieh | 8a4f547 | 2015-10-30 09:12:34 -0700 | [diff] [blame] | 125 | // TODO(guoweis): Change to asynchronous DNS resolution to prevent the hang |
| 126 | // when passing true to the Destroy() which is a safer way to avoid the code |
| 127 | // unloaded before the thread exits. Please see webrtc bug 5139. |
| 128 | it->second->Destroy(false); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
| 132 | void UDPPort::AddressResolver::Resolve( |
| 133 | const rtc::SocketAddress& address) { |
| 134 | if (resolvers_.find(address) != resolvers_.end()) |
| 135 | return; |
| 136 | |
| 137 | rtc::AsyncResolverInterface* resolver = |
| 138 | socket_factory_->CreateAsyncResolver(); |
| 139 | resolvers_.insert( |
| 140 | std::pair<rtc::SocketAddress, rtc::AsyncResolverInterface*>( |
| 141 | address, resolver)); |
| 142 | |
| 143 | resolver->SignalDone.connect(this, |
| 144 | &UDPPort::AddressResolver::OnResolveResult); |
| 145 | |
| 146 | resolver->Start(address); |
| 147 | } |
| 148 | |
| 149 | bool UDPPort::AddressResolver::GetResolvedAddress( |
| 150 | const rtc::SocketAddress& input, |
| 151 | int family, |
| 152 | rtc::SocketAddress* output) const { |
| 153 | ResolverMap::const_iterator it = resolvers_.find(input); |
| 154 | if (it == resolvers_.end()) |
| 155 | return false; |
| 156 | |
| 157 | return it->second->GetResolvedAddress(family, output); |
| 158 | } |
| 159 | |
| 160 | void UDPPort::AddressResolver::OnResolveResult( |
| 161 | rtc::AsyncResolverInterface* resolver) { |
| 162 | for (ResolverMap::iterator it = resolvers_.begin(); |
| 163 | it != resolvers_.end(); ++it) { |
| 164 | if (it->second == resolver) { |
| 165 | SignalDone(it->first, resolver->GetError()); |
| 166 | return; |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | UDPPort::UDPPort(rtc::Thread* thread, |
| 172 | rtc::PacketSocketFactory* factory, |
| 173 | rtc::Network* network, |
| 174 | rtc::AsyncPacketSocket* socket, |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 175 | const std::string& username, |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 176 | const std::string& password, |
Guo-wei Shieh | fe3bc9d | 2015-08-20 08:48:20 -0700 | [diff] [blame] | 177 | const std::string& origin, |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 178 | bool emit_local_for_anyaddress) |
| 179 | : Port(thread, |
| 180 | factory, |
| 181 | network, |
| 182 | socket->GetLocalAddress().ipaddr(), |
| 183 | username, |
| 184 | password), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 185 | requests_(thread), |
| 186 | socket_(socket), |
| 187 | error_(0), |
| 188 | ready_(false), |
Guo-wei Shieh | fe3bc9d | 2015-08-20 08:48:20 -0700 | [diff] [blame] | 189 | stun_keepalive_delay_(KEEPALIVE_DELAY), |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 190 | emit_local_for_anyaddress_(emit_local_for_anyaddress) { |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 191 | requests_.set_origin(origin); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | UDPPort::UDPPort(rtc::Thread* thread, |
| 195 | rtc::PacketSocketFactory* factory, |
| 196 | rtc::Network* network, |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 197 | const rtc::IPAddress& ip, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 198 | uint16_t min_port, |
| 199 | uint16_t max_port, |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 200 | const std::string& username, |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 201 | const std::string& password, |
Guo-wei Shieh | fe3bc9d | 2015-08-20 08:48:20 -0700 | [diff] [blame] | 202 | const std::string& origin, |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 203 | bool emit_local_for_anyaddress) |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 204 | : Port(thread, |
| 205 | LOCAL_PORT_TYPE, |
| 206 | factory, |
| 207 | network, |
| 208 | ip, |
| 209 | min_port, |
| 210 | max_port, |
| 211 | username, |
| 212 | password), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 213 | requests_(thread), |
| 214 | socket_(NULL), |
| 215 | error_(0), |
| 216 | ready_(false), |
Guo-wei Shieh | fe3bc9d | 2015-08-20 08:48:20 -0700 | [diff] [blame] | 217 | stun_keepalive_delay_(KEEPALIVE_DELAY), |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 218 | emit_local_for_anyaddress_(emit_local_for_anyaddress) { |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 219 | requests_.set_origin(origin); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | bool UDPPort::Init() { |
honghaiz | e2af9ef | 2016-03-03 08:27:47 -0800 | [diff] [blame] | 223 | // If this is a zero-cost network, it will keep on sending STUN binding |
| 224 | // requests indefinitely to keep the NAT binding alive. Otherwise, stop |
| 225 | // sending STUN binding requests after HIGH_COST_PORT_KEEPALIVE_LIFETIME. |
| 226 | stun_keepalive_lifetime_ = (network_cost() == 0) |
| 227 | ? INFINITE_LIFETIME |
| 228 | : HIGH_COST_PORT_KEEPALIVE_LIFETIME; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 229 | if (!SharedSocket()) { |
| 230 | ASSERT(socket_ == NULL); |
| 231 | socket_ = socket_factory()->CreateUdpSocket( |
| 232 | rtc::SocketAddress(ip(), 0), min_port(), max_port()); |
| 233 | if (!socket_) { |
| 234 | LOG_J(LS_WARNING, this) << "UDP socket creation failed"; |
| 235 | return false; |
| 236 | } |
| 237 | socket_->SignalReadPacket.connect(this, &UDPPort::OnReadPacket); |
| 238 | } |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 239 | socket_->SignalSentPacket.connect(this, &UDPPort::OnSentPacket); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 240 | socket_->SignalReadyToSend.connect(this, &UDPPort::OnReadyToSend); |
| 241 | socket_->SignalAddressReady.connect(this, &UDPPort::OnLocalAddressReady); |
| 242 | requests_.SignalSendPacket.connect(this, &UDPPort::OnSendPacket); |
| 243 | return true; |
| 244 | } |
| 245 | |
| 246 | UDPPort::~UDPPort() { |
| 247 | if (!SharedSocket()) |
| 248 | delete socket_; |
| 249 | } |
| 250 | |
| 251 | void UDPPort::PrepareAddress() { |
| 252 | ASSERT(requests_.empty()); |
| 253 | if (socket_->GetState() == rtc::AsyncPacketSocket::STATE_BOUND) { |
| 254 | OnLocalAddressReady(socket_, socket_->GetLocalAddress()); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | void UDPPort::MaybePrepareStunCandidate() { |
| 259 | // Sending binding request to the STUN server if address is available to |
| 260 | // prepare STUN candidate. |
| 261 | if (!server_addresses_.empty()) { |
| 262 | SendStunBindingRequests(); |
| 263 | } else { |
| 264 | // Port is done allocating candidates. |
| 265 | MaybeSetPortCompleteOrError(); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | Connection* UDPPort::CreateConnection(const Candidate& address, |
Honghai Zhang | f9945b2 | 2015-12-15 12:20:13 -0800 | [diff] [blame] | 270 | CandidateOrigin origin) { |
| 271 | if (!SupportsProtocol(address.protocol())) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 272 | return NULL; |
Honghai Zhang | f9945b2 | 2015-12-15 12:20:13 -0800 | [diff] [blame] | 273 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 274 | |
| 275 | if (!IsCompatibleAddress(address.address())) { |
| 276 | return NULL; |
| 277 | } |
| 278 | |
| 279 | if (SharedSocket() && Candidates()[0].type() != LOCAL_PORT_TYPE) { |
| 280 | ASSERT(false); |
| 281 | return NULL; |
| 282 | } |
| 283 | |
| 284 | Connection* conn = new ProxyConnection(this, 0, address); |
| 285 | AddConnection(conn); |
| 286 | return conn; |
| 287 | } |
| 288 | |
| 289 | int UDPPort::SendTo(const void* data, size_t size, |
| 290 | const rtc::SocketAddress& addr, |
| 291 | const rtc::PacketOptions& options, |
| 292 | bool payload) { |
| 293 | int sent = socket_->SendTo(data, size, addr, options); |
| 294 | if (sent < 0) { |
| 295 | error_ = socket_->GetError(); |
| 296 | LOG_J(LS_ERROR, this) << "UDP send of " << size |
| 297 | << " bytes failed with error " << error_; |
| 298 | } |
| 299 | return sent; |
| 300 | } |
| 301 | |
| 302 | int UDPPort::SetOption(rtc::Socket::Option opt, int value) { |
| 303 | return socket_->SetOption(opt, value); |
| 304 | } |
| 305 | |
| 306 | int UDPPort::GetOption(rtc::Socket::Option opt, int* value) { |
| 307 | return socket_->GetOption(opt, value); |
| 308 | } |
| 309 | |
| 310 | int UDPPort::GetError() { |
| 311 | return error_; |
| 312 | } |
| 313 | |
| 314 | void UDPPort::OnLocalAddressReady(rtc::AsyncPacketSocket* socket, |
| 315 | const rtc::SocketAddress& address) { |
Guo-wei Shieh | fe3bc9d | 2015-08-20 08:48:20 -0700 | [diff] [blame] | 316 | // When adapter enumeration is disabled and binding to the any address, the |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 317 | // default local address will be issued as a candidate instead if |
| 318 | // |emit_local_for_anyaddress| is true. This is to allow connectivity for |
| 319 | // applications which absolutely requires a HOST candidate. |
Guo-wei Shieh | fe3bc9d | 2015-08-20 08:48:20 -0700 | [diff] [blame] | 320 | rtc::SocketAddress addr = address; |
Guo-wei Shieh | e03cab9 | 2015-11-11 11:11:19 -0800 | [diff] [blame] | 321 | |
| 322 | // If MaybeSetDefaultLocalAddress fails, we keep the "any" IP so that at |
| 323 | // least the port is listening. |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 324 | MaybeSetDefaultLocalAddress(&addr); |
Guo-wei Shieh | fe3bc9d | 2015-08-20 08:48:20 -0700 | [diff] [blame] | 325 | |
| 326 | AddAddress(addr, addr, rtc::SocketAddress(), UDP_PROTOCOL_NAME, "", "", |
Guo-wei Shieh | 3d564c1 | 2015-08-19 16:51:15 -0700 | [diff] [blame] | 327 | LOCAL_PORT_TYPE, ICE_TYPE_PREFERENCE_HOST, 0, false); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 328 | MaybePrepareStunCandidate(); |
| 329 | } |
| 330 | |
tfarina | 20a3461 | 2015-11-02 16:20:22 -0800 | [diff] [blame] | 331 | void UDPPort::OnReadPacket(rtc::AsyncPacketSocket* socket, |
| 332 | const char* data, |
| 333 | size_t size, |
| 334 | const rtc::SocketAddress& remote_addr, |
| 335 | const rtc::PacketTime& packet_time) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 336 | ASSERT(socket == socket_); |
tfarina | 20a3461 | 2015-11-02 16:20:22 -0800 | [diff] [blame] | 337 | ASSERT(!remote_addr.IsUnresolvedIP()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 338 | |
| 339 | // Look for a response from the STUN server. |
| 340 | // Even if the response doesn't match one of our outstanding requests, we |
| 341 | // will eat it because it might be a response to a retransmitted packet, and |
| 342 | // we already cleared the request when we got the first response. |
| 343 | if (server_addresses_.find(remote_addr) != server_addresses_.end()) { |
| 344 | requests_.CheckResponse(data, size); |
| 345 | return; |
| 346 | } |
| 347 | |
| 348 | if (Connection* conn = GetConnection(remote_addr)) { |
| 349 | conn->OnReadPacket(data, size, packet_time); |
| 350 | } else { |
| 351 | Port::OnReadPacket(data, size, remote_addr, PROTO_UDP); |
| 352 | } |
| 353 | } |
| 354 | |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 355 | void UDPPort::OnSentPacket(rtc::AsyncPacketSocket* socket, |
| 356 | const rtc::SentPacket& sent_packet) { |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 357 | PortInterface::SignalSentPacket(sent_packet); |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 358 | } |
| 359 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 360 | void UDPPort::OnReadyToSend(rtc::AsyncPacketSocket* socket) { |
| 361 | Port::OnReadyToSend(); |
| 362 | } |
| 363 | |
| 364 | void UDPPort::SendStunBindingRequests() { |
| 365 | // We will keep pinging the stun server to make sure our NAT pin-hole stays |
honghaiz | 45b0efd | 2015-12-04 08:57:26 -0800 | [diff] [blame] | 366 | // open until the deadline (specified in SendStunBindingRequest). |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 367 | ASSERT(requests_.empty()); |
| 368 | |
| 369 | for (ServerAddresses::const_iterator it = server_addresses_.begin(); |
| 370 | it != server_addresses_.end(); ++it) { |
| 371 | SendStunBindingRequest(*it); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | void UDPPort::ResolveStunAddress(const rtc::SocketAddress& stun_addr) { |
| 376 | if (!resolver_) { |
| 377 | resolver_.reset(new AddressResolver(socket_factory())); |
| 378 | resolver_->SignalDone.connect(this, &UDPPort::OnResolveResult); |
| 379 | } |
| 380 | |
Honghai Zhang | 0f490a5 | 2015-12-07 12:06:20 -0800 | [diff] [blame] | 381 | LOG_J(LS_INFO, this) << "Starting STUN host lookup for " |
| 382 | << stun_addr.ToSensitiveString(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 383 | resolver_->Resolve(stun_addr); |
| 384 | } |
| 385 | |
| 386 | void UDPPort::OnResolveResult(const rtc::SocketAddress& input, |
| 387 | int error) { |
| 388 | ASSERT(resolver_.get() != NULL); |
| 389 | |
| 390 | rtc::SocketAddress resolved; |
| 391 | if (error != 0 || |
| 392 | !resolver_->GetResolvedAddress(input, ip().family(), &resolved)) { |
| 393 | LOG_J(LS_WARNING, this) << "StunPort: stun host lookup received error " |
| 394 | << error; |
| 395 | OnStunBindingOrResolveRequestFailed(input); |
| 396 | return; |
| 397 | } |
| 398 | |
| 399 | server_addresses_.erase(input); |
| 400 | |
| 401 | if (server_addresses_.find(resolved) == server_addresses_.end()) { |
| 402 | server_addresses_.insert(resolved); |
| 403 | SendStunBindingRequest(resolved); |
| 404 | } |
| 405 | } |
| 406 | |
tfarina | 20a3461 | 2015-11-02 16:20:22 -0800 | [diff] [blame] | 407 | void UDPPort::SendStunBindingRequest(const rtc::SocketAddress& stun_addr) { |
| 408 | if (stun_addr.IsUnresolvedIP()) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 409 | ResolveStunAddress(stun_addr); |
| 410 | |
| 411 | } else if (socket_->GetState() == rtc::AsyncPacketSocket::STATE_BOUND) { |
| 412 | // Check if |server_addr_| is compatible with the port's ip. |
| 413 | if (IsCompatibleAddress(stun_addr)) { |
nisse | 1bffc1d | 2016-05-02 08:18:55 -0700 | [diff] [blame] | 414 | requests_.Send(new StunBindingRequest(this, stun_addr, rtc::TimeMillis(), |
honghaiz | e2af9ef | 2016-03-03 08:27:47 -0800 | [diff] [blame] | 415 | stun_keepalive_lifetime_)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 416 | } else { |
| 417 | // Since we can't send stun messages to the server, we should mark this |
| 418 | // port ready. |
| 419 | LOG(LS_WARNING) << "STUN server address is incompatible."; |
| 420 | OnStunBindingOrResolveRequestFailed(stun_addr); |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | |
Guo-wei Shieh | e03cab9 | 2015-11-11 11:11:19 -0800 | [diff] [blame] | 425 | bool UDPPort::MaybeSetDefaultLocalAddress(rtc::SocketAddress* addr) const { |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 426 | if (!addr->IsAnyIP() || !emit_local_for_anyaddress_ || |
| 427 | !Network()->default_local_address_provider()) { |
Guo-wei Shieh | e03cab9 | 2015-11-11 11:11:19 -0800 | [diff] [blame] | 428 | return true; |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 429 | } |
| 430 | rtc::IPAddress default_address; |
| 431 | bool result = |
| 432 | Network()->default_local_address_provider()->GetDefaultLocalAddress( |
| 433 | addr->family(), &default_address); |
Guo-wei Shieh | 953eabc | 2015-11-24 12:00:33 -0800 | [diff] [blame] | 434 | if (!result || default_address.IsNil()) { |
Guo-wei Shieh | e03cab9 | 2015-11-11 11:11:19 -0800 | [diff] [blame] | 435 | return false; |
| 436 | } |
| 437 | |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 438 | addr->SetIP(default_address); |
Guo-wei Shieh | e03cab9 | 2015-11-11 11:11:19 -0800 | [diff] [blame] | 439 | return true; |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 440 | } |
| 441 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 442 | void UDPPort::OnStunBindingRequestSucceeded( |
| 443 | const rtc::SocketAddress& stun_server_addr, |
| 444 | const rtc::SocketAddress& stun_reflected_addr) { |
| 445 | if (bind_request_succeeded_servers_.find(stun_server_addr) != |
| 446 | bind_request_succeeded_servers_.end()) { |
| 447 | return; |
| 448 | } |
| 449 | bind_request_succeeded_servers_.insert(stun_server_addr); |
| 450 | |
| 451 | // If socket is shared and |stun_reflected_addr| is equal to local socket |
| 452 | // address, or if the same address has been added by another STUN server, |
| 453 | // then discarding the stun address. |
| 454 | // For STUN, related address is the local socket address. |
| 455 | if ((!SharedSocket() || stun_reflected_addr != socket_->GetLocalAddress()) && |
| 456 | !HasCandidateWithAddress(stun_reflected_addr)) { |
| 457 | |
| 458 | rtc::SocketAddress related_address = socket_->GetLocalAddress(); |
Guo-wei Shieh | e03cab9 | 2015-11-11 11:11:19 -0800 | [diff] [blame] | 459 | // If we can't stamp the related address correctly, empty it to avoid leak. |
| 460 | if (!MaybeSetDefaultLocalAddress(&related_address) || |
| 461 | !(candidate_filter() & CF_HOST)) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 462 | // If candidate filter doesn't have CF_HOST specified, empty raddr to |
| 463 | // avoid local address leakage. |
| 464 | related_address = rtc::EmptySocketAddressWithFamily( |
| 465 | related_address.family()); |
| 466 | } |
| 467 | |
Guo-wei Shieh | 3d564c1 | 2015-08-19 16:51:15 -0700 | [diff] [blame] | 468 | AddAddress(stun_reflected_addr, socket_->GetLocalAddress(), related_address, |
| 469 | UDP_PROTOCOL_NAME, "", "", STUN_PORT_TYPE, |
| 470 | ICE_TYPE_PREFERENCE_SRFLX, 0, false); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 471 | } |
| 472 | MaybeSetPortCompleteOrError(); |
| 473 | } |
| 474 | |
| 475 | void UDPPort::OnStunBindingOrResolveRequestFailed( |
| 476 | const rtc::SocketAddress& stun_server_addr) { |
| 477 | if (bind_request_failed_servers_.find(stun_server_addr) != |
| 478 | bind_request_failed_servers_.end()) { |
| 479 | return; |
| 480 | } |
| 481 | bind_request_failed_servers_.insert(stun_server_addr); |
| 482 | MaybeSetPortCompleteOrError(); |
| 483 | } |
| 484 | |
| 485 | void UDPPort::MaybeSetPortCompleteOrError() { |
| 486 | if (ready_) |
| 487 | return; |
| 488 | |
| 489 | // Do not set port ready if we are still waiting for bind responses. |
| 490 | const size_t servers_done_bind_request = bind_request_failed_servers_.size() + |
| 491 | bind_request_succeeded_servers_.size(); |
| 492 | if (server_addresses_.size() != servers_done_bind_request) { |
| 493 | return; |
| 494 | } |
| 495 | |
| 496 | // Setting ready status. |
| 497 | ready_ = true; |
| 498 | |
| 499 | // The port is "completed" if there is no stun server provided, or the bind |
| 500 | // request succeeded for any stun server, or the socket is shared. |
| 501 | if (server_addresses_.empty() || |
| 502 | bind_request_succeeded_servers_.size() > 0 || |
| 503 | SharedSocket()) { |
| 504 | SignalPortComplete(this); |
| 505 | } else { |
| 506 | SignalPortError(this); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | // TODO: merge this with SendTo above. |
| 511 | void UDPPort::OnSendPacket(const void* data, size_t size, StunRequest* req) { |
| 512 | StunBindingRequest* sreq = static_cast<StunBindingRequest*>(req); |
| 513 | rtc::PacketOptions options(DefaultDscpValue()); |
| 514 | if (socket_->SendTo(data, size, sreq->server_addr(), options) < 0) |
| 515 | PLOG(LERROR, socket_->GetError()) << "sendto"; |
| 516 | } |
| 517 | |
| 518 | bool UDPPort::HasCandidateWithAddress(const rtc::SocketAddress& addr) const { |
| 519 | const std::vector<Candidate>& existing_candidates = Candidates(); |
| 520 | std::vector<Candidate>::const_iterator it = existing_candidates.begin(); |
| 521 | for (; it != existing_candidates.end(); ++it) { |
| 522 | if (it->address() == addr) |
| 523 | return true; |
| 524 | } |
| 525 | return false; |
| 526 | } |
| 527 | |
| 528 | } // namespace cricket |