ICMP Ping library for Go
Go to file
Alexandr Stelnykovych 70ede2ab32
Deadlock fix (#85)
Deadlock observed when starting multiple ping routines.
It happens when writting to `recv` channel (which is full) and no active
readers for this channel.

*Example to reproduce the issue* :

	var waiter sync.WaitGroup
	pingFunc := func() {
		defer waiter.Done()

		pinger, err := ping.NewPinger("8.8.8.8")
		if err != nil {
			return
		}

		pinger.SetPrivileged(true)
		pinger.Count = 5
		pinger.Interval = time.Second
		pinger.Timeout = time.Second * 4

		pinger.Run()
	}

	for i := 0; i < 1000; i++ {
		waiter.Add(1)
		go pingFunc()
	}

	waiter.Wait() // deadlock here! (reproducible almost every time)
2020-10-01 17:29:13 +02:00
.circleci Fix build (#102) 2020-09-15 18:48:24 +02:00
cmd/ping Return error from Run() (#81) 2020-09-17 15:08:15 +01:00
.gitignore Add goreleaser build (#98) 2020-09-14 08:20:13 +02:00
.golangci.yml Add golangci-lint configuration (#100) 2020-09-14 20:23:32 +02:00
.goreleaser.yml Add goreleaser build (#98) 2020-09-14 08:20:13 +02:00
go.mod Initialize go module (#93) 2020-09-13 12:04:30 +02:00
go.sum Initialize go module (#93) 2020-09-13 12:04:30 +02:00
LICENSE Initial commit 2016-02-01 15:06:45 -07:00
Makefile Add Makefile (#99) 2020-09-14 23:26:21 +02:00
ping_test.go Return an error when addr is empty 2020-09-18 13:04:29 +01:00
ping.go Deadlock fix (#85) 2020-10-01 17:29:13 +02:00
README.md Update readme and sample command to go-ping organization 2020-09-12 12:11:16 -07: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/go-ping/ping

To install the native Go ping executable:

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

Maintainers and Support:

This repo was originally in the personal account of @sparrc, but is now maintained by the go-ping organization.

For support and help, you usually find us in the #go-ping channel of Gophers slack. See https://invite.slack.golangbridge.org/ for an invite to the Gophers slack org.

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.