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