ICMP Ping library for Go
Go to file
Lincoln Thurlow 28a88d0810 patch: compare identifier for non-root access icmp (#32)
Commit d046b245 introduces a bug which causes ping to always fail.
The source of this bug is:

```
	// Check if reply from same ID
	body := m.Body.(*icmp.Echo)
	if body.ID != p.id {
		return nil
	}
```

Which due to the selection of p.id requires that SetPrivileged is
set to true.  In the case where Privileged (i.e p.network == udp)
it is left to the kernel to set the ICMP id.

https://lwn.net/Articles/443051/  Discusses the introduction of
non-setuid-less ping.  The kernel implementation for this
interface dictates using the local port, which gets mapped into
the ping_table struct.  There is no current implementation in the
go icmp library to address this problem directly.

To address this issue, I've added a `Tracker` field for `Pinger`
as well as `IcmpData` datastructure to allow for uniquely tracking
icmp requests.  The id (as with the `id` field) is not unique,
but will statistically rare for duplicates.
2018-11-06 14:10:49 +00:00
.circleci Update sample code, README, and circle v2 (#42) 2018-11-06 12:45:58 +00:00
cmd/ping Update sample code, README, and circle v2 (#42) 2018-11-06 12:45:58 +00:00
.gitignore uncommit ping binary 2016-02-01 15:52:45 -07:00
LICENSE Initial commit 2016-02-01 15:06:45 -07:00
ping_test.go Add integration tests in circle 2016-02-08 09:29:08 -07:00
ping.go patch: compare identifier for non-root access icmp (#32) 2018-11-06 14:10:49 +00:00
README.md Update sample code, README, and circle v2 (#42) 2018-11-06 12:45:58 +00:00

go-ping

GoDoc Circle CI

ICMP Ping library for Go, inspired by go-fastping

Here is a very simple example that sends & receives 3 packets:

pinger, err := ping.NewPinger("www.google.com")
if err != nil {
        panic(err)
}
pinger.Count = 3
pinger.Run() // blocks until finished
stats := pinger.Statistics() // get send/receive/rtt stats

Here is an example that emulates the unix ping command:

pinger, err := ping.NewPinger("www.google.com")
if err != nil {
        panic(err)
}

// listen for ctrl-C signal
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
	for _ = range c {
		pinger.Stop()
	}
}()

pinger.OnRecv = func(pkt *ping.Packet) {
        fmt.Printf("%d bytes from %s: icmp_seq=%d time=%v\n",
                pkt.Nbytes, pkt.IPAddr, pkt.Seq, pkt.Rtt)
}
pinger.OnFinish = func(stats *ping.Statistics) {
        fmt.Printf("\n--- %s ping statistics ---\n", stats.Addr)
        fmt.Printf("%d packets transmitted, %d packets received, %v%% packet loss\n",
                stats.PacketsSent, stats.PacketsRecv, stats.PacketLoss)
        fmt.Printf("round-trip min/avg/max/stddev = %v/%v/%v/%v\n",
                stats.MinRtt, stats.AvgRtt, stats.MaxRtt, stats.StdDevRtt)
}

fmt.Printf("PING %s (%s):\n", pinger.Addr(), pinger.IPAddr())
pinger.Run()

It sends ICMP packet(s) and waits for a response. If it receives a response, it calls the "receive" callback. When it's finished, it calls the "finish" callback.

For a full ping example, see cmd/ping/ping.go

Installation:

go get github.com/sparrc/go-ping

To install the native Go ping executable:

go get github.com/sparrc/go-ping/...
$GOPATH/bin/ping

Note on Linux Support:

This library attempts to send an "unprivileged" ping via UDP. On linux, this must be enabled by setting

sudo sysctl -w net.ipv4.ping_group_range="0   2147483647"

If you do not wish to do this, you can set pinger.SetPrivileged(true) and use setcap to allow your binary using go-ping to bind to raw sockets (or just run as super-user):

setcap cap_net_raw=+ep /bin/go-ping

See this blog and the Go icmp library for more details.

Note on Windows Support:

You must use pinger.SetPrivileged(true), otherwise you will receive an error:

Error listening for ICMP packets: socket: The requested protocol has not been configured into the system, or no implementation for it exists.

This should without admin privileges. Tested on Windows 10.