Subversion Repositories vgwhois

Rev

Rev 11 | Go to most recent revision | 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
#
5 daniel-mar 8
#  (c) 2012-2019 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
 
3 daniel-mar 32
	$iana_root_data = explode("\n", cached_file(IANA_IPV6_ASSIGNMENTS, CACHE_FILE_DIR));
33
	$iana_root_data = array_map('trim', $iana_root_data);
34
 
2 daniel-mar 35
	$iana_root_ver  = trim(str_replace('-', '', $iana_root_data[3]));
36
	$iana_root_data = grep($iana_root_data, 'whois.');
37
}
38
 
39
foreach ($rirs as &$rir) {
40
	$step = 0;
41
	$out = '';
42
 
43
	$loc_rir_whois_server = $rir_whois_server[$rir];
44
 
45
	if (USE_RIR_STAT_DATA) {
46
		$step++;
47
		echo "IPv6 $rir: Step $step - Get RIR statistics file\n";
48
 
3 daniel-mar 49
		$rirstat_url  = $rirstat_urls[$rir];
50
		$rirstat_file = explode("\n", cached_file($rirstat_url, CACHE_FILE_DIR));
2 daniel-mar 51
 
52
		$rirstat_file = antigrep($rirstat_file, '#');
53
 
54
		$tmp = explode('|', $rirstat_file[0]);
55
		$rirstat_ver = $tmp[5]; // enddate
56
		# $rirstat_ver = substr($rirstat_ver, 0, 4) . '-' . substr($rirstat_ver, 4, 2) . '-' . substr($rirstat_ver, 6, 2);
57
		unset($tmp);
58
 
59
		$out .= "#: version $rirstat_ver\n\n";
60
	} else if (USE_IANA_DATA) {
61
		$out .= "#: version $iana_root_ver\n\n";
62
	} else {
63
		fwrite(STDERR, "Configuration error: USE_RIR_STAT_DATA and USE_IANA_DATA cannot be both false.\n");
64
		exit(1);
65
	}
66
 
67
	$out .= "# ".strtoupper($rir)." IPv6\n";
11 daniel-mar 68
	$out .= "# Automatically generated by ".__DIR__."/vgwhois-pattern-update\n";
2 daniel-mar 69
	$out .= "# Generation timestamp: ".date('Y-m-d H:i:s \G\M\TO')."\n";
3 daniel-mar 70
	if (USE_IANA_DATA)     $out .= "# Source: ($iana_root_ver) ".IANA_IPV6_ASSIGNMENTS."\n";
2 daniel-mar 71
	if (USE_RIR_STAT_DATA) $out .= "# Source: ($rirstat_ver) $rirstat_url\n";
72
	$out .= "\n";
73
 
74
	$out .= ":whois|$loc_rir_whois_server";
75
	if ($ipv6_additional_params[$rir] != '') {
76
		$out .= '|prefix='.$ipv6_additional_params[$rir].'|';
77
	}
78
	$out .= "\n";
79
 
80
	$iana_blocks = array();
81
	if (USE_IANA_DATA) {
82
		$step++;
83
		echo "IPv6 $rir: Step $step - Parse IANA root blocks\n";
84
 
85
		$loc_roots = grep($iana_root_data, $loc_rir_whois_server);
86
 
87
		$iana_ary = array();
88
		foreach ($loc_roots as &$lr) {
89
			$lr = trim($lr);
90
			$tmp = explode(' ', $lr, 2);
91
			$lr = $tmp[0];
92
			unset($tmp);
93
			$lr = preg_replace('|^0*|s', '', $lr);
94
 
95
			// Break up the CIDR
96
			$ary = explode('/', $lr, 2);
97
			$bry = ipv6_cidr2range($ary[0], $ary[1]);
98
			$iana_blocks[$bry[0]] = $bry[1];
99
		}
100
	}
101
 
102
	$rir_blocks = array();
103
	if (USE_RIR_STAT_DATA) {
104
		$step++;
105
		echo "IPv6 $rir: Step $step - Parse RIR blocks\n";
106
 
107
#		$rirstat_file = grep($rirstat_file, 'ipv6');
108
#		$rirstat_file = antigrep($rirstat_file, 'summary');
109
##		array_shift($rirstat_file); // header line
110
 
111
		foreach ($rirstat_file as &$x) {
112
			$x = trim($x);
113
			if ($x == '') continue;
114
 
115
			$ary = explode('|', $x);
116
 
117
			# arin|* |asn|* |22244|summary
118
			# ^0   ^1 ^2  ^3 ^4    ^5
119
			if ($ary[5] == 'summary') continue;
120
 
121
			# Example:
122
			# afrinic|KE|ipv6|2c0f:fe88::|32|20100728|allocated
123
			# ^0      ^1 ^2   ^3          ^4 ^5       ^6
124
 
125
			# 2.1AP resp 2.3 (= extended) are compatible with 2.0
126
 
127
			$type = $ary[2];
128
			if ($type != 'ipv6') continue;
129
 
130
			$baseip = $ary[3];
131
			$cidr = $ary[4];
132
			# $state = $ary[6];
133
 
134
			$range = ipv6_cidr2range($baseip, $cidr);
135
			$topip = $range[1];
136
 
137
			$rir_blocks[$baseip] = $topip;
138
		}
139
	}
140
 
141
	if (USE_IANA_DATA && USE_RIR_STAT_DATA) {
142
		# Use both
143
		$step++;
144
		echo "IPv6 $rir: Step $step - Merging RIR and IANA stats\n";
145
		$blocks_merged = ipv6_merge_arrays($rir_blocks, $iana_blocks);
146
	} else if (USE_IANA_DATA) {
147
		# Use only IANA
148
		$blocks_merged = $iana_blocks;
149
	} else if (USE_RIR_STAT_DATA) {
150
		# Use only RIR data
151
		$blocks_merged = $rir_blocks;
152
	} else {
153
		fwrite(STDERR, "Configuration error: USE_RIR_STAT_DATA and USE_IANA_DATA cannot be both false.\n");
154
		exit(1);
155
	}
156
 
157
	$step++;
158
	$blocks_merged = ipv6_merge_address_blocks($blocks_merged, "IPv6 $rir: Step $step - ");
159
 
160
	// --- CIDR finden und ausgeben ---
161
 
162
	$step++;
163
	echo "IPv6 $rir: Step $step - Analyze address ranges and write CIDR blocks\n";
164
 
165
	foreach ($blocks_merged as $baseip => &$topip) {
166
		if (INCLUDE_BLOCK_RANGE_COMMENTS) $out .= "# $baseip - $topip\n";
167
 
168
		$cidrs = ipv6_range2cidr($baseip, $topip);
169
		foreach ($cidrs as &$cidr) {
170
			$out .= "=$cidr\n";
171
		}
172
	}
173
 
174
	# --- Write to file ---
175
 
176
	$step++;
177
	echo "IPv6 $rir: Step $step - Write to output file\n";
178
 
179
	$h = fopen(PATTERN_DIR."/ipv6_$rir", 'w') or die("Error opening file ".PATTERN_DIR."/ipv6_$rir");
180
	fwrite($h, $out) or die('Could not write to output file');
181
	fclose($h);
182
}
183
 
184
echo "IPv6 Finished!\n";