<?php
echo '<html><body>';
echo '<h1>IPv6 Address Summarization Calculator</h1>';
echo 'Instructions:<br>';
echo 'Enter a list of IPv6 addresses or prefixes you\'d like to summarize.<br>';
echo 'You can enter the addresses in compressed or full notation.<br>';
echo '<br>Example:<br>';
echo '2001:CC1E:2AB3:1A3C::<br>';
echo '2001:CC1E:2AB3:1A4D::<br>';
echo 'or<br>';
echo '2606:A700:ABC7:0001::<br>';
echo '2606:A700:ABC7:0002::<br>';
echo '2606:A700:ABC7:0003::<br><br>';
echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post">';
echo '<textarea id="ip" name="ip" rows="20" cols="70">';
if (isset($_POST['ip'])) { echo $_POST['ip']; }
echo '</textarea>';
echo '<p></p>';
echo '<input type="submit">';
echo '<input type="hidden" name="submitted" id="submitted"></input>';
echo '</form>';
echo '<p></p>';
if(isset($_POST["submitted"]) && $_POST['ip'] != "") {
$addr_count = 0;
$binary_ip = array();
$search="/^(?>(?>([a-f0-9]{1,4})(?>:(?1)){7}|(?!(?:.*[a-f0-9](?>:|$)){8,})((?1)(?>:(?1)){0,6})?::(?2)?)|(?>(?>(?1)(?>:(?1)){5}:|(?!(?:.*[a-f0-9]:){6,})(?3)?::(?>((?1)(?>:(?1)){0,4}):)?)?(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])(?>\.(?4)){3}))$/iD";
$hexarray=array('0' => '0000', '1' => '0001', '2' => '0010', '3' => '0011', '4' => '0100', '5' => '0101', '6' => '0110', '7' => '0111', '8' => '1000', '9' => '1001', 'A' => '1010', 'B' => '1011', 'C' => '1100', 'D' => '1101', 'E' => '1110', 'F' => '1111');
$result=explode(PHP_EOL,trim($_POST['ip']));
foreach ($result as $ip) {
if (strpos($ip, '::') !== false) {
$part = explode('::', $ip);
$part[0] = explode(':', $part[0]);
$missing = array();
for ($i = 0; $i < (8 - (count($part[0]) )); $i++)
array_push($missing, '0000');
$part = array_merge($part[0], $missing);
$newip = "";
$cnt=1;
foreach($part as $piece) {
if($cnt < 8) { $newip .= $piece.":"; }
if($cnt == 8) { $newip .= $piece; }
$cnt++;
}
$ip=$newip;
}
if(preg_match($search, $ip)) {
$addr_count++;
$ip = expand($ip);
$ip=str_replace(":","",$ip);
$fixed_ip_arr=str_split($ip,1);
$cnt=0;
$fullbinaryip = "";
foreach($fixed_ip_arr as $digit) {
$echoip = $hexarray[strtoupper($digit)];
$cnt++;
if($cnt % 4 == 0 && $cnt != 32) { $echoip .= ":"; }
$fullbinaryip .= $echoip;
}
array_push($binary_ip, $fullbinaryip);
} else {
echo "$ip is not in the correct format. If you pasted only a prefix, make sure you end it with ::. I'll ignore it and continue processing.<br>";
}
}
if($addr_count == 0) {
echo "<br>You didn't enter any valid addresses. Please try again.<br>";
exit;
}
echo "<br>Your addresses in binary form:<br>";
if(count($binary_ip) < 50) {
foreach($binary_ip as $value) {
echo $value."<br>";
}
} else {
echo "I don't want to spam you, so I'm not printing all the addresses!<br>";
}
$summary = _commonPrefix(str_replace(":","",$binary_ip));
echo "<br>These addresses can be summarized to: <br>";
$final_summary = rtrim(chunk_split(substr(str_replace(":","",$binary_ip['0']),0,$summary), 16, ':'),':');
echo $final_summary."/$summary<br>";
$summary_normal = explode(':', $final_summary);
$final_summary_normal = "";
foreach($summary_normal as $block) {
$block_smaller = str_split($block, 4);
foreach($block_smaller as $tiny) {
while (strlen($tiny) < 4) {
$tiny .= "0";
}
$final_summary_normal .= array_search($tiny, $hexarray);
}
}
echo "You can use this in your router summary statements:<br>";
echo rtrim(chunk_split($final_summary_normal, 4, ':'),':')."/$summary";
}
echo '</body>';
echo '</html>';
function expand($ip){
$hex = unpack("H*hex", inet_pton($ip));
$ip = substr(preg_replace("/([A-f0-9]{4})/", "$1:", $hex['hex']), 0, -1);
return $ip;
}
function _commonPrefix($array) {
if(count($array) < 2) {
if(count($array) == 0)
return false; // empty array: undefined prefix
else
return strlen($array[0]); // 1 element: trivial case
}
$len = max(array_map('strlen',$array)); // initial upper limit: max length of all strings.
$prevval = reset($array);
while(($newval = next($array)) !== FALSE) {
for($j = 0 ; $j < $len ; $j += 1)
if($newval[$j] != $prevval[$j])
$len = $j;
$prevval = $newval;
}
return $len;
}
?>
preferences:
36.56 ms | 402 KiB | 5 Q