Subversion Repositories vgwhois

Rev

Rev 3 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 daniel-mar 1
#!/usr/bin/php
2
<?php
3
 
4
#
5
#  generic Whois - Automatic Pattern Generator: IPv6
6
#
7
#  (c) 2012-2013 Daniel Marschall, ViaThinkSoft [www.viathinksoft.de]
8
#
9
#  Distribution, usage etc. pp. regulated by the current version of GPL.
10
#
11
#
12
#  Version 2013-03-08
13
#
14
 
15
error_reporting(E_ALL | E_NOTICE | E_STRICT | E_DEPRECATED);
16
 
17
require_once __DIR__ . '/config.inc.php';
18
require_once __DIR__ . '/../../shared/php_includes/common_functions.inc.php';
19
require_once __DIR__ . '/rirs.inc.php';
20
 
21
define('IANA_CACHE_DIR', __DIR__ . '/../.cache/pattern-generator/iana');
22
define('PATTERN_DIR', __DIR__ . '/../../main/pattern');
23
define('RIRSTATS_CACHE_DIR', __DIR__ . '/../.cache/pattern-generator/rirstats');
24
 
25
if (USE_IANA_DATA) {
26
	echo "IPv6 Preparation: Get IANA delegation file\n";
27
 
28
	$iana_root_url   = 'ftp://ftp.iana.org/assignments/ipv6-unicast-address-assignments/ipv6-unicast-address-assignments.txt';
29
	$iana_root_cache = IANA_CACHE_DIR . '/ipv6-unicast-address-assignments.txt';
30
	if ((USE_CACHE) && (file_exists($iana_root_cache))) {
31
		@mkdir(dirname($iana_root_cache), 0777, true);
32
		$iana_root_data = file($iana_root_cache, FILE_IGNORE_NEW_LINES);
33
	} else {
34
		$iana_root_data = file($iana_root_url, FILE_IGNORE_NEW_LINES);
35
	}
36
	$iana_root_ver  = trim(str_replace('-', '', $iana_root_data[3]));
37
	$iana_root_data = grep($iana_root_data, 'whois.');
38
}
39
 
