本文整理汇总了Java中org.pcap4j.core.PcapNativeException类的典型用法代码示例。如果您正苦于以下问题:Java PcapNativeException类的具体用法?Java PcapNativeException怎么用?Java PcapNativeException使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PcapNativeException类属于org.pcap4j.core包,在下文中一共展示了PcapNativeException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: handleExportPcapFile
import org.pcap4j.core.PcapNativeException; //导入依赖的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);
}
}
示例2: encodePcapFile
import org.pcap4j.core.PcapNativeException; //导入依赖的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;
}
}
示例3: testTCPProfile
import org.pcap4j.core.PcapNativeException; //导入依赖的package包/类
@Test(dataProvider = "packetData", dependsOnGroups = {"init"})
public void testTCPProfile(PacketData packetData) throws IOException, InterruptedException, PcapNativeException, NotOpenException, URISyntaxException {
TrexEthernetPacket ethernetPacket = buildIPV4Packet(packetData);
// prepare and save yaml data
LOG.info("Prepare and save Yaml file");
packetUtil.prepareAndSaveYamlFile(ethernetPacket.getPacket().getRawData(), packetData);
//Generate pcap files
LOG.info("Generate Pcap file for " + packetData.getTestFileName() + ".yaml");
packetUtil.generatePcapFile(packetData.getTestFileName());
// compare pcaps
boolean result = packetUtil.comparePcaps(packetData.getTestFileName(), "generated_" + packetData.getTestFileName());
Assert.assertEquals(result, true, "Invalid generated " + packetData.getTestFileName() + " pcap. ");
}
示例4: buildIPV4Packet
import org.pcap4j.core.PcapNativeException; //导入依赖的package包/类
/**
* Build IPV4 packet
*
* @param packetData
* @return
* @throws IOException
* @throws InterruptedException
* @throws PcapNativeException
* @throws NotOpenException
* @throws java.net.URISyntaxException
*/
public TrexEthernetPacket buildIPV4Packet(PacketData packetData) throws IOException, InterruptedException, PcapNativeException, NotOpenException, URISyntaxException {
LOG.info("create ethernet packet");
int packetLength = PacketBuilderHelper.getPacketLength(packetData.getPacketLength().getLengthType(), packetData.getPacketLength().getFrameLength(), packetData.getPacketLength().getMaxLength());
Payload payload = packetUtil.getPayload(packetData.getPayload());
TrexEthernetPacket ethernetPacket = packetUtil.prepareEthernetPacket(packetData, packetLength, payload);
// define VLAN
IpV4Packet.Builder ipV4Packet = getIPV4PacketBuilder(PacketBuilderHelper.getIPV4TotalLength(packetData.isTaggedVlan()), packetLength, payload, packetData);
if (!packetData.isTaggedVlan()) {
LOG.info("Add IPV4 packet");
ethernetPacket.buildPacket(ipV4Packet);
} else {
packetUtil.addVlanToPacket(ethernetPacket, ipV4Packet);
}
return ethernetPacket;
}
示例5: testIPV4Profile
import org.pcap4j.core.PcapNativeException; //导入依赖的package包/类
@Test(dataProvider = "packetData", dependsOnGroups = {"init"})
public void testIPV4Profile(PacketData packetData) throws IOException, InterruptedException, PcapNativeException, NotOpenException, URISyntaxException {
TrexEthernetPacket ethernetPacket = buildIPV4Packet(packetData);
// prepare and save yaml data
LOG.info("Prepare and save Yaml file");
packetUtil.prepareAndSaveYamlFile(ethernetPacket.getPacket().getRawData(), packetData);
//Generate pcap files
LOG.info("Generate Pcap file for " + packetData.getTestFileName() + ".yaml");
packetUtil.generatePcapFile(packetData.getTestFileName());
// compare pcaps
boolean result = packetUtil.comparePcaps(packetData.getTestFileName(), "generated_" + packetData.getTestFileName());
Assert.assertEquals(result, true, "Invalid generated " + packetData.getTestFileName() + " pcap. ");
}
示例6: testUDPProfile
import org.pcap4j.core.PcapNativeException; //导入依赖的package包/类
@Test(dataProvider = "packetData", dependsOnGroups = {"init"})
public void testUDPProfile(PacketData packetData) throws IOException, InterruptedException, PcapNativeException, NotOpenException, URISyntaxException {
TrexEthernetPacket ethernetPacket = buildIPV4Packet(packetData);
// prepare and save yaml data
LOG.info("Prepare and save Yaml file");
packetUtil.prepareAndSaveYamlFile(ethernetPacket.getPacket().getRawData(), packetData);
//Generate pcap files
LOG.info("Generate Pcap file for " + packetData.getTestFileName() + ".yaml");
packetUtil.generatePcapFile(packetData.getTestFileName());
// compare pcaps
boolean result = packetUtil.comparePcaps(packetData.getTestFileName(), "generated_" + packetData.getTestFileName());
Assert.assertEquals(result, true, "Invalid generated " + packetData.getTestFileName() + " pcap. ");
}
示例7: testEthernetProfile
import org.pcap4j.core.PcapNativeException; //导入依赖的package包/类
@Test(dataProvider = "packetData", dependsOnGroups = {"init"})
public void testEthernetProfile(PacketData packetData) throws IOException, InterruptedException, PcapNativeException, NotOpenException, URISyntaxException {
// create profile
LOG.info("create ethernet packet");
int packetLength = PacketBuilderHelper.getPacketLength(packetData.getPacketLength().getLengthType(), packetData.getPacketLength().getFrameLength(), packetData.getPacketLength().getMaxLength());
Payload payload = packetUtil.getPayload(packetData.getPayload());
TrexEthernetPacket ethernetPacket = packetUtil.prepareEthernetPacket(packetData, packetLength, payload);
if (!packetData.isTaggedVlan()) {
ethernetPacket.buildPacket(null);
} else {
packetUtil.addVlanToPacket(ethernetPacket, null);
}
// prepare and save yaml data
LOG.info("Prepare and save Yaml file");
packetUtil.prepareAndSaveYamlFile(ethernetPacket.getPacket().getRawData(), packetData);
//Generate pcap files
LOG.info("Generate Pcap file for " + packetData.getTestFileName() + ".yaml");
packetUtil.generatePcapFile(packetData.getTestFileName());
// compare pcaps
boolean result = packetUtil.comparePcaps(packetData.getTestFileName(), "generated_" + packetData.getTestFileName());
Assert.assertEquals(result, true, "Invalid generated " + packetData.getTestFileName() + " pcap. ");
}
示例8: show
import org.pcap4j.core.PcapNativeException; //导入依赖的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();
}
}
示例9: readTOPSsample
import org.pcap4j.core.PcapNativeException; //导入依赖的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();
}
示例10: readDEEPsample
import org.pcap4j.core.PcapNativeException; //导入依赖的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();
}
示例11: readDEEPSample
import org.pcap4j.core.PcapNativeException; //导入依赖的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();
}
示例12: setPcapFile
import org.pcap4j.core.PcapNativeException; //导入依赖的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();
}
示例13: comparePcaps
import org.pcap4j.core.PcapNativeException; //导入依赖的package包/类
/**
* Compare 2 pcap files, return true if equal, otherwise return false
*
* @param firstPCAP
* @param secondPCAP
* @return
* @throws PcapNativeException
* @throws NotOpenException
*/
public boolean comparePcaps(String firstPCAP, String secondPCAP) throws PcapNativeException, NotOpenException, URISyntaxException {
LOG.info("Comparing pcap files");
List<String> firstPcapPacketList = getpcapPacketList(getClass().getResource("/pcaps/" + firstPCAP + ".pcap").toURI().getPath());
List<String> secondPcapPacketList = getpcapPacketList(filePath + secondPCAP + ".pcap");
if (firstPcapPacketList.size() != secondPcapPacketList.size()) {
return false;
}
boolean equal = true;
for (int index = 0; index < firstPcapPacketList.size(); index++) {
equal = equal && (firstPcapPacketList.get(index).equals(secondPcapPacketList.get(index)));
}
return equal;
}
示例14: getpcapPacketList
import org.pcap4j.core.PcapNativeException; //导入依赖的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;
}
示例15: start
import org.pcap4j.core.PcapNativeException; //导入依赖的package包/类
public void start(String device) {
destroy();
try {
capture = new Capture(this);
capture.capture(device);
} catch (PcapNativeException | NotOpenException e) {
e.printStackTrace();
}
}