cidr/
internal_traits.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
use std::net::{
	IpAddr,
	Ipv4Addr,
	Ipv6Addr,
};

use crate::num::NumberOfAddresses;

/// Implemented for IPv4Addr, IPv6Addr AND IpAddr
pub trait PrivUnspecAddress: Sized {
	type _Tools;
}

/// seal `Cidr` trait
pub trait PrivCidr {}

/// seal `Inet` trait
pub trait PrivInet {}

/// seal `InetPair` trait
pub trait PrivInetPair {
	fn _covered_addresses(&self) -> NumberOfAddresses;
	fn _inc_first(&mut self) -> bool;
	fn _dec_second(&mut self) -> bool;
}

#[derive(Clone, Copy)]
struct Ipv4OverflowingOp {
	address: u32,
	net_mask: u32,
	host_mask: u32,
}

impl Ipv4OverflowingOp {
	const fn new(address: Ipv4Addr, prefix_len: u8) -> Self {
		let host_mask = Ipv4AddrTools::native_host_mask(prefix_len);
		let net_mask = !host_mask;
		let address: u32 = Ipv4AddrTools::to_native(address);
		Self {
			address,
			net_mask,
			host_mask,
		}
	}

	const fn handle_result(self, (mut res, mut overflow): (u32, bool)) -> (Ipv4Addr, bool) {
		let net_address = self.address & self.net_mask;
		if res & self.net_mask != net_address {
			// replace network in result with original network
			res = (res & self.host_mask) | net_address;
			overflow = true
		}
		(Ipv4AddrTools::from_native(res), overflow)
	}
}

pub struct Ipv4AddrTools(());

impl Ipv4AddrTools {
	pub(crate) const fn to_native(ip: Ipv4Addr) -> u32 {
		// const: u32::from
		u32::from_be_bytes(ip.octets())
	}

	pub(crate) const fn from_native(ip: u32) -> Ipv4Addr {
		// const: Ipv4Addr::from
		let ip = ip.to_be_bytes();
		Ipv4Addr::new(ip[0], ip[1], ip[2], ip[3])
	}

	pub(crate) const fn native_host_mask(prefix_len: u8) -> u32 {
		// const: unwrap_or(0)
		if let Some(mask) = (!0u32).checked_shr(prefix_len as u32) {
			mask
		} else {
			0
		}
	}

	pub(crate) const fn _has_zero_host_part(address: Ipv4Addr, prefix_len: u8) -> bool {
		let host_mask: u32 = Self::native_host_mask(prefix_len);
		let addr_num = Self::to_native(address);
		(addr_num & host_mask) == 0
	}

	pub(crate) const fn _overflowing_next(address: Ipv4Addr, prefix_len: u8) -> (Ipv4Addr, bool) {
		let op = Ipv4OverflowingOp::new(address, prefix_len);
		op.handle_result(op.address.overflowing_add(1))
	}

	pub(crate) const fn _overflowing_inc_u32(
		address: Ipv4Addr,
		prefix_len: u8,
		step_u32: u32,
	) -> (Ipv4Addr, bool) {
		let op = Ipv4OverflowingOp::new(address, prefix_len);
		let (res, overflow) = op.handle_result(op.address.overflowing_add(step_u32));
		(res, overflow)
	}

	pub(crate) const fn _overflowing_inc(
		address: Ipv4Addr,
		prefix_len: u8,
		step: u128,
	) -> (Ipv4Addr, bool) {
		let step_u32 = step as u32;
		let step_overflow = step_u32 as u128 != step;
		let (res, overflow) = Self::_overflowing_inc_u32(address, prefix_len, step_u32);
		(res, overflow || step_overflow)
	}

	pub(crate) const fn _overflowing_prev(address: Ipv4Addr, prefix_len: u8) -> (Ipv4Addr, bool) {
		let op = Ipv4OverflowingOp::new(address, prefix_len);
		op.handle_result(op.address.overflowing_sub(1))
	}

