llarochelle has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks, I need advice on how to deal with a large amount of ip addresses / subnets. I know there are modules out there, like Net::Netmask for instance, but I wonder if it could be achieved from core ? This is not a constraint, I'd like to have a discussion on this matter. For searching / matching purposes : Let's say I build a hash where the level would match the octets. Something like a tree ... Given IP addresses : ( 10.10.10.1 , 10.11.10.1, 11.10.0.1) The first level would contain 10 and 11, but the second level of 10 would contain 10, 11 and so on. Is this a good approach ? I know my whole question is not clear. I'm still confused. Best regards, Luc
  • Comment on Matching an IP/subnet amongst a large number of ip subnets

Replies are listed 'Best First'.
Re: Matching an IP/subnet amongst a large number of ip subnets
by Discipulus (Canon) on Oct 31, 2018 at 16:31 UTC
    Hello llarochelle,

    I used in the past NetAddr::IP that understands cidrs and has the useful within method.

    Example fragment:

    my $ip = new NetAddr::IP($address_given); my %nets; map { $nets{$_}= new NetAddr::IP($_),} qw( 192.168.0.0/29 10.10.10.0/ +24); foreach my $k (keys %nets){ if ($ip->within($nets{$k})) {

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Matching an IP/subnet amongst a large number of ip subnets
by hippo (Archbishop) on Oct 31, 2018 at 14:51 UTC
    I know there are modules out there, like Net::Netmask for instance, but I wonder if it could be achieved from core ?

    Everything can be achieved from core. If you know a module (like Net::Netmask) which does what you want, simply inspect the source (and the source of its dependencies) and do it that way yourself.

    FWIW, I like Net::CIDR::Lite.

Re: Matching an IP/subnet amongst a large number of ip subnets
by haukex (Archbishop) on Oct 31, 2018 at 15:32 UTC
    I wonder if it could be achieved from core ? This is not a constraint, I'd like to have a discussion on this matter.

    If there is a module, and it seems like there is, then what advantage would you gain from not using it? With a module, you get already tested code, with the possibility of updating the module independently, and so on...

Re: Matching an IP/subnet amongst a large number of ip subnets
by llarochelle (Beadle) on Nov 01, 2018 at 15:29 UTC
    Thank you for all your precious comments. I'll look into your favorite modules and see if I can benefit from it. Then I'll look inside and see if I can reuse some code for those one-of-a-kind basic applications where I'd prefer to use core only. Cheers !