A library for manipulation of IP (IPv4/IPv6) and MAC address representations
///|
test "readme_examples" {
// IPv4 addresses
let addr = @ipaddr.ipv4(192, 168, 1, 1)
let formatted = addr.format()
if formatted != "192.168.1.1" {
fail("IPv4 formatting failed")
}
// IPv4 CIDR prefixes
let prefix = @ipaddr.ipv4_prefix(@ipaddr.ipv4(192, 168, 1, 0), 24)
let contains = prefix.contains(addr)
if !contains {
fail("Address should be contained in prefix")
}
// MAC addresses
let mac_addr = @ipaddr.mac(0x00, 0x11, 0x22, 0x33, 0x44, 0x55)
let formatted_mac = mac_addr.format()
if formatted_mac != "00:11:22:33:44:55" {
fail("MAC formatting failed")
}
}impl Show for IpAddrErrorlet addr = @ipaddr.ipv4(192, 168, 1, 1)
let _ = addr.format() // "192.168.1.1"
let localhost = @ipaddr.ipv4(127, 0, 0, 1)
let _ = localhost.format() // "127.0.0.1"let addr = @ipaddr.Ipv4::from_int(0x7F000001) // 127.0.0.1
let _ = addr.format() // "127.0.0.1"
let localhost = @ipaddr.Ipv4::from_int(0x7F000001) // 127.0.0.1
let _ = localhost.format() // "127.0.0.1"
let _ = @ipaddr.Ipv4::from_int(0) // 0.0.0.0let broadcast = @ipaddr.ipv4(255, 255, 255, 255)
let _ = broadcast.is_broadcast() // true
let not_broadcast = @ipaddr.ipv4(192, 168, 1, 255)
let _ = not_broadcast.is_broadcast() // false
let almost_broadcast = @ipaddr.ipv4(255, 255, 255, 254)
let _ = almost_broadcast.is_broadcast() // falselet localhost = @ipaddr.ipv4(127, 0, 0, 1)
let _ = localhost.is_loopback() // true
let loopback_variant = @ipaddr.ipv4(127, 1, 2, 3)
let _ = loopback_variant.is_loopback() // true
let not_loopback = @ipaddr.ipv4(192, 168, 1, 1)
let _ = not_loopback.is_loopback() // falselet multicast1 = @ipaddr.ipv4(224, 0, 0, 1)
let _ = multicast1.is_multicast() // true
let multicast2 = @ipaddr.ipv4(239, 255, 255, 255)
let _ = multicast2.is_multicast() // true
let unicast = @ipaddr.ipv4(192, 168, 1, 1)
let _ = unicast.is_multicast() // falselet private1 = @ipaddr.ipv4(10, 0, 0, 1)
let _ = private1.is_private() // true
let private2 = @ipaddr.ipv4(172, 16, 0, 1)
let _ = private2.is_private() // true
let private3 = @ipaddr.ipv4(192, 168, 1, 1)
let _ = private3.is_private() // true
let public_addr = @ipaddr.ipv4(8, 8, 8, 8)
let _ = public_addr.is_private() // falselet _ = @ipaddr.Ipv4::new(127, 0, 0, 1)
let _ = @ipaddr.Ipv4::new(192, 168, 1, 100)
let _ = @ipaddr.Ipv4::new(8, 8, 8, 8)let _ = @ipaddr.Ipv4::parse_detailed("192.168.1.1")
let _ = @ipaddr.Ipv4::parse_detailed("256.1.1.1") // Err(InvalidOctet(256))
let _ = @ipaddr.Ipv4::parse_detailed("192.168.1") // Err(InvalidFormat)let addr = @ipaddr.ipv4(192, 168, 1, 1)
let _ = addr.to_int() // 3232235777
let localhost = @ipaddr.ipv4(127, 0, 0, 1)
let _ = localhost.to_int() // 2130706433
let zero = @ipaddr.ipv4(0, 0, 0, 0)
let _ = zero.to_int() // 0let _ = @ipaddr.Ipv4::try_from_int(0x7F000001) // Some(127.0.0.1)
let _ = @ipaddr.Ipv4::try_from_int(0) // Some(0.0.0.0)
let _ = @ipaddr.Ipv4::try_from_int(-1) // None - negativeimpl Show for Ipv4Prefixlet prefix = @ipaddr.ipv4_prefix(@ipaddr.ipv4(192, 168, 1, 0), 24)
let broadcast = prefix.broadcast() // 192.168.1.255
let _ = broadcast.format() // "192.168.1.255"
let prefix16 = @ipaddr.ipv4_prefix(@ipaddr.ipv4(10, 5, 0, 0), 16)
let _ = prefix16.broadcast() // 10.5.255.255
// Special case: /0 broadcast is 255.255.255.255
let default_route = @ipaddr.ipv4_prefix(@ipaddr.ipv4(0, 0, 0, 0), 0)
let _ = default_route.broadcast() // 255.255.255.255let subnet = @ipaddr.ipv4_prefix(@ipaddr.ipv4(192, 168, 1, 0), 24)
let addr1 = @ipaddr.ipv4(192, 168, 1, 1)
let _ = subnet.contains(addr1) // true
let addr2 = @ipaddr.ipv4(192, 168, 1, 255)
let _ = subnet.contains(addr2) // true
let addr3 = @ipaddr.ipv4(192, 168, 2, 1)
let _ = subnet.contains(addr3) // false
// Special case: /0 contains all addresses
let all_networks = @ipaddr.ipv4_prefix(@ipaddr.ipv4(0, 0, 0, 0), 0)
let _ = all_networks.contains(@ipaddr.ipv4(8, 8, 8, 8)) // truelet subnet = @ipaddr.Ipv4Prefix::new(@ipaddr.Ipv4::new(192, 168, 1, 0), 24)
let _ = subnet.host_count() // 256 (254 usable)
let host_route = @ipaddr.Ipv4Prefix::new(@ipaddr.Ipv4::new(10, 0, 0, 1), 32)
let _ = host_route.host_count() // 1
let point_to_point = @ipaddr.Ipv4Prefix::new(@ipaddr.Ipv4::new(10, 0, 0, 0), 31)
let _ = point_to_point.host_count() // 2let subnet = @ipaddr.Ipv4Prefix::new(@ipaddr.Ipv4::new(192, 168, 1, 0), 24)
let mask = subnet.mask()
let _ = mask.format() // "255.255.255.0"
let host_route = @ipaddr.Ipv4Prefix::new(@ipaddr.Ipv4::new(10, 0, 0, 1), 32)
let host_mask = host_route.mask()
let _ = host_mask.format() // "255.255.255.255"
let default_route = @ipaddr.Ipv4Prefix::new(@ipaddr.Ipv4::new(0, 0, 0, 0), 0)
let default_mask = default_route.mask()
let _ = default_mask.format() // "0.0.0.0"let prefix = @ipaddr.ipv4_prefix(@ipaddr.ipv4(192, 168, 1, 100), 24)
let network = prefix.network() // 192.168.1.0
let _ = network.format() // "192.168.1.0"
let prefix16 = @ipaddr.ipv4_prefix(@ipaddr.ipv4(10, 5, 10, 20), 16)
let _ = prefix16.network() // 10.5.0.0
// Special case: /0 network is 0.0.0.0
let default_route = @ipaddr.ipv4_prefix(@ipaddr.ipv4(8, 8, 8, 8), 0)
let _ = default_route.network() // 0.0.0.0// Create a /24 subnet (255.255.255.0 netmask)
let _ = @ipaddr.Ipv4Prefix::new(@ipaddr.Ipv4::new(192, 168, 1, 0), 24)
// Create a /16 subnet (255.255.0.0 netmask)
let _ = @ipaddr.Ipv4Prefix::new(@ipaddr.Ipv4::new(10, 0, 0, 0), 16)
// Create a /32 host route (255.255.255.255 netmask)
let _ = @ipaddr.Ipv4Prefix::new(@ipaddr.Ipv4::new(8, 8, 8, 8), 32)let _ = @ipaddr.Ipv4Prefix::parse("192.168.1.0/24")
let _ = @ipaddr.Ipv4Prefix::parse("10.0.0.0/8")
let _ = @ipaddr.Ipv4Prefix::parse("invalid") // None - wrong formatlet addr = @ipaddr.mac(0x00, 0x11, 0x22, 0x33, 0x44, 0x55)
let _ = addr.format() // "00:11:22:33:44:55"
let _ = @ipaddr.mac(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF)
let broadcast = @ipaddr.mac(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF)
let _ = broadcast.format() // "ff:ff:ff:ff:ff:ff"
let zero_mac = @ipaddr.mac(0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
let _ = zero_mac.format() // "00:00:00:00:00:00"let mac_from_1 = @ipaddr.Mac::from_int(1) // 00:00:00:00:00:01
let _ = mac_from_1.format() // "00:00:00:00:00:01"
let mac_from_large = @ipaddr.Mac::from_int(0x12345678) // 00:00:12:34:56:78
let _ = mac_from_large.format() // "00:00:12:34:56:78"
// Round trip conversion
let original = @ipaddr.mac(0x00, 0x00, 0x22, 0x33, 0x44, 0x55)
let int_val = original.to_int()
let _ = @ipaddr.Mac::from_int(int_val) // Same as originallet broadcast = @ipaddr.mac(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF)
let _ = broadcast.is_broadcast() // true
let normal_mac = @ipaddr.mac(0x00, 0x11, 0x22, 0x33, 0x44, 0x55)
let _ = normal_mac.is_broadcast() // false
let almost_broadcast = @ipaddr.mac(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE)
let _ = almost_broadcast.is_broadcast() // false// Globally unique address (second bit of 0x00 is 0)
let global_mac = @ipaddr.mac(0x00, 0x11, 0x22, 0x33, 0x44, 0x55)
let _ = global_mac.is_global() // true
// Another globally unique address (0x01 = 0b00000001, second bit is 0)
let global_mac2 = @ipaddr.mac(0x01, 0x11, 0x22, 0x33, 0x44, 0x55)
let _ = global_mac2.is_global() // true
// Locally administered address (second bit of 0x02 is 1)
let local_mac = @ipaddr.mac(0x02, 0x11, 0x22, 0x33, 0x44, 0x55)
let _ = local_mac.is_global() // false// Locally administered address (second bit of 0x02 is 1)
let local_mac = @ipaddr.mac(0x02, 0x11, 0x22, 0x33, 0x44, 0x55)
let _ = local_mac.is_local() // true
// Another locally administered address (0x03 = 0b00000011)
let local_mac2 = @ipaddr.mac(0x03, 0x11, 0x22, 0x33, 0x44, 0x55)
let _ = local_mac2.is_local() // true
// Globally unique address (second bit of 0x00 is 0)
let global_mac = @ipaddr.mac(0x00, 0x11, 0x22, 0x33, 0x44, 0x55)
let _ = global_mac.is_local() // false// Multicast address (first bit of 0x01 is 1)
let _ = @ipaddr.mac(0x01, 0x00, 0x5E, 0x00, 0x00, 0x01)
let multicast = @ipaddr.mac(0x01, 0x00, 0x5E, 0x00, 0x00, 0x01)
let _ = multicast.is_multicast() // true
// Broadcast is also multicast
let _ = @ipaddr.mac(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF)
let broadcast = @ipaddr.mac(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF)
let _ = broadcast.is_multicast() // true
// Unicast address (first bit of 0x00 is 0)
let unicast = @ipaddr.mac(0x00, 0x11, 0x22, 0x33, 0x44, 0x55)
let _ = unicast.is_multicast() // false// Create a typical MAC address
let _ = @ipaddr.Mac::new(0x00, 0x11, 0x22, 0x33, 0x44, 0x55)
// Create a broadcast MAC address
let _ = @ipaddr.Mac::new(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF)
// Create a multicast MAC address (first bit of first byte is 1)
let _ = @ipaddr.Mac::new(0x01, 0x00, 0x5E, 0x00, 0x00, 0x01)let addr = @ipaddr.mac(0x00, 0x11, 0x22, 0x33, 0x44, 0x55)
let (_, _, _) = addr.oui() // (0x00, 0x11, 0x22)
// Apple's OUI is 00:17:F2
let apple_mac = @ipaddr.mac(0x00, 0x17, 0xF2, 0x12, 0x34, 0x56)
let (_, _, _) = apple_mac.oui() // (0x00, 0x17, 0xF2)
// Intel's OUI is 00:15:17
let intel_mac = @ipaddr.mac(0x00, 0x15, 0x17, 0xAB, 0xCD, 0xEF)
let (_, _, _) = intel_mac.oui() // (0x00, 0x15, 0x17)let _ = @ipaddr.Mac::parse("00:11:22:33:44:55")
let _ = @ipaddr.Mac::parse("FF-FF-FF-FF-FF-FF")
let _ = @ipaddr.Mac::parse("invalid") // None - wrong formatlet addr = @ipaddr.mac(0x00, 0x11, 0x22, 0x33, 0x44, 0x55)
let _ = addr.to_int() // Uses bytes 0x22, 0x33, 0x44, 0x55
let simple_mac = @ipaddr.mac(0x00, 0x00, 0x00, 0x00, 0x00, 0x01)
let _ = simple_mac.to_int() // 1
let zero_mac = @ipaddr.mac(0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00)
let _ = zero_mac.to_int() // 0 (ignores first two 0xFF bytes)let _ = @ipaddr.Mac::try_from_int(0x22334455) // Some(00:00:22:33:44:55)
let _ = @ipaddr.Mac::try_from_int(0) // Some(00:00:00:00:00:00)
let _ = @ipaddr.Mac::try_from_int(-1) // None - negativelet _ = @ipaddr.ipv4(127, 0, 0, 1)
let _ = @ipaddr.ipv4(192, 168, 1, 100)
let _ = @ipaddr.ipv4(8, 8, 8, 8)// Create a /24 subnet (255.255.255.0 netmask)
let _ = @ipaddr.ipv4_prefix(@ipaddr.ipv4(192, 168, 1, 0), 24)
// Create a /16 subnet (255.255.0.0 netmask)
let _ = @ipaddr.ipv4_prefix(@ipaddr.ipv4(10, 0, 0, 0), 16)
// Create a /32 host route (255.255.255.255 netmask)
let _ = @ipaddr.ipv4_prefix(@ipaddr.ipv4(8, 8, 8, 8), 32)// Create a typical MAC address
let _ = @ipaddr.mac(0x00, 0x11, 0x22, 0x33, 0x44, 0x55)
// Create a broadcast MAC address
let _ = @ipaddr.mac(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF)
// Create a multicast MAC address (first bit of first byte is 1)
let _ = @ipaddr.mac(0x01, 0x00, 0x5E, 0x00, 0x00, 0x01)A library for manipulation of IP (IPv4/IPv6) and MAC address representations