#ip-address #ipv6 #ip #ipv4 #bogus

bogon

A Rust library to check if an IP address is a bogon

1 unstable release

new 0.1.0 Apr 30, 2024

#16 in #ipv6

MIT license

19KB
229 lines

Bogon

Rust

Bogon is a Rust library for checking whether an IP address is considered "bogus" or "bogon", meaning it's not valid for use on the public internet. This includes private IP addresses, loopback addresses, and other reserved addresses.

Features

  • Supports both IPv4 and IPv6 addresses.
  • Includes methods for checking bogus IP addresses using extension traits.
  • Strives to be as fast as possible. The compiler generates SIMD instructions for both IPv4 and IPv6 address checks.
  • IPv6 ranges are generated at build time from the IANA reserved address registry.

Examples

use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use bogon::is_bogon_str;

assert_eq!(bogon::is_bogon_str("127.0.0.1"), Ok(true));
assert_eq!(bogon::is_bogon_str("8.8.8.8"), Ok(false));
assert_eq!(bogon::is_bogon_str("::1"), Ok(true));
assert!(bogon::is_bogon_str("foo").is_err());

assert_eq!(bogon::is_bogon_v4(Ipv4Addr::new(127, 0, 0, 1)), true);
assert_eq!(bogon::is_bogon_v4(Ipv4Addr::new(8, 8, 8, 8)), false);

assert_eq!(bogon::is_bogon_v6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), true);

assert_eq!(bogon::is_bogon(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1))), true);
assert_eq!(bogon::is_bogon(IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8))), false);
assert_eq!(bogon::is_bogon(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1))), true);

Dependencies

~0.6–1.3MB
~28K SLoC