1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#[cfg(test)]
mod addr_test;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use super::*;
/// `Addr` is `ip:port`.
#[derive(PartialEq, Eq, Debug)]
pub struct Addr {
ip: IpAddr,
port: u16,
}
impl Default for Addr {
fn default() -> Self {
Addr {
ip: IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)),
port: 0,
}
}
}
impl fmt::Display for Addr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}:{}", self.ip, self.port)
}
}
impl Addr {
/// Returns this network.
pub fn network(&self) -> String {
"turn".to_owned()
}
/// Creates a new [`Addr`] from `n`.
pub fn from_socket_addr(n: &SocketAddr) -> Self {
let ip = n.ip();
let port = n.port();
Addr { ip, port }
}
/// Returns `true` if the `other` has the same IP address.
pub fn equal_ip(&self, other: &Addr) -> bool {
self.ip == other.ip
}
}
// FiveTuple represents 5-TUPLE value.
#[derive(PartialEq, Eq, Default)]
/// The five-tuple that uniquely identifies a TURN allocation.
///
/// Client address, server address and transport together — the server keys allocations by
/// this, so the same client may hold several over different transports.
pub struct FiveTuple {
/// The client's transport address.
pub client: Addr,
/// The server's transport address.
pub server: Addr,
/// The transport carrying the allocation.
pub proto: Protocol,
}
impl fmt::Display for FiveTuple {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}->{} ({})", self.client, self.server, self.proto)
}
}