Skip to content

Commit

Permalink
changed signature of DHT filter options to be more general
Browse files Browse the repository at this point in the history
  • Loading branch information
aschmahmann committed Apr 30, 2021
1 parent 4532fb0 commit 7e79983
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
20 changes: 14 additions & 6 deletions dht_filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package dht
import (
"bytes"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/network"
dhtcfg "github.com/libp2p/go-libp2p-kad-dht/internal/config"
"net"
"sync"
"time"

"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"

"github.com/google/gopacket/routing"
Expand Down Expand Up @@ -83,13 +83,14 @@ var _ QueryFilterFunc = PublicQueryFilter

// PublicRoutingTableFilter allows a peer to be added to the routing table if the connections to that peer indicate
// that it is on a public network
func PublicRoutingTableFilter(dht interface{}, conns []network.Conn) bool {
func PublicRoutingTableFilter(dht interface{}, p peer.ID) bool {
d := dht.(hasHost)

conns := d.Host().Network().ConnsToPeer(p)
if len(conns) == 0 {
return false
}

d := dht.(hasHost)

// Do we have a public address for this peer?
id := conns[0].RemotePeer()
known := d.Host().Peerstore().PeerInfo(id)
Expand Down Expand Up @@ -145,12 +146,19 @@ func getCachedRouter() routing.Router {

// PrivateRoutingTableFilter allows a peer to be added to the routing table if the connections to that peer indicate
// that it is on a private network
func PrivateRoutingTableFilter(dht interface{}, conns []network.Conn) bool {
func PrivateRoutingTableFilter(dht interface{}, p peer.ID) bool {
d := dht.(hasHost)
conns := d.Host().Network().ConnsToPeer(p)
return privRTFilter(d, conns)
}

func privRTFilter(dht interface{}, conns []network.Conn) bool {
d := dht.(hasHost)
h := d.Host()

router := getCachedRouter()
myAdvertisedIPs := make([]net.IP, 0)
for _, a := range d.Host().Addrs() {
for _, a := range h.Addrs() {
if isPublicAddr(a) && !isRelayAddr(a) {
ip, err := manet.ToIP(a)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion dht_filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestFilterCaching(t *testing.T) {
d := setupDHT(ctx, t, true)

remote, _ := manet.FromIP(net.IPv4(8, 8, 8, 8))
if PrivateRoutingTableFilter(d, []network.Conn{&mockConn{
if privRTFilter(d, []network.Conn{&mockConn{
local: d.Host().Peerstore().PeerInfo(d.Host().ID()),
remote: peer.AddrInfo{ID: "", Addrs: []ma.Multiaddr{remote}},
}}) {
Expand Down
14 changes: 11 additions & 3 deletions dual/dual_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package dual

import (
"context"
"github.com/libp2p/go-libp2p-core/host"
"testing"
"time"

"github.com/ipfs/go-cid"
u "github.com/ipfs/go-ipfs-util"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
peerstore "github.com/libp2p/go-libp2p-core/peerstore"
dht "github.com/libp2p/go-libp2p-kad-dht"
Expand All @@ -34,9 +34,17 @@ type customRtHelper struct {
allow peer.ID
}

func MkFilterForPeer() (func(_ interface{}, conns []network.Conn) bool, *customRtHelper) {
func MkFilterForPeer() (func(_ interface{}, p peer.ID) bool, *customRtHelper) {
helper := customRtHelper{}
f := func(_ interface{}, conns []network.Conn) bool {

type hasHost interface {
Host() host.Host
}

f := func(dht interface{}, p peer.ID) bool {
d := dht.(hasHost)
conns := d.Host().Network().ConnsToPeer(p)

for _, c := range conns {
if c.RemotePeer() == helper.allow {
return true
Expand Down
7 changes: 3 additions & 4 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
dssync "github.com/ipfs/go-datastore/sync"
"github.com/ipfs/go-ipns"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/protocol"
"github.com/libp2p/go-libp2p-kad-dht/providers"
Expand All @@ -29,7 +28,7 @@ type QueryFilterFunc func(dht interface{}, ai peer.AddrInfo) bool

// RouteTableFilterFunc is a filter applied when considering connections to keep in
// the local route table.
type RouteTableFilterFunc func(dht interface{}, conns []network.Conn) bool
type RouteTableFilterFunc func(dht interface{}, p peer.ID) bool

// Config is a structure containing all the options that can be used when constructing a DHT.
type Config struct {
Expand Down Expand Up @@ -65,8 +64,8 @@ type Config struct {
TestAddressUpdateProcessing bool
}

func EmptyQueryFilter(_ interface{}, ai peer.AddrInfo) bool { return true }
func EmptyRTFilter(_ interface{}, conns []network.Conn) bool { return true }
func EmptyQueryFilter(_ interface{}, ai peer.AddrInfo) bool { return true }
func EmptyRTFilter(_ interface{}, p peer.ID) bool { return true }

// Apply applies the given options to this Option
func (c *Config) Apply(opts ...Option) error {
Expand Down
2 changes: 1 addition & 1 deletion subscriber_notifee.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (dht *IpfsDHT) validRTPeer(p peer.ID) (bool, error) {
return false, err
}

return dht.routingTablePeerFilter == nil || dht.routingTablePeerFilter(dht, dht.Host().Network().ConnsToPeer(p)), nil
return dht.routingTablePeerFilter == nil || dht.routingTablePeerFilter(dht, p), nil
}

type disconnector interface {
Expand Down

0 comments on commit 7e79983

Please sign in to comment.