From 41b1eca7a62d450bc0d2357027a6be6acd51a7a6 Mon Sep 17 00:00:00 2001 From: Joachim Tingvold Date: Wed, 1 Apr 2015 03:49:53 +0200 Subject: Changes... --- fap/tools/tools_includes/ipcalc_functions.php | 134 ++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 fap/tools/tools_includes/ipcalc_functions.php (limited to 'fap/tools/tools_includes/ipcalc_functions.php') diff --git a/fap/tools/tools_includes/ipcalc_functions.php b/fap/tools/tools_includes/ipcalc_functions.php new file mode 100644 index 0000000..e848ef1 --- /dev/null +++ b/fap/tools/tools_includes/ipcalc_functions.php @@ -0,0 +1,134 @@ + 255 ) { + $newValue = $newValue - 256; + $carry = 1; + } else { + $carry = 0; + } + array_unshift($result,$newValue); + } + return $result; + } + + + /* Useful Functions */ + function _cdr2Bin ($cdrin,$len=4){ + if ( $len > 4 || $cdrin > 32 ) { // Are we ipv6? + return str_pad(str_pad("", $cdrin, "1"), 128, "0"); + } else { + return str_pad(str_pad("", $cdrin, "1"), 32, "0"); + } + } + + + function _bin2Cdr ($binin){ + return strlen(rtrim($binin,"0")); + } + + + function _cdr2Char ($cdrin,$len=4){ + $hex = _bin2Hex(_cdr2Bin($cdrin,$len)); + return _hex2Char($hex); + } + + + function _char2Cdr ($char){ + $bin = _hex2Bin(_char2Hex($char)); + return _bin2Cdr($bin); + } + + + function _hex2Char($hex){ + return pack('H*',$hex); + } + + + function _char2Hex($char){ + $hex = unpack('H*',$char); + return array_pop($hex); + } + + + function _hex2Bin($hex){ + $bin=''; + for($i=0;$i=0;$i-=4) + $hex.=dechex(bindec(substr($bin,$i,4))); + return strrev($hex); + } + + /* + Converts a v4/v6 subnet to the first usable IP + */ + function net_to_gw($net){ + $maxSubNets = '2048'; // Stop memory leak from invalid input or large ranges + $charHost = inet_pton(strtok($net, '/')); + $charMask = _cdr2Char(strtok('/'),strlen($charHost)); + $charHostMask = substr(_cdr2Char(127),-strlen($charHost)); + $charNet = $charHost & $charMask; // Supernet network address + $charHostMin = $charNet | ~$charHostMask; + return inet_ntop($charHostMin); + } + + + + + + + + +/* + + $maxSubNets = '2048'; // Stop memory leak from invalid input or large ranges + $superNet = '2a02:ed02:180a::13/64'; + if (ereg('/',$superNet)){ //if cidr type mask + $charHost = inet_pton(strtok($superNet, '/')); + $charMask = _cdr2Char(strtok('/'),strlen($charHost)); + } + + $charHostMask = substr(_cdr2Char(127),-strlen($charHost)); + $charNet = $charHost & $charMask; // Supernet network address + $charHostMin = $charNet | ~$charHostMask; + echo 'Første brukbare adresse i ' . $superNet . ': '; + echo inet_ntop($charHostMin); +*/ + +?> -- cgit v1.2.3