Skip to content

Commit

Permalink
Don't separate ping response header from received bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
sparrc committed Jun 13, 2019
1 parent e33cfb8 commit 4e5b655
Showing 1 changed file with 2 additions and 20 deletions.
22 changes: 2 additions & 20 deletions ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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++ {
Expand Down

0 comments on commit 4e5b655

Please sign in to comment.