NetAddr...InetBase(3)

πŸ¦‹ NetAddr::IP::InetBase(3) β€” the Perl manual page, bestie edition πŸ¦‹

okay so this is the NetAddr...InetBase(3) page from the User Contributed Perl Documentation β€” we're talking IPv4 and IPv6 utilities and honestly? it's giving infrastructure fairy godmother ✨


πŸ’œ NAME

NetAddr::IP::InetBase β€” IPv4 and IPV6 utilities. that's it, that's the whole vibe. no cap.


🌸 SYNOPSIS

like, here's how you actually pull it in:

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();

and yes, you also toggle the case vibes directly:

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

πŸŽ€ INSTALLATION

un-tar the distribution in an appropriate directory and then, bestie, run these in order β€” no skipping πŸ’…

perl Makefile.PL
make
make test
make install

make test is not optional, i don't make the rules (i do, actually, and i'm saying run it).


✨ DESCRIPTION

so NetAddr::IP::InetBase hands you a whole suite network of conversion functions, written in pure Perl, for flipping both IPv4 and IPv6 addresses to and from network address format and text format. it's the translator bestie of the IP world 🫢

the IPv6 functions support all rfc1884 formats. like:

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... you get it πŸ’–


  • $dotquad = inet_ntoa($netaddr); β€” converts a packed IPv4 network address into a dot-quad IP address.
input:        packed network address
returns:      IP address i.e. 10.4.12.123
  • $netaddr = inet_aton($dotquad); β€” converts 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); β€” converts a 128 bit binary IPv6 address into compressed rfc 1884 text representation. compression slay πŸ’…
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
okay important: this one does NOT compress adjacent strings of 0:0:0:0 into the :: format. read it and weep 😭
  • $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. hybrid icon behavior 🌈
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
again: NOT compressing. same energy. don't come for me, i'm just the messenger 🧚
  • $ipv6naddr = inet_any2n($dotquad or $ipv6_text); β€” 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. truly the "idk what you gave me but i'll handle it" friend πŸ’€
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. main character energy, it just knows πŸ”₯
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
yes bestie, still NOT compressing. it's a whole theme πŸŽ€
  • $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
one more time for the people in the back: NOT compressing πŸ™ˆ
  • $netaddr = inet_pton($AF_family,$text_addr); β€” 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. you're in charge, periodt πŸ‘‘
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); β€” 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 occurence of the longest string of zeros in the address. finally, the compression we've been waiting for ⭐
  • $ipv6naddr = ipv4to6($netaddr); β€” convert an ipv4 network address into an ipv6 network address. glow-up complete πŸ¦‹
input:        32 bit network address
returns:      128 bit network address
  • $rv = isIPv4($bits128); β€” 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. deprecated, bestie β€” treat it like an old flip phone, cute but don't rely on it πŸ’€
  • $rv = isNewIPv4($bits128); β€” returns true if the IPv6 128 bit string is of the form:
::ffff:d.d.d.d
  • $rv = isAnyIPv4($bits128); β€” returns 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. chill, lowercase, cozy 🌸
  • NetAddr::IP::InetBase::upper(); β€” return IPv6 strings in uppercase. LOUD MODE πŸ“£

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. it's giving detective work πŸ”
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); β€” converts IPv4 addresses of the form 127.1 to the long form 127.0.0.1. stretch that address out, bestie πŸ’…

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. no notes, it just doesn't touch what isn't its business 🫢


🌟 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 <michael@bizsystems.com> β€” the icon who wrote all this ✨


Copyright 2003 - 2012, Michael Robinton <michael@bizsystems.com>

All rights reserved.

This program is free software; you can redistribute it and/or modify it under the terms of either:

  1. the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version, or
  2. 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. (yes, all caps, they mean it 😀)

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 <michael@bizsystems.com> β€” yes, again. credited twice, deservedly πŸ’–


🌈 SEE ALSO

NetAddr::IP(3), NetAddr::IP::Lite(3), NetAddr::IP::Util(3) β€” go read the whole friend group, iykyk 🫢


✨ cute sign-off ✨ this page is from perl v5.42.0, dated 2016-05-28, and it's the NetAddr...InetBase(3) page in the User Contributed Perl Documentation. now go convert some addresses, bestie β€” you're gonna slay πŸ’…πŸŽ€
πŸ“¦ where this page comes from