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
use crate::Archived;
#[cfg_attr(feature = "validation", derive(bytecheck::CheckBytes))]
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[repr(transparent)]
pub struct ArchivedIpv4Addr {
octets: [Archived<u8>; 4],
}
impl ArchivedIpv4Addr {
#[inline]
pub const fn octets(&self) -> [u8; 4] {
self.octets
}
}
#[cfg_attr(feature = "validation", derive(bytecheck::CheckBytes))]
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[repr(transparent)]
pub struct ArchivedIpv6Addr {
octets: [Archived<u8>; 16],
}
impl ArchivedIpv6Addr {
#[inline]
pub const fn segments(&self) -> [u16; 8] {
[
u16::from_be_bytes([self.octets[0], self.octets[1]]),
u16::from_be_bytes([self.octets[2], self.octets[3]]),
u16::from_be_bytes([self.octets[4], self.octets[5]]),
u16::from_be_bytes([self.octets[6], self.octets[7]]),
u16::from_be_bytes([self.octets[8], self.octets[9]]),
u16::from_be_bytes([self.octets[10], self.octets[11]]),
u16::from_be_bytes([self.octets[12], self.octets[13]]),
u16::from_be_bytes([self.octets[14], self.octets[15]]),
]
}
}
#[cfg_attr(feature = "validation", derive(bytecheck::CheckBytes))]
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[repr(u8)]
pub enum ArchivedIpAddr {
V4(ArchivedIpv4Addr),
V6(ArchivedIpv6Addr),
}
impl ArchivedIpAddr {
#[inline]
pub const fn is_ipv4(&self) -> bool {
matches!(self, ArchivedIpAddr::V4(_))
}
#[inline]
pub const fn is_ipv6(&self) -> bool {
matches!(self, ArchivedIpAddr::V6(_))
}
}
#[cfg_attr(feature = "validation", derive(bytecheck::CheckBytes))]
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "strict", repr(C))]
pub struct ArchivedSocketAddrV4 {
pub(crate) ip: ArchivedIpv4Addr,
pub(crate) port: Archived<u16>,
}
impl ArchivedSocketAddrV4 {
#[inline]
pub const fn ip(&self) -> &ArchivedIpv4Addr {
&self.ip
}
#[inline]
pub const fn port(&self) -> u16 {
from_archived!(self.port)
}
}
#[cfg_attr(feature = "validation", derive(bytecheck::CheckBytes))]
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "strict", repr(C))]
pub struct ArchivedSocketAddrV6 {
pub(crate) ip: ArchivedIpv6Addr,
pub(crate) port: Archived<u16>,
pub(crate) flowinfo: Archived<u32>,
pub(crate) scope_id: Archived<u32>,
}
impl ArchivedSocketAddrV6 {
#[inline]
pub const fn flowinfo(&self) -> u32 {
from_archived!(self.flowinfo)
}
#[inline]
pub const fn ip(&self) -> &ArchivedIpv6Addr {
&self.ip
}
#[inline]
pub const fn port(&self) -> u16 {
from_archived!(self.port)
}
#[inline]
pub const fn scope_id(&self) -> u32 {
from_archived!(self.scope_id)
}
}
#[cfg_attr(feature = "validation", derive(bytecheck::CheckBytes))]
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[repr(u8)]
pub enum ArchivedSocketAddr {
V4(ArchivedSocketAddrV4),
V6(ArchivedSocketAddrV6),
}
impl ArchivedSocketAddr {
#[inline]
pub fn port(&self) -> u16 {
match self {
ArchivedSocketAddr::V4(addr) => addr.port(),
ArchivedSocketAddr::V6(addr) => addr.port(),
}
}
#[inline]
pub fn is_ipv4(&self) -> bool {
matches!(self, ArchivedSocketAddr::V4(_))
}
#[inline]
pub fn is_ipv6(&self) -> bool {
matches!(self, ArchivedSocketAddr::V6(_))
}
}