Search in sources :

Example 1 with PcapNativeException

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

the class TrafficProfile method encodePcapFile.

/**
 * @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;
    }
}
Also used : Packet(org.pcap4j.packet.Packet) NotOpenException(org.pcap4j.core.NotOpenException) PcapHandle(org.pcap4j.core.PcapHandle) PcapNativeException(org.pcap4j.core.PcapNativeException) IOException(java.io.IOException) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with PcapNativeException

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

the class ImportPcapWizardController method handleSelectPcapBtnClicked.

/**
 * Handle select pcap button click event
 * @param event
 */
@FXML
public void handleSelectPcapBtnClicked(ActionEvent event) {
    if (importedPacketPropertiesView.isValidInputValues()) {
        String loadFolderPath = PreferencesManager.getInstance().getLoadLocation();
        Stage owner = (Stage) wizardViewContainer.getScene().getWindow();
        File pcapFile = FileManager.getSelectedFile("Open Pcap File", "", owner, FileType.PCAP, loadFolderPath, false);
        if (pcapFile != null) {
            try {
                if (importedPacketTableView.setPcapFile(pcapFile)) {
                    wizardViewContainer.getChildren().clear();
                    wizardViewContainer.getChildren().add(importedPacketTableView);
                    updateBtnState(false);
                } else {
                    TrexAlertBuilder.build().setType(Alert.AlertType.ERROR).setContent("Invalid Pcap, it should be one flow with IPV4 packets").getAlert().showAndWait();
                }
            } catch (PcapNativeException | TimeoutException | NotOpenException ex) {
                TrexAlertBuilder.build().setType(Alert.AlertType.ERROR).setTitle("Import error").setHeader("An error has occurred while attempting to import.").setContent(ex.getLocalizedMessage()).getAlert().showAndWait();
            }
        }
    }
}
Also used : NotOpenException(org.pcap4j.core.NotOpenException) Stage(javafx.stage.Stage) PcapNativeException(org.pcap4j.core.PcapNativeException) File(java.io.File) TimeoutException(java.util.concurrent.TimeoutException) FXML(javafx.fxml.FXML)

Example 3 with PcapNativeException

use of org.pcap4j.core.PcapNativeException 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

TimeoutException (java.util.concurrent.TimeoutException)3 NotOpenException (org.pcap4j.core.NotOpenException)3 PcapNativeException (org.pcap4j.core.PcapNativeException)3 File (java.io.File)2 IOException (java.io.IOException)2 PcapHandle (org.pcap4j.core.PcapHandle)2 Packet (org.pcap4j.packet.Packet)2 EOFException (java.io.EOFException)1 FXML (javafx.fxml.FXML)1 Stage (javafx.stage.Stage)1 Dot1qVlanTagPacket (org.pcap4j.packet.Dot1qVlanTagPacket)1 EthernetPacket (org.pcap4j.packet.EthernetPacket)1 IllegalRawDataException (org.pcap4j.packet.IllegalRawDataException)1 IpV4Packet (org.pcap4j.packet.IpV4Packet)1 TcpPacket (org.pcap4j.packet.TcpPacket)1 UdpPacket (org.pcap4j.packet.UdpPacket)1