falco_event/types/net/
sockaddr.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use crate::event_derive::{FromBytes, FromBytesResult, ToBytes};
use crate::ffi::{PPM_AF_INET, PPM_AF_INET6, PPM_AF_LOCAL, PPM_AF_UNSPEC};
use crate::types::format::Format;
use crate::types::{Borrow, Borrowed, EndpointV4, EndpointV6};
use byteorder::{ReadBytesExt, WriteBytesExt};
use std::ffi::OsStr;
use std::fmt::Formatter;
use std::io::Write;
use std::os::unix::ffi::OsStrExt;
use std::path::{Path, PathBuf};

/// A socket address
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
#[derive(Debug)]
pub enum SockAddr<'a> {
    /// Unix sockets
    Unix(&'a Path),

    /// IPv4 sockets
    V4(EndpointV4),

    /// IPv6 socket
    V6(EndpointV6),

    /// any other address family is represented as the number (`PPM_AF_*` constant) and the raw data
    Other(
        u8,
        #[cfg_attr(feature = "serde", serde(with = "crate::types::serde::bytebuf"))] &'a [u8],
    ),
}

impl ToBytes for SockAddr<'_> {
    fn binary_size(&self) -> usize {
        match self {
            SockAddr::Unix(p) => 1 + p.binary_size(),
            SockAddr::V4(addr) => 1 + addr.binary_size(),
            SockAddr::V6(addr) => 1 + addr.binary_size(),
            SockAddr::Other(_, buf) => 1 + buf.len(),
        }
    }

    fn write<W: Write>(&self, mut writer: W) -> std::io::Result<()> {
        match self {
            SockAddr::Unix(p) => {
                writer.write_u8(PPM_AF_LOCAL as u8)?;
                p.write(writer)
            }
            SockAddr::V4(addr) => {
                writer.write_u8(PPM_AF_INET as u8)?;
                addr.write(writer)
            }
            SockAddr::V6(addr) => {
                writer.write_u8(PPM_AF_INET6 as u8)?;
                addr.write(writer)
            }
            SockAddr::Other(af, addr) => {
                writer.write_u8(*af)?;
                ToBytes::write(addr, writer)
            }
        }
    }

    fn default_repr() -> impl ToBytes {
        PPM_AF_UNSPEC as u8
    }
}

impl<'a> FromBytes<'a> for SockAddr<'a> {
    fn from_bytes(buf: &mut &'a [u8]) -> FromBytesResult<Self> {
        let variant = buf.read_u8()?;
        match variant as u32 {
            PPM_AF_LOCAL => {
                let path = <OsStr as OsStrExt>::from_bytes(buf);
                *buf = &[];
                Ok(Self::Unix(Path::new(path)))
            }
            PPM_AF_INET => {
                let addr = EndpointV4::from_bytes(buf)?;
                Ok(Self::V4(addr))
            }
            PPM_AF_INET6 => {
                let addr = EndpointV6::from_bytes(buf)?;
                Ok(Self::V6(addr))
            }
            _ => Ok(Self::Other(variant, buf)),
        }
    }
}

impl<F> Format<F> for SockAddr<'_>
where
    EndpointV4: Format<F>,
    EndpointV6: Format<F>,
    for<'a> &'a [u8]: Format<F>,
{
    fn format(&self, fmt: &mut Formatter) -> std::fmt::Result {
        match self {
            SockAddr::Unix(u) => {
                let bytes = u.as_os_str().as_bytes();
                fmt.write_str("unix://")?;
                bytes.format(fmt)
            }
            SockAddr::V4(v4) => v4.format(fmt),
            SockAddr::V6(v6) => v6.format(fmt),
            SockAddr::Other(af, raw) => write!(fmt, "<af={}>{:02x?}", af, raw),
        }
    }
}

/// A socket address (owned)
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
#[derive(Debug)]
pub enum OwnedSockAddr {
    /// Unix sockets
    Unix(PathBuf),

    /// IPv4 sockets
    V4(EndpointV4),

    /// IPv6 socket
    V6(EndpointV6),

    /// any other address family is represented as the number (`PPM_AF_*` constant) and the raw data
    Other(
        u8,
        #[cfg_attr(feature = "serde", serde(with = "crate::types::serde::bytebuf"))] Vec<u8>,
    ),
}

impl Borrowed for SockAddr<'_> {
    type Owned = OwnedSockAddr;
}

impl Borrow for OwnedSockAddr {
    type Borrowed<'b> = SockAddr<'b>;

    fn borrow(&self) -> Self::Borrowed<'_> {
        match self {
            OwnedSockAddr::Unix(u) => SockAddr::Unix(u),
            OwnedSockAddr::V4(v4) => SockAddr::V4(*v4),
            OwnedSockAddr::V6(v6) => SockAddr::V6(*v6),
            OwnedSockAddr::Other(af, raw) => SockAddr::Other(*af, raw),
        }
    }
}

#[cfg(all(test, feature = "serde"))]
mod tests {
    use crate::types::{OwnedSockAddr, Port, SockAddr};
    use std::net::{Ipv4Addr, Ipv6Addr};
    use std::path::Path;

    #[test]
    fn test_serde_sockaddr_unix() {
        let path = Path::new("/path/to/unix");
        let sockaddr = SockAddr::Unix(path);

        let json = serde_json::to_string(&sockaddr).unwrap();
        assert_eq!(json, r#"{"unix":"/path/to/unix"}"#);
        let sockaddr2: OwnedSockAddr = serde_json::from_str(&json).unwrap();

        let json2 = serde_json::to_string(&sockaddr2).unwrap();
        assert_eq!(json, json2);
    }

    #[test]
    fn test_serde_sockaddr_v4() {
        let sockaddr = SockAddr::V4((Ipv4Addr::LOCALHOST, Port(8080)));

        let json = serde_json::to_string(&sockaddr).unwrap();
        assert_eq!(json, r#"{"v4":["127.0.0.1",8080]}"#);
        let sockaddr2: OwnedSockAddr = serde_json::from_str(&json).unwrap();

        let json2 = serde_json::to_string(&sockaddr2).unwrap();
        assert_eq!(json, json2);
    }

    #[test]
    fn test_serde_sockaddr_v6() {
        let sockaddr = SockAddr::V6((Ipv6Addr::LOCALHOST, Port(8080)));

        let json = serde_json::to_string(&sockaddr).unwrap();
        assert_eq!(json, r#"{"v6":["::1",8080]}"#);
        let sockaddr2: OwnedSockAddr = serde_json::from_str(&json).unwrap();

        let json2 = serde_json::to_string(&sockaddr2).unwrap();
        assert_eq!(json, json2);
    }

    #[test]
    fn test_serde_sockaddr_other() {
        let sockaddr = SockAddr::Other(123, b"foo");

        let json = serde_json::to_string(&sockaddr).unwrap();
        assert_eq!(json, r#"{"other":[123,"foo"]}"#);
        let sockaddr2: OwnedSockAddr = serde_json::from_str(&json).unwrap();

        let json2 = serde_json::to_string(&sockaddr2).unwrap();
        assert_eq!(json, json2);
    }
}