Class: VagrantPlugins::Skytap::Util::Subnet
- Defined in:
- lib/vagrant-skytap/util/subnet.rb
Defined Under Namespace
Classes: InvalidSubnet
Instance Attribute Summary collapse
-
#address ⇒ Object
readonly
Returns the value of attribute address.
-
#mask ⇒ Object
readonly
Returns the value of attribute mask.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #contains?(ip) ⇒ Boolean
- #each_address ⇒ Object
-
#initialize(cidr_block) ⇒ Subnet
constructor
A new instance of Subnet.
- #max ⇒ Object
- #min_machine_ip ⇒ Object
- #network_portion ⇒ Object (also: #min)
- #normalize ⇒ Object
- #normalized? ⇒ Boolean
- #num_addresses ⇒ Object
- #overlaps?(other) ⇒ Boolean
- #strictly_subsumes?(other) ⇒ Boolean
- #subsumes?(other) ⇒ Boolean
- #to_s ⇒ Object
Constructor Details
#initialize(cidr_block) ⇒ Subnet
Returns a new instance of Subnet.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/vagrant-skytap/util/subnet.rb', line 11 def initialize(cidr_block) unless cidr_block =~ /^(.*)\/(.*)/ raise InvalidSubnet.new 'Not in CIDR block form (XX.XX.XX.XX/YY)' end network = $1 begin @size = Integer($2) rescue raise InvalidSubnet.new 'Invalid size' end @address = IpAddress.new(network) @mask = size_to_mask(@size) end |
Instance Attribute Details
#address ⇒ Object (readonly)
Returns the value of attribute address.
9 10 11 |
# File 'lib/vagrant-skytap/util/subnet.rb', line 9 def address @address end |
#mask ⇒ Object (readonly)
Returns the value of attribute mask.
9 10 11 |
# File 'lib/vagrant-skytap/util/subnet.rb', line 9 def mask @mask end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
9 10 11 |
# File 'lib/vagrant-skytap/util/subnet.rb', line 9 def size @size end |
Instance Method Details
#<=>(other) ⇒ Object
83 84 85 |
# File 'lib/vagrant-skytap/util/subnet.rb', line 83 def <=>(other) to_s <=> other.to_s end |
#==(other) ⇒ Object
32 33 34 35 36 |
# File 'lib/vagrant-skytap/util/subnet.rb', line 32 def ==(other) other.is_a?(Subnet) && \ (size == other.size) && \ network_portion == other.network_portion end |
#contains?(ip) ⇒ Boolean
58 59 60 61 |
# File 'lib/vagrant-skytap/util/subnet.rb', line 58 def contains?(ip) ip = IpAddress.new(ip) unless ip.is_a?(IpAddress) (ip & mask) == network_portion end |
#each_address ⇒ Object
54 55 56 |
# File 'lib/vagrant-skytap/util/subnet.rb', line 54 def each_address (min.to_i..max.to_i).each{|i| yield IpAddress.new(i)} end |
#max ⇒ Object
50 51 52 |
# File 'lib/vagrant-skytap/util/subnet.rb', line 50 def max @max ||= (mask.inverse | network_portion) end |
#min_machine_ip ⇒ Object
67 68 69 |
# File 'lib/vagrant-skytap/util/subnet.rb', line 67 def min_machine_ip min + 1 end |
#network_portion ⇒ Object Also known as: min
27 28 29 |
# File 'lib/vagrant-skytap/util/subnet.rb', line 27 def network_portion @network_portion ||= (mask & address) end |
#normalize ⇒ Object
75 76 77 |
# File 'lib/vagrant-skytap/util/subnet.rb', line 75 def normalize Subnet.new("#{network_portion}/#{size}") end |
#normalized? ⇒ Boolean
71 72 73 |
# File 'lib/vagrant-skytap/util/subnet.rb', line 71 def normalized? address == network_portion end |
#num_addresses ⇒ Object
63 64 65 |
# File 'lib/vagrant-skytap/util/subnet.rb', line 63 def num_addresses 2 ** (32-size) end |
#overlaps?(other) ⇒ Boolean
38 39 40 |
# File 'lib/vagrant-skytap/util/subnet.rb', line 38 def overlaps?(other) min <= other.max && max >= other.min end |
#strictly_subsumes?(other) ⇒ Boolean
46 47 48 |
# File 'lib/vagrant-skytap/util/subnet.rb', line 46 def strictly_subsumes?(other) subsumes?(other) && self != other end |
#subsumes?(other) ⇒ Boolean
42 43 44 |
# File 'lib/vagrant-skytap/util/subnet.rb', line 42 def subsumes?(other) min <= other.min && max >= other.max end |
#to_s ⇒ Object
79 80 81 |
# File 'lib/vagrant-skytap/util/subnet.rb', line 79 def to_s "#{address}/#{size}" end |