address outstanding discussion

This commit is contained in:
Will Scott
2020-03-19 15:22:13 -07:00
parent 922495b6fc
commit c032ad8c1b
4 changed files with 33 additions and 18 deletions

14
dht.go
View File

@ -205,7 +205,10 @@ func NewDHTClient(ctx context.Context, h host.Host, dstore ds.Batching) *IpfsDHT
}
func makeDHT(ctx context.Context, h host.Host, cfg config) (*IpfsDHT, error) {
rt, err := makeRoutingTable(h, cfg)
dht := new(IpfsDHT)
dht.host = h
rt, err := makeRoutingTable(dht, cfg)
if err != nil {
return nil, fmt.Errorf("failed to construct routing table,err=%s", err)
}
@ -223,7 +226,7 @@ func makeDHT(ctx context.Context, h host.Host, cfg config) (*IpfsDHT, error) {
}
}
dht := &IpfsDHT{
*dht = IpfsDHT{
datastore: cfg.datastore,
self: h.ID(),
peerstore: h.Peerstore(),
@ -256,7 +259,8 @@ func makeDHT(ctx context.Context, h host.Host, cfg config) (*IpfsDHT, error) {
return dht, nil
}
func makeRoutingTable(h host.Host, cfg config) (*kb.RoutingTable, error) {
func makeRoutingTable(dht *IpfsDHT, cfg config) (*kb.RoutingTable, error) {
h := dht.Host()
self := kb.ConvertPeerID(h.ID())
// construct the routing table with a peer validation function
pvF := func(c context.Context, p peer.ID) bool {
@ -264,7 +268,7 @@ func makeRoutingTable(h host.Host, cfg config) (*kb.RoutingTable, error) {
rtPvLogger.Errorf("failed to connect to peer %s for validation, err=%s", p, err)
return false
}
if !cfg.routingTable.peerFilter(h, h.Network().ConnsToPeer(p)) {
if !cfg.routingTable.peerFilter(dht, h.Network().ConnsToPeer(p)) {
return false
}
return true
@ -407,7 +411,7 @@ func (dht *IpfsDHT) peerFound(ctx context.Context, p peer.ID) {
logger.Event(ctx, "peerFound", p)
currentConns := dht.host.Network().ConnsToPeer(p)
if len(currentConns) > 0 {
if dht.routingTablePeerFilter(dht.host, currentConns) {
if dht.routingTablePeerFilter(dht, currentConns) {
dht.routingTable.HandlePeerAlive(p)
}
} else {

View File

@ -4,7 +4,6 @@ import (
"bytes"
"net"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
netroute "github.com/libp2p/go-netroute"
@ -14,14 +13,14 @@ import (
)
// QueryFilterFunc is a filter applied when considering peers to dial when querying
type QueryFilterFunc func(h host.Host, ai peer.AddrInfo) bool
type QueryFilterFunc func(dht *IpfsDHT, ai peer.AddrInfo) bool
// RouteTableFilterFunc is a filter applied when considering connections to keep in
// the local route table.
type RouteTableFilterFunc func(h host.Host, conns []network.Conn) bool
type RouteTableFilterFunc func(dht *IpfsDHT, conns []network.Conn) bool
// PublicQueryFilter returns true if the peer is suspected of being publicly accessible
func PublicQueryFilter(h host.Host, ai peer.AddrInfo) bool {
func PublicQueryFilter(_ *IpfsDHT, ai peer.AddrInfo) bool {
if len(ai.Addrs) == 0 {
return false
}
@ -38,9 +37,11 @@ func PublicQueryFilter(h host.Host, ai peer.AddrInfo) bool {
return hasPublicAddr
}
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(_ host.Host, conns []network.Conn) bool {
func PublicRoutingTableFilter(_ *IpfsDHT, conns []network.Conn) bool {
for _, c := range conns {
addr := c.RemoteMultiaddr()
if !isRelayAddr(addr) && manet.IsPublicAddr(addr) {
@ -50,9 +51,11 @@ func PublicRoutingTableFilter(_ host.Host, conns []network.Conn) bool {
return false
}
var _ RouteTableFilterFunc = PublicRoutingTableFilter
// PrivateQueryFilter returns true if the peer is suspected of being accessible over a shared private network
func PrivateQueryFilter(h host.Host, ai peer.AddrInfo) bool {
conns := h.Network().ConnsToPeer(ai.ID)
func PrivateQueryFilter(dht *IpfsDHT, ai peer.AddrInfo) bool {
conns := dht.Host().Network().ConnsToPeer(ai.ID)
if len(conns) > 0 {
for _, c := range conns {
if manet.IsPrivateAddr(c.RemoteMultiaddr()) {
@ -80,12 +83,14 @@ func PrivateQueryFilter(h host.Host, ai peer.AddrInfo) bool {
return hasPrivateAddr
}
var _ QueryFilterFunc = PrivateQueryFilter
// 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(h host.Host, conns []network.Conn) bool {
func PrivateRoutingTableFilter(dht *IpfsDHT, conns []network.Conn) bool {
router, _ := netroute.New()
myAdvertisedIPs := make([]net.IP, 0)
for _, a := range h.Addrs() {
for _, a := range dht.Host().Addrs() {
if manet.IsPublicAddr(a) {
ip, _ := manet.ToIP(a)
myAdvertisedIPs = append(myAdvertisedIPs, ip)
@ -125,6 +130,8 @@ func PrivateRoutingTableFilter(h host.Host, conns []network.Conn) bool {
return false
}
var _ RouteTableFilterFunc = PrivateRoutingTableFilter
func isEUI(ip net.IP) bool {
// per rfc 2373
return ip[11] == 0xff && ip[12] == 0xfe

View File

@ -9,7 +9,6 @@ import (
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/protocol"
host "github.com/libp2p/go-libp2p-host"
record "github.com/libp2p/go-libp2p-record"
)
@ -58,8 +57,8 @@ type config struct {
testProtocols []protocol.ID
}
func emptyQueryFilter(h host.Host, ai peer.AddrInfo) bool { return true }
func emptyRTFilter(h host.Host, conns []network.Conn) bool { return true }
func emptyQueryFilter(_ *IpfsDHT, ai peer.AddrInfo) bool { return true }
func emptyRTFilter(_ *IpfsDHT, conns []network.Conn) bool { return true }
// apply applies the given options to this Option
func (c *config) apply(opts ...Option) error {

View File

@ -3,6 +3,7 @@ package dht
import (
"context"
"errors"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/routing"
@ -304,8 +305,12 @@ func (q *query) queryPeer(ctx context.Context, p peer.ID) *queryResult {
continue
}
// add any other know addresses for the candidate peer.
curInfo := q.dht.peerstore.PeerInfo(next.ID)
next.Addrs = append(next.Addrs, curInfo.Addrs...)
// add their addresses to the dialer's peerstore
if q.dht.queryPeerFilter(q.dht.host, *next) {
if q.dht.queryPeerFilter(q.dht, *next) {
q.dht.peerstore.AddAddrs(next.ID, next.Addrs, pstore.TempAddrTTL)
closer := q.localPeers.Add(next.ID)
foundCloserPeer = foundCloserPeer || closer