Adding new devices under test.
[pingpong.git] / Code / Projects / SmartPlugDetector / src / main / java / edu / uci / iotproject / util / PcapPacketUtils.java
index ccd3d9ad4a89dde13172c5f2b824475d060913fa..f03110ee88024a3c553d8a56f632905d53b366ae 100644 (file)
@@ -199,8 +199,12 @@ public final class PcapPacketUtils {
      */
     public static List<List<List<PcapPacket>>>
             mergeSignatures(List<List<List<PcapPacket>>> signatures, List<Conversation> conversations) {
-        // Make a copy first.
-        List<List<List<PcapPacket>>> copySignatures = new ArrayList<>(signatures);
+
+        // TODO: THIS IS NOT A DEEP COPY; IT BASICALLY CREATES A REFERENCE TO THE SAME LIST OBJECT
+        // List<List<List<PcapPacket>>> copySignatures = new ArrayList<>(signatures);
+        // Make a deep copy first.
+        List<List<List<PcapPacket>>> copySignatures = new ArrayList<>();
+        listDeepCopy(copySignatures, signatures);
         // Traverse and look into the pairs of signatures.
         for (int first = 0; first < signatures.size(); first++) {
             List<List<PcapPacket>> firstList = signatures.get(first);
@@ -241,6 +245,9 @@ public final class PcapPacketUtils {
                 if (secondList.size() < SIGNATURE_MERGE_THRESHOLD) {
                     // Prune the unsuccessfully merged signatures (i.e., these will have size() < maxSignatureEl).
                     final int maxNumOfEl = maxSignatureEl;
+                    // TODO: DOUBLE CHECK IF WE REALLY NEED TO PRUNE FAILED BINDINGS
+                    // TODO: SOMETIMES THE SEQUENCES ARE JUST INCOMPLETE
+                    // TODO: AND BOTH THE COMPLETE AND INCOMPLETE SEQUENCES ARE VALID SIGNATURES!
                     firstList.removeIf(el -> el.size() < maxNumOfEl);
                     // Remove the merged set of signatures when successful.
                     signatures.remove(secondList);
@@ -254,6 +261,28 @@ public final class PcapPacketUtils {
         return signatures;
     }
 
+    /**
+     * Deep copy to create an entirely new {@link List} of {@link List} of {@link List} of {@link PcapPacket} objects.
+     * @param destList A {@link List} of {@link List} of {@link List} of
+     *          {@link PcapPacket} objects that will be the final container of the deep copy
+     * @param sourceList A {@link List} of {@link List} of {@link List} of
+     *          {@link PcapPacket} objects that will be the source of the deep copy.
+     */
+    private static void listDeepCopy(List<List<List<PcapPacket>>> destList, List<List<List<PcapPacket>>> sourceList) {
+
+        for(List<List<PcapPacket>> llPcapPacket : sourceList) {
+            List<List<PcapPacket>> tmpListOfList = new ArrayList<>();
+            for(List<PcapPacket> lPcapPacket : llPcapPacket) {
+                List<PcapPacket> tmpList = new ArrayList<>();
+                for(PcapPacket pcapPacket : lPcapPacket) {
+                    tmpList.add(pcapPacket);
+                }
+                tmpListOfList.add(tmpList);
+            }
+            destList.add(tmpListOfList);
+        }
+    }
+
     /**
      * Sort the signatures in the {@code List} of {@code List} of {@code List} of {@code PcapPacket} objects.
      * The purpose of this is to sort the order of signatures in the signature list. For detection purposes, we need
@@ -293,8 +322,8 @@ public final class PcapPacketUtils {
                         if (Integer.signum(compare) != Integer.signum(comparePrev)) {
                             // Throw an exception if the order of the two signatures is not consistent,
                             // E.g., 111, 222, 333 in one occassion and 222, 333, 111 in the other.
-                            throw new Error("For some reason, the order of signatures are not always consistent!" +
-                                    "Returning the original data structure of signatures...");
+//                            throw new Error("For some reason, the order of signatures are not always consistent!" +
+//                                    "Returning the original data structure of signatures...");
                         }
                     }
                     comparePrev = compare;
@@ -343,7 +372,9 @@ public final class PcapPacketUtils {
                 // Print out packet lengths in a sequence
                 int packetCounter = 0;
                 for(PcapPacket pcapPacket : listPcapPacket) {
-                    System.out.print(pcapPacket.length());
+                    if(pcapPacket != null) {
+                        System.out.print(pcapPacket.length());
+                    }
                     if(packetCounter < listPcapPacket.size() - 1) {
                         System.out.print(" ");  // Provide space if not last packet
                     } else {
@@ -354,4 +385,18 @@ public final class PcapPacketUtils {
             }
         }
     }
+
+    /**
+     * Remove a sequence in a signature object.
+     *
+     * @param signatures A {@link List} of {@link List} of {@link List} of
+     *          {@link PcapPacket} objects.
+     * @param sequenceIndex An index for a sequence that consists of {{@link List} of {@link List} of
+     *          {@link PcapPacket} objects.
+     */
+    public static void removeSequenceFromSignature(List<List<List<PcapPacket>>> signatures, int sequenceIndex) {
+
+        // Sequence index starts from 0
+        signatures.remove(sequenceIndex);
+    }
 }