jNetPcap 1.X

jNetPcap 1.X module: libpcap and packet decoder

2.8 - Dumping captured packet to an offline file

PcapDumper
StringBuilder errbuf = new StringBuilder();
String fname = "tests/test-afs.pcap";

Pcap pcap = Pcap.openOffline(fname, errbuf);

String ofile = "tmp-capture-file.cap";
PcapDumper dumper = pcap.dumpOpen(ofile); // output file

JBufferHandler<PcapDumper> dumpHandler = new JBufferHandler<PcapDumper>() {

  public void nextPacket(PcapHeader header, JBuffer buffer, PcapDumper dumper) {

    dumper.dump(header, buffer);
  }
};

pcap.loop(10, dumpHandler, dumper);
                
File file = new File(ofile);
System.out.printf("%s file has %d bytes in it!\n", ofile, file.length());
                
dumper.close(); // Won't be able to delete without explicit close
    pcap.close();
StringBuilder errbuf = new StringBuilder();
String fname = "tests/test-afs.pcap";

Pcap pcap = Pcap.openOffline(fname, errbuf);

String ofile = "tmp-capture-file.cap";
PcapDumper dumper = pcap.dumpOpen(ofile); // output file

pcap.loop(10, dumper); // Special native dumper call to loop
                
File file = new File(ofile);
System.out.printf("%s file has %d bytes in it!\n", ofile, file.length());
                
dumper.close(); // Won't be able to delete without explicit close
    pcap.close();

2.2 - Getting a List of Interfaces

PcapPcap.findAllDevs()ListPcapIfPcapIf
List<PcapIf> alldevs = new ArrayList<PcapIf>(); // Will be filled with NICs
StringBuilder errbuf = new StringBuilder();     // For any error msgs
                
int r = Pcap.findAllDevs(alldevs, errbuf);
if (r == Pcap.NOT_OK || alldevs.isEmpty()) {
  System.err.printf("Can't read list of devices, error is %s", errbuf.toString());
  return;
}
Pcap.findAllDevs()PcapIf.getName()Pcap.openLive()
Syndicate content