github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/net/icmp/messagebody.go (about) 1 // Copyright 2012 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package icmp 6 7 // A MessageBody represents an ICMP message body. 8 type MessageBody interface { 9 // Len returns the length of ICMP message body. 10 // The provided proto must be either the ICMPv4 or ICMPv6 11 // protocol number. 12 Len(proto int) int 13 14 // Marshal returns the binary encoding of ICMP message body. 15 // The provided proto must be either the ICMPv4 or ICMPv6 16 // protocol number. 17 Marshal(proto int) ([]byte, error) 18 } 19 20 // A RawBody represents a raw message body. 21 // 22 // A raw message body is excluded from message processing and can be 23 // used to construct applications such as protocol conformance 24 // testing. 25 type RawBody struct { 26 Data []byte // data 27 } 28 29 // Len implements the Len method of MessageBody interface. 30 func (p *RawBody) Len(proto int) int { 31 if p == nil { 32 return 0 33 } 34 return len(p.Data) 35 } 36 37 // Marshal implements the Marshal method of MessageBody interface. 38 func (p *RawBody) Marshal(proto int) ([]byte, error) { 39 return p.Data, nil 40 } 41 42 // parseRawBody parses b as an ICMP message body. 43 func parseRawBody(proto int, b []byte) (MessageBody, error) { 44 p := &RawBody{Data: make([]byte, len(b))} 45 copy(p.Data, b) 46 return p, nil 47 } 48 49 // A DefaultMessageBody represents the default message body. 50 // 51 // Deprecated: Use RawBody instead. 52 type DefaultMessageBody = RawBody