blob: 2463730ae839c860f138473fbe6ee65bececcc99 [file] [log] [blame]
David K. Bainbridge215e0242017-09-05 23:18:24 -07001// 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
5package icmp
6
7// A MessageBody represents an ICMP message body.
8type 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.
19type DefaultMessageBody struct {
20 Data []byte // data
21}
22
23// Len implements the Len method of MessageBody interface.
24func (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.
32func (p *DefaultMessageBody) Marshal(proto int) ([]byte, error) {
33 return p.Data, nil
34}
35
36// parseDefaultMessageBody parses b as an ICMP message body.
37func 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}