1use super::super::iana::OptionCode;
12use super::super::message_builder::OptBuilder;
13use super::super::net::IpAddr;
14use super::super::wire::{Compose, Composer, FormError, ParseError};
15use super::{ComposeOptData, Opt, OptData, ParseOptData};
16use core::fmt;
17use octseq::builder::OctetsBuilder;
18use octseq::octets::Octets;
19use octseq::parse::Parser;
20
21#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
41#[cfg_attr(feature = "serde", derive(serde::Serialize))]
42pub struct ClientSubnet {
43 source_prefix_len: u8,
45
46 scope_prefix_len: u8,
48
49 addr: IpAddr,
51}
52
53impl ClientSubnet {
54 pub(super) const CODE: OptionCode = OptionCode::CLIENT_SUBNET;
56
57 #[must_use]
64 pub fn new(
65 source_prefix_len: u8,
66 scope_prefix_len: u8,
67 addr: IpAddr,
68 ) -> ClientSubnet {
69 let source_prefix_len = normalize_prefix_len(addr, source_prefix_len);
70 let scope_prefix_len = normalize_prefix_len(addr, scope_prefix_len);
71 let (addr, _) = addr_apply_mask(addr, source_prefix_len);
72
73 ClientSubnet {
74 source_prefix_len,
75 scope_prefix_len,
76 addr,
77 }
78 }
79
80 #[must_use]
85 pub fn source_prefix_len(&self) -> u8 {
86 self.source_prefix_len
87 }
88
89 #[must_use]
94 pub fn scope_prefix_len(&self) -> u8 {
95 self.scope_prefix_len
96 }
97
98 #[must_use]
100 pub fn addr(&self) -> IpAddr {
101 self.addr
102 }
103
104 pub fn parse<Octs: AsRef<[u8]>>(
106 parser: &mut Parser<'_, Octs>,
107 ) -> Result<Self, ParseError> {
108 const ERR_ADDR_LEN: &str = "invalid address length in client \
109 subnet option";
110
111 let family = parser.parse_u16_be()?;
112 let source_prefix_len = parser.parse_u8()?;
113 let scope_prefix_len = parser.parse_u8()?;
114
115 let prefix_bytes = prefix_bytes(source_prefix_len);
122
123 let addr = match family {
124 1 => {
125 let mut buf = [0; 4];
126 if prefix_bytes > buf.len() {
127 return Err(ParseError::form_error(ERR_ADDR_LEN));
128 }
129 parser
130 .parse_buf(&mut buf[..prefix_bytes])
131 .map_err(|_| ParseError::form_error(ERR_ADDR_LEN))?;
132
133 if parser.remaining() != 0 {
134 return Err(ParseError::form_error(ERR_ADDR_LEN));
135 }
136
137 IpAddr::from(buf)
138 }
139 2 => {
140 let mut buf = [0; 16];
141 if prefix_bytes > buf.len() {
142 return Err(ParseError::form_error(ERR_ADDR_LEN));
143 }
144 parser
145 .parse_buf(&mut buf[..prefix_bytes])
146 .map_err(|_| ParseError::form_error(ERR_ADDR_LEN))?;
147
148 if parser.remaining() != 0 {
149 return Err(ParseError::form_error(ERR_ADDR_LEN));
150 }
151
152 IpAddr::from(buf)
153 }
154 _ => {
155 return Err(FormError::new(
156 "invalid client subnet address family",
157 )
158 .into())
159 }
160 };
161
162 let (addr, modified) = addr_apply_mask(addr, source_prefix_len);
165 if modified {
166 return Err(ParseError::form_error(ERR_ADDR_LEN));
167 }
168
169 Ok(ClientSubnet {
171 source_prefix_len,
172 scope_prefix_len,
173 addr,
174 })
175 }
176
177 pub(super) fn try_octets_from<E>(src: Self) -> Result<Self, E> {
181 Ok(src)
182 }
183}
184
185impl OptData for ClientSubnet {
188 fn code(&self) -> OptionCode {
189 OptionCode::CLIENT_SUBNET
190 }
191}
192
193impl<'a, Octs: AsRef<[u8]>> ParseOptData<'a, Octs> for ClientSubnet {
194 fn parse_option(
195 code: OptionCode,
196 parser: &mut Parser<'a, Octs>,
197 ) -> Result<Option<Self>, ParseError> {
198 if code == OptionCode::CLIENT_SUBNET {
199 Self::parse(parser).map(Some)
200 } else {
201 Ok(None)
202 }
203 }
204}
205
206impl ComposeOptData for ClientSubnet {
207 fn compose_len(&self) -> u16 {
208 u16::try_from(prefix_bytes(self.source_prefix_len)).unwrap() + 4
209 }
210
211 fn compose_option<Target: OctetsBuilder + ?Sized>(
212 &self,
213 target: &mut Target,
214 ) -> Result<(), Target::AppendError> {
215 let prefix_bytes = prefix_bytes(self.source_prefix_len);
216 match self.addr {
217 IpAddr::V4(addr) => {
218 1u16.compose(target)?;
219 self.source_prefix_len.compose(target)?;
220 self.scope_prefix_len.compose(target)?;
221 let array = addr.octets();
222 assert!(prefix_bytes <= array.len());
223 target.append_slice(&array[..prefix_bytes])
224 }
225 IpAddr::V6(addr) => {
226 2u16.compose(target)?;
227 self.source_prefix_len.compose(target)?;
228 self.scope_prefix_len.compose(target)?;
229 let array = addr.octets();
230 assert!(prefix_bytes <= array.len());
231 target.append_slice(&array[..prefix_bytes])
232 }
233 }
234 }
235}
236
237impl fmt::Display for ClientSubnet {
240 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
241 match self.addr {
242 IpAddr::V4(a) => {
243 if self.scope_prefix_len != 0 {
244 write!(
245 f,
246 "{}/{}/{}",
247 a, self.source_prefix_len, self.scope_prefix_len
248 )?;
249 } else {
250 write!(f, "{}/{}", a, self.source_prefix_len)?;
251 }
252 }
253 IpAddr::V6(a) => {
254 if self.scope_prefix_len != 0 {
255 write!(
256 f,
257 "{}/{}/{}",
258 a, self.source_prefix_len, self.scope_prefix_len
259 )?;
260 } else {
261 write!(f, "{}/{}", a, self.source_prefix_len)?;
262 }
263 }
264 }
265
266 Ok(())
267 }
268}
269
270impl<Octs: Octets> Opt<Octs> {
273 pub fn client_subnet(&self) -> Option<ClientSubnet> {
280 self.first()
281 }
282}
283
284impl<Target: Composer> OptBuilder<'_, Target> {
285 pub fn client_subnet(
286 &mut self,
287 source_prefix_len: u8,
288 scope_prefix_len: u8,
289 addr: IpAddr,
290 ) -> Result<(), Target::AppendError> {
291 self.push(&ClientSubnet::new(
292 source_prefix_len,
293 scope_prefix_len,
294 addr,
295 ))
296 }
297}
298
299fn prefix_bytes(bits: u8) -> usize {
303 usize::from(bits).div_ceil(8)
304}
305
306fn apply_bit_mask(buf: &mut [u8], mask: usize) -> bool {
310 let mut modified = false;
311
312 let mut p = mask / 8;
314 if p >= buf.len() {
315 return modified;
316 }
317
318 let bits = mask % 8;
320 if bits != 0 {
321 if buf[p].trailing_zeros() < (8 - bits) as u32 {
322 buf[p] &= 0xff << (8 - bits);
323 modified = true;
324 }
325 p += 1;
326 }
327
328 while p < buf.len() {
330 if buf[p] != 0 {
331 buf[p] = 0;
332 modified = true;
333 }
334 p += 1;
335 }
336
337 modified
338}
339
340fn addr_apply_mask(addr: IpAddr, len: u8) -> (IpAddr, bool) {
344 match addr {
345 IpAddr::V4(a) => {
346 let mut array = a.octets();
347 let m = apply_bit_mask(&mut array, len as usize);
348 (array.into(), m)
349 }
350 IpAddr::V6(a) => {
351 let mut array = a.octets();
352 let m = apply_bit_mask(&mut array, len as usize);
353 (array.into(), m)
354 }
355 }
356}
357
358fn normalize_prefix_len(addr: IpAddr, len: u8) -> u8 {
360 let max = match addr {
361 IpAddr::V4(_) => 32,
362 IpAddr::V6(_) => 128,
363 };
364
365 core::cmp::min(len, max)
366}
367
368#[cfg(all(test, feature = "std", feature = "bytes"))]
371mod tests {
372 use super::super::test::test_option_compose_parse;
373 use super::*;
374 use core::str::FromStr;
375 use octseq::builder::infallible;
376 use std::vec::Vec;
377
378 macro_rules! check {
379 ($name:ident, $addr:expr, $prefix:expr, $exp:expr, $ok:expr) => {
380 #[test]
381 fn $name() {
382 let addr = $addr.parse().unwrap();
383 let opt = ClientSubnet::new($prefix, 0, addr);
384 assert_eq!(opt.addr(), $exp.parse::<IpAddr>().unwrap());
385
386 let mut opt_ = opt.clone();
389 opt_.addr = addr;
390 let mut buf = Vec::new();
391
392 infallible(opt_.compose_option(&mut buf));
393 match ClientSubnet::parse(&mut Parser::from_ref(&buf)) {
394 Ok(v) => assert_eq!(opt, v),
395 Err(_) => assert!(!$ok),
396 }
397 }
398 };
399 }
400
401 check!(prefix_at_boundary_v4, "192.0.2.0", 24, "192.0.2.0", true);
402 check!(prefix_at_boundary_v6, "2001:db8::", 32, "2001:db8::", true);
403 check!(prefix_no_truncation, "192.0.2.0", 23, "192.0.2.0", true);
404 check!(prefix_need_truncation, "192.0.2.0", 22, "192.0.0.0", false);
405 check!(prefix_min, "192.0.2.0", 0, "0.0.0.0", true);
406 check!(prefix_max, "192.0.2.0", 32, "192.0.2.0", true);
407 check!(prefix_too_long, "192.0.2.0", 100, "192.0.2.0", false);
408
409 #[test]
410 #[allow(clippy::redundant_closure)] fn client_subnet_compose_parse() {
412 test_option_compose_parse(
413 &ClientSubnet::new(4, 6, IpAddr::from_str("127.0.0.1").unwrap()),
414 |parser| ClientSubnet::parse(parser),
415 );
416 }
417}