… | |
… | |
6 | use Fcntl; |
6 | use Fcntl; |
7 | |
7 | |
8 | use Coro; |
8 | use Coro; |
9 | use Coro::Event; |
9 | use Coro::Event; |
10 | use Coro::Semaphore; |
10 | use Coro::Semaphore; |
|
|
11 | use Coro::SemaphoreSet; |
11 | use Coro::Socket; |
12 | use Coro::Socket; |
|
|
13 | use Coro::Timer; |
|
|
14 | |
|
|
15 | use BerkeleyDB; |
12 | |
16 | |
13 | $Event::DIED = sub { |
17 | $Event::DIED = sub { |
14 | Event::verbose_exception_handler(@_); |
18 | Event::verbose_exception_handler(@_); |
15 | #Event::unloop_all(); |
19 | #Event::unloop_all(); |
16 | }; |
20 | }; |
… | |
… | |
60 | my $timeout = 5; |
64 | my $timeout = 5; |
61 | |
65 | |
62 | while () { |
66 | while () { |
63 | my $fh = new Coro::Socket |
67 | my $fh = new Coro::Socket |
64 | PeerAddr => $self->ip, |
68 | PeerAddr => $self->ip, |
65 | PeerPort => 'whois', |
69 | PeerPort => $self->{port} || "whois", |
66 | Timeout => 30; |
70 | Timeout => 30; |
67 | if ($fh) { |
71 | if ($fh) { |
68 | print $fh "$query\n"; |
72 | print $fh "$query\n"; |
69 | $fh->read($whois, 16*1024); # max 16k. whois stored |
73 | $fh->read($whois, 16*1024); # max 16k. whois stored |
70 | close $fh; |
74 | close $fh; |
… | |
… | |
80 | $timeout *= 2; |
84 | $timeout *= 2; |
81 | $timeout = 1 if $timeout > 600; |
85 | $timeout = 1 if $timeout > 600; |
82 | } else { |
86 | } else { |
83 | last; |
87 | last; |
84 | } |
88 | } |
|
|
89 | } else { |
|
|
90 | # only retry once a minute |
|
|
91 | print STDERR "unable to connect to $self->{ip} ($self->{name}), retrying...\n"; |
|
|
92 | Coro::Timer::sleep 300; |
85 | } |
93 | } |
86 | } |
94 | } |
87 | |
95 | |
88 | $netgeo::whois{$id} = $whois; |
96 | $netgeo::whois{$id} = $whois; |
89 | } |
97 | } |
… | |
… | |
112 | sub ip_request { |
120 | sub ip_request { |
113 | my ($self, $ip) = @_; |
121 | my ($self, $ip) = @_; |
114 | |
122 | |
115 | my $whois = $self->whois_request($ip); |
123 | my $whois = $self->whois_request($ip); |
116 | |
124 | |
117 | return () if $whois =~ /^No match/; |
125 | return if $whois =~ /^No match/; |
118 | |
126 | |
119 | if ($whois =~ /^To single out one record/m) { |
127 | if ($whois =~ /^To single out one record/m) { |
120 | my $handle; |
128 | my $handle; |
121 | while ($whois =~ /\G\S.*\(([A-Z0-9\-]+)\).*\n/mg) { |
129 | while ($whois =~ /\G\S.*\(([A-Z0-9\-]+)\).*\n/mg) { |
122 | $handle = $1; |
130 | $handle = $1; |
123 | #return if $handle =~ /-(RIPE|APNIC)/; # heuristic, bbut bad because ripe might not have better info |
131 | #return if $handle =~ /-(RIPE|APNIC)/; # heuristic, but bad because ripe might not have better info |
124 | } |
132 | } |
125 | $handle or die "$whois ($ip): unparseable multimatch\n"; |
133 | $handle or die "$whois ($ip): unparseable multimatch\n"; |
126 | $whois = $self->whois_request("!$handle"); |
134 | $whois = $self->whois_request("!$handle"); |
127 | } |
135 | } |
128 | |
136 | |
… | |
… | |
164 | $whois; |
172 | $whois; |
165 | } |
173 | } |
166 | |
174 | |
167 | package Whois::RIPE; |
175 | package Whois::RIPE; |
168 | |
176 | |
|
|
177 | use Socket; |
169 | use base Whois; |
178 | use base Whois; |
170 | |
179 | |
171 | sub sanitize { |
180 | sub sanitize { |
172 | local $_ = $_[1]; |
181 | local $_ = $_[1]; |
|
|
182 | |
173 | s/^%.*\n//gm; |
183 | s/^%.*\n//gm; |
174 | s/^\n+//; |
184 | s/^\n+//; |
175 | s/\n*$/\n/; |
185 | s/\n*$/\n/; |
|
|
186 | |
|
|
187 | s/^inetnum:\s+/*in: /gm; |
|
|
188 | s/^admin-c:\s+/*ac: /gm; |
|
|
189 | s/^tech-c:\s+/*tc: /gm; |
|
|
190 | s/^owner-c:\s+/*oc: /gm; |
|
|
191 | s/^country:\s+/*cy: /gm; |
|
|
192 | s/^phone:\s+/*ph: /gm; |
|
|
193 | s/^remarks:\s+/*rm: /gm; |
|
|
194 | s/^changed:\s+/*ch: /gm; |
|
|
195 | s/^created:\s+/*cr: /gm; |
|
|
196 | s/^address:\s+/*ad: /gm; |
|
|
197 | s/^status:\s+/*st: /gm; |
|
|
198 | s/^inetrev:\s+/*ir: /gm; |
|
|
199 | s/^nserver:\s+/*ns: /gm; |
|
|
200 | |
176 | $_; |
201 | $_; |
177 | } |
202 | } |
178 | |
203 | |
179 | sub ip_request { |
204 | sub ip_request { |
180 | my ($self, $ip) = @_; |
205 | my ($self, $ip) = @_; |
181 | |
206 | |
182 | my $whois = $self->whois_request("-FSTin $ip"); |
207 | my $whois = $self->whois_request("$self->{rflags}$ip"); |
|
|
208 | |
|
|
209 | $whois =~ s{ |
|
|
210 | (2[0-5][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9]) |
|
|
211 | (?:\. |
|
|
212 | (2[0-5][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9]) |
|
|
213 | (?:\. |
|
|
214 | (2[0-5][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9]) |
|
|
215 | (?:\. |
|
|
216 | (2[0-5][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9]) |
|
|
217 | )? |
|
|
218 | )? |
|
|
219 | )? |
|
|
220 | / |
|
|
221 | ([0-9]+) |
|
|
222 | }{ |
|
|
223 | my $ip = inet_aton sprintf "%d.%d.%d.%d", $1, $2, $3, $4; |
|
|
224 | my $net = 1 << (31 - $5); |
|
|
225 | my $mask = inet_aton 2 ** 32 - $net; |
|
|
226 | |
|
|
227 | my $ip1 = $ip & $mask; |
|
|
228 | my $ip2 = $ip1 | inet_aton $net * 2 - 1; |
|
|
229 | (inet_ntoa $ip1) . " - " . (inet_ntoa $ip2); |
|
|
230 | }gex; |
183 | |
231 | |
184 | $whois =~ /^\*in: 0\.0\.0\.0 - 255\.255\.255\.255/ |
232 | $whois =~ /^\*in: 0\.0\.0\.0 - 255\.255\.255\.255/ |
185 | and return; |
233 | and return; |
186 | |
234 | |
|
|
235 | $whois =~ /^\*de: This network range is not allocated to /m # APINIC e.g. 24.0.0.0 |
|
|
236 | and return; |
|
|
237 | |
187 | $whois =~ /^\*ac: XXX0/m # 192.0.0.0 |
238 | $whois =~ /^\*ac: XXX0/m # 192.0.0.0 |
|
|
239 | and return; |
|
|
240 | |
|
|
241 | $whois =~ /^\*st: (?:ALLOCATED )?UNSPECIFIED/m |
188 | and return; |
242 | and return; |
189 | |
243 | |
190 | $whois =~ /^%ERROR:/m |
244 | $whois =~ /^%ERROR:/m |
191 | and return; |
245 | and return; |
192 | |
246 | |
193 | #while ($whois =~ s/^\*(?:ac|tc):\s+(\S+)\n//m) { |
247 | #while ($whois =~ s/^\*(?:ac|tc):\s+(\S+)\n//m) { |
194 | # $whois .= $self->whois_request("-FSTpn $1"); |
248 | # $whois .= $self->whois_request("-FSTpn $1"); |
195 | #} |
249 | #} |
196 | |
250 | |
|
|
251 | #$whois =~ s/^\*(?:pn|nh|mb|ch|so|rz|ny|st|rm):.*\n//mg; |
|
|
252 | |
|
|
253 | $whois =~ s/\n+$//; |
|
|
254 | |
|
|
255 | $whois; |
|
|
256 | } |
|
|
257 | |
|
|
258 | package Whois::RWHOIS; |
|
|
259 | |
|
|
260 | use base Whois; |
|
|
261 | |
|
|
262 | sub sanitize { |
|
|
263 | local $_ = $_[1]; |
|
|
264 | s/^%referral\s+/referral:/gm; |
|
|
265 | s/^network://gm; |
|
|
266 | s/^%.*\n//gm; |
|
|
267 | s/^\n+//m; |
|
|
268 | s/\n*$/\n/m; |
|
|
269 | |
|
|
270 | s/^(\S+):\s*/\L$1: /gm; |
|
|
271 | s/^ip-network-block:/*in:/gm; |
|
|
272 | s/^country-code:/*cy:/gm; |
|
|
273 | s/^tech-contact;i:/*tc:/gm; |
|
|
274 | s/^updated:/*ch:/gm; |
|
|
275 | s/^street-address:/*ad:/gm; |
|
|
276 | s/^org-name:/*rm:/gm; |
|
|
277 | s/^created:/*cr:/gm; |
|
|
278 | |
|
|
279 | $_; |
|
|
280 | } |
|
|
281 | |
|
|
282 | sub ip_request { |
|
|
283 | my ($self, $ip) = @_; |
|
|
284 | |
|
|
285 | my $whois = $self->whois_request("$ip"); |
|
|
286 | |
|
|
287 | $whois =~ /^\*in: 0\.0\.0\.0 - 255\.255\.255\.255/ |
|
|
288 | and return; |
|
|
289 | |
|
|
290 | $whois =~ /^\*ac: XXX0/m # 192.0.0.0 |
|
|
291 | and return; |
|
|
292 | |
|
|
293 | $whois =~ /^%ERROR:/m |
|
|
294 | and return; |
|
|
295 | |
|
|
296 | #while ($whois =~ s/^\*(?:ac|tc):\s+(\S+)\n//m) { |
|
|
297 | # $whois .= $self->whois_request("-FSTpn $1"); |
|
|
298 | #} |
|
|
299 | |
197 | $whois =~ s/^\*(?:pn|nh|mb|ch|so|rz|ny|st|rm):.*\n//mg; |
300 | $whois =~ s/^\*(?:pn|nh|mb|ch|so|rz|ny|st|rm):.*\n//mg; |
198 | |
301 | |
199 | $whois =~ s/\n+$//; |
302 | $whois =~ s/\n+$//; |
200 | |
303 | |
201 | $whois; |
304 | $whois; |
202 | } |
305 | } |
203 | |
306 | |
204 | package netgeo; |
307 | package netgeo; |
205 | |
308 | |
|
|
309 | use Socket; |
206 | use BerkeleyDB; |
310 | use BerkeleyDB; |
207 | use Socket; |
|
|
208 | |
311 | |
209 | sub ip2int($) { |
312 | sub ip2int($) { |
210 | unpack "N", inet_aton $_[0]; |
313 | unpack "N", inet_aton $_[0]; |
211 | } |
314 | } |
212 | |
315 | |
… | |
… | |
214 | inet_ntoa pack "N", $_[0]; |
317 | inet_ntoa pack "N", $_[0]; |
215 | } |
318 | } |
216 | |
319 | |
217 | our %WHOIS; |
320 | our %WHOIS; |
218 | |
321 | |
219 | $WHOIS{ARIN} = new Whois::ARIN ARIN => "whois.arin.net", maxjobs => 12; |
322 | #$WHOIS{ARIN} = new Whois::ARIN ARIN => "whois.arin.net", port => 43, maxjobs => 12; |
|
|
323 | $WHOIS{ARIN} = new Whois::RWHOIS ARIN => "rwhois.arin.net", port => 4321, maxjobs => 12; |
220 | $WHOIS{RIPE} = new Whois::RIPE RIPE => "whois.ripe.net", maxjobs => 20; |
324 | $WHOIS{RIPE} = new Whois::RIPE RIPE => "whois.ripe.net", port => 43, rflags => "-FTin ", maxjobs => 20; |
|
|
325 | $WHOIS{APNIC} = new Whois::RIPE APNIC => "whois.apnic.net", port => 43, rflags => "-FTin ", maxjobs => 20; |
221 | $WHOIS{APNIC} = new Whois::RIPE APNIC => "whois.apnic.net", maxjobs => 20; |
326 | $WHOIS{LACNIC} = new Whois::RIPE LACNIC => "whois.lacnic.net", port => 43, maxjobs => 20; |
|
|
327 | |
|
|
328 | $whoislock = new Coro::SemaphoreSet; |
222 | |
329 | |
223 | sub ip_request { |
330 | sub ip_request { |
224 | my $ip = $_[0]; |
331 | my $ip = $_[0]; |
|
|
332 | |
|
|
333 | my $guard = $whoislock->guard($ip); |
225 | |
334 | |
226 | my $c = $iprange->db_cursor; |
335 | my $c = $iprange->db_cursor; |
227 | my $v; |
336 | my $v; |
228 | |
337 | |
229 | if (!$c->c_get((inet_aton $ip), $v, DB_SET_RANGE)) { |
338 | if (!$c->c_get((inet_aton $ip), $v, DB_SET_RANGE)) { |
230 | my ($ip0, $ip1, $whois) = split /\x0/, $v; |
339 | my ($ip0, $ip1, $whois) = split /\x0/, $v; |
231 | my $_ip = ip2int $ip; |
340 | my $_ip = ip2int $ip; |
232 | print "looked for $_ip, found $ip0, $ip1 ", length($v),"\n"; |
|
|
233 | if ($ip0 <= $_ip && $_ip <= $ip1) { |
341 | if ($ip0 <= $_ip && $_ip <= $ip1) { |
234 | return $whois; |
342 | return $whois; |
235 | } |
343 | } |
236 | } |
344 | } |
237 | |
345 | |
238 | print "looked for $ip, ONLY found $v->[0], $v->[1]\n"; |
|
|
239 | |
|
|
240 | my ($arin, $ripe, $apnic); |
346 | my ($arin, $ripe, $apnic); |
241 | |
347 | |
242 | $whois = $WHOIS{APNIC}->ip_request($ip) |
348 | $whois = $WHOIS{RIPE}->ip_request($ip) |
243 | || $WHOIS{RIPE} ->ip_request($ip) |
349 | || $WHOIS{APNIC} ->ip_request($ip) |
244 | || $WHOIS{ARIN} ->ip_request($ip); |
350 | || $WHOIS{ARIN} ->ip_request($ip) |
|
|
351 | # || $WHOIS{LACNIC}->ip_request($ip) |
|
|
352 | ; |
245 | |
353 | |
246 | $whois =~ /^\*in: ([0-9.]+)\s+-\s+([0-9.]+)\s*$/mi |
354 | $whois =~ /^\*in: ([0-9.]+)\s+-\s+([0-9.]+)\s*$/mi |
247 | or do { warn "$whois($ip): no addresses found\n", last }; |
355 | or do { warn "$whois($ip): no addresses found\n", last }; |
248 | |
356 | |
249 | my ($ip0, $ip1) = ($1, $2); |
357 | my ($ip0, $ip1) = ($1, $2); |
… | |
… | |
256 | $_ip = $_ip & 0xffffff00; |
364 | $_ip = $_ip & 0xffffff00; |
257 | $_ip0 = $_ip if $_ip0 < $_ip; |
365 | $_ip0 = $_ip if $_ip0 < $_ip; |
258 | $_ip1 = $_ip + 255 if $_ip1 > $_ip + 255; |
366 | $_ip1 = $_ip + 255 if $_ip1 > $_ip + 255; |
259 | } |
367 | } |
260 | |
368 | |
261 | print "setting entry ($_ip0, $_ip, $_ip1)\n"; |
|
|
262 | $iprange->db_put((pack "N", $_ip1), (join "\x0", $_ip0, $_ip1, $whois)); |
369 | $iprange->db_put((pack "N", $_ip1), (join "\x0", $_ip0, $_ip1, $whois)); |
263 | (tied %whois)->db_sync; |
370 | (tied %whois)->db_sync; |
264 | $iprange->db_sync; |
371 | $iprange->db_sync; |
265 | |
372 | |
266 | $whois; |
373 | $whois; |
267 | } |
374 | } |
268 | |
375 | |
|
|
376 | if (0) { |
|
|
377 | #print ip_request "68.52.164.8"; # goof |
|
|
378 | #print "\n\n"; |
|
|
379 | #print ip_request "200.202.220.222"; # lacnic |
|
|
380 | #print "\n\n"; |
|
|
381 | #print ip_request "62.116.167.250"; |
|
|
382 | #print "\n\n"; |
|
|
383 | #print ip_request "133.11.128.254"; # jp |
|
|
384 | #print "\n\n"; |
|
|
385 | print ip_request "80.131.153.93"; |
|
|
386 | print "\n\n"; |
|
|
387 | } |
269 | |
388 | |
|
|
389 | 1; |
270 | |
390 | |
|
|
391 | |