mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 18:32:23 +00:00
p2p/netutil: fixes for 4-in-6
This commit is contained in:
parent
ae3966eb01
commit
6ffd98d337
2 changed files with 27 additions and 7 deletions
|
|
@ -145,6 +145,9 @@ func IsLAN(ip net.IP) bool {
|
|||
|
||||
// IsLAN reports whether an IP is a local network address.
|
||||
func AddrIsLAN(ip netip.Addr) bool {
|
||||
if ip.Is4In6() {
|
||||
ip = netip.AddrFrom4(ip.As4())
|
||||
}
|
||||
if ip.IsLoopback() {
|
||||
return true
|
||||
}
|
||||
|
|
@ -160,10 +163,13 @@ func IsSpecialNetwork(ip net.IP) bool {
|
|||
// AddrIsSpecialNetwork reports whether an IP is located in a special-use network range
|
||||
// This includes broadcast, multicast and documentation addresses.
|
||||
func AddrIsSpecialNetwork(ip netip.Addr) bool {
|
||||
if ip.Is4In6() {
|
||||
ip = netip.AddrFrom4(ip.As4())
|
||||
}
|
||||
if ip.IsMulticast() {
|
||||
return true
|
||||
}
|
||||
if ip.Is4() || ip.Is4In6() {
|
||||
if ip.Is4() {
|
||||
return special4.ContainsAddr(ip)
|
||||
}
|
||||
return special6.ContainsAddr(ip)
|
||||
|
|
|
|||
|
|
@ -74,11 +74,11 @@ func TestParseNetlist(t *testing.T) {
|
|||
|
||||
func TestNilNetListContains(t *testing.T) {
|
||||
var list *Netlist
|
||||
checkContains(t, list.Contains, nil, []string{"1.2.3.4"})
|
||||
checkContains(t, list.Contains, list.ContainsAddr, nil, []string{"1.2.3.4"})
|
||||
}
|
||||
|
||||
func TestIsLAN(t *testing.T) {
|
||||
checkContains(t, IsLAN,
|
||||
checkContains(t, IsLAN, AddrIsLAN,
|
||||
[]string{ // included
|
||||
"127.0.0.1",
|
||||
"10.0.1.1",
|
||||
|
|
@ -88,18 +88,23 @@ func TestIsLAN(t *testing.T) {
|
|||
"fe80::f4a1:8eff:fec5:9d9d",
|
||||
"febf::ab32:2233",
|
||||
"fc00::4",
|
||||
// 4-in-6
|
||||
"::ffff:127.0.0.1",
|
||||
"::ffff:10.10.0.2",
|
||||
},
|
||||
[]string{ // excluded
|
||||
"192.0.2.1",
|
||||
"1.0.0.0",
|
||||
"172.32.0.1",
|
||||
"fec0::2233",
|
||||
// 4-in-6
|
||||
"::ffff:88.99.100.2",
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func TestIsSpecialNetwork(t *testing.T) {
|
||||
checkContains(t, IsSpecialNetwork,
|
||||
checkContains(t, IsSpecialNetwork, AddrIsSpecialNetwork,
|
||||
[]string{ // included
|
||||
"0.0.0.0",
|
||||
"0.2.0.8",
|
||||
|
|
@ -109,6 +114,9 @@ func TestIsSpecialNetwork(t *testing.T) {
|
|||
"255.255.255.255",
|
||||
"224.0.0.22", // IPv4 multicast
|
||||
"ff05::1:3", // IPv6 multicast
|
||||
// 4-in-6
|
||||
"::ffff:255.255.255.255",
|
||||
"::ffff:192.0.2.1",
|
||||
},
|
||||
[]string{ // excluded
|
||||
"192.0.3.1",
|
||||
|
|
@ -119,15 +127,21 @@ func TestIsSpecialNetwork(t *testing.T) {
|
|||
)
|
||||
}
|
||||
|
||||
func checkContains(t *testing.T, fn func(net.IP) bool, inc, exc []string) {
|
||||
func checkContains(t *testing.T, fn func(net.IP) bool, fn2 func(netip.Addr) bool, inc, exc []string) {
|
||||
for _, s := range inc {
|
||||
if !fn(parseIP(s)) {
|
||||
t.Error("returned false for included address", s)
|
||||
t.Error("returned false for included net.IP", s)
|
||||
}
|
||||
if !fn2(netip.MustParseAddr(s)) {
|
||||
t.Error("returned false for included netip.Addr", s)
|
||||
}
|
||||
}
|
||||
for _, s := range exc {
|
||||
if fn(parseIP(s)) {
|
||||
t.Error("returned true for excluded address", s)
|
||||
t.Error("returned true for excluded net.IP", s)
|
||||
}
|
||||
if fn2(netip.MustParseAddr(s)) {
|
||||
t.Error("returned true for excluded netip.Addr", s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue