xref: /webrtc/turn/src/allocation/five_tuple.rs (revision 04f0bd9e)
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)]
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 {
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 
32 impl 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 
38 impl FiveTuple {
39     // fingerprint is the identity of a FiveTuple
40     pub fn fingerprint(&self) -> String {
41         self.to_string()
42     }
43 }
44