rtc_turn/proto/
relayaddr.rs1#[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#[derive(PartialEq, Eq, Debug)]
19pub struct RelayedAddress {
20 pub ip: IpAddr,
22 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 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 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
66pub type XorRelayedAddress = RelayedAddress;