2

I'm trying to write a packet capturing java program using the jnetpcap library. But I wonder if I can get the PID from a given port? In my case, I'm trying to use Sigar API to get the PID (method name is getProcPort(protocol, port)).

PcapPacketHandler<String> jpacketHandler = new PcapPacketHandler<String>(){
    public void nextPacket(PcapPacket packet, String user) {
        Tcp tcp = new Tcp();
        Ip4 ip = new Ip4();
        String protocol;
        long port;
        if(packet.hasHeader(ip)&&packet.hasHeader(tcp)){
            protocol = tcp.getName();
            port = tcp.source();
            try {
                //but the following line will cause an error 
                long pid = sigar.getProcPort("tcp", Long.toString(port));
                System.out.println("pid : " + pid);
            } catch (SigarException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if(filtering==false){
                printPacketResult(packet, tcp, ip, port, protocol);
            }else if(filtering==true){
                filteredPrintPacketResult(packet, tcp, ip, port, protocol);
            }

        }                   
    }
};

and the error I get is:

org.hyperic.sigar.SigarNotImplementedException: This method has not been implemented on this platform at org.hyperic.sigar.SigarNotImplementedException.(SigarNotImplementedException.java:28) at org.hyperic.sigar.Sigar.getProcPort(Native Method) at org.hyperic.sigar.Sigar.getProcPort(Sigar.java:632) at networkInfo.PackageCapture$1.nextPacket(PackageCapture.java:84) at networkInfo.PackageCapture$1.nextPacket(PackageCapture.java:1) at org.jnetpcap.Pcap.loop(Native Method) at org.jnetpcap.Pcap.loop(Unknown Source) at networkInfo.PackageCapture.startNet(PackageCapture.java:111) at networkInfo.FilterChoice.choice(FilterChoice.java:27) at networkInfo.Main.main(Main.java:6)

Is it a possible? If you have some information, please give me some hint.

Draken
  • 3,049
  • 13
  • 32
  • 49
kmin
  • 21
  • 4
  • The exception message would indicate that getting the PID via Sigar is not supported. What OS are you trying to run this on? According to the JavaDoc for that method, it is only supported on Linux, Windows 2003, Windows XP, and AIX. – Ben Damer Aug 01 '16 at 17:55

0 Answers0