1 package edu.uci.iotproject.analysis;
3 import org.pcap4j.core.*;
5 import java.time.Instant;
6 import java.util.Collections;
8 import java.util.concurrent.TimeoutException;
11 * TODO add class documentation.
13 * @author Janus Varmarken
15 public class TriggerTrafficExtractor implements PcapPacketFilter {
17 private final String mPcapFilePath;
18 private final List<Instant> mTriggerTimes;
19 private final String mDeviceIp;
21 private int mTriggerIndex = 0;
24 private static final int INCLUSION_WINDOW_MILLIS = 3_000;
26 public TriggerTrafficExtractor(String pcapFilePath, List<Instant> triggerTimes, String deviceIp) throws PcapNativeException, NotOpenException {
27 mPcapFilePath = pcapFilePath;
28 // Ensure that trigger times are sorted in ascending as we rely on this fact in the logic that works out if a
29 // packet is related to a trigger.
30 Collections.sort(triggerTimes, (i1, i2) -> {
31 if (i1.isBefore(i2)) return -1;
32 else if (i2.isBefore(i1)) return 1;
35 mTriggerTimes = Collections.unmodifiableList(triggerTimes);
40 public void performExtraction(PacketListener... extractedPacketsConsumers) throws PcapNativeException, NotOpenException, TimeoutException {
43 handle = Pcaps.openOffline(mPcapFilePath, PcapHandle.TimestampPrecision.NANO);
44 } catch (PcapNativeException pne) {
45 handle = Pcaps.openOffline(mPcapFilePath);
47 // Use the native support for BPF to immediately filter irrelevant traffic.
48 handle.setFilter("ip host " + mDeviceIp, BpfProgram.BpfCompileMode.OPTIMIZE);
49 PcapHandleReader pcapReader = new PcapHandleReader(handle, this, extractedPacketsConsumers);
50 pcapReader.readFromHandle();
51 // Reset trigger index (in case client code chooses to rerun the extraction)
56 public boolean shouldIncludePacket(PcapPacket packet) {
57 if (mTriggerIndex >= mTriggerTimes.size()) {
58 // Don't include packet if we've exhausted the list of trigger times.
62 // TODO hmm, is this correct?
63 Instant trigger = mTriggerTimes.get(mTriggerIndex);
64 if (trigger.isBefore(packet.getTimestamp()) &&
65 packet.getTimestamp().isBefore(trigger.plusMillis(INCLUSION_WINDOW_MILLIS))) {
66 // Packet lies within INCLUSION_WINDOW_MILLIS after currently considered trigger, include it.
69 if (!trigger.isBefore(packet.getTimestamp())) {
70 // Packet is before currently considered trigger, so it shouldn't be included
73 // Packet is >= INCLUSION_WINDOW_MILLIS after currently considered trigger.
74 // Proceed to next trigger to see if it lies in range of that.
75 // Note that there's an assumption here that no two trigger intervals don't overlap!
77 return shouldIncludePacket(packet);