blob: 4616963aa0a998ced683c6212de84d7e51145c49 [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.
55const uint32 MIN_CONNECTION_LIFETIME = 10 * 1000; // 10 seconds.
Peter Thatcher04ac81f2015-09-21 11:48:28 -070056
57// The timeout duration when a connection does not receive anything.
58const uint32 WEAK_CONNECTION_RECEIVE_TIMEOUT = 2500; // 2.5 seconds
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000059
60// The length of time we wait before timing out writability on a connection.
61const uint32 CONNECTION_WRITE_TIMEOUT = 15 * 1000; // 15 seconds
62
63// The length of time we wait before we become unwritable.
64const uint32 CONNECTION_WRITE_CONNECT_TIMEOUT = 5 * 1000; // 5 seconds
65
66// The number of pings that must fail to respond before we become unwritable.
67const uint32 CONNECTION_WRITE_CONNECT_FAILURES = 5;
68
69// This is the length of time that we wait for a ping response to come back.
70const int CONNECTION_RESPONSE_TIMEOUT = 5 * 1000; // 5 seconds
71
72enum RelayType {
73 RELAY_GTURN, // Legacy google relay service.
74 RELAY_TURN // Standard (TURN) relay service.
75};
76
77enum IcePriorityValue {
78 // The reason we are choosing Relay preference 2 is because, we can run
79 // Relay from client to server on UDP/TCP/TLS. To distinguish the transport
80 // protocol, we prefer UDP over TCP over TLS.
81 // For UDP ICE_TYPE_PREFERENCE_RELAY will be 2.
82 // For TCP ICE_TYPE_PREFERENCE_RELAY will be 1.
83 // For TLS ICE_TYPE_PREFERENCE_RELAY will be 0.
84 // Check turnport.cc for setting these values.
85 ICE_TYPE_PREFERENCE_RELAY = 2,
86 ICE_TYPE_PREFERENCE_HOST_TCP = 90,
87 ICE_TYPE_PREFERENCE_SRFLX = 100,
88 ICE_TYPE_PREFERENCE_PRFLX = 110,
89 ICE_TYPE_PREFERENCE_HOST = 126
90};
91
92const char* ProtoToString(ProtocolType proto);
93bool StringToProto(const char* value, ProtocolType* proto);
94
95struct ProtocolAddress {
96 rtc::SocketAddress address;
97 ProtocolType proto;
98 bool secure;
99
100 ProtocolAddress(const rtc::SocketAddress& a, ProtocolType p)
101 : address(a), proto(p), secure(false) { }
102 ProtocolAddress(const rtc::SocketAddress& a, ProtocolType p, bool sec)
103 : address(a), proto(p), secure(sec) { }
104};
105
106typedef std::set<rtc::SocketAddress> ServerAddresses;
107
108// Represents a local communication mechanism that can be used to create
109// connections to similar mechanisms of the other client. Subclasses of this
110// one add support for specific mechanisms like local UDP ports.
111class Port : public PortInterface, public rtc::MessageHandler,
112 public sigslot::has_slots<> {
113 public:
pkasting@chromium.org332331f2014-11-06 20:19:22 +0000114 Port(rtc::Thread* thread,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000115 rtc::PacketSocketFactory* factory,
pkasting@chromium.org332331f2014-11-06 20:19:22 +0000116 rtc::Network* network,
117 const rtc::IPAddress& ip,
118 const std::string& username_fragment,
119 const std::string& password);
120 Port(rtc::Thread* thread,
121 const std::string& type,
122 rtc::PacketSocketFactory* factory,
123 rtc::Network* network,
124 const rtc::IPAddress& ip,
125 uint16 min_port,
126 uint16 max_port,
127 const std::string& username_fragment,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000128 const std::string& password);
129 virtual ~Port();
130
131 virtual const std::string& Type() const { return type_; }
132 virtual rtc::Network* Network() const { return network_; }
133
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000134 // Methods to set/get ICE role and tiebreaker values.
135 IceRole GetIceRole() const { return ice_role_; }
136 void SetIceRole(IceRole role) { ice_role_ = role; }
137
138 void SetIceTiebreaker(uint64 tiebreaker) { tiebreaker_ = tiebreaker; }
139 uint64 IceTiebreaker() const { return tiebreaker_; }
140
141 virtual bool SharedSocket() const { return shared_socket_; }
142 void ResetSharedSocket() { shared_socket_ = false; }
143
144 // The thread on which this port performs its I/O.
145 rtc::Thread* thread() { return thread_; }
146
147 // The factory used to create the sockets of this port.
148 rtc::PacketSocketFactory* socket_factory() const { return factory_; }
149 void set_socket_factory(rtc::PacketSocketFactory* factory) {
150 factory_ = factory;
151 }
152
153 // For debugging purposes.
154 const std::string& content_name() const { return content_name_; }
155 void set_content_name(const std::string& content_name) {
156 content_name_ = content_name;
157 }
158
159 int component() const { return component_; }
160 void set_component(int component) { component_ = component; }
161
162 bool send_retransmit_count_attribute() const {
163 return send_retransmit_count_attribute_;
164 }
165 void set_send_retransmit_count_attribute(bool enable) {
166 send_retransmit_count_attribute_ = enable;
167 }
168
169 // Identifies the generation that this port was created in.
170 uint32 generation() { return generation_; }
171 void set_generation(uint32 generation) { generation_ = generation; }
172
173 // ICE requires a single username/password per content/media line. So the
174 // |ice_username_fragment_| of the ports that belongs to the same content will
175 // be the same. However this causes a small complication with our relay
176 // server, which expects different username for RTP and RTCP.
177 //
178 // To resolve this problem, we implemented the username_fragment(),
179 // which returns a different username (calculated from
180 // |ice_username_fragment_|) for RTCP in the case of ICEPROTO_GOOGLE. And the
181 // username_fragment() simply returns |ice_username_fragment_| when running
182 // in ICEPROTO_RFC5245.
183 //
184 // As a result the ICEPROTO_GOOGLE will use different usernames for RTP and
185 // RTCP. And the ICEPROTO_RFC5245 will use same username for both RTP and
186 // RTCP.
187 const std::string username_fragment() const;
188 const std::string& password() const { return password_; }
189
190 // Fired when candidates are discovered by the port. When all candidates
191 // are discovered that belong to port SignalAddressReady is fired.
192 sigslot::signal2<Port*, const Candidate&> SignalCandidateReady;
193
194 // Provides all of the above information in one handy object.
195 virtual const std::vector<Candidate>& Candidates() const {
196 return candidates_;
197 }
198
199 // SignalPortComplete is sent when port completes the task of candidates
200 // allocation.
201 sigslot::signal1<Port*> SignalPortComplete;
202 // This signal sent when port fails to allocate candidates and this port
203 // can't be used in establishing the connections. When port is in shared mode
204 // and port fails to allocate one of the candidates, port shouldn't send
205 // this signal as other candidates might be usefull in establishing the
206 // connection.
207 sigslot::signal1<Port*> SignalPortError;
208
209 // Returns a map containing all of the connections of this port, keyed by the
210 // remote address.
211 typedef std::map<rtc::SocketAddress, Connection*> AddressMap;
212 const AddressMap& connections() { return connections_; }
213
214 // Returns the connection to the given address or NULL if none exists.
215 virtual Connection* GetConnection(
216 const rtc::SocketAddress& remote_addr);
217
218 // Called each time a connection is created.
219 sigslot::signal2<Port*, Connection*> SignalConnectionCreated;
220
221 // In a shared socket mode each port which shares the socket will decide
222 // to accept the packet based on the |remote_addr|. Currently only UDP
223 // port implemented this method.
224 // TODO(mallinath) - Make it pure virtual.
225 virtual bool HandleIncomingPacket(
226 rtc::AsyncPacketSocket* socket, const char* data, size_t size,
227 const rtc::SocketAddress& remote_addr,
228 const rtc::PacketTime& packet_time) {
229 ASSERT(false);
230 return false;
231 }
232
233 // Sends a response message (normal or error) to the given request. One of
234 // these methods should be called as a response to SignalUnknownAddress.
235 // NOTE: You MUST call CreateConnection BEFORE SendBindingResponse.
236 virtual void SendBindingResponse(StunMessage* request,
237 const rtc::SocketAddress& addr);
238 virtual void SendBindingErrorResponse(
239 StunMessage* request, const rtc::SocketAddress& addr,
240 int error_code, const std::string& reason);
241
242 void set_proxy(const std::string& user_agent,
243 const rtc::ProxyInfo& proxy) {
244 user_agent_ = user_agent;
245 proxy_ = proxy;
246 }
247 const std::string& user_agent() { return user_agent_; }
248 const rtc::ProxyInfo& proxy() { return proxy_; }
249
250 virtual void EnablePortPackets();
251
252 // Called if the port has no connections and is no longer useful.
253 void Destroy();
254
255 virtual void OnMessage(rtc::Message *pmsg);
256
257 // Debugging description of this port
258 virtual std::string ToString() const;
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000259 const rtc::IPAddress& ip() const { return ip_; }
pkasting@chromium.org332331f2014-11-06 20:19:22 +0000260 uint16 min_port() { return min_port_; }
261 uint16 max_port() { return max_port_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000262
263 // Timeout shortening function to speed up unit tests.
264 void set_timeout_delay(int delay) { timeout_delay_ = delay; }
265
266 // This method will return local and remote username fragements from the
267 // stun username attribute if present.
268 bool ParseStunUsername(const StunMessage* stun_msg,
269 std::string* local_username,
Peter Thatcher7cbd1882015-09-17 18:54:52 -0700270 std::string* remote_username) const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000271 void CreateStunUsername(const std::string& remote_username,
272 std::string* stun_username_attr_str) const;
273
274 bool MaybeIceRoleConflict(const rtc::SocketAddress& addr,
275 IceMessage* stun_msg,
276 const std::string& remote_ufrag);
277
278 // Called when the socket is currently able to send.
279 void OnReadyToSend();
280
281 // Called when the Connection discovers a local peer reflexive candidate.
282 // Returns the index of the new local candidate.
283 size_t AddPrflxCandidate(const Candidate& local);
284
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000285 void set_candidate_filter(uint32 candidate_filter) {
286 candidate_filter_ = candidate_filter;
287 }
288
289 protected:
290 enum {
291 MSG_CHECKTIMEOUT = 0,
292 MSG_FIRST_AVAILABLE
293 };
294
295 void set_type(const std::string& type) { type_ = type; }
296
297 void AddAddress(const rtc::SocketAddress& address,
298 const rtc::SocketAddress& base_address,
299 const rtc::SocketAddress& related_address,
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700300 const std::string& protocol,
301 const std::string& relay_protocol,
302 const std::string& tcptype,
303 const std::string& type,
304 uint32 type_preference,
305 uint32 relay_preference,
306 bool final);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000307
308 // Adds the given connection to the list. (Deleting removes them.)
309 void AddConnection(Connection* conn);
310
311 // Called when a packet is received from an unknown address that is not
312 // currently a connection. If this is an authenticated STUN binding request,
313 // then we will signal the client.
314 void OnReadPacket(const char* data, size_t size,
315 const rtc::SocketAddress& addr,
316 ProtocolType proto);
317
318 // If the given data comprises a complete and correct STUN message then the
319 // return value is true, otherwise false. If the message username corresponds
320 // with this port's username fragment, msg will contain the parsed STUN
321 // message. Otherwise, the function may send a STUN response internally.
322 // remote_username contains the remote fragment of the STUN username.
323 bool GetStunMessage(const char* data, size_t size,
324 const rtc::SocketAddress& addr,
325 IceMessage** out_msg, std::string* out_username);
326
327 // Checks if the address in addr is compatible with the port's ip.
328 bool IsCompatibleAddress(const rtc::SocketAddress& addr);
329
330 // Returns default DSCP value.
331 rtc::DiffServCodePoint DefaultDscpValue() const {
332 // No change from what MediaChannel set.
333 return rtc::DSCP_NO_CHANGE;
334 }
335
336 uint32 candidate_filter() { return candidate_filter_; }
337
338 private:
339 void Construct();
340 // Called when one of our connections deletes itself.
341 void OnConnectionDestroyed(Connection* conn);
342
343 // Checks if this port is useless, and hence, should be destroyed.
344 void CheckTimeout();
345
346 rtc::Thread* thread_;
347 rtc::PacketSocketFactory* factory_;
348 std::string type_;
349 bool send_retransmit_count_attribute_;
350 rtc::Network* network_;
351 rtc::IPAddress ip_;
pkasting@chromium.org332331f2014-11-06 20:19:22 +0000352 uint16 min_port_;
353 uint16 max_port_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000354 std::string content_name_;
355 int component_;
356 uint32 generation_;
357 // In order to establish a connection to this Port (so that real data can be
358 // sent through), the other side must send us a STUN binding request that is
359 // authenticated with this username_fragment and password.
360 // PortAllocatorSession will provide these username_fragment and password.
361 //
362 // Note: we should always use username_fragment() instead of using
363 // |ice_username_fragment_| directly. For the details see the comment on
364 // username_fragment().
365 std::string ice_username_fragment_;
366 std::string password_;
367 std::vector<Candidate> candidates_;
368 AddressMap connections_;
369 int timeout_delay_;
370 bool enable_port_packets_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000371 IceRole ice_role_;
372 uint64 tiebreaker_;
373 bool shared_socket_;
374 // Information to use when going through a proxy.
375 std::string user_agent_;
376 rtc::ProxyInfo proxy_;
377
378 // Candidate filter is pushed down to Port such that each Port could
379 // make its own decision on how to create candidates. For example,
380 // when IceTransportsType is set to relay, both RelayPort and
381 // TurnPort will hide raddr to avoid local address leakage.
382 uint32 candidate_filter_;
383
384 friend class Connection;
385};
386
387// Represents a communication link between a port on the local client and a
388// port on the remote client.
389class Connection : public rtc::MessageHandler,
390 public sigslot::has_slots<> {
391 public:
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700392 struct SentPing {
393 SentPing(const std::string id, uint32 sent_time)
394 : id(id),
395 sent_time(sent_time) {}
396
397 std::string id;
398 uint32 sent_time;
399 };
400
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000401 // States are from RFC 5245. http://tools.ietf.org/html/rfc5245#section-5.7.4
402 enum State {
403 STATE_WAITING = 0, // Check has not been performed, Waiting pair on CL.
404 STATE_INPROGRESS, // Check has been sent, transaction is in progress.
405 STATE_SUCCEEDED, // Check already done, produced a successful result.
406 STATE_FAILED // Check for this connection failed.
407 };
408
409 virtual ~Connection();
410
411 // The local port where this connection sends and receives packets.
412 Port* port() { return port_; }
413 const Port* port() const { return port_; }
414
415 // Returns the description of the local port
416 virtual const Candidate& local_candidate() const;
417
418 // Returns the description of the remote port to which we communicate.
419 const Candidate& remote_candidate() const { return remote_candidate_; }
420
421 // Returns the pair priority.
422 uint64 priority() const;
423
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000424 enum WriteState {
425 STATE_WRITABLE = 0, // we have received ping responses recently
426 STATE_WRITE_UNRELIABLE = 1, // we have had a few ping failures
427 STATE_WRITE_INIT = 2, // we have yet to receive a ping response
428 STATE_WRITE_TIMEOUT = 3, // we have had a large number of ping failures
429 };
430
431 WriteState write_state() const { return write_state_; }
432 bool writable() const { return write_state_ == STATE_WRITABLE; }
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700433 bool receiving() const { return receiving_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000434
435 // Determines whether the connection has finished connecting. This can only
436 // be false for TCP connections.
437 bool connected() const { return connected_; }
Honghai Zhang2b342bf2015-09-30 09:51:58 -0700438 bool weak() const { return !(writable() && receiving() && connected()); }
439 bool active() const {
440 // TODO(honghaiz): Move from using |write_state_| to using |pruned_|.
441 return write_state_ != STATE_WRITE_TIMEOUT;
442 }
443 // A connection is dead if it can be safely deleted.
444 bool dead(uint32 now) const;
honghaiz89374372015-09-24 13:14:47 -0700445
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000446 // Estimate of the round-trip time over this connection.
447 uint32 rtt() const { return rtt_; }
448
449 size_t sent_total_bytes();
450 size_t sent_bytes_second();
guoweis@webrtc.org930e0042014-11-17 19:42:14 +0000451 // Used to track how many packets are discarded in the application socket due
452 // to errors.
453 size_t sent_discarded_packets();
454 size_t sent_total_packets();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000455 size_t recv_total_bytes();
456 size_t recv_bytes_second();
457 sigslot::signal1<Connection*> SignalStateChange;
458
459 // Sent when the connection has decided that it is no longer of value. It
460 // will delete itself immediately after this call.
461 sigslot::signal1<Connection*> SignalDestroyed;
462
463 // The connection can send and receive packets asynchronously. This matches
464 // the interface of AsyncPacketSocket, which may use UDP or TCP under the
465 // covers.
466 virtual int Send(const void* data, size_t size,
467 const rtc::PacketOptions& options) = 0;
468
469 // Error if Send() returns < 0
470 virtual int GetError() = 0;
471
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700472 sigslot::signal4<Connection*, const char*, size_t, const rtc::PacketTime&>
473 SignalReadPacket;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000474
475 sigslot::signal1<Connection*> SignalReadyToSend;
476
477 // Called when a packet is received on this connection.
478 void OnReadPacket(const char* data, size_t size,
479 const rtc::PacketTime& packet_time);
480
481 // Called when the socket is currently able to send.
482 void OnReadyToSend();
483
484 // Called when a connection is determined to be no longer useful to us. We
485 // still keep it around in case the other side wants to use it. But we can
486 // safely stop pinging on it and we can allow it to time out if the other
487 // side stops using it as well.
488 bool pruned() const { return pruned_; }
489 void Prune();
490
491 bool use_candidate_attr() const { return use_candidate_attr_; }
492 void set_use_candidate_attr(bool enable);
493
honghaiz5a3acd82015-08-20 15:53:17 -0700494 bool nominated() const { return nominated_; }
495 void set_nominated(bool nominated) { nominated_ = nominated; }
496
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000497 void set_remote_ice_mode(IceMode mode) {
498 remote_ice_mode_ = mode;
499 }
500
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700501 void set_receiving_timeout(uint32 receiving_timeout_ms) {
502 receiving_timeout_ = receiving_timeout_ms;
503 }
504
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000505 // Makes the connection go away.
506 void Destroy();
507
508 // Checks that the state of this connection is up-to-date. The argument is
509 // the current time, which is compared against various timeouts.
510 void UpdateState(uint32 now);
511
512 // Called when this connection should try checking writability again.
513 uint32 last_ping_sent() const { return last_ping_sent_; }
514 void Ping(uint32 now);
Peter Thatcher1fe120a2015-06-10 11:33:17 -0700515 void ReceivedPingResponse();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000516
517 // Called whenever a valid ping is received on this connection. This is
518 // public because the connection intercepts the first ping for us.
519 uint32 last_ping_received() const { return last_ping_received_; }
520 void ReceivedPing();
521
522 // Debugging description of this connection
guoweis@webrtc.org8c9ff202014-12-04 07:56:02 +0000523 std::string ToDebugId() const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000524 std::string ToString() const;
525 std::string ToSensitiveString() const;
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700526 // Prints pings_since_last_response_ into a string.
527 void PrintPingsSinceLastResponse(std::string* pings, size_t max);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000528
529 bool reported() const { return reported_; }
530 void set_reported(bool reported) { reported_ = reported;}
531
honghaiz5a3acd82015-08-20 15:53:17 -0700532 // This signal will be fired if this connection is nominated by the
533 // controlling side.
534 sigslot::signal1<Connection*> SignalNominated;
Peter Thatcher54360512015-07-08 11:08:35 -0700535
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000536 // Invoked when Connection receives STUN error response with 487 code.
537 void HandleRoleConflictFromPeer();
538
539 State state() const { return state_; }
540
541 IceMode remote_ice_mode() const { return remote_ice_mode_; }
542
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000543 // Update the ICE password of the remote candidate if |ice_ufrag| matches
544 // the candidate's ufrag, and the candidate's passwrod has not been set.
545 void MaybeSetRemoteIceCredentials(const std::string& ice_ufrag,
546 const std::string& ice_pwd);
547
548 // If |remote_candidate_| is peer reflexive and is equivalent to
549 // |new_candidate| except the type, update |remote_candidate_| to
550 // |new_candidate|.
551 void MaybeUpdatePeerReflexiveCandidate(const Candidate& new_candidate);
552
Peter Thatcher54360512015-07-08 11:08:35 -0700553 // Returns the last received time of any data, stun request, or stun
554 // response in milliseconds
555 uint32 last_received();
556
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000557 protected:
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700558 enum { MSG_DELETE = 0, MSG_FIRST_AVAILABLE };
559
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000560 // Constructs a new connection to the given remote port.
561 Connection(Port* port, size_t index, const Candidate& candidate);
562
563 // Called back when StunRequestManager has a stun packet to send
564 void OnSendStunPacket(const void* data, size_t size, StunRequest* req);
565
566 // Callbacks from ConnectionRequest
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700567 virtual void OnConnectionRequestResponse(ConnectionRequest* req,
568 StunMessage* response);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000569 void OnConnectionRequestErrorResponse(ConnectionRequest* req,
570 StunMessage* response);
571 void OnConnectionRequestTimeout(ConnectionRequest* req);
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700572 void OnConnectionRequestSent(ConnectionRequest* req);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000573
574 // Changes the state and signals if necessary.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000575 void set_write_state(WriteState value);
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700576 void set_receiving(bool value);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000577 void set_state(State state);
578 void set_connected(bool value);
579
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000580 void OnMessage(rtc::Message *pmsg);
581
582 Port* port_;
583 size_t local_candidate_index_;
584 Candidate remote_candidate_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000585 WriteState write_state_;
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700586 bool receiving_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000587 bool connected_;
588 bool pruned_;
589 // By default |use_candidate_attr_| flag will be true,
honghaiz5a3acd82015-08-20 15:53:17 -0700590 // as we will be using aggressive nomination.
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000591 // But when peer is ice-lite, this flag "must" be initialized to false and
592 // turn on when connection becomes "best connection".
593 bool use_candidate_attr_;
honghaiz5a3acd82015-08-20 15:53:17 -0700594 // Whether this connection has been nominated by the controlling side via
595 // the use_candidate attribute.
596 bool nominated_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000597 IceMode remote_ice_mode_;
598 StunRequestManager requests_;
599 uint32 rtt_;
600 uint32 last_ping_sent_; // last time we sent a ping to the other side
601 uint32 last_ping_received_; // last time we received a ping from the other
602 // side
603 uint32 last_data_received_;
604 uint32 last_ping_response_received_;
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700605 std::vector<SentPing> pings_since_last_response_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000606
607 rtc::RateTracker recv_rate_tracker_;
608 rtc::RateTracker send_rate_tracker_;
guoweis@webrtc.org930e0042014-11-17 19:42:14 +0000609 uint32 sent_packets_discarded_;
610 uint32 sent_packets_total_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000611
612 private:
613 void MaybeAddPrflxCandidate(ConnectionRequest* request,
614 StunMessage* response);
615
616 bool reported_;
617 State state_;
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700618 // Time duration to switch from receiving to not receiving.
619 uint32 receiving_timeout_;
Honghai Zhang2b342bf2015-09-30 09:51:58 -0700620 uint32 time_created_ms_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000621
622 friend class Port;
623 friend class ConnectionRequest;
624};
625
626// ProxyConnection defers all the interesting work to the port
627class ProxyConnection : public Connection {
628 public:
629 ProxyConnection(Port* port, size_t index, const Candidate& candidate);
630
631 virtual int Send(const void* data, size_t size,
632 const rtc::PacketOptions& options);
633 virtual int GetError() { return error_; }
634
635 private:
636 int error_;
637};
638
639} // namespace cricket
640
641#endif // WEBRTC_P2P_BASE_PORT_H_