X-Git-Url: http://plrg.eecs.uci.edu/git/?p=pingpong.git;a=blobdiff_plain;f=Code%2FProjects%2FSmartPlugDetector%2Fsrc%2Fmain%2Fjava%2Fedu%2Fuci%2Fiotproject%2Futil%2FPcapPacketUtils.java;h=4e578419bee092e56a3d217507dfc9ff64be1efc;hp=67421b802e5d380fa762a17bfabe5a45ca8b55d5;hb=eaf6818c383973cdf5fe5a7f6ee88fcd88425cbf;hpb=9c41e9509bc2781359f70c07939b617c9e7e6b68 diff --git a/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/util/PcapPacketUtils.java b/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/util/PcapPacketUtils.java index 67421b8..4e57841 100644 --- a/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/util/PcapPacketUtils.java +++ b/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/util/PcapPacketUtils.java @@ -30,4 +30,20 @@ public final class PcapPacketUtils { return ipSrc.equals(ip) && srcPort == port; } + /** + * Helper method to determine if the given combination of IP and port matches the destination of the given packet. + * @param packet The packet to check. + * @param ip The IP to look for in the ip.dst field of {@code packet}. + * @param port The port to look for in the tcp.dstport field of {@code packet}. + * @return {@code true} if the given ip+port match the corresponding fields in {@code packet}. + */ + public static boolean isDestination(PcapPacket packet, String ip, int port) { + IpV4Packet ipPacket = Objects.requireNonNull(packet.get(IpV4Packet.class)); + // For now we only support TCP flows. + TcpPacket tcpPacket = Objects.requireNonNull(packet.get(TcpPacket.class)); + String ipDst = ipPacket.getHeader().getDstAddr().getHostAddress(); + int dstPort = tcpPacket.getHeader().getDstPort().valueAsInt(); + return ipDst.equals(ip) && dstPort == port; + } + }