示例#1
0
        //如果本节点是其邻居,那么判断没有收到所有数据包的情况
        public static MODEventTrustResult NotObservedEventTrustResult(int suspectedNode, int reportNode, string pkgIdent,
            MODEventCategoryType category, double[] speeds, bool[] isNeighbors)
        {
            //可能存在其他误差,设一个较小的值
            double b0 = 0.002, b1 = 0.002, b2 = 0.002, b3 = 0.002;

            if (!isNeighbors[0] && !isNeighbors[1])
            {
                b0 = b1 = b2 = b3 = 0.1;
            }
            else if (isNeighbors[0] && !isNeighbors[1])//prev是nb,sus不是nb
            {
                b0 = b1 = b2 = b3 = 0.1;
                if (speeds[0] > 4) //与速度有关
                {
                    b1 = Math.Min(0.3 * speeds[0] / baseSpeed, 0.6);
                    b3 = Math.Min(0.3 * speeds[0] / baseSpeed, 0.6);
                }
            }
            else if (!isNeighbors[0] && isNeighbors[1])//prev不是nb,sus是nb
            {
                b0 = b1 = b2 = b3 = 0.1;
                if (speeds[0] + speeds[2] > 4) //与速度有关
                {
                    b1 = Math.Min(0.3 * (speeds[0] + speeds[2]) / baseSpeed, 0.6);
                    b3 = Math.Min(0.3 * (speeds[0] + speeds[2]) / baseSpeed, 0.6);
                }
            }
            else//均为邻居
            {
                b0 = b1 = b2 = b3 = 0.1;
                if (speeds[0] + speeds[1] + speeds[2] > 4)
                { //与速度有关
                    b1 = Math.Min(0.3 * (speeds[0] + speeds[1] + speeds[2]) / baseSpeed, 0.6);
                    b3 = Math.Min(0.3 * (speeds[0] + speeds[1] + speeds[2]) / baseSpeed, 0.6);
                }
            }

            double total = b0 + b1 + b2 + b3 + 0.3;
            b0 = b0 / total;
            b1 = b1 / total;
            b2 = b2 / total;
            b3 = b3 / total;

            DSClass ds = new DSClass(pow(MODDropPacketEventType.END));
            ds.SetM(pow(MODDropPacketEventType.NotDropPacket), b0);
            ds.SetM(pow(MODDropPacketEventType.NotDropPacketButNotReceivePacket), b1);
            ds.SetM(pow(MODDropPacketEventType.DropPacketDueToBandwith), b2);
            ds.SetM(pow(MODDropPacketEventType.DropPacketMaliciously), b3);
            ds.SetM(pow(MODDropPacketEventType.END) - 1, 1 - b0 - b1 - b2 - b3);
            ds.Cal();

            MODEventTrustResult r = new MODEventTrustResult(suspectedNode, reportNode, pkgIdent, category, ds);
            if (ds.b[pow(MODEventType.NotDropPacket) + pow(MODEventType.NotDropPacketButNotReceivePacket)] < global.NormalBelief
                && ds.p[pow(MODEventType.NotDropPacket) + pow(MODEventType.NotDropPacketButNotReceivePacket)] < global.NormalPlausibility)//确实是攻击,恶意事件的信念大于正常事件,或恶意事件的信念大于某一个阈值
            {
                r.supportDroppingMalicious = 1;
            }
            else
                r.supportDroppingMalicious = -1;
            return r;
        }
