GitHubContribute in GitHub: Edit online

ipv4_is_private()

Checks if IPv4 string address belongs to a set of private network IPs.

Private network addresses were originally defined to assist in delaying IPv4 address exhaustion. IP packets originating from or addressed to a private IP address cannot be routed through the public internet.

Private IPv4 addresses

The Internet Engineering Task Force (IETF) has directed the Internet Assigned Numbers Authority (IANA) to reserve the following IPv4 address ranges for private networks:

IP address range Number of addresses Largest CIDR block (subnet mask)
10.0.0.0 – 10.255.255.255 16777216 10.0.0.0/8 (255.0.0.0)
172.16.0.0 – 172.31.255.255 1048576 172.16.0.0/12 (255.240.0.0)
192.168.0.0 – 192.168.255.255 65536 192.168.0.0/16 (255.255.0.0)
ipv4_is_private('192.168.1.1/24') == true
ipv4_is_private('10.1.2.3/24') == true
ipv4_is_private('9.9.9.9') == false
ipv4_is_private("127.0.0.1") == false

Syntax

ipv4_is_private(Expr)

Arguments

Name Type Required Description
Expr String A string expression representing an IPv4 address. IPv4 strings can be masked using IP-prefix notation.

IP-prefix notation

IP addresses can be defined with IP-prefix notation using a slash (/) character. The IP address to the left of the slash (/) is the base IP address. The number (1 to 32) to the right of the slash (/) is the number of contiguous 1 bit in the netmask.

For example, 192.168.2.0/24 will have an associated net/subnetmask containing 24 contiguous bits or 255.255.255.0 in dotted decimal format.

Returns

  • true: If the IPv4 address belongs to any of the private network ranges.
  • false: Otherwise.
  • null: If parsing of the input as IPv4 address string wasn't successful.

Example

Check if IPv4 belongs to a private network

print ipv4_is_private('9.9.9.9') == true // returns false

Results

print_0
0

print ipv4_is_private('9.9.9.9') == true // returns false

Results

print_0
0