use of org.pcap4j.packet.Packet in project trex-stateless-gui by cisco-system-traffic-generator.
the class ImportedPacketTableView method setPcapFile.
/**
* 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();
}
use of org.pcap4j.packet.Packet in project trex-stateless-gui by cisco-system-traffic-generator.
the class PacketUpdater method updateDstAddress.
/**
* Update destination IP address
*/
private Packet updateDstAddress(Packet packet) {
try {
if (importedProperties.isDestinationEnabled()) {
Inet4Address modifiedAddress = (Inet4Address) Inet4Address.getByAddress(convertIPToByte(importedProperties.getDstAddress()));
Packet.Builder builder = packet.getBuilder();
if (packet.get(IpV4Packet.class).getHeader().getDstAddr().getHostAddress().equals(defaultDstAddress)) {
builder.get(IpV4Packet.Builder.class).dstAddr(modifiedAddress);
} else {
builder.get(IpV4Packet.Builder.class).srcAddr(modifiedAddress);
}
packet = builder.build();
}
} catch (Exception ex) {
LOG.error("Error updating destination IP", ex);
}
return packet;
}
use of org.pcap4j.packet.Packet in project trex-stateless-gui by cisco-system-traffic-generator.
the class PacketUpdater method updateSrcAddress.
/**
* Update source IP address
*/
private Packet updateSrcAddress(Packet packet) {
try {
if (importedProperties.isSourceEnabled()) {
Inet4Address modifiedAddress = (Inet4Address) Inet4Address.getByAddress(convertIPToByte(importedProperties.getSrcAddress()));
Packet.Builder builder = packet.getBuilder();
if (packet.get(IpV4Packet.class).getHeader().getSrcAddr().getHostAddress().equals(defaultSrcAddress)) {
builder.get(IpV4Packet.Builder.class).srcAddr(modifiedAddress);
} else {
builder.get(IpV4Packet.Builder.class).dstAddr(modifiedAddress);
}
packet = builder.build();
}
} catch (Exception ex) {
LOG.error("Error updating source IP", ex);
}
return packet;
}
use of org.pcap4j.packet.Packet in project trex-stateless-gui by cisco-system-traffic-generator.
the class PacketTableView method handleExportPcapFile.
/**
* 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);
}
}
use of org.pcap4j.packet.Packet in project trex-stateless-gui by cisco-system-traffic-generator.
the class PacketUtil method getpcapPacketList.
/**
* 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;
}
Aggregations