Subversion Repositories vgwhois

Compare Revisions

No changes between revisions

Regard whitespace Rev 1 → Rev 2

/trunk/maintenance/pattern-generator/generate_ipv4
0,0 → 1,190
#!/usr/bin/php
<?php
 
#
# generic Whois - Automatic Pattern Generator: IPv4
#
# (c) 2012-2013 Daniel Marschall, ViaThinkSoft [www.viathinksoft.de]
#
# Distribution, usage etc. pp. regulated by the current version of GPL.
#
#
# Version 2013-03-08
#
 
error_reporting(E_ALL | E_NOTICE | E_STRICT | E_DEPRECATED);
 
require_once __DIR__ . '/config.inc.php';
require_once __DIR__ . '/../../shared/php_includes/common_functions.inc.php';
require_once __DIR__ . '/rirs.inc.php';
 
define('IANA_CACHE_DIR', __DIR__ . '/../.cache/pattern-generator/iana');
define('PATTERN_DIR', __DIR__ . '/../../main/pattern');
define('RIRSTATS_CACHE_DIR', __DIR__ . '/../.cache/pattern-generator/rirstats');
 
if (USE_IANA_DATA) {
echo "IPv4 Preparation: Get IANA delegation file\n";
 
$iana_root_url = 'ftp://ftp.iana.org/assignments/ipv4-address-space/ipv4-address-space.txt';
$iana_root_cache = IANA_CACHE_DIR . '/ipv4-address-space.txt';
if ((USE_CACHE) && (file_exists($iana_root_cache))) {
@mkdir(dirname($iana_root_cache), 0777, true);
$iana_root_data = file($iana_root_cache, FILE_IGNORE_NEW_LINES);
} else {
$iana_root_data = file($iana_root_url, FILE_IGNORE_NEW_LINES);
}
$iana_root_ver = trim(str_replace('-', '', $iana_root_data[3]));
$iana_root_data = grep($iana_root_data, 'whois.');
}
 
