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" |
| 21 | #include "webrtc/base/asyncpacketsocket.h" |
| 22 | |
| 23 | namespace rtc { |
| 24 | class AsyncResolver; |
| 25 | class SignalThread; |
| 26 | } |
| 27 | |
| 28 | namespace cricket { |
| 29 | |
| 30 | extern const char TURN_PORT_TYPE[]; |
| 31 | class TurnAllocateRequest; |
| 32 | class TurnEntry; |
| 33 | |
| 34 | class TurnPort : public Port { |
| 35 | public: |
| 36 | static TurnPort* Create(rtc::Thread* thread, |
| 37 | rtc::PacketSocketFactory* factory, |
| 38 | rtc::Network* network, |
| 39 | rtc::AsyncPacketSocket* socket, |
| 40 | const std::string& username, // ice username. |
| 41 | const std::string& password, // ice password. |
| 42 | const ProtocolAddress& server_address, |
| 43 | const RelayCredentials& credentials, |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 44 | int server_priority, |
| 45 | const std::string& origin) { |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 46 | return new TurnPort(thread, factory, network, socket, username, password, |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 47 | server_address, credentials, server_priority, origin); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | static TurnPort* Create(rtc::Thread* thread, |
| 51 | rtc::PacketSocketFactory* factory, |
| 52 | rtc::Network* network, |
| 53 | const rtc::IPAddress& ip, |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 54 | uint16 min_port, |
| 55 | uint16 max_port, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 56 | const std::string& username, // ice username. |
| 57 | const std::string& password, // ice password. |
| 58 | const ProtocolAddress& server_address, |
| 59 | const RelayCredentials& credentials, |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 60 | int server_priority, |
| 61 | const std::string& origin) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 62 | return new TurnPort(thread, factory, network, ip, min_port, max_port, |
| 63 | username, password, server_address, credentials, |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 64 | server_priority, origin); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | virtual ~TurnPort(); |
| 68 | |
| 69 | const ProtocolAddress& server_address() const { return server_address_; } |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 70 | // Returns an empty address if the local address has not been assigned. |
| 71 | rtc::SocketAddress GetLocalAddress() const; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 72 | |
| 73 | bool connected() const { return connected_; } |
| 74 | const RelayCredentials& credentials() const { return credentials_; } |
| 75 | |
| 76 | virtual void PrepareAddress(); |
| 77 | virtual Connection* CreateConnection( |
| 78 | const Candidate& c, PortInterface::CandidateOrigin origin); |
| 79 | virtual int SendTo(const void* data, size_t size, |
| 80 | const rtc::SocketAddress& addr, |
| 81 | const rtc::PacketOptions& options, |
| 82 | bool payload); |
| 83 | virtual int SetOption(rtc::Socket::Option opt, int value); |
| 84 | virtual int GetOption(rtc::Socket::Option opt, int* value); |
| 85 | virtual int GetError(); |
| 86 | |
| 87 | virtual bool HandleIncomingPacket( |
| 88 | rtc::AsyncPacketSocket* socket, const char* data, size_t size, |
| 89 | const rtc::SocketAddress& remote_addr, |
| 90 | const rtc::PacketTime& packet_time) { |
| 91 | OnReadPacket(socket, data, size, remote_addr, packet_time); |
| 92 | return true; |
| 93 | } |
| 94 | virtual void OnReadPacket(rtc::AsyncPacketSocket* socket, |
| 95 | const char* data, size_t size, |
| 96 | const rtc::SocketAddress& remote_addr, |
| 97 | const rtc::PacketTime& packet_time); |
| 98 | |
| 99 | virtual void OnReadyToSend(rtc::AsyncPacketSocket* socket); |
| 100 | |
| 101 | void OnSocketConnect(rtc::AsyncPacketSocket* socket); |
| 102 | void OnSocketClose(rtc::AsyncPacketSocket* socket, int error); |
| 103 | |
| 104 | |
| 105 | const std::string& hash() const { return hash_; } |
| 106 | const std::string& nonce() const { return nonce_; } |
| 107 | |
| 108 | int error() const { return error_; } |
| 109 | |
| 110 | void OnAllocateMismatch(); |
| 111 | |
| 112 | rtc::AsyncPacketSocket* socket() const { |
| 113 | return socket_; |
| 114 | } |
| 115 | |
| 116 | // Signal with resolved server address. |
| 117 | // Parameters are port, server address and resolved server address. |
| 118 | // This signal will be sent only if server address is resolved successfully. |
| 119 | sigslot::signal3<TurnPort*, |
| 120 | const rtc::SocketAddress&, |
| 121 | const rtc::SocketAddress&> SignalResolvedServerAddress; |
| 122 | |
| 123 | // This signal is only for testing purpose. |
| 124 | sigslot::signal3<TurnPort*, const rtc::SocketAddress&, int> |
| 125 | SignalCreatePermissionResult; |
| 126 | |
| 127 | protected: |
| 128 | TurnPort(rtc::Thread* thread, |
| 129 | rtc::PacketSocketFactory* factory, |
| 130 | rtc::Network* network, |
| 131 | rtc::AsyncPacketSocket* socket, |
| 132 | const std::string& username, |
| 133 | const std::string& password, |
| 134 | const ProtocolAddress& server_address, |
| 135 | const RelayCredentials& credentials, |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 136 | int server_priority, |
| 137 | const std::string& origin); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 138 | |
| 139 | TurnPort(rtc::Thread* thread, |
| 140 | rtc::PacketSocketFactory* factory, |
| 141 | rtc::Network* network, |
| 142 | const rtc::IPAddress& ip, |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 143 | uint16 min_port, |
| 144 | uint16 max_port, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 145 | const std::string& username, |
| 146 | const std::string& password, |
| 147 | const ProtocolAddress& server_address, |
| 148 | const RelayCredentials& credentials, |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 149 | int server_priority, |
| 150 | const std::string& origin); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 151 | |
| 152 | private: |
| 153 | enum { |
| 154 | MSG_ERROR = MSG_FIRST_AVAILABLE, |
guoweis@webrtc.org | 19e4e8d | 2015-01-10 02:41:32 +0000 | [diff] [blame^] | 155 | MSG_ALLOCATE_MISMATCH, |
| 156 | MSG_TRY_ALTERNATE_SERVER |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | typedef std::list<TurnEntry*> EntryList; |
| 160 | typedef std::map<rtc::Socket::Option, int> SocketOptionsMap; |
| 161 | typedef std::set<rtc::SocketAddress> AttemptedServerSet; |
| 162 | |
| 163 | virtual void OnMessage(rtc::Message* pmsg); |
| 164 | |
| 165 | bool CreateTurnClientSocket(); |
| 166 | |
| 167 | void set_nonce(const std::string& nonce) { nonce_ = nonce; } |
| 168 | void set_realm(const std::string& realm) { |
| 169 | if (realm != realm_) { |
| 170 | realm_ = realm; |
| 171 | UpdateHash(); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | bool SetAlternateServer(const rtc::SocketAddress& address); |
| 176 | void ResolveTurnAddress(const rtc::SocketAddress& address); |
| 177 | void OnResolveResult(rtc::AsyncResolverInterface* resolver); |
| 178 | |
| 179 | void AddRequestAuthInfo(StunMessage* msg); |
| 180 | void OnSendStunPacket(const void* data, size_t size, StunRequest* request); |
| 181 | // Stun address from allocate success response. |
| 182 | // Currently used only for testing. |
| 183 | void OnStunAddress(const rtc::SocketAddress& address); |
| 184 | void OnAllocateSuccess(const rtc::SocketAddress& address, |
| 185 | const rtc::SocketAddress& stun_address); |
| 186 | void OnAllocateError(); |
| 187 | void OnAllocateRequestTimeout(); |
| 188 | |
| 189 | void HandleDataIndication(const char* data, size_t size, |
| 190 | const rtc::PacketTime& packet_time); |
| 191 | void HandleChannelData(int channel_id, const char* data, size_t size, |
| 192 | const rtc::PacketTime& packet_time); |
| 193 | void DispatchPacket(const char* data, size_t size, |
| 194 | const rtc::SocketAddress& remote_addr, |
| 195 | ProtocolType proto, const rtc::PacketTime& packet_time); |
| 196 | |
| 197 | bool ScheduleRefresh(int lifetime); |
| 198 | void SendRequest(StunRequest* request, int delay); |
| 199 | int Send(const void* data, size_t size, |
| 200 | const rtc::PacketOptions& options); |
| 201 | void UpdateHash(); |
| 202 | bool UpdateNonce(StunMessage* response); |
| 203 | |
| 204 | bool HasPermission(const rtc::IPAddress& ipaddr) const; |
| 205 | TurnEntry* FindEntry(const rtc::SocketAddress& address) const; |
| 206 | TurnEntry* FindEntry(int channel_id) const; |
| 207 | TurnEntry* CreateEntry(const rtc::SocketAddress& address); |
| 208 | void DestroyEntry(const rtc::SocketAddress& address); |
| 209 | void OnConnectionDestroyed(Connection* conn); |
| 210 | |
| 211 | ProtocolAddress server_address_; |
| 212 | RelayCredentials credentials_; |
| 213 | AttemptedServerSet attempted_server_addresses_; |
| 214 | |
| 215 | rtc::AsyncPacketSocket* socket_; |
| 216 | SocketOptionsMap socket_options_; |
| 217 | rtc::AsyncResolverInterface* resolver_; |
| 218 | int error_; |
| 219 | |
| 220 | StunRequestManager request_manager_; |
| 221 | std::string realm_; // From 401/438 response message. |
| 222 | std::string nonce_; // From 401/438 response message. |
| 223 | std::string hash_; // Digest of username:realm:password |
| 224 | |
| 225 | int next_channel_number_; |
| 226 | EntryList entries_; |
| 227 | |
| 228 | bool connected_; |
| 229 | // By default the value will be set to 0. This value will be used in |
| 230 | // calculating the candidate priority. |
| 231 | int server_priority_; |
| 232 | |
| 233 | // The number of retries made due to allocate mismatch error. |
| 234 | size_t allocate_mismatch_retries_; |
| 235 | |
| 236 | friend class TurnEntry; |
| 237 | friend class TurnAllocateRequest; |
| 238 | friend class TurnRefreshRequest; |
| 239 | friend class TurnCreatePermissionRequest; |
| 240 | friend class TurnChannelBindRequest; |
| 241 | }; |
| 242 | |
| 243 | } // namespace cricket |
| 244 | |
| 245 | #endif // WEBRTC_P2P_BASE_TURNPORT_H_ |