示例#2
0
        static List<MODEventTrustResult> DeduceDropPacketMaliciously(int selfId, HashSet<MODPhenomemon> observedPhenomemons, double currentTime)
        {
            if (Global.getInstance().debug)
                Console.WriteLine("READER{0} Check DeduceDropPacketMaliciously", selfId);
            MODGlobal global = (MODGlobal)Global.getInstance();
            MODReader selfNode = (MODReader)global.readers[selfId];
            //每个节点处理该类事件的次数
            int[] eventCount = new int[global.readerNum];
            List<MODEventTrustResult> list = new List<MODEventTrustResult>();
            foreach (MODPhenomemon p in observedPhenomemons)
            {
                double a1, a2, a3, a4, a5, a6, a7, a19, a27;

                if (p.likehood <= global.SmallValue)
                    continue;
                if (p.type != MODPhenomemonType.RECV_PACKET)
                    continue;
                else if (p.pkg.Dst == p.nodeId)
                    continue;
                else if (p.pkg.Type != PacketType.DATA && p.pkg.Type != PacketType.COMMAND)
                    continue;
                else if (selfId == p.nodeId)//自己不检查自己的可信度
                    continue;
                else if (global.readers[p.nodeId].IsGateway)
                    continue;
                else if (currentTime - p.start < global.checkPhenomemonTimeout)
                    continue;

                int node = p.nodeId;
                eventCount[node]++;

                a1 = p.likehood; //likehood of receiving a packet at time p.start
                a2 = ConditionHappened(observedPhenomemons, MODPhenomemonType.SEND_PACKET, node, p.start, p.start + global.sendPacketTimeout, p.pkg);
                a3 = ConditionHappened(observedPhenomemons, MODPhenomemonType.NOT_SEND_PACKET, node, p.start, Scheduler.getInstance().currentTime, p.pkg);
                a4 = ConditionHappened(observedPhenomemons, MODPhenomemonType.BANDWIDTH_BUSY, selfId, p.start - global.checkPhenomemonTimeout, Scheduler.getInstance().currentTime);
                //如果对带宽占用没有知识,则正反都设置为未知。
                a5 = 0.9 - a4;
                a6 = Utility.Max(new double[]{
                    ConditionHappened(observedPhenomemons, MODPhenomemonType.MOVE_FAST, node, p.start - global.checkPhenomemonTimeout, Scheduler.getInstance().currentTime),
                    ConditionHappened(observedPhenomemons, MODPhenomemonType.MOVE_FAST, p.pkg.Prev, p.start - global.checkPhenomemonTimeout, Scheduler.getInstance().currentTime),
                    ConditionHappened(observedPhenomemons, MODPhenomemonType.MOVE_FAST, selfId, p.start - global.checkPhenomemonTimeout, Scheduler.getInstance().currentTime)
                });
                a7 = 0.9 - a6;
                //一个是观测节点和被观测节点的距离,看是否后者发送的消息能否被前者收到
                //另一个是,看源节点发送的数据是否能被被观测节点收到
                //a19 = Math.Max(FarDistanceLikehood(selfId, node, p.pkg.DstType==NodeType.OBJECT),
                //    FarDistanceLikehood(selfId, p.pkg.Prev, p.pkg.PrevType == NodeType.OBJECT));
                if (p.pkg.DstType == NodeType.OBJECT && selfNode.NearbyObjectCache.ContainsKey(p.pkg.Dst))
                    a19 = FarDistanceLikehood(selfId, node, true);
                else
                    a19 = FarDistanceLikehood(selfId, node, false);
                a27 = 0.9 - a19;

                //A1 AND A2 AND A7 AND A11 -> B1
                double b0 = DSClass.AND(a1, a2) * CF[(int)MODEventType.NotDropPacket];
                //A1 AND A2 AND A7 AND A11 -> B1
                double b1 = DSClass.AND(DSClass.AND(a1, a3), DSClass.OR(DSClass.OR(a4, a6), a19)) * CF[(int)MODEventType.NotDropPacketButNotReceivePacket];
                //A1 AND A2 AND A7 AND A11 -> B1
                double b2 = DSClass.AND(DSClass.AND(a1, a3), a4) * CF[(int)MODEventType.DropPacketDueToBandwith];
                //A1 AND A2 AND A7 AND A11 -> B1
                double b3 = DSClass.AND(DSClass.AND(DSClass.AND(DSClass.AND(a1, a3), a5), a7), a27) * CF[(int)MODEventType.DropPacketMaliciously];

                if (global.debug)
                {
                    Console.WriteLine("{0}->{1}:{2}", selfId, node, p.pkg.SrcSenderSeq);
                    Console.WriteLine("a1:" + a1);
                    Console.WriteLine("a2:" + a2);
                    Console.WriteLine("a3:" + a3);
                    Console.WriteLine("a4:" + a4);
                    Console.WriteLine("a5:" + a5);
                    Console.WriteLine("a6:" + a6);
                    Console.WriteLine("a7:" + a7);
                    Console.WriteLine("a19:" + a19);
                    Console.WriteLine("a27:" + a27);
                    Console.WriteLine("b0:" + b0);
                    Console.WriteLine("b1:" + b1);
                    Console.WriteLine("b2:" + b2);
                    Console.WriteLine("b3:" + b3);
                }

                DSClass ds = new DSClass(pow(MODDropPacketEventType.END));
                ds.SetM(pow(MODDropPacketEventType.NotDropPacket), b0);
                ds.SetM(pow(MODDropPacketEventType.NotDropPacketButNotReceivePacket), b1);
                ds.SetM(pow(MODDropPacketEventType.DropPacketDueToBandwith), b2);
                ds.SetM(pow(MODDropPacketEventType.DropPacketMaliciously), b3);
                ds.SetM(pow(MODDropPacketEventType.END) - 1, 1 - b0 - b1 - b2 - b3);
                ds.Cal();
                //ds.Output();
                //此处,我们先过滤一些正常事件,否则事件太多了
                if (ds.b[pow(MODEventType.NotDropPacket) + pow(MODEventType.NotDropPacketButNotReceivePacket)] < global.NormalBelief
                    && ds.p[pow(MODEventType.NotDropPacket) + pow(MODEventType.NotDropPacketButNotReceivePacket)] < global.NormalPlausibility)//确实是攻击,恶意事件的信念大于正常事件,或恶意事件的信念大于某一个阈值
                {
                    string pkgIdent = GetPacketIdent(p.pkg);
                    MODEventTrustResult r = new MODEventTrustResult(node, global.readers[selfId], pkgIdent, MODEventCategoryType.DropPacket, ds);
                    r.totalEventCount = eventCount[node];
                    r.app = p.pkg.AppId;
                    list.Add(r);
                    Console.WriteLine("{0:F4} reader{1} think reader{2} is not normal.", currentTime, selfId, p.nodeId);
                    if (global.debug)
                    {
                        r.ds.Output();
                    }
                }
            }
            return list;
        }
