blob: a08e6b602a86ee71916bb362b79fe9c2b502070e [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
pthatcherfa301802015-08-11 04:12:56 -0700130 // This method will set the flag which enables standard ICE/STUN procedures
131 // in STUN connectivity checks. Currently this method does
132 // 1. Add / Verify MI attribute in STUN binding requests.
133 // 2. Username attribute in STUN binding request will be RFRAF:LFRAG,
134 // as opposed to RFRAGLFRAG.
135 virtual void SetIceProtocolType(IceProtocolType protocol) {
136 ice_protocol_ = protocol;
137 }
138 virtual IceProtocolType IceProtocol() const { return ice_protocol_; }
139
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000140 // Methods to set/get ICE role and tiebreaker values.
141 IceRole GetIceRole() const { return ice_role_; }
142 void SetIceRole(IceRole role) { ice_role_ = role; }
143
144 void SetIceTiebreaker(uint64 tiebreaker) { tiebreaker_ = tiebreaker; }
145 uint64 IceTiebreaker() const { return tiebreaker_; }
146
147 virtual bool SharedSocket() const { return shared_socket_; }
148 void ResetSharedSocket() { shared_socket_ = false; }
149
150 // The thread on which this port performs its I/O.
151 rtc::Thread* thread() { return thread_; }
152
153 // The factory used to create the sockets of this port.
154 rtc::PacketSocketFactory* socket_factory() const { return factory_; }
155 void set_socket_factory(rtc::PacketSocketFactory* factory) {
156 factory_ = factory;
157 }
158
159 // For debugging purposes.
160 const std::string& content_name() const { return content_name_; }
161 void set_content_name(const std::string& content_name) {
162 content_name_ = content_name;
163 }
164
165 int component() const { return component_; }
166 void set_component(int component) { component_ = component; }
167
168 bool send_retransmit_count_attribute() const {
169 return send_retransmit_count_attribute_;
170 }
171 void set_send_retransmit_count_attribute(bool enable) {
172 send_retransmit_count_attribute_ = enable;
173 }
174
175 // Identifies the generation that this port was created in.
176 uint32 generation() { return generation_; }
177 void set_generation(uint32 generation) { generation_ = generation; }
178
179 // ICE requires a single username/password per content/media line. So the
180 // |ice_username_fragment_| of the ports that belongs to the same content will
181 // be the same. However this causes a small complication with our relay
182 // server, which expects different username for RTP and RTCP.
183 //
184 // To resolve this problem, we implemented the username_fragment(),
185 // which returns a different username (calculated from
186 // |ice_username_fragment_|) for RTCP in the case of ICEPROTO_GOOGLE. And the
187 // username_fragment() simply returns |ice_username_fragment_| when running
188 // in ICEPROTO_RFC5245.
189 //
190 // As a result the ICEPROTO_GOOGLE will use different usernames for RTP and
191 // RTCP. And the ICEPROTO_RFC5245 will use same username for both RTP and
192 // RTCP.
193 const std::string username_fragment() const;
194 const std::string& password() const { return password_; }
195
196 // Fired when candidates are discovered by the port. When all candidates
197 // are discovered that belong to port SignalAddressReady is fired.
198 sigslot::signal2<Port*, const Candidate&> SignalCandidateReady;
199
200 // Provides all of the above information in one handy object.
201 virtual const std::vector<Candidate>& Candidates() const {
202 return candidates_;
203 }
204
205 // SignalPortComplete is sent when port completes the task of candidates
206 // allocation.
207 sigslot::signal1<Port*> SignalPortComplete;
208 // This signal sent when port fails to allocate candidates and this port
209 // can't be used in establishing the connections. When port is in shared mode
210 // and port fails to allocate one of the candidates, port shouldn't send
211 // this signal as other candidates might be usefull in establishing the
212 // connection.
213 sigslot::signal1<Port*> SignalPortError;
214
215 // Returns a map containing all of the connections of this port, keyed by the
216 // remote address.
217 typedef std::map<rtc::SocketAddress, Connection*> AddressMap;
218 const AddressMap& connections() { return connections_; }
219
220 // Returns the connection to the given address or NULL if none exists.
221 virtual Connection* GetConnection(
222 const rtc::SocketAddress& remote_addr);
223
224 // Called each time a connection is created.
225 sigslot::signal2<Port*, Connection*> SignalConnectionCreated;
226
227 // In a shared socket mode each port which shares the socket will decide
228 // to accept the packet based on the |remote_addr|. Currently only UDP
229 // port implemented this method.
230 // TODO(mallinath) - Make it pure virtual.
231 virtual bool HandleIncomingPacket(
232 rtc::AsyncPacketSocket* socket, const char* data, size_t size,
233 const rtc::SocketAddress& remote_addr,
234 const rtc::PacketTime& packet_time) {
235 ASSERT(false);
236 return false;
237 }
238
239 // Sends a response message (normal or error) to the given request. One of
240 // these methods should be called as a response to SignalUnknownAddress.
241 // NOTE: You MUST call CreateConnection BEFORE SendBindingResponse.
242 virtual void SendBindingResponse(StunMessage* request,
243 const rtc::SocketAddress& addr);
244 virtual void SendBindingErrorResponse(
245 StunMessage* request, const rtc::SocketAddress& addr,
246 int error_code, const std::string& reason);
247
248 void set_proxy(const std::string& user_agent,
249 const rtc::ProxyInfo& proxy) {
250 user_agent_ = user_agent;
251 proxy_ = proxy;
252 }
253 const std::string& user_agent() { return user_agent_; }
254 const rtc::ProxyInfo& proxy() { return proxy_; }
255
256 virtual void EnablePortPackets();
257
258 // Called if the port has no connections and is no longer useful.
259 void Destroy();
260
261 virtual void OnMessage(rtc::Message *pmsg);
262
263 // Debugging description of this port
264 virtual std::string ToString() const;
pthatcher@webrtc.org0ba15332015-01-10 00:47:02 +0000265 const rtc::IPAddress& ip() const { return ip_; }
pkasting@chromium.org332331f2014-11-06 20:19:22 +0000266 uint16 min_port() { return min_port_; }
267 uint16 max_port() { return max_port_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000268
269 // Timeout shortening function to speed up unit tests.
270 void set_timeout_delay(int delay) { timeout_delay_ = delay; }
271
272 // This method will return local and remote username fragements from the
273 // stun username attribute if present.
274 bool ParseStunUsername(const StunMessage* stun_msg,
275 std::string* local_username,
pthatcherfa301802015-08-11 04:12:56 -0700276 std::string* remote_username,
277 IceProtocolType* remote_protocol_type) const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000278 void CreateStunUsername(const std::string& remote_username,
279 std::string* stun_username_attr_str) const;
280
281 bool MaybeIceRoleConflict(const rtc::SocketAddress& addr,
282 IceMessage* stun_msg,
283 const std::string& remote_ufrag);
284
285 // Called when the socket is currently able to send.
286 void OnReadyToSend();
287
288 // Called when the Connection discovers a local peer reflexive candidate.
289 // Returns the index of the new local candidate.
290 size_t AddPrflxCandidate(const Candidate& local);
291
pthatcherfa301802015-08-11 04:12:56 -0700292 // Returns if RFC 5245 ICE protocol is used.
293 bool IsStandardIce() const;
294
295 // Returns if Google ICE protocol is used.
296 bool IsGoogleIce() const;
297
298 // Returns if Hybrid ICE protocol is used.
299 bool IsHybridIce() const;
300
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000301 void set_candidate_filter(uint32 candidate_filter) {
302 candidate_filter_ = candidate_filter;
303 }
304
305 protected:
306 enum {
307 MSG_CHECKTIMEOUT = 0,
308 MSG_FIRST_AVAILABLE
309 };
310
311 void set_type(const std::string& type) { type_ = type; }
312
313 void AddAddress(const rtc::SocketAddress& address,
314 const rtc::SocketAddress& base_address,
315 const rtc::SocketAddress& related_address,
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700316 const std::string& protocol,
317 const std::string& relay_protocol,
318 const std::string& tcptype,
319 const std::string& type,
320 uint32 type_preference,
321 uint32 relay_preference,
322 bool final);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000323
324 // Adds the given connection to the list. (Deleting removes them.)
325 void AddConnection(Connection* conn);
326
327 // Called when a packet is received from an unknown address that is not
328 // currently a connection. If this is an authenticated STUN binding request,
329 // then we will signal the client.
330 void OnReadPacket(const char* data, size_t size,
331 const rtc::SocketAddress& addr,
332 ProtocolType proto);
333
334 // If the given data comprises a complete and correct STUN message then the
335 // return value is true, otherwise false. If the message username corresponds
336 // with this port's username fragment, msg will contain the parsed STUN
337 // message. Otherwise, the function may send a STUN response internally.
338 // remote_username contains the remote fragment of the STUN username.
339 bool GetStunMessage(const char* data, size_t size,
340 const rtc::SocketAddress& addr,
341 IceMessage** out_msg, std::string* out_username);
342
343 // Checks if the address in addr is compatible with the port's ip.
344 bool IsCompatibleAddress(const rtc::SocketAddress& addr);
345
346 // Returns default DSCP value.
347 rtc::DiffServCodePoint DefaultDscpValue() const {
348 // No change from what MediaChannel set.
349 return rtc::DSCP_NO_CHANGE;
350 }
351
352 uint32 candidate_filter() { return candidate_filter_; }
353
354 private:
355 void Construct();
356 // Called when one of our connections deletes itself.
357 void OnConnectionDestroyed(Connection* conn);
358
359 // Checks if this port is useless, and hence, should be destroyed.
360 void CheckTimeout();
361
362 rtc::Thread* thread_;
363 rtc::PacketSocketFactory* factory_;
364 std::string type_;
365 bool send_retransmit_count_attribute_;
366 rtc::Network* network_;
367 rtc::IPAddress ip_;
pkasting@chromium.org332331f2014-11-06 20:19:22 +0000368 uint16 min_port_;
369 uint16 max_port_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000370 std::string content_name_;
371 int component_;
372 uint32 generation_;
373 // In order to establish a connection to this Port (so that real data can be
374 // sent through), the other side must send us a STUN binding request that is
375 // authenticated with this username_fragment and password.
376 // PortAllocatorSession will provide these username_fragment and password.
377 //
378 // Note: we should always use username_fragment() instead of using
379 // |ice_username_fragment_| directly. For the details see the comment on
380 // username_fragment().
381 std::string ice_username_fragment_;
382 std::string password_;
383 std::vector<Candidate> candidates_;
384 AddressMap connections_;
385 int timeout_delay_;
386 bool enable_port_packets_;
pthatcherfa301802015-08-11 04:12:56 -0700387 IceProtocolType ice_protocol_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000388 IceRole ice_role_;
389 uint64 tiebreaker_;
390 bool shared_socket_;
391 // Information to use when going through a proxy.
392 std::string user_agent_;
393 rtc::ProxyInfo proxy_;
394
395 // Candidate filter is pushed down to Port such that each Port could
396 // make its own decision on how to create candidates. For example,
397 // when IceTransportsType is set to relay, both RelayPort and
398 // TurnPort will hide raddr to avoid local address leakage.
399 uint32 candidate_filter_;
400
401 friend class Connection;
402};
403
404// Represents a communication link between a port on the local client and a
405// port on the remote client.
406class Connection : public rtc::MessageHandler,
407 public sigslot::has_slots<> {
408 public:
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700409 struct SentPing {
410 SentPing(const std::string id, uint32 sent_time)
411 : id(id),
412 sent_time(sent_time) {}
413
414 std::string id;
415 uint32 sent_time;
416 };
417
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000418 // States are from RFC 5245. http://tools.ietf.org/html/rfc5245#section-5.7.4
419 enum State {
420 STATE_WAITING = 0, // Check has not been performed, Waiting pair on CL.
421 STATE_INPROGRESS, // Check has been sent, transaction is in progress.
422 STATE_SUCCEEDED, // Check already done, produced a successful result.
423 STATE_FAILED // Check for this connection failed.
424 };
425
426 virtual ~Connection();
427
428 // The local port where this connection sends and receives packets.
429 Port* port() { return port_; }
430 const Port* port() const { return port_; }
431
432 // Returns the description of the local port
433 virtual const Candidate& local_candidate() const;
434
435 // Returns the description of the remote port to which we communicate.
436 const Candidate& remote_candidate() const { return remote_candidate_; }
437
438 // Returns the pair priority.
439 uint64 priority() const;
440
441 enum ReadState {
442 STATE_READ_INIT = 0, // we have yet to receive a ping
443 STATE_READABLE = 1, // we have received pings recently
444 STATE_READ_TIMEOUT = 2, // we haven't received pings in a while
445 };
446
447 ReadState read_state() const { return read_state_; }
448 bool readable() const { return read_state_ == STATE_READABLE; }
449
450 enum WriteState {
451 STATE_WRITABLE = 0, // we have received ping responses recently
452 STATE_WRITE_UNRELIABLE = 1, // we have had a few ping failures
453 STATE_WRITE_INIT = 2, // we have yet to receive a ping response
454 STATE_WRITE_TIMEOUT = 3, // we have had a large number of ping failures
455 };
456
457 WriteState write_state() const { return write_state_; }
458 bool writable() const { return write_state_ == STATE_WRITABLE; }
459
460 // Determines whether the connection has finished connecting. This can only
461 // be false for TCP connections.
462 bool connected() const { return connected_; }
463
464 // Estimate of the round-trip time over this connection.
465 uint32 rtt() const { return rtt_; }
466
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
490 sigslot::signal4<Connection*, const char*, size_t,
491 const rtc::PacketTime&> SignalReadPacket;
492
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
512 void set_remote_ice_mode(IceMode mode) {
513 remote_ice_mode_ = mode;
514 }
515
516 // Makes the connection go away.
517 void Destroy();
518
519 // Checks that the state of this connection is up-to-date. The argument is
520 // the current time, which is compared against various timeouts.
521 void UpdateState(uint32 now);
522
523 // Called when this connection should try checking writability again.
524 uint32 last_ping_sent() const { return last_ping_sent_; }
525 void Ping(uint32 now);
Peter Thatcher1fe120a2015-06-10 11:33:17 -0700526 void ReceivedPingResponse();
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000527
528 // Called whenever a valid ping is received on this connection. This is
529 // public because the connection intercepts the first ping for us.
530 uint32 last_ping_received() const { return last_ping_received_; }
531 void ReceivedPing();
532
533 // Debugging description of this connection
guoweis@webrtc.org8c9ff202014-12-04 07:56:02 +0000534 std::string ToDebugId() const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000535 std::string ToString() const;
536 std::string ToSensitiveString() const;
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700537 // Prints pings_since_last_response_ into a string.
538 void PrintPingsSinceLastResponse(std::string* pings, size_t max);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000539
540 bool reported() const { return reported_; }
541 void set_reported(bool reported) { reported_ = reported;}
542
543 // This flag will be set if this connection is the chosen one for media
544 // transmission. This connection will send STUN ping with USE-CANDIDATE
545 // attribute.
546 sigslot::signal1<Connection*> SignalUseCandidate;
Peter Thatcher54360512015-07-08 11:08:35 -0700547
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000548 // Invoked when Connection receives STUN error response with 487 code.
549 void HandleRoleConflictFromPeer();
550
551 State state() const { return state_; }
552
553 IceMode remote_ice_mode() const { return remote_ice_mode_; }
554
jiayl@webrtc.orgdacdd942015-01-23 17:33:34 +0000555 // Update the ICE password of the remote candidate if |ice_ufrag| matches
556 // the candidate's ufrag, and the candidate's passwrod has not been set.
557 void MaybeSetRemoteIceCredentials(const std::string& ice_ufrag,
558 const std::string& ice_pwd);
559
560 // If |remote_candidate_| is peer reflexive and is equivalent to
561 // |new_candidate| except the type, update |remote_candidate_| to
562 // |new_candidate|.
563 void MaybeUpdatePeerReflexiveCandidate(const Candidate& new_candidate);
564
Peter Thatcher54360512015-07-08 11:08:35 -0700565 // Returns the last received time of any data, stun request, or stun
566 // response in milliseconds
567 uint32 last_received();
568
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000569 protected:
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700570 enum { MSG_DELETE = 0, MSG_FIRST_AVAILABLE };
571
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000572 // Constructs a new connection to the given remote port.
573 Connection(Port* port, size_t index, const Candidate& candidate);
574
575 // Called back when StunRequestManager has a stun packet to send
576 void OnSendStunPacket(const void* data, size_t size, StunRequest* req);
577
578 // Callbacks from ConnectionRequest
Guo-wei Shiehbe508a12015-04-06 12:48:47 -0700579 virtual void OnConnectionRequestResponse(ConnectionRequest* req,
580 StunMessage* response);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000581 void OnConnectionRequestErrorResponse(ConnectionRequest* req,
582 StunMessage* response);
583 void OnConnectionRequestTimeout(ConnectionRequest* req);
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700584 void OnConnectionRequestSent(ConnectionRequest* req);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000585
586 // Changes the state and signals if necessary.
587 void set_read_state(ReadState value);
588 void set_write_state(WriteState value);
589 void set_state(State state);
590 void set_connected(bool value);
591
592 // Checks if this connection is useless, and hence, should be destroyed.
593 void CheckTimeout();
594
595 void OnMessage(rtc::Message *pmsg);
596
597 Port* port_;
598 size_t local_candidate_index_;
599 Candidate remote_candidate_;
600 ReadState read_state_;
601 WriteState write_state_;
602 bool connected_;
603 bool pruned_;
604 // By default |use_candidate_attr_| flag will be true,
605 // as we will be using agrressive nomination.
606 // But when peer is ice-lite, this flag "must" be initialized to false and
607 // turn on when connection becomes "best connection".
608 bool use_candidate_attr_;
609 IceMode remote_ice_mode_;
610 StunRequestManager requests_;
611 uint32 rtt_;
612 uint32 last_ping_sent_; // last time we sent a ping to the other side
613 uint32 last_ping_received_; // last time we received a ping from the other
614 // side
615 uint32 last_data_received_;
616 uint32 last_ping_response_received_;
Peter Thatcher1cf6f812015-05-15 10:40:45 -0700617 std::vector<SentPing> pings_since_last_response_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000618
619 rtc::RateTracker recv_rate_tracker_;
620 rtc::RateTracker send_rate_tracker_;
guoweis@webrtc.org930e0042014-11-17 19:42:14 +0000621 uint32 sent_packets_discarded_;
622 uint32 sent_packets_total_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000623
624 private:
625 void MaybeAddPrflxCandidate(ConnectionRequest* request,
626 StunMessage* response);
627
628 bool reported_;
629 State state_;
630
631 friend class Port;
632 friend class ConnectionRequest;
633};
634
635// ProxyConnection defers all the interesting work to the port
636class ProxyConnection : public Connection {
637 public:
638 ProxyConnection(Port* port, size_t index, const Candidate& candidate);
639
640 virtual int Send(const void* data, size_t size,
641 const rtc::PacketOptions& options);
642 virtual int GetError() { return error_; }
643
644 private:
645 int error_;
646};
647
648} // namespace cricket
649
650#endif // WEBRTC_P2P_BASE_PORT_H_