2017-03-22 18:09:21 +01:00
|
|
|
// Copyright 2012 The Go 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 icmp
|
|
|
|
|
|
|
|
// A MessageBody represents an ICMP message body.
|
|
|
|
type MessageBody interface {
|
|
|
|
// Len returns the length of ICMP message body.
|
2019-03-17 20:19:56 +01:00
|
|
|
// The provided proto must be either the ICMPv4 or ICMPv6
|
|
|
|
// protocol number.
|
2017-03-22 18:09:21 +01:00
|
|
|
Len(proto int) int
|
|
|
|
|
|
|
|
// Marshal returns the binary encoding of ICMP message body.
|
2019-03-17 20:19:56 +01:00
|
|
|
// The provided proto must be either the ICMPv4 or ICMPv6
|
|
|
|
// protocol number.
|
2017-03-22 18:09:21 +01:00
|
|
|
Marshal(proto int) ([]byte, error)
|
|
|
|
}
|
|
|
|
|
2019-03-17 20:19:56 +01:00
|
|
|
// A RawBody represents a raw message body.
|
|
|
|
//
|
|
|
|
// A raw message body is excluded from message processing and can be
|
|
|
|
// used to construct applications such as protocol conformance
|
|
|
|
// testing.
|
|
|
|
type RawBody struct {
|
2017-03-22 18:09:21 +01:00
|
|
|
Data []byte // data
|
|
|
|
}
|
|
|
|
|
|
|
|
// Len implements the Len method of MessageBody interface.
|
2019-03-17 20:19:56 +01:00
|
|
|
func (p *RawBody) Len(proto int) int {
|
2017-03-22 18:09:21 +01:00
|
|
|
if p == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return len(p.Data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Marshal implements the Marshal method of MessageBody interface.
|
2019-03-17 20:19:56 +01:00
|
|
|
func (p *RawBody) Marshal(proto int) ([]byte, error) {
|
2017-03-22 18:09:21 +01:00
|
|
|
return p.Data, nil
|
|
|
|
}
|
|
|
|
|
2019-03-17 20:19:56 +01:00
|
|
|
// parseRawBody parses b as an ICMP message body.
|
|
|
|
func parseRawBody(proto int, b []byte) (MessageBody, error) {
|
|
|
|
p := &RawBody{Data: make([]byte, len(b))}
|
2017-03-22 18:09:21 +01:00
|
|
|
copy(p.Data, b)
|
|
|
|
return p, nil
|
|
|
|
}
|
2019-03-17 20:19:56 +01:00
|
|
|
|
|
|
|
// A DefaultMessageBody represents the default message body.
|
|
|
|
//
|
|
|
|
// Deprecated: Use RawBody instead.
|
|
|
|
type DefaultMessageBody = RawBody
|