Skip site navigation (1)Skip section navigation (2)

FreeBSD Manual Pages

  
 
  

home | help
IPAddress(3)	      User Contributed Perl Documentation	  IPAddress(3)

NAME
       Net::IPAddress -	Functions used to manipulate IP	addresses, masks and
       FQDN's.

SYNOPSIS
       use Net::IPAddress;

       @ISA = qw(Net::IPAddress);

DESCRIPTION
       "Net::IPAddr" is	a collection of	helpful	functions used to convert IP
       addresses to/from 32-bit	integers, applying subnet masks	to IP
       addresses, validating IP	address	strings, and splitting a FQDN 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 IP addresses
       only to find out	that they don't	sort the way you expected?  Here is
       the solution!  If you convert the IP addresses to 32-bit	integer
       addresses, they will sort in correct order.

       ip2num( STRING )
	   Returns the 32-bit integer of the passed IP address string.

	   "$ipnumA =A ip2num("10.1.1.1");" $ipnum is 167837953.

       num2ip( INTEGER )
	   Returns the IP address string of the	passed 32-bit IP address.

	   "$IPA =A num2ip(167837953);"	$IP is "10.1.1.1".

       validaddr( STRING )
	   Returns true	(1) if the IP address string is	a valid	and properly
	   formatted IP	address, and false (0) otherwise.

	   "$validA =A validaddr("10.1.2.1");"A	A #A returnsA true

	   "$validA =A validaddr("10.1.2.");"A A A #A returnsA false!

	   If you have your own	IP address validator, try the last one.	 Most
	   will	incorrectly compute that as a valid address.

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

	   Examples

	     "$subnetA =A mask("10.96.3.2",16);" #A $subnetA =A	"10.96.0.0"

	     "$subnetA =A mask("10.21.4.22","255.240.0.0");"
	     #A	$subnetA =A "10.16.0.0"

	     "$subnetA =A mask(167837953,"255.255.255.0");"
	     #A	$subnetA =A 167837952>

	   This	function, when used with the others, is	very useful for
	   computing IP	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 ".17" address of a /24 subnet. This	is done	easily
	   in the following example:

	     "useA Net::IPAddress"

	     "$serverA =A "10.8.9.12";"A "$newserverA =A num2ip(ip2num(mask($server,24))A +A 17);"A "printA "NewA serverA IPA isA $newserver\n";"

	     "NewA serverA IPA isA 10.8.9.17"

	     The following code	does exactly the same thing:

	     "useA Net::IPAddress;"

	     "$serverA =A "10.8.9.12";"A "$newserverA =A num2ip(mask(ip2num($server),24)A +A 17);"A "printA "NewA serverA IPA isA $newserver\n";"

       fqdn( FQDN )
	   This	function returns the host and domain of	the passed FQDN	(fully
	   qualified domain name).

	   "($host,$domain)A =A	fqdn("www.cpan.perl.org");"
	   #A $hostA =A	"www",A	$domainA =A "cpan.perl.org"

EXPORTS
       "Net::IPAddress"	exports	five functions "ip2num", "num2ip",
       "validaddr", "mask", and	"fqdn".

AUTHOR
       Scott Renner <srenner@mandtbank.com>, <srenner@comcast.net>

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.

perl v5.24.1			  2005-10-25			  IPAddress(3)

NAME | SYNOPSIS | DESCRIPTION | EXPORTS | AUTHOR | COPYRIGHT

Want to link to this manual page? Use this URL:
<https://www.freebsd.org/cgi/man.cgi?query=Net::IPAddress&sektion=3&manpath=FreeBSD+12.0-RELEASE+and+Ports>

home | help