1use core::net::{
2 Ipv4Addr,
3 Ipv6Addr,
4};
5
6use crate::{
7 utils::bigendian::int_helpers::{
8 u128,
9 u32,
10 u8,
11 },
12 FixedBitString,
13};
14
15fn with_ipv4_mut_slice<F, T>(addr: &mut Ipv4Addr, f: F) -> T
16where
17 F: FnOnce(&mut [u8]) -> T,
18{
19 let mut o = addr.octets();
20 let result = f(&mut o);
21 *addr = Ipv4Addr::from(o);
22 result
23}
24
25fn with_ipv4_mut_u32<F, T>(addr: &mut Ipv4Addr, f: F) -> T
26where
27 F: FnOnce(&mut u32) -> T,
28{
29 let mut o = addr.to_bits();
30 let result = f(&mut o);
31 *addr = Ipv4Addr::from_bits(o);
32 result
33}
34
35impl FixedBitString for Ipv4Addr {
36 const LEN: usize = 32;
37
38 fn inc(&mut self, prefix: usize) -> bool {
39 with_ipv4_mut_u32(self, |num| u32::element_inc(num, prefix))
40 }
41
42 fn get(&self, ndx: usize) -> bool {
43 u8::slice_get(&self.octets(), ndx)
44 }
45
46 fn set(&mut self, ndx: usize, bit: bool) {
47 with_ipv4_mut_slice(self, |slice| u8::slice_set(slice, ndx, bit))
48 }
49
50 fn flip(&mut self, ndx: usize) {
51 with_ipv4_mut_slice(self, |slice| u8::slice_flip(slice, ndx))
52 }
53
54 fn shared_prefix_len(&self, other: &Self) -> usize {
55 u32::element_shared_prefix_len(self.to_bits(), other.to_bits(), Self::LEN)
56 }
57
58 fn set_false_from(&mut self, ndx: usize) {
59 with_ipv4_mut_u32(self, |num| u32::element_set_false_from(num, ndx))
60 }
61
62 fn is_false_from(&self, ndx: usize) -> bool {
63 u32::element_is_false_from(self.to_bits(), ndx)
64 }
65
66 fn set_true_from(&mut self, ndx: usize) {
67 with_ipv4_mut_u32(self, |num| u32::element_set_true_from(num, ndx))
68 }
69
70 fn is_true_from(&self, ndx: usize) -> bool {
71 u32::element_is_true_from(self.to_bits(), ndx)
72 }
73
74 fn new_all_false() -> Self {
75 Ipv4Addr::from_bits(0)
76 }
77
78 fn new_all_true() -> Self {
79 Ipv4Addr::from_bits(!0)
80 }
81
82 fn contains(&self, prefix: usize, other: &Self) -> bool {
83 u32::element_contains(self.to_bits(), prefix, other.to_bits())
84 }
85}
86
87fn with_ipv6_mut_slice<F, T>(addr: &mut Ipv6Addr, f: F) -> T
88where
89 F: FnOnce(&mut [u8]) -> T,
90{
91 let mut o = addr.octets();
92 let result = f(&mut o);
93 *addr = Ipv6Addr::from(o);
94 result
95}
96
97fn with_ipv6_mut_u128<F, T>(addr: &mut Ipv6Addr, f: F) -> T
98where
99 F: FnOnce(&mut u128) -> T,
100{
101 let mut o = addr.to_bits();
102 let result = f(&mut o);
103 *addr = Ipv6Addr::from_bits(o);
104 result
105}
106
107impl FixedBitString for Ipv6Addr {
108 const LEN: usize = 128;
109
110 fn inc(&mut self, prefix: usize) -> bool {
111 with_ipv6_mut_u128(self, |num| u128::element_inc(num, prefix))
112 }
113
114 fn get(&self, ndx: usize) -> bool {
115 u8::slice_get(&self.octets(), ndx)
116 }
117
118 fn set(&mut self, ndx: usize, bit: bool) {
119 with_ipv6_mut_slice(self, |slice| u8::slice_set(slice, ndx, bit))
120 }
121
122 fn flip(&mut self, ndx: usize) {
123 with_ipv6_mut_slice(self, |slice| u8::slice_flip(slice, ndx))
124 }
125
126 fn shared_prefix_len(&self, other: &Self) -> usize {
127 u128::element_shared_prefix_len(self.to_bits(), other.to_bits(), Self::LEN)
128 }
129
130 fn set_false_from(&mut self, ndx: usize) {
131 with_ipv6_mut_u128(self, |num| u128::element_set_false_from(num, ndx))
132 }
133
134 fn is_false_from(&self, ndx: usize) -> bool {
135 u128::element_is_false_from(self.to_bits(), ndx)
136 }
137
138 fn set_true_from(&mut self, ndx: usize) {
139 with_ipv6_mut_u128(self, |num| u128::element_set_true_from(num, ndx))
140 }
141
142 fn is_true_from(&self, ndx: usize) -> bool {
143 u128::element_is_true_from(self.to_bits(), ndx)
144 }
145
146 fn new_all_false() -> Self {
147 Ipv6Addr::from_bits(0)
148 }
149
150 fn new_all_true() -> Self {
151 Ipv6Addr::from_bits(!0)
152 }
153
154 fn contains(&self, prefix: usize, other: &Self) -> bool {
155 u128::element_contains(self.to_bits(), prefix, other.to_bits())
156 }
157}