webrtc_turn/proto/
relayaddr.rs

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