diff --git a/plugins/providers/virtualbox/action/network_fix_ipv6.rb b/plugins/providers/virtualbox/action/network_fix_ipv6.rb index dcfa2632e..853890441 100644 --- a/plugins/providers/virtualbox/action/network_fix_ipv6.rb +++ b/plugins/providers/virtualbox/action/network_fix_ipv6.rb @@ -3,6 +3,7 @@ require "socket" require "log4r" +require "vagrant/util/presence" require "vagrant/util/scoped_hash_override" module VagrantPlugins @@ -12,6 +13,7 @@ module VagrantPlugins # a VM with an IPv6 host-only network will someties lose the # route to that machine. class NetworkFixIPv6 + include Vagrant::Util::Presence include Vagrant::Util::ScopedHashOverride def initialize(app, env) @@ -41,17 +43,15 @@ module VagrantPlugins # If we have no IPv6, forget it return if !has_v6 - ifaces = env[:machine].provider.driver.read_network_interfaces - ifaces.select! { |id, iface| iface[:type] == :hostonly } - iface_names = ifaces.values.map { |iface| iface[:hostonly] } - networks = env[:machine].provider.driver.read_host_only_interfaces - networks.select! { |network| iface_names.include?(network[:name]) } - networks.each do |network| - next if network[:ipv6] == "" - next if network[:status] != 'Up' - ip = IPAddr.new(network[:ipv6]) | - ('1' * (128 - network[:ipv6_prefix].to_i)).to_i(2) + networks(env).each do |network| + next if !present?(network[:ipv6]) + next if network[:status] != "Up" + + ip = IPAddr.new(network[:ipv6]) + ip |= ("1" * (128 - network[:ipv6_prefix].to_i)).to_i(2) + @logger.info("testing IPv6: #{ip}") + begin UDPSocket.new(Socket::AF_INET6).connect(ip.to_s, 80) rescue Errno::EHOSTUNREACH @@ -60,6 +60,21 @@ module VagrantPlugins end end end + + # The list of interface names for host-only adapters. + # @return [Array] + def host_only_interface_names(env) + env[:machine].provider.driver.read_network_interfaces + .map { |_, i| i[:hostonly] if i[:type] == :hostonly }.compact + end + + # The list of networks that are tied to a host-only adapter. + # @return [Array] + def networks(env) + iface_names = self.host_only_interface_names(env) + env[:machine].provider.driver.read_host_only_interfaces + .select { |network| iface_names.include?(network[:name]) } + end end end end