#include "p2p/base/turn_server.h"
#include <algorithm>
#include <memory>
#include <tuple>
#include <utility>
#include "absl/algorithm/container.h"
#include "absl/memory/memory.h"
#include "absl/strings/string_view.h"
#include "api/array_view.h"
#include "api/packet_socket_factory.h"
#include "api/task_queue/task_queue_base.h"
#include "api/transport/stun.h"
#include "p2p/base/async_stun_tcp_socket.h"
#include "rtc_base/byte_buffer.h"
#include "rtc_base/checks.h"
#include "rtc_base/crypto_random.h"
#include "rtc_base/logging.h"
#include "rtc_base/message_digest.h"
#include "rtc_base/socket_adapters.h"
#include "rtc_base/strings/string_builder.h"
namespace cricket {
namespace {
TimeDelta;
constexpr TimeDelta kNonceTimeout = …;
constexpr TimeDelta kDefaultAllocationTimeout = …;
constexpr TimeDelta kPermissionTimeout = …;
constexpr TimeDelta kChannelTimeout = …;
constexpr size_t kNonceKeySize = …;
constexpr size_t kNonceSize = …;
constexpr size_t TURN_CHANNEL_HEADER_SIZE = …;
bool IsTurnChannelData(uint16_t msg_type) { … }
}
int GetStunSuccessResponseTypeOrZero(const StunMessage& req) { … }
int GetStunErrorResponseTypeOrZero(const StunMessage& req) { … }
static void InitErrorResponse(int code,
absl::string_view reason,
StunMessage* resp) { … }
TurnServer::TurnServer(webrtc::TaskQueueBase* thread)
: … { … }
TurnServer::~TurnServer() { … }
void TurnServer::AddInternalSocket(rtc::AsyncPacketSocket* socket,
ProtocolType proto) { … }
void TurnServer::AddInternalServerSocket(
rtc::Socket* socket,
ProtocolType proto,
std::unique_ptr<rtc::SSLAdapterFactory> ssl_adapter_factory) { … }
void TurnServer::SetExternalSocketFactory(
rtc::PacketSocketFactory* factory,
const rtc::SocketAddress& external_addr) { … }
void TurnServer::OnNewInternalConnection(rtc::Socket* socket) { … }
void TurnServer::AcceptConnection(rtc::Socket* server_socket) { … }
void TurnServer::OnInternalSocketClose(rtc::AsyncPacketSocket* socket,
int err) { … }
void TurnServer::OnInternalPacket(rtc::AsyncPacketSocket* socket,
const rtc::ReceivedPacket& packet) { … }
void TurnServer::HandleStunMessage(TurnServerConnection* conn,
rtc::ArrayView<const uint8_t> payload) { … }
bool TurnServer::GetKey(const StunMessage* msg, std::string* key) { … }
bool TurnServer::CheckAuthorization(TurnServerConnection* conn,
StunMessage* msg,
absl::string_view key) { … }
void TurnServer::HandleBindingRequest(TurnServerConnection* conn,
const StunMessage* req) { … }
void TurnServer::HandleAllocateRequest(TurnServerConnection* conn,
const TurnMessage* msg,
absl::string_view key) { … }
std::string TurnServer::GenerateNonce(int64_t now) const { … }
bool TurnServer::ValidateNonce(absl::string_view nonce) const { … }
TurnServerAllocation* TurnServer::FindAllocation(TurnServerConnection* conn) { … }
TurnServerAllocation* TurnServer::CreateAllocation(TurnServerConnection* conn,
int proto,
absl::string_view key) { … }
void TurnServer::SendErrorResponse(TurnServerConnection* conn,
const StunMessage* req,
int code,
absl::string_view reason) { … }
void TurnServer::SendErrorResponseWithRealmAndNonce(TurnServerConnection* conn,
const StunMessage* msg,
int code,
absl::string_view reason) { … }
void TurnServer::SendErrorResponseWithAlternateServer(
TurnServerConnection* conn,
const StunMessage* msg,
const rtc::SocketAddress& addr) { … }
void TurnServer::SendStun(TurnServerConnection* conn, StunMessage* msg) { … }
void TurnServer::Send(TurnServerConnection* conn,
const rtc::ByteBufferWriter& buf) { … }
void TurnServer::DestroyAllocation(TurnServerAllocation* allocation) { … }
void TurnServer::DestroyInternalSocket(rtc::AsyncPacketSocket* socket) { … }
TurnServerConnection::TurnServerConnection(const rtc::SocketAddress& src,
ProtocolType proto,
rtc::AsyncPacketSocket* socket)
: … { … }
bool TurnServerConnection::operator==(const TurnServerConnection& c) const { … }
bool TurnServerConnection::operator<(const TurnServerConnection& c) const { … }
std::string TurnServerConnection::ToString() const { … }
TurnServerAllocation::TurnServerAllocation(TurnServer* server,
webrtc::TaskQueueBase* thread,
const TurnServerConnection& conn,
rtc::AsyncPacketSocket* socket,
absl::string_view key)
: … { … }
TurnServerAllocation::~TurnServerAllocation() { … }
std::string TurnServerAllocation::ToString() const { … }
void TurnServerAllocation::HandleTurnMessage(const TurnMessage* msg) { … }
void TurnServerAllocation::HandleAllocateRequest(const TurnMessage* msg) { … }
void TurnServerAllocation::HandleRefreshRequest(const TurnMessage* msg) { … }
void TurnServerAllocation::HandleSendIndication(const TurnMessage* msg) { … }
void TurnServerAllocation::HandleCreatePermissionRequest(
const TurnMessage* msg) { … }
void TurnServerAllocation::HandleChannelBindRequest(const TurnMessage* msg) { … }
void TurnServerAllocation::HandleChannelData(
rtc::ArrayView<const uint8_t> payload) { … }
void TurnServerAllocation::OnExternalPacket(rtc::AsyncPacketSocket* socket,
const rtc::ReceivedPacket& packet) { … }
TimeDelta TurnServerAllocation::ComputeLifetime(const TurnMessage& msg) { … }
bool TurnServerAllocation::HasPermission(const rtc::IPAddress& addr) { … }
void TurnServerAllocation::AddPermission(const rtc::IPAddress& addr) { … }
TurnServerAllocation::PermissionList::iterator
TurnServerAllocation::FindPermission(const rtc::IPAddress& addr) { … }
TurnServerAllocation::ChannelList::iterator TurnServerAllocation::FindChannel(
int channel_id) { … }
TurnServerAllocation::ChannelList::iterator TurnServerAllocation::FindChannel(
const rtc::SocketAddress& addr) { … }
void TurnServerAllocation::SendResponse(TurnMessage* msg) { … }
void TurnServerAllocation::SendBadRequestResponse(const TurnMessage* req) { … }
void TurnServerAllocation::SendErrorResponse(const TurnMessage* req,
int code,
absl::string_view reason) { … }
void TurnServerAllocation::SendExternal(const void* data,
size_t size,
const rtc::SocketAddress& peer) { … }
void TurnServerAllocation::PostDeleteSelf(TimeDelta delay) { … }
}