Dave Rolsky > Net-Works-0.13 > Net::Works::Network

Download:
Net-Works-0.13.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 0.13   Source   Latest Release: Net-Works-0.16

NAME ^

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

VERSION ^

version 0.13

SYNOPSIS ^

  use Net::Works::Network;

  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->last();
  print $last->as_string();     # 1.0.0.255

  my $iterator = $network->iterator();
  while ( my $ip = $iterator->() ) { print $ip . "\n"; }

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

  # All methods work with IPv4 and IPv6 subnets
  my $ipv6_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 128-bit integers (via Math::Int128) to represent the numeric value of an address as needed.

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, e.g., "4.3.2.0/24".

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, mask_length parameter, and an optional version parameter. The integer parameter should be an integer representation of an IP within the subnet. The mask_length parameter should be an integer between 0 and 32 for IPv4 or 0 and 128 for IPv6. 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->first_as_integer()

Returns the first IP in the network as an integer. This may be a Math::Int128 object.

$network->last()

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

$network->last_as_integer()

Returns the last IP in the network as an integer. This may be a Math::Int128 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

$network->contains($address_or_network)

This method accepts a single Net::Works::Address or Net::Works::Network object. It returns true if the given address or network is contained by the network it is called on. Note that a network always contains itself.

$network->split()

This returns a list of two new network objects representing the original network split into two halves. For example, splitting 1.1.10/24 returns 1.1.1.0/25 and 1.1.1.128/25.

If the original networks is a single address network (a /32 in IPv4 or /128 in IPv6) then this method returns an empty list.

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

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. You can pass an explicit version as the final argument. If you don't, we check whether either address contains a colon (:). If either of them does, we assume you want IPv6 subnets.

AUTHORS ^

CONTRIBUTORS ^

COPYRIGHT AND LICENSE ^

This software is copyright (c) 2013 by MaxMind, Inc..

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

syntax highlighting: