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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef P2P_BASE_TURNSERVER_H_ |
| 12 | #define P2P_BASE_TURNSERVER_H_ |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 13 | |
| 14 | #include <list> |
| 15 | #include <map> |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 16 | #include <memory> |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 17 | #include <set> |
| 18 | #include <string> |
deadbeef | 824f586 | 2016-08-24 15:06:53 -0700 | [diff] [blame] | 19 | #include <vector> |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 20 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "p2p/base/portinterface.h" |
| 22 | #include "rtc_base/asyncinvoker.h" |
| 23 | #include "rtc_base/asyncpacketsocket.h" |
| 24 | #include "rtc_base/messagequeue.h" |
| 25 | #include "rtc_base/sigslot.h" |
| 26 | #include "rtc_base/socketaddress.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 27 | |
| 28 | namespace rtc { |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 29 | class ByteBufferWriter; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 30 | class PacketSocketFactory; |
| 31 | class Thread; |
| 32 | } |
| 33 | |
| 34 | namespace cricket { |
| 35 | |
| 36 | class StunMessage; |
| 37 | class TurnMessage; |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 38 | class TurnServer; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 39 | |
| 40 | // The default server port for TURN, as specified in RFC5766. |
| 41 | const int TURN_SERVER_PORT = 3478; |
| 42 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 43 | // Encapsulates the client's connection to the server. |
| 44 | class TurnServerConnection { |
| 45 | public: |
| 46 | TurnServerConnection() : proto_(PROTO_UDP), socket_(NULL) {} |
| 47 | TurnServerConnection(const rtc::SocketAddress& src, |
| 48 | ProtocolType proto, |
| 49 | rtc::AsyncPacketSocket* socket); |
| 50 | const rtc::SocketAddress& src() const { return src_; } |
| 51 | rtc::AsyncPacketSocket* socket() { return socket_; } |
| 52 | bool operator==(const TurnServerConnection& t) const; |
| 53 | bool operator<(const TurnServerConnection& t) const; |
| 54 | std::string ToString() const; |
| 55 | |
| 56 | private: |
| 57 | rtc::SocketAddress src_; |
| 58 | rtc::SocketAddress dst_; |
| 59 | cricket::ProtocolType proto_; |
| 60 | rtc::AsyncPacketSocket* socket_; |
| 61 | }; |
| 62 | |
| 63 | // Encapsulates a TURN allocation. |
| 64 | // The object is created when an allocation request is received, and then |
| 65 | // handles TURN messages (via HandleTurnMessage) and channel data messages |
| 66 | // (via HandleChannelData) for this allocation when received by the server. |
| 67 | // The object self-deletes and informs the server if its lifetime timer expires. |
| 68 | class TurnServerAllocation : public rtc::MessageHandler, |
| 69 | public sigslot::has_slots<> { |
| 70 | public: |
| 71 | TurnServerAllocation(TurnServer* server_, |
| 72 | rtc::Thread* thread, |
| 73 | const TurnServerConnection& conn, |
| 74 | rtc::AsyncPacketSocket* server_socket, |
| 75 | const std::string& key); |
Steve Anton | f2737d2 | 2017-10-31 16:27:34 -0700 | [diff] [blame] | 76 | ~TurnServerAllocation() override; |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 77 | |
| 78 | TurnServerConnection* conn() { return &conn_; } |
| 79 | const std::string& key() const { return key_; } |
| 80 | const std::string& transaction_id() const { return transaction_id_; } |
| 81 | const std::string& username() const { return username_; } |
| 82 | const std::string& origin() const { return origin_; } |
| 83 | const std::string& last_nonce() const { return last_nonce_; } |
| 84 | void set_last_nonce(const std::string& nonce) { last_nonce_ = nonce; } |
| 85 | |
| 86 | std::string ToString() const; |
| 87 | |
| 88 | void HandleTurnMessage(const TurnMessage* msg); |
| 89 | void HandleChannelData(const char* data, size_t size); |
| 90 | |
| 91 | sigslot::signal1<TurnServerAllocation*> SignalDestroyed; |
| 92 | |
| 93 | private: |
| 94 | class Channel; |
| 95 | class Permission; |
| 96 | typedef std::list<Permission*> PermissionList; |
| 97 | typedef std::list<Channel*> ChannelList; |
| 98 | |
| 99 | void HandleAllocateRequest(const TurnMessage* msg); |
| 100 | void HandleRefreshRequest(const TurnMessage* msg); |
| 101 | void HandleSendIndication(const TurnMessage* msg); |
| 102 | void HandleCreatePermissionRequest(const TurnMessage* msg); |
| 103 | void HandleChannelBindRequest(const TurnMessage* msg); |
| 104 | |
| 105 | void OnExternalPacket(rtc::AsyncPacketSocket* socket, |
| 106 | const char* data, size_t size, |
| 107 | const rtc::SocketAddress& addr, |
| 108 | const rtc::PacketTime& packet_time); |
| 109 | |
| 110 | static int ComputeLifetime(const TurnMessage* msg); |
| 111 | bool HasPermission(const rtc::IPAddress& addr); |
| 112 | void AddPermission(const rtc::IPAddress& addr); |
| 113 | Permission* FindPermission(const rtc::IPAddress& addr) const; |
| 114 | Channel* FindChannel(int channel_id) const; |
| 115 | Channel* FindChannel(const rtc::SocketAddress& addr) const; |
| 116 | |
| 117 | void SendResponse(TurnMessage* msg); |
| 118 | void SendBadRequestResponse(const TurnMessage* req); |
| 119 | void SendErrorResponse(const TurnMessage* req, int code, |
| 120 | const std::string& reason); |
| 121 | void SendExternal(const void* data, size_t size, |
| 122 | const rtc::SocketAddress& peer); |
| 123 | |
| 124 | void OnPermissionDestroyed(Permission* perm); |
| 125 | void OnChannelDestroyed(Channel* channel); |
Steve Anton | f2737d2 | 2017-10-31 16:27:34 -0700 | [diff] [blame] | 126 | void OnMessage(rtc::Message* msg) override; |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 127 | |
| 128 | TurnServer* server_; |
| 129 | rtc::Thread* thread_; |
| 130 | TurnServerConnection conn_; |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 131 | std::unique_ptr<rtc::AsyncPacketSocket> external_socket_; |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 132 | std::string key_; |
| 133 | std::string transaction_id_; |
| 134 | std::string username_; |
| 135 | std::string origin_; |
| 136 | std::string last_nonce_; |
| 137 | PermissionList perms_; |
| 138 | ChannelList channels_; |
| 139 | }; |
| 140 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 141 | // An interface through which the MD5 credential hash can be retrieved. |
| 142 | class TurnAuthInterface { |
| 143 | public: |
| 144 | // Gets HA1 for the specified user and realm. |
| 145 | // HA1 = MD5(A1) = MD5(username:realm:password). |
| 146 | // Return true if the given username and realm are valid, or false if not. |
| 147 | virtual bool GetKey(const std::string& username, const std::string& realm, |
| 148 | std::string* key) = 0; |
Henrik Kjellander | 3fe372d | 2016-05-12 08:10:52 +0200 | [diff] [blame] | 149 | virtual ~TurnAuthInterface() = default; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | // An interface enables Turn Server to control redirection behavior. |
| 153 | class TurnRedirectInterface { |
| 154 | public: |
| 155 | virtual bool ShouldRedirect(const rtc::SocketAddress& address, |
| 156 | rtc::SocketAddress* out) = 0; |
| 157 | virtual ~TurnRedirectInterface() {} |
| 158 | }; |
| 159 | |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 160 | class StunMessageObserver { |
| 161 | public: |
| 162 | virtual void ReceivedMessage(const TurnMessage* msg) = 0; |
| 163 | virtual void ReceivedChannelData(const char* data, size_t size) = 0; |
| 164 | virtual ~StunMessageObserver() {} |
| 165 | }; |
| 166 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 167 | // The core TURN server class. Give it a socket to listen on via |
| 168 | // AddInternalServerSocket, and a factory to create external sockets via |
| 169 | // SetExternalSocketFactory, and it's ready to go. |
| 170 | // Not yet wired up: TCP support. |
| 171 | class TurnServer : public sigslot::has_slots<> { |
| 172 | public: |
deadbeef | 9794366 | 2016-07-12 11:04:50 -0700 | [diff] [blame] | 173 | typedef std::map<TurnServerConnection, std::unique_ptr<TurnServerAllocation>> |
| 174 | AllocationMap; |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 175 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 176 | explicit TurnServer(rtc::Thread* thread); |
Steve Anton | f2737d2 | 2017-10-31 16:27:34 -0700 | [diff] [blame] | 177 | ~TurnServer() override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 178 | |
| 179 | // Gets/sets the realm value to use for the server. |
| 180 | const std::string& realm() const { return realm_; } |
| 181 | void set_realm(const std::string& realm) { realm_ = realm; } |
| 182 | |
| 183 | // Gets/sets the value for the SOFTWARE attribute for TURN messages. |
| 184 | const std::string& software() const { return software_; } |
| 185 | void set_software(const std::string& software) { software_ = software; } |
| 186 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 187 | const AllocationMap& allocations() const { return allocations_; } |
| 188 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 189 | // Sets the authentication callback; does not take ownership. |
| 190 | void set_auth_hook(TurnAuthInterface* auth_hook) { auth_hook_ = auth_hook; } |
| 191 | |
| 192 | void set_redirect_hook(TurnRedirectInterface* redirect_hook) { |
| 193 | redirect_hook_ = redirect_hook; |
| 194 | } |
| 195 | |
| 196 | void set_enable_otu_nonce(bool enable) { enable_otu_nonce_ = enable; } |
| 197 | |
deadbeef | 376e123 | 2015-11-25 09:00:08 -0800 | [diff] [blame] | 198 | // If set to true, reject CreatePermission requests to RFC1918 addresses. |
| 199 | void set_reject_private_addresses(bool filter) { |
| 200 | reject_private_addresses_ = filter; |
| 201 | } |
| 202 | |
Taylor Brandstetter | ef18470 | 2016-06-23 17:35:47 -0700 | [diff] [blame] | 203 | void set_enable_permission_checks(bool enable) { |
| 204 | enable_permission_checks_ = enable; |
| 205 | } |
| 206 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 207 | // Starts listening for packets from internal clients. |
| 208 | void AddInternalSocket(rtc::AsyncPacketSocket* socket, |
| 209 | ProtocolType proto); |
| 210 | // Starts listening for the connections on this socket. When someone tries |
| 211 | // to connect, the connection will be accepted and a new internal socket |
| 212 | // will be added. |
| 213 | void AddInternalServerSocket(rtc::AsyncSocket* socket, |
| 214 | ProtocolType proto); |
| 215 | // Specifies the factory to use for creating external sockets. |
| 216 | void SetExternalSocketFactory(rtc::PacketSocketFactory* factory, |
| 217 | const rtc::SocketAddress& address); |
honghaiz | c463e20 | 2016-02-01 15:19:08 -0800 | [diff] [blame] | 218 | // For testing only. |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 219 | std::string SetTimestampForNextNonce(int64_t timestamp) { |
honghaiz | c463e20 | 2016-02-01 15:19:08 -0800 | [diff] [blame] | 220 | ts_for_next_nonce_ = timestamp; |
| 221 | return GenerateNonce(timestamp); |
| 222 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 223 | |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 224 | void SetStunMessageObserver( |
| 225 | std::unique_ptr<StunMessageObserver> observer) { |
| 226 | stun_message_observer_ = std::move(observer); |
| 227 | } |
| 228 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 229 | private: |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 230 | std::string GenerateNonce(int64_t now) const; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 231 | void OnInternalPacket(rtc::AsyncPacketSocket* socket, const char* data, |
| 232 | size_t size, const rtc::SocketAddress& address, |
| 233 | const rtc::PacketTime& packet_time); |
| 234 | |
| 235 | void OnNewInternalConnection(rtc::AsyncSocket* socket); |
| 236 | |
| 237 | // Accept connections on this server socket. |
| 238 | void AcceptConnection(rtc::AsyncSocket* server_socket); |
| 239 | void OnInternalSocketClose(rtc::AsyncPacketSocket* socket, int err); |
| 240 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 241 | void HandleStunMessage( |
| 242 | TurnServerConnection* conn, const char* data, size_t size); |
| 243 | void HandleBindingRequest(TurnServerConnection* conn, const StunMessage* msg); |
| 244 | void HandleAllocateRequest(TurnServerConnection* conn, const TurnMessage* msg, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 245 | const std::string& key); |
| 246 | |
| 247 | bool GetKey(const StunMessage* msg, std::string* key); |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 248 | bool CheckAuthorization(TurnServerConnection* conn, const StunMessage* msg, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 249 | const char* data, size_t size, |
| 250 | const std::string& key); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 251 | bool ValidateNonce(const std::string& nonce) const; |
| 252 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 253 | TurnServerAllocation* FindAllocation(TurnServerConnection* conn); |
| 254 | TurnServerAllocation* CreateAllocation( |
| 255 | TurnServerConnection* conn, int proto, const std::string& key); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 256 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 257 | void SendErrorResponse(TurnServerConnection* conn, const StunMessage* req, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 258 | int code, const std::string& reason); |
| 259 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 260 | void SendErrorResponseWithRealmAndNonce(TurnServerConnection* conn, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 261 | const StunMessage* req, |
| 262 | int code, |
| 263 | const std::string& reason); |
| 264 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 265 | void SendErrorResponseWithAlternateServer(TurnServerConnection* conn, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 266 | const StunMessage* req, |
| 267 | const rtc::SocketAddress& addr); |
| 268 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 269 | void SendStun(TurnServerConnection* conn, StunMessage* msg); |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 270 | void Send(TurnServerConnection* conn, const rtc::ByteBufferWriter& buf); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 271 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 272 | void OnAllocationDestroyed(TurnServerAllocation* allocation); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 273 | void DestroyInternalSocket(rtc::AsyncPacketSocket* socket); |
| 274 | |
deadbeef | 824f586 | 2016-08-24 15:06:53 -0700 | [diff] [blame] | 275 | // Just clears |sockets_to_delete_|; called asynchronously. |
| 276 | void FreeSockets(); |
| 277 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 278 | typedef std::map<rtc::AsyncPacketSocket*, |
| 279 | ProtocolType> InternalSocketMap; |
| 280 | typedef std::map<rtc::AsyncSocket*, |
| 281 | ProtocolType> ServerSocketMap; |
| 282 | |
| 283 | rtc::Thread* thread_; |
| 284 | std::string nonce_key_; |
| 285 | std::string realm_; |
| 286 | std::string software_; |
| 287 | TurnAuthInterface* auth_hook_; |
| 288 | TurnRedirectInterface* redirect_hook_; |
| 289 | // otu - one-time-use. Server will respond with 438 if it's |
| 290 | // sees the same nonce in next transaction. |
| 291 | bool enable_otu_nonce_; |
deadbeef | 376e123 | 2015-11-25 09:00:08 -0800 | [diff] [blame] | 292 | bool reject_private_addresses_ = false; |
Taylor Brandstetter | ef18470 | 2016-06-23 17:35:47 -0700 | [diff] [blame] | 293 | // Check for permission when receiving an external packet. |
| 294 | bool enable_permission_checks_ = true; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 295 | |
| 296 | InternalSocketMap server_sockets_; |
| 297 | ServerSocketMap server_listen_sockets_; |
deadbeef | 824f586 | 2016-08-24 15:06:53 -0700 | [diff] [blame] | 298 | // Used when we need to delete a socket asynchronously. |
| 299 | std::vector<std::unique_ptr<rtc::AsyncPacketSocket>> sockets_to_delete_; |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 300 | std::unique_ptr<rtc::PacketSocketFactory> external_socket_factory_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 301 | rtc::SocketAddress external_addr_; |
| 302 | |
| 303 | AllocationMap allocations_; |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 304 | |
deadbeef | 824f586 | 2016-08-24 15:06:53 -0700 | [diff] [blame] | 305 | rtc::AsyncInvoker invoker_; |
| 306 | |
honghaiz | c463e20 | 2016-02-01 15:19:08 -0800 | [diff] [blame] | 307 | // For testing only. If this is non-zero, the next NONCE will be generated |
| 308 | // from this value, and it will be reset to 0 after generating the NONCE. |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 309 | int64_t ts_for_next_nonce_ = 0; |
honghaiz | c463e20 | 2016-02-01 15:19:08 -0800 | [diff] [blame] | 310 | |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 311 | // For testing only. Used to observe STUN messages received. |
| 312 | std::unique_ptr<StunMessageObserver> stun_message_observer_; |
| 313 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 314 | friend class TurnServerAllocation; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 315 | }; |
| 316 | |
| 317 | } // namespace cricket |
| 318 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 319 | #endif // P2P_BASE_TURNSERVER_H_ |