Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using Microsoft.Win32;
- using System.Threading;
- using System.Runtime.InteropServices;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using PcapDotNet.Base;
- using PcapDotNet.Core;
- using PcapDotNet.Packets;
- using PcapDotNet.Packets.Arp;
- using PcapDotNet.Packets.Dns;
- using PcapDotNet.Packets.Ethernet;
- using PcapDotNet.Packets.Gre;
- using PcapDotNet.Packets.Http;
- using PcapDotNet.Packets.Icmp;
- using PcapDotNet.Packets.Igmp;
- using PcapDotNet.Packets.IpV4;
- using PcapDotNet.Packets.Transport;
- namespace NMLB
- {
- class MainThread
- {
- public static TapWorker tapWorker = null;
- public static IList<PhysicalWorker> physicalWorkers = new List<PhysicalWorker>();
- //[STAThread]
- public static void Main(string[] args)
- {
- if (args.Length < 8)
- Environment.Exit(0);
- tapWorker = new TapWorker(args[0], args[1], args[2]);
- Thread tapWorkerThread = new Thread(new ThreadStart(tapWorker.ReceivePackets));
- tapWorkerThread.Start();
- IList<Thread> physicalWorkerThreads = new List<Thread>();
- int deadGateways = 0;
- for (int i = 3; i < args.Length; i = i + 5)
- {
- if (args[i + 3] != "0")
- {
- physicalWorkers.Add(new PhysicalWorker(args[i], args[i + 1], args[i + 2], args[i + 3], args[i + 4]));
- physicalWorkerThreads.Add(new Thread(new ThreadStart(physicalWorkers[(i - 3) / 5 - deadGateways].ReceivePackets)));
- physicalWorkerThreads[(i - 3) / 5 - deadGateways].Start();
- }
- else
- deadGateways++;
- }
- }
- }
- // TAP interface worker
- class TapWorker
- {
- public static string ownProtocolAddressString = "192.168.200.1";
- public static string ownHardwareAddressString = "0A:03:03:03:03:03";
- public static string ifProtocolAddressString;
- public static string ifHardwareAddressString;
- public static IpV4Address ownProtocolAddress = new IpV4Address(ownProtocolAddressString);
- public static MacAddress ownHardwareAddress = new MacAddress(ownHardwareAddressString);
- public static IpV4Address ifProtocolAddress;
- public static MacAddress ifHardwareAddress;
- public static byte[] ownProtocolAddressByte = { 192, 168, 200, 1 };
- public static byte[] ownHardwareAddressByte = { 10, 3, 3, 3, 3, 3 };
- public static byte[] ifProtocolAddressByte;
- public static byte[] ifHardwareAddressByte;
- public static PacketCommunicator communicator;
- public static FragmentationBuffer fragBuffer = new FragmentationBuffer();
- public static IList<Packet> fragments = new List<Packet>();
- public static int MTU = 1500;
- string tapGuid;
- public TapWorker(string guid, string mac, string ip)
- {
- tapGuid = guid;
- ifHardwareAddressString = mac;
- ifProtocolAddressString = ip;
- ifHardwareAddress = new MacAddress(ifHardwareAddressString);
- ifProtocolAddress = new IpV4Address(ifProtocolAddressString);
- ifHardwareAddressByte = new byte[]
- {
- (byte)ifHardwareAddress.ToValue(),
- (byte)(ifHardwareAddress.ToValue() >> 8),
- (byte)(ifHardwareAddress.ToValue() >> 16),
- (byte)(ifHardwareAddress.ToValue() >> 24)
- };
- ifProtocolAddressByte = new byte[]
- {
- (byte)ifProtocolAddress.ToValue(),
- (byte)(ifProtocolAddress.ToValue() >> 8),
- (byte)(ifProtocolAddress.ToValue() >> 16),
- (byte)(ifProtocolAddress.ToValue() >> 24)
- };
- }
- public void SendPacket(Packet packet)
- {
- Console.WriteLine(packet.Ethernet.Source + " " + packet.Ethernet.Destination + " " + packet.Ethernet.IpV4.Source + " " + packet.Ethernet.IpV4.Destination + " " + packet.Ethernet.IpV4.Protocol);
- communicator.SendPacket(packet);
- }
- public void ReceivePackets()
- {
- // Retrieve the device list from the local machine
- IList<LivePacketDevice> allDevices = LivePacketDevice.AllLocalMachine;
- // Find the NPF device of the TAP interface
- PacketDevice selectedDevice = null;
- for (int i = 0; i != allDevices.Count; ++i)
- {
- LivePacketDevice device = allDevices[i];
- if (device.Name.Contains(tapGuid))
- {
- selectedDevice = device;
- break;
- }
- }
- if (selectedDevice == null)
- Environment.Exit(0);
- communicator = selectedDevice.Open(65536, PacketDeviceOpenAttributes.MaximumResponsiveness, 1000);
- {
- Console.WriteLine("Listening on " + selectedDevice.Description + "...");
- communicator.SetFilter("(ether dst " + ownHardwareAddressString + " and (ip or arp)) or (ether dst FF:FF:FF:FF:FF:FF and arp)");
- communicator.ReceivePackets(0, (packet) =>
- {
- if (packet.Ethernet.EtherType == EthernetType.Arp)
- {
- string TargetProtocolAddressString = packet.Ethernet.Arp.TargetProtocolAddress[0] + "." +
- packet.Ethernet.Arp.TargetProtocolAddress[1] + "." +
- packet.Ethernet.Arp.TargetProtocolAddress[2] + "." +
- packet.Ethernet.Arp.TargetProtocolAddress[3];
- if (TargetProtocolAddressString == ownProtocolAddressString && packet.Ethernet.Arp.Operation == ArpOperation.Request)
- {
- EthernetLayer ethernetLayer =
- new EthernetLayer
- {
- Source = ownHardwareAddress,
- Destination = packet.Ethernet.Source,
- EtherType = EthernetType.None,
- };
- ArpLayer arpLayer =
- new ArpLayer
- {
- ProtocolType = EthernetType.IpV4,
- Operation = ArpOperation.Reply,
- SenderHardwareAddress = ownHardwareAddressByte.AsReadOnly(),
- SenderProtocolAddress = packet.Ethernet.Arp.TargetProtocolAddress,
- TargetHardwareAddress = packet.Ethernet.Arp.SenderHardwareAddress,
- TargetProtocolAddress = packet.Ethernet.Arp.SenderProtocolAddress,
- };
- PacketBuilder builder = new PacketBuilder(ethernetLayer, arpLayer);
- communicator.SendPacket(builder.Build(DateTime.Now));
- }
- }
- if (packet.Ethernet.EtherType.ToString() == "IpV4")
- {
- if (packet.Ethernet.IpV4.Fragmentation.Options == IpV4FragmentationOptions.DoNotFragment ||
- packet.Ethernet.IpV4.Fragmentation.Options == IpV4FragmentationOptions.None && packet.Ethernet.IpV4.Fragmentation.Offset == 0)
- {
- IpV4Handler(packet);
- }
- if (packet.Ethernet.IpV4.Fragmentation.Options == IpV4FragmentationOptions.MoreFragments ||
- packet.Ethernet.IpV4.Fragmentation.Options == IpV4FragmentationOptions.None && packet.Ethernet.IpV4.Fragmentation.Offset > 0)
- {
- fragments = fragBuffer.Add(packet);
- if (fragments != null)
- for (int i = 0; i < fragments.Count; i++)
- {
- IpV4Handler(fragments[i], fragments);
- }
- }
- }
- });
- }
- }
- public static void IpV4Handler(Packet packet, IList<Packet> packetList = null)
- {
- if (packet.Ethernet.IpV4.Length > MTU)
- {
- EthernetLayer ethernetLayer =
- new EthernetLayer
- {
- Source = ownHardwareAddress,
- Destination = packet.Ethernet.Source,
- EtherType = EthernetType.None,
- };
- IpV4Layer ipV4Layer =
- new IpV4Layer
- {
- Source = packet.Ethernet.IpV4.Destination,
- CurrentDestination = packet.Ethernet.IpV4.Source,
- Fragmentation = packet.Ethernet.IpV4.Fragmentation,
- HeaderChecksum = null, // Will be filled automatically.
- Identification = 123,
- Options = packet.Ethernet.IpV4.Options,
- Protocol = packet.Ethernet.IpV4.Protocol,
- Ttl = packet.Ethernet.IpV4.Ttl,
- TypeOfService = packet.Ethernet.IpV4.TypeOfService,
- };
- IcmpDestinationUnreachableLayer payloadLayer = new IcmpDestinationUnreachableLayer
- {
- Code = IcmpCodeDestinationUnreachable.FragmentationNeededAndDoNotFragmentSet,
- NextHopMaximumTransmissionUnit = (ushort)MTU,
- };
- PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, payloadLayer);
- communicator.SendPacket(builder.Build(DateTime.Now));
- return;
- }
- if (packet.Ethernet.IpV4.Protocol == IpV4Protocol.InternetControlMessageProtocol)
- {
- if (packet.Ethernet.IpV4.Destination == ownProtocolAddress)
- {
- if ((packet.Ethernet.IpV4.Icmp.MessageType == IcmpMessageType.Echo || packet.Ethernet.IpV4.Fragmentation.Offset > 0))
- {
- EthernetLayer ethernetLayer =
- new EthernetLayer
- {
- Source = ownHardwareAddress,
- Destination = packet.Ethernet.Source,
- EtherType = EthernetType.None,
- };
- IpV4Layer ipV4Layer =
- new IpV4Layer
- {
- Source = packet.Ethernet.IpV4.Destination,
- CurrentDestination = packet.Ethernet.IpV4.Source,
- Fragmentation = packet.Ethernet.IpV4.Fragmentation,
- HeaderChecksum = null, // Will be filled automatically.
- Identification = 123,
- Options = packet.Ethernet.IpV4.Options,
- Protocol = packet.Ethernet.IpV4.Protocol,
- Ttl = packet.Ethernet.IpV4.Ttl,
- TypeOfService = packet.Ethernet.IpV4.TypeOfService,
- };
- if (packet.Ethernet.IpV4.Fragmentation.Options == IpV4FragmentationOptions.DoNotFragment ||
- packet.Ethernet.IpV4.Fragmentation.Options == IpV4FragmentationOptions.None && packet.Ethernet.IpV4.Fragmentation.Offset == 0)
- {
- byte[] icmpPacket = packet.Ethernet.IpV4.Payload.ToArray();
- icmpPacket[0] = 0;
- icmpPacket[2] = 0;
- icmpPacket[3] = 0;
- ushort checksum = IpFunctions.ComputeIpChecksum(icmpPacket);
- icmpPacket[2] = (byte)(checksum >> 8);
- icmpPacket[3] = (byte)(checksum & 0xff);
- PayloadLayer payloadLayer =
- new PayloadLayer
- {
- Data = new Datagram(icmpPacket),
- };
- PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, payloadLayer);
- communicator.SendPacket(builder.Build(DateTime.Now));
- }
- else if (packet.Ethernet.IpV4.Fragmentation.Offset == 0 && packet.Ethernet.IpV4.Fragmentation.Options == IpV4FragmentationOptions.MoreFragments)
- {
- byte[] icmpHeader = packet.Ethernet.IpV4.Payload.ToArray();
- icmpHeader[0] = 0;
- icmpHeader[2] = 0;
- icmpHeader[3] = 0;
- int icmpPacketLength = icmpHeader.Length;
- for (int i = 1; i < packetList.Count; i++)
- {
- icmpPacketLength += packetList[i].Ethernet.IpV4.Payload.ToArray().Length;
- }
- byte[] icmpPacket = new byte[icmpPacketLength];
- Buffer.BlockCopy(icmpHeader, 0, icmpPacket, 0, icmpHeader.Length);
- icmpPacketLength = icmpHeader.Length;
- for (int i = 1; i < packetList.Count; i++)
- {
- Buffer.BlockCopy(packetList[i].Ethernet.IpV4.Payload.ToArray(), 0, icmpPacket, icmpPacketLength, packetList[i].Ethernet.IpV4.Payload.ToArray().Length);
- icmpPacketLength += packetList[i].Ethernet.IpV4.Payload.ToArray().Length;
- }
- ushort checksum = IpFunctions.ComputeIpChecksum(icmpPacket);
- icmpHeader[2] = (byte)(checksum >> 8);
- icmpHeader[3] = (byte)(checksum & 0xff);
- PayloadLayer payloadLayer =
- new PayloadLayer
- {
- Data = new Datagram(icmpHeader),
- };
- PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, payloadLayer);
- communicator.SendPacket(builder.Build(DateTime.Now));
- }
- else
- {
- PayloadLayer payloadLayer =
- new PayloadLayer
- {
- Data = new Datagram(packet.Ethernet.IpV4.Payload.ToArray()),
- };
- PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, payloadLayer);
- communicator.SendPacket(builder.Build(DateTime.Now));
- }
- }
- }
- else if (packet.Ethernet.IpV4.Source == ifProtocolAddress)
- if ((packet.Ethernet.IpV4.Icmp.MessageType == IcmpMessageType.Echo || packet.Ethernet.IpV4.Fragmentation.Offset > 0))
- {
- {
- EthernetLayer ethernetLayer =
- new EthernetLayer
- {
- Source = MainThread.physicalWorkers[0].ifHardwareAddress,
- Destination = MainThread.physicalWorkers[0].gatewayHardwareAddress,
- EtherType = EthernetType.None,
- };
- IpV4Layer ipV4Layer =
- new IpV4Layer
- {
- Source = MainThread.physicalWorkers[0].ifProtocolAddress,
- CurrentDestination = packet.Ethernet.IpV4.Destination,
- Fragmentation = packet.Ethernet.IpV4.Fragmentation,
- HeaderChecksum = null, // Will be filled automatically.
- Identification = packet.Ethernet.IpV4.Identification,
- Options = packet.Ethernet.IpV4.Options,
- Protocol = packet.Ethernet.IpV4.Protocol,
- Ttl = packet.Ethernet.IpV4.Ttl,
- TypeOfService = packet.Ethernet.IpV4.TypeOfService,
- };
- PayloadLayer payloadLayer =
- new PayloadLayer
- {
- Data = new Datagram(packet.Ethernet.IpV4.Payload.ToArray()),
- };
- PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, payloadLayer);
- MainThread.physicalWorkers[0].SendPacket(builder.Build(DateTime.Now));
- }
- }
- }
- else if (packet.Ethernet.IpV4.Protocol == IpV4Protocol.Tcp || packet.Ethernet.IpV4.Protocol == IpV4Protocol.Udp)
- {
- if (packet.Ethernet.IpV4.Source == ifProtocolAddress)
- {
- EthernetLayer ethernetLayer =
- new EthernetLayer
- {
- Source = MainThread.physicalWorkers[0].ifHardwareAddress,
- Destination = MainThread.physicalWorkers[0].gatewayHardwareAddress,
- EtherType = packet.Ethernet.EtherType,
- };
- IpV4Layer ipV4Layer =
- new IpV4Layer
- {
- Source = MainThread.physicalWorkers[0].ifProtocolAddress,
- CurrentDestination = packet.Ethernet.IpV4.Destination,
- Fragmentation = packet.Ethernet.IpV4.Fragmentation,
- HeaderChecksum = null, // Will be filled automatically.
- Identification = packet.Ethernet.IpV4.Identification,
- Options = packet.Ethernet.IpV4.Options,
- Protocol = packet.Ethernet.IpV4.Protocol,
- Ttl = packet.Ethernet.IpV4.Ttl,
- TypeOfService = packet.Ethernet.IpV4.TypeOfService,
- };
- PayloadLayer payloadLayer =
- new PayloadLayer
- {
- Data = new Datagram(packet.Ethernet.IpV4.Payload.ToArray()),
- };
- PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, payloadLayer);
- MainThread.physicalWorkers[0].SendPacket(builder.Build(DateTime.Now));
- }
- }
- }
- }
- // Physical interface worker
- class PhysicalWorker
- {
- public PacketCommunicator communicator = null;
- public FragmentationBuffer fragBuffer = new FragmentationBuffer();
- public IList<Packet> fragments = new List<Packet>();
- public static int MTU = 1500;
- public string ifProtocolAddressString;
- public string ifHardwareAddressString;
- public string gatewayProtocolAddressString;
- public string gatewayHardwareAddressString;
- public IpV4Address ifProtocolAddress;
- public MacAddress ifHardwareAddress;
- public IpV4Address gatewayProtocolAddress;
- public MacAddress gatewayHardwareAddress;
- public byte[] ifProtocolAddressByte;
- public byte[] ifHardwareAddressByte;
- public byte[] gatewayProtocolAddressByte;
- public byte[] gatewayHardwareAddressByte;
- string ifGuid;
- public PhysicalWorker(string guid, string ifMac, string ifIp, string gatewayMac, string gatewayIp)
- {
- ifGuid = guid;
- ifHardwareAddressString = ifMac;
- ifProtocolAddressString = ifIp;
- gatewayHardwareAddressString = gatewayMac;
- gatewayProtocolAddressString = gatewayIp;
- ifHardwareAddress = new MacAddress(ifHardwareAddressString);
- ifProtocolAddress = new IpV4Address(ifProtocolAddressString);
- gatewayHardwareAddress = new MacAddress(gatewayHardwareAddressString);
- gatewayProtocolAddress = new IpV4Address(gatewayProtocolAddressString);
- ifHardwareAddressByte = new byte[]
- {
- (byte)ifHardwareAddress.ToValue(),
- (byte)(ifHardwareAddress.ToValue() >> 8),
- (byte)(ifHardwareAddress.ToValue() >> 16),
- (byte)(ifHardwareAddress.ToValue() >> 24)
- };
- ifProtocolAddressByte = new byte[]
- {
- (byte)ifProtocolAddress.ToValue(),
- (byte)(ifProtocolAddress.ToValue() >> 8),
- (byte)(ifProtocolAddress.ToValue() >> 16),
- (byte)(ifProtocolAddress.ToValue() >> 24)
- };
- gatewayHardwareAddressByte = new byte[]
- {
- (byte)gatewayHardwareAddress.ToValue(),
- (byte)(gatewayHardwareAddress.ToValue() >> 8),
- (byte)(gatewayHardwareAddress.ToValue() >> 16),
- (byte)(gatewayHardwareAddress.ToValue() >> 24)
- };
- gatewayProtocolAddressByte = new byte[]
- {
- (byte)gatewayProtocolAddress.ToValue(),
- (byte)(gatewayProtocolAddress.ToValue() >> 8),
- (byte)(gatewayProtocolAddress.ToValue() >> 16),
- (byte)(gatewayProtocolAddress.ToValue() >> 24)
- };
- }
- public void SendPacket(Packet packet)
- {
- Console.WriteLine(packet.Ethernet.Source + " " + packet.Ethernet.Destination + " " + packet.Ethernet.IpV4.Source + " " + packet.Ethernet.IpV4.Destination + " " + packet.Ethernet.IpV4.Protocol);
- communicator.SendPacket(packet);
- }
- public void ReceivePackets()
- {
- // Retrieve the device list from the local machine
- IList<LivePacketDevice> allDevices = LivePacketDevice.AllLocalMachine;
- // Find the NPF device of the interface
- PacketDevice selectedDevice = null;
- for (int i = 0; i != allDevices.Count; ++i)
- {
- LivePacketDevice device = allDevices[i];
- if (device.Name.Contains(ifGuid))
- {
- selectedDevice = device;
- break;
- }
- }
- if (selectedDevice == null)
- Environment.Exit(0);
- communicator = selectedDevice.Open(65536, PacketDeviceOpenAttributes.MaximumResponsiveness, 1000);
- {
- Console.WriteLine("Listening on " + selectedDevice.Description + "...");
- communicator.SetFilter("(ether dst " + ifHardwareAddressString + " and (ip or arp)) or (ether dst FF:FF:FF:FF:FF:FF and arp)");
- communicator.ReceivePackets(0, (packet) =>
- {
- //if (packet.Ethernet.EtherType == EthernetType.Arp)
- if (packet.Ethernet.IpV4.Protocol == IpV4Protocol.InternetControlMessageProtocol)
- {
- if (packet.Ethernet.IpV4.Destination == ifProtocolAddress)
- if ((packet.Ethernet.IpV4.Icmp.MessageType == IcmpMessageType.EchoReply || packet.Ethernet.IpV4.Fragmentation.Offset > 0))
- {
- EthernetLayer ethernetLayer =
- new EthernetLayer
- {
- Source = TapWorker.ownHardwareAddress,
- Destination = TapWorker.ifHardwareAddress,
- EtherType = EthernetType.None,
- };
- IpV4Layer ipV4Layer =
- new IpV4Layer
- {
- Source = packet.Ethernet.IpV4.Source,
- CurrentDestination = TapWorker.ifProtocolAddress,
- Fragmentation = packet.Ethernet.IpV4.Fragmentation,
- HeaderChecksum = null, // Will be filled automatically.
- Identification = packet.Ethernet.IpV4.Identification,
- Options = packet.Ethernet.IpV4.Options,
- Protocol = packet.Ethernet.IpV4.Protocol,
- Ttl = packet.Ethernet.IpV4.Ttl,
- TypeOfService = packet.Ethernet.IpV4.TypeOfService,
- };
- PayloadLayer payloadLayer =
- new PayloadLayer
- {
- Data = new Datagram(packet.Ethernet.IpV4.Payload.ToArray()),
- };
- PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, payloadLayer);
- MainThread.tapWorker.SendPacket(builder.Build(DateTime.Now));
- }
- }
- else if (packet.Ethernet.IpV4.Protocol == IpV4Protocol.Tcp)
- {
- if (packet.Ethernet.IpV4.Destination == ifProtocolAddress)
- {
- EthernetLayer ethernetLayer =
- new EthernetLayer
- {
- Source = TapWorker.ownHardwareAddress,
- Destination = TapWorker.ifHardwareAddress,
- EtherType = packet.Ethernet.EtherType,
- };
- IpV4Layer ipV4Layer =
- new IpV4Layer
- {
- Source = packet.Ethernet.IpV4.Source,
- CurrentDestination = TapWorker.ifProtocolAddress,
- Fragmentation = packet.Ethernet.IpV4.Fragmentation,
- HeaderChecksum = null, // Will be filled automatically.
- Identification = packet.Ethernet.IpV4.Identification,
- Options = packet.Ethernet.IpV4.Options,
- Protocol = packet.Ethernet.IpV4.Protocol,
- Ttl = packet.Ethernet.IpV4.Ttl,
- TypeOfService = packet.Ethernet.IpV4.TypeOfService,
- };
- PayloadLayer payloadLayer =
- new PayloadLayer
- {
- Data = new Datagram(packet.Ethernet.IpV4.Payload.ToArray()),
- };
- PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, payloadLayer);
- MainThread.tapWorker.SendPacket(builder.Build(DateTime.Now));
- }
- }
- });
- }
- }
- }
- class FragmentationBuffer
- {
- struct DictKey
- {
- public DictKey(IpV4Address ip, ushort id)
- {
- this.Ip = ip;
- this.Identification = id;
- }
- private IpV4Address Ip;
- private ushort Identification;
- }
- class DictValue
- {
- public IList<Packet> fragments = new List<Packet>();
- private int offset = 0;
- private ushort lastOffset = 0;
- public bool AddPacket(Packet packet)
- {
- fragments.Add(packet);
- if (packet.Ethernet.IpV4.Fragmentation.Options == IpV4FragmentationOptions.None && packet.Ethernet.IpV4.Fragmentation.Offset > 0)
- lastOffset = packet.Ethernet.IpV4.Fragmentation.Offset;
- else
- offset += packet.Ethernet.IpV4.Length - packet.Ethernet.IpV4.HeaderLength;
- if (offset == lastOffset)
- return true;
- else
- return false;
- }
- }
- private Dictionary<DictKey, DictValue> ElementList = new Dictionary<DictKey, DictValue>();
- private DictKey dictKey;
- private DictValue dictValue;
- private IList<Packet> fragments;
- public IList<Packet> Add(Packet packet)
- {
- dictKey = new DictKey(packet.Ethernet.IpV4.Source, packet.Ethernet.IpV4.Identification);
- if (ElementList.TryGetValue(dictKey, out dictValue))
- {
- if (dictValue.AddPacket(packet))
- {
- fragments = dictValue.fragments;
- ElementList.Remove(dictKey);
- return fragments;
- }
- else
- {
- fragments = null;
- return fragments;
- }
- }
- else
- {
- dictValue = new DictValue();
- dictValue.AddPacket(packet);
- ElementList.Add(dictKey, dictValue);
- fragments = null;
- return fragments;
- }
- }
- }
- static class IpFunctions
- {
- //Calculate ICMP checksum (same as IP, TCP, UDP, etc) - rfc1071
- public static ushort ComputeIpChecksum(byte[] data)
- {
- long sum = 0;
- int count = 0;
- int remaining = data.Length;
- while (remaining > 1)
- {
- sum += (long)((ushort)(data[count] << 8) | data[count + 1]);
- count += 2;
- remaining -= 2;
- }
- if (remaining > 0)
- sum += (long)(data[count] << 8);
- while (sum >> 16 != 0)
- sum = (sum & 0xffff) + (sum >> 16);
- sum = ~sum;
- return (ushort)sum;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment