Reading and parsing through packets; handling unwanted packets; ready to create a...
authorrtrimana <rtrimana@uci.edu>
Thu, 26 Apr 2018 00:42:55 +0000 (17:42 -0700)
committerrtrimana <rtrimana@uci.edu>
Thu, 26 Apr 2018 00:42:55 +0000 (17:42 -0700)
Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/EthernetFrame.java
Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/Ipv6Packet.java
Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/Main.java
Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/Packet.java [deleted file]

index c575a82636026bfe3917051adf104703e1a7c514..6e3deecfced2e7782c414650d390932b160003ad 100644 (file)
@@ -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;
index 469ed8b4d35a533ed6c47690d1e65f1ac801e244..c71c7b2e8d326ce5725d27995e225d831b9a3022 100644 (file)
@@ -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);
index 6fb5e9fcac59bf5008bba975f1c862bb571e8e3d..44085c38679cbc199c891c353b45bbef71c6cbdf 100644 (file)
@@ -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<Pcap.Packet> 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 (file)
index fcc67d6..0000000
+++ /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;
-    }
-
-}