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