turn/allocation/
five_tuple.rs

1#[cfg(test)]
2mod five_tuple_test;
3
4use std::fmt;
5use std::net::{Ipv4Addr, SocketAddr};
6
7use crate::proto::*;
8
9/// `FiveTuple` is the combination (client IP address and port, server IP
10/// address and port, and transport protocol (currently one of UDP,
11/// TCP, or TLS)) used to communicate between the client and the
12/// server.  The 5-tuple uniquely identifies this communication
13/// stream.  The 5-tuple also uniquely identifies the Allocation on
14/// the server.
15#[derive(PartialEq, Eq, Clone, Copy, Hash)]
16pub struct FiveTuple {
17    pub protocol: Protocol,
18    pub src_addr: SocketAddr,
19    pub dst_addr: SocketAddr,
20}
21
22impl Default for FiveTuple {
23    fn default() -> Self {
24        FiveTuple {
25            protocol: PROTO_UDP,
26            src_addr: SocketAddr::new(Ipv4Addr::new(0, 0, 0, 0).into(), 0),
27            dst_addr: SocketAddr::new(Ipv4Addr::new(0, 0, 0, 0).into(), 0),
28        }
29    }
30}
31
32impl fmt::Display for FiveTuple {
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        write!(f, "{}_{}_{}", self.protocol, self.src_addr, self.dst_addr)
35    }
36}
37
38impl fmt::Debug for FiveTuple {
39    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40        f.debug_struct("FiveTuple")
41            .field("protocol", &self.protocol)
42            .field("src_addr", &self.src_addr)
43            .field("dst_addr", &self.dst_addr)
44            .finish()
45    }
46}