Esempio n. 1
0
        public void Configuration(CommandLineApplication command)
        {
            command.Description = "Create CoAP profiles from the specific capture file.";
            command.HelpOption("-?|-Help");

            var inputCsvOption = command.Option("-InputCsvFile <string>",
                                                "A name of an input file in csv format that contains decoded CoAP packets.",
                                                CommandOptionType.MultipleValue);

            var inputCapOption = command.Option("-InputCapFile <string>",
                                                "A name of an input file in cap format that contains CoAP packets.",
                                                CommandOptionType.MultipleValue);

            var writeOption = command.Option("-WriteTo <filename>",
                                             "An output file to write binary representation of the profile.",
                                             CommandOptionType.SingleValue);

            var windowSizeOption = command.Option("-WindowSize <double>",
                                                  "A size of windows in seconds.",
                                                  CommandOptionType.MultipleValue);

            var protocolOption = command.Option("-Protocol <string>",
                                                "Specifies the protocol to analyze. Supported protocolas are: coap, iec. Default is coap.",
                                                CommandOptionType.SingleValue);

            var modelKeyOption = command.Option("-ModelKey <scheme>",
                                                "Model key represents an aggregation scheme that is used to build individual flow models. It is usually drawn from the following values: 'coap.code', 'coap.type', 'coap.uri_path'. Default is  'coap.code,coap.type,coap.uri_path'.",
                                                CommandOptionType.SingleValue);

            var aggregateOption = command.Option("-Aggregate <scheme>",
                                                 "Aggregation scheme enables to group flows to group of flows. It accepts any combination of 'ip.src', 'ip.dst', 'udp.srcport', 'udp.dstport'. Default is ''.",
                                                 CommandOptionType.SingleValue);

            var modelClassOption = command.Option("-ModelClass <string>",
                                                  "A name of class representing the model.",
                                                  CommandOptionType.SingleValue);

            var windowsCountOption = command.Option("-LearningWindows <double[%]>",
                                                    "A number of windows used to learn from the source data. Default is all. It is possible to specify a ratio of data used for learning, e.g., '20%'.",
                                                    CommandOptionType.SingleValue);


            command.OnExecute(() =>
            {
                if (!writeOption.HasValue())
                {
                    throw new CommandParsingException(command, $"{writeOption.ShortName} is required but was not provided!");
                }
                if (!(inputCsvOption.HasValue() || inputCapOption.HasValue()))
                {
                    throw new CommandParsingException(command, $"{inputCsvOption.ShortName} or {inputCapOption.ShortName} is required but was not provided!");
                }

                var settings = new Settings
                {
                    WindowSize      = windowSizeOption.HasValue() ? Double.Parse(windowSizeOption.Value()) : DefaultWindowSize,
                    Protocol        = protocolOption.HasValue() ? protocolOption.Value() : "coap",
                    FlowAggregation = aggregateOption.HasValue() ? aggregateOption.Value() : String.Empty,
                    ModelKey        = modelKeyOption.HasValue() ? modelKeyOption.Value() : String.Empty,
                    ModelClass      = modelClassOption.HasValue()? modelKeyOption.Value() : nameof(CoapStatisticalModel),
                    WindowsCount    = windowsCountOption.HasValue() ? LearningWindow.Parse(windowsCountOption.Value()) : LearningWindow.All
                };

                ProcessInput(inputCsvOption.Value(), inputCapOption.Value(), writeOption.Value(), settings);
                return(0);
            });
        }
Esempio n. 2
0
        private void LoadAndCompute(FlowProfile profile, string inputFile, IList <CoapPacketRecord> packets, double windowSize, LearningWindow learningWindows,
                                    ProtocolFactory protocolFactory, Enum modelKey, FlowKey.Fields flowAggregation = FlowKey.Fields.None)
        {
            var getFlowKeyFunc  = FlowKey.GetFlowKeyFunc(flowAggregation);
            var getModelKeyFunc = protocolFactory.GetModelKeyFunc(modelKey);

            Console.WriteLine("┌ Load packets and compute profile:");
            Console.WriteLine($"├─ input file: {inputFile}");
            Console.WriteLine($"├─ packets count: {packets.Count}");
            Console.WriteLine($"├─ window size: {windowSize} seconds");
            Console.WriteLine($"├─ learning window: {learningWindows.Meassure}({learningWindows.Value})");
            Console.WriteLine($"├─ protocol: {protocolFactory.Name}");
            Console.WriteLine($"├─ model key: {modelKey}");
            Console.WriteLine($"├─ flow aggregation: {flowAggregation}");
            var sw = new Stopwatch();

            sw.Start();

            var startTime  = packets.First().TimeEpoch;
            var packetBins = packets.GroupBy(p => (int)Math.Floor((p.TimeEpoch - startTime) / windowSize)).ToList();

            var learningBins = learningWindows.Meassure == LearningWindow.ValueType.Absolute ? packetBins.Take((int)learningWindows.Value) :
                               packetBins.Take((int)(packetBins.Count() * learningWindows.Value));
            var pb1 = new ProgressBar(packetBins.Count());

            foreach (var group in packetBins)
            {
                pb1.Next($"├─ processing bin {group.Key}: {group.Count()} items");
                var flows = protocolFactory.CollectCoapFlows(group, getModelKeyFunc, getFlowKeyFunc);
                ComputeProfile(profile, flows);
            }
            var pb2 = new ProgressBar(profile.Count);

            profile.Commit(() => pb2.Next($"├─ fitting models"));
            Console.WriteLine($"└ done [{sw.Elapsed}].");
        }