[go: up one dir, main page]

rkyv/
net.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
//! Archived versions of network types.

use core::net::{
    IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6,
};

use munge::munge;

use crate::{
    primitive::{ArchivedU16, ArchivedU32},
    Archive, Place, Portable,
};

/// An archived [`Ipv4Addr`].
#[derive(Portable)]
#[rkyv(crate)]
#[cfg_attr(feature = "bytecheck", derive(bytecheck::CheckBytes))]
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[repr(transparent)]
pub struct ArchivedIpv4Addr {
    octets: [u8; 4],
}

impl ArchivedIpv4Addr {
    /// Returns the four eight-bit integers that make up this address.
    #[inline]
    pub const fn octets(&self) -> [u8; 4] {
        self.octets
    }

    /// Returns an [`Ipv4Addr`] with the same value.
    #[inline]
    pub const fn as_ipv4(&self) -> Ipv4Addr {
        let octets = self.octets();
        Ipv4Addr::new(octets[0], octets[1], octets[2], octets[3])
    }

    /// Returns `true` if this is a broadcast address (255.255.255.255).
    ///
    /// See [`Ipv4Addr::is_broadcast`] for more details.
    #[inline]
    pub const fn is_broadcast(&self) -> bool {
        self.as_ipv4().is_broadcast()
    }

    /// Returns `true` if this address is in a range designated for
    /// documentation.
    ///
    /// See [`Ipv4Addr::is_documentation`] for more details.
    #[inline]
    pub const fn is_documentation(&self) -> bool {
        self.as_ipv4().is_documentation()
    }

    /// Returns `true` if the address is link-local (169.254.0.0/16).
    ///
    /// See [`Ipv4Addr::is_link_local`] for more details.
    #[inline]
    pub const fn is_link_local(&self) -> bool {
        self.as_ipv4().is_link_local()
    }

    /// Returns `true` if this is a loopback address (127.0.0.0/8).
    ///
    /// See [`Ipv4Addr::is_loopback`] for more details.
    #[inline]
    pub const fn is_loopback(&self) -> bool {
        self.as_ipv4().is_loopback()
    }

    /// Returns `true` if this is a multicast address (224.0.0.0/4).
    ///
    /// See [`Ipv4Addr::is_multicast`] for more details.
    #[inline]
    pub const fn is_multicast(&self) -> bool {
        self.as_ipv4().is_multicast()
    }

    /// Returns `true` if this is a private address.
    ///
    /// See [`Ipv4Addr::is_private`] for more details.
    #[inline]
    pub const fn is_private(&self) -> bool {
        self.as_ipv4().is_private()
    }

    /// Returns `true` for the special 'unspecified' address (0.0.0.0).
    ///
    /// See [`Ipv4Addr::is_unspecified`] for more details.
    #[inline]
    pub const fn is_unspecified(&self) -> bool {
        self.as_ipv4().is_unspecified()
    }

    /// Converts this address to an IPv4-compatible
    /// [`IPv6` address](std::net::Ipv6Addr).
    ///
    /// See [`Ipv4Addr::to_ipv6_compatible`] for more
    /// details.
    #[inline]
    #[allow(clippy::wrong_self_convention)]
    pub const fn to_ipv6_compatible(&self) -> Ipv6Addr {
        self.as_ipv4().to_ipv6_compatible()
    }

    /// Converts this address to an IPv4-mapped
    /// [`IPv6` address](std::net::Ipv6Addr).
    ///
    /// See [`Ipv4Addr::to_ipv6_mapped`] for more details.
    #[inline]
    #[allow(clippy::wrong_self_convention)]
    pub const fn to_ipv6_mapped(&self) -> Ipv6Addr {
        self.as_ipv4().to_ipv6_mapped()
    }

    /// Emplaces an `ArchivedIpv4Addr` with the given octets into a place.
    #[inline]
    pub fn emplace(octets: [u8; 4], out: Place<Self>) {
        unsafe {
            out.cast_unchecked::<[u8; 4]>().write(octets);
        }
    }
}

/// An archived [`Ipv6Addr`].
#[derive(Portable)]
#[rkyv(crate)]
#[cfg_attr(feature = "bytecheck", derive(bytecheck::CheckBytes))]
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[repr(transparent)]
pub struct ArchivedIpv6Addr {
    octets: [u8; 16],
}

