IP(3)

๐ŸŒธ NAME ๐ŸŒธ

okay bestie, this is IP(3) from User Contributed Perl Documentation, and the manual name is Net::IP โ€” the Perl extension for manipulating IPv4/IPv6 addresses. periodt. ๐Ÿ’…


โœจ SYNOPSIS โœจ

Here's the lil taste test, bestie ๐Ÿ“:

         use Net::IP;

         my $ip = new Net::IP ('193.0.1/24') or die (Net::IP::Error());
         print ("IP  : ".$ip->ip()."\n");
         print ("Sho : ".$ip->short()."\n");
         print ("Bin : ".$ip->binip()."\n");
         print ("Int : ".$ip->intip()."\n");
         print ("Mask: ".$ip->mask()."\n");
         print ("Last: ".$ip->last_ip()."\n");
         print ("Len : ".$ip->prefixlen()."\n");
         print ("Size: ".$ip->size()."\n");
         print ("Type: ".$ip->iptype()."\n");
         print ("Rev:  ".$ip->reverse_ip()."\n");

๐Ÿ’– DESCRIPTION ๐Ÿ’–

okay so this module gives you functions to deal with IPv4/IPv6 addresses. It can be used as a class, letting you instantiate IP objects โ€” and those objects can be single IP addresses, prefixes, or ranges of addresses. There's also a procedural way of accessing most of the functions. Most subroutines can take either IPv4 or IPv6 addresses transparently. no cap. โœจ


๐Ÿฆ‹ OBJECT-ORIENTED INTERFACE ๐Ÿฆ‹

๐ŸŽ€ Object Creation ๐ŸŽ€

A Net::IP object can be created from a single IP address:

         $ip = new Net::IP ('193.0.1.46') || die ...

Or from a Classless Prefix (a /24 prefix is equivalent to a C class):

         $ip = new Net::IP ('195.114.80/24') || die ...

Or from a range of addresses:

         $ip = new Net::IP ('20.34.101.207 - 201.3.9.99') || die ...

Or from a address plus a number:

         $ip = new Net::IP ('20.34.10.0 + 255') || die ...

The new() function accepts IPv4 and IPv6 addresses:

         $ip = new Net::IP ('dead:beef::/32') || die ...

Optionnaly (yes, that spelling, we keep it ๐Ÿ’…), the function can be passed the version of the IP. Otherwise, it tries to guess what the version is (see _is_ipv4() and _is_ipv6()).

         $ip = new Net::IP ('195/8',4); # Class A

๐Ÿ‘‘ OBJECT METHODS ๐Ÿ‘‘

Most of these methods are front-ends for the real functions, which use a procedural interface. Most functions return undef on failure, and a true value on success. A detailed description of the procedural interface is provided below.

๐Ÿ’… set

Set an IP address in an existing IP object. This method has the same functionality as the new() method, except that it reuses an existing object to store the new IP.

"$ip->set('130.23.1/24',4);"

Like new(), set() takes two arguments โ€” a string used to build an IP address, prefix, or range, and optionally, the IP version of the considered address.

It returns an IP object on success, and undef on failure.

๐Ÿ’… error

Return the current object error string. The error string is set whenever one of the methods produces an error. Also, a global, class-wide Error() function is avaliable.

"warn ($ip->error());"

๐Ÿ’… errno

Return the current object error number. The error number is set whenever one of the methods produces an error. Also, a global $ERRNO variable is set when an error is produced.

"warn ($ip->errno());"

๐Ÿ’… ip

Return the IP address (or first IP of the prefix or range) in quad format, as a string.

"print ($ip->ip());"

๐Ÿ’… binip

Return the IP address as a binary string of 0s and 1s.

"print ($ip->binip());"

๐Ÿ’… prefixlen

Return the length in bits of the current prefix.

"print ($ip->prefixlen());"

๐Ÿ’… version

Return the version of the current IP object (4 or 6).

"print ($ip->version());"

๐Ÿ’… size

Return the number of IP addresses in the current prefix or range. Use of this function requires Math::BigInt.

"print ($ip->size());"

๐Ÿ’… binmask

Return the binary mask of the current prefix, if applicable.

"print ($ip->binmask());"

๐Ÿ’… mask

Return the mask in quad format of the current prefix.

"print ($ip->mask());"

๐Ÿ’… prefix

Return the full prefix (ip+prefix length) in quad (standard) format.

"print ($ip->prefix());"

๐Ÿ’… print

Print the IP object (IP/Prefix or First - Last)

"print ($ip->print());"

๐Ÿ’… intip

Convert the IP in integer format and return it as a Math::BigInt object.