foreach ($rirs as &$rir) {
$step = 0;
$out = '';
 
$loc_rir_whois_server = $rir_whois_server[$rir];
 
if (USE_RIR_STAT_DATA) {
$step++;
echo "IPv4 $rir: Step $step - Get RIR statistics file\n";
 
$rirstat_url = $rirstat_urls[$rir];
$rirstat_cache = RIRSTATS_CACHE_DIR . "/$rir";
if ((USE_CACHE) && (file_exists($rirstat_cache))) {
@mkdir(dirname($rirstat_cache), 0777, true);
$rirstat_file = file($rirstat_cache);
} else {
$rirstat_file = file($rirstat_url);
}
 
$rirstat_file = antigrep($rirstat_file, '#');
 
$tmp = explode('|', $rirstat_file[0]);
$rirstat_ver = $tmp[5]; // enddate
# $rirstat_ver = substr($rirstat_ver, 0, 4) . '-' . substr($rirstat_ver, 4, 2) . '-' . substr($rirstat_ver, 6, 2);
unset($tmp);
 
$out .= "#: version $rirstat_ver\n\n";
} else if (USE_IANA_DATA) {
$out .= "#: version $iana_root_ver\n\n";
} else {
fwrite(STDERR, "Configuration error: USE_RIR_STAT_DATA and USE_IANA_DATA cannot be both false.\n");
exit(1);
}
 
$out .= "# ".strtoupper($rir)." IPv4\n";
$out .= "# Automatically generated by ".__DIR__."/gwhois-pattern-update\n";
$out .= "# Generation timestamp: ".date('Y-m-d H:i:s \G\M\TO')."\n";
if (USE_IANA_DATA) $out .= "# Source: ($iana_root_ver) $iana_root_url\n";
if (USE_RIR_STAT_DATA) $out .= "# Source: ($rirstat_ver) $rirstat_url\n";
$out .= "\n";
 
$out .= ":whois|$loc_rir_whois_server";
if ($ipv4_additional_params[$rir] != '') {
$out .= '|prefix='.$ipv4_additional_params[$rir].'|';
}
$out .= "\n";
 
$iana_blocks = array();
if (USE_IANA_DATA) {
$step++;
echo "IPv4 $rir: Step $step - Parse IANA root blocks\n";
 
$loc_roots = grep($iana_root_data, $loc_rir_whois_server);
 
$iana_ary = array();
foreach ($loc_roots as &$lr) {
$lr = trim($lr);
$tmp = explode(' ', $lr, 2);
$lr = $tmp[0];
unset($tmp);
$lr = preg_replace('|^0*|s', '', $lr);
 
// Break up the CIDR
$ary = explode('/', $lr, 2);
$bry = ipv4_cidr2range($ary[0], $ary[1]);
$iana_blocks[$bry[0]] = $bry[1];
}
}
 
$rir_blocks = array();
if (USE_RIR_STAT_DATA) {
$step++;
echo "IPv4 $rir: Step $step - Parse RIR blocks\n";
 
# $rirstat_file = grep($rirstat_file, 'ipv4');
# $rirstat_file = antigrep($rirstat_file, 'summary');
## array_shift($rirstat_file); // header line
 
foreach ($rirstat_file as &$x) {
$x = trim($x);
if ($x == '') continue;
 
$ary = explode('|', $x);
 
# arin|* |asn|* |22244|summary
# ^0 ^1 ^2 ^3 ^4 ^5
if ($ary[5] == 'summary') continue;
 
# Example:
# afrinic|ZM|ipv4|196.46.192.0|8192|20031024|allocated
# ^0 ^1 ^2 ^3 ^4 ^5 ^6
 
# 2.1AP resp 2.3 (= extended) are compatible with 2.0
 
$type = $ary[2];
if ($type != 'ipv4') continue;
 
$baseip = $ary[3];
$nums = $ary[4];
# $state = $ary[6];
 
$topip = ipv4_add($baseip, $nums-1);
 
$rir_blocks[$baseip] = $topip;
}
}
 
if (USE_IANA_DATA && USE_RIR_STAT_DATA) {
# Use both
$step++;
echo "IPv4 $rir: Step $step - Merging RIR and IANA stats\n";
$blocks_merged = ipv4_merge_arrays($rir_blocks, $iana_blocks);
} else if (USE_IANA_DATA) {
# Use only IANA
$blocks_merged = $iana_blocks;
} else if (USE_RIR_STAT_DATA) {
# Use only RIR data
$blocks_merged = $rir_blocks;
} else {
fwrite(STDERR, "Configuration error: USE_RIR_STAT_DATA and USE_IANA_DATA cannot be both false.\n");
exit(1);
}
 
$step++;
$blocks_merged = ipv4_merge_address_blocks($blocks_merged, "IPv4 $rir: Step $step - ");
 
// --- CIDR finden und ausgeben ---
 
$step++;
echo "IPv4 $rir: Step $step - Analyze address ranges and write CIDR blocks\n";
 
foreach ($blocks_merged as $baseip => &$topip) {
if (INCLUDE_BLOCK_RANGE_COMMENTS) $out .= "# $baseip - $topip\n";
 
$cidrs = ipv4_range2cidr($baseip, $topip, IPV4_SHORT_FORM);
foreach ($cidrs as &$cidr) {
$out .= "=$cidr\n";
}
}
 
# --- Write to file ---
 
$step++;
echo "IPv4 $rir: Step $step - Write to output file\n";
 
$h = fopen(PATTERN_DIR."/ipv4_$rir", 'w') or die("Error opening file ".PATTERN_DIR."/ipv4_$rir");
fwrite($h, $out) or die('Could not write to output file');
fclose($h);
}
 
echo "IPv4 Finished!\n";
Property changes:
Added: svn:executable
+*
\ No newline at end of property