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,
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 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 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
64pub type XorRelayedAddress = RelayedAddress;