mirror of
https://github.com/go-ping/ping.git
synced 2025-07-21 01:09:09 +00:00
Fix non-privileged (UDP) ping response parsing for OSes not supporting STRIPHDR sock option
Signed-off-by: Evgeniy Makeev <evgeniym@fb.com>
This commit is contained in:
parent
b89bb75386
commit
f4146feb68
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1,3 @@
|
|||||||
/ping
|
/ping
|
||||||
/dist
|
/dist
|
||||||
|
.idea
|
||||||
|
16
ping.go
16
ping.go
@ -580,7 +580,8 @@ func (p *Pinger) recvICMP(
|
|||||||
case <-p.done:
|
case <-p.done:
|
||||||
return nil
|
return nil
|
||||||
default:
|
default:
|
||||||
bytes := make([]byte, p.getMessageLength())
|
expectedSize := p.getMessageLength()
|
||||||
|
bytes := make([]byte, expectedSize)
|
||||||
if err := conn.SetReadDeadline(time.Now().Add(delay)); err != nil {
|
if err := conn.SetReadDeadline(time.Now().Add(delay)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -597,7 +598,20 @@ func (p *Pinger) recvICMP(
|
|||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
// setsockopt sysIP_STRIPHDR may not work, check if we got IP header & need to strip it
|
||||||
|
if !p.Privileged() && n >= expectedSize {
|
||||||
|
if p.ipv4 {
|
||||||
|
if hdr, err := ipv4.ParseHeader(bytes); err == nil && hdr != nil && hdr.TotalLen == n && hdr.Len == ipv4.HeaderLen {
|
||||||
|
|
||||||
|
bytes = bytes[hdr.Len:]
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if hdr, err := ipv6.ParseHeader(bytes); err == nil && hdr.PayloadLen == p.Size && hdr.NextHeader == protocolIPv6ICMP {
|
||||||
|
|
||||||
|
bytes = bytes[ipv6.HeaderLen:]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
select {
|
select {
|
||||||
case <-p.done:
|
case <-p.done:
|
||||||
return nil
|
return nil
|
||||||
|
@ -3,9 +3,20 @@
|
|||||||
|
|
||||||
package ping
|
package ping
|
||||||
|
|
||||||
|
import (
|
||||||
|
"golang.org/x/net/ipv4"
|
||||||
|
"golang.org/x/net/ipv6"
|
||||||
|
)
|
||||||
|
|
||||||
// Returns the length of an ICMP message.
|
// Returns the length of an ICMP message.
|
||||||
func (p *Pinger) getMessageLength() int {
|
func (p *Pinger) getMessageLength() int {
|
||||||
return p.Size + 8
|
if p.Privileged() {
|
||||||
|
return p.Size + 8
|
||||||
|
}
|
||||||
|
if p.ipv4 {
|
||||||
|
return p.Size + 8 + ipv4.HeaderLen
|
||||||
|
}
|
||||||
|
return p.Size + 8 + ipv6.HeaderLen
|
||||||
}
|
}
|
||||||
|
|
||||||
// Attempts to match the ID of an ICMP packet.
|
// Attempts to match the ID of an ICMP packet.
|
||||||
|
@ -3,9 +3,20 @@
|
|||||||
|
|
||||||
package ping
|
package ping
|
||||||
|
|
||||||
|
import (
|
||||||
|
"golang.org/x/net/ipv4"
|
||||||
|
"golang.org/x/net/ipv6"
|
||||||
|
)
|
||||||
|
|
||||||
// Returns the length of an ICMP message.
|
// Returns the length of an ICMP message.
|
||||||
func (p *Pinger) getMessageLength() int {
|
func (p *Pinger) getMessageLength() int {
|
||||||
return p.Size + 8
|
if p.Privileged() {
|
||||||
|
return p.Size + 8
|
||||||
|
}
|
||||||
|
if p.ipv4 {
|
||||||
|
return p.Size + 8 + ipv4.HeaderLen
|
||||||
|
}
|
||||||
|
return p.Size + 8 + ipv6.HeaderLen
|
||||||
}
|
}
|
||||||
|
|
||||||
// Attempts to match the ID of an ICMP packet.
|
// Attempts to match the ID of an ICMP packet.
|
||||||
|
Loading…
Reference in New Issue
Block a user