Table of Contents

Class IpV6Packet

Namespace
VpnHood.Net.Packets
Assembly
VpnHood.Net.Packets.dll

Represents an IPv6 packet (RFC 8200) over a buffer, which it reads and writes in place.

public class IpV6Packet : IpPacket, IDisposable
Inheritance
IpV6Packet
Implements
Inherited Members

Remarks

The PacketMemory a packet is made on says whether the packet owns its memory.

A parsed packet reads through its Hop-by-Hop Options, Destination Options, Fragment and Authentication headers to the upper layer, as Linux does. It stops at a Routing header and at a header that runs past the packet: then Protocol names that header and HasUnreadExtensionHeader is true. ESP, Mobility, HIP and Shim6 end the chain as protocols of their own: Protocol names them, and HasUnreadExtensionHeader is false. A fragment other than the first stops after its Fragment header, so its Payload holds fragment data. Header covers the headers read through, and IsFragment counts only the Fragment headers among them.

Constructors

IpV6Packet(PacketMemory)

Initializes a new instance of the IpV6Packet class by parsing the packet at the start of some memory.

public IpV6Packet(PacketMemory memory)

Parameters

memory PacketMemory

The memory, which holds the packet from its first byte and may be longer: a Memory<T> or an array the caller keeps, or memory given to the packet (see PacketMemory), which stays the caller's when parsing fails.

Remarks

The packet takes the memory's first 40 + payload-length bytes and leaves the rest.

Exceptions

ArgumentException

The bytes are no IPv6 packet, or the memory is shorter than the packet.

IndexOutOfRangeException

memory is empty.

IpV6Packet(PacketMemory, int, IpProtocol)

Initializes a new instance of the IpV6Packet class by building a new packet at the start of some memory.

public IpV6Packet(PacketMemory memory, int packetLength, IpProtocol protocol)

Parameters

memory PacketMemory

The memory, at least packetLength bytes: a Memory<T> or an array the caller keeps, or memory given to the packet (see PacketMemory), which stays the caller's when building fails.

packetLength int

The packet's total length in bytes, from 40 to 65575.

protocol IpProtocol

The Next Header: the protocol that follows the fixed header.

Remarks

The packet's bytes are cleared, then the version, the Next Header and the payload length are written. The addresses and the hop limit are left zero. A build that is refused writes nothing.

Exceptions

ArgumentException

packetLength is below 40 or above 65575.

ArgumentOutOfRangeException

packetLength is negative or exceeds the memory.

Properties

FlowLabel

Gets or sets the 20-bit flow label, from 0 to 1048575.

public int FlowLabel { get; set; }

Property Value

int

Exceptions

ArgumentOutOfRangeException

The value is negative or above 1048575.

HasUnreadExtensionHeader

Gets a value indicating whether reading stopped at an extension header, which Protocol then names instead of the upper layer.

public bool HasUnreadExtensionHeader { get; }

Property Value

bool

Header

Gets the fixed header and the extension headers read through; 40 bytes for a built packet.

public override Memory<byte> Header { get; }

Property Value

Memory<byte>

HopLimit

Gets or sets the hop limit: the number of hops the packet may still take.

public byte HopLimit { get; set; }

Property Value

byte

IsFragment

Gets a value indicating whether the packet is a fragment: it has a Fragment header whose offset is not zero or whose More Fragments flag is set. A packet built with the constructor is never one.

public override bool IsFragment { get; }

Property Value

bool

NextHeader

Gets the Next Header field of the fixed header: the first extension header or the upper layer.

public IpProtocol NextHeader { get; }

Property Value

IpProtocol

Protocol

Gets the protocol of the upper layer the payload carries, such as TCP.

public override IpProtocol Protocol { get; }

Property Value

IpProtocol

Remarks

An IPv6 packet reads through its extension headers to the upper layer; when it stops at one it does not read through, it names that header instead (see HasUnreadExtensionHeader).

TimeToLive

Gets or sets the hop limit. The same as HopLimit.

public override byte TimeToLive { get; set; }

Property Value

byte

TrafficClass

Gets or sets the traffic class byte: the Differentiated Services Code Point in its high 6 bits and ECN in its low 2.

public byte TrafficClass { get; set; }

Property Value

byte

Methods

GetPacketLength(ReadOnlySpan<byte>)

Reads an IPv6 packet's total length from its header, without parsing the rest.

public static int GetPacketLength(ReadOnlySpan<byte> buffer)

Parameters

buffer ReadOnlySpan<byte>

The packet's first bytes, at least 6.

Returns

int

40 plus the payload length field: the packet's length in bytes.

Exceptions

ArgumentException

The bytes are no IPv6 packet, or fewer than 6.

IndexOutOfRangeException

buffer is empty.