浏览代码

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

Do not allocate networks first ip
unclejack 11 年之前
父节点
当前提交
5258f833bc
共有 2 个文件被更改,包括 27 次插入1 次删除
  1. 6 1
      networkdriver/ipallocator/allocator.go
  2. 21 0
      networkdriver/ipallocator/allocator_test.go

+ 6 - 1
networkdriver/ipallocator/allocator.go

@@ -99,12 +99,17 @@ func getNextIp(address *net.IPNet) (*net.IP, error) {
 		return ip, nil
 		return ip, nil
 	}
 	}
 
 
+	var (
+		firstNetIP = address.IP.To4().Mask(address.Mask)
+		firstAsInt = ipToInt(&firstNetIP) + 1
+	)
+
 	pos = int32(allocated.PullBack())
 	pos = int32(allocated.PullBack())
 	for i := int32(0); i < max; i++ {
 	for i := int32(0); i < max; i++ {
 		pos = pos%max + 1
 		pos = pos%max + 1
 		next := int32(base + pos)
 		next := int32(base + pos)
 
 
-		if next == ownIP {
+		if next == ownIP || next == firstAsInt {
 			continue
 			continue
 		}
 		}
 
 

+ 21 - 0
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) {
 func assertIPEquals(t *testing.T, ip1, ip2 *net.IP) {
 	if !ip1.Equal(*ip2) {
 	if !ip1.Equal(*ip2) {
 		t.Fatalf("Expected IP %s, got %s", ip1, ip2)
 		t.Fatalf("Expected IP %s, got %s", ip1, ip2)