Class: IPAdmin::CIDR
- Inherits:
-
Object
- Object
- IPAdmin::CIDR
- Defined in:
- lib/cidr.rb
Instance Attribute Summary collapse
-
#all_f ⇒ Object
readonly
Integer of either 32 or 128 bits in length, with all bits set to 1.
-
#tag ⇒ Object
Hash of custom tags.
-
#version ⇒ Object
readonly
IP version 4 or 6.
Instance Method Summary collapse
-
#arpa ⇒ Object
Depending on the IP version of the current CIDR, return either an in-addr.arpa.
-
#bits ⇒ Object
Provide number of bits in Netmask.
-
#contains?(cidr) ⇒ Boolean
Determines if this CIDR contains (is supernet of) the provided CIDR address or IPAdmin::CIDR object.
-
#desc(options = nil) ⇒ Object
Returns network/netmask in CIDR format.
-
#enumerate(options = nil) ⇒ Object
Provide all IP addresses contained within the IP space of this CIDR.
-
#fill_in(options) ⇒ Object
Given a list of subnets of the current CIDR, return a new list with any holes (missing subnets) filled in.
-
#hostmask_ext ⇒ Object
Provide IPv4 Hostmask in extended format (y.y.y.y).
-
#initialize(options) ⇒ CIDR
constructor
-
Arguments: * CIDR address as a String, or a Hash with the following fields: - :CIDR – CIDR address, single IP, or IP and Netmask in extended format - String (optional) - :PackedIP – Integer representation of an IP address (optional) - :PackedNetmask – Integer representation of an IP Netmask (optional) - :Version – IP version - Integer (optional) - :Tag – Custom descriptor tag - Hash, tag => value.
-
-
#ip(options = nil) ⇒ Object
Provide original IP address passed during initialization.
-
#last(options = nil) ⇒ Object
(also: #broadcast)
Provide last IP address in this CIDR object.
-
#multicast_mac(options = nil) ⇒ Object
Assuming this CIDR is a valid multicast address (224.0.0.0/4 for IPv4 and ff00::/8 for IPv6), return its ethernet MAC address (EUI-48) mapping.
-
#netmask ⇒ Object
Provide netmask in CIDR format (/yy).
-
#netmask_ext ⇒ Object
Provide IPv4 netmask in extended format (y.y.y.y).
-
#network(options = nil) ⇒ Object
(also: #base, #first)
Provide base network address.
-
#next_ip(options = nil) ⇒ Object
Provide the next IP following the last available IP within this CIDR object.
-
#next_subnet(options = nil) ⇒ Object
Provide the next subnet following this CIDR object.
-
#nth(options) ⇒ Object
Provide the nth IP within this object.
-
#packed_hostmask ⇒ Object
Provide an Integer representation of the Hostmask of this object.
-
#packed_ip ⇒ Object
Provide an Integer representation of the IP address of this object.
-
#packed_netmask ⇒ Object
Provide an Integer representation of the Netmask of this object.
-
#packed_network ⇒ Object
Provide an Integer representation of the Network address of this object.
-
#range(options) ⇒ Object
Given two Indexes, return all IP addresses within the CIDR that are between them (inclusive).
-
#remainder(options) ⇒ Object
Given a single subnet of the current CIDR, provide the remainder of the subnets.
-
#resize(options) ⇒ Object
Resize the CIDR by changing the size of the Netmask.
-
#resize!(options) ⇒ Object
Resize this object by changing the size of the Netmask.
-
#size ⇒ Object
Provide number of IP addresses within this object.
-
#subnet(options = nil) ⇒ Object
Subnet this object.
Constructor Details
#initialize(options) ⇒ CIDR
-
Arguments:
-
CIDR address as a String, or a Hash with the following fields:
- :CIDR -- CIDR address, single IP, or IP and Netmask in extended format - String (optional) - :PackedIP -- Integer representation of an IP address (optional) - :PackedNetmask -- Integer representation of an IP Netmask (optional) - :Version -- IP version - Integer (optional) - :Tag -- Custom descriptor tag - Hash, tag => value. (optional)
-
-
Notes:
-
At a minimum, either :CIDR or :PackedIP must be provided.
-
CIDR formatted netmasks take precedence over extended formatted ones.
-
CIDR address defaults to a host network (/32 or /128) if netmask not provided.
-
:PackedIP takes precedence over IP given within :CIDR.
-
:PackedNetmask takes precedence over netmask given within :CIDR.
-
Version will be auto-detected if not specified
-
Examples:
cidr4 = IPAdmin::CIDR.new('192.168.1.1/24')
cidr4 = IPAdmin::CIDR.new(:CIDR => '192.168.1.1/24')
cidr4 = IPAdmin::CIDR.new('192.168.1.1 255.255.255.0')
cidr4_2 = IPAdmin::CIDR.new(:PackedIP => 0x0a010001,
:PackedNetmask => 0xffffff00
:Version => 4)
cidr6 = IPAdmin::CIDR.new(:CIDR => 'fec0::/64',
:Tag => {'interface' => 'g0/1'})
cidr6_2 = IPAdmin::CIDR.new(:CIDR => '::ffff:192.168.1.1/96')
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/cidr.rb', line 71 def initialize() @tag = {} if (!.kind_of?(Hash) && !.kind_of?(String)) raise ArgumentError, "Expected Hash or String, but #{.class} provided." end if (.kind_of? String) cidr = end if (.kind_of? Hash) # packed_ip takes precedence over cidr if (.has_key?(:PackedIP)) packed_ip = [:PackedIP] raise ArgumentError, "Expected Integer, but #{packed_ip.class} " + "provided for option :PackedIP." if (!packed_ip.kind_of?(Integer)) elsif (.has_key?(:CIDR)) cidr = [:CIDR] raise ArgumentError, "Expected String, but #{cidr.class} " + "provided for option :CIDR." if (!cidr.kind_of?(String)) else raise ArgumentError, "Missing argument: CIDR or PackedIP." end if (.has_key?(:PackedNetmask)) packed_netmask = [:PackedNetmask] raise ArgumentError, "Expected Integer, but #{packed_netmask.class} " + "provided for option :PackedNetmask." if (!packed_netmask.kind_of?(Integer)) end if (.has_key?(:Version)) @version = [:Version] if (@version != 4 && @version != 6) raise ArgumentError, ":Version should be 4 or 6, but was '#{version}'." end end if (.has_key?(:Tag)) @tag = [:Tag] if (!@tag.kind_of? Hash) raise ArgumentError, "Expected Hash, but #{@tag.class} provided for option :Tag." end end end if (packed_ip) # attempt to determine version if not provided if (!@version) if (packed_ip < 2**32) @version = 4 else @version = 6 end end # validate & store packed_ip IPAdmin.validate_ip_addr(:IP => packed_ip, :Version => @version) @ip = packed_ip else netmask = nil # if extended netmask provided. should only apply to ipv4 if (cidr =~ /.+\s+.+/ ) cidr,netmask = cidr.split(' ') end # determine version if not set if (!@version) if (cidr =~ /\./ && cidr !~ /:/) @version = 4 elsif (cidr =~ /:/) @version = 6 end end # if ipv6 and extended netmask was provided, then raise exception raise ArgumentError, "Garbage provided at end of IPv6 address." if (@version == 6 && netmask) # if netmask part of cidr, then over-ride any provided extended netmask. if (cidr =~ /\//) ip,netmask = cidr.split(/\//) if (!ip || !netmask) raise ArgumentError, "CIDR address is improperly formatted. Missing netmask after '/' character." end else ip = cidr end # pack ip @ip = IPAdmin.pack_ip_addr(:IP => ip, :Version => @version) end # set vars based on version if (@version == 4) @max_bits = 32 @all_f = 2**@max_bits - 1 else @max_bits = 128 @all_f = 2**@max_bits - 1 end # if no netmask or packed_netmask, then set as /32 or /128. # else validate. packed_netmask takes precedence over netmask if (!netmask && !packed_netmask) @netmask = @all_f else if (packed_netmask) IPAdmin.validate_ip_netmask(:Netmask => packed_netmask, :Packed => true, :Version => @version) @netmask = packed_netmask else IPAdmin.validate_ip_netmask(:Netmask => netmask, :Version => @version) @netmask = IPAdmin.pack_ip_netmask(:Netmask => netmask, :Version => @version) end end # set @network & @hostmask @network = (@ip & @netmask) @hostmask = @netmask ^ @all_f end |
Instance Attribute Details
#all_f ⇒ Object (readonly)
Integer of either 32 or 128 bits in length, with all bits set to 1
25 26 27 |
# File 'lib/cidr.rb', line 25 def all_f @all_f end |
#tag ⇒ Object
Hash of custom tags. Should be in the format tag => value.
22 23 24 |
# File 'lib/cidr.rb', line 22 def tag @tag end |
#version ⇒ Object (readonly)
IP version 4 or 6.
19 20 21 |
# File 'lib/cidr.rb', line 19 def version @version end |
Instance Method Details
#arpa ⇒ Object
Depending on the IP version of the current CIDR, return either an in-addr.arpa. or ip6.arpa. string. The netmask will be used to determine the length of the returned string.
-
Arguments:
-
none
-
-
Returns:
-
String
-
Examples:
cidr4 = IPAdmin::CIDR.new('192.168.1.1/24')
cidr6 = IPAdmin::CIDR.new(:CIDR => 'fec0::/64')
puts "arpa for #{cidr4.desc()} is #{cidr4.arpa}"
puts "arpa for #{cidr6.desc(:Short => true)} is #{cidr6.arpa}"
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/cidr.rb', line 216 def arpa() base = self.ip() netmask = self.bits() if (@version == 4) net = base.split('.') if (netmask) while (netmask < 32) net.pop netmask = netmask + 8 end end arpa = net.reverse.join('.') arpa << ".in-addr.arpa." elsif (@version == 6) fields = base.split(':') net = [] fields.each do |field| (field.split("")).each do |x| net.push(x) end end if (netmask) while (netmask < 128) net.pop netmask = netmask + 4 end end arpa = net.reverse.join('.') arpa << ".ip6.arpa." end return(arpa) end |
#bits ⇒ Object
276 277 278 |
# File 'lib/cidr.rb', line 276 def bits() return(IPAdmin.unpack_ip_netmask(:Integer => @netmask)) end |
#contains?(cidr) ⇒ Boolean
Determines if this CIDR contains (is supernet of) the provided CIDR address or IPAdmin::CIDR object.
-
Arguments:
-
CIDR address or IPAdmin::CIDR object
-
-
Returns:
-
true or false
-
Examples:
cidr4 = IPAdmin::CIDR.new('192.168.1.1/24')
cidr6 = IPAdmin::CIDR.new('fec0::/64')
cidr6_2 = IPAdmin::CIDR.new('fec0::/96')
puts "#{cidr4.desc} contains 192.168.1.2" if ( cidr4.contains?('192.168.1.2') )
puts "#{cidr6.desc} contains #{cidr6_2.desc(:Short => true)}" if ( cidr6.contains?(cidr6_2) )
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/cidr.rb', line 300 def contains?(cidr) is_contained = false 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 network = cidr.packed_network netmask = cidr.packed_netmask if (cidr.version != @version) raise ArgumentError, "Attempted to compare a version #{cidr.version} CIDR " + "with a version #{@version} CIDR." end # if network == @network, then we have to go by netmask length # else we can tell by or'ing network and @network by @hostmask # and comparing the results if (network == @network) is_contained = true if (netmask > @netmask) else if ( (network | @hostmask) == (@network | @hostmask) ) is_contained = true end end return(is_contained) end |
#desc(options = nil) ⇒ Object
Returns network/netmask in CIDR format.
-
Arguments:
-
Optional hash with the following fields:
- :IP -- if true, return the original ip/netmask passed during initialization (optional) - :Short -- if true, return IPv6 addresses in short-hand notation (optional)
-
-
Returns:
-
String
-
Examples:
cidr4 = IPAdmin::CIDR.new('192.168.1.1/24')
cidr6 = IPAdmin::CIDR.new('fec0::/64')
puts cidr4.desc(:IP => true)
puts "cidr4 description #{cidr4.desc()}"
puts "cidr6 description #{cidr6.desc()}"
puts "cidr6 short-hand description #{cidr6.desc(:Short => true)}"
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
# File 'lib/cidr.rb', line 358 def desc(=nil) short = false orig_ip = false if () if (!.kind_of? Hash) raise ArgumentError, "Expected Hash, but #{.class} provided." end if (.has_key?(:Short) && [:Short] == true) short = true end if (.has_key?(:IP) && [:IP] == true) orig_ip = true end end if (!orig_ip) ip = IPAdmin.unpack_ip_addr(:Integer => @network, :Version => @version) else ip = IPAdmin.unpack_ip_addr(:Integer => @ip, :Version => @version) end ip = IPAdmin.shorten(ip) if (short && @version == 6) mask = IPAdmin.unpack_ip_netmask(:Integer => @netmask) return("#{ip}/#{mask}") end |
#enumerate(options = nil) ⇒ Object
Provide all IP addresses contained within the IP space of this CIDR.
-
Arguments:
-
Optional Hash with the following fields:
- :Bitstep -- enumerate in X sized steps - Integer (optional) - :Limit -- limit returned list to X number of items - Integer (optional) - :Objectify -- if true, return IPAdmin::CIDR objects (optional) - :Short -- if true, return IPv6 addresses in short-hand notation (optional)
-
-
Returns:
-
Array of Strings, or Array of IPAdmin::CIDR objects
-
Examples:
cidr4 = IPAdmin::CIDR.new('192.168.1.1/24')
cidr6 = IPAdmin::CIDR.new('fec0::/64')
puts "first 4 cidr4 addresses (bitstep 32)"
cidr4.enumerate(:Limit => 4, :Bitstep => 32).each {|x| puts " #{x}"}
puts "first 4 cidr6 addresses (bitstep 32)"
cidr6.enumerate(:Limit => 4, :Bitstep => 32, :Objectify => true).each {|x| puts " #{x.desc}"}
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 |
# File 'lib/cidr.rb', line 411 def enumerate(=nil) bitstep = 1 objectify = false limit = nil short = false if () if (!.kind_of? Hash) raise ArgumentError, "Expected Hash, but #{.class} provided." end if( .has_key?(:Bitstep) ) bitstep = [:Bitstep] end if( .has_key?(:Objectify) && [:Objectify] == true ) objectify = true end if( .has_key?(:Limit) ) limit = [:Limit] end if( .has_key?(:Short) && [:Short] == true ) short = true end end list = [] my_ip = @network change_mask = @hostmask | my_ip until ( change_mask != (@hostmask | @network) ) if (!objectify) my_ip_s = IPAdmin.unpack_ip_addr(:Integer => my_ip, :Version => @version) my_ip_s = IPAdmin.shorten(my_ip_s) if (short && @version == 6) list.push( my_ip_s ) else list.push( IPAdmin::CIDR.new(:PackedIP => my_ip, :Version => @version) ) end my_ip = my_ip + bitstep change_mask = @hostmask | my_ip if (limit) limit = limit -1 break if (limit == 0) end end return(list) end |
#fill_in(options) ⇒ Object
Given a list of subnets of the current CIDR, return a new list with any holes (missing subnets) filled in.
-
Arguments:
-
Array of CIDR addresses, Array of IPAdmin::CIDR objects, or a Hash with the following fields:
- :List -- Array of CIDR addresses or IPAdmin::CIDR objects - :Objectify -- if true, return IPAdmin::CIDR objects (optional) - :Short -- if true, return IPv6 addresses in short-hand notation (optional)
-
-
Returns:
-
Array of CIDR Strings or Array of IPAdmin::CIDR objects
-
Examples:
subnets = cidr4.fill_in(['192.168.1.0/27','192.168.1.64/26','192.168.1.128/25'])
subnets = cidr4.fill_in(:List => ['192.168.1.0/27','192.168.1.64/26'], :Objectify => true)
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 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 573 574 575 576 |
# File 'lib/cidr.rb', line 484 def fill_in() short = false objectify = false # validate options if ( .kind_of?(Hash) ) if (!.has_key?(:List)) raise ArgumentError, "Missing argument: List." end if (.has_key?(:Short) && [:Short] == true) short = true end if (.has_key?(:Objectify) && [:Objectify] == true) objectify = true end # make sure :List is an array if ( ![:List].kind_of?(Array) ) raise ArgumentError, "Expected Array for option :List, " + "but #{list.class} provided." end list = [:List] elsif ( .kind_of?(Array) ) list = else raise ArgumentError, "Array or Hash expected but #{.class} provided." end # validate each cidr and store in cidr_list cidr_list = [] list.each do |obj| if (!obj.kind_of?(IPAdmin::CIDR)) begin obj = IPAdmin::CIDR.new(:CIDR => obj) rescue Exception => error aise ArgumentError, "A provided CIDR raised the following " + "errors: #{error}" end end if (!obj.version == self.version) raise "#{obj.desc(:Short => true)} is not a version #{self.version} address." end # make sure we contain the cidr if ( self.contains?(obj) == false ) raise "#{obj.desc(:Short => true)} does not fit " + "within the bounds of #{self.desc(:Short => true)}." end cidr_list.push(obj) end # sort our cidr's and see what is missing complete_list = [] expected = self.packed_network IPAdmin.sort(cidr_list).each do |cidr| network = cidr.packed_network bitstep = (@all_f + 1) - cidr.packed_netmask if (network > expected) num_ips_missing = network - expected sub_list = make_subnets_from_base_and_ip_count(expected,num_ips_missing) complete_list.concat(sub_list) elsif (network < expected) next end complete_list.push(IPAdmin::CIDR.new(:PackedIP => network, :PackedNetmask => cidr.packed_netmask, :Version => self.version)) expected = network + bitstep end # if expected is not the next subnet, then we're missing subnets # at the end of the cidr next_sub = self.next_subnet(:Objectify => true).packed_network if (expected != next_sub) num_ips_missing = next_sub - expected sub_list = make_subnets_from_base_and_ip_count(expected,num_ips_missing) complete_list.concat(sub_list) end # decide what to return if (!objectify) subnets = [] complete_list.each {|entry| subnets.push(entry.desc(:Short => short))} return(subnets) else return(complete_list) end end |
#hostmask_ext ⇒ Object
Provide IPv4 Hostmask in extended format (y.y.y.y).
-
Arguments:
-
none
-
-
Returns:
-
String
-
Examples:
puts cidr4.hostmask_ext()
593 594 595 596 597 598 599 600 601 |
# File 'lib/cidr.rb', line 593 def hostmask_ext() if (@version == 4) hostmask = IPAdmin.unpack_ip_addr(:Integer => @hostmask, :Version => @version) else raise "IPv6 does not support extended hostmask notation." end return(hostmask) end |
#ip(options = nil) ⇒ Object
Provide original IP address passed during initialization.
-
Arguments:
-
Optional Hash with the following fields:
- :Objectify -- if true, return IPAdmin::CIDR object (optional) - :Short -- if true, return IPv6 addresses in short-hand notation (optional)
-
-
Returns:
-
String or IPAdmin::CIDR object.
-
Examples:
puts cidr4.ip()
puts cidr4.ip(:Objectify => true).desc
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 |
# File 'lib/cidr.rb', line 621 def ip(=nil) objectify = false short = false if () if (!.kind_of?(Hash)) raise Argumenterror, "Expected Hash, but " + "#{.class} provided." end if( .has_key?(:Short) && [:Short] == true ) short = true end if( .has_key?(:Objectify) && [:Objectify] == true ) objectify = true end end if (!objectify) ip = IPAdmin.unpack_ip_addr(:Integer => @ip, :Version => @version) ip = IPAdmin.shorten(ip) if (short && @version == 6) else ip = IPAdmin::CIDR.new(:PackedIP => @ip, :Version => @version) end return(ip) end |
#last(options = nil) ⇒ Object Also known as: broadcast
Provide last IP address in this CIDR object.
-
Arguments:
-
Optional Hash with the following fields:
- :Objectify -- if true, return IPAdmin::CIDR object (optional) - :Short -- if true, return IPv6 addresses in short-hand notation (optional)
-
-
Returns:
-
String or IPAdmin::CIDR object.
-
-
Notes:
-
The broadcast() method is aliased to this method, and thus works for IPv6 despite the fact that the IPv6 protocol does not support IP broadcasting.
-
Examples:
puts cidr4.last()
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 |
# File 'lib/cidr.rb', line 674 def last(=nil) objectify = false short = false if () if (!.kind_of?(Hash)) raise Argumenterror, "Expected Hash, but " + "#{.class} provided." end if( .has_key?(:Short) && [:Short] == true ) short = true end if( .has_key?(:Objectify) && [:Objectify] == true ) objectify = true end end packed_ip = @network | @hostmask if (!objectify) ip = IPAdmin.unpack_ip_addr(:Integer => packed_ip, :Version => @version) ip = IPAdmin.shorten(ip) if (short && !objectify && @version == 6) else ip = IPAdmin::CIDR.new(:PackedIP => packed_ip, :Version => @version) end return(ip) end |
#multicast_mac(options = nil) ⇒ Object
Assuming this CIDR is a valid multicast address (224.0.0.0/4 for IPv4 and ff00::/8 for IPv6), return its ethernet MAC address (EUI-48) mapping.
-
Arguments:
-
Optional Hash with the following fields:
- :Objectify -- if true, return EUI objects (optional)
-
-
Returns:
-
String or IPAdmin::EUI48 object
-
-
Note:
-
MAC address is based on original IP address passed during initialization.
-
Examples:
mcast = IPAdmin::CIDR.new('224.0.0.6')
puts mcast.multicast_mac.address
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 |
# File 'lib/cidr.rb', line 728 def multicast_mac(=nil) objectify = false if () if (!.kind_of? Hash) raise ArgumentError, "Expected Hash, but #{.class} provided." end if (.has_key?(:Objectify) && [:Objectify] == true) objectify = true end end if (@version == 4) if (@ip & 0xf0000000 == 0xe0000000) # map low order 23-bits of ip to 01:00:5e:00:00:00 mac = @ip & 0x007fffff | 0x01005e000000 else raise "#{self.ip} is not a valid multicast address. IPv4 multicast " + "addresses should be in the range 224.0.0.0/4." end else if (@ip & (0xff << 120) == 0xff << 120) # map low order 32-bits of ip to 33:33:00:00:00:00 mac = @ip & (2**32-1) | 0x333300000000 else raise "#{self.ip} is not a valid multicast address. IPv6 multicast " + "addresses should be in the range ff00::/8." end end eui = IPAdmin::EUI48.new(:PackedEUI => mac) eui = eui.address if (!objectify) return(eui) end |
#netmask ⇒ Object
Provide netmask in CIDR format (/yy).
-
Arguments:
-
none
-
-
Returns:
-
String
-
Examples:
puts cidr4.netmask()
780 781 782 783 |
# File 'lib/cidr.rb', line 780 def netmask() bits = IPAdmin.unpack_ip_netmask(:Integer => @netmask) return("/#{bits}") end |
#netmask_ext ⇒ Object
Provide IPv4 netmask in extended format (y.y.y.y).
-
Arguments:
-
none
-
-
Returns:
-
String
-
Examples:
puts cidr4.netmask_ext()
800 801 802 803 804 805 806 807 808 809 |
# File 'lib/cidr.rb', line 800 def netmask_ext() if (@version == 4) netmask = IPAdmin.unpack_ip_addr(:Integer => @netmask) else raise "IPv6 does not support extended netmask notation. " + "Use netmask() method instead." end return(netmask) end |
#network(options = nil) ⇒ Object Also known as: base, first
Provide base network address.
-
Arguments:
-
Optional Hash with the following fields:
- :Objectify -- if true, return IPAdmin::CIDR object (optional) - :Short -- if true, return IPv6 addresses in short-hand notation (optional)
-
-
Returns:
-
String or IPAdmin::CIDR object.
-
Examples:
puts cidr4.network()
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 |
# File 'lib/cidr.rb', line 828 def network(=nil) objectify = false short = false if () if (!.kind_of?(Hash)) raise Argumenterror, "Expected Hash, but " + "#{.class} provided." end if( .has_key?(:Short) && [:Short] == true ) short = true end if( .has_key?(:Objectify) && [:Objectify] == true ) objectify = true end end if (!objectify) ip = IPAdmin.unpack_ip_addr(:Integer => @network, :Version => @version) ip = IPAdmin.shorten(ip) if (short && @version == 6) else ip = IPAdmin::CIDR.new(:PackedIP => @network, :Version => @version) end return(ip) end |
#next_ip(options = nil) ⇒ Object
Provide the next IP following the last available IP within this CIDR object.
-
Arguments:
-
Optional Hash with the following fields:
- :Bitstep -- step in X sized steps - Integer (optional) - :Objectify -- if true, return IPAdmin::CIDR object (optional) - :Short -- if true, return IPv6 addresses in short-hand notation (optional)
-
-
Returns:
-
String or IPAdmin::CIDR object.
-
Examples:
cidr4 = IPAdmin::CIDR.new('192.168.1.1/24')
cidr6 = IPAdmin::CIDR.new('fec0::/64')
puts "cidr4 next subnet #{cidr4.next_subnet()}"
puts "cidr6 next subnet #{cidr6.next_subnet(:Short => true)}"
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 |
# File 'lib/cidr.rb', line 883 def next_ip(=nil) bitstep = 1 objectify = false short = false if () if (!.kind_of?(Hash)) raise Argumenterror, "Expected Hash, but " + "#{.class} provided." end if( .has_key?(:Bitstep) ) bitstep = [:Bitstep] end if( .has_key?(:Short) && [:Short] == true ) short = true end if( .has_key?(:Objectify) && [:Objectify] == true ) objectify = true end end next_ip = @network + @hostmask + bitstep if (next_ip > @all_f) raise "Returned IP is out of bounds for IPv#{@version}." end if (!objectify) next_ip = IPAdmin.unpack_ip_addr(:Integer => next_ip, :Version => @version) next_ip = IPAdmin.shorten(next_ip) if (short && @version == 6) else next_ip = IPAdmin::CIDR.new(:PackedIP => next_ip, :Version => @version) end return(next_ip) end |
#next_subnet(options = nil) ⇒ Object
Provide the next subnet following this CIDR object. The next subnet will be of the same size as the current CIDR object.
-
Arguments:
-
Optional Hash with the following fields:
- :Bitstep -- step in X sized steps. - Integer (optional) - :Objectify -- if true, return IPAdmin::CIDR object (optional) - :Short -- if true, return IPv6 addresses in short-hand notation (optional)
-
-
Returns:
-
String or IPAdmin::CIDR object.
-
Examples:
cidr4 = IPAdmin::CIDR.new('192.168.1.1/24')
cidr6 = IPAdmin::CIDR.new('fec0::/64')
puts "cidr4 next subnet #{cidr4.next_subnet()}"
puts "cidr6 next subnet #{cidr6.next_subnet(:Short => true)}"
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 |
# File 'lib/cidr.rb', line 946 def next_subnet(=nil) bitstep = 1 objectify = false short = false if () if (!.kind_of?(Hash)) raise Argumenterror, "Expected Hash, but " + "#{.class} provided." end if( .has_key?(:Bitstep) ) bitstep = [:Bitstep] end if( .has_key?(:Short) && [:Short] == true ) short = true end if( .has_key?(:Objectify) && [:Objectify] == true ) objectify = true end end bitstep = bitstep * (2**(@max_bits - self.bits) ) next_sub = @network + bitstep if (next_sub > @all_f) raise "Returned subnet is out of bounds for IPv#{@version}." end if (!objectify) next_sub = IPAdmin.unpack_ip_addr(:Integer => next_sub, :Version => @version) next_sub = IPAdmin.shorten(next_sub) if (short && @version == 6) next_sub = next_sub << "/" << self.bits.to_s else next_sub = IPAdmin::CIDR.new(:PackedIP => next_sub, :PackedNetmask => self.packed_netmask, :Version => @version) end return(next_sub) end |
#nth(options) ⇒ Object
Provide the nth IP within this object.
-
Arguments:
-
Integer or a Hash with the following fields:
- :Index -- index number of the IP address to return - Integer - :Objectify -- if true, return IPAdmin::CIDR objects (optional) - :Short -- if true, return IPv6 addresses in short-hand notation (optional)
-
-
Returns:
-
String or IPAdmin::CIDR object.
-
Examples:
cidr4 = IPAdmin::CIDR.new('192.168.1.1/24')
puts cidr4.nth(1)
puts cidr4.nth(:Index => 1, :Objectify => true)
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 |
# File 'lib/cidr.rb', line 1010 def nth() objectify = false short = false if (.kind_of?(Hash)) if ( !.has_key?(:Index) ) raise ArgumentError, "Missing argument: Index." end index = [:Index] if( .has_key?(:Short) && [:Short] == true ) short = true end if( .has_key?(:Objectify) && [:Objectify] == true ) objectify = true end elsif (.kind_of?(Integer)) index = else raise ArgumentError, "Integer or Hash expected, but " + "#{.class} provided." end my_ip = @network + index if ( (@hostmask | my_ip) == (@hostmask | @network) ) if (!objectify) my_ip = IPAdmin.unpack_ip_addr(:Integer => my_ip, :Version => @version) my_ip = IPAdmin.shorten(my_ip) if (short && @version == 6) else my_ip = IPAdmin::CIDR.new(:PackedIP => my_ip, :Version => @version) end else raise "Index of #{index} returns IP that is out of " + "bounds of CIDR network." end return(my_ip) end |
#packed_hostmask ⇒ Object
Provide an Integer representation of the Hostmask of this object.
-
Arguments:
-
none
-
-
Returns:
-
Integer
-
Examples:
puts cidr4.packed_hostmask().to_s(16)
1067 1068 1069 |
# File 'lib/cidr.rb', line 1067 def packed_hostmask() return(@hostmask) end |
#packed_ip ⇒ Object
Provide an Integer representation of the IP address of this object.
-
Arguments:
-
none
-
-
Returns:
-
Integer
-
Examples:
puts cidr4.packed_ip().to_s(16)
1086 1087 1088 |
# File 'lib/cidr.rb', line 1086 def packed_ip() return(@ip) end |
#packed_netmask ⇒ Object
Provide an Integer representation of the Netmask of this object.
-
Arguments:
-
none
-
-
Returns:
-
Integer
-
Examples:
puts cidr4.packed_netmask().to_s(16)
1105 1106 1107 |
# File 'lib/cidr.rb', line 1105 def packed_netmask() return(@netmask) end |
#packed_network ⇒ Object
Provide an Integer representation of the Network address of this object.
-
Arguments:
-
none
-
-
Returns:
-
Integer
-
Examples:
packed = cidr4.packed_network().to_s(16)
1124 1125 1126 |
# File 'lib/cidr.rb', line 1124 def packed_network() return(@network) end |
#range(options) ⇒ Object
Given two Indexes, return all IP addresses within the CIDR that are between them (inclusive).
-
Arguments:
-
Array of (2) Integers, or a Hash with the following fields:
- :Bitstep -- enumerate in X sized steps - Integer (optional) - :Indexes -- index numbers of the addresses to use as boundaries - Array of (2) Integers - :Objectify -- if true, return IPAdmin::CIDR objects (optional) - :Short -- if true, return IPv6 addresses in short-hand notation (optional)
-
-
Returns:
-
Array of Strings, or Array of IPAdmin::CIDR objects
-
Examples:
cidr4 = IPAdmin::CIDR.new('192.168.1.1/24')
list = cidr4.range([0,1])
list = cidr4.range(:Indexes => [0,1], :Objectify => true)
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 |
# File 'lib/cidr.rb', line 1150 def range() objectify = false short = false bitstep = 1 if (.kind_of?(Hash)) if ( !.has_key?(:Indexes) ) raise ArgumentError, "Missing argument: Indexes." end indexes = [:Indexes] raise "Array expected but #{indexes.class} provided for argument: Indexes" if (!indexes.kind_of?(Array)) if( .has_key?(:Short) && [:Short] == true ) short = true end if( .has_key?(:Objectify) && [:Objectify] == true ) objectify = true end if( .has_key?(:Bitstep) ) bitstep = [:Bitstep] end elsif (.kind_of?(Array)) indexes = else raise Argumenterror, "Array or Hash expected, but #{.class} provided." end # validate & sort indexes indexes.sort! if (indexes.length != 2) raise "(2) index numbers are required." end if ( (indexes[0] < 0) || (indexes[0] > self.size) ) raise ArgumentError, "Index #{indexes[0]} is out of bounds for this CIDR." end if (indexes[1] >= self.size) raise ArgumentError, "Index #{indexes[1]} is out of bounds for this CIDR." end # make range start_ip = @network + indexes[0] end_ip = @network + indexes[1] my_ip = start_ip list = [] until (my_ip > end_ip) if (!objectify) ip = IPAdmin.unpack_ip_addr(:Integer => my_ip, :Version => @version) ip = IPAdmin.shorten(ip) if (short && @version == 6) else ip = IPAdmin::CIDR.new(:PackedIP => my_ip, :Version => @version) end list.push(ip) my_ip += bitstep end return(list) end |
#remainder(options) ⇒ Object
Given a single subnet of the current CIDR, provide the remainder of the subnets. For example if the original CIDR is 192.168.0.0/24 and you provide 192.168.0.64/26 as the portion to exclude, then 192.168.0.0/26, and 192.168.0.128/25 will be returned as the remainders.
-
Arguments:
-
CIDR address, IPAdmin::CIDR object, or a Hash with the following fields:
- :Exclude -- CIDR address or IPAdmin::CIDR object. - :Objectify -- if true, return IPAdmin::CIDR objects (optional) - :Short -- if true, return IPv6 addresses in short-hand notation (optional)
-
-
Returns:
-
Array of Strings, or Array of IPAdmin::CIDR objects
-
Examples:
cidr4.remainder('192.168.1.32/27').each {|x| puts x}
cidr4.remainder(:Exclude => '192.168.1.32/27', :Objectify => true).each {|x| puts x.desc}
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 |
# File 'lib/cidr.rb', line 1235 def remainder() short = nil objectify = nil if (.kind_of? Hash) if ( !.has_key?(:Exclude) ) raise ArgumentError, "Missing argument: Exclude." end to_exclude = [:Exclude] if( .has_key?(:Short) && [:Short] == true ) short = true end if( .has_key?(:Objectify) && [:Objectify] == true ) objectify = true end elsif to_exclude = else raise ArgumentError, "CIDR address or Hash expected, but #{.class} provided." end if ( !to_exclude.kind_of?(IPAdmin::CIDR) ) begin to_exclude = IPAdmin::CIDR.new(:CIDR => to_exclude) rescue Exception => error raise ArgumentError, "Argument :Exclude raised the following " + "errors: #{error}" end end # make sure 'to_exclude' is the same ip version if ( to_exclude.version != @version ) raise "#{to_exclude.desc(:Short => true)} is of a different " + "IP version than #{self.desc(:Short => true)}." end # make sure we contain 'to_exclude' if ( self.contains?(to_exclude) != true ) raise "#{to_exclude.desc(:Short => true)} does not fit " + "within the bounds of #{self.desc(:Short => true)}." end # split this cidr in half & see which half 'to_exclude' # belongs in. take that half & repeat the process. every time # we repeat, store off the non-matching half new_mask = self.bits + 1 lower_network = self.packed_network upper_network = self.packed_network + 2**(@max_bits - new_mask) new_subnets = [] until(new_mask > to_exclude.bits) if (to_exclude.packed_network < upper_network) match = lower_network non_match = upper_network else match = upper_network non_match = lower_network end if (!objectify) non_match = IPAdmin.unpack_ip_addr(:Integer => non_match, :Version => @version) non_match = IPAdmin.shorten(non_match) if (short && @version == 6) new_subnets.unshift("#{non_match}/#{new_mask}") else new_subnets.unshift(IPAdmin::CIDR.new(:PackedIP => non_match, :PackedNetmask => IPAdmin.pack_ip_netmask(new_mask), :Version => @version)) end new_mask = new_mask + 1 lower_network = match upper_network = match + 2**(@max_bits - new_mask) end return(new_subnets) end |
#resize(options) ⇒ Object
Resize the CIDR by changing the size of the Netmask. Return the resulting CIDR as a new object.
-
Arguments:
-
Integer, or a Hash with the following fields:
- :Netmask -- Number of bits of new Netmask - Integer
-
-
Returns:
-
IPAdmin::CIDR object
-
Examples:
cidr4 = IPAdmin::CIDR.new('192.168.1.1/24')
new_cidr = cidr4.resize(23)
new_cidr = cidr4.resize(:Netmask => 23)
puts new_cidr.desc
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 |
# File 'lib/cidr.rb', line 1337 def resize() if (.kind_of?(Hash)) if ( !.has_key?(:Netmask) ) raise Argumenterror, "Missing argument: Netmask." end bits = [:Netmask] elsif (.kind_of?(Integer)) bits = else raise Argumenterror, "Integer or Hash expected, but " + "#{.class} provided." end IPAdmin.validate_ip_netmask(:Netmask => bits, :Version => @version) netmask = IPAdmin.pack_ip_netmask(:Netmask => bits, :Version => @version) network = @network & netmask cidr = IPAdmin::CIDR.new(:PackedIP => network, :PackedNetmask => netmask, :Version => @version) return(cidr) end |
#resize!(options) ⇒ Object
Resize this object by changing the size of the Netmask.
-
Arguments:
-
Integer, or a Hash with the following fields:
- :Netmask -- Number of bits of new Netmask - Integer
-
-
Returns:
-
True
-
-
Notes:
-
If CIDR is resized such that the original IP is no longer contained within, then that IP will be reset to the base network address.
-
1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 |
# File 'lib/cidr.rb', line 1375 def resize!() if (.kind_of?(Hash)) if ( !.has_key?(:Netmask) ) raise Argumenterror, "Missing argument: Netmask." end bits = [:Netmask] elsif (.kind_of?(Integer)) bits = else raise Argumenterror, "Integer or Hash expected, but " + "#{.class} provided." end IPAdmin.validate_ip_netmask(:Netmask => bits, :Version => @version) netmask = IPAdmin.pack_ip_netmask(:Netmask => bits, :Version => @version) @netmask = netmask @network = @network & netmask @hostmask = @netmask ^ @all_f # check @ip if ((@ip & @netmask) != (@network)) @ip = @network end return(true) end |
#size ⇒ Object
Provide number of IP addresses within this object.
-
Arguments:
-
none
-
-
Returns:
-
Integer
-
Examples:
puts cidr4.size()
1418 1419 1420 |
# File 'lib/cidr.rb', line 1418 def size() return(@hostmask + 1) end |
#subnet(options = nil) ⇒ Object
Subnet this object. There are 2 ways to subnet:
* By providing the netmask of the new subnets in :Subnet.
* By providing the number of IP addresses needed in the new subnets in :IPCount
If :Mincount is not provided, then the CIDR will be fully subnetted. Otherwise, :Mincount number of subnets of the requested size will be returned and the remainder of the subnets will be summarized as much as possible. If neither :Subnet or :IPCount is provided, then the current CIDR will be split in half.
-
Arguments:
-
Optional Hash with the following fields:
- :IPCount -- Minimum number of IP's that new subnets should contain - Integer (optional) - :MinCount -- Minimum number of X sized subnets to return - Integer (optional) - :Objectify -- if true, return IPAdmin::CIDR objects (optional) - :Short -- if true, return IPv6 addresses in short-hand notation (optional) - :Subnet -- Netmask (in bits) of new subnets - Integer (optional)
-
-
Returns:
-
Array of Strings, or Array of IPAdmin::CIDR objects
-
-
Notes:
-
:Subnet always takes precedence over :IPCount.
-
Examples:
cidr_list = cidr4.subnet(:Subnet => 28, :MinCount => 3).each {|x| puts " #{x}"}
cidr_list = cidr4.subnet(:IPCount => 19).each {|x| puts " #{x}"}
cidr4.subnet(:Subnet => 28).each {|x| puts " #{x}"} "
cidr6.subnet(:Subnet => 67, :MinCount => 4, :Short => true).each {|x| puts " #{x}"}
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 |
# File 'lib/cidr.rb', line 1455 def subnet(=nil) my_network = self.packed_network my_mask = self.bits subnet_bits = my_mask + 1 min_count = nil objectify = false short = false if () if (!.kind_of? Hash) raise ArgumentError, "Expected Hash, but #{.class} provided." end if ( .has_key?(:IPCount) ) subnet_bits = IPAdmin.minimum_size(:IPCount => [:IPCount], :Version => @version) end if ( .has_key?(:Subnet) ) subnet_bits = [:Subnet] end if ( .has_key?(:MinCount) ) min_count = [:MinCount] end if( .has_key?(:Short) && [:Short] == true ) short = true end if( .has_key?(:Objectify) && [:Objectify] == true ) objectify = true end end # get number of subnets possible with the requested subnet_bits num_avail = 2**(subnet_bits - my_mask) # get the number of bits in the next supernet and # make sure min_count is a power of 2 bits_needed = 1 min_count = num_avail if (!min_count) until (2**bits_needed >= min_count) bits_needed += 1 end min_count = 2**bits_needed next_supernet_bits = subnet_bits - bits_needed # make sure subnet isnt bigger than available bits if (subnet_bits > @max_bits) raise "Requested subnet (#{subnet_bits}) does not fit " + "within the bounds of IPv#{@version}." end # make sure subnet is larger than mymask if (subnet_bits < my_mask) raise "Requested subnet (#{subnet_bits}) is too large for " + "current CIDR space." end # make sure MinCount is smaller than available subnets if (min_count > num_avail) raise "Requested subnet count (#{min_count}) exceeds subnets " + "available for allocation (#{num_avail})." end # list all 'subnet_bits' sized subnets of this cidr block # with a limit of min_count bitstep = 2**(@max_bits - subnet_bits) subnets = self.enumerate(:Bitstep => bitstep, :Limit => min_count) # save our subnets new_subnets = [] subnets.each do |subnet| if (!objectify) subnet = IPAdmin.shorten(subnet) if (short && @version == 6) new_subnets.push("#{subnet}/#{subnet_bits}") else new_subnets.push(IPAdmin::CIDR.new(:CIDR => "#{subnet}/#{subnet_bits}", :Version => @version)) end end # now go through the rest of the cidr space and make the rest # of the subnets. we want these to be as tightly merged as possible next_supernet_bitstep = (bitstep * min_count) next_supernet_ip = my_network + next_supernet_bitstep until (next_supernet_bits == my_mask) if (!objectify) next_network = IPAdmin.unpack_ip_addr(:Integer => next_supernet_ip, :Version => @version) next_network = IPAdmin.shorten(next_network) if (short && @version == 6) new_subnets.push("#{next_network}/#{next_supernet_bits}") else new_subnets.push(IPAdmin::CIDR.new(:PackedIP => next_supernet_ip, :PackedNetmask => IPAdmin.pack_ip_netmask(next_supernet_bits), :Version => @version)) end next_supernet_bits -= 1 next_supernet_ip = next_supernet_ip + next_supernet_bitstep next_supernet_bitstep = next_supernet_bitstep << 1 end return(new_subnets) end |