private void HandleIPv4Datagram(byte[] buffer) { int identification = 0; int num2 = 0; uint addr = 0; uint num4 = 0; int offset = 0; IPv4Datagram datagram = null; IPv4Fragment fragment = new IPv4Fragment(); fragment.MoreFlag = Sniffer.HeaderParser.ToByte(buffer, 50, 1) > 0; fragment.Offset = Sniffer.HeaderParser.ToInt(buffer, 0x33, 13) * 8; fragment.TTL = Sniffer.HeaderParser.ToInt(buffer, 0x40, 8); fragment.Length = Sniffer.HeaderParser.ToUShort(buffer, 0x10, 0x10); offset = Sniffer.HeaderParser.ToInt(buffer, 4, 4) * 4; fragment.SetData(buffer, offset, fragment.Length - offset); identification = Sniffer.HeaderParser.ToByte(buffer, 0x20, 0x10); num2 = Sniffer.HeaderParser.ToByte(buffer, 0x48, 8); addr = Sniffer.HeaderParser.ToUInt(buffer, 0x60, 0x20); num4 = Sniffer.HeaderParser.ToUInt(buffer, 0x80, 0x20); IPAddress source = IPAddress.Parse(IPv4Datagram.GetIPString(addr)); IPAddress dest = IPAddress.Parse(IPv4Datagram.GetIPString(num4)); datagram = this.m_Data.GetIPv4Datagram(identification, source, dest); if (datagram == null) { datagram = new IPv4Datagram(); datagram.Identification = identification; datagram.Source = source; datagram.Destination = dest; datagram.Protocol = num2; } datagram.AddFragment(fragment); if (!datagram.Complete) { this.m_Data.AddIPv4Datagram(datagram); this.FireIPv4FragmentReceived(fragment); } else { this.FireIPv4DatagramReceived(datagram); switch (datagram.Protocol) { case 6: this.HandleTcpPacket(datagram.Data, datagram.Source, datagram.Destination); break; case 0x11: this.HandleUdpDatagram(datagram.Data, datagram.Source, datagram.Destination); break; } if (datagram.WasFragmented()) { this.m_Data.RemoveIPv4Datagram(datagram); } } }
// Parses out an IPv4 Datagram. private void HandleIPv4Datagram(byte[] buffer) { int identification = 0; int protocol = 0; uint source = 0; uint dest = 0; int header_length = 0; IPv4Datagram datagram = null; IPAddress source_ip; IPAddress dest_ip; source = HeaderParser.ToUInt(buffer, 96, 32); dest = HeaderParser.ToUInt(buffer, 128, 32); source_ip = IPAddress.Parse(IPv4Datagram.GetIPString(source)); dest_ip = IPAddress.Parse(IPv4Datagram.GetIPString(dest)); if (isRelatedToThisCom(source_ip.ToString(), dest_ip.ToString())) { IPv4Fragment fragment = new IPv4Fragment(); fragment.MoreFlag = (HeaderParser.ToByte(buffer, 50, 1) > 0) ? true : false; fragment.Offset = HeaderParser.ToInt(buffer, 51, 13) * 8; fragment.TTL = HeaderParser.ToInt(buffer, 64, 8); fragment.Length = HeaderParser.ToUShort(buffer, 16, 16); header_length = HeaderParser.ToInt(buffer, 4, 4) * 4; fragment.SetData(buffer, header_length, fragment.Length - header_length); identification = HeaderParser.ToInt(buffer, 32, 16); protocol = HeaderParser.ToByte(buffer, 72, 8); // source = HeaderParser.ToUInt(buffer,96,32); // dest = HeaderParser.ToUInt(buffer,128,32); // source_ip = IPAddress.Parse(IPv4Datagram.GetIPString(source)); // dest_ip = IPAddress.Parse(IPv4Datagram.GetIPString(dest)); datagram = Data_.GetIPv4Datagram(identification, source_ip, dest_ip); if (datagram == null) { datagram = new IPv4Datagram(); datagram.IHL = HeaderParser.ToInt(buffer, 4, 4) * 4; datagram.TypeOfService = HeaderParser.ToInt(buffer, 8, 8); datagram.ReservedFlag = HeaderParser.ToInt(buffer, 48, 1); datagram.DontFragmentFlag = HeaderParser.ToInt(buffer, 49, 1); datagram.Identification = identification; datagram.TTL = HeaderParser.ToInt(buffer, 64, 8); datagram.Checksum = HeaderParser.ToInt(buffer, 80, 16); datagram.Source = source_ip; datagram.SourceName = DnsTable.GetName(source_ip.ToString()); datagram.DestinationName = DnsTable.GetName(dest_ip.ToString()); datagram.Destination = dest_ip; datagram.Protocol = protocol; if (datagram.IHL == 24) { datagram.Options = HeaderParser.ToInt(buffer, 160, 32); } } datagram.AddFragment(fragment); if (datagram.Complete) { datagram.SetPorts(); if (FilterManager.isAllowed(datagram.GetUpperProtocol(), datagram.SourceIP, datagram.DestinationIP, datagram.SPort, datagram.DPort)) { FireIPv4DatagramReceived(datagram); datagram.SetHeader(buffer); if (datagram.WasFragmented()) { Data_.RemoveIPv4Datagram(datagram); } } } else { Data_.AddIPv4Datagram(datagram); this.FireIPv4FragmentReceived(fragment); } } }