Printing directions in the toCSV() method in PrintUtils
authorrtrimana <rtrimana@uci.edu>
Wed, 12 Sep 2018 15:41:38 +0000 (08:41 -0700)
committerrtrimana <rtrimana@uci.edu>
Wed, 12 Sep 2018 15:41:38 +0000 (08:41 -0700)
Code/Projects/SmartPlugDetector/.idea/modules/SmartPlugDetector_main.iml
Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/Main.java
Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/analysis/PcapPacketPair.java
Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/util/PrintUtils.java

index 5134c0a60323a32356c2c1f8949322984a55c924..777fd3934f2cd050bc1d2896b1ac86d9176d53f7 100644 (file)
     <orderEntry type="library" name="Gradle: org.slf4j:slf4j-jdk14:1.8.0-beta2" level="project" />
     <orderEntry type="library" name="Gradle: net.java.dev.jna:jna:4.2.1" level="project" />
     <orderEntry type="library" name="Gradle: org.slf4j:slf4j-api:1.8.0-beta2" level="project" />
+    <orderEntry type="module-library">
+      <library>
+        <CLASSES>
+          <root url="file:///usr/share/java" />
+        </CLASSES>
+        <JAVADOC />
+        <SOURCES>
+          <root url="file:///usr/share/java" />
+        </SOURCES>
+        <jarDirectory url="file:///usr/share/java" recursive="false" />
+        <jarDirectory url="file:///usr/share/java" recursive="false" type="SOURCES" />
+      </library>
+    </orderEntry>
   </component>
 </module>
\ No newline at end of file
index 1d3888a284e71f3ad4206466c01c302fb996e916..c2f640113639b2e0c9b38364a596ebd73dbe87ee 100644 (file)
@@ -2,14 +2,12 @@ package edu.uci.iotproject;
 
 import static edu.uci.iotproject.analysis.UserAction.Type;
 
-import edu.uci.iotproject.analysis.TcpConversationUtils;
-import edu.uci.iotproject.analysis.TrafficLabeler;
-import edu.uci.iotproject.analysis.TriggerTrafficExtractor;
-import edu.uci.iotproject.analysis.UserAction;
+import edu.uci.iotproject.analysis.*;
 import edu.uci.iotproject.comparison.seqalignment.ExtractedSequence;
 import edu.uci.iotproject.comparison.seqalignment.SequenceAlignment;
 import edu.uci.iotproject.comparison.seqalignment.SequenceExtraction;
 import edu.uci.iotproject.io.TriggerTimesFileReader;
+import edu.uci.iotproject.util.PrintUtils;
 import org.pcap4j.core.*;
 import org.pcap4j.packet.namednumber.DataLinkType;
 
@@ -129,7 +127,7 @@ public class Main {
         final String inputPcapFile = path + "/2018-08/dlink-siren/dlink-siren.wlan1.local.pcap";
         final String outputPcapFile = path + "/2018-08/dlink-siren/dlink-siren-processed.pcap";
         final String triggerTimesFile = path + "/2018-08/dlink-siren/dlink-siren-aug-14-2018.timestamps";
-        final String deviceIp = "192.168.1.183"; // .246 == phone; .183 == siren
+        final String deviceIp = "192.168.1.246"; // .246 == phone; .183 == siren
 
         // 14) Nest thermostat August 15 experiment
 //        final String inputPcapFile = path + "/2018-08/nest/nest.wlan1.local.pcap";
@@ -221,6 +219,7 @@ public class Main {
         });
 
 
+        System.out.println("==== ON ====");
         // Print out all the pairs into a file for ON events
         File fileOnEvents = new File(onPairsPath);
         PrintWriter pwOn = null;
