| 1 | # Net::IPAddress - IP Addressing stuff | ||
| 2 | # Copyright(c) 2003-2005 Scott Renner <srenner@mandtbank.com>. All rights reserved | ||
| 3 | # This program is free software; you can redistribute it and/or | ||
| 4 | # modify it under the same terms as Perl itself. | ||
| 5 | |||
| 6 | package Net::IPAddress; | ||
| 7 | |||
| 8 | use strict; | ||
| 9 | use vars qw(@ISA @EXPORT $VERSION); | ||
| 10 | |||
| 11 | our $VERSION = "1.10"; | ||
| 12 | our @EXPORT = qw(ip2num num2ip mask validaddr fqdn); | ||
| 13 | our @ISA = qw(Exporter); | ||
| 14 | |||
| 15 | sub ip2num { | ||
| 16 | return(unpack("N",pack("C4",split(/\./,$_[0])))); | ||
| 17 | } | ||
| 18 | |||
| 19 | sub num2ip { | ||
| 20 | return(join(".",unpack("C4",pack("N",$_[0])))); | ||
| 21 | } | ||
| 22 | |||
| 23 | sub validaddr { | ||
| 24 | return(0) unless ($_[0]); | ||
| 25 | my (@ipaddr) = split(/\./,$_[0]); | ||
| 26 | return(0) if (scalar(@ipaddr) != 4); | ||
| 27 | my ($ip); | ||
| 28 | foreach $ip (@ipaddr) { | ||
| 29 | return(0) if ($ip eq ""); | ||
| 30 | return(0) if (($ip < 0) || ($ip > 255)) | ||
| 31 | } | ||
| 32 | return(1); | ||
| 33 | } | ||
| 34 | |||
| 35 | sub mask { | ||
| 36 | my ($ipaddr, $mask) = @_; | ||
| 37 | my $format = 0; | ||
| 38 | my ($addr); | ||
| 39 | if (validaddr($ipaddr)) { | ||
| 40 | $addr = ip2num($ipaddr); | ||
| 41 | $format = 1; | ||
| 42 | } else { | ||
| 43 | $addr = $ipaddr; | ||
| 44 | } | ||
| 45 | if (validaddr($mask)) { # Mask can be sent as either "255.255.0.0" or "16" | ||
| 46 | $mask = ip2num($mask); | ||
| 47 | } else { | ||
| 48 | $mask = (((1 << $mask) - 1) << (32 - $mask)); | ||
| 49 | } | ||
| 50 | my $result = $addr & $mask; | ||
| 51 | return($format ? num2ip($result) : $result); | ||
| 52 | } | ||
| 53 | |||
| 54 | sub fqdn { | ||
| 55 | my ($fqdn) = @_; | ||
| 56 | $fqdn =~ s/^\s*(.*?)\s*$/$1/; # remove leading and trailing spaces | ||
| 57 | my ($host,@domain) = split(/\./,$fqdn); # Split the domain | ||
| 58 | return scalar(@domain) > 0 ? ($host, join('.',@domain)) : undef | ||
| 59 | } | ||
| 60 | 1; | ||
| 61 | |||
| 62 | =head1 NAME | ||
| 63 | |||
| 64 | Net::IPAddress - Functions used to manipulate IP addresses, masks and FQDN's. | ||
| 65 | |||
| 66 | =head1 SYNOPSIS | ||
| 67 | |||
| 68 | use Net::IPAddress; | ||
| 69 | |||
| 70 | @ISA = qw(Net::IPAddress); | ||
| 71 | |||
| 72 | =head1 DESCRIPTION | ||
| 73 | |||
| 74 | C<Net::IPAddr> is a collection of helpful functions used to convert IP | ||
| 75 | addresses to/from 32-bit integers, applying subnet masks to IP addresses, | ||
| 76 | validating IP address strings, and splitting a FQDN into its host and domain | ||
| 77 | parts. | ||
| 78 | |||
| 79 | No rocket science here, but I have found these functions to very, very handy. | ||
| 80 | For example, have you ever tried to sort a list of IP addresses only to find | ||
| 81 | out that they don't sort the way you expected? Here is the solution! | ||
| 82 | If you convert the IP addresses to 32-bit integer addresses, they will sort | ||
| 83 | in correct order. | ||
| 84 | |||
| 85 | =over 4 | ||
| 86 | |||
| 87 | =item ip2num( STRING ) | ||
| 88 | |||
| 89 | Returns the 32-bit integer of the passed IP address string. | ||
| 90 | |||
| 91 | S<C<$ipnum = ip2num("10.1.1.1");>> | ||
| 92 | $ipnum is 167837953. | ||
| 93 | |||
| 94 | =item num2ip( INTEGER ) | ||
| 95 | |||
| 96 | Returns the IP address string of the passed 32-bit IP address. | ||
| 97 | |||
| 98 | S<C<$IP = num2ip(167837953);>> | ||
| 99 | $IP is "10.1.1.1". | ||
| 100 | |||
| 101 | =item validaddr( STRING ) | ||
| 102 | |||
| 103 | Returns true (1) if the IP address string is a valid and properly formatted | ||
| 104 | IP address, and false (0) otherwise. | ||
| 105 | |||
| 106 | S<C<$valid = validaddr("10.1.2.1");> # returns true> | ||
| 107 | |||
| 108 | S<C<$valid = validaddr("10.1.2.");> # returns false!> | ||
| 109 | |||
| 110 | If you have your own IP address validator, try the last one. Most will | ||
| 111 | incorrectly compute that as a valid address. | ||
| 112 | |||
| 113 | =item mask( IPADDRESS, MASK ) | ||
| 114 | |||
| 115 | Returns the result of binary (IPADDRESS & MASK). IPADDRESS can be either | ||
| 116 | an IP address string or a 32-bit integer address. MASK can be either an IP | ||
| 117 | address string, or the number of bits in the mask. The returned value will | ||
| 118 | be in the same format as the passed IP address. If you pass an IP address | ||
| 119 | string, then an IP address string is returned, if you pass a 32-bit integer | ||
| 120 | address then a 32-bit integer address is returned. | ||
| 121 | |||
| 122 | Examples | ||
| 123 | |||
| 124 | =over 2 | ||
| 125 | |||
| 126 | S<C<$subnet = mask("10.96.3.2",16);>> | ||
| 127 | S<# $subnet = "10.96.0.0"> | ||
| 128 | |||
| 129 | S<C<$subnet = mask("10.21.4.22","255.240.0.0");>> | ||
| 130 | S<# $subnet = "10.16.0.0"> | ||
| 131 | |||
| 132 | S<C<$subnet = mask(167837953,"255.255.255.0");>> | ||
| 133 | S<# $subnet = 167837952>> | ||
| 134 | |||
| 135 | =back | ||
| 136 | |||
| 137 | This function, when used with the others, is very useful for computing IP | ||
| 138 | addresses. For example, you need to add another server to a subnet that an | ||
| 139 | existing server is on. You want the new server to be the ".17" address of a | ||
| 140 | /24 subnet. This is done easily in the following example: | ||
| 141 | |||
| 142 | =over 2 | ||
| 143 | |||
| 144 | |||
| 145 | S<C<use Net::IPAddress>> | ||
| 146 | |||
| 147 | S<C<$server = "10.8.9.12";> | ||
| 148 | C<$newserver = num2ip(ip2num(mask($server,24)) + 17);> | ||
| 149 | C<print "New server IP is $newserver\n";>> | ||
| 150 | |||
| 151 | S<C<New server IP is 10.8.9.17>> | ||
| 152 | |||
| 153 | The following code does exactly the same thing: | ||
| 154 | |||
| 155 | S<C<use Net::IPAddress;>> | ||
| 156 | |||
| 157 | S<C<$server = "10.8.9.12";> | ||
| 158 | C<$newserver = num2ip(mask(ip2num($server),24) + 17);> | ||
| 159 | C<print "New server IP is $newserver\n";>> | ||
| 160 | |||
| 161 | =back | ||
| 162 | |||
| 163 | =item fqdn( FQDN ) | ||
| 164 | |||
| 165 | This function returns the host and domain of the passed FQDN (fully qualified | ||
| 166 | domain name). | ||
| 167 | |||
| 168 | S<C<($host,$domain) = fqdn("www.cpan.perl.org");>> | ||
| 169 | S<# $host = "www", $domain = "cpan.perl.org"> | ||
| 170 | |||
| 171 | =back | ||
| 172 | |||
| 173 | =head1 EXPORTS | ||
| 174 | |||
| 175 | C<Net::IPAddress> exports five functions C<ip2num>, C<num2ip>, C<validaddr>, | ||
| 176 | C<mask>, and C<fqdn>. | ||
| 177 | |||
| 178 | =head1 AUTHOR | ||
| 179 | |||
| 180 | Scott Renner <srenner@mandtbank.com>, <srenner@comcast.net> | ||
| 181 | |||
| 182 | =head1 COPYRIGHT | ||
| 183 | |||
| 184 | Copyright(c) 2003-2005 Scott Renner. All rights reserved. | ||
| 185 | This program is free software; you can redistribute it and/or | ||
| 186 | modify it under the same terms as Perl itself. | ||
| 187 | |||
| 188 | =cut |