David K. Bainbridge | 215e024 | 2017-09-05 23:18:24 -0700 | [diff] [blame] | 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 | // Proto must be either the ICMPv4 or ICMPv6 protocol number. |
| 11 | Len(proto int) int |
| 12 | |
| 13 | // Marshal returns the binary encoding of ICMP message body. |
| 14 | // Proto must be either the ICMPv4 or ICMPv6 protocol number. |
| 15 | Marshal(proto int) ([]byte, error) |
| 16 | } |
| 17 | |
| 18 | // A DefaultMessageBody represents the default message body. |
| 19 | type DefaultMessageBody struct { |
| 20 | Data []byte // data |
| 21 | } |
| 22 | |
| 23 | // Len implements the Len method of MessageBody interface. |
| 24 | func (p *DefaultMessageBody) Len(proto int) int { |
| 25 | if p == nil { |
| 26 | return 0 |
| 27 | } |
| 28 | return len(p.Data) |
| 29 | } |
| 30 | |
| 31 | // Marshal implements the Marshal method of MessageBody interface. |
| 32 | func (p *DefaultMessageBody) Marshal(proto int) ([]byte, error) { |
| 33 | return p.Data, nil |
| 34 | } |
| 35 | |
| 36 | // parseDefaultMessageBody parses b as an ICMP message body. |
| 37 | func parseDefaultMessageBody(proto int, b []byte) (MessageBody, error) { |
| 38 | p := &DefaultMessageBody{Data: make([]byte, len(b))} |
| 39 | copy(p.Data, b) |
| 40 | return p, nil |
| 41 | } |