1 package edu.uci.iotproject.detection.layer2;
3 import edu.uci.iotproject.analysis.TriggerTrafficExtractor;
4 import edu.uci.iotproject.util.PcapPacketUtils;
5 import org.pcap4j.core.PcapPacket;
6 import org.pcap4j.util.MacAddress;
8 import java.util.ArrayList;
12 * Attempts to detect the presence of a specific packet sequence in the set of packets provided through multiple calls
13 * to {@link #matchPacket(PcapPacket)}, considering only layer 2 information. This class has the same flavor as the
14 * {@link Layer2SequenceMatcher} class.
16 * @author Janus Varmarken {@literal <jvarmark@uci.edu>}
17 * @author Rahmadi Trimananda {@literal <rtrimana@uci.edu>}
19 public class Layer2RangeMatcher extends Layer2AbstractMatcher {
21 * The range this {@link Layer2RangeMatcher} is searching for.
23 private final List<PcapPacket> mLowerBound;
24 private final List<PcapPacket> mUpperBound;
25 private final double mEps;
28 * Create a {@code Layer2RangeMatcher}.
29 * @param lowerBound The lower bound of the sequence to match against (search for).
30 * @param upperBound The upper bound of the sequence to match against (search for).
31 * @param eps The epsilon value used in the DBSCAN algorithm.
33 public Layer2RangeMatcher(List<PcapPacket> lowerBound, List<PcapPacket> upperBound, double eps) {
34 // TODO: Just use the lower bound since both lower and upper bounds' packets essentially have the same direction
35 // TODO: for the same position in the array. Both arrays also have the same length.
37 mLowerBound = lowerBound;
38 mUpperBound = upperBound;
43 * Attempt to advance this {@code Layer2RangeMatcher} by matching {@code packet} against the packet that this
44 * {@code Layer2RangeMatcher} expects as the next packet of the sequence it is searching for.
46 * @return {@code true} if this {@code Layer2SequenceMatcher} could advance by adding {@code packet} to its set of
47 * matched packets, {@code false} otherwise.
49 public boolean matchPacket(PcapPacket packet) {
50 if (getMatchedPacketsCount() == getTargetSequencePacketCount()) {
51 // We already matched the entire sequence, so we can't match any more packets.
55 // Verify that new packet pertains to same flow as previously matched packets, if any.
56 if (getMatchedPacketsCount() > 0) {
57 MacAddress pktSrc = PcapPacketUtils.getEthSrcAddr(packet);
58 MacAddress pktDst = PcapPacketUtils.getEthDstAddr(packet);
59 MacAddress earlierPktSrc = PcapPacketUtils.getEthSrcAddr(mMatchedPackets.get(0));
60 MacAddress earlierPktDst = PcapPacketUtils.getEthDstAddr(mMatchedPackets.get(0));
61 if (!(pktSrc.equals(earlierPktSrc) && pktDst.equals(earlierPktDst) ||
62 pktSrc.equals(earlierPktDst) && pktDst.equals(earlierPktSrc))) {
67 // Get representative of the packet we expect to match next.
68 PcapPacket expectedLowerBound = mLowerBound.get(mMatchedPackets.size());
69 PcapPacket expectedUpperBound = mUpperBound.get(mMatchedPackets.size());
70 // First verify if the received packet has the length we're looking for (the length should be within the range).
71 if (expectedLowerBound.getOriginalLength() - (int) mEps <= packet.getOriginalLength() &&
72 packet.getOriginalLength() <= expectedUpperBound.getOriginalLength() + (int) mEps){
73 // If this is the first packet, we only need to verify that its length is correct. Time constraints are
74 // obviously satisfied as there are no previous packets. Furthermore, direction matches by definition as we
75 // don't know the MAC of the device (or phone) in advance, so we can't enforce a rule saying "first packet
76 // must originate from this particular MAC".
77 if (getMatchedPacketsCount() == 0) {
78 // Store packet as matched and advance.
79 mMatchedPackets.add(packet);
82 // Check if direction of packet matches expected direction.
83 boolean actualDirection = getPacketDirection(mMatchedPackets.get(getMatchedPacketsCount()-1),
84 mPacketDirections[getMatchedPacketsCount()-1], packet);
85 boolean expectedDirection = mPacketDirections[getMatchedPacketsCount()];
86 if (actualDirection != expectedDirection) {
89 // Next apply timing constraints:
90 // 1: to be a match, the packet must have a later timestamp than any other packet currently matched
91 // 2: does adding the packet cause the max allowed time between first packet and last packet to be exceeded?
92 if (!packet.getTimestamp().isAfter(mMatchedPackets.get(getMatchedPacketsCount()-1).getTimestamp())) {
95 if (packet.getTimestamp().isAfter(mMatchedPackets.get(0).getTimestamp().
96 plusMillis(TriggerTrafficExtractor.INCLUSION_WINDOW_MILLIS))) {
99 // If we made it here, it means that this packet has the expected length, direction, and obeys the timing
100 // constraints, so we store it and advance.
101 mMatchedPackets.add(packet);
102 if (mMatchedPackets.size() == mLowerBound.size()) {
103 // TODO report (to observers?) that we are done?
110 public int getTargetSequencePacketCount() {
111 return mLowerBound.size();
114 public List<PcapPacket> getTargetLowerBound() {
118 public List<PcapPacket> getTargetUpperBound() {