1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merge pull request #3866 from crosbymichael/dont-allocate-1

Do not allocate networks first ip
This commit is contained in:
unclejack 2014-01-31 09:47:28 -08:00
commit 5258f833bc
2 changed files with 27 additions and 1 deletions

View file

@ -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
}

View file

@ -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)