Added sub sequence search function, and put this into effect; seems to work just...
authorJanus Varmarken <varmarken@gmail.com>
Fri, 11 May 2018 05:12:13 +0000 (22:12 -0700)
committerJanus Varmarken <varmarken@gmail.com>
Fri, 11 May 2018 05:12:13 +0000 (22:12 -0700)
Code/Projects/SmartPlugDetector/pcap/TP_LINK_LOCAL_ON_SUBSET.pcap [new file with mode: 0644]
Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/FlowPatternFinder.java
Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/Main.java
Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/comparison/ComparisonFunctions.java

diff --git a/Code/Projects/SmartPlugDetector/pcap/TP_LINK_LOCAL_ON_SUBSET.pcap b/Code/Projects/SmartPlugDetector/pcap/TP_LINK_LOCAL_ON_SUBSET.pcap
new file mode 100644 (file)
index 0000000..209bfbf
Binary files /dev/null and b/Code/Projects/SmartPlugDetector/pcap/TP_LINK_LOCAL_ON_SUBSET.pcap differ
index af35f89bb8add4fe6d9b274fd6c9e83e8f5c15c0..112a713c88d35902eaf9c336b74a5218fc83724d 100644 (file)
@@ -168,7 +168,7 @@ public class FlowPatternFinder {
                     mConversations.remove(conversation);
                     // Create comparison task and send to executor service.
                     PatternComparisonTask<CompleteMatchPatternComparisonResult> comparisonTask =
                     mConversations.remove(conversation);
                     // Create comparison task and send to executor service.
                     PatternComparisonTask<CompleteMatchPatternComparisonResult> comparisonTask =
-                            new PatternComparisonTask<>(conversation, mPattern, ComparisonFunctions.COMPLETE_MATCH);
+                            new PatternComparisonTask<>(conversation, mPattern, ComparisonFunctions.SUB_SEQUENCE_COMPLETE_MATCH);
                     mPendingComparisons.add(EXECUTOR_SERVICE.submit(comparisonTask));
                     // Increment hostIndex to find the next
                     
                     mPendingComparisons.add(EXECUTOR_SERVICE.submit(comparisonTask));
                     // Increment hostIndex to find the next
                     
index b828323f5a0c5268d53d93bcb6adf7c9e3309743..624136fd6972cd7d13beab8b7dd109b310acac05 100644 (file)
@@ -22,7 +22,7 @@ public class Main {
 
     public static void main(String[] args) throws PcapNativeException, NotOpenException, EOFException, TimeoutException, UnknownHostException {
         final String fileName = args.length > 0 ? args[0] : "/home/rtrimana/pcap_processing/smart_home_traffic/Code/Projects/SmartPlugDetector/pcap/wlan1.local.remote.dns.pcap";
 
     public static void main(String[] args) throws PcapNativeException, NotOpenException, EOFException, TimeoutException, UnknownHostException {
         final String fileName = args.length > 0 ? args[0] : "/home/rtrimana/pcap_processing/smart_home_traffic/Code/Projects/SmartPlugDetector/pcap/wlan1.local.remote.dns.pcap";
-        final String trainingFileName = "./pcap/TP_LINK_LOCAL_ON.pcap";
+        final String trainingFileName = "./pcap/TP_LINK_LOCAL_ON_SUBSET.pcap";
         //final String trainingFileName = "./pcap/TP_LINK_REMOTE_ON.pcap";
 
         // ====== Debug code ======
         //final String trainingFileName = "./pcap/TP_LINK_REMOTE_ON.pcap";
 
         // ====== Debug code ======
index 2f44f3b957de894b374255e40137d2b3a156f4d5..15eda20fe115e5b92b741292cb7fff4ad0a986f2 100644 (file)
@@ -38,4 +38,40 @@ public class ComparisonFunctions {
         return new CompleteMatchPatternComparisonResult(conversation, flowPattern, true);
     };
 
         return new CompleteMatchPatternComparisonResult(conversation, flowPattern, true);
     };
 
-}
+    /**
+     * Comparison function that searches a {@link Conversation} looking for the presence of a complete match of a {@link FlowPattern}.
+     * Unlike {@link #COMPLETE_MATCH}, which searches for a 1:1 match between the {@code Conversation} and the {@code FlowPattern},
+     * this function targets cases where the {@code Conversation} is longer than the {@code FlowPattern}.
+     * In other words, this function searches for a complete match of a sub sequence of packets in the {@code Conversation}.
+     * Note: this is a slow, brute force search.
+     */
+    public static final BiFunction<Conversation, FlowPattern, CompleteMatchPatternComparisonResult> SUB_SEQUENCE_COMPLETE_MATCH = new BiFunction<Conversation, FlowPattern, CompleteMatchPatternComparisonResult>() {
+        // TODO needs review; I was tired when I wrote this :).
+        private boolean find(Conversation conversation, FlowPattern flowPattern, int nextIndex, int matchedIndices) {
+            if (matchedIndices == flowPattern.getLength()) {
+                // Found a full sub sequence.
+                return true;
+            }
+            List<PcapPacket> convPackets = conversation.getPackets();
+            if (nextIndex >= convPackets.size()) {
+                // Reached end of list without finding a match.
+                return false;
+            }
+            if (convPackets.get(nextIndex).get(TcpPacket.class).getPayload().length() == flowPattern.getPacketOrder().get(matchedIndices)) {
+                // So far, so good. Still need to check if the remainder of the sub sequence is present.
+                return find(conversation, flowPattern, ++nextIndex, ++matchedIndices);
+            } else {
+                // Miss; trace back and retry the search starting at the index immediately after the index from the
+                // recursive calls potentially started matching some of the sub sequence.
+                return find(conversation, flowPattern, nextIndex-matchedIndices+1, 0);
+            }
+        }
+
+        @Override
+        public CompleteMatchPatternComparisonResult apply(Conversation conversation, FlowPattern flowPattern) {
+            return new CompleteMatchPatternComparisonResult(conversation, flowPattern, find(conversation, flowPattern, 0, 0));
+        }
+
+    };
+
+}
\ No newline at end of file