From e31ef0167981f7a90a304b25fc23e1189d5e34ec Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 22 Dec 2011 11:47:52 -0800 Subject: [PATCH] NAT checking for forwarded ports --- lib/vagrant/action/vm/forward_ports.rb | 15 ++++++++----- lib/vagrant/driver/virtualbox.rb | 31 ++++++++++++++++++++------ 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/lib/vagrant/action/vm/forward_ports.rb b/lib/vagrant/action/vm/forward_ports.rb index ec246cd81..2fa6417da 100644 --- a/lib/vagrant/action/vm/forward_ports.rb +++ b/lib/vagrant/action/vm/forward_ports.rb @@ -91,6 +91,8 @@ module Vagrant def forward_ports(vm) ports = [] + interfaces = @env[:vm].driver.read_network_interfaces + @env[:vm].config.vm.forwarded_ports.each do |name, options| adapter = options[:adapter] + 1 message_attributes = { @@ -107,13 +109,16 @@ module Vagrant @env[:ui].info(I18n.t("vagrant.actions.vm.forward_ports.forwarding_entry", message_attributes)) + # Port forwarding requires the network interface to be a NAT interface, + # so verify that that is the case. + if interfaces[adapter][:type] != "nat" + @env[:ui].info(I18n.t("vagrant.actions.vm.forward_ports.non_nat", + message_attributes)) + next + end + # Add the options to the ports array to send to the driver later ports << options.merge(:name => name, :adapter => adapter) - - # TODO: Check for non-nat again... This was removed during the VBoxManage - # transition but should be brought back. - # @env[:ui].info(I18n.t("vagrant.actions.vm.forward_ports.non_nat", - # message_attributes)) end @env[:vm].driver.forward_ports(ports) diff --git a/lib/vagrant/driver/virtualbox.rb b/lib/vagrant/driver/virtualbox.rb index f4b8dd023..b399a1629 100644 --- a/lib/vagrant/driver/virtualbox.rb +++ b/lib/vagrant/driver/virtualbox.rb @@ -227,13 +227,6 @@ module Vagrant results end - # This reads the guest additions version for a VM. - def read_guest_additions_version - output = execute("guestproperty", "get", @uuid, "/VirtualBox/GuestAdd/Version") - return $1.to_s if output =~ /^Value: (.+?)$/ - return nil - end - # This reads the list of host only networks. def read_bridged_interfaces execute("list", "bridgedifs").split("\n\n").collect do |block| @@ -256,6 +249,13 @@ module Vagrant end end + # This reads the guest additions version for a VM. + def read_guest_additions_version + output = execute("guestproperty", "get", @uuid, "/VirtualBox/GuestAdd/Version") + return $1.to_s if output =~ /^Value: (.+?)$/ + return nil + end + # Reads and returns the available host only interfaces. def read_host_only_interfaces execute("list", "hostonlyifs").split("\n\n").collect do |block| @@ -286,6 +286,23 @@ module Vagrant nil end + # This reads the network interfaces and returns various information + # about them. + # + # @return [Hash] + def read_network_interfaces + nics = {} + execute("showvminfo", @uuid, "--machinereadable").split("\n").each do |line| + if line =~ /^nic(\d+)="(.+?)"$/ + nics[$1.to_i] = { + :type => $2.to_s + } + end + end + + nics + end + # This reads the state for the given UUID. The state of the VM # will be returned as a symbol. def read_state