PHP 8.5.0 Alpha 4 available for testing

Voting

: three plus five?
(Example: nine)

The Note You're Voting On

Khaless [at] bigpond [dot] com
21 years ago
I spent a while trying to use SOCK_RAW to send ICMP request packets so i could ping. This however lead me to need the internet checksum written as a php function, which was a little hard because of the way PHP handles variable types. Anyway, to save others the effort heres what i came up with, this returns Checksum for $data

<?PHP
// Computes Internet Checksum for $data
// will return a 16-bit internet checksum for $data
function inetChecksum($data)
{
// 32-bit accumilator, 16 bits at a time, adds odd bit on at end
for($i=0;$i<strlen($data);$i += 2)
{
if(
$data[$i+1]) $bits = unpack('n*',$data[$i].$data[$i+1]);
else
$bits = unpack('C*',$data[$i]);
$sum += $bits[1];
}

// Fold 32-bit sum to 16 bits
while ($sum>>16) $sum = ($sum & 0xffff) + ($sum >> 16);
$checksum = pack('n1',~$sum);
return
$checksum;
}
?>

And with this i was able to construct a correct PING Request.

<< Back to user notes page

To Top