Do some refactoring to prevent code duplication; change string sequence generation...
[pingpong.git] / Code / Projects / SmartPlugDetector / src / main / java / edu / uci / iotproject / util / PcapPacketUtils.java
1 package edu.uci.iotproject.util;
2
3 import org.pcap4j.core.PcapPacket;
4 import org.pcap4j.packet.IpV4Packet;
5 import org.pcap4j.packet.TcpPacket;
6
7 import java.util.Objects;
8
9 /**
10  * Utility methods for inspecting {@link PcapPacket} properties.
11  *
12  * @author Janus Varmarken {@literal <jvarmark@uci.edu>}
13  * @author Rahmadi Trimananda {@literal <rtrimana@uci.edu>}
14  */
15 public final class PcapPacketUtils {
16
17     /**
18      * Helper method to determine if the given combination of IP and port matches the source of the given packet.
19      * @param packet The packet to check.
20      * @param ip The IP to look for in the ip.src field of {@code packet}.
21      * @param port The port to look for in the tcp.port field of {@code packet}.
22      * @return {@code true} if the given ip+port match the corresponding fields in {@code packet}.
23      */
24     public static boolean isSource(PcapPacket packet, String ip, int port) {
25         IpV4Packet ipPacket = Objects.requireNonNull(packet.get(IpV4Packet.class));
26         // For now we only support TCP flows.
27         TcpPacket tcpPacket = Objects.requireNonNull(packet.get(TcpPacket.class));
28         String ipSrc = ipPacket.getHeader().getSrcAddr().getHostAddress();
29         int srcPort = tcpPacket.getHeader().getSrcPort().valueAsInt();
30         return ipSrc.equals(ip) && srcPort == port;
31     }
32
33     /**
34      * Helper method to determine if the given combination of IP and port matches the destination of the given packet.
35      * @param packet The packet to check.
36      * @param ip The IP to look for in the ip.dst field of {@code packet}.
37      * @param port The port to look for in the tcp.dstport field of {@code packet}.
38      * @return {@code true} if the given ip+port match the corresponding fields in {@code packet}.
39      */
40     public static boolean isDestination(PcapPacket packet, String ip, int port) {
41         IpV4Packet ipPacket = Objects.requireNonNull(packet.get(IpV4Packet.class));
42         // For now we only support TCP flows.
43         TcpPacket tcpPacket = Objects.requireNonNull(packet.get(TcpPacket.class));
44         String ipDst = ipPacket.getHeader().getDstAddr().getHostAddress();
45         int dstPort = tcpPacket.getHeader().getDstPort().valueAsInt();
46         return ipDst.equals(ip) && dstPort == port;
47     }
48
49     /**
50      * Checks if {@code packet} wraps a TCP packet that has the SYN flag set.
51      * @param packet A {@link PcapPacket} that is suspected to contain a {@link TcpPacket} for which the SYN flag is set.
52      * @return {@code true} <em>iff</em> {@code packet} contains a {@code TcpPacket} for which the SYN flag is set,
53      *         {@code false} otherwise.
54      */
55     public static boolean isSyn(PcapPacket packet) {
56         TcpPacket tcp = packet.get(TcpPacket.class);
57         return tcp != null && tcp.getHeader().getSyn();
58     }
59
60     /**
61      * Checks if {@code packet} wraps a TCP packet that has the ACK flag set.
62      * @param packet A {@link PcapPacket} that is suspected to contain a {@link TcpPacket} for which the ACK flag is set.
63      * @return {@code true} <em>iff</em> {@code packet} contains a {@code TcpPacket} for which the ACK flag is set,
64      *         {@code false} otherwise.
65      */
66     public static boolean isAck(PcapPacket packet) {
67         TcpPacket tcp = packet.get(TcpPacket.class);
68         return tcp != null && tcp.getHeader().getAck();
69     }
70
71 }