Question

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.

  1. I have downloaded JNetPcap 1.3.0.

  2. And as tutorial said built a java project. http://jnetpcap.com/examples/dumper <- this is the link.

  3. 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.

  4. 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/

  5. 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.

Was it helpful?

Solution

On Linux, a program will probably have to run as root, or with sufficient privileges granted in some other fashion, in order to be able to open any devices, and, currently, pcap_findalldevs(), which is presumably what the Pcap.findAllDevs method uses, tries to open each of the devices it finds, and only returns the devices it can open.

So you'll have to run your Java program as root, or will somehow have to arrange that it have sufficient privileges (CAP_NET_RAW and CAP_NET_ADMIN) to get a list of network adapters and open those adapters.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top