How can I split a packet into two packets using pcapdotnet? This is what I tried, but I don’t know whether it’s correct:
public IEnumerable<Packet> splitPacket(Packet packet)
{
EthernetLayer ethernet = (EthernetLayer)packet.Ethernet.ExtractLayer();
IpV4Layer ipV4Layer = (IpV4Layer)packet.Ethernet.IpV4.ExtractLayer();
DateTime packetTimestamp = packet.Timestamp;
ILayer payload = packet.Ethernet.IpV4.Payload.ExtractLayer();
IpV4Fragmentation.Equals(packet, packet);
yield return PacketBuilder.Build(packetTimestamp, ethernet, ipV4Layer, payload);
}
I have never used Pcap.Net, so I am not sure if this will work, but the general idea is to split the data (the “payload layer”) into several chunks and then send it. To make sure the fragments can be reassembled, you also need to add some info about the position (offset) of each fragment.
In Pcap.Net, the
IpV4Fragmentationclass contains two properties which define this:IpV4Fragmentation.Options:IpV4FragmentationOptions.MoreFragments,IpV4FragmentationOptions.NoneIpV4Fragmentation.Offset:With this in mind, I would write something like this:
(Disclaimer: this was written in Notepad, I have no clue if it even compiles, let alone works as it should):
[Updated with @brickner’s suggestions]