Commit Graph

73 Commits

Author SHA1 Message Date
Thomas Casteleyn
610518ea2b
Mark module as deprecated (#236) 2024-12-02 00:53:39 +00:00
Thomas Casteleyn
caaf2b72ea Add deprecation notice to README 2023-01-21 22:33:18 +00:00
Marcelo Magallon
b89bb75386
Report whether or not the TTL value was read (#205)
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>
2022-05-23 16:54:11 +02:00
chenlujjj
6d40342032
Delete redundant pinger.Stop() call (#197) 2022-04-28 15:36:15 +02:00
Marcelo E. Magallon
64d100b308 Goreleaser can no longer be installed using goreleaser.sh
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>
2022-04-27 09:11:44 +01:00
Charlie Jonas
779d1e9195 Set Packet.ID when sending ICMP requests 2021-11-30 11:55:50 +00:00
Nate Sales
27679364c9 feat: add configurable ICMP identifier 2021-11-30 11:55:50 +00:00
Cemre Mengu
6e2b003bff
Add ability to set TTL (#186) 2021-10-14 19:03:14 +01:00
Ben Kochie
3818264768 Use a UUID for packet tracking
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>
2021-09-11 16:15:12 +01:00
Marcelo Magallon
ff8be33200
Provide an abstraction over icmp.PacketConn (#166)
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.
2021-05-07 00:38:00 +01:00
Marcelo Magallon
e4e642a957
Use exponential backoff for read deadline (#162)
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>
2021-04-07 22:46:46 +01:00
Marcelo Magallon
1726e5ede5
Add Logger interface to avoid logging directly (#161)
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>
2021-04-03 00:25:49 +01:00
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
Charlie Jonas
54fa5d1e4a Add size flag to demo pinger
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>
2021-03-26 23:36:52 +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
Lee
ad0b2d22f9 fix doc name
Signed-off-by: Lee <lee10202013@gmail.com>
2021-02-05 21:39:47 +00:00
Lee Wei
42a6788a51
Update CONTRIBUTING.md (#128)
* init contributing guide
* update README

Signed-off-by: Lee <lee10202013@gmail.com>
Co-authored-by: Charlie Jonas <charlie@charliejonas.co.uk>
2021-02-05 09:25:45 +01: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
Rob Collier
acac328aae Update the test to use an always invalid hostname (under the .invalid tld) 2020-11-14 14:44:32 -08:00
Rob Collier
145e7e44ad TLD 'wtf' now exists, use 'wtfh' instead in TestNewPingerInvalid 2020-11-14 14:44:32 -08: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
Charlie Jonas
4db5733515
Update LICENSE and README
This commit extensively copyedits the README and adds a nod to our contributors in the LICENSE file.
2020-10-08 17:15:17 +01:00
Charlie Jonas
fe82a40fcd
Add EditorConfig file
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>
2020-10-08 17:12:42 +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
54d38cd396
Add Makefile (#99)
Add a Makefile to simplify build and test.

Signed-off-by: Ben Kochie <superq@gmail.com>
2020-09-14 23:26:21 +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
800dd84e47
Add goreleaser build (#98)
* Add goreleaser config.
* Update .gitignore to skip build output.
* Update CircleCI to Go 1.15 with new optimized image[0].
* Add release workflow step.

[0]: https://circleci.com/docs/2.0/circleci-images/#next-gen-circleci-images

Signed-off-by: Ben Kochie <superq@gmail.com>
2020-09-14 08:20:13 +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
Prasanna Kumar
805de73348
Initialize go module (#93)
* Initialize go module

* Update go.mod

* Update go.sum
2020-09-13 12:04:30 +02:00
Cam
41725b6c24 Update readme and sample command to go-ping organization
closes #90
2020-09-12 12:11:16 -07:00
Cam
4e5b655249 Don't separate ping response header from received bytes 2019-06-13 10:43:26 -07:00
Cam
e33cfb8ae7 Add processPacket unit tests
Add vet and unit test to circleci job
2019-06-04 11:15:55 -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