<commit_before>// Copyright 2012 Google, Inc. All rights reserved.
// Copyright 2009-2011 <NAME>. All rights reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree.

package layers

import (
	"encoding/binary"
	"encoding/hex"
	"errors"
	"fmt"
	"net"
	"time"

	"github.com/google/gopacket"
)

// Based on RFC 4861

// ICMPv6Opt indicate how to decode the data associated with each ICMPv6Option.
type ICMPv6Opt uint8

const (
	_ ICMPv6Opt = iota

	// ICMPv6OptSourceAddress contains the link-layer address of the sender of
	// the packet. It is used in the Neighbor Solicitation, Router
	// Solicitation, and Router Advertisement packets. Must be ignored for other
	// Neighbor discovery messages.
	ICMPv6OptSourceAddress

	// ICMPv6OptTargetAddress contains the link-layer address of the target. It
	// is used in Neighbor Advertisement and Redirect packets. Must be ignored
	// for other Neighbor discovery messages.
	ICMPv6OptTargetAddress

	// ICMPv6OptPrefixInfo provides hosts with on-link prefixes and prefixes
	// for Address Autoconfiguration. The Prefix Information option appears in
	// Router Advertisement packets and MUST be silently ignored for other
	// messages.
	ICMPv6OptPrefixInfo

	// ICMPv6OptRedirectedHeader is used in Redirect messages and contains all
	// or part of the packet that is being redirected.
	ICMPv6OptRedirectedHeader

	// ICMPv6OptMTU is used in Router Advertisement messages to ensure that all
	// nodes on a link use the same MTU value in those cases where the link MTU
	// is not well known. This option MUST be silently ignored for other
	// Neighbor Discovery messages.
	ICMPv6OptMTU
)

// ICMPv6Echo represents the structure of a ping.
type ICMPv6Echo struct {
	BaseLayer
	Identifier uint16
	SeqNumber  uint16
}

// ICMPv6RouterSolicitation is sent by hosts to find routers.
type ICMPv6RouterSolicitation struct {
	BaseLayer
	Options ICMPv6Options
}

// ICMPv6RouterAdvertisement is sent by routers in response to Solicitation.
type ICMPv6RouterAdvertisement struct {
	BaseLayer
	HopLimit       uint8
	Flags          uint8
	RouterLifetime uint16
	ReachableTime  uint32
	RetransTimer   uint32
	Options        ICMPv6Options
}

// ICMPv6NeighborSolicitation is sent to request the link-layer address of a
// target node.
type ICMPv6NeighborSolicitation struct {
	BaseLayer
	TargetAddress net.IP
	Options       ICMPv6Options
}

// ICMPv6NeighborAdvertisement is sent by nodes in response to Solicitation.
type ICMPv6NeighborAdvertisement struct {
	BaseLayer
	Flags         uint8
	TargetAddress net.IP
	Options       ICMPv6Options
}

// ICMPv6Redirect is sent by routers to inform hosts of a better first-hop node
// on the path to a destination.
type ICMPv6Redirect struct {
	BaseLayer
	TargetAddress      net.IP
	DestinationAddress net.IP
	Options            ICMPv6Options
}

// ICMPv6Option contains the type and data for a single option.
type ICMPv6Option struct {
	Type ICMPv6Opt
	Data []byte
}

// ICMPv6Options is a slice of ICMPv6Option.
type ICMPv6Options []ICMPv6Option

func (i ICMPv6Opt) String() string {
	switch i {
	case ICMPv6OptSourceAddress:
		return "SourceAddress"
	case ICMPv6OptTargetAddress:
		return "TargetAddress"
	case ICMPv6OptPrefixInfo:
		return "PrefixInfo"
	case ICMPv6OptRedirectedHeader:
		return "RedirectedHeader"
	case ICMPv6OptMTU:
		return "MTU"
	default:
		return fmt.Sprintf("Unknown(%d)", i)
	}
}

// LayerType returns LayerTypeICMPv6Echo.<commit_msg>Add missing CanDecode to ICMPv6Echo. This fixes #600<commit_after>// Copyright 2012 Google, Inc. All rights reserved.
// Copyright 2009-2011 <NAME>. All rights reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree.

package layers

