Updated roagen script to be able to capture and use max-length value

This commit is contained in:
netravnen 2018-12-14 13:05:20 +00:00
parent 69f3c9198f
commit 52bca474a6

View file

@ -5,196 +5,231 @@ echo shell_exec("/usr/bin/git -C ../registry/ pull origin master:master 2>&1");
/* /*
* Function: * Function:
* startsWith( $string, 'word') * startsWith ($string, "word", $length)
* *
* Find lines beginning with 'word'. * Find lines beginning with "word". Optionally
* give the length of the string you are looking for.
*/ */
function startsWith($haystack, $needle) function startsWith ($haystack, $needle, $length = "0")
{ {
$length = strlen($needle); if ($length <= 0 || $length > (strlen ($needle)))
return (substr($haystack, 0, $length) === $needle); $length = strlen ($needle);
return (substr ($haystack, 0, $length) === $needle);
} }
/* /*
* Function: * Function:
* endsWith( $string, 'word') * endsWith ($string, "word")
* *
* Find lines ending with 'word'. * Find lines ending with "word".
*/ */
function endsWith($haystack, $needle) function endsWith ($haystack, $needle)
{ {
$length = strlen($needle); $length = strlen ($needle);
if ($length == 0) {
return true;
}
return (substr($haystack, -$length) === $needle); if ($length == 0)
return true;
return (substr( $haystack, -$length) === $needle);
} }
// Create empty array $roas /*
* Function:
* trim_special_chars ($string)
*
* Remove special characters.
*/
function trim_special_chars ($string)
{
return (trim ($string, " \t\n\r\0\x0B"));
}
// Define array() we are going to populate with data.
$roas = array(); $roas = array();
// Set folders we need to scan.
$files6 = scandir("../registry/data/route6/");
$files4 = scandir("../registry/data/route/");
/* /*
* *
* IPv6 route objects * IPv6
* *
*/ */
// Set directory for which all files are to be scanned $i = 0; // Counter used with tmp $raw_array.
$files = scandir('../registry/data/route6/'); $raw_array = array(); // tmp array() used for storing data to be processed
foreach ($files6 as $file)
$h = 0; // count up when reading new file
$i = 0; // used when loop through sub-arrays. I.e. when route objects have more than one listed origin asn
foreach($files as $file)
{ {
$handle = fopen("../registry/data/route6/$file", "r"); $j = 0;
while (($line = fgets($handle)) !== false) {
// extract var $ta /*
if (startsWith($line,'source')) { * route6 with maxLength value set:
$source = array(); * - fd42:5d71:219::/48
preg_match('/([A-Z0-4]+)/',explode(':', $line)[1], $source); *
$ta = $source[1]; * $ cat ../registry/data/route6/fd42:5d71:219::_48
} * route6: fd42:5d71:219::/48
// extract var $route * origin: AS4242420119
// extract var $maskLength * max-length: 48
if (startsWith($line,'route6')) { * mnt-by: JRB0001-MNT
$prefix = array(); * source: DN42
$line2 = explode('6: ', $line); */
// validate v6 netmasks with following cmd
// cmd: ls -1 ../registry/data/route6/ | egrep -o "\b\_[0-9]+\b" | sed 's/\_//' | sort -n | uniq $data = file("../registry/data/route6/$file");
preg_match('/([a-f0-9\:]{0,128})\/(32|40|4[4-9]|5[0-9]|6[0-4]|80)/', $line2[1], $prefix);
$route = $prefix[0]; foreach ($data as $str)
$mask = $prefix[2]; {
} $str = trim_special_chars ($str);
// extract var $maxLength
/*if (startsWith($line,'max-length')) { if (startsWith ($str, ("max"), 3)) $raw_array[$i]["max"] = $str;
$maxLength = array(); elseif (startsWith ($str, ("source"), 6)) $raw_array[$i]["source"] = $str;
preg_match('/([0-9]+)/',explode(':', $line)[1], $maxLength); elseif (startsWith ($str, ("route"), 5)) $raw_array[$i]["route"] = $str;
$maxLength = $maxLength[1]; elseif (startsWith ($str, ("origin"), 6)) $raw_array[$i]["asn"][$j++] = $str;
} else {
$maxLength = ''; // Catch max-length not set in route object.
}*/ if (empty ($raw_array[$i]["max"])) $raw_array[$i]["max"] = -1;
// extract var $asn }
if (startsWith($line, 'origin')) { $i++;
$asn = array(); }
preg_match('/AS[0-9]+/', explode(':', $line)[1], $asn);
if (count($asn) > 1) { $k = 0;
foreach ($asn as $key => $value) {
if (!empty($value)) { foreach ($raw_array as $sub_array)
$roas['roas'][$i]['asn'] = $asn[0]; {
$roas['roas'][$i]['prefix'] = $route; // Extract prefix and subnet size
/*if (isset($ta)) { // Match prefix sizes 29-64, 80.
if ($ta == 'DN42') { $prefix = array();
$mask = ($mask <= 64 ? '64' : $mask); preg_match("/([a-f0-9\:]{0,128})\/(29|[3-5][0-9]|6[0-4]|80)/",
if ($maxLength != '') $mask = $maxLength; explode("6: ", $sub_array["route"])[1],
} $prefix);
} else {
$ta = 'NULL'; // Extract ta information
}*/ $source = array();
$roas['roas'][$i]['maxLength'] = $mask; preg_match ("/([A-Z0-4]+)/",
$roas['roas'][$i]['ta'] = (empty($ta) ? '' : $ta); explode(":", $sub_array["source"])[1],
$i++; $source);
}
} // Try to extract max-length information
} else { $maxlength = array();
$roas['roas'][$i]['asn'] = $asn[0]; if (($sub_array["max"]) != -1)
$roas['roas'][$i]['prefix'] = $route; preg_match ("/([0-9]+)/",
/*if (isset($ta)) { explode(":", $sub_array["max"])[1],
if ($ta == 'DN42') { $maxlength);
$mask = ($mask <= 64 ? '64' : $mask);
if ($maxLength != '') $mask = $maxLength; // Store extracted values
} $_prefix = $prefix[0];
} else { $_ta = (isset ($source[0]) ? $source[0] : "");
$ta = 'NULL'; // We need to do conditional setting of maxLength to avoid errornous output.
}*/ if (($sub_array["max"]) != -1)
$roas['roas'][$i]['maxLength'] = $mask; $_maxlength = (isset ($maxlength[0]) ? $maxlength[0] : "");
$roas['roas'][$i]['ta'] = (empty($ta) ? '' : $ta); else
$i++; // Do fallback to default prefix size if max-length was not set.
} $_maxlength = $prefix[2];
}
} // Loop through each asn in single route6 object and assign
fclose($handle); // close $file when finished reading data // other values accordingly.
$h++; foreach ($sub_array["asn"] as $asn)
{
// Extract ASxxxxx from string.
preg_match ("/AS[0-9]+/", explode (":", $asn)[1], $_asn);
$roas["roas"][$k]["asn"] = $_asn[0];
$roas["roas"][$k]["prefix"] = $_prefix;
$roas["roas"][$k]["maxLength"] = $_maxlength;
$roas["roas"][$k]["ta"] = $_ta;
$k++;
}
} }
/* /*
* *
* IPv4 route objects * IPv4
* *
*/ */
// Set directory for which all files are to be scanned $i = 0; // Counter used with tmp $raw_array.
$files = scandir('../registry/data/route/'); $raw_array = array(); // tmp array() used for storing data to be processed
foreach ($files4 as $file)
foreach($files as $file)
{ {
$handle = fopen("../registry/data/route/$file", "r"); $j = 0;
while (($line = fgets($handle)) !== false) {
// extract var $ta /*
if (startsWith($line,'source')) { * route with maxLength value set:
$source = array(); * - 172.20.1.0/24
preg_match('/([A-Z0-4]+)/',explode(':', $line)[1], $source); *
$ta = $source[1]; * $ cat ../registry/data/route/172.20.1.0_24
} * route: 172.20.1.0/24
// extract var $route * origin: AS4242420119
// extract var $maskLength * max-length: 24
if (startsWith($line,'route')) { * mnt-by: JRB0001-MNT
$prefix = array(); * source: DN42
preg_match('/(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/(3[0-2]|[0-2]?[0-9])/', explode(':', $line)[1], $prefix); */
$route = $prefix[0];
$mask = $prefix[5]; $data = file("../registry/data/route/$file");
}
// extract var $maxLength foreach ($data as $str)
/*$maxLength = ''; // ensure var is null when starting new loop {
if (startsWith($line,'max-length')) { $str = trim_special_chars ($str);
$maxLength = array();
preg_match('/([0-9]+)/',explode(':', $line)[1], $maxLength); if (startsWith ($str, ("max"), 3)) $raw_array[$i]["max"] = $str;
$maxLength = $maxLength[1]; elseif (startsWith ($str, ("source"), 6)) $raw_array[$i]["source"] = $str;
} else { elseif (startsWith ($str, ("route"), 5)) $raw_array[$i]["route"] = $str;
$maxLength = ''; elseif (startsWith ($str, ("origin"), 6)) $raw_array[$i]["asn"][$j++] = $str;
}*/
// extract var $asn // Catch max-length not set in route object.
if (startsWith($line, 'origin')) { if (empty ($raw_array[$i]["max"])) $raw_array[$i]["max"] = -1;
$asn = array(); }
preg_match('/AS[0-9]+/', explode(':', $line)[1], $asn); $i++;
if (count($asn) > 1) { }
foreach ($asn as $key => $value) {
if (!empty($value)) { foreach ($raw_array as $sub_array)
$roas['roas'][$i]['asn'] = $asn[0]; {
$roas['roas'][$i]['prefix'] = $route; // Extract prefix and subnet size
/*if (isset($ta)) { // Match prefix sizes 8-32.
if ($ta == 'DN42') { $prefix = array();
$mask = ($mask <= 28 ? '28' : $mask); preg_match("/([0-9\.]{7,15})\/([8-9]|[1-2][0-9]|3[0-2])/",
if ($maxLength != '') $mask = $maxLength; explode(":", $sub_array["route"])[1],
} $prefix);
} else {
$ta = 'NULL'; // Extract ta information
}*/ $source = array();
$roas['roas'][$i]['maxLength'] = $mask; preg_match ("/([A-Z0-4]+)/",
$roas['roas'][$i]['ta'] = (empty($ta) ? '' : $ta); explode(":", $sub_array["source"])[1],
$i++; $source);
}
} // Try to extract max-length information
} else { $maxlength = array();
$roas['roas'][$i]['asn'] = $asn[0]; if (($sub_array["max"]) != -1)
$roas['roas'][$i]['prefix'] = $route; preg_match ("/([0-9]+)/",
/*if (isset($ta)) { explode(":", $sub_array["max"])[1],
if ($ta == 'DN42') { $maxlength);
$mask = ($mask <= 28 ? '28' : $mask);
if ($maxLength != '') $mask = $maxLength; // Store extracted values
} $_prefix = $prefix[0];
} else { $_ta = (isset ($source[0]) ? $source[0] : "");
$ta = 'NULL'; // We need to do conditional setting of maxLength to avoid errornous output.
}*/ if (($sub_array["max"]) != -1)
$roas['roas'][$i]['maxLength'] = $mask; $_maxlength = (isset ($maxlength[0]) ? $maxlength[0] : "");
$roas['roas'][$i]['ta'] = (empty($ta) ? '' : $ta); else
$i++; // Do fallback to default prefix size if max-length was not set.
} $_maxlength = $prefix[2];
}
} // Loop through each asn in single route6 object and assign
fclose($handle); // close $file when finished reading data // other values accordingly.
$h++; foreach ($sub_array["asn"] as $asn)
{
// Extract ASxxxxx from string.
preg_match ("/AS[0-9]+/", explode (":", $asn)[1], $_asn);
$roas["roas"][$k]["asn"] = $_asn[0];
$roas["roas"][$k]["prefix"] = $_prefix;
$roas["roas"][$k]["maxLength"] = $_maxlength;
$roas["roas"][$k]["ta"] = $_ta;
$k++;
}
} }
// Do JSON encoding before writing result to file // Do JSON encoding before writing result to file