PcapHandle implements Closeable { public PcapStat getStats() throws PcapNativeException, NotOpenException { if (!open) { throw new NotOpenException(); } if (!handleLock.readLock().tryLock()) { throw new NotOpenException(); } try { if (!open) { throw new NotOpenException(); } if (Platform.isWindows()) { IntByReference pcapStatSize = new IntByReference(); Pointer psp = PcapLibrary.INSTANCE.win_pcap_stats_ex(handle, pcapStatSize); if (!getError() .equals("Cannot retrieve the extended statistics from a file or a TurboCap port")) { if (pcapStatSize.getValue() != 24) { throw new PcapNativeException(getError()); } if (psp == null) { throw new PcapNativeException(getError()); } return new PcapStat(psp, true); } } pcap_stat ps = new pcap_stat(); ps.setAutoSynch(false); int rc = NativeMappings.pcap_stats(handle, ps); if (rc < 0) { throw new PcapNativeException(getError(), rc); } return new PcapStat(ps.getPointer(), false); } finally { handleLock.readLock().unlock(); } } PcapHandle(Pointer handle, TimestampPrecision timestampPrecision); private  PcapHandle(Builder builder); DataLinkType getDlt(); void setDlt(DataLinkType dlt); boolean isOpen(); String getFilteringExpression(); TimestampPrecision getTimestampPrecision(); void setDirection(PcapDirection direction); Timestamp getTimestamp(); Integer getOriginalLength(); int getSnapshot(); SwappedType isSwapped(); int getMajorVersion(); int getMinorVersion(); BpfProgram compileFilter(String bpfExpression, BpfCompileMode mode, Inet4Address netmask); void setFilter(String bpfExpression, BpfCompileMode mode, Inet4Address netmask); void setFilter(String bpfExpression, BpfCompileMode mode); void setFilter(BpfProgram prog); void setBlockingMode(BlockingMode mode); BlockingMode getBlockingMode(); Packet getNextPacket(); byte[] getNextRawPacket(); Packet getNextPacketEx(); byte[] getNextRawPacketEx(); void loop(int packetCount, PacketListener listener); void loop(int packetCount, PacketListener listener, Executor executor); void loop(int packetCount, RawPacketListener listener); void loop(int packetCount, RawPacketListener listener, Executor executor); int dispatch(int packetCount, PacketListener listener); int dispatch(int packetCount, PacketListener listener, Executor executor); int dispatch(int packetCount, RawPacketListener listener); int dispatch(int packetCount, RawPacketListener listener, Executor executor); PcapDumper dumpOpen(String filePath); void loop(int packetCount, PcapDumper dumper); void breakLoop(); void sendPacket(Packet packet); void sendPacket(byte[] bytes); void sendPacket(byte[] bytes, int len); PcapStat getStats(); List<DataLinkType> listDatalinks(); String getError(); @Override void close(); @Override String toString(); static final Inet4Address PCAP_NETMASK_UNKNOWN; }
@Test public void testGetStats() throws Exception { if (ph != null) { ph.close(); } List<PcapNetworkInterface> nifs = Pcaps.findAllDevs(); if (nifs.isEmpty()) { ph = Pcaps.openDead(DataLinkType.EN10MB, 2048); try { ph.getStats(); fail("getStats on a pcap_open_dead pcap_t should throw a PcapNativeException."); } catch (PcapNativeException e) { assertEquals("Statistics aren't available from a pcap_open_dead pcap_t", e.getMessage()); } } else { try { ph = nifs.get(0).openLive(55555, PromiscuousMode.PROMISCUOUS, 100); PcapStat ps = ph.getStats(); assertNotNull(ps); } catch (PcapNativeException e) { assertTrue( "The exception should complain about permission to capture.", e.getMessage().contains("You don't have permission to capture on that device")); } } }