note 56061 added to ref.sockets

From: Date: Mon, 22 Aug 2005 13:22:25 +0000
Subject: note 56061 added to ref.sockets
Groups: php.notes 
Request: Send a blank email to php-notes+get-94026@lists.php.net to get a copy of this message
I've been using the ICMP Checksum calculation function written by Khaless [at] bigpond [dot] com. But when having an odd length of data, it failed, so I made my own instead, which adds a 0 if the data length is odd: <?php function icmpChecksum($data) { // Add a 0 to the end of the data, if it's an "odd length" if (strlen($data)%2) $data .= "\x00"; // Let PHP do all the dirty work $bit = unpack('n*', $data); $sum = array_sum($bit); // Stolen from: Khaless [at] bigpond [dot] com // The code from the original ping program: // sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */ // sum += (sum >> 16); /* add carry */ // which also works fine, but it seems to me that // Khaless will work on large data. while ($sum>>16) $sum = ($sum >> 16) + ($sum & 0xffff); return pack('n*', ~$sum); } ?> ---- Manual Page -- http://www.php.net/manual/en/ref.sockets.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+56061 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56061&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56061&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56061&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56061&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56061&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56061&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+56061&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#94026) next »