Modifying the value DELETED_SEQUENCES_OFF in the script from 0 to 1 as per bug/error...
[pingpong.git] / Code / Projects / PacketLevelSignatureExtractor / src / main / java / edu / uci / iotproject / detection / layer2 / Layer2RangeMatcher.java
1 package edu.uci.iotproject.detection.layer2;
2
3 import edu.uci.iotproject.analysis.TriggerTrafficExtractor;
4 import edu.uci.iotproject.util.PcapPacketUtils;
5 import org.pcap4j.core.PcapPacket;
6 import org.pcap4j.util.MacAddress;
7
8 import java.util.ArrayList;
9 import java.util.List;
10
11 /**
12  * Attempts to detect the presence of a specific packet sequence in the set of packets provided through multiple calls
13  * to {@link #matchPacket(PcapPacket)}, considering only layer 2 information. This class has the same flavor as the
14  * {@link Layer2SequenceMatcher} class.
15  *
16  * @author Janus Varmarken {@literal <jvarmark@uci.edu>}
17  * @author Rahmadi Trimananda {@literal <rtrimana@uci.edu>}
18  */
19 public class Layer2RangeMatcher extends Layer2AbstractMatcher {
20     /**
21      * The range this {@link Layer2RangeMatcher} is searching for.
22      */
23     private final List<PcapPacket> mLowerBound;
24     private final List<PcapPacket> mUpperBound;
25     private final double mEps;
26     private int mInclusionTimeMillis;
27     private int mSkippedPackets;
28
29     /**
30      * Create a {@code Layer2RangeMatcher}.
31      * @param lowerBound The lower bound of the sequence to match against (search for).
32      * @param upperBound The upper bound of the sequence to match against (search for).
33      * @param eps The epsilon value used in the DBSCAN algorithm.
34      * @param trainingRouterWlanMac The training router's WLAN MAC (used for determining the direction of packets).
35      * @param routerWlanMac The target trace router's WLAN MAC (used for determining the direction of packets).
36      */
37     public Layer2RangeMatcher(List<PcapPacket> lowerBound, List<PcapPacket> upperBound,
38                               int inclusionTimeMillis, double eps, String trainingRouterWlanMac, String routerWlanMac) {
39         // TODO: Just use the lower bound since both lower and upper bounds' packets essentially have the same direction
40         // TODO: for the same position in the array. Both arrays also have the same length.
41         super(lowerBound, trainingRouterWlanMac, routerWlanMac);
42         mLowerBound = lowerBound;
43         mUpperBound = upperBound;
44         mEps = eps;
45         mInclusionTimeMillis =
46                 inclusionTimeMillis == 0 ? TriggerTrafficExtractor.INCLUSION_WINDOW_MILLIS : inclusionTimeMillis;
47         mSkippedPackets = 0;
48     }
49
50     /**
51      * Attempt to advance this {@code Layer2RangeMatcher} by matching {@code packet} against the packet that this
52      * {@code Layer2RangeMatcher} expects as the next packet of the sequence it is searching for.
53      * @param packet
54      * @return {@code true} if this {@code Layer2SequenceMatcher} could advance by adding {@code packet} to its set of
55      *         matched packets, {@code false} otherwise.
56      */
57     public boolean matchPacket(PcapPacket packet) {
58         if (getMatchedPacketsCount() == getTargetSequencePacketCount()) {
59             // We already matched the entire sequence, so we can't match any more packets.
60             return false;
61         }
62
63         // Verify that new packet pertains to same flow as previously matched packets, if any.
64         if (getMatchedPacketsCount() > 0) {
65             MacAddress pktSrc = PcapPacketUtils.getEthSrcAddr(packet);
66             MacAddress pktDst = PcapPacketUtils.getEthDstAddr(packet);
67             MacAddress earlierPktSrc = PcapPacketUtils.getEthSrcAddr(mMatchedPackets.get(0));
68             MacAddress earlierPktDst = PcapPacketUtils.getEthDstAddr(mMatchedPackets.get(0));
69             if (!(pktSrc.equals(earlierPktSrc) && pktDst.equals(earlierPktDst) ||
70                     pktSrc.equals(earlierPktDst) && pktDst.equals(earlierPktSrc))) {
71                 return false;
72             }
73         }
74
75         // Get representative of the packet we expect to match next.
76         PcapPacket expectedLowerBound = mLowerBound.get(mMatchedPackets.size());
77         PcapPacket expectedUpperBound = mUpperBound.get(mMatchedPackets.size());
78         int lowerBound = expectedLowerBound.getOriginalLength();
79         int upperBound = expectedUpperBound.getOriginalLength();
80         // Do strict matching if the lower and upper bounds are the same length
81         // Do range matching with eps otherwise
82         if (lowerBound != upperBound) {
83             lowerBound = lowerBound - (int) mEps;
84             upperBound = upperBound + (int) mEps;
85         }
86         // First verify if the received packet has the length we're looking for (the length should be within the range).
87         if (lowerBound <= packet.getOriginalLength() && packet.getOriginalLength() <= upperBound){
88             // If this is the first packet, we only need to verify that its length is correct. Time constraints are
89             // obviously satisfied as there are no previous packets. Furthermore, direction matches by definition as we
90             // don't know the MAC of the device (or phone) in advance, so we can't enforce a rule saying "first packet
91             // must originate from this particular MAC".
92             if (getMatchedPacketsCount() == 0) {
93                 // Store packet as matched and advance.
94                 mMatchedPackets.add(packet);
95                 return true;
96             }
97             // Check if direction of packet matches expected direction.
98             boolean actualDirection = getPacketDirection(mMatchedPackets.get(getMatchedPacketsCount()-1),
99                     mPacketDirections[getMatchedPacketsCount()-1], packet);
100             boolean expectedDirection = mPacketDirections[getMatchedPacketsCount()];
101             if (actualDirection != expectedDirection) {
102                 return false;
103             }
104             // Next apply timing constraints:
105             // 1) to be a match, the packet must have a later timestamp than any other packet currently matched
106             // 2) does adding the packet cause the max allowed time between first packet and last packet to be exceeded?
107             if (!packet.getTimestamp().isAfter(mMatchedPackets.get(getMatchedPacketsCount()-1).getTimestamp())) {
108                 return false;
109             }
110             if (packet.getTimestamp().isAfter(mMatchedPackets.get(0).getTimestamp().
111                     plusMillis(mInclusionTimeMillis))) {
112                 return false;
113             }
114             // If we made it here, it means that this packet has the expected length, direction, and obeys the timing
115             // constraints, so we store it and advance.zzzz
116             mMatchedPackets.add(packet);
117             if (mMatchedPackets.size() == mLowerBound.size()) {
118                 // TODO report (to observers?) that we are done?
119             }
120             return true;
121         }
122         return false;
123     }
124
125     public int getTargetSequencePacketCount() {
126         return mLowerBound.size();
127     }
128
129     public List<PcapPacket> getTargetLowerBound() {
130         return mLowerBound;
131     }
132
133     public List<PcapPacket> getTargetUpperBound() {
134         return mUpperBound;
135     }
136 }