#include "p2p/base/turn_port.h"
#include <cstdint>
#include <functional>
#include <memory>
#include <utility>
#include <vector>
#include "absl/algorithm/container.h"
#include "absl/strings/match.h"
#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
#include "api/task_queue/pending_task_safety_flag.h"
#include "api/transport/stun.h"
#include "api/turn_customizer.h"
#include "p2p/base/connection.h"
#include "p2p/base/p2p_constants.h"
#include "rtc_base/async_packet_socket.h"
#include "rtc_base/byte_order.h"
#include "rtc_base/checks.h"
#include "rtc_base/experiments/field_trial_parser.h"
#include "rtc_base/logging.h"
#include "rtc_base/net_helpers.h"
#include "rtc_base/network/received_packet.h"
#include "rtc_base/socket_address.h"
#include "rtc_base/strings/string_builder.h"
namespace cricket {
IceCandidateType;
SafeTask;
TaskQueueBase;
TimeDelta;
static const int TURN_ALLOCATE_REQUEST = …;
const int STUN_ATTR_TURN_LOGGING_ID = …;
static const int TURN_DEFAULT_PORT = …;
static const int TURN_CHANNEL_NUMBER_START = …;
static constexpr TimeDelta kTurnPermissionTimeout = …;
static const size_t TURN_CHANNEL_HEADER_SIZE = …;
static const size_t MAX_ALLOCATE_MISMATCH_RETRIES = …;
static const int TURN_SUCCESS_RESULT_CODE = …;
inline bool IsTurnChannelData(uint16_t msg_type) { … }
static int GetRelayPreference(cricket::ProtocolType proto) { … }
class TurnAllocateRequest : public StunRequest { … };
class TurnRefreshRequest : public StunRequest { … };
class TurnCreatePermissionRequest : public StunRequest { … };
class TurnChannelBindRequest : public StunRequest { … };
class TurnEntry : public sigslot::has_slots<> { … };
TurnPort::TurnPort(const PortParametersRef& args,
rtc::AsyncPacketSocket* socket,
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,
rtc::SSLCertificateVerifier* tls_cert_verifier)
: … { … }
TurnPort::TurnPort(const PortParametersRef& args,
uint16_t min_port,
uint16_t max_port,
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,
rtc::SSLCertificateVerifier* tls_cert_verifier)
: … { … }
TurnPort::TurnPort(webrtc::TaskQueueBase* thread,
rtc::PacketSocketFactory* factory,
const rtc::Network* network,
rtc::AsyncPacketSocket* socket,
absl::string_view username,
absl::string_view password,
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,
rtc::SSLCertificateVerifier* tls_cert_verifier,
const webrtc::FieldTrialsView* field_trials)
: … { … }
TurnPort::TurnPort(webrtc::TaskQueueBase* thread,
rtc::PacketSocketFactory* factory,
const rtc::Network* network,
uint16_t min_port,
uint16_t max_port,
absl::string_view username,
absl::string_view password,
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,
rtc::SSLCertificateVerifier* tls_cert_verifier,
const webrtc::FieldTrialsView* field_trials)
: … { … }
TurnPort::~TurnPort() { … }
void TurnPort::set_realm(absl::string_view realm) { … }
rtc::SocketAddress TurnPort::GetLocalAddress() const { … }
ProtocolType TurnPort::GetProtocol() const { … }
TlsCertPolicy TurnPort::GetTlsCertPolicy() const { … }
void TurnPort::SetTlsCertPolicy(TlsCertPolicy tls_cert_policy) { … }
void TurnPort::SetTurnLoggingId(absl::string_view turn_logging_id) { … }
std::vector<std::string> TurnPort::GetTlsAlpnProtocols() const { … }
std::vector<std::string> TurnPort::GetTlsEllipticCurves() const { … }
void TurnPort::PrepareAddress() { … }
bool TurnPort::CreateTurnClientSocket() { … }
void TurnPort::OnSocketConnect(rtc::AsyncPacketSocket* socket) { … }
void TurnPort::OnSocketClose(rtc::AsyncPacketSocket* socket, int error) { … }
void TurnPort::OnAllocateMismatch() { … }
Connection* TurnPort::CreateConnection(const Candidate& remote_candidate,
CandidateOrigin origin) { … }
bool TurnPort::FailAndPruneConnection(const rtc::SocketAddress& address) { … }
int TurnPort::SetOption(rtc::Socket::Option opt, int value) { … }
int TurnPort::GetOption(rtc::Socket::Option opt, int* value) { … }
int TurnPort::GetError() { … }
int TurnPort::SendTo(const void* data,
size_t size,
const rtc::SocketAddress& addr,
const rtc::PacketOptions& options,
bool payload) { … }
bool TurnPort::CanHandleIncomingPacketsFrom(
const rtc::SocketAddress& addr) const { … }
void TurnPort::SendBindingErrorResponse(StunMessage* message,
const rtc::SocketAddress& addr,
int error_code,
absl::string_view reason) { … }
bool TurnPort::HandleIncomingPacket(rtc::AsyncPacketSocket* socket,
const rtc::ReceivedPacket& packet) { … }
void TurnPort::OnReadPacket(rtc::AsyncPacketSocket* socket,
const rtc::ReceivedPacket& packet) { … }
void TurnPort::OnSentPacket(rtc::AsyncPacketSocket* socket,
const rtc::SentPacket& sent_packet) { … }
void TurnPort::OnReadyToSend(rtc::AsyncPacketSocket* socket) { … }
bool TurnPort::SupportsProtocol(absl::string_view protocol) const { … }
bool TurnPort::SetAlternateServer(const rtc::SocketAddress& address) { … }
void TurnPort::ResolveTurnAddress(const rtc::SocketAddress& address) { … }
void TurnPort::OnSendStunPacket(const void* data,
size_t size,
StunRequest* request) { … }
void TurnPort::OnStunAddress(const rtc::SocketAddress& address) { … }
void TurnPort::OnAllocateSuccess(const rtc::SocketAddress& address,
const rtc::SocketAddress& stun_address) { … }
void TurnPort::OnAllocateError(int error_code, absl::string_view reason) { … }
void TurnPort::OnRefreshError() { … }
void TurnPort::HandleRefreshError() { … }
void TurnPort::Release() { … }
void TurnPort::Close() { … }
rtc::DiffServCodePoint TurnPort::StunDscpValue() const { … }
bool TurnPort::AllowedTurnPort(int port,
const webrtc::FieldTrialsView* field_trials) { … }
void TurnPort::TryAlternateServer() { … }
void TurnPort::OnAllocateRequestTimeout() { … }
void TurnPort::HandleDataIndication(const char* data,
size_t size,
int64_t packet_time_us) { … }
void TurnPort::HandleChannelData(int channel_id,
const char* data,
size_t size,
int64_t packet_time_us) { … }
void TurnPort::DispatchPacket(const char* data,
size_t size,
const rtc::SocketAddress& remote_addr,
ProtocolType proto,
int64_t packet_time_us) { … }
bool TurnPort::ScheduleRefresh(uint32_t lifetime) { … }
void TurnPort::SendRequest(StunRequest* req, int delay) { … }
void TurnPort::AddRequestAuthInfo(StunMessage* msg) { … }
int TurnPort::Send(const void* data,
size_t len,
const rtc::PacketOptions& options) { … }
void TurnPort::UpdateHash() { … }
bool TurnPort::UpdateNonce(StunMessage* response) { … }
void TurnPort::ResetNonce() { … }
bool TurnPort::HasPermission(const rtc::IPAddress& ipaddr) const { … }
TurnEntry* TurnPort::FindEntry(const rtc::SocketAddress& addr) const { … }
TurnEntry* TurnPort::FindEntry(int channel_id) const { … }
bool TurnPort::CreateOrRefreshEntry(Connection* conn, int channel_number) { … }
void TurnPort::HandleConnectionDestroyed(Connection* conn) { … }
void TurnPort::SetCallbacksForTest(CallbacksForTest* callbacks) { … }
bool TurnPort::SetEntryChannelIdForTesting(const rtc::SocketAddress& address,
int channel_id) { … }
std::string TurnPort::ReconstructServerUrl() { … }
void TurnPort::TurnCustomizerMaybeModifyOutgoingStunMessage(
StunMessage* message) { … }
bool TurnPort::TurnCustomizerAllowChannelData(const void* data,
size_t size,
bool payload) { … }
void TurnPort::MaybeAddTurnLoggingId(StunMessage* msg) { … }
TurnAllocateRequest::TurnAllocateRequest(TurnPort* port)
: … { … }
void TurnAllocateRequest::OnSent() { … }
void TurnAllocateRequest::OnResponse(StunMessage* response) { … }
void TurnAllocateRequest::OnErrorResponse(StunMessage* response) { … }
void TurnAllocateRequest::OnTimeout() { … }
void TurnAllocateRequest::OnAuthChallenge(StunMessage* response, int code) { … }
void TurnAllocateRequest::OnTryAlternate(StunMessage* response, int code) { … }
TurnRefreshRequest::TurnRefreshRequest(TurnPort* port, int lifetime )
: … { … }
void TurnRefreshRequest::OnSent() { … }
void TurnRefreshRequest::OnResponse(StunMessage* response) { … }
void TurnRefreshRequest::OnErrorResponse(StunMessage* response) { … }
void TurnRefreshRequest::OnTimeout() { … }
TurnCreatePermissionRequest::TurnCreatePermissionRequest(
TurnPort* port,
TurnEntry* entry,
const rtc::SocketAddress& ext_addr)
: … { … }
TurnCreatePermissionRequest::~TurnCreatePermissionRequest() { … }
void TurnCreatePermissionRequest::OnSent() { … }
void TurnCreatePermissionRequest::OnResponse(StunMessage* response) { … }
void TurnCreatePermissionRequest::OnErrorResponse(StunMessage* response) { … }
void TurnCreatePermissionRequest::OnTimeout() { … }
TurnChannelBindRequest::TurnChannelBindRequest(
TurnPort* port,
TurnEntry* entry,
int channel_id,
const rtc::SocketAddress& ext_addr)
: … { … }
TurnChannelBindRequest::~TurnChannelBindRequest() { … }
void TurnChannelBindRequest::OnSent() { … }
void TurnChannelBindRequest::OnResponse(StunMessage* response) { … }
void TurnChannelBindRequest::OnErrorResponse(StunMessage* response) { … }
void TurnChannelBindRequest::OnTimeout() { … }
TurnEntry::TurnEntry(TurnPort* port, Connection* conn, int channel_id)
: … { … }
TurnEntry::~TurnEntry() { … }
void TurnEntry::TrackConnection(Connection* conn) { … }
rtc::scoped_refptr<webrtc::PendingTaskSafetyFlag> TurnEntry::UntrackConnection(
Connection* conn) { … }
void TurnEntry::SendCreatePermissionRequest(int delay) { … }
void TurnEntry::SendChannelBindRequest(int delay) { … }
int TurnEntry::Send(const void* data,
size_t size,
bool payload,
const rtc::PacketOptions& options) { … }
void TurnEntry::OnCreatePermissionSuccess() { … }
void TurnEntry::OnCreatePermissionError(StunMessage* response, int code) { … }
void TurnEntry::OnCreatePermissionTimeout() { … }
void TurnEntry::OnChannelBindSuccess() { … }
void TurnEntry::OnChannelBindError(StunMessage* response, int code) { … }
void TurnEntry::OnChannelBindTimeout() { … }
}