Implementing relaxed matching for layer 2 and layer 3.
[pingpong.git] / Code / Projects / PacketLevelSignatureExtractor / src / main / java / edu / uci / iotproject / util / PcapPacketUtils.java
index 2601dc1cc35528e76d2857ca5dd2cf74b07b3509..68e7defe8d0924e984191a090d0d0a942ca43ea0 100644 (file)
@@ -13,6 +13,7 @@ import org.pcap4j.packet.TcpPacket;
 import org.pcap4j.util.MacAddress;
 
 import java.io.PrintWriter;
+import java.math.BigInteger;
 import java.util.*;
 
 /**
@@ -670,6 +671,12 @@ public final class PcapPacketUtils {
                                                        PcapPacket lowerBound, PcapPacket upperBound) {
 
         List<PcapPacket> listBounds = new ArrayList<>();
+        // Just return the lower and upper bounds when their values are the same --- faster
+        if (lowerBound.length() == upperBound.length()) {
+            listBounds.add(0, lowerBound);
+            listBounds.add(1, upperBound);
+            return listBounds;
+        }
         // Iterate over PcapPacket one by one
         for(List<List<PcapPacket>> listOfListPcapPacket : corePointRange) {
             List<PcapPacket> listCorePointLowerBound = listOfListPcapPacket.get(0);
@@ -841,4 +848,17 @@ public final class PcapPacketUtils {
         // Sequence index starts from 0
         signatures.remove(sequenceIndex);
     }
+
+    /**
+     * Convert a String MAC address into a byte array.
+     *
+     * @param macAddress A String that contains a MAC address to be converted into byte array.
+     */
+    public static byte[] stringToByteMacAddress(String macAddress) {
+
+        BigInteger biMacAddress = new BigInteger(macAddress, 16);
+        byte[] byteMacAddress = biMacAddress.toByteArray();
+        // Return from 1 to 6 since element 0 is 0 because of using BigInteger's method.
+        return Arrays.copyOfRange(byteMacAddress, 1, 7);
+    }
 }