xref: /webrtc/turn/src/allocation/five_tuple.rs (revision 26682c3d)
1 #[cfg(test)]
2 mod five_tuple_test;
3 
4 use crate::proto::*;
5 
6 use std::fmt;
7 use std::net::{Ipv4Addr, SocketAddr};
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)]
16 pub struct FiveTuple {
17     pub protocol: Protocol,
18     pub src_addr: SocketAddr,
19     pub dst_addr: SocketAddr,
20 }
21 
22 impl Default for FiveTuple {
default() -> Self23     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 
32 impl fmt::Display for FiveTuple {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result33     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34         write!(f, "{}_{}_{}", self.protocol, self.src_addr, self.dst_addr)
35     }
36 }
37 
38 impl fmt::Debug for FiveTuple {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result39     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 }
47