X-Git-Url: http://plrg.eecs.uci.edu/git/?p=pingpong.git;a=blobdiff_plain;f=Code%2FProjects%2FSmartPlugDetector%2Fsrc%2Fmain%2Fjava%2Fedu%2Fuci%2Fiotproject%2Futil%2FPcapPacketUtils.java;h=067af93edd6acf435ab154480b0a1434d2a9fa02;hp=cc958ee452fa2ee8b0f18c5558238e902a5be785;hb=6647790f717c4e02d11d6f1bfbe847a83dd1f32f;hpb=4bc5b760e1f3469747793b86ac61cbd6b3b917fc diff --git a/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/util/PcapPacketUtils.java b/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/util/PcapPacketUtils.java index cc958ee..067af93 100644 --- a/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/util/PcapPacketUtils.java +++ b/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/util/PcapPacketUtils.java @@ -1,6 +1,6 @@ package edu.uci.iotproject.util; -import edu.uci.iotproject.Conversation; +import edu.uci.iotproject.trafficreassembly.layer3.Conversation; import edu.uci.iotproject.analysis.PcapPacketPair; import edu.uci.iotproject.analysis.TcpConversationUtils; import edu.uci.iotproject.analysis.TriggerTrafficExtractor; @@ -28,6 +28,11 @@ public final class PcapPacketUtils { */ private static final int SIGNATURE_MERGE_THRESHOLD = 5; + /** + * This is an overlap counter (we consider overlaps between signatures if it happens more than once) + */ + private static int mOverlapCounter = 0; + /** * Gets the source address of the Ethernet part of {@code packet}. @@ -113,7 +118,7 @@ public final class PcapPacketUtils { * @param port The port to look for in the tcp.port field of {@code packet}. * @return {@code true} if the given ip+port match the corresponding fields in {@code packet}. */ - public static boolean isSource(PcapPacket packet, String ip, int port) { + public static boolean isSource(PcapPacket packet, String ip, int port) { IpV4Packet ipPacket = Objects.requireNonNull(packet.get(IpV4Packet.class)); // For now we only support TCP flows. TcpPacket tcpPacket = Objects.requireNonNull(packet.get(TcpPacket.class)); @@ -174,7 +179,7 @@ public final class PcapPacketUtils { } /** - * Checks if {@code packet} wraps a TCP packet that has the ACK flag set. + * Checks if {@code packet} wraps a TCP packet th at has the ACK flag set. * @param packet A {@link PcapPacket} that is suspected to contain a {@link TcpPacket} for which the ACK flag is set. * @return {@code true} iff {@code packet} contains a {@code TcpPacket} for which the ACK flag is set, * {@code false} otherwise. @@ -204,7 +209,7 @@ public final class PcapPacketUtils { ppListOfList.add(ppList); } // Sort the list of lists based on the first packet's timestamp! - Collections.sort(ppListOfList, (p1, p2) -> p1.get(0).getTimestamp().compareTo(p2.get(0).getTimestamp())); + Collections.sort(ppListOfList, (p1, p2) -> p1. get(0).getTimestamp().compareTo(p2.get(0).getTimestamp())); return ppListOfList; } @@ -318,9 +323,11 @@ public final class PcapPacketUtils { public static List>> sortSignatures(List>> signatures) { // TODO: This is the simplest solution!!! Might not cover all corner cases. // TODO: Sort the list of lists based on the first packet's timestamps! - //Collections.sort(signatures, (p1, p2) -> { - // return p1.get(0).get(0).getTimestamp().compareTo(p2.get(0).get(0).getTimestamp()); - //}); +// Collections.sort(signatures, (p1, p2) -> { +// //return p1.get(0).get(0).getTimestamp().compareTo(p2.get(0).get(0).getTimestamp()); +// int compare = p1.get(0).get(0).getTimestamp().compareTo(p2.get(0).get(0).getTimestamp()); +// return compare; +// }); // TODO: The following is a more complete solution that covers corner cases. // Sort the list of lists based on one-to-one comparison between timestamps of signatures on both lists. // This also takes into account the fact that the number of signatures in the two lists could be different. @@ -339,14 +346,17 @@ public final class PcapPacketUtils { if (Math.abs(timestamp1 - timestamp2) < TriggerTrafficExtractor.INCLUSION_WINDOW_MILLIS) { // If these two are within INCLUSION_WINDOW_MILLIS window then compare! compare = p1.get(count1).get(0).getTimestamp().compareTo(p2.get(count2).get(0).getTimestamp()); - if (comparePrev != 0) { // First time since it is 0 - 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..."); - } - } +// if (comparePrev != 0) { // First time since it is 0 +// 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("OVERLAP WARNING: " + "" + +// "Please remove one of the sequences: " + +// p1.get(0).get(0).length() + "... OR " + +// p2.get(0).get(0).length() + "..."); +// } +// } + overlapChecking(compare, comparePrev, p1.get(count1), p2.get(count2)); comparePrev = compare; count1++; count2++; @@ -364,6 +374,46 @@ public final class PcapPacketUtils { return signatures; } + /** + * Checks for overlapping between two packet sequences. + * @param compare Current comparison value between packet sequences p1 and p2 + * @param comparePrev Previous comparison value between packet sequences p1 and p2 + * @param sequence1 The packet sequence ({@link List} of {@link PcapPacket} objects). + * @param sequence2 The packet sequence ({@link List} of {@link PcapPacket} objects). + */ + private static void overlapChecking(int compare, int comparePrev, List sequence1, List sequence2) { + + // Check if p1 occurs before p2 but both have same overlap + if (comparePrev != 0) { // First time since it is 0 + 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("OVERLAP WARNING: " + "" + + "Two sequences have some overlap. Please remove one of the sequences: " + + sequence1.get(0).length() + "... OR " + + sequence2.get(0).length() + "..."); + } + } + // Check if p1 is longer than p2 and p2 occurs during the occurrence of p1 + int lastIndexOfSequence1 = sequence1.size() - 1; + int lastIndexOfSequence2 = sequence2.size() - 1; + int compareLast = + sequence1.get(lastIndexOfSequence1).getTimestamp().compareTo(sequence2.get(lastIndexOfSequence2).getTimestamp()); + // Check the signs of compare and compareLast + if ((compare <= 0 && compareLast > 0) || + (compareLast <= 0 && compare > 0)) { + mOverlapCounter++; + // TODO: Probably not the best approach but we consider overlap if it happens more than once + if (mOverlapCounter > 1) { + throw new Error("OVERLAP WARNING: " + "" + + "One sequence is in the other. Please remove one of the sequences: " + + sequence1.get(0).length() + "... OR " + + sequence2.get(0).length() + "..."); + } + } + + } + /** * Gets the {@link IpV4Packet} contained in {@code packet}, or throws a {@link NullPointerException} if * {@code packet} does not contain an {@link IpV4Packet}. @@ -398,7 +448,7 @@ public final class PcapPacketUtils { int sequenceCounter = 0; for(List> listListPcapPacket : signatures) { // Iterate over every member of a cluster/sequence - System.out.print("====== SEQUENCE " + sequenceCounter++); + System.out.print("====== SEQUENCE " + ++sequenceCounter); System.out.println(" - " + listListPcapPacket.size() + " MEMBERS ======"); for(List listPcapPacket : listListPcapPacket) { // Print out packet lengths in a sequence