In IPv6 a hop limit of 0 is valid, so the TTL the code is reporting is
actually a valid value in that case.
Initialize ttl to -1, so that in the case of not getting a TTL value
from the control message, we can let the rest of the code whether the
reported value was actually 0 or whether it couldn't be read.
Signed-off-by: Marcelo E. Magallon <marcelo.magallon@grafana.com>
This installation method was deprecated because the associated tool has
been deprecated.
Download "by hand" and run checks before trying to use binary.
Signed-off-by: Marcelo E. Magallon <marcelo.magallon@grafana.com>
Replace the random 8 byte tracker with a 16 byte UUID.
* Implement as a slice so that it can be extended later.
* Deprecate the exported `Pinger.Tracker` value.
https://github.com/go-ping/ping/issues/142
Signed-off-by: Ben Kochie <superq@gmail.com>
The differences between IPv4 and IPv6 APIs can be moved to a single type so that we don't need to keep track of them all over the code. We can also split Run() into two parts: the top one sets up the listener and the bottom one sends and receives packets. In this way, the bottom part can be tested using a mock packet connection.
Before this change, pinging any target once takes ~ 100 ms (the time it takes for Run() to return, not the RTT). After this change, it takes a time comparable to the RTT. The change comes from ReadFrom blocking for the specified delay if the done signal hasn't fired yet. It also improves a little the time it takes to ping a single target multiple times.
Since this is exponential backoff in the way Ethernet does it (random delay, with an increasing maximum value, only changing in case of a timeout), the delay neither necessarily doubles nor increases each time the operation has to be retried, but it does eventually settle on the higher end of the possibilities if there are many timeouts.
Signed-off-by: Marcelo E. Magallon <marcelo.magallon@grafana.com>
Committed-by: Marcelo E. Magallon <marcelo.magallon@grafana.com>
Committed-by: Charlie Jonas <charlie@charliejonas.co.uk>
This commit adds a simple level-based log interface that allows users of the library to control and customize the output, for example by providing a standard Go logger with different settings (timestamps, prefixes, etc).
Closes#16. Closes#103.
Signed-off-by: Marcelo E. Magallon <marcelo.magallon@grafana.com>
Adds a command line flag to the demo pinger which lets the user set the
the size of the ICMP payload. This is useful for testing. The default is
set at 16 which is the default for an unconfigured pinger.
Signed-off-by: Charlie Jonas <charlie@charliejonas.co.uk>
* 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>
* 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>
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>
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.
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>
EditorConfig (https://editorconfig.org) is a dotfile which stores
configuration that is supported by a wide variety of text editors and
IDEs both natively and with plugins. This allows us to enforce project
standards such as line endings and indentation style.
Signed-off-by: Charlie Jonas <charlie@charliejonas.co.uk>
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)
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>