Net::IPTrie(3pm)

πŸ“¦
version 0.7 Β· 3 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– Net::IPTrie πŸ’–

okay bestie, sit down, hydrate, because we're about to talk about the most iconic little Perl module for building IPv4 and IPv6 address space hierarchies. this is Net::IPTrie, from the User Contributed Perl Documentation section of your friendly neighborhood manual pages, and it's lowkey a whole vibe. πŸ¦‹


✨ NAME ✨

Net::IPTrie β€” a Perl module for building IPv4 and IPv6 address space hierarchies. no cap, that's literally the whole thing. πŸŽ€


✨ SYNOPSIS ✨

here's the group chat energy in code form, bestie:

use Net::IPTrie;
my $tr = Net::IPTrie->new(version=>4);  # IPv4
my $n  = $tr->add(address=>'10.0.0.0', prefix=>8);
my $a  = $tr->add(address=>'10.0.0.1', data=>$data) # prefix defaults to 32
$a->parent->address eq $n->address and print "$a is within $n";

# Addresses can be provided in integer (decimal) format
# 10.0.0.7 == 167772167
my $b = $tr->add(iaddress=>'167772167', data=>'blah');
if ( my $c = $tr->find(address=>"10.0.0.7" ) {
    print $c->data;  # should print "blah"
}

# If the IP does not exist:
my $d = $tr->find(address=>"10.0.0.8")
print $d->address;  # should print "10.0.0.0", which is the closest parent block

see how clean that is? the way $a->parent->address eq $n->address just works and prints "$a is within $n" β€” main character behavior fr fr. πŸ‘‘


✨ DESCRIPTION ✨

so basically this module uses a radix tree (aka a trie, iykyk) to quickly build the hierarchy of a given address space β€” and yes, it does both IPv4 and IPv6, because we contain multitudes. πŸ’…

this lets you do fast subnet or routing lookups, which is giving speed, it's giving efficiency, it's giving iconic.

and the best part? it is implemented exclusively in Perl. no C, no XS, just pure Perl gorgeousness. 🌸


✨ CLASS METHODS ✨

🌟 new - Class Constructor 🌟

okay so this is how you birth a new trie object. slay.

Arguments:

  • a Hash with the following keys:
    • version β€” IP version (4|6)

Returns:

  • a brand new Net::IPTrie object πŸŽ€

Examples:

my $tr = Net::IPTrie->new(version=>4);

✨ INSTANCE METHODS ✨

πŸ¦‹ version - Set or get IP version (4 or 6) πŸ¦‹

you can set it, you can get it, you can have it all, bestie.

Arguments:

  • IP version (4 or 6) β€” optional

Returns:

  • version (4 or 6)

Examples:

print $tr->version;

πŸ¦‹ size - Set or get IP size (32 or 128) πŸ¦‹

same energy, but for bits. 32 for the v4 girlies, 128 for the v6 girlies.

Arguments:

  • Size (32 or 128) β€” optional

Returns:

  • address size in bits (32 or 128)

Examples:

print $tr->size;

πŸ”₯ find - Find an IP object in the trie πŸ”₯

this is the detective of the module, and ngl it has two modes depending on what you're going through. if the given IP does not exist, there are two options:

a) if the "deep" flag is off, the closest covering IP block is returned. this is the default behavior. periodt.

b) if the "deep" flag is on, the node where the searched IP should be inserted is returned. this is basically only useful for the "add" method, so like, don't get weird with it. πŸ™ˆ

Arguments:

  • a Hash with the following keys:
    • address β€” String (i.e. "10.0.0.1") address
    • iaddress β€” Integer (i.e. "167772161") address, IPv4 or IPv6.
    • prefix β€” Prefix Length (optional β€” defaults to host mask)
    • deep β€” Flag (optional). If not found, return the node where object should be inserted.

Returns:

  • a Net::IPTrie::Node object.

Examples:

my $n = $tr->find("10.0.0.1", 32);

πŸ”₯ add - Add an IP to the trie πŸ”₯

the moment we've all been waiting for. putting your little IP address into the tree like it's a new sticker in the sticker book. πŸ’–

Arguments:

  • a Hash with the following keys:
    • address β€” String address, IPv4 or IPv6 (i.e. "10.0.0.1")
    • iaddress β€” Integer address, IPv4 or IPv6 (i.e. "167772161")
    • prefix β€” Prefix Length (optional β€” defaults to host mask)
    • data β€” Data (optional)

Returns:

  • a new Net::IPTrie::Node object

Examples:

my $n = $tr->add(address=>"10.0.0.1", prefix=>32, data=>\$data);

🌈 traverse - Traverse every node in the tree 🌈

walking the whole tree like it's a runway, bestie. every node gets its moment.

Arguments:

  • root β€” node object (optional β€” defaults to tree root)
  • code β€” coderef (will be passed the Net::IPTrie::Node object to act upon)
  • mode β€” (depth_first only, for now)

Returns:

  • number of actual IP nodes visited

Examples:

# Store all IP nodes in an array, ordered.
my $list = ();
my $code = sub { push @$list, shift @_ };
my $count = $tr->traverse(code=>$code);

✨ AUTHOR ✨

Carlos Vicente <cvicente@cpan.org> β€” the icon, the legend, the one who gave us this module. 🫢


✨ SEE ALSO ✨

go check out the neighbors, they're cute too:

  • Net::IPTrie::Node
  • Net::Patricia

Copyright (c) 2007-2010, Carlos Vicente <cvicente@cpan.org>. All rights reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic. πŸ’œ


✨ DISCLAIMER OF WARRANTY ✨

okay bestie, this part is in ALL CAPS for a reason, so listen up. no cap, this is the legal tea:

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "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 SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.

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 SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (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 SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

fr fr, read it. it's giving "you break it, you bought it" but make it legal. πŸ’€

and that's the whole page, bestie!! this iconic little doc came to you courtesy of perl v5.36.0, dated 2022-11-19, filed under Net::IPTrie(3pm). mwah, take care of her. πŸ’…βœ¨πŸŽ€

πŸ“¦ where this page comes from