1621c820525d8871d2b6dc770c3a2df1baddba50
[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      * Create a {@code Layer2AbstractMatcher}.
32      * @param sequence The sequence of the signature.
33      */
34     public Layer2AbstractMatcher(List<PcapPacket> sequence) {
35         mPacketDirections = new boolean[sequence.size()];
36         // Compute packet directions for sequence.
37         for (int i = 0; i < sequence.size(); i++) {
38             if (i == 0) {
39                 // No previous packet; boolean parameter is ignored in this special case.
40                 mPacketDirections[i] = getPacketDirection(null, true, sequence.get(i));
41             } else {
42                 // Base direction marker on direction of previous packet.
43                 PcapPacket prevPkt = sequence.get(i-1);
44                 boolean prevPktDirection = mPacketDirections[i-1];
45                 mPacketDirections[i] = getPacketDirection(prevPkt, prevPktDirection, sequence.get(i));
46             }
47         }
48     }
49
50     /**
51      * Compute the direction of a packet based on the previous packet. If no previous packet is provided, the direction
52      * of {@code currPkt} is {@code true} by definition.
53      * @param prevPkt The previous packet, if any.
54      * @param prevPktDirection The computed direction of the previous packet
55      * @param currPkt The current packet for which the direction is to be determined.
56      * @return The direction of {@code currPkt}.
57      */
58     protected boolean getPacketDirection(PcapPacket prevPkt, boolean prevPktDirection, PcapPacket currPkt) {
59         if (prevPkt == null) {
60             // By definition, use true as direction marker for first packet
61             return true;
62         }
63         if (PcapPacketUtils.getEthSrcAddr(prevPkt).equals(PcapPacketUtils.getEthSrcAddr(currPkt))) {
64             // Current packet goes in same direction as previous packet.
65             return prevPktDirection;
66         } else {
67             // Current packet goes in opposite direction of previous packet.
68             return !prevPktDirection;
69         }
70     }
71
72     /**
73      * See the implementer class for the following method.
74      *
75      * @param packet
76      * @return {@code true} if this {@code Layer2SequenceMatcher} could advance by adding {@code packet} to its set of
77      *         matched packets, {@code false} otherwise.
78      */
79     public abstract boolean matchPacket(PcapPacket packet);
80
81     /**
82      * See the implementer class for the following method.
83      */
84     public abstract int getTargetSequencePacketCount();
85
86     public int getMatchedPacketsCount() {
87         return mMatchedPackets.size();
88     }
89
90     public List<PcapPacket> getMatchedPackets() {
91         return mMatchedPackets;
92     }
93
94     /**
95      * Utility for {@code getMatchedPackets().get(getMatchedPackets().size()-1)}.
96      * @return The last matched packet, or {@code null} if no packets have been matched yet.
97      */
98     public PcapPacket getLastPacket() {
99         //return mSequence.size() > 0 ? mSequence.get(mSequence.size()-1) : null;
100         return mMatchedPackets.size() > 0 ? mMatchedPackets.get(mMatchedPackets.size()-1) : null;
101     }
102 }