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    pub ip: IpAddr,
21    pub port: u16,
22}
23
24impl Default for RelayedAddress {
25    fn default() -> Self {
26        RelayedAddress {
27            ip: IpAddr::V4(Ipv4Addr::from(0)),
28            port: 0,
29        }
30    }
31}
32
33impl fmt::Display for RelayedAddress {
34    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35        match self.ip {
36            IpAddr::V4(_) => write!(f, "{}:{}", self.ip, self.port),
37            IpAddr::V6(_) => write!(f, "[{}]:{}", self.ip, self.port),
38        }
39    }
40}
41
42impl Setter for RelayedAddress {
43    /// Adds `XOR-PEER-ADDRESS` to message.
44    fn add_to(&self, m: &mut Message) -> Result<()> {
45        let a = XorMappedAddress {
46            ip: self.ip,
47            port: self.port,
48        };
49        a.add_to_as(m, ATTR_XOR_RELAYED_ADDRESS)
50    }
51}
52
53impl Getter for RelayedAddress {
54    /// Decodes `XOR-PEER-ADDRESS` from message.
55    fn get_from(&mut self, m: &Message) -> Result<()> {
56        let mut a = XorMappedAddress::default();
57        a.get_from_as(m, ATTR_XOR_RELAYED_ADDRESS)?;
58        self.ip = a.ip;
59        self.port = a.port;
60        Ok(())
61    }
62}
63
64/// `XorRelayedAddress` implements `XOR-RELAYED-ADDRESS` attribute.
65///
66/// It specifies the address and port that the server allocated to the
67/// client. It is encoded in the same way as `XOR-MAPPED-ADDRESS`.
68///
69/// [RFC 5766 Section 14.5](https://www.rfc-editor.org/rfc/rfc5766#section-14.5).
70pub type XorRelayedAddress = RelayedAddress;