1

I'm having trouble opening found network devices with the jpcap library. I have installed winpcap and have jpcap.dll in system32 and syswow64. The following tutorial code crashes when trying to open device. The crash log:

PacketCapture: loading native library jpcap.. ok
net.sourceforge.jpcap.capture.CaptureDeviceOpenException: Error opening adapter: The system cannot find the device specified. (20)
    at net.sourceforge.jpcap.capture.PacketCapture.open(Native Method)
    at net.sourceforge.jpcap.capture.PacketCapture.open(PacketCapture.java:57)
    at networksnifferdesktop.NetworkSnifferDesktop.<init>(NetworkSnifferDesktop.java:26)
    at networksnifferdesktop.NetworkSnifferDesktop.main(NetworkSnifferDesktop.java:40)
Java Result: 1

In debug I can see that m_device is set to:

"\Device\NPF_{EC5226CF-3F55-4148-B40E-1FC3F8BB3398}   Realtek PCIe GBE Family Controller"

in the following code:

package networksnifferdesktop;

import net.sourceforge.jpcap.capture.*;
import net.sourceforge.jpcap.net.*;

public class NetworkSnifferDesktop
{
    private static final int INFINITE = -1;
    private static final int PACKET_COUNT = 10;

    // BPF filter for capturing any packet
    private static final String FILTER = "";

    private PacketCapture m_pcap;
    private String m_device;

    public NetworkSnifferDesktop() throws Exception
    {
        // Step 1:  Instantiate Capturing Engine
        m_pcap = new PacketCapture();

        // Step 2:  Check for devices
        m_device = m_pcap.findDevice();

        // Step 3:  Open Device for Capturing (requires root)
        m_pcap.open(m_device, true);

        // Step 4:  Add a BPF Filter (see tcpdump documentation)
        m_pcap.setFilter(FILTER, true);

        // Step 5:  Register a Listener for Raw Packets
        m_pcap.addRawPacketListener(new RawPacketHandler());

        // Step 6:  Capture Data (max. PACKET_COUNT packets)
        m_pcap.capture(PACKET_COUNT);
    }

    public static void main(String[] args)
    {
        try
        {
            NetworkSnifferDesktop example = new NetworkSnifferDesktop();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.exit(1);
        }
    }
}

class RawPacketHandler implements RawPacketListener
{
    private static int m_counter = 0;

    public void rawPacketArrived(RawPacket data)
    {
        m_counter++;
        System.out.println("Received packet (" + m_counter + ")");
    }
}
Jonathan
  • 20,053
  • 6
  • 63
  • 70
mechanicum
  • 699
  • 3
  • 14
  • 25
  • 1
    and where can i find the jpcap lib that i can import with something like `import jpcap.JpcapCaptor;`The only lib i could find is sourceforge's and it doesn't have JpcapCaptor class for example and almost all examples use this class. – mechanicum Dec 20 '12 at 15:55

1 Answers1

3

"\Device\NPF_{EC5226CF-3F55-4148-B40E-1FC3F8BB3398} Realtek PCIe GBE Family Controller", if you literally mean a String the first character of which is the "D" in "\Device" and the last character of which is the "r" in "Controller", is not a valid WinPcap device name string.

"\Device\NPF_{EC5226CF-3F55-4148-B40E-1FC3F8BB3398}" would be a valid device name string.

From looking at the Jpcap source, it appears that the findDevice method does NOT return valid device name strings. It's documented as returning "a string describing the network device"; what it returns is a string containing the device name string, a newline, two blanks, and the device's vendor description string. This has been reported as a Jpcap bug.

I would suggest that you scan the string looking for the first white-space character ("white-space" includes blanks and newlines), and use, as the device name to pass to the open routine, everything up to but not including that white-space character. (If you don't find a white-space character, use the entire string.)

  • winpcap 4.1.2. I'll give it a try tomorrow to see if your suggestion works. – mechanicum Dec 24 '12 at 11:54
  • 1
    I didn't think I'd made a suggestion when I originally wrote my answer, but I just looked at the Jpcap source, and it turns out to be a problem with the `findDevice` method; I've edited my answer to give a workaround - try that. –  Dec 24 '12 at 22:19
  • NB I added a note to that bug in 2008 stating that it had been fixed in the forthcoming release, which it had, but due to factors beyond my control, release 2.0 still hasn't appeared. The workaround is to remove everything from the first \n onwards. – user207421 Dec 25 '12 at 00:16
  • feels a little shaky but it works. jpcap has it's days i suppose. Thanks everyone. – mechanicum Dec 25 '12 at 12:40