示例#3
0
        public static MODEventTrustResult ForgeNormalEventTrustResult(int node, int reportNode, string pkgIdent, MODEventCategoryType category)
        {
            double b0 = 0.7;
            double b1 = 0.1;
            double b2 = 0.1;
            double b3 = 0.1;

            DSClass ds = new DSClass(pow(MODDropPacketEventType.END));
            ds.SetM(pow(MODDropPacketEventType.NotDropPacket), b0);
            ds.SetM(pow(MODDropPacketEventType.NotDropPacketButNotReceivePacket), b1);
            ds.SetM(pow(MODDropPacketEventType.DropPacketDueToBandwith), b2);
            ds.SetM(pow(MODDropPacketEventType.DropPacketMaliciously), b3);
            ds.SetM(pow(MODDropPacketEventType.END) - 1, 1 - b0 - b1 - b2 - b3);
            ds.Cal();

            MODEventTrustResult result = new MODEventTrustResult(node, reportNode, pkgIdent, category, ds);
            result.supportDroppingMalicious = -1;
            return result;
        }
示例#4
0
        public static MODEventTrustResult MergeMaliciousEventTrustResult(int node, Node reportNode, List<MODEventTrustResult> reports, 
            string pkgIdent, MODEventCategoryType category)
        {
            double b0 = 0.0;
            double b1 = 0.0;
            double b2 = 0.0;
            double b3 = 0.0;

            //各个b的方差
            double x0 = 0, x1 = 0, x2 = 0, x3 = 0;

            DSClass ds = new DSClass(pow(MODDropPacketEventType.END));

            //TODO,确认下面是否是正确的
            for (int i = 0; i < reports.Count; i++)
            {
                b0 += reports[i].ds.b[pow(MODDropPacketEventType.NotDropPacket)];
                b1 += reports[i].ds.b[pow(MODDropPacketEventType.NotDropPacketButNotReceivePacket)];
                b2 += reports[i].ds.b[pow(MODDropPacketEventType.DropPacketDueToBandwith)];
                b3 += reports[i].ds.b[pow(MODDropPacketEventType.DropPacketMaliciously)];
            }
            b0 = b0 / reports.Count;
            b1 = b1 / reports.Count;
            b2 = b2 / reports.Count;
            b3 = b3 / reports.Count;

            ds.SetM(pow(MODDropPacketEventType.NotDropPacket), b0);
            ds.SetM(pow(MODDropPacketEventType.NotDropPacketButNotReceivePacket), b1);
            ds.SetM(pow(MODDropPacketEventType.DropPacketDueToBandwith), b2);
            ds.SetM(pow(MODDropPacketEventType.DropPacketMaliciously), b3);
            ds.SetM(pow(MODDropPacketEventType.END) - 1, 1 - b0 - b1 - b2 - b3);
            ds.Cal();

            //计算方差,即一致度
            //TODO,确认下面是否是正确的
            for (int i = 0; i < reports.Count; i++)
            {
                x0 += Math.Pow(b0 - reports[i].ds.b[pow(MODDropPacketEventType.NotDropPacket)], 2);
                x1 += Math.Pow(b1 - reports[i].ds.b[pow(MODDropPacketEventType.NotDropPacketButNotReceivePacket)], 2);
                x2 += Math.Pow(b2 - reports[i].ds.b[pow(MODDropPacketEventType.DropPacketDueToBandwith)], 2);
                x3 += Math.Pow(b3 - reports[i].ds.b[pow(MODDropPacketEventType.DropPacketMaliciously)], 2);
            }

            MODEventTrustResult result = new MODEventTrustResult(node, reportNode, pkgIdent, category, ds);
            result.variance = x0 + x1 + x2 + x3;

            if (result.ds.b[pow(MODEventType.NotDropPacket) + pow(MODEventType.NotDropPacketButNotReceivePacket)] < global.NormalBelief
                && result.ds.p[pow(MODEventType.NotDropPacket) + pow(MODEventType.NotDropPacketButNotReceivePacket)] < global.NormalPlausibility)//确实是攻击,恶意事件的信念大于正常事件,或恶意事件的信念大于某一个阈值
                result.supportDroppingMalicious = 1;
            else
                result.supportDroppingMalicious = -1;
            return result;
        }
