clia_turn/proto/
addr.rs

1#[cfg(test)]
2mod addr_test;
3
4use std::net::{IpAddr, Ipv4Addr, SocketAddr};
5
6use super::*;
7
8/// `Addr` is `ip:port`.
9#[derive(PartialEq, Eq, Debug)]
10pub struct Addr {
11    ip: IpAddr,
12    port: u16,
13}
14
15impl Default for Addr {
16    fn default() -> Self {
17        Addr {
18            ip: IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)),
19            port: 0,
20        }
21    }
22}
23
24impl fmt::Display for Addr {
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        write!(f, "{}:{}", self.ip, self.port)
27    }
28}
29
30impl Addr {
31    /// Returns this network.
32    pub fn network(&self) -> String {
33        "turn".to_owned()
34    }
35
36    /// Creates a new [`Addr`] from `n`.
37    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    /// Returns `true` if the `other` has the same IP address.
45    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)]
52pub struct FiveTuple {
53    pub client: Addr,
54    pub server: Addr,
55    pub proto: Protocol,
56}
57
58impl fmt::Display for FiveTuple {
59    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60        write!(f, "{}->{} ({})", self.client, self.server, self.proto)
61    }
62}