Array-IntSpan
view release on metacpan or search on metacpan
lib/Array/IntSpan/IP.pm view on Meta::CPAN
#
# This file is part of Array-IntSpan
#
# This software is Copyright (c) 2014 by Dominique Dumont.
#
# This is free software, licensed under:
#
# The Artistic License 2.0 (GPL Compatible)
#
##########################################################################
#
# Array::IntSpan::IP - a Module for arrays using IP addresses as indices
#
# Author: Toby Everett
# Revision: 1.01
# Last Change: Makefile.PL
##########################################################################
# Copyright 2000 Toby Everett. All rights reserved.
#
# This module is distributed under the Artistic 2.0 License. See
# https://www.perlfoundation.org/artistic-license-20.html
#
# For comments, questions, bugs or general interest, feel free to
# contact Toby Everett at teverett@alascom.att.com
##########################################################################
use strict;
use Array::IntSpan;
package Array::IntSpan::IP;
$Array::IntSpan::IP::VERSION = '2.004';
use vars qw($VERSION @ISA);
$VERSION = '1.01';
@ISA = ('Array::IntSpan');
sub new {
my $class = shift;
my(@temp) = @_;
foreach my $i (@temp) {
$i->[0] = &ip_as_int($i->[0]);
$i->[1] = &ip_as_int($i->[1]);
}
return $class->SUPER::new(@temp);
}
sub set_range {
my $self = shift;
my(@temp) = @_;
$temp[0] = &ip_as_int($temp[0]);
$temp[1] = &ip_as_int($temp[1]);
return $self->SUPER::set_range(@temp);
}
sub lookup {
my $self = shift;
my($key) = @_;
return $self->SUPER::lookup(&ip_as_int($key));
}
sub ip_as_int {
my($value) = @_;
if ($value =~ /^(\d{1,3}\.){3}(\d{1,3})$/) {
my(@values) = split(/\./, $value);
scalar(grep {$_ > 255} @values) and croak("Unable to parse '$value' as an IP address.");
return 16777216*$values[0]+65536*$values[1]+256*$values[2]+$values[3];
} elsif (length($value) == 4) {
return unpack('N', $value)
} elsif ($value =~ /^\d+$/) {
return int($value);
} else {
croak("Unable to parse '$value' as an IP address.");
}
}
#The following code is courtesy of Mark Jacob-Dominus,
sub croak {
require Carp;
*croak = \&Carp::croak;
goto &croak;
}
1;
__END__
=head1 NAME
Array::IntSpan::IP - a Module for arrays using IP addresses as indices
=head1 SYNOPSIS
use Array::IntSpan::IP;
my $foo = Array::IntSpan::IP->new(['123.45.67.0', '123.45.67.255', 'Network 1'],
['123.45.68.0', '123.45.68.127', 'Network 2'],
['123.45.68.128', '123.45.68.255', 'Network 3']);
print "The address 123.45.68.37 is on network ".$foo->lookup("\173\105\150\45").".\n";
unless (defined($foo->lookup(((123*256+45)*256+65)*256+67))) {
print "The address 123.45.65.67 is not on a known network.\n";
}
print "The address 123.45.68.177 is on network ".$foo->lookup("123.45.68.177").".\n";
$foo->set_range('123.45.68.128', '123.45.68.255', 'Network 4');
print "The address 123.45.68.177 is now on network ".$foo->lookup("123.45.68.177").".\n";
=head1 DESCRIPTION
C<Array::IntSpan::IP> brings the advantages of C<Array::IntSpan> to IP
address indices. Anywhere you use an index in C<Array::IntSpan>, you
can use an IP address in one of three forms in C<Array::IntSpan::IP>.
The three accepted forms are:
=over 4
( run in 0.594 second using v1.01-cache-2.11-cpan-39bf76dae61 )