示例#5
0
 //两个结果的差别
 public static double Distance(MODEventTrustResult r1, MODEventTrustResult r2)
 {
     return Math.Pow(r1.ds.b[0] - r2.ds.b[0], 2)
         + Math.Pow(r1.ds.b[1] - r2.ds.b[1], 2)
         + Math.Pow(r1.ds.b[2] - r2.ds.b[2], 2)
         + Math.Pow(r1.ds.b[3] - r2.ds.b[3], 2);
 }
示例#6
0
        public static MODEventTrustResult DeduceDropPacketMaliciouslyByPacket(int selfId, 
            HashSet<MODPhenomemon> observedPhenomemons, double currentTime, MODPhenomemon p)
        {
            double a1, a2, a3, a4, a5, a6, a7, a19, a27;
            if (Global.getInstance().debug)
                Console.WriteLine("READER{0} Check DeduceDropPacketMaliciously", selfId);
            MODGlobal global = (MODGlobal)Global.getInstance();
            MODReader selfNode = (MODReader)global.readers[selfId];

            int node = p.nodeId;

            a1 = p.likehood; //likehood of receiving a packet at time p.start
            a2 = ConditionHappened(observedPhenomemons, MODPhenomemonType.SEND_PACKET, node, p.start, p.start + global.sendPacketTimeout, p.pkg);
            a3 = ConditionHappened(observedPhenomemons, MODPhenomemonType.NOT_SEND_PACKET, node, p.start, Scheduler.getInstance().currentTime, p.pkg);
            a4 = ConditionHappened(observedPhenomemons, MODPhenomemonType.BANDWIDTH_BUSY, selfId, p.start - global.checkPhenomemonTimeout, Scheduler.getInstance().currentTime);
            //如果对带宽占用没有知识,则正反都设置为未知。
            a5 = 0.9 - a4;
            a6 = Utility.Average(new double[]{
                    ConditionHappened(observedPhenomemons, MODPhenomemonType.MOVE_FAST, node, p.start - global.checkPhenomemonTimeout, Scheduler.getInstance().currentTime),
                    ConditionHappened(observedPhenomemons, MODPhenomemonType.MOVE_FAST, p.pkg.Prev, p.start - global.checkPhenomemonTimeout, Scheduler.getInstance().currentTime),
                    ConditionHappened(observedPhenomemons, MODPhenomemonType.MOVE_FAST, selfId, p.start - global.checkPhenomemonTimeout, Scheduler.getInstance().currentTime)
                });
            a7 = 0.9 - a6;
            //一个是观测节点和被观测节点的距离,看是否后者发送的消息能否被前者收到
            //另一个是,看源节点发送的数据是否能被被观测节点收到
            //a19 = Math.Max(FarDistanceLikehood(selfId, node, p.pkg.DstType==NodeType.OBJECT),
            //    FarDistanceLikehood(selfId, p.pkg.Prev, p.pkg.PrevType == NodeType.OBJECT));

            if (p.pkg.DstType == NodeType.OBJECT && selfNode.NearbyObjectCache.ContainsKey(p.pkg.Dst))
                a19 = FarDistanceLikehood(selfId, node, true);
            else
                a19 = FarDistanceLikehood(selfId, node, false);
            a27 = 0.9 - a19;

            //A1 AND A2 AND A7 AND A11 -> B1
            double b0 = DSClass.AND(a1, a2) * CF[(int)MODEventType.NotDropPacket];
            //A1 AND A2 AND A7 AND A11 -> B1
            double b1 = DSClass.AND(DSClass.AND(a1, a3), DSClass.OR(DSClass.OR(a4, a6), a19)) * CF[(int)MODEventType.NotDropPacketButNotReceivePacket];
            //A1 AND A2 AND A7 AND A11 -> B1
            double b2 = DSClass.AND(DSClass.AND(a1, a3), a4) * CF[(int)MODEventType.DropPacketDueToBandwith];
            //A1 AND A2 AND A7 AND A11 -> B1
            double b3 = DSClass.AND(DSClass.AND(DSClass.AND(DSClass.AND(a1, a3), a5), a7), a27) * CF[(int)MODEventType.DropPacketMaliciously];

            /*
            if (global.debug)
            {
                Console.WriteLine("{0}->{1}:{2}", selfId, node, p.pkg.SrcSenderSeq);
                Console.WriteLine("a1:" + a1);
                Console.WriteLine("a2:" + a2);
                Console.WriteLine("a3:" + a3);
                Console.WriteLine("a4:" + a4);
                Console.WriteLine("a5:" + a5);
                Console.WriteLine("a6:" + a6);
                Console.WriteLine("a7:" + a7);
                Console.WriteLine("a19:" + a19);
                Console.WriteLine("a27:" + a27);
                Console.WriteLine("b0:" + b0);
                Console.WriteLine("b1:" + b1);
                Console.WriteLine("b2:" + b2);
                Console.WriteLine("b3:" + b3);
            }*/

            DSClass ds = new DSClass(pow(MODDropPacketEventType.END));
            ds.SetM(pow(MODDropPacketEventType.NotDropPacket), b0);
            ds.SetM(pow(MODDropPacketEventType.NotDropPacketButNotReceivePacket), b1);
            ds.SetM(pow(MODDropPacketEventType.DropPacketDueToBandwith), b2);
            ds.SetM(pow(MODDropPacketEventType.DropPacketMaliciously), b3);
            ds.SetM(pow(MODDropPacketEventType.END) - 1, 1 - b0 - b1 - b2 - b3);
            ds.Cal();
            //ds.Output();
            string pkgIdent = GetPacketIdent(p.pkg);
            MODEventTrustResult r = new MODEventTrustResult(node, global.readers[selfId], pkgIdent, MODEventCategoryType.DropPacket, ds);

            //此处,我们先过滤一些正常事件,否则事件太多了
            if (ds.b[pow(MODEventType.NotDropPacket) + pow(MODEventType.NotDropPacketButNotReceivePacket)] < global.NormalBelief
                && ds.p[pow(MODEventType.NotDropPacket) + pow(MODEventType.NotDropPacketButNotReceivePacket)] < global.NormalPlausibility)//确实是攻击,恶意事件的信念大于正常事件,或恶意事件的信念大于某一个阈值
            {
                Console.WriteLine("{0:F4} reader{1} think reader{2} is not normal.", currentTime, selfId, p.nodeId);
                r.supportDroppingMalicious = 1;
                /*
                if (global.debug)
                    r.ds.Output();
                 * */
            }
            else
                r.supportDroppingMalicious = -1;
            return r;
        }
示例#7
0
        //与自己预测的相关度
        public static double CalculateRelativeMaliciousEventTrustResult(MODEventTrustResult r, MODEventTrustResult myreport)
        {
            double rb = r.ds.b[pow(MODDropPacketEventType.NotDropPacket)+pow(MODDropPacketEventType.NotDropPacketButNotReceivePacket)
                +pow(MODDropPacketEventType.DropPacketDueToBandwith)];
            double mb = myreport.ds.b[pow(MODDropPacketEventType.NotDropPacket)+pow(MODDropPacketEventType.NotDropPacketButNotReceivePacket)
                +pow(MODDropPacketEventType.DropPacketDueToBandwith)];

            double b1 = (rb + mb) / 2;

            double b3 = r.ds.b[pow(MODDropPacketEventType.DropPacketMaliciously)] + myreport.ds.b[pow(MODDropPacketEventType.DropPacketMaliciously)];
            b3 = b3 / 2;

            //各个b的方差
            //正常的方差和恶意的方差:
            double x1 = Math.Pow(b1 - rb, 2) + Math.Pow(b1 - mb, 2);
            double x2 = Math.Pow(b3 - r.ds.b[pow(MODDropPacketEventType.DropPacketMaliciously)], 2) + Math.Pow(b3 - myreport.ds.b[pow(MODDropPacketEventType.DropPacketMaliciously)], 2);
            /*
            DSClass ds = new DSClass(pow(MODDropPacketEventType.END));

            ds.SetM(pow(MODDropPacketEventType.NotDropPacket), b0);
            ds.SetM(pow(MODDropPacketEventType.NotDropPacketButNotReceivePacket), b1);
            ds.SetM(pow(MODDropPacketEventType.DropPacketDueToBandwith), b2);
            ds.SetM(pow(MODDropPacketEventType.DropPacketMaliciously), b3);
            ds.SetM(pow(MODDropPacketEventType.END) - 1, 1 - b0 - b1 - b2 - b3);
            ds.Cal();*/

            //x0 = Math.Pow(b0 - r.ds.b[pow(MODDropPacketEventType.NotDropPacket)], 2) + Math.Pow(b0 - myreport.ds.b[pow(MODDropPacketEventType.NotDropPacket)], 2);
            //x1 = Math.Pow(b1 - r.ds.b[pow(MODDropPacketEventType.NotDropPacketButNotReceivePacket)], 2) + Math.Pow(b1 - myreport.ds.b[pow(MODDropPacketEventType.NotDropPacketButNotReceivePacket)], 2);
            //x2 = Math.Pow(b2 - r.ds.b[pow(MODDropPacketEventType.DropPacketDueToBandwith)], 2) + Math.Pow(b2 - myreport.ds.b[pow(MODDropPacketEventType.DropPacketDueToBandwith)], 2);

            //return x0 + x1 + x2 + x3;
            return x1 + x2;
        }
