blob: 06efd2d3c235c4e976fda6f610c187741ac7542f [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#ifndef WEBRTC_P2P_BASE_PORT_H_
12#define WEBRTC_P2P_BASE_PORT_H_
13
14#include <map>
kwiberg3ec46792016-04-27 07:22:53 -070015#include <memory>
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000016#include <set>
17#include <string>
18#include <vector>
19
20#include "webrtc/p2p/base/candidate.h"
Honghai Zhangcc411c02016-03-29 17:27:21 -070021#include "webrtc/p2p/base/candidatepairinterface.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000022#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
35namespace cricket {
36
37class Connection;
38class ConnectionRequest;
39
40extern const char LOCAL_PORT_TYPE[];
41extern const char STUN_PORT_TYPE[];
42extern const char PRFLX_PORT_TYPE[];
43extern const char RELAY_PORT_TYPE[];
44
45extern const char UDP_PROTOCOL_NAME[];
46extern const char TCP_PROTOCOL_NAME[];
47extern const char SSLTCP_PROTOCOL_NAME[];
48
49// RFC 6544, TCP candidate encoding rules.
50extern const int DISCARD_PORT;
51extern const char TCPTYPE_ACTIVE_STR[];
52extern const char TCPTYPE_PASSIVE_STR[];
53extern const char TCPTYPE_SIMOPEN_STR[];
54
Honghai Zhang2b342bf2015-09-30 09:51:58 -070055// The minimum time we will wait before destroying a connection after creating
56// it.
honghaiz34b11eb2016-03-16 08:55:44 -070057static const int MIN_CONNECTION_LIFETIME = 10 * 1000; // 10 seconds.
Peter Thatcher04ac81f2015-09-21 11:48:28 -070058
Honghai Zhang2cd7afe2015-11-12 11:14:33 -080059// A connection will be declared dead if it has not received anything for this
60// long.
honghaiz34b11eb2016-03-16 08:55:44 -070061static const int DEAD_CONNECTION_RECEIVE_TIMEOUT = 30 * 1000; // 30 seconds.
Honghai Zhang2cd7afe2015-11-12 11:14:33 -080062
Peter Thatcher04ac81f2015-09-21 11:48:28 -070063// The timeout duration when a connection does not receive anything.
honghaiz34b11eb2016-03-16 08:55:44 -070064static const int WEAK_CONNECTION_RECEIVE_TIMEOUT = 2500; // 2.5 seconds
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000065
66// The length of time we wait before timing out writability on a connection.
honghaiz34b11eb2016-03-16 08:55:44 -070067static const int CONNECTION_WRITE_TIMEOUT = 15 * 1000; // 15 seconds
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000068
69// The length of time we wait before we become unwritable.
honghaiz34b11eb2016-03-16 08:55:44 -070070static const int CONNECTION_WRITE_CONNECT_TIMEOUT = 5 * 1000; // 5 seconds
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000071
72// This is the length of time that we wait for a ping response to come back.
honghaiz34b11eb2016-03-16 08:55:44 -070073static const int CONNECTION_RESPONSE_TIMEOUT = 5 * 1000; // 5 seconds
74
75// The number of pings that must fail to respond before we become unwritable.
76static const uint32_t CONNECTION_WRITE_CONNECT_FAILURES = 5;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000077
78enum RelayType {
79 RELAY_GTURN, // Legacy google relay service.
80 RELAY_TURN // Standard (TURN) relay service.
81};
82
83enum 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,
92 ICE_TYPE_PREFERENCE_HOST_TCP = 90,
93 ICE_TYPE_PREFERENCE_SRFLX = 100,
94 ICE_TYPE_PREFERENCE_PRFLX = 110,
95 ICE_TYPE_PREFERENCE_HOST = 126
96};
97
98const char* ProtoToString(ProtocolType proto);
99bool StringToProto(const char* value, ProtocolType* proto);
100
101struct ProtocolAddress {
102 rtc::SocketAddress address;
103 ProtocolType proto;
104 bool secure;
105
106 ProtocolAddress(const rtc::SocketAddress& a, ProtocolType p)
107 : address(a), proto(p), secure(false) { }
108 ProtocolAddress(const rtc::SocketAddress& a, ProtocolType p, bool sec)
109 : address(a), proto(p), secure(sec) { }
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700110
111 bool operator==(const ProtocolAddress& o) const {
112 return address == o.address && proto == o.proto && secure == o.secure;
113 }
114 bool operator!=(const ProtocolAddress& o) const { return !(*this == o); }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000115};
116
117typedef std::set<rtc::SocketAddress> ServerAddresses;
118
119// Represents a local communication mechanism that can be used to create
120// connections to similar mechanisms of the other client. Subclasses of this
121// one add support for specific mechanisms like local UDP ports.
122class Port : public PortInterface, public rtc::MessageHandler,
123 public sigslot::has_slots<> {
124 public:
pkasting@chromium.org332331f2014-11-06 20:19:22 +0000125 Port(rtc::Thread* thread,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000126 rtc::PacketSocketFactory* factory,
pkasting@chromium.org332331f2014-11-06 20:19:22 +0000127 rtc::Network* network,
128 const rtc::IPAddress& ip,
129 const std::string& username_fragment,
130 const std::string& password);
131 Port(rtc::Thread* thread,
132 const std::string& type,
133 rtc::PacketSocketFactory* factory,
134 rtc::Network* network,
135 const rtc::IPAddress& ip,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200136 uint16_t min_port,
137 uint16_t max_port,
pkasting@chromium.org332331f2014-11-06 20:19:22 +0000138 const std::string& username_fragment,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000139 const std::string& password);
140 virtual ~Port();
141
142 virtual const std::string& Type() const { return type_; }
143 virtual rtc::Network* Network() const { return network_; }
144
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000145 // Methods to set/get ICE role and tiebreaker values.
146 IceRole GetIceRole() const { return ice_role_; }
147 void SetIceRole(IceRole role) { ice_role_ = role; }
148
Peter Boström0c4e06b2015-10-07 12:23:21 +0200149 void SetIceTiebreaker(uint64_t tiebreaker) { tiebreaker_ = tiebreaker; }
150 uint64_t IceTiebreaker() const { return tiebreaker_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000151
152 virtual bool SharedSocket() const { return shared_socket_; }
153 void ResetSharedSocket() { shared_socket_ = false; }
154
155 // The thread on which this port performs its I/O.
156 rtc::Thread* thread() { return thread_; }
157
158 // The factory used to create the sockets of this port.
159 rtc::PacketSocketFactory* socket_factory() const { return factory_; }
160 void set_socket_factory(rtc::PacketSocketFactory* factory) {
161 factory_ = factory;
162 }
163
164 // For debugging purposes.
165 const std::string& content_name() const { return content_name_; }
166 void set_content_name(const std::string& content_name) {
167 content_name_ = content_name;
168 }
169
170 int component() const { return component_; }
171 void set_component(int component) { component_ = component; }
172
173 bool send_retransmit_count_attribute() const {
174 return send_retransmit_count_attribute_;
175 }
176 void set_send_retransmit_count_attribute(bool enable) {
177 send_retransmit_count_attribute_ = enable;
178 }
179
180 // Identifies the generation that this port was created in.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200181 uint32_t generation() { return generation_; }
182 void set_generation(uint32_t generation) { generation_ = generation; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000183
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000184 const std::string username_fragment() const;
185 const std::string& password() const { return password_; }
186
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700187 // May be called when this port was initially created by a pooled
188 // PortAllocatorSession, and is now being assigned to an ICE transport.
189 // Updates the information for candidates as well.
190 void SetIceParameters(int component,
191 const std::string& username_fragment,
192 const std::string& password);
193
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000194 // Fired when candidates are discovered by the port. When all candidates
195 // are discovered that belong to port SignalAddressReady is fired.
196 sigslot::signal2<Port*, const Candidate&> SignalCandidateReady;
197
198 // Provides all of the above information in one handy object.
199 virtual const std::vector<Candidate>& Candidates() const {
200 return candidates_;
201 }
202
203 // SignalPortComplete is sent when port completes the task of candidates
204 // allocation.
205 sigslot::signal1<Port*> SignalPortComplete;
206 // This signal sent when port fails to allocate candidates and this port
207 // can't be used in establishing the connections. When port is in shared mode
208 // and port fails to allocate one of the candidates, port shouldn't send
209 // this signal as other candidates might be usefull in establishing the
210 // connection.
211 sigslot::signal1<Port*> SignalPortError;
212
213 // Returns a map containing all of the connections of this port, keyed by the
214 // remote address.
215 typedef std::map<rtc::SocketAddress, Connection*> AddressMap;
216 const AddressMap& connections() { return connections_; }
217
218 // Returns the connection to the given address or NULL if none exists.
219 virtual Connection* GetConnection(
220 const rtc::SocketAddress& remote_addr);
221
222 // Called each time a connection is created.
223 sigslot::signal2<Port*, Connection*> SignalConnectionCreated;
224
225 // In a shared socket mode each port which shares the socket will decide
226 // to accept the packet based on the |remote_addr|. Currently only UDP
227 // port implemented this method.
228 // TODO(mallinath) - Make it pure virtual.
229 virtual bool HandleIncomingPacket(
230 rtc::AsyncPacketSocket* socket, const char* data, size_t size,
231 const rtc::SocketAddress& remote_addr,
232 const rtc::PacketTime& packet_time) {
233 ASSERT(false);
234 return false;
235 }
236
237 // Sends a response message (normal or error) to the given request. One of
238 // these methods should be called as a response to SignalUnknownAddress.
239 // NOTE: You MUST call CreateConnection BEFORE SendBindingResponse.
240 virtual void SendBindingResponse(StunMessage* request,
241 const rtc::SocketAddress& addr);
242 virtual void SendBindingErrorResponse(
243 StunMessage* request, const rtc::SocketAddress& addr,
244 int error_code, const std::string& reason);
245
246 void set_proxy(const std::string& user_agent,
247 const rtc::ProxyInfo& proxy) {
248 user_agent_ = user_agent;
249 proxy_ = proxy;
250 }
251 const std::string& user_agent() { return user_agent_; }
252 const rtc::ProxyInfo& proxy() { return proxy_; }
253
254 virtual void EnablePortPackets();
255
256 // Called if the port has no connections and is no longer useful.
257 void Destroy();
258
259 virtual void OnMessage(rtc::Message *pmsg);
260
261 // Debugging description of this port
262 virtual std::string ToString() const;
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000263 const rtc::IPAddress& ip() const { return ip_; }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200264 uint16_t min_port() { return min_port_; }
265 uint16_t max_port() { return max_port_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000266
267 // Timeout shortening function to speed up unit tests.
268 void set_timeout_delay(int delay) { timeout_delay_ = delay; }
269
270 // This method will return local and remote username fragements from the
271 // stun username attribute if present.
272 bool ParseStunUsername(const StunMessage* stun_msg,
273 std::string* local_username,
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700274 std::string* remote_username) const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000275 void CreateStunUsername(const std::string& remote_username,
276 std::string* stun_username_attr_str) const;
277
278 bool MaybeIceRoleConflict(const rtc::SocketAddress& addr,
279 IceMessage* stun_msg,
280 const std::string& remote_ufrag);
281
stefanc1aeaf02015-10-15 07:26:07 -0700282 // Called when a packet has been sent to the socket.
Stefan Holmer55674ff2016-01-14 15:49:16 +0100283 // This is made pure virtual to notify subclasses of Port that they MUST
284 // listen to AsyncPacketSocket::SignalSentPacket and then call
285 // PortInterface::OnSentPacket.
286 virtual void OnSentPacket(rtc::AsyncPacketSocket* socket,
287 const rtc::SentPacket& sent_packet) = 0;
stefanc1aeaf02015-10-15 07:26:07 -0700288
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000289 // Called when the socket is currently able to send.
290 void OnReadyToSend();
291
292 // Called when the Connection discovers a local peer reflexive candidate.
293 // Returns the index of the new local candidate.
294 size_t AddPrflxCandidate(const Candidate& local);
295
honghaiza0c44ea2016-03-23 16:07:48 -0700296 int16_t network_cost() const { return network_cost_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000297
298 protected:
299 enum {
honghaizd0b31432015-09-30 12:42:17 -0700300 MSG_DEAD = 0,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000301 MSG_FIRST_AVAILABLE
302 };
303
Honghai Zhang351d77b2016-05-20 15:08:29 -0700304 virtual void UpdateNetworkCost();
305
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000306 void set_type(const std::string& type) { type_ = type; }
307
308 void AddAddress(const rtc::SocketAddress& address,
309 const rtc::SocketAddress& base_address,
310 const rtc::SocketAddress& related_address,
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700311 const std::string& protocol,
312 const std::string& relay_protocol,
313 const std::string& tcptype,
314 const std::string& type,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200315 uint32_t type_preference,
316 uint32_t relay_preference,
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700317 bool final);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000318
honghaiz36f50e82016-06-01 15:57:03 -0700319 // Adds the given connection to the map keyed by the remote candidate address.
320 // If an existing connection has the same address, the existing one will be
321 // replaced and destroyed.
322 void AddOrReplaceConnection(Connection* conn);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000323
324 // Called when a packet is received from an unknown address that is not
325 // currently a connection. If this is an authenticated STUN binding request,
326 // then we will signal the client.
327 void OnReadPacket(const char* data, size_t size,
328 const rtc::SocketAddress& addr,
329 ProtocolType proto);
330
331 // If the given data comprises a complete and correct STUN message then the
332 // return value is true, otherwise false. If the message username corresponds
333 // with this port's username fragment, msg will contain the parsed STUN
334 // message. Otherwise, the function may send a STUN response internally.
335 // remote_username contains the remote fragment of the STUN username.
kwiberg6baec032016-03-15 11:09:39 -0700336 bool GetStunMessage(const char* data,
337 size_t size,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000338 const rtc::SocketAddress& addr,
kwiberg3ec46792016-04-27 07:22:53 -0700339 std::unique_ptr<IceMessage>* out_msg,
kwiberg6baec032016-03-15 11:09:39 -0700340 std::string* out_username);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000341
342 // Checks if the address in addr is compatible with the port's ip.
343 bool IsCompatibleAddress(const rtc::SocketAddress& addr);
344
345 // Returns default DSCP value.
346 rtc::DiffServCodePoint DefaultDscpValue() const {
347 // No change from what MediaChannel set.
348 return rtc::DSCP_NO_CHANGE;
349 }
350
honghaiz36f50e82016-06-01 15:57:03 -0700351 // Extra work to be done in subclasses when a connection is destroyed.
352 virtual void HandleConnectionDestroyed(Connection* conn) {}
353
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000354 private:
355 void Construct();
356 // Called when one of our connections deletes itself.
357 void OnConnectionDestroyed(Connection* conn);
358
honghaizd0b31432015-09-30 12:42:17 -0700359 // Whether this port is dead, and hence, should be destroyed on the controlled
360 // side.
361 bool dead() const {
362 return ice_role_ == ICEROLE_CONTROLLED && connections_.empty();
363 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000364
honghaize3c6c822016-02-17 13:00:28 -0800365 void OnNetworkInactive(const rtc::Network* network);
366
Honghai Zhang351d77b2016-05-20 15:08:29 -0700367 void OnNetworkTypeChanged(const rtc::Network* network);
368
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000369 rtc::Thread* thread_;
370 rtc::PacketSocketFactory* factory_;
371 std::string type_;
372 bool send_retransmit_count_attribute_;
373 rtc::Network* network_;
374 rtc::IPAddress ip_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200375 uint16_t min_port_;
376 uint16_t max_port_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000377 std::string content_name_;
378 int component_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200379 uint32_t generation_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000380 // In order to establish a connection to this Port (so that real data can be
381 // sent through), the other side must send us a STUN binding request that is
382 // authenticated with this username_fragment and password.
383 // PortAllocatorSession will provide these username_fragment and password.
384 //
385 // Note: we should always use username_fragment() instead of using
386 // |ice_username_fragment_| directly. For the details see the comment on
387 // username_fragment().
388 std::string ice_username_fragment_;
389 std::string password_;
390 std::vector<Candidate> candidates_;
391 AddressMap connections_;
392 int timeout_delay_;
393 bool enable_port_packets_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000394 IceRole ice_role_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200395 uint64_t tiebreaker_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000396 bool shared_socket_;
397 // Information to use when going through a proxy.
398 std::string user_agent_;
399 rtc::ProxyInfo proxy_;
400
honghaize1a0c942016-02-16 14:54:56 -0800401 // A virtual cost perceived by the user, usually based on the network type
402 // (WiFi. vs. Cellular). It takes precedence over the priority when
403 // comparing two connections.
honghaiza0c44ea2016-03-23 16:07:48 -0700404 uint16_t network_cost_;
honghaize1a0c942016-02-16 14:54:56 -0800405
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000406 friend class Connection;
407};
408
409// Represents a communication link between a port on the local client and a
410// port on the remote client.
Honghai Zhangcc411c02016-03-29 17:27:21 -0700411class Connection : public CandidatePairInterface,
412 public rtc::MessageHandler,
413 public sigslot::has_slots<> {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000414 public:
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700415 struct SentPing {
honghaiz34b11eb2016-03-16 08:55:44 -0700416 SentPing(const std::string id, int64_t sent_time)
Peter Boström0c4e06b2015-10-07 12:23:21 +0200417 : id(id), sent_time(sent_time) {}
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700418
419 std::string id;
honghaiz34b11eb2016-03-16 08:55:44 -0700420 int64_t sent_time;
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700421 };
422
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000423 // States are from RFC 5245. http://tools.ietf.org/html/rfc5245#section-5.7.4
424 enum State {
425 STATE_WAITING = 0, // Check has not been performed, Waiting pair on CL.
426 STATE_INPROGRESS, // Check has been sent, transaction is in progress.
427 STATE_SUCCEEDED, // Check already done, produced a successful result.
428 STATE_FAILED // Check for this connection failed.
429 };
430
431 virtual ~Connection();
432
433 // The local port where this connection sends and receives packets.
434 Port* port() { return port_; }
435 const Port* port() const { return port_; }
436
Honghai Zhangcc411c02016-03-29 17:27:21 -0700437 // Implementation of virtual methods in CandidatePairInterface.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000438 // Returns the description of the local port
439 virtual const Candidate& local_candidate() const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000440 // Returns the description of the remote port to which we communicate.
Honghai Zhangcc411c02016-03-29 17:27:21 -0700441 virtual const Candidate& remote_candidate() const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000442
443 // Returns the pair priority.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200444 uint64_t priority() const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000445
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000446 enum WriteState {
447 STATE_WRITABLE = 0, // we have received ping responses recently
448 STATE_WRITE_UNRELIABLE = 1, // we have had a few ping failures
449 STATE_WRITE_INIT = 2, // we have yet to receive a ping response
450 STATE_WRITE_TIMEOUT = 3, // we have had a large number of ping failures
451 };
452
453 WriteState write_state() const { return write_state_; }
454 bool writable() const { return write_state_ == STATE_WRITABLE; }
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700455 bool receiving() const { return receiving_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000456
457 // Determines whether the connection has finished connecting. This can only
458 // be false for TCP connections.
459 bool connected() const { return connected_; }
Honghai Zhang2b342bf2015-09-30 09:51:58 -0700460 bool weak() const { return !(writable() && receiving() && connected()); }
461 bool active() const {
Honghai Zhang2b342bf2015-09-30 09:51:58 -0700462 return write_state_ != STATE_WRITE_TIMEOUT;
463 }
464 // A connection is dead if it can be safely deleted.
honghaiz34b11eb2016-03-16 08:55:44 -0700465 bool dead(int64_t now) const;
honghaiz89374372015-09-24 13:14:47 -0700466
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000467 // Estimate of the round-trip time over this connection.
honghaiz34b11eb2016-03-16 08:55:44 -0700468 int rtt() const { return rtt_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000469
zhihuang5ecf16c2016-06-01 17:09:15 -0700470 ConnectionInfo stats();
471
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000472 sigslot::signal1<Connection*> SignalStateChange;
473
474 // Sent when the connection has decided that it is no longer of value. It
475 // will delete itself immediately after this call.
476 sigslot::signal1<Connection*> SignalDestroyed;
477
478 // The connection can send and receive packets asynchronously. This matches
479 // the interface of AsyncPacketSocket, which may use UDP or TCP under the
480 // covers.
481 virtual int Send(const void* data, size_t size,
482 const rtc::PacketOptions& options) = 0;
483
484 // Error if Send() returns < 0
485 virtual int GetError() = 0;
486
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700487 sigslot::signal4<Connection*, const char*, size_t, const rtc::PacketTime&>
488 SignalReadPacket;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000489
490 sigslot::signal1<Connection*> SignalReadyToSend;
491
492 // Called when a packet is received on this connection.
493 void OnReadPacket(const char* data, size_t size,
494 const rtc::PacketTime& packet_time);
495
496 // Called when the socket is currently able to send.
497 void OnReadyToSend();
498
499 // Called when a connection is determined to be no longer useful to us. We
500 // still keep it around in case the other side wants to use it. But we can
501 // safely stop pinging on it and we can allow it to time out if the other
502 // side stops using it as well.
503 bool pruned() const { return pruned_; }
504 void Prune();
505
506 bool use_candidate_attr() const { return use_candidate_attr_; }
507 void set_use_candidate_attr(bool enable);
508
honghaiz5a3acd82015-08-20 15:53:17 -0700509 bool nominated() const { return nominated_; }
510 void set_nominated(bool nominated) { nominated_ = nominated; }
511
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000512 void set_remote_ice_mode(IceMode mode) {
513 remote_ice_mode_ = mode;
514 }
515
honghaiz34b11eb2016-03-16 08:55:44 -0700516 void set_receiving_timeout(int64_t receiving_timeout_ms) {
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700517 receiving_timeout_ = receiving_timeout_ms;
518 }
519
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000520 // Makes the connection go away.
521 void Destroy();
522
deadbeef376e1232015-11-25 09:00:08 -0800523 // Makes the connection go away, in a failed state.
524 void FailAndDestroy();
525
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000526 // Checks that the state of this connection is up-to-date. The argument is
527 // the current time, which is compared against various timeouts.
honghaiz34b11eb2016-03-16 08:55:44 -0700528 void UpdateState(int64_t now);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000529
530 // Called when this connection should try checking writability again.
honghaiz34b11eb2016-03-16 08:55:44 -0700531 int64_t last_ping_sent() const { return last_ping_sent_; }
532 void Ping(int64_t now);
Peter Thatcher1fe120a2015-06-10 11:33:17 -0700533 void ReceivedPingResponse();
honghaiz34b11eb2016-03-16 08:55:44 -0700534 int64_t last_ping_response_received() const {
Honghai Zhang381b4212015-12-04 12:24:03 -0800535 return last_ping_response_received_;
536 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000537
538 // Called whenever a valid ping is received on this connection. This is
539 // public because the connection intercepts the first ping for us.
honghaiz34b11eb2016-03-16 08:55:44 -0700540 int64_t last_ping_received() const { return last_ping_received_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000541 void ReceivedPing();
honghaiz9b5ee9c2015-11-11 13:19:17 -0800542 // Handles the binding request; sends a response if this is a valid request.
543 void HandleBindingRequest(IceMessage* msg);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000544
545 // Debugging description of this connection
guoweis@webrtc.org8c9ff202014-12-04 07:56:02 +0000546 std::string ToDebugId() const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000547 std::string ToString() const;
548 std::string ToSensitiveString() const;
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700549 // Prints pings_since_last_response_ into a string.
550 void PrintPingsSinceLastResponse(std::string* pings, size_t max);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000551
552 bool reported() const { return reported_; }
553 void set_reported(bool reported) { reported_ = reported;}
554
honghaiz5a3acd82015-08-20 15:53:17 -0700555 // This signal will be fired if this connection is nominated by the
556 // controlling side.
557 sigslot::signal1<Connection*> SignalNominated;
Peter Thatcher54360512015-07-08 11:08:35 -0700558
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000559 // Invoked when Connection receives STUN error response with 487 code.
560 void HandleRoleConflictFromPeer();
561
562 State state() const { return state_; }
563
honghaiz524ecc22016-05-25 12:48:31 -0700564 int num_pings_sent() const { return num_pings_sent_; }
565
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000566 IceMode remote_ice_mode() const { return remote_ice_mode_; }
567
honghaize1a0c942016-02-16 14:54:56 -0800568 uint32_t ComputeNetworkCost() const;
569
Taylor Brandstetter0a1bc532016-04-19 18:03:26 -0700570 // Update the ICE password and/or generation of the remote candidate if a
571 // ufrag in |remote_ice_parameters| matches the candidate's ufrag, and the
572 // candidate's password and/or ufrag has not been set.
573 // |remote_ice_parameters| should be a list of known ICE parameters ordered
574 // by generation.
575 void MaybeSetRemoteIceCredentialsAndGeneration(const std::string& ice_ufrag,
576 const std::string& ice_pwd,
577 int generation);
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000578
579 // If |remote_candidate_| is peer reflexive and is equivalent to
580 // |new_candidate| except the type, update |remote_candidate_| to
581 // |new_candidate|.
582 void MaybeUpdatePeerReflexiveCandidate(const Candidate& new_candidate);
583
Peter Thatcher54360512015-07-08 11:08:35 -0700584 // Returns the last received time of any data, stun request, or stun
585 // response in milliseconds
honghaiz34b11eb2016-03-16 08:55:44 -0700586 int64_t last_received() const;
Peter Thatcher54360512015-07-08 11:08:35 -0700587
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000588 protected:
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700589 enum { MSG_DELETE = 0, MSG_FIRST_AVAILABLE };
590
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000591 // Constructs a new connection to the given remote port.
592 Connection(Port* port, size_t index, const Candidate& candidate);
593
594 // Called back when StunRequestManager has a stun packet to send
595 void OnSendStunPacket(const void* data, size_t size, StunRequest* req);
596
597 // Callbacks from ConnectionRequest
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700598 virtual void OnConnectionRequestResponse(ConnectionRequest* req,
599 StunMessage* response);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000600 void OnConnectionRequestErrorResponse(ConnectionRequest* req,
601 StunMessage* response);
602 void OnConnectionRequestTimeout(ConnectionRequest* req);
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700603 void OnConnectionRequestSent(ConnectionRequest* req);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000604
605 // Changes the state and signals if necessary.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000606 void set_write_state(WriteState value);
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700607 void set_receiving(bool value);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000608 void set_state(State state);
609 void set_connected(bool value);
610
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000611 void OnMessage(rtc::Message *pmsg);
612
613 Port* port_;
614 size_t local_candidate_index_;
615 Candidate remote_candidate_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000616 WriteState write_state_;
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700617 bool receiving_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000618 bool connected_;
619 bool pruned_;
620 // By default |use_candidate_attr_| flag will be true,
honghaiz5a3acd82015-08-20 15:53:17 -0700621 // as we will be using aggressive nomination.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000622 // But when peer is ice-lite, this flag "must" be initialized to false and
623 // turn on when connection becomes "best connection".
624 bool use_candidate_attr_;
honghaiz5a3acd82015-08-20 15:53:17 -0700625 // Whether this connection has been nominated by the controlling side via
626 // the use_candidate attribute.
627 bool nominated_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000628 IceMode remote_ice_mode_;
629 StunRequestManager requests_;
honghaiz34b11eb2016-03-16 08:55:44 -0700630 int rtt_;
631 int64_t last_ping_sent_; // last time we sent a ping to the other side
632 int64_t last_ping_received_; // last time we received a ping from the other
633 // side
634 int64_t last_data_received_;
635 int64_t last_ping_response_received_;
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700636 std::vector<SentPing> pings_since_last_response_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000637
638 rtc::RateTracker recv_rate_tracker_;
639 rtc::RateTracker send_rate_tracker_;
zhihuang5ecf16c2016-06-01 17:09:15 -0700640
641 ConnectionInfo stats_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000642
643 private:
644 void MaybeAddPrflxCandidate(ConnectionRequest* request,
645 StunMessage* response);
646
647 bool reported_;
648 State state_;
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700649 // Time duration to switch from receiving to not receiving.
honghaiz34b11eb2016-03-16 08:55:44 -0700650 int receiving_timeout_;
651 int64_t time_created_ms_;
honghaiz524ecc22016-05-25 12:48:31 -0700652 int num_pings_sent_ = 0;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000653
654 friend class Port;
655 friend class ConnectionRequest;
656};
657
deadbeef376e1232015-11-25 09:00:08 -0800658// ProxyConnection defers all the interesting work to the port.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000659class ProxyConnection : public Connection {
660 public:
deadbeef376e1232015-11-25 09:00:08 -0800661 ProxyConnection(Port* port, size_t index, const Candidate& remote_candidate);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000662
deadbeef376e1232015-11-25 09:00:08 -0800663 int Send(const void* data,
664 size_t size,
665 const rtc::PacketOptions& options) override;
666 int GetError() override { return error_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000667
668 private:
deadbeef376e1232015-11-25 09:00:08 -0800669 int error_ = 0;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000670};
671
672} // namespace cricket
673
674#endif // WEBRTC_P2P_BASE_PORT_H_