"print ($ip->intip());"

๐Ÿ’… hexip

Return the IP in hex format

"print ($ip->hexip());"

๐Ÿ’… hexmask

Return the mask in hex format

"print ($ip->hexmask());"

๐Ÿ’… short

Return the IP in short format:

  • IPv4 addresses: 194.5/16
  • IPv6 addresses: ab32:f000::

"print ($ip->short());"

๐Ÿ’… iptype

Return the IP Type โ€” this describes the type of an IP (Public, Private, Reserved, etc.) See procedural interface ip_iptype for more details.

"print ($ip->iptype());"

๐Ÿ’… reverse_ip

Return the reverse IP for a given IP address (in.addr. format).

The example in the page is literally:

"print ($ip->reserve_ip());"

yes, reserve_ip โ€” we don't fix lore ๐Ÿ’€

๐Ÿ’… last_ip

Return the last IP of a prefix/range in quad format.

"print ($ip->last_ip());"

๐Ÿ’… last_bin

Return the last IP of a prefix/range in binary format.

"print ($ip->last_bin());"

๐Ÿ’… last_int

Return the last IP of a prefix/range in integer format.

"print ($ip->last_int());"

๐Ÿ’… find_prefixes

This function finds all the prefixes that can be found between the two addresses of a range. The function returns a list of prefixes.

"@list = $ip->find_prefixes($other_ip));"

(extra ) is canon, bestie ๐Ÿซถ)

๐Ÿ’… bincomp

Binary comparaison of two IP objects. The function takes an operation and an IP object as arguments. It returns a boolean value.

The operation can be one of:

  • lt: less than (smaller than)
  • le: smaller or equal to
  • gt: greater than
  • ge: greater or equal to

"if ($ip->bincomp('lt',$ip2) {...}"

๐Ÿ’… binadd

Binary addition of two IP objects. The value returned is an IP object.

"my $sum = $ip->binadd($ip2);"

๐Ÿ’… aggregate

Aggregate 2 IPs โ€” Append one range/prefix of IPs to another. The last address of the first range must be the one immediately preceding the first address of the second range. A new IP object is returned.

"my $total = $ip->aggregate($ip2);"

๐Ÿ’… overlaps

Check if two IP ranges/prefixes overlap each other. The value returned by the function should be one of:

  • $IP_PARTIAL_OVERLAP โ€” ranges overlap
  • $IP_NO_OVERLAP โ€” no overlap
  • $IP_A_IN_B_OVERLAP โ€” range2 contains range1
  • $IP_B_IN_A_OVERLAP โ€” range1 contains range2
  • $IP_IDENTICAL โ€” ranges are identical
  • undef โ€” problem

"if ($ip->overlaps($ip2)==$IP_A_IN_B_OVERLAP) {...};"

๐Ÿ’… looping

The "+" operator is overloaded in order to allow looping though a whole range of IP addresses:

         my $ip = new Net::IP ('195.45.6.7 - 195.45.6.19') || die;
         # Loop
         do {
             print $ip->ip(), "\n";
         } while (++$ip);

The ++ operator returns undef when the last address of the range is reached.

๐Ÿ’… auth

Return IP authority information from the IP::Authority module

"$auth = ip-"auth ();>"

Note: IPv4 only ๐ŸŒˆ

๐ŸŒธ PROCEDURAL INTERFACE ๐ŸŒธ

okay so these functions are the ones doing the actual heavy lifting in the module ๐Ÿ’ช. like the OO methods, most of them return undef when things go wrong. and here's the lil plot twist: to get at the error codes and error strings, you don't reach for $ip->error() and $ip->errno() โ€” you use the global functions Error() and Errno() instead ๐Ÿซถ

also important, bestie: the procedural interface functions are not exported by default. if you want them in your namespace you gotta edit the use statement:

"use Net::IP qw(:PROC);"

๐Ÿ˜ญ Error

Returns the error string that matches the last error generated in the module. this one's also lowkey useful for the OO interface, because if the new() function fails you literally cannot call $ip->error(), so Error() is your only ride-or-die ๐Ÿ’–

warn Error();

๐Ÿ’€ Errno

Returns a numeric error code that corresponds to the error string that Error handed you.

๐Ÿงš ip_iptobin

Transforms an IP address into a bit string. slay.

Params  : IP address, IP version
Returns : binary IP string on success, undef otherwise

"$binip = ip_iptobin ($ip,6);"

๐Ÿฆ‹ ip_bintoip

Transforms a bit string back into an IP address. the reverse glow-up โœจ

Params  : binary IP, IP version
Returns : IP address on success, undef otherwise

