public FilteringEvent(PacketEvent packetEvent)
 {
     this.PacketID    = packetEvent.PacketID;
     this.Time        = packetEvent.Time;
     this.Source      = packetEvent.Source;
     this.Destination = packetEvent.Destination;
     this.Type        = packetEvent.Type;
 }
 public void InsertPacketEvent(PacketEvent packetEvent)
 {
     try
     {
         using (SQLiteConnection connection = new SQLiteConnection(connectionString))
         {
             connection.Open();
             SQLiteTransaction trans = connection.BeginTransaction();
             SQLiteCommand     cmd   = connection.CreateCommand();
             cmd.CommandText = string.Format("INSERT INTO {0}_PacketEvent(PacketID, Source, Destination, Type) VALUES(@PacketID, @Source, @Destination, @Type)", prefixNameOfTable);
             cmd.Parameters.Add("@PacketID", System.Data.DbType.Int32).Value    = packetEvent.PacketID;
             cmd.Parameters.Add("@Source", System.Data.DbType.Int32).Value      = packetEvent.Source;
             cmd.Parameters.Add("@Destination", System.Data.DbType.Int32).Value = packetEvent.Destination;
             cmd.Parameters.Add("@Type", System.Data.DbType.Boolean).Value      = packetEvent.Type == Network_Simulation.NetworkTopology.NodeType.Attacker ? true : false;
             cmd.ExecuteNonQuery();
             trans.Commit();
         }
     }
     catch { }
 }
Esempio n. 3
0
        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);
        }