falco_event/types/net/
ipv6addr.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
56
57
58
59
60
61
use crate::event_derive::{FromBytes, FromBytesError, FromBytesResult, ToBytes};
use crate::types::format::Format;
use std::fmt::Formatter;
use std::io::{Read, Write};
use std::net::Ipv6Addr;

impl FromBytes<'_> for Ipv6Addr {
    fn from_bytes(buf: &mut &[u8]) -> FromBytesResult<Self> {
        if buf.len() < 16 {
            return Err(FromBytesError::InvalidLength);
        }

        let mut out = [0u8; 16];
        buf.read_exact(&mut out)?;
        Ok(out.into())
    }
}

impl ToBytes for Ipv6Addr {
    fn binary_size(&self) -> usize {
        16
    }

    fn write<W: Write>(&self, mut writer: W) -> std::io::Result<()> {
        writer.write_all(self.octets().as_slice())?;
        Ok(())
    }

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

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

#[cfg(test)]
mod tests {
    use std::str::FromStr;

    use super::*;

    #[test]
    fn test_ipv6_addr() {
        let ip = Ipv6Addr::from_str("2001:4860:4860::8844").unwrap();

        let mut binary = Vec::new();
        ip.write(&mut binary).unwrap();
        assert_eq!(
            binary.as_slice(),
            b"\x20\x01\x48\x60\x48\x60\0\0\0\0\0\0\0\0\x88\x44".as_slice()
        );

        let mut buf = binary.as_slice();
        let ip2 = Ipv6Addr::from_bytes(&mut buf).unwrap();
        assert_eq!(ip, ip2);
    }
}