/* * 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.
*/
externconstint STUN_ATTR_TURN_LOGGING_ID; externconstchar 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.
};
staticbool Validate(const CreateRelayPortArgs& args) { // Do basic parameter validation. if (args.config->credentials.username.size() > kMaxTurnUsernameLength) {
RTC_LOG(LS_ERROR) << "Attempt to use TURN with a too long username "
<< "of length "
<< args.config->credentials.username.size(); returnfalse;
} // Do not connect to low-numbered ports. The default STUN port is 3478. if (!AllowedTurnPort(args.server_address->address.port(),
args.field_trials)) {
RTC_LOG(LS_ERROR) << "Attempt to use TURN to connect to port "
<< args.server_address->address.port(); returnfalse;
} returntrue;
}
// 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)) { return nullptr;
} // Using `new` to access a non-public constructor. return absl::WrapUnique( new TurnPort({.network_thread = args.network_thread,
.socket_factory = args.socket_factory,
.network = args.network,
.ice_username_fragment = args.username,
.ice_password = args.password,
.field_trials = args.field_trials},
socket, *args.server_address, args.config->credentials,
args.relative_priority, args.config->tls_alpn_protocols,
args.config->tls_elliptic_curves, args.turn_customizer,
args.config->tls_cert_verifier));
}
// 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`. static std::unique_ptr<TurnPort> Create(const CreateRelayPortArgs& args, int min_port, int max_port) { if (!Validate(args)) { return nullptr;
} // Using `new` to access a non-public constructor. return absl::WrapUnique(new TurnPort(
{.network_thread = args.network_thread,
.socket_factory = args.socket_factory,
.network = args.network,
.ice_username_fragment = args.username,
.ice_password = args.password,
.field_trials = args.field_trials},
min_port, max_port, *args.server_address, args.config->credentials,
args.relative_priority, args.config->tls_alpn_protocols,
args.config->tls_elliptic_curves, args.turn_customizer,
args.config->tls_cert_verifier));
}
~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;
// Checks if a connection exists for `addr` before forwarding the call to // the base class. void SendBindingErrorResponse(StunMessage* message, const rtc::SocketAddress& addr, int error_code,
absl::string_view reason) override;
bool HasRequests() { return !request_manager_.empty(); } void set_credentials(const RelayCredentials& credentials) {
credentials_ = credentials;
} // Finds the turn entry with `address` and sets its channel id. // Returns true if the entry is found. // This method must not be used in production, it is a test only // utility that doesn't check the channel id is valid according to // RFC5766. bool SetEntryChannelIdForTesting(const rtc::SocketAddress& address, int channel_id);
// NOTE: This method needs to be accessible for StunPort // return true if entry was created (i.e channel_number consumed). bool CreateOrRefreshEntry(Connection* conn, int channel_number);
// Marks the connection with remote address `address` failed and // pruned (a.k.a. write-timed-out). Returns true if a connection is found. bool FailAndPruneConnection(const rtc::SocketAddress& address);
ProtocolAddress server_address_; // Reconstruct the URL of the server which the candidate is gathered from. // A copy needs to be stored as server_address_ will resolve and clear its // hostname field.
std::string ReconstructServerUrl();
std::string server_url_;
rtc::AsyncPacketSocket* socket_;
SocketOptionsMap socket_options_;
std::unique_ptr<webrtc::AsyncDnsResolverInterface> resolver_; 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_;
std::vector<std::unique_ptr<TurnEntry>> entries_;
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_;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.