clean: split code in multiple files

This commit is contained in:
2025-10-30 10:20:31 -05:00
parent 94ae14f105
commit 8744d4490b
7 changed files with 196 additions and 153 deletions

36
src/dns.c Normal file
View File

@ -0,0 +1,36 @@
#include "print.h"
#include <arpa/inet.h>
#include <stddef.h>
#include <netdb.h>
#include <strings.h>
int dns_lookup(const char *hostname, char *ipstr)
{
struct addrinfo filter;
bzero(&filter, sizeof(filter));
filter.ai_family = AF_INET;
struct addrinfo *responses;
if (getaddrinfo(hostname, NULL, &filter, &responses) != 0)
{
print_err("dns lookup failed");
return 1;
}
struct addrinfo *first = responses;
struct in_addr *addr = &(((struct sockaddr_in *) responses->ai_addr)->sin_addr);
inet_ntop(first->ai_family, addr, ipstr, sizeof(char) * INET_ADDRSTRLEN);
freeaddrinfo(responses);
return 0;
}
int dns_reverse_lookup(const char *ipstr, char *hostname);
{
}

4
src/dns.h Normal file
View File

@ -0,0 +1,4 @@
#pragma once
int dns_lookup(const char *hostname, char *ipstr);
int dns_reverse_lookup(const char *ipstr, char *hostname);

View File

@ -1,3 +1,5 @@
#include "packet.h"
#include "print.h"
#include <signal.h> #include <signal.h>
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
@ -20,162 +22,12 @@
bool loop = true; bool loop = true;
void print_err(const char *format, ...)
{
va_list args;
puts("ft_ping: ");
va_start(args, format);
vprintf(format, args);
va_end(args);
putchar('\n');
}
unsigned short checksum(void *b, int len) {
unsigned short *buf = b;
unsigned int sum = 0;
unsigned short result;
for (sum = 0; len > 1; len -= 2)
sum += *buf++;
if (len == 1)
sum += *(unsigned char *)buf;
sum = (sum >> 16) + (sum & 0xFFFF);
sum += (sum >> 16);
result = ~sum;
return result;
}
int pktcmp(const char *sent, const char *received, size_t packet_size)
{
const struct icmphdr *sent_hdr = (const struct icmphdr*) sent;
const struct icmphdr *received_hdr = (const struct icmphdr*) received;
if (received_hdr->type != 0 || received_hdr->code != 8)
return 1;
if (received_hdr->un.echo.sequence == sent_hdr->un.echo.sequence)
return 2;
const char *sent_payload = sent + sizeof(struct icmphdr);
const char *received_payload = received + sizeof(struct icmphdr);
return memcmp(sent_payload, received_payload, packet_size - sizeof(struct icmphdr));
}
int fill_with_random(char *str, size_t nb)
{
int fd;
fd = open("/dev/random", O_RDONLY);
if (fd < 0)
{
print_err("error: open /dev/random failed.");
return 1;
}
if (read(fd, str, nb) != (ssize_t) nb)
{
close(fd);
print_err("error: read /dev/random failed.");
return 2;
}
close(fd);
return 0;
}
int update_packet(char *packet, size_t payload_size)
{
static size_t sequence = 1;
size_t hdr_size = sizeof(struct icmphdr);
size_t packet_size = hdr_size + payload_size;
struct icmphdr *hdr = (void*)packet;
hdr->un.echo.sequence = htons(sequence++);
hdr->checksum = 0;
if (fill_with_random(packet + hdr_size, payload_size))
{
free(packet);
return 1;
}
hdr->checksum = checksum(packet, packet_size);
return 0;
}
void *create_packet(size_t payload_size)
{
size_t hdr_size = sizeof(struct icmphdr);
size_t packet_size = hdr_size + payload_size;
char *packet;
packet = malloc(packet_size);
if (packet == NULL)
{
print_err("allocation failed.");
return NULL;
}
struct icmphdr *hdr = (struct icmphdr *)packet;
hdr->type = ICMP_ECHO;
hdr->code = 0;
hdr->un.echo.id = htons(getpid() & 0xFFFF);
update_packet(packet, payload_size);
return packet;
}
int packet_check(char *packet, size_t packet_size)
{
struct icmphdr *hdr = (struct icmphdr *) packet;
const uint16_t checksum_bak = hdr->checksum;
hdr->checksum = 0;
int tmp = checksum(packet, packet_size);
hdr->checksum = checksum_bak;
return tmp - checksum_bak;
}
void signal_handler(int code) void signal_handler(int code)
{ {
(void) code; (void) code;
loop = false; loop = false;
} }
char *dns_lookup(const char *hostname)
{
char *ipstr = malloc(sizeof(char) * INET_ADDRSTRLEN);
if (ipstr == NULL)
return NULL;
struct addrinfo filter;
bzero(&filter, sizeof(filter));
filter.ai_family = AF_INET;
struct addrinfo *responses;
if (getaddrinfo(hostname, NULL, &filter, &responses) != 0)
{
print_err("dns lookup failed");
free(ipstr);
return NULL;
}
struct addrinfo *first = responses;
struct in_addr *addr = &(((struct sockaddr_in *) responses->ai_addr)->sin_addr);
inet_ntop(first->ai_family, addr, ipstr, sizeof(char) * INET_ADDRSTRLEN);
freeaddrinfo(responses);
return ipstr;
}
int main(int ac, char **av) int main(int ac, char **av)
{ {
(void) ac; (void) ac;
@ -204,7 +56,7 @@ int main(int ac, char **av)
return 1; return 1;
} }
char *packet = create_packet(payload_size); char *packet = packet_create(payload_size);
if (packet == NULL) if (packet == NULL)
{ {
close(sockfd); close(sockfd);
@ -248,14 +100,19 @@ int main(int ac, char **av)
} }
gettimeofday(&stop, NULL); gettimeofday(&stop, NULL);
} }
while (len == packet_size && packet_check(buffer, packet_size) == 0 && pktcmp(packet, buffer, packet_size) == 0); while (len == packet_size && packet_check(buffer, packet_size) == 0 && packet_compare(packet, buffer, packet_size) == 0);
double time_interval = ((stop.tv_sec - start.tv_sec) * 1000 + (stop.tv_usec - start.tv_usec)) / 1000; double time_interval = ((stop.tv_sec - start.tv_sec) * 1000 + (stop.tv_usec - start.tv_usec)) / 1000;
printf("icmp reply received %fms\n", time_interval); printf("icmp reply received %fms\n", time_interval);
sleep(1); sleep(1);
update_packet(packet, payload_size); if (packet_update(packet, payload_size))
{
close(sockfd);
free(buffer);
free(packet);
}
} }
free(packet); free(packet);
free(buffer); free(buffer);

