Have some problems with JNetPcap.
I uses Ubuntu 12.04, and trying to make packet snipper that based in java language.
What I did is below.
I have downloaded JNetPcap 1.3.0.
And as tutorial said built a java project. http://jnetpcap.com/examples/dumper <- this is the link.
I typed just like that link and I got my first problem. PcapHandler Class is deprecated. So I find the document and replace it with ByteBufferHandler.
Now I compile this project and got an unsatifiedLinked Error. I have tried with static block to load that library. After some attempts I copied "libjnetpcap.so" to /usr/lib/
now I remove unsatisfiedLinked Error. but somehow it stops in 1st Error check. It prints "1st error check : ", then exit automatically.
public static void main(String[] args) {
List<PcapIf> alldevs = new ArrayList<PcapIf>(); StringBuilder errbuff = new StringBuilder(); int r = Pcap.findAllDevs(alldevs, errbuff); //============1st check if(r == Pcap.NOT_OK || alldevs.isEmpty()){ System.err.printf("1st error check : %s\n", errbuff.toString()); return; } PcapIf device = alldevs.get(1); //===================== END int snaplen = 64 * 1024; int flags = Pcap.MODE_PROMISCUOUS; int timeout = 10 * 1000; Pcap pcap = Pcap.openLive(device.getName(),snaplen, flags, timeout, errbuff); //============2nd check if(pcap == null){ System.err.printf("2nd error check : %s\n", errbuff.toString()); return; } //===================== END String ofile = "/home/juneyoungoh/tmp_capture_file.cap"; final PcapDumper dumper = pcap.dumpOpen(ofile); ByteBufferHandler<PcapDumper> handler = new ByteBufferHandler<PcapDumper>() { @Override public void nextPacket(PcapHeader arg0, ByteBuffer arg1, PcapDumper arg2) { dumper.dump(arg0, arg1); } }; pcap.loop(10,handler, dumper); File file = new File(ofile); System.out.printf("%s file has %d bytes in it!\n", ofile, file.length()); dumper.close(); pcap.close(); if(file.exists()){ file.delete(); }
}
if is there any good reference or wonderful idea, please share.
Thanks.