SYNOPSIS

    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

DESCRIPTION

This module uses a radix tree (or trie) to quickly build the hierarchy of a given address space (both IPv4 and IPv6). This allows the user to perform fast subnet or routing lookups. It is implemented exclusively in Perl.

CLASS METHODS

new - Class Constructor

Arguments: Hash with the following keys: version - IP version (4|6) Returns: New Net::IPTrie object Examples: my $tr = Net::IPTrie->new(version=>4);

INSTANCE METHODS

version - Set or get \s-1IP\s0 version (4 or 6)

Arguments: IP version (4 or 6) - optional Returns: version (4 or 6) Examples: print $tr->version;

size - Set or get \s-1IP\s0 size (32 or 128)

Arguments: Size (32 or 128) - optional Returns: Address size in bits (32 or 128) Examples: print $tr->size;

find - Find an \s-1IP\s0 object in the trie

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. 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.

Arguments: Hash with 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: Net::IPTrie::Node object. Examples: my $n = $tr->find("10.0.0.1", 32);

add - Add an \s-1IP\s0 to the trie

Arguments: Hash with 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: 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

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

RELATED TO Net::IPTrie…

Net::IPTrie::Node Net::Patricia

LICENCE AND COPYRIGHT

Copyright (c) 2007-2010, Carlos Vicente <[email protected]>. 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

\s-1BECAUSE\s0 \s-1THIS\s0 \s-1SOFTWARE\s0 \s-1IS\s0 \s-1LICENSED\s0 \s-1FREE\s0 \s-1OF\s0 \s-1CHARGE\s0, \s-1THERE\s0 \s-1IS\s0 \s-1NO\s0 \s-1WARRANTY\s0 \s-1FOR\s0 \s-1THE\s0 \s-1SOFTWARE\s0, \s-1TO\s0 \s-1THE\s0 \s-1EXTENT\s0 \s-1PERMITTED\s0 \s-1BY\s0 \s-1APPLICABLE\s0 \s-1LAW\s0. \s-1EXCEPT\s0 \s-1WHEN\s0 \s-1OTHERWISE\s0 \s-1STATED\s0 \s-1IN\s0 \s-1WRITING\s0 \s-1THE\s0 \s-1COPYRIGHT\s0 \s-1HOLDERS\s0 \s-1AND/OR\s0 \s-1OTHER\s0 \s-1PARTIES\s0 \s-1PROVIDE\s0 \s-1THE\s0 \s-1SOFTWARE\s0 \*(L"\s-1AS\s0 \s-1IS\s0\*(R" \s-1WITHOUT\s0 \s-1WARRANTY\s0 \s-1OF\s0 \s-1ANY\s0 \s-1KIND\s0, \s-1EITHER\s0 \s-1EXPRESSED\s0 \s-1OR\s0 \s-1IMPLIED\s0, \s-1INCLUDING\s0, \s-1BUT\s0 \s-1NOT\s0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