blob: 3a3d9ead8bab9330d8c7206f168b538d809dedca [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/port.h"
12
13#include <algorithm>
14#include <vector>
15
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000016#include "webrtc/base/base64.h"
nissec80e7412017-01-11 05:56:46 -080017#include "webrtc/base/checks.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000018#include "webrtc/base/crc32.h"
19#include "webrtc/base/helpers.h"
20#include "webrtc/base/logging.h"
21#include "webrtc/base/messagedigest.h"
honghaize3c6c822016-02-17 13:00:28 -080022#include "webrtc/base/network.h"
zsteinf42cc9d2017-03-27 16:17:19 -070023#include "webrtc/base/ptr_util.h"
kwiberg07038562017-06-12 11:40:47 -070024#include "webrtc/base/safe_minmax.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000025#include "webrtc/base/stringencode.h"
26#include "webrtc/base/stringutils.h"
zsteinf42cc9d2017-03-27 16:17:19 -070027#include "webrtc/p2p/base/common.h"
28#include "webrtc/p2p/base/portallocator.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000029
30namespace {
31
32// Determines whether we have seen at least the given maximum number of
33// pings fail to have a response.
34inline bool TooManyFailures(
Peter Thatcher1cf6f812015-05-15 10:40:45 -070035 const std::vector<cricket::Connection::SentPing>& pings_since_last_response,
Peter Boström0c4e06b2015-10-07 12:23:21 +020036 uint32_t maximum_failures,
honghaiz34b11eb2016-03-16 08:55:44 -070037 int rtt_estimate,
38 int64_t now) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000039 // If we haven't sent that many pings, then we can't have failed that many.
40 if (pings_since_last_response.size() < maximum_failures)
41 return false;
42
43 // Check if the window in which we would expect a response to the ping has
44 // already elapsed.
honghaiz34b11eb2016-03-16 08:55:44 -070045 int64_t expected_response_time =
Peter Thatcher1cf6f812015-05-15 10:40:45 -070046 pings_since_last_response[maximum_failures - 1].sent_time + rtt_estimate;
47 return now > expected_response_time;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000048}
49
50// Determines whether we have gone too long without seeing any response.
51inline bool TooLongWithoutResponse(
Peter Thatcher1cf6f812015-05-15 10:40:45 -070052 const std::vector<cricket::Connection::SentPing>& pings_since_last_response,
honghaiz34b11eb2016-03-16 08:55:44 -070053 int64_t maximum_time,
54 int64_t now) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000055 if (pings_since_last_response.size() == 0)
56 return false;
57
Peter Thatcher1cf6f812015-05-15 10:40:45 -070058 auto first = pings_since_last_response[0];
59 return now > (first.sent_time + maximum_time);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000060}
61
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000062// We will restrict RTT estimates (when used for determining state) to be
63// within a reasonable range.
honghaiz34b11eb2016-03-16 08:55:44 -070064const int MINIMUM_RTT = 100; // 0.1 seconds
skvlad51072462017-02-02 11:50:14 -080065const int MAXIMUM_RTT = 60000; // 60 seconds
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000066
67// When we don't have any RTT data, we have to pick something reasonable. We
68// use a large value just in case the connection is really slow.
skvlad51072462017-02-02 11:50:14 -080069const int DEFAULT_RTT = 3000; // 3 seconds
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000070
71// Computes our estimate of the RTT given the current estimate.
honghaiz34b11eb2016-03-16 08:55:44 -070072inline int ConservativeRTTEstimate(int rtt) {
kwiberg07038562017-06-12 11:40:47 -070073 return rtc::SafeClamp(2 * rtt, MINIMUM_RTT, MAXIMUM_RTT);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000074}
75
76// Weighting of the old rtt value to new data.
77const int RTT_RATIO = 3; // 3 : 1
78
pthatcher94a2f212017-02-08 14:42:22 -080079// The delay before we begin checking if this port is useless. We set
80// it to a little higher than a total STUN timeout.
81const int kPortTimeoutDelay = cricket::STUN_TOTAL_TIMEOUT + 5000;
zsteinabbacbf2017-03-20 10:53:12 -070082
83// For packet loss estimation.
84const int64_t kConsiderPacketLostAfter = 3000; // 3 seconds
85
86// For packet loss estimation.
87const int64_t kForgetPacketAfter = 30000; // 30 seconds
88
Honghai Zhang351d77b2016-05-20 15:08:29 -070089} // namespace
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000090
91namespace cricket {
92
zhihuang38989e52017-03-21 11:04:53 -070093// TODO(ronghuawu): Use "local", "srflx", "prflx" and "relay". But this requires
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000094// the signaling part be updated correspondingly as well.
95const char LOCAL_PORT_TYPE[] = "local";
96const char STUN_PORT_TYPE[] = "stun";
97const char PRFLX_PORT_TYPE[] = "prflx";
98const char RELAY_PORT_TYPE[] = "relay";
99
100const char UDP_PROTOCOL_NAME[] = "udp";
101const char TCP_PROTOCOL_NAME[] = "tcp";
102const char SSLTCP_PROTOCOL_NAME[] = "ssltcp";
hnsl277b2502016-12-13 05:17:23 -0800103const char TLS_PROTOCOL_NAME[] = "tls";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000104
hnsl277b2502016-12-13 05:17:23 -0800105static const char* const PROTO_NAMES[] = {UDP_PROTOCOL_NAME, TCP_PROTOCOL_NAME,
106 SSLTCP_PROTOCOL_NAME,
107 TLS_PROTOCOL_NAME};
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000108
109const char* ProtoToString(ProtocolType proto) {
110 return PROTO_NAMES[proto];
111}
112
113bool StringToProto(const char* value, ProtocolType* proto) {
114 for (size_t i = 0; i <= PROTO_LAST; ++i) {
115 if (_stricmp(PROTO_NAMES[i], value) == 0) {
116 *proto = static_cast<ProtocolType>(i);
117 return true;
118 }
119 }
120 return false;
121}
122
123// RFC 6544, TCP candidate encoding rules.
124const int DISCARD_PORT = 9;
125const char TCPTYPE_ACTIVE_STR[] = "active";
126const char TCPTYPE_PASSIVE_STR[] = "passive";
127const char TCPTYPE_SIMOPEN_STR[] = "so";
128
129// Foundation: An arbitrary string that is the same for two candidates
130// that have the same type, base IP address, protocol (UDP, TCP,
131// etc.), and STUN or TURN server. If any of these are different,
132// then the foundation will be different. Two candidate pairs with
133// the same foundation pairs are likely to have similar network
134// characteristics. Foundations are used in the frozen algorithm.
Honghai Zhang80f1db92016-01-27 11:54:45 -0800135static std::string ComputeFoundation(const std::string& type,
136 const std::string& protocol,
137 const std::string& relay_protocol,
138 const rtc::SocketAddress& base_address) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000139 std::ostringstream ost;
Honghai Zhang80f1db92016-01-27 11:54:45 -0800140 ost << type << base_address.ipaddr().ToString() << protocol << relay_protocol;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200141 return rtc::ToString<uint32_t>(rtc::ComputeCrc32(ost.str()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000142}
143
pkasting@chromium.org332331f2014-11-06 20:19:22 +0000144Port::Port(rtc::Thread* thread,
Honghai Zhangd00c0572016-06-28 09:44:47 -0700145 const std::string& type,
pkasting@chromium.org332331f2014-11-06 20:19:22 +0000146 rtc::PacketSocketFactory* factory,
147 rtc::Network* network,
148 const rtc::IPAddress& ip,
149 const std::string& username_fragment,
150 const std::string& password)
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000151 : thread_(thread),
152 factory_(factory),
Honghai Zhangd00c0572016-06-28 09:44:47 -0700153 type_(type),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000154 send_retransmit_count_attribute_(false),
155 network_(network),
156 ip_(ip),
157 min_port_(0),
158 max_port_(0),
159 component_(ICE_CANDIDATE_COMPONENT_DEFAULT),
160 generation_(0),
161 ice_username_fragment_(username_fragment),
162 password_(password),
163 timeout_delay_(kPortTimeoutDelay),
164 enable_port_packets_(false),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000165 ice_role_(ICEROLE_UNKNOWN),
166 tiebreaker_(0),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700167 shared_socket_(true) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000168 Construct();
169}
170
pkasting@chromium.org332331f2014-11-06 20:19:22 +0000171Port::Port(rtc::Thread* thread,
172 const std::string& type,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000173 rtc::PacketSocketFactory* factory,
pkasting@chromium.org332331f2014-11-06 20:19:22 +0000174 rtc::Network* network,
175 const rtc::IPAddress& ip,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200176 uint16_t min_port,
177 uint16_t max_port,
pkasting@chromium.org332331f2014-11-06 20:19:22 +0000178 const std::string& username_fragment,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000179 const std::string& password)
180 : thread_(thread),
181 factory_(factory),
182 type_(type),
183 send_retransmit_count_attribute_(false),
184 network_(network),
185 ip_(ip),
186 min_port_(min_port),
187 max_port_(max_port),
188 component_(ICE_CANDIDATE_COMPONENT_DEFAULT),
189 generation_(0),
190 ice_username_fragment_(username_fragment),
191 password_(password),
192 timeout_delay_(kPortTimeoutDelay),
193 enable_port_packets_(false),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000194 ice_role_(ICEROLE_UNKNOWN),
195 tiebreaker_(0),
Taylor Brandstetter417eebe2016-05-23 16:02:19 -0700196 shared_socket_(false) {
nisseede5da42017-01-12 05:15:36 -0800197 RTC_DCHECK(factory_ != NULL);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000198 Construct();
199}
200
201void Port::Construct() {
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700202 // TODO(pthatcher): Remove this old behavior once we're sure no one
203 // relies on it. If the username_fragment and password are empty,
204 // we should just create one.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000205 if (ice_username_fragment_.empty()) {
nisseede5da42017-01-12 05:15:36 -0800206 RTC_DCHECK(password_.empty());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000207 ice_username_fragment_ = rtc::CreateRandomString(ICE_UFRAG_LENGTH);
208 password_ = rtc::CreateRandomString(ICE_PWD_LENGTH);
209 }
Honghai Zhang351d77b2016-05-20 15:08:29 -0700210 network_->SignalTypeChanged.connect(this, &Port::OnNetworkTypeChanged);
211 network_cost_ = network_->GetCost();
honghaize1a0c942016-02-16 14:54:56 -0800212
Honghai Zhanga74363c2016-07-28 18:06:15 -0700213 thread_->PostDelayed(RTC_FROM_HERE, timeout_delay_, this,
214 MSG_DESTROY_IF_DEAD);
Honghai Zhang351d77b2016-05-20 15:08:29 -0700215 LOG_J(LS_INFO, this) << "Port created with network cost " << network_cost_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000216}
217
218Port::~Port() {
219 // Delete all of the remaining connections. We copy the list up front
220 // because each deletion will cause it to be modified.
221
222 std::vector<Connection*> list;
223
224 AddressMap::iterator iter = connections_.begin();
225 while (iter != connections_.end()) {
226 list.push_back(iter->second);
227 ++iter;
228 }
229
Peter Boström0c4e06b2015-10-07 12:23:21 +0200230 for (uint32_t i = 0; i < list.size(); i++)
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000231 delete list[i];
232}
233
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700234void Port::SetIceParameters(int component,
235 const std::string& username_fragment,
236 const std::string& password) {
237 component_ = component;
238 ice_username_fragment_ = username_fragment;
239 password_ = password;
240 for (Candidate& c : candidates_) {
241 c.set_component(component);
242 c.set_username(username_fragment);
243 c.set_password(password);
244 }
245}
246
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000247Connection* Port::GetConnection(const rtc::SocketAddress& remote_addr) {
248 AddressMap::const_iterator iter = connections_.find(remote_addr);
249 if (iter != connections_.end())
250 return iter->second;
251 else
252 return NULL;
253}
254
255void Port::AddAddress(const rtc::SocketAddress& address,
256 const rtc::SocketAddress& base_address,
257 const rtc::SocketAddress& related_address,
258 const std::string& protocol,
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700259 const std::string& relay_protocol,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000260 const std::string& tcptype,
261 const std::string& type,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200262 uint32_t type_preference,
263 uint32_t relay_preference,
Peter Boström2758c662017-02-13 20:33:27 -0500264 bool final) {
265 AddAddress(address, base_address, related_address, protocol, relay_protocol,
266 tcptype, type, type_preference, relay_preference, "", final);
267}
268
269void Port::AddAddress(const rtc::SocketAddress& address,
270 const rtc::SocketAddress& base_address,
271 const rtc::SocketAddress& related_address,
272 const std::string& protocol,
273 const std::string& relay_protocol,
274 const std::string& tcptype,
275 const std::string& type,
276 uint32_t type_preference,
277 uint32_t relay_preference,
zhihuang26d99c22017-02-13 12:47:27 -0800278 const std::string& url,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000279 bool final) {
280 if (protocol == TCP_PROTOCOL_NAME && type == LOCAL_PORT_TYPE) {
nisseede5da42017-01-12 05:15:36 -0800281 RTC_DCHECK(!tcptype.empty());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000282 }
283
honghaiza0c44ea2016-03-23 16:07:48 -0700284 std::string foundation =
285 ComputeFoundation(type, protocol, relay_protocol, base_address);
286 Candidate c(component_, protocol, address, 0U, username_fragment(), password_,
287 type, generation_, foundation, network_->id(), network_cost_);
288 c.set_priority(
289 c.GetPriority(type_preference, network_->preference(), relay_preference));
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700290 c.set_relay_protocol(relay_protocol);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000291 c.set_tcptype(tcptype);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000292 c.set_network_name(network_->name());
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000293 c.set_network_type(network_->type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000294 c.set_related_address(related_address);
zhihuang26d99c22017-02-13 12:47:27 -0800295 c.set_url(url);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000296 candidates_.push_back(c);
297 SignalCandidateReady(this, c);
298
299 if (final) {
300 SignalPortComplete(this);
301 }
302}
303
honghaiz36f50e82016-06-01 15:57:03 -0700304void Port::AddOrReplaceConnection(Connection* conn) {
305 auto ret = connections_.insert(
306 std::make_pair(conn->remote_candidate().address(), conn));
307 // If there is a different connection on the same remote address, replace
308 // it with the new one and destroy the old one.
309 if (ret.second == false && ret.first->second != conn) {
310 LOG_J(LS_WARNING, this)
311 << "A new connection was created on an existing remote address. "
312 << "New remote candidate: " << conn->remote_candidate().ToString();
313 ret.first->second->SignalDestroyed.disconnect(this);
314 ret.first->second->Destroy();
315 ret.first->second = conn;
316 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000317 conn->SignalDestroyed.connect(this, &Port::OnConnectionDestroyed);
318 SignalConnectionCreated(this, conn);
319}
320
321void Port::OnReadPacket(
322 const char* data, size_t size, const rtc::SocketAddress& addr,
323 ProtocolType proto) {
324 // If the user has enabled port packets, just hand this over.
325 if (enable_port_packets_) {
326 SignalReadPacket(this, data, size, addr);
327 return;
328 }
329
330 // If this is an authenticated STUN request, then signal unknown address and
331 // send back a proper binding response.
kwiberg3ec46792016-04-27 07:22:53 -0700332 std::unique_ptr<IceMessage> msg;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000333 std::string remote_username;
kwiberg6baec032016-03-15 11:09:39 -0700334 if (!GetStunMessage(data, size, addr, &msg, &remote_username)) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000335 LOG_J(LS_ERROR, this) << "Received non-STUN packet from unknown address ("
336 << addr.ToSensitiveString() << ")";
337 } else if (!msg) {
338 // STUN message handled already
339 } else if (msg->type() == STUN_BINDING_REQUEST) {
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700340 LOG(LS_INFO) << "Received STUN ping "
341 << " id=" << rtc::hex_encode(msg->transaction_id())
342 << " from unknown address " << addr.ToSensitiveString();
343
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000344 // Check for role conflicts.
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700345 if (!MaybeIceRoleConflict(addr, msg.get(), remote_username)) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000346 LOG(LS_INFO) << "Received conflicting role from the peer.";
347 return;
348 }
349
350 SignalUnknownAddress(this, addr, proto, msg.get(), remote_username, false);
351 } else {
352 // NOTE(tschmelcher): STUN_BINDING_RESPONSE is benign. It occurs if we
353 // pruned a connection for this port while it had STUN requests in flight,
354 // because we then get back responses for them, which this code correctly
355 // does not handle.
356 if (msg->type() != STUN_BINDING_RESPONSE) {
357 LOG_J(LS_ERROR, this) << "Received unexpected STUN message type ("
358 << msg->type() << ") from unknown address ("
359 << addr.ToSensitiveString() << ")";
360 }
361 }
362}
363
364void Port::OnReadyToSend() {
365 AddressMap::iterator iter = connections_.begin();
366 for (; iter != connections_.end(); ++iter) {
367 iter->second->OnReadyToSend();
368 }
369}
370
371size_t Port::AddPrflxCandidate(const Candidate& local) {
372 candidates_.push_back(local);
373 return (candidates_.size() - 1);
374}
375
kwiberg6baec032016-03-15 11:09:39 -0700376bool Port::GetStunMessage(const char* data,
377 size_t size,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000378 const rtc::SocketAddress& addr,
kwiberg3ec46792016-04-27 07:22:53 -0700379 std::unique_ptr<IceMessage>* out_msg,
kwiberg6baec032016-03-15 11:09:39 -0700380 std::string* out_username) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000381 // NOTE: This could clearly be optimized to avoid allocating any memory.
382 // However, at the data rates we'll be looking at on the client side,
383 // this probably isn't worth worrying about.
nisseede5da42017-01-12 05:15:36 -0800384 RTC_DCHECK(out_msg != NULL);
385 RTC_DCHECK(out_username != NULL);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000386 out_username->clear();
387
388 // Don't bother parsing the packet if we can tell it's not STUN.
389 // In ICE mode, all STUN packets will have a valid fingerprint.
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700390 if (!StunMessage::ValidateFingerprint(data, size)) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000391 return false;
392 }
393
394 // Parse the request message. If the packet is not a complete and correct
395 // STUN message, then ignore it.
kwiberg3ec46792016-04-27 07:22:53 -0700396 std::unique_ptr<IceMessage> stun_msg(new IceMessage());
jbauchf1f87202016-03-30 06:43:37 -0700397 rtc::ByteBufferReader buf(data, size);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000398 if (!stun_msg->Read(&buf) || (buf.Length() > 0)) {
399 return false;
400 }
401
402 if (stun_msg->type() == STUN_BINDING_REQUEST) {
403 // Check for the presence of USERNAME and MESSAGE-INTEGRITY (if ICE) first.
404 // If not present, fail with a 400 Bad Request.
405 if (!stun_msg->GetByteString(STUN_ATTR_USERNAME) ||
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700406 !stun_msg->GetByteString(STUN_ATTR_MESSAGE_INTEGRITY)) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000407 LOG_J(LS_ERROR, this) << "Received STUN request without username/M-I "
408 << "from " << addr.ToSensitiveString();
409 SendBindingErrorResponse(stun_msg.get(), addr, STUN_ERROR_BAD_REQUEST,
410 STUN_ERROR_REASON_BAD_REQUEST);
411 return true;
412 }
413
414 // If the username is bad or unknown, fail with a 401 Unauthorized.
415 std::string local_ufrag;
416 std::string remote_ufrag;
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700417 if (!ParseStunUsername(stun_msg.get(), &local_ufrag, &remote_ufrag) ||
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000418 local_ufrag != username_fragment()) {
419 LOG_J(LS_ERROR, this) << "Received STUN request with bad local username "
420 << local_ufrag << " from "
421 << addr.ToSensitiveString();
422 SendBindingErrorResponse(stun_msg.get(), addr, STUN_ERROR_UNAUTHORIZED,
423 STUN_ERROR_REASON_UNAUTHORIZED);
424 return true;
425 }
426
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000427 // If ICE, and the MESSAGE-INTEGRITY is bad, fail with a 401 Unauthorized
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700428 if (!stun_msg->ValidateMessageIntegrity(data, size, password_)) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000429 LOG_J(LS_ERROR, this) << "Received STUN request with bad M-I "
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000430 << "from " << addr.ToSensitiveString()
431 << ", password_=" << password_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000432 SendBindingErrorResponse(stun_msg.get(), addr, STUN_ERROR_UNAUTHORIZED,
433 STUN_ERROR_REASON_UNAUTHORIZED);
434 return true;
435 }
436 out_username->assign(remote_ufrag);
437 } else if ((stun_msg->type() == STUN_BINDING_RESPONSE) ||
438 (stun_msg->type() == STUN_BINDING_ERROR_RESPONSE)) {
439 if (stun_msg->type() == STUN_BINDING_ERROR_RESPONSE) {
440 if (const StunErrorCodeAttribute* error_code = stun_msg->GetErrorCode()) {
441 LOG_J(LS_ERROR, this) << "Received STUN binding error:"
442 << " class=" << error_code->eclass()
443 << " number=" << error_code->number()
444 << " reason='" << error_code->reason() << "'"
445 << " from " << addr.ToSensitiveString();
446 // Return message to allow error-specific processing
447 } else {
448 LOG_J(LS_ERROR, this) << "Received STUN binding error without a error "
449 << "code from " << addr.ToSensitiveString();
450 return true;
451 }
452 }
453 // NOTE: Username should not be used in verifying response messages.
454 out_username->clear();
455 } else if (stun_msg->type() == STUN_BINDING_INDICATION) {
456 LOG_J(LS_VERBOSE, this) << "Received STUN binding indication:"
457 << " from " << addr.ToSensitiveString();
458 out_username->clear();
459 // No stun attributes will be verified, if it's stun indication message.
460 // Returning from end of the this method.
461 } else {
462 LOG_J(LS_ERROR, this) << "Received STUN packet with invalid type ("
463 << stun_msg->type() << ") from "
464 << addr.ToSensitiveString();
465 return true;
466 }
467
468 // Return the STUN message found.
kwiberg6baec032016-03-15 11:09:39 -0700469 *out_msg = std::move(stun_msg);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000470 return true;
471}
472
473bool Port::IsCompatibleAddress(const rtc::SocketAddress& addr) {
474 int family = ip().family();
475 // We use single-stack sockets, so families must match.
476 if (addr.family() != family) {
477 return false;
478 }
479 // Link-local IPv6 ports can only connect to other link-local IPv6 ports.
Peter Thatcherb8b01432015-07-07 16:45:53 -0700480 if (family == AF_INET6 &&
481 (IPIsLinkLocal(ip()) != IPIsLinkLocal(addr.ipaddr()))) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000482 return false;
483 }
484 return true;
485}
486
487bool Port::ParseStunUsername(const StunMessage* stun_msg,
488 std::string* local_ufrag,
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700489 std::string* remote_ufrag) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000490 // The packet must include a username that either begins or ends with our
491 // fragment. It should begin with our fragment if it is a request and it
492 // should end with our fragment if it is a response.
493 local_ufrag->clear();
494 remote_ufrag->clear();
495 const StunByteStringAttribute* username_attr =
496 stun_msg->GetByteString(STUN_ATTR_USERNAME);
497 if (username_attr == NULL)
498 return false;
499
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700500 // RFRAG:LFRAG
501 const std::string username = username_attr->GetString();
502 size_t colon_pos = username.find(":");
503 if (colon_pos == std::string::npos) {
504 return false;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000505 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000506
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700507 *local_ufrag = username.substr(0, colon_pos);
508 *remote_ufrag = username.substr(colon_pos + 1, username.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000509 return true;
510}
511
512bool Port::MaybeIceRoleConflict(
513 const rtc::SocketAddress& addr, IceMessage* stun_msg,
514 const std::string& remote_ufrag) {
515 // Validate ICE_CONTROLLING or ICE_CONTROLLED attributes.
516 bool ret = true;
517 IceRole remote_ice_role = ICEROLE_UNKNOWN;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200518 uint64_t remote_tiebreaker = 0;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000519 const StunUInt64Attribute* stun_attr =
520 stun_msg->GetUInt64(STUN_ATTR_ICE_CONTROLLING);
521 if (stun_attr) {
522 remote_ice_role = ICEROLE_CONTROLLING;
523 remote_tiebreaker = stun_attr->value();
524 }
525
526 // If |remote_ufrag| is same as port local username fragment and
527 // tie breaker value received in the ping message matches port
528 // tiebreaker value this must be a loopback call.
529 // We will treat this as valid scenario.
530 if (remote_ice_role == ICEROLE_CONTROLLING &&
531 username_fragment() == remote_ufrag &&
532 remote_tiebreaker == IceTiebreaker()) {
533 return true;
534 }
535
536 stun_attr = stun_msg->GetUInt64(STUN_ATTR_ICE_CONTROLLED);
537 if (stun_attr) {
538 remote_ice_role = ICEROLE_CONTROLLED;
539 remote_tiebreaker = stun_attr->value();
540 }
541
542 switch (ice_role_) {
543 case ICEROLE_CONTROLLING:
544 if (ICEROLE_CONTROLLING == remote_ice_role) {
545 if (remote_tiebreaker >= tiebreaker_) {
546 SignalRoleConflict(this);
547 } else {
548 // Send Role Conflict (487) error response.
549 SendBindingErrorResponse(stun_msg, addr,
550 STUN_ERROR_ROLE_CONFLICT, STUN_ERROR_REASON_ROLE_CONFLICT);
551 ret = false;
552 }
553 }
554 break;
555 case ICEROLE_CONTROLLED:
556 if (ICEROLE_CONTROLLED == remote_ice_role) {
557 if (remote_tiebreaker < tiebreaker_) {
558 SignalRoleConflict(this);
559 } else {
560 // Send Role Conflict (487) error response.
561 SendBindingErrorResponse(stun_msg, addr,
562 STUN_ERROR_ROLE_CONFLICT, STUN_ERROR_REASON_ROLE_CONFLICT);
563 ret = false;
564 }
565 }
566 break;
567 default:
nissec80e7412017-01-11 05:56:46 -0800568 RTC_NOTREACHED();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000569 }
570 return ret;
571}
572
573void Port::CreateStunUsername(const std::string& remote_username,
574 std::string* stun_username_attr_str) const {
575 stun_username_attr_str->clear();
576 *stun_username_attr_str = remote_username;
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700577 stun_username_attr_str->append(":");
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000578 stun_username_attr_str->append(username_fragment());
579}
580
581void Port::SendBindingResponse(StunMessage* request,
582 const rtc::SocketAddress& addr) {
nisseede5da42017-01-12 05:15:36 -0800583 RTC_DCHECK(request->type() == STUN_BINDING_REQUEST);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000584
585 // Retrieve the username from the request.
586 const StunByteStringAttribute* username_attr =
587 request->GetByteString(STUN_ATTR_USERNAME);
nisseede5da42017-01-12 05:15:36 -0800588 RTC_DCHECK(username_attr != NULL);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000589 if (username_attr == NULL) {
590 // No valid username, skip the response.
591 return;
592 }
593
594 // Fill in the response message.
595 StunMessage response;
596 response.SetType(STUN_BINDING_RESPONSE);
597 response.SetTransactionID(request->transaction_id());
598 const StunUInt32Attribute* retransmit_attr =
599 request->GetUInt32(STUN_ATTR_RETRANSMIT_COUNT);
600 if (retransmit_attr) {
601 // Inherit the incoming retransmit value in the response so the other side
602 // can see our view of lost pings.
zsteinf42cc9d2017-03-27 16:17:19 -0700603 response.AddAttribute(rtc::MakeUnique<StunUInt32Attribute>(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000604 STUN_ATTR_RETRANSMIT_COUNT, retransmit_attr->value()));
605
606 if (retransmit_attr->value() > CONNECTION_WRITE_CONNECT_FAILURES) {
607 LOG_J(LS_INFO, this)
608 << "Received a remote ping with high retransmit count: "
609 << retransmit_attr->value();
610 }
611 }
612
zsteinf42cc9d2017-03-27 16:17:19 -0700613 response.AddAttribute(rtc::MakeUnique<StunXorAddressAttribute>(
614 STUN_ATTR_XOR_MAPPED_ADDRESS, addr));
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700615 response.AddMessageIntegrity(password_);
616 response.AddFingerprint();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000617
618 // Send the response message.
jbauchf1f87202016-03-30 06:43:37 -0700619 rtc::ByteBufferWriter buf;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000620 response.Write(&buf);
621 rtc::PacketOptions options(DefaultDscpValue());
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700622 auto err = SendTo(buf.Data(), buf.Length(), addr, options, false);
623 if (err < 0) {
624 LOG_J(LS_ERROR, this)
625 << "Failed to send STUN ping response"
626 << ", to=" << addr.ToSensitiveString()
627 << ", err=" << err
628 << ", id=" << rtc::hex_encode(response.transaction_id());
629 } else {
630 // Log at LS_INFO if we send a stun ping response on an unwritable
631 // connection.
honghaiz9b5ee9c2015-11-11 13:19:17 -0800632 Connection* conn = GetConnection(addr);
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700633 rtc::LoggingSeverity sev = (conn && !conn->writable()) ?
634 rtc::LS_INFO : rtc::LS_VERBOSE;
635 LOG_JV(sev, this)
636 << "Sent STUN ping response"
637 << ", to=" << addr.ToSensitiveString()
638 << ", id=" << rtc::hex_encode(response.transaction_id());
zhihuang5ecf16c2016-06-01 17:09:15 -0700639
640 conn->stats_.sent_ping_responses++;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000641 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000642}
643
644void Port::SendBindingErrorResponse(StunMessage* request,
645 const rtc::SocketAddress& addr,
646 int error_code, const std::string& reason) {
nisseede5da42017-01-12 05:15:36 -0800647 RTC_DCHECK(request->type() == STUN_BINDING_REQUEST);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000648
649 // Fill in the response message.
650 StunMessage response;
651 response.SetType(STUN_BINDING_ERROR_RESPONSE);
652 response.SetTransactionID(request->transaction_id());
653
654 // When doing GICE, we need to write out the error code incorrectly to
655 // maintain backwards compatiblility.
zsteinf42cc9d2017-03-27 16:17:19 -0700656 auto error_attr = StunAttribute::CreateErrorCode();
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700657 error_attr->SetCode(error_code);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000658 error_attr->SetReason(reason);
zsteinf42cc9d2017-03-27 16:17:19 -0700659 response.AddAttribute(std::move(error_attr));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000660
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700661 // Per Section 10.1.2, certain error cases don't get a MESSAGE-INTEGRITY,
662 // because we don't have enough information to determine the shared secret.
663 if (error_code != STUN_ERROR_BAD_REQUEST &&
664 error_code != STUN_ERROR_UNAUTHORIZED)
665 response.AddMessageIntegrity(password_);
666 response.AddFingerprint();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000667
668 // Send the response message.
jbauchf1f87202016-03-30 06:43:37 -0700669 rtc::ByteBufferWriter buf;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000670 response.Write(&buf);
671 rtc::PacketOptions options(DefaultDscpValue());
672 SendTo(buf.Data(), buf.Length(), addr, options, false);
673 LOG_J(LS_INFO, this) << "Sending STUN binding error: reason=" << reason
674 << " to " << addr.ToSensitiveString();
675}
676
Honghai Zhanga74363c2016-07-28 18:06:15 -0700677void Port::KeepAliveUntilPruned() {
678 // If it is pruned, we won't bring it up again.
679 if (state_ == State::INIT) {
680 state_ = State::KEEP_ALIVE_UNTIL_PRUNED;
681 }
682}
683
684void Port::Prune() {
685 state_ = State::PRUNED;
686 thread_->Post(RTC_FROM_HERE, this, MSG_DESTROY_IF_DEAD);
687}
688
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000689void Port::OnMessage(rtc::Message *pmsg) {
nisseede5da42017-01-12 05:15:36 -0800690 RTC_DCHECK(pmsg->message_id == MSG_DESTROY_IF_DEAD);
Honghai Zhanga74363c2016-07-28 18:06:15 -0700691 bool dead =
692 (state_ == State::INIT || state_ == State::PRUNED) &&
693 connections_.empty() &&
694 rtc::TimeMillis() - last_time_all_connections_removed_ >= timeout_delay_;
695 if (dead) {
honghaizd0b31432015-09-30 12:42:17 -0700696 Destroy();
697 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000698}
699
Honghai Zhang351d77b2016-05-20 15:08:29 -0700700void Port::OnNetworkTypeChanged(const rtc::Network* network) {
nisseede5da42017-01-12 05:15:36 -0800701 RTC_DCHECK(network == network_);
Honghai Zhang351d77b2016-05-20 15:08:29 -0700702
703 UpdateNetworkCost();
704}
705
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000706std::string Port::ToString() const {
707 std::stringstream ss;
honghaize3c6c822016-02-17 13:00:28 -0800708 ss << "Port[" << std::hex << this << std::dec << ":" << content_name_ << ":"
709 << component_ << ":" << generation_ << ":" << type_ << ":"
710 << network_->ToString() << "]";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000711 return ss.str();
712}
713
Honghai Zhang351d77b2016-05-20 15:08:29 -0700714// TODO(honghaiz): Make the network cost configurable from user setting.
715void Port::UpdateNetworkCost() {
716 uint16_t new_cost = network_->GetCost();
717 if (network_cost_ == new_cost) {
718 return;
719 }
720 LOG(LS_INFO) << "Network cost changed from " << network_cost_
721 << " to " << new_cost
722 << ". Number of candidates created: " << candidates_.size()
723 << ". Number of connections created: " << connections_.size();
724 network_cost_ = new_cost;
725 for (cricket::Candidate& candidate : candidates_) {
726 candidate.set_network_cost(network_cost_);
727 }
728 // Network cost change will affect the connection selection criteria.
729 // Signal the connection state change on each connection to force a
730 // re-sort in P2PTransportChannel.
731 for (auto kv : connections_) {
732 Connection* conn = kv.second;
733 conn->SignalStateChange(conn);
734 }
735}
736
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000737void Port::EnablePortPackets() {
738 enable_port_packets_ = true;
739}
740
741void Port::OnConnectionDestroyed(Connection* conn) {
742 AddressMap::iterator iter =
743 connections_.find(conn->remote_candidate().address());
nisseede5da42017-01-12 05:15:36 -0800744 RTC_DCHECK(iter != connections_.end());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000745 connections_.erase(iter);
honghaiz36f50e82016-06-01 15:57:03 -0700746 HandleConnectionDestroyed(conn);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000747
Honghai Zhanga74363c2016-07-28 18:06:15 -0700748 // Ports time out after all connections fail if it is not marked as
749 // "keep alive until pruned."
honghaizd0b31432015-09-30 12:42:17 -0700750 // Note: If a new connection is added after this message is posted, but it
751 // fails and is removed before kPortTimeoutDelay, then this message will
Honghai Zhangb5db1ec2016-07-28 13:23:05 -0700752 // not cause the Port to be destroyed.
Honghai Zhanga74363c2016-07-28 18:06:15 -0700753 if (connections_.empty()) {
Honghai Zhangb5db1ec2016-07-28 13:23:05 -0700754 last_time_all_connections_removed_ = rtc::TimeMillis();
Honghai Zhanga74363c2016-07-28 18:06:15 -0700755 thread_->PostDelayed(RTC_FROM_HERE, timeout_delay_, this,
756 MSG_DESTROY_IF_DEAD);
honghaizd0b31432015-09-30 12:42:17 -0700757 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000758}
759
760void Port::Destroy() {
nisseede5da42017-01-12 05:15:36 -0800761 RTC_DCHECK(connections_.empty());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000762 LOG_J(LS_INFO, this) << "Port deleted";
763 SignalDestroyed(this);
764 delete this;
765}
766
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000767const std::string Port::username_fragment() const {
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700768 return ice_username_fragment_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000769}
770
771// A ConnectionRequest is a simple STUN ping used to determine writability.
772class ConnectionRequest : public StunRequest {
773 public:
774 explicit ConnectionRequest(Connection* connection)
775 : StunRequest(new IceMessage()),
776 connection_(connection) {
777 }
778
779 virtual ~ConnectionRequest() {
780 }
781
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700782 void Prepare(StunMessage* request) override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000783 request->SetType(STUN_BINDING_REQUEST);
784 std::string username;
785 connection_->port()->CreateStunUsername(
786 connection_->remote_candidate().username(), &username);
787 request->AddAttribute(
zsteinf42cc9d2017-03-27 16:17:19 -0700788 rtc::MakeUnique<StunByteStringAttribute>(STUN_ATTR_USERNAME, username));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000789
790 // connection_ already holds this ping, so subtract one from count.
791 if (connection_->port()->send_retransmit_count_attribute()) {
zsteinf42cc9d2017-03-27 16:17:19 -0700792 request->AddAttribute(rtc::MakeUnique<StunUInt32Attribute>(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000793 STUN_ATTR_RETRANSMIT_COUNT,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200794 static_cast<uint32_t>(connection_->pings_since_last_response_.size() -
795 1)));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000796 }
honghaiza0c44ea2016-03-23 16:07:48 -0700797 uint32_t network_info = connection_->port()->Network()->id();
798 network_info = (network_info << 16) | connection_->port()->network_cost();
zsteinf42cc9d2017-03-27 16:17:19 -0700799 request->AddAttribute(rtc::MakeUnique<StunUInt32Attribute>(
800 STUN_ATTR_NETWORK_INFO, network_info));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000801
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700802 // Adding ICE_CONTROLLED or ICE_CONTROLLING attribute based on the role.
803 if (connection_->port()->GetIceRole() == ICEROLE_CONTROLLING) {
zsteinf42cc9d2017-03-27 16:17:19 -0700804 request->AddAttribute(rtc::MakeUnique<StunUInt64Attribute>(
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700805 STUN_ATTR_ICE_CONTROLLING, connection_->port()->IceTiebreaker()));
Honghai Zhang8cd8f812016-08-03 19:50:41 -0700806 // We should have either USE_CANDIDATE attribute or ICE_NOMINATION
807 // attribute but not both. That was enforced in p2ptransportchannel.
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700808 if (connection_->use_candidate_attr()) {
zsteinf42cc9d2017-03-27 16:17:19 -0700809 request->AddAttribute(
810 rtc::MakeUnique<StunByteStringAttribute>(STUN_ATTR_USE_CANDIDATE));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000811 }
Honghai Zhang8cd8f812016-08-03 19:50:41 -0700812 if (connection_->nomination() &&
813 connection_->nomination() != connection_->acked_nomination()) {
zsteinf42cc9d2017-03-27 16:17:19 -0700814 request->AddAttribute(rtc::MakeUnique<StunUInt32Attribute>(
Honghai Zhang8cd8f812016-08-03 19:50:41 -0700815 STUN_ATTR_NOMINATION, connection_->nomination()));
816 }
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700817 } else if (connection_->port()->GetIceRole() == ICEROLE_CONTROLLED) {
zsteinf42cc9d2017-03-27 16:17:19 -0700818 request->AddAttribute(rtc::MakeUnique<StunUInt64Attribute>(
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700819 STUN_ATTR_ICE_CONTROLLED, connection_->port()->IceTiebreaker()));
820 } else {
nissec80e7412017-01-11 05:56:46 -0800821 RTC_NOTREACHED();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000822 }
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700823
824 // Adding PRIORITY Attribute.
825 // Changing the type preference to Peer Reflexive and local preference
826 // and component id information is unchanged from the original priority.
827 // priority = (2^24)*(type preference) +
828 // (2^8)*(local preference) +
829 // (2^0)*(256 - component ID)
Taylor Brandstetter62351c92016-08-11 16:05:07 -0700830 uint32_t type_preference =
831 (connection_->local_candidate().protocol() == TCP_PROTOCOL_NAME)
832 ? ICE_TYPE_PREFERENCE_PRFLX_TCP
833 : ICE_TYPE_PREFERENCE_PRFLX;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200834 uint32_t prflx_priority =
Taylor Brandstetter62351c92016-08-11 16:05:07 -0700835 type_preference << 24 |
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700836 (connection_->local_candidate().priority() & 0x00FFFFFF);
zsteinf42cc9d2017-03-27 16:17:19 -0700837 request->AddAttribute(rtc::MakeUnique<StunUInt32Attribute>(
838 STUN_ATTR_PRIORITY, prflx_priority));
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700839
840 // Adding Message Integrity attribute.
841 request->AddMessageIntegrity(connection_->remote_candidate().password());
842 // Adding Fingerprint.
843 request->AddFingerprint();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000844 }
845
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700846 void OnResponse(StunMessage* response) override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000847 connection_->OnConnectionRequestResponse(this, response);
848 }
849
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700850 void OnErrorResponse(StunMessage* response) override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000851 connection_->OnConnectionRequestErrorResponse(this, response);
852 }
853
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700854 void OnTimeout() override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000855 connection_->OnConnectionRequestTimeout(this);
856 }
857
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700858 void OnSent() override {
859 connection_->OnConnectionRequestSent(this);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000860 // Each request is sent only once. After a single delay , the request will
861 // time out.
862 timeout_ = true;
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700863 }
864
865 int resend_delay() override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000866 return CONNECTION_RESPONSE_TIMEOUT;
867 }
868
869 private:
870 Connection* connection_;
871};
872
873//
874// Connection
875//
876
guoweis@webrtc.org930e0042014-11-17 19:42:14 +0000877Connection::Connection(Port* port,
878 size_t index,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000879 const Candidate& remote_candidate)
guoweis@webrtc.org930e0042014-11-17 19:42:14 +0000880 : port_(port),
881 local_candidate_index_(index),
882 remote_candidate_(remote_candidate),
Honghai Zhang8cd8f812016-08-03 19:50:41 -0700883 recv_rate_tracker_(100, 10u),
884 send_rate_tracker_(100, 10u),
guoweis@webrtc.org930e0042014-11-17 19:42:14 +0000885 write_state_(STATE_WRITE_INIT),
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700886 receiving_(false),
guoweis@webrtc.org930e0042014-11-17 19:42:14 +0000887 connected_(true),
888 pruned_(false),
889 use_candidate_attr_(false),
890 remote_ice_mode_(ICEMODE_FULL),
891 requests_(port->thread()),
892 rtt_(DEFAULT_RTT),
893 last_ping_sent_(0),
894 last_ping_received_(0),
895 last_data_received_(0),
896 last_ping_response_received_(0),
zsteinabbacbf2017-03-20 10:53:12 -0700897 packet_loss_estimator_(kConsiderPacketLostAfter, kForgetPacketAfter),
guoweis@webrtc.org930e0042014-11-17 19:42:14 +0000898 reported_(false),
hbos06495bc2017-01-02 08:08:18 -0800899 state_(IceCandidatePairState::WAITING),
Honghai Zhang2b342bf2015-09-30 09:51:58 -0700900 receiving_timeout_(WEAK_CONNECTION_RECEIVE_TIMEOUT),
nisse1bffc1d2016-05-02 08:18:55 -0700901 time_created_ms_(rtc::TimeMillis()) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000902 // All of our connections start in WAITING state.
903 // TODO(mallinath) - Start connections from STATE_FROZEN.
904 // Wire up to send stun packets
905 requests_.SignalSendPacket.connect(this, &Connection::OnSendStunPacket);
906 LOG_J(LS_INFO, this) << "Connection created";
907}
908
909Connection::~Connection() {
910}
911
912const Candidate& Connection::local_candidate() const {
nisseede5da42017-01-12 05:15:36 -0800913 RTC_DCHECK(local_candidate_index_ < port_->Candidates().size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000914 return port_->Candidates()[local_candidate_index_];
915}
916
Honghai Zhangcc411c02016-03-29 17:27:21 -0700917const Candidate& Connection::remote_candidate() const {
918 return remote_candidate_;
919}
920
Peter Boström0c4e06b2015-10-07 12:23:21 +0200921uint64_t Connection::priority() const {
922 uint64_t priority = 0;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000923 // RFC 5245 - 5.7.2. Computing Pair Priority and Ordering Pairs
924 // Let G be the priority for the candidate provided by the controlling
925 // agent. Let D be the priority for the candidate provided by the
926 // controlled agent.
927 // pair priority = 2^32*MIN(G,D) + 2*MAX(G,D) + (G>D?1:0)
928 IceRole role = port_->GetIceRole();
929 if (role != ICEROLE_UNKNOWN) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200930 uint32_t g = 0;
931 uint32_t d = 0;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000932 if (role == ICEROLE_CONTROLLING) {
933 g = local_candidate().priority();
934 d = remote_candidate_.priority();
935 } else {
936 g = remote_candidate_.priority();
937 d = local_candidate().priority();
938 }
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000939 priority = std::min(g, d);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000940 priority = priority << 32;
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000941 priority += 2 * std::max(g, d) + (g > d ? 1 : 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000942 }
943 return priority;
944}
945
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000946void Connection::set_write_state(WriteState value) {
947 WriteState old_value = write_state_;
948 write_state_ = value;
949 if (value != old_value) {
guoweis@webrtc.org8c9ff202014-12-04 07:56:02 +0000950 LOG_J(LS_VERBOSE, this) << "set_write_state from: " << old_value << " to "
951 << value;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000952 SignalStateChange(this);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000953 }
954}
955
honghaiz9ad0db52016-07-14 19:30:28 -0700956void Connection::UpdateReceiving(int64_t now) {
honghaize58d73d2016-10-24 16:38:26 -0700957 bool receiving =
958 last_received() > 0 && now <= last_received() + receiving_timeout_;
honghaiz9ad0db52016-07-14 19:30:28 -0700959 if (receiving_ == receiving) {
960 return;
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700961 }
honghaiz9ad0db52016-07-14 19:30:28 -0700962 LOG_J(LS_VERBOSE, this) << "set_receiving to " << receiving;
963 receiving_ = receiving;
964 receiving_unchanged_since_ = now;
965 SignalStateChange(this);
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700966}
967
hbos06495bc2017-01-02 08:08:18 -0800968void Connection::set_state(IceCandidatePairState state) {
969 IceCandidatePairState old_state = state_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000970 state_ = state;
971 if (state != old_state) {
972 LOG_J(LS_VERBOSE, this) << "set_state";
973 }
974}
975
976void Connection::set_connected(bool value) {
977 bool old_value = connected_;
978 connected_ = value;
979 if (value != old_value) {
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700980 LOG_J(LS_VERBOSE, this) << "set_connected from: " << old_value << " to "
981 << value;
Taylor Brandstetterb825aee2016-06-29 13:07:16 -0700982 SignalStateChange(this);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000983 }
984}
985
986void Connection::set_use_candidate_attr(bool enable) {
987 use_candidate_attr_ = enable;
988}
989
990void Connection::OnSendStunPacket(const void* data, size_t size,
991 StunRequest* req) {
992 rtc::PacketOptions options(port_->DefaultDscpValue());
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700993 auto err = port_->SendTo(
994 data, size, remote_candidate_.address(), options, false);
995 if (err < 0) {
996 LOG_J(LS_WARNING, this) << "Failed to send STUN ping "
997 << " err=" << err
998 << " id=" << rtc::hex_encode(req->id());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000999 }
1000}
1001
1002void Connection::OnReadPacket(
1003 const char* data, size_t size, const rtc::PacketTime& packet_time) {
kwiberg3ec46792016-04-27 07:22:53 -07001004 std::unique_ptr<IceMessage> msg;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001005 std::string remote_ufrag;
1006 const rtc::SocketAddress& addr(remote_candidate_.address());
kwiberg6baec032016-03-15 11:09:39 -07001007 if (!port_->GetStunMessage(data, size, addr, &msg, &remote_ufrag)) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001008 // The packet did not parse as a valid STUN message
Peter Thatcher04ac81f2015-09-21 11:48:28 -07001009 // This is a data packet, pass it along.
nisse1bffc1d2016-05-02 08:18:55 -07001010 last_data_received_ = rtc::TimeMillis();
honghaiz9ad0db52016-07-14 19:30:28 -07001011 UpdateReceiving(last_data_received_);
Peter Thatcher04ac81f2015-09-21 11:48:28 -07001012 recv_rate_tracker_.AddSamples(size);
1013 SignalReadPacket(this, data, size, packet_time);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001014
Peter Thatcher04ac81f2015-09-21 11:48:28 -07001015 // If timed out sending writability checks, start up again
1016 if (!pruned_ && (write_state_ == STATE_WRITE_TIMEOUT)) {
1017 LOG(LS_WARNING) << "Received a data packet on a timed-out Connection. "
1018 << "Resetting state to STATE_WRITE_INIT.";
1019 set_write_state(STATE_WRITE_INIT);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001020 }
1021 } else if (!msg) {
1022 // The packet was STUN, but failed a check and was handled internally.
1023 } else {
1024 // The packet is STUN and passed the Port checks.
1025 // Perform our own checks to ensure this packet is valid.
honghaizd0b31432015-09-30 12:42:17 -07001026 // If this is a STUN request, then update the receiving bit and respond.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001027 // If this is a STUN response, then update the writable bit.
Peter Thatcher1cf6f812015-05-15 10:40:45 -07001028 // Log at LS_INFO if we receive a ping on an unwritable connection.
1029 rtc::LoggingSeverity sev = (!writable() ? rtc::LS_INFO : rtc::LS_VERBOSE);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001030 switch (msg->type()) {
1031 case STUN_BINDING_REQUEST:
Peter Thatcher1cf6f812015-05-15 10:40:45 -07001032 LOG_JV(sev, this) << "Received STUN ping"
1033 << ", id=" << rtc::hex_encode(msg->transaction_id());
1034
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001035 if (remote_ufrag == remote_candidate_.username()) {
honghaiz9b5ee9c2015-11-11 13:19:17 -08001036 HandleBindingRequest(msg.get());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001037 } else {
1038 // The packet had the right local username, but the remote username
1039 // was not the right one for the remote address.
1040 LOG_J(LS_ERROR, this)
1041 << "Received STUN request with bad remote username "
1042 << remote_ufrag;
1043 port_->SendBindingErrorResponse(msg.get(), addr,
1044 STUN_ERROR_UNAUTHORIZED,
1045 STUN_ERROR_REASON_UNAUTHORIZED);
1046
1047 }
1048 break;
1049
1050 // Response from remote peer. Does it match request sent?
1051 // This doesn't just check, it makes callbacks if transaction
1052 // id's match.
1053 case STUN_BINDING_RESPONSE:
1054 case STUN_BINDING_ERROR_RESPONSE:
Peter Thatcher7cbd1882015-09-17 18:54:52 -07001055 if (msg->ValidateMessageIntegrity(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001056 data, size, remote_candidate().password())) {
1057 requests_.CheckResponse(msg.get());
1058 }
1059 // Otherwise silently discard the response message.
1060 break;
1061
honghaizd0b31432015-09-30 12:42:17 -07001062 // Remote end point sent an STUN indication instead of regular binding
1063 // request. In this case |last_ping_received_| will be updated but no
1064 // response will be sent.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001065 case STUN_BINDING_INDICATION:
Peter Thatcher04ac81f2015-09-21 11:48:28 -07001066 ReceivedPing();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001067 break;
1068
1069 default:
nissec80e7412017-01-11 05:56:46 -08001070 RTC_NOTREACHED();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001071 break;
1072 }
1073 }
1074}
1075
honghaiz9b5ee9c2015-11-11 13:19:17 -08001076void Connection::HandleBindingRequest(IceMessage* msg) {
1077 // This connection should now be receiving.
1078 ReceivedPing();
1079
1080 const rtc::SocketAddress& remote_addr = remote_candidate_.address();
1081 const std::string& remote_ufrag = remote_candidate_.username();
1082 // Check for role conflicts.
1083 if (!port_->MaybeIceRoleConflict(remote_addr, msg, remote_ufrag)) {
1084 // Received conflicting role from the peer.
1085 LOG(LS_INFO) << "Received conflicting role from the peer.";
1086 return;
1087 }
1088
zhihuang5ecf16c2016-06-01 17:09:15 -07001089 stats_.recv_ping_requests++;
1090
honghaiz9b5ee9c2015-11-11 13:19:17 -08001091 // This is a validated stun request from remote peer.
1092 port_->SendBindingResponse(msg, remote_addr);
1093
1094 // If it timed out on writing check, start up again
1095 if (!pruned_ && write_state_ == STATE_WRITE_TIMEOUT) {
1096 set_write_state(STATE_WRITE_INIT);
1097 }
1098
1099 if (port_->GetIceRole() == ICEROLE_CONTROLLED) {
Honghai Zhang8cd8f812016-08-03 19:50:41 -07001100 const StunUInt32Attribute* nomination_attr =
1101 msg->GetUInt32(STUN_ATTR_NOMINATION);
1102 uint32_t nomination = 0;
1103 if (nomination_attr) {
1104 nomination = nomination_attr->value();
1105 if (nomination == 0) {
1106 LOG(LS_ERROR) << "Invalid nomination: " << nomination;
1107 }
1108 } else {
1109 const StunByteStringAttribute* use_candidate_attr =
1110 msg->GetByteString(STUN_ATTR_USE_CANDIDATE);
1111 if (use_candidate_attr) {
1112 nomination = 1;
1113 }
1114 }
1115 // We don't un-nominate a connection, so we only keep a larger nomination.
1116 if (nomination > remote_nomination_) {
1117 set_remote_nomination(nomination);
honghaiz9b5ee9c2015-11-11 13:19:17 -08001118 SignalNominated(this);
1119 }
1120 }
Honghai Zhang351d77b2016-05-20 15:08:29 -07001121 // Set the remote cost if the network_info attribute is available.
1122 // Note: If packets are re-ordered, we may get incorrect network cost
1123 // temporarily, but it should get the correct value shortly after that.
1124 const StunUInt32Attribute* network_attr =
1125 msg->GetUInt32(STUN_ATTR_NETWORK_INFO);
1126 if (network_attr) {
1127 uint32_t network_info = network_attr->value();
1128 uint16_t network_cost = static_cast<uint16_t>(network_info);
1129 if (network_cost != remote_candidate_.network_cost()) {
1130 remote_candidate_.set_network_cost(network_cost);
1131 // Network cost change will affect the connection ranking, so signal
1132 // state change to force a re-sort in P2PTransportChannel.
1133 SignalStateChange(this);
1134 }
1135 }
honghaiz9b5ee9c2015-11-11 13:19:17 -08001136}
1137
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001138void Connection::OnReadyToSend() {
deadbeefdd7fb432016-09-30 15:16:48 -07001139 SignalReadyToSend(this);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001140}
1141
1142void Connection::Prune() {
Honghai Zhang2b342bf2015-09-30 09:51:58 -07001143 if (!pruned_ || active()) {
Honghai Zhang1590c392016-05-24 13:15:02 -07001144 LOG_J(LS_INFO, this) << "Connection pruned";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001145 pruned_ = true;
1146 requests_.Clear();
1147 set_write_state(STATE_WRITE_TIMEOUT);
1148 }
1149}
1150
1151void Connection::Destroy() {
nisse7eaa4ea2017-05-08 05:25:41 -07001152 // TODO(deadbeef, nisse): This may leak if an application closes a
1153 // PeerConnection and then quickly destroys the PeerConnectionFactory (along
1154 // with the networking thread on which this message is posted). Also affects
1155 // tests, with a workaround in
1156 // AutoSocketServerThread::~AutoSocketServerThread.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001157 LOG_J(LS_VERBOSE, this) << "Connection destroyed";
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001158 port_->thread()->Post(RTC_FROM_HERE, this, MSG_DELETE);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001159}
1160
deadbeef376e1232015-11-25 09:00:08 -08001161void Connection::FailAndDestroy() {
hbos06495bc2017-01-02 08:08:18 -08001162 set_state(IceCandidatePairState::FAILED);
deadbeef376e1232015-11-25 09:00:08 -08001163 Destroy();
1164}
1165
honghaiz079a7a12016-06-22 16:26:29 -07001166void Connection::FailAndPrune() {
hbos06495bc2017-01-02 08:08:18 -08001167 set_state(IceCandidatePairState::FAILED);
honghaiz079a7a12016-06-22 16:26:29 -07001168 Prune();
1169}
1170
Peter Thatcher1cf6f812015-05-15 10:40:45 -07001171void Connection::PrintPingsSinceLastResponse(std::string* s, size_t max) {
1172 std::ostringstream oss;
1173 oss << std::boolalpha;
1174 if (pings_since_last_response_.size() > max) {
1175 for (size_t i = 0; i < max; i++) {
1176 const SentPing& ping = pings_since_last_response_[i];
1177 oss << rtc::hex_encode(ping.id) << " ";
1178 }
1179 oss << "... " << (pings_since_last_response_.size() - max) << " more";
1180 } else {
1181 for (const SentPing& ping : pings_since_last_response_) {
1182 oss << rtc::hex_encode(ping.id) << " ";
1183 }
1184 }
1185 *s = oss.str();
1186}
1187
honghaiz34b11eb2016-03-16 08:55:44 -07001188void Connection::UpdateState(int64_t now) {
1189 int rtt = ConservativeRTTEstimate(rtt_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001190
Peter Thatcherb2d26232015-05-15 11:25:14 -07001191 if (LOG_CHECK_LEVEL(LS_VERBOSE)) {
Peter Thatcher1cf6f812015-05-15 10:40:45 -07001192 std::string pings;
1193 PrintPingsSinceLastResponse(&pings, 5);
1194 LOG_J(LS_VERBOSE, this) << "UpdateState()"
1195 << ", ms since last received response="
1196 << now - last_ping_response_received_
1197 << ", ms since last received data="
1198 << now - last_data_received_
1199 << ", rtt=" << rtt
1200 << ", pings_since_last_response=" << pings;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001201 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001202
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001203 // Check the writable state. (The order of these checks is important.)
1204 //
1205 // Before becoming unwritable, we allow for a fixed number of pings to fail
1206 // (i.e., receive no response). We also have to give the response time to
1207 // get back, so we include a conservative estimate of this.
1208 //
1209 // Before timing out writability, we give a fixed amount of time. This is to
1210 // allow for changes in network conditions.
1211
1212 if ((write_state_ == STATE_WRITABLE) &&
1213 TooManyFailures(pings_since_last_response_,
1214 CONNECTION_WRITE_CONNECT_FAILURES,
1215 rtt,
1216 now) &&
1217 TooLongWithoutResponse(pings_since_last_response_,
1218 CONNECTION_WRITE_CONNECT_TIMEOUT,
1219 now)) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001220 uint32_t max_pings = CONNECTION_WRITE_CONNECT_FAILURES;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001221 LOG_J(LS_INFO, this) << "Unwritable after " << max_pings
1222 << " ping failures and "
Peter Thatcher1cf6f812015-05-15 10:40:45 -07001223 << now - pings_since_last_response_[0].sent_time
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001224 << " ms without a response,"
1225 << " ms since last received ping="
1226 << now - last_ping_received_
1227 << " ms since last received data="
1228 << now - last_data_received_
1229 << " rtt=" << rtt;
1230 set_write_state(STATE_WRITE_UNRELIABLE);
1231 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001232 if ((write_state_ == STATE_WRITE_UNRELIABLE ||
1233 write_state_ == STATE_WRITE_INIT) &&
1234 TooLongWithoutResponse(pings_since_last_response_,
1235 CONNECTION_WRITE_TIMEOUT,
1236 now)) {
1237 LOG_J(LS_INFO, this) << "Timed out after "
Peter Thatcher1cf6f812015-05-15 10:40:45 -07001238 << now - pings_since_last_response_[0].sent_time
1239 << " ms without a response"
1240 << ", rtt=" << rtt;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001241 set_write_state(STATE_WRITE_TIMEOUT);
1242 }
Peter Thatcher04ac81f2015-09-21 11:48:28 -07001243
honghaiz9ad0db52016-07-14 19:30:28 -07001244 // Update the receiving state.
1245 UpdateReceiving(now);
Honghai Zhang2b342bf2015-09-30 09:51:58 -07001246 if (dead(now)) {
1247 Destroy();
1248 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001249}
1250
honghaiz34b11eb2016-03-16 08:55:44 -07001251void Connection::Ping(int64_t now) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001252 last_ping_sent_ = now;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001253 ConnectionRequest *req = new ConnectionRequest(this);
deadbeef86c40a12017-06-28 09:37:23 -07001254 // If not using renomination, we use "1" to mean "nominated" and "0" to mean
1255 // "not nominated". If using renomination, values greater than 1 are used for
1256 // re-nominated pairs.
1257 int nomination = use_candidate_attr_ ? 1 : 0;
1258 if (nomination_ > 0) {
1259 nomination = nomination_;
1260 }
1261 pings_since_last_response_.push_back(SentPing(req->id(), now, nomination));
zsteinabbacbf2017-03-20 10:53:12 -07001262 packet_loss_estimator_.ExpectResponse(req->id(), now);
Peter Thatcher1cf6f812015-05-15 10:40:45 -07001263 LOG_J(LS_VERBOSE, this) << "Sending STUN ping "
Honghai Zhang8cd8f812016-08-03 19:50:41 -07001264 << ", id=" << rtc::hex_encode(req->id())
1265 << ", nomination=" << nomination_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001266 requests_.Send(req);
hbos06495bc2017-01-02 08:08:18 -08001267 state_ = IceCandidatePairState::IN_PROGRESS;
honghaiz524ecc22016-05-25 12:48:31 -07001268 num_pings_sent_++;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001269}
1270
1271void Connection::ReceivedPing() {
nisse1bffc1d2016-05-02 08:18:55 -07001272 last_ping_received_ = rtc::TimeMillis();
honghaiz9ad0db52016-07-14 19:30:28 -07001273 UpdateReceiving(last_ping_received_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001274}
1275
Honghai Zhang8cd8f812016-08-03 19:50:41 -07001276void Connection::ReceivedPingResponse(int rtt, const std::string& request_id) {
hbosbf8d3e52017-02-28 06:34:47 -08001277 RTC_DCHECK_GE(rtt, 0);
Peter Thatcher1fe120a2015-06-10 11:33:17 -07001278 // We've already validated that this is a STUN binding response with
1279 // the correct local and remote username for this connection.
1280 // So if we're not already, become writable. We may be bringing a pruned
1281 // connection back to life, but if we don't really want it, we can always
1282 // prune it again.
Honghai Zhang8cd8f812016-08-03 19:50:41 -07001283 auto iter = std::find_if(
1284 pings_since_last_response_.begin(), pings_since_last_response_.end(),
1285 [request_id](const SentPing& ping) { return ping.id == request_id; });
1286 if (iter != pings_since_last_response_.end() &&
1287 iter->nomination > acked_nomination_) {
1288 acked_nomination_ = iter->nomination;
1289 }
1290
hbosbf8d3e52017-02-28 06:34:47 -08001291 total_round_trip_time_ms_ += rtt;
1292 current_round_trip_time_ms_ = rtc::Optional<uint32_t>(
1293 static_cast<uint32_t>(rtt));
1294
Honghai Zhang8cd8f812016-08-03 19:50:41 -07001295 pings_since_last_response_.clear();
honghaiz9ad0db52016-07-14 19:30:28 -07001296 last_ping_response_received_ = rtc::TimeMillis();
1297 UpdateReceiving(last_ping_response_received_);
Peter Thatcher1fe120a2015-06-10 11:33:17 -07001298 set_write_state(STATE_WRITABLE);
hbos06495bc2017-01-02 08:08:18 -08001299 set_state(IceCandidatePairState::SUCCEEDED);
skvladd0309122017-02-02 17:18:37 -08001300 if (rtt_samples_ > 0) {
1301 rtt_ = (RTT_RATIO * rtt_ + rtt) / (RTT_RATIO + 1);
1302 } else {
1303 rtt_ = rtt;
1304 }
zhihuang435264a2016-06-21 11:28:38 -07001305 rtt_samples_++;
Peter Thatcher1fe120a2015-06-10 11:33:17 -07001306}
1307
honghaiz34b11eb2016-03-16 08:55:44 -07001308bool Connection::dead(int64_t now) const {
honghaiz37389b42016-01-04 21:57:33 -08001309 if (last_received() > 0) {
1310 // If it has ever received anything, we keep it alive until it hasn't
1311 // received anything for DEAD_CONNECTION_RECEIVE_TIMEOUT. This covers the
1312 // normal case of a successfully used connection that stops working. This
1313 // also allows a remote peer to continue pinging over a locally inactive
1314 // (pruned) connection.
1315 return (now > (last_received() + DEAD_CONNECTION_RECEIVE_TIMEOUT));
1316 }
1317
1318 if (active()) {
1319 // If it has never received anything, keep it alive as long as it is
1320 // actively pinging and not pruned. Otherwise, the connection might be
1321 // deleted before it has a chance to ping. This is the normal case for a
1322 // new connection that is pinging but hasn't received anything yet.
Honghai Zhang2b342bf2015-09-30 09:51:58 -07001323 return false;
1324 }
1325
honghaiz37389b42016-01-04 21:57:33 -08001326 // If it has never received anything and is not actively pinging (pruned), we
1327 // keep it around for at least MIN_CONNECTION_LIFETIME to prevent connections
1328 // from being pruned too quickly during a network change event when two
1329 // networks would be up simultaneously but only for a brief period.
1330 return now > (time_created_ms_ + MIN_CONNECTION_LIFETIME);
Honghai Zhang2b342bf2015-09-30 09:51:58 -07001331}
1332
Taylor Brandstetterb825aee2016-06-29 13:07:16 -07001333bool Connection::stable(int64_t now) const {
zhihuang435264a2016-06-21 11:28:38 -07001334 // A connection is stable if it's RTT has converged and it isn't missing any
1335 // responses. We should send pings at a higher rate until the RTT converges
1336 // and whenever a ping response is missing (so that we can detect
1337 // unwritability faster)
1338 return rtt_converged() && !missing_responses(now);
1339}
1340
guoweis@webrtc.org8c9ff202014-12-04 07:56:02 +00001341std::string Connection::ToDebugId() const {
1342 std::stringstream ss;
1343 ss << std::hex << this;
1344 return ss.str();
1345}
1346
honghaize1a0c942016-02-16 14:54:56 -08001347uint32_t Connection::ComputeNetworkCost() const {
1348 // TODO(honghaiz): Will add rtt as part of the network cost.
Honghai Zhang351d77b2016-05-20 15:08:29 -07001349 return port()->network_cost() + remote_candidate_.network_cost();
honghaize1a0c942016-02-16 14:54:56 -08001350}
1351
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001352std::string Connection::ToString() const {
1353 const char CONNECT_STATE_ABBREV[2] = {
1354 '-', // not connected (false)
1355 'C', // connected (true)
1356 };
Peter Thatcher04ac81f2015-09-21 11:48:28 -07001357 const char RECEIVE_STATE_ABBREV[2] = {
1358 '-', // not receiving (false)
1359 'R', // receiving (true)
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001360 };
1361 const char WRITE_STATE_ABBREV[4] = {
1362 'W', // STATE_WRITABLE
1363 'w', // STATE_WRITE_UNRELIABLE
1364 '-', // STATE_WRITE_INIT
1365 'x', // STATE_WRITE_TIMEOUT
1366 };
1367 const std::string ICESTATE[4] = {
1368 "W", // STATE_WAITING
1369 "I", // STATE_INPROGRESS
1370 "S", // STATE_SUCCEEDED
1371 "F" // STATE_FAILED
1372 };
1373 const Candidate& local = local_candidate();
1374 const Candidate& remote = remote_candidate();
1375 std::stringstream ss;
Honghai Zhang8cd8f812016-08-03 19:50:41 -07001376 ss << "Conn[" << ToDebugId() << ":" << port_->content_name() << ":"
1377 << local.id() << ":" << local.component() << ":" << local.generation()
1378 << ":" << local.type() << ":" << local.protocol() << ":"
1379 << local.address().ToSensitiveString() << "->" << remote.id() << ":"
1380 << remote.component() << ":" << remote.priority() << ":" << remote.type()
1381 << ":" << remote.protocol() << ":" << remote.address().ToSensitiveString()
1382 << "|" << CONNECT_STATE_ABBREV[connected()]
1383 << RECEIVE_STATE_ABBREV[receiving()] << WRITE_STATE_ABBREV[write_state()]
hbos06495bc2017-01-02 08:08:18 -08001384 << ICESTATE[static_cast<int>(state())] << "|" << remote_nomination() << "|"
1385 << nomination() << "|" << priority() << "|";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001386 if (rtt_ < DEFAULT_RTT) {
1387 ss << rtt_ << "]";
1388 } else {
1389 ss << "-]";
1390 }
1391 return ss.str();
1392}
1393
1394std::string Connection::ToSensitiveString() const {
1395 return ToString();
1396}
1397
1398void Connection::OnConnectionRequestResponse(ConnectionRequest* request,
1399 StunMessage* response) {
Peter Thatcher1cf6f812015-05-15 10:40:45 -07001400 // Log at LS_INFO if we receive a ping response on an unwritable
1401 // connection.
1402 rtc::LoggingSeverity sev = !writable() ? rtc::LS_INFO : rtc::LS_VERBOSE;
1403
honghaiz34b11eb2016-03-16 08:55:44 -07001404 int rtt = request->Elapsed();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001405
Peter Thatcherb2d26232015-05-15 11:25:14 -07001406 if (LOG_CHECK_LEVEL_V(sev)) {
Peter Thatcher1cf6f812015-05-15 10:40:45 -07001407 std::string pings;
1408 PrintPingsSinceLastResponse(&pings, 5);
1409 LOG_JV(sev, this) << "Received STUN ping response"
Peter Thatcher42af6ca2015-05-15 12:23:27 -07001410 << ", id=" << rtc::hex_encode(request->id())
1411 << ", code=0" // Makes logging easier to parse.
1412 << ", rtt=" << rtt
Peter Thatcher42af6ca2015-05-15 12:23:27 -07001413 << ", pings_since_last_response=" << pings;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001414 }
Honghai Zhang8cd8f812016-08-03 19:50:41 -07001415 ReceivedPingResponse(rtt, request->id());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001416
zsteinabbacbf2017-03-20 10:53:12 -07001417 int64_t time_received = rtc::TimeMillis();
1418 packet_loss_estimator_.ReceivedResponse(request->id(), time_received);
1419
zhihuang5ecf16c2016-06-01 17:09:15 -07001420 stats_.recv_ping_responses++;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001421
Taylor Brandstetter62351c92016-08-11 16:05:07 -07001422 MaybeUpdateLocalCandidate(request, response);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001423}
1424
1425void Connection::OnConnectionRequestErrorResponse(ConnectionRequest* request,
1426 StunMessage* response) {
deadbeef996fc6b2017-04-26 09:21:22 -07001427 int error_code = response->GetErrorCodeValue();
Peter Thatcher1cf6f812015-05-15 10:40:45 -07001428 LOG_J(LS_INFO, this) << "Received STUN error response"
1429 << " id=" << rtc::hex_encode(request->id())
1430 << " code=" << error_code
1431 << " rtt=" << request->Elapsed();
1432
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001433 if (error_code == STUN_ERROR_UNKNOWN_ATTRIBUTE ||
1434 error_code == STUN_ERROR_SERVER_ERROR ||
1435 error_code == STUN_ERROR_UNAUTHORIZED) {
1436 // Recoverable error, retry
1437 } else if (error_code == STUN_ERROR_STALE_CREDENTIALS) {
1438 // Race failure, retry
1439 } else if (error_code == STUN_ERROR_ROLE_CONFLICT) {
1440 HandleRoleConflictFromPeer();
1441 } else {
1442 // This is not a valid connection.
1443 LOG_J(LS_ERROR, this) << "Received STUN error response, code="
1444 << error_code << "; killing connection";
deadbeef376e1232015-11-25 09:00:08 -08001445 FailAndDestroy();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001446 }
1447}
1448
1449void Connection::OnConnectionRequestTimeout(ConnectionRequest* request) {
1450 // Log at LS_INFO if we miss a ping on a writable connection.
Peter Thatcher1cf6f812015-05-15 10:40:45 -07001451 rtc::LoggingSeverity sev = writable() ? rtc::LS_INFO : rtc::LS_VERBOSE;
1452 LOG_JV(sev, this) << "Timing-out STUN ping "
1453 << rtc::hex_encode(request->id())
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001454 << " after " << request->Elapsed() << " ms";
1455}
1456
Peter Thatcher1cf6f812015-05-15 10:40:45 -07001457void Connection::OnConnectionRequestSent(ConnectionRequest* request) {
1458 // Log at LS_INFO if we send a ping on an unwritable connection.
1459 rtc::LoggingSeverity sev = !writable() ? rtc::LS_INFO : rtc::LS_VERBOSE;
1460 LOG_JV(sev, this) << "Sent STUN ping"
Peter Thatcher42af6ca2015-05-15 12:23:27 -07001461 << ", id=" << rtc::hex_encode(request->id())
Honghai Zhang8cd8f812016-08-03 19:50:41 -07001462 << ", use_candidate=" << use_candidate_attr()
1463 << ", nomination=" << nomination();
zhihuang5ecf16c2016-06-01 17:09:15 -07001464 stats_.sent_ping_requests_total++;
1465 if (stats_.recv_ping_responses == 0) {
1466 stats_.sent_ping_requests_before_first_response++;
1467 }
Peter Thatcher1cf6f812015-05-15 10:40:45 -07001468}
1469
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001470void Connection::HandleRoleConflictFromPeer() {
1471 port_->SignalRoleConflict(port_);
1472}
1473
Honghai Zhang4cedf2b2016-08-31 08:18:11 -07001474void Connection::MaybeSetRemoteIceParametersAndGeneration(
1475 const IceParameters& ice_params,
Taylor Brandstetter0a1bc532016-04-19 18:03:26 -07001476 int generation) {
Honghai Zhang4cedf2b2016-08-31 08:18:11 -07001477 if (remote_candidate_.username() == ice_params.ufrag &&
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +00001478 remote_candidate_.password().empty()) {
Honghai Zhang4cedf2b2016-08-31 08:18:11 -07001479 remote_candidate_.set_password(ice_params.pwd);
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +00001480 }
Taylor Brandstetter0a1bc532016-04-19 18:03:26 -07001481 // TODO(deadbeef): A value of '0' for the generation is used for both
1482 // generation 0 and "generation unknown". It should be changed to an
1483 // rtc::Optional to fix this.
Honghai Zhang4cedf2b2016-08-31 08:18:11 -07001484 if (remote_candidate_.username() == ice_params.ufrag &&
1485 remote_candidate_.password() == ice_params.pwd &&
Taylor Brandstetter0a1bc532016-04-19 18:03:26 -07001486 remote_candidate_.generation() == 0) {
1487 remote_candidate_.set_generation(generation);
1488 }
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +00001489}
1490
1491void Connection::MaybeUpdatePeerReflexiveCandidate(
1492 const Candidate& new_candidate) {
1493 if (remote_candidate_.type() == PRFLX_PORT_TYPE &&
1494 new_candidate.type() != PRFLX_PORT_TYPE &&
1495 remote_candidate_.protocol() == new_candidate.protocol() &&
1496 remote_candidate_.address() == new_candidate.address() &&
1497 remote_candidate_.username() == new_candidate.username() &&
1498 remote_candidate_.password() == new_candidate.password() &&
1499 remote_candidate_.generation() == new_candidate.generation()) {
1500 remote_candidate_ = new_candidate;
1501 }
1502}
1503
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001504void Connection::OnMessage(rtc::Message *pmsg) {
nisseede5da42017-01-12 05:15:36 -08001505 RTC_DCHECK(pmsg->message_id == MSG_DELETE);
honghaiz18f9da02016-06-01 23:53:01 -07001506 LOG(LS_INFO) << "Connection deleted with number of pings sent: "
1507 << num_pings_sent_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001508 SignalDestroyed(this);
1509 delete this;
1510}
1511
honghaiz34b11eb2016-03-16 08:55:44 -07001512int64_t Connection::last_received() const {
Peter Thatcher54360512015-07-08 11:08:35 -07001513 return std::max(last_data_received_,
1514 std::max(last_ping_received_, last_ping_response_received_));
1515}
1516
zhihuang5ecf16c2016-06-01 17:09:15 -07001517ConnectionInfo Connection::stats() {
1518 stats_.recv_bytes_second = round(recv_rate_tracker_.ComputeRate());
1519 stats_.recv_total_bytes = recv_rate_tracker_.TotalSampleCount();
1520 stats_.sent_bytes_second = round(send_rate_tracker_.ComputeRate());
1521 stats_.sent_total_bytes = send_rate_tracker_.TotalSampleCount();
hbos06495bc2017-01-02 08:08:18 -08001522 stats_.receiving = receiving_;
1523 stats_.writable = write_state_ == STATE_WRITABLE;
1524 stats_.timeout = write_state_ == STATE_WRITE_TIMEOUT;
1525 stats_.new_connection = !reported_;
1526 stats_.rtt = rtt_;
1527 stats_.local_candidate = local_candidate();
1528 stats_.remote_candidate = remote_candidate();
1529 stats_.key = this;
1530 stats_.state = state_;
1531 stats_.priority = priority();
hbos92eaec62017-02-27 01:38:08 -08001532 stats_.nominated = nominated();
hbosbf8d3e52017-02-28 06:34:47 -08001533 stats_.total_round_trip_time_ms = total_round_trip_time_ms_;
1534 stats_.current_round_trip_time_ms = current_round_trip_time_ms_;
zhihuang5ecf16c2016-06-01 17:09:15 -07001535 return stats_;
guoweis@webrtc.org930e0042014-11-17 19:42:14 +00001536}
1537
Taylor Brandstetter62351c92016-08-11 16:05:07 -07001538void Connection::MaybeUpdateLocalCandidate(ConnectionRequest* request,
1539 StunMessage* response) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001540 // RFC 5245
1541 // The agent checks the mapped address from the STUN response. If the
1542 // transport address does not match any of the local candidates that the
1543 // agent knows about, the mapped address represents a new candidate -- a
1544 // peer reflexive candidate.
1545 const StunAddressAttribute* addr =
1546 response->GetAddress(STUN_ATTR_XOR_MAPPED_ADDRESS);
1547 if (!addr) {
1548 LOG(LS_WARNING) << "Connection::OnConnectionRequestResponse - "
1549 << "No MAPPED-ADDRESS or XOR-MAPPED-ADDRESS found in the "
1550 << "stun response message";
1551 return;
1552 }
1553
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001554 for (size_t i = 0; i < port_->Candidates().size(); ++i) {
1555 if (port_->Candidates()[i].address() == addr->GetAddress()) {
Taylor Brandstetter62351c92016-08-11 16:05:07 -07001556 if (local_candidate_index_ != i) {
1557 LOG_J(LS_INFO, this) << "Updating local candidate type to srflx.";
1558 local_candidate_index_ = i;
1559 // SignalStateChange to force a re-sort in P2PTransportChannel as this
1560 // Connection's local candidate has changed.
1561 SignalStateChange(this);
1562 }
1563 return;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001564 }
1565 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001566
1567 // RFC 5245
1568 // Its priority is set equal to the value of the PRIORITY attribute
1569 // in the Binding request.
1570 const StunUInt32Attribute* priority_attr =
1571 request->msg()->GetUInt32(STUN_ATTR_PRIORITY);
1572 if (!priority_attr) {
1573 LOG(LS_WARNING) << "Connection::OnConnectionRequestResponse - "
1574 << "No STUN_ATTR_PRIORITY found in the "
1575 << "stun response message";
1576 return;
1577 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001578 const uint32_t priority = priority_attr->value();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001579 std::string id = rtc::CreateRandomString(8);
1580
1581 Candidate new_local_candidate;
1582 new_local_candidate.set_id(id);
1583 new_local_candidate.set_component(local_candidate().component());
1584 new_local_candidate.set_type(PRFLX_PORT_TYPE);
1585 new_local_candidate.set_protocol(local_candidate().protocol());
1586 new_local_candidate.set_address(addr->GetAddress());
1587 new_local_candidate.set_priority(priority);
1588 new_local_candidate.set_username(local_candidate().username());
1589 new_local_candidate.set_password(local_candidate().password());
1590 new_local_candidate.set_network_name(local_candidate().network_name());
guoweis@webrtc.org950c5182014-12-16 23:01:31 +00001591 new_local_candidate.set_network_type(local_candidate().network_type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001592 new_local_candidate.set_related_address(local_candidate().address());
Taylor Brandstetterf7c15a92016-06-22 13:13:55 -07001593 new_local_candidate.set_generation(local_candidate().generation());
Honghai Zhang80f1db92016-01-27 11:54:45 -08001594 new_local_candidate.set_foundation(ComputeFoundation(
1595 PRFLX_PORT_TYPE, local_candidate().protocol(),
1596 local_candidate().relay_protocol(), local_candidate().address()));
honghaiza0c44ea2016-03-23 16:07:48 -07001597 new_local_candidate.set_network_id(local_candidate().network_id());
1598 new_local_candidate.set_network_cost(local_candidate().network_cost());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001599
1600 // Change the local candidate of this Connection to the new prflx candidate.
Taylor Brandstetter62351c92016-08-11 16:05:07 -07001601 LOG_J(LS_INFO, this) << "Updating local candidate type to prflx.";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001602 local_candidate_index_ = port_->AddPrflxCandidate(new_local_candidate);
1603
1604 // SignalStateChange to force a re-sort in P2PTransportChannel as this
1605 // Connection's local candidate has changed.
1606 SignalStateChange(this);
1607}
1608
Taylor Brandstetterb825aee2016-06-29 13:07:16 -07001609bool Connection::rtt_converged() const {
zhihuang435264a2016-06-21 11:28:38 -07001610 return rtt_samples_ > (RTT_RATIO + 1);
1611}
1612
Taylor Brandstetterb825aee2016-06-29 13:07:16 -07001613bool Connection::missing_responses(int64_t now) const {
zhihuang435264a2016-06-21 11:28:38 -07001614 if (pings_since_last_response_.empty()) {
1615 return false;
1616 }
1617
1618 int64_t waiting = now - pings_since_last_response_[0].sent_time;
1619 return waiting > 2 * rtt();
1620}
1621
deadbeef376e1232015-11-25 09:00:08 -08001622ProxyConnection::ProxyConnection(Port* port,
1623 size_t index,
1624 const Candidate& remote_candidate)
1625 : Connection(port, index, remote_candidate) {}
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001626
1627int ProxyConnection::Send(const void* data, size_t size,
1628 const rtc::PacketOptions& options) {
zhihuang5ecf16c2016-06-01 17:09:15 -07001629 stats_.sent_total_packets++;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001630 int sent = port_->SendTo(data, size, remote_candidate_.address(),
1631 options, true);
1632 if (sent <= 0) {
nisseede5da42017-01-12 05:15:36 -08001633 RTC_DCHECK(sent < 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001634 error_ = port_->GetError();
zhihuang5ecf16c2016-06-01 17:09:15 -07001635 stats_.sent_discarded_packets++;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001636 } else {
Tim Psiaki63046262015-09-14 10:38:08 -07001637 send_rate_tracker_.AddSamples(sent);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001638 }
1639 return sent;
1640}
1641
1642} // namespace cricket