Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Storing and searching sets of IP address ranges

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


in reply to Storing and searching sets of IP address ranges

IP6 addresses are 128-bit numbers. Perl can't represent those natively, which is the only reason Array::IntSpan (and thus Array::IntSpan::Fields) won't do. However, it could easily be modified by work with strings such as the packed representation of an ip4/ip6 address (by switching numerical operators <, ==, etc with string operators lt, eq, etc).

Replies are listed 'Best First'.
Re^2: Storing and searching sets of IP address ranges
by ikegami (Patriarch) on Jul 06, 2010 at 22:52 UTC
    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
      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://848328]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found