ping: add a useful error message when pinging self
We don't allow dialing self for many (very good) reasons. fixes #4542 License: MIT Signed-off-by: Steven Allen <steven@stebalien.com>
Steven Allen committed
Jan 4, 2018 at 10:23 UTC
aa95dcbdf2eb1943da09dc731af0823ffab97d33
1 file changed
+9
core/commands/ping.go
+9
@@ -3,6 +3,7 @@ package commands
3
import (
4
"bytes"
5
"context"
6
+ "errors"
7
"fmt"
8
"io"
9
"strings"
@@ -26,6 +27,9 @@ type PingResult struct {
27
Text string
28
}
29
30
+// ErrPingSelf is returned when the user attempts to ping themself.
31
+var ErrPingSelf = errors.New("error: can't ping self")
32
+
33
var PingCmd = &cmds.Command{
34
Helptext: cmdkit.HelpText{
35
Tagline: "Send echo request packets to IPFS hosts.",
@@ -84,6 +88,11 @@ trip latency information.
88
return
89
}
90
91
+ if peerID == n.Identity {
92
+ res.SetError(ErrPingSelf, cmdkit.ErrNormal)
93
+ return
94
+ }
95
+
96
if addr != nil {
97
n.Peerstore.AddAddr(peerID, addr, pstore.TempAddrTTL) // temporary
98
}