Fix size issue when deserialising on Windows (#144)

This commit is contained in:
Charlie Jonas
2021-02-16 21:04:19 +00:00
committed by GitHub
parent ab39f29b51
commit 25d1413fb7
3 changed files with 25 additions and 2 deletions

View File

@@ -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
}

8
utils_other.go Normal file
View File

@@ -0,0 +1,8 @@
// +build !windows
package ping
// Returns the length of an ICMP message.
func (p *Pinger) getMessageLength() int {
return p.Size + 8
}

16
utils_windows.go Normal file
View File

@@ -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
}