ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/myhttpd/netgeo.pl
(Generate patch)

Comparing Coro/myhttpd/netgeo.pl (file contents):
Revision 1.4 by root, Tue Aug 28 02:30:49 2001 UTC vs.
Revision 1.18 by root, Sat Dec 8 21:01:16 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines