1
0
mirror of https://github.com/go-ping/ping.git synced 2025-05-07 23:16:24 +00:00

Don't separate ping response header from received bytes

This commit is contained in:
Cam 2019-06-05 13:23:35 -07:00 committed by Cameron Sparr
parent e33cfb8ae7
commit 4e5b655249

22
ping.go
View File

@ -441,23 +441,16 @@ func (p *Pinger) recvICMP(
func (p *Pinger) processPacket(recv *packet) error {
receivedAt := time.Now()
var bytes []byte
var proto int
if p.ipv4 {
if p.network == "ip" {
bytes = ipv4Payload(recv)
} else {
bytes = recv.bytes
}
proto = protocolICMP
} else {
bytes = recv.bytes
proto = protocolIPv6ICMP
}
var m *icmp.Message
var err error
if m, err = icmp.ParseMessage(proto, bytes[:recv.nbytes]); err != nil {
if m, err = icmp.ParseMessage(proto, recv.bytes); err != nil {
return fmt.Errorf("error parsing icmp message: %s", err.Error())
}
@ -475,8 +468,7 @@ func (p *Pinger) processPacket(recv *packet) error {
switch pkt := m.Body.(type) {
case *icmp.Echo:
// If we are priviledged, we can match icmp.ID
// If we are privileged, we can match icmp.ID
if p.network == "ip" {
// Check if reply from same ID
if pkt.ID != p.id {
@ -574,16 +566,6 @@ func (p *Pinger) listen(netProto string) *icmp.PacketConn {
return conn
}
func ipv4Payload(recv *packet) []byte {
b := recv.bytes
if len(b) < ipv4.HeaderLen {
return b
}
hdrlen := int(b[0]&0x0f) << 2
recv.nbytes -= hdrlen
return b[hdrlen:]
}
func bytesToTime(b []byte) time.Time {
var nsec int64
for i := uint8(0); i < 8; i++ {