IPUtils
Parse, match, and analyze IP addresses and CIDR ranges
Loading...
Searching...
No Matches
Wikimedia\IPUtils Class Reference

Play with IP addresses and IP ranges. More...

Static Public Member Functions

static isIPAddress ( $ip)
 Determine if a string is as valid IP address or network (CIDR prefix).
 
static isIPv6 ( $ip)
 Given a string, determine if it as valid IP in IPv6 only.
 
static isIPv4 ( $ip)
 Given a string, determine if it as valid IP in IPv4 only.
 
static isValid ( $ip)
 Validate an IP address.
 
static isValidIPv4 ( $ip)
 Validate an IPv4 address.
 
static isValidIPv6 ( $ip)
 Validate an IPv6 address.
 
static isValidRange ( $ipRange)
 Validate an IP range (valid in either IPv4 OR IPv6; given with valid CIDR prefix or in explicit notation).
 
static sanitizeIP ( $ip)
 Convert an IP into a verbose, uppercase, normalized form.
 
static prettifyIP ( $ip)
 Prettify an IP for display to end users.
 
static splitHostAndPort ( $both)
 Given a host/port string, like one might find in the host part of a URL per RFC 2732, split the hostname part and the port part and return an array with an element for each.
 
static combineHostAndPort ( $host, $port, $defaultPort=false)
 Given a host name and a port, combine them into host/port string like you might find in a URL.
 
static formatHex ( $hex)
 Convert an IPv4 or IPv6 hexadecimal representation back to readable format.
 
static hexToOctet ( $ip_hex)
 Converts a hexadecimal number to an IPv6 address in octet notation.
 
static hexToQuad ( $ip_hex)
 Converts a hexadecimal number to an IPv4 address in quad-dotted notation.
 
static isPublic ( $ip)
 Determine if an IP address really is an IP address, and if it is public, i.e.
 
static toHex ( $ip)
 Return a zero-padded upper case hexadecimal representation of an IP address.
 
static parseCIDR ( $range)
 Convert a network specification in CIDR notation to an integer network and a number of bits.
 
static parseRange ( $range)
 Given a string range in a number of formats, return the start and end of the range in hexadecimal.
 
static isInRange ( $addr, $range)
 Determine if a given IPv4/IPv6 address is in a given CIDR network.
 
static isInRanges ( $ip, $ranges)
 Determines if an IP address is a list of CIDR a.b.c.d/n ranges.
 
static canonicalize ( $addr)
 Convert some unusual representations of IPv4 addresses to their canonical dotted quad representation.
 
static sanitizeRange ( $range)
 Gets rid of unneeded numbers in quad-dotted/octet IP strings For example, 127.111.113.151/24 -> 127.111.113.0/24.
 
static getSubnet ( $ip)
 Returns the subnet of a given IP.
 
static getIPsInRange ( $range)
 Return all the addresses in a given range.
 

Public Attributes

const RE_IP_BYTE = '(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|0?[0-9]?[0-9])'
 An IPv4 address is made of 4 bytes from x00 to xFF which is d0 to d255.
 
const RE_IPV6_GAP = ':(?:0+:)*(?::(?:0+:)*)?'
 For IPv6 canonicalization (NOT for strict validation; these are quite lax!)
 

Detailed Description

Play with IP addresses and IP ranges.

Member Function Documentation

◆ canonicalize()

static Wikimedia\IPUtils::canonicalize (   $addr)
static

Convert some unusual representations of IPv4 addresses to their canonical dotted quad representation.

This currently only checks a few IPV4-to-IPv6 related cases. More unusual representations may be added later.

Parameters
string$addrSomething that might be an IP address
Returns
string|null Valid IP address or null
-taint none

◆ combineHostAndPort()

static Wikimedia\IPUtils::combineHostAndPort (   $host,
  $port,
  $defaultPort = false 
)
static

Given a host name and a port, combine them into host/port string like you might find in a URL.

