Cleanups of ipv6 changes.

I was late re-reviewing and some comments did not get resolved.
pull/6/head
Tim Hockin 2014-11-17 09:33:14 -08:00
parent 931cd3a2df
commit 9c218f0a19
3 changed files with 16 additions and 9 deletions

View File

@ -46,11 +46,12 @@ func (s *ipAddrSet) Init() {
s.ips = map[string]bool{}
}
// Adds to the ipAddrSet; returns true iff it was added (was not already in set)
// Gets the number of IPs in the set
func (s *ipAddrSet) Size() int {
return len(s.ips)
}
// Tests whether the set holds a given IP
func (s *ipAddrSet) Contains(ip net.IP) bool {
key := ip.String()
exists := s.ips[key]
@ -112,13 +113,11 @@ func newIPAllocator(subnet *net.IPNet) *ipAllocator {
}
ipa.used.Init()
zero := make(net.IP, len(subnet.IP), len(subnet.IP))
network := make(net.IP, len(subnet.IP), len(subnet.IP))
for i := 0; i < len(subnet.IP); i++ {
zero[i] = subnet.IP[i] & subnet.Mask[i]
network[i] = subnet.IP[i] & subnet.Mask[i]
}
ipa.used.Add(zero) // block the zero addr
ipa.used.Add(subnet.IP) // block the network addr
ipa.used.Add(network) // block the network addr
broadcast := make(net.IP, len(subnet.IP), len(subnet.IP))
for i := 0; i < len(subnet.IP); i++ {

View File

@ -54,6 +54,14 @@ func TestAllocate(t *testing.T) {
_, ipnet, _ := net.ParseCIDR("93.76.0.0/22")
ipa := newIPAllocator(ipnet)
if err := ipa.Allocate(net.ParseIP("93.76.0.0")); err == nil {
t.Errorf("expected failure")
}
if err := ipa.Allocate(net.ParseIP("93.76.3.255")); err == nil {
t.Errorf("expected failure")
}
if err := ipa.Allocate(net.ParseIP("93.76.0.1")); err != nil {
t.Errorf("expected success, got %s", err)
}

View File

@ -38,11 +38,11 @@ type Interface interface {
IsIpv6() bool
}
type Protocol bool
type Protocol byte
const (
ProtocolIpv4 Protocol = false
ProtocolIpv6 Protocol = true
ProtocolIpv4 Protocol = iota + 1
ProtocolIpv6
)
type Table string