David K. Bainbridge | 215e024 | 2017-09-05 23:18:24 -0700 | [diff] [blame] | 1 | // Copyright 2014 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 | import ( |
| 8 | "encoding/binary" |
| 9 | "golang.org/x/net/internal/iana" |
| 10 | ) |
| 11 | |
| 12 | // A ParamProb represents an ICMP parameter problem message body. |
| 13 | type ParamProb struct { |
| 14 | Pointer uintptr // offset within the data where the error was detected |
| 15 | Data []byte // data, known as original datagram field |
| 16 | Extensions []Extension // extensions |
| 17 | } |
| 18 | |
| 19 | // Len implements the Len method of MessageBody interface. |
| 20 | func (p *ParamProb) Len(proto int) int { |
| 21 | if p == nil { |
| 22 | return 0 |
| 23 | } |
| 24 | l, _ := multipartMessageBodyDataLen(proto, p.Data, p.Extensions) |
| 25 | return 4 + l |
| 26 | } |
| 27 | |
| 28 | // Marshal implements the Marshal method of MessageBody interface. |
| 29 | func (p *ParamProb) Marshal(proto int) ([]byte, error) { |
| 30 | if proto == iana.ProtocolIPv6ICMP { |
| 31 | b := make([]byte, p.Len(proto)) |
| 32 | binary.BigEndian.PutUint32(b[:4], uint32(p.Pointer)) |
| 33 | copy(b[4:], p.Data) |
| 34 | return b, nil |
| 35 | } |
| 36 | b, err := marshalMultipartMessageBody(proto, p.Data, p.Extensions) |
| 37 | if err != nil { |
| 38 | return nil, err |
| 39 | } |
| 40 | b[0] = byte(p.Pointer) |
| 41 | return b, nil |
| 42 | } |
| 43 | |
| 44 | // parseParamProb parses b as an ICMP parameter problem message body. |
| 45 | func parseParamProb(proto int, b []byte) (MessageBody, error) { |
| 46 | if len(b) < 4 { |
| 47 | return nil, errMessageTooShort |
| 48 | } |
| 49 | p := &ParamProb{} |
| 50 | if proto == iana.ProtocolIPv6ICMP { |
| 51 | p.Pointer = uintptr(binary.BigEndian.Uint32(b[:4])) |
| 52 | p.Data = make([]byte, len(b)-4) |
| 53 | copy(p.Data, b[4:]) |
| 54 | return p, nil |
| 55 | } |
| 56 | p.Pointer = uintptr(b[0]) |
| 57 | var err error |
| 58 | p.Data, p.Extensions, err = parseMultipartMessageBody(proto, b) |
| 59 | if err != nil { |
| 60 | return nil, err |
| 61 | } |
| 62 | return p, nil |
| 63 | } |