	pub(crate) const fn _overflowing_dec_u32(
		address: Ipv4Addr,
		prefix_len: u8,
		step_u32: u32,
	) -> (Ipv4Addr, bool) {
		let op = Ipv4OverflowingOp::new(address, prefix_len);
		let (res, overflow) = op.handle_result(op.address.overflowing_sub(step_u32));
		(res, overflow)
	}

	pub(crate) const fn _overflowing_dec(
		address: Ipv4Addr,
		prefix_len: u8,
		step: u128,
	) -> (Ipv4Addr, bool) {
		let step_u32 = step as u32;
		let step_overflow = step_u32 as u128 != step;
		let (res, overflow) = Self::_overflowing_dec_u32(address, prefix_len, step_u32);
		(res, overflow || step_overflow)
	}

	/*
		pub(crate) const fn _overflowing_sub(address: Ipv4Addr, other: Ipv4Addr) -> (u128, bool) {
			let (res, overflow) = Self::to_native(address).overflowing_sub(Self::to_native(other));
			(res as u128, overflow)
		}
	*/

	pub(crate) const fn _prefix_match(address: Ipv4Addr, other: Ipv4Addr, prefix_len: u8) -> bool {
		let net_mask: u32 = !Self::native_host_mask(prefix_len);
		(Self::to_native(address) & net_mask) == (Self::to_native(other) & net_mask)
	}

	pub(crate) const fn _network_address(address: Ipv4Addr, prefix_len: u8) -> Ipv4Addr {
		let net_mask: u32 = !Self::native_host_mask(prefix_len);
		Self::from_native(Self::to_native(address) & net_mask)
	}

	pub(crate) const fn _last_address(address: Ipv4Addr, prefix_len: u8) -> Ipv4Addr {
		let host_mask: u32 = Self::native_host_mask(prefix_len);
		Self::from_native(Self::to_native(address) | host_mask)
	}

	pub(crate) const fn _network_mask(prefix_len: u8) -> Ipv4Addr {
		Self::from_native(!Self::native_host_mask(prefix_len))
	}
}

impl PrivUnspecAddress for Ipv4Addr {
	type _Tools = Ipv4AddrTools;
}

#[derive(Clone, Copy)]
struct Ipv6OverflowingOp {
	address: u128,
	net_mask: u128,
	host_mask: u128,
}

impl Ipv6OverflowingOp {
	const fn new(address: Ipv6Addr, prefix_len: u8) -> Self {
		let host_mask = Ipv6AddrTools::native_host_mask(prefix_len);
		let net_mask = !host_mask;
		let address: u128 = u128::from_be_bytes(address.octets());
		Self {
			address,
			net_mask,
			host_mask,
		}
	}

	const fn handle_result(self, (mut res, mut overflow): (u128, bool)) -> (Ipv6Addr, bool) {
		let net_address = self.address & self.net_mask;
		if res & self.net_mask != net_address {
			// replace network in result with original network
			res = (res & self.host_mask) | net_address;
			overflow = true
		}
		(Ipv6AddrTools::from_native(res), overflow)
	}
}

pub struct Ipv6AddrTools(());

impl Ipv6AddrTools {
	pub(crate) const fn to_native(ip: Ipv6Addr) -> u128 {
		// const: u128::from
		u128::from_be_bytes(ip.octets())
	}

	pub(crate) const fn from_native(ip: u128) -> Ipv6Addr {
		// const: Ipv6Addr::from
		let ip = ip.to_be_bytes();
		#[cfg(not(feature = "no_unsafe"))]
		{
			// Ipv6Addr is a newtype for the octets, it just doesn't have a constructor
			// for it apart from Ipv6Addr::from, which isn't const
			//
			// this should be "safe":
			// * if Ipv6Addr has a different size than [u8; 16] transmute won't compile
			// * if the size matches, Ipv6Addr can't store any other data - it must store exactly the octets
			// * it is unlikely std would ever store this in another endianness
			unsafe { std::mem::transmute(ip) }
		}
		#[cfg(feature = "no_unsafe")]
		{
			// safe variant, but "slow"
			Ipv6Addr::new(
				u16::from_be_bytes([ip[0], ip[1]]),
				u16::from_be_bytes([ip[2], ip[3]]),
				u16::from_be_bytes([ip[4], ip[5]]),
				u16::from_be_bytes([ip[6], ip[7]]),
				u16::from_be_bytes([ip[8], ip[9]]),
				u16::from_be_bytes([ip[10], ip[11]]),
				u16::from_be_bytes([ip[12], ip[13]]),
				u16::from_be_bytes([ip[14], ip[15]]),
			)
		}
	}

