/// <summary> /// Creates a new instance of this class /// </summary> public IPv4Frame() { this.iVersion = 4; this.tosTypeOfService = new IPTypeOfService(); this.iIdentification = 0; this.ifFlags = new IPFlags(false, false); this.iFragmentOffset = 0; this.iTimeToLive = 0xFF; this.iProtocol = IPProtocol.Other; this.ipaSource = IPAddress.Any; this.ipaDestination = IPAddress.Any; this.ipoOptions = new IPv4Options(); }
/// <summary> /// Creates a new instance of this class by parsing the given data. /// </summary> /// <param name="bRaw">The data to parse</param> public IPv4Frame(byte[] bRaw) { int iHeaderLength; int iTotalLength; if (bRaw.Length < 20) { throw new ArgumentException("Invalid packet"); } this.iVersion = (bRaw[0] & 0xF0) >> 4; iHeaderLength = (bRaw[0] & 0x0F) * 4; this.iHeaderLength = iHeaderLength; if (iHeaderLength < 20) { throw new ArgumentException("Invalid packet header"); } this.tosTypeOfService = new IPTypeOfService(bRaw[1]); iTotalLength = (int)bRaw[2] * 256 + (int)bRaw[3]; if (iTotalLength > bRaw.Length) { throw new ArgumentException("Corrupt packet length"); } this.iIdentification = bRaw[4] * (uint)256 + bRaw[5]; this.ifFlags = new IPFlags((bRaw[6] & 0x40) != 0, ((bRaw[6] & 0x20)) != 0); this.iFragmentOffset = 0; this.iFragmentOffset |= (byte)((bRaw[6] & 0x1F) << 8); this.iFragmentOffset |= (byte)(bRaw[7]); this.iTimeToLive = bRaw[8]; if (Enum.IsDefined(typeof(IPProtocol), (int)bRaw[9])) { this.iProtocol = (IPProtocol)bRaw[9]; } else { this.iProtocol = IPProtocol.Other; } ipaSource = new IPAddress(BitConverter.ToUInt32(bRaw, 12)); ipaDestination = new IPAddress(BitConverter.ToUInt32(bRaw, 16)); if (iHeaderLength > 20) { byte[] bOptionData = new byte[iHeaderLength - 20]; for (int iC1 = 20; iC1 < iHeaderLength - 20; iC1++) { bOptionData[iC1] = bRaw[iC1 + 20]; } this.ipoOptions = new IPv4Options(bOptionData); } else { this.ipoOptions = new IPv4Options(); } Encapsulate(bRaw, iHeaderLength, iTotalLength - iHeaderLength); }