"$ip = ip_bintoip ($binip,6);"

๐Ÿ“ ip_bintoint

Transforms a bit string into a BigInt.

Params  : binary IP
Returns : BigInt

"$bigint = new Math::BigInt (ip_bintoint($binip));"

๐Ÿ”ฅ ip_inttobin

Transforms a BigInt into a bit string. โš ๏ธ warning: this one sets warnings ("-w") off โ€” and it has to, because Math::BigInt is not compliant. not me defending the chaos but here we are ๐Ÿ™ˆ

Params  : BigInt, IP version
Returns : binary IP

"$binip = ip_inttobin ($bigint);"

๐Ÿ’… ip_get_version

Tries to guess the IP version of an IP address. a little detective moment ๐Ÿ”

Params  : IP address
Returns : 4, 6, undef(unable to determine)

"$version = ip_get_version ($ip)"

๐ŸŽ€ ip_is_ipv4

Checks if an IP address is of type 4.

Params  : IP address
Returns : 1 (yes) or 0 (no)

"ip_is_ipv4($ip) and print "$ip is IPv4";"

๐ŸŽ€ ip_is_ipv6

Checks if an IP address is of type 6.

Params            : IP address
Returns           : 1 (yes) or 0 (no)

"ip_is_ipv6($ip) and print "$ip is IPv6";"

๐ŸŒˆ ip_expand_address

Expands an IP address out of compact notation. it's giving โœจstretchโœจ

Params  : IP address, IP version
Returns : expanded IP address or undef on failure

"$ip = ip_expand_address ($ip,4);"

๐Ÿ’œ ip_get_mask

Gets the IP mask from a prefix length.

Params  : Prefix length, IP version
Returns : Binary Mask

"$mask = ip_get_mask ($len,6);"

โญ ip_last_address_bin

Returns the last binary address of a prefix. the final boss of the range ๐Ÿ‘‘

Params  : First binary IP, prefix length, IP version
Returns : Binary IP

"$lastbin = ip_last_address_bin ($ip,$len,6);"

๐ŸŒธ ip_splitprefix

Splits a prefix into an IP and a prefix length. if you passed it a plain old simple IP, it just hands it right back to you, no drama.

Params  : Prefix
Returns : IP, optionnaly length of prefix

"($ip,$len) = ip_splitprefix ($prefix)"

๐Ÿงš ip_prefix_to_range

Gets a whole range of IPs out of a prefix.

Params  : Prefix, IP version
Returns : First IP, last IP

"($ip1,$ip2) = ip_prefix_to_range ($prefix,6);"

๐Ÿ’€ ip_bincomp

Compares binary IPs with <, >, <=, >=.

Operators are lt(<), le(<=), gt(>), and ge(>=).

Params  : First binary IP, operator, Last binary IP
Returns : 1 (yes), 0 (no), or undef (problem)

"ip_bincomp ($ip1,'lt',$ip2) == 1 or do {}"

๐Ÿ“ ip_binadd

Adds two binary IPs together. math but make it cute ๐Ÿงฎ

Params  : First binary IP, Last binary IP
Returns : Binary sum or undef (problem)

"$binip = ip_binadd ($bin1,$bin2);"

๐Ÿฆ‹ ip_get_prefix_length

Gets the prefix length for a given range of 2 IPs.

Params  : First binary IP, Last binary IP
Returns : Length of prefix or undef (problem)

"$len = ip_get_prefix_length ($ip1,$ip2);"

๐Ÿ”ฅ ip_range_to_prefix

Returns all the prefixes between two IPs.

Params  : First IP (binary format), Last IP (binary format), IP version
Returns : List of Prefixes or undef (problem)

the prefixes it gives back have the form q.q.q.q/nn.

"@prefix = ip_range_to_prefix ($ip1,$ip2,6);"

๐ŸŽ€ ip_compress_v4_prefix

Compresses an IPv4 Prefix. glow-down in the best way ๐Ÿ’…

Params  : IP, Prefix length
Returns : Compressed Prefix

"$ip = ip_compress_v4_prefix ($ip, $len);"

๐ŸŒˆ ip_compress_address

Compresses an IPv6 address. if it's an IPv4 it literally just returns the IP, no notes.

Params  : IP, IP version
Returns : Compressed IP or undef (problem)

"$ip = ip_compress_adress ($ip, $version);"

๐Ÿซถ ip_is_overlap

Checks if two ranges of IPs overlap. the "are we a thing or not" of IP ranges ๐Ÿ˜ญ