40
foreach ($rirs as &$rir) {
41
	$step = 0;
42
	$out = '';
43
 
44
	$loc_rir_whois_server = $rir_whois_server[$rir];
45
 
46
	if (USE_RIR_STAT_DATA) {
47
		$step++;
48
		echo "IPv6 $rir: Step $step - Get RIR statistics file\n";
49
 
50
		$rirstat_url   = $rirstat_urls[$rir];
51
		$rirstat_cache = RIRSTATS_CACHE_DIR . "/$rir";
52
		if ((USE_CACHE) && (file_exists($rirstat_cache))) {
53
			@mkdir(dirname($rirstat_cache), 0777, true);
54
			$rirstat_file = file($rirstat_cache);
55
		} else {
56
			$rirstat_file = file($rirstat_url);
57
		}
58
 
59
		$rirstat_file = antigrep($rirstat_file, '#');
60
 
61
		$tmp = explode('|', $rirstat_file[0]);
62
		$rirstat_ver = $tmp[5]; // enddate
63
		# $rirstat_ver = substr($rirstat_ver, 0, 4) . '-' . substr($rirstat_ver, 4, 2) . '-' . substr($rirstat_ver, 6, 2);
64
		unset($tmp);
65
 
66
		$out .= "#: version $rirstat_ver\n\n";
67
	} else if (USE_IANA_DATA) {
68
		$out .= "#: version $iana_root_ver\n\n";
69
	} else {
70
		fwrite(STDERR, "Configuration error: USE_RIR_STAT_DATA and USE_IANA_DATA cannot be both false.\n");
71
		exit(1);
72
	}
73
 
74
	$out .= "# ".strtoupper($rir)." IPv6\n";
75
	$out .= "# Automatically generated by ".__DIR__."/gwhois-pattern-update\n";
76
	$out .= "# Generation timestamp: ".date('Y-m-d H:i:s \G\M\TO')."\n";
77
	if (USE_IANA_DATA)     $out .= "# Source: ($iana_root_ver) $iana_root_url\n";
78
	if (USE_RIR_STAT_DATA) $out .= "# Source: ($rirstat_ver) $rirstat_url\n";
79
	$out .= "\n";
80
 
81
	$out .= ":whois|$loc_rir_whois_server";
82
	if ($ipv6_additional_params[$rir] != '') {
83
		$out .= '|prefix='.$ipv6_additional_params[$rir].'|';
84
	}
85
	$out .= "\n";
86
 
87
	$iana_blocks = array();
88
	if (USE_IANA_DATA) {
89
		$step++;
90
		echo "IPv6 $rir: Step $step - Parse IANA root blocks\n";
91
 
92
		$loc_roots = grep($iana_root_data, $loc_rir_whois_server);
93
 
94
		$iana_ary = array();
95
		foreach ($loc_roots as &$lr) {
96
			$lr = trim($lr);
97
			$tmp = explode(' ', $lr, 2);
98
			$lr = $tmp[0];
99
			unset($tmp);
100
			$lr = preg_replace('|^0*|s', '', $lr);
101
 
102
			// Break up the CIDR
103
			$ary = explode('/', $lr, 2);
104
			$bry = ipv6_cidr2range($ary[0], $ary[1]);
105
			$iana_blocks[$bry[0]] = $bry[1];
106
		}
107
	}
108
 
109
	$rir_blocks = array();
110
	if (USE_RIR_STAT_DATA) {
111
		$step++;
112
		echo "IPv6 $rir: Step $step - Parse RIR blocks\n";
113
 
114
#		$rirstat_file = grep($rirstat_file, 'ipv6');
115
#		$rirstat_file = antigrep($rirstat_file, 'summary');
116
##		array_shift($rirstat_file); // header line
117
 
118
		foreach ($rirstat_file as &$x) {
119
			$x = trim($x);
120
			if ($x == '') continue;
121
 
122
			$ary = explode('|', $x);
123
 
124
			# arin|* |asn|* |22244|summary
125
			# ^0   ^1 ^2  ^3 ^4    ^5
126
			if ($ary[5] == 'summary') continue;
127
 
128
			# Example:
129
			# afrinic|KE|ipv6|2c0f:fe88::|32|20100728|allocated
130
			# ^0      ^1 ^2   ^3          ^4 ^5       ^6
131
 
132
			# 2.1AP resp 2.3 (= extended) are compatible with 2.0
133
 
134
			$type = $ary[2];
135
			if ($type != 'ipv6') continue;
136
 
137
			$baseip = $ary[3];
138
			$cidr = $ary[4];
139
			# $state = $ary[6];
140
 
141
			$range = ipv6_cidr2range($baseip, $cidr);
142
			$topip = $range[1];
143
 
144
			$rir_blocks[$baseip] = $topip;
145
		}
146
	}
147
 
148
	if (USE_IANA_DATA && USE_RIR_STAT_DATA) {
149
		# Use both
150
		$step++;
151
		echo "IPv6 $rir: Step $step - Merging RIR and IANA stats\n";
152
		$blocks_merged = ipv6_merge_arrays($rir_blocks, $iana_blocks);
153
	} else if (USE_IANA_DATA) {
154
		# Use only IANA
155
		$blocks_merged = $iana_blocks;
156
	} else if (USE_RIR_STAT_DATA) {
157
		# Use only RIR data
158
		$blocks_merged = $rir_blocks;
159
	} else {
160
		fwrite(STDERR, "Configuration error: USE_RIR_STAT_DATA and USE_IANA_DATA cannot be both false.\n");
161
		exit(1);
162
	}
163
 
164
	$step++;
165
	$blocks_merged = ipv6_merge_address_blocks($blocks_merged, "IPv6 $rir: Step $step - ");
166
 
167
	// --- CIDR finden und ausgeben ---
168
 
169
	$step++;
170
	echo "IPv6 $rir: Step $step - Analyze address ranges and write CIDR blocks\n";
171
 
172
	foreach ($blocks_merged as $baseip => &$topip) {
173
		if (INCLUDE_BLOCK_RANGE_COMMENTS) $out .= "# $baseip - $topip\n";
174
 
175
		$cidrs = ipv6_range2cidr($baseip, $topip);
176
		foreach ($cidrs as &$cidr) {
177
			$out .= "=$cidr\n";
178
		}
179
	}
180
 
181
	# --- Write to file ---
182
 
183
	$step++;
184
	echo "IPv6 $rir: Step $step - Write to output file\n";
185
 
186
	$h = fopen(PATTERN_DIR."/ipv6_$rir", 'w') or die("Error opening file ".PATTERN_DIR."/ipv6_$rir");
187
	fwrite($h, $out) or die('Could not write to output file');
188
	fclose($h);
189
}
190
 
191
echo "IPv6 Finished!\n";