Skip to content

Commit bf52ece

Browse files
committed
FIX docker-archive-public#2231 - Detect duplicate interfaces
Signed-off-by: David Gageot <david@gageot.net>
1 parent 35b2b04 commit bf52ece

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

drivers/virtualbox/network.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ const (
1515
)
1616

1717
var (
18-
reHostonlyInterfaceCreated = regexp.MustCompile(`Interface '(.+)' was successfully created`)
18+
reHostonlyInterfaceCreated = regexp.MustCompile(`Interface '(.+)' was successfully created`)
19+
errDuplicateHostOnlyInterfaceNetworks = errors.New("VirtualBox is configured with multiple host-only interfaces with the same IP. Please remove all of them but one.")
1920
)
2021

2122
// Host-only network.
@@ -152,6 +153,10 @@ func getOrCreateHostOnlyNetwork(hostIP net.IP, netmask net.IPMask, dhcpIP net.IP
152153
return nil, err
153154
}
154155

156+
if len(nets) != countUniqueIps(nets) {
157+
return nil, errDuplicateHostOnlyInterfaceNetworks
158+
}
159+
155160
hostOnlyNet := getHostOnlyNetwork(nets, hostIP, netmask)
156161
if hostOnlyNet != nil {
157162
return hostOnlyNet, nil
@@ -182,6 +187,16 @@ func getOrCreateHostOnlyNetwork(hostIP net.IP, netmask net.IPMask, dhcpIP net.IP
182187
return hostOnlyNet, nil
183188
}
184189

190+
func countUniqueIps(nets map[string]*hostOnlyNetwork) int {
191+
ips := map[string]bool{}
192+
193+
for _, n := range nets {
194+
ips[n.IPv4.IP.String()] = true
195+
}
196+
197+
return len(ips)
198+
}
199+
185200
// DHCP server info.
186201
type dhcpServer struct {
187202
NetworkName string

drivers/virtualbox/network_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,15 @@ func TestGetHostOnlyNetwork(t *testing.T) {
211211
assert.Equal(t, "HostInterfaceNetworking-vboxnet0", net.NetworkName)
212212
assert.NoError(t, err)
213213
}
214+
215+
func TestFailWithDuplicateHostOnlyNetworks(t *testing.T) {
216+
vbox := &VBoxManagerMock{
217+
args: "list hostonlyifs",
218+
stdOut: stdOutTwoHostOnlyNetwork,
219+
}
220+
221+
net, err := getOrCreateHostOnlyNetwork(net.ParseIP("192.168.99.1"), parseIPv4Mask("255.255.255.0"), nil, nil, nil, vbox)
222+
223+
assert.Nil(t, net)
224+
assert.Equal(t, errDuplicateHostOnlyInterfaceNetworks, err)
225+
}

0 commit comments

Comments
 (0)