Params  : Four binary IPs (begin of range 1,end1,begin2,end2), IP version
    $IP_PARTIAL_OVERLAP (ranges overlap)
    $IP_NO_OVERLAP      (no overlap)
    $IP_A_IN_B_OVERLAP  (range2 contains range1)
    $IP_B_IN_A_OVERLAP  (range1 contains range2)
    $IP_IDENTICAL       (ranges are identical)
    undef               (problem)

"(ip_is_overlap($rb1,$re1,$rb2,$re2,4) eq $IP_A_IN_B_OVERLAP) and do
{};"

๐Ÿงš ip_get_embedded_ipv4

Gets an IPv4 that's embedded inside an IPv6 address. it's giving hidden gem โœจ

Params  : IPv6
Returns : IPv4 string or undef (not found)

"$ip4 = ip_get_embedded($ip6);"

๐Ÿ’œ ip_check_mask

Checks the validity of a binary IP mask.

Params  : Mask
Returns : 1 or undef (invalid)

"ip_check_mask($binmask) or do {};"

It checks whether the mask has only 1s followed by 0s. no random stragglers allowed ๐Ÿ™…โ€โ™€๏ธ

๐Ÿ‘‘ ip_aggregate

Aggregates 2 ranges of binary IPs into one.

Params  : 1st range (1st IP, Last IP), last range (1st IP, last IP), IP version
Returns : prefix or undef (invalid)

"$prefix = ip_aggregate ($bip1,$eip1,$bip2,$eip2) || die ..."

โญ ip_iptypev4

Returns the type of an IPv4 address.

Params:  binary IP
Returns: type as of the following table or undef (invalid ip)

See RFC 5735 and RFC 6598.

Address Block       Present Use                Reference
-------------------------------------------------------------------
0.0.0.0/8           "This" Network             RFC 1122 PRIVATE
10.0.0.0/8          Private-Use Networks       RFC 1918 PRIVATE
100.64.0.0/10       CGN Shared Address Space   RFC 6598 SHARED
127.0.0.0/8         Loopback                   RFC 1122 LOOPBACK
169.254.0.0/16      Link Local                 RFC 3927 LINK-LOCAL
172.16.0.0/12       Private-Use Networks       RFC 1918 PRIVATE
192.0.0.0/24        IETF Protocol Assignments  RFC 5736 RESERVED
192.0.2.0/24        TEST-NET-1                 RFC 5737 TEST-NET
192.88.99.0/24      6to4 Relay Anycast         RFC 3068 6TO4-RELAY
192.168.0.0/16      Private-Use Networks       RFC 1918 PRIVATE
198.18.0.0/15       Network Interconnect
                    Device Benchmark Testing   RFC 2544 RESERVED
198.51.100.0/24     TEST-NET-2                 RFC 5737 TEST-NET
203.0.113.0/24      TEST-NET-3                 RFC 5737 TEST-NET
224.0.0.0/4         Multicast                  RFC 3171 MULTICAST
240.0.0.0/4         Reserved for Future Use    RFC 1112 RESERVED
255.255.255.255/32  Limited Broadcast          RFC 919  BROADCAST
                                               RFC 922

๐ŸŒธ ip_iptypev6

Returns the type of an IPv6 address.

Params:  binary ip
Returns: type as of the following table or undef (invalid)

See IANA Internet Protocol Version 6 Address Space http://www.iana.org/assignments/ipv6-address-space/ipv6-address-space.txt and IANA IPv6 Special Purpose Address Registry http://www.iana.org/assignments/iana-ipv6-special-registry/iana-ipv6-special-registry.txt.

