2019-06-09 07:24:20 +00:00
|
|
|
package ping
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2022-08-19 16:34:07 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/binary"
|
2019-06-09 07:24:20 +00:00
|
|
|
"errors"
|
|
|
|
"io"
|
2022-08-19 16:34:07 +00:00
|
|
|
mrand "math/rand"
|
2019-06-09 07:24:20 +00:00
|
|
|
"time"
|
|
|
|
|
2021-10-19 13:43:41 +00:00
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2022-04-01 16:16:46 +00:00
|
|
|
pool "github.com/libp2p/go-buffer-pool"
|
2022-11-04 13:57:20 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/host"
|
|
|
|
"github.com/libp2p/go-libp2p/core/network"
|
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
2019-06-09 07:24:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var log = logging.Logger("ping")
|
|
|
|
|
2022-04-01 16:16:46 +00:00
|
|
|
const (
|
|
|
|
PingSize = 32
|
|
|
|
pingTimeout = time.Second * 60
|
2019-06-09 07:24:20 +00:00
|
|
|
|
2022-04-01 16:16:46 +00:00
|
|
|
ID = "/ipfs/ping/1.0.0"
|
2019-06-09 07:24:20 +00:00
|
|
|
|
2022-04-01 16:16:46 +00:00
|
|
|
ServiceName = "libp2p.ping"
|
|
|
|
)
|
2019-06-09 07:24:20 +00:00
|
|
|
|
|
|
|
type PingService struct {
|
|
|
|
Host host.Host
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPingService(h host.Host) *PingService {
|
|
|
|
ps := &PingService{h}
|
|
|
|
h.SetStreamHandler(ID, ps.PingHandler)
|
|
|
|
return ps
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PingService) PingHandler(s network.Stream) {
|
2022-04-01 16:16:46 +00:00
|
|
|
if err := s.Scope().SetService(ServiceName); err != nil {
|
|
|
|
log.Debugf("error attaching stream to ping service: %s", err)
|
|
|
|
s.Reset()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.Scope().ReserveMemory(PingSize, network.ReservationPriorityAlways); err != nil {
|
|
|
|
log.Debugf("error reserving memory for ping stream: %s", err)
|
|
|
|
s.Reset()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer s.Scope().ReleaseMemory(PingSize)
|
|
|
|
|
|
|
|
buf := pool.Get(PingSize)
|
|
|
|
defer pool.Put(buf)
|
2019-06-09 07:24:20 +00:00
|
|
|
|
|
|
|
errCh := make(chan error, 1)
|
|
|
|
defer close(errCh)
|
|
|
|
timer := time.NewTimer(pingTimeout)
|
|
|
|
defer timer.Stop()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-timer.C:
|
|
|
|
log.Debug("ping timeout")
|
|
|
|
case err, ok := <-errCh:
|
|
|
|
if ok {
|
|
|
|
log.Debug(err)
|
|
|
|
} else {
|
|
|
|
log.Error("ping loop failed without error")
|
|
|
|
}
|
|
|
|
}
|
2023-02-22 21:58:17 +00:00
|
|
|
s.Close()
|
2019-06-09 07:24:20 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
_, err := io.ReadFull(s, buf)
|
|
|
|
if err != nil {
|
|
|
|
errCh <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = s.Write(buf)
|
|
|
|
if err != nil {
|
|
|
|
errCh <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
timer.Reset(pingTimeout)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Result is a result of a ping attempt, either an RTT or an error.
|
|
|
|
type Result struct {
|
|
|
|
RTT time.Duration
|
|
|
|
Error error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ps *PingService) Ping(ctx context.Context, p peer.ID) <-chan Result {
|
|
|
|
return Ping(ctx, ps.Host, p)
|
|
|
|
}
|
|
|
|
|
2022-04-01 16:16:46 +00:00
|
|
|
func pingError(err error) chan Result {
|
|
|
|
ch := make(chan Result, 1)
|
|
|
|
ch <- Result{Error: err}
|
|
|
|
close(ch)
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
// Ping pings the remote peer until the context is canceled, returning a stream
|
|
|
|
// of RTTs or errors.
|
|
|
|
func Ping(ctx context.Context, h host.Host, p peer.ID) <-chan Result {
|
2024-06-05 20:10:03 +00:00
|
|
|
s, err := h.NewStream(network.WithAllowLimitedConn(ctx, "ping"), p, ID)
|
2019-06-09 07:24:20 +00:00
|
|
|
if err != nil {
|
2022-04-01 16:16:46 +00:00
|
|
|
return pingError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.Scope().SetService(ServiceName); err != nil {
|
|
|
|
log.Debugf("error attaching stream to ping service: %s", err)
|
|
|
|
s.Reset()
|
|
|
|
return pingError(err)
|
2019-06-09 07:24:20 +00:00
|
|
|
}
|
|
|
|
|
2022-08-19 16:34:07 +00:00
|
|
|
b := make([]byte, 8)
|
|
|
|
if _, err := rand.Read(b); err != nil {
|
|
|
|
log.Errorf("failed to get cryptographic random: %s", err)
|
|
|
|
s.Reset()
|
|
|
|
return pingError(err)
|
|
|
|
}
|
|
|
|
ra := mrand.New(mrand.NewSource(int64(binary.BigEndian.Uint64(b))))
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
|
|
|
|
out := make(chan Result)
|
|
|
|
go func() {
|
|
|
|
defer close(out)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
for ctx.Err() == nil {
|
|
|
|
var res Result
|
2022-08-19 16:34:07 +00:00
|
|
|
res.RTT, res.Error = ping(s, ra)
|
2019-06-09 07:24:20 +00:00
|
|
|
|
|
|
|
// canceled, ignore everything.
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// No error, record the RTT.
|
|
|
|
if res.Error == nil {
|
|
|
|
h.Peerstore().RecordLatency(p, res.RTT)
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case out <- res:
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2024-06-05 20:10:03 +00:00
|
|
|
context.AfterFunc(ctx, func() {
|
2019-06-09 07:24:20 +00:00
|
|
|
// forces the ping to abort.
|
|
|
|
s.Reset()
|
2024-06-05 20:10:03 +00:00
|
|
|
})
|
2019-06-09 07:24:20 +00:00
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2022-08-19 16:34:07 +00:00
|
|
|
func ping(s network.Stream, randReader io.Reader) (time.Duration, error) {
|
2022-04-01 16:16:46 +00:00
|
|
|
if err := s.Scope().ReserveMemory(2*PingSize, network.ReservationPriorityAlways); err != nil {
|
|
|
|
log.Debugf("error reserving memory for ping stream: %s", err)
|
|
|
|
s.Reset()
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer s.Scope().ReleaseMemory(2 * PingSize)
|
|
|
|
|
|
|
|
buf := pool.Get(PingSize)
|
|
|
|
defer pool.Put(buf)
|
|
|
|
|
2022-08-19 16:34:07 +00:00
|
|
|
if _, err := io.ReadFull(randReader, buf); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2019-06-09 07:24:20 +00:00
|
|
|
|
|
|
|
before := time.Now()
|
2022-08-19 16:34:07 +00:00
|
|
|
if _, err := s.Write(buf); err != nil {
|
2019-06-09 07:24:20 +00:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2022-04-01 16:16:46 +00:00
|
|
|
rbuf := pool.Get(PingSize)
|
|
|
|
defer pool.Put(rbuf)
|
|
|
|
|
2022-08-19 16:34:07 +00:00
|
|
|
if _, err := io.ReadFull(s, rbuf); err != nil {
|
2019-06-09 07:24:20 +00:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(buf, rbuf) {
|
2021-10-19 13:43:41 +00:00
|
|
|
return 0, errors.New("ping packet was incorrect")
|
2019-06-09 07:24:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return time.Since(before), nil
|
|
|
|
}
|