Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: Storing and searching sets of IP address ranges

by ikegami (Patriarch)
on Jul 06, 2010 at 22:52 UTC ( [id://848340]=note: print w/replies, xml ) Need Help??


in reply to Re: Storing and searching sets of IP address ranges
in thread Storing and searching sets of IP address ranges

On the other hand, who says we have to use native numbers!
use strict; use warnings; use Array::IntSpan qw( ); use Math::BigInt qw( ); use Socket qw( AF_INET6 inet_pton ); sub ip6_to_num { return Math::BigInt->new( '0x' . unpack 'H32', inet_pton AF_INET6, $_[0] ); } my $span = Array::IntSpan->new( [ ip6_to_num('2001:0db8:85a3:0000:0000:0000:0000:0000'), ip6_to_num('2001:0db8:85a3:ffff:ffff:ffff:ffff:ffff'), 'Some Network', ] ); for my $addr (qw( 2001:db8:85a3::8a2e:370:7334 2001:dc8:85a3::8a2e:370:7334 )) { my $network = $span->lookup(ip6_to_num($addr)); printf("Address %s is %s\n", $addr, $network ? "present in $network" : 'absent', ); }
Address 2001:db8:85a3::8a2e:370:7334 is present in Some Network Address 2001:dc8:85a3::8a2e:370:7334 is absent

Replies are listed 'Best First'.
Re^3: Storing and searching sets of IP address ranges
by acferen (Sexton) on Jul 07, 2010 at 12:47 UTC
    I like this as a quick way to get something working, but I think I may ultimately need to go with your first suggestion and change the operators. I already have IP addresses as 4/16 byte entities so changing the operators will save [un]pack calls for each lookup.

    Thanks,
    -Andrew

      It's not using unpack that's gonna slow down that solution, it's using overloaded operators.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://848340]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-19 14:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found