Removing error message for additional feature (relaxed matching).
[pingpong.git] / Code / Projects / PacketLevelSignatureExtractor / src / main / java / edu / uci / iotproject / detection / layer2 / Layer2RangeMatcher.java
index cc1a3d1c13956941a08d39fc125fdd731ac8e092..298a0abdf31093f33650643a5da2a311bfe41f6d 100644 (file)
@@ -23,20 +23,28 @@ public class Layer2RangeMatcher extends Layer2AbstractMatcher {
     private final List<PcapPacket> mLowerBound;
     private final List<PcapPacket> mUpperBound;
     private final double mEps;
+    private int mInclusionTimeMillis;
+    private int mSkippedPackets;
 
     /**
      * Create a {@code Layer2RangeMatcher}.
      * @param lowerBound The lower bound of the sequence to match against (search for).
      * @param upperBound The upper bound of the sequence to match against (search for).
      * @param eps The epsilon value used in the DBSCAN algorithm.
+     * @param trainingRouterWlanMac The training router's WLAN MAC (used for determining the direction of packets).
+     * @param routerWlanMac The target trace router's WLAN MAC (used for determining the direction of packets).
      */
-    public Layer2RangeMatcher(List<PcapPacket> lowerBound, List<PcapPacket> upperBound, double eps) {
+    public Layer2RangeMatcher(List<PcapPacket> lowerBound, List<PcapPacket> upperBound,
+                              int inclusionTimeMillis, double eps, String trainingRouterWlanMac, String routerWlanMac) {
         // TODO: Just use the lower bound since both lower and upper bounds' packets essentially have the same direction
         // TODO: for the same position in the array. Both arrays also have the same length.
-        super(lowerBound);
+        super(lowerBound, trainingRouterWlanMac, routerWlanMac);
         mLowerBound = lowerBound;
         mUpperBound = upperBound;
         mEps = eps;
+        mInclusionTimeMillis =
+                inclusionTimeMillis == 0 ? TriggerTrafficExtractor.INCLUSION_WINDOW_MILLIS : inclusionTimeMillis;
+        mSkippedPackets = 0;
     }
 
     /**
@@ -67,9 +75,16 @@ public class Layer2RangeMatcher extends Layer2AbstractMatcher {
         // Get representative of the packet we expect to match next.
         PcapPacket expectedLowerBound = mLowerBound.get(mMatchedPackets.size());
         PcapPacket expectedUpperBound = mUpperBound.get(mMatchedPackets.size());
+        int lowerBound = expectedLowerBound.getOriginalLength();
+        int upperBound = expectedUpperBound.getOriginalLength();
+        // Do strict matching if the lower and upper bounds are the same length
+        // Do range matching with eps otherwise
+        if (lowerBound != upperBound) {
+            lowerBound = lowerBound - (int) mEps;
+            upperBound = upperBound + (int) mEps;
+        }
         // First verify if the received packet has the length we're looking for (the length should be within the range).
-        if (expectedLowerBound.getOriginalLength() - (int) mEps <= packet.getOriginalLength() &&
-                packet.getOriginalLength() <= expectedUpperBound.getOriginalLength() + (int) mEps){
+        if (lowerBound <= packet.getOriginalLength() && packet.getOriginalLength() <= upperBound){
             // If this is the first packet, we only need to verify that its length is correct. Time constraints are
             // obviously satisfied as there are no previous packets. Furthermore, direction matches by definition as we
             // don't know the MAC of the device (or phone) in advance, so we can't enforce a rule saying "first packet
@@ -87,23 +102,20 @@ public class Layer2RangeMatcher extends Layer2AbstractMatcher {
                 return false;
             }
             // Next apply timing constraints:
-            // 1: to be a match, the packet must have a later timestamp than any other packet currently matched
-            // 2: does adding the packet cause the max allowed time between first packet and last packet to be exceeded?
+            // 1) to be a match, the packet must have a later timestamp than any other packet currently matched
+            // 2) does adding the packet cause the max allowed time between first packet and last packet to be exceeded?
             if (!packet.getTimestamp().isAfter(mMatchedPackets.get(getMatchedPacketsCount()-1).getTimestamp())) {
                 return false;
             }
             if (packet.getTimestamp().isAfter(mMatchedPackets.get(0).getTimestamp().
-                    plusMillis(TriggerTrafficExtractor.INCLUSION_WINDOW_MILLIS))) {
+                    plusMillis(mInclusionTimeMillis))) {
                 return false;
             }
             // If we made it here, it means that this packet has the expected length, direction, and obeys the timing
-            // constraints, so we store it and advance.
+            // constraints, so we store it and advance.zzzz
             mMatchedPackets.add(packet);
             if (mMatchedPackets.size() == mLowerBound.size()) {
                 // TODO report (to observers?) that we are done?
-                if (mMatchedPackets.size() == 4) {
-                    System.out.println();
-                }
             }
             return true;
         }
@@ -119,6 +131,6 @@ public class Layer2RangeMatcher extends Layer2AbstractMatcher {
     }
 
     public List<PcapPacket> getTargetUpperBound() {
-        return mLowerBound;
+        return mUpperBound;
     }
 }