Search in sources :

Example 1 with IllegalRawDataException

use of org.pcap4j.packet.IllegalRawDataException in project trex-stateless-gui by cisco-system-traffic-generator.

the class RecordController method toEtherPkt.

private EthernetPacket toEtherPkt(CapturedPkt pkt) {
    byte[] pktBinary = Base64.getDecoder().decode(pkt.getBinary());
    EthernetPacket ethPkt = null;
    try {
        ethPkt = EthernetPacket.newPacket(pktBinary, 0, pktBinary.length);
    } catch (IllegalRawDataException e) {
        LOG.error("Save PCAP. Unable to parse pkt from server.", e);
        return null;
    }
    return ethPkt;
}
Also used : IllegalRawDataException(org.pcap4j.packet.IllegalRawDataException) EthernetPacket(org.pcap4j.packet.EthernetPacket)

Example 2 with IllegalRawDataException

use of org.pcap4j.packet.IllegalRawDataException 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);
    }
}
Also used : Window(javafx.stage.Window) DialogWindow(com.exalttech.trex.ui.dialog.DialogWindow) Packet(org.pcap4j.packet.Packet) EthernetPacket(org.pcap4j.packet.EthernetPacket) IllegalRawDataException(org.pcap4j.packet.IllegalRawDataException) IOException(java.io.IOException) Timestamp(java.sql.Timestamp) Profile(com.exalttech.trex.remote.models.profiles.Profile) TableProfile(com.exalttech.trex.ui.views.models.TableProfile) TrafficProfile(com.exalttech.trex.util.TrafficProfile) File(java.io.File)

Example 3 with IllegalRawDataException

use of org.pcap4j.packet.IllegalRawDataException in project trex-stateless-gui by cisco-system-traffic-generator.

the class TrexEthernetPacket method fixPacketLength.

private void fixPacketLength() {
    try {
        EthernetPacket newPacket = packet;
        if (packet.getRawData().length < getLength()) {
            byte[] pad = new byte[getLength() - packet.getRawData().length];
            newPacket = EthernetPacket.newPacket(ArrayUtils.addAll(packet.getRawData(), pad), 0, getLength());
        } else {
            newPacket = EthernetPacket.newPacket(packet.getRawData(), 0, getLength());
        }
        setPacket((EthernetPacket) newPacket);
    } catch (IllegalRawDataException ex) {
        Logger.getLogger(TrexEthernetPacket.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Also used : IllegalRawDataException(org.pcap4j.packet.IllegalRawDataException) EthernetPacket(org.pcap4j.packet.EthernetPacket)

Example 4 with IllegalRawDataException

use of org.pcap4j.packet.IllegalRawDataException in project trex-stateless-gui by cisco-system-traffic-generator.

the class PacketParser method parseFile.

/**
 * @param fileName
 * @param packetInfo
 */
public void parseFile(String fileName, PacketInfo packetInfo) {
    Packet packet;
    try {
        this.packetInfo = packetInfo;
        total_octetes = 0;
        PcapHandle handle = Pcaps.openOffline(fileName);
        packet = handle.getNextPacketEx();
        packetInfo.setPacket(packet);
        extractPacketInfo(packet);
        handle.close();
    } catch (PcapNativeException | EOFException | TimeoutException | NotOpenException ex) {
        packet = null;
        LOG.warn("Failed to extract packet info from first try");
    }
    if (packet == null) {
        try {
            LOG.info("Attempting alternate method to read Pcap file");
            byte[] pkt = FileUtils.readFileToByteArray(new File(fileName));
            packet = EthernetPacket.newPacket(pkt, 0, pkt.length);
            extractPacketInfo(packet);
        } catch (IOException | IllegalRawDataException ex) {
            LOG.error("Failed to read Pcap file", ex);
        }
    }
}
Also used : Packet(org.pcap4j.packet.Packet) TcpPacket(org.pcap4j.packet.TcpPacket) EthernetPacket(org.pcap4j.packet.EthernetPacket) IpV4Packet(org.pcap4j.packet.IpV4Packet) Dot1qVlanTagPacket(org.pcap4j.packet.Dot1qVlanTagPacket) UdpPacket(org.pcap4j.packet.UdpPacket) NotOpenException(org.pcap4j.core.NotOpenException) IllegalRawDataException(org.pcap4j.packet.IllegalRawDataException) PcapHandle(org.pcap4j.core.PcapHandle) EOFException(java.io.EOFException) PcapNativeException(org.pcap4j.core.PcapNativeException) IOException(java.io.IOException) File(java.io.File) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

EthernetPacket (org.pcap4j.packet.EthernetPacket)4 IllegalRawDataException (org.pcap4j.packet.IllegalRawDataException)4 File (java.io.File)2 IOException (java.io.IOException)2 Packet (org.pcap4j.packet.Packet)2 Profile (com.exalttech.trex.remote.models.profiles.Profile)1 DialogWindow (com.exalttech.trex.ui.dialog.DialogWindow)1 TableProfile (com.exalttech.trex.ui.views.models.TableProfile)1 TrafficProfile (com.exalttech.trex.util.TrafficProfile)1 EOFException (java.io.EOFException)1 Timestamp (java.sql.Timestamp)1 TimeoutException (java.util.concurrent.TimeoutException)1 Window (javafx.stage.Window)1 NotOpenException (org.pcap4j.core.NotOpenException)1 PcapHandle (org.pcap4j.core.PcapHandle)1 PcapNativeException (org.pcap4j.core.PcapNativeException)1 Dot1qVlanTagPacket (org.pcap4j.packet.Dot1qVlanTagPacket)1 IpV4Packet (org.pcap4j.packet.IpV4Packet)1 TcpPacket (org.pcap4j.packet.TcpPacket)1 UdpPacket (org.pcap4j.packet.UdpPacket)1