Source for file IPCALC.php
Documentation is available at IPCALC.php
* Project: IPCALCLogic :: The internal API about IPCALC class
* File: IPCALC/IPCALC.php
* This class is subpackage of IPCALC class, and support various
* @subpackage IPCALCLogic
* @author JoungKyun.Kim <http://oops.org>
* @copyright (c) 2018, JoungKyun.Kim
* This class is subpackage of IPCALC class, and support various
// {{{ (long) IPCALCLogic::signed_casting ($v)
* Convert unsigned long to signed long
* @return long signed long value
* @param long unsigned long value
private static function signed_casting (&$v) {
// {{{ (long) IPCALCLogic::not_operand ($v)
* @param logn decimical value
private static function not_operand ($v) {
for ( $i= 0; $i< $l; $i++ )
return $z ? bindec ($z) : false;
// {{{ (long) IPCALCLogic::ip2long ($ip)
* Return unsigned proper address about given dotted ipv4 address
* ip2long API of PHP is returnd signed long value, but this api
* is returned unsigned long value.
* @return long proper address of long type
* @param string dotted ipv4 address
// {{{ (boolean) IPCALCLogic::valid_ipv4_addr ($ip)
* Check given adddress is valid or not
* @param string dotted ipv4 address
$p = self::ip2long ($ip);
if ( ! $p || ! preg_match ('/[.]/', $ip) )
if ( (int) $p < 16777216 )
// {{{ (long) IPCALCLogic::prefix2long ($prefix)
* Convert network prefix to long type network mask
* @return long long type network maks or false
* @param int decimical network prefix
if ( (int) $prefix > 32 || (int) $prefix < 0 )
$prefix = 32 - (int) $prefix;
// {{{ (int) IPCALCLogic::long2prefix ($mask)
* Convert long type network mask to decimical network prefix
* @return int Decimical netowrk prefix
* @param long long type network mask
self::signed_casting ($mask);
for ( $i = 0; $i < 32; $i++ ) {
if ( !((int) $mask & ((2 << $i) - 1)) )
// {{{ (long) IPCALCLogic::network ($ip, $mask)
* Get long type network address for given ip address
* @param string dotted ipv4 address or long proper address
* @param string dotted network mask or network prefix
static function network ($ip, $mask) {
self::signed_casting ($ip);
self::signed_casting ($mask);
if ( (int) $mask >= 0 && (int) $mask < 33 )
$mask = self::prefix2long ($mask);
return (int) $ip & (int) $mask;
// {{{ (long) IPCALCLogic::broadcast ($ip, $mask)
* Get long type broadcast address for given ip address
* @param string dotted ipv4 address or long proper address
* @param string dotted network mask or network prefix
self::signed_casting ($ip);
self::signed_casting ($mask);
if ( (int) $mask >= 0 && (int) $mask < 33 )
$mask = self::prefix2long ((int) $mask);
$r = ((int) $ip & (int) $mask) | self::not_operand ($mask);
// {{{ (int) IPCALCLogic::guess_prefix ($start, $end)
* Get decimical network prefix about given start and end ip address
* @return int Decimical network prefix
* @param string Dotted ipv4 address or long proper address
* @param string Dotted ipv4 address or long proper address
self::signed_casting ($start);
self::signed_casting ($end);
while ( ! ($n & 0x80000000) ) {
while ( (int) $end > (int) $n ) {
$n = sprintf ('%u', self::broadcast ($start, $prefix));
// {{{ (long) IPCALCLogic::guess_netmask ($start, $end)
* Get long type network mask about given start and end ip address
* @return string Dotted IPv4 address
* @param string Dotted ipv4 address or long proper address
* @param string Dotted ipv4 address or long proper address
$r = self::prefix2long (self::guess_prefix ($start, $end));
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
|