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: IPv4 |
||
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 "IPv4 Preparation: Get IANA delegation file\n"; |
||
27 | |||
28 | $iana_root_url = 'ftp://ftp.iana.org/assignments/ipv4-address-space/ipv4-address-space.txt'; |
||
29 | $iana_root_cache = IANA_CACHE_DIR . '/ipv4-address-space.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 "IPv4 $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)." IPv4\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 ($ipv4_additional_params[$rir] != '') { |
||
83 | $out .= '|prefix='.$ipv4_additional_params[$rir].'|'; |
||
84 | } |
||
85 | $out .= "\n"; |
||
86 | |||
87 | $iana_blocks = array(); |
||
88 | if (USE_IANA_DATA) { |
||
89 | $step++; |
||
90 | echo "IPv4 $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 = ipv4_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 "IPv4 $rir: Step $step - Parse RIR blocks\n"; |
||
113 | |||
114 | # $rirstat_file = grep($rirstat_file, 'ipv4'); |
||
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|ZM|ipv4|196.46.192.0|8192|20031024|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 != 'ipv4') continue; |
||
136 | |||
137 | $baseip = $ary[3]; |
||
138 | $nums = $ary[4]; |
||
139 | # $state = $ary[6]; |
||
140 | |||
141 | $topip = ipv4_add($baseip, $nums-1); |
||
142 | |||
143 | $rir_blocks[$baseip] = $topip; |
||
144 | } |
||
145 | } |
||
146 | |||
147 | if (USE_IANA_DATA && USE_RIR_STAT_DATA) { |
||
148 | # Use both |
||
149 | $step++; |
||
150 | echo "IPv4 $rir: Step $step - Merging RIR and IANA stats\n"; |
||
151 | $blocks_merged = ipv4_merge_arrays($rir_blocks, $iana_blocks); |
||
152 | } else if (USE_IANA_DATA) { |
||
153 | # Use only IANA |
||
154 | $blocks_merged = $iana_blocks; |
||
155 | } else if (USE_RIR_STAT_DATA) { |
||
156 | # Use only RIR data |
||
157 | $blocks_merged = $rir_blocks; |
||
158 | } else { |
||
159 | fwrite(STDERR, "Configuration error: USE_RIR_STAT_DATA and USE_IANA_DATA cannot be both false.\n"); |
||
160 | exit(1); |
||
161 | } |
||
162 | |||
163 | $step++; |
||
164 | $blocks_merged = ipv4_merge_address_blocks($blocks_merged, "IPv4 $rir: Step $step - "); |
||
165 | |||
166 | // --- CIDR finden und ausgeben --- |
||
167 | |||
168 | $step++; |
||
169 | echo "IPv4 $rir: Step $step - Analyze address ranges and write CIDR blocks\n"; |
||
170 | |||
171 | foreach ($blocks_merged as $baseip => &$topip) { |
||
172 | if (INCLUDE_BLOCK_RANGE_COMMENTS) $out .= "# $baseip - $topip\n"; |
||
173 | |||
174 | $cidrs = ipv4_range2cidr($baseip, $topip, IPV4_SHORT_FORM); |
||
175 | foreach ($cidrs as &$cidr) { |
||
176 | $out .= "=$cidr\n"; |
||
177 | } |
||
178 | } |
||
179 | |||
180 | # --- Write to file --- |
||
181 | |||
182 | $step++; |
||
183 | echo "IPv4 $rir: Step $step - Write to output file\n"; |
||
184 | |||
185 | $h = fopen(PATTERN_DIR."/ipv4_$rir", 'w') or die("Error opening file ".PATTERN_DIR."/ipv4_$rir"); |
||
186 | fwrite($h, $out) or die('Could not write to output file'); |
||
187 | fclose($h); |
||
188 | } |
||
189 | |||
190 | echo "IPv4 Finished!\n"; |