From 9324623b8e1e672adb3268eb09b7ff328cb8cc71 Mon Sep 17 00:00:00 2001 From: Adam Amstrong Date: Mon, 3 Nov 2008 15:34:49 +0000 Subject: [PATCH] ipv6 things git-svn-id: http://www.observium.org/svn/observer/trunk@269 61d68cd4-352d-0410-923a-c4978735b2b8 --- contrib/generate-dns.php | 4 +- includes/functions.php | 3 +- includes/ipv6-functions.php | 286 ++++++++++++++++++++++++++++++++++++ ipv6.php | 24 +-- 4 files changed, 291 insertions(+), 26 deletions(-) create mode 100644 includes/ipv6-functions.php diff --git a/contrib/generate-dns.php b/contrib/generate-dns.php index 96b810653..d56b044ce 100755 --- a/contrib/generate-dns.php +++ b/contrib/generate-dns.php @@ -5,8 +5,8 @@ include("config.php"); include("includes/functions.php"); -$dnsdblink = mysql_connect('localhost', 'adama', 'balr0g'); -$dnsdb = mysql_select_db('vostron_vegadns', $dnsdblink); +$dnsdblink = mysql_connect('localhost', 'user', 'pass'); +$dnsdb = mysql_select_db('tinydns', $dnsdblink); $link = mysql_connect($config['db_host'], $config['db_user'], $config['db_pass']); $db = mysql_select_db($config['db_name'], $link); diff --git a/includes/functions.php b/includes/functions.php index 01b00e049..f4e34d01d 100755 --- a/includes/functions.php +++ b/includes/functions.php @@ -18,6 +18,7 @@ include_once($config['install_dir'] . "/includes/print-functions.php"); include_once($config['install_dir'] . "/includes/billing-functions.php"); include_once($config['install_dir'] . "/includes/cisco-entities.php"); include_once($config['install_dir'] . "/includes/syslog.php"); +#include_once($config['install_dir'] . "/includes/ipv6-functions.php"); function mres($string) { // short function wrapper because the real one is stupidly long and ugly. aestetics. @@ -26,8 +27,6 @@ function mres($string) { function validate_hostip($host) { - - } function write_dev_attrib($device_id, $attrib_type, $attrib_value) { diff --git a/includes/ipv6-functions.php b/includes/ipv6-functions.php new file mode 100644 index 000000000..5671d85ff --- /dev/null +++ b/includes/ipv6-functions.php @@ -0,0 +1,286 @@ +0) { + $match = ''; + foreach($zeros[0] as $zero) { + if (strlen($zero) > strlen($match)) + $match = $zero; + } + $cip = preg_replace('/' . $match . '/', ':', $cip, 1); + } + $cip = preg_replace('/((^:)|(:$))/', '' ,$cip); + $cip = preg_replace('/((^:)|(:$))/', '::' ,$cip); + } + if('' != $netmask) { + $cip = $cip.'/'.$netmask; + } + return $cip; + } + + function SplitV64($ip) { + $ip = Net_IPv6::removeNetmaskSpec($ip); + $ip = Net_IPv6::Uncompress($ip); + if (strstr($ip, '.')) { + $pos = strrpos($ip, ':'); + $ip{$pos} = '_'; + $ipPart = explode('_', $ip); + return $ipPart; + } else { + return array($ip, ""); + } + } + + function checkIPv6($ip) { + $ip = Net_IPv6::removeNetmaskSpec($ip); + $ipPart = Net_IPv6::SplitV64($ip); + $count = 0; + if (!empty($ipPart[0])) { + $ipv6 =explode(':', $ipPart[0]); + for ($i = 0; $i < count($ipv6); $i++) { + $dec = hexdec($ipv6[$i]); + $hex = strtoupper(preg_replace("/^[0]{1,3}(.*[0-9a-fA-F])$/", "\\1", $ipv6[$i])); + if ($ipv6[$i] >= 0 && $dec <= 65535 && $hex == strtoupper(dechex($dec))) { + $count++; + } + } + if (8 == $count) { + return true; + } elseif (6 == $count and !empty($ipPart[1])) { + $ipv4 = explode('.',$ipPart[1]); + $count = 0; + for ($i = 0; $i < count($ipv4); $i++) { + if ($ipv4[$i] >= 0 && (integer)$ipv4[$i] <= 255 && preg_match("/^\d{1,3}$/", $ipv4[$i])) { + $count++; + } + } + if (4 == $count) { + return true; + } + } else { + return false; + } + + } else { + return false; + } + } + + function _ip2Bin($ip) { + $binstr = ''; + $ip = Net_IPv6::removeNetmaskSpec($ip); + $ip = Net_IPv6::Uncompress($ip); + $parts = explode(':', $ip); + foreach($parts as $v) { + $str = base_convert($v, 16, 2); + $binstr .= str_pad($str, 16, '0', STR_PAD_LEFT); + } + return $binstr; + } + + function _bin2Ip($bin) { + $ip = ""; + if(strlen($bin)<128) { + $bin = str_pad($str, 128, '0', STR_PAD_LEFT); + } + $parts = str_split($bin, "16"); + foreach($parts as $v) { + $str = base_convert($v, 2, 16); + $ip .= $str.":"; + } + $ip = substr($ip, 0,-1); + return $ip; + } + +} + +?> diff --git a/ipv6.php b/ipv6.php index 86c0bea93..06264d1e0 100755 --- a/ipv6.php +++ b/ipv6.php @@ -9,13 +9,11 @@ while ($device = mysql_fetch_array($q)) { echo("\n" . $device['hostname'] . " : "); - $oids = shell_exec("snmpwalk -v2c -c ".$device['community']." ".$device['hostname']." ipAddressIfIndex.ipv6 -Osq"); + $oids = shell_exec($config['snmpwalk']." -".$device['snmpver']." -c ".$device['community']." ".$device['hostname']." ipAddressIfIndex.ipv6 -Osq"); $oids = str_replace("ipAddressIfIndex.ipv6.", "", $oids); $oids = str_replace("\"", "", $oids); $oids = trim($oids); - foreach(explode("\n", $oids) as $data) { $data = trim($data); list($ipv6addr,$ifIndex) = explode(" ", $data); - $oid = ""; $sep = ''; $adsep = ''; unset($address); @@ -35,8 +33,6 @@ while ($device = mysql_fetch_array($q)) { $network = trim(shell_exec($config['sipcalc']." $address/$cidr | grep Subnet | cut -f 2 -d '-'")); $comp = trim(shell_exec($config['sipcalc']." $address/$cidr | grep Compressed | cut -f 2 -d '-'")); - $valid_ips[] = $address . " " . $ifIndex; - if (mysql_result(mysql_query("SELECT count(*) FROM `interfaces` WHERE device_id = '".$device['device_id']."' AND `ifIndex` = '$ifIndex'"), 0) != '0' && $cidr > '0' && $cidr < '129' && $comp != '::1') { $i_query = "SELECT interface_id FROM `interfaces` WHERE device_id = '".$device['device_id']."' AND `ifIndex` = '$ifIndex'"; $interface_id = mysql_result(mysql_query($i_query), 0); @@ -53,23 +49,7 @@ while ($device = mysql_fetch_array($q)) { mysql_query("INSERT INTO `ip6adjacencies` (`network_id`, `interface_id`) VALUES ('$network_id', '$interface_id')"); echo("A"); } - } else { echo("."); } - + } else { echo("."); } } - - $sql = "SELECT * FROM ip6addr AS A, interfaces AS I WHERE A.interface_id = I.interface_id AND I.device_id = '".$device['device_id']."'"; - $data = mysql_query($sql); - while($row = mysql_fetch_array($data)) { - unset($valid); - foreach($valid_ips as $valid_ip) { - if($row['addr'] . " " . $row['ifIndex'] == $valid_ip) { $valid = 1; } - } - if(!$valid) { echo("-"); mysql_query("DELETE FROM ip6addr WHERE ip6addr_id = '".$row['ip6addr_id']."'");} - } - - unset($valid_ips); - - echo("\n"); - } ?>