TrafficLabeler.java: implemented support for exporting the labeled traffic in differe...
authorJanus Varmarken <varmarken@gmail.com>
Thu, 2 Aug 2018 23:37:29 +0000 (16:37 -0700)
committerJanus Varmarken <varmarken@gmail.com>
Thu, 2 Aug 2018 23:37:29 +0000 (16:37 -0700)
Main.java: invoke TrafficLabeler to get the set of TCP conversations per user action.
TriggerTrafficExtractor.java: change inclusion interval to 10 seconds.

Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/Main.java
Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/analysis/TrafficLabeler.java
Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/analysis/TriggerTrafficExtractor.java

index a687cacc3c6476a538b45ce1a402139598677f94..f37719a03a1baa3ae6b55ecfcd5b95ea9d48be56 100644 (file)
@@ -42,16 +42,16 @@ public class Main {
 //        final String deviceIp = "192.168.1.246"; // .246 == phone; .199 == dlink plug?
 
         // TP-Link July 25 experiment
-//        final String inputPcapFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/tplink/tplink.wlan1.local.pcap";
-//        final String outputPcapFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/tplink/tplink-processed.pcap";
-//        final String triggerTimesFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/tplink/tplink-july-25-2018.timestamps";
-//        final String deviceIp = "192.168.1.159";
+        final String inputPcapFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/tplink/tplink.wlan1.local.pcap";
+        final String outputPcapFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/tplink/tplink-processed.pcap";
+        final String triggerTimesFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/tplink/tplink-july-25-2018.timestamps";
+        final String deviceIp = "192.168.1.159";
 
         // Wemo July 30 experiment
-        final String inputPcapFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/wemo/wemo.wlan1.local.pcap";
-        final String outputPcapFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/wemo/wemo-processed.pcap";
-        final String triggerTimesFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/wemo/wemo-july-30-2018.timestamps";
-        final String deviceIp = "192.168.1.145";
+//        final String inputPcapFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/wemo/wemo.wlan1.local.pcap";
+//        final String outputPcapFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/wemo/wemo-processed.pcap";
+//        final String triggerTimesFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-07/wemo/wemo-july-30-2018.timestamps";
+//        final String deviceIp = "192.168.1.145";
 
         // TP-Link BULB August 1 experiment
 //        final String inputPcapFile = "/Users/varmarken/temp/UCI IoT Project/experiments/2018-08/tplink-bulb/tplink-bulb.wlan1.local.pcap";
@@ -105,6 +105,10 @@ public class Main {
         final Map<String, Map<String, Integer>> pktPairFreqsByHostname =
                 TcpConversationUtils.countPacketPairFrequenciesByHostname(allConversations, dnsMap);
         System.out.println("Counted frequencies of packet pairs per hostname");
+        // For each user action, reassemble the set of TCP connections occurring shortly after
+        final Map<UserAction, List<Conversation>> userActionToConversations = trafficLabeler.getLabeledReassembledTcpTraffic();
+        System.out.println("Reassembled TCP conversations occurring shortly after each user event");
+
         // -------------------------------------------------------------------------------------------------------------
         // -------------------------------------------------------------------------------------------------------------
     }
index 226f67f7ced3a7ed7f1a1385cc0157fad9373c81..2de59c3f64d6424217eebea40bd130058e08e9a7 100644 (file)
@@ -1,10 +1,13 @@
 package edu.uci.iotproject.analysis;
 
+import edu.uci.iotproject.Conversation;
+import edu.uci.iotproject.TcpReassembler;
 import org.pcap4j.core.PacketListener;
 import org.pcap4j.core.PcapPacket;
 
 import java.time.Instant;
 import java.util.*;
+import java.util.function.Function;
 
 /**
  * A {@link PacketListener} that marks network traffic as (potentially) related to a user's actions by comparing the
@@ -66,10 +69,59 @@ public class TrafficLabeler implements PacketListener {
 
     /**
      * Get the total number of packets labeled by this {@code TrafficLabeler}.
+     *
      * @return the total number of packets labeled by this {@code TrafficLabeler}.
      */
     public long getTotalPacketCount() {
         return mPackets;
     }
 
-}
+    /**
+     * Get the labeled traffic.
+     *
+     * @return A {@link Map} in which a {@link UserAction} points to a {@link List} of {@link PcapPacket}s believed to
+     *         be related (occurring as a result of) that {@code UserAction}.
+     */
+    public Map<UserAction, List<PcapPacket>> getLabeledTraffic() {
+        return Collections.unmodifiableMap(mActionToTrafficMap);
+    }
+
+    /**
+     * Like {@link #getLabeledTraffic()}, but allows the caller to supply a mapping function that is applied to
+     * the traffic associated with each {@link UserAction} (the traffic label) before returning the labeled traffic.
+     * This may for example be useful for a caller who wishes to perform some postprocessing of labeled traffic, e.g.,
+     * in order to perform additional filtering or to transform the representation of labeled traffic.
+     * <p>
+     *     An example usecase is provided in {@link #getLabeledReassembledTcpTraffic()} which uses this function to
+     *     build a {@link Map} in which a {@link UserAction} points to the reassembled TCP connections believed to have
+     *     occurred as a result of that {@code UserAction}.
+     * </p>
+     *
+     * @param mappingFunction A mapping function that converts a {@link List} of {@link PcapPacket} into some other type
+     *                        {@code T}.
+     * @param <T> The return type of {@code mappingFunction}.
+     * @return A {@link Map} in which a {@link UserAction} points to the result of applying {@code mappingFunction} to
+     *         the set of packets believed to be related (occurring as a result of) that {@code UserAction}.
+     */
+    public <T> Map<UserAction, T> getLabeledTraffic(Function<List<PcapPacket>, T> mappingFunction) {
+        Map<UserAction, T> result = new HashMap<>();
+        mActionToTrafficMap.forEach((ua, packets) -> result.put(ua, mappingFunction.apply(packets)));
+        return result;
+    }
+
+
+    /**
+     * Get the labeled traffic reassembled as TCP connections (<b>note:</b> <em>discards</em> all non-TCP traffic).
+     *
+     * @return A {@link Map} in which a {@link UserAction} points to a {@link List} of {@link Conversation}s believed to
+     *         be related (occurring as a result of) that {@code UserAction}.
+     */
+    public Map<UserAction, List<Conversation>> getLabeledReassembledTcpTraffic() {
+        return getLabeledTraffic(packets -> {
+            TcpReassembler tcpReassembler = new TcpReassembler();
+            packets.forEach(p -> tcpReassembler.gotPacket(p));
+            return tcpReassembler.getTcpConversations();
+        });
+    }
+
+}
\ No newline at end of file
index 594fa2b02b834b1ad927f36ad6a3edd2c19f1a63..ab783a7361ee48b3a6b0d671c2eda636f86399a8 100644 (file)
@@ -26,7 +26,7 @@ public class TriggerTrafficExtractor implements PcapPacketFilter {
      */
     private long mIncludedPackets = 0;
 
-    public static final int INCLUSION_WINDOW_MILLIS = 20_000;
+    public static final int INCLUSION_WINDOW_MILLIS = 10_000;
 
     public TriggerTrafficExtractor(String pcapFilePath, List<Instant> triggerTimes, String deviceIp) throws PcapNativeException, NotOpenException {
         mPcapFilePath = pcapFilePath;