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.
12 * @author Janus Varmarken {@literal <jvarmark@uci.edu>}
13 * @author Rahmadi Trimananda {@literal <rtrimana@uci.edu>}
15 public final class PcapPacketUtils {
18 * Gets the source IP (in decimal format) of an IPv4 packet.
19 * @param packet The packet for which the IPv4 source address is to be extracted.
20 * @return The decimal representation of the source IP of {@code packet} <em>iff</em> {@code packet} wraps an
21 * {@link IpV4Packet}, otherwise {@code null}.
23 public static String getSourceIp(PcapPacket packet) {
24 IpV4Packet ipPacket = packet.get(IpV4Packet.class);
25 return ipPacket == null ? null : ipPacket.getHeader().getSrcAddr().getHostAddress();
29 * Helper method to determine if the given combination of IP and port matches the source of the given packet.
30 * @param packet The packet to check.
31 * @param ip The IP to look for in the ip.src field of {@code packet}.
32 * @param port The port to look for in the tcp.port field of {@code packet}.
33 * @return {@code true} if the given ip+port match the corresponding fields in {@code packet}.
35 public static boolean isSource(PcapPacket packet, String ip, int port) {
36 IpV4Packet ipPacket = Objects.requireNonNull(packet.get(IpV4Packet.class));
37 // For now we only support TCP flows.
38 TcpPacket tcpPacket = Objects.requireNonNull(packet.get(TcpPacket.class));
39 String ipSrc = ipPacket.getHeader().getSrcAddr().getHostAddress();
40 int srcPort = tcpPacket.getHeader().getSrcPort().valueAsInt();
41 return ipSrc.equals(ip) && srcPort == port;
45 * Helper method to determine if the given combination of IP and port matches the destination of the given packet.
46 * @param packet The packet to check.
47 * @param ip The IP to look for in the ip.dst field of {@code packet}.
48 * @param port The port to look for in the tcp.dstport field of {@code packet}.
49 * @return {@code true} if the given ip+port match the corresponding fields in {@code packet}.
51 public static boolean isDestination(PcapPacket packet, String ip, int port) {
52 IpV4Packet ipPacket = Objects.requireNonNull(packet.get(IpV4Packet.class));
53 // For now we only support TCP flows.
54 TcpPacket tcpPacket = Objects.requireNonNull(packet.get(TcpPacket.class));
55 String ipDst = ipPacket.getHeader().getDstAddr().getHostAddress();
56 int dstPort = tcpPacket.getHeader().getDstPort().valueAsInt();
57 return ipDst.equals(ip) && dstPort == port;
61 * Checks if {@code packet} wraps a TCP packet that has the SYN flag set.
62 * @param packet A {@link PcapPacket} that is suspected to contain a {@link TcpPacket} for which the SYN flag is set.
63 * @return {@code true} <em>iff</em> {@code packet} contains a {@code TcpPacket} for which the SYN flag is set,
64 * {@code false} otherwise.
66 public static boolean isSyn(PcapPacket packet) {
67 TcpPacket tcp = packet.get(TcpPacket.class);
68 return tcp != null && tcp.getHeader().getSyn();
72 * Checks if {@code packet} wraps a TCP packet that has the ACK flag set.
73 * @param packet A {@link PcapPacket} that is suspected to contain a {@link TcpPacket} for which the ACK flag is set.
74 * @return {@code true} <em>iff</em> {@code packet} contains a {@code TcpPacket} for which the ACK flag is set,
75 * {@code false} otherwise.
77 public static boolean isAck(PcapPacket packet) {
78 TcpPacket tcp = packet.get(TcpPacket.class);
79 return tcp != null && tcp.getHeader().getAck();