blob: 1c43c935f26c4bbc69662406ebb7122c262eec7a [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2004--2005, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifndef TALK_P2P_BASE_PORT_H_
29#define TALK_P2P_BASE_PORT_H_
30
31#include <string>
32#include <vector>
33#include <map>
34
wu@webrtc.orga9890802013-12-13 00:21:03 +000035#include "talk/base/asyncpacketsocket.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036#include "talk/base/network.h"
37#include "talk/base/proxyinfo.h"
38#include "talk/base/ratetracker.h"
39#include "talk/base/sigslot.h"
40#include "talk/base/socketaddress.h"
41#include "talk/base/thread.h"
42#include "talk/p2p/base/candidate.h"
43#include "talk/p2p/base/packetsocketfactory.h"
44#include "talk/p2p/base/portinterface.h"
45#include "talk/p2p/base/stun.h"
46#include "talk/p2p/base/stunrequest.h"
47#include "talk/p2p/base/transport.h"
48
henrike@webrtc.org28e20752013-07-10 00:45:36 +000049namespace cricket {
50
51class Connection;
52class ConnectionRequest;
53
54extern const char LOCAL_PORT_TYPE[];
55extern const char STUN_PORT_TYPE[];
56extern const char PRFLX_PORT_TYPE[];
57extern const char RELAY_PORT_TYPE[];
58
59extern const char UDP_PROTOCOL_NAME[];
60extern const char TCP_PROTOCOL_NAME[];
61extern const char SSLTCP_PROTOCOL_NAME[];
62
63// The length of time we wait before timing out readability on a connection.
64const uint32 CONNECTION_READ_TIMEOUT = 30 * 1000; // 30 seconds
65
66// The length of time we wait before timing out writability on a connection.
67const uint32 CONNECTION_WRITE_TIMEOUT = 15 * 1000; // 15 seconds
68
69// The length of time we wait before we become unwritable.
70const uint32 CONNECTION_WRITE_CONNECT_TIMEOUT = 5 * 1000; // 5 seconds
71
72// The number of pings that must fail to respond before we become unwritable.
73const uint32 CONNECTION_WRITE_CONNECT_FAILURES = 5;
74
75// This is the length of time that we wait for a ping response to come back.
76const int CONNECTION_RESPONSE_TIMEOUT = 5 * 1000; // 5 seconds
77
78enum RelayType {
79 RELAY_GTURN, // Legacy google relay service.
80 RELAY_TURN // Standard (TURN) relay service.
81};
82
83enum IcePriorityValue {
84 // The reason we are choosing Relay preference 2 is because, we can run
85 // Relay from client to server on UDP/TCP/TLS. To distinguish the transport
86 // protocol, we prefer UDP over TCP over TLS.
87 // For UDP ICE_TYPE_PREFERENCE_RELAY will be 2.
88 // For TCP ICE_TYPE_PREFERENCE_RELAY will be 1.
89 // For TLS ICE_TYPE_PREFERENCE_RELAY will be 0.
90 // Check turnport.cc for setting these values.
91 ICE_TYPE_PREFERENCE_RELAY = 2,
92 ICE_TYPE_PREFERENCE_HOST_TCP = 90,
93 ICE_TYPE_PREFERENCE_SRFLX = 100,
94 ICE_TYPE_PREFERENCE_PRFLX = 110,
95 ICE_TYPE_PREFERENCE_HOST = 126
96};
97
98const char* ProtoToString(ProtocolType proto);
99bool StringToProto(const char* value, ProtocolType* proto);
100
101struct ProtocolAddress {
102 talk_base::SocketAddress address;
103 ProtocolType proto;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000104 bool secure;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105
106 ProtocolAddress(const talk_base::SocketAddress& a, ProtocolType p)
wu@webrtc.org91053e72013-08-10 07:18:04 +0000107 : address(a), proto(p), secure(false) { }
108 ProtocolAddress(const talk_base::SocketAddress& a, ProtocolType p, bool sec)
109 : address(a), proto(p), secure(sec) { }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110};
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 talk_base::MessageHandler,
116 public sigslot::has_slots<> {
117 public:
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000118 Port(talk_base::Thread* thread, talk_base::PacketSocketFactory* factory,
119 talk_base::Network* network, const talk_base::IPAddress& ip,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000120 const std::string& username_fragment, const std::string& password);
121 Port(talk_base::Thread* thread, const std::string& type,
122 talk_base::PacketSocketFactory* factory,
123 talk_base::Network* network, const talk_base::IPAddress& ip,
124 int min_port, int max_port, const std::string& username_fragment,
125 const std::string& password);
126 virtual ~Port();
127
128 virtual const std::string& Type() const { return type_; }
129 virtual talk_base::Network* Network() const { return network_; }
130
131 // This method will set the flag which enables standard ICE/STUN procedures
132 // in STUN connectivity checks. Currently this method does
133 // 1. Add / Verify MI attribute in STUN binding requests.
134 // 2. Username attribute in STUN binding request will be RFRAF:LFRAG,
135 // as opposed to RFRAGLFRAG.
136 virtual void SetIceProtocolType(IceProtocolType protocol) {
137 ice_protocol_ = protocol;
138 }
139 virtual IceProtocolType IceProtocol() const { return ice_protocol_; }
140
141 // Methods to set/get ICE role and tiebreaker values.
mallinath@webrtc.orga5506692013-08-12 21:18:15 +0000142 IceRole GetIceRole() const { return ice_role_; }
143 void SetIceRole(IceRole role) { ice_role_ = role; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000144
mallinath@webrtc.orga5506692013-08-12 21:18:15 +0000145 void SetIceTiebreaker(uint64 tiebreaker) { tiebreaker_ = tiebreaker; }
146 uint64 IceTiebreaker() const { return tiebreaker_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000147
148 virtual bool SharedSocket() const { return shared_socket_; }
149
150 // The thread on which this port performs its I/O.
151 talk_base::Thread* thread() { return thread_; }
152
153 // The factory used to create the sockets of this port.
154 talk_base::PacketSocketFactory* socket_factory() const { return factory_; }
155 void set_socket_factory(talk_base::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 const talk_base::SocketAddress& related_address() const {
176 return related_address_;
177 }
178 void set_related_address(const talk_base::SocketAddress& address) {
179 related_address_ = address;
180 }
181
182 // Identifies the generation that this port was created in.
183 uint32 generation() { return generation_; }
184 void set_generation(uint32 generation) { generation_ = generation; }
185
186 // ICE requires a single username/password per content/media line. So the
187 // |ice_username_fragment_| of the ports that belongs to the same content will
188 // be the same. However this causes a small complication with our relay
189 // server, which expects different username for RTP and RTCP.
190 //
191 // To resolve this problem, we implemented the username_fragment(),
192 // which returns a different username (calculated from
193 // |ice_username_fragment_|) for RTCP in the case of ICEPROTO_GOOGLE. And the
194 // username_fragment() simply returns |ice_username_fragment_| when running
195 // in ICEPROTO_RFC5245.
196 //
197 // As a result the ICEPROTO_GOOGLE will use different usernames for RTP and
198 // RTCP. And the ICEPROTO_RFC5245 will use same username for both RTP and
199 // RTCP.
200 const std::string username_fragment() const;
201 const std::string& password() const { return password_; }
202
203 // Fired when candidates are discovered by the port. When all candidates
204 // are discovered that belong to port SignalAddressReady is fired.
205 sigslot::signal2<Port*, const Candidate&> SignalCandidateReady;
206
207 // Provides all of the above information in one handy object.
208 virtual const std::vector<Candidate>& Candidates() const {
209 return candidates_;
210 }
211
212 // SignalPortComplete is sent when port completes the task of candidates
213 // allocation.
214 sigslot::signal1<Port*> SignalPortComplete;
215 // This signal sent when port fails to allocate candidates and this port
216 // can't be used in establishing the connections. When port is in shared mode
217 // and port fails to allocate one of the candidates, port shouldn't send
218 // this signal as other candidates might be usefull in establishing the
219 // connection.
220 sigslot::signal1<Port*> SignalPortError;
221
222 // Returns a map containing all of the connections of this port, keyed by the
223 // remote address.
224 typedef std::map<talk_base::SocketAddress, Connection*> AddressMap;
225 const AddressMap& connections() { return connections_; }
226
227 // Returns the connection to the given address or NULL if none exists.
228 virtual Connection* GetConnection(
229 const talk_base::SocketAddress& remote_addr);
230
231 // Called each time a connection is created.
232 sigslot::signal2<Port*, Connection*> SignalConnectionCreated;
233
234 // In a shared socket mode each port which shares the socket will decide
235 // to accept the packet based on the |remote_addr|. Currently only UDP
236 // port implemented this method.
237 // TODO(mallinath) - Make it pure virtual.
238 virtual bool HandleIncomingPacket(
239 talk_base::AsyncPacketSocket* socket, const char* data, size_t size,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000240 const talk_base::SocketAddress& remote_addr,
241 const talk_base::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000242 ASSERT(false);
243 return false;
244 }
245
246 // Sends a response message (normal or error) to the given request. One of
247 // these methods should be called as a response to SignalUnknownAddress.
248 // NOTE: You MUST call CreateConnection BEFORE SendBindingResponse.
249 virtual void SendBindingResponse(StunMessage* request,
250 const talk_base::SocketAddress& addr);
251 virtual void SendBindingErrorResponse(
252 StunMessage* request, const talk_base::SocketAddress& addr,
253 int error_code, const std::string& reason);
254
255 void set_proxy(const std::string& user_agent,
256 const talk_base::ProxyInfo& proxy) {
257 user_agent_ = user_agent;
258 proxy_ = proxy;
259 }
260 const std::string& user_agent() { return user_agent_; }
261 const talk_base::ProxyInfo& proxy() { return proxy_; }
262
263 virtual void EnablePortPackets();
264
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000265 // Called if the port has no connections and is no longer useful.
266 void Destroy();
267
268 virtual void OnMessage(talk_base::Message *pmsg);
269
270 // Debugging description of this port
271 virtual std::string ToString() const;
272 talk_base::IPAddress& ip() { return ip_; }
273 int min_port() { return min_port_; }
274 int max_port() { return max_port_; }
275
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +0000276 // Timeout shortening function to speed up unit tests.
277 void set_timeout_delay(int delay) { timeout_delay_ = delay; }
278
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000279 // This method will return local and remote username fragements from the
280 // stun username attribute if present.
281 bool ParseStunUsername(const StunMessage* stun_msg,
282 std::string* local_username,
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000283 std::string* remote_username,
284 IceProtocolType* remote_protocol_type) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000285 void CreateStunUsername(const std::string& remote_username,
286 std::string* stun_username_attr_str) const;
287
288 bool MaybeIceRoleConflict(const talk_base::SocketAddress& addr,
289 IceMessage* stun_msg,
290 const std::string& remote_ufrag);
291
292 // Called when the socket is currently able to send.
293 void OnReadyToSend();
294
295 // Called when the Connection discovers a local peer reflexive candidate.
296 // Returns the index of the new local candidate.
297 size_t AddPrflxCandidate(const Candidate& local);
298
299 // Returns if RFC 5245 ICE protocol is used.
300 bool IsStandardIce() const;
301
302 // Returns if Google ICE protocol is used.
303 bool IsGoogleIce() const;
304
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000305 // Returns if Hybrid ICE protocol is used.
306 bool IsHybridIce() const;
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000307
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000308 protected:
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000309 enum {
310 MSG_CHECKTIMEOUT = 0,
311 MSG_FIRST_AVAILABLE
312 };
313
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000314 void set_type(const std::string& type) { type_ = type; }
315 // Fills in the local address of the port.
316 void AddAddress(const talk_base::SocketAddress& address,
317 const talk_base::SocketAddress& base_address,
318 const std::string& protocol, const std::string& type,
319 uint32 type_preference, bool final);
320
321 // Adds the given connection to the list. (Deleting removes them.)
322 void AddConnection(Connection* conn);
323
324 // Called when a packet is received from an unknown address that is not
325 // currently a connection. If this is an authenticated STUN binding request,
326 // then we will signal the client.
327 void OnReadPacket(const char* data, size_t size,
328 const talk_base::SocketAddress& addr,
329 ProtocolType proto);
330
331 // If the given data comprises a complete and correct STUN message then the
332 // return value is true, otherwise false. If the message username corresponds
333 // with this port's username fragment, msg will contain the parsed STUN
334 // message. Otherwise, the function may send a STUN response internally.
335 // remote_username contains the remote fragment of the STUN username.
336 bool GetStunMessage(const char* data, size_t size,
337 const talk_base::SocketAddress& addr,
338 IceMessage** out_msg, std::string* out_username);
339
340 // Checks if the address in addr is compatible with the port's ip.
341 bool IsCompatibleAddress(const talk_base::SocketAddress& addr);
342
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000343 // Returns default DSCP value.
344 talk_base::DiffServCodePoint DefaultDscpValue() const {
345 // No change from what MediaChannel set.
346 return talk_base::DSCP_NO_CHANGE;
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000347 }
348
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000349 private:
350 void Construct();
351 // Called when one of our connections deletes itself.
352 void OnConnectionDestroyed(Connection* conn);
353
354 // Checks if this port is useless, and hence, should be destroyed.
355 void CheckTimeout();
356
357 talk_base::Thread* thread_;
358 talk_base::PacketSocketFactory* factory_;
359 std::string type_;
360 bool send_retransmit_count_attribute_;
361 talk_base::Network* network_;
362 talk_base::IPAddress ip_;
363 int min_port_;
364 int max_port_;
365 std::string content_name_;
366 int component_;
367 uint32 generation_;
368 talk_base::SocketAddress related_address_;
369 // In order to establish a connection to this Port (so that real data can be
370 // sent through), the other side must send us a STUN binding request that is
371 // authenticated with this username_fragment and password.
372 // PortAllocatorSession will provide these username_fragment and password.
373 //
374 // Note: we should always use username_fragment() instead of using
375 // |ice_username_fragment_| directly. For the details see the comment on
376 // username_fragment().
377 std::string ice_username_fragment_;
378 std::string password_;
379 std::vector<Candidate> candidates_;
380 AddressMap connections_;
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +0000381 int timeout_delay_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000382 bool enable_port_packets_;
383 IceProtocolType ice_protocol_;
mallinath@webrtc.orga5506692013-08-12 21:18:15 +0000384 IceRole ice_role_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000385 uint64 tiebreaker_;
386 bool shared_socket_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000387 // Information to use when going through a proxy.
388 std::string user_agent_;
389 talk_base::ProxyInfo proxy_;
390
391 friend class Connection;
392};
393
394// Represents a communication link between a port on the local client and a
395// port on the remote client.
396class Connection : public talk_base::MessageHandler,
397 public sigslot::has_slots<> {
398 public:
399 // States are from RFC 5245. http://tools.ietf.org/html/rfc5245#section-5.7.4
400 enum State {
401 STATE_WAITING = 0, // Check has not been performed, Waiting pair on CL.
402 STATE_INPROGRESS, // Check has been sent, transaction is in progress.
403 STATE_SUCCEEDED, // Check already done, produced a successful result.
404 STATE_FAILED // Check for this connection failed.
405 };
406
407 virtual ~Connection();
408
409 // The local port where this connection sends and receives packets.
410 Port* port() { return port_; }
411 const Port* port() const { return port_; }
412
413 // Returns the description of the local port
414 virtual const Candidate& local_candidate() const;
415
416 // Returns the description of the remote port to which we communicate.
417 const Candidate& remote_candidate() const { return remote_candidate_; }
418
419 // Returns the pair priority.
420 uint64 priority() const;
421
422 enum ReadState {
423 STATE_READ_INIT = 0, // we have yet to receive a ping
424 STATE_READABLE = 1, // we have received pings recently
425 STATE_READ_TIMEOUT = 2, // we haven't received pings in a while
426 };
427
428 ReadState read_state() const { return read_state_; }
429 bool readable() const { return read_state_ == STATE_READABLE; }
430
431 enum WriteState {
432 STATE_WRITABLE = 0, // we have received ping responses recently
433 STATE_WRITE_UNRELIABLE = 1, // we have had a few ping failures
434 STATE_WRITE_INIT = 2, // we have yet to receive a ping response
435 STATE_WRITE_TIMEOUT = 3, // we have had a large number of ping failures
436 };
437
438 WriteState write_state() const { return write_state_; }
439 bool writable() const { return write_state_ == STATE_WRITABLE; }
440
441 // Determines whether the connection has finished connecting. This can only
442 // be false for TCP connections.
443 bool connected() const { return connected_; }
444
445 // Estimate of the round-trip time over this connection.
446 uint32 rtt() const { return rtt_; }
447
448 size_t sent_total_bytes();
449 size_t sent_bytes_second();
450 size_t recv_total_bytes();
451 size_t recv_bytes_second();
452 sigslot::signal1<Connection*> SignalStateChange;
453
454 // Sent when the connection has decided that it is no longer of value. It
455 // will delete itself immediately after this call.
456 sigslot::signal1<Connection*> SignalDestroyed;
457
458 // The connection can send and receive packets asynchronously. This matches
459 // the interface of AsyncPacketSocket, which may use UDP or TCP under the
460 // covers.
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000461 virtual int Send(const void* data, size_t size,
462 talk_base::DiffServCodePoint dscp) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000463
464 // Error if Send() returns < 0
465 virtual int GetError() = 0;
466
wu@webrtc.orga9890802013-12-13 00:21:03 +0000467 sigslot::signal4<Connection*, const char*, size_t,
468 const talk_base::PacketTime&> SignalReadPacket;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000469
470 sigslot::signal1<Connection*> SignalReadyToSend;
471
472 // Called when a packet is received on this connection.
wu@webrtc.orga9890802013-12-13 00:21:03 +0000473 void OnReadPacket(const char* data, size_t size,
474 const talk_base::PacketTime& packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000475
476 // Called when the socket is currently able to send.
477 void OnReadyToSend();
478
479 // Called when a connection is determined to be no longer useful to us. We
480 // still keep it around in case the other side wants to use it. But we can
481 // safely stop pinging on it and we can allow it to time out if the other
482 // side stops using it as well.
483 bool pruned() const { return pruned_; }
484 void Prune();
485
486 bool use_candidate_attr() const { return use_candidate_attr_; }
487 void set_use_candidate_attr(bool enable);
488
489 void set_remote_ice_mode(IceMode mode) {
490 remote_ice_mode_ = mode;
491 }
492
493 // Makes the connection go away.
494 void Destroy();
495
496 // Checks that the state of this connection is up-to-date. The argument is
497 // the current time, which is compared against various timeouts.
498 void UpdateState(uint32 now);
499
500 // Called when this connection should try checking writability again.
501 uint32 last_ping_sent() const { return last_ping_sent_; }
502 void Ping(uint32 now);
503
504 // Called whenever a valid ping is received on this connection. This is
505 // public because the connection intercepts the first ping for us.
506 uint32 last_ping_received() const { return last_ping_received_; }
507 void ReceivedPing();
508
509 // Debugging description of this connection
510 std::string ToString() const;
511 std::string ToSensitiveString() const;
512
513 bool reported() const { return reported_; }
514 void set_reported(bool reported) { reported_ = reported;}
515
516 // This flag will be set if this connection is the chosen one for media
517 // transmission. This connection will send STUN ping with USE-CANDIDATE
518 // attribute.
519 sigslot::signal1<Connection*> SignalUseCandidate;
520 // Invoked when Connection receives STUN error response with 487 code.
521 void HandleRoleConflictFromPeer();
522
523 State state() const { return state_; }
524
525 IceMode remote_ice_mode() const { return remote_ice_mode_; }
526
527 protected:
528 // Constructs a new connection to the given remote port.
529 Connection(Port* port, size_t index, const Candidate& candidate);
530
531 // Called back when StunRequestManager has a stun packet to send
532 void OnSendStunPacket(const void* data, size_t size, StunRequest* req);
533
534 // Callbacks from ConnectionRequest
535 void OnConnectionRequestResponse(ConnectionRequest* req,
536 StunMessage* response);
537 void OnConnectionRequestErrorResponse(ConnectionRequest* req,
538 StunMessage* response);
539 void OnConnectionRequestTimeout(ConnectionRequest* req);
540
541 // Changes the state and signals if necessary.
542 void set_read_state(ReadState value);
543 void set_write_state(WriteState value);
544 void set_state(State state);
545 void set_connected(bool value);
546
547 // Checks if this connection is useless, and hence, should be destroyed.
548 void CheckTimeout();
549
550 void OnMessage(talk_base::Message *pmsg);
551
552 Port* port_;
553 size_t local_candidate_index_;
554 Candidate remote_candidate_;
555 ReadState read_state_;
556 WriteState write_state_;
557 bool connected_;
558 bool pruned_;
559 // By default |use_candidate_attr_| flag will be true,
560 // as we will be using agrressive nomination.
561 // But when peer is ice-lite, this flag "must" be initialized to false and
562 // turn on when connection becomes "best connection".
563 bool use_candidate_attr_;
564 IceMode remote_ice_mode_;
565 StunRequestManager requests_;
566 uint32 rtt_;
567 uint32 last_ping_sent_; // last time we sent a ping to the other side
568 uint32 last_ping_received_; // last time we received a ping from the other
569 // side
570 uint32 last_data_received_;
571 uint32 last_ping_response_received_;
572 std::vector<uint32> pings_since_last_response_;
573
574 talk_base::RateTracker recv_rate_tracker_;
575 talk_base::RateTracker send_rate_tracker_;
576
577 private:
578 void MaybeAddPrflxCandidate(ConnectionRequest* request,
579 StunMessage* response);
580
581 bool reported_;
582 State state_;
583
584 friend class Port;
585 friend class ConnectionRequest;
586};
587
588// ProxyConnection defers all the interesting work to the port
589class ProxyConnection : public Connection {
590 public:
591 ProxyConnection(Port* port, size_t index, const Candidate& candidate);
592
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000593 virtual int Send(const void* data, size_t size,
594 talk_base::DiffServCodePoint dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000595 virtual int GetError() { return error_; }
596
597 private:
598 int error_;
599};
600
601} // namespace cricket
602
603#endif // TALK_P2P_BASE_PORT_H_