github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/net/icmp/paramprob.go (about)

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