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 | // P2PTransportChannel wraps up the state management of the connection between |
| 12 | // two P2P clients. Clients have candidate ports for connecting, and |
| 13 | // connections which are combinations of candidates from each end (Alice and |
| 14 | // Bob each have candidates, one candidate from Alice and one candidate from |
| 15 | // Bob are used to make a connection, repeat to make many connections). |
| 16 | // |
| 17 | // When all of the available connections become invalid (non-writable), we |
| 18 | // kick off a process of determining more candidates and more connections. |
| 19 | // |
| 20 | #ifndef WEBRTC_P2P_BASE_P2PTRANSPORTCHANNEL_H_ |
| 21 | #define WEBRTC_P2P_BASE_P2PTRANSPORTCHANNEL_H_ |
| 22 | |
| 23 | #include <map> |
| 24 | #include <string> |
| 25 | #include <vector> |
| 26 | #include "webrtc/p2p/base/candidate.h" |
| 27 | #include "webrtc/p2p/base/p2ptransport.h" |
| 28 | #include "webrtc/p2p/base/portallocator.h" |
| 29 | #include "webrtc/p2p/base/portinterface.h" |
| 30 | #include "webrtc/p2p/base/transport.h" |
| 31 | #include "webrtc/p2p/base/transportchannelimpl.h" |
| 32 | #include "webrtc/base/asyncpacketsocket.h" |
| 33 | #include "webrtc/base/sigslot.h" |
| 34 | |
| 35 | namespace cricket { |
| 36 | |
| 37 | // Adds the port on which the candidate originated. |
| 38 | class RemoteCandidate : public Candidate { |
| 39 | public: |
| 40 | RemoteCandidate(const Candidate& c, PortInterface* origin_port) |
| 41 | : Candidate(c), origin_port_(origin_port) {} |
| 42 | |
| 43 | PortInterface* origin_port() { return origin_port_; } |
| 44 | |
| 45 | private: |
| 46 | PortInterface* origin_port_; |
| 47 | }; |
| 48 | |
| 49 | // P2PTransportChannel manages the candidates and connection process to keep |
| 50 | // two P2P clients connected to each other. |
| 51 | class P2PTransportChannel : public TransportChannelImpl, |
| 52 | public rtc::MessageHandler { |
| 53 | public: |
| 54 | P2PTransportChannel(const std::string& content_name, |
| 55 | int component, |
| 56 | P2PTransport* transport, |
| 57 | PortAllocator *allocator); |
| 58 | virtual ~P2PTransportChannel(); |
| 59 | |
| 60 | // From TransportChannelImpl: |
| 61 | virtual Transport* GetTransport() { return transport_; } |
| 62 | virtual void SetIceRole(IceRole role); |
| 63 | virtual IceRole GetIceRole() const { return ice_role_; } |
| 64 | virtual void SetIceTiebreaker(uint64 tiebreaker); |
| 65 | virtual size_t GetConnectionCount() const { return connections_.size(); } |
| 66 | virtual bool GetIceProtocolType(IceProtocolType* type) const; |
| 67 | virtual void SetIceProtocolType(IceProtocolType type); |
| 68 | virtual void SetIceCredentials(const std::string& ice_ufrag, |
| 69 | const std::string& ice_pwd); |
| 70 | virtual void SetRemoteIceCredentials(const std::string& ice_ufrag, |
| 71 | const std::string& ice_pwd); |
| 72 | virtual void SetRemoteIceMode(IceMode mode); |
| 73 | virtual void Connect(); |
| 74 | virtual void Reset(); |
| 75 | virtual void OnSignalingReady(); |
| 76 | virtual void OnCandidate(const Candidate& candidate); |
| 77 | |
| 78 | // From TransportChannel: |
| 79 | virtual int SendPacket(const char *data, size_t len, |
| 80 | const rtc::PacketOptions& options, int flags); |
| 81 | virtual int SetOption(rtc::Socket::Option opt, int value); |
| 82 | virtual int GetError() { return error_; } |
| 83 | virtual bool GetStats(std::vector<ConnectionInfo>* stats); |
| 84 | |
| 85 | const Connection* best_connection() const { return best_connection_; } |
| 86 | void set_incoming_only(bool value) { incoming_only_ = value; } |
| 87 | |
| 88 | // Note: This is only for testing purpose. |
| 89 | // |ports_| should not be changed from outside. |
| 90 | const std::vector<PortInterface *>& ports() { return ports_; } |
| 91 | |
| 92 | IceMode remote_ice_mode() const { return remote_ice_mode_; } |
| 93 | |
| 94 | // DTLS methods. |
| 95 | virtual bool IsDtlsActive() const { return false; } |
| 96 | |
| 97 | // Default implementation. |
| 98 | virtual bool GetSslRole(rtc::SSLRole* role) const { |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | virtual bool SetSslRole(rtc::SSLRole role) { |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | // Set up the ciphers to use for DTLS-SRTP. |
| 107 | virtual bool SetSrtpCiphers(const std::vector<std::string>& ciphers) { |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | // Find out which DTLS-SRTP cipher was negotiated |
| 112 | virtual bool GetSrtpCipher(std::string* cipher) { |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | // Returns false because the channel is not encrypted by default. |
| 117 | virtual bool GetLocalIdentity(rtc::SSLIdentity** identity) const { |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | virtual bool GetRemoteCertificate(rtc::SSLCertificate** cert) const { |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | // Allows key material to be extracted for external encryption. |
| 126 | virtual bool ExportKeyingMaterial( |
| 127 | const std::string& label, |
| 128 | const uint8* context, |
| 129 | size_t context_len, |
| 130 | bool use_context, |
| 131 | uint8* result, |
| 132 | size_t result_len) { |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | virtual bool SetLocalIdentity(rtc::SSLIdentity* identity) { |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | // Set DTLS Remote fingerprint. Must be after local identity set. |
| 141 | virtual bool SetRemoteFingerprint( |
| 142 | const std::string& digest_alg, |
| 143 | const uint8* digest, |
| 144 | size_t digest_len) { |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | // Helper method used only in unittest. |
| 149 | rtc::DiffServCodePoint DefaultDscpValue() const; |
| 150 | |
| 151 | private: |
| 152 | rtc::Thread* thread() { return worker_thread_; } |
| 153 | PortAllocatorSession* allocator_session() { |
| 154 | return allocator_sessions_.back(); |
| 155 | } |
| 156 | |
| 157 | void Allocate(); |
| 158 | void UpdateConnectionStates(); |
| 159 | void RequestSort(); |
| 160 | void SortConnections(); |
| 161 | void SwitchBestConnectionTo(Connection* conn); |
| 162 | void UpdateChannelState(); |
| 163 | void HandleWritable(); |
| 164 | void HandleNotWritable(); |
| 165 | void HandleAllTimedOut(); |
| 166 | |
| 167 | Connection* GetBestConnectionOnNetwork(rtc::Network* network); |
| 168 | bool CreateConnections(const Candidate &remote_candidate, |
| 169 | PortInterface* origin_port, bool readable); |
| 170 | bool CreateConnection(PortInterface* port, const Candidate& remote_candidate, |
| 171 | PortInterface* origin_port, bool readable); |
| 172 | bool FindConnection(cricket::Connection* connection) const; |
| 173 | |
| 174 | uint32 GetRemoteCandidateGeneration(const Candidate& candidate); |
| 175 | bool IsDuplicateRemoteCandidate(const Candidate& candidate); |
| 176 | void RememberRemoteCandidate(const Candidate& remote_candidate, |
| 177 | PortInterface* origin_port); |
| 178 | bool IsPingable(Connection* conn); |
| 179 | Connection* FindNextPingableConnection(); |
| 180 | void PingConnection(Connection* conn); |
| 181 | void AddAllocatorSession(PortAllocatorSession* session); |
| 182 | void AddConnection(Connection* connection); |
| 183 | |
| 184 | void OnPortReady(PortAllocatorSession *session, PortInterface* port); |
| 185 | void OnCandidatesReady(PortAllocatorSession *session, |
| 186 | const std::vector<Candidate>& candidates); |
| 187 | void OnCandidatesAllocationDone(PortAllocatorSession* session); |
| 188 | void OnUnknownAddress(PortInterface* port, |
| 189 | const rtc::SocketAddress& addr, |
| 190 | ProtocolType proto, |
| 191 | IceMessage* stun_msg, |
| 192 | const std::string& remote_username, |
| 193 | bool port_muxed); |
| 194 | void OnPortDestroyed(PortInterface* port); |
| 195 | void OnRoleConflict(PortInterface* port); |
| 196 | |
| 197 | void OnConnectionStateChange(Connection* connection); |
| 198 | void OnReadPacket(Connection *connection, const char *data, size_t len, |
| 199 | const rtc::PacketTime& packet_time); |
| 200 | void OnReadyToSend(Connection* connection); |
| 201 | void OnConnectionDestroyed(Connection *connection); |
| 202 | |
| 203 | void OnUseCandidate(Connection* conn); |
| 204 | |
| 205 | virtual void OnMessage(rtc::Message *pmsg); |
| 206 | void OnSort(); |
| 207 | void OnPing(); |
| 208 | |
| 209 | P2PTransport* transport_; |
| 210 | PortAllocator *allocator_; |
| 211 | rtc::Thread *worker_thread_; |
| 212 | bool incoming_only_; |
| 213 | bool waiting_for_signaling_; |
| 214 | int error_; |
| 215 | std::vector<PortAllocatorSession*> allocator_sessions_; |
| 216 | std::vector<PortInterface *> ports_; |
| 217 | std::vector<Connection *> connections_; |
| 218 | Connection* best_connection_; |
| 219 | // Connection selected by the controlling agent. This should be used only |
| 220 | // at controlled side when protocol type is RFC5245. |
| 221 | Connection* pending_best_connection_; |
| 222 | std::vector<RemoteCandidate> remote_candidates_; |
| 223 | bool sort_dirty_; // indicates whether another sort is needed right now |
| 224 | bool was_writable_; |
| 225 | typedef std::map<rtc::Socket::Option, int> OptionMap; |
| 226 | OptionMap options_; |
| 227 | std::string ice_ufrag_; |
| 228 | std::string ice_pwd_; |
| 229 | std::string remote_ice_ufrag_; |
| 230 | std::string remote_ice_pwd_; |
| 231 | IceProtocolType protocol_type_; |
| 232 | IceMode remote_ice_mode_; |
| 233 | IceRole ice_role_; |
| 234 | uint64 tiebreaker_; |
| 235 | uint32 remote_candidate_generation_; |
| 236 | |
| 237 | DISALLOW_EVIL_CONSTRUCTORS(P2PTransportChannel); |
| 238 | }; |
| 239 | |
| 240 | } // namespace cricket |
| 241 | |
| 242 | #endif // WEBRTC_P2P_BASE_P2PTRANSPORTCHANNEL_H_ |