From d046b245186684963b4d7e3fca8dc4b1538c976a Mon Sep 17 00:00:00 2001 From: webfrank Date: Wed, 29 Aug 2018 19:21:50 +0200 Subject: [PATCH] Added ICMP Packet ID to distinguish ICMP replies in multithreading environment (#15) --- ping.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/ping.go b/ping.go index c37c251..32ef8f2 100644 --- a/ping.go +++ b/ping.go @@ -90,7 +90,10 @@ func NewPinger(addr string) (*Pinger, error) { Interval: time.Second, Timeout: time.Second * 100000, Count: -1, - + id: rand.Intn(0xffff), + network: "udp", + ipv4: ipv4, + size: timeSliceLength, network: "udp", ipv4: ipv4, Size: timeSliceLength, @@ -140,9 +143,10 @@ type Pinger struct { ipaddr *net.IPAddr addr string - ipv4 bool - source string - + ipv4 bool + source string + size int + id int sequence int network string } @@ -430,6 +434,12 @@ func (p *Pinger) processPacket(recv *packet) error { return nil } + // Check if reply from same ID + body := m.Body.(*icmp.Echo) + if body.ID != p.id { + return nil + } + outPkt := &Packet{ Nbytes: recv.nbytes, IPAddr: p.ipaddr, @@ -475,7 +485,7 @@ func (p *Pinger) sendICMP(conn *icmp.PacketConn) error { bytes, err := (&icmp.Message{ Type: typ, Code: 0, Body: &icmp.Echo{ - ID: rand.Intn(65535), + ID: p.id, Seq: p.sequence, Data: t, },