diff --git a/plugins/providers/virtualbox/action/network.rb b/plugins/providers/virtualbox/action/network.rb index cb8f6da7a..d0c9aeb6e 100644 --- a/plugins/providers/virtualbox/action/network.rb +++ b/plugins/providers/virtualbox/action/network.rb @@ -287,44 +287,32 @@ module VagrantPlugins interface_name: interface[:name] end end - - # Split the IP address into its components - ip_parts = netaddr.split(".").map { |i| i.to_i } - - # Calculate the adapter IP, which we assume is the IP ".1" at - # the end usually. - adapter_ip = ip_parts.dup - adapter_ip[3] += 1 - options[:adapter_ip] ||= adapter_ip.join(".") elsif ip.ipv6? # Default subnet prefix length options[:netmask] ||= 64 - # Set adapter IP to ::1 - options[:adapter_ip] ||= (ip.mask(options[:netmask].to_i) | 1).to_s - # Append a 6 to the end of the type options[:type] = "#{options[:type]}6".to_sym else raise "BUG: Unknown IP type: #{ip.inspect}" end + # Calculate the adapter IP which is the network address with + # the final bit + 1. Usually it is "x.x.x.1" for IPv4 and + # "::1" for IPv6 + options[:adapter_ip] ||= (ip.mask(options[:netmask]) | 1).to_s + dhcp_options = {} if options[:type] == :dhcp - # Calculate the DHCP server IP, which is the network address - # with the final octet + 2. So "172.28.0.0" turns into "172.28.0.2" - dhcp_ip = ip_parts.dup - dhcp_ip[3] += 2 - dhcp_options[:dhcp_ip] = options[:dhcp_ip] || dhcp_ip.join(".") - - # Calculate the lower and upper bound for the DHCP server - dhcp_lower = ip_parts.dup - dhcp_lower[3] += 3 - dhcp_options[:dhcp_lower] = options[:dhcp_lower] || dhcp_lower.join(".") - - dhcp_upper = ip_parts.dup - dhcp_upper[3] = 254 - dhcp_options[:dhcp_upper] = options[:dhcp_upper] || dhcp_upper.join(".") + # Calculate the DHCP server IP and lower & upper bound + # Example: for "192.168.22.64/26" network range those are: + # dhcp_ip: "192.168.22.66", + # dhcp_lower: "192.168.22.67" + # dhcp_upper: "192.168.22.126" + ip_range = ip.mask(options[:netmask]).to_range + dhcp_options[:dhcp_ip] = options[:dhcp_ip] || (ip_range.first | 2).to_s + dhcp_options[:dhcp_lower] = options[:dhcp_lower] || (ip_range.first | 3).to_s + dhcp_options[:dhcp_upper] = options[:dhcp_upper] || (ip_range.last(2).first).to_s end return {