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 | |
honghaiz | 32f3996 | 2015-11-17 11:36:31 -0800 | [diff] [blame] | 19 | #include "webrtc/base/asyncinvoker.h" |
| 20 | #include "webrtc/base/asyncpacketsocket.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 21 | #include "webrtc/p2p/base/port.h" |
| 22 | #include "webrtc/p2p/client/basicportallocator.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. |
| 41 | STATE_DISCONNECTED, // TCP connection died, cannot send any packets. |
| 42 | }; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 43 | static TurnPort* Create(rtc::Thread* thread, |
| 44 | rtc::PacketSocketFactory* factory, |
| 45 | rtc::Network* network, |
| 46 | rtc::AsyncPacketSocket* socket, |
| 47 | const std::string& username, // ice username. |
| 48 | const std::string& password, // ice password. |
| 49 | const ProtocolAddress& server_address, |
| 50 | const RelayCredentials& credentials, |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 51 | int server_priority, |
| 52 | const std::string& origin) { |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 53 | return new TurnPort(thread, factory, network, socket, username, password, |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 54 | server_address, credentials, server_priority, origin); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | static TurnPort* Create(rtc::Thread* thread, |
| 58 | rtc::PacketSocketFactory* factory, |
| 59 | rtc::Network* network, |
| 60 | const rtc::IPAddress& ip, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 61 | uint16_t min_port, |
| 62 | uint16_t max_port, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 63 | const std::string& username, // ice username. |
| 64 | const std::string& password, // ice password. |
| 65 | const ProtocolAddress& server_address, |
| 66 | const RelayCredentials& credentials, |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 67 | int server_priority, |
| 68 | const std::string& origin) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 69 | return new TurnPort(thread, factory, network, ip, min_port, max_port, |
| 70 | username, password, server_address, credentials, |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 71 | server_priority, origin); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | virtual ~TurnPort(); |
| 75 | |
| 76 | const ProtocolAddress& server_address() const { return server_address_; } |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 77 | // Returns an empty address if the local address has not been assigned. |
| 78 | rtc::SocketAddress GetLocalAddress() const; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 79 | |
honghaiz | b19eba3 | 2015-08-03 10:23:31 -0700 | [diff] [blame] | 80 | bool ready() const { return state_ == STATE_READY; } |
| 81 | bool connected() const { |
| 82 | return state_ == STATE_READY || state_ == STATE_CONNECTED; |
| 83 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 84 | const RelayCredentials& credentials() const { return credentials_; } |
| 85 | |
| 86 | virtual void PrepareAddress(); |
| 87 | virtual Connection* CreateConnection( |
| 88 | const Candidate& c, PortInterface::CandidateOrigin origin); |
| 89 | virtual int SendTo(const void* data, size_t size, |
| 90 | const rtc::SocketAddress& addr, |
| 91 | const rtc::PacketOptions& options, |
| 92 | bool payload); |
| 93 | virtual int SetOption(rtc::Socket::Option opt, int value); |
| 94 | virtual int GetOption(rtc::Socket::Option opt, int* value); |
| 95 | virtual int GetError(); |
| 96 | |
| 97 | virtual bool HandleIncomingPacket( |
| 98 | rtc::AsyncPacketSocket* socket, const char* data, size_t size, |
| 99 | const rtc::SocketAddress& remote_addr, |
| 100 | const rtc::PacketTime& packet_time) { |
| 101 | OnReadPacket(socket, data, size, remote_addr, packet_time); |
| 102 | return true; |
| 103 | } |
| 104 | virtual void OnReadPacket(rtc::AsyncPacketSocket* socket, |
| 105 | const char* data, size_t size, |
| 106 | const rtc::SocketAddress& remote_addr, |
| 107 | const rtc::PacketTime& packet_time); |
| 108 | |
| 109 | virtual void OnReadyToSend(rtc::AsyncPacketSocket* socket); |
| 110 | |
| 111 | void OnSocketConnect(rtc::AsyncPacketSocket* socket); |
| 112 | void OnSocketClose(rtc::AsyncPacketSocket* socket, int error); |
| 113 | |
| 114 | |
| 115 | const std::string& hash() const { return hash_; } |
| 116 | const std::string& nonce() const { return nonce_; } |
| 117 | |
| 118 | int error() const { return error_; } |
| 119 | |
| 120 | void OnAllocateMismatch(); |
| 121 | |
| 122 | rtc::AsyncPacketSocket* socket() const { |
| 123 | return socket_; |
| 124 | } |
| 125 | |
honghaiz | 32f3996 | 2015-11-17 11:36:31 -0800 | [diff] [blame] | 126 | // For testing only. |
| 127 | rtc::AsyncInvoker* invoker() { return &invoker_; } |
| 128 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 129 | // Signal with resolved server address. |
| 130 | // Parameters are port, server address and resolved server address. |
| 131 | // This signal will be sent only if server address is resolved successfully. |
| 132 | sigslot::signal3<TurnPort*, |
| 133 | const rtc::SocketAddress&, |
| 134 | const rtc::SocketAddress&> SignalResolvedServerAddress; |
| 135 | |
Honghai Zhang | f67c548 | 2015-12-11 15:16:54 -0800 | [diff] [blame^] | 136 | // All public methods/signals below are for testing only. |
| 137 | sigslot::signal2<TurnPort*, int> SignalTurnRefreshResult; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 138 | sigslot::signal3<TurnPort*, const rtc::SocketAddress&, int> |
| 139 | SignalCreatePermissionResult; |
Honghai Zhang | 8597543 | 2015-11-12 11:07:12 -0800 | [diff] [blame] | 140 | void FlushRequests() { request_manager_.Flush(); } |
Honghai Zhang | f67c548 | 2015-12-11 15:16:54 -0800 | [diff] [blame^] | 141 | void set_credentials(RelayCredentials& credentials) { |
| 142 | credentials_ = credentials; |
| 143 | } |
| 144 | // Finds the turn entry with |address| and sets its channel id. |
| 145 | // Returns true if the entry is found. |
| 146 | bool SetEntryChannelId(const rtc::SocketAddress& address, int channel_id); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 147 | |
| 148 | protected: |
| 149 | TurnPort(rtc::Thread* thread, |
| 150 | rtc::PacketSocketFactory* factory, |
| 151 | rtc::Network* network, |
| 152 | rtc::AsyncPacketSocket* socket, |
| 153 | const std::string& username, |
| 154 | const std::string& password, |
| 155 | const ProtocolAddress& server_address, |
| 156 | const RelayCredentials& credentials, |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 157 | int server_priority, |
| 158 | const std::string& origin); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 159 | |
| 160 | TurnPort(rtc::Thread* thread, |
| 161 | rtc::PacketSocketFactory* factory, |
| 162 | rtc::Network* network, |
| 163 | const rtc::IPAddress& ip, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 164 | uint16_t min_port, |
| 165 | uint16_t max_port, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 166 | const std::string& username, |
| 167 | const std::string& password, |
| 168 | const ProtocolAddress& server_address, |
| 169 | const RelayCredentials& credentials, |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 170 | int server_priority, |
| 171 | const std::string& origin); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 172 | |
| 173 | private: |
| 174 | enum { |
Honghai Zhang | f67c548 | 2015-12-11 15:16:54 -0800 | [diff] [blame^] | 175 | MSG_ALLOCATE_ERROR = MSG_FIRST_AVAILABLE, |
guoweis@webrtc.org | 19e4e8d | 2015-01-10 02:41:32 +0000 | [diff] [blame] | 176 | MSG_ALLOCATE_MISMATCH, |
| 177 | MSG_TRY_ALTERNATE_SERVER |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | typedef std::list<TurnEntry*> EntryList; |
| 181 | typedef std::map<rtc::Socket::Option, int> SocketOptionsMap; |
| 182 | typedef std::set<rtc::SocketAddress> AttemptedServerSet; |
| 183 | |
| 184 | virtual void OnMessage(rtc::Message* pmsg); |
| 185 | |
| 186 | bool CreateTurnClientSocket(); |
| 187 | |
| 188 | void set_nonce(const std::string& nonce) { nonce_ = nonce; } |
| 189 | void set_realm(const std::string& realm) { |
| 190 | if (realm != realm_) { |
| 191 | realm_ = realm; |
| 192 | UpdateHash(); |
| 193 | } |
| 194 | } |
| 195 | |
Honghai Zhang | f67c548 | 2015-12-11 15:16:54 -0800 | [diff] [blame^] | 196 | // Shuts down the turn port, usually because of some fatal errors. |
| 197 | void Close(); |
| 198 | void OnTurnRefreshError() { Close(); } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 199 | bool SetAlternateServer(const rtc::SocketAddress& address); |
| 200 | void ResolveTurnAddress(const rtc::SocketAddress& address); |
| 201 | void OnResolveResult(rtc::AsyncResolverInterface* resolver); |
| 202 | |
| 203 | void AddRequestAuthInfo(StunMessage* msg); |
| 204 | void OnSendStunPacket(const void* data, size_t size, StunRequest* request); |
| 205 | // Stun address from allocate success response. |
| 206 | // Currently used only for testing. |
| 207 | void OnStunAddress(const rtc::SocketAddress& address); |
| 208 | void OnAllocateSuccess(const rtc::SocketAddress& address, |
| 209 | const rtc::SocketAddress& stun_address); |
| 210 | void OnAllocateError(); |
| 211 | void OnAllocateRequestTimeout(); |
| 212 | |
| 213 | void HandleDataIndication(const char* data, size_t size, |
| 214 | const rtc::PacketTime& packet_time); |
| 215 | void HandleChannelData(int channel_id, const char* data, size_t size, |
| 216 | const rtc::PacketTime& packet_time); |
| 217 | void DispatchPacket(const char* data, size_t size, |
| 218 | const rtc::SocketAddress& remote_addr, |
| 219 | ProtocolType proto, const rtc::PacketTime& packet_time); |
| 220 | |
| 221 | bool ScheduleRefresh(int lifetime); |
| 222 | void SendRequest(StunRequest* request, int delay); |
| 223 | int Send(const void* data, size_t size, |
| 224 | const rtc::PacketOptions& options); |
| 225 | void UpdateHash(); |
| 226 | bool UpdateNonce(StunMessage* response); |
| 227 | |
| 228 | bool HasPermission(const rtc::IPAddress& ipaddr) const; |
| 229 | TurnEntry* FindEntry(const rtc::SocketAddress& address) const; |
| 230 | TurnEntry* FindEntry(int channel_id) const; |
honghaiz | c3e0fe7 | 2015-12-02 16:43:25 -0800 | [diff] [blame] | 231 | bool EntryExists(TurnEntry* e); |
honghaiz | 32f3996 | 2015-11-17 11:36:31 -0800 | [diff] [blame] | 232 | void CreateOrRefreshEntry(const rtc::SocketAddress& address); |
| 233 | void DestroyEntry(TurnEntry* entry); |
| 234 | // Destroys the entry only if |timestamp| matches the destruction timestamp |
| 235 | // in |entry|. |
| 236 | void DestroyEntryIfNotCancelled(TurnEntry* entry, uint32_t timestamp); |
| 237 | void ScheduleEntryDestruction(TurnEntry* entry); |
| 238 | void CancelEntryDestruction(TurnEntry* entry); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 239 | void OnConnectionDestroyed(Connection* conn); |
| 240 | |
Honghai Zhang | f67c548 | 2015-12-11 15:16:54 -0800 | [diff] [blame^] | 241 | // Destroys the connection with remote address |address|. Returns true if |
| 242 | // a connection is found and destroyed. |
| 243 | bool DestroyConnection(const rtc::SocketAddress& address); |
| 244 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 245 | ProtocolAddress server_address_; |
| 246 | RelayCredentials credentials_; |
| 247 | AttemptedServerSet attempted_server_addresses_; |
| 248 | |
| 249 | rtc::AsyncPacketSocket* socket_; |
| 250 | SocketOptionsMap socket_options_; |
| 251 | rtc::AsyncResolverInterface* resolver_; |
| 252 | int error_; |
| 253 | |
| 254 | StunRequestManager request_manager_; |
| 255 | std::string realm_; // From 401/438 response message. |
| 256 | std::string nonce_; // From 401/438 response message. |
| 257 | std::string hash_; // Digest of username:realm:password |
| 258 | |
| 259 | int next_channel_number_; |
| 260 | EntryList entries_; |
| 261 | |
honghaiz | b19eba3 | 2015-08-03 10:23:31 -0700 | [diff] [blame] | 262 | PortState state_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 263 | // By default the value will be set to 0. This value will be used in |
| 264 | // calculating the candidate priority. |
| 265 | int server_priority_; |
| 266 | |
| 267 | // The number of retries made due to allocate mismatch error. |
| 268 | size_t allocate_mismatch_retries_; |
| 269 | |
honghaiz | 32f3996 | 2015-11-17 11:36:31 -0800 | [diff] [blame] | 270 | rtc::AsyncInvoker invoker_; |
| 271 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 272 | friend class TurnEntry; |
| 273 | friend class TurnAllocateRequest; |
| 274 | friend class TurnRefreshRequest; |
| 275 | friend class TurnCreatePermissionRequest; |
| 276 | friend class TurnChannelBindRequest; |
| 277 | }; |
| 278 | |
| 279 | } // namespace cricket |
| 280 | |
| 281 | #endif // WEBRTC_P2P_BASE_TURNPORT_H_ |