| 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 P2P_BASE_TURN_SERVER_H_ |
| 12 | #define P2P_BASE_TURN_SERVER_H_ |
| 13 |
| 14 | #include <list> |
| 15 | #include <map> |
| 16 | #include <memory> |
| 17 | #include <set> |
| 18 | #include <string> |
| 19 | #include <utility> |
| 20 | #include <vector> |
| 21 |
| 22 | #include "absl/strings/string_view.h" |
| 23 | #include "api/sequence_checker.h" |
| 24 | #include "api/task_queue/pending_task_safety_flag.h" |
| 25 | #include "api/task_queue/task_queue_base.h" |
| 26 | #include "api/units/time_delta.h" |
| 27 | #include "p2p/base/port_interface.h" |
| 28 | #include "rtc_base/async_packet_socket.h" |
| 29 | #include "rtc_base/network/received_packet.h" |
| 30 | #include "rtc_base/socket_address.h" |
| 31 | #include "rtc_base/ssl_adapter.h" |
| 32 | #include "rtc_base/third_party/sigslot/sigslot.h" |
| 33 |
| 34 | namespace rtc { |
| 35 | class ByteBufferWriter; |
| 36 | class PacketSocketFactory; |
| 37 | } // namespace rtc |
| 38 |
| 39 | namespace cricket { |
| 40 |
| 41 | class StunMessage; |
| 42 | class TurnMessage; |
| 43 | class TurnServer; |
| 44 |
| 45 | // The default server port for TURN, as specified in RFC5766. |
| 46 | const int TURN_SERVER_PORT = 3478; |
| 47 |
| 48 | // Encapsulates the client's connection to the server. |
| 49 | class TurnServerConnection { |
| 50 | public: |
| 51 | TurnServerConnection() : proto_(PROTO_UDP), socket_(NULL) {} |
| 52 | TurnServerConnection(const rtc::SocketAddress& src, |
| 53 | ProtocolType proto, |
| 54 | rtc::AsyncPacketSocket* socket); |
| 55 | const rtc::SocketAddress& src() const { return src_; } |
| 56 | rtc::AsyncPacketSocket* socket() { return socket_; } |
| 57 | bool operator==(const TurnServerConnection& t) const; |
| 58 | bool operator<(const TurnServerConnection& t) const; |
| 59 | std::string ToString() const; |
| 60 |
| 61 | private: |
| 62 | rtc::SocketAddress src_; |
| 63 | rtc::SocketAddress dst_; |
| 64 | cricket::ProtocolType proto_; |
| 65 | rtc::AsyncPacketSocket* socket_; |
| 66 | }; |
| 67 |
| 68 | // Encapsulates a TURN allocation. |
| 69 | // The object is created when an allocation request is received, and then |
| 70 | // handles TURN messages (via HandleTurnMessage) and channel data messages |
| 71 | // (via HandleChannelData) for this allocation when received by the server. |
| 72 | // The object informs the server when its lifetime timer expires. |
| 73 | class TurnServerAllocation { |
| 74 | public: |
| 75 | TurnServerAllocation(TurnServer* server_, |
| 76 | webrtc::TaskQueueBase* thread, |
| 77 | const TurnServerConnection& conn, |
| 78 | rtc::AsyncPacketSocket* server_socket, |
| 79 | absl::string_view key); |
| 80 | virtual ~TurnServerAllocation(); |
| 81 |
| 82 | TurnServerConnection* conn() { return &conn_; } |
| 83 | const std::string& key() const { return key_; } |
| 84 | const std::string& transaction_id() const { return transaction_id_; } |
| 85 | const std::string& username() const { return username_; } |
| 86 | const std::string& last_nonce() const { return last_nonce_; } |
| 87 | void set_last_nonce(absl::string_view nonce) { |
| 88 | last_nonce_ = std::string(nonce); |
| 89 | } |
| 90 |
| 91 | std::string ToString() const; |
| 92 |
| 93 | void HandleTurnMessage(const TurnMessage* msg); |
| 94 | void HandleChannelData(rtc::ArrayView<const uint8_t> payload); |
| 95 |
| 96 | private: |
| 97 | struct Channel { |
| 98 | webrtc::ScopedTaskSafety pending_delete; |
| 99 | int id; |
| 100 | rtc::SocketAddress peer; |
| 101 | }; |
| 102 | struct Permission { |
| 103 | webrtc::ScopedTaskSafety pending_delete; |
| 104 | rtc::IPAddress peer; |
| 105 | }; |
| 106 | using PermissionList = std::list<Permission>; |
| 107 | using ChannelList = std::list<Channel>; |
| 108 |
| 109 | void PostDeleteSelf(webrtc::TimeDelta delay); |
| 110 |
| 111 | void HandleAllocateRequest(const TurnMessage* msg); |
| 112 | void HandleRefreshRequest(const TurnMessage* msg); |
| 113 | void HandleSendIndication(const TurnMessage* msg); |
| 114 | void HandleCreatePermissionRequest(const TurnMessage* msg); |
| 115 | void HandleChannelBindRequest(const TurnMessage* msg); |
| 116 |
| 117 | void OnExternalPacket(rtc::AsyncPacketSocket* socket, |
| 118 | const rtc::ReceivedPacket& packet); |
| 119 |
| 120 | static webrtc::TimeDelta ComputeLifetime(const TurnMessage& msg); |
| 121 | bool HasPermission(const rtc::IPAddress& addr); |
| 122 | void AddPermission(const rtc::IPAddress& addr); |
| 123 | PermissionList::iterator FindPermission(const rtc::IPAddress& addr); |
| 124 | ChannelList::iterator FindChannel(int channel_id); |
| 125 | ChannelList::iterator FindChannel(const rtc::SocketAddress& addr); |
| 126 |
| 127 | void SendResponse(TurnMessage* msg); |
| 128 | void SendBadRequestResponse(const TurnMessage* req); |
| 129 | void SendErrorResponse(const TurnMessage* req, |
| 130 | int code, |
| 131 | absl::string_view reason); |
| 132 | void SendExternal(const void* data, |
| 133 | size_t size, |
| 134 | const rtc::SocketAddress& peer); |
| 135 |
| 136 | TurnServer* const server_; |
| 137 | webrtc::TaskQueueBase* const thread_; |
| 138 | TurnServerConnection conn_; |
| 139 | std::unique_ptr<rtc::AsyncPacketSocket> external_socket_; |
| 140 | std::string key_; |
| 141 | std::string transaction_id_; |
| 142 | std::string username_; |
| 143 | std::string last_nonce_; |
| 144 | PermissionList perms_; |
| 145 | ChannelList channels_; |
| 146 | webrtc::ScopedTaskSafety safety_; |
| 147 | }; |
| 148 |
| 149 | // An interface through which the MD5 credential hash can be retrieved. |
| 150 | class TurnAuthInterface { |
| 151 | public: |
| 152 | // Gets HA1 for the specified user and realm. |
| 153 | // HA1 = MD5(A1) = MD5(username:realm:password). |
| 154 | // Return true if the given username and realm are valid, or false if not. |
| 155 | virtual bool GetKey(absl::string_view username, |
| 156 | absl::string_view realm, |
| 157 | std::string* key) = 0; |
| 158 | virtual ~TurnAuthInterface() = default; |
| 159 | }; |
| 160 |
| 161 | // An interface enables Turn Server to control redirection behavior. |
| 162 | class TurnRedirectInterface { |
| 163 | public: |
| 164 | virtual bool ShouldRedirect(const rtc::SocketAddress& address, |
| 165 | rtc::SocketAddress* out) = 0; |
| 166 | virtual ~TurnRedirectInterface() {} |
| 167 | }; |
| 168 |
| 169 | class StunMessageObserver { |
| 170 | public: |
| 171 | virtual void ReceivedMessage(const TurnMessage* msg) = 0; |
| 172 | virtual void ReceivedChannelData(rtc::ArrayView<const uint8_t> payload) = 0; |
| 173 | virtual ~StunMessageObserver() {} |
| 174 | }; |
| 175 |
| 176 | // The core TURN server class. Give it a socket to listen on via |
| 177 | // AddInternalServerSocket, and a factory to create external sockets via |
| 178 | // SetExternalSocketFactory, and it's ready to go. |
| 179 | // Not yet wired up: TCP support. |
| 180 | class TurnServer : public sigslot::has_slots<> { |
| 181 | public: |
| 182 | typedef std::map<TurnServerConnection, std::unique_ptr<TurnServerAllocation>> |
| 183 | AllocationMap; |
| 184 |
| 185 | explicit TurnServer(webrtc::TaskQueueBase* thread); |
| 186 | ~TurnServer() override; |
| 187 |
| 188 | // Gets/sets the realm value to use for the server. |
| 189 | const std::string& realm() const { |
| 190 | RTC_DCHECK_RUN_ON(thread_); |
| 191 | return realm_; |
| 192 | } |
| 193 | void set_realm(absl::string_view realm) { |
| 194 | RTC_DCHECK_RUN_ON(thread_); |
| 195 | realm_ = std::string(realm); |
| 196 | } |
| 197 |
| 198 | // Gets/sets the value for the SOFTWARE attribute for TURN messages. |
| 199 | const std::string& software() const { |
| 200 | RTC_DCHECK_RUN_ON(thread_); |
| 201 | return software_; |
| 202 | } |
| 203 | void set_software(absl::string_view software) { |
| 204 | RTC_DCHECK_RUN_ON(thread_); |
| 205 | software_ = std::string(software); |
| 206 | } |
| 207 |
| 208 | const AllocationMap& allocations() const { |
| 209 | RTC_DCHECK_RUN_ON(thread_); |
| 210 | return allocations_; |
| 211 | } |
| 212 |
| 213 | // Sets the authentication callback; does not take ownership. |
| 214 | void set_auth_hook(TurnAuthInterface* auth_hook) { |
| 215 | RTC_DCHECK_RUN_ON(thread_); |
| 216 | auth_hook_ = auth_hook; |
| 217 | } |
| 218 |
| 219 | void set_redirect_hook(TurnRedirectInterface* redirect_hook) { |
| 220 | RTC_DCHECK_RUN_ON(thread_); |
| 221 | redirect_hook_ = redirect_hook; |
| 222 | } |
| 223 |
| 224 | void set_enable_otu_nonce(bool enable) { |
| 225 | RTC_DCHECK_RUN_ON(thread_); |
| 226 | enable_otu_nonce_ = enable; |
| 227 | } |
| 228 |
| 229 | // If set to true, reject CreatePermission requests to RFC1918 addresses. |
| 230 | void set_reject_private_addresses(bool filter) { |
| 231 | RTC_DCHECK_RUN_ON(thread_); |
| 232 | reject_private_addresses_ = filter; |
| 233 | } |
| 234 |
| 235 | void set_enable_permission_checks(bool enable) { |
| 236 | RTC_DCHECK_RUN_ON(thread_); |
| 237 | enable_permission_checks_ = enable; |
| 238 | } |
| 239 |
| 240 | // Starts listening for packets from internal clients. |
| 241 | void AddInternalSocket(rtc::AsyncPacketSocket* socket, ProtocolType proto); |
| 242 | // Starts listening for the connections on this socket. When someone tries |
| 243 | // to connect, the connection will be accepted and a new internal socket |
| 244 | // will be added. |
| 245 | void AddInternalServerSocket( |
| 246 | rtc::Socket* socket, |
| 247 | ProtocolType proto, |
| 248 | std::unique_ptr<rtc::SSLAdapterFactory> ssl_adapter_factory = nullptr); |
| 249 | // Specifies the factory to use for creating external sockets. |
| 250 | void SetExternalSocketFactory(rtc::PacketSocketFactory* factory, |
| 251 | const rtc::SocketAddress& address); |
| 252 | // For testing only. |
| 253 | std::string SetTimestampForNextNonce(int64_t timestamp) { |
| 254 | RTC_DCHECK_RUN_ON(thread_); |
| 255 | ts_for_next_nonce_ = timestamp; |
| 256 | return GenerateNonce(timestamp); |
| 257 | } |
| 258 |
| 259 | void SetStunMessageObserver(std::unique_ptr<StunMessageObserver> observer) { |
| 260 | RTC_DCHECK_RUN_ON(thread_); |
| 261 | stun_message_observer_ = std::move(observer); |
| 262 | } |
| 263 |
| 264 | private: |
| 265 | // All private member functions and variables should have access restricted to |
| 266 | // thread_. But compile-time annotations are missing for members access from |
| 267 | // TurnServerAllocation (via friend declaration). |
| 268 |
| 269 | std::string GenerateNonce(int64_t now) const RTC_RUN_ON(thread_); |
| 270 | void OnInternalPacket(rtc::AsyncPacketSocket* socket, |
| 271 | const rtc::ReceivedPacket& packet) RTC_RUN_ON(thread_); |
| 272 |
| 273 | void OnNewInternalConnection(rtc::Socket* socket); |
| 274 |
| 275 | // Accept connections on this server socket. |
| 276 | void AcceptConnection(rtc::Socket* server_socket) RTC_RUN_ON(thread_); |
| 277 | void OnInternalSocketClose(rtc::AsyncPacketSocket* socket, int err); |
| 278 |
| 279 | void HandleStunMessage(TurnServerConnection* conn, |
| 280 | rtc::ArrayView<const uint8_t> payload) |
| 281 | RTC_RUN_ON(thread_); |
| 282 | void HandleBindingRequest(TurnServerConnection* conn, const StunMessage* msg) |
| 283 | RTC_RUN_ON(thread_); |
| 284 | void HandleAllocateRequest(TurnServerConnection* conn, |
| 285 | const TurnMessage* msg, |
| 286 | absl::string_view key) RTC_RUN_ON(thread_); |
| 287 |
| 288 | bool GetKey(const StunMessage* msg, std::string* key) RTC_RUN_ON(thread_); |
| 289 | bool CheckAuthorization(TurnServerConnection* conn, |
| 290 | StunMessage* msg, |
| 291 | absl::string_view key) RTC_RUN_ON(thread_); |
| 292 | bool ValidateNonce(absl::string_view nonce) const RTC_RUN_ON(thread_); |
| 293 |
| 294 | TurnServerAllocation* FindAllocation(TurnServerConnection* conn) |
| 295 | RTC_RUN_ON(thread_); |
| 296 | TurnServerAllocation* CreateAllocation(TurnServerConnection* conn, |
| 297 | int proto, |
| 298 | absl::string_view key) |
| 299 | RTC_RUN_ON(thread_); |
| 300 |
| 301 | void SendErrorResponse(TurnServerConnection* conn, |
| 302 | const StunMessage* req, |
| 303 | int code, |
| 304 | absl::string_view reason); |
| 305 |
| 306 | void SendErrorResponseWithRealmAndNonce(TurnServerConnection* conn, |
| 307 | const StunMessage* req, |
| 308 | int code, |
| 309 | absl::string_view reason) |
| 310 | RTC_RUN_ON(thread_); |
| 311 |
| 312 | void SendErrorResponseWithAlternateServer(TurnServerConnection* conn, |
| 313 | const StunMessage* req, |
| 314 | const rtc::SocketAddress& addr) |
| 315 | RTC_RUN_ON(thread_); |
| 316 |
| 317 | void SendStun(TurnServerConnection* conn, StunMessage* msg); |
| 318 | void Send(TurnServerConnection* conn, const rtc::ByteBufferWriter& buf); |
| 319 |
| 320 | void DestroyAllocation(TurnServerAllocation* allocation) RTC_RUN_ON(thread_); |
| 321 | void DestroyInternalSocket(rtc::AsyncPacketSocket* socket) |
| 322 | RTC_RUN_ON(thread_); |
| 323 |
| 324 | typedef std::map<rtc::AsyncPacketSocket*, ProtocolType> InternalSocketMap; |
| 325 | struct ServerSocketInfo { |
| 326 | ProtocolType proto; |
| 327 | // If non-null, used to wrap accepted sockets. |
| 328 | std::unique_ptr<rtc::SSLAdapterFactory> ssl_adapter_factory; |
| 329 | }; |
| 330 | typedef std::map<rtc::Socket*, ServerSocketInfo> ServerSocketMap; |
| 331 |
| 332 | webrtc::TaskQueueBase* const thread_; |
| 333 | const std::string nonce_key_; |
| 334 | std::string realm_ RTC_GUARDED_BY(thread_); |
| 335 | std::string software_ RTC_GUARDED_BY(thread_); |
| 336 | TurnAuthInterface* auth_hook_ RTC_GUARDED_BY(thread_); |
| 337 | TurnRedirectInterface* redirect_hook_ RTC_GUARDED_BY(thread_); |
| 338 | // otu - one-time-use. Server will respond with 438 if it's |
| 339 | // sees the same nonce in next transaction. |
| 340 | bool enable_otu_nonce_ RTC_GUARDED_BY(thread_); |
| 341 | bool reject_private_addresses_ = false; |
| 342 | // Check for permission when receiving an external packet. |
| 343 | bool enable_permission_checks_ = true; |
| 344 |
| 345 | InternalSocketMap server_sockets_ RTC_GUARDED_BY(thread_); |
| 346 | ServerSocketMap server_listen_sockets_ RTC_GUARDED_BY(thread_); |
| 347 | std::unique_ptr<rtc::PacketSocketFactory> external_socket_factory_ |
| 348 | RTC_GUARDED_BY(thread_); |
| 349 | rtc::SocketAddress external_addr_ RTC_GUARDED_BY(thread_); |
| 350 |
| 351 | AllocationMap allocations_ RTC_GUARDED_BY(thread_); |
| 352 |
| 353 | // For testing only. If this is non-zero, the next NONCE will be generated |
| 354 | // from this value, and it will be reset to 0 after generating the NONCE. |
| 355 | int64_t ts_for_next_nonce_ RTC_GUARDED_BY(thread_) = 0; |
| 356 |
| 357 | // For testing only. Used to observe STUN messages received. |
| 358 | std::unique_ptr<StunMessageObserver> stun_message_observer_ |
| 359 | RTC_GUARDED_BY(thread_); |
| 360 |
| 361 | friend class TurnServerAllocation; |
| 362 | }; |
| 363 |
| 364 | } // namespace cricket |
| 365 |
| 366 | #endif // P2P_BASE_TURN_SERVER_H_ |
| 367 |