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