add icmp_echo packet preparation
This commit is contained in:
2
Makefile
2
Makefile
@ -1,6 +1,6 @@
|
|||||||
NAME = ft_ping
|
NAME = ft_ping
|
||||||
CC=cc
|
CC=cc
|
||||||
FLAGS=-Werror -Wextra -Wall
|
FLAGS=-Werror -Wextra -Wall -g
|
||||||
|
|
||||||
SRCS_DIR = srcs
|
SRCS_DIR = srcs
|
||||||
OBJS_DIR = .objs
|
OBJS_DIR = .objs
|
||||||
|
@ -1,7 +1,87 @@
|
|||||||
#include "includes/ft_ping.h"
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_ping.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/04/24 00:03:56 by tomoron #+# #+# */
|
||||||
|
/* Updated: 2025/04/24 22:45:16 by tomoron ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
int main(void)
|
#include "includes/ft_ping.h"
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <byteswap.h>
|
||||||
|
|
||||||
|
void set_packet_data(uint8_t *packet_data)
|
||||||
{
|
{
|
||||||
printf("AAAAAAAAAAAAAAAAAAA");
|
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(htons(~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.time.tv_sec = bswap_64(packet.time.tv_sec);
|
||||||
|
packet.time.tv_usec = bswap_64(packet.time.tv_usec);
|
||||||
|
|
||||||
|
packet.checksum = 0;
|
||||||
|
packet.checksum = calc_checksum(&packet, sizeof(t_icmp_echo));
|
||||||
|
|
||||||
|
return(packet);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
(void)argc;
|
||||||
|
(void)argv;
|
||||||
|
t_icmp_echo packet;
|
||||||
|
int fd;
|
||||||
|
|
||||||
|
packet = prepare_icmp_echo(0, 1);
|
||||||
|
fd = open("test", O_WRONLY | O_CREAT, 0700);
|
||||||
|
write(fd, &packet, sizeof(t_icmp_echo));
|
||||||
|
printf("checksum : %x\n", packet.checksum);
|
||||||
|
|
||||||
return(0);
|
return(0);
|
||||||
}
|
}
|
||||||
|
@ -2,5 +2,29 @@
|
|||||||
# define FT_PING_H
|
# define FT_PING_H
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/time.h>
|
||||||
|
|
||||||
|
#define PACKET_DATA_LENGTH 40
|
||||||
|
|
||||||
|
typedef struct s_settings
|
||||||
|
{
|
||||||
|
char *host;
|
||||||
|
int count;
|
||||||
|
int ttl;
|
||||||
|
} t_settings;
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct s_icmp_echo
|
||||||
|
{
|
||||||
|
uint8_t type;
|
||||||
|
uint8_t code;
|
||||||
|
uint16_t checksum;
|
||||||
|
uint16_t identifier;
|
||||||
|
uint16_t sequence;
|
||||||
|
struct timeval time;
|
||||||
|
uint8_t data[40];
|
||||||
|
} t_icmp_echo;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Reference in New Issue
Block a user