henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 33 | namespace cricket { |
| 34 | |
| 35 | class Connection; |
| 36 | class ConnectionRequest; |
| 37 | |
| 38 | extern const char LOCAL_PORT_TYPE[]; |
| 39 | extern const char STUN_PORT_TYPE[]; |
| 40 | extern const char PRFLX_PORT_TYPE[]; |
| 41 | extern const char RELAY_PORT_TYPE[]; |
| 42 | |
| 43 | extern const char UDP_PROTOCOL_NAME[]; |
| 44 | extern const char TCP_PROTOCOL_NAME[]; |
| 45 | extern const char SSLTCP_PROTOCOL_NAME[]; |
| 46 | |
| 47 | // RFC 6544, TCP candidate encoding rules. |
| 48 | extern const int DISCARD_PORT; |
| 49 | extern const char TCPTYPE_ACTIVE_STR[]; |
| 50 | extern const char TCPTYPE_PASSIVE_STR[]; |
| 51 | extern const char TCPTYPE_SIMOPEN_STR[]; |
| 52 | |
| 53 | // The length of time we wait before timing out readability on a connection. |
| 54 | const uint32 CONNECTION_READ_TIMEOUT = 30 * 1000; // 30 seconds |
| 55 | |
| 56 | // The length of time we wait before timing out writability on a connection. |
| 57 | const uint32 CONNECTION_WRITE_TIMEOUT = 15 * 1000; // 15 seconds |
| 58 | |
| 59 | // The length of time we wait before we become unwritable. |
| 60 | const uint32 CONNECTION_WRITE_CONNECT_TIMEOUT = 5 * 1000; // 5 seconds |
| 61 | |
| 62 | // The number of pings that must fail to respond before we become unwritable. |
| 63 | const uint32 CONNECTION_WRITE_CONNECT_FAILURES = 5; |
| 64 | |
| 65 | // This is the length of time that we wait for a ping response to come back. |
| 66 | const int CONNECTION_RESPONSE_TIMEOUT = 5 * 1000; // 5 seconds |
| 67 | |
| 68 | enum RelayType { |
| 69 | RELAY_GTURN, // Legacy google relay service. |
| 70 | RELAY_TURN // Standard (TURN) relay service. |
| 71 | }; |
| 72 | |
| 73 | enum 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 | |
| 88 | const char* ProtoToString(ProtocolType proto); |
| 89 | bool StringToProto(const char* value, ProtocolType* proto); |
| 90 | |
| 91 | struct 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 | |
| 102 | typedef 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. |
| 107 | class Port : public PortInterface, public rtc::MessageHandler, |
| 108 | public sigslot::has_slots<> { |
| 109 | public: |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 110 | Port(rtc::Thread* thread, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 111 | rtc::PacketSocketFactory* factory, |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 112 | 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.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 124 | 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 | |
| 130 | // 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 | |
| 140 | // 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.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 265 | const rtc::IPAddress& ip() const { return ip_; } |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 266 | uint16 min_port() { return min_port_; } |
| 267 | uint16 max_port() { return max_port_; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 268 | |
| 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, |
| 276 | std::string* remote_username, |
| 277 | IceProtocolType* remote_protocol_type) const; |
| 278 | 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 | |
| 292 | // 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 | |
| 301 | 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, |
| 316 | const std::string& protocol, const std::string& tcptype, |
| 317 | const std::string& type, uint32 type_preference, |
| 318 | uint32 relay_preference, bool final); |
| 319 | |
| 320 | // Adds the given connection to the list. (Deleting removes them.) |
| 321 | void AddConnection(Connection* conn); |
| 322 | |
| 323 | // Called when a packet is received from an unknown address that is not |
| 324 | // currently a connection. If this is an authenticated STUN binding request, |
| 325 | // then we will signal the client. |
| 326 | void OnReadPacket(const char* data, size_t size, |
| 327 | const rtc::SocketAddress& addr, |
| 328 | ProtocolType proto); |
| 329 | |
| 330 | // If the given data comprises a complete and correct STUN message then the |
| 331 | // return value is true, otherwise false. If the message username corresponds |
| 332 | // with this port's username fragment, msg will contain the parsed STUN |
| 333 | // message. Otherwise, the function may send a STUN response internally. |
| 334 | // remote_username contains the remote fragment of the STUN username. |
| 335 | bool GetStunMessage(const char* data, size_t size, |
| 336 | const rtc::SocketAddress& addr, |
| 337 | IceMessage** out_msg, std::string* out_username); |
| 338 | |
| 339 | // Checks if the address in addr is compatible with the port's ip. |
| 340 | bool IsCompatibleAddress(const rtc::SocketAddress& addr); |
| 341 | |
| 342 | // Returns default DSCP value. |
| 343 | rtc::DiffServCodePoint DefaultDscpValue() const { |
| 344 | // No change from what MediaChannel set. |
| 345 | return rtc::DSCP_NO_CHANGE; |
| 346 | } |
| 347 | |
| 348 | uint32 candidate_filter() { return candidate_filter_; } |
| 349 | |
| 350 | private: |
| 351 | void Construct(); |
| 352 | // Called when one of our connections deletes itself. |
| 353 | void OnConnectionDestroyed(Connection* conn); |
| 354 | |
| 355 | // Checks if this port is useless, and hence, should be destroyed. |
| 356 | void CheckTimeout(); |
| 357 | |
| 358 | rtc::Thread* thread_; |
| 359 | rtc::PacketSocketFactory* factory_; |
| 360 | std::string type_; |
| 361 | bool send_retransmit_count_attribute_; |
| 362 | rtc::Network* network_; |
| 363 | rtc::IPAddress ip_; |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 364 | uint16 min_port_; |
| 365 | uint16 max_port_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 366 | std::string content_name_; |
| 367 | int component_; |
| 368 | uint32 generation_; |
| 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_; |
| 381 | int timeout_delay_; |
| 382 | bool enable_port_packets_; |
| 383 | IceProtocolType ice_protocol_; |
| 384 | IceRole ice_role_; |
| 385 | uint64 tiebreaker_; |
| 386 | bool shared_socket_; |
| 387 | // Information to use when going through a proxy. |
| 388 | std::string user_agent_; |
| 389 | rtc::ProxyInfo proxy_; |
| 390 | |
| 391 | // Candidate filter is pushed down to Port such that each Port could |
| 392 | // make its own decision on how to create candidates. For example, |
| 393 | // when IceTransportsType is set to relay, both RelayPort and |
| 394 | // TurnPort will hide raddr to avoid local address leakage. |
| 395 | uint32 candidate_filter_; |
| 396 | |
| 397 | friend class Connection; |
| 398 | }; |
| 399 | |
| 400 | // Represents a communication link between a port on the local client and a |
| 401 | // port on the remote client. |
| 402 | class Connection : public rtc::MessageHandler, |
| 403 | public sigslot::has_slots<> { |
| 404 | public: |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame^] | 405 | struct SentPing { |
| 406 | SentPing(const std::string id, uint32 sent_time) |
| 407 | : id(id), |
| 408 | sent_time(sent_time) {} |
| 409 | |
| 410 | std::string id; |
| 411 | uint32 sent_time; |
| 412 | }; |
| 413 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 414 | // States are from RFC 5245. http://tools.ietf.org/html/rfc5245#section-5.7.4 |
| 415 | enum State { |
| 416 | STATE_WAITING = 0, // Check has not been performed, Waiting pair on CL. |
| 417 | STATE_INPROGRESS, // Check has been sent, transaction is in progress. |
| 418 | STATE_SUCCEEDED, // Check already done, produced a successful result. |
| 419 | STATE_FAILED // Check for this connection failed. |
| 420 | }; |
| 421 | |
| 422 | virtual ~Connection(); |
| 423 | |
| 424 | // The local port where this connection sends and receives packets. |
| 425 | Port* port() { return port_; } |
| 426 | const Port* port() const { return port_; } |
| 427 | |
| 428 | // Returns the description of the local port |
| 429 | virtual const Candidate& local_candidate() const; |
| 430 | |
| 431 | // Returns the description of the remote port to which we communicate. |
| 432 | const Candidate& remote_candidate() const { return remote_candidate_; } |
| 433 | |
| 434 | // Returns the pair priority. |
| 435 | uint64 priority() const; |
| 436 | |
| 437 | enum ReadState { |
| 438 | STATE_READ_INIT = 0, // we have yet to receive a ping |
| 439 | STATE_READABLE = 1, // we have received pings recently |
| 440 | STATE_READ_TIMEOUT = 2, // we haven't received pings in a while |
| 441 | }; |
| 442 | |
| 443 | ReadState read_state() const { return read_state_; } |
| 444 | bool readable() const { return read_state_ == STATE_READABLE; } |
| 445 | |
| 446 | enum WriteState { |
| 447 | STATE_WRITABLE = 0, // we have received ping responses recently |
| 448 | STATE_WRITE_UNRELIABLE = 1, // we have had a few ping failures |
| 449 | STATE_WRITE_INIT = 2, // we have yet to receive a ping response |
| 450 | STATE_WRITE_TIMEOUT = 3, // we have had a large number of ping failures |
| 451 | }; |
| 452 | |
| 453 | WriteState write_state() const { return write_state_; } |
| 454 | bool writable() const { return write_state_ == STATE_WRITABLE; } |
| 455 | |
| 456 | // Determines whether the connection has finished connecting. This can only |
| 457 | // be false for TCP connections. |
| 458 | bool connected() const { return connected_; } |
| 459 | |
| 460 | // Estimate of the round-trip time over this connection. |
| 461 | uint32 rtt() const { return rtt_; } |
| 462 | |
| 463 | size_t sent_total_bytes(); |
| 464 | size_t sent_bytes_second(); |
guoweis@webrtc.org | 930e004 | 2014-11-17 19:42:14 +0000 | [diff] [blame] | 465 | // Used to track how many packets are discarded in the application socket due |
| 466 | // to errors. |
| 467 | size_t sent_discarded_packets(); |
| 468 | size_t sent_total_packets(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 469 | size_t recv_total_bytes(); |
| 470 | size_t recv_bytes_second(); |
| 471 | sigslot::signal1<Connection*> SignalStateChange; |
| 472 | |
| 473 | // Sent when the connection has decided that it is no longer of value. It |
| 474 | // will delete itself immediately after this call. |
| 475 | sigslot::signal1<Connection*> SignalDestroyed; |
| 476 | |
| 477 | // The connection can send and receive packets asynchronously. This matches |
| 478 | // the interface of AsyncPacketSocket, which may use UDP or TCP under the |
| 479 | // covers. |
| 480 | virtual int Send(const void* data, size_t size, |
| 481 | const rtc::PacketOptions& options) = 0; |
| 482 | |
| 483 | // Error if Send() returns < 0 |
| 484 | virtual int GetError() = 0; |
| 485 | |
| 486 | sigslot::signal4<Connection*, const char*, size_t, |
| 487 | const rtc::PacketTime&> SignalReadPacket; |
| 488 | |
| 489 | sigslot::signal1<Connection*> SignalReadyToSend; |
| 490 | |
| 491 | // Called when a packet is received on this connection. |
| 492 | void OnReadPacket(const char* data, size_t size, |
| 493 | const rtc::PacketTime& packet_time); |
| 494 | |
| 495 | // Called when the socket is currently able to send. |
| 496 | void OnReadyToSend(); |
| 497 | |
| 498 | // Called when a connection is determined to be no longer useful to us. We |
| 499 | // still keep it around in case the other side wants to use it. But we can |
| 500 | // safely stop pinging on it and we can allow it to time out if the other |
| 501 | // side stops using it as well. |
| 502 | bool pruned() const { return pruned_; } |
| 503 | void Prune(); |
| 504 | |
| 505 | bool use_candidate_attr() const { return use_candidate_attr_; } |
| 506 | void set_use_candidate_attr(bool enable); |
| 507 | |
| 508 | void set_remote_ice_mode(IceMode mode) { |
| 509 | remote_ice_mode_ = mode; |
| 510 | } |
| 511 | |
| 512 | // Makes the connection go away. |
| 513 | void Destroy(); |
| 514 | |
| 515 | // Checks that the state of this connection is up-to-date. The argument is |
| 516 | // the current time, which is compared against various timeouts. |
| 517 | void UpdateState(uint32 now); |
| 518 | |
| 519 | // Called when this connection should try checking writability again. |
| 520 | uint32 last_ping_sent() const { return last_ping_sent_; } |
| 521 | void Ping(uint32 now); |
| 522 | |
| 523 | // Called whenever a valid ping is received on this connection. This is |
| 524 | // public because the connection intercepts the first ping for us. |
| 525 | uint32 last_ping_received() const { return last_ping_received_; } |
| 526 | void ReceivedPing(); |
| 527 | |
| 528 | // Debugging description of this connection |
guoweis@webrtc.org | 8c9ff20 | 2014-12-04 07:56:02 +0000 | [diff] [blame] | 529 | std::string ToDebugId() const; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 530 | std::string ToString() const; |
| 531 | std::string ToSensitiveString() const; |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame^] | 532 | // Prints pings_since_last_response_ into a string. |
| 533 | void PrintPingsSinceLastResponse(std::string* pings, size_t max); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 534 | |
| 535 | bool reported() const { return reported_; } |
| 536 | void set_reported(bool reported) { reported_ = reported;} |
| 537 | |
| 538 | // This flag will be set if this connection is the chosen one for media |
| 539 | // transmission. This connection will send STUN ping with USE-CANDIDATE |
| 540 | // attribute. |
| 541 | sigslot::signal1<Connection*> SignalUseCandidate; |
| 542 | // Invoked when Connection receives STUN error response with 487 code. |
| 543 | void HandleRoleConflictFromPeer(); |
| 544 | |
| 545 | State state() const { return state_; } |
| 546 | |
| 547 | IceMode remote_ice_mode() const { return remote_ice_mode_; } |
| 548 | |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 549 | // Update the ICE password of the remote candidate if |ice_ufrag| matches |
| 550 | // the candidate's ufrag, and the candidate's passwrod has not been set. |
| 551 | void MaybeSetRemoteIceCredentials(const std::string& ice_ufrag, |
| 552 | const std::string& ice_pwd); |
| 553 | |
| 554 | // If |remote_candidate_| is peer reflexive and is equivalent to |
| 555 | // |new_candidate| except the type, update |remote_candidate_| to |
| 556 | // |new_candidate|. |
| 557 | void MaybeUpdatePeerReflexiveCandidate(const Candidate& new_candidate); |
| 558 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 559 | protected: |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 560 | enum { MSG_DELETE = 0, MSG_FIRST_AVAILABLE }; |
| 561 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 562 | // Constructs a new connection to the given remote port. |
| 563 | Connection(Port* port, size_t index, const Candidate& candidate); |
| 564 | |
| 565 | // Called back when StunRequestManager has a stun packet to send |
| 566 | void OnSendStunPacket(const void* data, size_t size, StunRequest* req); |
| 567 | |
| 568 | // Callbacks from ConnectionRequest |
Guo-wei Shieh | be508a1 | 2015-04-06 12:48:47 -0700 | [diff] [blame] | 569 | virtual void OnConnectionRequestResponse(ConnectionRequest* req, |
| 570 | StunMessage* response); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 571 | void OnConnectionRequestErrorResponse(ConnectionRequest* req, |
| 572 | StunMessage* response); |
| 573 | void OnConnectionRequestTimeout(ConnectionRequest* req); |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame^] | 574 | void OnConnectionRequestSent(ConnectionRequest* req); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 575 | |
| 576 | // Changes the state and signals if necessary. |
| 577 | void set_read_state(ReadState value); |
| 578 | void set_write_state(WriteState value); |
| 579 | void set_state(State state); |
| 580 | void set_connected(bool value); |
| 581 | |
| 582 | // Checks if this connection is useless, and hence, should be destroyed. |
| 583 | void CheckTimeout(); |
| 584 | |
| 585 | void OnMessage(rtc::Message *pmsg); |
| 586 | |
| 587 | Port* port_; |
| 588 | size_t local_candidate_index_; |
| 589 | Candidate remote_candidate_; |
| 590 | ReadState read_state_; |
| 591 | WriteState write_state_; |
| 592 | bool connected_; |
| 593 | bool pruned_; |
| 594 | // By default |use_candidate_attr_| flag will be true, |
| 595 | // as we will be using agrressive nomination. |
| 596 | // But when peer is ice-lite, this flag "must" be initialized to false and |
| 597 | // turn on when connection becomes "best connection". |
| 598 | bool use_candidate_attr_; |
| 599 | IceMode remote_ice_mode_; |
| 600 | StunRequestManager requests_; |
| 601 | uint32 rtt_; |
| 602 | uint32 last_ping_sent_; // last time we sent a ping to the other side |
| 603 | uint32 last_ping_received_; // last time we received a ping from the other |
| 604 | // side |
| 605 | uint32 last_data_received_; |
| 606 | uint32 last_ping_response_received_; |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame^] | 607 | std::vector<SentPing> pings_since_last_response_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 608 | |
| 609 | rtc::RateTracker recv_rate_tracker_; |
| 610 | rtc::RateTracker send_rate_tracker_; |
guoweis@webrtc.org | 930e004 | 2014-11-17 19:42:14 +0000 | [diff] [blame] | 611 | uint32 sent_packets_discarded_; |
| 612 | uint32 sent_packets_total_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 613 | |
| 614 | private: |
| 615 | void MaybeAddPrflxCandidate(ConnectionRequest* request, |
| 616 | StunMessage* response); |
| 617 | |
| 618 | bool reported_; |
| 619 | State state_; |
| 620 | |
| 621 | friend class Port; |
| 622 | friend class ConnectionRequest; |
| 623 | }; |
| 624 | |
| 625 | // ProxyConnection defers all the interesting work to the port |
| 626 | class ProxyConnection : public Connection { |
| 627 | public: |
| 628 | ProxyConnection(Port* port, size_t index, const Candidate& candidate); |
| 629 | |
| 630 | virtual int Send(const void* data, size_t size, |
| 631 | const rtc::PacketOptions& options); |
| 632 | virtual int GetError() { return error_; } |
| 633 | |
| 634 | private: |
| 635 | int error_; |
| 636 | }; |
| 637 | |
| 638 | } // namespace cricket |
| 639 | |
| 640 | #endif // WEBRTC_P2P_BASE_PORT_H_ |