blob: 8ed8c44887238ca5e260113783a96e21e6731a76 [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
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 Shieh9af97f82015-11-10 14:47:39 -080016#include "webrtc/base/checks.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000017#include "webrtc/base/common.h"
18#include "webrtc/base/helpers.h"
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -070019#include "webrtc/base/ipaddress.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000020#include "webrtc/base/logging.h"
21#include "webrtc/base/nethelpers.h"
22
23namespace cricket {
24
25// TODO: Move these to a common place (used in relayport too)
26const int KEEPALIVE_DELAY = 10 * 1000; // 10 seconds - sort timeouts
honghaize2af9ef2016-03-03 08:27:47 -080027const int RETRY_TIMEOUT = 50 * 1000; // 50 seconds
28// Lifetime chosen for STUN ports on low-cost networks.
29const int INFINITE_LIFETIME = -1;
30// Lifetime for STUN ports on high-cost networks: 2 minutes
31const int HIGH_COST_PORT_KEEPALIVE_LIFETIME = 2 * 60 * 1000;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000032
33// Handles a binding request sent to the STUN server.
34class StunBindingRequest : public StunRequest {
35 public:
honghaiz45b0efd2015-12-04 08:57:26 -080036 StunBindingRequest(UDPPort* port,
37 const rtc::SocketAddress& addr,
honghaiz34b11eb2016-03-16 08:55:44 -070038 int64_t start_time,
honghaize2af9ef2016-03-03 08:27:47 -080039 int lifetime)
40 : port_(port),
41 server_addr_(addr),
42 start_time_(start_time),
43 lifetime_(lifetime) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000044
45 virtual ~StunBindingRequest() {
46 }
47
48 const rtc::SocketAddress& server_addr() const { return server_addr_; }
49
Peter Thatcher1cf6f812015-05-15 10:40:45 -070050 virtual void Prepare(StunMessage* request) override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000051 request->SetType(STUN_BINDING_REQUEST);
52 }
53
Peter Thatcher1cf6f812015-05-15 10:40:45 -070054 virtual void OnResponse(StunMessage* response) override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000055 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
honghaize2af9ef2016-03-03 08:27:47 -080067 // The keep-alive requests will be stopped after its lifetime has passed.
nisse1bffc1d2016-05-02 08:18:55 -070068 if (WithinLifetime(rtc::TimeMillis())) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000069 port_->requests_.SendDelayed(
honghaize2af9ef2016-03-03 08:27:47 -080070 new StunBindingRequest(port_, server_addr_, start_time_, lifetime_),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000071 port_->stun_keepalive_delay());
72 }
73 }
74
Peter Thatcher1cf6f812015-05-15 10:40:45 -070075 virtual void OnErrorResponse(StunMessage* response) override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000076 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:"
honghaize2af9ef2016-03-03 08:27:47 -080081 << " class=" << attr->eclass()
82 << " number=" << attr->number() << " reason='"
83 << attr->reason() << "'";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000084 }
85
86 port_->OnStunBindingOrResolveRequestFailed(server_addr_);
87
nisse1bffc1d2016-05-02 08:18:55 -070088 int64_t now = rtc::TimeMillis();
honghaize2af9ef2016-03-03 08:27:47 -080089 if (WithinLifetime(now) &&
Honghai Zhang82d78622016-05-06 11:29:15 -070090 rtc::TimeDiff(now, start_time_) < RETRY_TIMEOUT) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000091 port_->requests_.SendDelayed(
honghaize2af9ef2016-03-03 08:27:47 -080092 new StunBindingRequest(port_, server_addr_, start_time_, lifetime_),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000093 port_->stun_keepalive_delay());
94 }
95 }
Peter Thatcher1cf6f812015-05-15 10:40:45 -070096 virtual void OnTimeout() override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000097 LOG(LS_ERROR) << "Binding request timed out from "
honghaize2af9ef2016-03-03 08:27:47 -080098 << port_->GetLocalAddress().ToSensitiveString() << " ("
99 << port_->Network()->name() << ")";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000100
101 port_->OnStunBindingOrResolveRequestFailed(server_addr_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000102 }
103
104 private:
honghaize2af9ef2016-03-03 08:27:47 -0800105 // Returns true if |now| is within the lifetime of the request (a negative
106 // lifetime means infinite).
honghaiz34b11eb2016-03-16 08:55:44 -0700107 bool WithinLifetime(int64_t now) const {
Honghai Zhang82d78622016-05-06 11:29:15 -0700108 return lifetime_ < 0 || rtc::TimeDiff(now, start_time_) <= lifetime_;
honghaize2af9ef2016-03-03 08:27:47 -0800109 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000110 UDPPort* port_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000111 const rtc::SocketAddress server_addr_;
honghaize2af9ef2016-03-03 08:27:47 -0800112
honghaiz34b11eb2016-03-16 08:55:44 -0700113 int64_t start_time_;
honghaize2af9ef2016-03-03 08:27:47 -0800114 // The time duration for which this request will be rescheduled.
115 int lifetime_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000116};
117
118UDPPort::AddressResolver::AddressResolver(
119 rtc::PacketSocketFactory* factory)
120 : socket_factory_(factory) {}
121
122UDPPort::AddressResolver::~AddressResolver() {
123 for (ResolverMap::iterator it = resolvers_.begin();
124 it != resolvers_.end(); ++it) {
Guo-wei Shieh8a4f5472015-10-30 09:12:34 -0700125 // 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.org269fb4b2014-10-28 22:20:11 +0000129 }
130}
131
132void 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
149bool 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
160void 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
171UDPPort::UDPPort(rtc::Thread* thread,
172 rtc::PacketSocketFactory* factory,
173 rtc::Network* network,
174 rtc::AsyncPacketSocket* socket,
pkasting@chromium.org332331f2014-11-06 20:19:22 +0000175 const std::string& username,
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000176 const std::string& password,
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700177 const std::string& origin,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800178 bool emit_local_for_anyaddress)
179 : Port(thread,
180 factory,
181 network,
182 socket->GetLocalAddress().ipaddr(),
183 username,
184 password),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000185 requests_(thread),
186 socket_(socket),
187 error_(0),
188 ready_(false),
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700189 stun_keepalive_delay_(KEEPALIVE_DELAY),
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800190 emit_local_for_anyaddress_(emit_local_for_anyaddress) {
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000191 requests_.set_origin(origin);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000192}
193
194UDPPort::UDPPort(rtc::Thread* thread,
195 rtc::PacketSocketFactory* factory,
196 rtc::Network* network,
pkasting@chromium.org332331f2014-11-06 20:19:22 +0000197 const rtc::IPAddress& ip,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200198 uint16_t min_port,
199 uint16_t max_port,
pkasting@chromium.org332331f2014-11-06 20:19:22 +0000200 const std::string& username,
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000201 const std::string& password,
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700202 const std::string& origin,
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800203 bool emit_local_for_anyaddress)
Peter Boström0c4e06b2015-10-07 12:23:21 +0200204 : Port(thread,
205 LOCAL_PORT_TYPE,
206 factory,
207 network,
208 ip,
209 min_port,
210 max_port,
211 username,
212 password),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000213 requests_(thread),
214 socket_(NULL),
215 error_(0),
216 ready_(false),
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700217 stun_keepalive_delay_(KEEPALIVE_DELAY),
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800218 emit_local_for_anyaddress_(emit_local_for_anyaddress) {
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000219 requests_.set_origin(origin);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000220}
221
222bool UDPPort::Init() {
honghaize2af9ef2016-03-03 08:27:47 -0800223 // 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.org269fb4b2014-10-28 22:20:11 +0000229 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 }
stefanc1aeaf02015-10-15 07:26:07 -0700239 socket_->SignalSentPacket.connect(this, &UDPPort::OnSentPacket);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000240 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
246UDPPort::~UDPPort() {
247 if (!SharedSocket())
248 delete socket_;
249}
250
251void UDPPort::PrepareAddress() {
252 ASSERT(requests_.empty());
253 if (socket_->GetState() == rtc::AsyncPacketSocket::STATE_BOUND) {
254 OnLocalAddressReady(socket_, socket_->GetLocalAddress());
255 }
256}
257
258void 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
269Connection* UDPPort::CreateConnection(const Candidate& address,
Honghai Zhangf9945b22015-12-15 12:20:13 -0800270 CandidateOrigin origin) {
271 if (!SupportsProtocol(address.protocol())) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000272 return NULL;
Honghai Zhangf9945b22015-12-15 12:20:13 -0800273 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000274
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
289int 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
302int UDPPort::SetOption(rtc::Socket::Option opt, int value) {
303 return socket_->SetOption(opt, value);
304}
305
306int UDPPort::GetOption(rtc::Socket::Option opt, int* value) {
307 return socket_->GetOption(opt, value);
308}
309
310int UDPPort::GetError() {
311 return error_;
312}
313
314void UDPPort::OnLocalAddressReady(rtc::AsyncPacketSocket* socket,
315 const rtc::SocketAddress& address) {
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700316 // When adapter enumeration is disabled and binding to the any address, the
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800317 // 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 Shiehfe3bc9d2015-08-20 08:48:20 -0700320 rtc::SocketAddress addr = address;
Guo-wei Shiehe03cab92015-11-11 11:11:19 -0800321
322 // If MaybeSetDefaultLocalAddress fails, we keep the "any" IP so that at
323 // least the port is listening.
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800324 MaybeSetDefaultLocalAddress(&addr);
Guo-wei Shiehfe3bc9d2015-08-20 08:48:20 -0700325
326 AddAddress(addr, addr, rtc::SocketAddress(), UDP_PROTOCOL_NAME, "", "",
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700327 LOCAL_PORT_TYPE, ICE_TYPE_PREFERENCE_HOST, 0, false);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000328 MaybePrepareStunCandidate();
329}
330
tfarina20a34612015-11-02 16:20:22 -0800331void 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.org269fb4b2014-10-28 22:20:11 +0000336 ASSERT(socket == socket_);
tfarina20a34612015-11-02 16:20:22 -0800337 ASSERT(!remote_addr.IsUnresolvedIP());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000338
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
stefanc1aeaf02015-10-15 07:26:07 -0700355void UDPPort::OnSentPacket(rtc::AsyncPacketSocket* socket,
356 const rtc::SentPacket& sent_packet) {
Stefan Holmer55674ff2016-01-14 15:49:16 +0100357 PortInterface::SignalSentPacket(sent_packet);
stefanc1aeaf02015-10-15 07:26:07 -0700358}
359
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000360void UDPPort::OnReadyToSend(rtc::AsyncPacketSocket* socket) {
361 Port::OnReadyToSend();
362}
363
364void UDPPort::SendStunBindingRequests() {
365 // We will keep pinging the stun server to make sure our NAT pin-hole stays
honghaiz45b0efd2015-12-04 08:57:26 -0800366 // open until the deadline (specified in SendStunBindingRequest).
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000367 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
375void 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 Zhang0f490a52015-12-07 12:06:20 -0800381 LOG_J(LS_INFO, this) << "Starting STUN host lookup for "
382 << stun_addr.ToSensitiveString();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000383 resolver_->Resolve(stun_addr);
384}
385
386void 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
tfarina20a34612015-11-02 16:20:22 -0800407void UDPPort::SendStunBindingRequest(const rtc::SocketAddress& stun_addr) {
408 if (stun_addr.IsUnresolvedIP()) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000409 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)) {
nisse1bffc1d2016-05-02 08:18:55 -0700414 requests_.Send(new StunBindingRequest(this, stun_addr, rtc::TimeMillis(),
honghaize2af9ef2016-03-03 08:27:47 -0800415 stun_keepalive_lifetime_));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000416 } 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 Shiehe03cab92015-11-11 11:11:19 -0800425bool UDPPort::MaybeSetDefaultLocalAddress(rtc::SocketAddress* addr) const {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800426 if (!addr->IsAnyIP() || !emit_local_for_anyaddress_ ||
427 !Network()->default_local_address_provider()) {
Guo-wei Shiehe03cab92015-11-11 11:11:19 -0800428 return true;
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800429 }
430 rtc::IPAddress default_address;
431 bool result =
432 Network()->default_local_address_provider()->GetDefaultLocalAddress(
433 addr->family(), &default_address);
Guo-wei Shieh953eabc2015-11-24 12:00:33 -0800434 if (!result || default_address.IsNil()) {
Guo-wei Shiehe03cab92015-11-11 11:11:19 -0800435 return false;
436 }
437
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800438 addr->SetIP(default_address);
Guo-wei Shiehe03cab92015-11-11 11:11:19 -0800439 return true;
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800440}
441
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000442void 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 Shiehe03cab92015-11-11 11:11:19 -0800459 // 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.org269fb4b2014-10-28 22:20:11 +0000462 // 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 Shieh3d564c12015-08-19 16:51:15 -0700468 AddAddress(stun_reflected_addr, socket_->GetLocalAddress(), related_address,
469 UDP_PROTOCOL_NAME, "", "", STUN_PORT_TYPE,
470 ICE_TYPE_PREFERENCE_SRFLX, 0, false);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000471 }
472 MaybeSetPortCompleteOrError();
473}
474
475void 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
485void 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.
511void 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
518bool 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