本文整理汇总了Java中org.jnetpcap.PcapIf类的典型用法代码示例。如果您正苦于以下问题:Java PcapIf类的具体用法?Java PcapIf怎么用?Java PcapIf使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PcapIf类属于org.jnetpcap包,在下文中一共展示了PcapIf类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: init
import org.jnetpcap.PcapIf; //导入依赖的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: showCaptureOptions
import org.jnetpcap.PcapIf; //导入依赖的package包/类
@FXML
protected void showCaptureOptions(ActionEvent event) {
List<PcapIf> interfaces = PcapManager.getInstance().getDeviceList();
OptionDialog optionDialog = new OptionDialog(interfaces);
Optional<NetworkAdapter> optional = optionDialog.showAndWait();
if (optional.isPresent()) { // If result is not null, start capture immediately
startCapture(new ActionEvent(menuItemStart, null));
} else {
NetworkAdapter networkAdapter = PcapManager.getInstance().getNetworkAdapter();
if (networkAdapter != null) {
setStatusInfo(String.format(Config.getString("label_select_xx"), networkAdapter));
} else {
setStatusWarning(Config.getString("label_select_nothing"));
}
}
}
示例3: testWinPcapMainCommentCreateStr
import org.jnetpcap.PcapIf; //导入依赖的package包/类
/**
* Test win pcap main comment create str.
*/
public void testWinPcapMainCommentCreateStr() {
String source = "rpcap://";
List<PcapIf> alldevs = new ArrayList<PcapIf>();
int r = WinPcap.findAllDevsEx(source, auth, alldevs, errbuf);
if (r != Pcap.OK) {
fail(errbuf.toString());
return;
}
StringBuilder buf = new StringBuilder();
WinPcap.createSrcStr(buf, WinPcap.SRC_IFLOCAL, null, null, alldevs.get(0)
.getName(), errbuf);
System.out.println("Our source string is " + alldevs.get(0).getName());
}
示例4: openLive
import org.jnetpcap.PcapIf; //导入依赖的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);
}
示例5: getPcapDevices
import org.jnetpcap.PcapIf; //导入依赖的package包/类
/**
* This function returns a list of <code>PcapIf</code>s with certain
* DLT.
*
* @param rescan If true, don't use cached data
* @param dlt DLT constant, for example PcapDLT.CONST_EN10MB
* @return
* @throws PcapException
*/
public static List<PcapIf> getPcapDevices(boolean rescan, int dlt) throws PcapException {
List<PcapIf> pcapDevices = getAllPcapDevices(rescan);
List<PcapIf> pcapDevicesWithRightDlt = new ArrayList<PcapIf>();
for (PcapIf pcapIf : pcapDevices) {
StringBuilder errbuf = new StringBuilder();
Pcap pcap = Pcap.openLive(pcapIf.getName(), 0, 0, 0, errbuf);
if (pcap != null && pcap.datalink() == dlt) {
pcapDevicesWithRightDlt.add(pcapIf);
}
}
return Collections.unmodifiableList(pcapDevicesWithRightDlt);
}
示例6: macToName
import org.jnetpcap.PcapIf; //导入依赖的package包/类
public static String macToName(String network_MAC) throws PcapException, IOException {
// If the list is empty, we populate it
if (allPcapDevices == null)
getAllPcapDevices(true);
for (PcapIf pcapIf : allPcapDevices) {
if(pcapIf.getHardwareAddress() != null &&
FormatUtils.mac(pcapIf.getHardwareAddress()).equals(network_MAC))
{
return pcapIf.getName();
}
}
throw new PcapException("Can't find device matching MAC address ");
}
示例7: listen
import org.jnetpcap.PcapIf; //导入依赖的package包/类
@Override
public CaptureQueue listen() throws InterruptedException, NoSuchElementException {
List<PcapIf> devices = NetInterfaces.getNetworkInterfaces();
if (devices.isEmpty()) {
throw new NoSuchElementException("No network interfaces found!");
}
CountDownLatch latch = new CountDownLatch(1);
AtomicReference<CaptureQueue> result = new AtomicReference<>();
for (PcapIf device : devices) {
NetInterfaceListener listener = new NetInterfaceListener(device, latch, result);
executor.execute(listener);
}
HCapUtils.logger.info("Waiting on listeners...");
latch.await();
HCapUtils.logger.info("Listener found!");
return result.get();
}
示例8: getNetworkInterfaces
import org.jnetpcap.PcapIf; //导入依赖的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;
}
示例9: run
import org.jnetpcap.PcapIf; //导入依赖的package包/类
@Override
protected void run(String ... args) throws Exception {
super.run(args);
if (interfaceId == -1) {
printNetworkInterfaces();
interfaceId = 0;
}
PcapIf device = selectPcapIf(interfaceId);
System.out.println("Recording from: " + device.getName() + " (" + device.getDescription() + ')');
StringBuilder err = new StringBuilder();
final Pcap pcap = Pcap.openLive(device.getName(), CaptureSettings.PACKET_SNAP_LENGTH, Pcap.MODE_NON_PROMISCUOUS, CaptureSettings.OPEN_LIVE_TIMEOUT_MILLIS, err);
if (pcap == null)
throw new IllegalArgumentException(err.toString());
if (captureFilter != null)
setupFilter(pcap, captureFilter);
runCaptureLoop(pcap, err);
}
示例10: listDevices
import org.jnetpcap.PcapIf; //导入依赖的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;
}
示例11: openAnyDevice
import org.jnetpcap.PcapIf; //导入依赖的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();
}
示例12: getLivePcapStream
import org.jnetpcap.PcapIf; //导入依赖的package包/类
private static final Pcap getLivePcapStream(final String inputDevice) throws IOException {
final PcapIf device = getNetworkDevice(inputDevice, false);
if (device == null) {
throw new IOException("No suitable device found, select an existing device.");
}
// Configuration constants.
final int snaplen = 64 * 1024; // Set buffer large enough for complete packets, no truncation
final int flags = Pcap.MODE_PROMISCUOUS; // Capture all packets that are found.
final int timeout = 10 * 1000; // 10 seconds in millis
// Buffer for c-type errors.
final StringBuilder errbuf = new StringBuilder();
// Open the live stream.
final Pcap pcap = Pcap.openLive(device.getName(), snaplen, flags, timeout, errbuf);
if (pcap == null) {
throw new IOException(errbuf.toString());
}
return pcap;
}
示例13: getNetworkDevices
import org.jnetpcap.PcapIf; //导入依赖的package包/类
/**
* List of network devices
*
* @return the list
*/
@Override
public List<Pair<Integer, String>> getNetworkDevices() {
final List<Pair<Integer, String>> list = new ArrayList<>();
int i = 0;
for (final PcapIf net : _devices) {
final String text = net.getDescription() == null || net.getDescription().trim().length() == 0 ? net.getName()
: (net.getDescription() + " (" + net.getName() + ")");
list.add(Pair.of(i++, text));
}
return list;
}
示例14: notifyInterface
import org.jnetpcap.PcapIf; //导入依赖的package包/类
@Override
public void notifyInterface() {
if (!_devices.isEmpty()) {
for (final INetworkInterfaceListener<PcapIf> listener : getListeners()) {
final PcapIf net = _devices.get(_index);
listener.notifyNewNetworkInterface(net, _gatewayMac.get(net));
}
}
}
示例15: init
import org.jnetpcap.PcapIf; //导入依赖的package包/类
/**
* @see org.leo.traceroute.core.IComponent#init(org.leo.traceroute.core.ServiceFactory)
*/
@SuppressWarnings("unchecked")
@Override
public void init(final ServiceFactory services) throws Exception {
super.init(services);
_schedule = Executors.newScheduledThreadPool(2);
if (_services != null) {
((INetworkService<PcapIf>) _services.getJnetcapNetwork()).addListener(this);
}
}