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

小默

【轉(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 小默 閱讀(750) 評(píng)論(0)  編輯 收藏 引用 所屬分類(lèi): Network

導(dǎo)航

統(tǒng)計(jì)

留言簿(13)

隨筆分類(lèi)(287)

隨筆檔案(289)

漏洞

搜索

積分與排名

最新評(píng)論

閱讀排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            欧美一区三区二区在线观看| 一区二区三区日韩精品视频| 国产精品mv在线观看| 久久免费国产| 国产精品你懂的在线欣赏| 亚洲第一视频| 国产一区二区视频在线观看 | 牛牛精品成人免费视频| 国产精品人成在线观看免费| 亚洲国内精品| 亚洲激情综合| 久久人人看视频| 久久蜜桃精品| 国产亚洲永久域名| 亚洲欧美另类在线| 午夜精彩国产免费不卡不顿大片| 欧美女同在线视频| 亚洲高清资源| 亚洲精选中文字幕| 欧美大片在线观看一区| 免费久久精品视频| 一区免费观看| 久久全球大尺度高清视频| 久久久久一区二区三区四区| 国产一区欧美日韩| 欧美一区二区三区男人的天堂 | 免费欧美在线| 亚洲第一视频| 美女91精品| 欧美国产精品劲爆| 亚洲精品日韩在线观看| 每日更新成人在线视频| 亚洲人成人一区二区在线观看| 亚洲三级电影全部在线观看高清| 免费欧美日韩国产三级电影| 欧美国产精品一区| 亚洲激情午夜| 欧美日本国产视频| 亚洲永久在线| 久久亚洲二区| 亚洲伦理在线| 欧美三级在线| 西西裸体人体做爰大胆久久久| 久久久久亚洲综合| 影音先锋亚洲精品| 欧美激情小视频| 制服丝袜激情欧洲亚洲| 欧美在线三区| 亚洲成人在线视频播放| 欧美精品在线一区| 99精品视频免费在线观看| 性久久久久久| 亚洲国产99| 国产精品伦一区| 久久久蜜桃精品| 亚洲人体1000| 久久精品理论片| 亚洲精品九九| 国产欧美日韩三级| 久久综合99re88久久爱| 日韩一级网站| 久久亚洲精品一区| 亚洲天堂av综合网| 伊人久久男人天堂| 欧美视频一区二区在线观看 | 欧美色视频在线| 欧美在线日韩| 一本久道久久综合中文字幕| 久久久久99精品国产片| 一本大道久久a久久综合婷婷| 国产伦精品一区二区三区在线观看 | 亚洲性夜色噜噜噜7777| 男女激情久久| 欧美一区二区三区啪啪| 亚洲精品美女在线观看| 国产午夜精品一区二区三区欧美 | 欧美激情一区二区三区高清视频| 亚洲一区二区在线| 亚洲黄色精品| 国产自产高清不卡| 欧美午夜精品久久久久久浪潮| 久久久欧美一区二区| 亚洲一区二区高清视频| 亚洲国产中文字幕在线观看| 久久久久国产一区二区| 亚洲欧美日韩在线| 99成人免费视频| 亚洲电影免费观看高清| 国产日韩精品入口| 欧美调教视频| 欧美精品偷拍| 免费在线观看成人av| 久久国产精品高清| 亚洲欧美日韩一区二区三区在线 | 亚洲女人av| 一本大道久久a久久精二百| 欧美激情在线有限公司| 美国十次成人| 美日韩精品免费| 久久久久女教师免费一区| 性做久久久久久免费观看欧美| 中文在线一区| 亚洲视频在线视频| 亚洲婷婷在线| 亚洲一区二区三区在线| 一区二区三区免费网站| 99视频精品全国免费| 一本久道久久综合狠狠爱| 99国产精品| 一区二区日韩精品| 亚洲网站在线观看| 亚洲女女做受ⅹxx高潮| 午夜精品久久久久久久| 久久精彩免费视频| 久久久久久久成人| 蜜桃av一区| 亚洲国产精品v| 亚洲精品免费电影| 一区二区三区回区在观看免费视频| 99精品欧美一区二区蜜桃免费| 一区二区三区日韩精品| 亚洲男人的天堂在线观看 | 亚洲欧美成人一区二区三区| 亚洲一区二区不卡免费| 欧美在线视频a| 麻豆91精品| 欧美精品在线观看播放| 国产精品福利在线| 国产麻豆精品theporn| 黑人极品videos精品欧美裸| 亚洲国产第一| 一区二区三区高清| 欧美一区亚洲| 欧美成人小视频| 日韩视频第一页| 久久爱另类一区二区小说| 免费久久精品视频| 欧美视频一区二区三区| 国产在线拍偷自揄拍精品| 亚洲国产91| 午夜精品福利电影| 男同欧美伦乱| 一区二区高清视频在线观看| 欧美一区二区三区精品| 欧美成人有码| 国产日韩av高清| 日韩一二三区视频| 欧美一区在线视频| 亚洲国产日韩一级| 性18欧美另类| 欧美精品三级在线观看| 国产婷婷色一区二区三区四区| 亚洲福利久久| 久久成人国产| 亚洲三级国产| 久久久久国产精品午夜一区| 欧美日韩在线一区二区三区| 黑人巨大精品欧美一区二区小视频 | 亚洲精品中文字| 久久久久女教师免费一区| 99re66热这里只有精品4| 久久亚洲影院| 国产视频久久久久| 正在播放欧美视频| 欧美暴力喷水在线| 性做久久久久久久久| 欧美人成在线视频| 亚洲福利专区| 久久久久久久综合日本| 一区二区三区久久| 欧美激情视频一区二区三区在线播放| 国产亚洲人成网站在线观看| 亚洲视频在线观看视频| 亚洲高清毛片| 麻豆91精品91久久久的内涵| 国产日韩视频| 亚洲欧美在线一区二区| 亚洲美女福利视频网站| 欧美+亚洲+精品+三区| 黄色成人免费网站| 欧美一区二区三区在线看| 在线视频免费在线观看一区二区| 欧美1区免费| 亚洲激情六月丁香| 欧美成人在线免费视频| 久久久久久久性| 韩日成人av| 久久婷婷影院| 久久国产毛片| 一区二区在线视频| 美女露胸一区二区三区| 久久精品中文字幕一区| 国产一区二区三区的电影 | 亚洲精品一区二区在线观看| 免播放器亚洲| 久久只精品国产| 亚洲精品在线观| 亚洲区中文字幕| 欧美天堂亚洲电影院在线观看| 中文国产亚洲喷潮|