Cleanup of layer 2 sequence detection: get rid of some experimental classes
[pingpong.git] / Code / Projects / SmartPlugDetector / src / main / java / edu / uci / iotproject / detection / Layer2SequenceMatcher.java
1 package edu.uci.iotproject.detection;
2
3 import edu.uci.iotproject.analysis.TriggerTrafficExtractor;
4 import org.pcap4j.core.PcapPacket;
5
6 import java.util.ArrayList;
7 import java.util.List;
8
9 /**
10  * TODO add class documentation.
11  *
12  * @author Janus Varmarken
13  */
14 public class Layer2SequenceMatcher {
15
16     /**
17      * The sequence this {@link Layer2SequenceMatcher} is searching for.
18      */
19     private final List<PcapPacket> mSequence;
20
21     /**
22      * Buffer of actual packets seen so far that match the searched sequence (i.e., constitutes a subsequence of the
23      * searched sequence).
24      */
25     private final List<PcapPacket> mMatchedPackets = new ArrayList<>();
26
27     public Layer2SequenceMatcher(List<PcapPacket> sequence) {
28         mSequence = sequence;
29     }
30
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())) {
41                 return false;
42             }
43             if (mMatchedPackets.size() > 0 &&
44                     packet.getTimestamp().
45                             isAfter(mMatchedPackets.get(0).getTimestamp().
46                                     plusMillis(TriggerTrafficExtractor.INCLUSION_WINDOW_MILLIS))) {
47                 // Packet too
48                 return false;
49             }
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.
55             }
56             return true;
57         }
58         return false;
59     }
60
61     public int getMatchedPacketsCount() {
62         return mMatchedPackets.size();
63     }
64
65     public int getTargetSequencePacketCount() {
66         return mSequence.size();
67     }
68
69     public List<PcapPacket> getTargetSequence() {
70         return mSequence;
71     }
72
73     public List<PcapPacket> getMatchedPackets() {
74         return mMatchedPackets;
75     }
76 }