1use core::fmt;
12use super::super::iana::OptionCode;
13use super::super::message_builder::OptBuilder;
14use super::super::net::IpAddr;
15use super::super::wire::{Compose, Composer, FormError, ParseError};
16use super::{Opt, OptData, ComposeOptData, ParseOptData};
17use octseq::builder::OctetsBuilder;
18use octseq::octets::Octets;
19use octseq::parse::Parser;
20
21#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
41pub struct ClientSubnet {
42 source_prefix_len: u8,
44
45 scope_prefix_len: u8,
47
48 addr: IpAddr,
50}
51
52impl ClientSubnet {
53 #[must_use]
60 pub fn new(
61 source_prefix_len: u8,
62 scope_prefix_len: u8,
63 addr: IpAddr,
64 ) -> ClientSubnet {
65 let source_prefix_len = normalize_prefix_len(addr, source_prefix_len);
66 let scope_prefix_len = normalize_prefix_len(addr, scope_prefix_len);
67 let (addr, _) = addr_apply_mask(addr, source_prefix_len);
68
69 ClientSubnet {
70 source_prefix_len,
71 scope_prefix_len,
72 addr,
73 }
74 }
75
76 #[must_use]
81 pub fn source_prefix_len(&self) -> u8 {
82 self.source_prefix_len
83 }
84
85 #[must_use]
90 pub fn scope_prefix_len(&self) -> u8 {
91 self.scope_prefix_len
92 }
93
94 #[must_use]
96 pub fn addr(&self) -> IpAddr {
97 self.addr
98 }
99
100 pub fn parse<Octs: AsRef<[u8]>>(
102 parser: &mut Parser<Octs>
103 ) -> Result<Self, ParseError> {
104 const ERR_ADDR_LEN: &str = "invalid address length in client \
105 subnet option";
106
107 let family = parser.parse_u16_be()?;
108 let source_prefix_len = parser.parse_u8()?;
109 let scope_prefix_len = parser.parse_u8()?;
110
111 let prefix_bytes = prefix_bytes(source_prefix_len);
118
119 let addr = match family {
120 1 => {
121 let mut buf = [0; 4];
122 if prefix_bytes > buf.len() {
123 return Err(ParseError::form_error(ERR_ADDR_LEN));
124 }
125 parser
126 .parse_buf(&mut buf[..prefix_bytes])
127 .map_err(|_| ParseError::form_error(ERR_ADDR_LEN))?;
128
129 if parser.remaining() != 0 {
130 return Err(ParseError::form_error(ERR_ADDR_LEN));
131 }
132
133 IpAddr::from(buf)
134 }
135 2 => {
136 let mut buf = [0; 16];
137 if prefix_bytes > buf.len() {
138 return Err(ParseError::form_error(ERR_ADDR_LEN));
139 }
140 parser
141 .parse_buf(&mut buf[..prefix_bytes])
142 .map_err(|_| ParseError::form_error(ERR_ADDR_LEN))?;
143
144 if parser.remaining() != 0 {
145 return Err(ParseError::form_error(ERR_ADDR_LEN));
146 }
147
148 IpAddr::from(buf)
149 }
150 _ => {
151 return Err(FormError::new(
152 "invalid client subnet address family",
153 )
154 .into())
155 }
156 };
157
158 let (addr, modified) = addr_apply_mask(addr, source_prefix_len);
161 if modified {
162 return Err(ParseError::form_error(ERR_ADDR_LEN));
163 }
164
165 Ok(ClientSubnet {
167 source_prefix_len,
168 scope_prefix_len,
169 addr,
170 })
171 }
172}
173
174impl OptData for ClientSubnet {
177 fn code(&self) -> OptionCode {
178 OptionCode::ClientSubnet
179 }
180}
181
182impl<'a, Octs: AsRef<[u8]>> ParseOptData<'a, Octs> for ClientSubnet {
183 fn parse_option(
184 code: OptionCode,
185 parser: &mut Parser<'a, Octs>,
186 ) -> Result<Option<Self>, ParseError> {
187 if code == OptionCode::ClientSubnet {
188 Self::parse(parser).map(Some)
189 }
190 else {
191 Ok(None)
192 }
193 }
194}
195
196impl ComposeOptData for ClientSubnet {
197 fn compose_len(&self) -> u16 {
198 u16::try_from(prefix_bytes(self.source_prefix_len)).unwrap() + 4
199 }
200
201 fn compose_option<Target: OctetsBuilder + ?Sized>(
202 &self, target: &mut Target
203 ) -> Result<(), Target::AppendError> {
204 let prefix_bytes = prefix_bytes(self.source_prefix_len);
205 match self.addr {
206 IpAddr::V4(addr) => {
207 1u16.compose(target)?;
208 self.source_prefix_len.compose(target)?;
209 self.scope_prefix_len.compose(target)?;
210 let array = addr.octets();
211 assert!(prefix_bytes <= array.len());
212 target.append_slice(&array[..prefix_bytes])
213 }
214 IpAddr::V6(addr) => {
215 2u16.compose(target)?;
216 self.source_prefix_len.compose(target)?;
217 self.scope_prefix_len.compose(target)?;
218 let array = addr.octets();
219 assert!(prefix_bytes <= array.len());
220 target.append_slice(&array[..prefix_bytes])
221 }
222 }
223 }
224}
225
226impl fmt::Display for ClientSubnet {
229 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
230 match self.addr {
231 IpAddr::V4(a) => {
232 if self.scope_prefix_len != 0 {
233 write!(f, "{}/{}/{}", a, self.source_prefix_len,
234 self.scope_prefix_len)?;
235 } else {
236 write!(f, "{}/{}", a, self.source_prefix_len)?;
237 }
238 }
239 IpAddr::V6(a) => {
240 if self.scope_prefix_len != 0 {
241 write!(f, "{}/{}/{}", a, self.source_prefix_len,
242 self.scope_prefix_len)?;
243 } else {
244 write!(f, "{}/{}", a, self.source_prefix_len)?;
245 }
246 }
247 }
248
249 Ok(())
250 }
251}
252
253impl<Octs: Octets> Opt<Octs> {
256 pub fn client_subnet(&self) -> Option<ClientSubnet> {
263 self.first()
264 }
265}
266
267impl<'a, Target: Composer> OptBuilder<'a, Target> {
268 pub fn client_subnet(
269 &mut self,
270 source_prefix_len: u8,
271 scope_prefix_len: u8,
272 addr: IpAddr,
273 ) -> Result<(), Target::AppendError> {
274 self.push(
275 &ClientSubnet::new(source_prefix_len, scope_prefix_len, addr)
276 )
277 }
278}
279
280fn prefix_bytes(bits: u8) -> usize {
284 (usize::from(bits) + 7) / 8
285}
286
287fn apply_bit_mask(buf: &mut [u8], mask: usize) -> bool {
291 let mut modified = false;
292
293 let mut p = mask / 8;
295 if p >= buf.len() {
296 return modified;
297 }
298
299 let bits = mask % 8;
301 if bits != 0 {
302 if buf[p].trailing_zeros() < (8 - bits) as u32 {
303 buf[p] &= 0xff << (8 - bits);
304 modified = true;
305 }
306 p += 1;
307 }
308
309 while p < buf.len() {
311 if buf[p] != 0 {
312 buf[p] = 0;
313 modified = true;
314 }
315 p += 1;
316 }
317
318 modified
319}
320
321fn addr_apply_mask(addr: IpAddr, len: u8) -> (IpAddr, bool) {
325 match addr {
326 IpAddr::V4(a) => {
327 let mut array = a.octets();
328 let m = apply_bit_mask(&mut array, len as usize);
329 (array.into(), m)
330 }
331 IpAddr::V6(a) => {
332 let mut array = a.octets();
333 let m = apply_bit_mask(&mut array, len as usize);
334 (array.into(), m)
335 }
336 }
337}
338
339fn normalize_prefix_len(addr: IpAddr, len: u8) -> u8 {
341 let max = match addr {
342 IpAddr::V4(_) => 32,
343 IpAddr::V6(_) => 128,
344 };
345
346 core::cmp::min(len, max)
347}
348
349#[cfg(all(test, feature="std", feature = "bytes"))]
352mod tests {
353 use super::*;
354 use super::super::test::test_option_compose_parse;
355 use octseq::builder::infallible;
356 use std::vec::Vec;
357 use core::str::FromStr;
358
359 macro_rules! check {
360 ($name:ident, $addr:expr, $prefix:expr, $exp:expr, $ok:expr) => {
361 #[test]
362 fn $name() {
363 let addr = $addr.parse().unwrap();
364 let opt = ClientSubnet::new($prefix, 0, addr);
365 assert_eq!(opt.addr(), $exp.parse::<IpAddr>().unwrap());
366
367 let mut opt_ = opt.clone();
370 opt_.addr = addr;
371 let mut buf = Vec::new();
372
373 infallible(opt_.compose_option(&mut buf));
374 match ClientSubnet::parse(&mut Parser::from_ref(&buf)) {
375 Ok(v) => assert_eq!(opt, v),
376 Err(_) => assert!(!$ok),
377 }
378 }
379 };
380 }
381
382 check!(prefix_at_boundary_v4, "192.0.2.0", 24, "192.0.2.0", true);
383 check!(prefix_at_boundary_v6, "2001:db8::", 32, "2001:db8::", true);
384 check!(prefix_no_truncation, "192.0.2.0", 23, "192.0.2.0", true);
385 check!(prefix_need_truncation, "192.0.2.0", 22, "192.0.0.0", false);
386 check!(prefix_min, "192.0.2.0", 0, "0.0.0.0", true);
387 check!(prefix_max, "192.0.2.0", 32, "192.0.2.0", true);
388 check!(prefix_too_long, "192.0.2.0", 100, "192.0.2.0", false);
389
390 #[test]
391 #[allow(clippy::redundant_closure)] fn client_subnet_compose_parse() {
393 test_option_compose_parse(
394 &ClientSubnet::new(4, 6, IpAddr::from_str("127.0.0.1").unwrap()),
395 |parser| ClientSubnet::parse(parser)
396 );
397 }
398}