falco_event/types/net/
ipnet.rs

1use crate::fields::FromBytes;
2use crate::fields::{FromBytesError, ToBytes};
3use std::fmt::{Debug, Formatter};
4use std::io::Write;
5use std::net::IpAddr;
6
7/// An IP network
8///
9/// This is a wrapper around [IpAddr] that makes it a distinct type, suitable for storing
10/// IP (v4 or v6) subnets.
11#[derive(Copy, Clone, PartialEq, Eq)]
12pub struct IpNet(pub IpAddr);
13
14impl FromBytes<'_> for IpNet {
15    #[inline]
16    fn from_bytes(buf: &mut &[u8]) -> Result<Self, FromBytesError> {
17        Ok(Self(IpAddr::from_bytes(buf)?))
18    }
19}
20
21impl ToBytes for IpNet {
22    #[inline]
23    fn binary_size(&self) -> usize {
24        self.0.binary_size()
25    }
26
27    #[inline]
28    fn write<W: Write>(&self, writer: W) -> std::io::Result<()> {
29        self.0.write(writer)
30    }
31
32    #[inline]
33    fn default_repr() -> impl ToBytes {
34        IpAddr::default_repr()
35    }
36}
37
38impl Debug for IpNet {
39    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
40        Debug::fmt(&self.0, f)
41    }
42}