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