• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            小默

            【轉(zhuǎn)】IP checksum definition

            IP checksum definition

            The IP checksum is the 16 bit one's complement of the one's complement sum of all 16 bit words in the header.

            One question many people may ask is "What is the 1's complement sum ?". This is because all computers utilize the 2's complement representation and the 1's complement is not used. The following gives a short introduction.

            2's complement fixed point integers (8-bit)

            BinaryDecimalHex
            0000 0000000
            0000 0001101
            0000 0010202
            0000 0011303
            1111 1111-1FF
            1111 1110-2FE
            1111 1101-3FD

            Let's add two intergers: 
            -3 + 5 = 2 
            FD + 05 = 01 02
            Discarding the carry (01) gives the correct result.

            1's complement fixed point integers (8-bit)

            BinaryDecimalHex
            0000 0000000
            0000 0001101
            0000 0010202
            0000 0011303
            1111 1111-0FF
            1111 1110-1FE
            1111 1101-2FD
            1111 1100-3FC

            Add the same numbers: 
            -3 + 5 = 2
            FC + 05 = 01 01
            Adding the carry (01) to the LSB (01) gives the correct result:
            01 + 01 = 02

            So, the 1's complement sum is done by summing the numbers and adding the carry (or carries) to the result..

            Simple Internet checksum example

            Suppose we have an 8-bit, 2's complement, machine and send the packet

            FE 05 00

            where 00 is the checksum field.

            Let's calculate and verify the Internet checksum.

             FE + 05  =  01 03

            This is the result of the normal (2's complement) addition. The 1's complement sum requires the addition of the carry to the 8-bit word (even though we will not get the same result)

             03 + 01 = 04  

            so the 1's complement sum of FE + 05 is 04.

            The 1's complement of the 1's complement sum (Internet checksum) will be 

            ~04  = FB

            and the packet will be sent as

            FE 05 FB 

            Now, at the receiving end we add all the received bytes, including the checksum (again using the 2's complement representation)

            FE + 05 + FB  = 01 FE  

            The 1's complement sum is  

             FE + 01 = FF = -0  

            which checks that the transmission was OK (see below).

            A more complex example (32-bit machine)

            As shown in RFC 1071, the checksum calculation is done in the following way:

            (1) Adjacent octets to be checksummed are paired to form 16-bit integers, and the 1's complement sum of these 16-bit integers is formed.

            (2) To generate a checksum, the checksum field itself is cleared, the 16-bit 1's complement sum is computed over the octets concerned, and the 1's complement of this sum is placed in the checksum field.

            (3) To check a checksum, the 1's complement sum is computed over the same set of octets, including the checksum field. If the result is all 1 bits (-0 in 1's complement arithmetic), the check succeeds.

            Packet

            01 00 F2 03 F4 F5 F6 F7 00 00

            (00 00 is the checksum field)

            Form the 16-bit words

            0100 F203 F4F5 F6F7

            Calculate 2's complement sum

            0100 + F203 + F4F5 + F6F7 = 0002 DEEF (store the sum in a 32-bit word)

            Add the carries (0002) to get the 16-bit 1's complement sum

            DEEF + 002 = DEF1

            Calculate 1's complement of the 1's complement sum

            ~DEF1 = 210E

            We send the packet including the checksum 21 0E

            01 00 F2 03 F4 F5 F6 F7 21 0E

            At the receiving

            0100 + F203 + F4F5 + F6F7 + 210E = 0002 FFFD
            FFFD + 0002 = FFFF

            which checks OK.

            Comments
            It may look awkword to use a 1's complement addition on 2's complement machines. This method however has its own benefits.

            Probably the most important is that it is endian independent. Little Endian computers store hex numbers with the LSB last (Intel processors for example). Big Endian computers put the LSB first (IBM mainframes for example). When carry is added to the LSB to form the 1's complement sum (see the example) it doesn't matter if we add 03 + 01 or 01 + 03. The result is the same.

            Other benefits include the easiness of checking the transmission and the checksum calculation plus a variety of ways to speed up the calculation by updating only IP fields that have changed.


            The IP Header Checksum is computed on the header fields only. 
            Before starting the calculation, the checksum fields (octets 11 and 12) 
            are made equal to zero. 

            In the example code, 
            u16 buff[] is an array containing all octets in the header with octets 11 and 12 equal to zero. 
            u16 len_ip_header is the length (number of octets) of the header.


            /*
            **************************************************************************
            Function: ip_sum_calc
            Description: Calculate the 16 bit IP sum.
            ***************************************************************************
            */
            typedef unsigned short u16;
            typedef unsigned long u32;

            u16 ip_sum_calc(u16 len_ip_header, u16 buff[])
            {
                    u16 word16;
                    u32 sum=0;
                    u16 i;
                
                    // make 16 bit words out of every two adjacent 8 bit words in the packet
                    // and add them up
                    for (i=0;i<len_ip_header;i=i+2){
                            word16 =((buff[i]<<8)&0xFF00)+(buff[i+1]&0xFF);
                            sum = sum + (u32) word16;
                    }

                    // take only 16 bits out of the 32 bit sum and add up the carries
                    while (sum>>16)
                            sum = (sum & 0xFFFF)+(sum >> 16);

                    // one's complement the result
                    sum = ~sum;

                    return ((u16) sum);
            }

            posted on 2010-10-20 17:05 小默 閱讀(686) 評(píng)論(0)  編輯 收藏 引用 所屬分類: Network

            導(dǎo)航

            統(tǒng)計(jì)

            留言簿(13)

            隨筆分類(287)

            隨筆檔案(289)

            漏洞

            搜索

            積分與排名

            最新評(píng)論

            閱讀排行榜

            久久福利青草精品资源站| 日本欧美久久久久免费播放网| 欧美精品国产综合久久| 日韩亚洲欧美久久久www综合网| 无码国内精品久久人妻蜜桃| 热综合一本伊人久久精品| 久久影视综合亚洲| 日日狠狠久久偷偷色综合免费| 久久久久人妻精品一区三寸蜜桃| 国内精品久久久久国产盗摄| 国产精品99久久久久久董美香| 久久99精品久久久久久齐齐| 久久精品国产亚洲5555| 色天使久久综合网天天| 久久人人爽人人爽人人av东京热 | 亚洲国产精品婷婷久久| 狠狠色丁香婷综合久久| 亚洲乱亚洲乱淫久久| 久久青青草原精品国产不卡| 狠狠色丁香婷婷久久综合五月| AV无码久久久久不卡蜜桃| 91视频国产91久久久| 久久久久99精品成人片牛牛影视 | 久久久久久国产a免费观看不卡 | 国产精品久久久久jk制服| 久久99久久99小草精品免视看| 国产女人aaa级久久久级| 久久婷婷国产剧情内射白浆| 国产综合久久久久| 精品久久久久久99人妻| 亚洲狠狠婷婷综合久久蜜芽| 国产精品一久久香蕉国产线看| 久久精品国产精品亚洲人人| 国产偷久久久精品专区 | 国产一区二区三区久久精品| 久久久精品久久久久久 | 久久久久亚洲精品男人的天堂| 亚洲中文字幕无码一久久区| 精品久久久久久国产牛牛app| 久久久久久国产精品美女| 亚洲天堂久久精品|