Step 3 - setup the packet handler
This is the meat of our application and the main loop for us. All the packets come here. We pick out the ip4 packets and we process each one of them.
public class IpReassemblyExample implements PcapPacketHandler
We process each Ip4 packet a little differently depending if the the
Ip4.FLAG_MORE_FRAGEMNTS is set. If it is not set that means it is the last fragment, otherwise we received a packet inside fragment. If a packet is not fragmented at all, it only contains a single fragment and is always the last fragment and we treat it as a last segment.
We use to methods,
bufferLastFragment() to record the fragments in the reassembly buffer. The
bufferLastFragment() is a little bit special in that it records the length of the entire ip datagram we are reassembling and if all the fragments arrived in sequence it also means we're done with this buffer.
At the end of this loop, we check the time out queue to see if there is anything ready to be timed out. A better approach would be put this check in a sub thread, but for our purpose this a simple check every time a packet arrives is sufficient.