Commit Graph

40 Commits

Author SHA1 Message Date
Charlie Jonas
80a5113803
Reorder channel selection order and make Stop() idempotent (#159)
This fixes #94, closes #116 and also closes #157.
2021-03-27 00:20:15 +00:00
Charlie Jonas
01e0869fb9 Wrap errors when using fmt.Errorf 2021-03-26 23:37:49 +00:00
Marcelo Magallon
df22d52de1
Add OnSetup callback (#155)
Signed-off-by: Marcelo E. Magallon <marcelo.magallon@grafana.com>
Merged-by: Charlie Jonas <charlie@charliejonas.co.uk>
2021-03-25 23:48:16 +00:00
Jean Raby
d90f3778a8
Protect stats with RWMutex (#151)
* Protect stats with RWMutex

updateStats() now also updates rtts so it is protected by the
lock.
Statistics() should now be callable from other goroutines.

PacketsSent and PacketsRecvDuplicates can still be updated while
Statistics() is running, and thus could yield strange results
(sent<recv)

To fix this, atomics could be used, but it would require changing the
types of the fields from int to int64, thus changing the public api.

Signed-off-by: Jean Raby <jean@raby.sh>
2021-03-12 09:51:07 +01:00
Jean Raby
5f9dd908cc
Fix packet matching; compute statistics as packets are received (#150)
* Fix unprivileged packet matching on Linux
* Compute statistics on-the-fly as packets are received

Signed-off-by: Jean Raby <jean@raby.sh>
Signed-off-by: Charlie Jonas <charlie@charliejonas.co.uk>
Co-authored-by: Charlie Jonas <charlie@charliejonas.co.uk>
2021-03-11 22:01:31 +00:00
Charlie Jonas
25d1413fb7
Fix size issue when deserialising on Windows (#144) 2021-02-16 21:04:19 +00:00
paulc
ab39f29b51
add trackerLength to p.Size in New() 2021-02-07 15:00:27 -08:00
Charlie Jonas
62f79f1f4f
Make Tracker uint64 and check for both UDP and ICMP (#134) 2021-02-07 22:15:49 +00:00
Charlie Jonas
38783b05ce Fix size of array into which ICMP messages are deserialised 2021-02-07 22:14:39 +00:00
Ben Kochie
b6486c6f1f Make default Timeout "forever"
Make the default Timeout setting the maximum Go Duration, which is
290 years. Effectively forever.

Signed-off-by: Ben Kochie <superq@gmail.com>
2021-02-01 19:22:33 +00:00
Steven Soroka
52eed920f9
Use MaxUint16 for IDs (#132) 2021-02-01 09:55:49 +00:00
Steven Soroka
30a8f08ad2
Process duplicate packets and make seeds goroutine-safe (#130)
Duplicate ICMP packets are now detected and processed using a separate callback and field in the statistics struct.

Co-authored-by: Charlie Jonas <charlie@charliejonas.co.uk>
Co-authored-by: Ben Kochie <superq@gmail.com>
2021-01-31 13:15:27 +00:00
Charlie Jonas
3300c582a6
Document that Run() returns errors (#112)
* Update example code in README
* Update example code in godoc
* Add missing full stops

Co-authored-by: Ben Kochie <superq@gmail.com>
2020-11-15 14:19:31 +01:00
rrcollier
3977ed7266
Add an OnSend handler (#114)
This commit adds an OnSend handler, similar to the OnReceive handler but called when a packet is sent. It reuses the Packet type, but does not set the TTL or RTT.
2020-10-22 13:20:18 +01:00
rrcollier
265e7c64b3
Add option not to store all received RTTs (#115)
This prevents memory bloat from a process that wants to do very long-running ping tests.
2020-10-20 23:19:59 +01:00
Charlie Jonas
5f9dc3248b Ignore control message errors on Windows
The x/net/ipv4 and x/net/ipv6 packages do not implement the necessary
control message functionality on Microsoft Windows. This commit ignores
any control message errors that are returned by the SetControlMessage
function if the Go runtime is compiled for Windows. This fixes #105.

Signed-off-by: Charlie Jonas <charlie@charliejonas.co.uk>
2020-10-08 17:15:48 +01:00
Ben Kochie
671c40f29a Fix return on deadlock fix
Fix the return value bug introduced by #85

Signed-off-by: Ben Kochie <superq@gmail.com>
2020-10-01 22:41:34 +01:00
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
Lifei Chen
e8ae07c3ce
Return an error when addr is empty 2020-09-18 13:04:29 +01:00
maddie
e9da6dae98
Return error from Run() (#81)
* Return error to caller and let them handle it
* Use explicit return in Run()
* Silence linter for not handling recvICMP error
2020-09-17 15:08:15 +01:00
Ben Kochie
83991da571
Fix build (#102)
* Fix go fmt issue.
* Run golangci-lint in CI.

Signed-off-by: Ben Kochie <superq@gmail.com>
2020-09-15 18:48:24 +02:00
Ben Kochie
e11dda7fa5
Add golangci-lint configuration (#100)
* Add a configuration for golangci-lint.
* Fix some linter issues.

Signed-off-by: Ben Kochie <superq@gmail.com>
2020-09-14 20:23:32 +02:00
Ben Kochie
8e89829cd5
Don't resolve when creating Pinger (#65)
Move the DNS resolver out of the NewPinger() function in order to allow
adjusting of IPv4 vs IPv6 DNS resolution before running. This also
allows the user to verify resolution.
* Create new `New()` method that returns a bare default struct.
* Create new `Resolve()` method.
* Call `Resolve()` from `SetAddr()`.
* Call `Resolve()` automatically from `Run()`.
* Remove unecessary private `run()` method.

Update ping command for simplifed return values of `NewPinger()`.

Signed-off-by: Ben Kochie <superq@gmail.com>
2020-09-14 07:41:27 +02:00
Cam
4e5b655249 Don't separate ping response header from received bytes 2019-06-13 10:43:26 -07:00
Greg
56df11e077 Make processPacket more performant (#59)
* Make processPacket more performant

* Add more info for debugging to returned error

* remove old benchmark

* change print statement to error return
2019-06-03 16:11:21 -07:00
Alirie Gray
d596e7045d Fix runtime panic 2019-05-30 15:36:06 -07:00
Alirie Gray
3eec4c7c0d Add ability to specifiy source IP address 2019-05-29 16:33:37 -07:00
Maximiliano Churichi
5cc5e2921d Fix TTL comment 2019-05-28 15:33:38 -07:00
Clint Armstrong
3745d7b591 expose ttl on returned ping 2019-05-28 15:33:38 -07:00
Cameron Sparr
ef3ab45e41
Don't oversend packets
fixes part of #6
2018-11-06 16:54:34 +00:00
Ben Kochie
3a0b5a7d8e Fix CPU use (#40)
* Move default case out of select block.
* Add defers to close resources.
* `go fmt` cleanup.
2018-11-06 16:04:36 +00:00
Cameron Sparr
9db3df4bca
Set a random source seed to avoid ID conflicts
closes #14
closes #33
2018-11-06 14:30:27 +00:00
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
Ben Kochie
687023bdc7 Include adddress in Packet (#38)
Include the original address in the Packet type for use in
`pinger.OnRecv()`.
2018-11-06 12:47:14 +00:00
Xie Zhenye
667d0a66f2 remove signal handler, add Stop method (#10)
* remove signal handler, add Stop method

* Added ICMP Packet ID to distinguish ICMP replies
2018-11-06 10:48:48 +00:00
Maciek Szczesniak
ad2f544f6b Removed duplicated fields in struct (#27) 2018-08-30 17:03:48 +01:00
webfrank
d046b24518 Added ICMP Packet ID to distinguish ICMP replies in multithreading environment (#15) 2018-08-29 18:21:50 +01:00
keeZey
5740bb1ff1 Change size to Size to make it exportable (#18)
I have made Size exportable as i require the ability to change the
packetsize on a per host basis
2018-08-29 18:14:37 +01:00
Cameron Sparr
dae25ab260 Write unit tests 2016-02-07 14:15:39 -07:00
Cameron Sparr
77403bd6ed First commit of go-ping library 2016-02-01 15:21:12 -07:00