Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions defs/pinger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package defs

import (
"net"
"testing"
)

// --ipv4 and --ipv6 name the family the whole test runs over, so the ICMP
// ping must not resolve its target in the other one.
func TestNewPingerResolvesWithinTheRequestedFamily(t *testing.T) {
cases := []struct {
host string
network string
wantErr bool
}{
{"127.0.0.1", "ip", false},
{"127.0.0.1", "ip4", false},
{"127.0.0.1", "ip6", true},
{"::1", "ip", false},
{"::1", "ip6", false},
{"::1", "ip4", true},
}

for _, c := range cases {
t.Run(c.host+" over "+c.network, func(t *testing.T) {
p, err := newPinger(c.host, c.network)
if c.wantErr {
if err == nil {
t.Fatalf("resolved %s to %s, want an error", c.host, p.IPAddr())
}
return
}
if err != nil {
t.Fatalf("newPinger: %v", err)
}
if got := p.IPAddr(); got == nil || !got.IP.Equal(net.ParseIP(c.host)) {
t.Errorf("resolved to %v, want %s", got, c.host)
}
})
}
}

// The family was ignored for a name with addresses in both: the name was
// resolved before the family was set. localhost is such a name on most
// systems. Where it has no address in a family, there is nothing to tell
// apart, and that family is skipped.
func TestNewPingerResolvesANameWithinTheRequestedFamily(t *testing.T) {
for _, network := range []string{"ip4", "ip6"} {
t.Run(network, func(t *testing.T) {
if _, err := net.ResolveIPAddr(network, "localhost"); err != nil {
t.Skipf("localhost has no %s address here: %v", network, err)
}

p, err := newPinger("localhost", network)
if err != nil {
t.Fatalf("newPinger: %v", err)
}
got := p.IPAddr()
if got == nil || (got.IP.To4() != nil) != (network == "ip4") {
t.Errorf("resolved to %v over %s", got, network)
}
})
}
}
15 changes: 13 additions & 2 deletions defs/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,12 @@ func (s *Server) ICMPPingAndJitter(count int, srcIp, network string) (float64, f
return 0, 0, err
}

p, err := probing.NewPinger(u.Hostname())
p, err := newPinger(u.Hostname(), network)
if err != nil {
output.WriteDebug("Failed to resolve ping target: %s\n", err)
output.WriteDebug("Will try TCP ping\n")
return s.PingAndJitter(count + 2)
}
p.SetNetwork(network)
p.Count = count
p.Timeout = time.Duration(count) * time.Second
if srcIp != "" {
Expand Down Expand Up @@ -228,6 +227,18 @@ func (s *Server) ICMPPingAndJitter(count int, srcIp, network string) (float64, f
return rttMillis(stats.AvgRtt), jitter, nil
}

// newPinger returns a pinger for host, resolved within network ("ip", "ip4"
// or "ip6"). The network is set before resolving: probing.NewPinger resolves
// at once with "ip", and a pinger that has an address does not resolve again.
func newPinger(host, network string) (*probing.Pinger, error) {
p := probing.New(host)
p.SetNetwork(network)
if err := p.Resolve(); err != nil {
return nil, err
}
return p, nil
}

// addressFamily names the IP version of an address, for reporting which path a
// measurement took. Accepts either a bare address or one with a port.
func addressFamily(addr string) string {
Expand Down