65 lines
1.7 KiB
C
65 lines
1.7 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* icmp.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2025/04/24 22:49:22 by tomoron #+# #+# */
|
|
/* Updated: 2025/04/30 17:53:19 by tomoron ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
#include "includes/ft_ping.h"
|
|
|
|
static void set_packet_data(uint8_t *packet_data)
|
|
{
|
|
int i;
|
|
|
|
i = 0;
|
|
while(i < PACKET_DATA_LENGTH)
|
|
{
|
|
packet_data[i] = i;
|
|
i++;
|
|
}
|
|
}
|
|
|
|
uint16_t calc_checksum(void *ptr, size_t len)
|
|
{
|
|
uint32_t res;
|
|
uint16_t *data;
|
|
|
|
data = ptr;
|
|
res = 0;
|
|
while (len > 1)
|
|
{
|
|
res += *data;
|
|
data++;
|
|
len -= 2;
|
|
}
|
|
if (len)
|
|
res += *(uint8_t *)data;
|
|
|
|
while (res >> 16) {
|
|
res = (res & 0xFFFF) + (res >> 16);
|
|
}
|
|
|
|
return(~res);
|
|
}
|
|
|
|
t_icmp_echo prepare_icmp_echo(uint16_t sequence, uint16_t identifier)
|
|
{
|
|
t_icmp_echo packet;
|
|
|
|
packet.type = 8;
|
|
packet.code = 0;
|
|
packet.identifier = htons(identifier);
|
|
packet.sequence = htons(sequence);
|
|
set_packet_data(packet.data);
|
|
gettimeofday(&packet.time, 0);
|
|
|
|
packet.checksum = 0;
|
|
packet.checksum = calc_checksum(&packet, sizeof(t_icmp_echo));
|
|
|
|
return(packet);
|
|
}
|