If the host contains a colon, wrap it in square brackets like in RFC 2732. If the port matches the default port, omit the port specification

Parameters
string$host
int$port
bool | int$defaultPort
Returns
string

◆ formatHex()

static Wikimedia\IPUtils::formatHex (   $hex)
static

Convert an IPv4 or IPv6 hexadecimal representation back to readable format.

Parameters
string$hexNumber, with "v6-" prefix if it is IPv6
Returns
string Quad-dotted (IPv4) or octet notation (IPv6)

◆ getIPsInRange()

static Wikimedia\IPUtils::getIPsInRange (   $range)
static

Return all the addresses in a given range.

This currently does not support IPv6 ranges and is limited to /16 block (65535 addresses).

Parameters
string$rangeIP ranges to get the IPs within
Returns
string[] Array of addresses in the range
Exceptions
InvalidArgumentExceptionIf input uses IPv6
InvalidArgumentExceptionIf input range is too large

◆ getSubnet()

static Wikimedia\IPUtils::getSubnet (   $ip)
static

Returns the subnet of a given IP.

Parameters
string$ip
Returns
string|false

◆ hexToOctet()

static Wikimedia\IPUtils::hexToOctet (   $ip_hex)
static

Converts a hexadecimal number to an IPv6 address in octet notation.

Parameters
string$ip_hexPure hex (no v6- prefix)
Returns
string (of format a:b:c:d:e:f:g:h)

◆ hexToQuad()

static Wikimedia\IPUtils::hexToQuad (   $ip_hex)
static

Converts a hexadecimal number to an IPv4 address in quad-dotted notation.

Parameters
string$ip_hexPure hex
Returns
string (of format a.b.c.d)

◆ isInRange()

static Wikimedia\IPUtils::isInRange (   $addr,
  $range 
)
static

Determine if a given IPv4/IPv6 address is in a given CIDR network.

Parameters
string$addrThe address to check against the given range.
string$rangeThe range to check the given address against.
Returns
bool Whether or not the given address is in the given range.
Note
This can return unexpected results for invalid arguments! Make sure you pass a valid IP address and IP range.

◆ isInRanges()

static Wikimedia\IPUtils::isInRanges (   $ip,
  $ranges 
)
static

Determines if an IP address is a list of CIDR a.b.c.d/n ranges.

Parameters
string$ipthe IP to check
array$rangesthe IP ranges, each element a range
Returns
bool true if the specified adress belongs to the specified range; otherwise, false.

◆ isIPAddress()

static Wikimedia\IPUtils::isIPAddress (   $ip)
static

Determine if a string is as valid IP address or network (CIDR prefix).

SIIT IPv4-translated addresses are rejected.

Note
canonicalize() tries to convert translated addresses to IPv4.
Parameters
string$ipPossible IP address
Returns
bool

◆ isIPv4()

static Wikimedia\IPUtils::isIPv4 (   $ip)
static

Given a string, determine if it as valid IP in IPv4 only.

Note
Unlike isValid(), this looks for networks too.
Parameters
string$ipPossible IP address
Returns
bool

◆ isIPv6()

static Wikimedia\IPUtils::isIPv6 (   $ip)
static

Given a string, determine if it as valid IP in IPv6 only.

Note
Unlike isValid(), this looks for networks too.
Parameters
string$ipPossible IP address
Returns
bool

◆ isPublic()

static Wikimedia\IPUtils::isPublic (   $ip)
static

Determine if an IP address really is an IP address, and if it is public, i.e.

not RFC 1918 or similar

Parameters
string$ip
Returns
bool

◆ isValid()

static Wikimedia\IPUtils::isValid (   $ip)
static

Validate an IP address.

Ranges are NOT considered valid. SIIT IPv4-translated addresses are rejected.

Note
canonicalize() tries to convert translated addresses to IPv4.
Parameters
string$ip
Returns
bool True if it is valid

◆ isValidIPv4()

static Wikimedia\IPUtils::isValidIPv4 (   $ip)
static

Validate an IPv4 address.

Ranges are NOT considered valid.

Parameters
string$ip
Returns
bool True if it is valid

◆ isValidIPv6()

static Wikimedia\IPUtils::isValidIPv6 (   $ip)
static

Validate an IPv6 address.

Ranges are NOT considered valid. SIIT IPv4-translated addresses are rejected.

Note
canonicalize() tries to convert translated addresses to IPv4.
Parameters
string$ip
Returns
bool True if it is valid

◆ isValidRange()

static Wikimedia\IPUtils::isValidRange (   $ipRange)
static

Validate an IP range (valid in either IPv4 OR IPv6; given with valid CIDR prefix or in explicit notation).

SIIT IPv4-translated addresses are rejected.

Note
canonicalize() tries to convert translated addresses to IPv4.
Parameters
string$ipRange
Returns
bool True if it is valid

◆ parseCIDR()

static Wikimedia\IPUtils::parseCIDR (   $range)
static

Convert a network specification in CIDR notation to an integer network and a number of bits.

Parameters
string$rangeIP with CIDR prefix
Returns
array [int|string, int]

◆ parseRange()

static Wikimedia\IPUtils::parseRange (   $range)
static

Given a string range in a number of formats, return the start and end of the range in hexadecimal.

Formats are: 1.2.3.4/24 CIDR 1.2.3.4 - 1.2.3.5 Explicit range 1.2.3.4 Single IP

2001:0db8:85a3::7344/96 CIDR 2001:0db8:85a3::7344 - 2001:0db8:85a3::7344 Explicit range 2001:0db8:85a3::7344 Single IP

Parameters
string$rangeIP range
Returns
array{string,string}|array{false,false} If the start or end of the range is invalid, then array [false, false] is returned

◆ prettifyIP()

static Wikimedia\IPUtils::prettifyIP (   $ip)
static

Prettify an IP for display to end users.

This will make it more compact and lower-case.

Parameters
string$ip
Returns
string|null

◆ sanitizeIP()

static Wikimedia\IPUtils::sanitizeIP (   $ip)
static

Convert an IP into a verbose, uppercase, normalized form.

Both IPv4 and IPv6 addresses are trimmed. Additionally, IPv6 addresses in octet notation are expanded to 8 words; IPv4 addresses have leading zeros, in each octet, removed.

Parameters
string$ipIP address in quad or octet form (CIDR or not).
Returns
string|null

◆ sanitizeRange()

static Wikimedia\IPUtils::sanitizeRange (   $range)
static

Gets rid of unneeded numbers in quad-dotted/octet IP strings For example, 127.111.113.151/24 -> 127.111.113.0/24.

Parameters
string$rangeIP address to normalize
Returns
string

◆ splitHostAndPort()

static Wikimedia\IPUtils::splitHostAndPort (   $both)
static

Given a host/port string, like one might find in the host part of a URL per RFC 2732, split the hostname part and the port part and return an array with an element for each.

If there is no port part, the array will have false in place of the port. If the string was invalid in some way, false is returned.

This was easy with IPv4 and was generally done in an ad-hoc way, but with IPv6 it's somewhat more complicated due to the need to parse the square brackets and colons.

A bare IPv6 address is accepted despite the lack of square brackets.

Parameters
string$bothThe string with the host (or IPv4/IPv6 address) and port
Returns
array|false Array normally, false on certain failures

◆ toHex()

static Wikimedia\IPUtils::toHex (   $ip)
static

Return a zero-padded upper case hexadecimal representation of an IP address.

Hexadecimal addresses are used because they can easily be extended to IPv6 support. To separate the ranges, the return value from this function for an IPv6 address will be prefixed with "v6-", a non- hexadecimal string which sorts after the IPv4 addresses.

Parameters
string$ipQuad dotted/octet IP address.
Returns
string|bool False on failure

The documentation for this class was generated from the following file: