Table of Contents

Class IcmpV4Packet

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

Represents an ICMPv4 message (RFC 792) over a buffer, which it reads and writes in place.

public class IcmpV4Packet : IChecksumPayloadPacket, IPayloadPacket
Inheritance
IcmpV4Packet
Implements
Inherited Members

Remarks

Changing a field does not update the checksum: call UpdateChecksum(ReadOnlySpan<byte>, ReadOnlySpan<byte>) before sending.

Constructors

IcmpV4Packet(Memory<byte>, bool)

Initializes a new instance of the IcmpV4Packet class over a buffer, to parse a message or to build one.

public IcmpV4Packet(Memory<byte> buffer, bool building)

Parameters

buffer Memory<byte>

The message's bytes, header and payload, from 8 to 65535 bytes.

building bool

true to build: the 8-byte header is cleared and the payload is left as it is. false to parse the bytes as they are.

Exceptions

ArgumentException

buffer is shorter than 8 bytes or longer than 65535.

Properties

Buffer

Gets the packet's bytes, header and payload. When the packet is an IP packet's payload, they are that packet's own bytes, not a copy, and live only as long as its memory.

public Memory<byte> Buffer { get; }

Property Value

Memory<byte>

Checksum

Gets or sets the checksum field as it is in the header.

public ushort Checksum { get; set; }

Property Value

ushort

Code

Gets or sets the message code, whose meaning depends on Type; see IcmpV4Code.

public byte Code { get; set; }

Property Value

byte

Identifier

Gets or sets the identifier of an echo request or reply.

public ushort Identifier { get; set; }

Property Value

ushort

Remarks

For another type, the getter returns 0.

Exceptions

InvalidOperationException

Set on a message that is not an echo request or reply.

IsEcho

Gets a value indicating whether the message is an echo request or an echo reply.

public bool IsEcho { get; }

Property Value

bool

MessageSpecific

Gets or sets the 4 bytes after the checksum, whose meaning depends on Type: an echo's identifier and sequence number, or the next-hop MTU of a "fragmentation needed" message, in its low 16 bits.

public uint MessageSpecific { get; set; }

Property Value

uint

Payload

Gets the message's payload: the bytes after the 8-byte header, such as an echo's data or the start of the packet an error is about.

public Memory<byte> Payload { get; }

Property Value

Memory<byte>

SequenceNumber

Gets or sets the sequence number of an echo request or reply.

public ushort SequenceNumber { get; set; }

Property Value

ushort

Remarks

For another type, the getter returns 0.

Exceptions

InvalidOperationException

Set on a message that is not an echo request or reply.

Type

Gets or sets the message type.

public IcmpV4Type Type { get; set; }

Property Value

IcmpV4Type

Methods

ComputeChecksum(ReadOnlySpan<byte>, ReadOnlySpan<byte>)

Computes the checksum the message calls for, without writing it.

public ushort ComputeChecksum(ReadOnlySpan<byte> sourceAddress, ReadOnlySpan<byte> destinationAddress)

Parameters

sourceAddress ReadOnlySpan<byte>

Ignored: ICMPv4 sums no pseudo-header.

destinationAddress ReadOnlySpan<byte>

Ignored: ICMPv4 sums no pseudo-header.

Returns

ushort

The checksum, as a number to write in network byte order.

Remarks

The checksum field is zero while the sum runs and is restored after, so no other thread may use the packet meanwhile.

IsChecksumValid(ReadOnlySpan<byte>, ReadOnlySpan<byte>)

Checks the checksum field against the message.

public bool IsChecksumValid(ReadOnlySpan<byte> sourceAddress, ReadOnlySpan<byte> destinationAddress)

Parameters

sourceAddress ReadOnlySpan<byte>

Ignored: ICMPv4 sums no pseudo-header.

destinationAddress ReadOnlySpan<byte>

Ignored: ICMPv4 sums no pseudo-header.

Returns

bool

true if the checksum is valid; otherwise, false.

ToString()

Returns a one-line description of the message for logs: its type, code and payload length, with an echo's identifier and sequence number.

public override string ToString()

Returns

string

The description.

UpdateChecksum(ReadOnlySpan<byte>, ReadOnlySpan<byte>)

Computes the checksum and writes it to Checksum.

public void UpdateChecksum(ReadOnlySpan<byte> sourceAddress, ReadOnlySpan<byte> destinationAddress)

Parameters

sourceAddress ReadOnlySpan<byte>

Ignored: ICMPv4 sums no pseudo-header.

destinationAddress ReadOnlySpan<byte>

Ignored: ICMPv4 sums no pseudo-header.