青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

小默

【轉】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 小默 閱讀(739) 評論(0)  編輯 收藏 引用 所屬分類: Network

導航

統計

留言簿(13)

隨筆分類(287)

隨筆檔案(289)

漏洞

搜索

積分與排名

最新評論

閱讀排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            国产在线欧美日韩| 亚洲国产精品电影在线观看| 亚洲精品久久| 亚洲大片在线观看| 悠悠资源网久久精品| 在线观看国产欧美| 亚洲成人在线网站| 亚洲日本一区二区三区| 亚洲精品国产精品乱码不99按摩| 亚洲日韩欧美视频一区| 欧美亚洲视频在线观看| 亚洲二区视频在线| 亚洲精品视频免费| 一本久久综合亚洲鲁鲁五月天| 日韩视频免费在线| 亚洲专区一二三| 久久av红桃一区二区小说| 久久婷婷影院| 欧美日韩国产综合久久| 国产精品性做久久久久久| 国产亚洲一区二区三区在线观看| 国产一区二区三区在线播放免费观看| 狠狠色狠狠色综合| 999亚洲国产精| 性一交一乱一区二区洋洋av| 久久午夜色播影院免费高清| 欧美激情一区二区三区在线视频观看 | 久久gogo国模啪啪人体图| 久久久久久电影| 欧美激情日韩| 亚洲视频在线看| 久久久久久久一区二区三区| 欧美国产视频日韩| 国产欧美一区二区三区在线看蜜臀 | 欧美午夜寂寞影院| 国产一区二区电影在线观看| 亚洲国产一区二区三区高清| 亚洲素人在线| 麻豆国产va免费精品高清在线| 91久久国产精品91久久性色| 亚洲一区日韩| 免费在线国产精品| 国产精品主播| 亚洲人成在线观看网站高清| 亚洲欧美资源在线| 亚洲第一在线综合在线| 亚洲一区二区三区四区中文| 久久综合中文| 国产精品午夜国产小视频| 亚洲国内自拍| 久久福利精品| 亚洲精选成人| 久久频这里精品99香蕉| 国产精品久久久久久久app| 亚洲丶国产丶欧美一区二区三区| 亚洲一区在线看| 亚洲国产高清视频| 欧美自拍偷拍午夜视频| 欧美视频在线一区二区三区| 一区二区三区在线看| 亚洲欧美国产精品桃花| 亚洲国产成人av| 久久精品亚洲一区二区| 国产精品嫩草99a| 亚洲精品国精品久久99热一| 久久精品在线观看| 亚洲无线一线二线三线区别av| 欧美18av| 伊人久久久大香线蕉综合直播 | 欧美aⅴ99久久黑人专区| 国产偷自视频区视频一区二区| 一本色道久久综合| 欧美暴力喷水在线| 欧美一区二区黄| 国产精品久久久久久av下载红粉 | 亚洲片在线资源| 久久夜色撩人精品| 国产一区二区三区四区三区四| 午夜精品久久久久久| 亚洲精品中文字幕女同| 欧美3dxxxxhd| 91久久久久久久久| 在线观看欧美亚洲| 亚洲精品日本| 欧美成人一区二区三区在线观看| 先锋影音久久| 国产精品一区二区在线观看网站| 99热免费精品在线观看| 亚洲高清三级视频| 免费不卡在线观看| 亚洲黄色一区二区三区| 蜜桃伊人久久| 久久亚洲欧美| 在线国产日韩| 女仆av观看一区| 久久婷婷国产麻豆91天堂| 国产在线一区二区三区四区| 久久国产精品久久久久久电车| 亚洲一区三区电影在线观看| 国产精品a久久久久| 亚洲免费在线观看视频| 亚洲天堂av在线免费观看| 国产精品国产三级国产专播品爱网| 中文日韩欧美| 国产精品99久久久久久久vr| 欧美视频成人| 亚洲欧美日韩国产| 亚洲一区二区在线观看视频| 国产精品任我爽爆在线播放| 午夜精品三级视频福利| 亚洲欧美在线免费观看| 国产一区视频观看| 裸体女人亚洲精品一区| 久久综合五月| 日韩视频在线观看免费| 日韩亚洲一区二区| 国产精品国产三级国产专播精品人 | 欧美性片在线观看| 亚洲中字黄色| 午夜久久99| 黄色小说综合网站| 欧美大秀在线观看| 欧美精品在欧美一区二区少妇| 一区二区三区www| 亚洲午夜精品久久久久久浪潮| 国产免费观看久久| 麻豆精品在线观看| 欧美不卡视频一区发布| 在线亚洲+欧美+日本专区| 亚洲视频每日更新| 黑人一区二区三区四区五区| 欧美成人综合网站| 欧美日韩日韩| 欧美中文日韩| 免费h精品视频在线播放| 中日韩美女免费视频网站在线观看| aa日韩免费精品视频一| 国产欧美日韩激情| 欧美大片第1页| 欧美色道久久88综合亚洲精品| 久久国产精品99国产精| 久久久久久久久一区二区| 日韩视频一区二区| 亚洲欧美日韩精品一区二区| 亚洲高清视频的网址| 99精品福利视频| 国模大胆一区二区三区| 欧美激情一区二区三区全黄| 国产精品av免费在线观看| 久久亚洲不卡| 欧美日韩免费一区| 麻豆成人91精品二区三区| 欧美日韩精品一本二本三本| 久久精品99| 欧美男人的天堂| 久久久青草婷婷精品综合日韩 | 99精品国产福利在线观看免费| 亚洲性人人天天夜夜摸| 亚洲成色www8888| 这里只有精品在线播放| 在线日本欧美| 亚洲影视在线| 亚洲精品国产精品国自产在线| 亚洲欧美不卡| av成人免费| 久久久久国产精品人| 亚洲一区二区三区视频播放| 久久久久久久综合狠狠综合| 亚洲网站在线看| 媚黑女一区二区| 久久国产日韩| 欧美网站大全在线观看| 欧美freesex8一10精品| 国产毛片一区| 9国产精品视频| 亚洲人www| 久久精品一区蜜桃臀影院| 亚洲天堂成人| 欧美高清你懂得| 久久影视三级福利片| 欧美中文字幕| 男同欧美伦乱| 久久一区欧美| 很黄很黄激情成人| 久久精品成人一区二区三区| 欧美在线一二三四区| 国产欧美日韩另类视频免费观看| 亚洲视频高清| 欧美在线欧美在线| 国产一区日韩二区欧美三区| 亚洲男女自偷自拍图片另类| 久久er精品视频| 在线精品福利| 欧美日韩岛国| 亚洲在线视频| 免费日韩成人| 99亚洲一区二区| 国产欧美在线观看一区| 久久久久九九九九| 亚洲人精品午夜|