Subversion Repositories vgwhois

Rev

Rev 77 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 daniel-mar 1
#!/usr/bin/php
2
<?php
3
 
4
#
11 daniel-mar 5
#  VGWhoIs (ViaThinkSoft Global WhoIs, a fork of generic Whois / gwhois)
5 daniel-mar 6
#  Maintenance / Developer utilities
2 daniel-mar 7
#
88 daniel-mar 8
#  (c) 2012-2022 by Daniel Marschall, ViaThinkSoft <info@daniel-marschall.de>
2 daniel-mar 9
#
5 daniel-mar 10
#  License: https://www.gnu.org/licenses/gpl-2.0.html (GPL version 2)
2 daniel-mar 11
#
12
 
13
error_reporting(E_ALL | E_NOTICE | E_STRICT | E_DEPRECATED);
14
 
15
require_once __DIR__ . '/config.inc.php';
16
require_once __DIR__ . '/../../shared/php_includes/common_functions.inc.php';
77 daniel-mar 17
 
18
/** @var array $rirs */
19
/** @var array $supports_extended_rirstat */
20
/** @var array $rir_whois_server */
21
/** @var array $rir_domain */
22
/** @var array $rirstat_urls */
23
/** @var array $ipv4_additional_params */
24
/** @var array $ipv6_additional_params */
25
/** @var array $asn_additional_params */
26
/** @var array $tld_additional_params */
2 daniel-mar 27
require_once __DIR__ . '/rirs.inc.php';
28
 
29
if (USE_IANA_DATA) {
30
	echo "IPv4 Preparation: Get IANA delegation file\n";
31
 
88 daniel-mar 32
	$iana_root_data = explode("\n", $cont = cached_file(IANA_IPV4_ASSIGNMENTS, CACHE_FILE_DIR));
3 daniel-mar 33
	$iana_root_data = array_map('trim', $iana_root_data);
34
 
88 daniel-mar 35
	preg_match('@(20\\d\\d\\-\\d\\d\\-\\d\\d)@ismU', $cont, $m);
36
 
37
	$iana_root_ver  = trim(str_replace('-', '', $m[1]));
2 daniel-mar 38
	$iana_root_data = grep($iana_root_data, 'whois.');
39
}
40
 
