Implementing relaxed matching for layer 2 and layer 3.
[pingpong.git] / Code / Projects / PacketLevelSignatureExtractor / src / main / java / edu / uci / iotproject / detection / layer2 / Layer2ClusterMatcher.java
index 0bfc6b2..9de3bd6 100644 (file)
@@ -8,10 +8,7 @@ import edu.uci.iotproject.detection.AbstractClusterMatcher;
 import edu.uci.iotproject.trafficreassembly.layer2.Layer2FlowObserver;
 import org.pcap4j.core.*;
 
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 import java.util.function.Function;
 
 /**
@@ -58,15 +55,21 @@ public class Layer2ClusterMatcher extends AbstractClusterMatcher implements Laye
     private String mTrainingRouterWlanMac;
     private String mRouterWlanMac;
 
+    /**
+     * Relaxed matching
+     */
+    private int mDelta;
+    private Set<Integer> mPacketSet;
+
     /**
      * Create a new {@link Layer2ClusterMatcher} that attempts to find occurrences of {@code cluster}'s members.
      * @param cluster The sequence mutations that the new {@link Layer2ClusterMatcher} should search for.
      */
     public Layer2ClusterMatcher(List<List<PcapPacket>> cluster, String trainingRouterWlanMac, String routerWlanMac, int inclusionTimeMillis,
-                                boolean isRangeBased, double eps, int limitSkippedPackets) {
+                                boolean isRangeBased, double eps, int limitSkippedPackets, int delta, Set<Integer> packetSet) {
         // Consider all flows if no flow filter specified.
         this(cluster, trainingRouterWlanMac, routerWlanMac, flow -> true, inclusionTimeMillis, isRangeBased, eps,
-                limitSkippedPackets);
+                limitSkippedPackets, delta, packetSet);
     }
 
     /**
@@ -86,7 +89,7 @@ public class Layer2ClusterMatcher extends AbstractClusterMatcher implements Laye
      */
     public Layer2ClusterMatcher(List<List<PcapPacket>> cluster, String trainingRouterWlanMac, String routerWlanMac,
                                 Function<Layer2Flow, Boolean> flowFilter, int inclusionTimeMillis, boolean isRangeBased,
-                                double eps, int limitSkippedPackets) {
+                                double eps, int limitSkippedPackets, int delta, Set<Integer> packetSet) {
         super(cluster, isRangeBased);
         mFlowFilter = flowFilter;
         mTrainingRouterWlanMac = trainingRouterWlanMac;
@@ -99,6 +102,8 @@ public class Layer2ClusterMatcher extends AbstractClusterMatcher implements Laye
         mSkippedPackets = new ArrayList<>();
         // Give integer's MAX_VALUE if -1
         mLimitSkippedPackets = limitSkippedPackets == -1 ? Integer.MAX_VALUE : limitSkippedPackets;
+        mDelta = delta;
+        mPacketSet = packetSet;
     }
 
     @Override
@@ -110,6 +115,7 @@ public class Layer2ClusterMatcher extends AbstractClusterMatcher implements Laye
         }
     }
 
+    // TODO: Relaxed matching is applied in conservative matching
     private void conservativeMatching(Layer2Flow flow, PcapPacket newPacket) {
         if (mPerFlowSeqMatchers.get(flow) == null) {
             // If this is the first time we encounter this flow, we need to set up sequence matchers for it.
@@ -121,7 +127,7 @@ public class Layer2ClusterMatcher extends AbstractClusterMatcher implements Laye
             // Prepare a "state 0" sequence matcher for each sequence variation in the cluster.
             for (int i = 0; i < matchers.length; i++) {
                 matchers[i][0] = new Layer2SequenceMatcher(mCluster.get(i), mInclusionTimeMillis, mTrainingRouterWlanMac,
-                        mRouterWlanMac);
+                        mRouterWlanMac, mDelta, mPacketSet);
             }
             // Associate the new sequence matcher table with the new flow
             mPerFlowSeqMatchers.put(flow, matchers);
@@ -166,7 +172,7 @@ public class Layer2ClusterMatcher extends AbstractClusterMatcher implements Laye
                     // from state zero completed its matching or if it replaced a different one in state 1 or not.
                     if (sm.getMatchedPacketsCount() == 1) {
                         matchers[i][j] = new Layer2SequenceMatcher(sm.getTargetSequence(), mInclusionTimeMillis,
-                                mTrainingRouterWlanMac, mRouterWlanMac);
+                                mTrainingRouterWlanMac, mRouterWlanMac, mDelta, mPacketSet);
                     }
                 }
             }