Question

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 + ")");
    }
}
Was it helpful?

Solution

"\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.)

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