Skip to content

Commit

Permalink
feat: dht ping (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
dozyio authored Dec 2, 2024
1 parent b682bf3 commit c246e52
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,25 @@ func DhtGetClosestPeers(ctx context.Context, key []byte, proto protocol.ID, ma m
return ais, nil
}

func DhtPing(ctx context.Context, proto protocol.ID, ma multiaddr.Multiaddr) error {
ai, err := peer.AddrInfoFromP2pAddr(ma)
if err != nil {
return err
}

m, err := DhtProtocolMessenger(ctx, proto, ai)
if err != nil {
return err
}

err = m.Ping(ctx, ai.ID)
if err != nil {
return err
}

return nil
}

// dhtMsgSender handles sending dht wire protocol messages to a given peer
type dhtMsgSender struct {
h host.Host
Expand Down
34 changes: 34 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,40 @@ func main() {
},
},
},
{
Name: "ping",
ArgsUsage: "<multiaddr>",
Usage: "ping a DHT node",
Description: "sends a DHT ping to the target",
Action: func(c *cli.Context) error {
if c.NArg() != 1 {
return fmt.Errorf("invalid number of arguments")
}
maStr := c.Args().Get(0)
protoID := c.String("protocolID")

ma, err := multiaddr.NewMultiaddr(maStr)
if err != nil {
return err
}

err = vole.DhtPing(c.Context, protocol.ID(protoID), ma)
if err != nil {
return err
}

fmt.Println("Ok")
return nil
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "protocolID",
Usage: "the protocol ID",
DefaultText: "/ipfs/kad/1.0.0",
Value: "/ipfs/kad/1.0.0",
},
},
},
},
},
{
Expand Down

0 comments on commit c246e52

Please sign in to comment.