impl ArchivedIpv6Addr {
    /// Returns the eight 16-bit segments that make up this address.
    #[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]]),
        ]
    }

    /// Returns an [`Ipv6Addr`] with the same value.
    #[inline]
    pub const fn as_ipv6(&self) -> Ipv6Addr {
        let segments = self.segments();
        Ipv6Addr::new(
            segments[0],
            segments[1],
            segments[2],
            segments[3],
            segments[4],
            segments[5],
            segments[6],
            segments[7],
        )
    }

    /// Returns `true` if this is a loopback address (::1).
    ///
    /// See [`Ipv6Addr::is_loopback()`](std::net::Ipv6Addr::is_loopback()) for
    /// more details.
    #[inline]
    pub const fn is_loopback(&self) -> bool {
        self.as_ipv6().is_loopback()
    }

    /// Returns `true` if this is a multicast address (ff00::/8).
    ///
    /// See [`Ipv6Addr::is_multicast()`](std::net::Ipv6Addr::is_multicast()) for
    /// more details.
    #[inline]
    pub const fn is_multicast(&self) -> bool {
        self.as_ipv6().is_multicast()
    }

    /// Returns `true` for the special 'unspecified' address (::).
    ///
    /// See [`Ipv6Addr::is_unspecified()`](std::net::Ipv6Addr::is_unspecified())
    /// for more details.
    #[inline]
    pub const fn is_unspecified(&self) -> bool {
        self.as_ipv6().is_unspecified()
    }

    /// Returns the sixteen eight-bit integers the IPv6 address consists of.
    #[inline]
    pub const fn octets(&self) -> [u8; 16] {
        self.as_ipv6().octets()
    }

    /// Converts this address to an [`IPv4` address](std::net::Ipv4Addr).
    /// Returns [`None`] if this address is neither IPv4-compatible or
    /// IPv4-mapped.
    #[inline]
    #[allow(clippy::wrong_self_convention)]
    pub const fn to_ipv4(&self) -> Option<Ipv4Addr> {
        self.as_ipv6().to_ipv4()
    }

    /// Emplaces an `ArchivedIpv6Addr` with the given octets into a place.
    #[inline]
    pub fn emplace(octets: [u8; 16], out: Place<Self>) {
        unsafe {
            out.cast_unchecked::<[u8; 16]>().write(octets);
        }
    }
}

/// An archived [`IpAddr`].
#[derive(Portable)]
#[rkyv(crate)]
#[cfg_attr(feature = "bytecheck", derive(bytecheck::CheckBytes))]
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[repr(u8)]
pub enum ArchivedIpAddr {
    /// An IPv4 address.
    V4(ArchivedIpv4Addr),
    /// An IPv6 address.
    V6(ArchivedIpv6Addr),
}

impl ArchivedIpAddr {
    /// Returns `true` if this address is an [`IPv4`
    /// address](std::net::IpAddr::V4), and `false` otherwise.
    #[inline]
    pub const fn is_ipv4(&self) -> bool {
        matches!(self, ArchivedIpAddr::V4(_))
    }

    /// Returns `true` if this address is an [`IPv6`
    /// address](std::net::IpAddr::V6), and `false` otherwise.
    #[inline]
    pub const fn is_ipv6(&self) -> bool {
        matches!(self, ArchivedIpAddr::V6(_))
    }

    /// Returns an [`IpAddr`] with the same value.
    #[inline]
    pub const fn as_ipaddr(&self) -> IpAddr {
        match self {
            ArchivedIpAddr::V4(ipv4) => IpAddr::V4(ipv4.as_ipv4()),
            ArchivedIpAddr::V6(ipv6) => IpAddr::V6(ipv6.as_ipv6()),
        }
    }

    /// Returns `true` if this is a loopback address.
    ///
    /// See [`IpAddr::is_loopback()`](std::net::IpAddr::is_loopback()) for more
    /// details.
    #[inline]
    pub const fn is_loopback(&self) -> bool {
        match self {
            ArchivedIpAddr::V4(ip) => ip.is_loopback(),
            ArchivedIpAddr::V6(ip) => ip.is_loopback(),
        }
    }

    /// Returns `true` if this is a multicast address.
    ///
    /// See [`IpAddr::is_multicast()`](std::net::IpAddr::is_multicast()) for
    /// more details.
    #[inline]
    pub const fn is_multicast(&self) -> bool {
        match self {
            ArchivedIpAddr::V4(ip) => ip.is_multicast(),
            ArchivedIpAddr::V6(ip) => ip.is_multicast(),
        }
    }

    /// Returns `true` for the special 'unspecified' address.
    ///
    /// See [`IpAddr::is_unspecified()`](std::net::IpAddr::is_unspecified()) for
    /// more details.
    #[inline]
    pub const fn is_unspecified(&self) -> bool {
        match self {
            ArchivedIpAddr::V4(ip) => ip.is_unspecified(),
            ArchivedIpAddr::V6(ip) => ip.is_unspecified(),
        }
    }
}

/// An archived [`SocketAddrV4`].
#[derive(
    Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, Portable, PartialOrd,
)]
#[cfg_attr(feature = "bytecheck", derive(bytecheck::CheckBytes))]
#[rkyv(crate)]
#[repr(C)]
pub struct ArchivedSocketAddrV4 {
    ip: ArchivedIpv4Addr,
    port: ArchivedU16,
}

impl ArchivedSocketAddrV4 {
    /// Returns the IP address associated with this socket address.
    #[inline]
    pub const fn ip(&self) -> &ArchivedIpv4Addr {
        &self.ip
    }

    /// Returns the port number associated with this socket address.
    #[inline]
    pub const fn port(&self) -> u16 {
        self.port.to_native()
    }

