Skip to content

Commit 2c8f387

Browse files
committed
return info for connected peers in handleFindPeer
This way, users who are actually trying to find a peer (not just nodes near a key in the DHT) can find that peer, even if they aren't a DHT server and/or aren't in anyone's routing table. fixes #161
1 parent 2941d6e commit 2c8f387

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

dht.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
ci "github.com/libp2p/go-libp2p-crypto"
2424
host "github.com/libp2p/go-libp2p-host"
2525
kb "github.com/libp2p/go-libp2p-kbucket"
26+
inet "github.com/libp2p/go-libp2p-net"
2627
peer "github.com/libp2p/go-libp2p-peer"
2728
pstore "github.com/libp2p/go-libp2p-peerstore"
2829
protocol "github.com/libp2p/go-libp2p-protocol"
@@ -272,11 +273,12 @@ func (dht *IpfsDHT) Update(ctx context.Context, p peer.ID) {
272273

273274
// FindLocal looks for a peer with a given ID connected to this dht and returns the peer and the table it was found in.
274275
func (dht *IpfsDHT) FindLocal(id peer.ID) pstore.PeerInfo {
275-
p := dht.routingTable.Find(id)
276-
if p != "" {
277-
return dht.peerstore.PeerInfo(p)
276+
switch dht.host.Network().Connectedness(id) {
277+
case inet.Connected, inet.CanConnect:
278+
return dht.peerstore.PeerInfo(id)
279+
default:
280+
return pstore.PeerInfo{}
278281
}
279-
return pstore.PeerInfo{}
280282
}
281283

282284
// findPeerSingle asks peer 'p' if they know where the peer with id 'id' is

handlers.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
u "github.com/ipfs/go-ipfs-util"
1313
pb "github.com/libp2p/go-libp2p-kad-dht/pb"
1414
lgbl "github.com/libp2p/go-libp2p-loggables"
15+
inet "github.com/libp2p/go-libp2p-net"
1516
peer "github.com/libp2p/go-libp2p-peer"
1617
pstore "github.com/libp2p/go-libp2p-peerstore"
1718
recpb "github.com/libp2p/go-libp2p-record/pb"
@@ -266,10 +267,28 @@ func (dht *IpfsDHT) handleFindPeer(ctx context.Context, p peer.ID, pmes *pb.Mess
266267
var closest []peer.ID
267268

268269
// if looking for self... special case where we send it on CloserPeers.
269-
if peer.ID(pmes.GetKey()) == dht.self {
270+
targetPid := peer.ID(pmes.GetKey())
271+
if targetPid == dht.self {
270272
closest = []peer.ID{dht.self}
271273
} else {
272274
closest = dht.betterPeersToQuery(pmes, p, CloserPeerCount)
275+
276+
// Never tell a peer about itself.
277+
if targetPid != p {
278+
// If we're connected to the target peer, report their
279+
// peer info. This makes FindPeer work even if the
280+
// target peer isn't in our routing table.
281+
//
282+
// Alternatively, we could just check our peerstore.
283+
// However, we don't want to return out of date
284+
// information. We can change this in the future when we
285+
// add a progressive, asynchronous `SearchPeer` function
286+
// and improve peer routing in the host.
287+
switch dht.host.Network().Connectedness(targetPid) {
288+
case inet.Connected, inet.CanConnect:
289+
closest = append(closest, targetPid)
290+
}
291+
}
273292
}
274293

275294
if closest == nil {

0 commit comments

Comments
 (0)