/// <summary> /// using the raw and current system time to initialize the packet; /// </summary> /// <param name="raw"></param> /// <param name="time"></param> public Packet(byte[] raw, bool isIpv6) { ///all the following exceptions should be caught when invoking this constructor; if (raw == null) { throw new ArgumentNullException(); } ///when the orginal length is less than the header lenght of ipv6 = 40, it must be wrong; if (raw.Length < 40) { throw new ArgumentException(); } PacketV6 ipv6Data = new PacketV6(); ipv6Data = ipv6Data.Create(raw); raw_Packet = raw; dateTime = DateTime.Now; headLength = ipv6Data.Ipv6HeaderLength; src_IPAddress = ipv6Data.ipSourceAddress; des_IPAddress = ipv6Data.ipDestinationAddress; totalLength = ipv6Data.ipPayloadLength; //Baseado na tabela no site da cisco https://www.cisco.com/en/US/technologies/tk648/tk872/technologies_white_paper0900aecd8054d37d.html switch (ipv6Data.ipNextHeader) { case 6: protocolType = ProtocolType.TCP; break; case 17: protocolType = ProtocolType.UDP; break; case 58: protocolType = ProtocolType.ICMP; break; default: protocolType = ProtocolType.OTHERS; break; } }
/// <summary> /// This routine creates an instance of the Ipv6Header class from a byte /// array that is a received IGMP packet. This is useful when a packet /// is received from the network and the header object needs to be /// constructed from those values. /// </summary> /// <param name="ipv6Packet">Byte array containing the binary IPv6 header</param> /// <returns>Returns the Ipv6Header object created from the byte array</returns> public PacketV6 Create(byte[] ipv6Packet) { PacketV6 ipv6Header = new PacketV6(); if (ipv6Packet.Length < ipv6Header.Ipv6HeaderLength) { return(null); } byte[] addressBytes = new byte[16]; uint tempVal, tempVal2; tempVal = ipv6Packet[0]; tempVal = (tempVal >> 4) & 0xF; ipv6Header.ipVersion = (byte)tempVal; tempVal = ipv6Packet[0]; tempVal = (tempVal & 0xF) >> 4; ipv6Header.ipTrafficClass = (byte)(tempVal | (uint)((ipv6Packet[1] >> 4) & 0xF)); tempVal2 = ipv6Packet[1]; tempVal2 = (tempVal2 & 0xF) << 16; tempVal = ipv6Packet[2]; tempVal = tempVal << 8; ipv6Header.ipFlow = tempVal2 | tempVal | ipv6Packet[3]; ipv6Header.ipNextHeader = ipv6Packet[4]; ipv6Header.ipHopLimit = ipv6Packet[5]; Array.Copy(ipv6Packet, 6, addressBytes, 0, 16); ipv6Header.ipSourceAddress = new IPAddress(addressBytes); Array.Copy(ipv6Packet, 24, addressBytes, 0, 16); ipv6Header.ipDestinationAddress = new IPAddress(addressBytes); return(ipv6Header); }