SYNOPSIS

use Net::IPAddress;

@ISA = qw(Net::IPAddress);

DESCRIPTION

\*(C`Net::IPAddr\*(C' is a collection of helpful functions used to convert \s-1IP\s0 addresses to/from 32-bit integers, applying subnet masks to \s-1IP\s0 addresses, validating \s-1IP\s0 address strings, and splitting a \s-1FQDN\s0 into its host and domain parts.

No rocket science here, but I have found these functions to very, very handy. For example, have you ever tried to sort a list of \s-1IP\s0 addresses only to find out that they don't sort the way you expected? Here is the solution! If you convert the \s-1IP\s0 addresses to 32-bit integer addresses, they will sort in correct order.

ip2num( \s-1STRING\s0 )

Returns the 32-bit integer of the passed \s-1IP\s0 address string. \*(C`$ipnum = ip2num("10.1.1.1");\*(C' $ipnum is 167837953.

num2ip( \s-1INTEGER\s0 )

Returns the \s-1IP\s0 address string of the passed 32-bit \s-1IP\s0 address. \*(C`$IP = num2ip(167837953);\*(C' $IP is \*(L"10.1.1.1\*(R".

validaddr( \s-1STRING\s0 )

Returns true (1) if the \s-1IP\s0 address string is a valid and properly formatted \s-1IP\s0 address, and false (0) otherwise. \*(C`$valid = validaddr("10.1.2.1");\*(C'  # returns true \*(C`$valid = validaddr("10.1.2.");\*(C'   # returns false! If you have your own \s-1IP\s0 address validator, try the last one. Most will incorrectly compute that as a valid address.

mask( \s-1IPADDRESS\s0, \s-1MASK\s0 )

Returns the result of binary (\s-1IPADDRESS\s0 & \s-1MASK\s0). \s-1IPADDRESS\s0 can be either an \s-1IP\s0 address string or a 32-bit integer address. \s-1MASK\s0 can be either an \s-1IP\s0 address string, or the number of bits in the mask. The returned value will be in the same format as the passed \s-1IP\s0 address. If you pass an \s-1IP\s0 address string, then an \s-1IP\s0 address string is returned, if you pass a 32-bit integer address then a 32-bit integer address is returned. Examples

\*(C`$subnet = mask("10.96.3.2",16);\*(C' # $subnet = \*(L"10.96.0.0\*(R" \*(C`$subnet = mask("10.21.4.22","255.240.0.0");\*(C' # $subnet = \*(L"10.16.0.0\*(R" \*(C`$subnet = mask(167837953,"255.255.255.0");\*(C' # $subnet = 167837952>

This function, when used with the others, is very useful for computing \s-1IP\s0 addresses. For example, you need to add another server to a subnet that an existing server is on. You want the new server to be the \*(L".17\*(R" address of a /24 subnet. This is done easily in the following example:

\*(C`use Net::IPAddress\*(C' \*(C`$server = "10.8.9.12";\*(C' \*(C`$newserver = num2ip(ip2num(mask($server,24)) + 17);\*(C' \*(C`print "New server IP is $newserver\n";\*(C' \*(C`New server IP is 10.8.9.17\*(C' The following code does exactly the same thing: \*(C`use Net::IPAddress;\*(C' \*(C`$server = "10.8.9.12";\*(C' \*(C`$newserver = num2ip(mask(ip2num($server),24) + 17);\*(C' \*(C`print "New server IP is $newserver\n";\*(C'

fqdn( \s-1FQDN\s0 )

This function returns the host and domain of the passed \s-1FQDN\s0 (fully qualified domain name). \*(C`($host,$domain) = fqdn("www.cpan.perl.org");\*(C' # $host = \*(L"www\*(R", $domain = \*(L"cpan.perl.org\*(R"

EXPORTS

\*(C`Net::IPAddress\*(C' exports five functions \*(C`ip2num\*(C', \*(C`num2ip\*(C', \*(C`validaddr\*(C', \*(C`mask\*(C', and \*(C`fqdn\*(C'.

AUTHOR

COPYRIGHT

Copyright(c) 2003-2005 Scott Renner. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.