Skip to content
This repository has been archived by the owner on Nov 5, 2021. It is now read-only.

Commit

Permalink
For OS X, compute checksum for IPv4 SOCK_DGRAM sockets as well.
Browse files Browse the repository at this point in the history
Darwin kernel never seems to compute checksum for IPv4.

Fixes: #537
PiperOrigin-RevId: 348598094
  • Loading branch information
manugarg committed Dec 22, 2020
1 parent ef2d286 commit 9063cc7
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions probes/ping/pingutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package ping
import (
"encoding/binary"
"net"
"runtime"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -101,11 +102,14 @@ func (p *Probe) prepareRequestPacket(pktbuf []byte, runID, seq uint16, unixNano
// Fill payload with the bytes corresponding to current time.
prepareRequestPayload(pktbuf[8:], unixNano)

// For IPv6 and datagram sockets ,checksum is computed by the kernel.
if p.ipVer != 6 && !p.useDatagramSocket {
csum := checksum(pktbuf)
pktbuf[2] ^= byte(csum)
pktbuf[3] ^= byte(csum >> 8)
// For IPv6 checksum is always computed by the kernel.
// For IPv4, we compute checksum only if using RAW socket or OS is darwin.
if p.ipVer == 4 {
if !p.useDatagramSocket || runtime.GOOS == "darwin" {
csum := checksum(pktbuf)
pktbuf[2] ^= byte(csum)
pktbuf[3] ^= byte(csum >> 8)
}
}
}

Expand Down

0 comments on commit 9063cc7

Please sign in to comment.