Class: Antfarm::IPAddrExt

Inherits:
IPAddr
  • Object
show all
Defined in:
lib/antfarm.rb

Overview

Some explanation to having @netmask and such:

If you create a new IPAddr object and you include
the network information for the IP address, IPAddr
doesn't keep track of the actual address, and
instead just keeps track of the network.  For
example, if you were to create a new IPAddr object
using the following code:

IPAddr.new("192.168.101.5/24")

the resulting object would be of the form:

<IPAddr: IPv4:192.168.101.0/255.255.255.0>

and there would be no way to retrieve the original
address (192.168.101.5).  By creating this class,
Michael has made it possible to keep track of both
the address and the network information.  This is
useful in the case of creating a new IPInterface
object.

TODO: If a netmask is given, should we somehow check

to see if an address is being given with network
information or if a network is being specified,
and if it is a network, should we validate that
the network address is valid with the given
netmask?  This may be done automatically... I
need to look more into how IPAddr works.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ IPAddrExt

Returns a new instance of IPAddrExt.



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/antfarm.rb', line 144

def initialize(value)
  address,netmask = value.split('/')
  super(address)

  if self.ipv4?
    @netmask = IPAddr.new('255.255.255.255')
    @addr_bits = 32
  elsif self.ipv6?
    @netmask = IPAddr.new('ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff')
    @addr_bits = 128
  else
    #TODO: Error
  end
   
  if netmask
    @netmask = @netmask.mask(netmask)
  end
end

Instance Attribute Details

#netmaskObject

Returns the value of attribute netmask.



163
164
165
# File 'lib/antfarm.rb', line 163

def netmask
  @netmask
end

Instance Method Details

#broadcastObject



186
187
188
# File 'lib/antfarm.rb', line 186

def broadcast
  return self.network | ~self.netmask
end

#in_address_list?(addr_str_list) ⇒ Boolean

Returns:

  • (Boolean)


212
213
214
215
216
217
218
219
220
221
# File 'lib/antfarm.rb', line 212

def in_address_list?(addr_str_list)
  for addr_str in addr_str_list
    addr = IPAddr.new(addr_str)
    if addr.include?(self)
      return true
    end
  end

  return false    
end

#loopback_address?Boolean

TODO: track down IPv6 localnet mask (guessing /10 for now)

Returns:

  • (Boolean)


200
201
202
203
# File 'lib/antfarm.rb', line 200

def loopback_address?
  loopback_addr_list = ['127.0.0.0/8', '::1', 'fe00::/10']
  return self.in_address_list?(loopback_addr_list)
end

#multicast_address?Boolean

Need to verify the IPv4 multicast addrs (couldn’t find the whole block, only the currently assigned ranges within the block)

Returns:

  • (Boolean)


207
208
209
210
# File 'lib/antfarm.rb', line 207

def multicast_address?
  multicast_addr_list = ['224.0.0.0/4', 'ff00::/8']
  return self.in_address_list?(multicast_addr_list)
end

#netmask_lengthObject



165
166
167
168
169
170
171
172
173
174
175
# File 'lib/antfarm.rb', line 165

def netmask_length
  mask_len = @addr_bits
  unless (~@netmask).to_i == 0
    res = Math.log((~@netmask).to_i) / Math.log(2)
    if res.finite?
      mask_len -= res.round
    end
  end

  return mask_len
end

#networkObject



177
178
179
# File 'lib/antfarm.rb', line 177

def network
  return self.mask(self.netmask.to_s)
end

#network_in_network?(network) ⇒ Boolean

Decides if the given network is a subset of this network. This method was added since SQLite3 cannot handle CIDR’s ‘natively’ like PostgreSQL can. Note that this method also works if the network given is actually a host.

Returns:

  • (Boolean)


227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/antfarm.rb', line 227

def network_in_network?(network)
  broadcast = nil

  if network.kind_of?(String)
    broadcast = IPAddrExt.new(network).broadcast
    network = IPAddr.new(network)
  elsif network.kind_of?(Antfarm::IPAddrExt)
    broadcast = network.broadcast
    network = IPAddr.new(network.to_cidr_string)
  else
    raise(ArgumentError, "argument should be either a String or an Antfarm::IPAddrExt object", caller)
  end

  return false unless IPAddr.new(self.to_cidr_string).include?(network)
  return false unless IPAddr.new(self.to_cidr_string).include?(broadcast)
  return true
end

#private_address?Boolean

TODO: track down the IPv6 private use ranges and include them

Returns:

  • (Boolean)


191
192
193
194
195
196
197
# File 'lib/antfarm.rb', line 191

def private_address?
  private_addr_list = [
    '10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16',
    'fe80::/10', 'fec0::/10'
  ]
  return self.in_address_list?(private_addr_list)
end

#to_cidr_stringObject



181
182
183
184
# File 'lib/antfarm.rb', line 181

def to_cidr_string
  str = sprintf("%s/%s", self.network.to_string, self.netmask_length.to_s)
  return str
end