package edu.uci.iotproject;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
+import edu.uci.iotproject.maclayer.MacLayerFlowPattern;
+import edu.uci.iotproject.maclayer.MacLayerFlowPatternFinder;
+import org.pcap4j.core.*;
+
+import java.io.EOFException;
+import java.net.UnknownHostException;
+import java.util.*;
+import java.util.concurrent.TimeoutException;
/**
- * Entry point of the application.
+ * This is a system that reads PCAP files to compare
+ * patterns of DNS hostnames, packet sequences, and packet
+ * lengths with training data to determine certain events
+ * or actions for smart home devices.
*
* @author Janus Varmarken
+ * @author Rahmadi Trimananda (rtrimana@uci.edu)
+ * @version 0.1
*/
public class Main {
- private static int counter = 0;
- public static void main(String[] args) throws Exception {
- System.out.println("it works");
- //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";
-
+
+ public static void main(String[] args) throws PcapNativeException, NotOpenException, EOFException, TimeoutException, UnknownHostException {
+ // -------------------------------------------------------------------------------------------------------------
+ // Example/debug code for searching for a pattern at the MAC layer.
+ String fileName = "./pcap/mac-tplink.local.pcapng";
+ PcapHandle handle;
try {
- Pcap data = Pcap.fromFile(file);
- //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();
+ handle = Pcaps.openOffline(fileName, PcapHandle.TimestampPrecision.NANO);
+ } catch (PcapNativeException pne) {
+ handle = Pcaps.openOffline(fileName);
}
- }
-
- private String cloudIPAddress(String hostName) {
- if (hostName.equals("events.tplinkra.com"))
- return "205.251.203.26";
- else
- return null;
- }
-
- // TODO move to separate class
- // Add parameter that is the trace to be analyzed (most like the pcap library's representation of a flow)
- public String findPattern(Map<String, List<Integer>> hostnameToPacketLengths, String smartPlugIp) {
+// Arrays.asList(1590, 1590, 1590, 1001, 337, 197, 636, 1311, 177) // Full pattern (all non-zero payload packets).
+ MacLayerFlowPattern pattern = new MacLayerFlowPattern("TP_LINK_LOCAL_OFF_MAC", "50:c7:bf:33:1f:09", Arrays.asList(637, 1312));
+ MacLayerFlowPatternFinder finder = new MacLayerFlowPatternFinder(handle, pattern);
+ finder.findFlowPattern();
+ // -------------------------------------------------------------------------------------------------------------
- // No difference, output "Complete match"
- // If difference, output <Packet no, deviation from expected> for each packet
- return null;
+// final String fileName = args.length > 0 ? args[0] : "/home/rtrimana/pcap_processing/smart_home_traffic/Code/Projects/SmartPlugDetector/pcap/wlan1.local.remote.dns.pcap";
+// final String trainingFileName = "./pcap/TP_LINK_LOCAL_ON_SUBSET.pcap";
+// //final String trainingFileName = "./pcap/TP_LINK_REMOTE_ON.pcap";
+//
+// // ====== Debug code ======
+// PcapHandle handle;
+// PcapHandle trainingPcap;
+// try {
+// handle = Pcaps.openOffline(fileName, PcapHandle.TimestampPrecision.NANO);
+// trainingPcap = Pcaps.openOffline(trainingFileName, PcapHandle.TimestampPrecision.NANO);
+// } catch (PcapNativeException pne) {
+// handle = Pcaps.openOffline(fileName);
+// trainingPcap = Pcaps.openOffline(trainingFileName);
+// }
+//
+// // TODO: The followings are the way to extract multiple hostnames and their associated packet lengths lists
+// //List<String> list = new ArrayList<>();
+// //list.add("events.tplinkra.com");
+// //FlowPattern fp = new FlowPattern("TP_LINK_LOCAL_ON", list, trainingPcap);
+// //List<String> list2 = new ArrayList<>();
+// //list2.add("devs.tplinkcloud.com");
+// //list2.add("events.tplinkra.com");
+// //FlowPattern fp3 = new FlowPattern("TP_LINK_REMOTE_ON", list2, trainingPcap);
+//
+// FlowPattern fp = new FlowPattern("TP_LINK_LOCAL_ON", "events.tplinkra.com", trainingPcap);
+// FlowPatternFinder fpf = new FlowPatternFinder(handle, fp);
+// fpf.start();
+//
+// // ========================
}
}