BGPmon-CPM-Prefix-Finder-1

 view release on metacpan or  search on metacpan

lib/BGPmon/CPM/Prefix/Finder.pm  view on Meta::CPAN

    ## elligible for expansion
    if(!$covered && $elligible){
      %ipExpanded = BGPmon::CPM::Prefix::Finder::expandIP($p);
      if( $ipExpanded{'error'} ){
        $ipExpanded{'msg'} = $ipExpanded{'error'};
        $return_set{$p} = \%ipExpanded;
        next;
      }
      if( defined($ipExpanded{'orghandle'}) 
            && $ipExpanded{'orghandle'} !~ /UNKNOWN/){
        $ipExpanded{'orgid'} = $ipExpanded{'orghandle'};
        my @nets = BGPmon::CPM::Prefix::Finder::orghandle2nets(
                                                      $ipExpanded{'orghandle'});
        my @new_prefixes;
        foreach my $net (@nets){
          push @new_prefixes,
               BGPmon::CPM::Prefix::Finder::inetnum2prefixes($net);
        }
        $ipExpanded{'nets'} = \@new_prefixes;
        push @ranges,@new_prefixes;
        $ipExpanded{'msg'} = "whois $p ($ipExpanded{'orghandle'})";

lib/BGPmon/CPM/Prefix/Finder.pm  view on Meta::CPAN

  }
  return %return_set;
}


=head2 expandIP

This subroutine looks into the whois databases and 
Input: ip address
Output: a hash with as many of the following keys as possible
"netname","inetnum","descr","country","orgid","source","netname",
"orgname","orghandle"

=cut
sub expandIP{

  my $ip = shift;
  my %org_info;

  ## step 1: query arin to find the organization (this may be under 
  ##         netname or ??)

lib/BGPmon/CPM/Prefix/Finder.pm  view on Meta::CPAN

          push @attributes, @{$obj->{'attributes'}->{'attribute'}};
        }
      }
      foreach my $att (@attributes){
        if(grep /$att->{'name'}/, 
              ("netname","inetnum","descr","source","country")){
          $org_info{$att->{'name'}} = $att->{'value'};
        }    
        if(grep /$att->{'name'}/, ("remarks")){
          if($att->{'value'} =~ /org-id:\s*(\S+)/){
            $org_info{'orgid'} = $1;
          }
        }
        
      }
    }
  ## get the data about the organization out of the original structure
  }else{
    $org_info{'source'} = 'ARIN';
    $org_info{'netname'} = $res_struct->{'net'}->{'name'}->{'$'};
    $org_info{'orgname'} = $res_struct->{'net'}->{'orgRef'}->{'@name'};

t/BGPmon-CPM-Prefix-Finder.t  view on Meta::CPAN


#########################

use Test::More skip_all => "Skipping. Be sure your database is running before manually running tests.";
use Data::Dumper;
BEGIN { use_ok('BGPmon::CPM::Prefix::Finder') };

#########################

## this ip address belongs to NAB. This call should find source, inetnum, 
## netname, orgid
my $test_ip = "164.53.220.193";
my %org_data = BGPmon::CPM::Prefix::Finder::expandIP($test_ip);
ok($org_data{'source'} eq "APNIC","expected: APNIC got: ".$org_data{'source'});
ok($org_data{'netname'} eq "NABAUS");
ok($org_data{'inetnum'} eq "164.53.0.0 - 164.53.255.255");
ok($org_data{'orgid'} eq "NAB");

## this ip address belongs to ntt america. This call should find source, 
## inetnume, netname, orghandle
$test_ip = "128.242.112.97";
%org_data = BGPmon::CPM::Prefix::Finder::expandIP($test_ip);
ok($org_data{'source'} eq "ARIN", "expected: ARIN got: " . $org_data{'source'});
ok($org_data{'netname'} eq "NTTA-128-242", "expected: NTTA-128-242 got: "
   . $org_data{'netname'});
ok($org_data{'inetnum'} eq "128.242.0.0-128.242.255.255",
   "expected 128.242.0.0-128.242.255.255 got: " . $org_data{'inetnum'});



( run in 1.325 second using v1.01-cache-2.11-cpan-5735350b133 )