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;
import org.apache.commons.math3.stat.clustering.Cluster;
import org.pcap4j.core.PcapPacket;
+import org.pcap4j.packet.EthernetPacket;
import org.pcap4j.packet.IpV4Packet;
import org.pcap4j.packet.TcpPacket;
+import org.pcap4j.util.MacAddress;
import java.util.*;
*/
private static final int SIGNATURE_MERGE_THRESHOLD = 5;
+
+ /**
+ * Gets the source address of the Ethernet part of {@code packet}.
+ * @param packet The packet for which the Ethernet source address is to be extracted.
+ * @return The source address of the Ethernet part of {@code packet}.
+ */
+ public static MacAddress getEthSrcAddr(PcapPacket packet) {
+ return getEthernetPacketOrThrow(packet).getHeader().getSrcAddr();
+ }
+
+ /**
+ * Gets the destination address of the Ethernet part of {@code packet}.
+ * @param packet The packet for which the Ethernet destination address is to be extracted.
+ * @return The destination address of the Ethernet part of {@code packet}.
+ */
+ public static MacAddress getEthDstAddr(PcapPacket packet) {
+ return getEthernetPacketOrThrow(packet).getHeader().getDstAddr();
+ }
+
/**
* Determines if a given {@link PcapPacket} wraps a {@link TcpPacket}.
* @param packet The {@link PcapPacket} to inspect.
public static List<List<List<PcapPacket>>> sortSignatures(List<List<List<PcapPacket>>> 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());
+// });
// 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.
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;
/**
* Gets the {@link IpV4Packet} contained in {@code packet}, or throws a {@link NullPointerException} if
* {@code packet} does not contain an {@link IpV4Packet}.
- * @param packet A {@link PcapPacket} that is expected to contain a {@link IpV4Packet}.
+ * @param packet A {@link PcapPacket} that is expected to contain an {@link IpV4Packet}.
* @return The {@link IpV4Packet} contained in {@code packet}.
* @throws NullPointerException if {@code packet} does not encapsulate an {@link IpV4Packet}.
*/
return Objects.requireNonNull(packet.get(IpV4Packet.class), "not an IPv4 packet");
}
+ /**
+ * Gets the {@link EthernetPacket} contained in {@code packet}, or throws a {@link NullPointerException} if
+ * {@code packet} does not contain an {@link EthernetPacket}.
+ * @param packet A {@link PcapPacket} that is expected to contain an {@link EthernetPacket}.
+ * @return The {@link EthernetPacket} contained in {@code packet}.
+ * @throws NullPointerException if {@code packet} does not encapsulate an {@link EthernetPacket}.
+ */
+ private static final EthernetPacket getEthernetPacketOrThrow(PcapPacket packet) {
+ return Objects.requireNonNull(packet.get(EthernetPacket.class), "not an Ethernet packet");
+ }
+
/**
* Print signatures in {@code List} of {@code List} of {@code List} of {@code PcapPacket} objects.
*