diff --git a/ping.go b/ping.go index 127f0c5..b2c96ba 100644 --- a/ping.go +++ b/ping.go @@ -472,8 +472,7 @@ func (p *Pinger) recvICMP( case <-p.done: return nil default: - // ICMP messages have an 8-byte header. - bytes := make([]byte, p.Size+8) + bytes := make([]byte, p.getMessageLength()) if err := conn.SetReadDeadline(time.Now().Add(time.Millisecond * 100)); err != nil { return err } diff --git a/utils_other.go b/utils_other.go new file mode 100644 index 0000000..cd73ab2 --- /dev/null +++ b/utils_other.go @@ -0,0 +1,8 @@ +// +build !windows + +package ping + +// Returns the length of an ICMP message. +func (p *Pinger) getMessageLength() int { + return p.Size + 8 +} diff --git a/utils_windows.go b/utils_windows.go new file mode 100644 index 0000000..2861f64 --- /dev/null +++ b/utils_windows.go @@ -0,0 +1,16 @@ +// +build windows + +package ping + +import ( + "golang.org/x/net/ipv4" + "golang.org/x/net/ipv6" +) + +// Returns the length of an ICMP message, plus the IP packet header. +func (p *Pinger) getMessageLength() int { + if p.ipv4 { + return p.Size + 8 + ipv4.HeaderLen + } + return p.Size + 8 + ipv6.HeaderLen +}