blob: 95abe86464931e2d4029e0c5ac2c9770f998cc93 [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>
15#include <set>
16#include <string>
17#include <vector>
18
19#include "webrtc/p2p/base/candidate.h"
20#include "webrtc/p2p/base/packetsocketfactory.h"
21#include "webrtc/p2p/base/portinterface.h"
22#include "webrtc/p2p/base/stun.h"
23#include "webrtc/p2p/base/stunrequest.h"
24#include "webrtc/p2p/base/transport.h"
25#include "webrtc/base/asyncpacketsocket.h"
26#include "webrtc/base/network.h"
27#include "webrtc/base/proxyinfo.h"
28#include "webrtc/base/ratetracker.h"
29#include "webrtc/base/sigslot.h"
30#include "webrtc/base/socketaddress.h"
31#include "webrtc/base/thread.h"
32
33namespace cricket {
34
35class Connection;
36class ConnectionRequest;
37
38extern const char LOCAL_PORT_TYPE[];
39extern const char STUN_PORT_TYPE[];
40extern const char PRFLX_PORT_TYPE[];
41extern const char RELAY_PORT_TYPE[];
42
43extern const char UDP_PROTOCOL_NAME[];
44extern const char TCP_PROTOCOL_NAME[];
45extern const char SSLTCP_PROTOCOL_NAME[];
46
47// RFC 6544, TCP candidate encoding rules.
48extern const int DISCARD_PORT;
49extern const char TCPTYPE_ACTIVE_STR[];
50extern const char TCPTYPE_PASSIVE_STR[];
51extern const char TCPTYPE_SIMOPEN_STR[];
52
Honghai Zhang2b342bf2015-09-30 09:51:58 -070053// The minimum time we will wait before destroying a connection after creating
54// it.
Peter Boström0c4e06b2015-10-07 12:23:21 +020055const uint32_t MIN_CONNECTION_LIFETIME = 10 * 1000; // 10 seconds.
Peter Thatcher04ac81f2015-09-21 11:48:28 -070056
Honghai Zhang2cd7afe2015-11-12 11:14:33 -080057// A connection will be declared dead if it has not received anything for this
58// long.
59const uint32_t DEAD_CONNECTION_RECEIVE_TIMEOUT = 30 * 1000; // 30 seconds.
60
Peter Thatcher04ac81f2015-09-21 11:48:28 -070061// The timeout duration when a connection does not receive anything.
Peter Boström0c4e06b2015-10-07 12:23:21 +020062const uint32_t WEAK_CONNECTION_RECEIVE_TIMEOUT = 2500; // 2.5 seconds
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000063
64// The length of time we wait before timing out writability on a connection.
Peter Boström0c4e06b2015-10-07 12:23:21 +020065const uint32_t CONNECTION_WRITE_TIMEOUT = 15 * 1000; // 15 seconds
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000066
67// The length of time we wait before we become unwritable.
Peter Boström0c4e06b2015-10-07 12:23:21 +020068const uint32_t CONNECTION_WRITE_CONNECT_TIMEOUT = 5 * 1000; // 5 seconds
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000069
70// The number of pings that must fail to respond before we become unwritable.
Peter Boström0c4e06b2015-10-07 12:23:21 +020071const uint32_t CONNECTION_WRITE_CONNECT_FAILURES = 5;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000072
73// This is the length of time that we wait for a ping response to come back.
74const int CONNECTION_RESPONSE_TIMEOUT = 5 * 1000; // 5 seconds
75
76enum RelayType {
77 RELAY_GTURN, // Legacy google relay service.
78 RELAY_TURN // Standard (TURN) relay service.
79};
80
81enum IcePriorityValue {
82 // The reason we are choosing Relay preference 2 is because, we can run
83 // Relay from client to server on UDP/TCP/TLS. To distinguish the transport
84 // protocol, we prefer UDP over TCP over TLS.
85 // For UDP ICE_TYPE_PREFERENCE_RELAY will be 2.
86 // For TCP ICE_TYPE_PREFERENCE_RELAY will be 1.
87 // For TLS ICE_TYPE_PREFERENCE_RELAY will be 0.
88 // Check turnport.cc for setting these values.
89 ICE_TYPE_PREFERENCE_RELAY = 2,
90 ICE_TYPE_PREFERENCE_HOST_TCP = 90,
91 ICE_TYPE_PREFERENCE_SRFLX = 100,
92 ICE_TYPE_PREFERENCE_PRFLX = 110,
93 ICE_TYPE_PREFERENCE_HOST = 126
94};
95
96const char* ProtoToString(ProtocolType proto);
97bool StringToProto(const char* value, ProtocolType* proto);
98
99struct ProtocolAddress {
100 rtc::SocketAddress address;
101 ProtocolType proto;
102 bool secure;
103
104 ProtocolAddress(const rtc::SocketAddress& a, ProtocolType p)
105 : address(a), proto(p), secure(false) { }
106 ProtocolAddress(const rtc::SocketAddress& a, ProtocolType p, bool sec)
107 : address(a), proto(p), secure(sec) { }
108};
109
110typedef std::set<rtc::SocketAddress> ServerAddresses;
111
112// Represents a local communication mechanism that can be used to create
113// connections to similar mechanisms of the other client. Subclasses of this
114// one add support for specific mechanisms like local UDP ports.
115class Port : public PortInterface, public rtc::MessageHandler,
116 public sigslot::has_slots<> {
117 public:
pkasting@chromium.org332331f2014-11-06 20:19:22 +0000118 Port(rtc::Thread* thread,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000119 rtc::PacketSocketFactory* factory,
pkasting@chromium.org332331f2014-11-06 20:19:22 +0000120 rtc::Network* network,
121 const rtc::IPAddress& ip,
122 const std::string& username_fragment,
123 const std::string& password);
124 Port(rtc::Thread* thread,
125 const std::string& type,
126 rtc::PacketSocketFactory* factory,
127 rtc::Network* network,
128 const rtc::IPAddress& ip,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200129 uint16_t min_port,
130 uint16_t max_port,
pkasting@chromium.org332331f2014-11-06 20:19:22 +0000131 const std::string& username_fragment,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000132 const std::string& password);
133 virtual ~Port();
134
135 virtual const std::string& Type() const { return type_; }
136 virtual rtc::Network* Network() const { return network_; }
137
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000138 // Methods to set/get ICE role and tiebreaker values.
139 IceRole GetIceRole() const { return ice_role_; }
140 void SetIceRole(IceRole role) { ice_role_ = role; }
141
Peter Boström0c4e06b2015-10-07 12:23:21 +0200142 void SetIceTiebreaker(uint64_t tiebreaker) { tiebreaker_ = tiebreaker; }
143 uint64_t IceTiebreaker() const { return tiebreaker_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000144
145 virtual bool SharedSocket() const { return shared_socket_; }
146 void ResetSharedSocket() { shared_socket_ = false; }
147
148 // The thread on which this port performs its I/O.
149 rtc::Thread* thread() { return thread_; }
150
151 // The factory used to create the sockets of this port.
152 rtc::PacketSocketFactory* socket_factory() const { return factory_; }
153 void set_socket_factory(rtc::PacketSocketFactory* factory) {
154 factory_ = factory;
155 }
156
157 // For debugging purposes.
158 const std::string& content_name() const { return content_name_; }
159 void set_content_name(const std::string& content_name) {
160 content_name_ = content_name;
161 }
162
163 int component() const { return component_; }
164 void set_component(int component) { component_ = component; }
165
166 bool send_retransmit_count_attribute() const {
167 return send_retransmit_count_attribute_;
168 }
169 void set_send_retransmit_count_attribute(bool enable) {
170 send_retransmit_count_attribute_ = enable;
171 }
172
173 // Identifies the generation that this port was created in.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200174 uint32_t generation() { return generation_; }
175 void set_generation(uint32_t generation) { generation_ = generation; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000176
177 // ICE requires a single username/password per content/media line. So the
178 // |ice_username_fragment_| of the ports that belongs to the same content will
179 // be the same. However this causes a small complication with our relay
180 // server, which expects different username for RTP and RTCP.
181 //
182 // To resolve this problem, we implemented the username_fragment(),
183 // which returns a different username (calculated from
184 // |ice_username_fragment_|) for RTCP in the case of ICEPROTO_GOOGLE. And the
185 // username_fragment() simply returns |ice_username_fragment_| when running
186 // in ICEPROTO_RFC5245.
187 //
188 // As a result the ICEPROTO_GOOGLE will use different usernames for RTP and
189 // RTCP. And the ICEPROTO_RFC5245 will use same username for both RTP and
190 // RTCP.
191 const std::string username_fragment() const;
192 const std::string& password() const { return password_; }
193
194 // 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
Peter Boström0c4e06b2015-10-07 12:23:21 +0200296 void set_candidate_filter(uint32_t candidate_filter) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000297 candidate_filter_ = candidate_filter;
298 }
honghaize1a0c942016-02-16 14:54:56 -0800299 int32_t network_cost() const { return network_cost_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000300
301 protected:
302 enum {
honghaizd0b31432015-09-30 12:42:17 -0700303 MSG_DEAD = 0,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000304 MSG_FIRST_AVAILABLE
305 };
306
307 void set_type(const std::string& type) { type_ = type; }
308
309 void AddAddress(const rtc::SocketAddress& address,
310 const rtc::SocketAddress& base_address,
311 const rtc::SocketAddress& related_address,
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700312 const std::string& protocol,
313 const std::string& relay_protocol,
314 const std::string& tcptype,
315 const std::string& type,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200316 uint32_t type_preference,
317 uint32_t relay_preference,
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700318 bool final);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000319
320 // Adds the given connection to the list. (Deleting removes them.)
321 void AddConnection(Connection* conn);
322
323 // Called when a packet is received from an unknown address that is not
324 // currently a connection. If this is an authenticated STUN binding request,
325 // then we will signal the client.
326 void OnReadPacket(const char* data, size_t size,
327 const rtc::SocketAddress& addr,
328 ProtocolType proto);
329
330 // If the given data comprises a complete and correct STUN message then the
331 // return value is true, otherwise false. If the message username corresponds
332 // with this port's username fragment, msg will contain the parsed STUN
333 // message. Otherwise, the function may send a STUN response internally.
334 // remote_username contains the remote fragment of the STUN username.
335 bool GetStunMessage(const char* data, size_t size,
336 const rtc::SocketAddress& addr,
337 IceMessage** out_msg, std::string* out_username);
338
339 // Checks if the address in addr is compatible with the port's ip.
340 bool IsCompatibleAddress(const rtc::SocketAddress& addr);
341
342 // Returns default DSCP value.
343 rtc::DiffServCodePoint DefaultDscpValue() const {
344 // No change from what MediaChannel set.
345 return rtc::DSCP_NO_CHANGE;
346 }
347
Peter Boström0c4e06b2015-10-07 12:23:21 +0200348 uint32_t candidate_filter() { return candidate_filter_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000349
350 private:
351 void Construct();
352 // Called when one of our connections deletes itself.
353 void OnConnectionDestroyed(Connection* conn);
354
honghaizd0b31432015-09-30 12:42:17 -0700355 // Whether this port is dead, and hence, should be destroyed on the controlled
356 // side.
357 bool dead() const {
358 return ice_role_ == ICEROLE_CONTROLLED && connections_.empty();
359 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000360
361 rtc::Thread* thread_;
362 rtc::PacketSocketFactory* factory_;
363 std::string type_;
364 bool send_retransmit_count_attribute_;
365 rtc::Network* network_;
366 rtc::IPAddress ip_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200367 uint16_t min_port_;
368 uint16_t max_port_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000369 std::string content_name_;
370 int component_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200371 uint32_t generation_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000372 // In order to establish a connection to this Port (so that real data can be
373 // sent through), the other side must send us a STUN binding request that is
374 // authenticated with this username_fragment and password.
375 // PortAllocatorSession will provide these username_fragment and password.
376 //
377 // Note: we should always use username_fragment() instead of using
378 // |ice_username_fragment_| directly. For the details see the comment on
379 // username_fragment().
380 std::string ice_username_fragment_;
381 std::string password_;
382 std::vector<Candidate> candidates_;
383 AddressMap connections_;
384 int timeout_delay_;
385 bool enable_port_packets_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000386 IceRole ice_role_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200387 uint64_t tiebreaker_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000388 bool shared_socket_;
389 // Information to use when going through a proxy.
390 std::string user_agent_;
391 rtc::ProxyInfo proxy_;
392
393 // Candidate filter is pushed down to Port such that each Port could
394 // make its own decision on how to create candidates. For example,
395 // when IceTransportsType is set to relay, both RelayPort and
396 // TurnPort will hide raddr to avoid local address leakage.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200397 uint32_t candidate_filter_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000398
honghaize1a0c942016-02-16 14:54:56 -0800399 // A virtual cost perceived by the user, usually based on the network type
400 // (WiFi. vs. Cellular). It takes precedence over the priority when
401 // comparing two connections.
402 uint32_t network_cost_;
403
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000404 friend class Connection;
405};
406
407// Represents a communication link between a port on the local client and a
408// port on the remote client.
409class Connection : public rtc::MessageHandler,
410 public sigslot::has_slots<> {
411 public:
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700412 struct SentPing {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200413 SentPing(const std::string id, uint32_t sent_time)
414 : id(id), sent_time(sent_time) {}
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700415
416 std::string id;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200417 uint32_t sent_time;
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700418 };
419
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000420 // States are from RFC 5245. http://tools.ietf.org/html/rfc5245#section-5.7.4
421 enum State {
422 STATE_WAITING = 0, // Check has not been performed, Waiting pair on CL.
423 STATE_INPROGRESS, // Check has been sent, transaction is in progress.
424 STATE_SUCCEEDED, // Check already done, produced a successful result.
425 STATE_FAILED // Check for this connection failed.
426 };
427
428 virtual ~Connection();
429
430 // The local port where this connection sends and receives packets.
431 Port* port() { return port_; }
432 const Port* port() const { return port_; }
433
434 // Returns the description of the local port
435 virtual const Candidate& local_candidate() const;
436
437 // Returns the description of the remote port to which we communicate.
438 const Candidate& remote_candidate() const { return remote_candidate_; }
439
440 // Returns the pair priority.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200441 uint64_t priority() const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000442
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000443 enum WriteState {
444 STATE_WRITABLE = 0, // we have received ping responses recently
445 STATE_WRITE_UNRELIABLE = 1, // we have had a few ping failures
446 STATE_WRITE_INIT = 2, // we have yet to receive a ping response
447 STATE_WRITE_TIMEOUT = 3, // we have had a large number of ping failures
448 };
449
450 WriteState write_state() const { return write_state_; }
451 bool writable() const { return write_state_ == STATE_WRITABLE; }
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700452 bool receiving() const { return receiving_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000453
454 // Determines whether the connection has finished connecting. This can only
455 // be false for TCP connections.
456 bool connected() const { return connected_; }
Honghai Zhang2b342bf2015-09-30 09:51:58 -0700457 bool weak() const { return !(writable() && receiving() && connected()); }
458 bool active() const {
Honghai Zhang2b342bf2015-09-30 09:51:58 -0700459 return write_state_ != STATE_WRITE_TIMEOUT;
460 }
461 // A connection is dead if it can be safely deleted.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200462 bool dead(uint32_t now) const;
honghaiz89374372015-09-24 13:14:47 -0700463
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000464 // Estimate of the round-trip time over this connection.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200465 uint32_t rtt() const { return rtt_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000466
467 size_t sent_total_bytes();
468 size_t sent_bytes_second();
guoweis@webrtc.org930e0042014-11-17 19:42:14 +0000469 // Used to track how many packets are discarded in the application socket due
470 // to errors.
471 size_t sent_discarded_packets();
472 size_t sent_total_packets();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000473 size_t recv_total_bytes();
474 size_t recv_bytes_second();
475 sigslot::signal1<Connection*> SignalStateChange;
476
477 // Sent when the connection has decided that it is no longer of value. It
478 // will delete itself immediately after this call.
479 sigslot::signal1<Connection*> SignalDestroyed;
480
481 // The connection can send and receive packets asynchronously. This matches
482 // the interface of AsyncPacketSocket, which may use UDP or TCP under the
483 // covers.
484 virtual int Send(const void* data, size_t size,
485 const rtc::PacketOptions& options) = 0;
486
487 // Error if Send() returns < 0
488 virtual int GetError() = 0;
489
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700490 sigslot::signal4<Connection*, const char*, size_t, const rtc::PacketTime&>
491 SignalReadPacket;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000492
493 sigslot::signal1<Connection*> SignalReadyToSend;
494
495 // Called when a packet is received on this connection.
496 void OnReadPacket(const char* data, size_t size,
497 const rtc::PacketTime& packet_time);
498
499 // Called when the socket is currently able to send.
500 void OnReadyToSend();
501
502 // Called when a connection is determined to be no longer useful to us. We
503 // still keep it around in case the other side wants to use it. But we can
504 // safely stop pinging on it and we can allow it to time out if the other
505 // side stops using it as well.
506 bool pruned() const { return pruned_; }
507 void Prune();
508
509 bool use_candidate_attr() const { return use_candidate_attr_; }
510 void set_use_candidate_attr(bool enable);
511
honghaiz5a3acd82015-08-20 15:53:17 -0700512 bool nominated() const { return nominated_; }
513 void set_nominated(bool nominated) { nominated_ = nominated; }
514
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000515 void set_remote_ice_mode(IceMode mode) {
516 remote_ice_mode_ = mode;
517 }
518
Peter Boström0c4e06b2015-10-07 12:23:21 +0200519 void set_receiving_timeout(uint32_t receiving_timeout_ms) {
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700520 receiving_timeout_ = receiving_timeout_ms;
521 }
522
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000523 // Makes the connection go away.
524 void Destroy();
525
deadbeef376e1232015-11-25 09:00:08 -0800526 // Makes the connection go away, in a failed state.
527 void FailAndDestroy();
528
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000529 // Checks that the state of this connection is up-to-date. The argument is
530 // the current time, which is compared against various timeouts.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200531 void UpdateState(uint32_t now);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000532
533 // Called when this connection should try checking writability again.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200534 uint32_t last_ping_sent() const { return last_ping_sent_; }
535 void Ping(uint32_t now);
Peter Thatcher1fe120a2015-06-10 11:33:17 -0700536 void ReceivedPingResponse();
Honghai Zhang381b4212015-12-04 12:24:03 -0800537 uint32_t last_ping_response_received() const {
538 return last_ping_response_received_;
539 }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000540
541 // Called whenever a valid ping is received on this connection. This is
542 // public because the connection intercepts the first ping for us.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200543 uint32_t last_ping_received() const { return last_ping_received_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000544 void ReceivedPing();
honghaiz9b5ee9c2015-11-11 13:19:17 -0800545 // Handles the binding request; sends a response if this is a valid request.
546 void HandleBindingRequest(IceMessage* msg);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000547
548 // Debugging description of this connection
guoweis@webrtc.org8c9ff202014-12-04 07:56:02 +0000549 std::string ToDebugId() const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000550 std::string ToString() const;
551 std::string ToSensitiveString() const;
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700552 // Prints pings_since_last_response_ into a string.
553 void PrintPingsSinceLastResponse(std::string* pings, size_t max);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000554
555 bool reported() const { return reported_; }
556 void set_reported(bool reported) { reported_ = reported;}
557
honghaiz5a3acd82015-08-20 15:53:17 -0700558 // This signal will be fired if this connection is nominated by the
559 // controlling side.
560 sigslot::signal1<Connection*> SignalNominated;
Peter Thatcher54360512015-07-08 11:08:35 -0700561
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000562 // Invoked when Connection receives STUN error response with 487 code.
563 void HandleRoleConflictFromPeer();
564
565 State state() const { return state_; }
566
567 IceMode remote_ice_mode() const { return remote_ice_mode_; }
568
honghaize1a0c942016-02-16 14:54:56 -0800569 uint32_t ComputeNetworkCost() const;
570
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000571 // Update the ICE password of the remote candidate if |ice_ufrag| matches
572 // the candidate's ufrag, and the candidate's passwrod has not been set.
573 void MaybeSetRemoteIceCredentials(const std::string& ice_ufrag,
574 const std::string& ice_pwd);
575
576 // If |remote_candidate_| is peer reflexive and is equivalent to
577 // |new_candidate| except the type, update |remote_candidate_| to
578 // |new_candidate|.
579 void MaybeUpdatePeerReflexiveCandidate(const Candidate& new_candidate);
580
Peter Thatcher54360512015-07-08 11:08:35 -0700581 // Returns the last received time of any data, stun request, or stun
582 // response in milliseconds
Honghai Zhang2cd7afe2015-11-12 11:14:33 -0800583 uint32_t last_received() const;
Peter Thatcher54360512015-07-08 11:08:35 -0700584
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000585 protected:
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700586 enum { MSG_DELETE = 0, MSG_FIRST_AVAILABLE };
587
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000588 // Constructs a new connection to the given remote port.
589 Connection(Port* port, size_t index, const Candidate& candidate);
590
591 // Called back when StunRequestManager has a stun packet to send
592 void OnSendStunPacket(const void* data, size_t size, StunRequest* req);
593
594 // Callbacks from ConnectionRequest
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700595 virtual void OnConnectionRequestResponse(ConnectionRequest* req,
596 StunMessage* response);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000597 void OnConnectionRequestErrorResponse(ConnectionRequest* req,
598 StunMessage* response);
599 void OnConnectionRequestTimeout(ConnectionRequest* req);
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700600 void OnConnectionRequestSent(ConnectionRequest* req);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000601
602 // Changes the state and signals if necessary.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000603 void set_write_state(WriteState value);
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700604 void set_receiving(bool value);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000605 void set_state(State state);
606 void set_connected(bool value);
607
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000608 void OnMessage(rtc::Message *pmsg);
609
610 Port* port_;
611 size_t local_candidate_index_;
612 Candidate remote_candidate_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000613 WriteState write_state_;
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700614 bool receiving_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000615 bool connected_;
616 bool pruned_;
617 // By default |use_candidate_attr_| flag will be true,
honghaiz5a3acd82015-08-20 15:53:17 -0700618 // as we will be using aggressive nomination.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000619 // But when peer is ice-lite, this flag "must" be initialized to false and
620 // turn on when connection becomes "best connection".
621 bool use_candidate_attr_;
honghaiz5a3acd82015-08-20 15:53:17 -0700622 // Whether this connection has been nominated by the controlling side via
623 // the use_candidate attribute.
624 bool nominated_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000625 IceMode remote_ice_mode_;
626 StunRequestManager requests_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200627 uint32_t rtt_;
628 uint32_t last_ping_sent_; // last time we sent a ping to the other side
629 uint32_t last_ping_received_; // last time we received a ping from the other
630 // side
631 uint32_t last_data_received_;
632 uint32_t last_ping_response_received_;
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700633 std::vector<SentPing> pings_since_last_response_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000634
635 rtc::RateTracker recv_rate_tracker_;
636 rtc::RateTracker send_rate_tracker_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200637 uint32_t sent_packets_discarded_;
638 uint32_t sent_packets_total_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000639
640 private:
641 void MaybeAddPrflxCandidate(ConnectionRequest* request,
642 StunMessage* response);
643
644 bool reported_;
645 State state_;
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700646 // Time duration to switch from receiving to not receiving.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200647 uint32_t receiving_timeout_;
648 uint32_t time_created_ms_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000649
650 friend class Port;
651 friend class ConnectionRequest;
652};
653
deadbeef376e1232015-11-25 09:00:08 -0800654// ProxyConnection defers all the interesting work to the port.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000655class ProxyConnection : public Connection {
656 public:
deadbeef376e1232015-11-25 09:00:08 -0800657 ProxyConnection(Port* port, size_t index, const Candidate& remote_candidate);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000658
deadbeef376e1232015-11-25 09:00:08 -0800659 int Send(const void* data,
660 size_t size,
661 const rtc::PacketOptions& options) override;
662 int GetError() override { return error_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000663
664 private:
deadbeef376e1232015-11-25 09:00:08 -0800665 int error_ = 0;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000666};
667
668} // namespace cricket
669
670#endif // WEBRTC_P2P_BASE_PORT_H_