	pub(crate) const fn native_host_mask(prefix_len: u8) -> u128 {
		// const: unwrap_or(0)
		if let Some(mask) = (!0u128).checked_shr(prefix_len as u32) {
			mask
		} else {
			0
		}
	}

	pub(crate) const fn _has_zero_host_part(address: Ipv6Addr, prefix_len: u8) -> bool {
		let host_mask: u128 = Self::native_host_mask(prefix_len);
		let addr_num = Self::to_native(address);
		(addr_num & host_mask) == 0
	}

	pub(crate) const fn _overflowing_next(address: Ipv6Addr, prefix_len: u8) -> (Ipv6Addr, bool) {
		let op = Ipv6OverflowingOp::new(address, prefix_len);
		op.handle_result(op.address.overflowing_add(1))
	}

	pub(crate) const fn _overflowing_inc(
		address: Ipv6Addr,
		prefix_len: u8,
		step: u128,
	) -> (Ipv6Addr, bool) {
		let op = Ipv6OverflowingOp::new(address, prefix_len);
		op.handle_result(op.address.overflowing_add(step))
	}

	pub(crate) const fn _overflowing_prev(address: Ipv6Addr, prefix_len: u8) -> (Ipv6Addr, bool) {
		let op = Ipv6OverflowingOp::new(address, prefix_len);
		op.handle_result(op.address.overflowing_sub(1))
	}

	pub(crate) const fn _overflowing_dec(
		address: Ipv6Addr,
		prefix_len: u8,
		step: u128,
	) -> (Ipv6Addr, bool) {
		let op = Ipv6OverflowingOp::new(address, prefix_len);
		op.handle_result(op.address.overflowing_sub(step))
	}

	/*
		pub(crate) const fn _overflowing_sub(address: Ipv6Addr, other: Ipv6Addr) -> (u128, bool) {
			Self::to_native(address).overflowing_sub(Self::to_native(other))
		}
	*/

	pub(crate) const fn _prefix_match(address: Ipv6Addr, other: Ipv6Addr, prefix_len: u8) -> bool {
		let net_mask: u128 = !Self::native_host_mask(prefix_len);
		(Self::to_native(address) & net_mask) == (Self::to_native(other) & net_mask)
	}

	pub(crate) const fn _network_address(address: Ipv6Addr, prefix_len: u8) -> Ipv6Addr {
		let net_mask: u128 = !Self::native_host_mask(prefix_len);
		Self::from_native(Self::to_native(address) & net_mask)
	}

	pub(crate) const fn _last_address(address: Ipv6Addr, prefix_len: u8) -> Ipv6Addr {
		let host_mask: u128 = Self::native_host_mask(prefix_len);
		Self::from_native(Self::to_native(address) | host_mask)
	}

	pub(crate) const fn _network_mask(prefix_len: u8) -> Ipv6Addr {
		Self::from_native(!Self::native_host_mask(prefix_len))
	}
}

impl PrivUnspecAddress for Ipv6Addr {
	type _Tools = Ipv6AddrTools;
}

pub struct IpAddrTools(());

