falco_event/types/net/
endpoint.rs

1use crate::fields::{FromBytes, FromBytesError, ToBytes};
2use std::io::Write;
3use std::net::{Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6};
4
5impl ToBytes for SocketAddrV4 {
6    #[inline]
7    fn binary_size(&self) -> usize {
8        self.ip().binary_size() + self.port().binary_size()
9    }
10
11    //noinspection DuplicatedCode
12    #[inline]
13    fn write<W: Write>(&self, mut writer: W) -> std::io::Result<()> {
14        self.ip().write(&mut writer)?;
15        self.port().write(writer)
16    }
17
18    #[inline]
19    fn default_repr() -> impl ToBytes {
20        SocketAddrV4::new(Ipv4Addr::from(0), 0)
21    }
22}
23
24impl FromBytes<'_> for SocketAddrV4 {
25    #[inline]
26    fn from_bytes(buf: &mut &'_ [u8]) -> Result<Self, FromBytesError> {
27        Ok(SocketAddrV4::new(
28            FromBytes::from_bytes(buf)?,
29            FromBytes::from_bytes(buf)?,
30        ))
31    }
32}
33
34impl ToBytes for SocketAddrV6 {
35    #[inline]
36    fn binary_size(&self) -> usize {
37        self.ip().binary_size() + self.port().binary_size()
38    }
39
40    //noinspection DuplicatedCode
41    #[inline]
42    fn write<W: Write>(&self, mut writer: W) -> std::io::Result<()> {
43        self.ip().write(&mut writer)?;
44        self.port().write(writer)
45    }
46
47    #[inline]
48    fn default_repr() -> impl ToBytes {
49        SocketAddrV6::new(Ipv6Addr::from(0), 0, 0, 0)
50    }
51}
52
53impl FromBytes<'_> for SocketAddrV6 {
54    #[inline]
55    fn from_bytes(buf: &mut &'_ [u8]) -> Result<Self, FromBytesError> {
56        Ok(SocketAddrV6::new(
57            FromBytes::from_bytes(buf)?,
58            FromBytes::from_bytes(buf)?,
59            0,
60            0,
61        ))
62    }
63}