本文整理汇总了Java中org.pcap4j.core.PcapNetworkInterface类的典型用法代码示例。如果您正苦于以下问题:Java PcapNetworkInterface类的具体用法?Java PcapNetworkInterface怎么用?Java PcapNetworkInterface使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
PcapNetworkInterface类属于org.pcap4j.core包,在下文中一共展示了PcapNetworkInterface类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: _selectSuitableNetworkInterface
import org.pcap4j.core.PcapNetworkInterface; //导入依赖的package包/类
/**
* select a most suitable network interface according to the address
*
* @param address
* @return
*/
private SelectedInterface _selectSuitableNetworkInterface(InetAddress address) {
int similiarBytes = Integer.MIN_VALUE;
SelectedInterface selectedInterface = new SelectedInterface();
byte[] inputIpInBytes = address.getAddress();
for (PcapNetworkInterface currentInterface : _localPcapNetworkInterfaces) {
List<PcapAddress> addresses = currentInterface.getAddresses();
if (addresses != null) {
for (PcapAddress ipAddress : addresses) {
// make sure the address should be same type, all ipv4 or all ipv6
if (!_isSameTypeAddress(address, ipAddress.getAddress())) {
continue;
}
byte[] ipInBytes = ipAddress.getAddress().getAddress();
int currentSimiliarBytes = _similarBytes(inputIpInBytes, ipInBytes);
if (currentSimiliarBytes > similiarBytes) {
selectedInterface._selectedNetworkInterface = currentInterface;
selectedInterface._selectedIpAddress = ipAddress.getAddress();
similiarBytes = currentSimiliarBytes;
}
}
}
}
return selectedInterface;
}
示例2: show
import org.pcap4j.core.PcapNetworkInterface; //导入依赖的package包/类
void show(Config config) {
try {
List<PcapNetworkInterface> devices = Pcaps.findAllDevs();
List<String> names = devices.stream().map(PcapNetworkInterface::getName).collect(Collectors.toList());
ChoiceDialog<String> dialog = new ChoiceDialog<>(names.get(0), names);
dialog.initModality(Modality.WINDOW_MODAL);
dialog.setResizable(false);
dialog.setHeaderText("Choose a network interface:");
Optional<String> result = dialog.showAndWait();
if (result.isPresent()) {
config.setDevice(dialog.getSelectedItem());
MainWindow app = new MainWindow("Knitcap");
app.start(config);
}
} catch (PcapNativeException e) {
e.printStackTrace();
}
}
示例3: getMode
import org.pcap4j.core.PcapNetworkInterface; //导入依赖的package包/类
PromiscuousMode getMode(PcapNetworkInterface pi){
PromiscuousMode mode=null;
String string=(pi.getDescription()+":"+pi.getName()).toLowerCase();
if(string.contains("wireless")){
mode=PromiscuousMode.NONPROMISCUOUS;
}else {
mode=PromiscuousMode.PROMISCUOUS;
}
return mode;
}
示例4: toInterfaceAddresses
import org.pcap4j.core.PcapNetworkInterface; //导入依赖的package包/类
@Nonnull
private static InterfaceAddress[] toInterfaceAddresses(@Nonnull PcapNetworkInterface iface) {
Preconditions.checkNotNull(iface, "PcapNetworkInterface was null.");
List<InterfaceAddress> out = new ArrayList<InterfaceAddress>();
for (PcapAddress address : iface.getAddresses())
out.add(toInterfaceAddress(address));
return out.toArray(new InterfaceAddress[out.size()]);
}
示例5: getDevices
import org.pcap4j.core.PcapNetworkInterface; //导入依赖的package包/类
public Map<String, String> getDevices() {
Map<String, String> map = new TreeMap<String, String>();
int id = 0;
for (PcapNetworkInterface dev: DEVICE_LIST) {
StringBuilder sb = new StringBuilder(100);
id++;
sb.append(id).append(":").append(dev.getName());
map.put(sb.toString(), sb.toString());
}
return map;
}
示例6: packetInit
import org.pcap4j.core.PcapNetworkInterface; //导入依赖的package包/类
/**
* ��Ʈ��ũ ��Ŷ �ʱ�ȭ
*
* @return PcapHandle
*/
public PcapHandle packetInit() {
PcapHandle handle = null;
String connIP = null;
String reg = "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$";
try {
// ���� IP �ּ�
InetAddress local = InetAddress.getLocalHost();
String localP = local.getHostAddress();
List<String> list = new ArrayList<String>();
List<String> ip_list = getLocalServerIp();
for(int i = 0; i < ip_list.size(); i++) {
if(ip_list.get(i).matches(reg)) {
System.out.println(ip_list.get(i));
list.add(ip_list.get(i));
}
}
// �ӽ��� ���� �����ǰ� 1���̻��϶�.
if(list.size() > 1) {
for(int i = 0; i < list.size(); i++) {
if(!list.get(i).equals(localP)) {
// ���� �����ǰ� �ƴѰ�,
connIP = list.get(i);
}
}
} else {
connIP = localP;
}
System.out.println("�������� ������: " + connIP);
// �����ϰ��� �ϴ� �������̽� ����
InetAddress addr = InetAddress.getByName(connIP);
PcapNetworkInterface nif = Pcaps.getDevByAddress(addr);
// ��Ŷ��� �ڵ鷯 ����
PromiscuousMode mode = PromiscuousMode.PROMISCUOUS;
handle = nif.openLive(SNAPLEN, mode, TIME_OUT);
} catch (Exception e) {
e.printStackTrace();
}
return handle;
}
示例7: getLocalInterfaces
import org.pcap4j.core.PcapNetworkInterface; //导入依赖的package包/类
public List<PcapNetworkInterface> getLocalInterfaces() {
if (!_initted) {
throw new IllegalStateException("Fail to init component", _initError);
}
return _localPcapNetworkInterfaces;
}
示例8: main
import org.pcap4j.core.PcapNetworkInterface; //导入依赖的package包/类
/**
* @param args the remote device ip lists split by comma
*/
public static void main(String[] args) {
// check libpcap file really exists
String libpcapKeyName = "org.pcap4j.core.pcapLibName";
String libpcapSet = System.getProperty(libpcapKeyName);
if (libpcapSet == null || libpcapSet.isEmpty()) {
System.out.println(String.format("no libpcap property set, try with -D%s=YourLibPcapFile", libpcapKeyName));
return;
}
File libpcapFile = new File(libpcapSet);
if (!(libpcapFile.exists() && libpcapFile.isFile())) {
System.out.println("libpcap file not exists " + libpcapFile.getAbsolutePath());
return;
}
System.out.println("Use libpcap file - " + libpcapFile.getAbsolutePath());
System.out.println();
System.out.println("Version - " + Pcaps.libVersion());
// list all interfaces
List<PcapNetworkInterface> localInterfaces = MacAddressHelper.getInstance().getLocalInterfaces();
System.out.println("List local interfaces");
for (PcapNetworkInterface localIntf : localInterfaces) {
System.out.println("\t" + localIntf);
}
if (args == null || args.length == 0) {
System.out.println("No remote device ips provided. try with arguments IP1,IP2 ....");
return;
}
for (String ip : args) {
System.out.println("Start find mac for ip - " + ip);
try {
MacAddress mac = MacAddressHelper.getInstance().getMacAddress(InetAddress.getByName(ip));
System.out.println("The mac is - " + mac);
}
catch (UnknownHostException e) {
System.out.println("Unknown host " + ip);
e.printStackTrace();
}
}
MacAddressHelper.getInstance().shutdown();
}
示例9: getLocalAddr
import org.pcap4j.core.PcapNetworkInterface; //导入依赖的package包/类
public static void getLocalAddr() throws InterruptedException, PcapNativeException, UnknownHostException, SocketException,
ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
if (Settings.getDouble("autoload", 0) == 1) {
addr = InetAddress.getByName(Settings.get("addr", ""));
return;
}
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
final JFrame frame = new JFrame("Network Device");
frame.setFocusableWindowState(true);
final JLabel ipLab = new JLabel("Select LAN IP obtained from Network Settings:", JLabel.LEFT);
final JComboBox<String> lanIP = new JComboBox<String>();
final JLabel lanLabel = new JLabel("If your device IP isn't in the dropdown, provide it below.");
final JTextField lanText = new JTextField(Settings.get("addr", ""));
ArrayList<InetAddress> inets = new ArrayList<InetAddress>();
for (PcapNetworkInterface i : Pcaps.findAllDevs()) {
for (PcapAddress x : i.getAddresses()) {
InetAddress xAddr = x.getAddress();
if (xAddr != null && x.getNetmask() != null && !xAddr.toString().equals("/0.0.0.0")) {
NetworkInterface inf = NetworkInterface.getByInetAddress(x.getAddress());
if (inf != null && inf.isUp() && !inf.isVirtual()) {
inets.add(xAddr);
lanIP.addItem((lanIP.getItemCount() + 1) + " - " + inf.getDisplayName() + " ::: " + xAddr.getHostAddress());
System.out.println("Found: " + lanIP.getItemCount() + " - " + inf.getDisplayName() + " ::: " + xAddr.getHostAddress());
}
}
}
}
if (lanIP.getItemCount() == 0) {
JOptionPane.showMessageDialog(null, "Unable to locate devices.\nPlease try running the program in Admin Mode.\nIf this does not work, you may need to reboot your computer.",
"Error", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
lanIP.setFocusable(false);
final JButton start = new JButton("Start");
start.addActionListener(e -> {
try {
if (lanText.getText().length() >= 7 && !lanText.getText().equals("0.0.0.0")) { // 7 is because the minimum field is 0.0.0.0
addr = InetAddress.getByName(lanText.getText());
System.out.println("Using IP from textfield: " + lanText.getText());
} else {
addr = inets.get(lanIP.getSelectedIndex());
System.out.println("Using device from dropdown: " + lanIP.getSelectedItem());
}
Settings.set("addr", addr.getHostAddress().replaceAll("/", ""));
frame.setVisible(false);
frame.dispose();
} catch (UnknownHostException e1) {
e1.printStackTrace();
}
});
frame.setLayout(new GridLayout(5, 1));
frame.add(ipLab);
frame.add(lanIP);
frame.add(lanLabel);
frame.add(lanText);
frame.add(start);
frame.setAlwaysOnTop(true);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
while (frame.isVisible())
Thread.sleep(10);
}
示例10: apply
import org.pcap4j.core.PcapNetworkInterface; //导入依赖的package包/类
@Override
public PcapNetworkInterfaceWrapper apply(PcapNetworkInterface pcapNetworkInterface) {
return new PcapNetworkInterfaceWrapper(pcapNetworkInterface);
}
示例11: PcapNetworkInterfaceWrapper
import org.pcap4j.core.PcapNetworkInterface; //导入依赖的package包/类
private PcapNetworkInterfaceWrapper(PcapNetworkInterface pcapNetworkInterface) {
if (pcapNetworkInterface == null) {
throw new IllegalArgumentException("Don't pass null.");
}
this.pcapNetworkInterface = pcapNetworkInterface;
}
示例12: getDeviceList
import org.pcap4j.core.PcapNetworkInterface; //导入依赖的package包/类
public static List<PcapNetworkInterface> getDeviceList() {
return DEVICE_LIST;
}