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:
Idan Horowitz 2021-03-30 21:18:28 +03:00 committed by Andreas Kling
parent ea34ba6fa6
commit aa6547492e
Notes: sideshowbarker 2024-07-18 20:56:21 +09:00
3 changed files with 19 additions and 15 deletions

View file

@ -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, &params);
__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);
}
}

View file

@ -109,4 +109,6 @@ int serenity_readlink(const char* path, size_t path_length, char* buffer, size_t
int getkeymap(char* name_buffer, size_t name_buffer_size, uint32_t* map, uint32_t* shift_map, uint32_t* alt_map, uint32_t* altgr_map, uint32_t* shift_altgr_map);
int setkeymap(const char* name, const uint32_t* map, uint32_t* const shift_map, const uint32_t* alt_map, const uint32_t* altgr_map, const uint32_t* shift_altgr_map);
uint16_t internet_checksum(const void* ptr, size_t count);
__END_DECLS

View file

@ -29,6 +29,7 @@
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/ip_icmp.h>
#include <serenity.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
@ -37,21 +38,6 @@
#include <time.h>
#include <unistd.h>
static uint16_t internet_checksum(const void* ptr, size_t count)
{
uint32_t checksum = 0;
auto* w = (const uint16_t*)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);
}
static int total_pings;
static int successful_pings;
static uint32_t total_ms;