xref: /aosp_15_r20/external/webrtc/p2p/base/turn_server.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright 2012 The WebRTC Project Authors. All rights reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #ifndef P2P_BASE_TURN_SERVER_H_
12*d9f75844SAndroid Build Coastguard Worker #define P2P_BASE_TURN_SERVER_H_
13*d9f75844SAndroid Build Coastguard Worker 
14*d9f75844SAndroid Build Coastguard Worker #include <list>
15*d9f75844SAndroid Build Coastguard Worker #include <map>
16*d9f75844SAndroid Build Coastguard Worker #include <memory>
17*d9f75844SAndroid Build Coastguard Worker #include <set>
18*d9f75844SAndroid Build Coastguard Worker #include <string>
19*d9f75844SAndroid Build Coastguard Worker #include <utility>
20*d9f75844SAndroid Build Coastguard Worker #include <vector>
21*d9f75844SAndroid Build Coastguard Worker 
22*d9f75844SAndroid Build Coastguard Worker #include "absl/strings/string_view.h"
23*d9f75844SAndroid Build Coastguard Worker #include "api/sequence_checker.h"
24*d9f75844SAndroid Build Coastguard Worker #include "api/task_queue/pending_task_safety_flag.h"
25*d9f75844SAndroid Build Coastguard Worker #include "api/task_queue/task_queue_base.h"
26*d9f75844SAndroid Build Coastguard Worker #include "api/units/time_delta.h"
27*d9f75844SAndroid Build Coastguard Worker #include "p2p/base/port_interface.h"
28*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/async_packet_socket.h"
29*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/socket_address.h"
30*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/ssl_adapter.h"
31*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/third_party/sigslot/sigslot.h"
32*d9f75844SAndroid Build Coastguard Worker 
33*d9f75844SAndroid Build Coastguard Worker namespace rtc {
34*d9f75844SAndroid Build Coastguard Worker class ByteBufferWriter;
35*d9f75844SAndroid Build Coastguard Worker class PacketSocketFactory;
36*d9f75844SAndroid Build Coastguard Worker }  // namespace rtc
37*d9f75844SAndroid Build Coastguard Worker 
38*d9f75844SAndroid Build Coastguard Worker namespace cricket {
39*d9f75844SAndroid Build Coastguard Worker 
40*d9f75844SAndroid Build Coastguard Worker class StunMessage;
41*d9f75844SAndroid Build Coastguard Worker class TurnMessage;
42*d9f75844SAndroid Build Coastguard Worker class TurnServer;
43*d9f75844SAndroid Build Coastguard Worker 
44*d9f75844SAndroid Build Coastguard Worker // The default server port for TURN, as specified in RFC5766.
45*d9f75844SAndroid Build Coastguard Worker const int TURN_SERVER_PORT = 3478;
46*d9f75844SAndroid Build Coastguard Worker 
47*d9f75844SAndroid Build Coastguard Worker // Encapsulates the client's connection to the server.
48*d9f75844SAndroid Build Coastguard Worker class TurnServerConnection {
49*d9f75844SAndroid Build Coastguard Worker  public:
TurnServerConnection()50*d9f75844SAndroid Build Coastguard Worker   TurnServerConnection() : proto_(PROTO_UDP), socket_(NULL) {}
51*d9f75844SAndroid Build Coastguard Worker   TurnServerConnection(const rtc::SocketAddress& src,
52*d9f75844SAndroid Build Coastguard Worker                        ProtocolType proto,
53*d9f75844SAndroid Build Coastguard Worker                        rtc::AsyncPacketSocket* socket);
src()54*d9f75844SAndroid Build Coastguard Worker   const rtc::SocketAddress& src() const { return src_; }
socket()55*d9f75844SAndroid Build Coastguard Worker   rtc::AsyncPacketSocket* socket() { return socket_; }
56*d9f75844SAndroid Build Coastguard Worker   bool operator==(const TurnServerConnection& t) const;
57*d9f75844SAndroid Build Coastguard Worker   bool operator<(const TurnServerConnection& t) const;
58*d9f75844SAndroid Build Coastguard Worker   std::string ToString() const;
59*d9f75844SAndroid Build Coastguard Worker 
60*d9f75844SAndroid Build Coastguard Worker  private:
61*d9f75844SAndroid Build Coastguard Worker   rtc::SocketAddress src_;
62*d9f75844SAndroid Build Coastguard Worker   rtc::SocketAddress dst_;
63*d9f75844SAndroid Build Coastguard Worker   cricket::ProtocolType proto_;
64*d9f75844SAndroid Build Coastguard Worker   rtc::AsyncPacketSocket* socket_;
65*d9f75844SAndroid Build Coastguard Worker };
66*d9f75844SAndroid Build Coastguard Worker 
67*d9f75844SAndroid Build Coastguard Worker // Encapsulates a TURN allocation.
68*d9f75844SAndroid Build Coastguard Worker // The object is created when an allocation request is received, and then
69*d9f75844SAndroid Build Coastguard Worker // handles TURN messages (via HandleTurnMessage) and channel data messages
70*d9f75844SAndroid Build Coastguard Worker // (via HandleChannelData) for this allocation when received by the server.
71*d9f75844SAndroid Build Coastguard Worker // The object informs the server when its lifetime timer expires.
72*d9f75844SAndroid Build Coastguard Worker class TurnServerAllocation : public sigslot::has_slots<> {
73*d9f75844SAndroid Build Coastguard Worker  public:
74*d9f75844SAndroid Build Coastguard Worker   TurnServerAllocation(TurnServer* server_,
75*d9f75844SAndroid Build Coastguard Worker                        webrtc::TaskQueueBase* thread,
76*d9f75844SAndroid Build Coastguard Worker                        const TurnServerConnection& conn,
77*d9f75844SAndroid Build Coastguard Worker                        rtc::AsyncPacketSocket* server_socket,
78*d9f75844SAndroid Build Coastguard Worker                        absl::string_view key);
79*d9f75844SAndroid Build Coastguard Worker   ~TurnServerAllocation() override;
80*d9f75844SAndroid Build Coastguard Worker 
conn()81*d9f75844SAndroid Build Coastguard Worker   TurnServerConnection* conn() { return &conn_; }
key()82*d9f75844SAndroid Build Coastguard Worker   const std::string& key() const { return key_; }
transaction_id()83*d9f75844SAndroid Build Coastguard Worker   const std::string& transaction_id() const { return transaction_id_; }
username()84*d9f75844SAndroid Build Coastguard Worker   const std::string& username() const { return username_; }
last_nonce()85*d9f75844SAndroid Build Coastguard Worker   const std::string& last_nonce() const { return last_nonce_; }
set_last_nonce(absl::string_view nonce)86*d9f75844SAndroid Build Coastguard Worker   void set_last_nonce(absl::string_view nonce) {
87*d9f75844SAndroid Build Coastguard Worker     last_nonce_ = std::string(nonce);
88*d9f75844SAndroid Build Coastguard Worker   }
89*d9f75844SAndroid Build Coastguard Worker 
90*d9f75844SAndroid Build Coastguard Worker   std::string ToString() const;
91*d9f75844SAndroid Build Coastguard Worker 
92*d9f75844SAndroid Build Coastguard Worker   void HandleTurnMessage(const TurnMessage* msg);
93*d9f75844SAndroid Build Coastguard Worker   void HandleChannelData(const char* data, size_t size);
94*d9f75844SAndroid Build Coastguard Worker 
95*d9f75844SAndroid Build Coastguard Worker  private:
96*d9f75844SAndroid Build Coastguard Worker   struct Channel {
97*d9f75844SAndroid Build Coastguard Worker     webrtc::ScopedTaskSafety pending_delete;
98*d9f75844SAndroid Build Coastguard Worker     int id;
99*d9f75844SAndroid Build Coastguard Worker     rtc::SocketAddress peer;
100*d9f75844SAndroid Build Coastguard Worker   };
101*d9f75844SAndroid Build Coastguard Worker   struct Permission {
102*d9f75844SAndroid Build Coastguard Worker     webrtc::ScopedTaskSafety pending_delete;
103*d9f75844SAndroid Build Coastguard Worker     rtc::IPAddress peer;
104*d9f75844SAndroid Build Coastguard Worker   };
105*d9f75844SAndroid Build Coastguard Worker   using PermissionList = std::list<Permission>;
106*d9f75844SAndroid Build Coastguard Worker   using ChannelList = std::list<Channel>;
107*d9f75844SAndroid Build Coastguard Worker 
108*d9f75844SAndroid Build Coastguard Worker   void PostDeleteSelf(webrtc::TimeDelta delay);
109*d9f75844SAndroid Build Coastguard Worker 
110*d9f75844SAndroid Build Coastguard Worker   void HandleAllocateRequest(const TurnMessage* msg);
111*d9f75844SAndroid Build Coastguard Worker   void HandleRefreshRequest(const TurnMessage* msg);
112*d9f75844SAndroid Build Coastguard Worker   void HandleSendIndication(const TurnMessage* msg);
113*d9f75844SAndroid Build Coastguard Worker   void HandleCreatePermissionRequest(const TurnMessage* msg);
114*d9f75844SAndroid Build Coastguard Worker   void HandleChannelBindRequest(const TurnMessage* msg);
115*d9f75844SAndroid Build Coastguard Worker 
116*d9f75844SAndroid Build Coastguard Worker   void OnExternalPacket(rtc::AsyncPacketSocket* socket,
117*d9f75844SAndroid Build Coastguard Worker                         const char* data,
118*d9f75844SAndroid Build Coastguard Worker                         size_t size,
119*d9f75844SAndroid Build Coastguard Worker                         const rtc::SocketAddress& addr,
120*d9f75844SAndroid Build Coastguard Worker                         const int64_t& packet_time_us);
121*d9f75844SAndroid Build Coastguard Worker 
122*d9f75844SAndroid Build Coastguard Worker   static webrtc::TimeDelta ComputeLifetime(const TurnMessage& msg);
123*d9f75844SAndroid Build Coastguard Worker   bool HasPermission(const rtc::IPAddress& addr);
124*d9f75844SAndroid Build Coastguard Worker   void AddPermission(const rtc::IPAddress& addr);
125*d9f75844SAndroid Build Coastguard Worker   PermissionList::iterator FindPermission(const rtc::IPAddress& addr);
126*d9f75844SAndroid Build Coastguard Worker   ChannelList::iterator FindChannel(int channel_id);
127*d9f75844SAndroid Build Coastguard Worker   ChannelList::iterator FindChannel(const rtc::SocketAddress& addr);
128*d9f75844SAndroid Build Coastguard Worker 
129*d9f75844SAndroid Build Coastguard Worker   void SendResponse(TurnMessage* msg);
130*d9f75844SAndroid Build Coastguard Worker   void SendBadRequestResponse(const TurnMessage* req);
131*d9f75844SAndroid Build Coastguard Worker   void SendErrorResponse(const TurnMessage* req,
132*d9f75844SAndroid Build Coastguard Worker                          int code,
133*d9f75844SAndroid Build Coastguard Worker                          absl::string_view reason);
134*d9f75844SAndroid Build Coastguard Worker   void SendExternal(const void* data,
135*d9f75844SAndroid Build Coastguard Worker                     size_t size,
136*d9f75844SAndroid Build Coastguard Worker                     const rtc::SocketAddress& peer);
137*d9f75844SAndroid Build Coastguard Worker 
138*d9f75844SAndroid Build Coastguard Worker   TurnServer* const server_;
139*d9f75844SAndroid Build Coastguard Worker   webrtc::TaskQueueBase* const thread_;
140*d9f75844SAndroid Build Coastguard Worker   TurnServerConnection conn_;
141*d9f75844SAndroid Build Coastguard Worker   std::unique_ptr<rtc::AsyncPacketSocket> external_socket_;
142*d9f75844SAndroid Build Coastguard Worker   std::string key_;
143*d9f75844SAndroid Build Coastguard Worker   std::string transaction_id_;
144*d9f75844SAndroid Build Coastguard Worker   std::string username_;
145*d9f75844SAndroid Build Coastguard Worker   std::string last_nonce_;
146*d9f75844SAndroid Build Coastguard Worker   PermissionList perms_;
147*d9f75844SAndroid Build Coastguard Worker   ChannelList channels_;
148*d9f75844SAndroid Build Coastguard Worker   webrtc::ScopedTaskSafety safety_;
149*d9f75844SAndroid Build Coastguard Worker };
150*d9f75844SAndroid Build Coastguard Worker 
151*d9f75844SAndroid Build Coastguard Worker // An interface through which the MD5 credential hash can be retrieved.
152*d9f75844SAndroid Build Coastguard Worker class TurnAuthInterface {
153*d9f75844SAndroid Build Coastguard Worker  public:
154*d9f75844SAndroid Build Coastguard Worker   // Gets HA1 for the specified user and realm.
155*d9f75844SAndroid Build Coastguard Worker   // HA1 = MD5(A1) = MD5(username:realm:password).
156*d9f75844SAndroid Build Coastguard Worker   // Return true if the given username and realm are valid, or false if not.
157*d9f75844SAndroid Build Coastguard Worker   virtual bool GetKey(absl::string_view username,
158*d9f75844SAndroid Build Coastguard Worker                       absl::string_view realm,
159*d9f75844SAndroid Build Coastguard Worker                       std::string* key) = 0;
160*d9f75844SAndroid Build Coastguard Worker   virtual ~TurnAuthInterface() = default;
161*d9f75844SAndroid Build Coastguard Worker };
162*d9f75844SAndroid Build Coastguard Worker 
163*d9f75844SAndroid Build Coastguard Worker // An interface enables Turn Server to control redirection behavior.
164*d9f75844SAndroid Build Coastguard Worker class TurnRedirectInterface {
165*d9f75844SAndroid Build Coastguard Worker  public:
166*d9f75844SAndroid Build Coastguard Worker   virtual bool ShouldRedirect(const rtc::SocketAddress& address,
167*d9f75844SAndroid Build Coastguard Worker                               rtc::SocketAddress* out) = 0;
~TurnRedirectInterface()168*d9f75844SAndroid Build Coastguard Worker   virtual ~TurnRedirectInterface() {}
169*d9f75844SAndroid Build Coastguard Worker };
170*d9f75844SAndroid Build Coastguard Worker 
171*d9f75844SAndroid Build Coastguard Worker class StunMessageObserver {
172*d9f75844SAndroid Build Coastguard Worker  public:
173*d9f75844SAndroid Build Coastguard Worker   virtual void ReceivedMessage(const TurnMessage* msg) = 0;
174*d9f75844SAndroid Build Coastguard Worker   virtual void ReceivedChannelData(const char* data, size_t size) = 0;
~StunMessageObserver()175*d9f75844SAndroid Build Coastguard Worker   virtual ~StunMessageObserver() {}
176*d9f75844SAndroid Build Coastguard Worker };
177*d9f75844SAndroid Build Coastguard Worker 
178*d9f75844SAndroid Build Coastguard Worker // The core TURN server class. Give it a socket to listen on via
179*d9f75844SAndroid Build Coastguard Worker // AddInternalServerSocket, and a factory to create external sockets via
180*d9f75844SAndroid Build Coastguard Worker // SetExternalSocketFactory, and it's ready to go.
181*d9f75844SAndroid Build Coastguard Worker // Not yet wired up: TCP support.
182*d9f75844SAndroid Build Coastguard Worker class TurnServer : public sigslot::has_slots<> {
183*d9f75844SAndroid Build Coastguard Worker  public:
184*d9f75844SAndroid Build Coastguard Worker   typedef std::map<TurnServerConnection, std::unique_ptr<TurnServerAllocation>>
185*d9f75844SAndroid Build Coastguard Worker       AllocationMap;
186*d9f75844SAndroid Build Coastguard Worker 
187*d9f75844SAndroid Build Coastguard Worker   explicit TurnServer(webrtc::TaskQueueBase* thread);
188*d9f75844SAndroid Build Coastguard Worker   ~TurnServer() override;
189*d9f75844SAndroid Build Coastguard Worker 
190*d9f75844SAndroid Build Coastguard Worker   // Gets/sets the realm value to use for the server.
realm()191*d9f75844SAndroid Build Coastguard Worker   const std::string& realm() const {
192*d9f75844SAndroid Build Coastguard Worker     RTC_DCHECK_RUN_ON(thread_);
193*d9f75844SAndroid Build Coastguard Worker     return realm_;
194*d9f75844SAndroid Build Coastguard Worker   }
set_realm(absl::string_view realm)195*d9f75844SAndroid Build Coastguard Worker   void set_realm(absl::string_view realm) {
196*d9f75844SAndroid Build Coastguard Worker     RTC_DCHECK_RUN_ON(thread_);
197*d9f75844SAndroid Build Coastguard Worker     realm_ = std::string(realm);
198*d9f75844SAndroid Build Coastguard Worker   }
199*d9f75844SAndroid Build Coastguard Worker 
200*d9f75844SAndroid Build Coastguard Worker   // Gets/sets the value for the SOFTWARE attribute for TURN messages.
software()201*d9f75844SAndroid Build Coastguard Worker   const std::string& software() const {
202*d9f75844SAndroid Build Coastguard Worker     RTC_DCHECK_RUN_ON(thread_);
203*d9f75844SAndroid Build Coastguard Worker     return software_;
204*d9f75844SAndroid Build Coastguard Worker   }
set_software(absl::string_view software)205*d9f75844SAndroid Build Coastguard Worker   void set_software(absl::string_view software) {
206*d9f75844SAndroid Build Coastguard Worker     RTC_DCHECK_RUN_ON(thread_);
207*d9f75844SAndroid Build Coastguard Worker     software_ = std::string(software);
208*d9f75844SAndroid Build Coastguard Worker   }
209*d9f75844SAndroid Build Coastguard Worker 
allocations()210*d9f75844SAndroid Build Coastguard Worker   const AllocationMap& allocations() const {
211*d9f75844SAndroid Build Coastguard Worker     RTC_DCHECK_RUN_ON(thread_);
212*d9f75844SAndroid Build Coastguard Worker     return allocations_;
213*d9f75844SAndroid Build Coastguard Worker   }
214*d9f75844SAndroid Build Coastguard Worker 
215*d9f75844SAndroid Build Coastguard Worker   // Sets the authentication callback; does not take ownership.
set_auth_hook(TurnAuthInterface * auth_hook)216*d9f75844SAndroid Build Coastguard Worker   void set_auth_hook(TurnAuthInterface* auth_hook) {
217*d9f75844SAndroid Build Coastguard Worker     RTC_DCHECK_RUN_ON(thread_);
218*d9f75844SAndroid Build Coastguard Worker     auth_hook_ = auth_hook;
219*d9f75844SAndroid Build Coastguard Worker   }
220*d9f75844SAndroid Build Coastguard Worker 
set_redirect_hook(TurnRedirectInterface * redirect_hook)221*d9f75844SAndroid Build Coastguard Worker   void set_redirect_hook(TurnRedirectInterface* redirect_hook) {
222*d9f75844SAndroid Build Coastguard Worker     RTC_DCHECK_RUN_ON(thread_);
223*d9f75844SAndroid Build Coastguard Worker     redirect_hook_ = redirect_hook;
224*d9f75844SAndroid Build Coastguard Worker   }
225*d9f75844SAndroid Build Coastguard Worker 
set_enable_otu_nonce(bool enable)226*d9f75844SAndroid Build Coastguard Worker   void set_enable_otu_nonce(bool enable) {
227*d9f75844SAndroid Build Coastguard Worker     RTC_DCHECK_RUN_ON(thread_);
228*d9f75844SAndroid Build Coastguard Worker     enable_otu_nonce_ = enable;
229*d9f75844SAndroid Build Coastguard Worker   }
230*d9f75844SAndroid Build Coastguard Worker 
231*d9f75844SAndroid Build Coastguard Worker   // If set to true, reject CreatePermission requests to RFC1918 addresses.
set_reject_private_addresses(bool filter)232*d9f75844SAndroid Build Coastguard Worker   void set_reject_private_addresses(bool filter) {
233*d9f75844SAndroid Build Coastguard Worker     RTC_DCHECK_RUN_ON(thread_);
234*d9f75844SAndroid Build Coastguard Worker     reject_private_addresses_ = filter;
235*d9f75844SAndroid Build Coastguard Worker   }
236*d9f75844SAndroid Build Coastguard Worker 
set_enable_permission_checks(bool enable)237*d9f75844SAndroid Build Coastguard Worker   void set_enable_permission_checks(bool enable) {
238*d9f75844SAndroid Build Coastguard Worker     RTC_DCHECK_RUN_ON(thread_);
239*d9f75844SAndroid Build Coastguard Worker     enable_permission_checks_ = enable;
240*d9f75844SAndroid Build Coastguard Worker   }
241*d9f75844SAndroid Build Coastguard Worker 
242*d9f75844SAndroid Build Coastguard Worker   // Starts listening for packets from internal clients.
243*d9f75844SAndroid Build Coastguard Worker   void AddInternalSocket(rtc::AsyncPacketSocket* socket, ProtocolType proto);
244*d9f75844SAndroid Build Coastguard Worker   // Starts listening for the connections on this socket. When someone tries
245*d9f75844SAndroid Build Coastguard Worker   // to connect, the connection will be accepted and a new internal socket
246*d9f75844SAndroid Build Coastguard Worker   // will be added.
247*d9f75844SAndroid Build Coastguard Worker   void AddInternalServerSocket(
248*d9f75844SAndroid Build Coastguard Worker       rtc::Socket* socket,
249*d9f75844SAndroid Build Coastguard Worker       ProtocolType proto,
250*d9f75844SAndroid Build Coastguard Worker       std::unique_ptr<rtc::SSLAdapterFactory> ssl_adapter_factory = nullptr);
251*d9f75844SAndroid Build Coastguard Worker   // Specifies the factory to use for creating external sockets.
252*d9f75844SAndroid Build Coastguard Worker   void SetExternalSocketFactory(rtc::PacketSocketFactory* factory,
253*d9f75844SAndroid Build Coastguard Worker                                 const rtc::SocketAddress& address);
254*d9f75844SAndroid Build Coastguard Worker   // For testing only.
SetTimestampForNextNonce(int64_t timestamp)255*d9f75844SAndroid Build Coastguard Worker   std::string SetTimestampForNextNonce(int64_t timestamp) {
256*d9f75844SAndroid Build Coastguard Worker     RTC_DCHECK_RUN_ON(thread_);
257*d9f75844SAndroid Build Coastguard Worker     ts_for_next_nonce_ = timestamp;
258*d9f75844SAndroid Build Coastguard Worker     return GenerateNonce(timestamp);
259*d9f75844SAndroid Build Coastguard Worker   }
260*d9f75844SAndroid Build Coastguard Worker 
SetStunMessageObserver(std::unique_ptr<StunMessageObserver> observer)261*d9f75844SAndroid Build Coastguard Worker   void SetStunMessageObserver(std::unique_ptr<StunMessageObserver> observer) {
262*d9f75844SAndroid Build Coastguard Worker     RTC_DCHECK_RUN_ON(thread_);
263*d9f75844SAndroid Build Coastguard Worker     stun_message_observer_ = std::move(observer);
264*d9f75844SAndroid Build Coastguard Worker   }
265*d9f75844SAndroid Build Coastguard Worker 
266*d9f75844SAndroid Build Coastguard Worker  private:
267*d9f75844SAndroid Build Coastguard Worker   // All private member functions and variables should have access restricted to
268*d9f75844SAndroid Build Coastguard Worker   // thread_. But compile-time annotations are missing for members access from
269*d9f75844SAndroid Build Coastguard Worker   // TurnServerAllocation (via friend declaration), and the On* methods, which
270*d9f75844SAndroid Build Coastguard Worker   // are called via sigslot.
271*d9f75844SAndroid Build Coastguard Worker   std::string GenerateNonce(int64_t now) const RTC_RUN_ON(thread_);
272*d9f75844SAndroid Build Coastguard Worker   void OnInternalPacket(rtc::AsyncPacketSocket* socket,
273*d9f75844SAndroid Build Coastguard Worker                         const char* data,
274*d9f75844SAndroid Build Coastguard Worker                         size_t size,
275*d9f75844SAndroid Build Coastguard Worker                         const rtc::SocketAddress& address,
276*d9f75844SAndroid Build Coastguard Worker                         const int64_t& packet_time_us);
277*d9f75844SAndroid Build Coastguard Worker 
278*d9f75844SAndroid Build Coastguard Worker   void OnNewInternalConnection(rtc::Socket* socket);
279*d9f75844SAndroid Build Coastguard Worker 
280*d9f75844SAndroid Build Coastguard Worker   // Accept connections on this server socket.
281*d9f75844SAndroid Build Coastguard Worker   void AcceptConnection(rtc::Socket* server_socket) RTC_RUN_ON(thread_);
282*d9f75844SAndroid Build Coastguard Worker   void OnInternalSocketClose(rtc::AsyncPacketSocket* socket, int err);
283*d9f75844SAndroid Build Coastguard Worker 
284*d9f75844SAndroid Build Coastguard Worker   void HandleStunMessage(TurnServerConnection* conn,
285*d9f75844SAndroid Build Coastguard Worker                          const char* data,
286*d9f75844SAndroid Build Coastguard Worker                          size_t size) RTC_RUN_ON(thread_);
287*d9f75844SAndroid Build Coastguard Worker   void HandleBindingRequest(TurnServerConnection* conn, const StunMessage* msg)
288*d9f75844SAndroid Build Coastguard Worker       RTC_RUN_ON(thread_);
289*d9f75844SAndroid Build Coastguard Worker   void HandleAllocateRequest(TurnServerConnection* conn,
290*d9f75844SAndroid Build Coastguard Worker                              const TurnMessage* msg,
291*d9f75844SAndroid Build Coastguard Worker                              absl::string_view key) RTC_RUN_ON(thread_);
292*d9f75844SAndroid Build Coastguard Worker 
293*d9f75844SAndroid Build Coastguard Worker   bool GetKey(const StunMessage* msg, std::string* key) RTC_RUN_ON(thread_);
294*d9f75844SAndroid Build Coastguard Worker   bool CheckAuthorization(TurnServerConnection* conn,
295*d9f75844SAndroid Build Coastguard Worker                           StunMessage* msg,
296*d9f75844SAndroid Build Coastguard Worker                           const char* data,
297*d9f75844SAndroid Build Coastguard Worker                           size_t size,
298*d9f75844SAndroid Build Coastguard Worker                           absl::string_view key) RTC_RUN_ON(thread_);
299*d9f75844SAndroid Build Coastguard Worker   bool ValidateNonce(absl::string_view nonce) const RTC_RUN_ON(thread_);
300*d9f75844SAndroid Build Coastguard Worker 
301*d9f75844SAndroid Build Coastguard Worker   TurnServerAllocation* FindAllocation(TurnServerConnection* conn)
302*d9f75844SAndroid Build Coastguard Worker       RTC_RUN_ON(thread_);
303*d9f75844SAndroid Build Coastguard Worker   TurnServerAllocation* CreateAllocation(TurnServerConnection* conn,
304*d9f75844SAndroid Build Coastguard Worker                                          int proto,
305*d9f75844SAndroid Build Coastguard Worker                                          absl::string_view key)
306*d9f75844SAndroid Build Coastguard Worker       RTC_RUN_ON(thread_);
307*d9f75844SAndroid Build Coastguard Worker 
308*d9f75844SAndroid Build Coastguard Worker   void SendErrorResponse(TurnServerConnection* conn,
309*d9f75844SAndroid Build Coastguard Worker                          const StunMessage* req,
310*d9f75844SAndroid Build Coastguard Worker                          int code,
311*d9f75844SAndroid Build Coastguard Worker                          absl::string_view reason);
312*d9f75844SAndroid Build Coastguard Worker 
313*d9f75844SAndroid Build Coastguard Worker   void SendErrorResponseWithRealmAndNonce(TurnServerConnection* conn,
314*d9f75844SAndroid Build Coastguard Worker                                           const StunMessage* req,
315*d9f75844SAndroid Build Coastguard Worker                                           int code,
316*d9f75844SAndroid Build Coastguard Worker                                           absl::string_view reason)
317*d9f75844SAndroid Build Coastguard Worker       RTC_RUN_ON(thread_);
318*d9f75844SAndroid Build Coastguard Worker 
319*d9f75844SAndroid Build Coastguard Worker   void SendErrorResponseWithAlternateServer(TurnServerConnection* conn,
320*d9f75844SAndroid Build Coastguard Worker                                             const StunMessage* req,
321*d9f75844SAndroid Build Coastguard Worker                                             const rtc::SocketAddress& addr)
322*d9f75844SAndroid Build Coastguard Worker       RTC_RUN_ON(thread_);
323*d9f75844SAndroid Build Coastguard Worker 
324*d9f75844SAndroid Build Coastguard Worker   void SendStun(TurnServerConnection* conn, StunMessage* msg);
325*d9f75844SAndroid Build Coastguard Worker   void Send(TurnServerConnection* conn, const rtc::ByteBufferWriter& buf);
326*d9f75844SAndroid Build Coastguard Worker 
327*d9f75844SAndroid Build Coastguard Worker   void DestroyAllocation(TurnServerAllocation* allocation) RTC_RUN_ON(thread_);
328*d9f75844SAndroid Build Coastguard Worker   void DestroyInternalSocket(rtc::AsyncPacketSocket* socket)
329*d9f75844SAndroid Build Coastguard Worker       RTC_RUN_ON(thread_);
330*d9f75844SAndroid Build Coastguard Worker 
331*d9f75844SAndroid Build Coastguard Worker   typedef std::map<rtc::AsyncPacketSocket*, ProtocolType> InternalSocketMap;
332*d9f75844SAndroid Build Coastguard Worker   struct ServerSocketInfo {
333*d9f75844SAndroid Build Coastguard Worker     ProtocolType proto;
334*d9f75844SAndroid Build Coastguard Worker     // If non-null, used to wrap accepted sockets.
335*d9f75844SAndroid Build Coastguard Worker     std::unique_ptr<rtc::SSLAdapterFactory> ssl_adapter_factory;
336*d9f75844SAndroid Build Coastguard Worker   };
337*d9f75844SAndroid Build Coastguard Worker   typedef std::map<rtc::Socket*, ServerSocketInfo> ServerSocketMap;
338*d9f75844SAndroid Build Coastguard Worker 
339*d9f75844SAndroid Build Coastguard Worker   webrtc::TaskQueueBase* const thread_;
340*d9f75844SAndroid Build Coastguard Worker   const std::string nonce_key_;
341*d9f75844SAndroid Build Coastguard Worker   std::string realm_ RTC_GUARDED_BY(thread_);
342*d9f75844SAndroid Build Coastguard Worker   std::string software_ RTC_GUARDED_BY(thread_);
343*d9f75844SAndroid Build Coastguard Worker   TurnAuthInterface* auth_hook_ RTC_GUARDED_BY(thread_);
344*d9f75844SAndroid Build Coastguard Worker   TurnRedirectInterface* redirect_hook_ RTC_GUARDED_BY(thread_);
345*d9f75844SAndroid Build Coastguard Worker   // otu - one-time-use. Server will respond with 438 if it's
346*d9f75844SAndroid Build Coastguard Worker   // sees the same nonce in next transaction.
347*d9f75844SAndroid Build Coastguard Worker   bool enable_otu_nonce_ RTC_GUARDED_BY(thread_);
348*d9f75844SAndroid Build Coastguard Worker   bool reject_private_addresses_ = false;
349*d9f75844SAndroid Build Coastguard Worker   // Check for permission when receiving an external packet.
350*d9f75844SAndroid Build Coastguard Worker   bool enable_permission_checks_ = true;
351*d9f75844SAndroid Build Coastguard Worker 
352*d9f75844SAndroid Build Coastguard Worker   InternalSocketMap server_sockets_ RTC_GUARDED_BY(thread_);
353*d9f75844SAndroid Build Coastguard Worker   ServerSocketMap server_listen_sockets_ RTC_GUARDED_BY(thread_);
354*d9f75844SAndroid Build Coastguard Worker   std::unique_ptr<rtc::PacketSocketFactory> external_socket_factory_
355*d9f75844SAndroid Build Coastguard Worker       RTC_GUARDED_BY(thread_);
356*d9f75844SAndroid Build Coastguard Worker   rtc::SocketAddress external_addr_ RTC_GUARDED_BY(thread_);
357*d9f75844SAndroid Build Coastguard Worker 
358*d9f75844SAndroid Build Coastguard Worker   AllocationMap allocations_ RTC_GUARDED_BY(thread_);
359*d9f75844SAndroid Build Coastguard Worker 
360*d9f75844SAndroid Build Coastguard Worker   // For testing only. If this is non-zero, the next NONCE will be generated
361*d9f75844SAndroid Build Coastguard Worker   // from this value, and it will be reset to 0 after generating the NONCE.
362*d9f75844SAndroid Build Coastguard Worker   int64_t ts_for_next_nonce_ RTC_GUARDED_BY(thread_) = 0;
363*d9f75844SAndroid Build Coastguard Worker 
364*d9f75844SAndroid Build Coastguard Worker   // For testing only. Used to observe STUN messages received.
365*d9f75844SAndroid Build Coastguard Worker   std::unique_ptr<StunMessageObserver> stun_message_observer_
366*d9f75844SAndroid Build Coastguard Worker       RTC_GUARDED_BY(thread_);
367*d9f75844SAndroid Build Coastguard Worker 
368*d9f75844SAndroid Build Coastguard Worker   friend class TurnServerAllocation;
369*d9f75844SAndroid Build Coastguard Worker };
370*d9f75844SAndroid Build Coastguard Worker 
371*d9f75844SAndroid Build Coastguard Worker }  // namespace cricket
372*d9f75844SAndroid Build Coastguard Worker 
373*d9f75844SAndroid Build Coastguard Worker #endif  // P2P_BASE_TURN_SERVER_H_
374