Search in sources :

Example 1 with Packet

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();
}
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 2 with Packet

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;
}
Also used : Packet(org.pcap4j.packet.Packet) TcpPacket(org.pcap4j.packet.TcpPacket) IpV4Packet(org.pcap4j.packet.IpV4Packet) UdpPacket(org.pcap4j.packet.UdpPacket) Inet4Address(java.net.Inet4Address) UnknownHostException(java.net.UnknownHostException)

Example 3 with 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;
}
Also used : Packet(org.pcap4j.packet.Packet) TcpPacket(org.pcap4j.packet.TcpPacket) IpV4Packet(org.pcap4j.packet.IpV4Packet) UdpPacket(org.pcap4j.packet.UdpPacket) Inet4Address(java.net.Inet4Address) UnknownHostException(java.net.UnknownHostException)

Example 4 with 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);
    }
}
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) TableProfile(com.exalttech.trex.ui.views.models.TableProfile) Profile(com.exalttech.trex.remote.models.profiles.Profile) TrafficProfile(com.exalttech.trex.util.TrafficProfile) File(java.io.File)

Example 5 with Packet

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;
}
Also used : TrexEthernetPacket(com.exalttech.trex.packets.TrexEthernetPacket) IpV4Packet(org.pcap4j.packet.IpV4Packet) Packet(org.pcap4j.packet.Packet) EthernetPacket(org.pcap4j.packet.EthernetPacket) TrexVlanPacket(com.exalttech.trex.packets.TrexVlanPacket) PcapHandle(org.pcap4j.core.PcapHandle)

Aggregations

Packet (org.pcap4j.packet.Packet)7 IpV4Packet (org.pcap4j.packet.IpV4Packet)4 PcapHandle (org.pcap4j.core.PcapHandle)3 EthernetPacket (org.pcap4j.packet.EthernetPacket)3 TcpPacket (org.pcap4j.packet.TcpPacket)3 UdpPacket (org.pcap4j.packet.UdpPacket)3 PacketInfo (com.exalttech.trex.ui.models.PacketInfo)2 EOFException (java.io.EOFException)2 File (java.io.File)2 IOException (java.io.IOException)2 Inet4Address (java.net.Inet4Address)2 UnknownHostException (java.net.UnknownHostException)2 TimeoutException (java.util.concurrent.TimeoutException)2 NotOpenException (org.pcap4j.core.NotOpenException)2 PcapNativeException (org.pcap4j.core.PcapNativeException)2 IllegalRawDataException (org.pcap4j.packet.IllegalRawDataException)2 TrexEthernetPacket (com.exalttech.trex.packets.TrexEthernetPacket)1 TrexVlanPacket (com.exalttech.trex.packets.TrexVlanPacket)1 Profile (com.exalttech.trex.remote.models.profiles.Profile)1 DialogWindow (com.exalttech.trex.ui.dialog.DialogWindow)1