ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/myhttpd/netgeo.pl
Revision: 1.20
Committed: Fri Jan 29 22:29:04 2010 UTC (14 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.19: +18 -13 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #!/usr/bin/perl
2    
3     # APNIC refer: KRNIC (for 211.104.0.0)
4    
5     use Socket;
6     use Fcntl;
7    
8     use Coro;
9 root 1.18 use Coro::EV;
10 root 1.1 use Coro::Semaphore;
11 root 1.6 use Coro::SemaphoreSet;
12 root 1.1 use Coro::Socket;
13 root 1.13 use Coro::Timer;
14 root 1.1
15 root 1.7 use BerkeleyDB;
16    
17 root 1.4 tie %netgeo::whois, BerkeleyDB::Btree,
18     -Env => $db_env,
19     -Filename => "whois",
20     -Flags => DB_CREATE,
21     or die "unable to create/open whois table";
22     $netgeo::iprange = new BerkeleyDB::Btree
23     -Env => $db_env,
24     -Filename => "iprange",
25     -Flags => DB_CREATE,
26     or die "unable to create/open iprange table";
27    
28 root 1.1 package Whois;
29    
30 root 1.18 use Coro::EV;
31 root 1.1
32     sub new {
33     my $class = shift;
34     my $name = shift;
35     my $ip = shift;
36     my $self = bless { name => $name, ip => $ip, @_ }, $class;
37     $self->{maxjobs} = new Coro::Semaphore $self->{maxjobs} || 1;
38     $self;
39     }
40    
41     sub ip {
42     $_[0]{ip};
43     }
44    
45     sub sanitize {
46     $_[1];
47     }
48    
49     sub whois_request {
50     my ($self, $query) = @_;
51    
52 root 1.4 my $id = "$self->{name}\x0$query";
53     my $whois = $netgeo::whois{$id};
54    
55     unless (defined $whois) {
56     print "WHOIS($self->{name},$query)\n";
57 root 1.1
58     my $guard = $self->{maxjobs}->guard;
59     my $timeout = 5;
60    
61     while () {
62     my $fh = new Coro::Socket
63     PeerAddr => $self->ip,
64 root 1.10 PeerPort => $self->{port} || "whois",
65 root 1.1 Timeout => 30;
66     if ($fh) {
67     print $fh "$query\n";
68     $fh->read($whois, 16*1024); # max 16k. whois stored
69     close $fh;
70     $whois =~ s/\015?\012/\n/g;
71     $whois = $self->sanitize($whois);
72     if ($whois eq ""
73     or ($whois =~ /query limit/i && $whois =~ /exceeded/i) # ARIN
74     or ($whois =~ /wait a while and try again/i) # ARIN
75     or ($whois =~ /^%ERROR:202:/) # RIPE/APNIC
76     ) {
77     print "retrying in $timeout seconds\n";#d#
78     do_timer(desc => "timer2", after => $timeout);
79     $timeout *= 2;
80     $timeout = 1 if $timeout > 600;
81     } else {
82     last;
83     }
84 root 1.13 } else {
85     # only retry once a minute
86     print STDERR "unable to connect to $self->{ip} ($self->{name}), retrying...\n";
87 root 1.14 Coro::Timer::sleep 300;
88 root 1.1 }
89     }
90    
91 root 1.4 $netgeo::whois{$id} = $whois;
92 root 1.1 }
93    
94     $whois;
95     }
96    
97     package Whois::ARIN;
98    
99     use Date::Parse;
100    
101     use base Whois;
102    
103     sub sanitize {
104     local $_ = $_[1];
105     s/\n[\t ]{6,}([0-9.]+ - [0-9.]+)/ $1/g;
106     $_;
107     }
108    
109     # there are only two problems with arin's whois database:
110     # a) the data cannot be trusted and often is old or even wrong
111     # b) the database format is nonparsable
112     # (no spaces between netname/ip and netnames can end in digits ;)
113     # of course, the only source to find out about global
114     # address distribution is... arin.
115     sub ip_request {
116     my ($self, $ip) = @_;
117    
118     my $whois = $self->whois_request($ip);
119    
120 root 1.16 return if $whois =~ /^No match/;
121 root 1.1
122     if ($whois =~ /^To single out one record/m) {
123     my $handle;
124     while ($whois =~ /\G\S.*\(([A-Z0-9\-]+)\).*\n/mg) {
125     $handle = $1;
126 root 1.16 #return if $handle =~ /-(RIPE|APNIC)/; # heuristic, but bad because ripe might not have better info
127 root 1.1 }
128     $handle or die "$whois ($ip): unparseable multimatch\n";
129     $whois = $self->whois_request("!$handle");
130     }
131    
132     my ($address, $info, $coordinator, undef) = split /\n\n/, $whois;
133    
134     $info =~ /^\s+Netname: (\S+)$/mi
135     or die "$whois($ip): no netname\n";
136     my $netname = $1;
137    
138     $info =~ /^\s+Netblock: ([0-9.]+\s+-\s+[0-9.]+)\s*$/mi
139     or die "$whois($ip): no netblock\n";
140     my $netblock = $1;
141    
142     my $maintainer;
143    
144     if ($info =~ /^\s+Maintainer: (\S+)\s*$/mi) {
145     $maintainer = "*ma: $1\n";
146     return if $1 =~ /^(?:AP|RIPE)$/;
147     }
148    
149     $coordinator =~ s/^\s+Coordinator:\s*//si
150     or $coordinator = "";
151    
152     $address =~ s/\n\s*(\S+)$//
153     or die "$whois($ip): no parseable country ($address)\n";
154     my $country = $1;
155    
156     $address =~ s/^\s*/*de: /mg;
157     $coordinator =~ s/^\s*/*ad: /mg;
158    
159     $whois = <<EOF;
160     *in: $netblock
161     *na: $netname
162     *cy: $country
163     $maintainer$address
164     $coordinator
165     EOF
166     $whois =~ s/\n+$//;
167     $whois;
168     }
169    
170     package Whois::RIPE;
171    
172 root 1.10 use Socket;
173 root 1.1 use base Whois;
174    
175     sub sanitize {
176     local $_ = $_[1];
177 root 1.10
178 root 1.1 s/^%.*\n//gm;
179     s/^\n+//;
180     s/\n*$/\n/;
181 root 1.10
182     s/^inetnum:\s+/*in: /gm;
183     s/^admin-c:\s+/*ac: /gm;
184     s/^tech-c:\s+/*tc: /gm;
185     s/^owner-c:\s+/*oc: /gm;
186     s/^country:\s+/*cy: /gm;
187     s/^phone:\s+/*ph: /gm;
188 root 1.12 s/^remarks:\s+/*rm: /gm;
189 root 1.10 s/^changed:\s+/*ch: /gm;
190     s/^created:\s+/*cr: /gm;
191     s/^address:\s+/*ad: /gm;
192     s/^status:\s+/*st: /gm;
193     s/^inetrev:\s+/*ir: /gm;
194     s/^nserver:\s+/*ns: /gm;
195    
196     $_;
197     }
198    
199     sub ip_request {
200     my ($self, $ip) = @_;
201    
202     my $whois = $self->whois_request("$self->{rflags}$ip");
203    
204     $whois =~ s{
205     (2[0-5][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])
206     (?:\.
207     (2[0-5][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])
208     (?:\.
209     (2[0-5][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])
210     (?:\.
211     (2[0-5][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])
212     )?
213     )?
214     )?
215     /
216     ([0-9]+)
217     }{
218     my $ip = inet_aton sprintf "%d.%d.%d.%d", $1, $2, $3, $4;
219     my $net = 1 << (31 - $5);
220     my $mask = inet_aton 2 ** 32 - $net;
221    
222     my $ip1 = $ip & $mask;
223     my $ip2 = $ip1 | inet_aton $net * 2 - 1;
224     (inet_ntoa $ip1) . " - " . (inet_ntoa $ip2);
225     }gex;
226    
227     $whois =~ /^\*in: 0\.0\.0\.0 - 255\.255\.255\.255/
228     and return;
229    
230 root 1.20 $whois =~ /^\*na: ERX-NETBLOCK/m # ripe(?) 146.230.128.210
231     and return;
232    
233     $whois =~ /^\*de: This network range is not allocated to /m # APNIC e.g. 24.0.0.0
234 root 1.17 and return;
235    
236 root 1.20 $whois =~ /^\*de: Not allocated by APNIC/m # APNIC e.g. 189.47.24.97
237 root 1.19 and return;
238    
239 root 1.10 $whois =~ /^\*ac: XXX0/m # 192.0.0.0
240     and return;
241    
242     $whois =~ /^\*st: (?:ALLOCATED )?UNSPECIFIED/m
243     and return;
244    
245     $whois =~ /^%ERROR:/m
246     and return;
247    
248     #while ($whois =~ s/^\*(?:ac|tc):\s+(\S+)\n//m) {
249     # $whois .= $self->whois_request("-FSTpn $1");
250     #}
251    
252     #$whois =~ s/^\*(?:pn|nh|mb|ch|so|rz|ny|st|rm):.*\n//mg;
253    
254     $whois =~ s/\n+$//;
255    
256     $whois;
257     }
258    
259     package Whois::RWHOIS;
260    
261     use base Whois;
262    
263     sub sanitize {
264     local $_ = $_[1];
265     s/^%referral\s+/referral:/gm;
266     s/^network://gm;
267     s/^%.*\n//gm;
268     s/^\n+//m;
269     s/\n*$/\n/m;
270    
271     s/^(\S+):\s*/\L$1: /gm;
272     s/^ip-network-block:/*in:/gm;
273     s/^country-code:/*cy:/gm;
274     s/^tech-contact;i:/*tc:/gm;
275     s/^updated:/*ch:/gm;
276     s/^street-address:/*ad:/gm;
277 root 1.12 s/^org-name:/*rm:/gm;
278 root 1.10 s/^created:/*cr:/gm;
279    
280 root 1.1 $_;
281     }
282    
283     sub ip_request {
284     my ($self, $ip) = @_;
285    
286 root 1.10 my $whois = $self->whois_request("$ip");
287 root 1.1
288     $whois =~ /^\*in: 0\.0\.0\.0 - 255\.255\.255\.255/
289     and return;
290    
291     $whois =~ /^\*ac: XXX0/m # 192.0.0.0
292     and return;
293    
294     $whois =~ /^%ERROR:/m
295     and return;
296    
297     #while ($whois =~ s/^\*(?:ac|tc):\s+(\S+)\n//m) {
298     # $whois .= $self->whois_request("-FSTpn $1");
299     #}
300    
301     $whois =~ s/^\*(?:pn|nh|mb|ch|so|rz|ny|st|rm):.*\n//mg;
302    
303     $whois =~ s/\n+$//;
304    
305     $whois;
306     }
307    
308 root 1.4 package netgeo;
309    
310     use Socket;
311 root 1.8 use BerkeleyDB;
312 root 1.1
313     sub ip2int($) {
314     unpack "N", inet_aton $_[0];
315     }
316    
317     sub int2ip($) {
318     inet_ntoa pack "N", $_[0];
319     }
320    
321     our %WHOIS;
322    
323 root 1.20 #$WHOIS{ARIN} = new Whois::ARIN ARIN => "whois.arin.net", port => 43, maxjobs => 12;
324     $WHOIS{ARIN} = new Whois::RWHOIS ARIN => "rwhois.arin.net", port => 4321, maxjobs => 1;
325     $WHOIS{RIPE} = new Whois::RIPE RIPE => "whois.ripe.net", port => 43, rflags => "-FTin ", maxjobs => 1;
326     $WHOIS{AFRINIC} = new Whois::RIPE AFRINIC => "whois.afrinic.net", port => 43, rflags => "-FTin ", maxjobs => 1;
327     $WHOIS{APNIC} = new Whois::RIPE APNIC => "whois.apnic.net", port => 43, rflags => "-FTin ", maxjobs => 1;
328     $WHOIS{LACNIC} = new Whois::RIPE LACNIC => "whois.lacnic.net", port => 43, maxjobs => 1;
329 root 1.1
330 root 1.6 $whoislock = new Coro::SemaphoreSet;
331    
332 root 1.1 sub ip_request {
333     my $ip = $_[0];
334 root 1.6
335     my $guard = $whoislock->guard($ip);
336 root 1.1
337 root 1.4 my $c = $iprange->db_cursor;
338     my $v;
339    
340     if (!$c->c_get((inet_aton $ip), $v, DB_SET_RANGE)) {
341     my ($ip0, $ip1, $whois) = split /\x0/, $v;
342     my $_ip = ip2int $ip;
343     if ($ip0 <= $_ip && $_ip <= $ip1) {
344     return $whois;
345 root 1.1 }
346 root 1.4 }
347    
348     my ($arin, $ripe, $apnic);
349    
350 root 1.9 $whois = $WHOIS{RIPE}->ip_request($ip)
351 root 1.10 || $WHOIS{APNIC} ->ip_request($ip)
352 root 1.20 || $WHOIS{AFRINIC} ->ip_request($ip)
353     || $WHOIS{LACNIC}->ip_request($ip)
354 root 1.9 || $WHOIS{ARIN} ->ip_request($ip)
355 root 1.10 ;
356 root 1.1
357 root 1.4 $whois =~ /^\*in: ([0-9.]+)\s+-\s+([0-9.]+)\s*$/mi
358     or do { warn "$whois($ip): no addresses found\n", last };
359 root 1.1
360 root 1.4 my ($ip0, $ip1) = ($1, $2);
361    
362     my $_ip = ip2int($ip);
363     my $_ip0 = ip2int($ip0);
364     my $_ip1 = ip2int($ip1);
365    
366     if ($_ip0 + 256 < $_ip1) {
367     $_ip = $_ip & 0xffffff00;
368     $_ip0 = $_ip if $_ip0 < $_ip;
369     $_ip1 = $_ip + 255 if $_ip1 > $_ip + 255;
370 root 1.1 }
371 root 1.4
372     $iprange->db_put((pack "N", $_ip1), (join "\x0", $_ip0, $_ip1, $whois));
373     (tied %whois)->db_sync;
374     $iprange->db_sync;
375 root 1.1
376     $whois;
377 root 1.10 }
378    
379 root 1.20 sub clear_cache() {
380     %netgeo::whois = ();
381     $netgeo::iprange->truncate (my $dummy);
382     }
383    
384 root 1.10 if (0) {
385     #print ip_request "68.52.164.8"; # goof
386     #print "\n\n";
387     #print ip_request "200.202.220.222"; # lacnic
388     #print "\n\n";
389     #print ip_request "62.116.167.250";
390     #print "\n\n";
391     #print ip_request "133.11.128.254"; # jp
392     #print "\n\n";
393 root 1.15 print ip_request "80.131.153.93";
394     print "\n\n";
395 root 1.1 }
396    
397 root 1.11 1;
398 root 1.1
399