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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#![doc = include_str!("../README.md")]
#![no_std]
#![forbid(unsafe_code)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(docsrs, allow(unused_attributes))]
#![deny(missing_docs)]
#[cfg(test)]
extern crate std;
use core::net::{IpAddr, Ipv4Addr, Ipv6Addr};
pub use ipnet::{IpNet, Ipv4Net, Ipv6Net};
pub use forwarding_black_list::{FORWARDING_BLACKLIST, FORWARDING_BLACKLIST_ID};
mod forwarding_black_list;
macro_rules! rfcs {
($(($index:literal, $id:literal)), +$(,)?) => {
paste::paste! {
$(
pub use [<rfc $id>]::[< RFC $id >];
)+
$(
mod [<rfc $id>];
)+
bitflags::bitflags! {
/// Address-related RFC flags
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
pub struct Filter: u128 {
$(
#[doc = "[RFC " $id "](https://tools.ietf.org/html/rfc" $id ")"]
const [<RFC $id>] = 1 << $index;
)+
/// Addresses that should not be forwarded
const FORWARDING_BLACKLIST = 1 << 127;
}
}
const RFCS: &[RFC] = &[
$([<RFC $id>]),+,
FORWARDING_BLACKLIST
];
impl ::core::ops::Index<u32> for RFCs {
type Output = RFC;
fn index(&self, index: u32) -> &Self::Output {
match index {
$(
$id => &RFCS[$index],
)+
val if val == FORWARDING_BLACKLIST_ID => RFCS.last().unwrap(),
val => panic!("{val} is not a valid RFC identifier"),
}
}
}
impl ::core::ops::Index<&str> for RFCs {
type Output = RFC;
fn index(&self, index: &str) -> &Self::Output {
let s = if index.starts_with("RFC") {
index.trim_start_matches("RFC").trim()
} else if index.starts_with("rfc") {
index.trim_start_matches("rfc").trim()
} else {
index
};
match s {
$(
stringify!($id) => &RFCS[$index],
)+
"blacklist" | "BLACKLIST" | "blocked" | "BLOCKED" => RFCS.last().unwrap(),
val if val == stringify!(FORWARDING_BLACKLIST_ID) => RFCS.last().unwrap(),
val => panic!("{val} is not a valid RFC identifier"),
}
}
}
impl RFCs {
/// Returns the number of known RFCs
///
/// ## Example
///
/// ```rust
/// use iprfc::RFCs;
///
/// assert_eq!(29, RFCs::len());
/// ```
#[inline]
pub const fn len() -> usize {
RFCS.len()
}
/// Returns `true` if such id is a valid RFC identifier
///
/// ## Example
///
/// ```rust
/// use iprfc::{RFCs, FORWARDING_BLACKLIST_ID};
///
/// assert!(RFCs::contains(6890));
/// assert!(RFCs::contains(FORWARDING_BLACKLIST_ID));
/// assert!(!RFCs::contains(9999));
/// ```
#[inline]
pub const fn contains(id: u32) -> bool {
match id {
$(
$id => true,
)+
FORWARDING_BLACKLIST_ID => true,
_ => false,
}
}
/// Returns the RFC with the given id, if it exists
///
/// ## Example
///
/// ```rust
/// use iprfc::RFCs;
///
/// let rfc = RFCs::get(6890);
/// assert!(rfc.is_some());
///
/// let rfc = RFCs::get(9999);
/// assert!(rfc.is_none());
/// ```
#[inline]
pub const fn get(id: u32) -> Option<&'static RFC> {
match id {
$(
$id => Some(&[<RFC $id>]),
)+
FORWARDING_BLACKLIST_ID => Some(&FORWARDING_BLACKLIST),
_ => None,
}
}
/// Returns the RFC with the given id, if it exists, otherwise panics
///
/// ## Example
///
/// ```rust
/// use iprfc::RFCs;
///
/// let rfc = RFCs::get_unchecked(6890);
/// ```
#[inline]
pub const fn get_unchecked(id: u32) -> &'static RFC {
match id {
$(
$id => &[<RFC $id>],
)+
FORWARDING_BLACKLIST_ID => &FORWARDING_BLACKLIST,
_ => panic!("not a valid RFC identifier"),
}
}
}
}
};
}
rfcs! {
(0, 919), (1, 1112), (2, 1122), (3, 1918), (4, 2544), (5, 2765), (6, 2928), (7, 3056), (8, 3068), (9, 3171), (10, 3330), (11, 3849), (12, 3927), (13, 4038), (14, 4193), (15, 4291), (16, 4380), (17, 4773), (18, 4843), (19, 5180), (20, 5735), (21, 5737), (22, 6052), (23, 6333), (24, 6598), (25, 6666), (26, 6890), (27, 7335),
}
/// All known RFCs
///
/// Useful resources:
///
/// * https://www.iana.org/assignments/ipv6-address-space/ipv6-address-space.xhtml
/// * https://www.iana.org/assignments/ipv6-unicast-address-assignments/ipv6-unicast-address-assignments.xhtml
/// * https://www.iana.org/assignments/ipv6-address-space/ipv6-address-space.xhtml
#[allow(rustdoc::bare_urls)]
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Hash)]
pub struct RFCs;
impl RFCs {
/// Iterate over all known RFCs
///
/// ## Example
///
/// ```rust
/// use iprfc::RFCs;
///
/// for rfc in RFCs::iter() {
/// println!("{}", rfc.id());
/// }
/// ```
#[inline]
pub fn iter<'a>() -> core::slice::Iter<'a, RFC> {
RFCS.iter()
}
/// Returns a subset of [`RFCs`] by filtering with the given [`Filter`]
///
/// ## Example
///
/// ```rust
/// use iprfc::{RFCs, Filter};
///
/// let subset = RFCs::filter(Filter::RFC919 | Filter::RFC1122);
/// ```
#[inline]
pub const fn filter(filter: Filter) -> Subset {
Subset(filter)
}
}
/// A subset of [`RFCs`], useful for filtering
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct Subset(Filter);
impl Subset {
/// Returns `true` if the subset contains the `T`
///
/// ## Example
///
/// ```rust
/// use iprfc::{RFCs, Filter};
/// use std::net::{Ipv4Addr, Ipv6Addr};
///
/// let subset = RFCs::filter(Filter::RFC919 | Filter::RFC1122);
/// assert!(subset.contains(&Ipv4Addr::BROADCAST));
/// assert!(!subset.contains(&Ipv6Addr::LOCALHOST));
/// ```
pub fn contains<T>(&self, ip: &T) -> bool
where
RFC: Contains<T>,
{
self.0.iter_names().any(|(n, _)| RFCs[n].contains(ip))
}
}
/// RFC
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct RFC {
id: u32,
ip_nets: &'static [IpNet],
ipv4_nets: &'static [Ipv4Net],
ipv6_nets: &'static [Ipv6Net],
}
impl RFC {
/// Get the identifier of the RFC
///
/// ## Example
///
/// ```rust
/// use iprfc::RFC6890;
///
/// assert_eq!(6890, RFC6890.id());
/// ```
#[inline]
pub const fn id(&self) -> u32 {
self.id
}
/// Get all of the IP addresses of the RFC
///
/// ## Example
///
/// ```rust
/// use iprfc::RFC6890;
///
/// for ip in RFC6890.ip_nets() {
/// println!("{}", ip);
/// }
/// ```
#[inline]
pub const fn ip_nets(&self) -> &'static [IpNet] {
self.ip_nets
}
/// Get all of the IPv4 addresses of the RFC
///
/// ## Example
///
/// ```rust
/// use iprfc::RFC6890;
///
/// for ip in RFC6890.ipv4_nets() {
/// println!("{}", ip);
/// }
/// ```
#[inline]
pub const fn ipv4_nets(&self) -> &'static [Ipv4Net] {
self.ipv4_nets
}
/// Get all of the IPv6 addresses of the RFC
///
/// ## Example
///
/// ```rust
/// use iprfc::RFC6890;
///
/// for ip in RFC6890.ipv6_nets() {
/// println!("{}", ip);
/// }
#[inline]
pub const fn ipv6_nets(&self) -> &'static [Ipv6Net] {
self.ipv6_nets
}
/// Returns `true` if the ip is contained by the [`RFC`].
///
/// ## Example
///
/// ```rust
/// use iprfc::{RFC6890, IpNet, Ipv4Net, Ipv6Net};
/// use std::net::{Ipv4Addr, Ipv6Addr, IpAddr};
/// use std::str::FromStr;
///
/// // IPv4 address examples
/// let private_ip = Ipv4Addr::new(192, 168, 1, 1);
/// assert!(RFC6890.contains(&private_ip));
///
/// let loopback_ip = Ipv4Addr::new(127, 0, 0, 1);
/// assert!(RFC6890.contains(&loopback_ip));
///
/// let public_ip = Ipv4Addr::new(8, 8, 8, 8);
/// assert!(!RFC6890.contains(&public_ip));
///
/// let testnet_ip = Ipv4Addr::new(192, 0, 2, 1);
/// assert!(RFC6890.contains(&testnet_ip));
///
/// // IPv4 network examples
/// let private_net = Ipv4Net::from_str("192.168.0.0/24").unwrap();
/// assert!(RFC6890.contains(&private_net));
///
/// let public_net = Ipv4Net::from_str("8.8.8.0/24").unwrap();
/// assert!(!RFC6890.contains(&public_net));
///
/// let testnet_net = Ipv4Net::from_str("192.0.2.0/28").unwrap();
/// assert!(RFC6890.contains(&testnet_net));
///
/// // IPv6 address examples
/// let ipv6_loopback = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1);
/// assert!(RFC6890.contains(&ipv6_loopback));
///
/// let ipv6_doc = Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1);
/// assert!(RFC6890.contains(&ipv6_doc));
///
/// let ipv6_linklocal = Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 1);
/// assert!(RFC6890.contains(&ipv6_linklocal));
///
/// // IPv6 network examples
/// let ipv6_doc_net = Ipv6Net::from_str("2001:db8::/48").unwrap();
/// assert!(RFC6890.contains(&ipv6_doc_net));
///
/// let ipv6_ula_net = Ipv6Net::from_str("fc00::/7").unwrap();
/// assert!(RFC6890.contains(&ipv6_ula_net));
///
/// let ipv6_global_net = Ipv6Net::from_str("2a00::/32").unwrap();
/// assert!(!RFC6890.contains(&ipv6_global_net));
///
/// // Works with IpNet enum too
/// let ip_net: IpNet = private_net.into();
/// assert!(RFC6890.contains(&ip_net));
///
/// // Works with IpAddr enum too
/// let ip: IpAddr = private_ip.into();
/// assert!(RFC6890.contains(&ip));
/// ```
#[inline]
pub fn contains<T>(&self, ip: &T) -> bool
where
Self: Contains<T>,
{
Contains::contains(self, ip)
}
}
/// Returns `true` if the [`RFC`] contains `T`.
pub trait Contains<T>: sealed::Sealed {
/// Returns `true` if the [`RFC`] contains `T`.
fn contains(&self, t: &T) -> bool;
}
impl Contains<IpNet> for RFC {
#[inline]
fn contains(&self, ip: &IpNet) -> bool {
self.ip_nets.iter().any(|&i| i.contains(ip))
}
}
impl Contains<IpAddr> for RFC {
#[inline]
fn contains(&self, ip: &IpAddr) -> bool {
self.ip_nets.iter().any(|&i| i.contains(ip))
}
}
impl Contains<Ipv4Net> for RFC {
#[inline]
fn contains(&self, ip: &Ipv4Net) -> bool {
self.ipv4_nets.iter().any(|&i| i.contains(ip))
}
}
impl Contains<Ipv4Addr> for RFC {
#[inline]
fn contains(&self, ip: &Ipv4Addr) -> bool {
self.ipv4_nets.iter().any(|&i| i.contains(ip))
}
}
impl Contains<Ipv6Net> for RFC {
#[inline]
fn contains(&self, ip: &Ipv6Net) -> bool {
self.ipv6_nets.iter().any(|&i| i.contains(ip))
}
}
impl Contains<Ipv6Addr> for RFC {
#[inline]
fn contains(&self, ip: &Ipv6Addr) -> bool {
self.ipv6_nets.iter().any(|&i| i.contains(ip))
}
}
mod sealed {
pub trait Sealed {}
impl Sealed for super::RFC {}
}
#[test]
fn test_indexable_by_id() {
let rfc = RFCs[6890];
assert_eq!(6890, rfc.id());
let rfc = RFCs[FORWARDING_BLACKLIST_ID];
assert_eq!(FORWARDING_BLACKLIST_ID, rfc.id());
}
#[test]
#[should_panic]
fn test_indexable_by_id_panic() {
let _ = RFCs[9999];
}
#[test]
fn test_get() {
let rfc = RFCs::get(FORWARDING_BLACKLIST_ID).unwrap();
assert_eq!(FORWARDING_BLACKLIST_ID, rfc.id());
let rfc = RFCs::get(7335).unwrap();
assert_eq!(7335, rfc.id());
}
#[test]
fn test_get_unchecked() {
let rfc = RFCs::get_unchecked(FORWARDING_BLACKLIST_ID);
assert_eq!(FORWARDING_BLACKLIST_ID, rfc.id());
let rfc = RFCs::get_unchecked(7335);
assert_eq!(7335, rfc.id());
}
#[test]
#[should_panic]
fn test_get_unchecked_panic() {
let _ = RFCs::get_unchecked(9999);
}
#[test]
fn test_indexable_by_str() {
let rfc = RFCs["6890"];
assert_eq!(6890, rfc.id());
let rfc = RFCs["RFC6890"];
assert_eq!(6890, rfc.id());
let rfc = RFCs["rfc 6890"];
assert_eq!(6890, rfc.id());
let rfc = RFCs["blacklist"];
assert_eq!(rfc.id(), FORWARDING_BLACKLIST_ID);
}
#[test]
#[should_panic]
fn test_indexable_by_str_panic() {
let _ = RFCs["9999"];
}