Data::Semantic::Net::IPAddress::IPv4 - Semantic data class for IPv4 addresses


Data-Semantic-Net documentation Contained in the Data-Semantic-Net distribution.

Index


Code Index:

NAME

Top

Data::Semantic::Net::IPAddress::IPv4 - Semantic data class for IPv4 addresses

VERSION

Top

version 1.101760

SYNOPSIS

Top

    my $obj = Data::Semantic::Net::IPAddress::IPv4->new;
    if ($obj->is_valid('...')) {
        # ...
    }

DESCRIPTION

Top

This class can tell whether a value is an IPv4 address. The valid() method will respect the inherited forbid_internal boolean attribute.

METHODS

Top

is_internal

Takes a value and returns true if the indicated IPv4 address is an internal address, for example, 10.0.1.1, 127.0.0.1 or 192.168.1.3.

is_valid_normalized_value

FIXME

INSTALLATION

Top

See perlmodinstall for information and options on installing Perl modules.

BUGS AND LIMITATIONS

Top

No bugs have been reported.

Please report any bugs or feature requests through the web interface at http://rt.cpan.org.

AVAILABILITY

Top

The latest version of this module is available from the Comprehensive Perl Archive Network (CPAN). Visit http://www.perl.com/CPAN/ to find a CPAN site near you, or see http://search.cpan.org/dist/Data-Semantic-Net/.

The development version lives at http://github.com/hanekomu/Data-Semantic-Net/. Instead of sending patches, please fork this project using the standard git and github infrastructure.

AUTHOR

Top

  Marcel Gruenauer <marcel@cpan.org>

COPYRIGHT AND LICENSE

Top


Data-Semantic-Net documentation Contained in the Data-Semantic-Net distribution.

use 5.008;
use strict;
use warnings;

package Data::Semantic::Net::IPAddress::IPv4;
BEGIN {
  $Data::Semantic::Net::IPAddress::IPv4::VERSION = '1.101760';
}
# ABSTRACT: Semantic data class for IPv4 addresses
use Net::IP qw(ip_is_ipv4 ip_iptype ip_iptobin);
use parent qw(Data::Semantic::Net::IPAddress);

# update RESERVED IPv4 ranges according to RFC 5737
my @ranges = qw(198.51.100.0/24 203.0.113.0/24);
for my $range (@ranges) {
    my $ip = Net::IP->new($range) or die Net::IP::Error();
    my $prefix = substr $ip->binip, 0, $ip->prefixlen;
    $Net::IP::IPv4ranges{ $prefix } = 'RESERVED';
}

sub is_valid_normalized_value {
    my ($self, $value) = @_;

    # Net::IP has a "nice" DWIM feature which
    # autocompletes IPs with less than 4 octets, so check for that
    $self->SUPER::is_valid_normalized_value($value)
      && ip_is_ipv4($value)
      && (my @dummy = split(/\./, $value)) == 4;
}

sub is_internal {
    my ($self, $value) = @_;
    return unless defined $value;

    # Net::IP::ip_iptype needs the IP in binary, although this is not
    # documented.
    my $type = ip_iptype(ip_iptobin($value, 4), 4) || 'PUBLIC';
    $type ne 'PUBLIC';
}

sub normalize {
    my ($self, $value) = @_;
    return undef unless (my @dummy = split(/\./, $value)) == 4;
    # omit leading zeroes in octets, e.g., 213.160.065.064 -> 213.160.65.64
    my $ip = Net::IP->new($value) or die Net::IP::Error();
    $ip->ip;
}

1;


__END__