    /// Returns a [`SocketAddrV4`] with the same value.
    #[inline]
    pub fn as_socket_addr_v4(&self) -> SocketAddrV4 {
        SocketAddrV4::new(self.ip().as_ipv4(), self.port())
    }

    /// Emplaces an `ArchivedSocketAddrV4` of the given `value` into a place.
    #[inline]
    pub fn emplace(value: &SocketAddrV4, out: Place<Self>) {
        munge!(let ArchivedSocketAddrV4 { ip, port } = out);
        value.ip().resolve((), ip);
        value.port().resolve((), port);
    }
}

/// An archived [`SocketAddrV6`].
#[derive(
    Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, Portable, PartialOrd,
)]
#[cfg_attr(feature = "bytecheck", derive(bytecheck::CheckBytes))]
#[rkyv(crate)]
#[repr(C)]
pub struct ArchivedSocketAddrV6 {
    ip: ArchivedIpv6Addr,
    port: ArchivedU16,
    flowinfo: ArchivedU32,
    scope_id: ArchivedU32,
}

impl ArchivedSocketAddrV6 {
    /// Returns the flow information associated with this address.
    ///
    /// See [`SocketAddrV6::flowinfo()`](std::net::SocketAddrV6::flowinfo()) for
    /// more details.
    #[inline]
    pub const fn flowinfo(&self) -> u32 {
        self.flowinfo.to_native()
    }

    /// Returns the IP address associated with this socket address.
    #[inline]
    pub const fn ip(&self) -> &ArchivedIpv6Addr {
        &self.ip
    }

    /// Returns the port number associated with this socket address.
    #[inline]
    pub const fn port(&self) -> u16 {
        self.port.to_native()
    }

    /// Returns the scope ID associated with this address.
    ///
    /// See [`SocketAddrV6::scope_id()`](std::net::SocketAddrV6::scope_id()) for
    /// more details.
    #[inline]
    pub const fn scope_id(&self) -> u32 {
        self.scope_id.to_native()
    }

    /// Returns a [`SocketAddrV6`] with the same value.
    #[inline]
    pub fn as_socket_addr_v6(&self) -> SocketAddrV6 {
        SocketAddrV6::new(
            self.ip().as_ipv6(),
            self.port(),
            self.flowinfo(),
            self.scope_id(),
        )
    }

    /// Emplaces an `ArchivedSocketAddrV6` of the given `value` into a place.
    #[inline]
    pub fn emplace(value: &SocketAddrV6, out: Place<Self>) {
        munge!(let ArchivedSocketAddrV6 { ip, port, flowinfo, scope_id } = out);
        value.ip().resolve((), ip);
        value.port().resolve((), port);
        value.flowinfo().resolve((), flowinfo);
        value.scope_id().resolve((), scope_id);
    }
}

/// An archived [`SocketAddr`].
#[derive(Portable)]
#[rkyv(crate)]
#[cfg_attr(feature = "bytecheck", derive(bytecheck::CheckBytes))]
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[repr(u8)]
pub enum ArchivedSocketAddr {
    /// An IPv4 socket address.
    V4(ArchivedSocketAddrV4),
    /// An IPv6 socket address.
    V6(ArchivedSocketAddrV6),
}

impl ArchivedSocketAddr {
    /// Returns the port number associated with this socket address.
    #[inline]
    pub fn port(&self) -> u16 {
        match self {
            ArchivedSocketAddr::V4(addr) => addr.port(),
            ArchivedSocketAddr::V6(addr) => addr.port(),
        }
    }

    /// Returns `true` if the [IP address](std::net::IpAddr) in this
    /// `ArchivedSocketAddr` is an [`IPv4` address](std::net::IpAddr::V4),
    /// and `false` otherwise.
    #[inline]
    pub fn is_ipv4(&self) -> bool {
        matches!(self, ArchivedSocketAddr::V4(_))
    }

    /// Returns `true` if the [IP address](std::net::IpAddr) in this
    /// `ArchivedSocketAddr` is an [`IPv6` address](std::net::IpAddr::V6),
    /// and `false` otherwise.
    #[inline]
    pub fn is_ipv6(&self) -> bool {
        matches!(self, ArchivedSocketAddr::V6(_))
    }

    /// Returns a [`SocketAddr`] with the same value.
    #[inline]
    pub fn as_socket_addr(&self) -> SocketAddr {
        match self {
            ArchivedSocketAddr::V4(addr) => {
                SocketAddr::V4(addr.as_socket_addr_v4())
            }
            ArchivedSocketAddr::V6(addr) => {
                SocketAddr::V6(addr.as_socket_addr_v6())
            }
        }
    }

    /// Returns the IP address associated with this socket address.
    #[inline]
    pub fn ip(&self) -> IpAddr {
        match self {
            ArchivedSocketAddr::V4(addr) => IpAddr::V4(addr.ip().as_ipv4()),
            ArchivedSocketAddr::V6(addr) => IpAddr::V6(addr.ip().as_ipv6()),
        }
    }
}