falco_event/types/net/
ipv6net.rs

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
use crate::event_derive::{FromBytes, FromBytesResult, ToBytes};
use crate::types::format::Format;
use std::fmt::Formatter;
use std::io::Write;
use std::net::Ipv6Addr;

/// An IPv6 network
///
/// This is a wrapper around [Ipv6Addr] that makes it a distinct type, suitable for storing
/// IPv6 subnets.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Ipv6Net(pub Ipv6Addr);

impl FromBytes<'_> for Ipv6Net {
    fn from_bytes(buf: &mut &[u8]) -> FromBytesResult<Self> {
        Ok(Self(Ipv6Addr::from_bytes(buf)?))
    }
}

impl ToBytes for Ipv6Net {
    fn binary_size(&self) -> usize {
        self.0.binary_size()
    }

    fn write<W: Write>(&self, writer: W) -> std::io::Result<()> {
        self.0.write(writer)
    }

    fn default_repr() -> impl ToBytes {
        Ipv6Addr::default_repr()
    }
}

impl<F> Format<F> for Ipv6Net {
    fn format(&self, fmt: &mut Formatter) -> std::fmt::Result {
        write!(fmt, "{}", self.0)
    }
}

#[cfg(all(test, feature = "serde"))]
mod serde_tests {
    use std::net::Ipv6Addr;

    #[test]
    fn test_serde_ipv6net() {
        let endpoint = super::Ipv6Net(Ipv6Addr::LOCALHOST);

        let json = serde_json::to_string(&endpoint).unwrap();
        assert_eq!(json, "\"::1\"");

        let endpoint2: super::Ipv6Net = serde_json::from_str(&json).unwrap();
        assert_eq!(endpoint, endpoint2);
    }
}