Browse code

Not not allocate networks first ip

Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)

Michael Crosby authored on 2014/01/31 16:05:16
Showing 2 changed files
... ...
@@ -99,12 +99,17 @@ func getNextIp(address *net.IPNet) (*net.IP, error) {
99 99
 		return ip, nil
100 100
 	}
101 101
 
102
+	var (
103
+		firstNetIP = address.IP.To4().Mask(address.Mask)
104
+		firstAsInt = ipToInt(&firstNetIP) + 1
105
+	)
106
+
102 107
 	pos = int32(allocated.PullBack())
103 108
 	for i := int32(0); i < max; i++ {
104 109
 		pos = pos%max + 1
105 110
 		next := int32(base + pos)
106 111
 
107
-		if next == ownIP {
112
+		if next == ownIP || next == firstAsInt {
108 113
 			continue
109 114
 		}
110 115
 
... ...
@@ -213,6 +213,27 @@ func TestIPAllocator(t *testing.T) {
213 213
 	}
214 214
 }
215 215
 
216
+func TestAllocateFirstIP(t *testing.T) {
217
+	defer reset()
218
+	network := &net.IPNet{
219
+		IP:   []byte{192, 168, 0, 0},
220
+		Mask: []byte{255, 255, 255, 0},
221
+	}
222
+
223
+	firstIP := network.IP.To4().Mask(network.Mask)
224
+	first := ipToInt(&firstIP) + 1
225
+
226
+	ip, err := RequestIP(network, nil)
227
+	if err != nil {
228
+		t.Fatal(err)
229
+	}
230
+	allocated := ipToInt(ip)
231
+
232
+	if allocated == first {
233
+		t.Fatalf("allocated ip should not equal first ip: %d == %d", first, allocated)
234
+	}
235
+}
236
+
216 237
 func assertIPEquals(t *testing.T, ip1, ip2 *net.IP) {
217 238
 	if !ip1.Equal(*ip2) {
218 239
 		t.Fatalf("Expected IP %s, got %s", ip1, ip2)