blob: f04e61942682b63de0bb6b8ea70d90fa59bd49c3 [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
16#include "webrtc/p2p/base/common.h"
17#include "webrtc/p2p/base/portallocator.h"
18#include "webrtc/base/base64.h"
19#include "webrtc/base/crc32.h"
20#include "webrtc/base/helpers.h"
21#include "webrtc/base/logging.h"
22#include "webrtc/base/messagedigest.h"
23#include "webrtc/base/scoped_ptr.h"
24#include "webrtc/base/stringencode.h"
25#include "webrtc/base/stringutils.h"
26
27namespace {
28
29// Determines whether we have seen at least the given maximum number of
30// pings fail to have a response.
31inline bool TooManyFailures(
Peter Thatcher1cf6f812015-05-15 10:40:45 -070032 const std::vector<cricket::Connection::SentPing>& pings_since_last_response,
Peter Boström0c4e06b2015-10-07 12:23:21 +020033 uint32_t maximum_failures,
34 uint32_t rtt_estimate,
35 uint32_t now) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000036 // If we haven't sent that many pings, then we can't have failed that many.
37 if (pings_since_last_response.size() < maximum_failures)
38 return false;
39
40 // Check if the window in which we would expect a response to the ping has
41 // already elapsed.
Peter Boström0c4e06b2015-10-07 12:23:21 +020042 uint32_t expected_response_time =
Peter Thatcher1cf6f812015-05-15 10:40:45 -070043 pings_since_last_response[maximum_failures - 1].sent_time + rtt_estimate;
44 return now > expected_response_time;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000045}
46
47// Determines whether we have gone too long without seeing any response.
48inline bool TooLongWithoutResponse(
Peter Thatcher1cf6f812015-05-15 10:40:45 -070049 const std::vector<cricket::Connection::SentPing>& pings_since_last_response,
Peter Boström0c4e06b2015-10-07 12:23:21 +020050 uint32_t maximum_time,
51 uint32_t now) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000052 if (pings_since_last_response.size() == 0)
53 return false;
54
Peter Thatcher1cf6f812015-05-15 10:40:45 -070055 auto first = pings_since_last_response[0];
56 return now > (first.sent_time + maximum_time);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000057}
58
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000059// We will restrict RTT estimates (when used for determining state) to be
60// within a reasonable range.
Peter Boström0c4e06b2015-10-07 12:23:21 +020061const uint32_t MINIMUM_RTT = 100; // 0.1 seconds
62const uint32_t MAXIMUM_RTT = 3000; // 3 seconds
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000063
64// When we don't have any RTT data, we have to pick something reasonable. We
65// use a large value just in case the connection is really slow.
Peter Boström0c4e06b2015-10-07 12:23:21 +020066const uint32_t DEFAULT_RTT = MAXIMUM_RTT;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000067
68// Computes our estimate of the RTT given the current estimate.
Peter Boström0c4e06b2015-10-07 12:23:21 +020069inline uint32_t ConservativeRTTEstimate(uint32_t rtt) {
andresp@webrtc.orgff689be2015-02-12 11:54:26 +000070 return std::max(MINIMUM_RTT, std::min(MAXIMUM_RTT, 2 * rtt));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000071}
72
73// Weighting of the old rtt value to new data.
74const int RTT_RATIO = 3; // 3 : 1
75
76// The delay before we begin checking if this port is useless.
77const int kPortTimeoutDelay = 30 * 1000; // 30 seconds
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000078}
79
80namespace cricket {
81
82// TODO(ronghuawu): Use "host", "srflx", "prflx" and "relay". But this requires
83// the signaling part be updated correspondingly as well.
84const char LOCAL_PORT_TYPE[] = "local";
85const char STUN_PORT_TYPE[] = "stun";
86const char PRFLX_PORT_TYPE[] = "prflx";
87const char RELAY_PORT_TYPE[] = "relay";
88
89const char UDP_PROTOCOL_NAME[] = "udp";
90const char TCP_PROTOCOL_NAME[] = "tcp";
91const char SSLTCP_PROTOCOL_NAME[] = "ssltcp";
92
93static const char* const PROTO_NAMES[] = { UDP_PROTOCOL_NAME,
94 TCP_PROTOCOL_NAME,
95 SSLTCP_PROTOCOL_NAME };
96
97const char* ProtoToString(ProtocolType proto) {
98 return PROTO_NAMES[proto];
99}
100
101bool StringToProto(const char* value, ProtocolType* proto) {
102 for (size_t i = 0; i <= PROTO_LAST; ++i) {
103 if (_stricmp(PROTO_NAMES[i], value) == 0) {
104 *proto = static_cast<ProtocolType>(i);
105 return true;
106 }
107 }
108 return false;
109}
110
111// RFC 6544, TCP candidate encoding rules.
112const int DISCARD_PORT = 9;
113const char TCPTYPE_ACTIVE_STR[] = "active";
114const char TCPTYPE_PASSIVE_STR[] = "passive";
115const char TCPTYPE_SIMOPEN_STR[] = "so";
116
117// Foundation: An arbitrary string that is the same for two candidates
118// that have the same type, base IP address, protocol (UDP, TCP,
119// etc.), and STUN or TURN server. If any of these are different,
120// then the foundation will be different. Two candidate pairs with
121// the same foundation pairs are likely to have similar network
122// characteristics. Foundations are used in the frozen algorithm.
123static std::string ComputeFoundation(
124 const std::string& type,
125 const std::string& protocol,
126 const rtc::SocketAddress& base_address) {
127 std::ostringstream ost;
128 ost << type << base_address.ipaddr().ToString() << protocol;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200129 return rtc::ToString<uint32_t>(rtc::ComputeCrc32(ost.str()));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000130}
131
pkasting@chromium.org332331f2014-11-06 20:19:22 +0000132Port::Port(rtc::Thread* thread,
133 rtc::PacketSocketFactory* factory,
134 rtc::Network* network,
135 const rtc::IPAddress& ip,
136 const std::string& username_fragment,
137 const std::string& password)
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000138 : thread_(thread),
139 factory_(factory),
140 send_retransmit_count_attribute_(false),
141 network_(network),
142 ip_(ip),
143 min_port_(0),
144 max_port_(0),
145 component_(ICE_CANDIDATE_COMPONENT_DEFAULT),
146 generation_(0),
147 ice_username_fragment_(username_fragment),
148 password_(password),
149 timeout_delay_(kPortTimeoutDelay),
150 enable_port_packets_(false),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000151 ice_role_(ICEROLE_UNKNOWN),
152 tiebreaker_(0),
153 shared_socket_(true),
154 candidate_filter_(CF_ALL) {
155 Construct();
156}
157
pkasting@chromium.org332331f2014-11-06 20:19:22 +0000158Port::Port(rtc::Thread* thread,
159 const std::string& type,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000160 rtc::PacketSocketFactory* factory,
pkasting@chromium.org332331f2014-11-06 20:19:22 +0000161 rtc::Network* network,
162 const rtc::IPAddress& ip,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200163 uint16_t min_port,
164 uint16_t max_port,
pkasting@chromium.org332331f2014-11-06 20:19:22 +0000165 const std::string& username_fragment,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000166 const std::string& password)
167 : thread_(thread),
168 factory_(factory),
169 type_(type),
170 send_retransmit_count_attribute_(false),
171 network_(network),
172 ip_(ip),
173 min_port_(min_port),
174 max_port_(max_port),
175 component_(ICE_CANDIDATE_COMPONENT_DEFAULT),
176 generation_(0),
177 ice_username_fragment_(username_fragment),
178 password_(password),
179 timeout_delay_(kPortTimeoutDelay),
180 enable_port_packets_(false),
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000181 ice_role_(ICEROLE_UNKNOWN),
182 tiebreaker_(0),
183 shared_socket_(false),
184 candidate_filter_(CF_ALL) {
185 ASSERT(factory_ != NULL);
186 Construct();
187}
188
189void Port::Construct() {
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700190 // TODO(pthatcher): Remove this old behavior once we're sure no one
191 // relies on it. If the username_fragment and password are empty,
192 // we should just create one.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000193 if (ice_username_fragment_.empty()) {
194 ASSERT(password_.empty());
195 ice_username_fragment_ = rtc::CreateRandomString(ICE_UFRAG_LENGTH);
196 password_ = rtc::CreateRandomString(ICE_PWD_LENGTH);
197 }
198 LOG_J(LS_INFO, this) << "Port created";
199}
200
201Port::~Port() {
202 // Delete all of the remaining connections. We copy the list up front
203 // because each deletion will cause it to be modified.
204
205 std::vector<Connection*> list;
206
207 AddressMap::iterator iter = connections_.begin();
208 while (iter != connections_.end()) {
209 list.push_back(iter->second);
210 ++iter;
211 }
212
Peter Boström0c4e06b2015-10-07 12:23:21 +0200213 for (uint32_t i = 0; i < list.size(); i++)
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000214 delete list[i];
215}
216
217Connection* Port::GetConnection(const rtc::SocketAddress& remote_addr) {
218 AddressMap::const_iterator iter = connections_.find(remote_addr);
219 if (iter != connections_.end())
220 return iter->second;
221 else
222 return NULL;
223}
224
225void Port::AddAddress(const rtc::SocketAddress& address,
226 const rtc::SocketAddress& base_address,
227 const rtc::SocketAddress& related_address,
228 const std::string& protocol,
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700229 const std::string& relay_protocol,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000230 const std::string& tcptype,
231 const std::string& type,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200232 uint32_t type_preference,
233 uint32_t relay_preference,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000234 bool final) {
235 if (protocol == TCP_PROTOCOL_NAME && type == LOCAL_PORT_TYPE) {
236 ASSERT(!tcptype.empty());
237 }
238
239 Candidate c;
240 c.set_id(rtc::CreateRandomString(8));
241 c.set_component(component_);
242 c.set_type(type);
243 c.set_protocol(protocol);
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700244 c.set_relay_protocol(relay_protocol);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000245 c.set_tcptype(tcptype);
246 c.set_address(address);
247 c.set_priority(c.GetPriority(type_preference, network_->preference(),
248 relay_preference));
249 c.set_username(username_fragment());
250 c.set_password(password_);
251 c.set_network_name(network_->name());
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000252 c.set_network_type(network_->type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000253 c.set_generation(generation_);
254 c.set_related_address(related_address);
255 c.set_foundation(ComputeFoundation(type, protocol, base_address));
256 candidates_.push_back(c);
257 SignalCandidateReady(this, c);
258
259 if (final) {
260 SignalPortComplete(this);
261 }
262}
263
264void Port::AddConnection(Connection* conn) {
265 connections_[conn->remote_candidate().address()] = conn;
266 conn->SignalDestroyed.connect(this, &Port::OnConnectionDestroyed);
267 SignalConnectionCreated(this, conn);
268}
269
270void Port::OnReadPacket(
271 const char* data, size_t size, const rtc::SocketAddress& addr,
272 ProtocolType proto) {
273 // If the user has enabled port packets, just hand this over.
274 if (enable_port_packets_) {
275 SignalReadPacket(this, data, size, addr);
276 return;
277 }
278
279 // If this is an authenticated STUN request, then signal unknown address and
280 // send back a proper binding response.
281 rtc::scoped_ptr<IceMessage> msg;
282 std::string remote_username;
283 if (!GetStunMessage(data, size, addr, msg.accept(), &remote_username)) {
284 LOG_J(LS_ERROR, this) << "Received non-STUN packet from unknown address ("
285 << addr.ToSensitiveString() << ")";
286 } else if (!msg) {
287 // STUN message handled already
288 } else if (msg->type() == STUN_BINDING_REQUEST) {
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700289 LOG(LS_INFO) << "Received STUN ping "
290 << " id=" << rtc::hex_encode(msg->transaction_id())
291 << " from unknown address " << addr.ToSensitiveString();
292
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000293 // Check for role conflicts.
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700294 if (!MaybeIceRoleConflict(addr, msg.get(), remote_username)) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000295 LOG(LS_INFO) << "Received conflicting role from the peer.";
296 return;
297 }
298
299 SignalUnknownAddress(this, addr, proto, msg.get(), remote_username, false);
300 } else {
301 // NOTE(tschmelcher): STUN_BINDING_RESPONSE is benign. It occurs if we
302 // pruned a connection for this port while it had STUN requests in flight,
303 // because we then get back responses for them, which this code correctly
304 // does not handle.
305 if (msg->type() != STUN_BINDING_RESPONSE) {
306 LOG_J(LS_ERROR, this) << "Received unexpected STUN message type ("
307 << msg->type() << ") from unknown address ("
308 << addr.ToSensitiveString() << ")";
309 }
310 }
311}
312
stefanc1aeaf02015-10-15 07:26:07 -0700313void Port::OnSentPacket(const rtc::SentPacket& sent_packet) {
314 PortInterface::SignalSentPacket(this, sent_packet);
315}
316
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000317void Port::OnReadyToSend() {
318 AddressMap::iterator iter = connections_.begin();
319 for (; iter != connections_.end(); ++iter) {
320 iter->second->OnReadyToSend();
321 }
322}
323
324size_t Port::AddPrflxCandidate(const Candidate& local) {
325 candidates_.push_back(local);
326 return (candidates_.size() - 1);
327}
328
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000329bool Port::GetStunMessage(const char* data, size_t size,
330 const rtc::SocketAddress& addr,
331 IceMessage** out_msg, std::string* out_username) {
332 // NOTE: This could clearly be optimized to avoid allocating any memory.
333 // However, at the data rates we'll be looking at on the client side,
334 // this probably isn't worth worrying about.
335 ASSERT(out_msg != NULL);
336 ASSERT(out_username != NULL);
337 *out_msg = NULL;
338 out_username->clear();
339
340 // Don't bother parsing the packet if we can tell it's not STUN.
341 // In ICE mode, all STUN packets will have a valid fingerprint.
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700342 if (!StunMessage::ValidateFingerprint(data, size)) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000343 return false;
344 }
345
346 // Parse the request message. If the packet is not a complete and correct
347 // STUN message, then ignore it.
348 rtc::scoped_ptr<IceMessage> stun_msg(new IceMessage());
349 rtc::ByteBuffer buf(data, size);
350 if (!stun_msg->Read(&buf) || (buf.Length() > 0)) {
351 return false;
352 }
353
354 if (stun_msg->type() == STUN_BINDING_REQUEST) {
355 // Check for the presence of USERNAME and MESSAGE-INTEGRITY (if ICE) first.
356 // If not present, fail with a 400 Bad Request.
357 if (!stun_msg->GetByteString(STUN_ATTR_USERNAME) ||
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700358 !stun_msg->GetByteString(STUN_ATTR_MESSAGE_INTEGRITY)) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000359 LOG_J(LS_ERROR, this) << "Received STUN request without username/M-I "
360 << "from " << addr.ToSensitiveString();
361 SendBindingErrorResponse(stun_msg.get(), addr, STUN_ERROR_BAD_REQUEST,
362 STUN_ERROR_REASON_BAD_REQUEST);
363 return true;
364 }
365
366 // If the username is bad or unknown, fail with a 401 Unauthorized.
367 std::string local_ufrag;
368 std::string remote_ufrag;
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700369 if (!ParseStunUsername(stun_msg.get(), &local_ufrag, &remote_ufrag) ||
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000370 local_ufrag != username_fragment()) {
371 LOG_J(LS_ERROR, this) << "Received STUN request with bad local username "
372 << local_ufrag << " from "
373 << addr.ToSensitiveString();
374 SendBindingErrorResponse(stun_msg.get(), addr, STUN_ERROR_UNAUTHORIZED,
375 STUN_ERROR_REASON_UNAUTHORIZED);
376 return true;
377 }
378
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000379 // If ICE, and the MESSAGE-INTEGRITY is bad, fail with a 401 Unauthorized
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700380 if (!stun_msg->ValidateMessageIntegrity(data, size, password_)) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000381 LOG_J(LS_ERROR, this) << "Received STUN request with bad M-I "
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000382 << "from " << addr.ToSensitiveString()
383 << ", password_=" << password_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000384 SendBindingErrorResponse(stun_msg.get(), addr, STUN_ERROR_UNAUTHORIZED,
385 STUN_ERROR_REASON_UNAUTHORIZED);
386 return true;
387 }
388 out_username->assign(remote_ufrag);
389 } else if ((stun_msg->type() == STUN_BINDING_RESPONSE) ||
390 (stun_msg->type() == STUN_BINDING_ERROR_RESPONSE)) {
391 if (stun_msg->type() == STUN_BINDING_ERROR_RESPONSE) {
392 if (const StunErrorCodeAttribute* error_code = stun_msg->GetErrorCode()) {
393 LOG_J(LS_ERROR, this) << "Received STUN binding error:"
394 << " class=" << error_code->eclass()
395 << " number=" << error_code->number()
396 << " reason='" << error_code->reason() << "'"
397 << " from " << addr.ToSensitiveString();
398 // Return message to allow error-specific processing
399 } else {
400 LOG_J(LS_ERROR, this) << "Received STUN binding error without a error "
401 << "code from " << addr.ToSensitiveString();
402 return true;
403 }
404 }
405 // NOTE: Username should not be used in verifying response messages.
406 out_username->clear();
407 } else if (stun_msg->type() == STUN_BINDING_INDICATION) {
408 LOG_J(LS_VERBOSE, this) << "Received STUN binding indication:"
409 << " from " << addr.ToSensitiveString();
410 out_username->clear();
411 // No stun attributes will be verified, if it's stun indication message.
412 // Returning from end of the this method.
413 } else {
414 LOG_J(LS_ERROR, this) << "Received STUN packet with invalid type ("
415 << stun_msg->type() << ") from "
416 << addr.ToSensitiveString();
417 return true;
418 }
419
420 // Return the STUN message found.
421 *out_msg = stun_msg.release();
422 return true;
423}
424
425bool Port::IsCompatibleAddress(const rtc::SocketAddress& addr) {
426 int family = ip().family();
427 // We use single-stack sockets, so families must match.
428 if (addr.family() != family) {
429 return false;
430 }
431 // Link-local IPv6 ports can only connect to other link-local IPv6 ports.
Peter Thatcherb8b01432015-07-07 16:45:53 -0700432 if (family == AF_INET6 &&
433 (IPIsLinkLocal(ip()) != IPIsLinkLocal(addr.ipaddr()))) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000434 return false;
435 }
436 return true;
437}
438
439bool Port::ParseStunUsername(const StunMessage* stun_msg,
440 std::string* local_ufrag,
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700441 std::string* remote_ufrag) const {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000442 // The packet must include a username that either begins or ends with our
443 // fragment. It should begin with our fragment if it is a request and it
444 // should end with our fragment if it is a response.
445 local_ufrag->clear();
446 remote_ufrag->clear();
447 const StunByteStringAttribute* username_attr =
448 stun_msg->GetByteString(STUN_ATTR_USERNAME);
449 if (username_attr == NULL)
450 return false;
451
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700452 // RFRAG:LFRAG
453 const std::string username = username_attr->GetString();
454 size_t colon_pos = username.find(":");
455 if (colon_pos == std::string::npos) {
456 return false;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000457 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000458
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700459 *local_ufrag = username.substr(0, colon_pos);
460 *remote_ufrag = username.substr(colon_pos + 1, username.size());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000461 return true;
462}
463
464bool Port::MaybeIceRoleConflict(
465 const rtc::SocketAddress& addr, IceMessage* stun_msg,
466 const std::string& remote_ufrag) {
467 // Validate ICE_CONTROLLING or ICE_CONTROLLED attributes.
468 bool ret = true;
469 IceRole remote_ice_role = ICEROLE_UNKNOWN;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200470 uint64_t remote_tiebreaker = 0;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000471 const StunUInt64Attribute* stun_attr =
472 stun_msg->GetUInt64(STUN_ATTR_ICE_CONTROLLING);
473 if (stun_attr) {
474 remote_ice_role = ICEROLE_CONTROLLING;
475 remote_tiebreaker = stun_attr->value();
476 }
477
478 // If |remote_ufrag| is same as port local username fragment and
479 // tie breaker value received in the ping message matches port
480 // tiebreaker value this must be a loopback call.
481 // We will treat this as valid scenario.
482 if (remote_ice_role == ICEROLE_CONTROLLING &&
483 username_fragment() == remote_ufrag &&
484 remote_tiebreaker == IceTiebreaker()) {
485 return true;
486 }
487
488 stun_attr = stun_msg->GetUInt64(STUN_ATTR_ICE_CONTROLLED);
489 if (stun_attr) {
490 remote_ice_role = ICEROLE_CONTROLLED;
491 remote_tiebreaker = stun_attr->value();
492 }
493
494 switch (ice_role_) {
495 case ICEROLE_CONTROLLING:
496 if (ICEROLE_CONTROLLING == remote_ice_role) {
497 if (remote_tiebreaker >= tiebreaker_) {
498 SignalRoleConflict(this);
499 } else {
500 // Send Role Conflict (487) error response.
501 SendBindingErrorResponse(stun_msg, addr,
502 STUN_ERROR_ROLE_CONFLICT, STUN_ERROR_REASON_ROLE_CONFLICT);
503 ret = false;
504 }
505 }
506 break;
507 case ICEROLE_CONTROLLED:
508 if (ICEROLE_CONTROLLED == remote_ice_role) {
509 if (remote_tiebreaker < tiebreaker_) {
510 SignalRoleConflict(this);
511 } else {
512 // Send Role Conflict (487) error response.
513 SendBindingErrorResponse(stun_msg, addr,
514 STUN_ERROR_ROLE_CONFLICT, STUN_ERROR_REASON_ROLE_CONFLICT);
515 ret = false;
516 }
517 }
518 break;
519 default:
520 ASSERT(false);
521 }
522 return ret;
523}
524
525void Port::CreateStunUsername(const std::string& remote_username,
526 std::string* stun_username_attr_str) const {
527 stun_username_attr_str->clear();
528 *stun_username_attr_str = remote_username;
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700529 stun_username_attr_str->append(":");
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000530 stun_username_attr_str->append(username_fragment());
531}
532
533void Port::SendBindingResponse(StunMessage* request,
534 const rtc::SocketAddress& addr) {
535 ASSERT(request->type() == STUN_BINDING_REQUEST);
536
537 // Retrieve the username from the request.
538 const StunByteStringAttribute* username_attr =
539 request->GetByteString(STUN_ATTR_USERNAME);
540 ASSERT(username_attr != NULL);
541 if (username_attr == NULL) {
542 // No valid username, skip the response.
543 return;
544 }
545
546 // Fill in the response message.
547 StunMessage response;
548 response.SetType(STUN_BINDING_RESPONSE);
549 response.SetTransactionID(request->transaction_id());
550 const StunUInt32Attribute* retransmit_attr =
551 request->GetUInt32(STUN_ATTR_RETRANSMIT_COUNT);
552 if (retransmit_attr) {
553 // Inherit the incoming retransmit value in the response so the other side
554 // can see our view of lost pings.
555 response.AddAttribute(new StunUInt32Attribute(
556 STUN_ATTR_RETRANSMIT_COUNT, retransmit_attr->value()));
557
558 if (retransmit_attr->value() > CONNECTION_WRITE_CONNECT_FAILURES) {
559 LOG_J(LS_INFO, this)
560 << "Received a remote ping with high retransmit count: "
561 << retransmit_attr->value();
562 }
563 }
564
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700565 response.AddAttribute(
566 new StunXorAddressAttribute(STUN_ATTR_XOR_MAPPED_ADDRESS, addr));
567 response.AddMessageIntegrity(password_);
568 response.AddFingerprint();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000569
570 // Send the response message.
571 rtc::ByteBuffer buf;
572 response.Write(&buf);
573 rtc::PacketOptions options(DefaultDscpValue());
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700574 auto err = SendTo(buf.Data(), buf.Length(), addr, options, false);
575 if (err < 0) {
576 LOG_J(LS_ERROR, this)
577 << "Failed to send STUN ping response"
578 << ", to=" << addr.ToSensitiveString()
579 << ", err=" << err
580 << ", id=" << rtc::hex_encode(response.transaction_id());
581 } else {
582 // Log at LS_INFO if we send a stun ping response on an unwritable
583 // connection.
honghaiz9b5ee9c2015-11-11 13:19:17 -0800584 Connection* conn = GetConnection(addr);
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700585 rtc::LoggingSeverity sev = (conn && !conn->writable()) ?
586 rtc::LS_INFO : rtc::LS_VERBOSE;
587 LOG_JV(sev, this)
588 << "Sent STUN ping response"
589 << ", to=" << addr.ToSensitiveString()
590 << ", id=" << rtc::hex_encode(response.transaction_id());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000591 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000592}
593
594void Port::SendBindingErrorResponse(StunMessage* request,
595 const rtc::SocketAddress& addr,
596 int error_code, const std::string& reason) {
597 ASSERT(request->type() == STUN_BINDING_REQUEST);
598
599 // Fill in the response message.
600 StunMessage response;
601 response.SetType(STUN_BINDING_ERROR_RESPONSE);
602 response.SetTransactionID(request->transaction_id());
603
604 // When doing GICE, we need to write out the error code incorrectly to
605 // maintain backwards compatiblility.
606 StunErrorCodeAttribute* error_attr = StunAttribute::CreateErrorCode();
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700607 error_attr->SetCode(error_code);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000608 error_attr->SetReason(reason);
609 response.AddAttribute(error_attr);
610
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700611 // Per Section 10.1.2, certain error cases don't get a MESSAGE-INTEGRITY,
612 // because we don't have enough information to determine the shared secret.
613 if (error_code != STUN_ERROR_BAD_REQUEST &&
614 error_code != STUN_ERROR_UNAUTHORIZED)
615 response.AddMessageIntegrity(password_);
616 response.AddFingerprint();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000617
618 // Send the response message.
619 rtc::ByteBuffer buf;
620 response.Write(&buf);
621 rtc::PacketOptions options(DefaultDscpValue());
622 SendTo(buf.Data(), buf.Length(), addr, options, false);
623 LOG_J(LS_INFO, this) << "Sending STUN binding error: reason=" << reason
624 << " to " << addr.ToSensitiveString();
625}
626
627void Port::OnMessage(rtc::Message *pmsg) {
honghaizd0b31432015-09-30 12:42:17 -0700628 ASSERT(pmsg->message_id == MSG_DEAD);
629 if (dead()) {
630 Destroy();
631 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000632}
633
634std::string Port::ToString() const {
635 std::stringstream ss;
636 ss << "Port[" << content_name_ << ":" << component_
637 << ":" << generation_ << ":" << type_
638 << ":" << network_->ToString() << "]";
639 return ss.str();
640}
641
642void Port::EnablePortPackets() {
643 enable_port_packets_ = true;
644}
645
646void Port::OnConnectionDestroyed(Connection* conn) {
647 AddressMap::iterator iter =
648 connections_.find(conn->remote_candidate().address());
649 ASSERT(iter != connections_.end());
650 connections_.erase(iter);
651
honghaizd0b31432015-09-30 12:42:17 -0700652 // On the controlled side, ports time out after all connections fail.
653 // Note: If a new connection is added after this message is posted, but it
654 // fails and is removed before kPortTimeoutDelay, then this message will
655 // still cause the Port to be destroyed.
656 if (dead()) {
657 thread_->PostDelayed(timeout_delay_, this, MSG_DEAD);
658 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000659}
660
661void Port::Destroy() {
662 ASSERT(connections_.empty());
663 LOG_J(LS_INFO, this) << "Port deleted";
664 SignalDestroyed(this);
665 delete this;
666}
667
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000668const std::string Port::username_fragment() const {
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700669 return ice_username_fragment_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000670}
671
672// A ConnectionRequest is a simple STUN ping used to determine writability.
673class ConnectionRequest : public StunRequest {
674 public:
675 explicit ConnectionRequest(Connection* connection)
676 : StunRequest(new IceMessage()),
677 connection_(connection) {
678 }
679
680 virtual ~ConnectionRequest() {
681 }
682
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700683 void Prepare(StunMessage* request) override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000684 request->SetType(STUN_BINDING_REQUEST);
685 std::string username;
686 connection_->port()->CreateStunUsername(
687 connection_->remote_candidate().username(), &username);
688 request->AddAttribute(
689 new StunByteStringAttribute(STUN_ATTR_USERNAME, username));
690
691 // connection_ already holds this ping, so subtract one from count.
692 if (connection_->port()->send_retransmit_count_attribute()) {
693 request->AddAttribute(new StunUInt32Attribute(
694 STUN_ATTR_RETRANSMIT_COUNT,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200695 static_cast<uint32_t>(connection_->pings_since_last_response_.size() -
696 1)));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000697 }
698
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700699 // Adding ICE_CONTROLLED or ICE_CONTROLLING attribute based on the role.
700 if (connection_->port()->GetIceRole() == ICEROLE_CONTROLLING) {
701 request->AddAttribute(new StunUInt64Attribute(
702 STUN_ATTR_ICE_CONTROLLING, connection_->port()->IceTiebreaker()));
703 // Since we are trying aggressive nomination, sending USE-CANDIDATE
704 // attribute in every ping.
705 // If we are dealing with a ice-lite end point, nomination flag
706 // in Connection will be set to false by default. Once the connection
707 // becomes "best connection", nomination flag will be turned on.
708 if (connection_->use_candidate_attr()) {
709 request->AddAttribute(new StunByteStringAttribute(
710 STUN_ATTR_USE_CANDIDATE));
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000711 }
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700712 } else if (connection_->port()->GetIceRole() == ICEROLE_CONTROLLED) {
713 request->AddAttribute(new StunUInt64Attribute(
714 STUN_ATTR_ICE_CONTROLLED, connection_->port()->IceTiebreaker()));
715 } else {
716 ASSERT(false);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000717 }
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700718
719 // Adding PRIORITY Attribute.
720 // Changing the type preference to Peer Reflexive and local preference
721 // and component id information is unchanged from the original priority.
722 // priority = (2^24)*(type preference) +
723 // (2^8)*(local preference) +
724 // (2^0)*(256 - component ID)
Peter Boström0c4e06b2015-10-07 12:23:21 +0200725 uint32_t prflx_priority =
726 ICE_TYPE_PREFERENCE_PRFLX << 24 |
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700727 (connection_->local_candidate().priority() & 0x00FFFFFF);
728 request->AddAttribute(
729 new StunUInt32Attribute(STUN_ATTR_PRIORITY, prflx_priority));
730
731 // Adding Message Integrity attribute.
732 request->AddMessageIntegrity(connection_->remote_candidate().password());
733 // Adding Fingerprint.
734 request->AddFingerprint();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000735 }
736
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700737 void OnResponse(StunMessage* response) override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000738 connection_->OnConnectionRequestResponse(this, response);
739 }
740
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700741 void OnErrorResponse(StunMessage* response) override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000742 connection_->OnConnectionRequestErrorResponse(this, response);
743 }
744
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700745 void OnTimeout() override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000746 connection_->OnConnectionRequestTimeout(this);
747 }
748
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700749 void OnSent() override {
750 connection_->OnConnectionRequestSent(this);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000751 // Each request is sent only once. After a single delay , the request will
752 // time out.
753 timeout_ = true;
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700754 }
755
756 int resend_delay() override {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000757 return CONNECTION_RESPONSE_TIMEOUT;
758 }
759
760 private:
761 Connection* connection_;
762};
763
764//
765// Connection
766//
767
guoweis@webrtc.org930e0042014-11-17 19:42:14 +0000768Connection::Connection(Port* port,
769 size_t index,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000770 const Candidate& remote_candidate)
guoweis@webrtc.org930e0042014-11-17 19:42:14 +0000771 : port_(port),
772 local_candidate_index_(index),
773 remote_candidate_(remote_candidate),
guoweis@webrtc.org930e0042014-11-17 19:42:14 +0000774 write_state_(STATE_WRITE_INIT),
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700775 receiving_(false),
guoweis@webrtc.org930e0042014-11-17 19:42:14 +0000776 connected_(true),
777 pruned_(false),
778 use_candidate_attr_(false),
honghaiz5a3acd82015-08-20 15:53:17 -0700779 nominated_(false),
guoweis@webrtc.org930e0042014-11-17 19:42:14 +0000780 remote_ice_mode_(ICEMODE_FULL),
781 requests_(port->thread()),
782 rtt_(DEFAULT_RTT),
783 last_ping_sent_(0),
784 last_ping_received_(0),
785 last_data_received_(0),
786 last_ping_response_received_(0),
Tim Psiaki63046262015-09-14 10:38:08 -0700787 recv_rate_tracker_(100u, 10u),
788 send_rate_tracker_(100u, 10u),
guoweis@webrtc.org930e0042014-11-17 19:42:14 +0000789 sent_packets_discarded_(0),
790 sent_packets_total_(0),
791 reported_(false),
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700792 state_(STATE_WAITING),
Honghai Zhang2b342bf2015-09-30 09:51:58 -0700793 receiving_timeout_(WEAK_CONNECTION_RECEIVE_TIMEOUT),
794 time_created_ms_(rtc::Time()) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000795 // All of our connections start in WAITING state.
796 // TODO(mallinath) - Start connections from STATE_FROZEN.
797 // Wire up to send stun packets
798 requests_.SignalSendPacket.connect(this, &Connection::OnSendStunPacket);
799 LOG_J(LS_INFO, this) << "Connection created";
800}
801
802Connection::~Connection() {
803}
804
805const Candidate& Connection::local_candidate() const {
806 ASSERT(local_candidate_index_ < port_->Candidates().size());
807 return port_->Candidates()[local_candidate_index_];
808}
809
Peter Boström0c4e06b2015-10-07 12:23:21 +0200810uint64_t Connection::priority() const {
811 uint64_t priority = 0;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000812 // RFC 5245 - 5.7.2. Computing Pair Priority and Ordering Pairs
813 // Let G be the priority for the candidate provided by the controlling
814 // agent. Let D be the priority for the candidate provided by the
815 // controlled agent.
816 // pair priority = 2^32*MIN(G,D) + 2*MAX(G,D) + (G>D?1:0)
817 IceRole role = port_->GetIceRole();
818 if (role != ICEROLE_UNKNOWN) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200819 uint32_t g = 0;
820 uint32_t d = 0;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000821 if (role == ICEROLE_CONTROLLING) {
822 g = local_candidate().priority();
823 d = remote_candidate_.priority();
824 } else {
825 g = remote_candidate_.priority();
826 d = local_candidate().priority();
827 }
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000828 priority = std::min(g, d);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000829 priority = priority << 32;
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000830 priority += 2 * std::max(g, d) + (g > d ? 1 : 0);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000831 }
832 return priority;
833}
834
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000835void Connection::set_write_state(WriteState value) {
836 WriteState old_value = write_state_;
837 write_state_ = value;
838 if (value != old_value) {
guoweis@webrtc.org8c9ff202014-12-04 07:56:02 +0000839 LOG_J(LS_VERBOSE, this) << "set_write_state from: " << old_value << " to "
840 << value;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000841 SignalStateChange(this);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000842 }
843}
844
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700845void Connection::set_receiving(bool value) {
846 if (value != receiving_) {
847 LOG_J(LS_VERBOSE, this) << "set_receiving to " << value;
848 receiving_ = value;
849 SignalStateChange(this);
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700850 }
851}
852
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000853void Connection::set_state(State state) {
854 State old_state = state_;
855 state_ = state;
856 if (state != old_state) {
857 LOG_J(LS_VERBOSE, this) << "set_state";
858 }
859}
860
861void Connection::set_connected(bool value) {
862 bool old_value = connected_;
863 connected_ = value;
864 if (value != old_value) {
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700865 LOG_J(LS_VERBOSE, this) << "set_connected from: " << old_value << " to "
866 << value;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000867 }
868}
869
870void Connection::set_use_candidate_attr(bool enable) {
871 use_candidate_attr_ = enable;
872}
873
874void Connection::OnSendStunPacket(const void* data, size_t size,
875 StunRequest* req) {
876 rtc::PacketOptions options(port_->DefaultDscpValue());
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700877 auto err = port_->SendTo(
878 data, size, remote_candidate_.address(), options, false);
879 if (err < 0) {
880 LOG_J(LS_WARNING, this) << "Failed to send STUN ping "
881 << " err=" << err
882 << " id=" << rtc::hex_encode(req->id());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000883 }
884}
885
886void Connection::OnReadPacket(
887 const char* data, size_t size, const rtc::PacketTime& packet_time) {
888 rtc::scoped_ptr<IceMessage> msg;
889 std::string remote_ufrag;
890 const rtc::SocketAddress& addr(remote_candidate_.address());
891 if (!port_->GetStunMessage(data, size, addr, msg.accept(), &remote_ufrag)) {
892 // The packet did not parse as a valid STUN message
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700893 // This is a data packet, pass it along.
894 set_receiving(true);
895 last_data_received_ = rtc::Time();
896 recv_rate_tracker_.AddSamples(size);
897 SignalReadPacket(this, data, size, packet_time);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000898
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700899 // If timed out sending writability checks, start up again
900 if (!pruned_ && (write_state_ == STATE_WRITE_TIMEOUT)) {
901 LOG(LS_WARNING) << "Received a data packet on a timed-out Connection. "
902 << "Resetting state to STATE_WRITE_INIT.";
903 set_write_state(STATE_WRITE_INIT);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000904 }
905 } else if (!msg) {
906 // The packet was STUN, but failed a check and was handled internally.
907 } else {
908 // The packet is STUN and passed the Port checks.
909 // Perform our own checks to ensure this packet is valid.
honghaizd0b31432015-09-30 12:42:17 -0700910 // If this is a STUN request, then update the receiving bit and respond.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000911 // If this is a STUN response, then update the writable bit.
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700912 // Log at LS_INFO if we receive a ping on an unwritable connection.
913 rtc::LoggingSeverity sev = (!writable() ? rtc::LS_INFO : rtc::LS_VERBOSE);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000914 switch (msg->type()) {
915 case STUN_BINDING_REQUEST:
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700916 LOG_JV(sev, this) << "Received STUN ping"
917 << ", id=" << rtc::hex_encode(msg->transaction_id());
918
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000919 if (remote_ufrag == remote_candidate_.username()) {
honghaiz9b5ee9c2015-11-11 13:19:17 -0800920 HandleBindingRequest(msg.get());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000921 } else {
922 // The packet had the right local username, but the remote username
923 // was not the right one for the remote address.
924 LOG_J(LS_ERROR, this)
925 << "Received STUN request with bad remote username "
926 << remote_ufrag;
927 port_->SendBindingErrorResponse(msg.get(), addr,
928 STUN_ERROR_UNAUTHORIZED,
929 STUN_ERROR_REASON_UNAUTHORIZED);
930
931 }
932 break;
933
934 // Response from remote peer. Does it match request sent?
935 // This doesn't just check, it makes callbacks if transaction
936 // id's match.
937 case STUN_BINDING_RESPONSE:
938 case STUN_BINDING_ERROR_RESPONSE:
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700939 if (msg->ValidateMessageIntegrity(
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000940 data, size, remote_candidate().password())) {
941 requests_.CheckResponse(msg.get());
942 }
943 // Otherwise silently discard the response message.
944 break;
945
honghaizd0b31432015-09-30 12:42:17 -0700946 // Remote end point sent an STUN indication instead of regular binding
947 // request. In this case |last_ping_received_| will be updated but no
948 // response will be sent.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000949 case STUN_BINDING_INDICATION:
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700950 ReceivedPing();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000951 break;
952
953 default:
954 ASSERT(false);
955 break;
956 }
957 }
958}
959
honghaiz9b5ee9c2015-11-11 13:19:17 -0800960void Connection::HandleBindingRequest(IceMessage* msg) {
961 // This connection should now be receiving.
962 ReceivedPing();
963
964 const rtc::SocketAddress& remote_addr = remote_candidate_.address();
965 const std::string& remote_ufrag = remote_candidate_.username();
966 // Check for role conflicts.
967 if (!port_->MaybeIceRoleConflict(remote_addr, msg, remote_ufrag)) {
968 // Received conflicting role from the peer.
969 LOG(LS_INFO) << "Received conflicting role from the peer.";
970 return;
971 }
972
973 // This is a validated stun request from remote peer.
974 port_->SendBindingResponse(msg, remote_addr);
975
976 // If it timed out on writing check, start up again
977 if (!pruned_ && write_state_ == STATE_WRITE_TIMEOUT) {
978 set_write_state(STATE_WRITE_INIT);
979 }
980
981 if (port_->GetIceRole() == ICEROLE_CONTROLLED) {
982 const StunByteStringAttribute* use_candidate_attr =
983 msg->GetByteString(STUN_ATTR_USE_CANDIDATE);
984 if (use_candidate_attr) {
985 set_nominated(true);
986 SignalNominated(this);
987 }
988 }
989}
990
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000991void Connection::OnReadyToSend() {
992 if (write_state_ == STATE_WRITABLE) {
993 SignalReadyToSend(this);
994 }
995}
996
997void Connection::Prune() {
Honghai Zhang2b342bf2015-09-30 09:51:58 -0700998 if (!pruned_ || active()) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000999 LOG_J(LS_VERBOSE, this) << "Connection pruned";
1000 pruned_ = true;
1001 requests_.Clear();
1002 set_write_state(STATE_WRITE_TIMEOUT);
1003 }
1004}
1005
1006void Connection::Destroy() {
1007 LOG_J(LS_VERBOSE, this) << "Connection destroyed";
Peter Thatcher04ac81f2015-09-21 11:48:28 -07001008 port_->thread()->Post(this, MSG_DELETE);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001009}
1010
Peter Thatcher1cf6f812015-05-15 10:40:45 -07001011void Connection::PrintPingsSinceLastResponse(std::string* s, size_t max) {
1012 std::ostringstream oss;
1013 oss << std::boolalpha;
1014 if (pings_since_last_response_.size() > max) {
1015 for (size_t i = 0; i < max; i++) {
1016 const SentPing& ping = pings_since_last_response_[i];
1017 oss << rtc::hex_encode(ping.id) << " ";
1018 }
1019 oss << "... " << (pings_since_last_response_.size() - max) << " more";
1020 } else {
1021 for (const SentPing& ping : pings_since_last_response_) {
1022 oss << rtc::hex_encode(ping.id) << " ";
1023 }
1024 }
1025 *s = oss.str();
1026}
1027
Peter Boström0c4e06b2015-10-07 12:23:21 +02001028void Connection::UpdateState(uint32_t now) {
1029 uint32_t rtt = ConservativeRTTEstimate(rtt_);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001030
Peter Thatcherb2d26232015-05-15 11:25:14 -07001031 if (LOG_CHECK_LEVEL(LS_VERBOSE)) {
Peter Thatcher1cf6f812015-05-15 10:40:45 -07001032 std::string pings;
1033 PrintPingsSinceLastResponse(&pings, 5);
1034 LOG_J(LS_VERBOSE, this) << "UpdateState()"
1035 << ", ms since last received response="
1036 << now - last_ping_response_received_
1037 << ", ms since last received data="
1038 << now - last_data_received_
1039 << ", rtt=" << rtt
1040 << ", pings_since_last_response=" << pings;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001041 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001042
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001043 // Check the writable state. (The order of these checks is important.)
1044 //
1045 // Before becoming unwritable, we allow for a fixed number of pings to fail
1046 // (i.e., receive no response). We also have to give the response time to
1047 // get back, so we include a conservative estimate of this.
1048 //
1049 // Before timing out writability, we give a fixed amount of time. This is to
1050 // allow for changes in network conditions.
1051
1052 if ((write_state_ == STATE_WRITABLE) &&
1053 TooManyFailures(pings_since_last_response_,
1054 CONNECTION_WRITE_CONNECT_FAILURES,
1055 rtt,
1056 now) &&
1057 TooLongWithoutResponse(pings_since_last_response_,
1058 CONNECTION_WRITE_CONNECT_TIMEOUT,
1059 now)) {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001060 uint32_t max_pings = CONNECTION_WRITE_CONNECT_FAILURES;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001061 LOG_J(LS_INFO, this) << "Unwritable after " << max_pings
1062 << " ping failures and "
Peter Thatcher1cf6f812015-05-15 10:40:45 -07001063 << now - pings_since_last_response_[0].sent_time
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001064 << " ms without a response,"
1065 << " ms since last received ping="
1066 << now - last_ping_received_
1067 << " ms since last received data="
1068 << now - last_data_received_
1069 << " rtt=" << rtt;
1070 set_write_state(STATE_WRITE_UNRELIABLE);
1071 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001072 if ((write_state_ == STATE_WRITE_UNRELIABLE ||
1073 write_state_ == STATE_WRITE_INIT) &&
1074 TooLongWithoutResponse(pings_since_last_response_,
1075 CONNECTION_WRITE_TIMEOUT,
1076 now)) {
1077 LOG_J(LS_INFO, this) << "Timed out after "
Peter Thatcher1cf6f812015-05-15 10:40:45 -07001078 << now - pings_since_last_response_[0].sent_time
1079 << " ms without a response"
1080 << ", rtt=" << rtt;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001081 set_write_state(STATE_WRITE_TIMEOUT);
1082 }
Peter Thatcher04ac81f2015-09-21 11:48:28 -07001083
1084 // Check the receiving state.
Peter Boström0c4e06b2015-10-07 12:23:21 +02001085 uint32_t last_recv_time = last_received();
Peter Thatcher04ac81f2015-09-21 11:48:28 -07001086 bool receiving = now <= last_recv_time + receiving_timeout_;
1087 set_receiving(receiving);
Honghai Zhang2b342bf2015-09-30 09:51:58 -07001088 if (dead(now)) {
1089 Destroy();
1090 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001091}
1092
Peter Boström0c4e06b2015-10-07 12:23:21 +02001093void Connection::Ping(uint32_t now) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001094 last_ping_sent_ = now;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001095 ConnectionRequest *req = new ConnectionRequest(this);
Peter Thatcher1cf6f812015-05-15 10:40:45 -07001096 pings_since_last_response_.push_back(SentPing(req->id(), now));
1097 LOG_J(LS_VERBOSE, this) << "Sending STUN ping "
1098 << ", id=" << rtc::hex_encode(req->id());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001099 requests_.Send(req);
1100 state_ = STATE_INPROGRESS;
1101}
1102
1103void Connection::ReceivedPing() {
Peter Thatcher04ac81f2015-09-21 11:48:28 -07001104 set_receiving(true);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001105 last_ping_received_ = rtc::Time();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001106}
1107
Peter Thatcher1fe120a2015-06-10 11:33:17 -07001108void Connection::ReceivedPingResponse() {
1109 // We've already validated that this is a STUN binding response with
1110 // the correct local and remote username for this connection.
1111 // So if we're not already, become writable. We may be bringing a pruned
1112 // connection back to life, but if we don't really want it, we can always
1113 // prune it again.
Peter Thatcher04ac81f2015-09-21 11:48:28 -07001114 set_receiving(true);
Peter Thatcher1fe120a2015-06-10 11:33:17 -07001115 set_write_state(STATE_WRITABLE);
1116 set_state(STATE_SUCCEEDED);
1117 pings_since_last_response_.clear();
1118 last_ping_response_received_ = rtc::Time();
1119}
1120
Peter Boström0c4e06b2015-10-07 12:23:21 +02001121bool Connection::dead(uint32_t now) const {
Honghai Zhang2b342bf2015-09-30 09:51:58 -07001122 if (now < (time_created_ms_ + MIN_CONNECTION_LIFETIME)) {
1123 // A connection that hasn't passed its minimum lifetime is still alive.
1124 // We do this to prevent connections from being pruned too quickly
1125 // during a network change event when two networks would be up
1126 // simultaneously but only for a brief period.
1127 return false;
1128 }
1129
1130 if (receiving_) {
1131 // A connection that is receiving is alive.
1132 return false;
1133 }
1134
1135 // A connection is alive until it is inactive.
1136 return !active();
1137
1138 // TODO(honghaiz): Move from using the write state to using the receiving
1139 // state with something like the following:
1140 // return (now > (last_received() + DEAD_CONNECTION_RECEIVE_TIMEOUT));
1141}
1142
guoweis@webrtc.org8c9ff202014-12-04 07:56:02 +00001143std::string Connection::ToDebugId() const {
1144 std::stringstream ss;
1145 ss << std::hex << this;
1146 return ss.str();
1147}
1148
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001149std::string Connection::ToString() const {
1150 const char CONNECT_STATE_ABBREV[2] = {
1151 '-', // not connected (false)
1152 'C', // connected (true)
1153 };
Peter Thatcher04ac81f2015-09-21 11:48:28 -07001154 const char RECEIVE_STATE_ABBREV[2] = {
1155 '-', // not receiving (false)
1156 'R', // receiving (true)
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001157 };
1158 const char WRITE_STATE_ABBREV[4] = {
1159 'W', // STATE_WRITABLE
1160 'w', // STATE_WRITE_UNRELIABLE
1161 '-', // STATE_WRITE_INIT
1162 'x', // STATE_WRITE_TIMEOUT
1163 };
1164 const std::string ICESTATE[4] = {
1165 "W", // STATE_WAITING
1166 "I", // STATE_INPROGRESS
1167 "S", // STATE_SUCCEEDED
1168 "F" // STATE_FAILED
1169 };
1170 const Candidate& local = local_candidate();
1171 const Candidate& remote = remote_candidate();
1172 std::stringstream ss;
guoweis@webrtc.org8c9ff202014-12-04 07:56:02 +00001173 ss << "Conn[" << ToDebugId()
1174 << ":" << port_->content_name()
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001175 << ":" << local.id() << ":" << local.component()
1176 << ":" << local.generation()
1177 << ":" << local.type() << ":" << local.protocol()
1178 << ":" << local.address().ToSensitiveString()
1179 << "->" << remote.id() << ":" << remote.component()
1180 << ":" << remote.priority()
1181 << ":" << remote.type() << ":"
1182 << remote.protocol() << ":" << remote.address().ToSensitiveString() << "|"
1183 << CONNECT_STATE_ABBREV[connected()]
Peter Thatcher04ac81f2015-09-21 11:48:28 -07001184 << RECEIVE_STATE_ABBREV[receiving()]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001185 << WRITE_STATE_ABBREV[write_state()]
1186 << ICESTATE[state()] << "|"
1187 << priority() << "|";
1188 if (rtt_ < DEFAULT_RTT) {
1189 ss << rtt_ << "]";
1190 } else {
1191 ss << "-]";
1192 }
1193 return ss.str();
1194}
1195
1196std::string Connection::ToSensitiveString() const {
1197 return ToString();
1198}
1199
1200void Connection::OnConnectionRequestResponse(ConnectionRequest* request,
1201 StunMessage* response) {
Peter Thatcher1cf6f812015-05-15 10:40:45 -07001202 // Log at LS_INFO if we receive a ping response on an unwritable
1203 // connection.
1204 rtc::LoggingSeverity sev = !writable() ? rtc::LS_INFO : rtc::LS_VERBOSE;
1205
Peter Boström0c4e06b2015-10-07 12:23:21 +02001206 uint32_t rtt = request->Elapsed();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001207
Peter Thatcher1fe120a2015-06-10 11:33:17 -07001208 ReceivedPingResponse();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001209
Peter Thatcherb2d26232015-05-15 11:25:14 -07001210 if (LOG_CHECK_LEVEL_V(sev)) {
Peter Thatcher42af6ca2015-05-15 12:23:27 -07001211 bool use_candidate = (
1212 response->GetByteString(STUN_ATTR_USE_CANDIDATE) != nullptr);
Peter Thatcher1cf6f812015-05-15 10:40:45 -07001213 std::string pings;
1214 PrintPingsSinceLastResponse(&pings, 5);
1215 LOG_JV(sev, this) << "Received STUN ping response"
Peter Thatcher42af6ca2015-05-15 12:23:27 -07001216 << ", id=" << rtc::hex_encode(request->id())
1217 << ", code=0" // Makes logging easier to parse.
1218 << ", rtt=" << rtt
1219 << ", use_candidate=" << use_candidate
1220 << ", pings_since_last_response=" << pings;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001221 }
1222
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001223 rtt_ = (RTT_RATIO * rtt_ + rtt) / (RTT_RATIO + 1);
1224
Peter Thatcher7cbd1882015-09-17 18:54:52 -07001225 MaybeAddPrflxCandidate(request, response);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001226}
1227
1228void Connection::OnConnectionRequestErrorResponse(ConnectionRequest* request,
1229 StunMessage* response) {
1230 const StunErrorCodeAttribute* error_attr = response->GetErrorCode();
1231 int error_code = STUN_ERROR_GLOBAL_FAILURE;
1232 if (error_attr) {
Peter Thatcher7cbd1882015-09-17 18:54:52 -07001233 error_code = error_attr->code();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001234 }
1235
Peter Thatcher1cf6f812015-05-15 10:40:45 -07001236 LOG_J(LS_INFO, this) << "Received STUN error response"
1237 << " id=" << rtc::hex_encode(request->id())
1238 << " code=" << error_code
1239 << " rtt=" << request->Elapsed();
1240
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001241 if (error_code == STUN_ERROR_UNKNOWN_ATTRIBUTE ||
1242 error_code == STUN_ERROR_SERVER_ERROR ||
1243 error_code == STUN_ERROR_UNAUTHORIZED) {
1244 // Recoverable error, retry
1245 } else if (error_code == STUN_ERROR_STALE_CREDENTIALS) {
1246 // Race failure, retry
1247 } else if (error_code == STUN_ERROR_ROLE_CONFLICT) {
1248 HandleRoleConflictFromPeer();
1249 } else {
1250 // This is not a valid connection.
1251 LOG_J(LS_ERROR, this) << "Received STUN error response, code="
1252 << error_code << "; killing connection";
1253 set_state(STATE_FAILED);
Honghai Zhang2b342bf2015-09-30 09:51:58 -07001254 Destroy();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001255 }
1256}
1257
1258void Connection::OnConnectionRequestTimeout(ConnectionRequest* request) {
1259 // Log at LS_INFO if we miss a ping on a writable connection.
Peter Thatcher1cf6f812015-05-15 10:40:45 -07001260 rtc::LoggingSeverity sev = writable() ? rtc::LS_INFO : rtc::LS_VERBOSE;
1261 LOG_JV(sev, this) << "Timing-out STUN ping "
1262 << rtc::hex_encode(request->id())
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001263 << " after " << request->Elapsed() << " ms";
1264}
1265
Peter Thatcher1cf6f812015-05-15 10:40:45 -07001266void Connection::OnConnectionRequestSent(ConnectionRequest* request) {
1267 // Log at LS_INFO if we send a ping on an unwritable connection.
1268 rtc::LoggingSeverity sev = !writable() ? rtc::LS_INFO : rtc::LS_VERBOSE;
Peter Thatcher42af6ca2015-05-15 12:23:27 -07001269 bool use_candidate = use_candidate_attr();
Peter Thatcher1cf6f812015-05-15 10:40:45 -07001270 LOG_JV(sev, this) << "Sent STUN ping"
Peter Thatcher42af6ca2015-05-15 12:23:27 -07001271 << ", id=" << rtc::hex_encode(request->id())
1272 << ", use_candidate=" << use_candidate;
Peter Thatcher1cf6f812015-05-15 10:40:45 -07001273}
1274
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001275void Connection::HandleRoleConflictFromPeer() {
1276 port_->SignalRoleConflict(port_);
1277}
1278
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +00001279void Connection::MaybeSetRemoteIceCredentials(const std::string& ice_ufrag,
1280 const std::string& ice_pwd) {
1281 if (remote_candidate_.username() == ice_ufrag &&
1282 remote_candidate_.password().empty()) {
1283 remote_candidate_.set_password(ice_pwd);
1284 }
1285}
1286
1287void Connection::MaybeUpdatePeerReflexiveCandidate(
1288 const Candidate& new_candidate) {
1289 if (remote_candidate_.type() == PRFLX_PORT_TYPE &&
1290 new_candidate.type() != PRFLX_PORT_TYPE &&
1291 remote_candidate_.protocol() == new_candidate.protocol() &&
1292 remote_candidate_.address() == new_candidate.address() &&
1293 remote_candidate_.username() == new_candidate.username() &&
1294 remote_candidate_.password() == new_candidate.password() &&
1295 remote_candidate_.generation() == new_candidate.generation()) {
1296 remote_candidate_ = new_candidate;
1297 }
1298}
1299
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001300void Connection::OnMessage(rtc::Message *pmsg) {
1301 ASSERT(pmsg->message_id == MSG_DELETE);
honghaizd0b31432015-09-30 12:42:17 -07001302 LOG_J(LS_INFO, this) << "Connection deleted";
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001303 SignalDestroyed(this);
1304 delete this;
1305}
1306
Peter Boström0c4e06b2015-10-07 12:23:21 +02001307uint32_t Connection::last_received() {
Peter Thatcher54360512015-07-08 11:08:35 -07001308 return std::max(last_data_received_,
1309 std::max(last_ping_received_, last_ping_response_received_));
1310}
1311
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001312size_t Connection::recv_bytes_second() {
Tim Psiakiad13d2f2015-11-10 16:34:50 -08001313 return round(recv_rate_tracker_.ComputeRate());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001314}
1315
1316size_t Connection::recv_total_bytes() {
Tim Psiaki63046262015-09-14 10:38:08 -07001317 return recv_rate_tracker_.TotalSampleCount();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001318}
1319
1320size_t Connection::sent_bytes_second() {
Tim Psiakiad13d2f2015-11-10 16:34:50 -08001321 return round(send_rate_tracker_.ComputeRate());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001322}
1323
1324size_t Connection::sent_total_bytes() {
Tim Psiaki63046262015-09-14 10:38:08 -07001325 return send_rate_tracker_.TotalSampleCount();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001326}
1327
guoweis@webrtc.org930e0042014-11-17 19:42:14 +00001328size_t Connection::sent_discarded_packets() {
1329 return sent_packets_discarded_;
1330}
1331
1332size_t Connection::sent_total_packets() {
1333 return sent_packets_total_;
1334}
1335
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001336void Connection::MaybeAddPrflxCandidate(ConnectionRequest* request,
1337 StunMessage* response) {
1338 // RFC 5245
1339 // The agent checks the mapped address from the STUN response. If the
1340 // transport address does not match any of the local candidates that the
1341 // agent knows about, the mapped address represents a new candidate -- a
1342 // peer reflexive candidate.
1343 const StunAddressAttribute* addr =
1344 response->GetAddress(STUN_ATTR_XOR_MAPPED_ADDRESS);
1345 if (!addr) {
1346 LOG(LS_WARNING) << "Connection::OnConnectionRequestResponse - "
1347 << "No MAPPED-ADDRESS or XOR-MAPPED-ADDRESS found in the "
1348 << "stun response message";
1349 return;
1350 }
1351
1352 bool known_addr = false;
1353 for (size_t i = 0; i < port_->Candidates().size(); ++i) {
1354 if (port_->Candidates()[i].address() == addr->GetAddress()) {
1355 known_addr = true;
1356 break;
1357 }
1358 }
1359 if (known_addr) {
1360 return;
1361 }
1362
1363 // RFC 5245
1364 // Its priority is set equal to the value of the PRIORITY attribute
1365 // in the Binding request.
1366 const StunUInt32Attribute* priority_attr =
1367 request->msg()->GetUInt32(STUN_ATTR_PRIORITY);
1368 if (!priority_attr) {
1369 LOG(LS_WARNING) << "Connection::OnConnectionRequestResponse - "
1370 << "No STUN_ATTR_PRIORITY found in the "
1371 << "stun response message";
1372 return;
1373 }
Peter Boström0c4e06b2015-10-07 12:23:21 +02001374 const uint32_t priority = priority_attr->value();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001375 std::string id = rtc::CreateRandomString(8);
1376
1377 Candidate new_local_candidate;
1378 new_local_candidate.set_id(id);
1379 new_local_candidate.set_component(local_candidate().component());
1380 new_local_candidate.set_type(PRFLX_PORT_TYPE);
1381 new_local_candidate.set_protocol(local_candidate().protocol());
1382 new_local_candidate.set_address(addr->GetAddress());
1383 new_local_candidate.set_priority(priority);
1384 new_local_candidate.set_username(local_candidate().username());
1385 new_local_candidate.set_password(local_candidate().password());
1386 new_local_candidate.set_network_name(local_candidate().network_name());
guoweis@webrtc.org950c5182014-12-16 23:01:31 +00001387 new_local_candidate.set_network_type(local_candidate().network_type());
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001388 new_local_candidate.set_related_address(local_candidate().address());
1389 new_local_candidate.set_foundation(
1390 ComputeFoundation(PRFLX_PORT_TYPE, local_candidate().protocol(),
1391 local_candidate().address()));
1392
1393 // Change the local candidate of this Connection to the new prflx candidate.
1394 local_candidate_index_ = port_->AddPrflxCandidate(new_local_candidate);
1395
1396 // SignalStateChange to force a re-sort in P2PTransportChannel as this
1397 // Connection's local candidate has changed.
1398 SignalStateChange(this);
1399}
1400
1401ProxyConnection::ProxyConnection(Port* port, size_t index,
1402 const Candidate& candidate)
1403 : Connection(port, index, candidate), error_(0) {
1404}
1405
1406int ProxyConnection::Send(const void* data, size_t size,
1407 const rtc::PacketOptions& options) {
1408 if (write_state_ == STATE_WRITE_INIT || write_state_ == STATE_WRITE_TIMEOUT) {
1409 error_ = EWOULDBLOCK;
1410 return SOCKET_ERROR;
1411 }
guoweis@webrtc.org930e0042014-11-17 19:42:14 +00001412 sent_packets_total_++;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001413 int sent = port_->SendTo(data, size, remote_candidate_.address(),
1414 options, true);
1415 if (sent <= 0) {
1416 ASSERT(sent < 0);
1417 error_ = port_->GetError();
guoweis@webrtc.org930e0042014-11-17 19:42:14 +00001418 sent_packets_discarded_++;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001419 } else {
Tim Psiaki63046262015-09-14 10:38:08 -07001420 send_rate_tracker_.AddSamples(sent);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001421 }
1422 return sent;
1423}
1424
1425} // namespace cricket