示例#8
0
        private MODEventTrustResult GetEventTrustResult(string pkgIdent, MODPhenomemon p, List<MODEventTrustResult> results, 
            int suspectedNodeId, int reportNodeId, MODEventTrustResult realr)
        {
            //0为正常行为,1为伪造异常报告,2为伪造正常报告
            bool forgeReport = false;

            MODEventTrustResult result = null;

            if (!this.IsMalicious())
                return realr;
            if (global.debug)
                Console.WriteLine("Reader{0} GetEventTrustResult", this.Id);

            bool isAccept = false;
            bool isSupportM = false;
            bool isCompositeReportSupport = false;
            //如果是恶意节点,则会考察检测节点可能的动作
            if (this.IsMalicious())
            {
                if (global.Step1DeduceMethod == DeduceMethod.Native)//原始的话,恶意节点与真实情况相反
                {
                    forgeReport = true;
                    string sSupport = "";
                    if (global.DropData == false)
                    {
                        if (realr.supportDroppingMalicious < 0)
                            sSupport = "supporting";
                        else
                            sSupport = "nonsupporting";
                    }
                    else// global.DropData == true
                    {
                        if (realr.supportDroppingMalicious > 0)
                            sSupport = "nonsupporting";
                        else
                            sSupport = "supporting";
                    }

                    Console.WriteLine("{0:F4} [{1}] {2}{3} deduces {4} {5}{6} is {7} by {8}. [{9}:{10}]\t[{11}:{12}]\t[{13}:{14}]-${15}$:{16}",
                        scheduler.currentTime, "DEDUCTION1-1", this.type, this.Id, sSupport, NodeType.READER, this.Id, "accept", this.Id,
                        0, 0, 1, 1, 1, 1, pkgIdent, "None");
                }
                else if (global.Step1DeduceMethod == DeduceMethod.Game || global.Step1DeduceMethod == DeduceMethod.OrgGame
                    || global.Step1DeduceMethod == DeduceMethod.CoOrgGame)
                {
                    //如果是博弈论,则判断检测节点的观点
                    //此处仅以其周围邻居为参考,而非报告节点的邻居,这是由于ad-hoc的局限性所致的
                    Dictionary<Node, MODEventTrustResult> localcachedresults = new Dictionary<Node, MODEventTrustResult>();
                    foreach (MODEventTrustResult r in results)
                        localcachedresults.Add(Reader.GetReader(r.reportNodeId), r);

                    ReduceReports(localcachedresults);
                    //初始化邻居的结构,且找到最久的邻居
                    int minNbId = GetLongestNormalNeighbor();
                    if (minNbId < 0)//没有正常节点
                        minNbId = GetOneNormalNodeFromReports(localcachedresults);

                    if (minNbId < 0)//还是没有正常节点
                        forgeReport = true;
                    else
                    {
                        Reader minNbNode = global.readers[minNbId];

                        MODEventTrustResult mr = null;
                        if (this.IsMalicious() && realr.supportDroppingMalicious <= 0)
                        {
                            //如果事件是正常的,那么伪造恶意事件
                            mr = MODEventTrust.ForgeMaliciousEventTrustResult(suspectedNodeId, this.Id, pkgIdent, MODEventCategoryType.DropPacket);
                            mr.supportDroppingMalicious = 1;
                        }
                        else if (this.IsMalicious() && realr.supportDroppingMalicious > 0)
                        {
                            //如果事件是恶意的,那么伪造正常事件
                            mr = MODEventTrust.ForgeNormalEventTrustResult(suspectedNodeId, this.Id, pkgIdent, MODEventCategoryType.DropPacket);
                            mr.supportDroppingMalicious = -1;
                        }
                        else
                        {
                            mr = new MODEventTrustResult(suspectedNodeId, this.Id, pkgIdent, MODEventCategoryType.DropPacket, realr.ds);
                            mr.supportDroppingMalicious = realr.supportDroppingMalicious;
                        }
                        localcachedresults.Add(this, mr);

                        AddNeighborReports(minNbNode, realr, suspectedNodeId, pkgIdent, localcachedresults);

                        Node[] reportNodes = localcachedresults.Keys.ToArray();

                        if (!this.pNBNormal.ContainsKey(minNbNode))
                            this.pNBNormal.Add(minNbNode, new Dictionary<Node, double>());
                        if (!this.NBNodeIteractions.ContainsKey(minNbNode))
                            this.NBNodeIteractions.Add(minNbNode, new List<IteratorType>());

                        if (!this.pNBNormal[minNbNode].ContainsKey(this))
                            this.pNBNormal[minNbNode].Add(this, global.pInitNormal);

                        foreach (int nbId in this.Neighbors.Keys)
                        {
                            MODReader nbNode = (MODReader)global.readers[nbId];
                            if (!this.pNBNormal[minNbNode].ContainsKey(nbNode))
                                this.pNBNormal[minNbNode].Add(nbNode, global.pInitNormal);
                        }
                        //有一些节点不是我的邻居,暂且也用nbNode表示吧
                        foreach (Node nbNode in reportNodes)
                        {
                            if (!this.pNBNormal[minNbNode].ContainsKey(nbNode))
                                this.pNBNormal[minNbNode].Add(nbNode, global.pInitNormal);
                        }

                        if (!this.NBNodeTrustWeights.ContainsKey(minNbNode))
                        {
                            this.NBNodeTrustWeights.Add(minNbNode, new Dictionary<Node, double>());
                            this.NBNodeTrustWeightsLastUpdate.Add(minNbNode, new Dictionary<Node, double>());
                            this.NBNodeSuspectCount.Add(minNbNode, new Dictionary<Node, List<double>>());
                            this.NBNodeHistoryMyVariance.Add(minNbNode, new Dictionary<Node, List<double>>());
                            this.NBNodeHistoryTotalVariance.Add(minNbNode, new Dictionary<Node, List<double>>());
                        }

                        Dictionary<Node, double> pNormal = new Dictionary<Node, double>();

                        if (global.Step1DeduceMethod == DeduceMethod.OrgGame || global.Step1DeduceMethod == DeduceMethod.CoOrgGame)
                        {
                            List<DirectTrustEntity>[] orgDirectTrustWeight = new List<DirectTrustEntity>[global.orgNum];
                            for (int i = 0; i < global.orgNum; i++)
                            {
                                orgDirectTrustWeight[i] = new List<DirectTrustEntity>();
                            }
                            Dictionary<Node, MODEventTrustResult> suspectedReportNodes =
                            AdjustNodeTrust(this.NBNodeTrustWeights[minNbNode], this.NBNodeTrustWeightsLastUpdate[minNbNode],
                                this.NBNodeSuspectCount[minNbNode], orgDirectTrustWeight,
                                this.NBNodeHistoryMyVariance[minNbNode], this.NBNodeHistoryTotalVariance[minNbNode],
                                localcachedresults, pkgIdent, minNbNode, suspectedNodeId);

                        }
                        //这个邻居节点对该节点邻居的印象
                        foreach (Node reportNode in reportNodes)
                        {
                            Organization org = global.orgs[((Reader)reportNode).OrgId];
                            if (this.NBNodeTrustWeights[minNbNode].ContainsKey(org))
                                pNormal.Add(reportNode, this.pNBNormal[minNbNode][reportNode] * this.NBNodeTrustWeights[minNbNode][org]);
                            else
                                pNormal.Add(reportNode, this.pNBNormal[minNbNode][reportNode]);
                        }

                        //计算报告节点的live时间,可估算在supportM的条件下,normal的概率
                        if (!this.NBNodeIteractions.ContainsKey(minNbNode))
                            this.NBNodeIteractions.Add(minNbNode, new List<IteratorType>());
                        double pDrop = global.pInitDrop;
                        //SetDropBySupport(ref pDropBySupportM, ref pDropByNonsupportM, this.NBNodeIteractions[minNbNode]);

                        if (global.debug)
                            Console.WriteLine("deducing reports:{0}", reportNodes.Length);
                        //然后模拟计算

                        Deduce2Result d2r = DeduceA2(reportNodes, false, suspectedNodeId, localcachedresults, minNbNode.Id,
                            pDrop, pNormal, this.NBNodeTrustWeights[minNbNode], this.NBNodeTrustWeightsLastUpdate[minNbNode],
                            this.NBNodeIteractions[minNbNode]);
                        isAccept = d2r.IsAccept;
                        isCompositeReportSupport = d2r.IsTotalReportSupport;
                        //此时,真实事件是否正常已知,自己的性质,检测节点的最佳行为已知,但是由于整体报告的性质可能出现变化,
                        //设有共有n个节点,m个正常节点,那么如果少于p-m/2个恶意节点改变自己的报告,则整体报告维持不变,否则整体报告改变。
                        isSupportM = DeduceA1(realr.supportDroppingMalicious <= 0, isCompositeReportSupport, isAccept);

                        if (realr.supportDroppingMalicious > 0 && !isSupportM)
                            forgeReport = true;
                        else if (realr.supportDroppingMalicious <= 0 && isSupportM)
                            forgeReport = true;
                        else
                            forgeReport = false;
                    }
                }
                else
                {
                    forgeReport = true;
                }

            }

            //恶意节点
            if(forgeReport == false)
            {
                Console.WriteLine("READER{0} not forge a report", this.Id);
                return realr;
            }
            else if(forgeReport == true)
            {
                if (realr.supportDroppingMalicious > 0)//真实事件是恶意的
                {
                    Console.WriteLine("READER{0} forge a normal report", this.Id);
                    result = MODEventTrust.ForgeNormalEventTrustResult(suspectedNodeId, this.Id,
                        pkgIdent, MODEventCategoryType.DropPacket);
                }
                else//真实事件是正常的
                {
                    Console.WriteLine("READER{0} forge a malicious report", this.Id);
                    result = MODEventTrust.ForgeMaliciousEventTrustResult(suspectedNodeId, this.Id,
                        pkgIdent, MODEventCategoryType.DropPacket);
                }
                return result;
            }
            return null;
        }
