Source for file IPv4.php
Documentation is available at IPv4.php
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Eric Kilfoil <eric@ypass.net> |
// +----------------------------------------------------------------------+
require_once("PEAR.php");
* Map of bitmasks to subnets
* This array contains every valid netmask. The index of the dot quad
* netmask value is the corresponding CIDR notation (bitmask).
$GLOBALS['Net_IPv4_Netmask_Map'] = array (
* Class to provide IPv4 calculations
* Provides methods for validating IP addresses, calculating netmasks,
* broadcast addresses, network addresses, conversion routines, etc.
* @author Eric Kilfoil <edk@ypass.net>
* Validate the syntax of the given IP adress
* Using the PHP long2ip() and ip2long() functions, convert the IP
* address from a string to a long and back. If the original still
* matches the converted IP address, it's a valid address. This
* function does not allow for IP addresses to be formatted as long
* @param string $ip IP address in the format x.x.x.x
* @return bool true if syntax is valid, otherwise false
* Validate the syntax of the given IP address (compatibility)
* This function is identical to Net_IPv4::validateIP(). It is included
* merely for compatibility reasons.
* @param string $ip IP address
* @return bool true if syntax is valid, otherwise false
* Validate the syntax of a four octet netmask
* There are 33 valid netmask values. This function will compare the
* string passed as $netmask to the predefined 33 values and return
* true or false. This is most likely much faster than performing the
* calculation to determine the validity of the netmask.
* @param string $netmask Netmask
* @return bool true if syntax is valid, otherwise false
if (! in_array($netmask, $GLOBALS['Net_IPv4_Netmask_Map'])) {
* Parse a formatted IP address
* Given a network qualified IP address, attempt to parse out the parts
* and calculate qualities of the address.
* The following formats are possible:
* [dot quad ip]/[ bitmask ]
* [dot quad ip]/[ dot quad netmask ]
* [dot quad ip]/[ hex string netmask ]
* The first would be [IP Address]/[BitMask]:
* The second would be [IP Address] [Subnet Mask in quad dot notation]:
* 192.168.0.0/255.255.0.0
* The third would be [IP Address] [Subnet Mask as Hex string]
* $cidr = '192.168.0.50/16';
* $net = Net_IPv4::parseAddress($cidr);
* echo $net->network; // 192.168.0.0
* echo $net->ip; // 192.168.0.50
* echo $net->broadcast; // 192.168.255.255
* echo $net->bitmask; // 16
* echo $net->long; // 3232235520 (long/double version of 192.168.0.50)
* echo $net->netmask; // 255.255.0.0
* @param string $ip IP address netmask combination
* @return object true if syntax is valid, otherwise false
if (! $myself->validateIP ($parts[0 ])) {
return(PEAR ::raiseError ("invalid IP address"));
// Check the style of netmask that was entered
* a hexadecimal string was entered
if (eregi("^([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$", $parts[1 ], $regs)) {
$myself->netmask = hexdec($regs[1 ]) . "." . hexdec($regs[2 ]) . "." .
* a standard dot quad netmask was entered.
} else if (strchr($parts[1 ], ".")) {
if (! $myself->validateNetmask ($parts[1 ])) {
return(PEAR ::raiseError ("invalid netmask value"));
$myself->netmask = $parts[1 ];
* a CIDR bitmask type was entered
} else if ($parts[1 ] >= 0 && $parts[1 ] <= 32 ) {
$myself->bitmask = $parts[1 ];
* Some unknown format of netmask was entered
return(PEAR ::raiseError ("invalid netmask value"));
} else if ($myself->validateIP ($address)) {
return(PEAR ::raiseError ("invalid IP address"));
* Calculates network information based on an IP address and netmask.
* Fully populates the object properties based on the IP address and
* netmask/bitmask properties. Once these two fields are populated,
* calculate() will perform calculations to determine the network and
* broadcast address of the network.
* @return mixed true if no errors occured, otherwise PEAR_Error object
$validNM = $GLOBALS['Net_IPv4_Netmask_Map'];
if (! is_a($this, "net_ipv4")) {
return(PEAR ::raiseError ("cannot calculate on uninstantiated Net_IPv4 class"));
/* Find out if we were given an ip address in dot quad notation or
* a network long ip address. Whichever was given, populate the
return(PEAR ::raiseError ("invalid IP address"));
return(PEAR ::raiseError ("ip address not specified"));
* Check to see if we were supplied with a bitmask or a netmask.
* Populate the other field as needed.
return(PEAR ::raiseError ("netmask or bitmask are required for calculation"));
$bitmask = $ipobj->bitmask;
if (PEAR ::isError ($ipobj)) {
return(PEAR ::raiseError ("IP addresses must be an understood format or a Net_IPv4 object"));
if (PEAR ::isError ($ipobj)) {
return(PEAR ::raiseError ("IP addresses must be an understood format or a Net_IPv4 object"));
if ($ipobj1->network == $ipobj2->network &&
$ipobj1->bitmask == $ipobj2->bitmask ) {
* Converts a dot-quad formmated IP address into a hexadecimal string
* Converts a hexadecimal string into a dot-quad formatted IP address
if (eregi("^([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$",
* Converts an IP address to a PHP double. Better than ip2long because
* a long in PHP is a signed integer.
* Determines whether or not the supplied IP is within the supplied network.
* This function determines whether an IP address is within a network.
* The IP address ($ip) must be supplied in dot-quad format, and the
* network ($network) may be either a string containing a CIDR
* formatted network definition, or a Net_IPv4 object.
* @param string $ip A quad-dot representation of an IP address
* @param string $network A string representing the network in CIDR format or a Net_IPv4 object.
* @return boolean true if the IP address exists within the network
if ($ip >= $net && $ip <= $bcast) {
* vim: sts=4 ts=4 sw=4 cindent fdm=marker
Documentation generated on Mon, 11 Mar 2019 10:15:59 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|