golang.org/x/net@v0.25.1-0.20240516223405-c87a5b62e243/icmp/timeexceeded.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 (
     8  	"golang.org/x/net/internal/iana"
     9  	"golang.org/x/net/ipv4"
    10  	"golang.org/x/net/ipv6"
    11  )
    12  
    13  // A TimeExceeded represents an ICMP time exceeded message body.
    14  type TimeExceeded struct {
    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 *TimeExceeded) Len(proto int) int {
    21  	if p == nil {
    22  		return 0
    23  	}
    24  	l, _ := multipartMessageBodyDataLen(proto, true, p.Data, p.Extensions)
    25  	return l
    26  }
    27  
    28  // Marshal implements the Marshal method of MessageBody interface.
    29  func (p *TimeExceeded) Marshal(proto int) ([]byte, error) {
    30  	var typ Type
    31  	switch proto {
    32  	case iana.ProtocolICMP:
    33  		typ = ipv4.ICMPTypeTimeExceeded
    34  	case iana.ProtocolIPv6ICMP:
    35  		typ = ipv6.ICMPTypeTimeExceeded
    36  	default:
    37  		return nil, errInvalidProtocol
    38  	}
    39  	if !validExtensions(typ, p.Extensions) {
    40  		return nil, errInvalidExtension
    41  	}
    42  	return marshalMultipartMessageBody(proto, true, p.Data, p.Extensions)
    43  }
    44  
    45  // parseTimeExceeded parses b as an ICMP time exceeded message body.
    46  func parseTimeExceeded(proto int, typ Type, b []byte) (MessageBody, error) {
    47  	if len(b) < 4 {
    48  		return nil, errMessageTooShort
    49  	}
    50  	p := &TimeExceeded{}
    51  	var err error
    52  	p.Data, p.Extensions, err = parseMultipartMessageBody(proto, typ, b)
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  	return p, nil
    57  }