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