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" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 24 | #include "rtc_base/async_packet_socket.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 25 | #include "rtc_base/socket_address.h" |
Niels Möller | ac9a288 | 2021-10-20 15:25:09 +0200 | [diff] [blame] | 26 | #include "rtc_base/ssl_adapter.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_; } |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 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, |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 106 | const char* data, |
| 107 | size_t size, |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 108 | const rtc::SocketAddress& addr, |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 109 | const int64_t& packet_time_us); |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 110 | |
Tommi | 408143d | 2022-06-01 15:29:31 +0200 | [diff] [blame] | 111 | static int ComputeLifetime(const TurnMessage& msg); |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 112 | bool HasPermission(const rtc::IPAddress& addr); |
| 113 | void AddPermission(const rtc::IPAddress& addr); |
| 114 | Permission* FindPermission(const rtc::IPAddress& addr) const; |
| 115 | Channel* FindChannel(int channel_id) const; |
| 116 | Channel* FindChannel(const rtc::SocketAddress& addr) const; |
| 117 | |
| 118 | void SendResponse(TurnMessage* msg); |
| 119 | void SendBadRequestResponse(const TurnMessage* req); |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 120 | void SendErrorResponse(const TurnMessage* req, |
| 121 | int code, |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 122 | const std::string& reason); |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 123 | void SendExternal(const void* data, |
| 124 | size_t size, |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 125 | const rtc::SocketAddress& peer); |
| 126 | |
| 127 | void OnPermissionDestroyed(Permission* perm); |
| 128 | void OnChannelDestroyed(Channel* channel); |
Steve Anton | f2737d2 | 2017-10-31 16:27:34 -0700 | [diff] [blame] | 129 | void OnMessage(rtc::Message* msg) override; |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 130 | |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 131 | TurnServer* const server_; |
| 132 | rtc::Thread* const thread_; |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 133 | TurnServerConnection conn_; |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 134 | std::unique_ptr<rtc::AsyncPacketSocket> external_socket_; |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 135 | std::string key_; |
| 136 | std::string transaction_id_; |
| 137 | std::string username_; |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 138 | std::string last_nonce_; |
| 139 | PermissionList perms_; |
| 140 | ChannelList channels_; |
| 141 | }; |
| 142 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 143 | // An interface through which the MD5 credential hash can be retrieved. |
| 144 | class TurnAuthInterface { |
| 145 | public: |
| 146 | // Gets HA1 for the specified user and realm. |
| 147 | // HA1 = MD5(A1) = MD5(username:realm:password). |
| 148 | // 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] | 149 | virtual bool GetKey(const std::string& username, |
| 150 | const std::string& realm, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 151 | std::string* key) = 0; |
Henrik Kjellander | 3fe372d | 2016-05-12 08:10:52 +0200 | [diff] [blame] | 152 | virtual ~TurnAuthInterface() = default; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | // An interface enables Turn Server to control redirection behavior. |
| 156 | class TurnRedirectInterface { |
| 157 | public: |
| 158 | virtual bool ShouldRedirect(const rtc::SocketAddress& address, |
| 159 | rtc::SocketAddress* out) = 0; |
| 160 | virtual ~TurnRedirectInterface() {} |
| 161 | }; |
| 162 | |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 163 | class StunMessageObserver { |
| 164 | public: |
| 165 | virtual void ReceivedMessage(const TurnMessage* msg) = 0; |
| 166 | virtual void ReceivedChannelData(const char* data, size_t size) = 0; |
| 167 | virtual ~StunMessageObserver() {} |
| 168 | }; |
| 169 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 170 | // The core TURN server class. Give it a socket to listen on via |
| 171 | // AddInternalServerSocket, and a factory to create external sockets via |
| 172 | // SetExternalSocketFactory, and it's ready to go. |
| 173 | // Not yet wired up: TCP support. |
| 174 | class TurnServer : public sigslot::has_slots<> { |
| 175 | public: |
deadbeef | 9794366 | 2016-07-12 11:04:50 -0700 | [diff] [blame] | 176 | typedef std::map<TurnServerConnection, std::unique_ptr<TurnServerAllocation>> |
| 177 | AllocationMap; |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 178 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 179 | explicit TurnServer(rtc::Thread* thread); |
Steve Anton | f2737d2 | 2017-10-31 16:27:34 -0700 | [diff] [blame] | 180 | ~TurnServer() override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 181 | |
| 182 | // Gets/sets the realm value to use for the server. |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 183 | const std::string& realm() const { |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 184 | RTC_DCHECK_RUN_ON(thread_); |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 185 | return realm_; |
| 186 | } |
| 187 | void set_realm(const std::string& realm) { |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 188 | RTC_DCHECK_RUN_ON(thread_); |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 189 | realm_ = realm; |
| 190 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 191 | |
| 192 | // Gets/sets the value for the SOFTWARE attribute for TURN messages. |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 193 | const std::string& software() const { |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 194 | RTC_DCHECK_RUN_ON(thread_); |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 195 | return software_; |
| 196 | } |
| 197 | void set_software(const std::string& software) { |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 198 | RTC_DCHECK_RUN_ON(thread_); |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 199 | software_ = software; |
| 200 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 201 | |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 202 | const AllocationMap& allocations() const { |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 203 | RTC_DCHECK_RUN_ON(thread_); |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 204 | return allocations_; |
| 205 | } |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 206 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 207 | // Sets the authentication callback; does not take ownership. |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 208 | void set_auth_hook(TurnAuthInterface* auth_hook) { |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 209 | RTC_DCHECK_RUN_ON(thread_); |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 210 | auth_hook_ = auth_hook; |
| 211 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 212 | |
| 213 | void set_redirect_hook(TurnRedirectInterface* redirect_hook) { |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 214 | RTC_DCHECK_RUN_ON(thread_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 215 | redirect_hook_ = redirect_hook; |
| 216 | } |
| 217 | |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 218 | void set_enable_otu_nonce(bool enable) { |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 219 | RTC_DCHECK_RUN_ON(thread_); |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 220 | enable_otu_nonce_ = enable; |
| 221 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 222 | |
deadbeef | 376e123 | 2015-11-25 09:00:08 -0800 | [diff] [blame] | 223 | // If set to true, reject CreatePermission requests to RFC1918 addresses. |
| 224 | void set_reject_private_addresses(bool filter) { |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 225 | RTC_DCHECK_RUN_ON(thread_); |
deadbeef | 376e123 | 2015-11-25 09:00:08 -0800 | [diff] [blame] | 226 | reject_private_addresses_ = filter; |
| 227 | } |
| 228 | |
Taylor Brandstetter | ef18470 | 2016-06-23 17:35:47 -0700 | [diff] [blame] | 229 | void set_enable_permission_checks(bool enable) { |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 230 | RTC_DCHECK_RUN_ON(thread_); |
Taylor Brandstetter | ef18470 | 2016-06-23 17:35:47 -0700 | [diff] [blame] | 231 | enable_permission_checks_ = enable; |
| 232 | } |
| 233 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 234 | // Starts listening for packets from internal clients. |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 235 | void AddInternalSocket(rtc::AsyncPacketSocket* socket, ProtocolType proto); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 236 | // Starts listening for the connections on this socket. When someone tries |
| 237 | // to connect, the connection will be accepted and a new internal socket |
| 238 | // will be added. |
Niels Möller | ac9a288 | 2021-10-20 15:25:09 +0200 | [diff] [blame] | 239 | void AddInternalServerSocket( |
| 240 | rtc::Socket* socket, |
| 241 | ProtocolType proto, |
| 242 | std::unique_ptr<rtc::SSLAdapterFactory> ssl_adapter_factory = nullptr); |
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) { |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 248 | RTC_DCHECK_RUN_ON(thread_); |
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) { |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 254 | RTC_DCHECK_RUN_ON(thread_); |
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: |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 259 | // All private member functions and variables should have access restricted to |
| 260 | // thread_. But compile-time annotations are missing for members access from |
| 261 | // TurnServerAllocation (via friend declaration), and the On* methods, which |
| 262 | // are called via sigslot. |
| 263 | std::string GenerateNonce(int64_t now) const RTC_RUN_ON(thread_); |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 264 | void OnInternalPacket(rtc::AsyncPacketSocket* socket, |
| 265 | const char* data, |
| 266 | size_t size, |
| 267 | const rtc::SocketAddress& address, |
| 268 | const int64_t& packet_time_us); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 269 | |
Niels Möller | d0b8879 | 2021-08-12 10:32:30 +0200 | [diff] [blame] | 270 | void OnNewInternalConnection(rtc::Socket* socket); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 271 | |
| 272 | // Accept connections on this server socket. |
Niels Möller | d0b8879 | 2021-08-12 10:32:30 +0200 | [diff] [blame] | 273 | void AcceptConnection(rtc::Socket* server_socket) RTC_RUN_ON(thread_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 274 | void OnInternalSocketClose(rtc::AsyncPacketSocket* socket, int err); |
| 275 | |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 276 | void HandleStunMessage(TurnServerConnection* conn, |
| 277 | const char* data, |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 278 | size_t size) RTC_RUN_ON(thread_); |
| 279 | void HandleBindingRequest(TurnServerConnection* conn, const StunMessage* msg) |
| 280 | RTC_RUN_ON(thread_); |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 281 | void HandleAllocateRequest(TurnServerConnection* conn, |
| 282 | const TurnMessage* msg, |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 283 | const std::string& key) RTC_RUN_ON(thread_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 284 | |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 285 | bool GetKey(const StunMessage* msg, std::string* key) RTC_RUN_ON(thread_); |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 286 | bool CheckAuthorization(TurnServerConnection* conn, |
Harald Alvestrand | 07d83c8 | 2021-03-02 08:09:53 +0000 | [diff] [blame] | 287 | StunMessage* msg, |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 288 | const char* data, |
| 289 | size_t size, |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 290 | const std::string& key) RTC_RUN_ON(thread_); |
| 291 | bool ValidateNonce(const std::string& nonce) const RTC_RUN_ON(thread_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 292 | |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 293 | TurnServerAllocation* FindAllocation(TurnServerConnection* conn) |
| 294 | RTC_RUN_ON(thread_); |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 295 | TurnServerAllocation* CreateAllocation(TurnServerConnection* conn, |
| 296 | int proto, |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 297 | const std::string& key) |
| 298 | RTC_RUN_ON(thread_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 299 | |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 300 | void SendErrorResponse(TurnServerConnection* conn, |
| 301 | const StunMessage* req, |
| 302 | int code, |
| 303 | const std::string& reason); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 304 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 305 | void SendErrorResponseWithRealmAndNonce(TurnServerConnection* conn, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 306 | const StunMessage* req, |
| 307 | int code, |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 308 | const std::string& reason) |
| 309 | RTC_RUN_ON(thread_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 310 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 311 | void SendErrorResponseWithAlternateServer(TurnServerConnection* conn, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 312 | const StunMessage* req, |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 313 | const rtc::SocketAddress& addr) |
| 314 | RTC_RUN_ON(thread_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 315 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 316 | void SendStun(TurnServerConnection* conn, StunMessage* msg); |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 317 | void Send(TurnServerConnection* conn, const rtc::ByteBufferWriter& buf); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 318 | |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 319 | void OnAllocationDestroyed(TurnServerAllocation* allocation) |
| 320 | RTC_RUN_ON(thread_); |
| 321 | void DestroyInternalSocket(rtc::AsyncPacketSocket* socket) |
| 322 | RTC_RUN_ON(thread_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 323 | |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 324 | typedef std::map<rtc::AsyncPacketSocket*, ProtocolType> InternalSocketMap; |
Niels Möller | ac9a288 | 2021-10-20 15:25:09 +0200 | [diff] [blame] | 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; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 331 | |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 332 | rtc::Thread* 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_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 338 | // otu - one-time-use. Server will respond with 438 if it's |
| 339 | // sees the same nonce in next transaction. |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 340 | bool enable_otu_nonce_ RTC_GUARDED_BY(thread_); |
deadbeef | 376e123 | 2015-11-25 09:00:08 -0800 | [diff] [blame] | 341 | bool reject_private_addresses_ = false; |
Taylor Brandstetter | ef18470 | 2016-06-23 17:35:47 -0700 | [diff] [blame] | 342 | // Check for permission when receiving an external packet. |
| 343 | bool enable_permission_checks_ = true; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 344 | |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 345 | InternalSocketMap server_sockets_ RTC_GUARDED_BY(thread_); |
| 346 | ServerSocketMap server_listen_sockets_ RTC_GUARDED_BY(thread_); |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 347 | std::unique_ptr<rtc::PacketSocketFactory> external_socket_factory_ |
| 348 | RTC_GUARDED_BY(thread_); |
| 349 | rtc::SocketAddress external_addr_ RTC_GUARDED_BY(thread_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 350 | |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 351 | AllocationMap allocations_ RTC_GUARDED_BY(thread_); |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 352 | |
honghaiz | c463e20 | 2016-02-01 15:19:08 -0800 | [diff] [blame] | 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. |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 355 | int64_t ts_for_next_nonce_ RTC_GUARDED_BY(thread_) = 0; |
honghaiz | c463e20 | 2016-02-01 15:19:08 -0800 | [diff] [blame] | 356 | |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 357 | // For testing only. Used to observe STUN messages received. |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 358 | std::unique_ptr<StunMessageObserver> stun_message_observer_ |
| 359 | RTC_GUARDED_BY(thread_); |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 360 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 361 | friend class TurnServerAllocation; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 362 | }; |
| 363 | |
| 364 | } // namespace cricket |
| 365 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 366 | #endif // P2P_BASE_TURN_SERVER_H_ |