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