簡體   English   中英

ospf校驗和是如何計算的?

[英]How is the ospf checksum calculated?

我無法計算出准確的校驗和。 我的路由器因此拒絕了我的 Hello 消息。 這是十六進制的包(從 ospf 頭開始)

                                           vv vv <-- my program's checksum
0000   02 01 00 30 c0 a8 03 0a 00 00 00 00 f3 84 00 00  ...0............
0010   00 00 00 00 00 00 00 00 ff ff ff 00 00 0a 00 01  ................
0020   00 00 00 28 c0 a8 03 0a c0 a8 03 0a              ...(........

Wireshark 調用0xf384假並說預期值為0xb382 我已經確認我的算法以正確的順序成對選擇字節,然后將它們加在一起:

0201 + 0030 + c0a8 + ... + 030a

無需擔心身份驗證。 我們甚至沒有被教如何設置它。 最后,這是我計算校驗和的嘗試:

OspfHeader result;

result.Type = type;
result.VersionNumber = 0x02;
result.PacketLength = htons(24 + content_len);
result.AuthType = htons(auth_type);
result.AuthValue = auth_val;
result.AreaId = area_id;
result.RouterId = router_id;

uint16_t header_buffer[12];
memcpy(header_buffer, &result, 24);

uint32_t checksum;
for(int i = 0; i < 8; i++)
{
    checksum += ntohs(header_buffer[i]);
    checksum = (checksum & 0xFFFF) + (checksum >> 16);
}

for(int i = 0; i + 1 < content_len; i += 2)
{
    checksum += (content_buffer[i] << 8) + content_buffer[i+1];
    checksum = (checksum & 0xFFF) + (checksum >> 16);
}

result.Checksum = htons(checksum xor 0xFFFF);

return result;

我不確定我到底在哪里搞砸了。 有什么線索嗎?

Mac_3.2.57$cat ospfCheckSum2.c
#include <stdio.h>

int main(void){

    // array was wrong len
    unsigned short header_buffer[22] = {
        0x0201, 0x0030, 0xc0a8, 0x030a,
        0x0000, 0x0000, 0x0000, 0x0000,
        0x0000, 0x0000, 0x0000, 0x0000,
        0xffff, 0xff00, 0x000a, 0x0001,
        0x0000, 0x0028, 0xc0a8, 0x030a,
        0xc0a8, 0x030a
    };
    // note ospf len is also wrong, BTW
    
    // was not init'd
    unsigned int checksum = 0;

    // was only scanning 8 vs 22
    for(int i = 0; i < 22; i++)
    {
        checksum += header_buffer[i];
        checksum = (checksum & 0xFFFF) + (checksum >> 16);
    }
    
    // len not odd, so this is not needed
    //for(int i = 0; i + 1 < content_len; i += 2)
    //{
    //    checksum += (content_buffer[i] << 8) + content_buffer[i+1];
    //    checksum = (checksum & 0xFFF) + (checksum >> 16);
    //}
    
    printf("result is %04x\n", checksum ^ 0xFFFF); // no "xor" for me (why not do ~ instead?)

    // also: where/what is: content_len, content_buffer, result, OspfHeader 

    return(0);
}
Mac_3.2.57$cc ospfCheckSum2.c
Mac_3.2.57$./a.out 
result is b382
Mac_3.2.57$

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM