1 package edu.uci.iotproject.detection.layer2;
3 import edu.uci.iotproject.analysis.TriggerTrafficExtractor;
4 import org.pcap4j.core.PcapPacket;
6 import java.util.ArrayList;
10 * TODO add class documentation.
12 * @author Janus Varmarken
14 public class Layer2SequenceMatcher {
17 * The sequence this {@link Layer2SequenceMatcher} is searching for.
19 private final List<PcapPacket> mSequence;
22 * Buffer of actual packets seen so far that match the searched sequence (i.e., constitutes a subsequence of the
25 private final List<PcapPacket> mMatchedPackets = new ArrayList<>();
27 public Layer2SequenceMatcher(List<PcapPacket> sequence) {
31 public boolean matchPacket(PcapPacket packet) {
32 // The packet we want to match next.
33 PcapPacket expected = mSequence.get(mMatchedPackets.size());
34 // First verify if the received packet has the length we're looking for.
35 if (packet.getOriginalLength() == expected.getOriginalLength()) {
36 // Next apply timing constraints:
37 // - to be a match, the packet must have a later timestamp than any other packet currently matched
38 // - does adding the packet cause the max allowed time between first packet and last packet to be exceeded?
39 if (mMatchedPackets.size() > 0 &&
40 !packet.getTimestamp().isAfter(mMatchedPackets.get(mMatchedPackets.size()-1).getTimestamp())) {
43 if (mMatchedPackets.size() > 0 &&
44 packet.getTimestamp().
45 isAfter(mMatchedPackets.get(0).getTimestamp().
46 plusMillis(TriggerTrafficExtractor.INCLUSION_WINDOW_MILLIS))) {
50 // TODO (how to) check directions?
51 // This packet has a length matching next packet of searched sequence, so we store it and advance.
52 mMatchedPackets.add(packet);
53 if (mMatchedPackets.size() == mSequence.size()) {
54 // TODO report (to observers?) that we are done.
61 public int getMatchedPacketsCount() {
62 return mMatchedPackets.size();
65 public int getTargetSequencePacketCount() {
66 return mSequence.size();
69 public List<PcapPacket> getTargetSequence() {
73 public List<PcapPacket> getMatchedPackets() {
74 return mMatchedPackets;