public IPHeader(byte[] byBuffer, int nReceived) { NetBinaryReader nbr = new NetBinaryReader(byBuffer, 0, nReceived); Version = nbr.ReadNible(); HeaderLength = nbr.ReadNible(); Precedence = (Precedence)nbr.ReadCustomAmount(3); LowDelay = nbr.ReadBit(); HighThroughput = nbr.ReadBit(); HighRelibility = nbr.ReadBit(); nbr.SkipPadBits(); TotalLength = nbr.ReadUInt16(); Identification = nbr.ReadUInt16(); nbr.ReadBit(); MayFragment = !nbr.ReadBit(); LastFragment = !nbr.ReadBit(); FragmentOffset = (nbr.ReadPadBits() << 3) + nbr.ReadByte(); TTL = nbr.ReadByte(); Protocol = (ProtocolType)nbr.ReadByte(); HeaderChecksum = IPAddress.NetworkToHostOrder(nbr.ReadInt16()); Source = new IPAddress(nbr.ReadBytes(4)); Destination = new IPAddress(nbr.ReadBytes(4)); }
public TcpHeader(byte[] byIpData, int start) { NetBinaryReader nbr = new NetBinaryReader(byIpData, start); SourcePort = nbr.ReadUInt16(); DestinationPort = nbr.ReadUInt16(); SequenceNumber = nbr.ReadUInt32(); AcknowledgmentNumber = nbr.ReadUInt32(); DataOffset = nbr.ReadNible(); nbr.SkipPadBits(); nbr.ReadCustomAmount(2); URG = nbr.ReadBit(); ACK = nbr.ReadBit(); PSH = nbr.ReadBit(); RST = nbr.ReadBit(); SYN = nbr.ReadBit(); FIN = nbr.ReadBit(); Window = nbr.ReadUInt16(); Checksum = nbr.ReadUInt16(); UrgentPointer = nbr.ReadUInt16(); //TODO: test byte option; long optionsStart = nbr.BaseStream.Position; Options = new byte[0][]; while ((option = nbr.ReadByte()) != 0 || nbr.BaseStream.Position > start + DataOffset) // 0 = End list { if (option == 1) continue; // 1 = No Option if (option == 2) // Max Segm size { byte length = nbr.ReadByte(); byte[] cur = new byte[length]; for (byte i = 0; i < length; i++) { cur[i] = nbr.ReadByte(); } Array.Resize(ref Options, Options.Length + 1); Options[Options.Length - 1] = cur; } } long dataLength = DataOffset - optionsStart; Data = new byte[dataLength]; for (int i = 0; i < dataLength; i++) { Data[i] = nbr.ReadByte(); } }