North American Network Operators Group

Date Prev | Date Next | Date Index | Thread Index | Author Index | Historical

Re: IP Prefixes are allocated ..

  • From: Florian Weimer
  • Date: Mon Nov 28 02:21:47 2005

* Christopher L. Morrow:

> he might be satisfied with:
>
> mail.pch.net.           86400   IN      A       206.220.231.1
>
> :~> host -W 6 -R 10 -t txt  1.231.220.206.asn.routeviews.org
> 1.231.220.206.asn.routeviews.org text "3856" "206.220.228.0" "22"
>
> which is AS 3856 routing 206.220.228.0/22 ... which contains the /32
> above.

asn.routeviews.org doesn't do longest-prefix matching, so you need a
short Perl script to get the correct ASN, attached below.  However,
this is a bit slow thanks to the overhead of loading Net::DNS, see
<http://www.enyo.de/fw/notes/perl-probleme.html> (German).

#!/usr/bin/perl

use warnings;
use strict;
use Net::DNS;

if (@ARGV != 1 && $ARGV[0] !~ /^\d+\.\d+\.\d+\.\d+$/) {
    print STDERR "usage: ip2asn A.B.C.D\n";
    exit 1;
}

my $suffix = 'asn.routeviews.org';
my $name = join '.', (reverse split /\./, $ARGV[0]), $suffix;

my $res = Net::DNS::Resolver->new;
my $packet = $res->query($name, 'TXT');
my @txt;
@txt = $packet->answer if $packet;

my ($longest_net, $longest_length, $longest_asn);
for my $rr (@txt) {
    my ($asn, $net, $length) = $rr->char_str_list;
    if ((! defined $longest_length) || $length > $longest_length) {
	$longest_net = $net;
	$longest_length = $length;
	$longest_asn = $asn;
    }
}

if (defined $longest_asn && $longest_asn < 64511) {
    print "$longest_asn\n";
} else {
    print "0\n";
}