Skip to main content

rtc_turn/proto/
relayaddr.rs

1#[cfg(test)]
2mod relayaddr_test;
3
4use std::fmt;
5use std::net::{IpAddr, Ipv4Addr};
6
7use shared::error::Result;
8use stun::attributes::*;
9use stun::message::*;
10use stun::xoraddr::*;
11
12/// `RelayedAddress` implements `XOR-RELAYED-ADDRESS` attribute.
13///
14/// It specifies the address and port that the server allocated to the
15/// client. It is encoded in the same way as `XOR-MAPPED-ADDRESS`.
16///
17/// [RFC 5766 Section 14.5](https://www.rfc-editor.org/rfc/rfc5766#section-14.5).
18#[derive(PartialEq, Eq, Debug)]
19pub struct RelayedAddress {
20    /// The relayed IP address.
21    pub ip: IpAddr,
22    /// The relayed port.
23    pub port: u16,
24}
25
26impl Default for RelayedAddress {
27    fn default() -> Self {
28        RelayedAddress {
29            ip: IpAddr::V4(Ipv4Addr::from(0)),
30            port: 0,
31        }
32    }
33}
34
35impl fmt::Display for RelayedAddress {
36    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37        match self.ip {
38            IpAddr::V4(_) => write!(f, "{}:{}", self.ip, self.port),
39            IpAddr::V6(_) => write!(f, "[{}]:{}", self.ip, self.port),
40        }
41    }
42}
43
44impl Setter for RelayedAddress {
45    /// Adds `XOR-PEER-ADDRESS` to message.
46    fn add_to(&self, m: &mut Message) -> Result<()> {
47        let a = XorMappedAddress {
48            ip: self.ip,
49            port: self.port,
50        };
51        a.add_to_as(m, ATTR_XOR_RELAYED_ADDRESS)
52    }
53}
54
55impl Getter for RelayedAddress {
56    /// Decodes `XOR-PEER-ADDRESS` from message.
57    fn get_from(&mut self, m: &Message) -> Result<()> {
58        let mut a = XorMappedAddress::default();
59        a.get_from_as(m, ATTR_XOR_RELAYED_ADDRESS)?;
60        self.ip = a.ip;
61        self.port = a.port;
62        Ok(())
63    }
64}
65
66/// `XorRelayedAddress` implements `XOR-RELAYED-ADDRESS` attribute.
67///
68/// It specifies the address and port that the server allocated to the
69/// client. It is encoded in the same way as `XOR-MAPPED-ADDRESS`.
70///
71/// [RFC 5766 Section 14.5](https://www.rfc-editor.org/rfc/rfc5766#section-14.5).
72pub type XorRelayedAddress = RelayedAddress;