======================================================================== * Lite/README ======================================================================== NAME NetAddr::IP::Lite - Manages IPv4 and IPv6 addresses and subnets SYNOPSIS use NetAddr::IP::Lite qw( Zeros Ones V4mask V4net :aton DEPRECATED ! :old_nth :upper :lower ); my $ip = new NetAddr::IP::Lite '127.0.0.1'; or if your prefer my $ip = NetAddr::IP::Lite->new('127.0.0.1); or from a packed IPv4 address my $ip = new_from_aton NetAddr::IP::Lite (inet_aton('127.0.0.1')); or from an octal filtered IPv4 address my $ip = new_no NetAddr::IP::Lite '127.012.0.0'; print "The address is ", $ip->addr, " with mask ", $ip->mask, "\n" ; if ($ip->within(new NetAddr::IP::Lite "127.0.0.0", "255.0.0.0")) { print "Is a loopback address\n"; } # This prints 127.0.0.1/32 print "You can also say $ip...\n"; The following four functions return ipV6 representations of: :: = Zeros(); FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF = Ones(); FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:: = V4mask(); ::FFFF:FFFF = V4net(); INSTALLATION Un-tar the distribution in an appropriate directory and type: perl Makefile.PL make make test make install NetAddr::IP::Lite depends on NetAddr::IP::Util which installs by default with its primary functions compiled using Perl's XS extensions to build a 'C' library. If you do not have a 'C' complier available or would like the slower Pure Perl version for some other reason, then type: perl Makefile.PL -noxs make make test make install DESCRIPTION This module provides an object-oriented abstraction on top of IP addresses or IP subnets, that allows for easy manipulations. Most of the operations of NetAddr::IP are supported. This module will work with older versions of Perl and is compatible with Math::BigInt. * By default NetAddr::IP functions and methods return string IPv6 addresses in uppercase. To change that to lowercase: NOTE: the AUGUST 2010 RFC5952 states: 4.3. Lowercase The characters "a", "b", "c", "d", "e", and "f" in an IPv6 address MUST be represented in lowercase. It is recommended that all NEW applications using NetAddr::IP::Lite be invoked as shown on the next line. use NetAddr::IP::Lite qw(:lower); * To ensure the current IPv6 string case behavior even if the default changes: use NetAddr::IP::Lite qw(:upper); The internal representation of all IP objects is in 128 bit IPv6 notation. IPv4 and IPv6 objects may be freely mixed. The supported operations are described below: Overloaded Operators Assignment ("=") Has been optimized to copy one NetAddr::IP::Lite object to another very quickly. "->copy()" The assignment ("=") operation is only put in to operation when the copied object is further mutated by another overloaded operation. See the overload manpage SPECIAL SYMBOLS FOR "use overload" for details. "->copy()" actually creates a new object when called. Stringification An object can be used just as a string. For instance, the following code my $ip = new NetAddr::IP::Lite '192.168.1.123'; print "$ip\n"; Will print the string 192.168.1.123/32. my $ip = new6 NetAddr::IP::Lite '192.168.1.123'; print "$ip\n"; Will print the string 0:0:0:0:0:0:C0A8:17B/128 Equality You can test for equality with either "eq", "ne", "==" or "!=". "eq", "ne" allows the comparison with arbitrary strings as well as NetAddr::IP::Lite objects. The following example: if (NetAddr::IP::Lite->new('127.0.0.1','255.0.0.0') eq '127.0.0.1/8') { print "Yes\n"; } Will print out "Yes". Comparison with "==" and "!=" requires both operands to be NetAddr::IP::Lite objects. Comparison via >, <, >=, <=, <=> and "cmp" Internally, all network objects are represented in 128 bit format. The numeric representation of the network is compared through the corresponding operation. Comparisons are tried first on the address portion of the object and if that is equal then the NUMERIC cidr portion of the masks are compared. This leads to the counterintuitive result that /24 > /16 Comparison should not be done on netaddr objects with different CIDR as this may produce indeterminate - unexpected results, rather the determination of which netblock is larger or smaller should be done by comparing $ip1->masklen <=> $ip2->masklen Addition of a constant ("+") Add a 32 bit signed constant to the address part of a NetAddr object. This operation changes the address part to point so many hosts above the current objects start address. For instance, this code: print NetAddr::IP::Lite->new('127.0.0.1/8') + 5; will output 127.0.0.6/8. The address will wrap around at the broadcast back to the network address. This code: print NetAddr::IP::Lite->new('10.0.0.1/24') + 255; outputs 10.0.0.0/24. Returns the the unchanged object when the constant is missing or out of range. 2147483647 <= constant >= -2147483648 Subtraction of a constant ("-") The complement of the addition of a constant. Difference ("-") Returns the difference between the address parts of two NetAddr::IP::Lite objects address parts as a 32 bit signed number. Returns undef if the difference is out of range. Auto-increment Auto-incrementing a NetAddr::IP::Lite object causes the address part to be adjusted to the next host address within the subnet. It will wrap at the broadcast address and start again from the network address. Auto-decrement Auto-decrementing a NetAddr::IP::Lite object performs exactly the opposite of auto-incrementing it, as you would expect. Methods "->new([$addr, [ $mask|IPv6 ]])" "->new6([$addr, [ $mask]])" "->new_no([$addr, [ $mask]])" "->new_from_aton($netaddr)" new_cis and new_cis6 are DEPRECATED "->new_cis("$addr $mask)" "->new_cis6("$addr $mask)" The first two methods create a new address with the supplied address in "$addr" and an optional netmask "$mask", which can be omitted to get a /32 or /128 netmask for IPv4 / IPv6 addresses respectively. The third method "new_no" is exclusively for IPv4 addresses and filters improperly formatted dot quad strings for leading 0's that would normally be interpreted as octal format by NetAddr per the specifications for inet_aton. new_from_aton takes a packed IPv4 address and assumes a /32 mask. This function replaces the DEPRECATED :aton functionality which is fundamentally broken. The last two methods new_cis and new_cis6 differ from new and new6 only in that they except the common Cisco address notation for address/mask pairs with a space as a separator instead of a slash (/) These methods are DEPRECATED because the functionality is now included in the other "new" methods i.e. ->new_cis('1.2.3.0 24') or ->new_cis6('::1.2.3.0 120') "->new6" and "->new_cis6" mark the address as being in ipV6 address space even if the format would suggest otherwise. i.e. ->new6('1.2.3.4') will result in ::102:304 addresses submitted to ->new in ipV6 notation will remain in that notation permanently. i.e. ->new('::1.2.3.4') will result in ::102:304 whereas new('1.2.3.4') would print out as 1.2.3.4 See "STRINGIFICATION" below. "$addr" can be almost anything that can be resolved to an IP address in all the notations I have seen over time. It can optionally contain the mask in CIDR notation. If the OPTIONAL perl module Socket6 is available in the local library it will autoload and ipV6 host6 names will be resolved as well as ipV4 hostnames. prefix notation is understood, with the limitation that the range specified by the prefix must match with a valid subnet. Addresses in the same format returned by "inet_aton" or "gethostbyname" can also be understood, although no mask can be specified for them. The default is to not attempt to recognize this format, as it seems to be seldom used. ###### DEPRECATED, will be remove in version 5 ############ To accept addresses in that format, invoke the module as in use NetAddr::IP::Lite ':aton' ###### USE new_from_aton instead ########################## If called with no arguments, 'default' is assumed. If called with an empty string as the argument, returns 'undef' "$addr" can be any of the following and possibly more... n.n n.n/mm n.n mm n.n.n n.n.n/mm n.n.n mm n.n.n.n n.n.n.n/mm 32 bit cidr notation n.n.n.n mm n.n.n.n/m.m.m.m n.n.n.n m.m.m.m loopback, localhost, broadcast, any, default x.x.x.x/host 0xABCDEF, 0b111111000101011110, (or a bcd number) a netaddr as returned by 'inet_aton' Any RFC1884 notation ::n.n.n.n ::n.n.n.n/mmm 128 bit cidr notation ::n.n.n.n/::m.m.m.m ::x:x ::x:x/mmm x:x:x:x:x:x:x:x x:x:x:x:x:x:x:x/mmm x:x:x:x:x:x:x:x/m:m:m:m:m:m:m:m any RFC1884 notation loopback, localhost, unspecified, any, default ::x:x/host 0xABCDEF, 0b111111000101011110 within the limits of perl's number resolution 123456789012 a 'big' bcd number (bigger than perl likes) and Math::BigInt If called with no arguments, 'default' is assumed. If called with and empty string as the argument, 'undef' is returned; "->broadcast()" Returns a new object referring to the broadcast address of a given subnet. The broadcast address has all ones in all the bit positions where the netmask has zero bits. This is normally used to address all the hosts in a given subnet. "->network()" Returns a new object referring to the network address of a given subnet. A network address has all zero bits where the bits of the netmask are zero. Normally this is used to refer to a subnet. "->addr()" Returns a scalar with the address part of the object as an IPv4 or IPv6 text string as appropriate. This is useful for printing or for passing the address part of the NetAddr::IP::Lite object to other components that expect an IP address. If the object is an ipV6 address or was created using ->new6($ip) it will be reported in ipV6 hex format otherwise it will be reported in dot quad format only if it resides in ipV4 address space. "->mask()" Returns a scalar with the mask as an IPv4 or IPv6 text string as described above. "->masklen()" Returns a scalar the number of one bits in the mask. "->bits()" Returns the width of the address in bits. Normally 32 for v4 and 128 for v6. "->version()" Returns the version of the address or subnet. Currently this can be either 4 or 6. "->cidr()" Returns a scalar with the address and mask in CIDR notation. A NetAddr::IP::Lite object *stringifies* to the result of this function. (see comments about ->new6() and ->addr() for output formats) "->aton()" Returns the address part of the NetAddr::IP::Lite object in the same format as the "inet_aton()" or "ipv6_aton" function respectively. If the object was created using ->new6($ip), the address returned will always be in ipV6 format, even for addresses in ipV4 address space. "->range()" Returns a scalar with the base address and the broadcast address separated by a dash and spaces. This is called range notation. "->numeric()" When called in a scalar context, will return a numeric representation of the address part of the IP address. When called in an array context, it returns a list of two elements. The first element is as described, the second element is the numeric representation of the netmask. This method is essential for serializing the representation of a subnet. "->bigint()" When called in a scalar context, will return a Math::BigInt representation of the address part of the IP address. When called in an array contest, it returns a list of two elements. The first element is as described, the second element is the Math::BigInt representation of the netmask. "$me->contains($other)" Returns true when "$me" completely contains "$other". False is returned otherwise and "undef" is returned if "$me" and "$other" are not both "NetAddr::IP::Lite" objects. "$me->within($other)" The complement of "->contains()". Returns true when "$me" is completely contained within "$other", undef if "$me" and "$other" are not both "NetAddr::IP::Lite" objects. C->is_rfc1918()> Returns true when "$me" is an RFC 1918 address. 10.0.0.0 - 10.255.255.255 (10/8 prefix) 172.16.0.0 - 172.31.255.255 (172.16/12 prefix) 192.168.0.0 - 192.168.255.255 (192.168/16 prefix) "->first()" Returns a new object representing the first usable IP address within the subnet (ie, the first host address). "->last()" Returns a new object representing the last usable IP address within the subnet (ie, one less than the broadcast address). "->nth($index)" Returns a new object representing the *n*-th usable IP address within the subnet (ie, the *n*-th host address). If no address is available (for example, when the network is too small for "$index" hosts), "undef" is returned. Version 4.00 of NetAddr::IP and version 1.00 of NetAddr::IP::Lite implements "->nth($index)" and "->num()" exactly as the documentation states. Previous versions behaved slightly differently and not in a consistent manner. To use the old behavior for "->nth($index)" and "->num()": use NetAddr::IP::Lite qw(:old_nth); old behavior: NetAddr::IP->new('10/32')->nth(0) == undef NetAddr::IP->new('10/32')->nth(1) == undef NetAddr::IP->new('10/31')->nth(0) == undef NetAddr::IP->new('10/31')->nth(1) == 10.0.0.1/31 NetAddr::IP->new('10/30')->nth(0) == undef NetAddr::IP->new('10/30')->nth(1) == 10.0.0.1/30 NetAddr::IP->new('10/30')->nth(2) == 10.0.0.2/30 NetAddr::IP->new('10/30')->nth(3) == 10.0.0.3/30 Note that in each case, the broadcast address is represented in the output set and that the 'zero'th index is alway undef except for a point-to-point /31 or /127 network where there are exactly two addresses in the network. new behavior: NetAddr::IP->new('10/32')->nth(0) == 10.0.0.0/32 NetAddr::IP->new('10.1/32'->nth(0) == 10.0.0.1/32 NetAddr::IP->new('10/31')->nth(0) == 10.0.0.0/32 NetAddr::IP->new('10/31')->nth(1) == 10.0.0.1/32 NetAddr::IP->new('10/30')->nth(0) == 10.0.0.1/30 NetAddr::IP->new('10/30')->nth(1) == 10.0.0.2/30 NetAddr::IP->new('10/30')->nth(2) == undef Note that a /32 net always has 1 usable address while a /31 has exactly two usable addresses for point-to-point addressing. The first index (0) returns the address immediately following the network address except for a /31 or /127 when it return the network address. "->num()" As of version 4.42 of NetAddr::IP and version 1.27 of NetAddr::IP::Lite a /31 and /127 with return a net num value of 2 instead of 0 (zero) for point-to-point networks. Version 4.00 of NetAddr::IP and version 1.00 of NetAddr::IP::Lite return the number of usable IP addresses within the subnet, not counting the broadcast or network address. Previous versions worked only for ipV4 addresses, returned a maximum span of 2**32 and returned the number of IP addresses not counting the broadcast address. (one greater than the new behavior) To use the old behavior for "->nth($index)" and "->num()": use NetAddr::IP::Lite qw(:old_nth); WARNING: NetAddr::IP will calculate and return a numeric string for network ranges as large as 2**128. These values are TEXT strings and perl can treat them as integers for numeric calculations. Perl on 32 bit platforms only handles integer numbers up to 2**32 and on 64 bit platforms to 2**64. If you wish to manipulate numeric strings returned by NetAddr::IP that are larger than 2**32 or 2**64, respectively, you must load additional modules such as Math::BigInt, bignum or some similar package to do the integer math. EXPORT_OK Zeros Ones V4mask V4net :aton DEPRECATED :old_nth :upper :lower AUTHORS Luis E. Muñoz , Michael Robinton WARRANTY This software comes with the same warranty as perl itself (ie, none), so by using it you accept any and all the liability. COPYRIGHT This software is (c) Luis E. Muñoz, 1999 - 2005 and (c) Michael Robinton, 2006 - 2012. All rights reserved. This program is free software; you can redistribute it and/or modify it under the terms of either: a) the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version, or b) the "Artistic License" which comes with this distribution. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the GNU General Public License or the Artistic License for more details. You should have received a copy of the Artistic License with this distribution, in the file named "Artistic". If not, I'll be glad to provide one. You should also have received a copy of the GNU General Public License along with this program in the file named "Copying". If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301 USA or visit their web page on the internet at: http://www.gnu.org/copyleft/gpl.html. SEE ALSO NetAddr::IP(3), NetAddr::IP::Util(3), NetAddr::IP::InetBase(3) ======================================================================== * Lite/Util/README ======================================================================== NAME NetAddr::IP::Util -- IPv4/6 and 128 bit number utilities SYNOPSIS use NetAddr::IP::Util qw( inet_aton inet_ntoa ipv6_aton ipv6_ntoa ipv6_n2x ipv6_n2d inet_any2n hasbits isIPv4 isNewIPv4 isAnyIPv4 inet_n2dx inet_n2ad inet_pton inet_ntop inet_4map6 ipv4to6 mask4to6 ipanyto6 maskanyto6 ipv6to4 packzeros shiftleft addconst add128 sub128 notcontiguous bin2bcd bcd2bin mode AF_INET AF_INET6 naip_gethostbyname ); use NetAddr::IP::Util qw(:all :inet :ipv4 :ipv6 :math) :inet => inet_aton, inet_ntoa, ipv6_aton ipv6_ntoa, ipv6_n2x, ipv6_n2d, inet_any2n, inet_n2dx, inet_n2ad, inet_pton, inet_ntop, inet_4map6, ipv4to6, mask4to6, ipanyto6, packzeros maskanyto6, ipv6to4, naip_gethostbyname :ipv4 => inet_aton, inet_ntoa :ipv6 => ipv6_aton, ipv6_ntoa, ipv6_n2x, ipv6_n2d, inet_any2n, inet_n2dx, inet_n2ad, inet_pton, inet_ntop, inet_4map6, ipv4to6, mask4to6, ipanyto6, maskanyto6, ipv6to4, packzeros, naip_gethostbyname :math => hasbits, isIPv4, isNewIPv4, isAnyIPv4, addconst, add128, sub128, notcontiguous, bin2bcd, bcd2bin, shiftleft $dotquad = inet_ntoa($netaddr); $netaddr = inet_aton($dotquad); $ipv6naddr = ipv6_aton($ipv6_text); $ipv6_text = ipvt_ntoa($ipv6naddr); $hex_text = ipv6_n2x($ipv6naddr); $dec_text = ipv6_n2d($ipv6naddr); $hex_text = packzeros($hex_text); $ipv6naddr = inet_any2n($dotquad or $ipv6_text); $ipv6naddr = inet_4map6($netaddr or $ipv6naddr); $rv = hasbits($bits128); $rv = isIPv4($bits128); $rv = isNewIPv4($bits128); $rv = isAnyIPv4($bits128); $dotquad or $hex_text = inet_n2dx($ipv6naddr); $dotquad or $dec_text = inet_n2ad($ipv6naddr); $netaddr = inet_pton($AF_family,$hex_text); $hex_text = inet_ntop($AF_family,$netaddr); $ipv6naddr = ipv4to6($netaddr); $ipv6naddr = mask4to6($netaddr); $ipv6naddr = ipanyto6($netaddr); $ipv6naddr = maskanyto6($netaddr); $netaddr = ipv6to4($pv6naddr); $bitsX2 = shiftleft($bits128,$n); $carry = addconst($ipv6naddr,$signed_32con); ($carry,$ipv6naddr)=addconst($ipv6naddr,$signed_32con); $carry = add128($ipv6naddr1,$ipv6naddr2); ($carry,$ipv6naddr)=add128($ipv6naddr1,$ipv6naddr2); $carry = sub128($ipv6naddr1,$ipv6naddr2); ($carry,$ipv6naddr)=sub128($ipv6naddr1,$ipv6naddr2); ($spurious,$cidr) = notcontiguous($mask128); $bcdtext = bin2bcd($bits128); $bits128 = bcd2bin($bcdtxt); $modetext = mode; ($name,$aliases,$addrtype,$length,@addrs)=naip_gethostbyname(NAME); $trueif = havegethostbyname2(); NetAddr::IP::Util::lower(); NetAddr::IP::Util::upper(); INSTALLATION Un-tar the distribution in an appropriate directory and type: perl Makefile.PL make make test make install NetAddr::IP::Util installs by default with its primary functions compiled using Perl's XS extensions to build a 'C' library. If you do not have a 'C' complier available or would like the slower Pure Perl version for some other reason, then type: perl Makefile.PL -noxs make make test make install DESCRIPTION NetAddr::IP::Util provides a suite of tools for manipulating and converting IPv4 and IPv6 addresses into 128 bit string context and back to text. The strings can be manipulated with Perl's logical operators: and & or | xor ^ ~ compliment in the same manner as 'vec' strings. The IPv6 functions support all rfc1884 formats. i.e. x:x:x:x:x:x:x:x:x x:x:x:x:x:x:x:d.d.d.d ::x:x:x ::x:d.d.d.d and so on... * $dotquad = inet_ntoa($netaddr); Convert a packed IPv4 network address to a dot-quad IP address. input: packed network address returns: IP address i.e. 10.4.12.123 * $netaddr = inet_aton($dotquad); Convert a dot-quad IP address into an IPv4 packed network address. input: IP address i.e. 192.5.16.32 returns: packed network address * $ipv6addr = ipv6_aton($ipv6_text); Takes an IPv6 address of the form described in rfc1884 and returns a 128 bit binary RDATA string. input: ipv6 text returns: 128 bit RDATA string * $ipv6_text = ipv6_ntoa($ipv6naddr); Convert a 128 bit binary IPv6 address to compressed rfc 1884 text representation. input: 128 bit RDATA string returns: ipv6 text * $hex_text = ipv6_n2x($ipv6addr); Takes an IPv6 RDATA string and returns an 8 segment IPv6 hex address input: 128 bit RDATA string returns: x:x:x:x:x:x:x:x * $dec_text = ipv6_n2d($ipv6addr); Takes an IPv6 RDATA string and returns a mixed hex - decimal IPv6 address with the 6 uppermost chunks in hex and the lower 32 bits in dot-quad representation. input: 128 bit RDATA string returns: x:x:x:x:x:x:d.d.d.d * $ipv6naddr = inet_any2n($dotquad or $ipv6_text); This function converts a text IPv4 or IPv6 address in text format in any standard notation into a 128 bit IPv6 string address. It prefixes any dot-quad address (if found) with '::' and passes it to ipv6_aton. input: dot-quad or rfc1844 address returns: 128 bit IPv6 string * $rv = hasbits($bits128); This function returns true if there are one's present in the 128 bit string and false if all the bits are zero. i.e. if (hasbits($bits128)) { &do_something; } or if (hasbits($bits128 & $mask128) { &do_something; } This allows the implementation of logical functions of the form of: if ($bits128 & $mask128) { ... input: 128 bit IPv6 string returns: true if any bits are present * $ipv6naddr = inet_4map6($netaddr or $ipv6naddr This function returns an ipV6 network address with the first 80 bits set to zero and the next 16 bits set to one, while the last 32 bits are filled with the ipV4 address. input: ipV4 netaddr or ipV6 netaddr returns: ipV6 netaddr returns: undef on error An ipV6 network address must be in one of the two compatible ipV4 mapped address spaces. i.e. ::ffff::d.d.d.d or ::d.d.d.d * $rv = isIPv4($bits128); This function returns true if there are no on bits present in the IPv6 portion of the 128 bit string and false otherwise. i.e. the address must be of the form - ::d.d.d.d Note: this is an old and deprecated ipV4 compatible ipV6 address * $rv = isNewIPv4($bits128); This function return true if the IPv6 128 bit string is of the form ::ffff::d.d.d.d * $rv = isAnyIPv4($bits128); This function return true if the IPv6 bit string is of the form ::d.d.d.d or ::ffff::d.d.d.d * $dotquad or $hex_text = inet_n2dx($ipv6naddr); This function does the right thing and returns the text for either a dot-quad IPv4 or a hex notation IPv6 address. input: 128 bit IPv6 string returns: ddd.ddd.ddd.ddd or x:x:x:x:x:x:x:x * $dotquad or $dec_text = inet_n2ad($ipv6naddr); This function does the right thing and returns the text for either a dot-quad IPv4 or a hex::decimal notation IPv6 address. input: 128 bit IPv6 string returns: ddd.ddd.ddd.ddd or x:x:x:x:x:x:ddd.ddd.ddd.dd * $netaddr = inet_pton($AF_family,$hex_text); This function takes an IP address in IPv4 or IPv6 text format and converts it into binary format. The type of IP address conversion is controlled by the FAMILY argument. * $hex_text = inet_ntop($AF_family,$netaddr); This function takes and IP address in binary format and converts it into text format. The type of IP address conversion is controlled by the FAMILY argument. NOTE: inet_ntop ALWAYS returns lowercase characters. * $hex_text = packzeros($hex_text); This function optimizes and rfc 1884 IPv6 hex address to reduce the number of long strings of zero bits as specified in rfc 1884, 2.2 (2) by substituting :: for the first occurence of the longest string of zeros in the address. * $ipv6naddr = ipv4to6($netaddr); Convert an ipv4 network address into an IPv6 network address. input: 32 bit network address returns: 128 bit network address * $ipv6naddr = mask4to6($netaddr); Convert an ipv4 network address/mask into an ipv6 network mask. input: 32 bit network/mask address returns: 128 bit network/mask address NOTE: returns the high 96 bits as one's * $ipv6naddr = ipanyto6($netaddr); Similar to ipv4to6 except that this function takes either an IPv4 or IPv6 input and always returns a 128 bit IPv6 network address. input: 32 or 128 bit network address returns: 128 bit network address * $ipv6naddr = maskanyto6($netaddr); Similar to mask4to6 except that this function takes either an IPv4 or IPv6 netmask and always returns a 128 bit IPv6 netmask. input: 32 or 128 bit network mask returns: 128 bit network mask * $netaddr = ipv6to4($pv6naddr); Truncate the upper 96 bits of a 128 bit address and return the lower 32 bits. Returns an IPv4 address as returned by inet_aton. input: 128 bit network address returns: 32 bit inet_aton network address * $bitsXn = shiftleft($bits128,$n); input: 128 bit string variable, number of shifts [optional] returns: bits X n shifts NOTE: a single shift is performed if $n is not specified * addconst($ipv6naddr,$signed_32con); Add a signed constant to a 128 bit string variable. input: 128 bit IPv6 string, signed 32 bit integer returns: scalar carry array (carry, result) * add128($ipv6naddr1,$ipv6naddr2); Add two 128 bit string variables. input: 128 bit string var1, 128 bit string var2 returns: scalar carry array (carry, result) * sub128($ipv6naddr1,$ipv6naddr2); Subtract two 128 bit string variables. input: 128 bit string var1, 128 bit string var2 returns: scalar carry array (carry, result) Note: The carry from this operation is the result of adding the one's complement of ARG2 +1 to the ARG1. It is logically NOT borrow. i.e. if ARG1 >= ARG2 then carry = 1 or if ARG1 < ARG2 then carry = 0 * ($spurious,$cidr) = notcontiguous($mask128); This function counts the bit positions remaining in the mask when the rightmost '0's are removed. input: 128 bit netmask returns true if there are spurious zero bits remaining in the mask, false if the mask is contiguous one's, 128 bit cidr number * $bcdtext = bin2bcd($bits128); Convert a 128 bit binary string into binary coded decimal text digits. input: 128 bit string variable returns: string of bcd text digits * $bits128 = bcd2bin($bcdtxt); Convert a bcd text string to 128 bit string variable input: string of bcd text digits returns: 128 bit string variable * $modetext = mode; Returns the operating mode of this module. input: none returns: "Pure Perl" or "CC XS" * ($name,$aliases,$addrtype,$length,@addrs)=naip_gethostbyname(NAME); Replacement for Perl's gethostbyname if Socket6 is available In ARRAY context, returns a list of five elements, the hostname or NAME, a space separated list of C_NAMES, AF family, length of the address structure, and an array of one or more netaddr's In SCALAR context, returns the first netaddr. This function ALWAYS returns an IPv6 address, even on IPv4 only systems. IPv4 addresses are mapped into IPv6 space in the form: ::FFFF:FFFF:d.d.d.d This is NOT the expected result from Perl's gethostbyname2. It is instead equivalent to: On an IPv4 only system: $ipv6naddr = ipv4to6 scalar ( gethostbyname( name )); On a system with Socket6 and a working gethostbyname2: $ipv6naddr = gethostbyname2( name, AF_INET6 ); and if that fails, the IPv4 conversion above. For a gethostbyname2 emulator that behave like Socket6, see: the Net::DNS::Dig manpage * $trueif = havegethostbyname2(); This function returns TRUE if Socket6 has a functioning gethostbyname2, otherwise it returns FALSE. See the comments above about the behavior of naip_gethostbyname. * NetAddr::IP::Util::lower(); Return IPv6 strings in lowercase. * NetAddr::IP::Util::upper(); Return IPv6 strings in uppercase. This is the default. EXAMPLES # convert any textual IP address into a 128 bit vector # sub text2vec { my($anyIP,$anyMask) = @_; # not IPv4 bit mask my $notiv4 = ipv6_aton('FFFF:FFFF:FFFF:FFFF:FFFF:FFFF::'); my $vecip = inet_any2n($anyIP); my $mask = inet_any2n($anyMask); # extend mask bits for IPv4 my $bits = 128; # default unless (hasbits($mask & $notiv4)) { $mask |= $notiv4; $bits = 32; } return ($vecip, $mask, $bits); } ... alternate implementation, a little faster sub text2vec { my($anyIP,$anyMask) = @_; # not IPv4 bit mask my $notiv4 = ipv6_aton('FFFF:FFFF:FFFF:FFFF:FFFF:FFFF::'); my $vecip = inet_any2n($anyIP); my $mask = inet_any2n($anyMask); # extend mask bits for IPv4 my $bits = 128; # default if (isIPv4($mask)) { $mask |= $notiv4; $bits = 32; } return ($vecip, $mask, $bits); } ... elsewhere $nip = { addr => $vecip, mask => $mask, bits => $bits, }; # return network and broadcast addresses from IP and Mask # sub netbroad { my($nip) = shift; my $notmask = ~ $nip->{mask}; my $bcast = $nip->{addr} | $notmask; my $network = $nip->{addr} & $nip->{mask}; return ($network, $broadcast); } # check if address is within a network # sub within { my($nip,$net) = @_; my $addr = $nip->{addr} my($nw,$bc) = netbroad($net); # arg1 >= arg2, sub128 returns true return (sub128($addr,$nw) && sub128($bc,$addr)) ? 1 : 0; } # truely hard way to do $ip++ # add a constant, wrapping at netblock boundaries # to subtract the constant, negate it before calling # 'addwrap' since 'addconst' will extend the sign bits # sub addwrap { my($nip,$const) = @_; my $addr = $nip->{addr}; my $mask = $nip->{mask}; my $bits = $nip->{bits}; my $notmask = ~ $mask; my $hibits = $addr & $mask; $addr = addconst($addr,$const); my $wraponly = $addr & $notmask; my $newip = { addr => $hibits | $wraponly, mask => $mask, bits => $bits, }; # bless $newip as appropriate return $newip; } # something more useful # increment a /24 net to the NEXT net at the boundry my $nextnet = 256; # for /24 LOOP: while (...continuing) { your code.... ... my $lastip = $ip-copy(); $ip++; if ($ip < $lastip) { # host part wrapped? # discard carry (undef, $ip->{addr} = addconst($ip->{addr}, $nextnet); } next LOOP; } EXPORT_OK inet_aton inet_ntoa ipv6_aton ipv6_ntoa ipv6_n2x ipv6_n2d inet_any2n hasbits isIPv4 isNewIPv4 isAnyIPv4 inet_n2dx inet_n2ad inet_pton inet_ntop inet_4map6 ipv4to6 mask4to6 ipanyto6 maskanyto6 ipv6to4 packzeros shiftleft addconst add128 sub128 notcontiguous bin2bcd bcd2bin mode naip_gethostbyname havegethostbyname2 AUTHOR Michael Robinton COPYRIGHT Copyright 2003 - 2013, Michael Robinton All rights reserved. This program is free software; you can redistribute it and/or modify it under the terms of either: a) the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version, or b) the "Artistic License" which comes with this distribution. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the GNU General Public License or the Artistic License for more details. You should have received a copy of the Artistic License with this distribution, in the file named "Artistic". If not, I'll be glad to provide one. You should also have received a copy of the GNU General Public License along with this program in the file named "Copying". If not, write to the Free Software Foundation, Inc. 51 Franklin Street, Fifth Floor Boston, MA 02110-1301 USA. or visit their web page on the internet at: http://www.gnu.org/copyleft/gpl.html. AUTHOR Michael Robinton SEE ALSO NetAddr::IP(3), NetAddr::IP::Lite(3), NetAddr::IP::InetBase(3) ======================================================================== * Copying ======================================================================== GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Lesser General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) year name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type how w'. This is free software, and you are welcome to redistribute it under certain conditions; type how c' for details. The hypothetical commands how w' and how c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than how w' and how c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program nomovision' (which makes passes at compilers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License.