The calculation method of UDP checksum

Source: Internet
Author: User
How to find the checksum of UDP, my IP is the 192.168.1.59 port number is 30002
The connected machine is the 192.168.1.179 port number is 30000

And then how do you find the checksum?


Reply to discussion (solution)

Goo ~ (???) B Don't understand

is chechsum how to get to, also have byte length? Do I need a byte length? Byte length If it is 66 then how to get to this checksum?

This is usually a system-level or network card to do, PHP is very difficult to do it?

Well, PHP is not a lot of this, and this checksum and transmission of data, all of the sum, and different or, and then summed. What But you don't know what to add?

Find a C code to transplant.

Find a C code to transplant.
There are no examples of what ah. Key PHP 16 in the number, is to first turn to binary, in order to make a different or sum?

PHP provides all the relevant functions
You put the part you don't have.

$FileData =array ();

8 byte header
$FileData [0]=0x75;
$FileData [1]=0x30;
$FileData [2]=0x75;
$FileData [3]=0x32;
$FileData [4]=0x00;
$FileData [5]=0x20;

------UDP Header Checksum




$a = $FileData [0]*256+ $FileData [1];
$b = $FileData [2]*256+ $FileData [3];
$c = $FileData [4]*256+ $FileData [5];
$d = ($a + $b + $c);

$FileData [6]=~ (($d >>8) &0xff);
$FileData [7]=~ ($d &0xff);
//---------------------

16 bytes of data
ID Number: 01020304
$FileData [8]=0x01;
$FileData [9]=0x02;
$FileData [10]=0x03;
$FileData [11]=0x04;

Action command: 0x00000001
$FileData [12]=0x00;
$FileData [13]=0x00;
$FileData [14]=0x00;
$FileData [15]=0x02;

Operation Result: 0x00000000
$FileData [16]=0x00;
$FileData [17]=0x00;
$FileData [18]=0x00;
$FileData [19]=0x01;

Keep
$FileData [20]=0x00;
$FileData [21]=0x00;
$FileData [22]=0x00;
$FileData [23]=0x00;

//---------------------
8-byte checksum
$FileData [24]= $FileData [0]^ $FileData [8]^ $FileData [16];
$FileData [25]= $FileData [1]^ $FileData [9]^ $FileData [17];
$FileData [26]= $FileData [2]^ $FileData [10]^ $FileData [18];
$FileData [27]= $FileData [3]^ $FileData [11]^ $FileData [19];
$FileData [28]= $FileData [4]^ $FileData [12]^ $FileData [20];
$FileData [29]= $FileData [5]^ $FileData [13]^ $FileData [21];
$FileData [30]= $FileData [6]^ $FileData [14]^ $FileData [22];
$FileData [31]= $FileData [7]^ $FileData [15]^ $FileData [23];

$arrayLen = sizeof ($FileData);
foreach ($FileData as $arr)
{
$str. = $arr;
}

Echo $str;
$str should be 16 bytes of data
Data content definition for UDP messages
Identification number Operation command operation data (result) retention
4 bytes 4 bytes 4 bytes 4 bytes


So now I want to get its checksum.

The checksum I wrote above is incorrect, and the output data is not correct.

PHP provides all the relevant functions
You put the part you don't have.

And how does IP turn into 16 binary? This is not the way,

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.