1 #[cfg(test)] 2 mod addr_test; 3 4 use super::*; 5 6 use std::net::{IpAddr, Ipv4Addr, SocketAddr}; 7 8 // Addr is ip:port. 9 #[derive(PartialEq, Eq, Debug)] 10 pub struct Addr { 11 ip: IpAddr, 12 port: u16, 13 } 14 15 impl Default for Addr { default() -> Self16 fn default() -> Self { 17 Addr { 18 ip: IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 19 port: 0, 20 } 21 } 22 } 23 24 impl fmt::Display for Addr { fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result25 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 26 write!(f, "{}:{}", self.ip, self.port) 27 } 28 } 29 30 impl Addr { 31 // Network implements net.Addr. network(&self) -> String32 pub fn network(&self) -> String { 33 "turn".to_owned() 34 } 35 36 // sets addr. from_socket_addr(n: &SocketAddr) -> Self37 pub fn from_socket_addr(n: &SocketAddr) -> Self { 38 let ip = n.ip(); 39 let port = n.port(); 40 41 Addr { ip, port } 42 } 43 44 // EqualIP returns true if a and b have equal IP addresses. equal_ip(&self, other: &Addr) -> bool45 pub fn equal_ip(&self, other: &Addr) -> bool { 46 self.ip == other.ip 47 } 48 } 49 50 // FiveTuple represents 5-TUPLE value. 51 #[derive(PartialEq, Eq, Default)] 52 pub struct FiveTuple { 53 pub client: Addr, 54 pub server: Addr, 55 pub proto: Protocol, 56 } 57 58 impl fmt::Display for FiveTuple { fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result59 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 60 write!(f, "{}->{} ({})", self.client, self.server, self.proto) 61 } 62 } 63