24小时热门版块排行榜    

北京石油化工学院2026年研究生招生接收调剂公告
查看: 416  |  回复: 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的回帖
相关版块跳转 我要订阅楼主 小果冻洋 的主题更新
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 26考研调剂 +4 Wnz.20030617 2026-04-01 5/250 2026-04-02 16:11 by 1939136013狗壮
[考研] 081200-11408-276学硕求调剂 +3 崔wj 2026-04-02 3/150 2026-04-02 15:06 by cal0306
[考研] 一志愿南昌大学324求调剂 +12 hanamiko 2026-04-01 12/600 2026-04-02 14:51 by 5896
[考研] 298求调剂 +3 zzz,,r 2026-04-02 6/300 2026-04-02 14:34 by zzz,,r
[考研] 学硕化学工程与技术,一志愿中国海洋大学320+求调剂 +8 披星河 2026-04-02 8/400 2026-04-02 14:12 by oooqiao
[考研] 290求调剂 +3 1314捧花 2026-04-02 3/150 2026-04-02 13:34 by Ecowxq666!
[考研] 材料求调剂 一志愿哈工大总分298分,前三科223分 +12 dongfang59 2026-03-27 12/600 2026-04-02 12:26 by ms629
[考研] 348求调剂 +6 小懒虫不懒了 2026-03-27 7/350 2026-04-02 11:18 by sanrepian
[考研] 070305高分子化学与物理 304分求调剂 +14 c297914 2026-03-28 14/700 2026-04-02 09:35 by 星空星月
[考研] 266分求材料化工冶金矿业等专业的调剂 +7 哇呼哼呼哼 2026-03-26 7/350 2026-04-01 20:47 by cqupH
[考研] 调剂 +5 好好读书。 2026-03-28 7/350 2026-04-01 15:32 by 王亮_大连医科大
[考研] 土木304求调剂 +5 顶级擦擦 2026-03-31 5/250 2026-04-01 08:15 by fdcxdystjk¥
[考研] 材料科学与工程求调剂 +13 深V宿舍吧 2026-03-29 13/650 2026-03-31 19:50 by Dyhoer
[考研] 复试调剂 +7 双马尾痞老板2 2026-03-31 7/350 2026-03-31 19:49 by Dyhoer
[考研] 生物考研337分求调剂 +4 cgxin 2026-03-30 6/300 2026-03-31 14:18 by 记事本2026
[考研] 276求调剂 +3 赵久华 2026-03-29 3/150 2026-03-31 10:06 by cal0306
[考研] 08工科,295,接受跨专业调剂 +6 lmnlzy 2026-03-30 6/300 2026-03-31 10:04 by cal0306
[考研] 本科新能源科学与工程,一志愿华理能动285求调剂 +7 AZMK 2026-03-28 11/550 2026-03-28 21:01 by xxxsssccc
[考研] 本科新能源科学与工程,一志愿华理能动285求调剂 +3 AZMK 2026-03-27 5/250 2026-03-28 16:19 by xxxsssccc
[考研] 085602 化工专硕 338分 求调剂 +12 路痴小琪 2026-03-27 12/600 2026-03-28 15:41 by L135790
信息提示
请填处理意见