package icmp import ( "bytes" "encoding/binary" "net" "time" "golang.org/x/net/icmp" ) func netAddrToIPAddr(a net.Addr) *net.IPAddr { switch v := a.(type) { case *net.UDPAddr: if ip := v.IP.To4(); ip != nil { return ip } case *net.IPAddr: if ip := v.IP.To4(); ip != nil { return ip } } return nil } func BuildEchoRequestMessage(id, seq, size int, icmptype Type) ([]byte, error) { if size < 24 { size = 24 } bs := bytes.Repeat([]byte{1}, size) binary.BigEndian.PutUint64(bs, uint64(time.Now().UnixNano())) binary.BigEndian.PutUint64(bs[8:], uint64(seq)) binary.BigEndian.PutUint64(bs[16:], uint64(id)) body := &icmp.Echo{ ID: id % 65535, Seq: seq % 65536, Data: bs, } msg := &icmp.Message{ Type: icmptype, Code: 0, Body: body, } msgBytes, err := msg.Marshal(nil) if err != nil { return nil, err } return msgBytes, nil }