Class: VagrantPlugins::Skytap::Util::Subnet

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-skytap/util/subnet.rb

Defined Under Namespace

Classes: InvalidSubnet

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cidr_block) ⇒ Subnet

Returns a new instance of Subnet.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/vagrant-skytap/util/subnet.rb', line 33

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

#addressObject (readonly)

Returns the value of attribute address.



31
32
33
# File 'lib/vagrant-skytap/util/subnet.rb', line 31

def address
  @address
end

#maskObject (readonly)

Returns the value of attribute mask.



31
32
33
# File 'lib/vagrant-skytap/util/subnet.rb', line 31

def mask
  @mask
end

#sizeObject (readonly)

Returns the value of attribute size.



31
32
33
# File 'lib/vagrant-skytap/util/subnet.rb', line 31

def size
  @size
end

Instance Method Details

#<=>(other) ⇒ Object



105
106
107
# File 'lib/vagrant-skytap/util/subnet.rb', line 105

def <=>(other)
  to_s <=> other.to_s
end

#==(other) ⇒ Object



54
55
56
57
58
# File 'lib/vagrant-skytap/util/subnet.rb', line 54

def ==(other)
  other.is_a?(Subnet) && \
  (size == other.size) && \
  network_portion == other.network_portion
end

#contains?(ip) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
# File 'lib/vagrant-skytap/util/subnet.rb', line 80

def contains?(ip)
  ip = IpAddress.new(ip) unless ip.is_a?(IpAddress)
  (ip & mask) == network_portion
end

#each_addressObject



76
77
78
# File 'lib/vagrant-skytap/util/subnet.rb', line 76

def each_address
  (min.to_i..max.to_i).each{|i| yield IpAddress.new(i)}
end

#maxObject



72
73
74
# File 'lib/vagrant-skytap/util/subnet.rb', line 72

def max
  @max ||= (mask.inverse | network_portion)
end

#min_machine_ipObject



89
90
91
# File 'lib/vagrant-skytap/util/subnet.rb', line 89

def min_machine_ip
  min + 1
end

#network_portionObject Also known as: min



49
50
51
# File 'lib/vagrant-skytap/util/subnet.rb', line 49

def network_portion
  @network_portion ||= (mask & address)
end

#normalizeObject



97
98
99
# File 'lib/vagrant-skytap/util/subnet.rb', line 97

def normalize
  Subnet.new("#{network_portion}/#{size}")
end

#normalized?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/vagrant-skytap/util/subnet.rb', line 93

def normalized?
  address == network_portion
end

#num_addressesObject



85
86
87
# File 'lib/vagrant-skytap/util/subnet.rb', line 85

def num_addresses
  2 ** (32-size)
end

#overlaps?(other) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/vagrant-skytap/util/subnet.rb', line 60

def overlaps?(other)
  min <= other.max && max >= other.min
end

#strictly_subsumes?(other) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/vagrant-skytap/util/subnet.rb', line 68

def strictly_subsumes?(other)
  subsumes?(other) && self != other
end

#subsumes?(other) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/vagrant-skytap/util/subnet.rb', line 64

def subsumes?(other)
  min <= other.min && max >= other.max
end

#to_sObject



101
102
103
# File 'lib/vagrant-skytap/util/subnet.rb', line 101

def to_s
  "#{address}/#{size}"
end