Skip to content

Commit

Permalink
Use our forked ParseIP and ParseCIDR
Browse files Browse the repository at this point in the history
  • Loading branch information
thockin committed Jun 28, 2021
1 parent ca7b03c commit b65c373
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 40 deletions.
4 changes: 2 additions & 2 deletions net/ipnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func ParseIPNets(specs ...string) (IPNetSet, error) {
ipnetset := make(IPNetSet)
for _, spec := range specs {
spec = strings.TrimSpace(spec)
_, ipnet, err := net.ParseCIDR(spec)
_, ipnet, err := ParseCIDRSloppy(spec)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -128,7 +128,7 @@ type IPSet map[string]net.IP
func ParseIPSet(items ...string) (IPSet, error) {
ipset := make(IPSet)
for _, item := range items {
ip := net.ParseIP(strings.TrimSpace(item))
ip := ParseIPSloppy(strings.TrimSpace(item))
if ip == nil {
return nil, fmt.Errorf("error parsing IP %q", item)
}
Expand Down
30 changes: 15 additions & 15 deletions net/ipnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
)

func parseIPNet(s string) *net.IPNet {
_, net, err := net.ParseCIDR(s)
_, net, err := ParseCIDRSloppy(s)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -158,10 +158,10 @@ func TestIPSet(t *testing.T) {
s := IPSet{}
s2 := IPSet{}

a := net.ParseIP("1.0.0.0")
b := net.ParseIP("2.0.0.0")
c := net.ParseIP("3.0.0.0")
d := net.ParseIP("4.0.0.0")
a := ParseIPSloppy("1.0.0.0")
b := ParseIPSloppy("2.0.0.0")
c := ParseIPSloppy("3.0.0.0")
d := ParseIPSloppy("4.0.0.0")

s.Insert(a, b)
if len(s) != 2 {
Expand Down Expand Up @@ -199,9 +199,9 @@ func TestIPSet(t *testing.T) {

func TestIPSetDeleteMultiples(t *testing.T) {
s := IPSet{}
a := net.ParseIP("1.0.0.0")
b := net.ParseIP("2.0.0.0")
c := net.ParseIP("3.0.0.0")
a := ParseIPSloppy("1.0.0.0")
b := ParseIPSloppy("2.0.0.0")
c := ParseIPSloppy("3.0.0.0")

s.Insert(a, b, c)
if len(s) != 3 {
Expand Down Expand Up @@ -231,11 +231,11 @@ func TestParseIPSet(t *testing.T) {
if len(s) != 4 {
t.Errorf("Expected len=3: %d", len(s))
}
a := net.ParseIP("1.0.0.0")
b := net.ParseIP("2.0.0.0")
c := net.ParseIP("3.0.0.0")
d := net.ParseIP("::ffff:4.0.0.0")
e := net.ParseIP("4.0.0.0")
a := ParseIPSloppy("1.0.0.0")
b := ParseIPSloppy("2.0.0.0")
c := ParseIPSloppy("3.0.0.0")
d := ParseIPSloppy("::ffff:4.0.0.0")
e := ParseIPSloppy("4.0.0.0")

if !s.Has(a) || !s.Has(b) || !s.Has(c) || !s.Has(d) || !s.Has(e) {
t.Errorf("Unexpected contents: %#v", s)
Expand All @@ -256,13 +256,13 @@ func TestIPSetDifference(t *testing.T) {
if len(c) != 1 {
t.Errorf("Expected len=1: %d", len(c))
}
if !c.Has(net.ParseIP("3.0.0.0")) {
if !c.Has(ParseIPSloppy("3.0.0.0")) {
t.Errorf("Unexpected contents: %#v", c)
}
if len(d) != 2 {
t.Errorf("Expected len=2: %d", len(d))
}
if !d.Has(net.ParseIP("4.0.0.0")) || !d.Has(net.ParseIP("5.0.0.0")) {
if !d.Has(ParseIPSloppy("4.0.0.0")) || !d.Has(ParseIPSloppy("5.0.0.0")) {
t.Errorf("Unexpected contents: %#v", d)
}
}
Expand Down
12 changes: 6 additions & 6 deletions net/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
func ParseCIDRs(cidrsString []string) ([]*net.IPNet, error) {
cidrs := make([]*net.IPNet, 0, len(cidrsString))
for _, cidrString := range cidrsString {
_, cidr, err := net.ParseCIDR(cidrString)
_, cidr, err := ParseCIDRSloppy(cidrString)
if err != nil {
return nil, fmt.Errorf("failed to parse cidr value:%q with error:%v", cidrString, err)
}
Expand Down Expand Up @@ -71,7 +71,7 @@ func IsDualStackIPs(ips []net.IP) (bool, error) {
func IsDualStackIPStrings(ips []string) (bool, error) {
parsedIPs := make([]net.IP, 0, len(ips))
for _, ip := range ips {
parsedIP := net.ParseIP(ip)
parsedIP := ParseIPSloppy(ip)
parsedIPs = append(parsedIPs, parsedIP)
}
return IsDualStackIPs(parsedIPs)
Expand Down Expand Up @@ -120,14 +120,14 @@ func IsIPv6(netIP net.IP) bool {

// IsIPv6String returns if ip is IPv6.
func IsIPv6String(ip string) bool {
netIP := net.ParseIP(ip)
netIP := ParseIPSloppy(ip)
return IsIPv6(netIP)
}

// IsIPv6CIDRString returns if cidr is IPv6.
// This assumes cidr is a valid CIDR.
func IsIPv6CIDRString(cidr string) bool {
ip, _, _ := net.ParseCIDR(cidr)
ip, _, _ := ParseCIDRSloppy(cidr)
return IsIPv6(ip)
}

Expand All @@ -144,7 +144,7 @@ func IsIPv4(netIP net.IP) bool {

// IsIPv4String returns if ip is IPv4.
func IsIPv4String(ip string) bool {
netIP := net.ParseIP(ip)
netIP := ParseIPSloppy(ip)
return IsIPv4(netIP)
}

Expand All @@ -157,7 +157,7 @@ func IsIPv4CIDR(cidr *net.IPNet) bool {
// IsIPv4CIDRString returns if cidr is IPv4.
// This assumes cidr is a valid CIDR.
func IsIPv4CIDRString(cidr string) bool {
ip, _, _ := net.ParseCIDR(cidr)
ip, _, _ := ParseCIDRSloppy(cidr)
return IsIPv4(ip)
}

Expand Down
32 changes: 16 additions & 16 deletions net/net_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func TestDualStackIPs(t *testing.T) {
for _, tc := range testCases {
ips := make([]net.IP, 0, len(tc.ips))
for _, ip := range tc.ips {
parsedIP := net.ParseIP(ip)
parsedIP := ParseIPSloppy(ip)
ips = append(ips, parsedIP)
}
dualStack, err := IsDualStackIPs(ips)
Expand Down Expand Up @@ -250,7 +250,7 @@ func TestDualStackCIDRs(t *testing.T) {
for _, tc := range testCases {
cidrs := make([]*net.IPNet, 0, len(tc.cidrs))
for _, cidr := range tc.cidrs {
_, parsedCIDR, _ := net.ParseCIDR(cidr)
_, parsedCIDR, _ := ParseCIDRSloppy(cidr)
cidrs = append(cidrs, parsedCIDR)
}

Expand Down Expand Up @@ -325,15 +325,15 @@ func TestIsIPv6(t *testing.T) {
expectIPv6: false,
},
{
ip: net.ParseIP("127.0.0.1"),
ip: ParseIPSloppy("127.0.0.1"),
expectIPv6: false,
},
{
ip: net.ParseIP("10.20.40.40"),
ip: ParseIPSloppy("10.20.40.40"),
expectIPv6: false,
},
{
ip: net.ParseIP("172.17.3.0"),
ip: ParseIPSloppy("172.17.3.0"),
expectIPv6: false,
},
{
Expand All @@ -349,11 +349,11 @@ func TestIsIPv6(t *testing.T) {
expectIPv6: true,
},
{
ip: net.ParseIP("fd00::600d:f00d"),
ip: ParseIPSloppy("fd00::600d:f00d"),
expectIPv6: true,
},
{
ip: net.ParseIP("2001:db8::5"),
ip: ParseIPSloppy("2001:db8::5"),
expectIPv6: true,
},
}
Expand Down Expand Up @@ -440,7 +440,7 @@ func TestIsIPv6CIDR(t *testing.T) {
}

for _, tc := range testCases {
_, cidr, _ := net.ParseCIDR(tc.cidr)
_, cidr, _ := ParseCIDRSloppy(tc.cidr)
res := IsIPv6CIDR(cidr)
if res != tc.expectResult {
t.Errorf("%v: want IsIPv6CIDR=%v, got %v", tc.desc, tc.expectResult, res)
Expand Down Expand Up @@ -504,15 +504,15 @@ func TestIsIPv4(t *testing.T) {
expectIPv4: true,
},
{
ip: net.ParseIP("127.0.0.1"),
ip: ParseIPSloppy("127.0.0.1"),
expectIPv4: true,
},
{
ip: net.ParseIP("10.20.40.40"),
ip: ParseIPSloppy("10.20.40.40"),
expectIPv4: true,
},
{
ip: net.ParseIP("172.17.3.0"),
ip: ParseIPSloppy("172.17.3.0"),
expectIPv4: true,
},
{
Expand All @@ -528,11 +528,11 @@ func TestIsIPv4(t *testing.T) {
expectIPv4: false,
},
{
ip: net.ParseIP("fd00::600d:f00d"),
ip: ParseIPSloppy("fd00::600d:f00d"),
expectIPv4: false,
},
{
ip: net.ParseIP("2001:db8::5"),
ip: ParseIPSloppy("2001:db8::5"),
expectIPv4: false,
},
}
Expand Down Expand Up @@ -619,7 +619,7 @@ func TestIsIPv4CIDR(t *testing.T) {
}

for _, tc := range testCases {
_, cidr, _ := net.ParseCIDR(tc.cidr)
_, cidr, _ := ParseCIDRSloppy(tc.cidr)
res := IsIPv4CIDR(cidr)
if res != tc.expectResult {
t.Errorf("%v: want IsIPv4CIDR=%v, got %v", tc.desc, tc.expectResult, res)
Expand Down Expand Up @@ -726,7 +726,7 @@ func TestRangeSize(t *testing.T) {
}

for _, tc := range testCases {
_, cidr, err := net.ParseCIDR(tc.cidr)
_, cidr, err := ParseCIDRSloppy(tc.cidr)
if err != nil {
t.Errorf("failed to parse cidr for test %s, unexpected error: '%s'", tc.name, err)
}
Expand Down Expand Up @@ -792,7 +792,7 @@ func TestGetIndexedIP(t *testing.T) {
}

for _, tc := range testCases {
_, subnet, err := net.ParseCIDR(tc.cidr)
_, subnet, err := ParseCIDRSloppy(tc.cidr)
if err != nil {
t.Errorf("failed to parse cidr %s, unexpected error: '%s'", tc.cidr, err)
}
Expand Down
33 changes: 33 additions & 0 deletions net/parse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package net

import (
forkednet "k8s.io/utils/internal/third_party/forked/golang/net"
)

// ParseIPSloppy is identical to Go's standard net.ParseIP, except that it allows
// leading '0' characters on numbers. Go used to allow this and then changed
// the behavior in 1.17. We're choosing to keep it for compat with potential
// stored values.
var ParseIPSloppy = forkednet.ParseIP

// ParseCIDRSloppy is identical to Go's standard net.ParseCIDR, except that it allows
// leading '0' characters on numbers. Go used to allow this and then changed
// the behavior in 1.17. We're choosing to keep it for compat with potential
// stored values.
var ParseCIDRSloppy = forkednet.ParseCIDR
2 changes: 1 addition & 1 deletion net/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func NewLocalPort(desc, ip string, ipFamily IPFamily, port int, protocol Protoco
return nil, fmt.Errorf("Invalid IP family %s", ipFamily)
}
if ip != "" {
parsedIP := net.ParseIP(ip)
parsedIP := ParseIPSloppy(ip)
if parsedIP == nil {
return nil, fmt.Errorf("invalid ip address %s", ip)
}
Expand Down

0 comments on commit b65c373

Please sign in to comment.