1 package edu.uci.iotproject.util;
3 import org.pcap4j.core.PcapPacket;
4 import org.pcap4j.packet.IpV4Packet;
5 import org.pcap4j.packet.TcpPacket;
7 import java.util.Objects;
10 * Utility methods for inspecting {@link PcapPacket} properties. Currently not used.
12 * @author Janus Varmarken {@literal <jvarmark@uci.edu>}
13 * @author Rahmadi Trimananda {@literal <rtrimana@uci.edu>}
15 public final class PcapPacketUtils {
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}.
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;