本文整理汇总了Java中org.pcap4j.core.Pcaps类的典型用法代码示例。如果您正苦于以下问题:Java Pcaps类的具体用法?Java Pcaps怎么用?Java Pcaps使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Pcaps类属于org.pcap4j.core包,在下文中一共展示了Pcaps类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: checkPCap
import org.pcap4j.core.Pcaps; //导入依赖的package包/类
/** Check the WinPcap lib installation. */
private static boolean checkPCap() {
try {
System.out.println("Pcap Info: " + Pcaps.libVersion());
} catch (Error e) {
e.printStackTrace();
message("You MUST have NPCap or WinPCap installed to allow this program to monitor the lobby!"
+ (Desktop.isDesktopSupported() ? "\nAn installer link will attempt to open." : "Please go to https://www.winpcap.org/ and install it."));
if (Desktop.isDesktopSupported()) {
try {
Desktop.getDesktop().browse(new URL("https://nmap.org/npcap/").toURI());
} catch (IOException | URISyntaxException e1) {
e1.printStackTrace();
message("We couldn't open the URL for you, so go to https://nmap.org/npcap/ and install it!");
}
}
return false;
}
return true;
}
示例2: handleExportPcapFile
import org.pcap4j.core.Pcaps; //导入依赖的package包/类
/**
* Export stream to pcap file
*/
public void handleExportPcapFile() {
try {
Profile p = tabledata.getProfiles().get(streamPacketTableView.getSelectionModel().getSelectedIndex());
String packetBinary = p.getStream().getPacket().getBinary();
byte[] pkt = Base64.decodeBase64(packetBinary);
Packet packet = EthernetPacket.newPacket(pkt, 0, pkt.length);
File pcapFile = File.createTempFile("temp-file-name", ".pcap");
PcapHandle handle = Pcaps.openDead(DataLinkType.EN10MB, 65536);
PcapDumper dumper = handle.dumpOpen(pcapFile.getAbsolutePath());
Timestamp ts = new Timestamp(0);
dumper.dump(packet, ts);
dumper.close();
handle.close();
String fileName = p.getName() + ".pcap";
Window owner = streamPacketTableView.getScene().getWindow();
FileManager.exportFile("Save Pcap File", fileName, pcapFile, owner, FileType.PCAP);
} catch (IllegalRawDataException | IOException | PcapNativeException | NotOpenException ex) {
LOG.error("Error during generate JSON file", ex);
}
}
示例3: encodePcapFile
import org.pcap4j.core.Pcaps; //导入依赖的package包/类
/**
* @param binaryFile
* @return Encodes the bytes array of a PCAP File using Base64
*/
public String encodePcapFile(String binaryFile) {
try {
PcapHandle handle = Pcaps.openOffline(binaryFile);
Packet packet = handle.getNextPacketEx();
handle.close();
byte[] pkt = packet.getRawData();
byte[] bytesEncoded = Base64.encodeBase64(pkt);
return new String(bytesEncoded);
} catch (IOException | PcapNativeException | TimeoutException | NotOpenException ex) {
LOG.error("Error encoding pcap file", ex);
return binaryFile;
}
}
示例4: show
import org.pcap4j.core.Pcaps; //导入依赖的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();
}
}
示例5: readTOPSsample
import org.pcap4j.core.Pcaps; //导入依赖的package包/类
private void readTOPSsample() throws PcapNativeException, InterruptedException, NotOpenException {
final PcapHandle handle = Pcaps.openOffline("C:\\Users\\wojci\\Desktop\\IEX\\20171104_IEXTP1_TOPS1.6.pcap", PcapHandle.TimestampPrecision.NANO);
handle.loop(-1, new PacketListener() {
@Override
public void gotPacket(final Packet packet) {
final byte[] data = packet.getPayload().getPayload().getPayload().getRawData();
final IEXSegment block = IEXTOPSMessageBlock.createIEXSegment(data);
System.out.println(block);
}
});
handle.close();
}
示例6: readDEEPsample
import org.pcap4j.core.Pcaps; //导入依赖的package包/类
private void readDEEPsample() throws PcapNativeException, InterruptedException, NotOpenException {
final PcapHandle handle = Pcaps.openOffline("path_to_pcap", PcapHandle.TimestampPrecision.MICRO);
handle.loop(-1, new PacketListener() {
@Override
public void gotPacket(final Packet packet) {
final byte[] data = packet.getPayload().getPayload().getPayload().getRawData();
final IEXSegment block = IEXDEEPMessageBlock.createIEXSegment(data);
System.out.println(block);
}
});
handle.close();
}
示例7: readDEEPSample
import org.pcap4j.core.Pcaps; //导入依赖的package包/类
private void readDEEPSample() throws PcapNativeException, NotOpenException, InterruptedException {
final PcapHandle handle = Pcaps.openOffline("path_to_pcap", PcapHandle.TimestampPrecision.NANO);
handle.loop(-1, new PacketListener() {
@Override
public void gotPacket(Packet packet) {
final byte[] data = packet.getPayload().getPayload().getPayload().getRawData();
final IEXSegment block = IEXDEEPMessageBlock.createIEXSegment(data);
for (final IEXMessage iexMessage : block.getMessages()) {
if (iexMessage instanceof IEXPriceLevelUpdateMessage) {
final IEXPriceLevelUpdateMessage iexPriceLevelUpdateMessage = (IEXPriceLevelUpdateMessage) iexMessage;
final IEXOrderBook iexOrderBook = orderBookStore.getOrderBook(iexPriceLevelUpdateMessage.getSymbol())
.orElseGet(() -> new IEXOrderBook(iexPriceLevelUpdateMessage.getSymbol()));
iexOrderBook.priceLevelUpdate(iexPriceLevelUpdateMessage);
orderBookStore.updateOrderBook(iexOrderBook);
if (iexOrderBook.getSymbol().equals("AAPL")) {
System.out.println(iexOrderBook);
}
}
}
}
});
handle.close();
}
示例8: setPcapFile
import org.pcap4j.core.Pcaps; //导入依赖的package包/类
/**
* Parse pcap file to get all streams
*
* @param pcapFile
* @return
*/
public boolean setPcapFile(File pcapFile) {
List<PacketInfo> packetInfoList = new ArrayList<>();
try {
PacketUpdater.getInstance().reset();
PcapHandle handler = Pcaps.openOffline(pcapFile.getAbsolutePath());
PacketParser parser = new PacketParser();
Packet packet;
while ((packet = handler.getNextPacketEx()) != null) {
if (!PacketUpdater.getInstance().validatePacket(packet)) {
break;
}
PacketInfo packetInfo = new PacketInfo();
packet = PacketUpdater.getInstance().updatePacketSrcDst(packet);
packetInfo.setPacket(packet);
packetInfo.setTimeStamp(handler.getTimestamp().getTime());
parser.parsePacket(packet, packetInfo);
packetInfoList.add(packetInfo);
}
} catch (EOFException e) {
LOG.info("End of pcap file");
} catch (PcapNativeException | TimeoutException | NotOpenException ex) {
LOG.error("Error parsing selectd pcap file", ex);
}
setTableData(packetInfoList);
return PacketUpdater.getInstance().isValidPacket();
}
示例9: getpcapPacketList
import org.pcap4j.core.Pcaps; //导入依赖的package包/类
/**
* Read pcap file to get all includes packet
*
* @param pcapFile
* @return
* @throws PcapNativeException
* @throws NotOpenException
*/
private List<String> getpcapPacketList(String pcapFile) throws PcapNativeException, NotOpenException {
PcapHandle handler = Pcaps.openOffline(pcapFile);
Packet packet = null;
List<String> packetList = new ArrayList<>();
while ((packet = handler.getNextPacket()) != null) {
packetList.add(Hex.encodeHexString(packet.getRawData()));
}
return packetList;
}
示例10: getAllNetworkInterfaces
import org.pcap4j.core.Pcaps; //导入依赖的package包/类
/**
* Returns all Pcap network interfaces relying on {@link Pcaps#findAllDevs()}.
*
* @return A {@link Iterable} of all {@link PcapNetworkInterfaceWrapper}s
*/
public static Iterable<PcapNetworkInterfaceWrapper> getAllNetworkInterfaces() {
try {
final Iterable<PcapNetworkInterfaceWrapper> allNetworkInterfaces = Iterables
.transform(Pcaps.findAllDevs(), PcapNetworkInterfaceWrapper.TRANSFORMER);
return allNetworkInterfaces;
} catch (PcapNativeException e) {
throw new RuntimeException(e);
}
}
示例11: getNetworkInterfaceByName
import org.pcap4j.core.Pcaps; //导入依赖的package包/类
/**
* Returns the Pcap network interface with the given name relying on {@link Pcaps#getDevByName(String)}. If no
* interface is found, null is returned.
*
* @param name The name of the Pcap network interface
* @return The network interface with the given name. Returns null, if no interface is found
*/
public static PcapNetworkInterfaceWrapper getNetworkInterfaceByName(String name) {
try {
return PcapNetworkInterfaceWrapper.TRANSFORMER.apply(Pcaps.getDevByName(name));
} catch (PcapNativeException e) {
throw new RuntimeException(e);
}
}
示例12: packetInit
import org.pcap4j.core.Pcaps; //导入依赖的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;
}
示例13: MacAddressHelper
import org.pcap4j.core.Pcaps; //导入依赖的package包/类
private MacAddressHelper() {
try {
_localPcapNetworkInterfaces = Pcaps.findAllDevs();
_executor = Executors.newScheduledThreadPool(_threadCount);
_IPv6_BROADCAST_IPADDRESS_PREFIX = Inet6Address.getByName("FF02::1:FF00:0000").getAddress();
_IPv6_BROADCAST_MACADDRESS_PREFIX = MacAddress.getByName("33:33:ff:00:00:00").getAddress();
_localAddresse2MacAddress = new HashMap<>();
Enumeration<NetworkInterface> localNetworkInterfaces = NetworkInterface.getNetworkInterfaces();
while (localNetworkInterfaces.hasMoreElements()) {
NetworkInterface nwInterface = localNetworkInterfaces.nextElement();
byte[] mac = nwInterface.getHardwareAddress();
Enumeration<InetAddress> addresses = nwInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress currentIp = addresses.nextElement();
if (mac == null) {
System.out.println("Can't find mac address for local ip=" + currentIp);
_localAddresse2MacAddress.put(currentIp, null);
}
else {
// although jdk said, the returned value is a mac address, but actually this may fail on some local mac address
// the length is NOT 6 bytes
if (mac.length != MacAddress.SIZE_IN_BYTES) {
_localAddresse2MacAddress.put(currentIp, null);
System.out.println(String.format("Found invalid mac address ip=%s,mac=%s", currentIp, ByteArrays.toHexString(mac,
":")));
}
else {
_localAddresse2MacAddress.put(currentIp, MacAddress.getByAddress(mac));
}
}
}
}
System.out.println(String.format("Mac Address helper init done localips=%d, threadPool=%d, readTimeout(ms)=%d, waitResponse(ms)" +
"=%d, waitReceiveTaskStart(ms)=%d",
_localAddresse2MacAddress.size(), _threadCount,
_readTimeoutInMillSeconds, _waitResponseTimeoutInMillSeconds, _waitReceiveTaskStartRunningInSeconds));
_initted = true;
}
catch (Throwable e) {
_initError = e;
}
}
示例14: _getFilter4IPv4
import org.pcap4j.core.Pcaps; //导入依赖的package包/类
private String _getFilter4IPv4(InetAddress remoteAddress, MacAddress localMacAddress, InetAddress localIpAdress) {
return String.format("arp and src host %s and dst host %s and ether dst %s",
remoteAddress.getHostAddress(),
localIpAdress.getHostAddress(),
Pcaps.toBpfString(localMacAddress));
}
示例15: _getFilter4IPv6
import org.pcap4j.core.Pcaps; //导入依赖的package包/类
private String _getFilter4IPv6(Inet6Address remoteAddress, MacAddress localMacAddress, Inet6Address localIpAdress) {
return String.format("icmp6 and src host %s and dst host %s and ether dst %s",
_removePercentInIPv6Address(remoteAddress.getHostAddress()),
_removePercentInIPv6Address(localIpAdress.getHostAddress()),
Pcaps.toBpfString(localMacAddress));
}