pub struct Client { /* private fields */ }Expand description
Client is a STUN client
Implementations§
Source§impl Client
impl Client
Sourcepub fn allocate(&mut self, now: Instant) -> Result<TransactionId>
pub fn allocate(&mut self, now: Instant) -> Result<TransactionId>
Sends an Allocate request for a UDP relayed address to the TURN server.
Returns the id that Event::AllocateResponse, Event::AllocateError or
Event::TransactionTimeout will name.
Sourcepub fn allocate_with(
&mut self,
now: Instant,
options: AllocateOptions,
) -> Result<TransactionId>
pub fn allocate_with( &mut self, now: Instant, options: AllocateOptions, ) -> Result<TransactionId>
Sends an Allocate request with options (RFC 8656 §7.1): an address family, a second
(IPv6) address, a lifetime, DONT-FRAGMENT, an even port, a reserved port.
§Errors
Fails, with nothing sent, if the RFC forbids the combination of options (see
AllocateOptions), or if the server has refused the address family and may not be
asked again: for good after a 440 (Address Family not Supported), for a minute after
it had no capacity for one of two families, and after it answered with an address of
a family that was not asked for (RFC 8656 §7.3, §7.4). An application that believes
the cause is gone calls Client::clear_allocate_restrictions.
Sourcepub fn clear_allocate_restrictions(&mut self)
pub fn clear_allocate_restrictions(&mut self)
Lets Client::allocate_with ask the server again for what it refused: an address
family it answered 440 or 508 for, or any allocation after it answered with a family
that was not asked for. RFC 8656 §7.3 and §7.4 forbid asking again until the client
“believes the mismatch has been fixed”; calling this says the application does.
Source§impl Client
impl Client
Sourcepub fn new(
config: ClientConfig,
crypto_provider: Arc<dyn RTCCryptoProvider>,
) -> Result<Self>
pub fn new( config: ClientConfig, crypto_provider: Arc<dyn RTCCryptoProvider>, ) -> Result<Self>
Returns a new TURN client.
The crypto provider is supplied by the caller; this crate never resolves a default.
Sourcepub fn relay(&mut self, relayed_addr: SocketAddr) -> Result<Relay<'_>>
pub fn relay(&mut self, relayed_addr: SocketAddr) -> Result<Relay<'_>>
Borrows the allocation for relayed_addr so data can be sent or permissions created.
§Errors
Fails if this client has no allocation for that address — it was never allocated, or has already been closed.
Sourcepub fn has_transaction(&self, id: &TransactionId) -> bool
pub fn has_transaction(&self, id: &TransactionId) -> bool
Whether this client is waiting on a response for transaction id.
A STUN response belongs to whoever sent the matching request — RFC 5389 §7.3.3 matches them by transaction ID, and nothing else identifies the owner. That is only a question worth asking when several STUN users share one socket, which is exactly the situation a host is in when it points a STUN gatherer and this client at the same server address: both see every response, and the four-tuple is identical for both, so addresses cannot say whose a response is.
Such a host calls this to route a response, rather than guessing from the peer address and
handing it to a client that will silently discard it — handle_read
drops a response with no matching transaction, as it must.
Applies to responses only. TURN ChannelData carries no transaction ID at all, and indications are not transaction-matched; both are routed by address.
Sourcepub fn send_binding_request_to(
&mut self,
now: Instant,
to: SocketAddr,
) -> Result<TransactionId>
pub fn send_binding_request_to( &mut self, now: Instant, to: SocketAddr, ) -> Result<TransactionId>
send_binding_request_to sends a new STUN request to the given transport address return key to find out corresponding Event either BindingResponse or BindingRequestTimeout
Sourcepub fn send_binding_request(&mut self, now: Instant) -> Result<TransactionId>
pub fn send_binding_request(&mut self, now: Instant) -> Result<TransactionId>
send_binding_request sends a new STUN request to the STUN server return key to find out corresponding Event either BindingResponse or BindingRequestTimeout
Sourcepub fn update_credentials(
&mut self,
username: String,
password: String,
) -> Result<()>
pub fn update_credentials( &mut self, username: String, password: String, ) -> Result<()>
Replaces the long-term credential used to sign subsequent requests, keeping any existing allocation.
This is for a new password for the same username. An allocation belongs to the username that created it: RFC 8656 §5 has the server answer a request for it made under another username with 441 (Wrong Credentials). Giving up the allocation and allocating again is no way round a rotation either, since the server still holds the allocation for the 5-tuple and answers the new Allocate with 437 (Allocation Mismatch). So with a new password the existing allocation is re-signed, and its permissions and channel bindings survive.
A new username takes effect for the next allocate only: RFC 8656
section 5 has every request about an allocation use the username that created it, so
live allocations keep signing with theirs (and their password). To move to the new
username, Relay::close an allocation, wait for Event::AllocationReleased, and
allocate again.
The realm is not re-negotiated. It was learned from the server’s 401 during the
first Allocate, and a credential rotation keeps the same server, so it still
applies. Follow this with Self::refresh_allocations so the server sees the new
credential before the allocation would otherwise expire.
Sourcepub fn refresh_allocations(&mut self, now: Instant) -> Result<()>
pub fn refresh_allocations(&mut self, now: Instant) -> Result<()>
Refreshes every live allocation, re-signing each with the current credential.
Each allocation is refreshed with its own current lifetime, so this extends rather
than changes it. Intended to follow update_credentials.
Sourcepub fn follow_alternate(
&mut self,
now: Instant,
redirect: &TryAlternate,
) -> Result<SocketAddr>
pub fn follow_alternate( &mut self, now: Instant, redirect: &TryAlternate, ) -> Result<SocketAddr>
Points the client at the alternate server of a 300 (Try Alternate), for an application that chooses to follow the redirection (RFC 8489 §10, RFC 8656 §7.4). The client never follows one by itself: the application owns the socket and the choice of server.
redirect comes from ErrorResponse::try_alternate, which is there only for a 300
that was authenticated with the allocation’s credential; RFC 8489 §14.8 has a client
validate the response before it redirects itself. (An application that trusts the
address of an unauthenticated 300, ErrorResponse::alternate_server, makes the
TryAlternate itself.) The server of the address family the client reached the old
one over is taken, and becomes Client::turn_server_addr. The application then
calls Client::allocate again: the request goes to the alternate server over the
same transport protocol and with the same credentials. Over TLS or DTLS the
application checks the new server’s certificate against
TryAlternate::domain when it is there, and against the name it used for the old
server when it is not.
§Errors
Fails, and leaves the server as it is, when an Allocate was already sent to the alternate server within the last five minutes: the redirection is a loop and RFC 8489 §10 has the client ignore it and take the transaction as failed. Fails as well while the client holds an allocation (its server cannot change), or when no TURN server is configured.
Sourcepub fn set_path_mtu(&mut self, mtu: Option<usize>)
pub fn set_path_mtu(&mut self, mtu: Option<usize>)
The path MTU to the server, when the application knows it (RFC 8489 Section 6.1): a
request over UDP larger than it allows is refused with ErrPacketTooBig. None (the
default): unknown, 576 bytes for IPv4 and 1280 for IPv6.
Sourcepub fn turn_server_addr(&self) -> Option<SocketAddr>
pub fn turn_server_addr(&self) -> Option<SocketAddr>
Returns the TURN server address, if configured.
Sourcepub fn local_addr(&self) -> SocketAddr
pub fn local_addr(&self) -> SocketAddr
Returns the local address this client is bound to.