Prefix      Allocation           Reference
-------------------------------------------------------------
0000::/8    Reserved by IETF     [RFC4291] RESERVED
0100::/8    Reserved by IETF     [RFC4291] RESERVED
0200::/7    Reserved by IETF     [RFC4048] RESERVED
0400::/6    Reserved by IETF     [RFC4291] RESERVED
0800::/5    Reserved by IETF     [RFC4291] RESERVED
1000::/4    Reserved by IETF     [RFC4291] RESERVED
2000::/3    Global Unicast       [RFC4291] GLOBAL-UNICAST
4000::/3    Reserved by IETF     [RFC4291] RESERVED
6000::/3    Reserved by IETF     [RFC4291] RESERVED
8000::/3    Reserved by IETF     [RFC4291] RESERVED
A000::/3    Reserved by IETF     [RFC4291] RESERVED
C000::/3    Reserved by IETF     [RFC4291] RESERVED
E000::/4    Reserved by IETF     [RFC4291] RESERVED
F000::/5    Reserved by IETF     [RFC4291] RESERVED
F800::/6    Reserved by IETF     [RFC4291] RESERVED
FC00::/7    Unique Local Unicast [RFC4193] UNIQUE-LOCAL-UNICAST
FE00::/9    Reserved by IETF     [RFC4291] RESERVED
FE80::/10   Link Local Unicast   [RFC4291] LINK-LOCAL-UNICAST
FEC0::/10   Reserved by IETF     [RFC3879] RESERVED
FF00::/8    Multicast            [RFC4291] MULTICAST
Prefix          Assignment            Reference
---------------------------------------------------------------------
::1/128         Loopback Address      [RFC4291] UNSPECIFIED
::/128          Unspecified Address   [RFC4291] LOOPBACK
::FFFF:0:0/96   IPv4-mapped Address   [RFC4291] IPV4MAP
0100::/64       Discard-Only Prefix   [RFC6666] DISCARD
2001:0000::/32  TEREDO                [RFC4380] TEREDO
2001:0002::/48  BMWG                  [RFC5180] BMWG
2001:db8::/32   Documentation Prefix  [RFC3849] DOCUMENTATION
2001:10::/28    ORCHID                [RFC4843] ORCHID
2002::/16       6to4                  [RFC3056] 6TO4
FC00::/7        Unique-Local          [RFC4193] UNIQUE-LOCAL-UNICAST
FE80::/10       Linked-Scoped Unicast [RFC4291] LINK-LOCAL-UNICAST
FF00::/8        Multicast             [RFC4291] MULTICAST

๐Ÿฆ‹ ip_iptype

Returns the type of an IP (Public, Private, Reserved).

Params  : Binary IP to test, IP version (defaults to 6)
Returns : type (see ip_iptypev4 and ip_iptypev6 for details) or undef (invalid)

"$type = ip_iptype ($ip);"

๐Ÿ’– ip_check_prefix

Checks the validity of a prefix.

Params  : binary IP, length of prefix, IP version
Returns : 1 or undef (invalid)

It checks whether the variant part of a prefix only has 0s, and whether the length is correct. two-for-one vibe check ๐ŸŽ€

"ip_check_prefix ($ip,$len,$ipv) or do {};"

๐Ÿ”ฅ ip_reverse

Gets a reverse name from a prefix.

Params  : IP, length of prefix, IP version
Returns : Reverse name or undef (error)

"$reverse = ip_reverse ($ip);"

๐Ÿ“ ip_normalize

Normalizes data into a range/prefix of IP addresses.

Params  : Data String (Single IP, Range, Prefix)
Returns : ip1, ip2 (if range/prefix) or undef (error)

"($ip1,$ip2) = ip_normalize ($data);"

๐Ÿ’œ ip_auth

Returns IP authority information from the IP::Authority module.

Params  : IP, version
Returns : Auth info (RI for RIPE, AR for ARIN, etc)

"$auth = ip_auth ($ip,4);"
Note: IPv4 only. no v6, sorry bestie ๐Ÿ˜”

๐Ÿ’€ BUGS ๐Ÿ’€

The Math::BigInt library is needed for functions that use integers โ€” those are ip_inttobin, ip_bintoint, and the size method. In a next version, Math::BigInt will become optionnal. manifesting that for them fr fr ๐Ÿ™

๐Ÿ’– AUTHORS ๐Ÿ’–

Ok so this page did NOT build itself, bestie โ€” real people made this happen and we're gonna give them their flowers ๐ŸŒธ

  • Manuel Valente <manuel.valente@gmail.com> โ€” yeah, that's the whole contact info, no cap.
  • The original IPv4 code? That's Monica Cortes Sack <mcortes@ripe.net>. Iconic behavior. ๐Ÿ‘‘
  • And the original IPv6 code came from Lee Wilmot <lee@ripe.net>. We stan a v6 pioneer โœจ

๐Ÿงš BASED ON ๐Ÿงš

This whole situation is built on top of these three, fr fr:

  • ipv4pack.pm
  • iplib.pm
  • iplibncc.pm

Like, the lore runs deep. ๐Ÿ“š


๐ŸŒˆ SEE ALSO ๐ŸŒˆ

  • perl(1) โ€” go touch the source, it's giving foundational ๐Ÿ’œ
  • IP::Authority โ€” the sibling module, iykyk ๐Ÿฆ‹

Ok that's the whole page, you made it to the end!! ๐ŸŽ‰ Go manipulate some addresses, bestie, you got this ๐Ÿ’…

This was perl v5.42.0, dated 2024-03-04 โ€” aka the IP(3) page, and now you know her. Periodt. ๐ŸŽ€๐Ÿ’–

๐Ÿ“ฆ where this page comes from