github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/net/icmp/packettoobig.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  // A PacketTooBig represents an ICMP packet too big message body.
     8  type PacketTooBig struct {
     9  	MTU  int    // maximum transmission unit of the nexthop link
    10  	Data []byte // data, known as original datagram field
    11  }
    12  
    13  // Len implements the Len method of MessageBody interface.
    14  func (p *PacketTooBig) Len(proto int) int {
    15  	if p == nil {
    16  		return 0
    17  	}
    18  	return 4 + len(p.Data)
    19  }
    20  
    21  // Marshal implements the Marshal method of MessageBody interface.
    22  func (p *PacketTooBig) Marshal(proto int) ([]byte, error) {
    23  	b := make([]byte, 4+len(p.Data))
    24  	b[0], b[1], b[2], b[3] = byte(p.MTU>>24), byte(p.MTU>>16), byte(p.MTU>>8), byte(p.MTU)
    25  	copy(b[4:], p.Data)
    26  	return b, nil
    27  }
    28  
    29  // parsePacketTooBig parses b as an ICMP packet too big message body.
    30  func parsePacketTooBig(proto int, b []byte) (MessageBody, error) {
    31  	bodyLen := len(b)
    32  	if bodyLen < 4 {
    33  		return nil, errMessageTooShort
    34  	}
    35  	p := &PacketTooBig{MTU: int(b[0])<<24 | int(b[1])<<16 | int(b[2])<<8 | int(b[3])}
    36  	if bodyLen > 4 {
    37  		p.Data = make([]byte, bodyLen-4)
    38  		copy(p.Data, b[4:])
    39  	}
    40  	return p, nil
    41  }