Skip to content

Commit

Permalink
netutil: imp code
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Mar 17, 2022
1 parent ed09315 commit d5d770c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
1 change: 1 addition & 0 deletions netutil/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ func SingleIPSubnet(ip net.IP) (n *net.IPNet) {
//
// Any error returned will have the underlying type of *AddrError.
func ValidateIP(ip net.IP) (err error) {
// TODO(a.garipov): Get rid of unnecessary allocations in case of valid IP.
defer makeAddrError(&err, ip.String(), AddrKindIP)

switch l := len(ip); l {
Expand Down
16 changes: 9 additions & 7 deletions netutil/subnetset.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,15 @@ func NewEmptySubnetSet() (ss SubnetSet) { return SubnetSetFunc(func(net.IP) bool
func NewAnySubnetSet() (ss SubnetSet) {
// Use a couple of *net.IPNet since it seems being faster than validating
// with ValidateIP, see BenchmarkNewAnySubnetSet.
return NewSubnetSet(&net.IPNet{
IP: make(net.IP, net.IPv4len),
Mask: make(net.IPMask, net.IPv4len),
}, &net.IPNet{
IP: make(net.IP, net.IPv6len),
Mask: make(net.IPMask, net.IPv6len),
})
return &sliceSubnetSet{
nets: []*net.IPNet{{
IP: make(net.IP, net.IPv4len),
Mask: make(net.IPMask, net.IPv4len),
}, {
IP: make(net.IP, net.IPv6len),
Mask: make(net.IPMask, net.IPv6len),
}},
}
}

// IsLocallyServed checks if ip belongs to any network defined by RFC 6303:
Expand Down
8 changes: 3 additions & 5 deletions netutil/subnetset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,13 +323,11 @@ func BenchmarkSubnetSet_optimized(b *testing.B) {
})
}

func isValidIP(ip net.IP) (ok bool) {
return netutil.ValidateIP(ip) == nil
}

func BenchmarkNewAnySubnetSet(b *testing.B) {
ipnetBased := netutil.NewAnySubnetSet()
funcBased := netutil.SubnetSetFunc(isValidIP)
funcBased := netutil.SubnetSetFunc(func(ip net.IP) (ok bool) {
return netutil.ValidateIP(ip) == nil
})

// Create a bunch of random IP addresses.
ips := make([]net.IP, 128)
Expand Down

0 comments on commit d5d770c

Please sign in to comment.