webrtc_turn/proto/
relayaddr.rs1#[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#[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 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 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
65pub type XORRelayedAddress = RelayedAddress;