pako_tools/
three_tuple.rs

1use std::{
2    fmt,
3    net::{IpAddr, Ipv4Addr},
4};
5
6use serde::Serialize;
7
8/// Network 3-tuple: layer 4 protocol (e.g TCP or UDP), source and destination IP addresses
9#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd, Serialize)]
10pub struct ThreeTuple {
11    /// Source IP address
12    pub src: IpAddr,
13    /// Destination IP address
14    pub dst: IpAddr,
15    /// Layer 4 protocol (e.g TCP, UDP)
16    pub l4_proto: u8,
17}
18
19impl ThreeTuple {
20    pub fn l3_proto(&self) -> u16 {
21        match self.src {
22            IpAddr::V4(_) => 0x0800,
23            IpAddr::V6(_) => 0x86DD,
24        }
25    }
26}
27
28impl Default for ThreeTuple {
29    fn default() -> Self {
30        ThreeTuple {
31            src: IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)),
32            dst: IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)),
33            l4_proto: 0,
34        }
35    }
36}
37
38impl fmt::Display for ThreeTuple {
39    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
40        write!(f, "{} -> {} [{}]", self.src, self.dst, self.l4_proto)
41    }
42}