henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef P2P_BASE_PORT_H_ |
| 12 | #define P2P_BASE_PORT_H_ |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 13 | |
| 14 | #include <map> |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 15 | #include <memory> |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 16 | #include <set> |
| 17 | #include <string> |
| 18 | #include <vector> |
| 19 | |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame^] | 20 | #include "absl/types/optional.h" |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 21 | #include "api/candidate.h" |
Qingsi Wang | dea6889 | 2018-03-27 10:55:21 -0700 | [diff] [blame] | 22 | #include "api/rtcerror.h" |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 23 | #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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 26 | #include "p2p/base/candidatepairinterface.h" |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 27 | #include "p2p/base/p2pconstants.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 28 | #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 Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 35 | #include "rtc_base/nethelper.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 36 | #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.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 42 | |
| 43 | namespace cricket { |
| 44 | |
| 45 | class Connection; |
| 46 | class ConnectionRequest; |
| 47 | |
| 48 | extern const char LOCAL_PORT_TYPE[]; |
| 49 | extern const char STUN_PORT_TYPE[]; |
| 50 | extern const char PRFLX_PORT_TYPE[]; |
| 51 | extern const char RELAY_PORT_TYPE[]; |
| 52 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 53 | // RFC 6544, TCP candidate encoding rules. |
| 54 | extern const int DISCARD_PORT; |
| 55 | extern const char TCPTYPE_ACTIVE_STR[]; |
| 56 | extern const char TCPTYPE_PASSIVE_STR[]; |
| 57 | extern const char TCPTYPE_SIMOPEN_STR[]; |
| 58 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 59 | enum RelayType { |
| 60 | RELAY_GTURN, // Legacy google relay service. |
| 61 | RELAY_TURN // Standard (TURN) relay service. |
| 62 | }; |
| 63 | |
| 64 | enum IcePriorityValue { |
hnsl | 277b250 | 2016-12-13 05:17:23 -0800 | [diff] [blame] | 65 | ICE_TYPE_PREFERENCE_RELAY_TLS = 0, |
| 66 | ICE_TYPE_PREFERENCE_RELAY_TCP = 1, |
| 67 | ICE_TYPE_PREFERENCE_RELAY_UDP = 2, |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 68 | ICE_TYPE_PREFERENCE_PRFLX_TCP = 80, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 69 | 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 | |
hbos | 06495bc | 2017-01-02 08:08:18 -0800 | [diff] [blame] | 75 | // States are from RFC 5245. http://tools.ietf.org/html/rfc5245#section-5.7.4 |
| 76 | enum 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 Wang | d5e0fcd | 2018-02-26 19:29:05 -0800 | [diff] [blame] | 85 | // Stats that we can return about the port of a STUN candidate. |
Qingsi Wang | 72a43a1 | 2018-02-20 16:03:18 -0800 | [diff] [blame] | 86 | class 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. |
| 101 | class 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 Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame^] | 110 | absl::optional<StunStats> stun_stats; |
Qingsi Wang | 72a43a1 | 2018-02-20 16:03:18 -0800 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | typedef std::vector<CandidateStats> CandidateStatsList; |
| 114 | |
Taylor Brandstetter | 6e2e7ce | 2017-12-19 10:26:23 -0800 | [diff] [blame] | 115 | // Stats that we can return about the connections for a transport channel. |
| 116 | // TODO(hta): Rename to ConnectionStats |
| 117 | struct 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 Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame^] | 155 | absl::optional<uint32_t> current_round_trip_time_ms; |
Taylor Brandstetter | 6e2e7ce | 2017-12-19 10:26:23 -0800 | [diff] [blame] | 156 | }; |
| 157 | |
Qingsi Wang | 72a43a1 | 2018-02-20 16:03:18 -0800 | [diff] [blame] | 158 | // Information about all the candidate pairs of a channel. |
Taylor Brandstetter | 6e2e7ce | 2017-12-19 10:26:23 -0800 | [diff] [blame] | 159 | typedef std::vector<ConnectionInfo> ConnectionInfos; |
| 160 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 161 | const char* ProtoToString(ProtocolType proto); |
| 162 | bool StringToProto(const char* value, ProtocolType* proto); |
| 163 | |
| 164 | struct ProtocolAddress { |
| 165 | rtc::SocketAddress address; |
| 166 | ProtocolType proto; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 167 | |
| 168 | ProtocolAddress(const rtc::SocketAddress& a, ProtocolType p) |
hnsl | 277b250 | 2016-12-13 05:17:23 -0800 | [diff] [blame] | 169 | : address(a), proto(p) {} |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 170 | |
| 171 | bool operator==(const ProtocolAddress& o) const { |
hnsl | 277b250 | 2016-12-13 05:17:23 -0800 | [diff] [blame] | 172 | return address == o.address && proto == o.proto; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 173 | } |
| 174 | bool operator!=(const ProtocolAddress& o) const { return !(*this == o); } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | typedef 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. |
| 182 | class Port : public PortInterface, public rtc::MessageHandler, |
| 183 | public sigslot::has_slots<> { |
| 184 | public: |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 185 | // INIT: The state when a port is just created. |
| 186 | // KEEP_ALIVE_UNTIL_PRUNED: A port should not be destroyed even if no |
| 187 | // connection is using it. |
| 188 | // PRUNED: It will be destroyed if no connection is using it for a period of |
| 189 | // 30 seconds. |
| 190 | enum class State { INIT, KEEP_ALIVE_UNTIL_PRUNED, PRUNED }; |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 191 | Port(rtc::Thread* thread, |
Honghai Zhang | d00c057 | 2016-06-28 09:44:47 -0700 | [diff] [blame] | 192 | const std::string& type, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 193 | rtc::PacketSocketFactory* factory, |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 194 | rtc::Network* network, |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 195 | const std::string& username_fragment, |
| 196 | const std::string& password); |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 197 | // TODO(deadbeef): Delete this constructor once clients are moved off of it. |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 198 | Port(rtc::Thread* thread, |
| 199 | const std::string& type, |
| 200 | rtc::PacketSocketFactory* factory, |
| 201 | rtc::Network* network, |
| 202 | const rtc::IPAddress& ip, |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 203 | const std::string& username_fragment, |
Steve Anton | f2737d2 | 2017-10-31 16:27:34 -0700 | [diff] [blame] | 204 | const std::string& password); |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 205 | Port(rtc::Thread* thread, |
| 206 | const std::string& type, |
| 207 | rtc::PacketSocketFactory* factory, |
| 208 | rtc::Network* network, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 209 | uint16_t min_port, |
| 210 | uint16_t max_port, |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 211 | const std::string& username_fragment, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 212 | const std::string& password); |
Steve Anton | 1cf1b7d | 2017-10-30 10:00:15 -0700 | [diff] [blame] | 213 | ~Port() override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 214 | |
Qingsi Wang | 4ff5443 | 2018-03-01 18:25:20 -0800 | [diff] [blame] | 215 | // Note that the port type does NOT uniquely identify different subclasses of |
| 216 | // Port. Use the 2-tuple of the port type AND the protocol (GetProtocol()) to |
| 217 | // uniquely identify subclasses. Whenever a new subclass of Port introduces a |
| 218 | // conflit in the value of the 2-tuple, make sure that the implementation that |
| 219 | // relies on this 2-tuple for RTTI is properly changed. |
Steve Anton | 1cf1b7d | 2017-10-30 10:00:15 -0700 | [diff] [blame] | 220 | const std::string& Type() const override; |
| 221 | rtc::Network* Network() const override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 222 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 223 | // Methods to set/get ICE role and tiebreaker values. |
Steve Anton | 1cf1b7d | 2017-10-30 10:00:15 -0700 | [diff] [blame] | 224 | IceRole GetIceRole() const override; |
| 225 | void SetIceRole(IceRole role) override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 226 | |
Steve Anton | 1cf1b7d | 2017-10-30 10:00:15 -0700 | [diff] [blame] | 227 | void SetIceTiebreaker(uint64_t tiebreaker) override; |
| 228 | uint64_t IceTiebreaker() const override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 229 | |
Steve Anton | 1cf1b7d | 2017-10-30 10:00:15 -0700 | [diff] [blame] | 230 | bool SharedSocket() const override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 231 | void ResetSharedSocket() { shared_socket_ = false; } |
| 232 | |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 233 | // Should not destroy the port even if no connection is using it. Called when |
| 234 | // a port is ready to use. |
| 235 | void KeepAliveUntilPruned(); |
| 236 | // Allows a port to be destroyed if no connection is using it. |
| 237 | void Prune(); |
| 238 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 239 | // The thread on which this port performs its I/O. |
| 240 | rtc::Thread* thread() { return thread_; } |
| 241 | |
| 242 | // The factory used to create the sockets of this port. |
| 243 | rtc::PacketSocketFactory* socket_factory() const { return factory_; } |
| 244 | void set_socket_factory(rtc::PacketSocketFactory* factory) { |
| 245 | factory_ = factory; |
| 246 | } |
| 247 | |
| 248 | // For debugging purposes. |
| 249 | const std::string& content_name() const { return content_name_; } |
| 250 | void set_content_name(const std::string& content_name) { |
| 251 | content_name_ = content_name; |
| 252 | } |
| 253 | |
| 254 | int component() const { return component_; } |
| 255 | void set_component(int component) { component_ = component; } |
| 256 | |
| 257 | bool send_retransmit_count_attribute() const { |
| 258 | return send_retransmit_count_attribute_; |
| 259 | } |
| 260 | void set_send_retransmit_count_attribute(bool enable) { |
| 261 | send_retransmit_count_attribute_ = enable; |
| 262 | } |
| 263 | |
| 264 | // Identifies the generation that this port was created in. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 265 | uint32_t generation() const { return generation_; } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 266 | void set_generation(uint32_t generation) { generation_ = generation; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 267 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 268 | const std::string username_fragment() const; |
| 269 | const std::string& password() const { return password_; } |
| 270 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 271 | // May be called when this port was initially created by a pooled |
| 272 | // PortAllocatorSession, and is now being assigned to an ICE transport. |
| 273 | // Updates the information for candidates as well. |
| 274 | void SetIceParameters(int component, |
| 275 | const std::string& username_fragment, |
| 276 | const std::string& password); |
| 277 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 278 | // Fired when candidates are discovered by the port. When all candidates |
| 279 | // are discovered that belong to port SignalAddressReady is fired. |
| 280 | sigslot::signal2<Port*, const Candidate&> SignalCandidateReady; |
| 281 | |
| 282 | // Provides all of the above information in one handy object. |
Steve Anton | 1cf1b7d | 2017-10-30 10:00:15 -0700 | [diff] [blame] | 283 | const std::vector<Candidate>& Candidates() const override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 284 | |
| 285 | // SignalPortComplete is sent when port completes the task of candidates |
| 286 | // allocation. |
| 287 | sigslot::signal1<Port*> SignalPortComplete; |
| 288 | // This signal sent when port fails to allocate candidates and this port |
| 289 | // can't be used in establishing the connections. When port is in shared mode |
| 290 | // and port fails to allocate one of the candidates, port shouldn't send |
| 291 | // this signal as other candidates might be usefull in establishing the |
| 292 | // connection. |
| 293 | sigslot::signal1<Port*> SignalPortError; |
| 294 | |
| 295 | // Returns a map containing all of the connections of this port, keyed by the |
| 296 | // remote address. |
| 297 | typedef std::map<rtc::SocketAddress, Connection*> AddressMap; |
| 298 | const AddressMap& connections() { return connections_; } |
| 299 | |
| 300 | // Returns the connection to the given address or NULL if none exists. |
Steve Anton | 1cf1b7d | 2017-10-30 10:00:15 -0700 | [diff] [blame] | 301 | Connection* GetConnection(const rtc::SocketAddress& remote_addr) override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 302 | |
| 303 | // Called each time a connection is created. |
| 304 | sigslot::signal2<Port*, Connection*> SignalConnectionCreated; |
| 305 | |
| 306 | // In a shared socket mode each port which shares the socket will decide |
| 307 | // to accept the packet based on the |remote_addr|. Currently only UDP |
| 308 | // port implemented this method. |
| 309 | // TODO(mallinath) - Make it pure virtual. |
Steve Anton | 1cf1b7d | 2017-10-30 10:00:15 -0700 | [diff] [blame] | 310 | virtual bool HandleIncomingPacket(rtc::AsyncPacketSocket* socket, |
| 311 | const char* data, |
| 312 | size_t size, |
| 313 | const rtc::SocketAddress& remote_addr, |
| 314 | const rtc::PacketTime& packet_time); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 315 | |
Jonas Oreland | 202994c | 2017-12-18 12:10:43 +0100 | [diff] [blame] | 316 | // Shall the port handle packet from this |remote_addr|. |
| 317 | // This method is overridden by TurnPort. |
| 318 | virtual bool CanHandleIncomingPacketsFrom( |
| 319 | const rtc::SocketAddress& remote_addr) const; |
| 320 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 321 | // Sends a response message (normal or error) to the given request. One of |
| 322 | // these methods should be called as a response to SignalUnknownAddress. |
| 323 | // NOTE: You MUST call CreateConnection BEFORE SendBindingResponse. |
Steve Anton | 1cf1b7d | 2017-10-30 10:00:15 -0700 | [diff] [blame] | 324 | void SendBindingResponse(StunMessage* request, |
| 325 | const rtc::SocketAddress& addr) override; |
| 326 | void SendBindingErrorResponse(StunMessage* request, |
| 327 | const rtc::SocketAddress& addr, |
| 328 | int error_code, |
| 329 | const std::string& reason) override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 330 | |
| 331 | void set_proxy(const std::string& user_agent, |
| 332 | const rtc::ProxyInfo& proxy) { |
| 333 | user_agent_ = user_agent; |
| 334 | proxy_ = proxy; |
| 335 | } |
| 336 | const std::string& user_agent() { return user_agent_; } |
| 337 | const rtc::ProxyInfo& proxy() { return proxy_; } |
| 338 | |
Steve Anton | 1cf1b7d | 2017-10-30 10:00:15 -0700 | [diff] [blame] | 339 | void EnablePortPackets() override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 340 | |
| 341 | // Called if the port has no connections and is no longer useful. |
| 342 | void Destroy(); |
| 343 | |
Steve Anton | 1cf1b7d | 2017-10-30 10:00:15 -0700 | [diff] [blame] | 344 | void OnMessage(rtc::Message* pmsg) override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 345 | |
| 346 | // Debugging description of this port |
Steve Anton | 1cf1b7d | 2017-10-30 10:00:15 -0700 | [diff] [blame] | 347 | std::string ToString() const override; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 348 | uint16_t min_port() { return min_port_; } |
| 349 | uint16_t max_port() { return max_port_; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 350 | |
| 351 | // Timeout shortening function to speed up unit tests. |
| 352 | void set_timeout_delay(int delay) { timeout_delay_ = delay; } |
| 353 | |
| 354 | // This method will return local and remote username fragements from the |
| 355 | // stun username attribute if present. |
| 356 | bool ParseStunUsername(const StunMessage* stun_msg, |
| 357 | std::string* local_username, |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 358 | std::string* remote_username) const; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 359 | void CreateStunUsername(const std::string& remote_username, |
| 360 | std::string* stun_username_attr_str) const; |
| 361 | |
| 362 | bool MaybeIceRoleConflict(const rtc::SocketAddress& addr, |
| 363 | IceMessage* stun_msg, |
| 364 | const std::string& remote_ufrag); |
| 365 | |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 366 | // Called when a packet has been sent to the socket. |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 367 | // This is made pure virtual to notify subclasses of Port that they MUST |
| 368 | // listen to AsyncPacketSocket::SignalSentPacket and then call |
| 369 | // PortInterface::OnSentPacket. |
| 370 | virtual void OnSentPacket(rtc::AsyncPacketSocket* socket, |
| 371 | const rtc::SentPacket& sent_packet) = 0; |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 372 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 373 | // Called when the socket is currently able to send. |
| 374 | void OnReadyToSend(); |
| 375 | |
| 376 | // Called when the Connection discovers a local peer reflexive candidate. |
| 377 | // Returns the index of the new local candidate. |
| 378 | size_t AddPrflxCandidate(const Candidate& local); |
| 379 | |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 380 | int16_t network_cost() const { return network_cost_; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 381 | |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame^] | 382 | void GetStunStats(absl::optional<StunStats>* stats) override{}; |
Qingsi Wang | 72a43a1 | 2018-02-20 16:03:18 -0800 | [diff] [blame] | 383 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 384 | protected: |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 385 | enum { MSG_DESTROY_IF_DEAD = 0, MSG_FIRST_AVAILABLE }; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 386 | |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 387 | virtual void UpdateNetworkCost(); |
| 388 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 389 | void set_type(const std::string& type) { type_ = type; } |
| 390 | |
Peter Boström | 2758c66 | 2017-02-13 20:33:27 -0500 | [diff] [blame] | 391 | // Deprecated. Use the AddAddress() method below with "url" instead. |
| 392 | // TODO(zhihuang): Remove this after downstream applications stop using it. |
| 393 | void AddAddress(const rtc::SocketAddress& address, |
| 394 | const rtc::SocketAddress& base_address, |
| 395 | const rtc::SocketAddress& related_address, |
| 396 | const std::string& protocol, |
| 397 | const std::string& relay_protocol, |
| 398 | const std::string& tcptype, |
| 399 | const std::string& type, |
| 400 | uint32_t type_preference, |
| 401 | uint32_t relay_preference, |
| 402 | bool final); |
| 403 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 404 | void AddAddress(const rtc::SocketAddress& address, |
| 405 | const rtc::SocketAddress& base_address, |
| 406 | const rtc::SocketAddress& related_address, |
Guo-wei Shieh | 3d564c1 | 2015-08-19 16:51:15 -0700 | [diff] [blame] | 407 | const std::string& protocol, |
| 408 | const std::string& relay_protocol, |
| 409 | const std::string& tcptype, |
| 410 | const std::string& type, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 411 | uint32_t type_preference, |
| 412 | uint32_t relay_preference, |
zhihuang | 26d99c2 | 2017-02-13 12:47:27 -0800 | [diff] [blame] | 413 | const std::string& url, |
Guo-wei Shieh | 3d564c1 | 2015-08-19 16:51:15 -0700 | [diff] [blame] | 414 | bool final); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 415 | |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 416 | // Adds the given connection to the map keyed by the remote candidate address. |
| 417 | // If an existing connection has the same address, the existing one will be |
| 418 | // replaced and destroyed. |
| 419 | void AddOrReplaceConnection(Connection* conn); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 420 | |
| 421 | // Called when a packet is received from an unknown address that is not |
| 422 | // currently a connection. If this is an authenticated STUN binding request, |
| 423 | // then we will signal the client. |
| 424 | void OnReadPacket(const char* data, size_t size, |
| 425 | const rtc::SocketAddress& addr, |
| 426 | ProtocolType proto); |
| 427 | |
| 428 | // If the given data comprises a complete and correct STUN message then the |
| 429 | // return value is true, otherwise false. If the message username corresponds |
| 430 | // with this port's username fragment, msg will contain the parsed STUN |
| 431 | // message. Otherwise, the function may send a STUN response internally. |
| 432 | // remote_username contains the remote fragment of the STUN username. |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 433 | bool GetStunMessage(const char* data, |
| 434 | size_t size, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 435 | const rtc::SocketAddress& addr, |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 436 | std::unique_ptr<IceMessage>* out_msg, |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 437 | std::string* out_username); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 438 | |
| 439 | // Checks if the address in addr is compatible with the port's ip. |
| 440 | bool IsCompatibleAddress(const rtc::SocketAddress& addr); |
| 441 | |
| 442 | // Returns default DSCP value. |
| 443 | rtc::DiffServCodePoint DefaultDscpValue() const { |
| 444 | // No change from what MediaChannel set. |
| 445 | return rtc::DSCP_NO_CHANGE; |
| 446 | } |
| 447 | |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 448 | // Extra work to be done in subclasses when a connection is destroyed. |
| 449 | virtual void HandleConnectionDestroyed(Connection* conn) {} |
| 450 | |
Qingsi Wang | 6e641e6 | 2018-04-11 20:14:17 -0700 | [diff] [blame] | 451 | void CopyPortInformationToPacketInfo(rtc::PacketInfo* info) const; |
| 452 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 453 | private: |
| 454 | void Construct(); |
| 455 | // Called when one of our connections deletes itself. |
| 456 | void OnConnectionDestroyed(Connection* conn); |
| 457 | |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 458 | void OnNetworkTypeChanged(const rtc::Network* network); |
| 459 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 460 | rtc::Thread* thread_; |
| 461 | rtc::PacketSocketFactory* factory_; |
| 462 | std::string type_; |
| 463 | bool send_retransmit_count_attribute_; |
| 464 | rtc::Network* network_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 465 | uint16_t min_port_; |
| 466 | uint16_t max_port_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 467 | std::string content_name_; |
| 468 | int component_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 469 | uint32_t generation_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 470 | // In order to establish a connection to this Port (so that real data can be |
| 471 | // sent through), the other side must send us a STUN binding request that is |
| 472 | // authenticated with this username_fragment and password. |
| 473 | // PortAllocatorSession will provide these username_fragment and password. |
| 474 | // |
| 475 | // Note: we should always use username_fragment() instead of using |
| 476 | // |ice_username_fragment_| directly. For the details see the comment on |
| 477 | // username_fragment(). |
| 478 | std::string ice_username_fragment_; |
| 479 | std::string password_; |
| 480 | std::vector<Candidate> candidates_; |
| 481 | AddressMap connections_; |
| 482 | int timeout_delay_; |
| 483 | bool enable_port_packets_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 484 | IceRole ice_role_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 485 | uint64_t tiebreaker_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 486 | bool shared_socket_; |
| 487 | // Information to use when going through a proxy. |
| 488 | std::string user_agent_; |
| 489 | rtc::ProxyInfo proxy_; |
| 490 | |
honghaiz | e1a0c94 | 2016-02-16 14:54:56 -0800 | [diff] [blame] | 491 | // A virtual cost perceived by the user, usually based on the network type |
| 492 | // (WiFi. vs. Cellular). It takes precedence over the priority when |
| 493 | // comparing two connections. |
Kári Tristan Helgason | 47d3a01 | 2017-10-24 15:28:51 +0200 | [diff] [blame] | 494 | int16_t network_cost_; |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 495 | State state_ = State::INIT; |
Honghai Zhang | b5db1ec | 2016-07-28 13:23:05 -0700 | [diff] [blame] | 496 | int64_t last_time_all_connections_removed_ = 0; |
honghaiz | e1a0c94 | 2016-02-16 14:54:56 -0800 | [diff] [blame] | 497 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 498 | friend class Connection; |
| 499 | }; |
| 500 | |
| 501 | // Represents a communication link between a port on the local client and a |
| 502 | // port on the remote client. |
Honghai Zhang | cc411c0 | 2016-03-29 17:27:21 -0700 | [diff] [blame] | 503 | class Connection : public CandidatePairInterface, |
| 504 | public rtc::MessageHandler, |
| 505 | public sigslot::has_slots<> { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 506 | public: |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 507 | struct SentPing { |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 508 | SentPing(const std::string id, int64_t sent_time, uint32_t nomination) |
| 509 | : id(id), sent_time(sent_time), nomination(nomination) {} |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 510 | |
| 511 | std::string id; |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 512 | int64_t sent_time; |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 513 | uint32_t nomination; |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 514 | }; |
| 515 | |
Steve Anton | 1cf1b7d | 2017-10-30 10:00:15 -0700 | [diff] [blame] | 516 | ~Connection() override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 517 | |
| 518 | // The local port where this connection sends and receives packets. |
| 519 | Port* port() { return port_; } |
| 520 | const Port* port() const { return port_; } |
| 521 | |
Honghai Zhang | cc411c0 | 2016-03-29 17:27:21 -0700 | [diff] [blame] | 522 | // Implementation of virtual methods in CandidatePairInterface. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 523 | // Returns the description of the local port |
Steve Anton | 1cf1b7d | 2017-10-30 10:00:15 -0700 | [diff] [blame] | 524 | const Candidate& local_candidate() const override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 525 | // Returns the description of the remote port to which we communicate. |
Steve Anton | 1cf1b7d | 2017-10-30 10:00:15 -0700 | [diff] [blame] | 526 | const Candidate& remote_candidate() const override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 527 | |
| 528 | // Returns the pair priority. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 529 | uint64_t priority() const; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 530 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 531 | enum WriteState { |
| 532 | STATE_WRITABLE = 0, // we have received ping responses recently |
| 533 | STATE_WRITE_UNRELIABLE = 1, // we have had a few ping failures |
| 534 | STATE_WRITE_INIT = 2, // we have yet to receive a ping response |
| 535 | STATE_WRITE_TIMEOUT = 3, // we have had a large number of ping failures |
| 536 | }; |
| 537 | |
| 538 | WriteState write_state() const { return write_state_; } |
| 539 | bool writable() const { return write_state_ == STATE_WRITABLE; } |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 540 | bool receiving() const { return receiving_; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 541 | |
| 542 | // Determines whether the connection has finished connecting. This can only |
| 543 | // be false for TCP connections. |
| 544 | bool connected() const { return connected_; } |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 545 | bool weak() const { return !(writable() && receiving() && connected()); } |
| 546 | bool active() const { |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 547 | return write_state_ != STATE_WRITE_TIMEOUT; |
| 548 | } |
honghaiz | 059e183 | 2016-06-24 11:03:55 -0700 | [diff] [blame] | 549 | |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 550 | // A connection is dead if it can be safely deleted. |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 551 | bool dead(int64_t now) const; |
honghaiz | 8937437 | 2015-09-24 13:14:47 -0700 | [diff] [blame] | 552 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 553 | // Estimate of the round-trip time over this connection. |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 554 | int rtt() const { return rtt_; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 555 | |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 556 | int unwritable_timeout() const; |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame^] | 557 | void set_unwritable_timeout(const absl::optional<int>& value_ms) { |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 558 | unwritable_timeout_ = value_ms; |
| 559 | } |
| 560 | int unwritable_min_checks() const; |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame^] | 561 | void set_unwritable_min_checks(const absl::optional<int>& value) { |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 562 | unwritable_min_checks_ = value; |
| 563 | } |
| 564 | |
hbos | 06495bc | 2017-01-02 08:08:18 -0800 | [diff] [blame] | 565 | // Gets the |ConnectionInfo| stats, where |best_connection| has not been |
| 566 | // populated (default value false). |
zhihuang | 5ecf16c | 2016-06-01 17:09:15 -0700 | [diff] [blame] | 567 | ConnectionInfo stats(); |
| 568 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 569 | sigslot::signal1<Connection*> SignalStateChange; |
| 570 | |
| 571 | // Sent when the connection has decided that it is no longer of value. It |
| 572 | // will delete itself immediately after this call. |
| 573 | sigslot::signal1<Connection*> SignalDestroyed; |
| 574 | |
| 575 | // The connection can send and receive packets asynchronously. This matches |
| 576 | // the interface of AsyncPacketSocket, which may use UDP or TCP under the |
| 577 | // covers. |
| 578 | virtual int Send(const void* data, size_t size, |
| 579 | const rtc::PacketOptions& options) = 0; |
| 580 | |
| 581 | // Error if Send() returns < 0 |
| 582 | virtual int GetError() = 0; |
| 583 | |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 584 | sigslot::signal4<Connection*, const char*, size_t, const rtc::PacketTime&> |
| 585 | SignalReadPacket; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 586 | |
| 587 | sigslot::signal1<Connection*> SignalReadyToSend; |
| 588 | |
| 589 | // Called when a packet is received on this connection. |
| 590 | void OnReadPacket(const char* data, size_t size, |
| 591 | const rtc::PacketTime& packet_time); |
| 592 | |
| 593 | // Called when the socket is currently able to send. |
| 594 | void OnReadyToSend(); |
| 595 | |
| 596 | // Called when a connection is determined to be no longer useful to us. We |
| 597 | // still keep it around in case the other side wants to use it. But we can |
| 598 | // safely stop pinging on it and we can allow it to time out if the other |
| 599 | // side stops using it as well. |
| 600 | bool pruned() const { return pruned_; } |
| 601 | void Prune(); |
| 602 | |
| 603 | bool use_candidate_attr() const { return use_candidate_attr_; } |
| 604 | void set_use_candidate_attr(bool enable); |
| 605 | |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 606 | void set_nomination(uint32_t value) { nomination_ = value; } |
| 607 | |
| 608 | uint32_t remote_nomination() const { return remote_nomination_; } |
hbos | 92eaec6 | 2017-02-27 01:38:08 -0800 | [diff] [blame] | 609 | // One or several pairs may be nominated based on if Regular or Aggressive |
| 610 | // Nomination is used. https://tools.ietf.org/html/rfc5245#section-8 |
| 611 | // |nominated| is defined both for the controlling or controlled agent based |
| 612 | // on if a nomination has been pinged or acknowledged. The controlled agent |
| 613 | // gets its |remote_nomination_| set when pinged by the controlling agent with |
| 614 | // a nomination value. The controlling agent gets its |acked_nomination_| set |
| 615 | // when receiving a response to a nominating ping. |
| 616 | bool nominated() const { return acked_nomination_ || remote_nomination_; } |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 617 | // Public for unit tests. |
| 618 | void set_remote_nomination(uint32_t remote_nomination) { |
| 619 | remote_nomination_ = remote_nomination; |
| 620 | } |
| 621 | // Public for unit tests. |
| 622 | uint32_t acked_nomination() const { return acked_nomination_; } |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 623 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 624 | void set_remote_ice_mode(IceMode mode) { |
| 625 | remote_ice_mode_ = mode; |
| 626 | } |
| 627 | |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 628 | int receiving_timeout() const; |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame^] | 629 | void set_receiving_timeout(absl::optional<int> receiving_timeout_ms) { |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 630 | receiving_timeout_ = receiving_timeout_ms; |
| 631 | } |
| 632 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 633 | // Makes the connection go away. |
| 634 | void Destroy(); |
| 635 | |
deadbeef | 376e123 | 2015-11-25 09:00:08 -0800 | [diff] [blame] | 636 | // Makes the connection go away, in a failed state. |
| 637 | void FailAndDestroy(); |
| 638 | |
honghaiz | 079a7a1 | 2016-06-22 16:26:29 -0700 | [diff] [blame] | 639 | // Prunes the connection and sets its state to STATE_FAILED, |
| 640 | // It will not be used or send pings although it can still receive packets. |
| 641 | void FailAndPrune(); |
| 642 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 643 | // Checks that the state of this connection is up-to-date. The argument is |
| 644 | // the current time, which is compared against various timeouts. |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 645 | void UpdateState(int64_t now); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 646 | |
| 647 | // Called when this connection should try checking writability again. |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 648 | int64_t last_ping_sent() const { return last_ping_sent_; } |
| 649 | void Ping(int64_t now); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 650 | void ReceivedPingResponse(int rtt, const std::string& request_id); |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 651 | int64_t last_ping_response_received() const { |
Honghai Zhang | 381b421 | 2015-12-04 12:24:03 -0800 | [diff] [blame] | 652 | return last_ping_response_received_; |
| 653 | } |
Honghai Zhang | fd16da2 | 2016-08-17 16:12:46 -0700 | [diff] [blame] | 654 | // Used to check if any STUN ping response has been received. |
| 655 | int rtt_samples() const { return rtt_samples_; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 656 | |
| 657 | // Called whenever a valid ping is received on this connection. This is |
| 658 | // public because the connection intercepts the first ping for us. |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 659 | int64_t last_ping_received() const { return last_ping_received_; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 660 | void ReceivedPing(); |
honghaiz | 9b5ee9c | 2015-11-11 13:19:17 -0800 | [diff] [blame] | 661 | // Handles the binding request; sends a response if this is a valid request. |
| 662 | void HandleBindingRequest(IceMessage* msg); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 663 | |
Honghai Zhang | 572b094 | 2016-06-23 12:26:57 -0700 | [diff] [blame] | 664 | int64_t last_data_received() const { return last_data_received_; } |
| 665 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 666 | // Debugging description of this connection |
guoweis@webrtc.org | 8c9ff20 | 2014-12-04 07:56:02 +0000 | [diff] [blame] | 667 | std::string ToDebugId() const; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 668 | std::string ToString() const; |
| 669 | std::string ToSensitiveString() const; |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 670 | // Structured description of this candidate pair. |
| 671 | const webrtc::IceCandidatePairDescription& ToLogDescription(); |
| 672 | // Integer typed hash value of this candidate pair. |
| 673 | uint32_t hash() { return hash_; } |
| 674 | void set_ice_event_log(webrtc::IceEventLog* ice_event_log) { |
| 675 | ice_event_log_ = ice_event_log; |
| 676 | } |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 677 | // Prints pings_since_last_response_ into a string. |
| 678 | void PrintPingsSinceLastResponse(std::string* pings, size_t max); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 679 | |
| 680 | bool reported() const { return reported_; } |
| 681 | void set_reported(bool reported) { reported_ = reported;} |
Qingsi Wang | 10a0e51 | 2018-05-16 13:37:03 -0700 | [diff] [blame] | 682 | // The following two methods are only used for logging in ToString above, and |
| 683 | // this flag is set true by P2PTransportChannel for its selected candidate |
| 684 | // pair. |
| 685 | bool selected() const { return selected_; } |
| 686 | void set_selected(bool selected) { selected_ = selected; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 687 | |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 688 | // This signal will be fired if this connection is nominated by the |
| 689 | // controlling side. |
| 690 | sigslot::signal1<Connection*> SignalNominated; |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 691 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 692 | // Invoked when Connection receives STUN error response with 487 code. |
| 693 | void HandleRoleConflictFromPeer(); |
| 694 | |
hbos | 06495bc | 2017-01-02 08:08:18 -0800 | [diff] [blame] | 695 | IceCandidatePairState state() const { return state_; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 696 | |
honghaiz | 524ecc2 | 2016-05-25 12:48:31 -0700 | [diff] [blame] | 697 | int num_pings_sent() const { return num_pings_sent_; } |
| 698 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 699 | IceMode remote_ice_mode() const { return remote_ice_mode_; } |
| 700 | |
honghaiz | e1a0c94 | 2016-02-16 14:54:56 -0800 | [diff] [blame] | 701 | uint32_t ComputeNetworkCost() const; |
| 702 | |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 703 | // Update the ICE password and/or generation of the remote candidate if the |
| 704 | // ufrag in |params| matches the candidate's ufrag, and the |
Taylor Brandstetter | 0a1bc53 | 2016-04-19 18:03:26 -0700 | [diff] [blame] | 705 | // candidate's password and/or ufrag has not been set. |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 706 | void MaybeSetRemoteIceParametersAndGeneration(const IceParameters& params, |
| 707 | int generation); |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 708 | |
| 709 | // If |remote_candidate_| is peer reflexive and is equivalent to |
| 710 | // |new_candidate| except the type, update |remote_candidate_| to |
| 711 | // |new_candidate|. |
| 712 | void MaybeUpdatePeerReflexiveCandidate(const Candidate& new_candidate); |
| 713 | |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 714 | // Returns the last received time of any data, stun request, or stun |
| 715 | // response in milliseconds |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 716 | int64_t last_received() const; |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 717 | // Returns the last time when the connection changed its receiving state. |
| 718 | int64_t receiving_unchanged_since() const { |
| 719 | return receiving_unchanged_since_; |
| 720 | } |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 721 | |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 722 | bool stable(int64_t now) const; |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 723 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 724 | protected: |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 725 | enum { MSG_DELETE = 0, MSG_FIRST_AVAILABLE }; |
| 726 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 727 | // Constructs a new connection to the given remote port. |
| 728 | Connection(Port* port, size_t index, const Candidate& candidate); |
| 729 | |
| 730 | // Called back when StunRequestManager has a stun packet to send |
| 731 | void OnSendStunPacket(const void* data, size_t size, StunRequest* req); |
| 732 | |
| 733 | // Callbacks from ConnectionRequest |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 734 | virtual void OnConnectionRequestResponse(ConnectionRequest* req, |
| 735 | StunMessage* response); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 736 | void OnConnectionRequestErrorResponse(ConnectionRequest* req, |
| 737 | StunMessage* response); |
| 738 | void OnConnectionRequestTimeout(ConnectionRequest* req); |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 739 | void OnConnectionRequestSent(ConnectionRequest* req); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 740 | |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 741 | bool rtt_converged() const; |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 742 | |
| 743 | // If the response is not received within 2 * RTT, the response is assumed to |
| 744 | // be missing. |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 745 | bool missing_responses(int64_t now) const; |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 746 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 747 | // Changes the state and signals if necessary. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 748 | void set_write_state(WriteState value); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 749 | void UpdateReceiving(int64_t now); |
hbos | 06495bc | 2017-01-02 08:08:18 -0800 | [diff] [blame] | 750 | void set_state(IceCandidatePairState state); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 751 | void set_connected(bool value); |
| 752 | |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 753 | uint32_t nomination() const { return nomination_; } |
| 754 | |
Steve Anton | 1cf1b7d | 2017-10-30 10:00:15 -0700 | [diff] [blame] | 755 | void OnMessage(rtc::Message* pmsg) override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 756 | |
| 757 | Port* port_; |
| 758 | size_t local_candidate_index_; |
| 759 | Candidate remote_candidate_; |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 760 | |
| 761 | ConnectionInfo stats_; |
| 762 | rtc::RateTracker recv_rate_tracker_; |
| 763 | rtc::RateTracker send_rate_tracker_; |
| 764 | |
| 765 | private: |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 766 | // Update the local candidate based on the mapped address attribute. |
| 767 | // If the local candidate changed, fires SignalStateChange. |
| 768 | void MaybeUpdateLocalCandidate(ConnectionRequest* request, |
| 769 | StunMessage* response); |
| 770 | |
Bjorn Terelius | 59b4e3e | 2018-05-30 17:14:08 +0200 | [diff] [blame] | 771 | void LogCandidatePairConfig(webrtc::IceCandidatePairConfigType type); |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 772 | void LogCandidatePairEvent(webrtc::IceCandidatePairEventType type); |
| 773 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 774 | WriteState write_state_; |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 775 | bool receiving_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 776 | bool connected_; |
| 777 | bool pruned_; |
Qingsi Wang | 10a0e51 | 2018-05-16 13:37:03 -0700 | [diff] [blame] | 778 | bool selected_ = false; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 779 | // By default |use_candidate_attr_| flag will be true, |
honghaiz | 5a3acd8 | 2015-08-20 15:53:17 -0700 | [diff] [blame] | 780 | // as we will be using aggressive nomination. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 781 | // But when peer is ice-lite, this flag "must" be initialized to false and |
| 782 | // turn on when connection becomes "best connection". |
| 783 | bool use_candidate_attr_; |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 784 | // Used by the controlling side to indicate that this connection will be |
| 785 | // selected for transmission if the peer supports ICE-renomination when this |
| 786 | // value is positive. A larger-value indicates that a connection is nominated |
| 787 | // later and should be selected by the controlled side with higher precedence. |
| 788 | // A zero-value indicates not nominating this connection. |
| 789 | uint32_t nomination_ = 0; |
| 790 | // The last nomination that has been acknowledged. |
| 791 | uint32_t acked_nomination_ = 0; |
| 792 | // Used by the controlled side to remember the nomination value received from |
| 793 | // the controlling side. When the peer does not support ICE re-nomination, |
| 794 | // its value will be 1 if the connection has been nominated. |
| 795 | uint32_t remote_nomination_ = 0; |
| 796 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 797 | IceMode remote_ice_mode_; |
| 798 | StunRequestManager requests_; |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 799 | int rtt_; |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 800 | int rtt_samples_ = 0; |
hbos | bf8d3e5 | 2017-02-28 06:34:47 -0800 | [diff] [blame] | 801 | // https://w3c.github.io/webrtc-stats/#dom-rtcicecandidatepairstats-totalroundtriptime |
| 802 | uint64_t total_round_trip_time_ms_ = 0; |
| 803 | // https://w3c.github.io/webrtc-stats/#dom-rtcicecandidatepairstats-currentroundtriptime |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame^] | 804 | absl::optional<uint32_t> current_round_trip_time_ms_; |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 805 | int64_t last_ping_sent_; // last time we sent a ping to the other side |
| 806 | int64_t last_ping_received_; // last time we received a ping from the other |
| 807 | // side |
| 808 | int64_t last_data_received_; |
| 809 | int64_t last_ping_response_received_; |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 810 | int64_t receiving_unchanged_since_ = 0; |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 811 | std::vector<SentPing> pings_since_last_response_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 812 | |
zstein | abbacbf | 2017-03-20 10:53:12 -0700 | [diff] [blame] | 813 | PacketLossEstimator packet_loss_estimator_; |
| 814 | |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame^] | 815 | absl::optional<int> unwritable_timeout_; |
| 816 | absl::optional<int> unwritable_min_checks_; |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 817 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 818 | bool reported_; |
hbos | 06495bc | 2017-01-02 08:08:18 -0800 | [diff] [blame] | 819 | IceCandidatePairState state_; |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 820 | // Time duration to switch from receiving to not receiving. |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame^] | 821 | absl::optional<int> receiving_timeout_; |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 822 | int64_t time_created_ms_; |
honghaiz | 524ecc2 | 2016-05-25 12:48:31 -0700 | [diff] [blame] | 823 | int num_pings_sent_ = 0; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 824 | |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame^] | 825 | absl::optional<webrtc::IceCandidatePairDescription> log_description_; |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 826 | uint32_t hash_; |
| 827 | webrtc::IceEventLog* ice_event_log_ = nullptr; |
| 828 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 829 | friend class Port; |
| 830 | friend class ConnectionRequest; |
| 831 | }; |
| 832 | |
deadbeef | 376e123 | 2015-11-25 09:00:08 -0800 | [diff] [blame] | 833 | // ProxyConnection defers all the interesting work to the port. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 834 | class ProxyConnection : public Connection { |
| 835 | public: |
deadbeef | 376e123 | 2015-11-25 09:00:08 -0800 | [diff] [blame] | 836 | ProxyConnection(Port* port, size_t index, const Candidate& remote_candidate); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 837 | |
deadbeef | 376e123 | 2015-11-25 09:00:08 -0800 | [diff] [blame] | 838 | int Send(const void* data, |
| 839 | size_t size, |
| 840 | const rtc::PacketOptions& options) override; |
Steve Anton | 1cf1b7d | 2017-10-30 10:00:15 -0700 | [diff] [blame] | 841 | int GetError() override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 842 | |
| 843 | private: |
deadbeef | 376e123 | 2015-11-25 09:00:08 -0800 | [diff] [blame] | 844 | int error_ = 0; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 845 | }; |
| 846 | |
| 847 | } // namespace cricket |
| 848 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 849 | #endif // P2P_BASE_PORT_H_ |