mirror of
https://github.com/tailscale/tailscale.git
synced 2025-08-13 22:47:30 +00:00
wgengine, net/packet, cmd/tailscale: add ICMP echo
Updates tailscale/corp#754 Signed-off-by: James Tucker <james@tailscale.com>
This commit is contained in:

committed by
James Tucker

parent
66f9292835
commit
ae483d3446
29
net/packet/icmp.go
Normal file
29
net/packet/icmp.go
Normal file
@@ -0,0 +1,29 @@
|
||||
// Copyright (c) 2022 Tailscale Inc & AUTHORS All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package packet
|
||||
|
||||
import (
|
||||
crand "crypto/rand"
|
||||
|
||||
"encoding/binary"
|
||||
)
|
||||
|
||||
// ICMPEchoPayload generates a new random ID/Sequence pair, and returns a uint32
|
||||
// derived from them, along with the id, sequence and given payload in a buffer.
|
||||
// It returns an error if the random source could not be read.
|
||||
func ICMPEchoPayload(payload []byte) (idSeq uint32, buf []byte) {
|
||||
buf = make([]byte, len(payload)+4)
|
||||
|
||||
// make a completely random id/sequence combo, which is very unlikely to
|
||||
// collide with a running ping sequence on the host system. Errors are
|
||||
// ignored, that would result in collisions, but errors reading from the
|
||||
// random device are rare, and will cause this process universe to soon end.
|
||||
crand.Read(buf[:4])
|
||||
|
||||
idSeq = binary.LittleEndian.Uint32(buf)
|
||||
copy(buf[4:], payload)
|
||||
|
||||
return
|
||||
}
|
Reference in New Issue
Block a user