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 "IPv6 Preparation: Get IANA delegation file\n";
31
 
88 daniel-mar 32
	$iana_root_data = explode("\n", $cont = cached_file(IANA_IPV6_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 "IPv6 $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)." IPv6\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_IPV6_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 ($ipv6_additional_params[$rir] != '') {
78
		$out .= '|prefix='.$ipv6_additional_params[$rir].'|';
79
	}
80
	$out .= "\n";
81
 
82
	$iana_blocks = array();
83
	if (USE_IANA_DATA) {
84
		$step++;
85
		echo "IPv6 $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 = ipv6_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 "IPv6 $rir: Step $step - Parse RIR blocks\n";
108
 
109
#		$rirstat_file = grep($rirstat_file, 'ipv6');
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|KE|ipv6|2c0f:fe88::|32|20100728|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 != 'ipv6') continue;
131
 
132
			$baseip = $ary[3];
133
			$cidr = $ary[4];
134
			# $state = $ary[6];
135
 
136
			$range = ipv6_cidr2range($baseip, $cidr);
137
			$topip = $range[1];
138
 
139
			$rir_blocks[$baseip] = $topip;
140
		}
141
	}
142
 
143
	if (USE_IANA_DATA && USE_RIR_STAT_DATA) {
144
		# Use both
145
		$step++;
146
		echo "IPv6 $rir: Step $step - Merging RIR and IANA stats\n";
147
		$blocks_merged = ipv6_merge_arrays($rir_blocks, $iana_blocks);
148
	} else if (USE_IANA_DATA) {
149
		# Use only IANA
150
		$blocks_merged = $iana_blocks;
151
	} else if (USE_RIR_STAT_DATA) {
152
		# Use only RIR data
153
		$blocks_merged = $rir_blocks;
154
	} else {
155
		fwrite(STDERR, "Configuration error: USE_RIR_STAT_DATA and USE_IANA_DATA cannot be both false.\n");
156
		exit(1);
157
	}
158
 
159
	$step++;
160
	$blocks_merged = ipv6_merge_address_blocks($blocks_merged, "IPv6 $rir: Step $step - ");
161
 
162
	// --- CIDR finden und ausgeben ---
163
 
164
	$step++;
165
	echo "IPv6 $rir: Step $step - Analyze address ranges and write CIDR blocks\n";
166
 
167
	foreach ($blocks_merged as $baseip => &$topip) {
168
		if (INCLUDE_BLOCK_RANGE_COMMENTS) $out .= "# $baseip - $topip\n";
169
 
170
		$cidrs = ipv6_range2cidr($baseip, $topip);
171
		foreach ($cidrs as &$cidr) {
172
			$out .= "=$cidr\n";
173
		}
174
	}
175
 
176
	# --- Write to file ---
177
 
178
	$step++;
179
	echo "IPv6 $rir: Step $step - Write to output file\n";
180
 
181
	$h = fopen(PATTERN_DIR."/ipv6_$rir", 'w') or die("Error opening file ".PATTERN_DIR."/ipv6_$rir");
182
	fwrite($h, $out) or die('Could not write to output file');
183
	fclose($h);
184
}
185
 
186
echo "IPv6 Finished!\n";