module Vagrant::Util::NetworkIP

Public Instance Methods

network_address(ip, subnet) click to toggle source

Returns the network address of the given IP and subnet.

@return [String]

# File lib/vagrant/util/network_ip.rb, line 7
def network_address(ip, subnet)
  ip      = ip_parts(ip)
  netmask = ip_parts(subnet)

  # Bitwise-AND each octet to get the network address
  # in octets and join each part with a period to get
  # the resulting network address.
  ip.map { |part| part & netmask.shift }.join(".")
end

Protected Instance Methods

ip_parts(ip) click to toggle source

Splits an IP into the four octets and returns each as an integer in an array.

@return [Array<Integer>]

# File lib/vagrant/util/network_ip.rb, line 23
def ip_parts(ip)
  ip.split(".").map { |i| i.to_i }
end