From 8744d4490bd2744ffdab04ee693d3e4807542e2f Mon Sep 17 00:00:00 2001 From: starnakin Date: Thu, 30 Oct 2025 10:20:31 -0500 Subject: [PATCH] clean: split code in multiple files --- src/dns.c | 36 ++++++++++++ src/dns.h | 4 ++ src/main.c | 163 ++++----------------------------------------------- src/packet.c | 120 +++++++++++++++++++++++++++++++++++++ src/packet.h | 8 +++ src/print.c | 15 +++++ src/print.h | 3 + 7 files changed, 196 insertions(+), 153 deletions(-) create mode 100644 src/dns.c create mode 100644 src/dns.h create mode 100644 src/packet.c create mode 100644 src/packet.h create mode 100644 src/print.c create mode 100644 src/print.h diff --git a/src/dns.c b/src/dns.c new file mode 100644 index 0000000..ec47f86 --- /dev/null +++ b/src/dns.c @@ -0,0 +1,36 @@ + + +#include "print.h" +#include +#include +#include +#include + +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); +{ + +} \ No newline at end of file diff --git a/src/dns.h b/src/dns.h new file mode 100644 index 0000000..ed07549 --- /dev/null +++ b/src/dns.h @@ -0,0 +1,4 @@ +#pragma once + +int dns_lookup(const char *hostname, char *ipstr); +int dns_reverse_lookup(const char *ipstr, char *hostname); \ No newline at end of file diff --git a/src/main.c b/src/main.c index 7a03283..672c369 100644 --- a/src/main.c +++ b/src/main.c @@ -1,3 +1,5 @@ +#include "packet.h" +#include "print.h" #include #include #include @@ -20,162 +22,12 @@ 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) code; 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) { (void) ac; @@ -204,7 +56,7 @@ int main(int ac, char **av) return 1; } - char *packet = create_packet(payload_size); + char *packet = packet_create(payload_size); if (packet == NULL) { close(sockfd); @@ -248,14 +100,19 @@ int main(int ac, char **av) } 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; printf("icmp reply received %fms\n", time_interval); sleep(1); - update_packet(packet, payload_size); + if (packet_update(packet, payload_size)) + { + close(sockfd); + free(buffer); + free(packet); + } } free(packet); free(buffer); diff --git a/src/packet.c b/src/packet.c new file mode 100644 index 0000000..c221146 --- /dev/null +++ b/src/packet.c @@ -0,0 +1,120 @@ +#include "print.h" +#include +#include +#include +#include +#include +#include + +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; +} \ No newline at end of file diff --git a/src/packet.h b/src/packet.h new file mode 100644 index 0000000..32ffe59 --- /dev/null +++ b/src/packet.h @@ -0,0 +1,8 @@ +#pragma once + +#include + +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); \ No newline at end of file diff --git a/src/print.c b/src/print.c new file mode 100644 index 0000000..4760fee --- /dev/null +++ b/src/print.c @@ -0,0 +1,15 @@ + + +#include +#include + +void print_err(const char *format, ...) +{ + va_list args; + + puts("ft_ping: "); + va_start(args, format); + vprintf(format, args); + va_end(args); + putchar('\n'); +} \ No newline at end of file diff --git a/src/print.h b/src/print.h new file mode 100644 index 0000000..d36fa59 --- /dev/null +++ b/src/print.h @@ -0,0 +1,3 @@ +#pragma once + +void print_err(const char *format, ...); \ No newline at end of file