Adding skipped packets analysis.
[pingpong.git] / Code / Projects / PacketLevelSignatureExtractor / src / main / java / edu / uci / iotproject / detection / layer2 / Layer2AbstractMatcher.java
1 package edu.uci.iotproject.detection.layer2;
2
3 import edu.uci.iotproject.util.PcapPacketUtils;
4 import org.pcap4j.core.PcapPacket;
5
6 import java.util.ArrayList;
7 import java.util.List;
8
9 /**
10  * Base class for layer 2 matchers ({@code Layer2SequenceMatcher} and {@code Layer2RangeMatcher}).
11  *
12  * @author Janus Varmarken {@literal <jvarmark@uci.edu>}
13  * @author Rahmadi Trimananda {@literal <rtrimana@uci.edu>}
14  */
15 abstract public class Layer2AbstractMatcher {
16
17     /**
18      * Buffer of actual packets seen so far that match the searched range (i.e., constitutes a subsequence).
19      */
20     protected final List<PcapPacket> mMatchedPackets = new ArrayList<>();
21
22     /**
23      * Models the directions of packets. As the sequence matcher assumes that it is only presented
24      * with packet from a single flow (packets exchanged between two devices), we can model the packet directions with a
25      * single bit. We don't have any notion "phone to device" or "device to phone" as we don't know the MAC addresses
26      * of devices in advance during matching.
27      */
28     protected final boolean[] mPacketDirections;
29
30     /**
31      * Keep track of the numbers of skipped packets
32      */
33     protected int mSkippedPackets;
34     protected int mMaxSkippedPackets;
35
36     /**
37      * Create a {@code Layer2AbstractMatcher}.
38      * @param sequence The sequence of the signature.
39      */
40     public Layer2AbstractMatcher(List<PcapPacket> sequence) {
41         mPacketDirections = new boolean[sequence.size()];
42         // Compute packet directions for sequence.
43         for (int i = 0; i < sequence.size(); i++) {
44             if (i == 0) {
45                 // No previous packet; boolean parameter is ignored in this special case.
46                 mPacketDirections[i] = getPacketDirection(null, true, sequence.get(i));
47             } else {
48                 // Base direction marker on direction of previous packet.
49                 PcapPacket prevPkt = sequence.get(i-1);
50                 boolean prevPktDirection = mPacketDirections[i-1];
51                 mPacketDirections[i] = getPacketDirection(prevPkt, prevPktDirection, sequence.get(i));
52             }
53         }
54         mSkippedPackets = 0;
55         mMaxSkippedPackets = 0;
56     }
57
58     /**
59      * Compute the direction of a packet based on the previous packet. If no previous packet is provided, the direction
60      * of {@code currPkt} is {@code true} by definition.
61      * @param prevPkt The previous packet, if any.
62      * @param prevPktDirection The computed direction of the previous packet
63      * @param currPkt The current packet for which the direction is to be determined.
64      * @return The direction of {@code currPkt}.
65      */
66     protected boolean getPacketDirection(PcapPacket prevPkt, boolean prevPktDirection, PcapPacket currPkt) {
67         if (prevPkt == null) {
68             // By definition, use true as direction marker for first packet
69             return true;
70         }
71         if (PcapPacketUtils.getEthSrcAddr(prevPkt).equals(PcapPacketUtils.getEthSrcAddr(currPkt))) {
72             // Current packet goes in same direction as previous packet.
73             return prevPktDirection;
74         } else {
75             // Current packet goes in opposite direction of previous packet.
76             return !prevPktDirection;
77         }
78     }
79
80     /**
81      * See the implementer class for the following method.
82      *
83      * @param packet
84      * @return {@code true} if this {@code Layer2SequenceMatcher} could advance by adding {@code packet} to its set of
85      *         matched packets, {@code false} otherwise.
86      */
87     public abstract boolean matchPacket(PcapPacket packet);
88
89     /**
90      * See the implementer class for the following method.
91      */
92     public abstract int getTargetSequencePacketCount();
93
94     public int getMatchedPacketsCount() {
95         return mMatchedPackets.size();
96     }
97
98     public List<PcapPacket> getMatchedPackets() {
99         return mMatchedPackets;
100     }
101
102     public int getMaxSkippedPackets() {
103         return mMaxSkippedPackets;
104     }
105
106     /**
107      * Utility for {@code getMatchedPackets().get(getMatchedPackets().size()-1)}.
108      * @return The last matched packet, or {@code null} if no packets have been matched yet.
109      */
110     public PcapPacket getLastPacket() {
111         //return mSequence.size() > 0 ? mSequence.get(mSequence.size()-1) : null;
112         return mMatchedPackets.size() > 0 ? mMatchedPackets.get(mMatchedPackets.size()-1) : null;
113     }
114 }