Class PacketUtil
- Namespace
- VpnHood.Net.Packets
- Assembly
- VpnHood.Net.Packets.dll
Provides helpers to read an IP packet's length and to compute Internet checksums.
public static class PacketUtil
- Inheritance
-
PacketUtil
- Inherited Members
Methods
ComputeChecksum(ReadOnlySpan<byte>)
Computes the Internet checksum (RFC 1071) of some bytes. The same as ComputeChecksum(ReadOnlySpan<byte>).
public static ushort ComputeChecksum(ReadOnlySpan<byte> data)
Parameters
dataReadOnlySpan<byte>The bytes to sum, with any checksum field among them zero.
Returns
- ushort
The checksum, as a number to write in network byte order.
ComputeChecksum(ReadOnlySpan<byte>, ReadOnlySpan<byte>)
Computes the Internet checksum (RFC 1071) of a pseudo-header followed by some bytes. The same as ComputeChecksum(ReadOnlySpan<byte>, ReadOnlySpan<byte>).
public static ushort ComputeChecksum(ReadOnlySpan<byte> pseudoHeader, ReadOnlySpan<byte> data)
Parameters
pseudoHeaderReadOnlySpan<byte>The bytes summed first, such as an IP pseudo-header; may be empty.
dataReadOnlySpan<byte>The bytes summed after them, with any checksum field among them zero.
Returns
- ushort
The checksum, as a number to write in network byte order.
ComputeChecksum(ReadOnlySpan<byte>, ReadOnlySpan<byte>, byte, ReadOnlySpan<byte>)
Computes the checksum of a TCP, UDP or ICMPv6 packet: the Internet checksum of its IP pseudo-header and its bytes.
public static ushort ComputeChecksum(ReadOnlySpan<byte> sourceAddress, ReadOnlySpan<byte> destinationAddress, byte protocol, ReadOnlySpan<byte> data)
Parameters
sourceAddressReadOnlySpan<byte>The source address of the IP packet: 4 bytes for IPv4, 16 for IPv6.
destinationAddressReadOnlySpan<byte>The destination address of the IP packet, of the same length.
protocolbyteThe protocol number the pseudo-header carries, such as 6 for TCP.
dataReadOnlySpan<byte>The upper-layer packet, header and payload, with its checksum field zero.
Returns
- ushort
The checksum, as a number to write in network byte order.
Exceptions
- ArgumentException
The addresses are not both 4 or both 16 bytes long.
ReadPacketLength(ReadOnlySpan<byte>)
Reads an IP packet's total length from its header, without parsing the rest.
public static int ReadPacketLength(ReadOnlySpan<byte> buffer)
Parameters
bufferReadOnlySpan<byte>The packet's first bytes: at least 4 for IPv4 and 6 for IPv6.
Returns
- int
The packet's length in bytes, header included.
Exceptions
- ArgumentException
bufferis too short to hold the length.- IndexOutOfRangeException
bufferis empty.- NotSupportedException
The packet is neither IPv4 nor IPv6.