From d86c1b064dad349737b4a423fe8d0c42db4ca45e Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Thu, 30 Jan 2014 23:05:16 -0800 Subject: [PATCH] Not not allocate networks first ip Docker-DCO-1.1-Signed-off-by: Michael Crosby (github: crosbymichael) --- networkdriver/ipallocator/allocator.go | 7 ++++++- networkdriver/ipallocator/allocator_test.go | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/networkdriver/ipallocator/allocator.go b/networkdriver/ipallocator/allocator.go index 33401d5caf..1c5a7b4cc2 100644 --- a/networkdriver/ipallocator/allocator.go +++ b/networkdriver/ipallocator/allocator.go @@ -99,12 +99,17 @@ func getNextIp(address *net.IPNet) (*net.IP, error) { return ip, nil } + var ( + firstNetIP = address.IP.To4().Mask(address.Mask) + firstAsInt = ipToInt(&firstNetIP) + 1 + ) + pos = int32(allocated.PullBack()) for i := int32(0); i < max; i++ { pos = pos%max + 1 next := int32(base + pos) - if next == ownIP { + if next == ownIP || next == firstAsInt { continue } diff --git a/networkdriver/ipallocator/allocator_test.go b/networkdriver/ipallocator/allocator_test.go index 871f143521..5e9fcfc983 100644 --- a/networkdriver/ipallocator/allocator_test.go +++ b/networkdriver/ipallocator/allocator_test.go @@ -213,6 +213,27 @@ func TestIPAllocator(t *testing.T) { } } +func TestAllocateFirstIP(t *testing.T) { + defer reset() + network := &net.IPNet{ + IP: []byte{192, 168, 0, 0}, + Mask: []byte{255, 255, 255, 0}, + } + + firstIP := network.IP.To4().Mask(network.Mask) + first := ipToInt(&firstIP) + 1 + + ip, err := RequestIP(network, nil) + if err != nil { + t.Fatal(err) + } + allocated := ipToInt(ip) + + if allocated == first { + t.Fatalf("allocated ip should not equal first ip: %d == %d", first, allocated) + } +} + func assertIPEquals(t *testing.T, ip1, ip2 *net.IP) { if !ip1.Equal(*ip2) { t.Fatalf("Expected IP %s, got %s", ip1, ip2)