NetAddr::IP::InetBase(3) IPv4 and IPV6 utilities

SYNOPSIS


use NetAddr::IP::Base qw(
:upper
inet_aton
inet_ntoa
ipv6_aton
ipv6_ntoa
ipv6_n2x
ipv6_n2d
inet_any2n
inet_n2dx
inet_n2ad
inet_pton
inet_ntop
packzeros
isIPv4
isNewIPv4
isAnyIPv4
AF_INET
AF_INET6
fake_AF_INET6
fillIPv4
);
use NetAddr::IP::Util qw(:all :inet :ipv4 :ipv6 :math)
:ipv4 => inet_aton, inet_ntoa, fillIPv4
:ipv6 => ipv6_aton, ipv6_ntoa,ipv6_n2x, ipv6_n2d,
inet_any2n, inet_n2dx, inet_n2ad
inet_pton, inet_ntop, packzeros
$dotquad = inet_ntoa($netaddr);
$netaddr = inet_aton($dotquad);
$ipv6naddr = ipv6_aton($ipv6_text);
$ipv6_text = ipv6_ntoa($ipv6naddr);
$hex_text = ipv6_n2x($ipv6naddr);
$dec_text = ipv6_n2d($ipv6naddr);
$ipv6naddr = inet_any2n($dotquad or $ipv6_text);
$dotquad or $hex_text = inet_n2dx($ipv6naddr);
$dotquad or $dec_text = inet_n2ad($ipv6naddr);
$netaddr = inet_pton($AF_family,$text_addr);
$text_addr = inet_ntop($AF_family,$netaddr);
$hex_text = packzeros($hex_text);
$rv = isIPv4($bits128);
$rv = isNewIPv4($bits128);
$rv = isAnyIPv4($bits128);
$constant = AF_INET();
$constant = AF_INET6();
$trueif = fake_AF_INET6();
$ip_filled = fillIPv4($shortIP);
NetAddr::IP::InetBase::lower();
NetAddr::IP::InetBase::upper();

INSTALLATION

Un-tar the distribution in an appropriate directory and type:

        perl Makefile.PL
        make
        make test
        make install

DESCRIPTION

NetAddr::IP::InetBase provides a suite network of conversion functions written in pure Perl for converting both IPv4 and IPv6 addresses to and from network address format and text format.

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
    
  • $ipv6text = 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
      Note: this function does NOT compress adjacent
      strings of 0:0:0:0 into the :: format
    
  • $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
      Note: this function does NOT compress adjacent
      strings of 0:0:0:0 into the :: format
    
  • $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
    
  • $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
      Note: this function does NOT compress adjacent
      strings of 0:0:0:0 into the :: format
    
  • $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
      Note: this function does NOT compress adjacent
      strings of 0:0:0:0 into the :: format
    
  • $netaddr = inet_pton($AF_family,$text_addr);

    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.

    NOTE: inet_pton, inet_ntop and AF_INET6 come from the Socket6 library if it is present on this host.

  • $text_addr = 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.

    NOTE: inet_pton, inet_ntop and AF_INET6 come from the Socket6 library if it is present on this host.

  • $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 occurrence 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
    
  • $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
    
  • NetAddr::IP::InetBase::lower();

    Return IPv6 strings in lowercase. This is the default.

  • NetAddr::IP::InetBase::upper();

    Return IPv6 strings in uppercase.

    The default may be set to uppercase when the module is loaded by invoking the TAG :upper. i.e.

            use NetAddr::IP::InetBase qw( :upper );
    
  • $constant = AF_INET;

    This function returns the system value for AF_INET.

  • $constant = AF_INET6;

    AF_INET6 is sometimes present in the Socket library and always present in the Socket6 library. When the Socket library does not contain AF_INET6 and when Socket6 is not present, a place holder value is "guessed" based on the underlying host operating system. See fake_AF_INET6 below.

    NOTE: inet_pton, inet_ntop and AF_INET6 come from the Socket6 library if it is present on this host.

  • $trueif = fake_AF_INET6;

    This function return FALSE if AF_INET6 is provided by Socket or Socket6. Otherwise, it returns the best guess value based on name of the host operating system.

  • $ip_filled = fillIPv4($shortIP);

    This function converts IPv4 addresses of the form 127.1 to the long form 127.0.0.1

    If the function is passed an argument that does not match the form of an IP address, the original argument is returned. i.e. pass it a hostname or a short IP and it will return a hostname or a filled IP.

EXPORT_OK

        :upper
        inet_aton
        inet_ntoa
        ipv6_aton
        ipv6_ntoa
        ipv6_n2x
        ipv6_n2d
        inet_any2n
        inet_n2dx
        inet_n2ad
        inet_pton
        inet_ntop
        packzeros
        isIPv4
        isNewIPv4
        isAnyIPv4
        AF_INET
        AF_INET6
        fake_AF_INET6
        fillIPv4

%EXPORT_TAGS

        :all
        :ipv4
        :ipv6
        :upper

AUTHOR

Michael Robinton <[email protected]>

COPYRIGHT

Copyright 2003 - 2012, Michael Robinton <[email protected]>

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 <[email protected]>