Resolving conflict.
[pingpong.git] / Code / Projects / SmartPlugDetector / src / main / java / edu / uci / iotproject / Main.java
1 package edu.uci.iotproject;
2
3 import static edu.uci.iotproject.analysis.UserAction.Type;
4
5 import edu.uci.iotproject.analysis.*;
6 import edu.uci.iotproject.comparison.seqalignment.ExtractedSequence;
7 import edu.uci.iotproject.comparison.seqalignment.SequenceAlignment;
8 import edu.uci.iotproject.comparison.seqalignment.SequenceExtraction;
9 import edu.uci.iotproject.io.TriggerTimesFileReader;
10 import edu.uci.iotproject.util.PrintUtils;
11 import org.apache.commons.math3.stat.clustering.Cluster;
12 import org.apache.commons.math3.stat.clustering.DBSCANClusterer;
13 import org.pcap4j.core.*;
14 import org.pcap4j.packet.namednumber.DataLinkType;
15
16 import java.io.EOFException;
17 import java.io.File;
18 import java.io.PrintWriter;
19 import java.net.UnknownHostException;
20 import java.time.Instant;
21 import java.util.*;
22 import java.util.concurrent.TimeoutException;
23 import java.util.stream.Collectors;
24 import java.util.stream.Stream;
25
26 /**
27  * This is a system that reads PCAP files to compare
28  * patterns of DNS hostnames, packet sequences, and packet
29  * lengths with training data to determine certain events
30  * or actions for smart home devices.
31  *
32  * @author Janus Varmarken
33  * @author Rahmadi Trimananda (rtrimana@uci.edu)
34  * @version 0.1
35  */
36 public class Main {
37
38
39     public static void main(String[] args) throws PcapNativeException, NotOpenException, EOFException, TimeoutException, UnknownHostException {
40         // -------------------------------------------------------------------------------------------------------------
41         // ------------ # Code for extracting traffic generated by a device within x seconds of a trigger # ------------
42         // Paths to input and output files (consider supplying these as arguments instead) and IP of the device for
43         // which traffic is to be extracted:
44 //        String path = "/scratch/July-2018"; // Rahmadi
45         String path = "/Users/varmarken/temp/UCI IoT Project/experiments"; // Janus
46         boolean verbose = true;
47         final String onPairsPath = "/scratch/July-2018/on.txt";
48         final String offPairsPath = "/scratch/July-2018/off.txt";
49
50         // 1) D-Link July 26 experiment
51 //        final String inputPcapFile = path + "/2018-07/dlink/dlink.wlan1.local.pcap";
52 //        final String outputPcapFile = path + "/2018-07/dlink/dlink-processed.pcap";
53 //        final String triggerTimesFile = path + "/2018-07/dlink/dlink-july-26-2018.timestamps";
54 //        final String deviceIp = "192.168.1.246"; // .246 == phone; .199 == dlink plug?
55
56         // 2) TP-Link July 25 experiment
57 //        final String inputPcapFile = path + "/2018-07/tplink/tplink.wlan1.local.pcap";
58 //        final String outputPcapFile = path + "/2018-07/tplink/tplink-processed.pcap";
59 //        final String triggerTimesFile = path + "/2018-07/tplink/tplink-july-25-2018.timestamps";
60 //        final String deviceIp = "192.168.1.159";
61
62         // 2b) TP-Link July 25 experiment TRUNCATED:
63         // Only contains "true local" events, i.e., before the behavior changes to remote-like behavior.
64         // Last included event is at July 25 10:38:11; file filtered to only include packets with arrival time <= 10:38:27.
65 //        final String inputPcapFile = path + "/2018-07/tplink/tplink.wlan1.local.truncated.pcap";
66 //        final String outputPcapFile = path + "/2018-07/tplink/tplink-processed.truncated.pcap";
67 //        final String triggerTimesFile = path + "/2018-07/tplink/tplink-july-25-2018.truncated.timestamps";
68 //        final String deviceIp = "192.168.1.159";
69
70         // 3) SmartThings Plug July 25 experiment
71 //        final String inputPcapFile = path + "/2018-07/stplug/stplug.wlan1.local.pcap";
72 //        final String outputPcapFile = path + "/2018-07/stplug/stplug-processed.pcap";
73 //        final String triggerTimesFile = path + "/2018-07/stplug/smartthings-july-25-2018.timestamps";
74 //        final String deviceIp = "192.168.1.246"; // .246 == phone; .142 == SmartThings Hub (note: use eth0 capture for this!)
75
76         // 4) Wemo July 30 experiment
77 //        final String inputPcapFile = path + "/2018-07/wemo/wemo.wlan1.local.pcap";
78 //        final String outputPcapFile = path + "/2018-07/wemo/wemo-processed.pcap";
79 //        final String triggerTimesFile = path + "/2018-07/wemo/wemo-july-30-2018.timestamps";
80 //        final String deviceIp = "192.168.1.145";
81
82         // 5) Wemo Insight July 31 experiment
83 //        final String inputPcapFile = path + "/2018-07/wemoinsight/wemoinsight.wlan1.local.pcap";
84 //        final String outputPcapFile = path + "/2018-07/wemoinsight/wemoinsight-processed.pcap";
85 //        final String triggerTimesFile = path + "/2018-07/wemoinsight/wemo-insight-july-31-2018.timestamps";
86 //        final String deviceIp = "192.168.1.135";
87
88         // 6) TP-Link Bulb August 1 experiment
89 //        final String inputPcapFile = path + "/2018-08/tplink-bulb/tplinkbulb.wlan1.local.pcap";
90 //        final String outputPcapFile = path + "/2018-08/tplink-bulb/tplinkbulb-processed.pcap";
91 //        final String triggerTimesFile = path + "/2018-08/tplink-bulb/tplink-bulb-aug-3-2018.timestamps";
92 //        final String deviceIp = "192.168.1.140"; // .246 == phone; .140 == TP-Link bulb
93
94         // 7) Kwikset Doorlock August 6 experiment
95 //        final String inputPcapFile = path + "/2018-08/kwikset-doorlock/kwikset-doorlock.wlan1.local.pcap";
96 //        final String outputPcapFile = path + "/2018-08/kwikset-doorlock/kwikset-doorlock-processed.pcap";
97 //        final String triggerTimesFile = path + "/2018-08/kwikset-doorlock/kwikset-doorlock-aug-6-2018.timestamps";
98 //        final String deviceIp = "192.168.1.246"; // .246 == phone; .142 == SmartThings Hub (note: use eth0 capture for this!)
99
100         // September 12, 2018 - includes both wlan1 and eth1 interfaces
101         //final String inputPcapFile = path + "/2018-08/kwikset-doorlock/kwikset3.wlan1.local.pcap";
102 //        final String inputPcapFile = path + "/2018-08/kwikset-doorlock/kwikset3.eth1.local.pcap";
103 //        final String outputPcapFile = path + "/2018-08/kwikset-doorlock/kwikset3-processed.pcap";
104 //        final String triggerTimesFile = path + "/2018-08/kwikset-doorlock/kwikset-doorlock-sept-12-2018.timestamps";
105 //        final String deviceIp = "192.168.1.142"; // .246 == phone; .142 == SmartThings Hub (note: use eth0 capture for this!)
106
107         // 8) Hue Bulb August 7 experiment
108 //        final String inputPcapFile = path + "/2018-08/hue-bulb/hue-bulb.wlan1.local.pcap";
109 //        final String outputPcapFile = path + "/2018-08/hue-bulb/hue-bulb-processed.pcap";
110 //        final String triggerTimesFile = path + "/2018-08/hue-bulb/hue-bulb-aug-7-2018.timestamps";
111 //        final String deviceIp = "192.168.1.246";
112
113         // 9) Lifx Bulb August 8 experiment
114 //        final String inputPcapFile = path + "/2018-08/lifx-bulb/lifx-bulb.wlan1.local.pcap";
115 //        final String outputPcapFile = path + "/2018-08/lifx-bulb/lifx-bulb-processed.pcap";
116 //        final String triggerTimesFile = path + "/2018-08/lifx-bulb/lifx-bulb-aug-8-2018.timestamps";
117 //        final String deviceIp = "192.168.1.246"; // .246 == phone; .231 == Lifx
118
119         // 10) Amcrest Camera August 9 experiment
120 //        final String inputPcapFile = path + "/2018-08/amcrest-camera/amcrest-camera.wlan1.local.pcap";
121 //        final String outputPcapFile = path + "/2018-08/amcrest-camera/amcrest-camera-processed.pcap";
122 //        final String triggerTimesFile = path + "/2018-08/amcrest-camera/amcrest-camera-aug-9-2018.timestamps";
123 //        final String deviceIp = "192.168.1.246"; // .246 == phone; .235 == camera
124
125         // 11) Arlo Camera August 10 experiment
126 //        final String inputPcapFile = path + "/2018-08/arlo-camera/arlo-camera.wlan1.local.pcap";
127 //        final String outputPcapFile = path + "/2018-08/arlo-camera/arlo-camera-processed.pcap";
128 //        final String triggerTimesFile = path + "/2018-08/arlo-camera/arlo-camera-aug-10-2018.timestamps";
129 //        final String deviceIp = "192.168.1.140"; // .246 == phone; .140 == camera
130
131         // 12) Blossom sprinkler August 13 experiment
132 //        final String inputPcapFile = path + "/2018-08/blossom/blossom.wlan1.local.pcap";
133 //        final String outputPcapFile = path + "/2018-08/blossom/blossom-processed.pcap";
134 //        final String triggerTimesFile = path + "/2018-08/blossom/blossom-aug-13-2018.timestamps";
135 //        final String deviceIp = "192.168.1.246"; // .246 == phone; .229 == sprinkler
136
137 //        // 13) DLink siren August 14 experiment
138         final String inputPcapFile = path + "/2018-08/dlink-siren/dlink-siren.wlan1.local.pcap";
139         final String outputPcapFile = path + "/2018-08/dlink-siren/dlink-siren-processed.pcap";
140         final String triggerTimesFile = path + "/2018-08/dlink-siren/dlink-siren-aug-14-2018.timestamps";
141         final String deviceIp = "192.168.1.183"; // .246 == phone; .183 == siren
142
143         // 14) Nest thermostat August 15 experiment
144 //        final String inputPcapFile = path + "/2018-08/nest/nest.wlan1.local.pcap";
145 //        final String outputPcapFile = path + "/2018-08/nest/nest-processed.pcap";
146 //        final String triggerTimesFile = path + "/2018-08/nest/nest-aug-15-2018.timestamps";
147 //        final String deviceIp = "192.168.1.246"; // .246 == phone; .127 == Nest thermostat
148
149         // 15) Alexa August 16 experiment
150 //        final String inputPcapFile = path + "/2018-08/alexa/alexa.wlan1.local.pcap";
151 //        final String outputPcapFile = path + "/2018-08/alexa/alexa-processed.pcap";
152 //        final String triggerTimesFile = path + "/2018-08/alexa/alexa-aug-16-2018.timestamps";
153 //        final String deviceIp = "192.168.1.225"; // .246 == phone; .225 == Alexa
154         // August 17
155 //        final String inputPcapFile = path + "/2018-08/alexa/alexa2.wlan1.local.pcap";
156 //        final String outputPcapFile = path + "/2018-08/alexa/alexa2-processed.pcap";
157 //        final String triggerTimesFile = path + "/2018-08/alexa/alexa-aug-17-2018.timestamps";
158 //        final String deviceIp = "192.168.1.225"; // .246 == phone; .225 == Alexa
159
160         // September 17
161 //        final String inputPcapFile = path + "/2018-08/noise/noise.eth1.pcap";
162 //        final String outputPcapFile = path + "/2018-08/noise/noise-processed.pcap";
163 //        final String triggerTimesFile = path + "/2018-08/noise/noise-sept-17-2018.timestamps";
164 //        final String deviceIp = "192.168.1.142"; //  .142 == SmartThings Hub; .199 == dlink plug; .183 == siren
165
166         TriggerTimesFileReader ttfr = new TriggerTimesFileReader();
167         List<Instant> triggerTimes = ttfr.readTriggerTimes(triggerTimesFile, false);
168         // Tag each trigger with "ON" or "OFF", assuming that the first trigger is an "ON" and that they alternate.
169         List<UserAction> userActions = new ArrayList<>();
170         for (int i = 0; i < triggerTimes.size(); i++) {
171             userActions.add(new UserAction(i % 2 == 0 ? Type.TOGGLE_ON : Type.TOGGLE_OFF, triggerTimes.get(i)));
172         }
173         TriggerTrafficExtractor tte = new TriggerTrafficExtractor(inputPcapFile, triggerTimes, deviceIp);
174         final PcapDumper outputter = Pcaps.openDead(DataLinkType.EN10MB, 65536).dumpOpen(outputPcapFile);
175         DnsMap dnsMap = new DnsMap();
176         TcpReassembler tcpReassembler = new TcpReassembler();
177         TrafficLabeler trafficLabeler = new TrafficLabeler(userActions);
178         tte.performExtraction(pkt -> {
179             try {
180                 outputter.dump(pkt);
181             } catch (NotOpenException e) {
182                 e.printStackTrace();
183             }
184         }, dnsMap, tcpReassembler, trafficLabeler);
185         outputter.flush();
186         outputter.close();
187
188         if (tte.getPacketsIncludedCount() != trafficLabeler.getTotalPacketCount()) {
189             // Sanity/debug check
190             throw new AssertionError(String.format("mismatch between packet count in %s and %s",
191                     TriggerTrafficExtractor.class.getSimpleName(), TrafficLabeler.class.getSimpleName()));
192         }
193
194         // Extract all conversations present in the filtered trace.
195         List<Conversation> allConversations = tcpReassembler.getTcpConversations();
196         // Group conversations by hostname.
197         Map<String, List<Conversation>> convsByHostname = TcpConversationUtils.groupConversationsByHostname(allConversations, dnsMap);
198         System.out.println("Grouped conversations by hostname.");
199         // For each hostname, count the frequencies of packet lengths exchanged with that hostname.
200         final Map<String, Map<Integer, Integer>> pktLenFreqsByHostname = new HashMap<>();
201         convsByHostname.forEach((host, convs) -> pktLenFreqsByHostname.put(host, TcpConversationUtils.countPacketLengthFrequencies(convs)));
202         System.out.println("Counted frequencies of packet lengths exchanged with each hostname.");
203         // For each hostname, count the frequencies of packet sequences (i.e., count how many conversations exchange a
204         // sequence of packets of some specific lengths).
205         final Map<String, Map<String, Integer>> pktSeqFreqsByHostname = new HashMap<>();
206         convsByHostname.forEach((host, convs) -> pktSeqFreqsByHostname.put(host, TcpConversationUtils.countPacketSequenceFrequencies(convs)));
207         System.out.println("Counted frequencies of packet sequences exchanged with each hostname.");
208         // For each hostname, count frequencies of packet pairs exchanged with that hostname across all conversations
209         final Map<String, Map<String, Integer>> pktPairFreqsByHostname =
210                 TcpConversationUtils.countPacketPairFrequenciesByHostname(allConversations, dnsMap);
211         System.out.println("Counted frequencies of packet pairs per hostname");
212         // For each user action, reassemble the set of TCP connections occurring shortly after
213         final Map<UserAction, List<Conversation>> userActionToConversations = trafficLabeler.getLabeledReassembledTcpTraffic();
214         final Map<UserAction, Map<String, List<Conversation>>> userActionsToConvsByHostname = trafficLabeler.getLabeledReassembledTcpTraffic(dnsMap);
215         System.out.println("Reassembled TCP conversations occurring shortly after each user event");
216
217
218
219         /*
220          * NOTE: no need to generate these more complex on/off maps that also contain mappings from hostname and
221          * sequence identifiers as we do not care about hostnames and sequences during clustering.
222          * We can simply use the UserAction->List<Conversation> map to generate ON/OFF groupings of conversations.
223          */
224         /*
225         // Contains all ON events: hostname -> sequence identifier -> list of conversations with that sequence
226         Map<String, Map<String, List<Conversation>>> ons = new HashMap<>();
227         // Contains all OFF events: hostname -> sequence identifier -> list of conversations with that sequence
228         Map<String, Map<String, List<Conversation>>> offs = new HashMap<>();
229         userActionsToConvsByHostname.forEach((ua, hostnameToConvs) -> {
230             Map<String, Map<String, List<Conversation>>> outer = ua.getType() == Type.TOGGLE_ON ? ons : offs;
231             hostnameToConvs.forEach((host, convs) -> {
232                 Map<String, List<Conversation>> seqsToConvs = TcpConversationUtils.
233                         groupConversationsByPacketSequence(convs, verbose);
234                 outer.merge(host, seqsToConvs, (oldMap, newMap) -> {
235                     newMap.forEach((sequence, cs) -> oldMap.merge(sequence, cs, (list1, list2) -> {
236                         list1.addAll(list2);
237                         return list1;
238                     }));
239                     return oldMap;
240                 });
241             });
242         });
243         */
244
245         // ================================================ CLUSTERING ================================================
246         // Note: no need to use the more convoluted on/off maps; can simply use the UserAction->List<Conversation> map
247         // when don't care about hostnames and sequences (see comment earlier).
248         List<Conversation> onConversations = userActionToConversations.entrySet().stream().
249                 filter(e -> e.getKey().getType() == Type.TOGGLE_ON). // drop all OFF events from stream
250                 map(e -> e.getValue()). // no longer interested in the UserActions
251                 flatMap(List::stream). // flatten List<List<T>> to a List<T>
252                 collect(Collectors.toList());
253         List<Conversation> offConversations = userActionToConversations.entrySet().stream().
254                 filter(e -> e.getKey().getType() == Type.TOGGLE_OFF).
255                 map(e -> e.getValue()).
256                 flatMap(List::stream).
257                 collect(Collectors.toList());
258         List<PcapPacketPair> onPairs = onConversations.stream().
259                 map(c -> c.isTls() ? TcpConversationUtils.extractTlsAppDataPacketPairs(c) :
260                         TcpConversationUtils.extractPacketPairs(c)).
261                 flatMap(List::stream). // flatten List<List<>> to List<>
262                 collect(Collectors.toList());
263         List<PcapPacketPair> offPairs = offConversations.stream().
264                 map(c -> c.isTls() ? TcpConversationUtils.extractTlsAppDataPacketPairs(c) :
265                         TcpConversationUtils.extractPacketPairs(c)).
266                 flatMap(List::stream). // flatten List<List<>> to List<>
267                 collect(Collectors.toList());
268         // Note: need to update the DnsMap of all PcapPacketPairs if we want to use the IP/hostname-sensitive distance.
269         Stream.concat(Stream.of(onPairs), Stream.of(offPairs)).flatMap(List::stream).forEach(p -> p.setDnsMap(dnsMap));
270         // Perform clustering on conversation logged as part of all ON events.
271         DBSCANClusterer<PcapPacketPair> onClusterer = new DBSCANClusterer<>(10.0, 5);
272         List<Cluster<PcapPacketPair>> onClusters = onClusterer.cluster(onPairs);
273         // Perform clustering on conversation logged as part of all OFF events.
274         DBSCANClusterer<PcapPacketPair> offClusterer = new DBSCANClusterer<>(10.0, 5);
275         List<Cluster<PcapPacketPair>> offClusters = offClusterer.cluster(offPairs);
276         // Output clusters
277         System.out.println("========================================");
278         System.out.println("       Clustering results for ON        ");
279         System.out.println("       Number of clusters: " + onClusters.size());
280         int count = 0;
281         for (Cluster<PcapPacketPair> c : onClusters) {
282             System.out.println(String.format("<<< Cluster #%02d (%03d points) >>>", ++count, c.getPoints().size()));
283             System.out.print(PrintUtils.toSummaryString(c));
284         }
285         System.out.println("========================================");
286         System.out.println("       Clustering results for OFF       ");
287         System.out.println("       Number of clusters: " + offClusters.size());
288         count = 0;
289         for (Cluster<PcapPacketPair> c : offClusters) {
290             System.out.println(String.format("<<< Cluster #%03d (%06d points) >>>", ++count, c.getPoints().size()));
291             System.out.print(PrintUtils.toSummaryString(c));
292         }
293         System.out.println("========================================");
294         // ============================================================================================================
295
296         /*
297         System.out.println("==== ON ====");
298         // Print out all the pairs into a file for ON events
299         File fileOnEvents = new File(onPairsPath);
300         PrintWriter pwOn = null;
301         try {
302             pwOn = new PrintWriter(fileOnEvents);
303         } catch(Exception ex) {
304             ex.printStackTrace();
305         }
306         for(Map.Entry<String, Map<String, List<Conversation>>> entry : ons.entrySet()) {
307             Map<String, List<Conversation>> seqsToConvs = entry.getValue();
308             for(Map.Entry<String, List<Conversation>> entryConv : seqsToConvs.entrySet()) {
309                 List<Conversation> listConv = entryConv.getValue();
310                 // Just get the first Conversation because all Conversations in this group
311                 // should have the same pairs of Application Data.
312                 for(Conversation conv : listConv) {
313                     // Process only if it is a TLS packet
314                     if (conv.isTls()) {
315                         List<PcapPacketPair> tlsAppDataList = TcpConversationUtils.extractTlsAppDataPacketPairs(conv);
316                         for(PcapPacketPair pair: tlsAppDataList) {
317                             System.out.println(PrintUtils.toCsv(pair, dnsMap));
318                             pwOn.println(PrintUtils.toCsv(pair, dnsMap));
319                         }
320                     } else { // Non-TLS conversations
321                         List<PcapPacketPair> packetList = TcpConversationUtils.extractPacketPairs(conv);
322                         for(PcapPacketPair pair: packetList) {
323                             System.out.println(PrintUtils.toCsv(pair, dnsMap));
324                             pwOn.println(PrintUtils.toCsv(pair, dnsMap));
325                         }
326                     }
327                 }
328             }
329         }
330         pwOn.close();
331
332         System.out.println("==== OFF ====");
333         // Print out all the pairs into a file for ON events
334         File fileOffEvents = new File(offPairsPath);
335         PrintWriter pwOff = null;
336         try {
337             pwOff = new PrintWriter(fileOffEvents);
338         } catch(Exception ex) {
339             ex.printStackTrace();
340         }
341         for(Map.Entry<String, Map<String, List<Conversation>>> entry : offs.entrySet()) {
342             Map<String, List<Conversation>> seqsToConvs = entry.getValue();
343             for(Map.Entry<String, List<Conversation>> entryConv : seqsToConvs.entrySet()) {
344                 List<Conversation> listConv = entryConv.getValue();
345                 // Just get the first Conversation because all Conversations in this group
346                 // should have the same pairs of Application Data.
347                 for(Conversation conv : listConv) {
348                     // Process only if it is a TLS packet
349                     if (conv.isTls()) {
350                         List<PcapPacketPair> tlsAppDataList = TcpConversationUtils.extractTlsAppDataPacketPairs(conv);
351                         for(PcapPacketPair pair: tlsAppDataList) {
352                             System.out.println(PrintUtils.toCsv(pair, dnsMap));
353                             pwOff.println(PrintUtils.toCsv(pair, dnsMap));
354                         }
355                     } else { // Non-TLS conversations
356                         List<PcapPacketPair> packetList = TcpConversationUtils.extractPacketPairs(conv);
357                         for (PcapPacketPair pair : packetList) {
358                             System.out.println(PrintUtils.toCsv(pair, dnsMap));
359                             pwOff.println(PrintUtils.toCsv(pair, dnsMap));
360                         }
361                     }
362                 }
363             }
364         }
365         pwOff.close();
366         */
367
368 //        // ================================================================================================
369 //        // <<< Some work-in-progress/explorative code that extracts a "representative" sequence >>>
370 //        //
371 //        // Currently need to know relevant hostname in advance :(
372 //        String hostname = "events.tplinkra.com";
373 ////        String hostname = "rfe-us-west-1.dch.dlink.com";
374 //        // Conversations with 'hostname' for ON events.
375 //        List<Conversation> onsForHostname = new ArrayList<>();
376 //        // Conversations with 'hostname' for OFF events.
377 //        List<Conversation> offsForHostname = new ArrayList<>();
378 //        // "Unwrap" sequence groupings in ons/offs maps.
379 //        ons.get(hostname).forEach((k,v) -> onsForHostname.addAll(v));
380 //        offs.get(hostname).forEach((k,v) -> offsForHostname.addAll(v));
381 //
382 //
383 //        Map<String, List<Conversation>> onsForHostnameGroupedByTlsAppDataSequence = TcpConversationUtils.groupConversationsByTlsApplicationDataPacketSequence(onsForHostname);
384 //
385 //
386 //        // Extract representative sequence for ON and OFF by providing the list of conversations with
387 //        // 'hostname' observed for each event type (the training data).
388 //        SequenceExtraction seqExtraction = new SequenceExtraction();
389 ////        ExtractedSequence extractedSequenceForOn = seqExtraction.extract(onsForHostname);
390 ////        ExtractedSequence extractedSequenceForOff = seqExtraction.extract(offsForHostname);
391 //
392 //        ExtractedSequence extractedSequenceForOn = seqExtraction.extractByTlsAppData(onsForHostname);
393 //        ExtractedSequence extractedSequenceForOff = seqExtraction.extractByTlsAppData(offsForHostname);
394 //
395 //        // Let's check how many ONs align with OFFs and vice versa (that is, how many times an event is incorrectly
396 //        // labeled).
397 //        int onsLabeledAsOff = 0;
398 //        Integer[] representativeOnSeq = TcpConversationUtils.getPacketLengthSequence(extractedSequenceForOn.getRepresentativeSequence());
399 //        Integer[] representativeOffSeq = TcpConversationUtils.getPacketLengthSequence(extractedSequenceForOff.getRepresentativeSequence());
400 //        SequenceAlignment<Integer> seqAlg = seqExtraction.getAlignmentAlgorithm();
401 //        for (Conversation c : onsForHostname) {
402 //            Integer[] onSeq = TcpConversationUtils.getPacketLengthSequence(c);
403 //            if (seqAlg.calculateAlignment(representativeOffSeq, onSeq) <= extractedSequenceForOff.getMaxAlignmentCost()) {
404 //                onsLabeledAsOff++;
405 //            }
406 //        }
407 //        int offsLabeledAsOn = 0;
408 //        for (Conversation c : offsForHostname) {
409 //            Integer[] offSeq = TcpConversationUtils.getPacketLengthSequence(c);
410 //            if (seqAlg.calculateAlignment(representativeOnSeq, offSeq) <= extractedSequenceForOn.getMaxAlignmentCost()) {
411 //                offsLabeledAsOn++;
412 //            }
413 //        }
414 //        System.out.println("");
415 //        // ================================================================================================
416 //
417 //
418 //        // -------------------------------------------------------------------------------------------------------------
419 //        // -------------------------------------------------------------------------------------------------------------
420     }
421
422 }
423
424
425 // TP-Link MAC 50:c7:bf:33:1f:09 and usually IP 192.168.1.159 (remember to verify per file)
426 // frame.len >= 556 && frame.len <= 558 && ip.addr == 192.168.1.159