import (
	"encoding/binary"
	"encoding/hex"
	"errors"
	"fmt"
	"net"
	"time"

	"github.com/google/gopacket"
)

// Based on RFC 4861

// ICMPv6Opt indicate how to decode the data associated with each ICMPv6Option.
type ICMPv6Opt uint8

const (
	_ ICMPv6Opt = iota

	// ICMPv6OptSourceAddress contains the link-layer address of the sender of
	// the packet. It is used in the Neighbor Solicitation, Router
	// Solicitation, and Router Advertisement packets. Must be ignored for other
	// Neighbor discovery messages.
	ICMPv6OptSourceAddress

	// ICMPv6OptTargetAddress contains the link-layer address of the target. It
	// is used in Neighbor Advertisement and Redirect packets. Must be ignored
	// for other Neighbor discovery messages.
	ICMPv6OptTargetAddress

	// ICMPv6OptPrefixInfo provides hosts with on-link prefixes and prefixes
	// for Address Autoconfiguration. The Prefix Information option appears in
	// Router Advertisement packets and MUST be silently ignored for other
	// messages.
	ICMPv6OptPrefixInfo

	// ICMPv6OptRedirectedHeader is used in Redirect messages and contains all
	// or part of the packet that is being redirected.
	ICMPv6OptRedirectedHeader

	// ICMPv6OptMTU is used in Router Advertisement messages to ensure that all
	// nodes on a link use the same MTU value in those cases where the link MTU
	// is not well known. This option MUST be silently ignored for other
	// Neighbor Discovery messages.
	ICMPv6OptMTU
)

// ICMPv6Echo represents the structure of a ping.
type ICMPv6Echo struct {
	BaseLayer
	Identifier uint16
	SeqNumber  uint16
}

// ICMPv6RouterSolicitation is sent by hosts to find routers.
type ICMPv6RouterSolicitation struct {
	BaseLayer
	Options ICMPv6Options
}

// ICMPv6RouterAdvertisement is sent by routers in response to Solicitation.
type ICMPv6RouterAdvertisement struct {
	BaseLayer
	HopLimit       uint8
	Flags          uint8
	RouterLifetime uint16
	ReachableTime  uint32
	RetransTimer   uint32
	Options        ICMPv6Options
}

// ICMPv6NeighborSolicitation is sent to request the link-layer address of a
// target node.
type ICMPv6NeighborSolicitation struct {
	BaseLayer
	TargetAddress net.IP
	Options       ICMPv6Options
}

// ICMPv6NeighborAdvertisement is sent by nodes in response to Solicitation.
type ICMPv6NeighborAdvertisement struct {
	BaseLayer
	Flags         uint8
	TargetAddress net.IP
	Options       ICMPv6Options
}

// ICMPv6Redirect is sent by routers to inform hosts of a better first-hop node
// on the path to a destination.
type ICMPv6Redirect struct {
	BaseLayer
	TargetAddress      net.IP
	DestinationAddress net.IP
	Options            ICMPv6Options
}

// ICMPv6Option contains the type and data for a single option.
type ICMPv6Option struct {
	Type ICMPv6Opt
	Data []byte
}

// ICMPv6Options is a slice of ICMPv6Option.
type ICMPv6Options []ICMPv6Option

func (i ICMPv6Opt) String() string {
	switch i {
	case ICMPv6OptSourceAddress:
		return "SourceAddress"
	case ICMPv6OptTargetAddress:
		return "TargetAddress"
	case ICMPv6OptPrefixInfo:
		return "PrefixInfo"
	case ICMPv6OptRedirectedHeader:
		return "RedirectedHeader"
	case ICMPv6OptMTU:
		return "MTU"
	default:
		return fmt.Sprintf("Unknown(%d)", i)
	}
}

// CanDecode returns the set of layer types that this DecodingLayer can decode.
func (i *ICMPv6Echo) CanDecode() gopacket.LayerClass {
	return LayerTypeICMPv6Echo
}

// LayerType returns LayerTypeICMPv6Echo.
{'fraction_non_alphanumeric': 0.0546907287505166, 'fraction_numerical': 0.02149056343849015, 'mean_word_length': 3.823920265780731, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '305946', 'n_tokens_mistral': 2377, 'n_tokens_neox': 2254, 'n_words': 831}