Implementing relaxed matching for layer 2 and layer 3.
[pingpong.git] / Code / Projects / PacketLevelSignatureExtractor / src / main / java / edu / uci / iotproject / detection / layer2 / Layer2SequenceMatcher.java
index 672fb72f3441b29abf667c1027c7d5590de0f0e4..55fe040eb2a480d92ab9093e20a6120e0f1360f3 100644 (file)
@@ -7,6 +7,7 @@ import org.pcap4j.util.MacAddress;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Set;
 
 /**
  * Attempts to detect the presence of a specific packet sequence in the set of packets provided through multiple calls
@@ -15,35 +16,32 @@ import java.util.List;
  * @author Janus Varmarken {@literal <jvarmark@uci.edu>}
  * @author Rahmadi Trimananda {@literal <rtrimana@uci.edu>}
  */
-public class Layer2SequenceMatcher {
+public class Layer2SequenceMatcher extends Layer2AbstractMatcher {
 
     /**
      * The sequence this {@link Layer2SequenceMatcher} is searching for.
      */
     private final List<PcapPacket> mSequence;
 
-    /**
-     * Buffer of actual packets seen so far that match the searched sequence (i.e., constitutes a subsequence of the
-     * searched sequence).
-     */
-    private final List<PcapPacket> mMatchedPackets = new ArrayList<>();
+    private int mInclusionTimeMillis;
 
     /**
-     * Models the directions of packets in {@link #mSequence}. As the sequence matcher assumes that it is only presented
-     * with packet from a single flow (packets exchanged between two devices), we can model the packet directions with a
-     * single bit. We don't have any notion "phone to device" or "device to phone" as we don't know the MAC addresses
-     * of devices in advance during matching.
+     * Relaxed matching
      */
-    private final boolean[] mPacketDirections;
+    private int mDelta;
+    private Set<Integer> mPacketSet;
 
     /**
      * Create a {@code Layer2SequenceMatcher}.
      * @param sequence The sequence to match against (search for).
+     * @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 Layer2SequenceMatcher(List<PcapPacket> sequence) {
+    public Layer2SequenceMatcher(List<PcapPacket> sequence, int inclusionTimeMillis, String trainingRouterWlanMac,
+                                 String routerWlanMac, int delta, Set<Integer> packetSet) {
+        super(sequence, trainingRouterWlanMac, routerWlanMac);
         mSequence = sequence;
         // Compute packet directions for sequence.
-        mPacketDirections = new boolean[sequence.size()];
         for (int i = 0; i < sequence.size(); i++) {
             if (i == 0) {
                 // No previous packet; boolean parameter is ignored in this special case.
@@ -55,6 +53,10 @@ public class Layer2SequenceMatcher {
                 mPacketDirections[i] = getPacketDirection(prevPkt, prevPktDirection, sequence.get(i));
             }
         }
+        mInclusionTimeMillis =
+                inclusionTimeMillis == 0 ? TriggerTrafficExtractor.INCLUSION_WINDOW_MILLIS : inclusionTimeMillis;
+        mDelta = delta;
+        mPacketSet = packetSet;
     }
 
     /**
@@ -85,7 +87,10 @@ public class Layer2SequenceMatcher {
         // Get representative of the packet we expect to match next.
         PcapPacket expected = mSequence.get(mMatchedPackets.size());
         // First verify if the received packet has the length we're looking for.
-        if (packet.getOriginalLength() == expected.getOriginalLength()) {
+        if ((mDelta > 0 && mPacketSet.contains(expected.getOriginalLength()) &&
+                expected.getOriginalLength() - mDelta <= packet.getOriginalLength() &&
+                packet.getOriginalLength() <= expected.getOriginalLength() + mDelta) ||
+                packet.getOriginalLength() == expected.getOriginalLength()) {
             // 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
@@ -108,8 +113,10 @@ public class Layer2SequenceMatcher {
             if (!packet.getTimestamp().isAfter(mMatchedPackets.get(getMatchedPacketsCount()-1).getTimestamp())) {
                 return false;
             }
+//            if (packet.getTimestamp().isAfter(mMatchedPackets.get(0).getTimestamp().
+//                            plusMillis(TriggerTrafficExtractor.INCLUSION_WINDOW_MILLIS))) {
             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
@@ -123,10 +130,6 @@ public class Layer2SequenceMatcher {
         return false;
     }
 
-    public int getMatchedPacketsCount() {
-        return mMatchedPackets.size();
-    }
-
     public int getTargetSequencePacketCount() {
         return mSequence.size();
     }
@@ -135,39 +138,4 @@ public class Layer2SequenceMatcher {
         return mSequence;
     }
 
-    public List<PcapPacket> getMatchedPackets() {
-        return mMatchedPackets;
-    }
-
-    /**
-     * Utility for {@code getMatchedPackets().get(getMatchedPackets().size()-1)}.
-     * @return The last matched packet, or {@code null} if no packets have been matched yet.
-     */
-    public PcapPacket getLastPacket() {
-        return mSequence.size() > 0 ? mSequence.get(mSequence.size()-1) : null;
-    }
-
-    /**
-     * Compute the direction of a packet based on the previous packet. If no previous packet is provided, the direction
-     * of {@code currPkt} is {@code true} by definition.
-     * @param prevPkt The previous packet, if any.
-     * @param prevPktDirection The computed direction of the previous packet
-     * @param currPkt The current packet for which the direction is to be determined.
-     * @return The direction of {@code currPkt}.
-     */
-    private boolean getPacketDirection(PcapPacket prevPkt, boolean prevPktDirection, PcapPacket currPkt) {
-        if (prevPkt == null) {
-            // By definition, use true as direction marker for first packet
-            return true;
-        }
-        if (PcapPacketUtils.getEthSrcAddr(prevPkt).equals(PcapPacketUtils.getEthSrcAddr(currPkt))) {
-            // Current packet goes in same direction as previous packet.
-            return prevPktDirection;
-        } else {
-            // Current packet goes in opposite direction of previous packet.
-            return !prevPktDirection;
-        }
-    }
-
-
 }