org.pcap4j.core.PcapHandle Java Examples

The following examples show how to use org.pcap4j.core.PcapHandle. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TrafficProfile.java    From trex-stateless-gui with Apache License 2.0 6 votes vote down vote up
/**
 * @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;
    }

}
 
Example #2
Source File: ImportedPacketTableView.java    From trex-stateless-gui with Apache License 2.0 5 votes vote down vote up
/**
 * Parse pcap file to get all streams
 *
 * @param pcapFile
 * @return
 */
public boolean setPcapFile(File pcapFile) throws PcapNativeException,
                                                 TimeoutException,
                                                 NotOpenException {
    List<PacketInfo> packetInfoList = new ArrayList<>();
    PacketUpdater.getInstance().reset();
    PcapHandle handler = Pcaps.openOffline(pcapFile.getAbsolutePath());
    PacketParser parser = new PacketParser();
    Packet packet;
    while (true) {
        try {
            packet = handler.getNextPacketEx();
        } catch (EOFException e) {
            break;
        }
        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);
    }
    setTableData(packetInfoList);
    return PacketUpdater.getInstance().isValidPacket();
}
 
Example #3
Source File: PacketUtil.java    From trex-stateless-gui with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}