2013-07-10 02:45:36 +02:00
|
|
|
/*
|
|
|
|
* libjingle
|
|
|
|
* Copyright 2004--2005, Google Inc.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
|
|
|
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
|
|
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
|
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "talk/p2p/base/stunport.h"
|
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
#include "webrtc/base/common.h"
|
|
|
|
#include "webrtc/base/logging.h"
|
|
|
|
#include "webrtc/base/helpers.h"
|
|
|
|
#include "webrtc/base/nethelpers.h"
|
2013-07-10 02:45:36 +02:00
|
|
|
#include "talk/p2p/base/common.h"
|
|
|
|
#include "talk/p2p/base/stun.h"
|
|
|
|
|
|
|
|
namespace cricket {
|
|
|
|
|
|
|
|
// TODO: Move these to a common place (used in relayport too)
|
|
|
|
const int KEEPALIVE_DELAY = 10 * 1000; // 10 seconds - sort timeouts
|
|
|
|
const int RETRY_DELAY = 50; // 50ms, from ICE spec
|
|
|
|
const int RETRY_TIMEOUT = 50 * 1000; // ICE says 50 secs
|
|
|
|
|
|
|
|
// Handles a binding request sent to the STUN server.
|
|
|
|
class StunBindingRequest : public StunRequest {
|
|
|
|
public:
|
|
|
|
StunBindingRequest(UDPPort* port, bool keep_alive,
|
2014-07-29 19:36:52 +02:00
|
|
|
const rtc::SocketAddress& addr)
|
2013-07-10 02:45:36 +02:00
|
|
|
: port_(port), keep_alive_(keep_alive), server_addr_(addr) {
|
2014-07-29 19:36:52 +02:00
|
|
|
start_time_ = rtc::Time();
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~StunBindingRequest() {
|
|
|
|
}
|
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
const rtc::SocketAddress& server_addr() const { return server_addr_; }
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
virtual void Prepare(StunMessage* request) {
|
|
|
|
request->SetType(STUN_BINDING_REQUEST);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void OnResponse(StunMessage* response) {
|
|
|
|
const StunAddressAttribute* addr_attr =
|
|
|
|
response->GetAddress(STUN_ATTR_MAPPED_ADDRESS);
|
|
|
|
if (!addr_attr) {
|
|
|
|
LOG(LS_ERROR) << "Binding response missing mapped address.";
|
|
|
|
} else if (addr_attr->family() != STUN_ADDRESS_IPV4 &&
|
|
|
|
addr_attr->family() != STUN_ADDRESS_IPV6) {
|
|
|
|
LOG(LS_ERROR) << "Binding address has bad family";
|
|
|
|
} else {
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::SocketAddress addr(addr_attr->ipaddr(), addr_attr->port());
|
2014-07-29 00:26:15 +02:00
|
|
|
port_->OnStunBindingRequestSucceeded(server_addr_, addr);
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
|
|
|
|
2014-07-29 00:26:15 +02:00
|
|
|
// We will do a keep-alive regardless of whether this request succeeds.
|
2013-07-10 02:45:36 +02:00
|
|
|
// This should have almost no impact on network usage.
|
|
|
|
if (keep_alive_) {
|
|
|
|
port_->requests_.SendDelayed(
|
|
|
|
new StunBindingRequest(port_, true, server_addr_),
|
|
|
|
port_->stun_keepalive_delay());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void OnErrorResponse(StunMessage* response) {
|
|
|
|
const StunErrorCodeAttribute* attr = response->GetErrorCode();
|
|
|
|
if (!attr) {
|
|
|
|
LOG(LS_ERROR) << "Bad allocate response error code";
|
|
|
|
} else {
|
|
|
|
LOG(LS_ERROR) << "Binding error response:"
|
|
|
|
<< " class=" << attr->eclass()
|
|
|
|
<< " number=" << attr->number()
|
|
|
|
<< " reason='" << attr->reason() << "'";
|
|
|
|
}
|
|
|
|
|
2014-07-29 00:26:15 +02:00
|
|
|
port_->OnStunBindingOrResolveRequestFailed(server_addr_);
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
if (keep_alive_
|
2014-07-29 19:36:52 +02:00
|
|
|
&& (rtc::TimeSince(start_time_) <= RETRY_TIMEOUT)) {
|
2013-07-10 02:45:36 +02:00
|
|
|
port_->requests_.SendDelayed(
|
|
|
|
new StunBindingRequest(port_, true, server_addr_),
|
|
|
|
port_->stun_keepalive_delay());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void OnTimeout() {
|
|
|
|
LOG(LS_ERROR) << "Binding request timed out from "
|
|
|
|
<< port_->GetLocalAddress().ToSensitiveString()
|
|
|
|
<< " (" << port_->Network()->name() << ")";
|
|
|
|
|
2014-07-29 00:26:15 +02:00
|
|
|
port_->OnStunBindingOrResolveRequestFailed(server_addr_);
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
if (keep_alive_
|
2014-07-29 19:36:52 +02:00
|
|
|
&& (rtc::TimeSince(start_time_) <= RETRY_TIMEOUT)) {
|
2013-07-10 02:45:36 +02:00
|
|
|
port_->requests_.SendDelayed(
|
|
|
|
new StunBindingRequest(port_, true, server_addr_),
|
|
|
|
RETRY_DELAY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
UDPPort* port_;
|
|
|
|
bool keep_alive_;
|
2014-07-29 19:36:52 +02:00
|
|
|
const rtc::SocketAddress server_addr_;
|
2013-07-10 02:45:36 +02:00
|
|
|
uint32 start_time_;
|
|
|
|
};
|
|
|
|
|
2014-07-29 00:26:15 +02:00
|
|
|
UDPPort::AddressResolver::AddressResolver(
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::PacketSocketFactory* factory)
|
2014-07-29 00:26:15 +02:00
|
|
|
: socket_factory_(factory) {}
|
|
|
|
|
|
|
|
UDPPort::AddressResolver::~AddressResolver() {
|
|
|
|
for (ResolverMap::iterator it = resolvers_.begin();
|
|
|
|
it != resolvers_.end(); ++it) {
|
|
|
|
it->second->Destroy(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UDPPort::AddressResolver::Resolve(
|
2014-07-29 19:36:52 +02:00
|
|
|
const rtc::SocketAddress& address) {
|
2014-07-29 00:26:15 +02:00
|
|
|
if (resolvers_.find(address) != resolvers_.end())
|
|
|
|
return;
|
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::AsyncResolverInterface* resolver =
|
2014-07-29 00:26:15 +02:00
|
|
|
socket_factory_->CreateAsyncResolver();
|
|
|
|
resolvers_.insert(
|
2014-07-29 19:36:52 +02:00
|
|
|
std::pair<rtc::SocketAddress, rtc::AsyncResolverInterface*>(
|
2014-07-29 00:26:15 +02:00
|
|
|
address, resolver));
|
|
|
|
|
|
|
|
resolver->SignalDone.connect(this,
|
|
|
|
&UDPPort::AddressResolver::OnResolveResult);
|
|
|
|
|
|
|
|
resolver->Start(address);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UDPPort::AddressResolver::GetResolvedAddress(
|
2014-07-29 19:36:52 +02:00
|
|
|
const rtc::SocketAddress& input,
|
2014-07-29 00:26:15 +02:00
|
|
|
int family,
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::SocketAddress* output) const {
|
2014-07-29 00:26:15 +02:00
|
|
|
ResolverMap::const_iterator it = resolvers_.find(input);
|
|
|
|
if (it == resolvers_.end())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return it->second->GetResolvedAddress(family, output);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UDPPort::AddressResolver::OnResolveResult(
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::AsyncResolverInterface* resolver) {
|
2014-07-29 00:26:15 +02:00
|
|
|
for (ResolverMap::iterator it = resolvers_.begin();
|
|
|
|
it != resolvers_.end(); ++it) {
|
|
|
|
if (it->second == resolver) {
|
|
|
|
SignalDone(it->first, resolver->GetError());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
UDPPort::UDPPort(rtc::Thread* thread,
|
|
|
|
rtc::PacketSocketFactory* factory,
|
|
|
|
rtc::Network* network,
|
|
|
|
rtc::AsyncPacketSocket* socket,
|
2013-07-10 02:45:36 +02:00
|
|
|
const std::string& username, const std::string& password)
|
2013-11-13 23:48:52 +01:00
|
|
|
: Port(thread, factory, network, socket->GetLocalAddress().ipaddr(),
|
2013-07-10 02:45:36 +02:00
|
|
|
username, password),
|
|
|
|
requests_(thread),
|
|
|
|
socket_(socket),
|
|
|
|
error_(0),
|
|
|
|
ready_(false),
|
|
|
|
stun_keepalive_delay_(KEEPALIVE_DELAY) {
|
|
|
|
}
|
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
UDPPort::UDPPort(rtc::Thread* thread,
|
|
|
|
rtc::PacketSocketFactory* factory,
|
|
|
|
rtc::Network* network,
|
|
|
|
const rtc::IPAddress& ip, int min_port, int max_port,
|
2013-11-13 23:48:52 +01:00
|
|
|
const std::string& username, const std::string& password)
|
2013-07-10 02:45:36 +02:00
|
|
|
: Port(thread, LOCAL_PORT_TYPE, factory, network, ip, min_port, max_port,
|
|
|
|
username, password),
|
|
|
|
requests_(thread),
|
|
|
|
socket_(NULL),
|
|
|
|
error_(0),
|
|
|
|
ready_(false),
|
|
|
|
stun_keepalive_delay_(KEEPALIVE_DELAY) {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UDPPort::Init() {
|
|
|
|
if (!SharedSocket()) {
|
|
|
|
ASSERT(socket_ == NULL);
|
|
|
|
socket_ = socket_factory()->CreateUdpSocket(
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::SocketAddress(ip(), 0), min_port(), max_port());
|
2013-07-10 02:45:36 +02:00
|
|
|
if (!socket_) {
|
|
|
|
LOG_J(LS_WARNING, this) << "UDP socket creation failed";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
socket_->SignalReadPacket.connect(this, &UDPPort::OnReadPacket);
|
|
|
|
}
|
|
|
|
socket_->SignalReadyToSend.connect(this, &UDPPort::OnReadyToSend);
|
|
|
|
socket_->SignalAddressReady.connect(this, &UDPPort::OnLocalAddressReady);
|
|
|
|
requests_.SignalSendPacket.connect(this, &UDPPort::OnSendPacket);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
UDPPort::~UDPPort() {
|
|
|
|
if (!SharedSocket())
|
|
|
|
delete socket_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UDPPort::PrepareAddress() {
|
|
|
|
ASSERT(requests_.empty());
|
2014-07-29 19:36:52 +02:00
|
|
|
if (socket_->GetState() == rtc::AsyncPacketSocket::STATE_BOUND) {
|
2013-07-10 02:45:36 +02:00
|
|
|
OnLocalAddressReady(socket_, socket_->GetLocalAddress());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UDPPort::MaybePrepareStunCandidate() {
|
|
|
|
// Sending binding request to the STUN server if address is available to
|
|
|
|
// prepare STUN candidate.
|
2014-07-29 00:26:15 +02:00
|
|
|
if (!server_addresses_.empty()) {
|
|
|
|
SendStunBindingRequests();
|
2013-07-10 02:45:36 +02:00
|
|
|
} else {
|
2014-05-14 00:58:27 +02:00
|
|
|
// Port is done allocating candidates.
|
2014-07-29 00:26:15 +02:00
|
|
|
MaybeSetPortCompleteOrError();
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Connection* UDPPort::CreateConnection(const Candidate& address,
|
|
|
|
CandidateOrigin origin) {
|
|
|
|
if (address.protocol() != "udp")
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!IsCompatibleAddress(address.address())) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SharedSocket() && Candidates()[0].type() != LOCAL_PORT_TYPE) {
|
|
|
|
ASSERT(false);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Connection* conn = new ProxyConnection(this, 0, address);
|
|
|
|
AddConnection(conn);
|
|
|
|
return conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
int UDPPort::SendTo(const void* data, size_t size,
|
2014-07-29 19:36:52 +02:00
|
|
|
const rtc::SocketAddress& addr,
|
|
|
|
const rtc::PacketOptions& options,
|
2013-09-23 22:34:45 +02:00
|
|
|
bool payload) {
|
2014-02-14 01:56:12 +01:00
|
|
|
int sent = socket_->SendTo(data, size, addr, options);
|
2013-07-10 02:45:36 +02:00
|
|
|
if (sent < 0) {
|
|
|
|
error_ = socket_->GetError();
|
|
|
|
LOG_J(LS_ERROR, this) << "UDP send of " << size
|
|
|
|
<< " bytes failed with error " << error_;
|
|
|
|
}
|
|
|
|
return sent;
|
|
|
|
}
|
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
int UDPPort::SetOption(rtc::Socket::Option opt, int value) {
|
2013-07-10 02:45:36 +02:00
|
|
|
return socket_->SetOption(opt, value);
|
|
|
|
}
|
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
int UDPPort::GetOption(rtc::Socket::Option opt, int* value) {
|
2013-07-10 02:45:36 +02:00
|
|
|
return socket_->GetOption(opt, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
int UDPPort::GetError() {
|
|
|
|
return error_;
|
|
|
|
}
|
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
void UDPPort::OnLocalAddressReady(rtc::AsyncPacketSocket* socket,
|
|
|
|
const rtc::SocketAddress& address) {
|
|
|
|
AddAddress(address, address, rtc::SocketAddress(),
|
2014-08-09 00:29:20 +02:00
|
|
|
UDP_PROTOCOL_NAME, "", LOCAL_PORT_TYPE,
|
|
|
|
ICE_TYPE_PREFERENCE_HOST, 0, false);
|
2013-07-10 02:45:36 +02:00
|
|
|
MaybePrepareStunCandidate();
|
|
|
|
}
|
|
|
|
|
2013-12-13 01:21:03 +01:00
|
|
|
void UDPPort::OnReadPacket(
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::AsyncPacketSocket* socket, const char* data, size_t size,
|
|
|
|
const rtc::SocketAddress& remote_addr,
|
|
|
|
const rtc::PacketTime& packet_time) {
|
2013-07-10 02:45:36 +02:00
|
|
|
ASSERT(socket == socket_);
|
2014-07-29 00:26:15 +02:00
|
|
|
ASSERT(!remote_addr.IsUnresolved());
|
2013-07-10 02:45:36 +02:00
|
|
|
|
|
|
|
// Look for a response from the STUN server.
|
|
|
|
// Even if the response doesn't match one of our outstanding requests, we
|
|
|
|
// will eat it because it might be a response to a retransmitted packet, and
|
|
|
|
// we already cleared the request when we got the first response.
|
2014-07-29 00:26:15 +02:00
|
|
|
if (server_addresses_.find(remote_addr) != server_addresses_.end()) {
|
2013-07-10 02:45:36 +02:00
|
|
|
requests_.CheckResponse(data, size);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Connection* conn = GetConnection(remote_addr)) {
|
2013-12-13 01:21:03 +01:00
|
|
|
conn->OnReadPacket(data, size, packet_time);
|
2013-07-10 02:45:36 +02:00
|
|
|
} else {
|
|
|
|
Port::OnReadPacket(data, size, remote_addr, PROTO_UDP);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
void UDPPort::OnReadyToSend(rtc::AsyncPacketSocket* socket) {
|
2013-07-10 02:45:36 +02:00
|
|
|
Port::OnReadyToSend();
|
|
|
|
}
|
|
|
|
|
2014-07-29 00:26:15 +02:00
|
|
|
void UDPPort::SendStunBindingRequests() {
|
2013-07-10 02:45:36 +02:00
|
|
|
// We will keep pinging the stun server to make sure our NAT pin-hole stays
|
|
|
|
// open during the call.
|
|
|
|
ASSERT(requests_.empty());
|
2014-07-29 00:26:15 +02:00
|
|
|
|
|
|
|
for (ServerAddresses::const_iterator it = server_addresses_.begin();
|
|
|
|
it != server_addresses_.end(); ++it) {
|
|
|
|
SendStunBindingRequest(*it);
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
void UDPPort::ResolveStunAddress(const rtc::SocketAddress& stun_addr) {
|
2014-07-29 00:26:15 +02:00
|
|
|
if (!resolver_) {
|
|
|
|
resolver_.reset(new AddressResolver(socket_factory()));
|
|
|
|
resolver_->SignalDone.connect(this, &UDPPort::OnResolveResult);
|
|
|
|
}
|
2013-07-10 02:45:36 +02:00
|
|
|
|
2014-07-29 00:26:15 +02:00
|
|
|
resolver_->Resolve(stun_addr);
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
void UDPPort::OnResolveResult(const rtc::SocketAddress& input,
|
2014-07-29 00:26:15 +02:00
|
|
|
int error) {
|
|
|
|
ASSERT(resolver_.get() != NULL);
|
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::SocketAddress resolved;
|
2014-07-29 00:26:15 +02:00
|
|
|
if (error != 0 ||
|
|
|
|
!resolver_->GetResolvedAddress(input, ip().family(), &resolved)) {
|
2013-07-10 02:45:36 +02:00
|
|
|
LOG_J(LS_WARNING, this) << "StunPort: stun host lookup received error "
|
2014-07-29 00:26:15 +02:00
|
|
|
<< error;
|
|
|
|
OnStunBindingOrResolveRequestFailed(input);
|
2013-11-13 23:48:52 +01:00
|
|
|
return;
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
|
|
|
|
2014-07-29 00:26:15 +02:00
|
|
|
server_addresses_.erase(input);
|
|
|
|
|
|
|
|
if (server_addresses_.find(resolved) == server_addresses_.end()) {
|
|
|
|
server_addresses_.insert(resolved);
|
|
|
|
SendStunBindingRequest(resolved);
|
|
|
|
}
|
2014-07-23 23:09:01 +02:00
|
|
|
}
|
|
|
|
|
2014-07-29 00:26:15 +02:00
|
|
|
void UDPPort::SendStunBindingRequest(
|
2014-07-29 19:36:52 +02:00
|
|
|
const rtc::SocketAddress& stun_addr) {
|
2014-07-29 00:26:15 +02:00
|
|
|
if (stun_addr.IsUnresolved()) {
|
|
|
|
ResolveStunAddress(stun_addr);
|
|
|
|
|
2014-07-29 19:36:52 +02:00
|
|
|
} else if (socket_->GetState() == rtc::AsyncPacketSocket::STATE_BOUND) {
|
2014-07-29 00:26:15 +02:00
|
|
|
// Check if |server_addr_| is compatible with the port's ip.
|
|
|
|
if (IsCompatibleAddress(stun_addr)) {
|
|
|
|
requests_.Send(new StunBindingRequest(this, true, stun_addr));
|
|
|
|
} else {
|
|
|
|
// Since we can't send stun messages to the server, we should mark this
|
|
|
|
// port ready.
|
|
|
|
LOG(LS_WARNING) << "STUN server address is incompatible.";
|
|
|
|
OnStunBindingOrResolveRequestFailed(stun_addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UDPPort::OnStunBindingRequestSucceeded(
|
2014-07-29 19:36:52 +02:00
|
|
|
const rtc::SocketAddress& stun_server_addr,
|
|
|
|
const rtc::SocketAddress& stun_reflected_addr) {
|
2014-07-29 00:26:15 +02:00
|
|
|
if (bind_request_succeeded_servers_.find(stun_server_addr) !=
|
|
|
|
bind_request_succeeded_servers_.end()) {
|
2013-07-10 02:45:36 +02:00
|
|
|
return;
|
2014-07-29 00:26:15 +02:00
|
|
|
}
|
|
|
|
bind_request_succeeded_servers_.insert(stun_server_addr);
|
2013-07-10 02:45:36 +02:00
|
|
|
|
2014-07-29 00:26:15 +02:00
|
|
|
if (!SharedSocket() || stun_reflected_addr != socket_->GetLocalAddress()) {
|
|
|
|
// If socket is shared and |stun_reflected_addr| is equal to local socket
|
2013-07-10 02:45:36 +02:00
|
|
|
// address then discarding the stun address.
|
2014-04-14 18:06:21 +02:00
|
|
|
// For STUN related address is local socket address.
|
2014-07-29 00:26:15 +02:00
|
|
|
AddAddress(stun_reflected_addr, socket_->GetLocalAddress(),
|
2014-08-09 00:29:20 +02:00
|
|
|
socket_->GetLocalAddress(), UDP_PROTOCOL_NAME, "",
|
|
|
|
STUN_PORT_TYPE, ICE_TYPE_PREFERENCE_SRFLX, 0, false);
|
2013-07-10 02:45:36 +02:00
|
|
|
}
|
2014-07-29 00:26:15 +02:00
|
|
|
MaybeSetPortCompleteOrError();
|
2014-07-19 00:28:36 +02:00
|
|
|
}
|
2014-07-18 23:34:11 +02:00
|
|
|
|
2014-07-29 00:26:15 +02:00
|
|
|
void UDPPort::OnStunBindingOrResolveRequestFailed(
|
2014-07-29 19:36:52 +02:00
|
|
|
const rtc::SocketAddress& stun_server_addr) {
|
2014-07-29 00:26:15 +02:00
|
|
|
if (bind_request_failed_servers_.find(stun_server_addr) !=
|
|
|
|
bind_request_failed_servers_.end()) {
|
2014-07-23 23:09:01 +02:00
|
|
|
return;
|
2014-07-29 00:26:15 +02:00
|
|
|
}
|
|
|
|
bind_request_failed_servers_.insert(stun_server_addr);
|
|
|
|
MaybeSetPortCompleteOrError();
|
2014-07-24 18:06:35 +02:00
|
|
|
}
|
2014-07-23 23:09:01 +02:00
|
|
|
|
2014-07-29 00:26:15 +02:00
|
|
|
void UDPPort::MaybeSetPortCompleteOrError() {
|
|
|
|
if (ready_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Do not set port ready if we are still waiting for bind responses.
|
|
|
|
const size_t servers_done_bind_request = bind_request_failed_servers_.size() +
|
|
|
|
bind_request_succeeded_servers_.size();
|
|
|
|
if (server_addresses_.size() != servers_done_bind_request) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-10 02:45:36 +02:00
|
|
|
// Setting ready status.
|
|
|
|
ready_ = true;
|
2014-07-29 00:26:15 +02:00
|
|
|
|
|
|
|
// The port is "completed" if there is no stun server provided, or the bind
|
|
|
|
// request succeeded for any stun server, or the socket is shared.
|
|
|
|
if (server_addresses_.empty() ||
|
|
|
|
bind_request_succeeded_servers_.size() > 0 ||
|
|
|
|
SharedSocket()) {
|
2013-07-10 02:45:36 +02:00
|
|
|
SignalPortComplete(this);
|
|
|
|
} else {
|
|
|
|
SignalPortError(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: merge this with SendTo above.
|
|
|
|
void UDPPort::OnSendPacket(const void* data, size_t size, StunRequest* req) {
|
|
|
|
StunBindingRequest* sreq = static_cast<StunBindingRequest*>(req);
|
2014-07-29 19:36:52 +02:00
|
|
|
rtc::PacketOptions options(DefaultDscpValue());
|
2014-02-14 01:56:12 +01:00
|
|
|
if (socket_->SendTo(data, size, sreq->server_addr(), options) < 0)
|
2013-07-10 02:45:36 +02:00
|
|
|
PLOG(LERROR, socket_->GetError()) << "sendto";
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace cricket
|