Net::Netmask - parse, manipulate and lookup IP network blocks |
Net::Netmask - parse, manipulate and lookup IP network blocks
use Net::Netmask;
$block = new Net::Netmask (network block) $block = new Net::Netmask (network block, netmask) $block = new2 Net::Netmask (network block) $block = new2 Net::Netmask (network block, netmask)
print $block; # a.b.c.d/bits print $block->base() print $block->mask() print $block->hostmask() print $block->bits() print $block->size() print $block->maxblock() print $block->broadcast() print $block->next() print $block->match($ip); print $block->nth(1, [$bitstep]);
if ($block->sameblock("network block")) ... if ($block->cmpblocks("network block")) ...
$newblock = $block->nextblock([count]);
for $ip ($block->enumerate([$bitstep])) { }
for $zone ($block->inaddr()) { }
my $table = {}; $block->storeNetblock([$table]) $block->deleteNetblock([$table]) @missingblocks = $block->cidrs2inverse(@blocks)
$block = findNetblock(ip, [$table]) $block = findOuterNetblock(ip, [$table]) @blocks = findAllNetblock(ip, [$table]) if ($block->checkNetblock([$table]) ... $block2 = $block1->findOuterNetblock([$table]) @blocks = dumpNetworkTable([$table])
@blocks = range2cidrlist($beginip, $endip); @blocks = cidrs2cidrs(@blocks_with_dups)
@listofblocks = cidrs2contiglists(@blocks);
@blocks = sort @blocks @blocks = sort_network_blocks(@blocks)
@sorted_ip_addrs = sort_by_ip_address(@unsorted_ip_addrs)
Net::Netmask parses and understands IPv4 CIDR blocks. It's built with an object-oriented interface. Nearly all functions are methods that operate on a Net::Netmask object.
There are methods that provide the nearly all bits of information about a network block that you might want.
There are also functions to put a network block into a table and then later lookup network blocks by IP address in that table. There are functions to turn a IP address range into a list of CIDR blocks. There are functions to turn a list of CIDR blocks into a list of IP addresses.
There is a function for sorting by text IP address.
Net::Netmask objects are created with an IP address and optionally a mask. There are many forms that are recognized:
There are two constructor methods: new
and new2
. The difference
is that new2
will return undef for invalid netmasks and new
will
return a netmask object even if the constructor could not figure out
what the network block should be.
With new
, the error string can be found as $block->{'ERROR'}. With
new2
the error can be found as Net::Netmask::errstr or
$Net::Netmask::error.
base()
amd broadcast().
For example, if we have the network 192.168.1.0/24, then
192.168.0.255 => 0 192.168.1.0 => "0 " 192.168.1.1 => 1 ... 192.168.1.255 => 255
$ip should be a dotted-quad (eg: ``192.168.66.3'')
It just happens that the return value is the position within the block. Since zero is a legal position, the true string ``0 '' is returned in it's place. ``0 '' is numerically zero though. When wanting to know the position inside the block, a good idiom is:
$pos = $block->match($ip) or die; $pos += 0;
If the optional argument is given, step through the block in increments of a given network size. To step by 4, use a bitstep of 30 (as in a /30 network).
Each tuple contains: the DNS zone name, the last component of the first IP address in the block in that zone, the last component of the last IP address in the block in that zone.
Examples: the list returned for the block '216.240.32.0/23' would be: '32.240.216.in-addr.arpa', 0, 255, '33.240.216.in-addr.arpa', 0, 255. The list returned for the block '216.240.32.64/27' would be: '32.240.216.in-addr.arpa', 64, 95.
The optional argument allows there to be more than one table. By default, an internal table is used. If more than one table is needed, then supply a reference to a HASH to store the data in.
The optional argument allows there to be more than one table. By default, an internal table is used. If more than one table is needed, then supply a reference to a HASH to store the data in.
The return value is either a Net::Netmask object or undef.
The block will be auto-converted from a string if it isn't already a Net::Netmask object. The list of blocks should be Net::Netmask objects.
The return value is a list of Net::Netmask objects.
Overloading doesn't seem to work completeley on perl before version 5.6.1. The test suite doesn't test overloading before that. At least for sort.
Net::Netmask
simply because
there doesn't seem to be a better place to put it on CPAN.
It turns out that there is one method for sorting dotted-quads
(``a.b.c.d'') that is faster than all the rest. This is that
way. Use it as sort_by_ip_address(@list_of_ips)
. That was
the theory anyway. Someone sent a faster version ...
sort @blocks
that also works.
The return value is either a Net::Netmask object or undef.
The return value is a list of Net::Netmask objects.
For example, range2cidrlist('216.240.32.128', '216.240.36.127'), will return a list of Net::Netmask objects that corrospond to:
216.240.32.128/25 216.240.33.0/24 216.240.34.0/23 216.240.36.0/25
cidrs2contiglists
will rearrange a list of Net::Netmask objects
such that contiguous sets are in sublists and each sublist is
discontigeous with the next.
For example, given a list of Net::Netmask objects corresponding to the following blocks:
216.240.32.128/25 216.240.33.0/24 216.240.36.0/25
cidrs2contiglists
will return a list with two sublists:
216.240.32.128/25 216.240.33.0/24
216.240.36.0/25
Overlapping blocks will be placed in the same sublist.
cidrs2cidrs
will collapse a list of Net::Netmask objects by
combining adjacent blocks into larger blocks. It returns a list
of blocks that covers exactly the same IP space. Overlapping
blocks will be collapsed.
Copyright (C) 1998-2006 David Muir Sharnoff. License hereby granted for anyone to use, modify or redistribute this module at their own risk. Please feed useful changes back to <muir@idiom.com>.
If you found this module useful, please thank me (the author) by giving me a chance to bid to provide your next high-speed Internet link. I run multiple ISPs and can generally save you money and provide a top-notch service for your T1s, T3s, OC3s, etc. Please send your request to <muir@idiom.com>. Thank you.
Net::Netmask - parse, manipulate and lookup IP network blocks |