mirror of
https://github.com/go-ping/ping.git
synced 2025-06-30 16:01:52 +00:00
Don't separate ping response header from received bytes
This commit is contained in:
parent
e33cfb8ae7
commit
4e5b655249
22
ping.go
22
ping.go
@ -441,23 +441,16 @@ func (p *Pinger) recvICMP(
|
|||||||
|
|
||||||
func (p *Pinger) processPacket(recv *packet) error {
|
func (p *Pinger) processPacket(recv *packet) error {
|
||||||
receivedAt := time.Now()
|
receivedAt := time.Now()
|
||||||
var bytes []byte
|
|
||||||
var proto int
|
var proto int
|
||||||
if p.ipv4 {
|
if p.ipv4 {
|
||||||
if p.network == "ip" {
|
|
||||||
bytes = ipv4Payload(recv)
|
|
||||||
} else {
|
|
||||||
bytes = recv.bytes
|
|
||||||
}
|
|
||||||
proto = protocolICMP
|
proto = protocolICMP
|
||||||
} else {
|
} else {
|
||||||
bytes = recv.bytes
|
|
||||||
proto = protocolIPv6ICMP
|
proto = protocolIPv6ICMP
|
||||||
}
|
}
|
||||||
|
|
||||||
var m *icmp.Message
|
var m *icmp.Message
|
||||||
var err error
|
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())
|
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) {
|
switch pkt := m.Body.(type) {
|
||||||
case *icmp.Echo:
|
case *icmp.Echo:
|
||||||
|
// If we are privileged, we can match icmp.ID
|
||||||
// If we are priviledged, we can match icmp.ID
|
|
||||||
if p.network == "ip" {
|
if p.network == "ip" {
|
||||||
// Check if reply from same ID
|
// Check if reply from same ID
|
||||||
if pkt.ID != p.id {
|
if pkt.ID != p.id {
|
||||||
@ -574,16 +566,6 @@ func (p *Pinger) listen(netProto string) *icmp.PacketConn {
|
|||||||
return conn
|
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 {
|
func bytesToTime(b []byte) time.Time {
|
||||||
var nsec int64
|
var nsec int64
|
||||||
for i := uint8(0); i < 8; i++ {
|
for i := uint8(0); i < 8; i++ {
|
||||||
|
Loading…
Reference in New Issue
Block a user