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 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
use std::convert::TryFrom; use std::fmt; use std::net::{IpAddr, Ipv6Addr}; use std::str::FromStr; use std::vec::IntoIter; use crate::errors::NetAddsError; use crate::range::Ipv6AddrRange; use crate::network::{NetworkAddrParseError, InvalidNetmaskError, InvalidNetmaskPrefixError}; use crate::iter::{IntoSmartIterator, Ipv6AddrSmartIterator}; /// An IPv6 address network. /// /// See [`crate::IpAddrNetwork`] for a type encompassing both IPv4 and IPv6 network. /// /// The size of an `Ipv6AddrNetwork` struct may vary depending on the target operating /// system. /// /// # Textual representation /// /// `Ipv6AddrNetwork` provides a [`FromStr`] implementation. The two parts are divided by `/`. /// The first part must contain an IPv6. The second part can either contain an IPv6 or an u8 /// between 0 and 128 which is valid as a netmask prefix. /// /// [`FromStr`]: std::str::FromStr /// /// # Examples /// /// ``` /// use std::net::Ipv6Addr; /// /// use net_adds::Ipv6AddrNetwork; /// /// let ip = Ipv6Addr::from(0x1); /// let network = Ipv6AddrNetwork::try_new(ip, 120).unwrap(); /// /// assert_eq!(Ok(network), "::1/120".parse()); /// assert_eq!(Ok(network), "::1/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00".parse()); /// /// let netmask = Ipv6Addr::new(0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFF00); /// let networkb = Ipv6AddrNetwork::try_new_with_addr(ip, netmask).unwrap(); /// /// assert_eq!(networkb, network); /// assert_eq!(Ok(networkb), "::1/120".parse()); /// assert_eq!(Ok(networkb), "::1/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00".parse()); /// ``` #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] pub struct Ipv6AddrNetwork { ip: Ipv6Addr, prefix: u8, netmask: Ipv6Addr, network: Ipv6Addr, broadcast: Ipv6Addr } impl Ipv6AddrNetwork { /// The max network size (netmask prefix). const MAX_SHORT_MASK_VALUE: u8 = 128; /// Returns an IPv4 network. /// /// If the netmask is not valid return an `NetAddsError::InvalidNetmaskPrefix(InvalidNetmaskPrefixError)`. pub fn try_new (ip: Ipv6Addr, prefix: u8) -> Result<Ipv6AddrNetwork, NetAddsError> { let iu = u128::from(ip); let nu = Self::prefix_to_ip(prefix)?; let netmask = Ipv6Addr::from(nu); let network = Ipv6Addr::from(iu & nu); let broadcast = Ipv6Addr::from(iu | !nu); Ok(Ipv6AddrNetwork { ip, prefix, netmask, network, broadcast }) } /// Returns an IPv4 network. /// /// If the netmask is not valid return an `NetAddsError::InvalidNetmask(InvalidNetmaskError)`. pub fn try_new_with_addr (ip: Ipv6Addr, netmask: Ipv6Addr) -> Result<Ipv6AddrNetwork, NetAddsError> { let iu = u128::from(ip); let nu = u128::from(netmask); let prefix = Self::ip_to_prefix(nu)?; let network = Ipv6Addr::from(iu & nu); let broadcast = Ipv6Addr::from(iu | !nu); Ok(Ipv6AddrNetwork { ip, prefix, netmask, network, broadcast }) } /// Returns the ip addr. pub fn ip (self) -> Ipv6Addr { self.ip } /// Returns the netmask prefix. pub fn prefix (self) -> u8 { self.prefix } /// Returns the netmask addr. pub fn netmask (self) -> Ipv6Addr { self.netmask } /// Returns the network addr. pub fn network (self) -> Ipv6Addr { self.network } /// Returns the broadcast addr. pub fn broadcast (self) -> Ipv6Addr { self.broadcast } /// Returns all ip of the network including the network and the broadcast addr. /// /// # Examples: /// /// ``` /// use std::net::Ipv6Addr; /// /// use net_adds::Ipv6AddrNetwork; /// /// let network = Ipv6AddrNetwork::try_new(Ipv6Addr::from(0x1), 126).unwrap(); /// /// assert_eq!(network.all(), vec![ /// Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0), /// Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), /// Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 2), /// Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 3) /// ]); /// ``` pub fn all (&self) -> Vec<Ipv6Addr> { Ipv6AddrRange::new(self.network(), self.broadcast()).all() } /// Returns all hosts (exclude network & broadcast addr). /// /// # Examples: /// /// ``` /// use std::net::Ipv6Addr; /// /// use net_adds::Ipv6AddrNetwork; /// /// let network = Ipv6AddrNetwork::try_new(Ipv6Addr::from(0x1), 126).unwrap(); /// /// assert_eq!(network.hosts(), vec![ /// Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), /// Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 2) /// ]); /// ``` pub fn hosts (&self) -> Vec<Ipv6Addr> { let mut ips = Ipv6AddrRange::new(self.network(), self.broadcast()).all(); ips.remove(0); ips.pop(); ips } /// Returns the number of ip's included in the network including the network and the broadcast addr. /// /// # Examples: /// /// ``` /// use std::net::Ipv6Addr; /// /// use net_adds::Ipv6AddrNetwork; /// /// let network = Ipv6AddrNetwork::try_new(Ipv6Addr::from(0x1), 120).unwrap(); /// /// assert_eq!(network.size(), 256); /// ``` pub fn size (&self) -> u128 { u128::from(self.broadcast()) - u128::from(self.network()) + 1 } /// Returns true if the ip argument is included in the network, else returns false. /// /// # Examples: /// /// ``` /// use std::net::Ipv6Addr; /// /// use net_adds::Ipv6AddrNetwork; /// /// let network = Ipv6AddrNetwork::try_new(Ipv6Addr::from(0x1), 64).unwrap(); /// /// assert!(network.has(Ipv6Addr::from(0x1))); /// assert!(network.has(Ipv6Addr::from(0xA))); /// assert!(network.has(Ipv6Addr::from(0x00FF))); /// /// assert!(!network.has(Ipv6Addr::from(0xFFFFFFFFFFFFFFFFFF00000000000000))); /// ``` pub fn has (&self, ip: Ipv6Addr) -> bool { let needle = u128::from(ip); let network = u128::from(self.network()); let broadcast = u128::from(self.broadcast()); return needle >= network && needle <= broadcast } /// Check the validity of a netmask under Ipv6Addr representation. /// /// If the netmask is not valid return an `NetAddsError::InvalidNetmask(InvalidNetmaskError)`. /// /// # Examples: /// /// ``` /// use std::net::Ipv6Addr; /// /// use net_adds::Ipv6AddrNetwork; /// /// let netmask = Ipv6Addr::from(0xFFFFFFFF000000000000000000000000); /// assert_eq!(Ipv6AddrNetwork::validate_netmask(u128::from(netmask)), Ok(u128::from(netmask))); /// /// let netmask = Ipv6Addr::from(0x0000000000000000000000000000FFFF); /// assert!(Ipv6AddrNetwork::validate_netmask(u128::from(netmask)).is_err()); /// ``` pub fn validate_netmask (netmask: u128) -> Result<u128, NetAddsError> { if netmask != 0 && (((!netmask + 1) & !netmask) != 0) { Err(NetAddsError::InvalidNetmask(InvalidNetmaskError(IpAddr::V6(Ipv6Addr::from(netmask))))) } else { Ok(netmask) } } /// Check the validity of a netmask under CIDR prefix representation. /// /// If the netmask prefix is not valid return an `NetAddsError::InvalidNetmaskPrefix(InvalidNetmaskPrefixError)`. /// /// # Examples: /// /// ``` /// use std::net::Ipv6Addr; /// /// use net_adds::Ipv6AddrNetwork; /// /// assert_eq!(Ipv6AddrNetwork::validate_prefix(128), Ok(128)); /// /// assert!(Ipv6AddrNetwork::validate_prefix(129).is_err()); /// ``` pub fn validate_prefix (prefix: u8) -> Result<u8, NetAddsError> { if prefix > Self::MAX_SHORT_MASK_VALUE { Err(NetAddsError::InvalidNetmaskPrefix(InvalidNetmaskPrefixError(prefix))) } else { Ok(prefix) } } /// Returns the Ipv6Addr representation of a CIDR prefix. /// /// If the netmask prefix is not valid return an `NetAddsError::InvalidNetmaskPrefix(InvalidNetmaskPrefixError)`. /// /// # Examples: /// /// ``` /// use std::net::Ipv6Addr; /// /// use net_adds::Ipv6AddrNetwork; /// /// assert_eq!(Ipv6AddrNetwork::prefix_to_ip(128), Ok(u128::MAX)); /// /// assert!(Ipv6AddrNetwork::prefix_to_ip(129).is_err()); /// ``` pub fn prefix_to_ip (prefix: u8) -> Result<u128, NetAddsError> { if Self::validate_prefix(prefix)? == 0 { Ok(0) } else { Ok((u128::MAX << (128 - prefix)) & u128::MAX) } } /// Returns the CIDR prefix representation of an Ipv6Addr. /// /// We count the bit that are equals to 0 by shifting the sequence to the right (bitwise >>). /// Then we subtract the number of bits equal to 0 from the maximum number of bits available on the netmask. /// /// If the netmask IPv6 is not valid return an `NetAddsError::InvalidNetmask(InvalidNetmaskError)`. /// /// # Examples: /// /// ``` /// use std::net::Ipv6Addr; /// /// use net_adds::Ipv6AddrNetwork; /// /// let netmask = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0); /// assert_eq!(Ipv6AddrNetwork::ip_to_prefix(u128::from(netmask)), Ok(0)); /// /// let netmask = Ipv6Addr::new(0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF); /// assert_eq!(Ipv6AddrNetwork::ip_to_prefix(u128::from(netmask)), Ok(128)); /// /// let netmask = Ipv6Addr::new(0x0, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF); /// assert!(Ipv6AddrNetwork::ip_to_prefix(u128::from(netmask)).is_err()); /// ``` pub fn ip_to_prefix (ip: u128) -> Result<u8, NetAddsError> { if Self::validate_netmask(ip)? == 0 { Ok(0) } else { let mut tmp = ip.clone(); let mut bits = 0; while (tmp & 0x1) == 0 { tmp = tmp >> 1; bits = bits + 1; } Ok(u8::try_from(Self::MAX_SHORT_MASK_VALUE - bits).unwrap()) } } } impl fmt::Display for Ipv6AddrNetwork { fn fmt (&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}/{}", &self.ip(), &self.prefix()) } } impl TryFrom<(Ipv6Addr, u8)> for Ipv6AddrNetwork { type Error = NetAddsError; /// Create an `Ipv6AddrNetwork` from a tuple of two slots, `Ipv6Addr` and `u8`. /// /// If the netmask prefix is not valid return an `NetAddsError::InvalidNetmaskPrefix(InvalidNetmaskPrefixError)`. /// /// # Examples: /// /// ``` /// use std::convert::TryFrom; /// use std::net::Ipv6Addr; /// /// use net_adds::Ipv6AddrNetwork; /// /// let ip = Ipv6Addr::from(0x1); /// /// assert_eq!(Ipv6AddrNetwork::try_from((ip, 92)), Ipv6AddrNetwork::try_new(ip, 92)); /// /// assert!(Ipv6AddrNetwork::try_from((ip, 129)).is_err()); /// ``` fn try_from ((ip, prefix): (Ipv6Addr, u8)) -> Result<Ipv6AddrNetwork, Self::Error> { Ipv6AddrNetwork::try_new(ip, prefix) } } impl TryFrom<(Ipv6Addr, Ipv6Addr)> for Ipv6AddrNetwork { type Error = NetAddsError; /// Create an `Ipv6AddrNetwork` from a tuple of two `Ipv6Addr`. /// /// # Examples: /// /// ``` /// use std::convert::TryFrom; /// use std::net::Ipv6Addr; /// /// use net_adds::Ipv6AddrNetwork; /// /// let ip = Ipv6Addr::from(1); /// /// let netmask = Ipv6Addr::new(0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFF00); /// assert_eq!(Ipv6AddrNetwork::try_from((ip, netmask)), Ipv6AddrNetwork::try_new_with_addr(ip, netmask)); /// /// let netmask = Ipv6Addr::new(0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0x2002); /// assert!(Ipv6AddrNetwork::try_from((ip, netmask)).is_err()); /// ``` fn try_from (ips: (Ipv6Addr, Ipv6Addr)) -> Result<Ipv6AddrNetwork, Self::Error> { Ipv6AddrNetwork::try_new_with_addr(ips.0, ips.1) } } impl FromStr for Ipv6AddrNetwork { type Err = NetAddsError; /// Parse a string as `Ipv6AddrNetwork`. /// /// If the string representation is not valid return an `NetAddsError::NetworkAddrParse(NetworkAddrParseError)`. /// /// # Examples: /// /// ``` /// use std::convert::TryFrom; /// use std::net::Ipv6Addr; /// /// use net_adds::Ipv6AddrNetwork; /// /// let network = Ipv6AddrNetwork::try_from((Ipv6Addr::from(1), 126)).expect("invalid network"); /// /// assert_eq!("::1/126".parse(), Ok(network)); /// assert_eq!("::1/ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffc".parse(), Ok(network)); /// ``` fn from_str (s: &str) -> Result<Self, Self::Err> { let mut parts = s.split('/'); let ip = parts.next().map(|part| { Ipv6Addr::from_str(part) .map_err(|_| NetAddsError::NetworkAddrParse(NetworkAddrParseError())) }); let netmask = parts.next().map(|part| { Ipv6Addr::from_str(part).or( part.parse::<u8>() .map_err(|_| NetAddsError::NetworkAddrParse(NetworkAddrParseError())) .and_then(|prefix| Ok(Ipv6Addr::from(Self::prefix_to_ip(prefix)?))) ) }); if ip.is_none() || netmask.is_none() || parts.next().is_some() { Err(NetAddsError::NetworkAddrParse(NetworkAddrParseError())) } else { Ipv6AddrNetwork::try_new_with_addr(ip.unwrap()?, netmask.unwrap()?) } } } impl IntoIterator for Ipv6AddrNetwork { type Item = Ipv6Addr; type IntoIter = IntoIter<Self::Item>; /// Create a `Ipv6Addr` iterator. The iterator include the network and the broadcast. /// /// # Examples: /// /// ``` /// use std::net::Ipv6Addr; /// /// use net_adds::Ipv6AddrNetwork; /// /// let mut iter = Ipv6AddrNetwork::try_new(Ipv6Addr::from(0x1), 126) /// .expect("invalid network") /// .into_iter(); /// /// assert_eq!(iter.next(), Some(Ipv6Addr::from(0))); /// assert_eq!(iter.next(), Some(Ipv6Addr::from(1))); /// assert_eq!(iter.next(), Some(Ipv6Addr::from(2))); /// assert_eq!(iter.next(), Some(Ipv6Addr::from(3))); /// assert_eq!(iter.next(), None); /// ``` fn into_iter (self) -> Self::IntoIter { self.all().into_iter() } } impl IntoSmartIterator for Ipv6AddrNetwork { type Item = Ipv6Addr; type IntoSmartIter = Ipv6AddrSmartIterator; /// Create a smart `Ipv6Addr` iterator. The iterator include the network and the broadcast. /// /// # Examples: /// /// ``` /// use std::net::Ipv6Addr; /// /// use net_adds::{Ipv6AddrNetwork, IntoSmartIterator}; /// /// let mut iter = Ipv6AddrNetwork::try_new(Ipv6Addr::from(0x1), 126) /// .expect("invalid network") /// .into_smart_iter(); /// /// assert_eq!(iter.next(), Some(Ipv6Addr::from(0))); /// assert_eq!(iter.next(), Some(Ipv6Addr::from(1))); /// assert_eq!(iter.next(), Some(Ipv6Addr::from(2))); /// assert_eq!(iter.next(), Some(Ipv6Addr::from(3))); /// assert_eq!(iter.next(), None); /// ``` fn into_smart_iter (self) -> Self::IntoSmartIter { Ipv6AddrSmartIterator::new(self.network(), self.broadcast()) } } #[cfg(test)] mod test { use std::net::Ipv6Addr; use crate::{Ipv6AddrNetwork, NetAddsError, NetworkAddrParseError, InvalidNetmaskPrefixError}; #[test] fn from_str () { let ip = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1); let network = Ipv6AddrNetwork::try_new(ip, 0); assert_eq!(network, "::1/0".parse()); assert_eq!(network, "::1/::".parse()); assert_eq!(network, "::1/0:0:0:0:0:0:0:0".parse()); assert_eq!(network, "::1/0000:0000:0000:0000:0000:0000:0000:0000".parse()); assert_eq!(network, "::1/0000:0000:0000::0000:0000".parse()); assert_eq!(network, "0:0:0:0:0:0:0:1/0".parse()); assert_eq!(network, "0:0:0:0:0:0:0:1/::".parse()); assert_eq!(network, "0:0:0:0:0:0:0:1/0000:0000:0000::0000:0000".parse()); let network = Ipv6AddrNetwork::try_new(ip, 96); assert_eq!(network, "::1/96".parse()); assert_eq!(network, "::1/ffff:ffff:ffff:ffff:ffff:ffff:0000:0000".parse()); assert_eq!(network, "::1/ffff:ffff:ffff:ffff:ffff:ffff::".parse()); assert_eq!(network, "0:0:0:0:0:0:0:1/96".parse()); assert_eq!(network, "0:0:0:0:0:0:0:1/ffff:ffff:ffff:ffff:ffff:ffff:0000:0000".parse()); assert_eq!(network, "0:0:0:0:0:0:0:1/ffff:ffff:ffff:ffff:ffff:ffff::".parse()); let network = Ipv6AddrNetwork::try_new(ip, 128); assert_eq!(network, "::1/128".parse()); assert_eq!(network, "::1/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff".parse()); assert_eq!(network, "0:0:0:0:0:0:0:1/128".parse()); assert_eq!(network, "0:0:0:0:0:0:0:1/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff".parse()); // invalid prefix. let err = Err(NetAddsError::InvalidNetmaskPrefix(InvalidNetmaskPrefixError(129))); assert_eq!(err, "::1/129".parse::<Ipv6AddrNetwork>()); let err = Err(NetAddsError::NetworkAddrParse(NetworkAddrParseError())); // ip is out of range (invalid char "z"). assert_eq!(err, "::fffz/24".parse::<Ipv6AddrNetwork>()); // ip is to short. assert_eq!(err, "0:0:0:0:0:0:0/24".parse::<Ipv6AddrNetwork>()); // no netmask. assert_eq!(err, "::1".parse::<Ipv6AddrNetwork>()); // too many ip. let s = "::1/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"; assert_eq!(err, s.parse::<Ipv6AddrNetwork>()); // no ip before `/`. assert_eq!(err, "/128".parse::<Ipv6AddrNetwork>()); // no netmask after `/`. assert_eq!(err, "::1/".parse::<Ipv6AddrNetwork>()); } }