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