Skip to main content

Client

Struct Client 

Source
pub struct Client { /* private fields */ }
Expand description

Client is a STUN client

Implementations§

Source§

impl Client

Source

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.

Source

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.

Source

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

Source

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.

Source

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.

Source

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.

Source

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

Source

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

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn turn_server_addr(&self) -> Option<SocketAddr>

Returns the TURN server address, if configured.

Source

pub fn local_addr(&self) -> SocketAddr

Returns the local address this client is bound to.

Trait Implementations§

Source§

impl Protocol<TransportMessage<BytesMut>, TransportMessage<BytesMut>, ()> for Client

Source§

type Rout = ()

Output read message type Read more
Source§

type Wout = TransportMessage<BytesMut>

Output write message type Read more
Source§

type Eout = Event

Output event type Read more
Source§

type Error = Error

Error type for protocol operations
Source§

type Time = Instant

Time/Instant type for timeout handling Read more
Source§

fn handle_read(&mut self, msg: TaggedBytesMut) -> Result<(), Self::Error>

Handle an incoming read message. Read more
Source§

fn poll_read(&mut self) -> Option<Self::Rout>

Poll for a processed read message. Read more
Source§

fn handle_write(&mut self, _msg: TaggedBytesMut) -> Result<(), Self::Error>

Handle an outgoing write message. Read more
Source§

fn poll_write(&mut self) -> Option<Self::Wout>

Poll for a processed write message. Read more
Source§

fn poll_event(&mut self) -> Option<Self::Eout>

Poll for a generated event. Read more
Source§

fn handle_timeout(&mut self, now: Instant) -> Result<(), Self::Error>

Handle a timeout event. Read more
Source§

fn poll_timeout(&mut self) -> Option<Instant>

Poll for the next timeout deadline. Read more
Source§

fn close(&mut self) -> Result<(), Self::Error>

Close the protocol. Read more
Source§

fn handle_event(&mut self, _evt: Ein) -> Result<(), Self::Error>

Handle a custom event. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Client

§

impl !UnwindSafe for Client

§

impl Forget for Client

§

impl Freeze for Client

§

impl Move for Client

§

impl Send for Client

§

impl Sync for Client

§

impl Unpin for Client

§

impl UnsafeUnpin for Client

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.