41
foreach ($rirs as &$rir) {
42
	$step = 0;
43
	$out = '';
44
 
45
	$loc_rir_whois_server = $rir_whois_server[$rir];
46
 
47
	if (USE_RIR_STAT_DATA) {
48
		$step++;
49
		echo "IPv4 $rir: Step $step - Get RIR statistics file\n";
50
 
3 daniel-mar 51
		$rirstat_url  = $rirstat_urls[$rir];
52
		$rirstat_file = explode("\n", cached_file($rirstat_url, CACHE_FILE_DIR));
2 daniel-mar 53
 
54
		$rirstat_file = antigrep($rirstat_file, '#');
55
 
56
		$tmp = explode('|', $rirstat_file[0]);
57
		$rirstat_ver = $tmp[5]; // enddate
58
		# $rirstat_ver = substr($rirstat_ver, 0, 4) . '-' . substr($rirstat_ver, 4, 2) . '-' . substr($rirstat_ver, 6, 2);
59
		unset($tmp);
60
 
61
		$out .= "#: version $rirstat_ver\n\n";
62
	} else if (USE_IANA_DATA) {
63
		$out .= "#: version $iana_root_ver\n\n";
64
	} else {
65
		fwrite(STDERR, "Configuration error: USE_RIR_STAT_DATA and USE_IANA_DATA cannot be both false.\n");
66
		exit(1);
67
	}
68
 
69
	$out .= "# ".strtoupper($rir)." IPv4\n";
11 daniel-mar 70
	$out .= "# Automatically generated by ".__DIR__."/vgwhois-pattern-update\n";
2 daniel-mar 71
	$out .= "# Generation timestamp: ".date('Y-m-d H:i:s \G\M\TO')."\n";
3 daniel-mar 72
	if (USE_IANA_DATA)     $out .= "# Source: ($iana_root_ver) ".IANA_IPV4_ASSIGNMENTS."\n";
2 daniel-mar 73
	if (USE_RIR_STAT_DATA) $out .= "# Source: ($rirstat_ver) $rirstat_url\n";
74
	$out .= "\n";
75
 
76
	$out .= ":whois|$loc_rir_whois_server";
77
	if ($ipv4_additional_params[$rir] != '') {
78
		$out .= '|prefix='.$ipv4_additional_params[$rir].'|';
79
	}
80
	$out .= "\n";
81
 
82
	$iana_blocks = array();
83
	if (USE_IANA_DATA) {
84
		$step++;
85
		echo "IPv4 $rir: Step $step - Parse IANA root blocks\n";
86
 
87
		$loc_roots = grep($iana_root_data, $loc_rir_whois_server);
88
 
89
		$iana_ary = array();
90
		foreach ($loc_roots as &$lr) {
91
			$lr = trim($lr);
92
			$tmp = explode(' ', $lr, 2);
93
			$lr = $tmp[0];
94
			unset($tmp);
95
			$lr = preg_replace('|^0*|s', '', $lr);
96
 
97
			// Break up the CIDR
98
			$ary = explode('/', $lr, 2);
99
			$bry = ipv4_cidr2range($ary[0], $ary[1]);
100
			$iana_blocks[$bry[0]] = $bry[1];
101
		}
102
	}
103
 
104
	$rir_blocks = array();
105
	if (USE_RIR_STAT_DATA) {
106
		$step++;
107
		echo "IPv4 $rir: Step $step - Parse RIR blocks\n";
108
 
109
#		$rirstat_file = grep($rirstat_file, 'ipv4');
110
#		$rirstat_file = antigrep($rirstat_file, 'summary');
111
##		array_shift($rirstat_file); // header line
112
 
113
		foreach ($rirstat_file as &$x) {
114
			$x = trim($x);
115
			if ($x == '') continue;
116
 
117
			$ary = explode('|', $x);
118
 
119
			# arin|* |asn|* |22244|summary
120
			# ^0   ^1 ^2  ^3 ^4    ^5
121
			if ($ary[5] == 'summary') continue;
122
 
123
			# Example:
124
			# afrinic|ZM|ipv4|196.46.192.0|8192|20031024|allocated
125
			# ^0      ^1 ^2   ^3           ^4   ^5       ^6
126
 
127
			# 2.1AP resp 2.3 (= extended) are compatible with 2.0
128
 
129
			$type = $ary[2];
130
			if ($type != 'ipv4') continue;
131
 
132
			$baseip = $ary[3];
133
			$nums = $ary[4];
134
			# $state = $ary[6];
135
 
77 daniel-mar 136
			$topip = ipv4_add($baseip, (int)$nums-1);
2 daniel-mar 137
 
138
			$rir_blocks[$baseip] = $topip;
139
		}
140
	}
141
 
142
	if (USE_IANA_DATA && USE_RIR_STAT_DATA) {
143
		# Use both
144
		$step++;
145
		echo "IPv4 $rir: Step $step - Merging RIR and IANA stats\n";
146
		$blocks_merged = ipv4_merge_arrays($rir_blocks, $iana_blocks);
147
	} else if (USE_IANA_DATA) {
148
		# Use only IANA
149
		$blocks_merged = $iana_blocks;
150
	} else if (USE_RIR_STAT_DATA) {
151
		# Use only RIR data
152
		$blocks_merged = $rir_blocks;
153
	} else {
154
		fwrite(STDERR, "Configuration error: USE_RIR_STAT_DATA and USE_IANA_DATA cannot be both false.\n");
155
		exit(1);
156
	}
157
 
158
	$step++;
159
	$blocks_merged = ipv4_merge_address_blocks($blocks_merged, "IPv4 $rir: Step $step - ");
160
 
161
	// --- CIDR finden und ausgeben ---
162
 
163
	$step++;
164
	echo "IPv4 $rir: Step $step - Analyze address ranges and write CIDR blocks\n";
165
 
166
	foreach ($blocks_merged as $baseip => &$topip) {
167
		if (INCLUDE_BLOCK_RANGE_COMMENTS) $out .= "# $baseip - $topip\n";
168
 
169
		$cidrs = ipv4_range2cidr($baseip, $topip, IPV4_SHORT_FORM);
170
		foreach ($cidrs as &$cidr) {
171
			$out .= "=$cidr\n";
172
		}
173
	}
174
 
175
	# --- Write to file ---
176
 
177
	$step++;
178
	echo "IPv4 $rir: Step $step - Write to output file\n";
179
 
180
	$h = fopen(PATTERN_DIR."/ipv4_$rir", 'w') or die("Error opening file ".PATTERN_DIR."/ipv4_$rir");
181
	fwrite($h, $out) or die('Could not write to output file');
182
	fclose($h);
183
}
184
 
185
echo "IPv4 Finished!\n";