blob: 09cbdef5087972279b9c47c73a606f8f0d37f76b [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef P2P_BASE_PORT_H_
12#define P2P_BASE_PORT_H_
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000013
14#include <map>
kwiberg3ec46792016-04-27 07:22:53 -070015#include <memory>
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000016#include <set>
17#include <string>
18#include <vector>
19
Danil Chapovalov00c71832018-06-15 15:58:38 +020020#include "absl/types/optional.h"
Patrik Höglunde2d6a062017-10-05 14:53:33 +020021#include "api/candidate.h"
Qingsi Wangdea68892018-03-27 10:55:21 -070022#include "api/rtcerror.h"
Qingsi Wang93a84392018-01-30 17:13:09 -080023#include "logging/rtc_event_log/events/rtc_event_ice_candidate_pair.h"
24#include "logging/rtc_event_log/events/rtc_event_ice_candidate_pair_config.h"
25#include "logging/rtc_event_log/icelogger.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "p2p/base/candidatepairinterface.h"
Qingsi Wang866e08d2018-03-22 17:54:23 -070027#include "p2p/base/p2pconstants.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "p2p/base/packetlossestimator.h"
29#include "p2p/base/packetsocketfactory.h"
30#include "p2p/base/portinterface.h"
31#include "p2p/base/stun.h"
32#include "p2p/base/stunrequest.h"
33#include "rtc_base/asyncpacketsocket.h"
34#include "rtc_base/checks.h"
Zhi Huang942bc2e2017-11-13 13:26:07 -080035#include "rtc_base/nethelper.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#include "rtc_base/network.h"
37#include "rtc_base/proxyinfo.h"
38#include "rtc_base/ratetracker.h"
39#include "rtc_base/sigslot.h"
40#include "rtc_base/socketaddress.h"
41#include "rtc_base/thread.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000042
43namespace cricket {
44
45class Connection;
46class ConnectionRequest;
47
48extern const char LOCAL_PORT_TYPE[];
49extern const char STUN_PORT_TYPE[];
50extern const char PRFLX_PORT_TYPE[];
51extern const char RELAY_PORT_TYPE[];
52
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000053// RFC 6544, TCP candidate encoding rules.
54extern const int DISCARD_PORT;
55extern const char TCPTYPE_ACTIVE_STR[];
56extern const char TCPTYPE_PASSIVE_STR[];
57extern const char TCPTYPE_SIMOPEN_STR[];
58
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000059enum RelayType {
Yves Gerey665174f2018-06-19 15:03:05 +020060 RELAY_GTURN, // Legacy google relay service.
61 RELAY_TURN // Standard (TURN) relay service.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000062};
63
64enum IcePriorityValue {
hnsl277b2502016-12-13 05:17:23 -080065 ICE_TYPE_PREFERENCE_RELAY_TLS = 0,
66 ICE_TYPE_PREFERENCE_RELAY_TCP = 1,
67 ICE_TYPE_PREFERENCE_RELAY_UDP = 2,
Taylor Brandstetter62351c92016-08-11 16:05:07 -070068 ICE_TYPE_PREFERENCE_PRFLX_TCP = 80,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000069 ICE_TYPE_PREFERENCE_HOST_TCP = 90,
70 ICE_TYPE_PREFERENCE_SRFLX = 100,
71 ICE_TYPE_PREFERENCE_PRFLX = 110,
72 ICE_TYPE_PREFERENCE_HOST = 126
73};
74
hbos06495bc2017-01-02 08:08:18 -080075// States are from RFC 5245. http://tools.ietf.org/html/rfc5245#section-5.7.4
76enum class IceCandidatePairState {
77 WAITING = 0, // Check has not been performed, Waiting pair on CL.
78 IN_PROGRESS, // Check has been sent, transaction is in progress.
79 SUCCEEDED, // Check already done, produced a successful result.
80 FAILED, // Check for this connection failed.
81 // According to spec there should also be a frozen state, but nothing is ever
82 // frozen because we have not implemented ICE freezing logic.
83};
84
Qingsi Wangd5e0fcd2018-02-26 19:29:05 -080085// Stats that we can return about the port of a STUN candidate.
Qingsi Wang72a43a12018-02-20 16:03:18 -080086class StunStats {
87 public:
88 StunStats() = default;
89 StunStats(const StunStats&) = default;
90 ~StunStats() = default;
91
92 StunStats& operator=(const StunStats& other) = default;
93
94 int stun_binding_requests_sent = 0;
95 int stun_binding_responses_received = 0;
96 double stun_binding_rtt_ms_total = 0;
97 double stun_binding_rtt_ms_squared_total = 0;
98};
99
100// Stats that we can return about a candidate.
101class CandidateStats {
102 public:
103 CandidateStats();
104 explicit CandidateStats(Candidate candidate);
105 CandidateStats(const CandidateStats&);
106 ~CandidateStats();
107
108 Candidate candidate;
109 // STUN port stats if this candidate is a STUN candidate.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200110 absl::optional<StunStats> stun_stats;
Qingsi Wang72a43a12018-02-20 16:03:18 -0800111};
112
113typedef std::vector<CandidateStats> CandidateStatsList;
114
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800115// Stats that we can return about the connections for a transport channel.
116// TODO(hta): Rename to ConnectionStats
117struct ConnectionInfo {
118 ConnectionInfo();
119 ConnectionInfo(const ConnectionInfo&);
120 ~ConnectionInfo();
121
122 bool best_connection; // Is this the best connection we have?
123 bool writable; // Has this connection received a STUN response?
124 bool receiving; // Has this connection received anything?
125 bool timeout; // Has this connection timed out?
126 bool new_connection; // Is this a newly created connection?
127 size_t rtt; // The STUN RTT for this connection.
128 size_t sent_total_bytes; // Total bytes sent on this connection.
129 size_t sent_bytes_second; // Bps over the last measurement interval.
130 size_t sent_discarded_packets; // Number of outgoing packets discarded due to
131 // socket errors.
132 size_t sent_total_packets; // Number of total outgoing packets attempted for
133 // sending.
134 size_t sent_ping_requests_total; // Number of STUN ping request sent.
135 size_t sent_ping_requests_before_first_response; // Number of STUN ping
136 // sent before receiving the first response.
137 size_t sent_ping_responses; // Number of STUN ping response sent.
138
139 size_t recv_total_bytes; // Total bytes received on this connection.
140 size_t recv_bytes_second; // Bps over the last measurement interval.
141 size_t recv_ping_requests; // Number of STUN ping request received.
142 size_t recv_ping_responses; // Number of STUN ping response received.
143 Candidate local_candidate; // The local candidate for this connection.
144 Candidate remote_candidate; // The remote candidate for this connection.
145 void* key; // A static value that identifies this conn.
146 // https://w3c.github.io/webrtc-stats/#dom-rtcicecandidatepairstats-state
147 IceCandidatePairState state;
148 // https://w3c.github.io/webrtc-stats/#dom-rtcicecandidatepairstats-priority
149 uint64_t priority;
150 // https://w3c.github.io/webrtc-stats/#dom-rtcicecandidatepairstats-nominated
151 bool nominated;
152 // https://w3c.github.io/webrtc-stats/#dom-rtcicecandidatepairstats-totalroundtriptime
153 uint64_t total_round_trip_time_ms;
154 // https://w3c.github.io/webrtc-stats/#dom-rtcicecandidatepairstats-currentroundtriptime
Danil Chapovalov00c71832018-06-15 15:58:38 +0200155 absl::optional<uint32_t> current_round_trip_time_ms;
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800156};
157
Qingsi Wang72a43a12018-02-20 16:03:18 -0800158// Information about all the candidate pairs of a channel.
Taylor Brandstetter6e2e7ce2017-12-19 10:26:23 -0800159typedef std::vector<ConnectionInfo> ConnectionInfos;
160
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000161const char* ProtoToString(ProtocolType proto);
162bool StringToProto(const char* value, ProtocolType* proto);
163
164struct ProtocolAddress {
165 rtc::SocketAddress address;
166 ProtocolType proto;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000167
168 ProtocolAddress(const rtc::SocketAddress& a, ProtocolType p)
hnsl277b2502016-12-13 05:17:23 -0800169 : address(a), proto(p) {}
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700170
171 bool operator==(const ProtocolAddress& o) const {
hnsl277b2502016-12-13 05:17:23 -0800172 return address == o.address && proto == o.proto;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700173 }
174 bool operator!=(const ProtocolAddress& o) const { return !(*this == o); }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000175};
176
177typedef std::set<rtc::SocketAddress> ServerAddresses;
178
179// Represents a local communication mechanism that can be used to create
180// connections to similar mechanisms of the other client. Subclasses of this
181// one add support for specific mechanisms like local UDP ports.
Yves Gerey665174f2018-06-19 15:03:05 +0200182class Port : public PortInterface,
183 public rtc::MessageHandler,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000184 public sigslot::has_slots<> {
185 public:
Honghai Zhanga74363c2016-07-28 18:06:15 -0700186 // INIT: The state when a port is just created.
187 // KEEP_ALIVE_UNTIL_PRUNED: A port should not be destroyed even if no
188 // connection is using it.
189 // PRUNED: It will be destroyed if no connection is using it for a period of
190 // 30 seconds.
191 enum class State { INIT, KEEP_ALIVE_UNTIL_PRUNED, PRUNED };
pkasting@chromium.org332331f2014-11-06 20:19:22 +0000192 Port(rtc::Thread* thread,
Honghai Zhangd00c0572016-06-28 09:44:47 -0700193 const std::string& type,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000194 rtc::PacketSocketFactory* factory,
pkasting@chromium.org332331f2014-11-06 20:19:22 +0000195 rtc::Network* network,
pkasting@chromium.org332331f2014-11-06 20:19:22 +0000196 const std::string& username_fragment,
197 const std::string& password);
deadbeef5c3c1042017-08-04 15:01:57 -0700198 Port(rtc::Thread* thread,
199 const std::string& type,
200 rtc::PacketSocketFactory* factory,
201 rtc::Network* network,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200202 uint16_t min_port,
203 uint16_t max_port,
pkasting@chromium.org332331f2014-11-06 20:19:22 +0000204 const std::string& username_fragment,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000205 const std::string& password);
Steve Anton1cf1b7d2017-10-30 10:00:15 -0700206 ~Port() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000207
Qingsi Wang4ff54432018-03-01 18:25:20 -0800208 // Note that the port type does NOT uniquely identify different subclasses of
209 // Port. Use the 2-tuple of the port type AND the protocol (GetProtocol()) to
210 // uniquely identify subclasses. Whenever a new subclass of Port introduces a
211 // conflit in the value of the 2-tuple, make sure that the implementation that
212 // relies on this 2-tuple for RTTI is properly changed.
Steve Anton1cf1b7d2017-10-30 10:00:15 -0700213 const std::string& Type() const override;
214 rtc::Network* Network() const override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000215
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000216 // Methods to set/get ICE role and tiebreaker values.
Steve Anton1cf1b7d2017-10-30 10:00:15 -0700217 IceRole GetIceRole() const override;
218 void SetIceRole(IceRole role) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000219
Steve Anton1cf1b7d2017-10-30 10:00:15 -0700220 void SetIceTiebreaker(uint64_t tiebreaker) override;
221 uint64_t IceTiebreaker() const override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000222
Steve Anton1cf1b7d2017-10-30 10:00:15 -0700223 bool SharedSocket() const override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000224 void ResetSharedSocket() { shared_socket_ = false; }
225
Honghai Zhanga74363c2016-07-28 18:06:15 -0700226 // Should not destroy the port even if no connection is using it. Called when
227 // a port is ready to use.
228 void KeepAliveUntilPruned();
229 // Allows a port to be destroyed if no connection is using it.
230 void Prune();
231
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000232 // The thread on which this port performs its I/O.
233 rtc::Thread* thread() { return thread_; }
234
235 // The factory used to create the sockets of this port.
236 rtc::PacketSocketFactory* socket_factory() const { return factory_; }
237 void set_socket_factory(rtc::PacketSocketFactory* factory) {
238 factory_ = factory;
239 }
240
241 // For debugging purposes.
242 const std::string& content_name() const { return content_name_; }
243 void set_content_name(const std::string& content_name) {
244 content_name_ = content_name;
245 }
246
247 int component() const { return component_; }
248 void set_component(int component) { component_ = component; }
249
250 bool send_retransmit_count_attribute() const {
251 return send_retransmit_count_attribute_;
252 }
253 void set_send_retransmit_count_attribute(bool enable) {
254 send_retransmit_count_attribute_ = enable;
255 }
256
257 // Identifies the generation that this port was created in.
deadbeef14f97f52016-06-22 17:14:15 -0700258 uint32_t generation() const { return generation_; }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200259 void set_generation(uint32_t generation) { generation_ = generation; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000260
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000261 const std::string username_fragment() const;
262 const std::string& password() const { return password_; }
263
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700264 // May be called when this port was initially created by a pooled
265 // PortAllocatorSession, and is now being assigned to an ICE transport.
266 // Updates the information for candidates as well.
267 void SetIceParameters(int component,
268 const std::string& username_fragment,
269 const std::string& password);
270
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000271 // Fired when candidates are discovered by the port. When all candidates
272 // are discovered that belong to port SignalAddressReady is fired.
273 sigslot::signal2<Port*, const Candidate&> SignalCandidateReady;
274
275 // Provides all of the above information in one handy object.
Steve Anton1cf1b7d2017-10-30 10:00:15 -0700276 const std::vector<Candidate>& Candidates() const override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000277
278 // SignalPortComplete is sent when port completes the task of candidates
279 // allocation.
280 sigslot::signal1<Port*> SignalPortComplete;
281 // This signal sent when port fails to allocate candidates and this port
282 // can't be used in establishing the connections. When port is in shared mode
283 // and port fails to allocate one of the candidates, port shouldn't send
284 // this signal as other candidates might be usefull in establishing the
285 // connection.
286 sigslot::signal1<Port*> SignalPortError;
287
288 // Returns a map containing all of the connections of this port, keyed by the
289 // remote address.
290 typedef std::map<rtc::SocketAddress, Connection*> AddressMap;
291 const AddressMap& connections() { return connections_; }
292
293 // Returns the connection to the given address or NULL if none exists.
Steve Anton1cf1b7d2017-10-30 10:00:15 -0700294 Connection* GetConnection(const rtc::SocketAddress& remote_addr) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000295
296 // Called each time a connection is created.
297 sigslot::signal2<Port*, Connection*> SignalConnectionCreated;
298
299 // In a shared socket mode each port which shares the socket will decide
300 // to accept the packet based on the |remote_addr|. Currently only UDP
301 // port implemented this method.
302 // TODO(mallinath) - Make it pure virtual.
Steve Anton1cf1b7d2017-10-30 10:00:15 -0700303 virtual bool HandleIncomingPacket(rtc::AsyncPacketSocket* socket,
304 const char* data,
305 size_t size,
306 const rtc::SocketAddress& remote_addr,
307 const rtc::PacketTime& packet_time);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000308
Jonas Oreland202994c2017-12-18 12:10:43 +0100309 // Shall the port handle packet from this |remote_addr|.
310 // This method is overridden by TurnPort.
311 virtual bool CanHandleIncomingPacketsFrom(
312 const rtc::SocketAddress& remote_addr) const;
313
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000314 // Sends a response message (normal or error) to the given request. One of
315 // these methods should be called as a response to SignalUnknownAddress.
316 // NOTE: You MUST call CreateConnection BEFORE SendBindingResponse.
Steve Anton1cf1b7d2017-10-30 10:00:15 -0700317 void SendBindingResponse(StunMessage* request,
318 const rtc::SocketAddress& addr) override;
319 void SendBindingErrorResponse(StunMessage* request,
320 const rtc::SocketAddress& addr,
321 int error_code,
322 const std::string& reason) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000323
Yves Gerey665174f2018-06-19 15:03:05 +0200324 void set_proxy(const std::string& user_agent, const rtc::ProxyInfo& proxy) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000325 user_agent_ = user_agent;
326 proxy_ = proxy;
327 }
328 const std::string& user_agent() { return user_agent_; }
329 const rtc::ProxyInfo& proxy() { return proxy_; }
330
Steve Anton1cf1b7d2017-10-30 10:00:15 -0700331 void EnablePortPackets() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000332
333 // Called if the port has no connections and is no longer useful.
334 void Destroy();
335
Steve Anton1cf1b7d2017-10-30 10:00:15 -0700336 void OnMessage(rtc::Message* pmsg) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000337
338 // Debugging description of this port
Steve Anton1cf1b7d2017-10-30 10:00:15 -0700339 std::string ToString() const override;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200340 uint16_t min_port() { return min_port_; }
341 uint16_t max_port() { return max_port_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000342
343 // Timeout shortening function to speed up unit tests.
344 void set_timeout_delay(int delay) { timeout_delay_ = delay; }
345
346 // This method will return local and remote username fragements from the
347 // stun username attribute if present.
348 bool ParseStunUsername(const StunMessage* stun_msg,
349 std::string* local_username,
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700350 std::string* remote_username) const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000351 void CreateStunUsername(const std::string& remote_username,
352 std::string* stun_username_attr_str) const;
353
354 bool MaybeIceRoleConflict(const rtc::SocketAddress& addr,
355 IceMessage* stun_msg,
356 const std::string& remote_ufrag);
357
stefanc1aeaf02015-10-15 07:26:07 -0700358 // Called when a packet has been sent to the socket.
Stefan Holmer55674ff2016-01-14 15:49:16 +0100359 // This is made pure virtual to notify subclasses of Port that they MUST
360 // listen to AsyncPacketSocket::SignalSentPacket and then call
361 // PortInterface::OnSentPacket.
362 virtual void OnSentPacket(rtc::AsyncPacketSocket* socket,
363 const rtc::SentPacket& sent_packet) = 0;
stefanc1aeaf02015-10-15 07:26:07 -0700364
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000365 // Called when the socket is currently able to send.
366 void OnReadyToSend();
367
368 // Called when the Connection discovers a local peer reflexive candidate.
369 // Returns the index of the new local candidate.
370 size_t AddPrflxCandidate(const Candidate& local);
371
honghaiza0c44ea2016-03-23 16:07:48 -0700372 int16_t network_cost() const { return network_cost_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000373
Danil Chapovalov00c71832018-06-15 15:58:38 +0200374 void GetStunStats(absl::optional<StunStats>* stats) override{};
Qingsi Wang72a43a12018-02-20 16:03:18 -0800375
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000376 protected:
Honghai Zhanga74363c2016-07-28 18:06:15 -0700377 enum { MSG_DESTROY_IF_DEAD = 0, MSG_FIRST_AVAILABLE };
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000378
Honghai Zhang351d77b2016-05-20 15:08:29 -0700379 virtual void UpdateNetworkCost();
380
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000381 void set_type(const std::string& type) { type_ = type; }
382
Peter Boström2758c662017-02-13 20:33:27 -0500383 // Deprecated. Use the AddAddress() method below with "url" instead.
384 // TODO(zhihuang): Remove this after downstream applications stop using it.
385 void AddAddress(const rtc::SocketAddress& address,
386 const rtc::SocketAddress& base_address,
387 const rtc::SocketAddress& related_address,
388 const std::string& protocol,
389 const std::string& relay_protocol,
390 const std::string& tcptype,
391 const std::string& type,
392 uint32_t type_preference,
393 uint32_t relay_preference,
394 bool final);
395
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000396 void AddAddress(const rtc::SocketAddress& address,
397 const rtc::SocketAddress& base_address,
398 const rtc::SocketAddress& related_address,
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700399 const std::string& protocol,
400 const std::string& relay_protocol,
401 const std::string& tcptype,
402 const std::string& type,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200403 uint32_t type_preference,
404 uint32_t relay_preference,
zhihuang26d99c22017-02-13 12:47:27 -0800405 const std::string& url,
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700406 bool final);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000407
honghaiz36f50e82016-06-01 15:57:03 -0700408 // Adds the given connection to the map keyed by the remote candidate address.
409 // If an existing connection has the same address, the existing one will be
410 // replaced and destroyed.
411 void AddOrReplaceConnection(Connection* conn);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000412
413 // Called when a packet is received from an unknown address that is not
414 // currently a connection. If this is an authenticated STUN binding request,
415 // then we will signal the client.
Yves Gerey665174f2018-06-19 15:03:05 +0200416 void OnReadPacket(const char* data,
417 size_t size,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000418 const rtc::SocketAddress& addr,
419 ProtocolType proto);
420
421 // If the given data comprises a complete and correct STUN message then the
422 // return value is true, otherwise false. If the message username corresponds
423 // with this port's username fragment, msg will contain the parsed STUN
424 // message. Otherwise, the function may send a STUN response internally.
425 // remote_username contains the remote fragment of the STUN username.
kwiberg6baec032016-03-15 11:09:39 -0700426 bool GetStunMessage(const char* data,
427 size_t size,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000428 const rtc::SocketAddress& addr,
kwiberg3ec46792016-04-27 07:22:53 -0700429 std::unique_ptr<IceMessage>* out_msg,
kwiberg6baec032016-03-15 11:09:39 -0700430 std::string* out_username);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000431
432 // Checks if the address in addr is compatible with the port's ip.
433 bool IsCompatibleAddress(const rtc::SocketAddress& addr);
434
435 // Returns default DSCP value.
436 rtc::DiffServCodePoint DefaultDscpValue() const {
437 // No change from what MediaChannel set.
438 return rtc::DSCP_NO_CHANGE;
439 }
440
honghaiz36f50e82016-06-01 15:57:03 -0700441 // Extra work to be done in subclasses when a connection is destroyed.
442 virtual void HandleConnectionDestroyed(Connection* conn) {}
443
Qingsi Wang6e641e62018-04-11 20:14:17 -0700444 void CopyPortInformationToPacketInfo(rtc::PacketInfo* info) const;
445
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000446 private:
447 void Construct();
448 // Called when one of our connections deletes itself.
449 void OnConnectionDestroyed(Connection* conn);
450
Honghai Zhang351d77b2016-05-20 15:08:29 -0700451 void OnNetworkTypeChanged(const rtc::Network* network);
452
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000453 rtc::Thread* thread_;
454 rtc::PacketSocketFactory* factory_;
455 std::string type_;
456 bool send_retransmit_count_attribute_;
457 rtc::Network* network_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200458 uint16_t min_port_;
459 uint16_t max_port_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000460 std::string content_name_;
461 int component_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200462 uint32_t generation_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000463 // In order to establish a connection to this Port (so that real data can be
464 // sent through), the other side must send us a STUN binding request that is
465 // authenticated with this username_fragment and password.
466 // PortAllocatorSession will provide these username_fragment and password.
467 //
468 // Note: we should always use username_fragment() instead of using
469 // |ice_username_fragment_| directly. For the details see the comment on
470 // username_fragment().
471 std::string ice_username_fragment_;
472 std::string password_;
473 std::vector<Candidate> candidates_;
474 AddressMap connections_;
475 int timeout_delay_;
476 bool enable_port_packets_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000477 IceRole ice_role_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200478 uint64_t tiebreaker_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000479 bool shared_socket_;
480 // Information to use when going through a proxy.
481 std::string user_agent_;
482 rtc::ProxyInfo proxy_;
483
honghaize1a0c942016-02-16 14:54:56 -0800484 // A virtual cost perceived by the user, usually based on the network type
485 // (WiFi. vs. Cellular). It takes precedence over the priority when
486 // comparing two connections.
Kári Tristan Helgason47d3a012017-10-24 15:28:51 +0200487 int16_t network_cost_;
Honghai Zhanga74363c2016-07-28 18:06:15 -0700488 State state_ = State::INIT;
Honghai Zhangb5db1ec2016-07-28 13:23:05 -0700489 int64_t last_time_all_connections_removed_ = 0;
honghaize1a0c942016-02-16 14:54:56 -0800490
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000491 friend class Connection;
492};
493
494// Represents a communication link between a port on the local client and a
495// port on the remote client.
Honghai Zhangcc411c02016-03-29 17:27:21 -0700496class Connection : public CandidatePairInterface,
497 public rtc::MessageHandler,
498 public sigslot::has_slots<> {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000499 public:
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700500 struct SentPing {
Honghai Zhang8cd8f812016-08-03 19:50:41 -0700501 SentPing(const std::string id, int64_t sent_time, uint32_t nomination)
502 : id(id), sent_time(sent_time), nomination(nomination) {}
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700503
504 std::string id;
honghaiz34b11eb2016-03-16 08:55:44 -0700505 int64_t sent_time;
Honghai Zhang8cd8f812016-08-03 19:50:41 -0700506 uint32_t nomination;
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700507 };
508
Steve Anton1cf1b7d2017-10-30 10:00:15 -0700509 ~Connection() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000510
511 // The local port where this connection sends and receives packets.
512 Port* port() { return port_; }
513 const Port* port() const { return port_; }
514
Honghai Zhangcc411c02016-03-29 17:27:21 -0700515 // Implementation of virtual methods in CandidatePairInterface.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000516 // Returns the description of the local port
Steve Anton1cf1b7d2017-10-30 10:00:15 -0700517 const Candidate& local_candidate() const override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000518 // Returns the description of the remote port to which we communicate.
Steve Anton1cf1b7d2017-10-30 10:00:15 -0700519 const Candidate& remote_candidate() const override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000520
521 // Returns the pair priority.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200522 uint64_t priority() const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000523
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000524 enum WriteState {
Yves Gerey665174f2018-06-19 15:03:05 +0200525 STATE_WRITABLE = 0, // we have received ping responses recently
526 STATE_WRITE_UNRELIABLE = 1, // we have had a few ping failures
527 STATE_WRITE_INIT = 2, // we have yet to receive a ping response
528 STATE_WRITE_TIMEOUT = 3, // we have had a large number of ping failures
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000529 };
530
531 WriteState write_state() const { return write_state_; }
532 bool writable() const { return write_state_ == STATE_WRITABLE; }
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700533 bool receiving() const { return receiving_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000534
535 // Determines whether the connection has finished connecting. This can only
536 // be false for TCP connections.
537 bool connected() const { return connected_; }
Honghai Zhang2b342bf2015-09-30 09:51:58 -0700538 bool weak() const { return !(writable() && receiving() && connected()); }
Yves Gerey665174f2018-06-19 15:03:05 +0200539 bool active() const { return write_state_ != STATE_WRITE_TIMEOUT; }
honghaiz059e1832016-06-24 11:03:55 -0700540
Honghai Zhang2b342bf2015-09-30 09:51:58 -0700541 // A connection is dead if it can be safely deleted.
honghaiz34b11eb2016-03-16 08:55:44 -0700542 bool dead(int64_t now) const;
honghaiz89374372015-09-24 13:14:47 -0700543
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000544 // Estimate of the round-trip time over this connection.
honghaiz34b11eb2016-03-16 08:55:44 -0700545 int rtt() const { return rtt_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000546
Qingsi Wang22e623a2018-03-13 10:53:57 -0700547 int unwritable_timeout() const;
Danil Chapovalov00c71832018-06-15 15:58:38 +0200548 void set_unwritable_timeout(const absl::optional<int>& value_ms) {
Qingsi Wang22e623a2018-03-13 10:53:57 -0700549 unwritable_timeout_ = value_ms;
550 }
551 int unwritable_min_checks() const;
Danil Chapovalov00c71832018-06-15 15:58:38 +0200552 void set_unwritable_min_checks(const absl::optional<int>& value) {
Qingsi Wang22e623a2018-03-13 10:53:57 -0700553 unwritable_min_checks_ = value;
554 }
555
hbos06495bc2017-01-02 08:08:18 -0800556 // Gets the |ConnectionInfo| stats, where |best_connection| has not been
557 // populated (default value false).
zhihuang5ecf16c2016-06-01 17:09:15 -0700558 ConnectionInfo stats();
559
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000560 sigslot::signal1<Connection*> SignalStateChange;
561
562 // Sent when the connection has decided that it is no longer of value. It
563 // will delete itself immediately after this call.
564 sigslot::signal1<Connection*> SignalDestroyed;
565
566 // The connection can send and receive packets asynchronously. This matches
567 // the interface of AsyncPacketSocket, which may use UDP or TCP under the
568 // covers.
Yves Gerey665174f2018-06-19 15:03:05 +0200569 virtual int Send(const void* data,
570 size_t size,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000571 const rtc::PacketOptions& options) = 0;
572
573 // Error if Send() returns < 0
574 virtual int GetError() = 0;
575
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700576 sigslot::signal4<Connection*, const char*, size_t, const rtc::PacketTime&>
577 SignalReadPacket;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000578
579 sigslot::signal1<Connection*> SignalReadyToSend;
580
581 // Called when a packet is received on this connection.
Yves Gerey665174f2018-06-19 15:03:05 +0200582 void OnReadPacket(const char* data,
583 size_t size,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000584 const rtc::PacketTime& packet_time);
585
586 // Called when the socket is currently able to send.
587 void OnReadyToSend();
588
589 // Called when a connection is determined to be no longer useful to us. We
590 // still keep it around in case the other side wants to use it. But we can
591 // safely stop pinging on it and we can allow it to time out if the other
592 // side stops using it as well.
593 bool pruned() const { return pruned_; }
594 void Prune();
595
596 bool use_candidate_attr() const { return use_candidate_attr_; }
597 void set_use_candidate_attr(bool enable);
598
Honghai Zhang8cd8f812016-08-03 19:50:41 -0700599 void set_nomination(uint32_t value) { nomination_ = value; }
600
601 uint32_t remote_nomination() const { return remote_nomination_; }
hbos92eaec62017-02-27 01:38:08 -0800602 // One or several pairs may be nominated based on if Regular or Aggressive
603 // Nomination is used. https://tools.ietf.org/html/rfc5245#section-8
604 // |nominated| is defined both for the controlling or controlled agent based
605 // on if a nomination has been pinged or acknowledged. The controlled agent
606 // gets its |remote_nomination_| set when pinged by the controlling agent with
607 // a nomination value. The controlling agent gets its |acked_nomination_| set
608 // when receiving a response to a nominating ping.
609 bool nominated() const { return acked_nomination_ || remote_nomination_; }
Honghai Zhang8cd8f812016-08-03 19:50:41 -0700610 // Public for unit tests.
611 void set_remote_nomination(uint32_t remote_nomination) {
612 remote_nomination_ = remote_nomination;
613 }
614 // Public for unit tests.
615 uint32_t acked_nomination() const { return acked_nomination_; }
honghaiz5a3acd82015-08-20 15:53:17 -0700616
Yves Gerey665174f2018-06-19 15:03:05 +0200617 void set_remote_ice_mode(IceMode mode) { remote_ice_mode_ = mode; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000618
Qingsi Wang866e08d2018-03-22 17:54:23 -0700619 int receiving_timeout() const;
Danil Chapovalov00c71832018-06-15 15:58:38 +0200620 void set_receiving_timeout(absl::optional<int> receiving_timeout_ms) {
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700621 receiving_timeout_ = receiving_timeout_ms;
622 }
623
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000624 // Makes the connection go away.
625 void Destroy();
626
deadbeef376e1232015-11-25 09:00:08 -0800627 // Makes the connection go away, in a failed state.
628 void FailAndDestroy();
629
honghaiz079a7a12016-06-22 16:26:29 -0700630 // Prunes the connection and sets its state to STATE_FAILED,
631 // It will not be used or send pings although it can still receive packets.
632 void FailAndPrune();
633
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000634 // Checks that the state of this connection is up-to-date. The argument is
635 // the current time, which is compared against various timeouts.
honghaiz34b11eb2016-03-16 08:55:44 -0700636 void UpdateState(int64_t now);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000637
638 // Called when this connection should try checking writability again.
honghaiz34b11eb2016-03-16 08:55:44 -0700639 int64_t last_ping_sent() const { return last_ping_sent_; }
640 void Ping(int64_t now);
Honghai Zhang8cd8f812016-08-03 19:50:41 -0700641 void ReceivedPingResponse(int rtt, const std::string& request_id);
honghaiz34b11eb2016-03-16 08:55:44 -0700642 int64_t last_ping_response_received() const {
Honghai Zhang381b4212015-12-04 12:24:03 -0800643 return last_ping_response_received_;
644 }
Honghai Zhangfd16da22016-08-17 16:12:46 -0700645 // Used to check if any STUN ping response has been received.
646 int rtt_samples() const { return rtt_samples_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000647
648 // Called whenever a valid ping is received on this connection. This is
649 // public because the connection intercepts the first ping for us.
honghaiz34b11eb2016-03-16 08:55:44 -0700650 int64_t last_ping_received() const { return last_ping_received_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000651 void ReceivedPing();
honghaiz9b5ee9c2015-11-11 13:19:17 -0800652 // Handles the binding request; sends a response if this is a valid request.
653 void HandleBindingRequest(IceMessage* msg);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000654
Honghai Zhang572b0942016-06-23 12:26:57 -0700655 int64_t last_data_received() const { return last_data_received_; }
656
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000657 // Debugging description of this connection
guoweis@webrtc.org8c9ff202014-12-04 07:56:02 +0000658 std::string ToDebugId() const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000659 std::string ToString() const;
660 std::string ToSensitiveString() const;
Qingsi Wang93a84392018-01-30 17:13:09 -0800661 // Structured description of this candidate pair.
662 const webrtc::IceCandidatePairDescription& ToLogDescription();
663 // Integer typed hash value of this candidate pair.
664 uint32_t hash() { return hash_; }
665 void set_ice_event_log(webrtc::IceEventLog* ice_event_log) {
666 ice_event_log_ = ice_event_log;
667 }
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700668 // Prints pings_since_last_response_ into a string.
669 void PrintPingsSinceLastResponse(std::string* pings, size_t max);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000670
671 bool reported() const { return reported_; }
Yves Gerey665174f2018-06-19 15:03:05 +0200672 void set_reported(bool reported) { reported_ = reported; }
Qingsi Wang10a0e512018-05-16 13:37:03 -0700673 // The following two methods are only used for logging in ToString above, and
674 // this flag is set true by P2PTransportChannel for its selected candidate
675 // pair.
676 bool selected() const { return selected_; }
677 void set_selected(bool selected) { selected_ = selected; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000678
honghaiz5a3acd82015-08-20 15:53:17 -0700679 // This signal will be fired if this connection is nominated by the
680 // controlling side.
681 sigslot::signal1<Connection*> SignalNominated;
Peter Thatcher54360512015-07-08 11:08:35 -0700682
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000683 // Invoked when Connection receives STUN error response with 487 code.
684 void HandleRoleConflictFromPeer();
685
hbos06495bc2017-01-02 08:08:18 -0800686 IceCandidatePairState state() const { return state_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000687
honghaiz524ecc22016-05-25 12:48:31 -0700688 int num_pings_sent() const { return num_pings_sent_; }
689
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000690 IceMode remote_ice_mode() const { return remote_ice_mode_; }
691
honghaize1a0c942016-02-16 14:54:56 -0800692 uint32_t ComputeNetworkCost() const;
693
Honghai Zhang4cedf2b2016-08-31 08:18:11 -0700694 // Update the ICE password and/or generation of the remote candidate if the
695 // ufrag in |params| matches the candidate's ufrag, and the
Taylor Brandstetter0a1bc532016-04-19 18:03:26 -0700696 // candidate's password and/or ufrag has not been set.
Honghai Zhang4cedf2b2016-08-31 08:18:11 -0700697 void MaybeSetRemoteIceParametersAndGeneration(const IceParameters& params,
698 int generation);
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000699
700 // If |remote_candidate_| is peer reflexive and is equivalent to
701 // |new_candidate| except the type, update |remote_candidate_| to
702 // |new_candidate|.
703 void MaybeUpdatePeerReflexiveCandidate(const Candidate& new_candidate);
704
Peter Thatcher54360512015-07-08 11:08:35 -0700705 // Returns the last received time of any data, stun request, or stun
706 // response in milliseconds
honghaiz34b11eb2016-03-16 08:55:44 -0700707 int64_t last_received() const;
honghaiz9ad0db52016-07-14 19:30:28 -0700708 // Returns the last time when the connection changed its receiving state.
709 int64_t receiving_unchanged_since() const {
710 return receiving_unchanged_since_;
711 }
Peter Thatcher54360512015-07-08 11:08:35 -0700712
Taylor Brandstetterb825aee2016-06-29 13:07:16 -0700713 bool stable(int64_t now) const;
zhihuang435264a2016-06-21 11:28:38 -0700714
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000715 protected:
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700716 enum { MSG_DELETE = 0, MSG_FIRST_AVAILABLE };
717
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000718 // Constructs a new connection to the given remote port.
719 Connection(Port* port, size_t index, const Candidate& candidate);
720
721 // Called back when StunRequestManager has a stun packet to send
722 void OnSendStunPacket(const void* data, size_t size, StunRequest* req);
723
724 // Callbacks from ConnectionRequest
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700725 virtual void OnConnectionRequestResponse(ConnectionRequest* req,
726 StunMessage* response);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000727 void OnConnectionRequestErrorResponse(ConnectionRequest* req,
728 StunMessage* response);
729 void OnConnectionRequestTimeout(ConnectionRequest* req);
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700730 void OnConnectionRequestSent(ConnectionRequest* req);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000731
Taylor Brandstetterb825aee2016-06-29 13:07:16 -0700732 bool rtt_converged() const;
zhihuang435264a2016-06-21 11:28:38 -0700733
734 // If the response is not received within 2 * RTT, the response is assumed to
735 // be missing.
Taylor Brandstetterb825aee2016-06-29 13:07:16 -0700736 bool missing_responses(int64_t now) const;
zhihuang435264a2016-06-21 11:28:38 -0700737
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000738 // Changes the state and signals if necessary.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000739 void set_write_state(WriteState value);
honghaiz9ad0db52016-07-14 19:30:28 -0700740 void UpdateReceiving(int64_t now);
hbos06495bc2017-01-02 08:08:18 -0800741 void set_state(IceCandidatePairState state);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000742 void set_connected(bool value);
743
Honghai Zhang8cd8f812016-08-03 19:50:41 -0700744 uint32_t nomination() const { return nomination_; }
745
Steve Anton1cf1b7d2017-10-30 10:00:15 -0700746 void OnMessage(rtc::Message* pmsg) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000747
748 Port* port_;
749 size_t local_candidate_index_;
750 Candidate remote_candidate_;
Honghai Zhang8cd8f812016-08-03 19:50:41 -0700751
752 ConnectionInfo stats_;
753 rtc::RateTracker recv_rate_tracker_;
754 rtc::RateTracker send_rate_tracker_;
755
756 private:
Taylor Brandstetter62351c92016-08-11 16:05:07 -0700757 // Update the local candidate based on the mapped address attribute.
758 // If the local candidate changed, fires SignalStateChange.
759 void MaybeUpdateLocalCandidate(ConnectionRequest* request,
760 StunMessage* response);
761
Bjorn Terelius59b4e3e2018-05-30 17:14:08 +0200762 void LogCandidatePairConfig(webrtc::IceCandidatePairConfigType type);
Qingsi Wang93a84392018-01-30 17:13:09 -0800763 void LogCandidatePairEvent(webrtc::IceCandidatePairEventType type);
764
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000765 WriteState write_state_;
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700766 bool receiving_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000767 bool connected_;
768 bool pruned_;
Qingsi Wang10a0e512018-05-16 13:37:03 -0700769 bool selected_ = false;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000770 // By default |use_candidate_attr_| flag will be true,
honghaiz5a3acd82015-08-20 15:53:17 -0700771 // as we will be using aggressive nomination.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000772 // But when peer is ice-lite, this flag "must" be initialized to false and
773 // turn on when connection becomes "best connection".
774 bool use_candidate_attr_;
Honghai Zhang8cd8f812016-08-03 19:50:41 -0700775 // Used by the controlling side to indicate that this connection will be
776 // selected for transmission if the peer supports ICE-renomination when this
777 // value is positive. A larger-value indicates that a connection is nominated
778 // later and should be selected by the controlled side with higher precedence.
779 // A zero-value indicates not nominating this connection.
780 uint32_t nomination_ = 0;
781 // The last nomination that has been acknowledged.
782 uint32_t acked_nomination_ = 0;
783 // Used by the controlled side to remember the nomination value received from
784 // the controlling side. When the peer does not support ICE re-nomination,
785 // its value will be 1 if the connection has been nominated.
786 uint32_t remote_nomination_ = 0;
787
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000788 IceMode remote_ice_mode_;
789 StunRequestManager requests_;
honghaiz34b11eb2016-03-16 08:55:44 -0700790 int rtt_;
zhihuang435264a2016-06-21 11:28:38 -0700791 int rtt_samples_ = 0;
hbosbf8d3e52017-02-28 06:34:47 -0800792 // https://w3c.github.io/webrtc-stats/#dom-rtcicecandidatepairstats-totalroundtriptime
793 uint64_t total_round_trip_time_ms_ = 0;
794 // https://w3c.github.io/webrtc-stats/#dom-rtcicecandidatepairstats-currentroundtriptime
Danil Chapovalov00c71832018-06-15 15:58:38 +0200795 absl::optional<uint32_t> current_round_trip_time_ms_;
honghaiz34b11eb2016-03-16 08:55:44 -0700796 int64_t last_ping_sent_; // last time we sent a ping to the other side
797 int64_t last_ping_received_; // last time we received a ping from the other
798 // side
799 int64_t last_data_received_;
800 int64_t last_ping_response_received_;
honghaiz9ad0db52016-07-14 19:30:28 -0700801 int64_t receiving_unchanged_since_ = 0;
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700802 std::vector<SentPing> pings_since_last_response_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000803
zsteinabbacbf2017-03-20 10:53:12 -0700804 PacketLossEstimator packet_loss_estimator_;
805
Danil Chapovalov00c71832018-06-15 15:58:38 +0200806 absl::optional<int> unwritable_timeout_;
807 absl::optional<int> unwritable_min_checks_;
Qingsi Wang22e623a2018-03-13 10:53:57 -0700808
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000809 bool reported_;
hbos06495bc2017-01-02 08:08:18 -0800810 IceCandidatePairState state_;
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700811 // Time duration to switch from receiving to not receiving.
Danil Chapovalov00c71832018-06-15 15:58:38 +0200812 absl::optional<int> receiving_timeout_;
honghaiz34b11eb2016-03-16 08:55:44 -0700813 int64_t time_created_ms_;
honghaiz524ecc22016-05-25 12:48:31 -0700814 int num_pings_sent_ = 0;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000815
Danil Chapovalov00c71832018-06-15 15:58:38 +0200816 absl::optional<webrtc::IceCandidatePairDescription> log_description_;
Qingsi Wang93a84392018-01-30 17:13:09 -0800817 uint32_t hash_;
818 webrtc::IceEventLog* ice_event_log_ = nullptr;
819
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000820 friend class Port;
821 friend class ConnectionRequest;
822};
823
deadbeef376e1232015-11-25 09:00:08 -0800824// ProxyConnection defers all the interesting work to the port.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000825class ProxyConnection : public Connection {
826 public:
deadbeef376e1232015-11-25 09:00:08 -0800827 ProxyConnection(Port* port, size_t index, const Candidate& remote_candidate);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000828
deadbeef376e1232015-11-25 09:00:08 -0800829 int Send(const void* data,
830 size_t size,
831 const rtc::PacketOptions& options) override;
Steve Anton1cf1b7d2017-10-30 10:00:15 -0700832 int GetError() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000833
834 private:
deadbeef376e1232015-11-25 09:00:08 -0800835 int error_ = 0;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000836};
837
838} // namespace cricket
839
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200840#endif // P2P_BASE_PORT_H_