本文整理汇总了Java中org.jnetpcap.Pcap.findAllDevs方法的典型用法代码示例。如果您正苦于以下问题:Java Pcap.findAllDevs方法的具体用法?Java Pcap.findAllDevs怎么用?Java Pcap.findAllDevs使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.jnetpcap.Pcap
的用法示例。
在下文中一共展示了Pcap.findAllDevs方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: init
import org.jnetpcap.Pcap; //导入方法依赖的package包/类
/**
* Initialize service
*
* @throws IOException
* @throws RouteException
*/
@Override
public void init(final ServiceFactory services) throws Exception {
super.init(services);
if (Env.INSTANCE.getOs() != OS.mac) {
services.updateStartup("init.sniffer.network", true);
try {
final StringBuilder sb = new StringBuilder();
final int r = Pcap.findAllDevs(_devices, sb);
if (r == Pcap.NOT_OK || _devices.isEmpty()) {
throw new IOException(sb.toString());
}
for (final PcapIf net : new ArrayList<>(_devices)) {
if (net.getAddresses().isEmpty()) {
_devices.remove(net);
} else {
_gatewayMac.put(net, net.getHardwareAddress());
}
}
} catch (final Throwable e) {
LOGGER.warn("Cannot find a suitable network device for tracing.", e);
}
}
}
示例2: openLive
import org.jnetpcap.Pcap; //导入方法依赖的package包/类
/**
* Open live.
*
* @param count
* the count
* @param handler
* the handler
*/
public static void openLive(long count, JPacketHandler<Pcap> handler) {
StringBuilder errbuf = new StringBuilder();
List<PcapIf> alldevs = new ArrayList<PcapIf>();
if (Pcap.findAllDevs(alldevs, errbuf) != Pcap.OK) {
throw new IllegalStateException(errbuf.toString());
}
Pcap pcap =
Pcap.openLive(alldevs.get(0).getName(),
Pcap.DEFAULT_SNAPLEN,
Pcap.DEFAULT_PROMISC,
Pcap.DEFAULT_TIMEOUT,
errbuf);
if (pcap == null) {
throw new IllegalArgumentException(errbuf.toString());
}
pcap.loop((int) count, handler, pcap);
}
示例3: getNetworkInterfaces
import org.jnetpcap.Pcap; //导入方法依赖的package包/类
/**
* Lists all enabled network interfaces on the system.
*/
public static List<PcapIf> getNetworkInterfaces() {
List<PcapIf> devices = new ArrayList<>();
StringBuilder errorBuilder = new StringBuilder();
if (Pcap.findAllDevs(devices, errorBuilder) == -1) {
HCapUtils.logger.severe("Unable to list interfaces! " + errorBuilder.toString());
return devices;
}
if (!devices.isEmpty()) {
HCapUtils.logger.info("Detected " + devices.size() + " network interfaces.");
} else {
HCapUtils.logger.info("No network interfaces detected.");
}
// not needed - see jnetpecap docs
// Pcap.freeAllDevs(deviceNames, errorBuilder);
return devices;
}
示例4: listDevices
import org.jnetpcap.Pcap; //导入方法依赖的package包/类
public static String listDevices() {
String ret = "";
NativeLibraryLoader.loadNativeLibs();
List<PcapIf> devices = new ArrayList<PcapIf>();
StringBuilder err = new StringBuilder();
if (Pcap.findAllDevs(devices, err) != Pcap.OK || devices.isEmpty()) {
log.error("Failed to find network devices");
throw new RuntimeException("Failed to find network devices!");
}
for (PcapIf dev : devices) {
ret += dev.getName() + "\n";
}
return ret;
}
示例5: openAnyDevice
import org.jnetpcap.Pcap; //导入方法依赖的package包/类
@Test
public void openAnyDevice() {
List<PcapIf> devices = new ArrayList<PcapIf>();
StringBuilder err = new StringBuilder();
if (Pcap.findAllDevs(devices, err) != Pcap.OK || devices.isEmpty()) {
throw new RuntimeException("Failed to find network devices!");
}
Pcap pcap = null;
for (PcapIf dev : devices) {
if (dev.getName().equalsIgnoreCase(Constants.ANY_DEVICE)) {
pcap = Pcap.create(Constants.ANY_DEVICE, err);
}
}
if (pcap == null) {
throw new RuntimeException("Failed to open \""
+ Constants.ANY_DEVICE + "\" device!");
}
pcap.close();
}
示例6: _testPcapCanSetRfMond
import org.jnetpcap.Pcap; //导入方法依赖的package包/类
public void _testPcapCanSetRfMond() throws SecurityException,
NoSuchMethodException {
List<PcapIf> alldevs = new ArrayList<PcapIf>();
StringBuilder errbuf = new StringBuilder();
Pcap.findAllDevs(alldevs, errbuf);
Pcap pcap = Pcap.create(alldevs.get(0).getName(), errbuf);
if (Pcap.isLoaded("canSetRfmon")) {
pcap.canSetRfmon();
}
pcap.close();
}
示例7: getAllPcapDevices
import org.jnetpcap.Pcap; //导入方法依赖的package包/类
/**
* This function returns a list of all available <code>PcapIf</code>s.
*
* @param rescan
* @return
* @throws PcapException
*/
public static List<PcapIf> getAllPcapDevices(boolean rescan) throws PcapException {
if (rescan || allPcapDevices == null) {
allPcapDevices = new ArrayList<PcapIf>();
StringBuilder errbuf = new StringBuilder();
int r = Pcap.findAllDevs(allPcapDevices, errbuf);
if (r != Pcap.OK) {
throw new PcapException("Can't read list of devices: " + errbuf.toString());
}
}
return Collections.unmodifiableList(allPcapDevices);
}
示例8: listNetworkInterfaces
import org.jnetpcap.Pcap; //导入方法依赖的package包/类
private static List<PcapIf> listNetworkInterfaces() throws Exception {
final StringBuilder errbuf = new StringBuilder();
List<PcapIf> result = new ArrayList<>(); // Will be filled with NICs
int r = Pcap.findAllDevs(result, errbuf);
if (r == Pcap.ERROR || result.isEmpty())
throw new Exception ("Can't read list of devices, error is :" + errbuf.toString());
return result;
}
示例9: createAndActivatePcap
import org.jnetpcap.Pcap; //导入方法依赖的package包/类
public static Pcap createAndActivatePcap(String device) {
NativeLibraryLoader.loadNativeLibs();
Pcap pcap = null;
List<PcapIf> devices = new ArrayList<PcapIf>();
StringBuilder err = new StringBuilder();
if (Pcap.findAllDevs(devices, err) != Pcap.OK || devices.isEmpty()) {
log.error("Failed to find network devices");
throw new RuntimeException("Failed to find network devices!");
}
for (PcapIf dev : devices) {
if (dev.getName().equalsIgnoreCase(device)) {
pcap = Pcap.create(device, err);
break;
}
}
if (pcap == null && devices.size() < 1) {
log.error("Failed to open device: " + device);
throw new RuntimeException("Failed to open \"" + device + "\" device!");
} else if (pcap == null) {
log.error("Failed to get requested device: " + device);
device = devices.get(0).getName();
log.error("Using first device found: " + device);
pcap = Pcap.create(device, err);
}
pcap.setSnaplen(Constants.SNAPLEN);
pcap.setPromisc(Constants.FLAGS);
pcap.setBufferSize(Constants.BUFFER_SIZE);
if (pcap.activate() != 0) {
log.error("Failed to activate device: " + device + " -> " + pcap.getErr());
throw new RuntimeException("Failed to activate \"" + device + "\" device!\n" + "Pcap error follows:\n" + pcap.getErr());
}
return pcap;
}
示例10: getDeviceList
import org.jnetpcap.Pcap; //导入方法依赖的package包/类
@Test
public void getDeviceList() {
List<PcapIf> devices = new ArrayList<PcapIf>();
StringBuilder err = new StringBuilder();
if (Pcap.findAllDevs(devices, err) != Pcap.OK || devices.isEmpty()) {
throw new RuntimeException("Failed to find network devices!");
}
for (PcapIf dev : devices) {
System.out.println(dev.getName() + " " + dev.getDescription());
}
}
示例11: generateListOfDevicesWithIPs
import org.jnetpcap.Pcap; //导入方法依赖的package包/类
private void generateListOfDevicesWithIPs()
{
List<PcapIf> deviceList = new ArrayList<>();
StringBuilder errbuf = new StringBuilder();
int r = Pcap.findAllDevs(deviceList, errbuf);
if (r == Pcap.ERROR || deviceList.isEmpty())
{
logger.log(Level.SEVERE, "Can't read list of devices, error is " + errbuf.toString());
return;
}
for (PcapIf device : deviceList)
{
String description = (device.getDescription() != null) ? device.getDescription() : "No description available";
String ip = null;
for (PcapAddr pcapAddr : device.getAddresses())
{
String temp = pcapAddr.getAddr().toString();
if (temp.contains(Ipv4Prefix))
{
ip = temp.replace(Ipv4Prefix, "");
break;
}
}
if (ip == null || ip.equals("[0.0.0.0]"))
continue;
byte[] hardwareAddress;
try
{
hardwareAddress = device.getHardwareAddress();
}
catch (IOException ioe)
{
hardwareAddress = new byte[8];
}
NICInfo nicInfo = new NICInfo(ip, hardwareAddress, description);
ipAndDescList.add(nicInfo);
nicInfoToPcapIf.put(nicInfo, device);
}
}
示例12: activateAnyDevice
import org.jnetpcap.Pcap; //导入方法依赖的package包/类
@Test
public void activateAnyDevice() {
List<PcapIf> devices = new ArrayList<PcapIf>();
StringBuilder err = new StringBuilder();
if (Pcap.findAllDevs(devices, err) != Pcap.OK || devices.isEmpty()) {
throw new RuntimeException("Failed to find network devices!");
}
for (PcapIf dev : devices) {
if (dev.getName().equalsIgnoreCase(Constants.ANY_DEVICE)) {
pcap = Pcap.create(Constants.ANY_DEVICE, err);
}
}
if (pcap == null) {
throw new RuntimeException("Failed to open \""
+ Constants.ANY_DEVICE + "\" device!");
}
pcap.setSnaplen(Constants.SNAPLEN);
pcap.setPromisc(Constants.FLAGS);
pcap.setBufferSize(Constants.BUFFER_SIZE);
if (pcap.activate() != 0) {
throw new RuntimeException("Failed to activate \""
+ Constants.ANY_DEVICE + "\" device!");
}
/*
* The pcap.loop is blocking so we use a thread as timer to stop
* capturing.
*/
Thread stopper = new Thread() {
@Override
public void run() {
try {
sleep(TestConstants.CAPTURE_DELAY);
pcap.breakloop();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
stopper.start();
int ret = pcap.loop(Pcap.LOOP_INFINITE,
new PcapPacketHandler<Object>() {
@Override
public void nextPacket(PcapPacket arg0, Object arg1) {
}
}, null);
/*
* As we break the loop we check the return value if this was correctly
* done.
*/
assertEquals(Pcap.ERROR_BREAK, ret);
pcap.close();
}
示例13: getNics
import org.jnetpcap.Pcap; //导入方法依赖的package包/类
@SuppressWarnings("deprecation")
public List <PcapIf> getNics(){
List<PcapIf> alldevs = new ArrayList<PcapIf>();
for(PcapIf p : alldevs ){
System.out.println(p.getName());
}
int r = Pcap.findAllDevs(alldevs, errbuf);
if (r == Pcap.NOT_OK || alldevs.isEmpty()) {
JOptionPane.showMessageDialog(null, "Not able to read list of devices \n"+ errbuf.toString(), "Error(Devices)", JOptionPane.ERROR_MESSAGE);
}
return alldevs;
}
示例14: getDeviceList
import org.jnetpcap.Pcap; //导入方法依赖的package包/类
public List<PcapIf> getDeviceList() {
List<PcapIf> pcapIfs = new ArrayList<>();
StringBuilder errBuf = new StringBuilder();
int code = Pcap.findAllDevs(pcapIfs, errBuf);
if (code != Pcap.OK || pcapIfs.isEmpty()) {
System.err.println("There is no network interface:" + errBuf);
}
return pcapIfs;
}