From 24d744dbf7d618cfb1703886801f2effefac64f9 Mon Sep 17 00:00:00 2001 From: junxu Date: Thu, 7 May 2015 05:22:52 +0000 Subject: [PATCH] Simplify the code in the RegisterSubnet method of ipallocator. --- ipallocator/allocator.go | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/ipallocator/allocator.go b/ipallocator/allocator.go index 36a5871..1560099 100644 --- a/ipallocator/allocator.go +++ b/ipallocator/allocator.go @@ -70,18 +70,14 @@ func (a *IPAllocator) RegisterSubnet(network *net.IPNet, subnet *net.IPNet) erro if _, ok := a.allocatedIPs[key]; ok { return ErrNetworkAlreadyRegistered } - n := newAllocatedMap(network) - beginIP, endIP := netutils.NetworkRange(subnet) - begin := big.NewInt(0).Add(ipToBigInt(beginIP), big.NewInt(1)) - end := big.NewInt(0).Sub(ipToBigInt(endIP), big.NewInt(1)) // Check that subnet is within network - if !(begin.Cmp(n.begin) >= 0 && end.Cmp(n.end) <= 0 && begin.Cmp(end) == -1) { + beginIP, endIP := netutils.NetworkRange(subnet) + if !(network.Contains(beginIP) && network.Contains(endIP)) { return ErrBadSubnet } - n.begin.Set(begin) - n.end.Set(end) - n.last.Sub(begin, big.NewInt(1)) + + n := newAllocatedMap(subnet) a.allocatedIPs[key] = n return nil }