示例#9
0
        private void AddNeighborReports(Node minNode, MODEventTrustResult realr, int suspectedNodeId, string pkgIdent, Dictionary<Node, MODEventTrustResult> reports)
        {
            {
                MODReader nbNode = (MODReader)minNode;
                //如果已经保存,则继续
                if (!reports.ContainsKey(nbNode))
                {
                    MODEventTrustResult r = null;
                    //恶意节点
                    if (nbNode.IsMalicious() && realr.supportDroppingMalicious <= 0)
                    {
                        //如果事件是正常的,那么伪造恶意事件
                        r = MODEventTrust.ForgeMaliciousEventTrustResult(suspectedNodeId, nbNode.Id, pkgIdent, MODEventCategoryType.DropPacket);
                        r.supportDroppingMalicious = 1;
                    }
                    else if (nbNode.IsMalicious() && realr.supportDroppingMalicious > 0)
                    {
                        //如果事件是恶意的,那么伪造正常事件
                        r = MODEventTrust.ForgeNormalEventTrustResult(suspectedNodeId, nbNode.Id, pkgIdent, MODEventCategoryType.DropPacket);
                        r.supportDroppingMalicious = -1;
                    }
                    else
                    {
                        r = new MODEventTrustResult(suspectedNodeId, nbNode.Id, pkgIdent, MODEventCategoryType.DropPacket, realr.ds);
                        r.supportDroppingMalicious = realr.supportDroppingMalicious;
                    }
                    reports.Add(nbNode, r);
                }

            }
            foreach (int nbId in this.Neighbors.Keys)
            {
                if (nbId == minNode.Id)
                    continue;
                MODReader nbNode = (MODReader)global.readers[nbId];
                //如果已经保存,则继续
                if (!reports.ContainsKey(nbNode))
                {
                    if (reports.Count >= global.MaxReportCount)
                        break;
                    MODEventTrustResult r = null;
                    //恶意节点
                    if (nbNode.IsMalicious() && realr.supportDroppingMalicious <= 0)
                    {
                        //如果事件是正常的,那么伪造恶意事件
                        r = MODEventTrust.ForgeMaliciousEventTrustResult(suspectedNodeId, nbNode.Id, pkgIdent, MODEventCategoryType.DropPacket);
                        r.supportDroppingMalicious = 1;
                    }
                    else if (nbNode.IsMalicious() && realr.supportDroppingMalicious > 0)
                    {
                        //如果事件是恶意的,那么伪造正常事件
                        r = MODEventTrust.ForgeNormalEventTrustResult(suspectedNodeId, nbNode.Id, pkgIdent, MODEventCategoryType.DropPacket);
                        r.supportDroppingMalicious = -1;
                    }
                    else
                    {
                        r = new MODEventTrustResult(suspectedNodeId, nbId, pkgIdent, MODEventCategoryType.DropPacket, realr.ds);
                        r.supportDroppingMalicious = realr.supportDroppingMalicious;
                    }
                    reports.Add(nbNode, r);
                }
            }
        }