Skip to main content

parse_ipv4_bytes

Function parse_ipv4_bytes 

Source
pub fn parse_ipv4_bytes(bytes: &[u8]) -> Option<Ipv4Addr>
Expand description

Parse an IPv4 address from a byte slice.

Performs strict validation of dotted-quad notation (e.g., 192.168.1.1). Rejects:

  • Octet values > 255
  • Leading zeros (e.g., 192.168.001.1)
  • Invalid formats

§Arguments

  • bytes - A byte slice containing a potential IPv4 address (7-15 bytes)

§Returns

Some(Ipv4Addr) if the bytes represent a valid IPv4 address, None otherwise.

§Example

use ip_extract::parse_ipv4_bytes;

assert_eq!(parse_ipv4_bytes(b"192.168.1.1"), Some("192.168.1.1".parse().unwrap()));
assert_eq!(parse_ipv4_bytes(b"256.1.1.1"), None);  // Out of range
assert_eq!(parse_ipv4_bytes(b"192.168.01.1"), None);  // Leading zero