From: rtrimana Date: Thu, 26 Apr 2018 00:42:55 +0000 (-0700) Subject: Reading and parsing through packets; handling unwanted packets; ready to create a... X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=e5847cc4f9f5191c28c0cf3635b0a1d0281959b3;p=pingpong.git Reading and parsing through packets; handling unwanted packets; ready to create a good parser to create a nice data structure. --- diff --git a/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/EthernetFrame.java b/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/EthernetFrame.java index c575a82..6e3deec 100644 --- a/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/EthernetFrame.java +++ b/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/EthernetFrame.java @@ -9,6 +9,7 @@ import java.util.Map; import java.util.HashMap; public class EthernetFrame extends KaitaiStruct { + private static int counter = 0; public static EthernetFrame fromFile(String fileName) throws IOException { return new EthernetFrame(new ByteBufferKaitaiStream(fileName)); } @@ -52,7 +53,6 @@ public class EthernetFrame extends KaitaiStruct { this.dstMac = this._io.readBytes(6); this.srcMac = this._io.readBytes(6); this.etherType = EtherTypeEnum.byId(this._io.readU2be()); - // We skip if etherType is NULL // Some packets, e.g. EAPOL and XID do not have etherType // and we are not interested in these packets @@ -64,12 +64,13 @@ public class EthernetFrame extends KaitaiStruct { this.body = new Ipv4Packet(_io__raw_body); break; } - case IPV6: { - this._raw_body = this._io.readBytesFull(); - KaitaiStream _io__raw_body = new ByteBufferKaitaiStream(_raw_body); - this.body = new Ipv6Packet(_io__raw_body); - break; - } + // TODO: Skip IPV6 for now and perhaps do it later + //case IPV6: { + // this._raw_body = this._io.readBytesFull(); + // KaitaiStream _io__raw_body = new ByteBufferKaitaiStream(_raw_body); + // this.body = new Ipv6Packet(_io__raw_body); + // break; + //} default: { this.body = this._io.readBytesFull(); break; diff --git a/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/Ipv6Packet.java b/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/Ipv6Packet.java index 469ed8b..c71c7b2 100644 --- a/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/Ipv6Packet.java +++ b/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/Ipv6Packet.java @@ -107,7 +107,6 @@ public class Ipv6Packet extends KaitaiStruct { private void _read() { this.nextHeaderType = this._io.readU1(); this.hdrExtLen = this._io.readU1(); - this.body = this._io.readBytes((hdrExtLen() - 1)); switch (nextHeaderType()) { case 0: { this.nextHeader = new OptionHopByHop(this._io, this, _root); diff --git a/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/Main.java b/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/Main.java index 6fb5e9f..44085c3 100644 --- a/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/Main.java +++ b/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/Main.java @@ -1,5 +1,6 @@ package edu.uci.iotproject; +import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -9,15 +10,46 @@ import java.util.Map; * @author Janus Varmarken */ public class Main { + private static int counter = 0; public static void main(String[] args) throws Exception { System.out.println("it works"); - //String file = "/home/rtrimana/pcap_processing/smart_home_traffic/Code/Projects/SmartPlugDetector/pcap/gre-sample.pcap"; + //String file = "/scratch/traffic_measurements/Switches-Feb2018/wemo/wlan1/wlan1.setup.pcap"; String file = "/home/rtrimana/pcap_processing/smart_home_traffic/Code/Projects/SmartPlugDetector/pcap/wlan1.local.dns.pcap"; try { Pcap data = Pcap.fromFile(file); - data.hdr(); + //data.hdr(); + List listPacket = data.packets(); + System.out.println("Number of packets: " + listPacket.size()); + System.out.println("==================="); + for(Pcap.Packet packet : listPacket) { + System.out.print("#" + counter++ + "\n"); + if (packet._root().hdr().network() == Pcap.Linktype.ETHERNET) { + EthernetFrame eFrame = (EthernetFrame) packet.body(); + if (eFrame.etherType() == EthernetFrame.EtherTypeEnum.IPV4) { + Ipv4Packet ip4Packet = (Ipv4Packet) eFrame.body(); + byte[] srcIp = ip4Packet.srcIpAddr(); + byte[] dstIp = ip4Packet.dstIpAddr(); + System.out.println("Byte length source: " + srcIp.length + " Byte length dest: " + dstIp.length); + System.out.print("Source: "); + for(int i = 0; i < srcIp.length; i++) { + System.out.print(Byte.toUnsignedInt(srcIp[i])); + if(i < srcIp.length-1) + System.out.print("."); + } + System.out.print(" - Dest: "); + for(int i = 0; i < dstIp.length; i++) { + System.out.print(Byte.toUnsignedInt(dstIp[i])); + if(i < dstIp.length-1) + System.out.print("."); + else + System.out.println("\n"); + } + } + } + } + } catch (Exception e) { e.printStackTrace(); } diff --git a/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/Packet.java b/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/Packet.java deleted file mode 100644 index fcc67d6..0000000 --- a/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/Packet.java +++ /dev/null @@ -1,35 +0,0 @@ -package edu.uci.iotproject; - -import java.util.Objects; - -/** - * Represents a network packet. - * - * @author Janus Varmarken - */ -public class Packet { - - /** - * The packet's length. - */ - private final int length; - - // TODO should we use hostname for src/dst such that we can map packets pertaining to the same host as similar even if they are sent to different IPs (due to load balancing)? - - /** - * The packet's source (IP). - */ - private final String source; - - /** - * The packet's destination (IP). - */ - private final String destionation; - - public Packet(String src, String dst, int length) { - this.source = Objects.requireNonNull(src); - this.destionation = Objects.requireNonNull(dst); - this.length = length; - } - -}