Skip to content

Commit

Permalink
Merge pull request #131 from squat/172-16-slash-12
Browse files Browse the repository at this point in the history
pkg/mesh: correctly idenitfy 172.16/12 IPs
  • Loading branch information
squat authored Mar 5, 2021
2 parents 4d1756c + 7cc707f commit 90a2540
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
6 changes: 3 additions & 3 deletions pkg/mesh/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ func isLocal(ip net.IP) bool {
func isPublic(ip net.IP) bool {
// Check RFC 1918 addresses.
if ip4 := ip.To4(); ip4 != nil {
switch true {
switch {
// Check for 10.0.0.0/8.
case ip4[0] == 10:
return false
// Check for 172.16.0.0/12.
case ip4[0] == 172 && ip4[1]&0xf0 == 0x01:
case ip4[0] == 172 && ip4[1]&0xf0 != 0:
return false
// Check for 192.168.0.0/16.
case ip4[0] == 192 && ip4[1] == 168:
Expand All @@ -58,7 +58,7 @@ func isPublic(ip net.IP) bool {
}
// Check RFC 4193 addresses.
if len(ip) == net.IPv6len {
switch true {
switch {
// Check for fd00::/8.
case ip[0] == 0xfd && ip[1] == 0x00:
return false
Expand Down
38 changes: 38 additions & 0 deletions pkg/mesh/ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,41 @@ func TestSortIPs(t *testing.T) {
}
}
}

func TestIsPublic(t *testing.T) {
for _, tc := range []struct {
name string
ip net.IP
out bool
}{
{
name: "10/8",
ip: net.ParseIP("10.0.0.1"),
out: false,
},
{
name: "172.16/12",
ip: net.ParseIP("172.16.0.0"),
out: false,
},
{
name: "172.16/12 random",
ip: net.ParseIP("172.24.135.46"),
out: false,
},
{
name: "below 172.16/12",
ip: net.ParseIP("172.15.255.255"),
out: true,
},
{
name: "192.168/16",
ip: net.ParseIP("192.168.0.0"),
out: false,
},
} {
if isPublic(tc.ip) != tc.out {
t.Errorf("test case %q: expected %t, got %t", tc.name, tc.out, !tc.out)
}
}
}

0 comments on commit 90a2540

Please sign in to comment.