コード例 #1
0
ファイル: DSClass.cs プロジェクト: marvelliu/IOTResearch
 public static DSClass Combine(DSClass a, DSClass b)
 {
     if(a.length != b.length)
     {
         Console.WriteLine("DSClass a and b not equal length.");
         Console.ReadLine();
         return null;
     }
     int n = a.length;
     DSClass ds = new DSClass(n);
     ds.m[0] = 0;
     for (int i = 1; i < n; i++)
     {
         // for ds.m[i]
         double x = 0;
         double y = 0;
         for (int j = 0; j < n; j++)
         {
             for (int k = 0; k < n; k++)
             {
                 if ((j & k) == i)
                     x += a.m[j] * b.m[k];
                 if ((j & k) == 0)
                     y += a.m[j] * b.m[k];
             }
         }
         ds.m[i] = x / (1 - y);
     }
     return ds;
 }
コード例 #2
0
        public double variance = 0; //其内部的方差

        #endregion Fields

        #region Constructors

        public MODEventTrustResult(int node, int reportNodeId, string pkgIdent, MODEventCategoryType category, DSClass ds)
        {
            this.nodeId = node;
            this.reportNodeId = Global.getInstance().readers[reportNodeId].Id;
            this.ds = ds;
            this.eventIdent = pkgIdent;
            //TODO app

            confirmBeliefThrehold = new Dictionary<MODEventType, double>();
            confirmBeliefThrehold[MODEventType.DropPacketMaliciously] = 0.3;
            this.timeStamp = Scheduler.getInstance().currentTime;
        }
コード例 #3
0
ファイル: DSClass.cs プロジェクト: marvelliu/IOTResearch
        public static DSClass CombineWithWeight(DSClass a, DSClass b, double weighta, double weightb)
        {
            if (a.length != b.length)
            {
                Console.WriteLine("DSClass a and b not equal length.");
                Console.ReadLine();
                return null;
            }
            int n = a.length;
            DSClass ds = new DSClass(n);

            for (int i = 0; i < n; i++)
            {
                ds.m[pow(i)] = a.m[pow(i)] * weighta + b.m[pow(i)] * weightb;
            }
            return ds;
        }
コード例 #4
0
 public MODEventTrustCategoryResult(int node, int app, MODEventCategoryType category, DSClass ds)
 {
     this.node = node;
     this.ds = ds;
     this.category = category;
     this.categoryIdent = node + "-" + category;
     this.app = app;
     this.confirmedEventNums = new int[(int)MODEventType.COUNT];
     //TODO app
 }
コード例 #5
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;
        }
コード例 #6
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;
        }
コード例 #7
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;
        }
コード例 #8
0
 public static double getDist(DSClass ds)
 {
     double x1 = ds.b[pow(MODEventType.NotDropPacket) + pow(MODEventType.NotDropPacketButNotReceivePacket)+
         pow(MODEventType.DropPacketDueToBandwith)];
     double x2 = ds.b[pow(MODEventType.DropPacketMaliciously)];
     return Math.Abs(x1 - x2);
 }
コード例 #9
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;
        }
コード例 #10
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;
        }
コード例 #11
0
ファイル: DSClass.cs プロジェクト: marvelliu/IOTResearch
 public static DSClass CombineWithWeight(DSClass[] d, double[] weights)
 {
     int n = d[0].length;
     DSClass ds = new DSClass(n);
     for (int i = 0; i < d.Length;i++ )
     {
         DSClass ds1 = d[i];
         double w = weights[i];
         if (ds1.length != n)
             throw new Exception("ds length not equal.");
         for (int j = 0; j < ds1.length; j++)
             ds.m[j] += ds1.m[j] * w;
     }
     return ds;
 }