add method for checking if a Conversation has been gracefully shut down.
[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     /**
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 }