@@ -238,58 +237,16 @@ public class Main {
                 for(Conversation conv : listConv) {
                     // Process only if it is a TLS packet
                     if (conv.isTls()) {
-                        List<PcapPacket> tlsAppDataList = conv.getTlsApplicationDataPackets();
-                        // Loop and print out packets
-                        int count = 0;
-                        // The direction of the first packet
-                        Conversation.Direction firstDir = null;
-                        // The length of the first packet
-                        int firstLen = 0;
-                        for (PcapPacket pcap : tlsAppDataList) {
-                            boolean isPair = false;
-                            if (count % 2 == 0) {
-                                firstDir = conv.getDirection(pcap);
-                                firstLen = pcap.length();
-                            } else {// count%2 == 1
-                                if(conv.getDirection(pcap) != firstDir) {
-                                    isPair = true;
-                                    pwOn.println(firstLen + ", " + pcap.length());
-                                    //System.out.println(firstDir + ", " + conv.getDirection(pcap));
-                                    //System.out.println(firstLen + ", " + pcap.length());
-                                }
-                            }
-                            count++;
-                            // If we can't create a pair then just pad it with 0
-                            if (count == tlsAppDataList.size() && !isPair) {
-                                pwOn.println(firstLen + ", 0");
-                            }
+                        List<PcapPacketPair> tlsAppDataList = TcpConversationUtils.extractTlsAppDataPacketPairs(conv);
+                        for(PcapPacketPair pair: tlsAppDataList) {
+                            System.out.println(PrintUtils.toCsv(pair, dnsMap));
+                            pwOn.println(PrintUtils.toCsv(pair, dnsMap));
                         }
                     } else { // Non-TLS conversations
-                        List<PcapPacket> packetList = conv.getPackets();
-                        // Loop and print out packets
-                        int count = 0;
-                        // The direction of the first packet
-                        Conversation.Direction firstDir = null;
-                        // The length of the first packet
-                        int firstLen = 0;
-                        for (PcapPacket pcap : packetList) {
-                            boolean isPair = false;
-                            if (count % 2 == 0) {
-                                firstDir = conv.getDirection(pcap);
-                                firstLen = pcap.length();
-                            } else {// count%2 == 1
-                                if(conv.getDirection(pcap) != firstDir) {
-                                    isPair = true;
-                                    pwOn.println(firstLen + ", " + pcap.length());
-                                    System.out.println(firstDir + ", " + conv.getDirection(pcap));
-                                    System.out.println(firstLen + ", " + pcap.length());
-                                }
-                            }
-                            count++;
-                            // If we can't create a pair then just pad it with 0
-                            if (count == packetList.size() && !isPair) {
-                                pwOn.println(firstLen + ", 0");
-                            }
+                        List<PcapPacketPair> packetList = TcpConversationUtils.extractPacketPairs(conv);
+                        for(PcapPacketPair pair: packetList) {
+                            System.out.println(PrintUtils.toCsv(pair, dnsMap));
+                            pwOn.println(PrintUtils.toCsv(pair, dnsMap));
                         }
                     }
                 }
@@ -297,6 +254,7 @@ public class Main {
         }
         pwOn.close();
 
+        System.out.println("==== OFF ====");
         // Print out all the pairs into a file for ON events
         File fileOffEvents = new File(offPairsPath);
         PrintWriter pwOff = null;
@@ -314,59 +272,16 @@ public class Main {
                 for(Conversation conv : listConv) {
                     // Process only if it is a TLS packet
                     if (conv.isTls()) {
-                        List<PcapPacket> tlsAppDataList = conv.getTlsApplicationDataPackets();
-                        // Loop and print out packets
-                        int count = 0;
-                        // The direction of the first packet
-                        Conversation.Direction firstDir = null;
-                        // The length of the first packet
-                        int firstLen = 0;
-                        for (PcapPacket pcap : tlsAppDataList) {
-                            boolean isPair = false;
-                            if (count % 2 == 0) {
-                                firstDir = conv.getDirection(pcap);
-                                firstLen = pcap.length();
-                            } else {// count%2 == 1
-                                if(conv.getDirection(pcap) != firstDir) {
-                                    isPair = true;
-                                    pwOff.println(firstLen + ", " + pcap.length());
-                                    System.out.println(firstDir + ", " + conv.getDirection(pcap));
-                                    System.out.println(firstLen + ", " + pcap.length());
-                                }
-                            }
-                            count++;
-                            // If we can't create a pair then just pad it with 0
-                            if (count == tlsAppDataList.size() && !isPair) {
-                                pwOff.println(firstLen + ", 0");
-                            }
-
+                        List<PcapPacketPair> tlsAppDataList = TcpConversationUtils.extractTlsAppDataPacketPairs(conv);
+                        for(PcapPacketPair pair: tlsAppDataList) {
+                            System.out.println(PrintUtils.toCsv(pair, dnsMap));
+                            pwOff.println(PrintUtils.toCsv(pair, dnsMap));
                         }
                     } else { // Non-TLS conversations
-                        List<PcapPacket> packetList = conv.getPackets();
-                        // Loop and print out packets
-                        int count = 0;
-                        // The direction of the first packet
-                        Conversation.Direction firstDir = null;
-                        // The length of the first packet
-                        int firstLen = 0;
-                        for (PcapPacket pcap : packetList) {
-                            boolean isPair = false;
-                            if (count % 2 == 0) {
-                                firstDir = conv.getDirection(pcap);
-                                firstLen = pcap.length();
-                            } else {// count%2 == 1
-                                if(conv.getDirection(pcap) != firstDir) {
-                                    isPair = true;
-                                    pwOff.println(firstLen + ", " + pcap.length());
-                                    System.out.println(firstDir + ", " + conv.getDirection(pcap));
-                                    System.out.println(firstLen + ", " + pcap.length());
-                                }
-                            }
-                            count++;
-                            // If we can't create a pair then just pad it with 0
-                            if (count == packetList.size() && !isPair) {
-                                pwOff.println(firstLen + ", 0");
-                            }
+                        List<PcapPacketPair> packetList = TcpConversationUtils.extractPacketPairs(conv);
+                        for (PcapPacketPair pair : packetList) {
+                            System.out.println(PrintUtils.toCsv(pair, dnsMap));
+                            pwOff.println(PrintUtils.toCsv(pair, dnsMap));
                         }
                     }
                 }
index f77531f0f6a083629a1c5ee798170abfa58b8029..cec81dfce78b529f2003a87f29b92c96c65a5e0d 100644 (file)
@@ -1,8 +1,11 @@
 package edu.uci.iotproject.analysis;
 
+import edu.uci.iotproject.util.PcapPacketUtils;
 import org.apache.commons.math3.ml.clustering.Clusterable;
 import org.pcap4j.core.PcapPacket;
 
+import java.net.InetAddress;
+import java.net.UnknownHostException;
 import java.util.Optional;
 
 /**
@@ -24,8 +27,36 @@ public class PcapPacketPair implements Clusterable {
 
     public PcapPacket getFirst() { return mFirst; }
 
+    public boolean isFirstClient() {
+        String firstIp = PcapPacketUtils.getSourceIp(mFirst);
+        InetAddress ia = null;
+        try {
+            ia = InetAddress.getByName(firstIp);
+        } catch (UnknownHostException ex) {
+            ex.printStackTrace();
+        }
+        return ia.isSiteLocalAddress();
+    }
+
     public Optional<PcapPacket> getSecond() { return mSecond; }
 
+    public boolean isSecondClient() {
+        // Return the value of the second source if it is not null
+        if (mSecond.isPresent()) {
+            String secondIp = PcapPacketUtils.getSourceIp(mSecond.get());
+            InetAddress ia = null;
+            try {
+                ia = InetAddress.getByName(secondIp);
+            } catch (UnknownHostException ex) {
+                ex.printStackTrace();
+            }
+            return ia.isSiteLocalAddress();
+        } else {
+            // When it is null, we always return the opposite of the first source's status
+            return !isFirstClient();
+        }
+    }
+
     @Override
     public String toString() {
         return String.format("%d, %s",
index 01839227e82ebeef7dca0d06c2e82d84ea86a011..ae6b5786c47503f5b40842f84a90d11fa01f50bc 100644 (file)
@@ -69,10 +69,16 @@ public class PrintUtils {
         // Fall back to IP if we couldn't second pair is present, but we couldn't map to (a) hostname(s).
         secondSrc = hostnames.isPresent() ? hostnames : secondSrc;
 
-        return String.format("%d, %d, %s, %s", packetPair.getFirst().getOriginalLength(),
+        // Check if the first source is C (client) or S (server)
+        String firstSrcCorS = packetPair.isFirstClient() ? "C" : "S";
+        String secondSrcCorS = packetPair.isSecondClient() ? "C" : "S";
+
+        return String.format("%d, %d, %s, %s, %s, %s", packetPair.getFirst().getOriginalLength(),
                 packetPair.getSecond().map(pp -> pp.getOriginalLength()).orElse(0),
                 firstSrc,
-                secondSrc.orElse("null"));
+                secondSrc.orElse("null"),
+                firstSrcCorS,
+                secondSrcCorS);
     }
 
 }