mirror of
https://github.com/oneclickvirt/backtrace.git
synced 2025-04-21 03:32:07 +08:00
36 lines
610 B
Go
36 lines
610 B
Go
package backtrace
|
|
|
|
import (
|
|
"net"
|
|
|
|
"golang.org/x/net/icmp"
|
|
"golang.org/x/net/ipv4"
|
|
)
|
|
|
|
func newPacketV4(id uint16, dst net.IP, ttl int) []byte {
|
|
// TODO: reuse buffers...
|
|
msg := icmp.Message{
|
|
Type: ipv4.ICMPTypeEcho,
|
|
Body: &icmp.Echo{
|
|
ID: int(id),
|
|
Seq: int(id),
|
|
},
|
|
}
|
|
p, _ := msg.Marshal(nil)
|
|
ip := &ipv4.Header{
|
|
Version: ipv4.Version,
|
|
Len: ipv4.HeaderLen,
|
|
TotalLen: ipv4.HeaderLen + len(p),
|
|
TOS: 16,
|
|
ID: int(id),
|
|
Dst: dst,
|
|
Protocol: ProtocolICMP,
|
|
TTL: ttl,
|
|
}
|
|
buf, err := ip.Marshal()
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
return append(buf, p...)
|
|
}
|