Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

record received TTLs #117

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func New(addr string) *Pinger {
Count: -1,
Interval: time.Second,
RecordRtts: true,
RecordTTLs: true,
Size: timeSliceLength + trackerLength,
Timeout: time.Duration(math.MaxInt64),

Expand Down Expand Up @@ -166,9 +167,16 @@ type Pinger struct {
// Set to false to avoid memory bloat for long running pings.
RecordRtts bool

// If true, keep a record of TTLs of all received packets.
// Set to false to avoid memory bloat for long running pings.
RecordTTLs bool

// rtts is all of the Rtts
rtts []time.Duration

// ttls is all of the TTLs
ttls []uint8

// OnSetup is called when Pinger has finished setting up the listening socket
OnSetup func()

Expand Down Expand Up @@ -285,6 +293,9 @@ type Statistics struct {
// Rtts is all of the round-trip times sent via this pinger.
Rtts []time.Duration

// TTLs is all of the TTLs received via this pinger.
TTLs []uint8

// MinRtt is the minimum round-trip time sent via this pinger.
MinRtt time.Duration

Expand All @@ -308,6 +319,10 @@ func (p *Pinger) updateStatistics(pkt *Packet) {
p.rtts = append(p.rtts, pkt.Rtt)
}

if p.RecordTTLs {
p.ttls = append(p.ttls, uint8(pkt.TTL))
}

if p.PacketsRecv == 1 || pkt.Rtt < p.minRtt {
p.minRtt = pkt.Rtt
}
Expand Down Expand Up @@ -643,6 +658,7 @@ func (p *Pinger) Statistics() *Statistics {
PacketsRecvDuplicates: p.PacketsRecvDuplicates,
PacketLoss: loss,
Rtts: p.rtts,
TTLs: p.ttls,
Addr: p.addr,
IPAddr: p.ipaddr,
MaxRtt: p.maxRtt,
Expand Down