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