public IPHeader(ArraySegment<byte> buffer)
{
using (var memoryStream = new MemoryStream(buffer.Array, buffer.Offset, buffer.Count))
{
using (var binaryReader = new BinaryReader(memoryStream))
{
var versionAndHeaderLength = binaryReader.ReadByte();
var differentiatedServices = binaryReader.ReadByte();
DifferentiatedServices = (byte)(differentiatedServices >> 2);
CongestionNotification = (byte)(differentiatedServices & 0x03);
TotalLength = (ushort) IPAddress.NetworkToHostOrder(binaryReader.ReadInt16());
Debug.Assert(TotalLength >= 20, "Invalid IP packet Total Lenght");
Identification = (ushort) IPAddress.NetworkToHostOrder(binaryReader.ReadInt16());
_flagsAndOffset = (ushort)IPAddress.NetworkToHostOrder(binaryReader.ReadInt16());
Ttl = binaryReader.ReadByte();
_protocol = binaryReader.ReadByte();
Checksum = IPAddress.NetworkToHostOrder(binaryReader.ReadInt16());
SourceAddress = new IPAddress(binaryReader.ReadUInt32());
DestinationAddress = new IPAddress(binaryReader.ReadUInt32());
HeaderLength = (versionAndHeaderLength & 0x0f) * 4;
}
}
Raw = buffer;
Data = new ArraySegment<byte>(buffer.Array, buffer.Offset + HeaderLength, MessageLength);
}