@@ -82,10 +82,17 @@ type Range struct {
82
82
83
83
// NewAllocatorCIDRRange creates a Range over a net.IPNet, calling allocatorFactory to construct the backing store.
84
84
func NewAllocatorCIDRRange (cidr * net.IPNet , allocatorFactory allocator.AllocatorFactory ) (* Range , error ) {
85
- max := RangeSize (cidr )
86
- base := bigForIP (cidr .IP )
85
+ max := utilnet . RangeSize (cidr )
86
+ base := utilnet . BigForIP (cidr .IP )
87
87
rangeSpec := cidr .String ()
88
88
89
+ if utilnet .IsIPv6CIDR (cidr ) {
90
+ // Limit the max size, since the allocator keeps a bitmap of that size.
91
+ if max > 65536 {
92
+ max = 65536
93
+ }
94
+ }
95
+
89
96
r := Range {
90
97
net : cidr ,
91
98
base : base .Add (base , big .NewInt (1 )), // don't use the network base
@@ -171,7 +178,7 @@ func (r *Range) AllocateNext() (net.IP, error) {
171
178
if ! ok {
172
179
return nil , ErrFull
173
180
}
174
- return addIPOffset (r .base , offset ), nil
181
+ return utilnet . AddIPOffset (r .base , offset ), nil
175
182
}
176
183
177
184
// Release releases the IP back to the pool. Releasing an
@@ -247,40 +254,8 @@ func (r *Range) contains(ip net.IP) (bool, int) {
247
254
return true , offset
248
255
}
249
256
250
- // bigForIP creates a big.Int based on the provided net.IP
251
- func bigForIP (ip net.IP ) * big.Int {
252
- b := ip .To4 ()
253
- if b == nil {
254
- b = ip .To16 ()
255
- }
256
- return big .NewInt (0 ).SetBytes (b )
257
- }
258
-
259
- // addIPOffset adds the provided integer offset to a base big.Int representing a
260
- // net.IP
261
- func addIPOffset (base * big.Int , offset int ) net.IP {
262
- return net .IP (big .NewInt (0 ).Add (base , big .NewInt (int64 (offset ))).Bytes ())
263
- }
264
-
265
257
// calculateIPOffset calculates the integer offset of ip from base such that
266
258
// base + offset = ip. It requires ip >= base.
267
259
func calculateIPOffset (base * big.Int , ip net.IP ) int {
268
- return int (big .NewInt (0 ).Sub (bigForIP (ip ), base ).Int64 ())
269
- }
270
-
271
- // RangeSize returns the size of a range in valid addresses.
272
- func RangeSize (subnet * net.IPNet ) int64 {
273
- ones , bits := subnet .Mask .Size ()
274
- if bits == 32 && (bits - ones ) >= 31 || bits == 128 && (bits - ones ) >= 127 {
275
- return 0
276
- }
277
- // For IPv6, the max size will be limited to 65536
278
- // This is due to the allocator keeping track of all the
279
- // allocated IP's in a bitmap. This will keep the size of
280
- // the bitmap to 64k.
281
- if bits == 128 && (bits - ones ) >= 16 {
282
- return int64 (1 ) << uint (16 )
283
- } else {
284
- return int64 (1 ) << uint (bits - ones )
285
- }
260
+ return int (big .NewInt (0 ).Sub (utilnet .BigForIP (ip ), base ).Int64 ())
286
261
}
0 commit comments