I want to get through a pcap
file and go to each packet. Then get IP Address and manipulate it. In the end, I'm going to write it into a new pcap
file.
I use pcap4j version 1.6.4 and below is how I get the Source IP Address:
String fname = "FileName";
String dumpFile = "newFileName";
PcapHandle h = Pcaps.openOffline(fname);
PcapDumper dumper = h.dumpOpen(newFileName);
Packet p = null;
while ((p = h.getNextPacket()) != null) {
IpV4Packet ip = p.get(IpV4Packet.class);
Inet4Address srcAddr = ip.getHeader().getSrcAddr();
}
As I mentioned, I got the Source IP Address and now I don't know how to set the new Source IP Address and write it to NewFileName.
Any help would be appreciated.
Packet objects in pcap4j are immutable. You can, however, create a new packet based on an existing one and then modify it, using a Builder.
In the following snippet, I'm creating a new modified packet (assuming replace() contains your logic of creating a new IP address):
You can then dump the created packet using: