henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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_TURNPORT_H_ |
| 12 | #define WEBRTC_P2P_BASE_TURNPORT_H_ |
| 13 | |
| 14 | #include <stdio.h> |
| 15 | #include <list> |
| 16 | #include <set> |
| 17 | #include <string> |
| 18 | |
| 19 | #include "webrtc/p2p/base/port.h" |
| 20 | #include "webrtc/p2p/client/basicportallocator.h" |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame] | 21 | #include "webrtc/rtc_base/asyncinvoker.h" |
| 22 | #include "webrtc/rtc_base/asyncpacketsocket.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 23 | |
| 24 | namespace rtc { |
| 25 | class AsyncResolver; |
| 26 | class SignalThread; |
| 27 | } |
| 28 | |
| 29 | namespace cricket { |
| 30 | |
| 31 | extern const char TURN_PORT_TYPE[]; |
| 32 | class TurnAllocateRequest; |
| 33 | class TurnEntry; |
| 34 | |
| 35 | class TurnPort : public Port { |
| 36 | public: |
honghaiz | b19eba3 | 2015-08-03 10:23:31 -0700 | [diff] [blame] | 37 | enum PortState { |
| 38 | STATE_CONNECTING, // Initial state, cannot send any packets. |
| 39 | STATE_CONNECTED, // Socket connected, ready to send stun requests. |
| 40 | STATE_READY, // Received allocate success, can send any packets. |
honghaiz | 079a7a1 | 2016-06-22 16:26:29 -0700 | [diff] [blame] | 41 | STATE_RECEIVEONLY, // Had REFRESH_REQUEST error, cannot send any packets. |
| 42 | STATE_DISCONNECTED, // TCP connection died, cannot send/receive any |
| 43 | // packets. |
honghaiz | b19eba3 | 2015-08-03 10:23:31 -0700 | [diff] [blame] | 44 | }; |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 45 | // Create a TURN port using the shared UDP socket, |socket|. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 46 | static TurnPort* Create(rtc::Thread* thread, |
| 47 | rtc::PacketSocketFactory* factory, |
| 48 | rtc::Network* network, |
| 49 | rtc::AsyncPacketSocket* socket, |
| 50 | const std::string& username, // ice username. |
| 51 | const std::string& password, // ice password. |
| 52 | const ProtocolAddress& server_address, |
| 53 | const RelayCredentials& credentials, |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 54 | int server_priority, |
maxmorin | e9ef907 | 2017-08-29 04:49:00 -0700 | [diff] [blame] | 55 | const std::string& origin) { |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 56 | return new TurnPort(thread, factory, network, socket, username, password, |
maxmorin | e9ef907 | 2017-08-29 04:49:00 -0700 | [diff] [blame] | 57 | server_address, credentials, server_priority, origin); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 58 | } |
| 59 | |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 60 | // Create a TURN port that will use a new socket, bound to |network| and |
| 61 | // using a port in the range between |min_port| and |max_port|. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 62 | static TurnPort* Create(rtc::Thread* thread, |
| 63 | rtc::PacketSocketFactory* factory, |
| 64 | rtc::Network* network, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 65 | uint16_t min_port, |
| 66 | uint16_t max_port, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 67 | const std::string& username, // ice username. |
| 68 | const std::string& password, // ice password. |
| 69 | const ProtocolAddress& server_address, |
| 70 | const RelayCredentials& credentials, |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 71 | int server_priority, |
Diogo Real | 1dca9d5 | 2017-08-29 12:18:32 -0700 | [diff] [blame] | 72 | const std::string& origin, |
Diogo Real | 7bd1f1b | 2017-09-08 12:50:41 -0700 | [diff] [blame] | 73 | const std::vector<std::string>& tls_alpn_protocols, |
| 74 | const std::vector<std::string>& tls_elliptic_curves) { |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 75 | return new TurnPort(thread, factory, network, min_port, max_port, username, |
| 76 | password, server_address, credentials, server_priority, |
Diogo Real | 7bd1f1b | 2017-09-08 12:50:41 -0700 | [diff] [blame] | 77 | origin, tls_alpn_protocols, tls_elliptic_curves); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | virtual ~TurnPort(); |
| 81 | |
| 82 | const ProtocolAddress& server_address() const { return server_address_; } |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 83 | // Returns an empty address if the local address has not been assigned. |
| 84 | rtc::SocketAddress GetLocalAddress() const; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 85 | |
honghaiz | b19eba3 | 2015-08-03 10:23:31 -0700 | [diff] [blame] | 86 | bool ready() const { return state_ == STATE_READY; } |
| 87 | bool connected() const { |
| 88 | return state_ == STATE_READY || state_ == STATE_CONNECTED; |
| 89 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 90 | const RelayCredentials& credentials() const { return credentials_; } |
| 91 | |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 92 | virtual ProtocolType GetProtocol() const { return server_address_.proto; } |
| 93 | |
hnsl | 0483362 | 2017-01-09 08:35:45 -0800 | [diff] [blame] | 94 | virtual TlsCertPolicy GetTlsCertPolicy() const { return tls_cert_policy_; } |
| 95 | |
| 96 | virtual void SetTlsCertPolicy(TlsCertPolicy tls_cert_policy) { |
| 97 | tls_cert_policy_ = tls_cert_policy; |
| 98 | } |
| 99 | |
Diogo Real | 1dca9d5 | 2017-08-29 12:18:32 -0700 | [diff] [blame] | 100 | virtual std::vector<std::string> GetTlsAlpnProtocols() const { |
| 101 | return tls_alpn_protocols_; |
| 102 | } |
| 103 | |
Diogo Real | 7bd1f1b | 2017-09-08 12:50:41 -0700 | [diff] [blame] | 104 | virtual std::vector<std::string> GetTlsEllipticCurves() const { |
| 105 | return tls_elliptic_curves_; |
| 106 | } |
| 107 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 108 | virtual void PrepareAddress(); |
| 109 | virtual Connection* CreateConnection( |
| 110 | const Candidate& c, PortInterface::CandidateOrigin origin); |
| 111 | virtual int SendTo(const void* data, size_t size, |
| 112 | const rtc::SocketAddress& addr, |
| 113 | const rtc::PacketOptions& options, |
| 114 | bool payload); |
| 115 | virtual int SetOption(rtc::Socket::Option opt, int value); |
| 116 | virtual int GetOption(rtc::Socket::Option opt, int* value); |
| 117 | virtual int GetError(); |
| 118 | |
Sergey Ulanov | 17fa672 | 2016-05-10 10:20:47 -0700 | [diff] [blame] | 119 | virtual bool HandleIncomingPacket(rtc::AsyncPacketSocket* socket, |
| 120 | const char* data, size_t size, |
| 121 | const rtc::SocketAddress& remote_addr, |
| 122 | const rtc::PacketTime& packet_time); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 123 | virtual void OnReadPacket(rtc::AsyncPacketSocket* socket, |
| 124 | const char* data, size_t size, |
| 125 | const rtc::SocketAddress& remote_addr, |
| 126 | const rtc::PacketTime& packet_time); |
| 127 | |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 128 | virtual void OnSentPacket(rtc::AsyncPacketSocket* socket, |
| 129 | const rtc::SentPacket& sent_packet); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 130 | virtual void OnReadyToSend(rtc::AsyncPacketSocket* socket); |
Honghai Zhang | f9945b2 | 2015-12-15 12:20:13 -0800 | [diff] [blame] | 131 | virtual bool SupportsProtocol(const std::string& protocol) const { |
| 132 | // Turn port only connects to UDP candidates. |
| 133 | return protocol == UDP_PROTOCOL_NAME; |
| 134 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 135 | |
| 136 | void OnSocketConnect(rtc::AsyncPacketSocket* socket); |
| 137 | void OnSocketClose(rtc::AsyncPacketSocket* socket, int error); |
| 138 | |
| 139 | |
| 140 | const std::string& hash() const { return hash_; } |
| 141 | const std::string& nonce() const { return nonce_; } |
| 142 | |
| 143 | int error() const { return error_; } |
| 144 | |
| 145 | void OnAllocateMismatch(); |
| 146 | |
| 147 | rtc::AsyncPacketSocket* socket() const { |
| 148 | return socket_; |
| 149 | } |
| 150 | |
honghaiz | 32f3996 | 2015-11-17 11:36:31 -0800 | [diff] [blame] | 151 | // For testing only. |
| 152 | rtc::AsyncInvoker* invoker() { return &invoker_; } |
| 153 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 154 | // Signal with resolved server address. |
| 155 | // Parameters are port, server address and resolved server address. |
| 156 | // This signal will be sent only if server address is resolved successfully. |
| 157 | sigslot::signal3<TurnPort*, |
| 158 | const rtc::SocketAddress&, |
| 159 | const rtc::SocketAddress&> SignalResolvedServerAddress; |
| 160 | |
Honghai Zhang | f67c548 | 2015-12-11 15:16:54 -0800 | [diff] [blame] | 161 | // All public methods/signals below are for testing only. |
| 162 | sigslot::signal2<TurnPort*, int> SignalTurnRefreshResult; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 163 | sigslot::signal3<TurnPort*, const rtc::SocketAddress&, int> |
| 164 | SignalCreatePermissionResult; |
honghaiz | 6b9ab92 | 2016-01-05 09:06:12 -0800 | [diff] [blame] | 165 | void FlushRequests(int msg_type) { request_manager_.Flush(msg_type); } |
| 166 | bool HasRequests() { return !request_manager_.empty(); } |
Honghai Zhang | f67c548 | 2015-12-11 15:16:54 -0800 | [diff] [blame] | 167 | void set_credentials(RelayCredentials& credentials) { |
| 168 | credentials_ = credentials; |
| 169 | } |
| 170 | // Finds the turn entry with |address| and sets its channel id. |
| 171 | // Returns true if the entry is found. |
| 172 | bool SetEntryChannelId(const rtc::SocketAddress& address, int channel_id); |
honghaiz | 9dfed79 | 2016-01-29 13:22:31 -0800 | [diff] [blame] | 173 | // Visible for testing. |
| 174 | // Shuts down the turn port, usually because of some fatal errors. |
| 175 | void Close(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 176 | |
| 177 | protected: |
| 178 | TurnPort(rtc::Thread* thread, |
| 179 | rtc::PacketSocketFactory* factory, |
| 180 | rtc::Network* network, |
| 181 | rtc::AsyncPacketSocket* socket, |
| 182 | const std::string& username, |
| 183 | const std::string& password, |
| 184 | const ProtocolAddress& server_address, |
| 185 | const RelayCredentials& credentials, |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 186 | int server_priority, |
maxmorin | e9ef907 | 2017-08-29 04:49:00 -0700 | [diff] [blame] | 187 | const std::string& origin); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 188 | |
| 189 | TurnPort(rtc::Thread* thread, |
| 190 | rtc::PacketSocketFactory* factory, |
| 191 | rtc::Network* network, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 192 | uint16_t min_port, |
| 193 | uint16_t max_port, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 194 | const std::string& username, |
| 195 | const std::string& password, |
| 196 | const ProtocolAddress& server_address, |
| 197 | const RelayCredentials& credentials, |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 198 | int server_priority, |
Diogo Real | 1dca9d5 | 2017-08-29 12:18:32 -0700 | [diff] [blame] | 199 | const std::string& origin, |
Diogo Real | 7bd1f1b | 2017-09-08 12:50:41 -0700 | [diff] [blame] | 200 | const std::vector<std::string>& tls_alpn_protocols, |
| 201 | const std::vector<std::string>& tls_elliptic_curves); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 202 | |
| 203 | private: |
| 204 | enum { |
Honghai Zhang | f67c548 | 2015-12-11 15:16:54 -0800 | [diff] [blame] | 205 | MSG_ALLOCATE_ERROR = MSG_FIRST_AVAILABLE, |
guoweis@webrtc.org | 19e4e8d | 2015-01-10 02:41:32 +0000 | [diff] [blame] | 206 | MSG_ALLOCATE_MISMATCH, |
honghaiz | 6b9ab92 | 2016-01-05 09:06:12 -0800 | [diff] [blame] | 207 | MSG_TRY_ALTERNATE_SERVER, |
| 208 | MSG_REFRESH_ERROR |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 209 | }; |
| 210 | |
| 211 | typedef std::list<TurnEntry*> EntryList; |
| 212 | typedef std::map<rtc::Socket::Option, int> SocketOptionsMap; |
| 213 | typedef std::set<rtc::SocketAddress> AttemptedServerSet; |
| 214 | |
| 215 | virtual void OnMessage(rtc::Message* pmsg); |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 216 | virtual void HandleConnectionDestroyed(Connection* conn); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 217 | |
| 218 | bool CreateTurnClientSocket(); |
| 219 | |
| 220 | void set_nonce(const std::string& nonce) { nonce_ = nonce; } |
| 221 | void set_realm(const std::string& realm) { |
| 222 | if (realm != realm_) { |
| 223 | realm_ = realm; |
| 224 | UpdateHash(); |
| 225 | } |
| 226 | } |
| 227 | |
honghaiz | 079a7a1 | 2016-06-22 16:26:29 -0700 | [diff] [blame] | 228 | void OnRefreshError(); |
| 229 | void HandleRefreshError(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 230 | bool SetAlternateServer(const rtc::SocketAddress& address); |
| 231 | void ResolveTurnAddress(const rtc::SocketAddress& address); |
| 232 | void OnResolveResult(rtc::AsyncResolverInterface* resolver); |
| 233 | |
| 234 | void AddRequestAuthInfo(StunMessage* msg); |
| 235 | void OnSendStunPacket(const void* data, size_t size, StunRequest* request); |
| 236 | // Stun address from allocate success response. |
| 237 | // Currently used only for testing. |
| 238 | void OnStunAddress(const rtc::SocketAddress& address); |
| 239 | void OnAllocateSuccess(const rtc::SocketAddress& address, |
| 240 | const rtc::SocketAddress& stun_address); |
| 241 | void OnAllocateError(); |
| 242 | void OnAllocateRequestTimeout(); |
| 243 | |
| 244 | void HandleDataIndication(const char* data, size_t size, |
| 245 | const rtc::PacketTime& packet_time); |
| 246 | void HandleChannelData(int channel_id, const char* data, size_t size, |
| 247 | const rtc::PacketTime& packet_time); |
| 248 | void DispatchPacket(const char* data, size_t size, |
| 249 | const rtc::SocketAddress& remote_addr, |
| 250 | ProtocolType proto, const rtc::PacketTime& packet_time); |
| 251 | |
| 252 | bool ScheduleRefresh(int lifetime); |
| 253 | void SendRequest(StunRequest* request, int delay); |
| 254 | int Send(const void* data, size_t size, |
| 255 | const rtc::PacketOptions& options); |
| 256 | void UpdateHash(); |
| 257 | bool UpdateNonce(StunMessage* response); |
honghaiz | c463e20 | 2016-02-01 15:19:08 -0800 | [diff] [blame] | 258 | void ResetNonce(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 259 | |
| 260 | bool HasPermission(const rtc::IPAddress& ipaddr) const; |
| 261 | TurnEntry* FindEntry(const rtc::SocketAddress& address) const; |
| 262 | TurnEntry* FindEntry(int channel_id) const; |
honghaiz | c3e0fe7 | 2015-12-02 16:43:25 -0800 | [diff] [blame] | 263 | bool EntryExists(TurnEntry* e); |
honghaiz | 32f3996 | 2015-11-17 11:36:31 -0800 | [diff] [blame] | 264 | void CreateOrRefreshEntry(const rtc::SocketAddress& address); |
| 265 | void DestroyEntry(TurnEntry* entry); |
| 266 | // Destroys the entry only if |timestamp| matches the destruction timestamp |
| 267 | // in |entry|. |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 268 | void DestroyEntryIfNotCancelled(TurnEntry* entry, int64_t timestamp); |
honghaiz | 32f3996 | 2015-11-17 11:36:31 -0800 | [diff] [blame] | 269 | void ScheduleEntryDestruction(TurnEntry* entry); |
| 270 | void CancelEntryDestruction(TurnEntry* entry); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 271 | |
honghaiz | 079a7a1 | 2016-06-22 16:26:29 -0700 | [diff] [blame] | 272 | // Marks the connection with remote address |address| failed and |
| 273 | // pruned (a.k.a. write-timed-out). Returns true if a connection is found. |
| 274 | bool FailAndPruneConnection(const rtc::SocketAddress& address); |
Honghai Zhang | f67c548 | 2015-12-11 15:16:54 -0800 | [diff] [blame] | 275 | |
zhihuang | 26d99c2 | 2017-02-13 12:47:27 -0800 | [diff] [blame] | 276 | // Reconstruct the URL of the server which the candidate is gathered from. |
| 277 | std::string ReconstructedServerUrl(); |
| 278 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 279 | ProtocolAddress server_address_; |
hnsl | 0483362 | 2017-01-09 08:35:45 -0800 | [diff] [blame] | 280 | TlsCertPolicy tls_cert_policy_ = TlsCertPolicy::TLS_CERT_POLICY_SECURE; |
Diogo Real | 1dca9d5 | 2017-08-29 12:18:32 -0700 | [diff] [blame] | 281 | std::vector<std::string> tls_alpn_protocols_; |
Diogo Real | 7bd1f1b | 2017-09-08 12:50:41 -0700 | [diff] [blame] | 282 | std::vector<std::string> tls_elliptic_curves_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 283 | RelayCredentials credentials_; |
| 284 | AttemptedServerSet attempted_server_addresses_; |
| 285 | |
| 286 | rtc::AsyncPacketSocket* socket_; |
| 287 | SocketOptionsMap socket_options_; |
| 288 | rtc::AsyncResolverInterface* resolver_; |
| 289 | int error_; |
| 290 | |
| 291 | StunRequestManager request_manager_; |
| 292 | std::string realm_; // From 401/438 response message. |
| 293 | std::string nonce_; // From 401/438 response message. |
| 294 | std::string hash_; // Digest of username:realm:password |
| 295 | |
| 296 | int next_channel_number_; |
| 297 | EntryList entries_; |
| 298 | |
honghaiz | b19eba3 | 2015-08-03 10:23:31 -0700 | [diff] [blame] | 299 | PortState state_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 300 | // By default the value will be set to 0. This value will be used in |
| 301 | // calculating the candidate priority. |
| 302 | int server_priority_; |
| 303 | |
| 304 | // The number of retries made due to allocate mismatch error. |
| 305 | size_t allocate_mismatch_retries_; |
| 306 | |
honghaiz | 32f3996 | 2015-11-17 11:36:31 -0800 | [diff] [blame] | 307 | rtc::AsyncInvoker invoker_; |
| 308 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 309 | friend class TurnEntry; |
| 310 | friend class TurnAllocateRequest; |
| 311 | friend class TurnRefreshRequest; |
| 312 | friend class TurnCreatePermissionRequest; |
| 313 | friend class TurnChannelBindRequest; |
| 314 | }; |
| 315 | |
| 316 | } // namespace cricket |
| 317 | |
| 318 | #endif // WEBRTC_P2P_BASE_TURNPORT_H_ |