24小时热门版块排行榜    

查看: 407  |  回复: 0

小果冻洋

新虫 (初入文坛)

[求助] 用C#编写的OSPF路由协议的五种报文,以下是包头处理部分,看不懂,求高人指点!

具体内容部分如下,求高手补全
        #region 包头处理

        public bool SetPacketLength(int len)
        {
            if (len <= 24)
                return false;

            if (packet == null)
                return false;

            if (packet.Length <= 24)
                return false;

            packet[2] = (byte)(len % 256);
            packet[3] = (byte)(len >> 8);

            return true;
        }

        public bool SetRouterId(byte[] routeId)
        {
            if (routeId == null)
                return false;

            if (routeId.Length != 4)
                return false;

            if (packet == null)
                return false;

            if (packet.Length <= 24)
                return false;

            for (int i = 0; i < 4; i++)
                packet[i + 4] = routeId;
            return true;
        }

        public bool SetAreaId(byte[] areaId)
        {
            if (areaId == null)
                return false;

            if (areaId.Length != 4)
                return false;

            if (packet == null)
                return false;

            if (packet.Length <= 24)
                return false;

            for (int i = 0; i < 4; i++)
                packet[i + 8] = areaId;
            return true;
        }

        public bool checksum(byte[] packet)
        {
            if (packet == null)
                return false;

            if (packet.Length <= 24)
                return false;

            packet[12] = packet[13] = 0;
            MemoryStream memStream = new MemoryStream(packet, 0, packet.Length);
            BinaryReader br = new BinaryReader(memStream);
            Int32 sum = 0;
            UInt16 val;
            while (memStream.Position < memStream.Length - 1)
            {
                val = (UInt16)IPAddress.NetworkToHostOrder(br.ReadInt16());
                sum += val;
            }
            // if we have a remaining byte we should add it
            if (memStream.Position < packet.Length)
            {
                sum += br.ReadByte();
            }
            // fold the sum into 16 bits
            while ((sum >> 16) != 0)    //sum = (sum>>16) + (sum&0xffff);         sum += (sum>>16); 
            {
                sum = (sum & 0xffff) + (sum >> 16);
            }
            int cs = ~sum & 0xFFFF;
            packet[12] = (byte)(cs % 256);
            packet[13] = (byte)(cs >> 8);
            return true;
        }

        public bool SetPacketHeadFields()
        {
            if (!SetPacketLength(len))
                return false;
            if (!SetRouterId(routeId))
                return false;
            if (!SetAreaId(areaId))
                return false;
            if (!checksum(packet))
                return false;
            return true;
        }
回复此楼

» 猜你喜欢

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 小果冻洋 的主题更新
信息提示
请填处理意见