Class NetAddr::Tree
In: lib/tree.rb
Parent: Object

Tree

A class & series of methods for creating and manipulating IP-based heirarchical trees. Both IPv4 and IPv6 are supported.

A sample tree would look like:

 192.168.1.0/24
   192.168.1.0/26
      192.168.1.0/27
         192.168.1.0/28
         192.168.1.16/29
            192.168.1.16/30
         192.168.1.24/30
            192.168.1.25/32
         192.168.1.28/30
      192.168.1.32/27
   192.168.1.64/26
      192.168.1.64/27
   192.168.1.128/26
   192.168.1.192/26

Methods

Public Class methods

Synopsis

Create a new Tree object.

 Example:
 NetAddr::Tree.new()

Arguments:

  • none

Public Instance methods

Synopsis

 Add a CIDR address or NetAddr::CIDR object to the tree.
 Example:
 tree.add!('192.168.1.0/24')
 cidr = NetAddr::CIDR.create('192.168.1.0/24', :Tag => {:title => 'test net'}
 tree.add!(cidr)

Arguments:

Returns:

  • nil

Synopsis

 Returns all the ancestors of the provided CIDR addresses.

 Example:
 tree.ancestors('192.168.1.0/27')

Arguments:

Returns:

Synopsis

 Returns all the immediate children of the provided CIDR addresses.

 Example:
 tree.children('192.168.1.0/24')

Arguments:

Returns:

Synopsis

 Remove the provided CIDR address from the tree.

 Example:
 tree.remove!('192.168.1.0/24')

Arguments:

Returns:

  • true on success or false on fail

Synopsis

 Return all descendants of the provided CIDR address.

 Example:
 tree.descendants('192.168.1.0/24')

Arguments:

Returns:

Synopsis

 Dump the contents of this tree.

 Example:
 tree.dump()

Arguments:

  • none

Returns:

  • ordered array of hashes with the following fields:
      :CIDR => NetAddr::CIDR object
      :Depth => (depth level in tree)
    

Synopsis

 Has a CIDR address already been added to the tree?

 Example:
 tree.exists?('192.168.1.0/24')

Arguments:

Returns:

  • true or false

Synopsis

 Fill in the missing subnets of a particular CIDR.

 Example:
 tree.fill_in!('192.168.1.0/24')

Arguments:

Returns:

  • true or false

Synopsis

 Find and return a CIDR from within the tree.

 Example:
 tree.find('192.168.1.0/24')

Arguments:

Returns:

Synopsis

 Find subnets that are of at least size X. Only subnets that are not themselves
 subnetted will be returned. :Subnet takes precedence over :IPCount

 Example:
 tree.find_space(:IPCount => 16)

Arguments:

  • Minimum subnet size in bits, or a Hash with the following keys:
      :Subnet - minimum subnet size in bits for returned subnets
      :IPCount - minimum IP count per subnet required for returned subnets
      :Version - restrict results to IPvX
    

Returns:

Synopsis

Find the longest matching branch of our tree to which a CIDR address belongs. Useful for performing ‘routing table’ style lookups.

 Example:
 tree.longest_match('192.168.1.1')

Arguments:

Returns:

merge_subnets!(cidr)

Alias for summarize_subnets!

Synopsis

 Remove all subnets of the provided CIDR address.

 Example:
 tree.prune!('192.168.1.0/24')

Arguments:

Returns:

  • true on success or false on fail

Synopsis

 Remove the provided CIDR address, and all of its subnets from the tree.

 Example:
 tree.remove!('192.168.1.0/24')

Arguments:

Returns:

  • true on success or false on fail

Synopsis

 Resize the provided CIDR address.

 Example:
 tree.resize!('192.168.1.0/24', 23)

Arguments:

  • CIDR address as a String or an NetAddr::CIDR object
  • Integer representing the bits of the new netmask

Returns:

  • true on success or false on fail

Synopsis

 Returns the root of the provided CIDR address.

 Example:
 tree.root('192.168.1.32/27')

Arguments:

Returns:

Synopsis

 Print the tree as a formatted string.

 Example:
 tree.show()

Arguments:

  • none

Returns:

  • String

Synopsis

 Return list of the sibling CIDRs of the provided CIDR address.

 Example:
 tree.siblings('192.168.1.0/27')

Arguments:

Returns:

Synopsis

 Summarize all subnets of the provided CIDR address. The subnets will be
 placed under the new summary address within the tree.

 Example:
 tree.summarize_subnets!('192.168.1.0/24')

Arguments:

Returns:

  • true on success or false on fail

Synopsis

 Return list of the top-level supernets of this tree.

 Example:
 tree.supernets()

Arguments:

  • none

Returns:

[Validate]