Search in sources :

Example 1 with NotOpenException

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

use of org.pcap4j.core.NotOpenException 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();
}
Also used : PacketParser(com.exalttech.trex.ui.views.streams.viewer.PacketParser) Packet(org.pcap4j.packet.Packet) NotOpenException(org.pcap4j.core.NotOpenException) PcapHandle(org.pcap4j.core.PcapHandle) ArrayList(java.util.ArrayList) EOFException(java.io.EOFException) PacketInfo(com.exalttech.trex.ui.models.PacketInfo) PcapNativeException(org.pcap4j.core.PcapNativeException) TimeoutException(java.util.concurrent.TimeoutException)

Example 3 with NotOpenException

use of org.pcap4j.core.NotOpenException 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 PcapHandle (org.pcap4j.core.PcapHandle)3 PcapNativeException (org.pcap4j.core.PcapNativeException)3 EOFException (java.io.EOFException)2 IOException (java.io.IOException)2 Packet (org.pcap4j.packet.Packet)2 PacketInfo (com.exalttech.trex.ui.models.PacketInfo)1 PacketParser (com.exalttech.trex.ui.views.streams.viewer.PacketParser)1 File (java.io.File)1 ArrayList (java.util.ArrayList)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