f> and I have list of following IP addresses :
f> 212.120.128.0|19;
f> 12.120.0.0|15;
f> 12.120.16.0|20;
f> 12.120.72.0|22;
f> 12.120.96.0|20;
f> 12.120.40.0|21;
f> 12.120.0.0|21;
f> 12.120.192.0|19;
f> 12.120.16.0|22;
f> 12.120.36.0|22;
f> 12.120.80.0|20;
f> 194.212.120.0|21;
f> 212.120.32.0|19;
f> 212.120.64.0|18;
f> 212.120.192.0|19;
f> 213.3.12.120|29;
f> 116.212.120.0|24;
f> 12.120.24.0|21;f> Now I need to map C_IP to list with longest prefix match. (As u can
f> there are many IP address with 12.120. but I need to map to one with
f> longest prefix match)
f> I am little confuse with largest prefix to smallest.
f> Example:
f> 12.120.16.0|20;
f> 12.120.96.0|20;
f> 12.120.40.0|21;
f> 12.120.72.0|22;
f> 12.120.16.0|22;
f> In above list what will be order of largest prefix to smallest.
f> And is there any tutorial with exmple where can I see steps to use
f> Net::Netmask
First of all, let's be clear about the problem as I see it, because I
think you haven't understood it clearly. If I'm wrong, sorry, but I'm
trying to help you.
I think you're trying to find the best (smallest) net block match for an
IP. The numbers you quote are net blocks, and you should know what that
means in IPv4 terms. Look it up if you don't.
The "longest" match you want really means the most specific net block,
meaning the net block with the most bits unmasked (thus, the fewest
addresses in it). If there's a tie by number of unmasked bits, the
first one found can win. In the example you give, either of the 22's
for example, if they match.
OK, so the code below walks through the net blocks, adds them to $table,
then finds the smallest net block in $table that matches the IPs. Do
"print Dumper $table" to see what the storage looks like, if you're
curious. I wrote it so you can give it multiple IPs from the command
line, and it doesn't catch the cases where the IP is not found or
invalid.
#!/usr/bin/perl
use warnings;
use strict;
use Data:
umper;
use Net::Netmask;
my $table = {};
while (<DATA>)
{
chomp;
my $net = new Net::Netmask ($_);
$net->storeNetblock($table);
}
print findNetblock($_, $table) . "\n" foreach @ARGV;
__DATA__
12.120.16.0/20
12.120.96.0/20
12.120.40.0/21
12.120.72.0/22
12.120.16.0/22