Accueil > > > IP CALCULATOR
IP CALCULATOR
Information sur la source
Description
Cette classe est la version PHP du programme unix ipcalc. Il permet de calculer à partir d'une adresse IP (en binaire, hexadécimal ou au format xxx.xxx.xxx.xxx (dotted quad)) et d'un masque de sous-réseau (en binaire, hexadécimal, dotted quad ou décimal) fournis en paramètres les informations suivantes : * Réseau * adresse de Broadcast * Plus petite adresse IP du sous réseau * Plus grande adresse IP du sous réseau * Adresse IP précédente * Adresse IP suivante * Réseau précédent * Réseau suivant * Nombre d'adresses IP dans le sous réseau Elle permet également de tester si une adresse fait parti du même sous réseau qu'une autre. Elle permet également la conversion des adresses d'un format à l'autre via des fonctions statiques et le calcul du réseau et de l'adresse de broadcast en statique.
Source
- Fichier de test :
- <?php
- define('NL', "\n");
- require('IP4Calc.class.php');
-
- echo('Test for 172.23.10.221/16'.NL);
- $oIP = new IP4Calc('172.23.10.221', '16');
- echo('Netmask (Quad): '.$oIP->get(IP4Calc::NETMASK, IP4Calc::QUAD_DOTTED).NL);
- echo('Netmask (Bin): '.$oIP->get(IP4Calc::NETMASK, IP4Calc::BIN).NL);
- echo('Netmask (Hex): '.$oIP->get(IP4Calc::NETMASK, IP4Calc::HEX).NL);
- echo('Network (Quad): '.$oIP->get(IP4Calc::NETWORK, IP4Calc::QUAD_DOTTED).NL);
- echo('Broadcast (Quad): '.$oIP->get(IP4Calc::BROADCAST, IP4Calc::QUAD_DOTTED).NL);
- echo('Lowest IP address of the subnet (Quad): '.$oIP->get(IP4Calc::MIN_HOST, IP4Calc::QUAD_DOTTED).NL);
- echo('Highest IP address of the subnet (Quad): '.$oIP->get(IP4Calc::MAX_HOST, IP4Calc::QUAD_DOTTED).NL);
- echo('The previous IP address (Quad): '.$oIP->get(IP4Calc::PREVIOUS_HOST, IP4Calc::QUAD_DOTTED).NL);
- echo('The next IP address (Quad): '.$oIP->get(IP4Calc::NEXT_HOST, IP4Calc::QUAD_DOTTED).NL);
- echo('The previous network with the same netmask (Quad): '.$oIP->get(IP4Calc::PREVIOUS_NETWORK, IP4Calc::QUAD_DOTTED).NL);
- echo('THe next network with the same netmask (Quad): '.$oIP->get(IP4Calc::NEXT_NETWORK, IP4Calc::QUAD_DOTTED).NL);
- echo('The number of IP address usable for hosts in this subnet: '.$oIP->count().NL);
- echo('Is 172.23.254.10 part of this subnet ? ');
- var_dump($oIP->partOf('172.23.254.10'));
- echo('Is 192.168.1.100 part of this subnet ? ');
- var_dump($oIP->partOf('192.168.1.100'));
- ?>
-
-
- Code de la classe :
- <?php
- if(!defined('IP4CALC_CLASS')) {
- define('IP4CALC_CLASS', true);
-
-
- /**
- * This class is about converting IPv4 addresses between the different format available and
- * calculating informations out of the IPv4 and netmask provided.
- * @package IP4Calc
- * @author Florian MAURY <pub[DASH]ip4calc[AT]x[DASH]cli[DOT]com>
- * @version 1.1, 08/18/09
- * @history 08/18/09 Integrating the constants in the class, adding checks for the get function
- */
- class IP4Calc {
- // Definition of constants to abstract the usage from the internal namespace
- const IP='IP';
- const NETMASK='Netmask';
- const NETWORK='Network';
- const BROADCAST='Broadcast';
- const MIN_HOST='MinHost';
- const MAX_HOST='MaxHost';
- const PREVIOUS_HOST='PrevHost';
- const NEXT_HOST='NextHost';
- const PREVIOUS_NETWORK='PrevNetwork';
- const NEXT_NETWORK='NextNetwork';
- const INT32='Int32';
- const BIN='Bin';
- const HEX='Hex';
- const QUAD_DOTTED='Quad';
- const DECIMAL='Dec';
-
- /**
- * Contains data provided, calculated and cached about the IP address used in this instance
- * @access private
- * @var Array
- */
- private $aAddresses;
-
- /**
- * @access public
- * @param string $sIP IP address in binary, hexadecimal or dotted quad format
- * @param mixed $mNetmask The netmask of the IP address in binary, hexadecimal, dotted quad or decimal (e.g. 24 for xxx.xxx.xxx.xxx/24)
- * @throws Exception If the IP address or the netmask are not provided in a valid format
- */
- public function __construct($sIP, $mNetmask) {
- $this->aAddresses = array();
- $this->aAddresses[self::IP] = array();
- $this->aAddresses[self::NETMASK] = array();
- $this->aAddresses[self::NETWORK] = array();
- $this->aAddresses[self::BROADCAST] = array();
- $this->aAddresses[self::MIN_HOST] = array();
- $this->aAddresses[self::MAX_HOST] = array();
- $this->aAddresses[self::PREVIOUS_HOST] = array();
- $this->aAddresses[self::NEXT_HOST] = array();
- $this->aAddresses[self::PREVIOUS_NETWORK] = array();
- $this->aAddresses[self::NEXT_NETWORK] = array();
-
- list($sFormat, $iIP) = $this->convertIPToInt32($sIP);
- $this->aAddresses[self::IP][$sFormat] = $sIP;
- $this->aAddresses[self::IP][self::INT32] = $iIP;
-
- // Detecting deciaml format for netmask
- if(preg_match('/^[0-9]{1,2}$/', (string) $mNetmask) > 0 && (integer) $mNetmask <= 32) {
- $this->aAddresses[self::NETMASK][self::DECIMAL] = (integer) $mNetmask;
- $this->aAddresses[self::NETMASK][self::INT32] = self::convertDecToInt32((integer) $mNetmask);
- }
- // Detecting Quad dotted format for netmask
- elseif(preg_match('/^((?:1?[0-9]{1,2})|(?:2[0-4][0-9])|(?:25[0-5]))\.((?:1?[0-9]{1,2})|(?:2[0-4][0-9])|(?:25[0-5]))\.((?:1?[0-9]{1,2})|(?:2[0-4][0-9])|(?:25[0-5]))\.((?:1?[0-9]{1,2})|(?:2[0-4][0-9])|(?:25[0-5]))$/', (string)$mNetmask, $aQuad) > 0) {
- $this->aAddresses[self::NETMASK][self::QUAD_DOTTED] = $mNetmask;
- array_shift($aQuad); // Removing the first field containing the whole string
- $this->aAddresses[self::NETMASK][self::INT32] = self::convertQuadToInt32($aQuad);
- }
- // Detecting hexadecimal format for netmask
- elseif(preg_match('/^(?:Ox)?(?:([0-9a-f])[:.\-]?$)/i', (string)$mNetmask, $aHex) > 0) {
- $this->aAddresses[self::NETMASK][self::HEX] = $mNetmask;
- $this->aAddresses[self::NETMASK][self::INT32] = self::convertHexToInt32($aHex);
- }
- // Detecting binary format for netmask
- elseif(preg_match('/^[0-1]{32}$/', (string)$mNetmask) > 0) {
- $this->aAddresses[self::NETMASK][self::BIN] = $mNetmask;
- $this->aAddresses[self::NETMASK][self::INT32] = self::convertBinToInt32($mNetmask);
- }
- else {
- throw new Exception('Unknown format for this netmask parameter.');
- }
- }
-
- /**
- * Convert an IP address from the dotted quad format to an unsigned int32.
- * @access public
- * @static
- * @param mixed $mQuad The IP address to convert. Can be a string containing the dotted quad address or a 4 slots array.
- * @throws Exception the parameter is not a valid dotted quad IP address
- * @return uint32 Since PHP doesn't handle unsigned int, it's a float.
- */
- static public function convertQuadToInt32($mQuad) {
- if(is_string($mQuad) === true) {
- // Checking the ip format + removing the eventual netmask
- if(preg_match('/^((?:1?[0-9]{1,2})|(?:2[0-4][0-9])|(?:25[0-5]))\.((?:1?[0-9]{1,2})|(?:2[0-4][0-9])|(?:25[0-5]))\.((?:1?[0-9]{1,2})|(?:2[0-4][0-9])|(?:25[0-5]))\.((?:1?[0-9]{1,2})|(?:2[0-4][0-9])|(?:25[0-5]))$/i', $mQuad, $aQuad) > 0) {
- array_shift($aQuad); // Removing the first field containing the whole string
- }
- else {
- throw new Exception('Bad format. Expect a quad dotted address.');
- }
- }
- else {
- $aQuad = $mQuad;
- }
- $iInt32Format = 0;
- for($i = 0 ; $i < 4 ; $i++) {
- $iInt32Format += (integer)$aQuad[3 - $i] * pow(2,8*$i);
- }
- return $iInt32Format;
- }
-
- /**
- * Convert an IP address from an unsigned int32 to a dotted quad.
- * @access public
- * @static
- * @param uint32 $iQuad The IP address to convert. Since PHP does'nt handle unsigned int, this is a float.
- * @return string A dotted quad address
- */
- static public function convertInt32ToQuad($iQuad) {
- $mask = pow(2, 8) - 1;
- return (string)(($iQuad / pow(2,24)) & $mask).'.'.(string)(($iQuad / pow(2,16)) & $mask).'.'.(string)(($iQuad / pow(2,8)) & $mask).'.'.(string)($iQuad & $mask);
- }
-
- /**
- * Convert an IP address from a hexadecimal expression to an unsigned int32.
- * @access public
- * @static
- * @param mixed $mHex The IP address to convert in hexadecimal. Can be a string representing the hexadecimal number or a 4 slots array.
- * @return uint32 Since PHP doesn't handle unsigned int, it's a float.
- */
- static public function convertHexToInt32($mHex) {
- $sHex='';
- if(is_array($mHex) === true) {
- $sHex = implode('', $mHex);
- }
- return hexdec($sHex);
- }
-
- /**
- * Convert an IP address from an unsigned int32 to a hexadecimal expression.
- * @access public
- * @static
- * @param uint32 $iHex The IP address to convert. Since PHP doesn't handle unsigned int, it's a float.
- * @return string The IP address in the hexadecimal format
- */
- static public function convertInt32ToHex($iHex) {
- return '0x'.dechex($iHex);
- }
-
- /**
- * Convert an IP address from binary format to an unsigned int32.
- * @access public
- * @static
- * @param string $sBin The IP address to convert. It's a 32 Bytes string of 1 and 0.
- * @return uint32 Since PHP doesn't handle unsigned int, it's a float.
- */
- static public function convertBinToInt32($sBin) {
- return bindec($sBin);
- }
-
- /**
- * Convert an IP address from an unsigned int32 to a string of 1 and 0 (binary format).
- * @access public
- * @static
- * @param uint32 $iBin The IP address to convert. Since PHP doesn't handle unsigned int, it's a float.
- * @return string It's a string of 1 and 0 representing the ip address in binary
- */
- static public function convertInt32ToBin($iBin) {
- return decbin($iBin);
- }
-
- /**
- * Convert a netmask from the short decimal format to an unsigned int32
- * @access public
- * @static
- * @param integer $iNetmask A short decimal netmask (e.g. 27 for xxx.xxx.xxx.xxx/27)
- * @return uint32 The netmask in uint32 format. Since PHP doesn't handle unsigned int32, it's a float
- */
- static public function convertDecToInt32($iNetmask) {
- $iInt32Format = 0;
- for($i = 0 ; $i < $iNetmask ; $i++) {
- $iInt32Format += pow(2,31-$i);
- }
- return $iInt32Format;
- }
-
- /**
- * Convert a netmask from an unsigned int32 to a short decimal
- * @access public
- * @static
- * @param uint32 $iNetmask The netmask in uint32 format. Since PHP doesn't handle unsigned int32, it's a float
- * @return integer A short decimal netmask (e.g. 27 for xxx.xxx.xxx.xxx/27)
- */
- static public function convertInt32ToDec($iNetmask) {
- $iDecFormat = 0;
- $bOver = false;
- for($i = 0 ; $i < 32 && $bOver === false ; $i++) {
- if((($iNetmask / pow(2,31-$i)) & 1) !== 0) {
- $iDecFormat++;
- }
- else {
- $bOver = true;
- }
- }
- return $iDecFormat;
- }
-
- /**
- * Get the network address of the given IP/Netmask couple
- * @access public
- * @static
- * @param uint32 $iIP An IP address from the subnet for which you want the network address. Since PHP doesn't handle unsigned int32, it's a float
- * @param uint32 $iNetmask The netmask of the subnet for which you want the network address. Since PHP doesn't handle unsigned int32, it's a float
- * @return uint32 The network IP address in uint32 format. Since PHP doesn't handle unsigned int32, it's a float
- */
- static public function getNetwork($iIP, $iNetmask) {
- // Little trick to solve the problem of the unability of PHP to do bitwise operation on unsigned int32.
- // I don't want to force people to use the gmp module
- $mask=pow(2,16) - 1;
- return (((($iIP / pow(2,16)) & $mask) & (($iNetmask / pow(2,16)) & $mask)) * pow(2,16)) + (($iIP & $mask) & ($iNetmask & $mask));
- }
-
- /**
- * Get the broadcast address of the given IP/Netmask couple
- * @access public
- * @static
- * @param uint32 $iIP An IP address from the subnet for which you want the broadcast address. Since PHP doesn't handle unsigned int32, it's a float
- * @param uint32 $iNetmask The netmask of the subnet for which you want the broadcast address. Since PHP doesn't handle unsigned int32, it's a float
- * @return uint32 The broadcast IP address in uint32 format. Since PHP doesn't handle unsigned int32, it's a float
- */
- static public function getBroadcast($iIP, $iNetmask) {
- return self::getNetwork($iIP,$iNetmask) + ((pow(2,32) - 1) - $iNetmask);
- }
-
- /**
- * Get the IP addresses of the different element that can be generated from the IP address given in the constructor.
- * @access public
- * @param string $sTarget The name of the element you want to get. Please, use the constants defined at the beginning of this file
- * @param string $sType The name of the return type of the element you want to get. Please, use the constants defined at the beginning of this file
- * @throws Exception If the $sTarget parameter is not a valid target
- * @return mixed The element requested in the type requested. Can be null if the element requested doesn't exist (case of previous host, previous network, next host, next network)
- */
- public function get($sTarget, $sType) {
- switch($sTarget) {
- case self::IP:
- case self::NETMASK:
- case self::NETWORK:
- case self::BROADCAST:
- case self::MIN_HOST:
- case self::MAX_HOST:
- case self::PREVIOUS_HOST:
- case self::NEXT_HOST:
- case self::PREVIOUS_NETWORK:
- case self::NEXT_NETWORK:
- break;
- default:
- throw new Exception('Unknown target.');
- break;
- }
- switch($sType) {
- case self::INT32:
- case self::QUAD_DOTTED:
- case self::HEX:
- case self::BIN:
- break;
- case self::DECIMAL:
- if($sTarget !== self::NETMASK) {
- throw new Exception('Invalid format for this target');
- }
- break;
- default:
- throw new Exception('Invalid format');
- break;
- }
-
- if(false === isset($this->aAddresses[$sTarget][$sType])) {
- if(false === isset($this->aAddresses[$sTarget][self::INT32])) {
- $this->compute($sTarget);
- }
- if($sType !== self::INT32) {
- $sFunctionName = 'convertInt32To'.$sType;
- $this->aAddresses[$sTarget][$sType] = self::$sFunctionName($this->aAddresses[$sTarget][self::INT32]);
- }
- }
- return $this->aAddresses[$sTarget][$sType];
- }
-
- /**
- * Get the number of IP addresses in the subnet of the IP provided in the constructor
- * @access public
- * @return integer The number of IP addresses in the subnet
- */
- public function count() {
- return (pow(2,32) - 1) - $this->get(self::NETMASK, self::INT32) - 1;
- }
-
- /**
- * Test if an IP address is part of the subnet of the IP address provided in the constructor
- * @access public
- * @param string $sIP The IP address to test
- * @return boolean True if the IP address provided is in the subnet, otherwise false
- */
- public function partOf($sIP) {
- list($sFormat, $iIP) = $this->convertIPToInt32($sIP);
- return ($this->get(self::MIN_HOST, self::INT32) <= $iIP && $this->get(self::MAX_HOST, self::INT32) >= $iIP);
- }
-
- /**
- * Compute the int32 format of the element requested in parameter and store it in the addresses table property of the class
- * @access private
- * @param string $sTarget The name of the element to be computed
- * @throws Exception If the $sTarget parameter is not a valid target
- */
- private function compute($sTarget) {
- switch($sTarget) {
- case self::NETWORK:
- $this->aAddresses[$sTarget][self::INT32] = self::getNetwork($this->get(self::IP, self::INT32), $this->get(self::NETMASK, self::INT32));
- break;
- case self::BROADCAST:
- $this->aAddresses[$sTarget][self::INT32] = self::getBroadcast($this->get(self::IP, self::INT32), $this->get(self::NETMASK, self::INT32));
- break;
- case self::MIN_HOST:
- $this->aAddresses[self::MIN_HOST][self::INT32] = $this->get(self::NETWORK, self::INT32) + 1;
- break;
- case self::MAX_HOST:
- $this->aAddresses[self::MAX_HOST][self::INT32] = $this->get(self::BROADCAST, self::INT32) - 1;
- break;
- case self::PREVIOUS_HOST:
- $this->aAddresses[self::PREVIOUS_HOST][self::INT32] = (($this->get(self::IP, self::INT32) - 1) === $this->get(self::NETWORK, self::INT32))?null:$this->get(self::IP, self::INT32) - 1;
- break;
- case self::NEXT_HOST:
- $this->aAddresses[self::NEXT_HOST][self::INT32] = (($this->get(self::IP, self::INT32) + 1) === $this->get(self::BROADCAST, self::INT32))?null:$this->get(self::IP, self::INT32) + 1;
- break;
- case self::PREVIOUS_NETWORK:
- $this->aAddresses[self::PREVIOUS_NETWORK][self::INT32] = ($this->get(self::NETWORK, self::INT32) === 0)?null:self::getNetwork($this->get(self::NETWORK, self::INT32) - 1, $this->get(self::NETMASK, self::INT32));
- break;
- case self::NEXT_NETWORK:
- $this->aAddresses[self::NEXT_NETWORK][self::INT32] = ($this->get(self::BROADCAST, self::INT32) === (255*pow(2,24) + 255*pow(2,16) + 255 * pow(2,8) + 255))?null:$this->get(self::BROADCAST, self::INT32) + 1;
- break;
- default:
- throw new Exception('Unknown compute target.');
- }
- }
-
- /**
- * Convert an IP address in any format into a uint32
- * @access private
- * @param string $sIP The IP address to convert
- * @throws Exception If the $sIP parameter is not a valid IP address
- * @return array(string, uint32) The format detected and the IP address in uint32 format. Since PHP doesn't handle unsigned int32, it's a float.
- */
- private function convertIPToInt32($sIP) {
- // Detecting Quad Dotted format for ip
- if(preg_match('/^((?:1?[0-9]{1,2})|(?:2[0-4][0-9])|(?:25[0-5]))\.((?:1?[0-9]{1,2})|(?:2[0-4][0-9])|(?:25[0-5]))\.((?:1?[0-9]{1,2})|(?:2[0-4][0-9])|(?:25[0-5]))\.((?:1?[0-9]{1,2})|(?:2[0-4][0-9])|(?:25[0-5]))$/', $sIP, $aQuad) > 0) {
- array_shift($aQuad); // Removing the first field containing the whole string
- $sFormat = self::QUAD_DOTTED;
- $iIP = self::convertQuadToInt32($aQuad);
- }
- // Detecting hexadecimal format for ip
- elseif(preg_match('/^(?:Ox)?(?:([0-9a-f])[:.\-]?$)/i', $sIP, $aHex) > 0) {
- array_shift($aHex); // Removing the first field containing the whole string
- $sFormat = self::HEX;
- $iIP = self::convertHexToInt32($aHex);
- }
- // Detecting binary format for ip
- elseif(preg_match('/^[0-1]{32}$/', $sIP) > 0) {
- $sFormat = self::BIN;
- $iIP = self::convertBinToInt32($sIp);
- }
- // IP is not provided in a valid format
- else{
- throw new Exception('Unknown format for the IP parameter.');
- }
- return array($sFormat, $iIP);
- }
- }
-
- }
- ?>
Fichier de test :
<?php
define('NL', "\n");
require('IP4Calc.class.php');
echo('Test for 172.23.10.221/16'.NL);
$oIP = new IP4Calc('172.23.10.221', '16');
echo('Netmask (Quad): '.$oIP->get(IP4Calc::NETMASK, IP4Calc::QUAD_DOTTED).NL);
echo('Netmask (Bin): '.$oIP->get(IP4Calc::NETMASK, IP4Calc::BIN).NL);
echo('Netmask (Hex): '.$oIP->get(IP4Calc::NETMASK, IP4Calc::HEX).NL);
echo('Network (Quad): '.$oIP->get(IP4Calc::NETWORK, IP4Calc::QUAD_DOTTED).NL);
echo('Broadcast (Quad): '.$oIP->get(IP4Calc::BROADCAST, IP4Calc::QUAD_DOTTED).NL);
echo('Lowest IP address of the subnet (Quad): '.$oIP->get(IP4Calc::MIN_HOST, IP4Calc::QUAD_DOTTED).NL);
echo('Highest IP address of the subnet (Quad): '.$oIP->get(IP4Calc::MAX_HOST, IP4Calc::QUAD_DOTTED).NL);
echo('The previous IP address (Quad): '.$oIP->get(IP4Calc::PREVIOUS_HOST, IP4Calc::QUAD_DOTTED).NL);
echo('The next IP address (Quad): '.$oIP->get(IP4Calc::NEXT_HOST, IP4Calc::QUAD_DOTTED).NL);
echo('The previous network with the same netmask (Quad): '.$oIP->get(IP4Calc::PREVIOUS_NETWORK, IP4Calc::QUAD_DOTTED).NL);
echo('THe next network with the same netmask (Quad): '.$oIP->get(IP4Calc::NEXT_NETWORK, IP4Calc::QUAD_DOTTED).NL);
echo('The number of IP address usable for hosts in this subnet: '.$oIP->count().NL);
echo('Is 172.23.254.10 part of this subnet ? ');
var_dump($oIP->partOf('172.23.254.10'));
echo('Is 192.168.1.100 part of this subnet ? ');
var_dump($oIP->partOf('192.168.1.100'));
?>
Code de la classe :
<?php
if(!defined('IP4CALC_CLASS')) {
define('IP4CALC_CLASS', true);
/**
* This class is about converting IPv4 addresses between the different format available and
* calculating informations out of the IPv4 and netmask provided.
* @package IP4Calc
* @author Florian MAURY <pub[DASH]ip4calc[AT]x[DASH]cli[DOT]com>
* @version 1.1, 08/18/09
* @history 08/18/09 Integrating the constants in the class, adding checks for the get function
*/
class IP4Calc {
// Definition of constants to abstract the usage from the internal namespace
const IP='IP';
const NETMASK='Netmask';
const NETWORK='Network';
const BROADCAST='Broadcast';
const MIN_HOST='MinHost';
const MAX_HOST='MaxHost';
const PREVIOUS_HOST='PrevHost';
const NEXT_HOST='NextHost';
const PREVIOUS_NETWORK='PrevNetwork';
const NEXT_NETWORK='NextNetwork';
const INT32='Int32';
const BIN='Bin';
const HEX='Hex';
const QUAD_DOTTED='Quad';
const DECIMAL='Dec';
/**
* Contains data provided, calculated and cached about the IP address used in this instance
* @access private
* @var Array
*/
private $aAddresses;
/**
* @access public
* @param string $sIP IP address in binary, hexadecimal or dotted quad format
* @param mixed $mNetmask The netmask of the IP address in binary, hexadecimal, dotted quad or decimal (e.g. 24 for xxx.xxx.xxx.xxx/24)
* @throws Exception If the IP address or the netmask are not provided in a valid format
*/
public function __construct($sIP, $mNetmask) {
$this->aAddresses = array();
$this->aAddresses[self::IP] = array();
$this->aAddresses[self::NETMASK] = array();
$this->aAddresses[self::NETWORK] = array();
$this->aAddresses[self::BROADCAST] = array();
$this->aAddresses[self::MIN_HOST] = array();
$this->aAddresses[self::MAX_HOST] = array();
$this->aAddresses[self::PREVIOUS_HOST] = array();
$this->aAddresses[self::NEXT_HOST] = array();
$this->aAddresses[self::PREVIOUS_NETWORK] = array();
$this->aAddresses[self::NEXT_NETWORK] = array();
list($sFormat, $iIP) = $this->convertIPToInt32($sIP);
$this->aAddresses[self::IP][$sFormat] = $sIP;
$this->aAddresses[self::IP][self::INT32] = $iIP;
// Detecting deciaml format for netmask
if(preg_match('/^[0-9]{1,2}$/', (string) $mNetmask) > 0 && (integer) $mNetmask <= 32) {
$this->aAddresses[self::NETMASK][self::DECIMAL] = (integer) $mNetmask;
$this->aAddresses[self::NETMASK][self::INT32] = self::convertDecToInt32((integer) $mNetmask);
}
// Detecting Quad dotted format for netmask
elseif(preg_match('/^((?:1?[0-9]{1,2})|(?:2[0-4][0-9])|(?:25[0-5]))\.((?:1?[0-9]{1,2})|(?:2[0-4][0-9])|(?:25[0-5]))\.((?:1?[0-9]{1,2})|(?:2[0-4][0-9])|(?:25[0-5]))\.((?:1?[0-9]{1,2})|(?:2[0-4][0-9])|(?:25[0-5]))$/', (string)$mNetmask, $aQuad) > 0) {
$this->aAddresses[self::NETMASK][self::QUAD_DOTTED] = $mNetmask;
array_shift($aQuad); // Removing the first field containing the whole string
$this->aAddresses[self::NETMASK][self::INT32] = self::convertQuadToInt32($aQuad);
}
// Detecting hexadecimal format for netmask
elseif(preg_match('/^(?:Ox)?(?:([0-9a-f])[:.\-]?$)/i', (string)$mNetmask, $aHex) > 0) {
$this->aAddresses[self::NETMASK][self::HEX] = $mNetmask;
$this->aAddresses[self::NETMASK][self::INT32] = self::convertHexToInt32($aHex);
}
// Detecting binary format for netmask
elseif(preg_match('/^[0-1]{32}$/', (string)$mNetmask) > 0) {
$this->aAddresses[self::NETMASK][self::BIN] = $mNetmask;
$this->aAddresses[self::NETMASK][self::INT32] = self::convertBinToInt32($mNetmask);
}
else {
throw new Exception('Unknown format for this netmask parameter.');
}
}
/**
* Convert an IP address from the dotted quad format to an unsigned int32.
* @access public
* @static
* @param mixed $mQuad The IP address to convert. Can be a string containing the dotted quad address or a 4 slots array.
* @throws Exception the parameter is not a valid dotted quad IP address
* @return uint32 Since PHP doesn't handle unsigned int, it's a float.
*/
static public function convertQuadToInt32($mQuad) {
if(is_string($mQuad) === true) {
// Checking the ip format + removing the eventual netmask
if(preg_match('/^((?:1?[0-9]{1,2})|(?:2[0-4][0-9])|(?:25[0-5]))\.((?:1?[0-9]{1,2})|(?:2[0-4][0-9])|(?:25[0-5]))\.((?:1?[0-9]{1,2})|(?:2[0-4][0-9])|(?:25[0-5]))\.((?:1?[0-9]{1,2})|(?:2[0-4][0-9])|(?:25[0-5]))$/i', $mQuad, $aQuad) > 0) {
array_shift($aQuad); // Removing the first field containing the whole string
}
else {
throw new Exception('Bad format. Expect a quad dotted address.');
}
}
else {
$aQuad = $mQuad;
}
$iInt32Format = 0;
for($i = 0 ; $i < 4 ; $i++) {
$iInt32Format += (integer)$aQuad[3 - $i] * pow(2,8*$i);
}
return $iInt32Format;
}
/**
* Convert an IP address from an unsigned int32 to a dotted quad.
* @access public
* @static
* @param uint32 $iQuad The IP address to convert. Since PHP does'nt handle unsigned int, this is a float.
* @return string A dotted quad address
*/
static public function convertInt32ToQuad($iQuad) {
$mask = pow(2, 8) - 1;
return (string)(($iQuad / pow(2,24)) & $mask).'.'.(string)(($iQuad / pow(2,16)) & $mask).'.'.(string)(($iQuad / pow(2,8)) & $mask).'.'.(string)($iQuad & $mask);
}
/**
* Convert an IP address from a hexadecimal expression to an unsigned int32.
* @access public
* @static
* @param mixed $mHex The IP address to convert in hexadecimal. Can be a string representing the hexadecimal number or a 4 slots array.
* @return uint32 Since PHP doesn't handle unsigned int, it's a float.
*/
static public function convertHexToInt32($mHex) {
$sHex='';
if(is_array($mHex) === true) {
$sHex = implode('', $mHex);
}
return hexdec($sHex);
}
/**
* Convert an IP address from an unsigned int32 to a hexadecimal expression.
* @access public
* @static
* @param uint32 $iHex The IP address to convert. Since PHP doesn't handle unsigned int, it's a float.
* @return string The IP address in the hexadecimal format
*/
static public function convertInt32ToHex($iHex) {
return '0x'.dechex($iHex);
}
/**
* Convert an IP address from binary format to an unsigned int32.
* @access public
* @static
* @param string $sBin The IP address to convert. It's a 32 Bytes string of 1 and 0.
* @return uint32 Since PHP doesn't handle unsigned int, it's a float.
*/
static public function convertBinToInt32($sBin) {
return bindec($sBin);
}
/**
* Convert an IP address from an unsigned int32 to a string of 1 and 0 (binary format).
* @access public
* @static
* @param uint32 $iBin The IP address to convert. Since PHP doesn't handle unsigned int, it's a float.
* @return string It's a string of 1 and 0 representing the ip address in binary
*/
static public function convertInt32ToBin($iBin) {
return decbin($iBin);
}
/**
* Convert a netmask from the short decimal format to an unsigned int32
* @access public
* @static
* @param integer $iNetmask A short decimal netmask (e.g. 27 for xxx.xxx.xxx.xxx/27)
* @return uint32 The netmask in uint32 format. Since PHP doesn't handle unsigned int32, it's a float
*/
static public function convertDecToInt32($iNetmask) {
$iInt32Format = 0;
for($i = 0 ; $i < $iNetmask ; $i++) {
$iInt32Format += pow(2,31-$i);
}
return $iInt32Format;
}
/**
* Convert a netmask from an unsigned int32 to a short decimal
* @access public
* @static
* @param uint32 $iNetmask The netmask in uint32 format. Since PHP doesn't handle unsigned int32, it's a float
* @return integer A short decimal netmask (e.g. 27 for xxx.xxx.xxx.xxx/27)
*/
static public function convertInt32ToDec($iNetmask) {
$iDecFormat = 0;
$bOver = false;
for($i = 0 ; $i < 32 && $bOver === false ; $i++) {
if((($iNetmask / pow(2,31-$i)) & 1) !== 0) {
$iDecFormat++;
}
else {
$bOver = true;
}
}
return $iDecFormat;
}
/**
* Get the network address of the given IP/Netmask couple
* @access public
* @static
* @param uint32 $iIP An IP address from the subnet for which you want the network address. Since PHP doesn't handle unsigned int32, it's a float
* @param uint32 $iNetmask The netmask of the subnet for which you want the network address. Since PHP doesn't handle unsigned int32, it's a float
* @return uint32 The network IP address in uint32 format. Since PHP doesn't handle unsigned int32, it's a float
*/
static public function getNetwork($iIP, $iNetmask) {
// Little trick to solve the problem of the unability of PHP to do bitwise operation on unsigned int32.
// I don't want to force people to use the gmp module
$mask=pow(2,16) - 1;
return (((($iIP / pow(2,16)) & $mask) & (($iNetmask / pow(2,16)) & $mask)) * pow(2,16)) + (($iIP & $mask) & ($iNetmask & $mask));
}
/**
* Get the broadcast address of the given IP/Netmask couple
* @access public
* @static
* @param uint32 $iIP An IP address from the subnet for which you want the broadcast address. Since PHP doesn't handle unsigned int32, it's a float
* @param uint32 $iNetmask The netmask of the subnet for which you want the broadcast address. Since PHP doesn't handle unsigned int32, it's a float
* @return uint32 The broadcast IP address in uint32 format. Since PHP doesn't handle unsigned int32, it's a float
*/
static public function getBroadcast($iIP, $iNetmask) {
return self::getNetwork($iIP,$iNetmask) + ((pow(2,32) - 1) - $iNetmask);
}
/**
* Get the IP addresses of the different element that can be generated from the IP address given in the constructor.
* @access public
* @param string $sTarget The name of the element you want to get. Please, use the constants defined at the beginning of this file
* @param string $sType The name of the return type of the element you want to get. Please, use the constants defined at the beginning of this file
* @throws Exception If the $sTarget parameter is not a valid target
* @return mixed The element requested in the type requested. Can be null if the element requested doesn't exist (case of previous host, previous network, next host, next network)
*/
public function get($sTarget, $sType) {
switch($sTarget) {
case self::IP:
case self::NETMASK:
case self::NETWORK:
case self::BROADCAST:
case self::MIN_HOST:
case self::MAX_HOST:
case self::PREVIOUS_HOST:
case self::NEXT_HOST:
case self::PREVIOUS_NETWORK:
case self::NEXT_NETWORK:
break;
default:
throw new Exception('Unknown target.');
break;
}
switch($sType) {
case self::INT32:
case self::QUAD_DOTTED:
case self::HEX:
case self::BIN:
break;
case self::DECIMAL:
if($sTarget !== self::NETMASK) {
throw new Exception('Invalid format for this target');
}
break;
default:
throw new Exception('Invalid format');
break;
}
if(false === isset($this->aAddresses[$sTarget][$sType])) {
if(false === isset($this->aAddresses[$sTarget][self::INT32])) {
$this->compute($sTarget);
}
if($sType !== self::INT32) {
$sFunctionName = 'convertInt32To'.$sType;
$this->aAddresses[$sTarget][$sType] = self::$sFunctionName($this->aAddresses[$sTarget][self::INT32]);
}
}
return $this->aAddresses[$sTarget][$sType];
}
/**
* Get the number of IP addresses in the subnet of the IP provided in the constructor
* @access public
* @return integer The number of IP addresses in the subnet
*/
public function count() {
return (pow(2,32) - 1) - $this->get(self::NETMASK, self::INT32) - 1;
}
/**
* Test if an IP address is part of the subnet of the IP address provided in the constructor
* @access public
* @param string $sIP The IP address to test
* @return boolean True if the IP address provided is in the subnet, otherwise false
*/
public function partOf($sIP) {
list($sFormat, $iIP) = $this->convertIPToInt32($sIP);
return ($this->get(self::MIN_HOST, self::INT32) <= $iIP && $this->get(self::MAX_HOST, self::INT32) >= $iIP);
}
/**
* Compute the int32 format of the element requested in parameter and store it in the addresses table property of the class
* @access private
* @param string $sTarget The name of the element to be computed
* @throws Exception If the $sTarget parameter is not a valid target
*/
private function compute($sTarget) {
switch($sTarget) {
case self::NETWORK:
$this->aAddresses[$sTarget][self::INT32] = self::getNetwork($this->get(self::IP, self::INT32), $this->get(self::NETMASK, self::INT32));
break;
case self::BROADCAST:
$this->aAddresses[$sTarget][self::INT32] = self::getBroadcast($this->get(self::IP, self::INT32), $this->get(self::NETMASK, self::INT32));
break;
case self::MIN_HOST:
$this->aAddresses[self::MIN_HOST][self::INT32] = $this->get(self::NETWORK, self::INT32) + 1;
break;
case self::MAX_HOST:
$this->aAddresses[self::MAX_HOST][self::INT32] = $this->get(self::BROADCAST, self::INT32) - 1;
break;
case self::PREVIOUS_HOST:
$this->aAddresses[self::PREVIOUS_HOST][self::INT32] = (($this->get(self::IP, self::INT32) - 1) === $this->get(self::NETWORK, self::INT32))?null:$this->get(self::IP, self::INT32) - 1;
break;
case self::NEXT_HOST:
$this->aAddresses[self::NEXT_HOST][self::INT32] = (($this->get(self::IP, self::INT32) + 1) === $this->get(self::BROADCAST, self::INT32))?null:$this->get(self::IP, self::INT32) + 1;
break;
case self::PREVIOUS_NETWORK:
$this->aAddresses[self::PREVIOUS_NETWORK][self::INT32] = ($this->get(self::NETWORK, self::INT32) === 0)?null:self::getNetwork($this->get(self::NETWORK, self::INT32) - 1, $this->get(self::NETMASK, self::INT32));
break;
case self::NEXT_NETWORK:
$this->aAddresses[self::NEXT_NETWORK][self::INT32] = ($this->get(self::BROADCAST, self::INT32) === (255*pow(2,24) + 255*pow(2,16) + 255 * pow(2,8) + 255))?null:$this->get(self::BROADCAST, self::INT32) + 1;
break;
default:
throw new Exception('Unknown compute target.');
}
}
/**
* Convert an IP address in any format into a uint32
* @access private
* @param string $sIP The IP address to convert
* @throws Exception If the $sIP parameter is not a valid IP address
* @return array(string, uint32) The format detected and the IP address in uint32 format. Since PHP doesn't handle unsigned int32, it's a float.
*/
private function convertIPToInt32($sIP) {
// Detecting Quad Dotted format for ip
if(preg_match('/^((?:1?[0-9]{1,2})|(?:2[0-4][0-9])|(?:25[0-5]))\.((?:1?[0-9]{1,2})|(?:2[0-4][0-9])|(?:25[0-5]))\.((?:1?[0-9]{1,2})|(?:2[0-4][0-9])|(?:25[0-5]))\.((?:1?[0-9]{1,2})|(?:2[0-4][0-9])|(?:25[0-5]))$/', $sIP, $aQuad) > 0) {
array_shift($aQuad); // Removing the first field containing the whole string
$sFormat = self::QUAD_DOTTED;
$iIP = self::convertQuadToInt32($aQuad);
}
// Detecting hexadecimal format for ip
elseif(preg_match('/^(?:Ox)?(?:([0-9a-f])[:.\-]?$)/i', $sIP, $aHex) > 0) {
array_shift($aHex); // Removing the first field containing the whole string
$sFormat = self::HEX;
$iIP = self::convertHexToInt32($aHex);
}
// Detecting binary format for ip
elseif(preg_match('/^[0-1]{32}$/', $sIP) > 0) {
$sFormat = self::BIN;
$iIP = self::convertBinToInt32($sIp);
}
// IP is not provided in a valid format
else{
throw new Exception('Unknown format for the IP parameter.');
}
return array($sFormat, $iIP);
}
}
}
?>
Historique
- 10 août 2009 11:43:38 :
- Typo
- 10 août 2009 12:50:35 :
- typo sur le test (copier coller du mot (quad) sur le nombre d'IP dans un réseau)
- 10 août 2009 15:15:08 :
- Correction d'un commentaire sur function convertIPToInt32 :
Le return type n'était pas uint32 mais array(string, uint32)... Commentaire écrit peut être un peu trop vite... je ne sais pas...
- 11 août 2009 10:32:16 :
- Ajout d'un zip, car il ne semble pas pratique de récupérer le code dans la section "Source"
- 11 août 2009 10:35:19 :
- Le code dans le zip n'était pas à jour au niveau des modifs apportées directement dans la section source... désolé :/
- 11 août 2009 16:29:25 :
- Rajout d'un @throws dans les commentaires de la fonction get. En effet, get appelle compute mais propage l'exception que compute peut générer.
Techniquement count et partOf devraient aussi propager ou gérer l'exception vu qu'ils utilisent get, mais cela voudrait dire que vous avez modifié les sources et crée un bug car les types sont rentrés en "dur" dans ces fonctions et l'utilisation d'un type invalide ne devrait donc pas être possible
- 18 août 2009 19:32:46 :
- Mise à jour du code en intégrant les constantes dans la classe, et ajout de contrôle dans la fonction get
- 18 août 2009 20:56:53 :
- Update du code source de la case "source" pour refléter les modifs apportées dans le zip
- 07 octobre 2009 18:07:20 :
- Erreur bête dans le constructeur causant un décalage (+8) des masques lorsqu'ils sont donnés sous la forme quad dotted.
- 18 décembre 2009 08:23:59 :
- Un grand merci à lerouxju qui m'a rapporté par message privé deux bugs dans la fonction get. J'avais oublié un break, causant une exception dans certains cas valides et... j'avais utilisé une mauvaise constante, relicat de la version sans constantes de classe, ce qui obfusquait le premier bug.
Je pense encore au support des /31 et /32 !
Sources de la même categorie
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
Conversion BMP vers JPG [ par Manson ]
Bonjour a tous,voilà j'aimerai savoir comment l'on peut faire pour convertir un BMP vers un JPG, car je ne trouve aucune fonction le permettant, alors
HELP! conversion de variables [ par eax ]
comment fait-on pour convertir une variable, par exemple:$toto=12,12,35,25250,251,1,0,2pour l'enregistrer dans un seul champ d'une base de données mys
conversion de date [ par Joez ]
voila je récupère la date de mysql sous la forme 2002-01-02 10:43:32 et je voudrais afficher cette date sous la forme Lundi 02 janvier 2002 à 10h4
probleme conversion HTML -> Texte -> HTML [ par Cho7Kipu ]
Coucou tt le monde !Bon alors j'explik mon probleme :J'ai fait un site de partition. Pour que mon moteur puisse rechercher des mots contenu dans une d
conversion d'un script js à php [ par pyranhaz ]
Salut,comment convertir ce script javascript en php ???<script language="Javascript"><!--ID=window.setTimeout("window.location='htt
Conversion de caractères issus de dbase.comment faire? [ par asterixobelix ]
<?php$base="eleves.dbf";$dbh =dbase_open($base,0);$nb_eleves=dbase_numrecords($dbh);print("$nb_eleves <BR>");$nb_champs=dbase_numfields($dbh)
conversion code c++ vers PHP [ par karolina64 ]
Bonjour,j'ai le code suivant écrit en C++ que je veux traduire en PHP :Maxord = 12;*snorm = 1.0;for (n=1; n<=maxord; n++) {*(snorm+n) = *(snorm+n-1
conversion fichier .doc en .html [ par thoru ]
Bonjour!Je débute en php et je ne sais pas s'il existe une possibilité de convertir un fichier .doc en fichier.html.Je vous explique je dois prendre d
convertisseur monétaire [ par attentio ]
salut a tous !j'aimerais savoir si il existe des scripts tout fait qui permettent de faire la conversion euro/dollar selon le cour de la bourse. En f
Tags Word - Conversion [ par jdaviaud ]
Bonjour a tous,J'essaye desespérément de faire une interface d'import de fichiers texte pour les convertir ensuite en fichier HTML, tout fonctionne im
|
Derniers Blogs
IMAGINE CUP 2012, MAKE A SIGN EN FINALEIMAGINE CUP 2012, MAKE A SIGN EN FINALE par junarnoalg
Voilà qui est fait, la nouvelle est officielle ! L'équipe belge "Make a Sign" va au pays des kangourous défendre son projet dans la catégorie Software Design. http://www.imaginecup.com/CompetitionsContent/Competition/WorldwideFinalists.aspx V...
Cliquez pour lire la suite de l'article par junarnoalg KINECT 1.5 IS OUT !KINECT 1.5 IS OUT ! par Vko
La version 1.5 du Kinect For Microsoft vient tout juste de sortir ! Plein de nouveautés: Tracking de squelette en Near Mode Détection en position assise Détection faciale avec un SDK dédié Documentation et des guideline (enfin) Un out...
Cliquez pour lire la suite de l'article par Vko LES ACTUALITéS DE LA SEMAINE SUR C2I.FR (14 MAI - 20 MAI) LES ACTUALITéS DE LA SEMAINE SUR C2I.FR (14 MAI - 20 MAI) par richardc
Mise à jour des Web API du 14 Mai
Réservez dès maintenant votre journée du 20 juin pour le Windows Azure Dev Camp 2012 à Paris
Mise à jour de Team Foundation Service
MechCommander 2 sur Windows 8
Entity Framework 5 Release Candidate e...
Cliquez pour lire la suite de l'article par richardc REACTIVE EXTENSIONS : CONSOMMER DES SERVICES AVEC RX PARTIE 3, LES PIèGES à éVITERREACTIVE EXTENSIONS : CONSOMMER DES SERVICES AVEC RX PARTIE 3, LES PIèGES à éVITER par Groc
Une mauvaise utilisation de rx lors de l'écriture d'une couche d'accès à des services peut conduire à des cas embarassants avec des erreurs mal gérées, des appels qui ne partent lorsqu'ils le devraient, et même des résultats incorrects . le tout nuis...
Cliquez pour lire la suite de l'article par Groc SHAREPOINT BLOG SITE, PROBLèME D'ARCHIVESSHAREPOINT BLOG SITE, PROBLèME D'ARCHIVES par junarnoalg
Dernièrement, nous avons migré le site
myTIC
vers un nouveau serveur SharePoint 2010. Dans les contenus que nous vouloins récupérer, nous avions un certain nombre de blogs.
Nous avons utilisé les commandes Power...
Cliquez pour lire la suite de l'article par junarnoalg
Forum
GOOGLE MAPGOOGLE MAP par fatmanajjar
Cliquez pour lire la suite par fatmanajjar
Logiciels
sDEVIS-FACTURES vlPRO (8.1.0.3)SDEVIS-FACTURES VLPRO (8.1.0.3)sDEVIS-FACTURES vlPRO a été mis au point pour les particuliers, créateurs, entrepreneurs, artisa... Cliquez pour télécharger sDEVIS-FACTURES vlPRO 974 Application Server (12.2.4.6)974 APPLICATION SERVER (12.2.4.6)Développez de puissantes applications dans un environnement de 'cloud computing', clusterisé, séc... Cliquez pour télécharger 974 Application Server vPicture (1.4.2.1)VPICTURE (1.4.2.1)Avec vPicture, hébergez vos images facilement et rapidement.
vPicture est un utilitaire simple, ... Cliquez pour télécharger vPicture Easy-Planning (2.2.1.6)EASY-PLANNING (2.2.1.6)Easy-Planning permet de créer des plannings sous la représentation de diagrammes et est adapté au... Cliquez pour télécharger Easy-Planning COM-BACKUP (2.0)COM-BACKUP (2.0)
COM-BACKUP est un logiciel de sauvegarde qui permet de planifier les sauvegardes de vos dossiers ...
Cliquez pour télécharger COM-BACKUP
|