#!/usr/bin/perl

use warnings;
use strict;
use Socket;

my $Suffix = ".ipv4.asn.beta.bgpdns.enyo.de";

sub rewrite_ip {
    my $IP = shift;
    my @Components = split /\./, $IP;

    for my $Value (@Components) {
	return $IP if $Value > 255;
    }

    my ($name,$aliases,$addrtype,$length,@addrs) 
	= gethostbyname (join (".", reverse (@Components)) . $Suffix);
    return "$IP (bogon)" unless defined $addrtype;
    return $IP unless $addrtype == AF_INET;
    return $IP unless $length == 4;
    return $IP unless @addrs > 0;
    
    my ($d1, $d2, $ASN) = unpack "CCn", $addrs[0];
    return "$IP (bogon)" if $ASN == 0;
    return "$IP (AS $ASN)";
}
    

while (my $Line = <>) {
    chomp $Line;
    
    $Line =~ s/\d+\.\d+\.\d+\.\d+/rewrite_ip ($&)/eg;
    print "$Line\n";
}

# arch-tag: 6afef9d3-4ee5-4a0c-86cf-73d16ff3afca
