note 39151 added to ref.sockets
| From: | Khaless at rn2 dot php dot net | Date: | Mon, 19 Jan 2004 02:55:34 +0000 |
| Subject: | note 39151 added to ref.sockets | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-63808@lists.php.net to get a copy of this message | ||
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.
----
Manual Page -- http://www.php.net/manual/en/ref.sockets.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+39151
Delete -- http://master.php.net/manage/user-notes.php?action=delete+39151&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+39151&report=yes
Search -- http://master.php.net/manage/user-notes.php