Skip to content

Commit 914fa98

Browse files
committed
Clean up IsDualStack* docs and code
1 parent 59f23ee commit 914fa98

File tree

2 files changed

+32
-40
lines changed

2 files changed

+32
-40
lines changed

net/ipfamily.go

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -32,72 +32,64 @@ const (
3232
IPv6 IPFamily = "6"
3333
)
3434

35-
// IsDualStackIPs returns if a slice of ips is:
36-
// - all are valid ips
37-
// - at least one ip from each family (v4 or v6)
35+
// IsDualStackIPs returns true if:
36+
// - all elements of ips are valid
37+
// - at least one IP from each family (v4 and v6) is present
3838
func IsDualStackIPs(ips []net.IP) (bool, error) {
3939
v4Found := false
4040
v6Found := false
41-
for _, ip := range ips {
42-
if ip == nil {
43-
return false, fmt.Errorf("ip %v is invalid", ip)
44-
}
45-
46-
if v4Found && v6Found {
47-
continue
48-
}
49-
50-
if IsIPv6(ip) {
41+
for i, ip := range ips {
42+
switch IPFamilyOf(ip) {
43+
case IPv4:
44+
v4Found = true
45+
case IPv6:
5146
v6Found = true
52-
continue
47+
default:
48+
return false, fmt.Errorf("invalid IP[%d]: %v", i, ip)
5349
}
54-
55-
v4Found = true
5650
}
5751

5852
return (v4Found && v6Found), nil
5953
}
6054

61-
// IsDualStackIPStrings returns if
62-
// - all are valid ips
63-
// - at least one ip from each family (v4 or v6)
55+
// IsDualStackIPStrings returns true if:
56+
// - all elements of ips can be parsed as IPs
57+
// - at least one IP from each family (v4 and v6) is present
6458
func IsDualStackIPStrings(ips []string) (bool, error) {
6559
parsedIPs := make([]net.IP, 0, len(ips))
66-
for _, ip := range ips {
60+
for i, ip := range ips {
6761
parsedIP := ParseIPSloppy(ip)
62+
if parsedIP == nil {
63+
return false, fmt.Errorf("invalid IP[%d]: %v", i, ip)
64+
}
6865
parsedIPs = append(parsedIPs, parsedIP)
6966
}
7067
return IsDualStackIPs(parsedIPs)
7168
}
7269

73-
// IsDualStackCIDRs returns if
74-
// - all are valid cidrs
75-
// - at least one cidr from each family (v4 or v6)
70+
// IsDualStackCIDRs returns true if:
71+
// - all elements of cidrs are non-nil
72+
// - at least one CIDR from each family (v4 and v6) is present
7673
func IsDualStackCIDRs(cidrs []*net.IPNet) (bool, error) {
7774
v4Found := false
7875
v6Found := false
79-
for _, cidr := range cidrs {
80-
if cidr == nil {
81-
return false, fmt.Errorf("cidr %v is invalid", cidr)
82-
}
83-
84-
if v4Found && v6Found {
85-
continue
86-
}
87-
88-
if IsIPv6(cidr.IP) {
76+
for i, cidr := range cidrs {
77+
switch IPFamilyOfCIDR(cidr) {
78+
case IPv4:
79+
v4Found = true
80+
case IPv6:
8981
v6Found = true
90-
continue
82+
default:
83+
return false, fmt.Errorf("invalid CIDR[%d]: %v", i, cidr)
9184
}
92-
v4Found = true
9385
}
9486

95-
return v4Found && v6Found, nil
87+
return (v4Found && v6Found), nil
9688
}
9789

9890
// IsDualStackCIDRStrings returns if
99-
// - all are valid cidrs
100-
// - at least one cidr from each family (v4 or v6)
91+
// - all elements of cidrs can be parsed as CIDRs
92+
// - at least one CIDR from each family (v4 and v6) is present
10193
func IsDualStackCIDRStrings(cidrs []string) (bool, error) {
10294
parsedCIDRs, err := ParseCIDRs(cidrs)
10395
if err != nil {

net/net.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ import (
2929
// order is maintained
3030
func ParseCIDRs(cidrsString []string) ([]*net.IPNet, error) {
3131
cidrs := make([]*net.IPNet, 0, len(cidrsString))
32-
for _, cidrString := range cidrsString {
32+
for i, cidrString := range cidrsString {
3333
_, cidr, err := ParseCIDRSloppy(cidrString)
3434
if err != nil {
35-
return nil, fmt.Errorf("failed to parse cidr value:%q with error:%v", cidrString, err)
35+
return nil, fmt.Errorf("invalid CIDR[%d]: %v (%v)", i, cidr, err)
3636
}
3737
cidrs = append(cidrs, cidr)
3838
}

0 commit comments

Comments
 (0)