Class IpV4Packet
- Namespace
- VpnHood.Net.Packets
- Assembly
- VpnHood.Net.Packets.dll
Represents an IPv4 packet (RFC 791) over a buffer, which it reads and writes in place.
public class IpV4Packet : IpPacket, IDisposable
- Inheritance
-
IpV4Packet
- Implements
- Inherited Members
Remarks
The PacketMemory a packet is made on says whether the packet owns its memory. Changing a header field does not update the header checksum: call UpdateHeaderChecksum() or UpdateAllChecksums(IpPacket) before sending.
Constructors
IpV4Packet(PacketMemory)
Initializes a new instance of the IpV4Packet class by parsing the packet at the start of some memory.
public IpV4Packet(PacketMemory memory)
Parameters
memoryPacketMemoryThe 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 total-length bytes and leaves the rest. It checks the header's version and lengths, not its checksum.
Exceptions
- ArgumentException
The bytes are no IPv4 packet, their header length or total length is invalid, or the memory is shorter than the packet.
- IndexOutOfRangeException
memoryis empty.
IpV4Packet(PacketMemory, int, IpProtocol, int)
Initializes a new instance of the IpV4Packet class by building a new packet at the start of some memory.
public IpV4Packet(PacketMemory memory, int packetLength, IpProtocol protocol, int optionsLength)
Parameters
memoryPacketMemoryThe memory, at least
packetLengthbytes: 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.packetLengthintThe packet's total length in bytes, from 20 to 65535, and at least 20 plus
optionsLength.protocolIpProtocolThe protocol of the payload.
optionsLengthintThe length of the header options, a multiple of 4 from 0 to 40.
Remarks
The packet's bytes are cleared, then the version, the header length, the protocol and the total length are written. The addresses, the time to live and the checksum are left zero. A build that is refused writes nothing.
Exceptions
- ArgumentException
packetLengthis below 20, above 65535, or shorter than the header with its options.- ArgumentOutOfRangeException
optionsLengthis out of range or not a multiple of 4, orpacketLengthis negative or exceeds the memory.
Properties
DontFragment
Gets or sets a value indicating whether routers may not fragment the packet.
public bool DontFragment { get; set; }
Property Value
Dscp
Gets or sets the Differentiated Services Code Point, the high 6 bits of the type-of-service byte.
public byte Dscp { get; set; }
Property Value
Remarks
A value is masked to its low 6 bits.
Ecn
Gets or sets the Explicit Congestion Notification codepoint, the low 2 bits of the type-of-service byte.
public IpEcnField Ecn { get; set; }
Property Value
FragmentFlags
Gets or sets the 3 flag bits: reserved, Don't Fragment and More Fragments, from the highest.
public byte FragmentFlags { get; set; }
Property Value
FragmentOffset
Gets or sets the fragment's place in the datagram, in units of 8 bytes, from 0 to 8191.
public int FragmentOffset { get; set; }
Property Value
Remarks
A value is masked to its low 13 bits, so a larger one wraps.
Header
Gets the bytes before the upper layer: the IP header with its options, and IPv6's extension headers.
public override Memory<byte> Header { get; }
Property Value
HeaderChecksum
Gets or sets the header checksum field as it is in the header.
public ushort HeaderChecksum { get; set; }
Property Value
Identification
Gets or sets the identification, which the fragments of one datagram share.
public ushort Identification { get; set; }
Property Value
InternetHeaderLength
Gets the header length in 32-bit words, from 5 to 15.
public int InternetHeaderLength { get; }
Property Value
IsFragment
Gets a value indicating whether the packet is a fragment: More Fragments is set, or the fragment offset is not zero.
public override bool IsFragment { get; }
Property Value
MoreFragments
Gets or sets a value indicating whether more fragments of the datagram follow this one.
public bool MoreFragments { get; set; }
Property Value
Options
Gets the header options: the bytes between the 20-byte fixed header and the payload.
public Memory<byte> Options { get; }
Property Value
Protocol
Gets the protocol of the upper layer the payload carries, such as TCP.
public override IpProtocol Protocol { get; }
Property Value
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 time to live: the number of hops the packet may still take.
public override byte TimeToLive { get; set; }
Property Value
TypeOfService
public byte TypeOfService { get; set; }
Property Value
Methods
GetPacketLength(ReadOnlySpan<byte>)
Reads an IPv4 packet's total length from its header, without parsing the rest.
public static int GetPacketLength(ReadOnlySpan<byte> buffer)
Parameters
bufferReadOnlySpan<byte>The packet's first bytes, at least 4.
Returns
- int
The total length field: the packet's length in bytes, header included.
Exceptions
- ArgumentException
The bytes are no IPv4 packet, or fewer than 4.
- IndexOutOfRangeException
bufferis empty.
IsHeaderChecksumValid()
Checks the header checksum against the header.
public bool IsHeaderChecksumValid()
Returns
UpdateHeaderChecksum()
Computes the header checksum and writes it to HeaderChecksum.
public void UpdateHeaderChecksum()