webrtc_turn/proto/
peeraddr.rs1#[cfg(test)]
2mod peeraddr_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)]
21pub struct PeerAddress {
22 pub ip: IpAddr,
23 pub port: u16,
24}
25
26impl Default for PeerAddress {
27 fn default() -> Self {
28 PeerAddress {
29 ip: IpAddr::V4(Ipv4Addr::from(0)),
30 port: 0,
31 }
32 }
33}
34
35impl fmt::Display for PeerAddress {
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 PeerAddress {
45 fn add_to(&self, m: &mut Message) -> Result<(), Error> {
47 let a = XORMappedAddress {
48 ip: self.ip,
49 port: self.port,
50 };
51 a.add_to_as(m, ATTR_XOR_PEER_ADDRESS)
52 }
53}
54
55impl Getter for PeerAddress {
56 fn get_from(&mut self, m: &Message) -> Result<(), Error> {
58 let mut a = XORMappedAddress::default();
59 a.get_from_as(m, ATTR_XOR_PEER_ADDRESS)?;
60 self.ip = a.ip;
61 self.port = a.port;
62 Ok(())
63 }
64}
65
66pub type XORPeerAddress = PeerAddress;