Class: IPAdmin::Tree
- Inherits:
-
Object
- Object
- IPAdmin::Tree
- Defined in:
- lib/tree.rb
Instance Method Summary collapse
-
#add!(new) ⇒ Object
Add a CIDR address or IPAdmin::CIDR object to the tree.
-
#ancestors(cidr) ⇒ Object
Returns all the ancestors of the provided CIDR addresses.
-
#children(cidr) ⇒ Object
Returns all the immediate children of the provided CIDR addresses.
-
#delete!(cidr) ⇒ Object
Remove the provided CIDR address from the tree.
-
#descendants(cidr) ⇒ Object
Return all descendants of the provided CIDR address.
-
#dump ⇒ Object
Dump the contents of this tree.
-
#exists?(cidr) ⇒ Boolean
Has a CIDR address already been added to the tree?.
-
#fill_in!(cidr) ⇒ Object
Fill in the missing subnets of a particular CIDR.
-
#find(cidr) ⇒ Object
Find and return a CIDR from within the tree.
-
#find_space(options) ⇒ Object
Find subnets that are of at least size X.
-
#initialize(options = nil) ⇒ Tree
constructor
- Arguments: * none.
-
#longest_match(cidr) ⇒ Object
Find the longest matching branch of our tree to which a CIDR address belongs.
-
#merge_subnets!(cidr) ⇒ Object
Merge (summarize) all subnets of the provided CIDR address.
-
#prune!(cidr) ⇒ Object
Remove all subnets of the provided CIDR address.
-
#remove!(cidr) ⇒ Object
Remove the provided CIDR address, and all of its subnets from the tree.
-
#resize!(cidr, bits) ⇒ Object
Resize the provided CIDR address.
-
#root(cidr) ⇒ Object
Returns the root of the provided CIDR address.
-
#show ⇒ Object
Print the tree in a nicely formatted string.
-
#siblings(cidr) ⇒ Object
Return list of the sibling CIDRs of the provided CIDR address.
-
#supernets ⇒ Object
Return list of the top-level supernets of this tree.
Constructor Details
Instance Method Details
#add!(new) ⇒ Object
Add a CIDR address or IPAdmin::CIDR object to the tree.
-
Arguments:
- CIDR address or IPAdmin::CIDR object
-
Returns:
- nothing
Examples: tree.add!('192.168.1.0/24')
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/tree.rb', line 45 def add!(new) duplicate = false # validate object if ( !new.kind_of?(IPAdmin::CIDR) ) begin cidr = IPAdmin::CIDR.new(:CIDR => new) rescue Exception => error raise ArgumentError, "Provided argument raised the following " + "errors: #{error}" end else cidr = IPAdmin::CIDR.new(:PackedIP => new.packed_ip, :PackedNetmask => new.packed_netmask, :Version => new.version, :Tag => new.tag) end version = cidr.version new_entry = NetStruct.new(cidr, nil, []) # find cidr's parent new_entry.parent = find_parent(cidr, @root) # check parent for subnets of cidr new_entry.parent.children.each do |old_entry| if (old_entry.cidr.version == version) comp = IPAdmin.compare(cidr,old_entry.cidr) if (comp && comp[0] == cidr) old_entry.parent = new_entry new_entry.children.push(old_entry) elsif (comp && comp == 1) duplicate = true break end end end if (!duplicate) new_entry.children.each do |old_entry| new_entry.parent.children.delete(old_entry) end # add new object as an ordered entry to parent.children add_to_parent(new_entry,new_entry.parent) end return() end |
#ancestors(cidr) ⇒ Object
Returns all the ancestors of the provided CIDR addresses.
-
Arguments:
- CIDR address or IPAdmin::CIDR object
-
Returns:
- Array of IPAdmin::CIDR objects
Examples: puts tree.ancestors('192.168.1.0/27')
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/tree.rb', line 115 def ancestors(cidr) # validate object if ( !cidr.kind_of?(IPAdmin::CIDR) ) begin cidr = IPAdmin::CIDR.new(:CIDR => cidr) rescue Exception => error raise ArgumentError, "Provided argument raised the following " + "errors: #{error}" end end list = [] parent = find_parent(cidr,@root) until (parent == @root) list.push(parent.cidr) parent = parent.parent end return(list) end |
#children(cidr) ⇒ Object
Returns all the immediate children of the provided CIDR addresses.
-
Arguments:
- CIDR address or IPAdmin::CIDR object
-
Returns:
- Array of IPAdmin::CIDR objects
Examples: puts tree.children('192.168.1.0/24')
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/tree.rb', line 157 def children(cidr) # validate object if ( !cidr.kind_of?(IPAdmin::CIDR) ) begin cidr = IPAdmin::CIDR.new(:CIDR => cidr) rescue Exception => error raise ArgumentError, "Provided argument raised the following " + "errors: #{error}" end end list = [] find_me(cidr).children.each do |child| list.push(child.cidr) end return(list) end |
#delete!(cidr) ⇒ Object
Remove the provided CIDR address from the tree.
-
Arguments:
- CIDR address of IPAdmin::CIDR object
-
Returns:
- true on success or false on fail
Examples: did_remove = tree.remove!('192.168.1.0/24')
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/tree.rb', line 235 def delete!(cidr) removed = false # validate object if ( !cidr.kind_of?(IPAdmin::CIDR) ) begin cidr = IPAdmin::CIDR.new(:CIDR => cidr) rescue Exception => error raise ArgumentError, "Provided argument raised the following " + "errors: #{error}" end end # find matching me = find_me(cidr) # remove if (me) parent = me.parent children = me.children parent.children.delete(me) children.each {|x| add_to_parent(x,parent)} removed = true end return(removed) end |
#descendants(cidr) ⇒ Object
Return all descendants of the provided CIDR address.
-
Arguments:
- CIDR address or IPAdmin::CIDR object
-
Returns:
- Array of IPAdmin::CIDR objects
Examples: descendants = tree.descendants('192.168.1.0/24')
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/tree.rb', line 196 def descendants(cidr) list = [] # validate object if ( !cidr.kind_of?(IPAdmin::CIDR) ) begin cidr = IPAdmin::CIDR.new(:CIDR => cidr) rescue Exception => error raise ArgumentError, "Provided argument raised the following " + "errors: #{error}" end end me = find_me(cidr) dump_children(me).each {|x| list.push(x[:NetStruct].cidr)} if (me) return(list) end |
#dump ⇒ Object
Dump the contents of this tree.
-
Arguments:
- none
-
Returns:
- ordered array of hashes with the following fields:
- :CIDR => IPAdmin::CIDR object
- :Depth => (depth level in tree) Examples: dumped = tree.dump()
- ordered array of hashes with the following fields:
284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/tree.rb', line 284 def dump() list = [] dumped = dump_children(@root) dumped.each do |entry| depth = entry[:Depth] net_struct = entry[:NetStruct] list.push({:Depth => depth, :CIDR => net_struct.cidr}) end return(list) end |
#exists?(cidr) ⇒ Boolean
Has a CIDR address already been added to the tree?
-
Arguments:
- CIDR address or IPAdmin::CIDR object
-
Returns:
- true or false
Examples: added = tree.exists?('192.168.1.0/24')
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/tree.rb', line 317 def exists?(cidr) found = false # validate object if ( !cidr.kind_of?(IPAdmin::CIDR) ) begin cidr = IPAdmin::CIDR.new(:CIDR => cidr) rescue Exception => error raise ArgumentError, "Provided argument raised the following " + "errors: #{error}" end end found = true if (find_me(cidr)) return(found) end |
#fill_in!(cidr) ⇒ Object
Fill in the missing subnets of a particular CIDR.
-
Arguments:
- CIDR address or IPAdmin::CIDR object
-
Returns:
- true or false
Examples: success = tree.fill_in!('192.168.1.0/24')
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 |
# File 'lib/tree.rb', line 354 def fill_in!(cidr) filled = false # validate object if ( !cidr.kind_of?(IPAdmin::CIDR) ) begin cidr = IPAdmin::CIDR.new(:CIDR => cidr) rescue Exception => error raise ArgumentError, "Provided argument raised the following " + "errors: #{error}" end end me = find_me(cidr) if (me && me.children.length != 0) list = [] me.children.each {|x| list.push(x.cidr)} me.cidr.fill_in(:List => list, :Objectify => true).each do |cidr| self.add!(cidr) end filled = true end return(filled) end |
#find(cidr) ⇒ Object
Find and return a CIDR from within the tree.
-
Arguments:
- CIDR address or IPAdmin::CIDR object
-
Returns:
- IPAdmin::CIDR object, or nil
Examples: cidr = tree.find('192.168.1.0/24')
399 400 401 402 403 404 405 406 407 408 409 410 |
# File 'lib/tree.rb', line 399 def find(cidr) if ( !cidr.kind_of?(IPAdmin::CIDR) ) begin cidr = IPAdmin::CIDR.new(:CIDR => cidr) rescue Exception => error raise ArgumentError, "Provided argument raised the following " + "errors: #{error}" end end return(find_me(cidr).cidr) end |
#find_space(options) ⇒ Object
Find subnets that are of at least size X. Only subnets that are not themselves subnetted will be returned.
-
Arguments:
- Minimum subnet size in bits, or a Hash with the following fields:
- :Subnet - minimum subnet size in bits for returned subnets
- :IPCount - minimum IP count per subnet required for returned subnets
- :Version - restrict results to IPvX
- Minimum subnet size in bits, or a Hash with the following fields:
-
Returns:
- Array of IPAdmin::CIDR objects
-
Notes:
- :Subnet takes precedence over :IPCount
Examples: subnets = tree.find_space(:IPCount => 16)
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 |
# File 'lib/tree.rb', line 439 def find_space() version = nil if (.kind_of? Integer) bits4 = bits6 = elsif (.kind_of? Hash) if (.has_key?(:Version)) version = [:Version] raise "IP version should be 4 or 6, but was #{version}." if (version != 4 && version !=6) end if (.has_key?(:Subnet)) bits4 = [:Subnet] bits6 = [:Subnet] elsif(.has_key?(:IPCount)) bits4 = IPAdmin.minimum_size(:IPCount => [:IPCount], :Version => 4) bits6 = IPAdmin.minimum_size(:IPCount => [:IPCount], :Version => 6) else raise "Missing arguments: :Subnet/:IPCount" end else raise "Integer or Hash expected, but #{.class} provided." end list4 = [] list6 = [] dump_children(@root).each do |entry| netstruct = entry[:NetStruct] if (netstruct.cidr.version == 4) if ( (netstruct.children.length == 0) && (netstruct.cidr.bits <= bits4) ) list4.push(netstruct.cidr) end else if ( (netstruct.children.length == 0) && (netstruct.cidr.bits <= bits6) ) list6.push(netstruct.cidr) end end end list = [] if (!version) list.concat(list4) list.concat(list6) else list.concat(list4) if (version == 4) list.concat(list6) if (version == 6) end return(list) end |
#longest_match(cidr) ⇒ Object
Find the longest matching branch of our tree to which a CIDR address belongs. Useful for performing 'routing table' style lookups.
-
Arguments:
- CIDR address or IPAdmin::CIDR object
-
Returns:
- IPAdmin::CIDR object, or nil
Examples: longest_match = tree.longest_match('192.168.1.1')
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 |
# File 'lib/tree.rb', line 511 def longest_match(cidr) if ( !cidr.kind_of?(IPAdmin::CIDR) ) begin cidr = IPAdmin::CIDR.new(:CIDR => cidr) rescue Exception => error raise ArgumentError, "Provided argument raised the following " + "errors: #{error}" end end found = find_me(cidr) found = find_parent(cidr, @root) if !found return(found.cidr) end |
#merge_subnets!(cidr) ⇒ Object
Merge (summarize) all subnets of the provided CIDR address.
-
Arguments:
- CIDR address or IPAdmin::CIDR object
-
Returns:
- true on success or false on fail
Examples: tree.merge_subnets!('192.168.1.0/24')
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 |
# File 'lib/tree.rb', line 547 def merge_subnets!(cidr) merged = false # validate object if ( !cidr.kind_of?(IPAdmin::CIDR) ) begin cidr = IPAdmin::CIDR.new(:CIDR => cidr) rescue Exception => error raise ArgumentError, "Provided argument raised the following " + "errors: #{error}" end end me = find_me(cidr) if (me) to_merge = [] me.children.each {|x| to_merge.push(x.cidr)} to_merge.each {|x| delete!(x)} IPAdmin.merge(to_merge).each {|x| add!(x)} merged = true end return(merged) end |
#prune!(cidr) ⇒ Object
Remove all subnets of the provided CIDR address.
-
Arguments:
- CIDR address or IPAdmin::CIDR object
-
Returns:
- true on success or false on fail
Examples: did_prune = tree.prune!('192.168.1.0/24')
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 |
# File 'lib/tree.rb', line 594 def prune!(cidr) pruned = false # validate object if ( !cidr.kind_of?(IPAdmin::CIDR) ) begin cidr = IPAdmin::CIDR.new(:CIDR => cidr) rescue Exception => error raise ArgumentError, "Provided argument raised the following " + "errors: #{error}" end end me = find_me(cidr) if (me) me.children.clear pruned = true end return(pruned) end |
#remove!(cidr) ⇒ Object
Remove the provided CIDR address, and all of its subnets from the tree.
-
Arguments:
- CIDR address of IPAdmin::CIDR object
-
Returns:
- true on success or false on fail
Examples: did_remove = tree.remove!('192.168.1.0/24')
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 |
# File 'lib/tree.rb', line 637 def remove!(cidr) removed = false found = nil # validate object if ( !cidr.kind_of?(IPAdmin::CIDR) ) begin cidr = IPAdmin::CIDR.new(:CIDR => cidr) rescue Exception => error raise ArgumentError, "Provided argument raised the following " + "errors: #{error}" end end me = find_me(cidr) if (me) parent = me.parent parent.children.delete(me) removed = true end return(removed) end |
#resize!(cidr, bits) ⇒ Object
Resize the provided CIDR address.
-
Arguments:
- CIDR address or IPAdmin::CIDR object
- Integer representing the bits of the new netmask
-
Returns:
- true on success or false on fail
Examples: resized = tree.resize!('192.168.1.0/24', 23)
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 |
# File 'lib/tree.rb', line 683 def resize!(cidr,bits) resized = false # validate cidr if ( !cidr.kind_of?(IPAdmin::CIDR) ) begin cidr = IPAdmin::CIDR.new(:CIDR => cidr) rescue Exception => error raise ArgumentError, "Provided argument raised the following " + "errors: #{error}" end end me = find_me(cidr) if (me) new = me.cidr.resize(bits) delete!(me.cidr) add!(new) resized = true end return(resized) end |
#root(cidr) ⇒ Object
Returns the root of the provided CIDR address.
-
Arguments:
- CIDR address or IPAdmin::CIDR object
-
Returns:
- IPAdmin::CIDR object
Examples: puts tree.root('192.168.1.32/27')
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 |
# File 'lib/tree.rb', line 728 def root(cidr) # validate object if ( !cidr.kind_of?(IPAdmin::CIDR) ) begin cidr = IPAdmin::CIDR.new(:CIDR => cidr) rescue Exception => error raise ArgumentError, "Provided argument raised the following " + "errors: #{error}" end end parent = find_parent(cidr,@root) if (parent && parent != @root) until (parent.parent == @root) parent = parent.parent end end return(parent.cidr) end |
#show ⇒ Object
Print the tree in a nicely formatted string.
-
Arguments:
- none
-
Returns:
- String representing the contents of the tree
Examples: puts tree.show()
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 |
# File 'lib/tree.rb', line 770 def show() printed = "" list = dump_children(@root) list.each do |entry| cidr = entry[:NetStruct].cidr depth = entry[:Depth] if (depth == 0) indent = "" else indent = " " * (depth*3) end printed << "#{indent}#{cidr.desc(:Short => true)}\n" end return(printed) end |
#siblings(cidr) ⇒ Object
Return list of the sibling CIDRs of the provided CIDR address.
-
Arguments:
- CIDR address or IPAdmin::CIDR object
-
Returns:
- Array of IPAdmin::CIDR objects
Examples: puts tree.siblings('192.168.1.0/27')
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 |
# File 'lib/tree.rb', line 810 def siblings(cidr) # validate object if ( !cidr.kind_of?(IPAdmin::CIDR) ) begin cidr = IPAdmin::CIDR.new(:CIDR => cidr) rescue Exception => error raise ArgumentError, "Provided argument raised the following " + "errors: #{error}" end end list = [] find_parent(cidr,@root).children.each do |entry| if (!IPAdmin.compare(cidr,entry.cidr)) list.push(entry.cidr) end end return(list) end |
#supernets ⇒ Object
Return list of the top-level supernets of this tree.
-
Arguments:
- none
-
Returns:
- Array of IPAdmin::CIDR objects
Examples: supernets = tree.supernets()
851 852 853 854 855 |
# File 'lib/tree.rb', line 851 def supernets() supernets = [] @root.children.each {|x| supernets.push(x.cidr)} return (supernets) end |