Skip to content

Commit

Permalink
update multiaddr
Browse files Browse the repository at this point in the history
  • Loading branch information
sukunrt committed Jul 10, 2023
1 parent c1cd180 commit d4284b9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 29 deletions.
17 changes: 1 addition & 16 deletions p2p/protocol/identify/obsaddr.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package identify
import (
"context"
"fmt"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -377,7 +376,7 @@ func shouldRecordObservation(host addrsProvider, network listenAddrsProvider, co
}

// Provided by NAT64 peers, these addresses are specific to the peer and not publicly routable
if isNAT64IPv4ConvertedIPv6Addr(observed) {
if manet.IsNAT64IPv4ConvertedIPv6Addr(observed) {
return false
}

Expand Down Expand Up @@ -438,20 +437,6 @@ func shouldRecordObservation(host addrsProvider, network listenAddrsProvider, co
return true
}

// isNAT64IPv4ConvertedIPv6Addr returns whether addr is an IPv6 address that begins with
// the well known prefix "64:ff9b" used for NAT64 Translation
// see RFC 6052
func isNAT64IPv4ConvertedIPv6Addr(addr ma.Multiaddr) bool {
ip, err := addr.ValueForProtocol(ma.P_IP6)
if err != nil {
return false
}
if strings.HasPrefix(ip, "64:ff9b::") {
return true
}
return false
}

func (oas *ObservedAddrManager) maybeRecordObservation(conn network.Conn, observed ma.Multiaddr) {
shouldRecord := shouldRecordObservation(oas.host, oas.host.Network(), conn, observed)
if shouldRecord {
Expand Down
37 changes: 24 additions & 13 deletions p2p/protocol/identify/obsaddr_glass_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,36 +105,47 @@ func TestShouldRecordObservationWithWebTransport(t *testing.T) {
require.True(t, shouldRecordObservation(h, h, c, observedAddr))
}

func TestIsWellKnownPrefixIPv4ConvertedIPv6Address(t *testing.T) {
func TestShouldRecordObservationWithNAT64Addr(t *testing.T) {
listenAddr1 := ma.StringCast("/ip4/0.0.0.0/tcp/1234")
ifaceAddr1 := ma.StringCast("/ip4/10.0.0.2/tcp/4321")
listenAddr2 := ma.StringCast("/ip6/::/tcp/1234")
ifaceAddr2 := ma.StringCast("/ip6/1::1/tcp/4321")

h := &mockHost{
listenAddrs: []ma.Multiaddr{listenAddr1, listenAddr2},
ifaceListenAddrs: []ma.Multiaddr{ifaceAddr1, ifaceAddr2},
addrs: []ma.Multiaddr{listenAddr1, listenAddr2},
}
c := &mockConn{
local: listenAddr1,
remote: ma.StringCast("/ip4/1.2.3.6/tcp/4321"),
}

cases := []struct {
addr ma.Multiaddr
want bool
failureReason string
}{
{
addr: ma.StringCast("/ip4/1.2.3.4/tcp/1234"),
want: false,
failureReason: "ip4 addresses should return false",
want: true,
failureReason: "IPv4 should be observed",
},
{
addr: ma.StringCast("/ip6/1::4/tcp/1234"),
want: false,
failureReason: "ip6 addresses doesn't have wellknown prefix",
},
{
addr: ma.StringCast("/ip6/::1/tcp/1234"),
want: false,
failureReason: "localhost addresses should return false",
want: true,
failureReason: "public IPv6 address should be observed",
},
{
addr: ma.StringCast("/ip6/64:ff9b::192.0.1.2/tcp/1234"),
want: true,
failureReason: "ip6 address begins with well-known prefix",
want: false,
failureReason: "NAT64 IPv6 address shouldn't be observed",
},
}
for i, tc := range cases {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
if isNAT64IPv4ConvertedIPv6Addr(tc.addr) != tc.want {

if shouldRecordObservation(h, h, c, tc.addr) != tc.want {
t.Fatalf("%s %s", tc.addr, tc.failureReason)
}
})
Expand Down

0 comments on commit d4284b9

Please sign in to comment.