Search in sources :

Example 1 with TSharkConfirmationImpl

use of com.att.aro.core.util.TSharkConfirmationImpl in project VideoOptimzer by attdevsupport.

the class PacketReaderLibraryImpl method readPacket.

@Override
public void readPacket(String packetfile, IPacketListener listener) throws IOException {
    // windows dependency only
    if (Util.isWindowsOS()) {
        checkNativeLib();
    }
    FileDetails fileDetails = parseFileDetails(packetfile);
    if (!fileDetails.isPcap() && !fileDetails.isPcapNG()) {
        LOGGER.error("Not a valid pcap file " + packetfile);
        return;
    }
    long start = System.currentTimeMillis();
    // Update system property org.pcap4j.af.inet6 to correct value based on original OS which captured pcap/pcapng file
    identifyOSInfo(packetfile);
    if (fileDetails.isPcap) {
        PcapHandle handle = null;
        try {
            handle = Pcaps.openOffline(packetfile);
        } catch (PcapNativeException e) {
            LOGGER.error("Error while reading pcap file " + packetfile, e);
            return;
        } catch (Exception ee) {
            LOGGER.error("Error unknow " + packetfile, ee);
            return;
        }
        try {
            int currentPacketNumber = 0;
            int totalPacketReads = 0;
            while (true) {
                try {
                    ++currentPacketNumber;
                    Packet pcap4jPacket = handle.getNextPacketEx();
                    // Get last packet's capture timestamp
                    Timestamp timestamp = handle.getTimestamp();
                    com.att.aro.core.packetreader.pojo.Packet packet = translatePcap4jPacket(timestamp.getTime() / 1000, timestamp.getNanos() / 1000, pcap4jPacket);
                    ++totalPacketReads;
                    listener.packetArrived(null, packet);
                } catch (EOFException e) {
                    LOGGER.info(String.format("Finished reading total %d packets out of %d packets for pcap file %s", totalPacketReads, currentPacketNumber - 1, packetfile));
                    break;
                } catch (Exception ex) {
                    LOGGER.debug("Error while reading packet number " + currentPacketNumber, ex);
                }
            }
        } catch (Exception e) {
            LOGGER.error("Error while reading pcap file " + packetfile, e);
        } finally {
            handle.close();
        }
    } else {
        TSharkConfirmationImpl tsharkConfirmation = new TSharkConfirmationImpl(externalProcessRunner);
        if (!tsharkConfirmation.checkTsharkVersion()) {
            throw new TsharkException("Wireshark is either not installed on your machine or its not in your path.");
        }
        handlePCAPNGFile(packetfile, listener);
    }
    LOGGER.info("Time to read pcap file in ms: " + (System.currentTimeMillis() - start));
}
Also used : TCPPacket(com.att.aro.core.packetreader.pojo.TCPPacket) IcmpV6CommonPacket(org.pcap4j.packet.IcmpV6CommonPacket) IcmpV4CommonPacket(org.pcap4j.packet.IcmpV4CommonPacket) UdpPacket(org.pcap4j.packet.UdpPacket) IPPacket(com.att.aro.core.packetreader.pojo.IPPacket) UnknownPacket(org.pcap4j.packet.UnknownPacket) Packet(org.pcap4j.packet.Packet) UDPPacket(com.att.aro.core.packetreader.pojo.UDPPacket) TcpPacket(org.pcap4j.packet.TcpPacket) IllegalPacket(org.pcap4j.packet.IllegalPacket) TSharkConfirmationImpl(com.att.aro.core.util.TSharkConfirmationImpl) TsharkException(com.att.aro.core.exception.TsharkException) PcapNativeException(org.pcap4j.core.PcapNativeException) Timestamp(java.sql.Timestamp) TsharkException(com.att.aro.core.exception.TsharkException) PcapNativeException(org.pcap4j.core.PcapNativeException) EOFException(java.io.EOFException) IOException(java.io.IOException) PcapHandle(org.pcap4j.core.PcapHandle) EOFException(java.io.EOFException)

Aggregations

TsharkException (com.att.aro.core.exception.TsharkException)1 IPPacket (com.att.aro.core.packetreader.pojo.IPPacket)1 TCPPacket (com.att.aro.core.packetreader.pojo.TCPPacket)1 UDPPacket (com.att.aro.core.packetreader.pojo.UDPPacket)1 TSharkConfirmationImpl (com.att.aro.core.util.TSharkConfirmationImpl)1 EOFException (java.io.EOFException)1 IOException (java.io.IOException)1 Timestamp (java.sql.Timestamp)1 PcapHandle (org.pcap4j.core.PcapHandle)1 PcapNativeException (org.pcap4j.core.PcapNativeException)1 IcmpV4CommonPacket (org.pcap4j.packet.IcmpV4CommonPacket)1 IcmpV6CommonPacket (org.pcap4j.packet.IcmpV6CommonPacket)1 IllegalPacket (org.pcap4j.packet.IllegalPacket)1 Packet (org.pcap4j.packet.Packet)1 TcpPacket (org.pcap4j.packet.TcpPacket)1 UdpPacket (org.pcap4j.packet.UdpPacket)1 UnknownPacket (org.pcap4j.packet.UnknownPacket)1