120
src/packet.c Normal file
View File

@ -0,0 +1,120 @@
#include "print.h"
#include <fcntl.h>
#include <netinet/ip_icmp.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static int fill_with_random(char *str, size_t nb)
{
int fd;
fd = open("/dev/random", O_RDONLY);
if (fd < 0)
{
print_err("error: open /dev/random failed.");
return 1;
}
if (read(fd, str, nb) != (ssize_t) nb)
{
close(fd);
print_err("error: read /dev/random failed.");
return 2;
}
close(fd);
return 0;
}
static unsigned short checksum(void *b, int len) {
unsigned short *buf = b;
unsigned int sum = 0;
unsigned short result;
for (sum = 0; len > 1; len -= 2)
sum += *buf++;
if (len == 1)
sum += *(unsigned char *)buf;
sum = (sum >> 16) + (sum & 0xFFFF);
sum += (sum >> 16);
result = ~sum;
return result;
}
int packet_compare(const char *sent, const char *received, size_t packet_size)
{
const struct icmphdr *sent_hdr = (const struct icmphdr*) sent;
const struct icmphdr *received_hdr = (const struct icmphdr*) received;
if (received_hdr->type != 0 || received_hdr->code != 8)
return 1;
if (received_hdr->un.echo.sequence == sent_hdr->un.echo.sequence)
return 2;
const char *sent_payload = sent + sizeof(struct icmphdr);
const char *received_payload = received + sizeof(struct icmphdr);
return memcmp(sent_payload, received_payload, packet_size - sizeof(struct icmphdr));
}
int packet_check(char *packet, size_t packet_size)
{
struct icmphdr *hdr = (struct icmphdr *) packet;
const uint16_t checksum_bak = hdr->checksum;
hdr->checksum = 0;
int tmp = checksum(packet, packet_size);
hdr->checksum = checksum_bak;
return tmp - checksum_bak;
}
int packet_update(char *packet, size_t payload_size)
{
static size_t sequence = 1;
size_t hdr_size = sizeof(struct icmphdr);
size_t packet_size = hdr_size + payload_size;
struct icmphdr *hdr = (void*)packet;
hdr->un.echo.sequence = htons(sequence++);
hdr->checksum = 0;
if (fill_with_random(packet + hdr_size, payload_size))
return 1;
hdr->checksum = checksum(packet, packet_size);
return 0;
}
void *packet_create(size_t payload_size)
{
size_t hdr_size = sizeof(struct icmphdr);
size_t packet_size = hdr_size + payload_size;
char *packet;
packet = malloc(packet_size);
if (packet == NULL)
{
print_err("allocation failed.");
return NULL;
}
struct icmphdr *hdr = (struct icmphdr *)packet;
hdr->type = ICMP_ECHO;
hdr->code = 0;
hdr->un.echo.id = htons(getpid() & 0xFFFF);
if (packet_update(packet, payload_size))
{
free(packet);
return NULL;
}
return packet;
}

8
src/packet.h Normal file
View File

@ -0,0 +1,8 @@
#pragma once
#include <stddef.h>
int packet_compare(const char *sent, const char *received, size_t packet_size);
int packet_check(char *packet, size_t packet_size);
int packet_update(char *packet, size_t payload_size);
void *packet_create(size_t payload_size);

15
src/print.c Normal file
View File

@ -0,0 +1,15 @@
#include <stdarg.h>
#include <stdio.h>
void print_err(const char *format, ...)
{
va_list args;
puts("ft_ping: ");
va_start(args, format);
vprintf(format, args);
va_end(args);
putchar('\n');
}

3
src/print.h Normal file
View File

@ -0,0 +1,3 @@
#pragma once
void print_err(const char *format, ...);