Prepare a data structure for keeping track of FIN and their corresponding ACK packets...
[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. Currently not used.
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 }