webrtc_turn/relay/
relay_none.rs

1use super::*;
2use crate::errors::*;
3
4use util::vnet::net::*;
5
6use async_trait::async_trait;
7
8// RelayAddressGeneratorNone returns the listener with no modifications
9pub struct RelayAddressGeneratorNone {
10    // Address is passed to Listen/ListenPacket when creating the Relay
11    pub address: String,
12    pub net: Arc<Net>,
13}
14
15#[async_trait]
16impl RelayAddressGenerator for RelayAddressGeneratorNone {
17    // validate confirms that the RelayAddressGenerator is properly initialized
18    fn validate(&self) -> Result<(), Error> {
19        if self.address.is_empty() {
20            Err(ERR_LISTENING_ADDRESS_INVALID.to_owned())
21        } else {
22            Ok(())
23        }
24    }
25
26    // Allocate a PacketConn (UDP) RelayAddress
27    async fn allocate_conn(
28        &self,
29        use_ipv4: bool,
30        requested_port: u16,
31    ) -> Result<(Arc<dyn Conn + Send + Sync>, SocketAddr), Error> {
32        let addr = self
33            .net
34            .resolve_addr(use_ipv4, &format!("{}:{}", self.address, requested_port))
35            .await?;
36        let conn = self.net.bind(addr).await?;
37        let relay_addr = conn.local_addr().await?;
38        Ok((conn, relay_addr))
39    }
40}