rkyv/
net.rs

1//! Archived versions of network types.
2
3use crate::Archived;
4
5/// An archived [`Ipv4Addr`](std::net::Ipv4Addr).
6#[cfg_attr(feature = "validation", derive(bytecheck::CheckBytes))]
7#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
8#[repr(transparent)]
9pub struct ArchivedIpv4Addr {
10    octets: [Archived<u8>; 4],
11}
12
13impl ArchivedIpv4Addr {
14    /// Returns the four eight-bit integers that make up this address.
15    #[inline]
16    pub const fn octets(&self) -> [u8; 4] {
17        self.octets
18    }
19}
20
21/// An archived [`Ipv6Addr`](std::net::Ipv6Addr).
22#[cfg_attr(feature = "validation", derive(bytecheck::CheckBytes))]
23#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
24#[repr(transparent)]
25pub struct ArchivedIpv6Addr {
26    octets: [Archived<u8>; 16],
27}
28
29impl ArchivedIpv6Addr {
30    /// Returns the eight 16-bit segments that make up this address.
31    #[inline]
32    pub const fn segments(&self) -> [u16; 8] {
33        [
34            u16::from_be_bytes([self.octets[0], self.octets[1]]),
35            u16::from_be_bytes([self.octets[2], self.octets[3]]),
36            u16::from_be_bytes([self.octets[4], self.octets[5]]),
37            u16::from_be_bytes([self.octets[6], self.octets[7]]),
38            u16::from_be_bytes([self.octets[8], self.octets[9]]),
39            u16::from_be_bytes([self.octets[10], self.octets[11]]),
40            u16::from_be_bytes([self.octets[12], self.octets[13]]),
41            u16::from_be_bytes([self.octets[14], self.octets[15]]),
42        ]
43    }
44}
45
46/// An archived [`IpAddr`](std::net::IpAddr).
47#[cfg_attr(feature = "validation", derive(bytecheck::CheckBytes))]
48#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
49#[repr(u8)]
50pub enum ArchivedIpAddr {
51    /// An IPv4 address.
52    V4(ArchivedIpv4Addr),
53    /// An IPv6 address.
54    V6(ArchivedIpv6Addr),
55}
56
57impl ArchivedIpAddr {
58    /// Returns `true` if this address is an [`IPv4` address](std::net::IpAddr::V4), and `false`
59    /// otherwise.
60    #[inline]
61    pub const fn is_ipv4(&self) -> bool {
62        matches!(self, ArchivedIpAddr::V4(_))
63    }
64
65    /// Returns `true` if this address is an [`IPv6` address](std::net::IpAddr::V6), and `false`
66    /// otherwise.
67    #[inline]
68    pub const fn is_ipv6(&self) -> bool {
69        matches!(self, ArchivedIpAddr::V6(_))
70    }
71}
72
73/// An archived [`SocketAddrV4`](std::net::SocketAddrV4).
74#[cfg_attr(feature = "validation", derive(bytecheck::CheckBytes))]
75#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
76#[cfg_attr(feature = "strict", repr(C))]
77pub struct ArchivedSocketAddrV4 {
78    pub(crate) ip: ArchivedIpv4Addr,
79    pub(crate) port: Archived<u16>,
80}
81
82impl ArchivedSocketAddrV4 {
83    /// Returns the IP address associated with this socket address.
84    #[inline]
85    pub const fn ip(&self) -> &ArchivedIpv4Addr {
86        &self.ip
87    }
88
89    /// Returns the port number associated with this socket address.
90    #[inline]
91    pub const fn port(&self) -> u16 {
92        from_archived!(self.port)
93    }
94}
95
96/// An archived [`SocketAddrV6`](std::net::SocketAddrV6).
97#[cfg_attr(feature = "validation", derive(bytecheck::CheckBytes))]
98#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
99#[cfg_attr(feature = "strict", repr(C))]
100pub struct ArchivedSocketAddrV6 {
101    pub(crate) ip: ArchivedIpv6Addr,
102    pub(crate) port: Archived<u16>,
103    pub(crate) flowinfo: Archived<u32>,
104    pub(crate) scope_id: Archived<u32>,
105}
106
107impl ArchivedSocketAddrV6 {
108    /// Returns the flow information associated with this address.
109    ///
110    /// See [`SocketAddrV6::flowinfo()`](std::net::SocketAddrV6::flowinfo()) for more details.
111    #[inline]
112    pub const fn flowinfo(&self) -> u32 {
113        from_archived!(self.flowinfo)
114    }
115
116    /// Returns the IP address associated with this socket address.
117    #[inline]
118    pub const fn ip(&self) -> &ArchivedIpv6Addr {
119        &self.ip
120    }
121
122    /// Returns the port number associated with this socket address.
123    #[inline]
124    pub const fn port(&self) -> u16 {
125        from_archived!(self.port)
126    }
127
128    /// Returns the scope ID associated with this address.
129    ///
130    /// See [`SocketAddrV6::scope_id()`](std::net::SocketAddrV6::scope_id()) for more details.
131    #[inline]
132    pub const fn scope_id(&self) -> u32 {
133        from_archived!(self.scope_id)
134    }
135}
136
137/// An archived [`SocketAddr`](std::net::SocketAddr).
138#[cfg_attr(feature = "validation", derive(bytecheck::CheckBytes))]
139#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
140#[repr(u8)]
141pub enum ArchivedSocketAddr {
142    /// An IPv4 socket address.
143    V4(ArchivedSocketAddrV4),
144    /// An IPv6 socket address.
145    V6(ArchivedSocketAddrV6),
146}
147
148impl ArchivedSocketAddr {
149    /// Returns the port number associated with this socket address.
150    #[inline]
151    pub fn port(&self) -> u16 {
152        match self {
153            ArchivedSocketAddr::V4(addr) => addr.port(),
154            ArchivedSocketAddr::V6(addr) => addr.port(),
155        }
156    }
157
158    /// Returns `true` if the [IP address](std::net::IpAddr) in this `ArchivedSocketAddr` is an
159    /// [`IPv4` address](std::net::IpAddr::V4), and `false` otherwise.
160    #[inline]
161    pub fn is_ipv4(&self) -> bool {
162        matches!(self, ArchivedSocketAddr::V4(_))
163    }
164
165    /// Returns `true` if the [IP address](std::net::IpAddr) in this `ArchivedSocketAddr` is an
166    /// [`IPv6` address](std::net::IpAddr::V6), and `false` otherwise.
167    #[inline]
168    pub fn is_ipv6(&self) -> bool {
169        matches!(self, ArchivedSocketAddr::V6(_))
170    }
171}