Skip to content

Commit

Permalink
record received TTLs
Browse files Browse the repository at this point in the history
Signed-off-by: Dragan Milic <[email protected]>
  • Loading branch information
draganm committed Aug 19, 2024
1 parent c25c886 commit 83a9678
Showing 1 changed file with 16 additions and 0 deletions.
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

0 comments on commit 83a9678

Please sign in to comment.