// we currently don't use the tools on IpAddr and related enums
impl IpAddrTools {
	/*
		pub(crate) const fn _has_zero_host_part(address: IpAddr, prefix_len: u8) -> bool {
			match address {
				IpAddr::V4(a) => Ipv4AddrTools::_has_zero_host_part(a, prefix_len),
				IpAddr::V6(a) => Ipv6AddrTools::_has_zero_host_part(a, prefix_len),
			}
		}

		pub(crate) const fn _overflowing_next(address: IpAddr, prefix_len: u8) -> (IpAddr, bool) {
			match address {
				IpAddr::V4(a) => {
					let (res, of) = Ipv4AddrTools::_overflowing_next(a, prefix_len);
					(IpAddr::V4(res), of)
				},
				IpAddr::V6(a) => {
					let (res, of) = Ipv6AddrTools::_overflowing_next(a, prefix_len);
					(IpAddr::V6(res), of)
				},
			}
		}

		pub(crate) const fn _overflowing_inc(address: IpAddr, prefix_len: u8, step: u128) -> (IpAddr, bool) {
			match address {
				IpAddr::V4(a) => {
					let (res, of) = Ipv4AddrTools::_overflowing_inc(a, prefix_len, step);
					(IpAddr::V4(res), of)
				},
				IpAddr::V6(a) => {
					let (res, of) = Ipv6AddrTools::_overflowing_inc(a, prefix_len, step);
					(IpAddr::V6(res), of)
				},
			}
		}

		pub(crate) const fn _overflowing_prev(address: IpAddr, prefix_len: u8) -> (IpAddr, bool) {
			match address {
				IpAddr::V4(a) => {
					let (res, of) = Ipv4AddrTools::_overflowing_prev(a, prefix_len);
					(IpAddr::V4(res), of)
				},
				IpAddr::V6(a) => {
					let (res, of) = Ipv6AddrTools::_overflowing_prev(a, prefix_len);
					(IpAddr::V6(res), of)
				},
			}
		}

		pub(crate) const fn _overflowing_dec(address: IpAddr, prefix_len: u8, step: u128) -> (IpAddr, bool) {
			match address {
				IpAddr::V4(a) => {
					let (res, of) = Ipv4AddrTools::_overflowing_dec(a, prefix_len, step);
					(IpAddr::V4(res), of)
				},
				IpAddr::V6(a) => {
					let (res, of) = Ipv6AddrTools::_overflowing_dec(a, prefix_len, step);
					(IpAddr::V6(res), of)
				},
			}
		}
	*/

	/*
		pub(crate) const fn _overflowing_sub(address: IpAddr, other: IpAddr) -> (u128, bool) {
			match (address, other) {
				(IpAddr::V4(a), IpAddr::V4(other)) => Ipv4AddrTools::_overflowing_sub(a, other),
				(IpAddr::V6(a), IpAddr::V6(other)) => Ipv6AddrTools::_overflowing_sub(a, other),
				_ => (0, false),
			}
		}
	*/

	/*
		fn _prefix_match(address: IpAddr, other: IpAddr, prefix_len: u8) -> bool {
			match (address, other) {
				(IpAddr::V4(a), IpAddr::V4(other)) => {
					Ipv4AddrTools::_prefix_match(a, other, prefix_len)
				},
				(IpAddr::V6(a), IpAddr::V6(other)) => {
					Ipv6AddrTools::_prefix_match(a, other, prefix_len)
				},
				_ => false,
			}
		}

		pub(crate) const fn _network_address(address: IpAddr, prefix_len: u8) -> IpAddr {
			match address {
				IpAddr::V4(a) => IpAddr::V4(Ipv4AddrTools::_network_address(a, prefix_len)),
				IpAddr::V6(a) => IpAddr::V6(Ipv6AddrTools::_network_address(a, prefix_len)),
			}
		}

		fn _last_address(address: IpAddr, prefix_len: u8) -> IpAddr {
			match address {
				IpAddr::V4(a) => IpAddr::V4(Ipv4AddrTools::_last_address(a, prefix_len)),
				IpAddr::V6(a) => IpAddr::V6(Ipv6AddrTools::_last_address(a, prefix_len)),
			}
		}
	*/
}

impl PrivUnspecAddress for IpAddr {
	type _Tools = IpAddrTools;
}