How to calculate the UDP checksum? my IP address is 192.168.1.59 and the port number is 30002.
The connected host is 192.168.1.179 and the port number is 30000.
Then how does one calculate the checksum?
Reply to discussion (solution)
Cool ~~ (???) B does not understand
How does chechsum obtain the length of bytes? Do I still need the byte length? If the byte length is 66, how can we get this checksum?
This is generally done by the system level or network card. is php hard to do?
Well, php does not have much in this regard. In addition, all the verification and transmitted data are added, exclusive or, and then sum .. Or something. But I don't know what to add?
Just find a C-language code and port it.
Just find a C-language code and port it.
Is there an example or something. In the key php, do we need to convert the hexadecimal number into a binary number before we can perform an exclusive or summation?
Php provides all related functions
You can post the parts you don't know.
$ 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] x 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: 01020304
$ FileData [8] = 0x01;
$ FileData [9] = 0x02;
$ FileData [10] = 0x03;
$ FileData [11] = 0x04;
// Operation 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;
// Reserved
$ 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 of UDP packets
Mark number Operation Command operation data (result) retained
4-byte 4-byte 4-byte
So now I want to get its checksum.
The checksum I wrote is incorrect, and the output data is incorrect.
Php provides all related functions
You can post the parts you don't know.
How can I convert an IP address to a hexadecimal address? No,