/// <summary> /// Starting run algorithm with specific source topology and K-diameter cut, /// and this round process will generate scope topology to scop_net_topo, /// and will add the deployment node to deployNodes structure. /// Finally, it will return the remain network topology with this process. /// </summary> /// <param name="src_net_topo">The source network topology</param> /// <param name="K">The K-diameter cut value</param> /// <param name="scope_net_topo">This round process that generte scope topology</param> /// <param name="deployNodes">The result of the deployment node id</param> /// <returns>The remain network topology after process the algorithm.</returns> private NetworkTopology startAlgorithm(NetworkTopology src_net_topo, int K, ref NetworkTopology scope_net_topo, ref List <int> deployNodes) { double max_hop_count = double.MinValue; while (max_hop_count < K) { int minDegree = int.MaxValue; int selectNode = -1; // to finding the neighbor node with minimum degree foreach (var scopeNode in scope_net_topo.Nodes) { foreach (int neighbor in src_net_topo.GetNeighborNodeIDs(scopeNode.ID)) { if (!scope_net_topo.Nodes.Exists(x => x.ID == neighbor)) { if (minDegree > src_net_topo.Nodes.Find(n => n.ID == neighbor).Degree) { minDegree = src_net_topo.Nodes.Find(n => n.ID == neighbor).Degree; selectNode = neighbor; } } } } // if nothing found, break the loop. if (selectNode == -1) { break; } // adding the node to the scope set, and computing the max hop count. else { scope_net_topo.Nodes.Add(src_net_topo.Nodes.Find(n => n.ID == selectNode)); foreach (var scopeNode in scope_net_topo.Nodes) { scope_net_topo.Edges.AddRange(src_net_topo.Edges.Where(e => e.Node1 == scopeNode.ID && e.Node2 == selectNode || e.Node1 == selectNode && e.Node2 == scopeNode.ID) .ToList()); } scope_net_topo.ComputingShortestPath(); foreach (var node1 in scope_net_topo.Nodes) { foreach (var node2 in scope_net_topo.Nodes) { int hop_count = scope_net_topo.GetShortestPath(node1.ID, node2.ID).Count; if (max_hop_count < hop_count) { max_hop_count = hop_count; } } } } } List <int> tmp = new List <int>(); NetworkTopology remain_topo; // Handling the neighbor nodes of each node in the scope network topology. foreach (var scopeNode in scope_net_topo.Nodes) { tmp.AddRange(src_net_topo.GetNeighborNodeIDs(scopeNode.ID).Except(scope_net_topo.Nodes.Select(x => x.ID))); } tmp = tmp.Distinct().ToList(); // During above process the tmp list will be deployment nodes, and add to deployNodes list. deployNodes.AddRange(tmp); // Adding deploy nodes to the scope network topology. foreach (int id in tmp) { scope_net_topo.Nodes.AddRange(src_net_topo.Nodes.Where(x => x.ID == id).ToList()); } // Adding deploy nodes's edge to the scope network topology. foreach (var scopeNode in scope_net_topo.Nodes) { scope_net_topo.Edges.AddRange(src_net_topo.Edges.Where(x => x.Node1 == scopeNode.ID || x.Node2 == scopeNode.ID).ToList()); } scope_net_topo.Edges = scope_net_topo.Edges.Distinct().ToList(); // Computing the complement set between source and scope network topology. remain_topo = src_net_topo - scope_net_topo; // Computing the complement set's shortest path. //remain_topo.ComputingShortestPath(); // Removing deployment nodes and edges from scope network topology. foreach (int id in tmp) { scope_net_topo.Nodes.RemoveAll(x => x.ID == id); scope_net_topo.Edges.RemoveAll(x => x.Node1 == id || x.Node2 == id); } return(remain_topo); }
public void Run(int attackPacketPerSec, int normalPacketPerSec, int totalPacket, int percentageOfAttackPacket, double probibilityOfPacketTunneling, double probibilityOfPacketMarking, double startFiltering, int initialTimeOfAttackPacket, bool dynamicProbability, bool considerDistance) { Random rd = new Random(); int victimID; int attackerIndex = 0; List <int> path; List <PacketEvent> packetEventList = new List <PacketEvent>(); List <PacketSentEvent> packetSentEventList = new List <PacketSentEvent>(); List <TunnelingEvent> tunnelingEventList = new List <TunnelingEvent>(); List <MarkingEvent> markingEventList = new List <MarkingEvent>(); List <FilteringEvent> filteringEventList = new List <FilteringEvent>(); foreach (NetworkTopology.Node node in topology.Nodes) { if (node.Tracer == NetworkTopology.TracerType.Tunneling) { node.ProbabilityOfTunneling = probibilityOfPacketTunneling; node.FilteringCount = probibilityOfPacketTunneling * 10; node.IsTunnelingActive = true; } } for (int i = 0; i < totalPacket; i++) { int tunnelingNodeID = -1; bool isTunneling = false; bool isMarking = false; bool isFiltering = false; bool shouldMarking = false; bool shouldFiltering = false; NetworkTopology.Node SourceNode; victimID = topology.idOfVictims[rd.Next(topology.idOfVictims.Count)]; do { if (rd.NextDouble() < (double)percentageOfAttackPacket / 100) { SourceNode = attackNode[attackerIndex % attackNode.Count]; attackerIndex++; } else { SourceNode = topology.Nodes[rd.Next(topology.Nodes.Count)]; } } while (SourceNode.ID == victimID); path = topology.GetShortestPath(SourceNode.ID, victimID); PacketEvent packetEvent = new PacketEvent() { PacketID = i, Source = SourceNode.ID, Destination = victimID, Time = SourceNode.Type == NetworkTopology.NodeType.Attacker ? initialTimeOfAttackPacket : 0, Type = SourceNode.Type }; packetEventList.Add(packetEvent); for (int j = 0; j < path.Count; j++) { packetEvent.Time = j == 0 ? packetEvent.Time : packetEvent.Time += topology.AdjacentMatrix[topology.NodeID2Index(path[j - 1]), topology.NodeID2Index(path[j])].Delay; switch (topology.Nodes[topology.NodeID2Index(path[j])].Tracer) { case NetworkTopology.TracerType.Tunneling: if (!isTunneling && !isMarking && rd.NextDouble() <= topology.Nodes.Find(node => node.ID == path[j]).ProbabilityOfTunneling&& topology.Nodes.Find(node => node.ID == path[j]).IsTunnelingActive) { TunnelingEvent tunnelingEvent = new TunnelingEvent(packetEvent); tunnelingEvent.TunnelingSrc = path[j]; // Re-computing path. //if (i < startFiltering * totalPacket / 100) if (markingEventList.Count * 100 / totalPacket < startFiltering) { path = ChooseTunnelingNode(path, j, NetworkTopology.TracerType.Marking, ref tunnelingEvent, considerDistance); shouldMarking = true; } else { path = ChooseTunnelingNode(path, j, NetworkTopology.TracerType.Filtering, ref tunnelingEvent, considerDistance); shouldFiltering = true; } tunnelingNodeID = tunnelingEvent.TunnelingSrc; tunnelingEventList.Add(tunnelingEvent); isTunneling = true; } break; case NetworkTopology.TracerType.Marking: if ((!isMarking && rd.NextDouble() <= probibilityOfPacketMarking && markingEventList.Count * 100 / totalPacket < startFiltering && !shouldFiltering) || shouldMarking) { MarkingEvent markingEvent = new MarkingEvent(packetEvent); if (shouldMarking && tunnelingNodeID != -1) { markingEvent.MarkingNodeID = tunnelingNodeID; } else { markingEvent.MarkingNodeID = path[j]; } markingEventList.Add(markingEvent); isMarking = true; shouldMarking = false; if (markingEventList.Count * 100 / totalPacket >= startFiltering) { foreach (MarkingEvent e in markingEventList.Where(e => e.Type != NetworkTopology.NodeType.Attacker)) { NetworkTopology.Node n = topology.Nodes.Find(node => node.ID == e.MarkingNodeID); if (n.Tracer == NetworkTopology.TracerType.Tunneling) { n.IsTunnelingActive = false; } } } } break; case NetworkTopology.TracerType.Filtering: if (shouldFiltering || markingEventList.Count * 100 / totalPacket >= startFiltering) { if (packetEvent.Type == NetworkTopology.NodeType.Attacker) { FilteringEvent filteringEvent = new FilteringEvent(packetEvent); filteringEvent.FilteringNodeID = path[j]; filteringEventList.Add(filteringEvent); isFiltering = true; // Adjust probability of Tunneling tracer which tunneling from... if (dynamicProbability && tunnelingNodeID != -1) { Network_Simulation.NetworkTopology.Node node = topology.Nodes.Find(n => n.ID == tunnelingNodeID); node.FilteringCount = node.FilteringCount >= 10 ? 10 : node.FilteringCount + 1; node.ProbabilityOfTunneling = node.FilteringCount / 10; } } else if (dynamicProbability && tunnelingNodeID != -1) { Network_Simulation.NetworkTopology.Node node = topology.Nodes.Find(n => n.ID == tunnelingNodeID); node.FilteringCount = node.FilteringCount <= 0 ? 0 : node.FilteringCount - 1; node.ProbabilityOfTunneling = node.FilteringCount / 10; } } break; } if (isFiltering) { break; } PacketSentEvent packetSentEvent = new PacketSentEvent(packetEvent); packetSentEvent.CurrentNodeID = path[j]; packetSentEvent.NextHopID = j == path.Count - 1 ? -1 : path[j + 1]; packetSentEvent.Length = j == path.Count - 1 ? 0 : topology.AdjacentMatrix[topology.NodeID2Index(path[j]), topology.NodeID2Index(path[j + 1])].Length; packetSentEventList.Add(packetSentEvent); } Report(i + 1, totalPacket); //DataUtility.Log("=============================="); //foreach (NetworkTopology.Node node in topology.Nodes) // DataUtility.Log(string.Format("Node%{0} FC:{1} Tp:{2}\n", node.ID, node.FilteringCount, node.ProbabilityOfTunneling)); } List <PacketSentEvent> tracingList = new List <PacketSentEvent>(); int packetID = 0; List <int> FilteringAndMarkingTracerID = new List <int>(); if (deployment.FilteringTracerID != null) { FilteringAndMarkingTracerID.AddRange(deployment.FilteringTracerID); } if (deployment.MarkingTracerID != null) { FilteringAndMarkingTracerID.AddRange(deployment.MarkingTracerID); } foreach (int victim in topology.idOfVictims) { foreach (int nodeID in FilteringAndMarkingTracerID) { if (victim == nodeID) { continue; } path = topology.GetShortestPath(victim, nodeID); for (int i = 0; i < path.Count - 1; i++) { tracingList.Add(new PacketSentEvent(packetID) { CurrentNodeID = path[i], NextHopID = path[i + 1], Length = topology.AdjacentMatrix[topology.NodeID2Index(path[i]), topology.NodeID2Index(path[i + 1])].Length }); } packetID++; } if (version == "Random" || version == "V2") { foreach (int nodeID in deployment.TunnelingTracerID) { if (victim == nodeID) { continue; } path = topology.GetShortestPath(victim, nodeID); for (int i = 0; i < path.Count - 1; i++) { tracingList.Add(new PacketSentEvent(packetID) { CurrentNodeID = path[i], NextHopID = path[i + 1], Length = topology.AdjacentMatrix[topology.NodeID2Index(path[i]), topology.NodeID2Index(path[i + 1])].Length }); } packetID++; } } } if (version == "V1") { foreach (int nodeID in deployment.TunnelingTracerID) { deployment.FilteringTracerID.Sort((x, y) => { return(topology.GetShortestPathCount(x, nodeID).CompareTo(topology.GetShortestPathCount(y, nodeID))); }); path = topology.GetShortestPath(deployment.FilteringTracerID.First(), nodeID); for (int i = 0; i < path.Count - 1; i++) { tracingList.Add(new PacketSentEvent(packetID) { CurrentNodeID = path[i], NextHopID = path[i + 1], Length = topology.AdjacentMatrix[topology.NodeID2Index(path[i]), topology.NodeID2Index(path[i + 1])].Length }); } packetID++; } } sql.InsertTracingCost(tracingList); sql.InsertPacketEvent(packetEventList); sql.InsertPacketSentEvent(packetSentEventList); sql.InsertTunnelingEvent(tunnelingEventList); sql.InsertMarkingEvent(markingEventList); sql.InsertFilteringEvent(filteringEventList); sql.LogDeploymentResult(topology, deployment); }
private void simSpecificDeployMethod() { int centerID, minE; List <int> path; List <MarkingEvent> markingEventList = new List <MarkingEvent>(); //List<PacketEvent> packetEventList = new List<PacketEvent>(); //List<PacketSentEvent> packetSentEventList = new List<PacketSentEvent>(); List <int> firstMeetTracerHopCountList = new List <int>(); List <int> srcToScopeCenterHopCountList = new List <int>(); List <int> attackerAreaCounts = new List <int>(); List <int> pathCountList = new List <int>(); Random rd = new Random(Guid.NewGuid().GetHashCode()); for (int i = 0; i < c_packetNumber; i++) { bool isMarking = false; NetworkTopology.Node srcNode = m_networkTopology.Nodes[rd.Next(m_networkTopology.Nodes.Count)]; NetworkTopology.Node desNode = null; while (desNode == null || desNode == srcNode) { desNode = m_networkTopology.Nodes[rd.Next(m_networkTopology.Nodes.Count)]; } NetworkTopology sourceScope = m_deployment.AllRoundScopeList.Find(scope => scope.Nodes.Exists(n => n.ID == srcNode.ID)); if (sourceScope != null) { if (sourceScope.FindCenterNodeID(out centerID, out minE) && srcNode.ID != centerID) { srcToScopeCenterHopCountList.Add(m_networkTopology.GetShortestPathCount(srcNode.ID, centerID) - 1); } else { srcToScopeCenterHopCountList.Add(0); } attackerAreaCounts.Add(sourceScope.Nodes.Count); } else { srcToScopeCenterHopCountList.Add(0); attackerAreaCounts.Add(0); } path = m_networkTopology.GetShortestPath(srcNode.ID, desNode.ID); PacketEvent packetEvent = new PacketEvent() { PacketID = i, Source = srcNode.ID, Destination = desNode.ID, Time = 0, Type = NetworkTopology.NodeType.Attacker }; //packetEventList.Add(packetEvent); for (int j = 0; j < path.Count; j++) { switch (m_networkTopology.Nodes[m_networkTopology.NodeID2Index(path[j])].Tracer) { case NetworkTopology.TracerType.None: break; case NetworkTopology.TracerType.Marking: if (!isMarking) { MarkingEvent markingEvent = new MarkingEvent(packetEvent); markingEvent.MarkingNodeID = path[j]; firstMeetTracerHopCountList.Add(j); isMarking = true; markingEventList.Add(markingEvent); } break; case NetworkTopology.TracerType.Tunneling: break; case NetworkTopology.TracerType.Filtering: break; default: break; } //PacketSentEvent packetSentEvent = new PacketSentEvent(packetEvent); //packetSentEvent.CurrentNodeID = path[j]; //packetSentEvent.NextHopID = j == path.Count - 1 ? -1 : path[j + 1]; //packetSentEvent.Length = j == path.Count - 1 ? 0 : m_networkTopology.AdjacentMatrix[m_networkTopology.NodeID2Index(path[j]), m_networkTopology.NodeID2Index(path[j + 1])].Length; //packetSentEventList.Add(packetSentEvent); } pathCountList.Add(path.Count - 1); if (!isMarking) { firstMeetTracerHopCountList.Add(path.Count - 1); } } m_networkTopology.Reset(); // Log into db double theoreticalUndetectedRatio = (double)m_deployment.AllRoundScopeList.Sum(s => s.Nodes.Count > 1 ? DataUtility.Combination(s.Nodes.Count, 2) : 0) / (double)DataUtility.Combination(m_networkTopology.Nodes.Count, 2); double upperboundUndetectedRatio = m_networkTopology.m_prob_hop.Sum(i => (m_networkTopology.m_prob_hop.ToList().IndexOf(i) >= 1 && m_networkTopology.m_prob_hop.ToList().IndexOf(i) <= m_deployment.K - 1) ? i : 0); double undetectedRatio = (double)(c_packetNumber - markingEventList.Count) / (double)c_packetNumber; double firstMeetTracerSearchingCost = (double)firstMeetTracerHopCountList.Sum() / (double)c_packetNumber; double srcToScopeCenterSearchingCost = (double)srcToScopeCenterHopCountList.Sum() / (double)c_packetNumber; double attackerScopeCountSearchingCost = attackerAreaCounts.Average(); double savingCost = 0; double survivalTrafficRatio = (double)firstMeetTracerHopCountList.Sum() / (double)pathCountList.Sum(); for (int i = 0; i < c_packetNumber; i++) { savingCost += pathCountList[i] - firstMeetTracerHopCountList[i]; } savingCost /= c_packetNumber; string cmd; // UndetectedRatio cmd = "INSERT INTO UndetectedRatio(file_name, node_counts, edge_counts, diameter, k, n, metric_name, ratio) VALUES(@file_name, @node_counts, @edge_counts, @diameter, @k, @n, @metric_name, @ratio);"; m_sqlite_utils.RunCommnad(cmd, new List <SQLiteParameter>() { new SQLiteParameter("@file_name", m_networkTopology.FileName), new SQLiteParameter("@node_counts", m_networkTopology.Nodes.Count), new SQLiteParameter("@edge_counts", m_networkTopology.Edges.Count), new SQLiteParameter("@diameter", m_networkTopology.Diameter), new SQLiteParameter("@k", m_deployment.K), new SQLiteParameter("@n", m_deployment.N), new SQLiteParameter("@metric_name", "K-Cut Deployment"), new SQLiteParameter("@ratio", undetectedRatio) }); cmd = "INSERT INTO UndetectedRatio(file_name, node_counts, edge_counts, diameter, k, n, metric_name, ratio) VALUES(@file_name, @node_counts, @edge_counts, @diameter, @k, @n, @metric_name, @ratio);"; m_sqlite_utils.RunCommnad(cmd, new List <SQLiteParameter>() { new SQLiteParameter("@file_name", m_networkTopology.FileName), new SQLiteParameter("@node_counts", m_networkTopology.Nodes.Count), new SQLiteParameter("@edge_counts", m_networkTopology.Edges.Count), new SQLiteParameter("@diameter", m_networkTopology.Diameter), new SQLiteParameter("@k", m_deployment.K), new SQLiteParameter("@n", m_deployment.N), new SQLiteParameter("@metric_name", "Theoretical Undetected Ratio"), new SQLiteParameter("@ratio", theoreticalUndetectedRatio) }); cmd = "INSERT INTO UndetectedRatio(file_name, node_counts, edge_counts, diameter, k, n, metric_name, ratio) VALUES(@file_name, @node_counts, @edge_counts, @diameter, @k, @n, @metric_name, @ratio);"; m_sqlite_utils.RunCommnad(cmd, new List <SQLiteParameter>() { new SQLiteParameter("@file_name", m_networkTopology.FileName), new SQLiteParameter("@node_counts", m_networkTopology.Nodes.Count), new SQLiteParameter("@edge_counts", m_networkTopology.Edges.Count), new SQLiteParameter("@diameter", m_networkTopology.Diameter), new SQLiteParameter("@k", m_deployment.K), new SQLiteParameter("@n", m_deployment.N), new SQLiteParameter("@metric_name", "Theoretical Undetected Ratio Upper Bound"), new SQLiteParameter("@ratio", upperboundUndetectedRatio) }); // Searching Cost cmd = "INSERT INTO SearchingCost(file_name, node_counts, edge_counts, diameter, k, n, metric_name, ratio) VALUES(@file_name, @node_counts, @edge_counts, @diameter, @k, @n, @metric_name, @ratio);"; m_sqlite_utils.RunCommnad(cmd, new List <SQLiteParameter>() { new SQLiteParameter("@file_name", m_networkTopology.FileName), new SQLiteParameter("@node_counts", m_networkTopology.Nodes.Count), new SQLiteParameter("@edge_counts", m_networkTopology.Edges.Count), new SQLiteParameter("@diameter", m_networkTopology.Diameter), new SQLiteParameter("@k", m_deployment.K), new SQLiteParameter("@n", m_deployment.N), new SQLiteParameter("@metric_name", "K-Cut Deployment - First Meet Tracer"), new SQLiteParameter("@ratio", firstMeetTracerSearchingCost) }); cmd = "INSERT INTO SearchingCost(file_name, node_counts, edge_counts, diameter, k, n, metric_name, ratio) VALUES(@file_name, @node_counts, @edge_counts, @diameter, @k, @n, @metric_name, @ratio);"; m_sqlite_utils.RunCommnad(cmd, new List <SQLiteParameter>() { new SQLiteParameter("@file_name", m_networkTopology.FileName), new SQLiteParameter("@node_counts", m_networkTopology.Nodes.Count), new SQLiteParameter("@edge_counts", m_networkTopology.Edges.Count), new SQLiteParameter("@diameter", m_networkTopology.Diameter), new SQLiteParameter("@k", m_deployment.K), new SQLiteParameter("@n", m_deployment.N), new SQLiteParameter("@metric_name", @"K-Cut Deployment - Attacker to Scope Center"), new SQLiteParameter("@ratio", srcToScopeCenterSearchingCost) }); cmd = "INSERT INTO SearchingCost(file_name, node_counts, edge_counts, diameter, k, n, metric_name, ratio) VALUES(@file_name, @node_counts, @edge_counts, @diameter, @k, @n, @metric_name, @ratio);"; m_sqlite_utils.RunCommnad(cmd, new List <SQLiteParameter>() { new SQLiteParameter("@file_name", m_networkTopology.FileName), new SQLiteParameter("@node_counts", m_networkTopology.Nodes.Count), new SQLiteParameter("@edge_counts", m_networkTopology.Edges.Count), new SQLiteParameter("@diameter", m_networkTopology.Diameter), new SQLiteParameter("@k", m_deployment.K), new SQLiteParameter("@n", m_deployment.N), new SQLiteParameter("@metric_name", @"K-Cut Deployment - The Number of Nodes in Attacker Area"), new SQLiteParameter("@ratio", attackerScopeCountSearchingCost) }); // Saving Cost cmd = "INSERT INTO SavingCost(file_name, node_counts, edge_counts, diameter, k, n, metric_name, ratio) VALUES(@file_name, @node_counts, @edge_counts, @diameter, @k, @n, @metric_name, @ratio);"; m_sqlite_utils.RunCommnad(cmd, new List <SQLiteParameter>() { new SQLiteParameter("@file_name", m_networkTopology.FileName), new SQLiteParameter("@node_counts", m_networkTopology.Nodes.Count), new SQLiteParameter("@edge_counts", m_networkTopology.Edges.Count), new SQLiteParameter("@diameter", m_networkTopology.Diameter), new SQLiteParameter("@k", m_deployment.K), new SQLiteParameter("@n", m_deployment.N), new SQLiteParameter("@metric_name", "K-Cut Deployment"), new SQLiteParameter("@ratio", savingCost) }); // Survival Malicious Traffic Ratio cmd = "INSERT INTO SurvivalMaliciousTrafficRatio(file_name, node_counts, edge_counts, diameter, k, n, metric_name, ratio) VALUES(@file_name, @node_counts, @edge_counts, @diameter, @k, @n, @metric_name, @ratio);"; m_sqlite_utils.RunCommnad(cmd, new List <SQLiteParameter>() { new SQLiteParameter("@file_name", m_networkTopology.FileName), new SQLiteParameter("@node_counts", m_networkTopology.Nodes.Count), new SQLiteParameter("@edge_counts", m_networkTopology.Edges.Count), new SQLiteParameter("@diameter", m_networkTopology.Diameter), new SQLiteParameter("@k", m_deployment.K), new SQLiteParameter("@n", m_deployment.N), new SQLiteParameter("@metric_name", "K-Cut Deployment"), new SQLiteParameter("@ratio", survivalTrafficRatio) }); }