mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 04:27:45 +00:00
LibC+ping: Move internet_checksum to serenity header
This will be useful for traceroute and any other packet related application, so this will reduce code duplication.
This commit is contained in:
parent
ea34ba6fa6
commit
aa6547492e
3 changed files with 19 additions and 15 deletions
|
@ -24,6 +24,7 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <errno.h>
|
||||
#include <serenity.h>
|
||||
#include <string.h>
|
||||
|
@ -149,4 +150,19 @@ int getkeymap(char* name_buffer, size_t name_buffer_size, u32* map, u32* shift_m
|
|||
int rc = syscall(SC_getkeymap, ¶ms);
|
||||
__RETURN_WITH_ERRNO(rc, rc, -1);
|
||||
}
|
||||
|
||||
u16 internet_checksum(const void* ptr, size_t count)
|
||||
{
|
||||
u32 checksum = 0;
|
||||
auto* w = (const u16*)ptr;
|
||||
while (count > 1) {
|
||||
checksum += ntohs(*w++);
|
||||
if (checksum & 0x80000000)
|
||||
checksum = (checksum & 0xffff) | (checksum >> 16);
|
||||
count -= 2;
|
||||
}
|
||||
while (checksum >> 16)
|
||||
checksum = (checksum & 0xffff) + (checksum >> 16);
|
||||
return htons(~checksum);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue