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