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 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
//! This crate provides storage and retrieval of IPv4 and IPv6 network prefixes. //! //! Currently, it uses `ip_network` crate, that provides IP network data structure and //! `treebitmap` as backend, that provides fast lookup times, and a small memory footprint. //! Backend can be changed in future releases. //! //! ## Examples //! //! ```rust //! use std::net::{IpAddr, Ipv6Addr}; //! use ip_network::{IpNetwork, Ipv6Network}; //! use ip_network_table::IpNetworkTable; //! //! let mut table = IpNetworkTable::new(); //! let network = IpNetwork::new(Ipv6Addr::new(0x2001, 0xdb8, 0xdead, 0xbeef, 0, 0, 0, 0), 64).unwrap(); //! let ip_address = Ipv6Addr::new(0x2001, 0xdb8, 0xdead, 0xbeef, 0, 0, 0, 0x1); //! //! assert_eq!(table.insert(network, "foo"), None); //! // Get value for network from table //! assert_eq!(table.longest_match(ip_address), Some((network, &"foo"))); //! ``` #![warn(rust_2018_idioms)] use ip_network::{IpNetwork, Ipv4Network, Ipv6Network}; use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; use ip_network_table_deps_treebitmap::IpLookupTable; // forked from `treebitmap` /// Table holding IPv4 and IPv6 network prefixes with value. #[derive(Default)] pub struct IpNetworkTable<T> { ipv4: IpLookupTable<Ipv4Addr, T>, ipv6: IpLookupTable<Ipv6Addr, T>, } impl<T> IpNetworkTable<T> { /// Constructs a new, empty `IpNetworkTable<T>`. pub fn new() -> Self { Self::with_capacity(0, 0) } /// Constructs a new, empty `IpNetworkTable<T>` with the specified capacity. pub fn with_capacity(ipv4_size: usize, ipv6_size: usize) -> Self { Self { ipv4: IpLookupTable::with_capacity(ipv4_size), ipv6: IpLookupTable::with_capacity(ipv6_size), } } /// Returns the number of elements in the table. First value is number of IPv4 networks and second is number of IPv6 networks. pub fn len(&self) -> (usize, usize) { (self.ipv4.len(), self.ipv6.len()) } /// Returns `true` if table is empty. pub fn is_empty(&self) -> bool { self.ipv4.is_empty() && self.ipv6.is_empty() } /// Insert a value for the `IpNetwork`. If prefix existed previously, the old value is returned. /// /// # Examples /// /// ``` /// use ip_network_table::IpNetworkTable; /// use ip_network::Ipv6Network; /// use std::net::Ipv6Addr; /// /// let mut table = IpNetworkTable::new(); /// let network = Ipv6Network::new(Ipv6Addr::new(0x2001, 0xdb8, 0xdead, 0xbeef, 0, 0, 0, 0), 64).unwrap(); /// /// assert_eq!(table.insert(network, "foo"), None); /// // Insert duplicate /// assert_eq!(table.insert(network, "bar"), Some("foo")); /// // Value is replaced /// assert_eq!(table.insert(network, "null"), Some("bar")); /// ``` pub fn insert<N: Into<IpNetwork>>(&mut self, network: N, data: T) -> Option<T> { match network.into() { IpNetwork::V4(ipv4_network) => self.ipv4.insert( ipv4_network.network_address(), ipv4_network.netmask() as u32, data, ), IpNetwork::V6(ipv6_network) => self.ipv6.insert( ipv6_network.network_address(), ipv6_network.netmask() as u32, data, ), } } /// Remove a `IpNetwork` from table. If prefix existed, the value is returned. /// /// # Examples /// /// ``` /// use ip_network_table::IpNetworkTable; /// use ip_network::Ipv6Network; /// use std::net::Ipv6Addr; /// /// let mut table = IpNetworkTable::new(); /// let network = Ipv6Network::new(Ipv6Addr::new(0x2001, 0xdb8, 0xdead, 0xbeef, 0, 0, 0, 0), 64).unwrap(); /// /// assert_eq!(table.insert(network, "foo"), None); /// // Remove network from table /// assert_eq!(table.remove(network), Some("foo")); /// // Network is removed /// assert_eq!(table.exact_match(network), None); /// ``` pub fn remove<N: Into<IpNetwork>>(&mut self, network: N) -> Option<T> { match network.into() { IpNetwork::V4(ipv4_network) => self.ipv4.remove( ipv4_network.network_address(), ipv4_network.netmask() as u32, ), IpNetwork::V6(ipv6_network) => self.ipv6.remove( ipv6_network.network_address(), ipv6_network.netmask() as u32, ), } } /// Get pointer to value from table based on exact network match. /// If network is not in table, `None` is returned. /// /// # Examples /// /// ``` /// use ip_network_table::IpNetworkTable; /// use ip_network::Ipv6Network; /// use std::net::Ipv6Addr; /// /// let mut table = IpNetworkTable::new(); /// let network_a = Ipv6Network::new(Ipv6Addr::new(0x2001, 0xdb8, 0xdead, 0xbeef, 0, 0, 0, 0), 64).unwrap(); /// let network_b = Ipv6Network::new(Ipv6Addr::new(0x2001, 0xdb8, 0xdead, 0xbeef, 0, 0, 0, 0), 128).unwrap(); /// /// assert_eq!(table.insert(network_a, "foo"), None); /// // Get value for network from table /// assert_eq!(table.exact_match(network_a), Some(&"foo")); /// // Network B doesnt exists in table /// assert_eq!(table.exact_match(network_b), None); /// ``` pub fn exact_match<N: Into<IpNetwork>>(&self, network: N) -> Option<&T> { match network.into() { IpNetwork::V4(ipv4_network) => self.ipv4.exact_match( ipv4_network.network_address(), ipv4_network.netmask() as u32, ), IpNetwork::V6(ipv6_network) => self.ipv6.exact_match( ipv6_network.network_address(), ipv6_network.netmask() as u32, ), } } /// Get mutable pointer to value from table based on exact network match. /// If network is not in table, `None` is returned. /// /// # Examples /// /// ``` /// use ip_network_table::IpNetworkTable; /// use ip_network::Ipv6Network; /// use std::net::Ipv6Addr; /// /// let mut table = IpNetworkTable::new(); /// let network_a = Ipv6Network::new(Ipv6Addr::new(0x2001, 0xdb8, 0xdead, 0xbeef, 0, 0, 0, 0), 64).unwrap(); /// let network_b = Ipv6Network::new(Ipv6Addr::new(0x2001, 0xdb8, 0xdead, 0xbeef, 0, 0, 0, 0), 128).unwrap(); /// /// assert_eq!(table.insert(network_a, "foo"), None); /// // Get value for network from table /// assert_eq!(table.exact_match_mut(network_a), Some(&mut "foo")); /// // Network B doesnt exists in table /// assert_eq!(table.exact_match(network_b), None); /// ``` pub fn exact_match_mut<N: Into<IpNetwork>>(&mut self, network: N) -> Option<&mut T> { match network.into() { IpNetwork::V4(ipv4_network) => self.ipv4.exact_match_mut( ipv4_network.network_address(), ipv4_network.netmask() as u32, ), IpNetwork::V6(ipv6_network) => self.ipv6.exact_match_mut( ipv6_network.network_address(), ipv6_network.netmask() as u32, ), } } /// Find most specific IP network in table that contains given IP address. If no network in table contains /// given IP address, `None` is returned. /// /// # Examples /// /// ``` /// use ip_network_table::IpNetworkTable; /// use ip_network::{IpNetwork, Ipv6Network}; /// use std::net::{IpAddr, Ipv6Addr}; /// /// let mut table = IpNetworkTable::new(); /// let network = IpNetwork::new(Ipv6Addr::new(0x2001, 0xdb8, 0xdead, 0xbeef, 0, 0, 0, 0), 64).unwrap(); /// let ip_address = Ipv6Addr::new(0x2001, 0xdb8, 0xdead, 0xbeef, 0, 0, 0, 0x1); /// /// assert_eq!(table.insert(network, "foo"), None); /// // Get value for network from table /// assert_eq!(table.longest_match(ip_address), Some((network, &"foo"))); /// ``` pub fn longest_match<I: Into<IpAddr>>(&self, ip: I) -> Option<(IpNetwork, &T)> { match ip.into() { IpAddr::V4(ipv4) => self .longest_match_ipv4(ipv4) .map(|(n, t)| (IpNetwork::V4(n), t)), IpAddr::V6(ipv6) => self .longest_match_ipv6(ipv6) .map(|(n, t)| (IpNetwork::V6(n), t)), } } /// Find most specific IP network in table that contains given IP address. If no network in table contains /// given IP address, `None` is returned. /// /// # Examples /// /// ``` /// use ip_network_table::IpNetworkTable; /// use ip_network::{IpNetwork, Ipv6Network}; /// use std::net::Ipv6Addr; /// /// let mut table = IpNetworkTable::new(); /// let network = IpNetwork::new(Ipv6Addr::new(0x2001, 0xdb8, 0xdead, 0xbeef, 0, 0, 0, 0), 64).unwrap(); /// let ip_address = Ipv6Addr::new(0x2001, 0xdb8, 0xdead, 0xbeef, 0, 0, 0, 0x1); /// /// assert_eq!(table.insert(network, "foo"), None); /// // Get value for network from table /// assert_eq!(table.longest_match_mut(ip_address), Some((network, &mut "foo"))); /// ``` pub fn longest_match_mut<I: Into<IpAddr>>(&mut self, ip: I) -> Option<(IpNetwork, &mut T)> { match ip.into() { IpAddr::V4(ipv4) => self.ipv4.longest_match_mut(ipv4).map(|(addr, mask, data)| { ( IpNetwork::V4(Ipv4Network::new(addr, mask as u8).unwrap()), data, ) }), IpAddr::V6(ipv6) => self.ipv6.longest_match_mut(ipv6).map(|(addr, mask, data)| { ( IpNetwork::V6(Ipv6Network::new(addr, mask as u8).unwrap()), data, ) }), } } /// Specific version of `longest_match` for IPv4 address. #[inline] pub fn longest_match_ipv4(&self, ip: Ipv4Addr) -> Option<(Ipv4Network, &T)> { self.ipv4 .longest_match(ip) .map(|(addr, mask, data)| (Ipv4Network::new(addr, mask as u8).unwrap(), data)) } /// Specific version of `longest_match` for IPv6 address. #[inline] pub fn longest_match_ipv6(&self, ip: Ipv6Addr) -> Option<(Ipv6Network, &T)> { self.ipv6 .longest_match(ip) .map(|(addr, mask, data)| (Ipv6Network::new(addr, mask as u8).unwrap(), data)) } /// Find all IP networks in table that contains given IP address. /// Returns iterator of `IpNetwork` and reference to value. /// /// # Examples /// /// ``` /// use ip_network_table::IpNetworkTable; /// use ip_network::{IpNetwork, Ipv6Network}; /// use std::net::Ipv6Addr; /// /// let mut table = IpNetworkTable::new(); /// let network = IpNetwork::new(Ipv6Addr::new(0x2001, 0xdb8, 0xdead, 0xbeef, 0, 0, 0, 0), 64).unwrap(); /// let ip_address = Ipv6Addr::new(0x2001, 0xdb8, 0xdead, 0xbeef, 0, 0, 0, 0x1); /// /// assert_eq!(table.insert(network, "foo"), None); /// // Get value for network from table /// assert_eq!(table.matches(ip_address).count(), 1); /// ``` pub fn matches<I: Into<IpAddr>>( &self, ip: I, ) -> Box<dyn Iterator<Item = (IpNetwork, &T)> + '_> { match ip.into() { IpAddr::V4(ipv4) => Box::new( self.matches_ipv4(ipv4) .map(|(network, data)| (IpNetwork::V4(network), data)), ), IpAddr::V6(ipv6) => Box::new( self.matches_ipv6(ipv6) .map(|(network, data)| (IpNetwork::V6(network), data)), ), } } /// Specific version of `matches` for IPv4 address. pub fn matches_ipv4(&self, ip: Ipv4Addr) -> impl Iterator<Item = (Ipv4Network, &T)> { self.ipv4 .matches(ip) .map(|(addr, mask, data)| (Ipv4Network::new(addr, mask as u8).unwrap(), data)) } /// Specific version of `matches` for IPv6 address. pub fn matches_ipv6(&self, ip: Ipv6Addr) -> impl Iterator<Item = (Ipv6Network, &T)> { self.ipv6 .matches(ip) .map(|(addr, mask, data)| (Ipv6Network::new(addr, mask as u8).unwrap(), data)) } /// Find all IP networks in table that contains given IP address. /// Returns iterator of `IpNetwork` and mutable reference to value. /// /// # Examples /// /// ``` /// use ip_network_table::IpNetworkTable; /// use ip_network::{IpNetwork, Ipv6Network}; /// use std::net::{IpAddr, Ipv6Addr}; /// /// let mut table = IpNetworkTable::new(); /// let network = IpNetwork::new(Ipv6Addr::new(0x2001, 0xdb8, 0xdead, 0xbeef, 0, 0, 0, 0), 64).unwrap(); /// let ip_address = Ipv6Addr::new(0x2001, 0xdb8, 0xdead, 0xbeef, 0, 0, 0, 0x1); /// /// assert_eq!(table.insert(network, "foo"), None); /// // Get value for network from table /// assert_eq!(table.matches_mut(ip_address).count(), 1); /// ``` pub fn matches_mut<I: Into<IpAddr>>( &mut self, ip: I, ) -> Box<dyn Iterator<Item = (IpNetwork, &mut T)> + '_> { match ip.into() { IpAddr::V4(ipv4) => Box::new( self.matches_mut_ipv4(ipv4) .map(|(network, data)| (IpNetwork::V4(network), data)), ), IpAddr::V6(ipv6) => Box::new( self.matches_mut_ipv6(ipv6) .map(|(network, data)| (IpNetwork::V6(network), data)), ), } } /// Specific version of `matches_mut` for IPv4 address. #[inline] pub fn matches_mut_ipv4( &mut self, ip: Ipv4Addr, ) -> impl Iterator<Item = (Ipv4Network, &mut T)> { self.ipv4 .matches_mut(ip) .map(|(addr, mask, data)| (Ipv4Network::new(addr, mask as u8).unwrap(), data)) } /// Specific version of `matches_mut` for IPv6 address. #[inline] pub fn matches_mut_ipv6( &mut self, ip: Ipv6Addr, ) -> impl Iterator<Item = (Ipv6Network, &mut T)> { self.ipv6 .matches_mut(ip) .map(|(addr, mask, data)| (Ipv6Network::new(addr, mask as u8).unwrap(), data)) } /// Iterator for all networks in table, first are iterated IPv4 and then IPv6 networks. Order is not guaranteed. /// /// # Examples /// /// ``` /// use ip_network_table::IpNetworkTable; /// use ip_network::{IpNetwork, Ipv4Network, Ipv6Network}; /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; /// /// let mut table: IpNetworkTable<&str> = IpNetworkTable::new(); /// let network_a = Ipv4Network::new(Ipv4Addr::new(192, 168, 0, 0), 24).unwrap(); /// assert_eq!(table.insert(network_a, "foo"), None); /// let network_b = Ipv6Network::new(Ipv6Addr::new(0x2001, 0xdb8, 0xdead, 0xbeef, 0, 0, 0, 0), 64).unwrap(); /// assert_eq!(table.insert(network_b, "foo"), None); /// /// let mut iterator = table.iter(); /// assert_eq!(iterator.next(), Some((IpNetwork::V4(network_a), &"foo"))); /// assert_eq!(iterator.next(), Some((IpNetwork::V6(network_b), &"foo"))); /// assert_eq!(iterator.next(), None); /// ``` pub fn iter(&self) -> impl Iterator<Item = (IpNetwork, &T)> { self.iter_ipv4() .map(|(network, data)| (IpNetwork::V4(network), data)) .chain( self.iter_ipv6() .map(|(network, data)| (IpNetwork::V6(network), data)), ) } /// Mutable iterator for all networks in table, first are iterated IPv4 and then IPv6 networks. Order is not guaranteed. /// /// # Examples /// /// ``` /// use ip_network_table::IpNetworkTable; /// use ip_network::{IpNetwork, Ipv4Network, Ipv6Network}; /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; /// /// let mut table: IpNetworkTable<&str> = IpNetworkTable::new(); /// let network_a = Ipv4Network::new(Ipv4Addr::new(192, 168, 0, 0), 24).unwrap(); /// assert_eq!(table.insert(network_a, "foo"), None); /// let network_b = Ipv6Network::new(Ipv6Addr::new(0x2001, 0xdb8, 0xdead, 0xbeef, 0, 0, 0, 0), 64).unwrap(); /// assert_eq!(table.insert(network_b, "foo"), None); /// /// let mut iterator = table.iter_mut(); /// for (network, value) in iterator { /// *value = "bar"; /// } /// /// assert_eq!(table.exact_match(network_a), Some(&"bar")); /// assert_eq!(table.exact_match(network_b), Some(&"bar")); /// ``` pub fn iter_mut(&mut self) -> impl Iterator<Item = (IpNetwork, &mut T)> { self.ipv4 .iter_mut() .map(|(addr, mask, data)| (IpNetwork::new(addr, mask as u8).unwrap(), data)) .chain( self.ipv6 .iter_mut() .map(|(addr, mask, data)| (IpNetwork::new(addr, mask as u8).unwrap(), data)), ) } /// Iterator for all IPv4 networks in table. Order is not guaranteed. pub fn iter_ipv4(&self) -> impl Iterator<Item = (Ipv4Network, &T)> { self.ipv4 .iter() .map(|(addr, mask, data)| (Ipv4Network::new(addr, mask as u8).unwrap(), data)) } /// Iterator for all IPv6 networks in table. Order is not guaranteed. pub fn iter_ipv6(&self) -> impl Iterator<Item = (Ipv6Network, &T)> { self.ipv6 .iter() .map(|(addr, mask, data)| (Ipv6Network::new(addr, mask as u8).unwrap(), data)) } /// Retains only the elements specified by the predicate. /// /// In other words, remove all pairs `(k, v)` such that `f(ip_network, &mut v)` returns `false`. /// /// # Examples /// /// ``` /// use ip_network_table::IpNetworkTable; /// use ip_network::{IpNetwork, Ipv4Network, Ipv6Network}; /// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; /// /// let mut table: IpNetworkTable<&str> = IpNetworkTable::new(); /// let network_a = Ipv4Network::new(Ipv4Addr::new(192, 168, 0, 0), 24).unwrap(); /// assert_eq!(table.insert(network_a, "foo"), None); /// let network_b = Ipv6Network::new(Ipv6Addr::new(0x2001, 0xdb8, 0xdead, 0xbeef, 0, 0, 0, 0), 64).unwrap(); /// assert_eq!(table.insert(network_b, "foo"), None); /// /// // Keep just IPv4 networks /// table.retain(|network, _| network.is_ipv4()); /// /// assert_eq!(table.exact_match(network_a), Some(&"foo")); /// assert_eq!(table.exact_match(network_b), None); /// ``` pub fn retain<F>(&mut self, mut f: F) where F: FnMut(IpNetwork, &mut T) -> bool, { let mut to_delete = vec![]; for (network, data) in self.iter_mut() { if !f(network, data) { to_delete.push(network); } } for network in to_delete { self.remove(network); } } } #[cfg(test)] mod tests { use crate::IpNetworkTable; use ip_network::{Ipv4Network, Ipv6Network}; use std::net::{Ipv4Addr, Ipv6Addr}; #[test] fn insert_ipv4_ipv6() { let mut table = IpNetworkTable::new(); table.insert( Ipv4Network::new(Ipv4Addr::new(127, 0, 0, 0), 16).unwrap(), 1, ); table.insert( Ipv6Network::new(Ipv6Addr::new(1, 2, 3, 4, 5, 6, 7, 8), 128).unwrap(), 1, ); } #[test] fn exact_match_ipv4() { let mut table = IpNetworkTable::new(); table.insert( Ipv4Network::new(Ipv4Addr::new(127, 0, 0, 0), 16).unwrap(), 1, ); let m = table.exact_match(Ipv4Network::new(Ipv4Addr::new(127, 0, 0, 0), 16).unwrap()); assert_eq!(m, Some(&1)); let m = table.exact_match(Ipv4Network::new(Ipv4Addr::new(127, 0, 0, 0), 17).unwrap()); assert_eq!(m, None); let m = table.exact_match(Ipv4Network::new(Ipv4Addr::new(127, 0, 0, 0), 15).unwrap()); assert_eq!(m, None); } #[test] fn exact_match_ipv6() { let mut table = IpNetworkTable::new(); table.insert( Ipv6Network::new(Ipv6Addr::new(1, 2, 3, 4, 5, 6, 7, 8), 127).unwrap(), 1, ); let m = table .exact_match(Ipv6Network::new(Ipv6Addr::new(1, 2, 3, 4, 5, 6, 7, 8), 127).unwrap()); assert_eq!(m, Some(&1)); let m = table .exact_match(Ipv6Network::new(Ipv6Addr::new(1, 2, 3, 4, 5, 6, 7, 8), 128).unwrap()); assert_eq!(m, None); let m = table .exact_match(Ipv6Network::new(Ipv6Addr::new(1, 2, 3, 4, 5, 6, 7, 8), 126).unwrap()); assert_eq!(m, None); } }