Table of Contents

Class UdpPacket

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

Represents a UDP datagram (RFC 768) over a buffer, which it reads and writes in place.

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

Remarks

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

Constructors

UdpPacket(Memory<byte>, bool)

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

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

Parameters

buffer Memory<byte>

The datagram's bytes, header and payload, exactly as long as the datagram: from 8 to 65535 bytes.

building bool

true to build: the 8-byte header is cleared and its length field set to the buffer's length, and the payload is left as it is. false to parse: the length field must match the buffer's length.

Exceptions

ArgumentException

buffer is shorter than 8 bytes or longer than 65535, or, when parsing, its length differs from the length field.

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. Zero means the sender computed none, which only IPv4 allows.

public ushort Checksum { get; set; }

Property Value

ushort

DestinationPort

Gets or sets the destination port.

public ushort DestinationPort { get; set; }

Property Value

ushort

Payload

Gets the datagram's payload: the bytes after the 8-byte header.

public Memory<byte> Payload { get; }

Property Value

Memory<byte>

SourcePort

Gets or sets the source port.

public ushort SourcePort { get; set; }

Property Value

ushort

Methods

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

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

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

Parameters

sourceAddress ReadOnlySpan<byte>

The source address of the IP packet, 4 or 16 bytes.

destinationAddress ReadOnlySpan<byte>

The destination address of the IP packet, of the same length.

Returns

ushort

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

Remarks

A computed zero is returned as 0xFFFF, since a zero field means no checksum (RFC 768). The checksum field is zero while the sum runs and is restored after, so no other thread may use the packet meanwhile.

Exceptions

ArgumentException

The addresses are not both 4 or both 16 bytes long.

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

Checks the checksum field against the datagram and the IP pseudo-header.

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

Parameters

sourceAddress ReadOnlySpan<byte>

The source address of the IP packet, 4 or 16 bytes.

destinationAddress ReadOnlySpan<byte>

The destination address of the IP packet, of the same length.

Returns

bool

true if the checksum is valid, or is zero over IPv4, where zero means none was computed (RFC 768); otherwise, false. Over IPv6 a zero checksum is invalid (RFC 8200, section 8.1).

Exceptions

ArgumentException

The checksum is not zero and the addresses are not both 4 or both 16 bytes long.

ToString()

Returns a one-line description of the datagram for logs: its ports and payload length.

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>

The source address of the IP packet, 4 or 16 bytes.

destinationAddress ReadOnlySpan<byte>

The destination address of the IP packet, of the same length.

Exceptions

ArgumentException

The addresses are not both 4 or both 16 bytes long.