+++ /dev/null
-package edu.uci.iotproject;
-
-import org.pcap4j.core.*;
-
-import java.io.EOFException;
-import java.util.concurrent.TimeoutException;
-/**
- * Opens and reads from a pcap file.
- * This class is nothing but a simple wrapper around some functionality in {@link Pcaps} and {@link PcapHandle} which
- * serves to simplify client code.
- * Note that the file is read in offline mode, i.e., this class does not support live processing of packets.
- *
- * @author Janus Varmarken {@literal <jvarmark@uci.edu>}
- * @author Rahmadi Trimananda {@literal <rtrimana@uci.edu>}
- */
-public class PcapReader {
-
- private final PcapHandle mHandle;
-
- /**
- * Create a new {@code PcapReader} that reads the file specified by the absolute path {@code fileName}.
- * @param fileName The absolute path to the pcap file to be read.
- * @param berkeleyPacketFilter A Berkeley Packet Filter to be applied when reading the PCAP file to filter out
- * unwanted packets immediately. May be {@code null} if no filter is to be applied.
- * @throws PcapNativeException If an error occurs in the pcap native library.
- * @throws NotOpenException If the pcap file cannot be opened.
- */
- public PcapReader(String fileName, String berkeleyPacketFilter) throws PcapNativeException, NotOpenException {
- PcapHandle handle;
- try {
- handle = Pcaps.openOffline(fileName, PcapHandle.TimestampPrecision.NANO);
- } catch (PcapNativeException pne) {
- handle = Pcaps.openOffline(fileName);
- }
- if(!handle.isOpen()) {
- throw new NotOpenException("could not open pcap file " + fileName);
- }
- if (berkeleyPacketFilter != null) {
- handle.setFilter(berkeleyPacketFilter, BpfProgram.BpfCompileMode.OPTIMIZE);
- }
- mHandle = handle;
- }
-
- /**
- * Reads the next packet in the pcap file.
- * @return The next packet in the pcap file, or {@code null} if all packets have been read.
- */
- public PcapPacket readNextPacket() {
- try {
- return mHandle.getNextPacketEx();
- } catch (EOFException eofe) {
- return null;
- } catch (PcapNativeException|TimeoutException|NotOpenException e) {
- // Wrap checked exceptions in unchecked exceptions to simplify client code.
- throw new RuntimeException(e);
- }
- }
-
-}
+++ /dev/null
-package edu.uci.iotproject.analysis;
-
-import edu.uci.iotproject.PcapPacketConsumer;
-import edu.uci.iotproject.PcapReader;
-import org.pcap4j.core.PcapPacket;
-
-import java.util.ArrayList;
-import java.util.List;
-
-/**
- * TODO add class documentation.
- *
- * @author Janus Varmarken {@literal <jvarmark@uci.edu>}
- * @author Rahmadi Trimananda {@literal <rtrimana@uci.edu>}
- */
-public class PcapProcessingPipeline {
-
- private final PcapReader mPcapReader;
- private final List<PcapPacketConsumer> mPacketConsumers;
- private final PcapPacketFilter mPacketFilter;
-
- public PcapProcessingPipeline(PcapReader pcapReader, PcapPacketFilter packetFilter) {
- mPcapReader = pcapReader;
- mPacketConsumers = new ArrayList<>();
- mPacketFilter = packetFilter;
- }
-
- public void addPcapPacketConsumer(PcapPacketConsumer packetConsumer) {
- mPacketConsumers.add(packetConsumer);
- }
-
- public void executePipeline() {
- PcapPacket packet;
- long count = 0;
- while ((packet = mPcapReader.readNextPacket()) != null) {
- if (mPacketFilter != null && !mPacketFilter.shouldIncludePacket(packet)) {
- continue;
- }
- for (PcapPacketConsumer consumer : mPacketConsumers) {
- consumer.consumePacket(packet);
- }
- count++;
- if (count % 1000 == 0) {
- System.out.println("Processed " + count + " packets");
- }
- }
- }
-
-}