ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/myhttpd/netgeo.pl
Revision: 1.12
Committed: Tue Sep 10 04:37:44 2002 UTC (21 years, 10 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.11: +2 -2 lines
Log Message:
*** empty log message ***

File Contents

# Content
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 use Coro::Event;
10 use Coro::Semaphore;
11 use Coro::SemaphoreSet;
12 use Coro::Socket;
13
14 use BerkeleyDB;
15
16 $Event::DIED = sub {
17 Event::verbose_exception_handler(@_);
18 #Event::unloop_all();
19 };
20
21 tie %netgeo::whois, BerkeleyDB::Btree,
22 -Env => $db_env,
23 -Filename => "whois",
24 -Flags => DB_CREATE,
25 or die "unable to create/open whois table";
26 $netgeo::iprange = new BerkeleyDB::Btree
27 -Env => $db_env,
28 -Filename => "iprange",
29 -Flags => DB_CREATE,
30 or die "unable to create/open iprange table";
31
32 package Whois;
33
34 use Coro::Event;
35
36 sub new {
37 my $class = shift;
38 my $name = shift;
39 my $ip = shift;
40 my $self = bless { name => $name, ip => $ip, @_ }, $class;
41 $self->{maxjobs} = new Coro::Semaphore $self->{maxjobs} || 1;
42 $self;
43 }
44
45 sub ip {
46 $_[0]{ip};
47 }
48
49 sub sanitize {
50 $_[1];
51 }
52
53 sub whois_request {
54 my ($self, $query) = @_;
55
56 my $id = "$self->{name}\x0$query";
57 my $whois = $netgeo::whois{$id};
58
59 unless (defined $whois) {
60 print "WHOIS($self->{name},$query)\n";
61
62 my $guard = $self->{maxjobs}->guard;
63 my $timeout = 5;
64
65 while () {
66 my $fh = new Coro::Socket
67 PeerAddr => $self->ip,
68 PeerPort => $self->{port} || "whois",
69 Timeout => 30;
70 if ($fh) {
71 print $fh "$query\n";
72 $fh->read($whois, 16*1024); # max 16k. whois stored
73 close $fh;
74 $whois =~ s/\015?\012/\n/g;
75 $whois = $self->sanitize($whois);
76 if ($whois eq ""
77 or ($whois =~ /query limit/i && $whois =~ /exceeded/i) # ARIN
78 or ($whois =~ /wait a while and try again/i) # ARIN
79 or ($whois =~ /^%ERROR:202:/) # RIPE/APNIC
80 ) {
81 print "retrying in $timeout seconds\n";#d#
82 do_timer(desc => "timer2", after => $timeout);
83 $timeout *= 2;
84 $timeout = 1 if $timeout > 600;
85 } else {
86 last;
87 }
88 }
89 }
90
91 $netgeo::whois{$id} = $whois;
92 }
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 return () if $whois =~ /^No match/;
121
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 #return if $handle =~ /-(RIPE|APNIC)/; # heuristic, bbut bad because ripe might not have better info
127 }
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 use Socket;
173 use base Whois;
174
175 sub sanitize {
176 local $_ = $_[1];
177
178 s/^%.*\n//gm;
179 s/^\n+//;
180 s/\n*$/\n/;
181
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 s/^remarks:\s+/*rm: /gm;
189 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 $whois =~ /^\*ac: XXX0/m # 192.0.0.0
231 and return;
232
233 $whois =~ /^\*st: (?:ALLOCATED )?UNSPECIFIED/m
234 and return;
235
236 $whois =~ /^%ERROR:/m
237 and return;
238
239 #while ($whois =~ s/^\*(?:ac|tc):\s+(\S+)\n//m) {
240 # $whois .= $self->whois_request("-FSTpn $1");
241 #}
242
243 #$whois =~ s/^\*(?:pn|nh|mb|ch|so|rz|ny|st|rm):.*\n//mg;
244
245 $whois =~ s/\n+$//;
246
247 $whois;
248 }
249
250 package Whois::RWHOIS;
251
252 use base Whois;
253
254 sub sanitize {
255 local $_ = $_[1];
256 s/^%referral\s+/referral:/gm;
257 s/^network://gm;
258 s/^%.*\n//gm;
259 s/^\n+//m;
260 s/\n*$/\n/m;
261
262 s/^(\S+):\s*/\L$1: /gm;
263 s/^ip-network-block:/*in:/gm;
264 s/^country-code:/*cy:/gm;
265 s/^tech-contact;i:/*tc:/gm;
266 s/^updated:/*ch:/gm;
267 s/^street-address:/*ad:/gm;
268 s/^org-name:/*rm:/gm;
269 s/^created:/*cr:/gm;
270
271 $_;
272 }
273
274 sub ip_request {
275 my ($self, $ip) = @_;
276
277 my $whois = $self->whois_request("$ip");
278
279 $whois =~ /^\*in: 0\.0\.0\.0 - 255\.255\.255\.255/
280 and return;
281
282 $whois =~ /^\*ac: XXX0/m # 192.0.0.0
283 and return;
284
285 $whois =~ /^%ERROR:/m
286 and return;
287
288 #while ($whois =~ s/^\*(?:ac|tc):\s+(\S+)\n//m) {
289 # $whois .= $self->whois_request("-FSTpn $1");
290 #}
291
292 $whois =~ s/^\*(?:pn|nh|mb|ch|so|rz|ny|st|rm):.*\n//mg;
293
294 $whois =~ s/\n+$//;
295
296 $whois;
297 }
298
299 package netgeo;
300
301 use Socket;
302 use BerkeleyDB;
303
304 sub ip2int($) {
305 unpack "N", inet_aton $_[0];
306 }
307
308 sub int2ip($) {
309 inet_ntoa pack "N", $_[0];
310 }
311
312 our %WHOIS;
313
314 #$WHOIS{ARIN} = new Whois::ARIN ARIN => "whois.arin.net", port => 43, maxjobs => 12;
315 $WHOIS{ARIN} = new Whois::RWHOIS ARIN => "rwhois.arin.net", port => 4321, maxjobs => 12;
316 $WHOIS{RIPE} = new Whois::RIPE RIPE => "whois.ripe.net", port => 43, rflags => "-FSTin ", maxjobs => 20;
317 $WHOIS{APNIC} = new Whois::RIPE APNIC => "whois.apnic.net", port => 43, rflags => "-FSTin ", maxjobs => 20;
318 $WHOIS{LACNIC} = new Whois::RIPE LACNIC => "whois.lacnic.net", port => 43, maxjobs => 20;
319
320 $whoislock = new Coro::SemaphoreSet;
321
322 sub ip_request {
323 my $ip = $_[0];
324
325 my $guard = $whoislock->guard($ip);
326
327 my $c = $iprange->db_cursor;
328 my $v;
329
330 if (!$c->c_get((inet_aton $ip), $v, DB_SET_RANGE)) {
331 my ($ip0, $ip1, $whois) = split /\x0/, $v;
332 my $_ip = ip2int $ip;
333 if ($ip0 <= $_ip && $_ip <= $ip1) {
334 return $whois;
335 }
336 }
337
338 my ($arin, $ripe, $apnic);
339
340 $whois = $WHOIS{RIPE}->ip_request($ip)
341 || $WHOIS{LACNIC}->ip_request($ip)
342 || $WHOIS{APNIC} ->ip_request($ip)
343 || $WHOIS{ARIN} ->ip_request($ip)
344 ;
345
346 $whois =~ /^\*in: ([0-9.]+)\s+-\s+([0-9.]+)\s*$/mi
347 or do { warn "$whois($ip): no addresses found\n", last };
348
349 my ($ip0, $ip1) = ($1, $2);
350
351 my $_ip = ip2int($ip);
352 my $_ip0 = ip2int($ip0);
353 my $_ip1 = ip2int($ip1);
354
355 if ($_ip0 + 256 < $_ip1) {
356 $_ip = $_ip & 0xffffff00;
357 $_ip0 = $_ip if $_ip0 < $_ip;
358 $_ip1 = $_ip + 255 if $_ip1 > $_ip + 255;
359 }
360
361 $iprange->db_put((pack "N", $_ip1), (join "\x0", $_ip0, $_ip1, $whois));
362 (tied %whois)->db_sync;
363 $iprange->db_sync;
364
365 $whois;
366 }
367
368 if (0) {
369 #print ip_request "68.52.164.8"; # goof
370 #print "\n\n";
371 #print ip_request "200.202.220.222"; # lacnic
372 #print "\n\n";
373 #print ip_request "62.116.167.250";
374 #print "\n\n";
375 #print ip_request "133.11.128.254"; # jp
376 #print "\n\n";
377 #print ip_request "151.197.52.251";
378 #print "\n\n";
379 }
380
381 1;
382
383