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 | |
Ali Tofigh | de2ac5a | 2022-06-30 11:58:26 +0200 | [diff] [blame^] | 22 | #include "absl/strings/string_view.h" |
Artem Titov | d15a575 | 2021-02-10 14:31:24 +0100 | [diff] [blame] | 23 | #include "api/sequence_checker.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 24 | #include "p2p/base/port_interface.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 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" |
Niels Möller | ac9a288 | 2021-10-20 15:25:09 +0200 | [diff] [blame] | 27 | #include "rtc_base/ssl_adapter.h" |
Artem Titov | e41c433 | 2018-07-25 15:04:28 +0200 | [diff] [blame] | 28 | #include "rtc_base/third_party/sigslot/sigslot.h" |
Sebastian Jansson | 4db28b5 | 2020-01-08 14:07:15 +0100 | [diff] [blame] | 29 | #include "rtc_base/thread.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 30 | |
| 31 | namespace rtc { |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 32 | class ByteBufferWriter; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 33 | class PacketSocketFactory; |
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. |
Tomas Gunnarsson | abdb470 | 2020-09-05 18:43:36 +0200 | [diff] [blame] | 70 | class TurnServerAllocation : public rtc::MessageHandlerAutoCleanup, |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 71 | public sigslot::has_slots<> { |
| 72 | public: |
| 73 | TurnServerAllocation(TurnServer* server_, |
| 74 | rtc::Thread* thread, |
| 75 | const TurnServerConnection& conn, |
| 76 | rtc::AsyncPacketSocket* server_socket, |
Ali Tofigh | de2ac5a | 2022-06-30 11:58:26 +0200 | [diff] [blame^] | 77 | absl::string_view 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_; } |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 84 | const std::string& last_nonce() const { return last_nonce_; } |
Tommi | e83500e | 2022-06-03 14:28:59 +0200 | [diff] [blame] | 85 | void set_last_nonce(absl::string_view nonce) { |
| 86 | last_nonce_ = std::string(nonce); |
| 87 | } |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 88 | |
| 89 | std::string ToString() const; |
| 90 | |
| 91 | void HandleTurnMessage(const TurnMessage* msg); |
| 92 | void HandleChannelData(const char* data, size_t size); |
| 93 | |
| 94 | sigslot::signal1<TurnServerAllocation*> SignalDestroyed; |
| 95 | |
| 96 | private: |
| 97 | class Channel; |
| 98 | class Permission; |
| 99 | typedef std::list<Permission*> PermissionList; |
| 100 | typedef std::list<Channel*> ChannelList; |
| 101 | |
| 102 | void HandleAllocateRequest(const TurnMessage* msg); |
| 103 | void HandleRefreshRequest(const TurnMessage* msg); |
| 104 | void HandleSendIndication(const TurnMessage* msg); |
| 105 | void HandleCreatePermissionRequest(const TurnMessage* msg); |
| 106 | void HandleChannelBindRequest(const TurnMessage* msg); |
| 107 | |
| 108 | void OnExternalPacket(rtc::AsyncPacketSocket* socket, |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 109 | const char* data, |
| 110 | size_t size, |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 111 | const rtc::SocketAddress& addr, |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 112 | const int64_t& packet_time_us); |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 113 | |
Tommi | 408143d | 2022-06-01 15:29:31 +0200 | [diff] [blame] | 114 | static int ComputeLifetime(const TurnMessage& msg); |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 115 | bool HasPermission(const rtc::IPAddress& addr); |
| 116 | void AddPermission(const rtc::IPAddress& addr); |
| 117 | Permission* FindPermission(const rtc::IPAddress& addr) const; |
| 118 | Channel* FindChannel(int channel_id) const; |
| 119 | Channel* FindChannel(const rtc::SocketAddress& addr) const; |
| 120 | |
| 121 | void SendResponse(TurnMessage* msg); |
| 122 | void SendBadRequestResponse(const TurnMessage* req); |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 123 | void SendErrorResponse(const TurnMessage* req, |
| 124 | int code, |
Ali Tofigh | de2ac5a | 2022-06-30 11:58:26 +0200 | [diff] [blame^] | 125 | absl::string_view reason); |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 126 | void SendExternal(const void* data, |
| 127 | size_t size, |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 128 | const rtc::SocketAddress& peer); |
| 129 | |
| 130 | void OnPermissionDestroyed(Permission* perm); |
| 131 | void OnChannelDestroyed(Channel* channel); |
Steve Anton | f2737d2 | 2017-10-31 16:27:34 -0700 | [diff] [blame] | 132 | void OnMessage(rtc::Message* msg) override; |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 133 | |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 134 | TurnServer* const server_; |
| 135 | rtc::Thread* const thread_; |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 136 | TurnServerConnection conn_; |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 137 | std::unique_ptr<rtc::AsyncPacketSocket> external_socket_; |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 138 | std::string key_; |
| 139 | std::string transaction_id_; |
| 140 | std::string username_; |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 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. |
Ali Tofigh | de2ac5a | 2022-06-30 11:58:26 +0200 | [diff] [blame^] | 152 | virtual bool GetKey(absl::string_view username, |
| 153 | absl::string_view 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 { |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 187 | RTC_DCHECK_RUN_ON(thread_); |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 188 | return realm_; |
| 189 | } |
Ali Tofigh | de2ac5a | 2022-06-30 11:58:26 +0200 | [diff] [blame^] | 190 | void set_realm(absl::string_view realm) { |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 191 | RTC_DCHECK_RUN_ON(thread_); |
Ali Tofigh | de2ac5a | 2022-06-30 11:58:26 +0200 | [diff] [blame^] | 192 | realm_ = std::string(realm); |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 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 { |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 197 | RTC_DCHECK_RUN_ON(thread_); |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 198 | return software_; |
| 199 | } |
Ali Tofigh | de2ac5a | 2022-06-30 11:58:26 +0200 | [diff] [blame^] | 200 | void set_software(absl::string_view software) { |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 201 | RTC_DCHECK_RUN_ON(thread_); |
Ali Tofigh | de2ac5a | 2022-06-30 11:58:26 +0200 | [diff] [blame^] | 202 | software_ = std::string(software); |
Seth Hampson | aed7164 | 2018-06-11 07:41:32 -0700 | [diff] [blame] | 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 { |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 206 | RTC_DCHECK_RUN_ON(thread_); |
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) { |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 212 | RTC_DCHECK_RUN_ON(thread_); |
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) { |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 217 | RTC_DCHECK_RUN_ON(thread_); |
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) { |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 222 | RTC_DCHECK_RUN_ON(thread_); |
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) { |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 228 | RTC_DCHECK_RUN_ON(thread_); |
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) { |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 233 | RTC_DCHECK_RUN_ON(thread_); |
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. |
Niels Möller | ac9a288 | 2021-10-20 15:25:09 +0200 | [diff] [blame] | 242 | void AddInternalServerSocket( |
| 243 | rtc::Socket* socket, |
| 244 | ProtocolType proto, |
| 245 | std::unique_ptr<rtc::SSLAdapterFactory> ssl_adapter_factory = nullptr); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 246 | // Specifies the factory to use for creating external sockets. |
| 247 | void SetExternalSocketFactory(rtc::PacketSocketFactory* factory, |
| 248 | const rtc::SocketAddress& address); |
honghaiz | c463e20 | 2016-02-01 15:19:08 -0800 | [diff] [blame] | 249 | // For testing only. |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 250 | std::string SetTimestampForNextNonce(int64_t timestamp) { |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 251 | RTC_DCHECK_RUN_ON(thread_); |
honghaiz | c463e20 | 2016-02-01 15:19:08 -0800 | [diff] [blame] | 252 | ts_for_next_nonce_ = timestamp; |
| 253 | return GenerateNonce(timestamp); |
| 254 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 255 | |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 256 | void SetStunMessageObserver(std::unique_ptr<StunMessageObserver> observer) { |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 257 | RTC_DCHECK_RUN_ON(thread_); |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 258 | stun_message_observer_ = std::move(observer); |
| 259 | } |
| 260 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 261 | private: |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 262 | // All private member functions and variables should have access restricted to |
| 263 | // thread_. But compile-time annotations are missing for members access from |
| 264 | // TurnServerAllocation (via friend declaration), and the On* methods, which |
| 265 | // are called via sigslot. |
| 266 | std::string GenerateNonce(int64_t now) const RTC_RUN_ON(thread_); |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 267 | void OnInternalPacket(rtc::AsyncPacketSocket* socket, |
| 268 | const char* data, |
| 269 | size_t size, |
| 270 | const rtc::SocketAddress& address, |
| 271 | const int64_t& packet_time_us); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 272 | |
Niels Möller | d0b8879 | 2021-08-12 10:32:30 +0200 | [diff] [blame] | 273 | void OnNewInternalConnection(rtc::Socket* socket); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 274 | |
| 275 | // Accept connections on this server socket. |
Niels Möller | d0b8879 | 2021-08-12 10:32:30 +0200 | [diff] [blame] | 276 | void AcceptConnection(rtc::Socket* server_socket) RTC_RUN_ON(thread_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 277 | void OnInternalSocketClose(rtc::AsyncPacketSocket* socket, int err); |
| 278 | |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 279 | void HandleStunMessage(TurnServerConnection* conn, |
| 280 | const char* data, |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 281 | size_t size) RTC_RUN_ON(thread_); |
| 282 | void HandleBindingRequest(TurnServerConnection* conn, const StunMessage* msg) |
| 283 | RTC_RUN_ON(thread_); |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 284 | void HandleAllocateRequest(TurnServerConnection* conn, |
| 285 | const TurnMessage* msg, |
Ali Tofigh | de2ac5a | 2022-06-30 11:58:26 +0200 | [diff] [blame^] | 286 | absl::string_view key) RTC_RUN_ON(thread_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 287 | |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 288 | bool GetKey(const StunMessage* msg, std::string* key) RTC_RUN_ON(thread_); |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 289 | bool CheckAuthorization(TurnServerConnection* conn, |
Harald Alvestrand | 07d83c8 | 2021-03-02 08:09:53 +0000 | [diff] [blame] | 290 | StunMessage* msg, |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 291 | const char* data, |
| 292 | size_t size, |
Ali Tofigh | de2ac5a | 2022-06-30 11:58:26 +0200 | [diff] [blame^] | 293 | absl::string_view key) RTC_RUN_ON(thread_); |
Tommi | e83500e | 2022-06-03 14:28:59 +0200 | [diff] [blame] | 294 | bool ValidateNonce(absl::string_view nonce) const RTC_RUN_ON(thread_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 295 | |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 296 | TurnServerAllocation* FindAllocation(TurnServerConnection* conn) |
| 297 | RTC_RUN_ON(thread_); |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 298 | TurnServerAllocation* CreateAllocation(TurnServerConnection* conn, |
| 299 | int proto, |
Ali Tofigh | de2ac5a | 2022-06-30 11:58:26 +0200 | [diff] [blame^] | 300 | absl::string_view key) |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 301 | RTC_RUN_ON(thread_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 302 | |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 303 | void SendErrorResponse(TurnServerConnection* conn, |
| 304 | const StunMessage* req, |
| 305 | int code, |
Ali Tofigh | de2ac5a | 2022-06-30 11:58:26 +0200 | [diff] [blame^] | 306 | absl::string_view reason); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 307 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 308 | void SendErrorResponseWithRealmAndNonce(TurnServerConnection* conn, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 309 | const StunMessage* req, |
| 310 | int code, |
Ali Tofigh | de2ac5a | 2022-06-30 11:58:26 +0200 | [diff] [blame^] | 311 | absl::string_view reason) |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 312 | RTC_RUN_ON(thread_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 313 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 314 | void SendErrorResponseWithAlternateServer(TurnServerConnection* conn, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 315 | const StunMessage* req, |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 316 | const rtc::SocketAddress& addr) |
| 317 | RTC_RUN_ON(thread_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 318 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 319 | void SendStun(TurnServerConnection* conn, StunMessage* msg); |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 320 | void Send(TurnServerConnection* conn, const rtc::ByteBufferWriter& buf); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 321 | |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 322 | void OnAllocationDestroyed(TurnServerAllocation* allocation) |
| 323 | RTC_RUN_ON(thread_); |
| 324 | void DestroyInternalSocket(rtc::AsyncPacketSocket* socket) |
| 325 | RTC_RUN_ON(thread_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 326 | |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 327 | typedef std::map<rtc::AsyncPacketSocket*, ProtocolType> InternalSocketMap; |
Niels Möller | ac9a288 | 2021-10-20 15:25:09 +0200 | [diff] [blame] | 328 | struct ServerSocketInfo { |
| 329 | ProtocolType proto; |
| 330 | // If non-null, used to wrap accepted sockets. |
| 331 | std::unique_ptr<rtc::SSLAdapterFactory> ssl_adapter_factory; |
| 332 | }; |
| 333 | typedef std::map<rtc::Socket*, ServerSocketInfo> ServerSocketMap; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 334 | |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 335 | rtc::Thread* const thread_; |
| 336 | const std::string nonce_key_; |
| 337 | std::string realm_ RTC_GUARDED_BY(thread_); |
| 338 | std::string software_ RTC_GUARDED_BY(thread_); |
| 339 | TurnAuthInterface* auth_hook_ RTC_GUARDED_BY(thread_); |
| 340 | TurnRedirectInterface* redirect_hook_ RTC_GUARDED_BY(thread_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 341 | // otu - one-time-use. Server will respond with 438 if it's |
| 342 | // sees the same nonce in next transaction. |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 343 | bool enable_otu_nonce_ RTC_GUARDED_BY(thread_); |
deadbeef | 376e123 | 2015-11-25 09:00:08 -0800 | [diff] [blame] | 344 | bool reject_private_addresses_ = false; |
Taylor Brandstetter | ef18470 | 2016-06-23 17:35:47 -0700 | [diff] [blame] | 345 | // Check for permission when receiving an external packet. |
| 346 | bool enable_permission_checks_ = true; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 347 | |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 348 | InternalSocketMap server_sockets_ RTC_GUARDED_BY(thread_); |
| 349 | ServerSocketMap server_listen_sockets_ RTC_GUARDED_BY(thread_); |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 350 | std::unique_ptr<rtc::PacketSocketFactory> external_socket_factory_ |
| 351 | RTC_GUARDED_BY(thread_); |
| 352 | rtc::SocketAddress external_addr_ RTC_GUARDED_BY(thread_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 353 | |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 354 | AllocationMap allocations_ RTC_GUARDED_BY(thread_); |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 355 | |
honghaiz | c463e20 | 2016-02-01 15:19:08 -0800 | [diff] [blame] | 356 | // For testing only. If this is non-zero, the next NONCE will be generated |
| 357 | // 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] | 358 | int64_t ts_for_next_nonce_ RTC_GUARDED_BY(thread_) = 0; |
honghaiz | c463e20 | 2016-02-01 15:19:08 -0800 | [diff] [blame] | 359 | |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 360 | // For testing only. Used to observe STUN messages received. |
Niels Möller | 76b51e2 | 2021-03-18 15:44:24 +0100 | [diff] [blame] | 361 | std::unique_ptr<StunMessageObserver> stun_message_observer_ |
| 362 | RTC_GUARDED_BY(thread_); |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 363 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 364 | friend class TurnServerAllocation; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 365 | }; |
| 366 | |
| 367 | } // namespace cricket |
| 368 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 369 | #endif // P2P_BASE_TURN_SERVER_H_ |