The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Net::Works::Network - An object representing a single IP address (4 or 6) subnet

VERSION

version 0.06

SYNOPSIS

  my $network = Net::Works::Network->new_from_string( string => '1.0.0.0/24' );
  print $network->as_string();          # 1.0.0.0/24
  print $network->mask_length();        # 24
  print $network->bits();               # 32
  print $network->version();            # 4

  my $first = $network->first();
  print $first->as_string();    # 1.0.0.0

  my $last = $network->first();
  print $last->as_string();     # 1.0.0.255

  my $iterator = $network->iterator();
  while ( my $ip = $iterator->() ) { ... }

  my $network = Net::Works::Network->new_from_string( string => '1.0.0.4/32' );
  print $network->max_mask_length(); # 30

  # All methods work with IPv4 and IPv6 subnets
  my $network = Net::Works::Network->new_from_string( string => 'a800:f000::/20' );

  my @subnets = Net::Works::Network->range_as_subnets( '1.1.1.1', '1.1.1.32' );
  print $_->as_string, "\n" for @subnets;
  # 1.1.1.1/32
  # 1.1.1.2/31
  # 1.1.1.4/30
  # 1.1.1.8/29
  # 1.1.1.16/28
  # 1.1.1.32/32

DESCRIPTION

Objects of this class represent an IP address network. It can handle both IPv4 and IPv6 subnets. It provides various methods for getting information about the subnet.

For IPv6, it uses big integers (via Math::BigInt) to represent the numeric value of an address as needed.

This module is currently a thin wrapper around NetAddr::IP but that could change in the future.

METHODS

This class provides the following methods:

Net::Works::Network->new_from_string( ... )

This method takes a string parameter and an optional version parameter. The string parameter should be a string representation of an IP address subnet.

The version parameter should be either 4 or 6, but you don't really need this unless you're trying to force a dotted quad to be interpreted as an IPv6 network or to a force an IPv6 address colon-separated hex number to be interpreted as an IPv4 network.

Net::Works::Network->new_from_integer( ... )

This method takes an integer parameter and an optional version parameter. The integer parameter should be an integer representation of an IP within the subnet. The version parameter should be either 4 or 6.

$network->as_string()

Returns a string representation of the network like "1.0.0.0/24" or "a800:f000::/105". The IP address in the string is the first address within the subnet.

$network->version()

Returns a 4 or 6 to indicate whether this is an IPv4 or IPv6 network.

$network->mask_length()

Returns the length of the netmask as an integer.

$network->bits()

Returns the number of bit of an address in the network, which is either 32 (IPv4) or 128 (IPv6).

$network->max_mask_length()

This returns the maximum possible numeric subnet that this network could fit in. In other words, the 1.1.1.0/32 subnet could be part of the 1.1.1.0/24 subnet, so this returns 24.

$network->first()

Returns the first IP in the network as an Net::Works::Address object.

$network->last()

Returns the last IP in the network as an Net::Works::Address object.

$network->iterator()

This returns an anonymous sub that returns one IP address in the range each time it's called.

For single address subnets (/32 or /128), this returns a single address.

When it has exhausted all the addresses in the network, it returns undef

Net::Works::Network->range_as_subnets( $first, $last )

Given two IP addresses as strings, this method breaks the range up into the largest subnets that include all the IP addresses in the range (including the two passed to this method).

It also excludes any reserved subnets in the range (such as the 10.0.0.0/8 or 169.254.0.0/16 ranges).

This method works with both IPv4 and IPv6 addresses. If either address contains a colon (:) then it assumes that you want IPv6 subnets.

AUTHORS

  • Dave Rolsky <autarch@urth.org>

  • Olaf Alders <olaf@wundercounter.com>

  • Greg Oschwald <oschwald@cpan.org>

COPYRIGHT AND LICENSE

This software is Copyright (c) 2012 by MaxMind, Inc..

This is free software, licensed under:

  The Artistic License 2.0 (GPL Compatible)