44085c38679cbc199c891c353b45bbef71c6cbdf
[pingpong.git] / Code / Projects / SmartPlugDetector / src / main / java / edu / uci / iotproject / Main.java
1 package edu.uci.iotproject;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.Map;
6
7 /**
8  * Entry point of the application.
9  *
10  * @author Janus Varmarken
11  */
12 public class Main {
13     private static int counter = 0;
14
15     public static void main(String[] args) throws Exception {
16         System.out.println("it works");
17         //String file = "/scratch/traffic_measurements/Switches-Feb2018/wemo/wlan1/wlan1.setup.pcap";
18         String file = "/home/rtrimana/pcap_processing/smart_home_traffic/Code/Projects/SmartPlugDetector/pcap/wlan1.local.dns.pcap";
19         
20         try {
21             Pcap data = Pcap.fromFile(file);
22             //data.hdr();
23             List<Pcap.Packet> listPacket = data.packets();
24             System.out.println("Number of packets: " + listPacket.size());
25             System.out.println("===================");
26             for(Pcap.Packet packet : listPacket) {
27                 System.out.print("#" + counter++ + "\n");
28                 if (packet._root().hdr().network() == Pcap.Linktype.ETHERNET) {
29                     EthernetFrame eFrame = (EthernetFrame) packet.body();
30                     if (eFrame.etherType() == EthernetFrame.EtherTypeEnum.IPV4) {
31                         Ipv4Packet ip4Packet = (Ipv4Packet) eFrame.body();
32                         byte[] srcIp = ip4Packet.srcIpAddr();
33                         byte[] dstIp = ip4Packet.dstIpAddr();
34                         System.out.println("Byte length source: " + srcIp.length + " Byte length dest: " + dstIp.length);
35                         System.out.print("Source: ");
36                         for(int i = 0; i < srcIp.length; i++) {
37                             System.out.print(Byte.toUnsignedInt(srcIp[i]));
38                             if(i < srcIp.length-1)
39                                 System.out.print(".");
40                         }
41                         System.out.print(" - Dest: ");
42                         for(int i = 0; i < dstIp.length; i++) {
43                             System.out.print(Byte.toUnsignedInt(dstIp[i]));
44                             if(i < dstIp.length-1)
45                                 System.out.print(".");
46                             else
47                                 System.out.println("\n");
48                         }
49                     }
50                 }
51             }
52             
53         } catch (Exception e) {
54             e.printStackTrace();
55         }
56     }
57
58     private String cloudIPAddress(String hostName) {
59         if (hostName.equals("events.tplinkra.com"))
60             return "205.251.203.26";
61         else
62             return null;
63     }
64
65     // TODO move to separate class
66     // Add parameter that is the trace to be analyzed (most like the pcap library's representation of a flow)
67     public String findPattern(Map<String, List<Integer>> hostnameToPacketLengths, String smartPlugIp) {
68
69         // No difference, output "Complete match"
70         // If difference, output <Packet no, deviation from expected> for each packet
71         return null;
72     }
73 }