Telegram-Android/TMessagesProj/jni/voip/webrtc/p2p/base/turn_port.h

368 lines
14 KiB
C
Raw Normal View History

2020-08-14 18:58:22 +02:00
/*
* Copyright 2012 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef P2P_BASE_TURN_PORT_H_
#define P2P_BASE_TURN_PORT_H_
#include <stdio.h>
#include <map>
#include <memory>
#include <set>
#include <string>
#include <vector>
#include "absl/memory/memory.h"
2023-02-18 22:24:25 +01:00
#include "absl/strings/string_view.h"
2022-03-11 17:49:54 +01:00
#include "api/async_dns_resolver.h"
2023-02-18 22:24:25 +01:00
#include "api/task_queue/pending_task_safety_flag.h"
#include "api/task_queue/task_queue_base.h"
2020-08-14 18:58:22 +02:00
#include "p2p/base/port.h"
#include "p2p/client/basic_port_allocator.h"
#include "rtc_base/async_packet_socket.h"
#include "rtc_base/ssl_certificate.h"
namespace webrtc {
class TurnCustomizer;
}
namespace cricket {
2020-12-23 08:48:30 +01:00
const int kMaxTurnUsernameLength = 509; // RFC 8489 section 14.3
2020-08-14 18:58:22 +02:00
extern const int STUN_ATTR_TURN_LOGGING_ID;
extern const char TURN_PORT_TYPE[];
class TurnAllocateRequest;
class TurnEntry;
class TurnPort : public Port {
public:
enum PortState {
STATE_CONNECTING, // Initial state, cannot send any packets.
STATE_CONNECTED, // Socket connected, ready to send stun requests.
STATE_READY, // Received allocate success, can send any packets.
STATE_RECEIVEONLY, // Had REFRESH_REQUEST error, cannot send any packets.
STATE_DISCONNECTED, // TCP connection died, cannot send/receive any
// packets.
};
2022-03-11 17:49:54 +01:00
2023-02-18 22:24:25 +01:00
static bool Validate(const CreateRelayPortArgs& args) {
2020-12-23 08:48:30 +01:00
// Do basic parameter validation.
2023-02-18 22:24:25 +01:00
if (args.config->credentials.username.size() > kMaxTurnUsernameLength) {
2021-06-25 02:43:10 +02:00
RTC_LOG(LS_ERROR) << "Attempt to use TURN with a too long username "
2023-02-18 22:24:25 +01:00
<< "of length "
<< args.config->credentials.username.size();
return false;
2021-06-25 02:43:10 +02:00
}
// Do not connect to low-numbered ports. The default STUN port is 3478.
2023-02-18 22:24:25 +01:00
if (!AllowedTurnPort(args.server_address->address.port(),
args.field_trials)) {
2021-06-25 02:43:10 +02:00
RTC_LOG(LS_ERROR) << "Attempt to use TURN to connect to port "
2023-02-18 22:24:25 +01:00
<< args.server_address->address.port();
return false;
}
return true;
}
// Create a TURN port using the shared UDP socket, `socket`.
static std::unique_ptr<TurnPort> Create(const CreateRelayPortArgs& args,
rtc::AsyncPacketSocket* socket) {
if (!Validate(args)) {
2020-12-23 08:48:30 +01:00
return nullptr;
}
2020-08-14 18:58:22 +02:00
// Using `new` to access a non-public constructor.
2022-03-11 17:49:54 +01:00
return absl::WrapUnique(
2023-02-18 22:24:25 +01:00
new TurnPort(args.network_thread, args.socket_factory, args.network,
socket, args.username, args.password, *args.server_address,
args.config->credentials, args.config->priority,
args.config->tls_alpn_protocols,
args.config->tls_elliptic_curves, args.turn_customizer,
args.config->tls_cert_verifier, args.field_trials));
2020-08-14 18:58:22 +02:00
}
2022-03-11 17:49:54 +01:00
// Create a TURN port that will use a new socket, bound to `network` and
// using a port in the range between `min_port` and `max_port`.
2023-02-18 22:24:25 +01:00
static std::unique_ptr<TurnPort> Create(const CreateRelayPortArgs& args,
int min_port,
int max_port) {
if (!Validate(args)) {
2020-12-23 08:48:30 +01:00
return nullptr;
}
2020-08-14 18:58:22 +02:00
// Using `new` to access a non-public constructor.
2023-02-18 22:24:25 +01:00
return absl::WrapUnique(
new TurnPort(args.network_thread, args.socket_factory, args.network,
min_port, max_port, args.username, args.password,
*args.server_address, args.config->credentials,
args.config->priority, args.config->tls_alpn_protocols,
args.config->tls_elliptic_curves, args.turn_customizer,
args.config->tls_cert_verifier, args.field_trials));
2020-08-14 18:58:22 +02:00
}
~TurnPort() override;
const ProtocolAddress& server_address() const { return server_address_; }
// Returns an empty address if the local address has not been assigned.
rtc::SocketAddress GetLocalAddress() const;
bool ready() const { return state_ == STATE_READY; }
bool connected() const {
return state_ == STATE_READY || state_ == STATE_CONNECTED;
}
const RelayCredentials& credentials() const { return credentials_; }
ProtocolType GetProtocol() const override;
virtual TlsCertPolicy GetTlsCertPolicy() const;
virtual void SetTlsCertPolicy(TlsCertPolicy tls_cert_policy);
2023-02-18 22:24:25 +01:00
void SetTurnLoggingId(absl::string_view turn_logging_id);
2020-08-14 18:58:22 +02:00
virtual std::vector<std::string> GetTlsAlpnProtocols() const;
virtual std::vector<std::string> GetTlsEllipticCurves() const;
// Release a TURN allocation by sending a refresh with lifetime 0.
// Sets state to STATE_RECEIVEONLY.
void Release();
void PrepareAddress() override;
Connection* CreateConnection(const Candidate& c,
PortInterface::CandidateOrigin origin) override;
int SendTo(const void* data,
size_t size,
const rtc::SocketAddress& addr,
const rtc::PacketOptions& options,
bool payload) override;
int SetOption(rtc::Socket::Option opt, int value) override;
int GetOption(rtc::Socket::Option opt, int* value) override;
int GetError() override;
bool HandleIncomingPacket(rtc::AsyncPacketSocket* socket,
const char* data,
size_t size,
const rtc::SocketAddress& remote_addr,
int64_t packet_time_us) override;
bool CanHandleIncomingPacketsFrom(
const rtc::SocketAddress& addr) const override;
virtual void OnReadPacket(rtc::AsyncPacketSocket* socket,
const char* data,
size_t size,
const rtc::SocketAddress& remote_addr,
const int64_t& packet_time_us);
void OnSentPacket(rtc::AsyncPacketSocket* socket,
const rtc::SentPacket& sent_packet) override;
virtual void OnReadyToSend(rtc::AsyncPacketSocket* socket);
2023-02-18 22:24:25 +01:00
bool SupportsProtocol(absl::string_view protocol) const override;
2020-08-14 18:58:22 +02:00
void OnSocketConnect(rtc::AsyncPacketSocket* socket);
void OnSocketClose(rtc::AsyncPacketSocket* socket, int error);
const std::string& hash() const { return hash_; }
const std::string& nonce() const { return nonce_; }
int error() const { return error_; }
void OnAllocateMismatch();
rtc::AsyncPacketSocket* socket() const { return socket_; }
2023-02-18 22:24:25 +01:00
StunRequestManager& request_manager() { return request_manager_; }
2020-08-14 18:58:22 +02:00
bool HasRequests() { return !request_manager_.empty(); }
void set_credentials(const RelayCredentials& credentials) {
credentials_ = credentials;
}
2022-03-11 17:49:54 +01:00
// Finds the turn entry with `address` and sets its channel id.
2020-08-14 18:58:22 +02:00
// Returns true if the entry is found.
bool SetEntryChannelId(const rtc::SocketAddress& address, int channel_id);
void HandleConnectionDestroyed(Connection* conn) override;
2023-02-18 22:24:25 +01:00
void CloseForTest() { Close(); }
// TODO(solenberg): Tests should be refactored to not peek at internal state.
class CallbacksForTest {
public:
virtual ~CallbacksForTest() {}
virtual void OnTurnCreatePermissionResult(int code) = 0;
virtual void OnTurnRefreshResult(int code) = 0;
virtual void OnTurnPortClosed() = 0;
};
void SetCallbacksForTest(CallbacksForTest* callbacks);
2020-08-14 18:58:22 +02:00
protected:
2023-02-18 22:24:25 +01:00
TurnPort(webrtc::TaskQueueBase* thread,
2020-08-14 18:58:22 +02:00
rtc::PacketSocketFactory* factory,
2023-02-18 22:24:25 +01:00
const rtc::Network* network,
2020-08-14 18:58:22 +02:00
rtc::AsyncPacketSocket* socket,
2023-02-18 22:24:25 +01:00
absl::string_view username,
absl::string_view password,
2020-08-14 18:58:22 +02:00
const ProtocolAddress& server_address,
const RelayCredentials& credentials,
int server_priority,
2023-02-18 22:24:25 +01:00
const std::vector<std::string>& tls_alpn_protocols,
const std::vector<std::string>& tls_elliptic_curves,
webrtc::TurnCustomizer* customizer,
rtc::SSLCertificateVerifier* tls_cert_verifier = nullptr,
const webrtc::FieldTrialsView* field_trials = nullptr);
2020-08-14 18:58:22 +02:00
2023-02-18 22:24:25 +01:00
TurnPort(webrtc::TaskQueueBase* thread,
2020-08-14 18:58:22 +02:00
rtc::PacketSocketFactory* factory,
2023-02-18 22:24:25 +01:00
const rtc::Network* network,
2020-08-14 18:58:22 +02:00
uint16_t min_port,
uint16_t max_port,
2023-02-18 22:24:25 +01:00
absl::string_view username,
absl::string_view password,
2020-08-14 18:58:22 +02:00
const ProtocolAddress& server_address,
const RelayCredentials& credentials,
int server_priority,
const std::vector<std::string>& tls_alpn_protocols,
const std::vector<std::string>& tls_elliptic_curves,
webrtc::TurnCustomizer* customizer,
2023-02-18 22:24:25 +01:00
rtc::SSLCertificateVerifier* tls_cert_verifier = nullptr,
const webrtc::FieldTrialsView* field_trials = nullptr);
2020-08-14 18:58:22 +02:00
2022-03-11 17:49:54 +01:00
// NOTE: This method needs to be accessible for StunPort
2020-08-14 18:58:22 +02:00
// return true if entry was created (i.e channel_number consumed).
2023-02-18 22:24:25 +01:00
bool CreateOrRefreshEntry(Connection* conn, int channel_number);
2020-08-14 18:58:22 +02:00
rtc::DiffServCodePoint StunDscpValue() const override;
2023-02-18 22:24:25 +01:00
// Shuts down the turn port, frees requests and deletes connections.
void Close();
2020-08-14 18:58:22 +02:00
2023-02-18 22:24:25 +01:00
private:
2020-08-14 18:58:22 +02:00
typedef std::map<rtc::Socket::Option, int> SocketOptionsMap;
typedef std::set<rtc::SocketAddress> AttemptedServerSet;
2023-02-18 22:24:25 +01:00
static bool AllowedTurnPort(int port,
const webrtc::FieldTrialsView* field_trials);
void TryAlternateServer();
2020-08-14 18:58:22 +02:00
bool CreateTurnClientSocket();
2023-02-18 22:24:25 +01:00
void set_nonce(absl::string_view nonce) { nonce_ = std::string(nonce); }
void set_realm(absl::string_view realm) {
2020-08-14 18:58:22 +02:00
if (realm != realm_) {
2023-02-18 22:24:25 +01:00
realm_ = std::string(realm);
2020-08-14 18:58:22 +02:00
UpdateHash();
}
}
void OnRefreshError();
void HandleRefreshError();
bool SetAlternateServer(const rtc::SocketAddress& address);
void ResolveTurnAddress(const rtc::SocketAddress& address);
void OnResolveResult(rtc::AsyncResolverInterface* resolver);
void AddRequestAuthInfo(StunMessage* msg);
void OnSendStunPacket(const void* data, size_t size, StunRequest* request);
// Stun address from allocate success response.
// Currently used only for testing.
void OnStunAddress(const rtc::SocketAddress& address);
void OnAllocateSuccess(const rtc::SocketAddress& address,
const rtc::SocketAddress& stun_address);
2023-02-18 22:24:25 +01:00
void OnAllocateError(int error_code, absl::string_view reason);
2020-08-14 18:58:22 +02:00
void OnAllocateRequestTimeout();
void HandleDataIndication(const char* data,
size_t size,
int64_t packet_time_us);
void HandleChannelData(int channel_id,
const char* data,
size_t size,
int64_t packet_time_us);
void DispatchPacket(const char* data,
size_t size,
const rtc::SocketAddress& remote_addr,
ProtocolType proto,
int64_t packet_time_us);
bool ScheduleRefresh(uint32_t lifetime);
void SendRequest(StunRequest* request, int delay);
int Send(const void* data, size_t size, const rtc::PacketOptions& options);
void UpdateHash();
bool UpdateNonce(StunMessage* response);
void ResetNonce();
bool HasPermission(const rtc::IPAddress& ipaddr) const;
TurnEntry* FindEntry(const rtc::SocketAddress& address) const;
TurnEntry* FindEntry(int channel_id) const;
2022-03-11 17:49:54 +01:00
// Marks the connection with remote address `address` failed and
2020-08-14 18:58:22 +02:00
// pruned (a.k.a. write-timed-out). Returns true if a connection is found.
bool FailAndPruneConnection(const rtc::SocketAddress& address);
// Reconstruct the URL of the server which the candidate is gathered from.
2023-02-18 22:24:25 +01:00
std::string ReconstructedServerUrl();
2020-08-14 18:58:22 +02:00
void MaybeAddTurnLoggingId(StunMessage* message);
void TurnCustomizerMaybeModifyOutgoingStunMessage(StunMessage* message);
bool TurnCustomizerAllowChannelData(const void* data,
size_t size,
bool payload);
ProtocolAddress server_address_;
TlsCertPolicy tls_cert_policy_ = TlsCertPolicy::TLS_CERT_POLICY_SECURE;
std::vector<std::string> tls_alpn_protocols_;
std::vector<std::string> tls_elliptic_curves_;
rtc::SSLCertificateVerifier* tls_cert_verifier_;
RelayCredentials credentials_;
AttemptedServerSet attempted_server_addresses_;
rtc::AsyncPacketSocket* socket_;
SocketOptionsMap socket_options_;
2022-03-11 17:49:54 +01:00
std::unique_ptr<webrtc::AsyncDnsResolverInterface> resolver_;
2020-08-14 18:58:22 +02:00
int error_;
rtc::DiffServCodePoint stun_dscp_value_;
StunRequestManager request_manager_;
std::string realm_; // From 401/438 response message.
std::string nonce_; // From 401/438 response message.
std::string hash_; // Digest of username:realm:password
int next_channel_number_;
2023-02-18 22:24:25 +01:00
std::vector<std::unique_ptr<TurnEntry>> entries_;
2020-08-14 18:58:22 +02:00
PortState state_;
// By default the value will be set to 0. This value will be used in
// calculating the candidate priority.
int server_priority_;
// The number of retries made due to allocate mismatch error.
size_t allocate_mismatch_retries_;
// Optional TurnCustomizer that can modify outgoing messages. Once set, this
// must outlive the TurnPort's lifetime.
webrtc::TurnCustomizer* turn_customizer_ = nullptr;
// Optional TurnLoggingId.
// An identifier set by application that is added to TURN_ALLOCATE_REQUEST
// and can be used to match client/backend logs.
// TODO(jonaso): This should really be initialized in constructor,
// but that is currently so terrible. Fix once constructor is changed
// to be more easy to work with.
std::string turn_logging_id_;
2021-06-25 02:43:10 +02:00
webrtc::ScopedTaskSafety task_safety_;
2023-02-18 22:24:25 +01:00
CallbacksForTest* callbacks_for_test_ = nullptr;
2020-08-14 18:58:22 +02:00
friend class TurnEntry;
friend class TurnAllocateRequest;
friend class TurnRefreshRequest;
friend class TurnCreatePermissionRequest;
friend class TurnChannelBindRequest;
};
} // namespace cricket
#endif // P2P_BASE_TURN_PORT_H_