C# Class Tx.Network.PacketParser

Class that provides methods for parsing of IP packets.
For many of the operations in this class a set of bits less than one (1) byte is needed. Bitwise operators and masks are needed to extract those bit values (0 or 1) from a byte. All data from the network is received as Big-Endian.Windows is Little-Endian. You can validate this with BitConverter.IsLittleEndian Any sequence of bytes greater than 1 from the network has the most-significant byte at the LAST (aka End) position in the sequence. Network bytes are ALWAYS Big-Endian. To do the "right thing" in Windows'-little-endian-world: - A SINGULAR byte does not require any changes - A CHARACTER array (aka string) or other Byte-Array requires no changes -- ASCII works this way. -- Other encodings may vary so look up the rules. - Any NUMBER comprised of more than 1 byte from the Network is Big-Endian -- Therefore to make it work in Windows Network-to-Host-Order must be performed. -- All numbers are even multiples of a byte ( greater than 1) -- For example: | Network bytes will appear as [Ox80, 0x56] == 32854 (if cast to Int16 this will be a negative number) | NTHO in Windows will provide[0x56, 0x80] == 22144 Please refer to the usage of -System.Net.IPAddress.NetworkToHostOrder(). -- NOTE this operates on SIGNED Integers(16,32,64) only. -- All Internet header numbers are UNSIGNED Integers(16,32,64) so this makes no sense to me. - System.BitConverter.ToInt16() or .ToInt32() Typically the implementation would look like this: - (ushort)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(buffer, 2)); - At zero-based-index 2 for (byte[])buffer of arbitrary size > 5 bytes - Return type is SHORT so a cast to USHORT is required while inside Internet Headers. !!! !WARNING: the Bytes in an IP-Address (v4 or v6) are READ as a Byte-Array and not a Number! !NToH Should not be used on Addresses! !!!
Datei anzeigen Open project: Reactive-Extensions/Tx Class Usage Examples

Public Methods

Method Description
Parse ( ArraySegment data ) : IpPacket

Parses the specified binary data.

Parse ( DateTimeOffset receivedTime, bool reuseOriginalBuffer, byte packetBytes, int offset, int packetBytesLength ) : IpPacket

Parses the specified binary data.

Method Details

Parse() public static method

Parses the specified binary data.
if data is empty.
public static Parse ( ArraySegment data ) : IpPacket
data ArraySegment The data.
return IpPacket

Parse() public static method

Parses the specified binary data.
packetBytes is empty. IPv4 only currently supported.
public static Parse ( DateTimeOffset receivedTime, bool reuseOriginalBuffer, byte packetBytes, int offset, int packetBytesLength ) : IpPacket
receivedTime DateTimeOffset The received time.
reuseOriginalBuffer bool if set to true then reuse original buffer.
packetBytes byte The binary data.
offset int The offset in the binary data.
packetBytesLength int Length of the packet bytes.
return IpPacket