IP首部校正和計算

來源:互聯網
上載者:User

標籤:ret   方法   span   寫代碼   bsp   編寫   執行個體   sum   head   

根據RFC1071文檔的計算方法,編寫代碼實現IP首部校正和的計算

 

計算步驟:

1.首先將IP首部中校正和欄位置0

2.將IP首部每16bit進行相加,如果有進位產生,則將進位加到最低位。

3.將計算的結果取反即可

 

 1 unsigned short checksum(int count,unsigned short* addr) 2 { 3     long sum = 0; 4  5     while(count > 1) 6     { 7         sum   +=*addr++; 8         count -= sizeof(unsigned short); 9     }10 11     if(count > 0)12     {13         sum  +=*(unsigned char*)addr;14     }15 16     while(sum >> 16)17     {18         sum = (sum & 0xFFFF) + (sum >> 16);19     }20     21     return (unsigned short)(~sum);22 }23 int main(int argc,char* argv[])24 {25     unsigned char IpHeader[] = "\xa1\x00\x00\x45\x00\x00\x21\x2c\x00\x00\x11\x01\x2f\x0a\x1e\xac\xfa\xff\xff\xef"; //抓包執行個體  檢驗和是0xe6e326 27     printf("0x%0x\n",checksum(sizeof(IpHeader)-1,(unsigned short *)IpHeader));28   29     return 0;30 }

 

IP首部校正和計算

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.