I am listening a specific network card and capturing TCP(just TCP) packets using jPcap library. However, I need to have whole TCP sessions, not individual packets.
In Wireshark, I can choose "follow tcp stream", so that I can get the whole conversation from beginning to end. I want to do exactly that in Java. How can I reconstruct these packets in real-time? I want to reconstruct TCP sessions while listening the network card and capturing new packets. How can I achieve this? Here is my code to capture packets:
jpcap.NetworkInterface[] devices = JpcapCaptor.getDeviceList();
JpcapCaptor captor = JpcapCaptor.openDevice(devices[1], 65535, true, 1000);
JpcapWriter writer = JpcapWriter.openDumpFile(captor, "myNetworkDump");
captor.loopPacket(-1, new PacketPrinter(writer));
class PacketPrinter implements PacketReceiver {
private HashMap<Long, ArrayList<Packet>> sessions;
private BufferedWriter out;
private JpcapWriter writer;
Map<Long, TCPBodyData> bodies = new HashMap<Long, TCPBodyData>();
public PacketPrinter(JpcapWriter writer) {
this.writer = writer;
this.sessions = new HashMap<Long, ArrayList<Packet>>();
}
public void receivePacket(Packet packet) {
System.out.println(packet);
if (packet instanceof TCPPacket) {
TCPPacket tcppacl = (TCPPacket) packet;
byte[] body = addBodyData(tcppacl);
// System.out.println(new String(body));
}
}
}