From 0811342010661fde4efa3fb973630fb02079e4fe Mon Sep 17 00:00:00 2001 From: vauden Date: Tue, 19 Nov 2024 19:22:38 +0100 Subject: [PATCH] Add: more commentary & clean code --- includes/ft_ping.h | 11 +++++++++-- src/flags.c | 2 +- src/packets.c | 6 +++--- src/utils.c | 2 +- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/includes/ft_ping.h b/includes/ft_ping.h index 7db5c6d..6949a26 100644 --- a/includes/ft_ping.h +++ b/includes/ft_ping.h @@ -52,23 +52,30 @@ struct flags }; // PACKET UTILS -uint16_t make_checksum(uint16_t *data, int len); +/* Returns the checksum of data*/ +uint16_t make_checksum(const uint16_t *data, int len); +/* Get the address of hostname from the host data base */ struct in_addr get_addr_by_hostname(char *hostname); bool parse_packet(struct net_packet packet, uint16_t seq, uint16_t checksum); // MATH +/* Returns the standard deviation of timestamp_array*/ double get_stddev(const double *timestamp_array); +/* Returns the average value of timestamp_array*/ double get_avg(const double *timestamp_array); +/* Returns the smallest value of timestamp_array*/ double get_min(const double *timestamp_array); +/* Returns the biggest value of timestamp_array*/ double get_max(const double *timestamp_array); long ft_atoi(const char *nptr); // VERBOSE -void print_help(void); bool parse_opt(int argc, char **argv, struct flags *flags); void print_packet_error(struct net_packet packet); // STATS void print_recap(char *ip, const struct packet_stats stats); +/* Add time into the timestamp_array in sockstats*/ void fill_timestamp_array(struct packet_stats *sockstats, double time); +/* Returns the current time*/ double get_timestamp(); \ No newline at end of file diff --git a/src/flags.c b/src/flags.c index d186406..575da18 100644 --- a/src/flags.c +++ b/src/flags.c @@ -15,7 +15,7 @@ const char *flag_list[] = NULL, }; -void print_help(void) +static void print_help(void) { for (int i = 0; flag_list[i]; i++) printf("%s\n", flag_list[i]); diff --git a/src/packets.c b/src/packets.c index add1448..644df21 100644 --- a/src/packets.c +++ b/src/packets.c @@ -1,7 +1,7 @@ #include "ft_ping.h" #include "icmp_codes.h" -static const char *get_error_message(uint8_t type, uint8_t code) +static const char *get_error_message(const uint8_t type, const uint8_t code) { switch(type) { @@ -14,7 +14,7 @@ static const char *get_error_message(uint8_t type, uint8_t code) } return (NULL); } -bool parse_packet(struct net_packet packet, uint16_t seq, uint16_t checksum) +bool parse_packet(const struct net_packet packet, const uint16_t seq, const uint16_t checksum) { if (packet.icmp_hdr->seq != seq || make_checksum((uint16_t *)packet.icmp_hdr, sizeof(*packet.icmp_hdr)) != checksum) return (false); @@ -23,7 +23,7 @@ bool parse_packet(struct net_packet packet, uint16_t seq, uint16_t checksum) return (true); } -void print_packet_error(struct net_packet packet) +void print_packet_error(const struct net_packet packet) { fprintf(stderr, "%d bytes from %s: %s\n", packet.n_bytes, inet_ntoa(packet.addr.sin_addr), get_error_message(packet.icmp_hdr->type, packet.icmp_hdr->code)); } \ No newline at end of file diff --git a/src/utils.c b/src/utils.c index 7ef04fd..dbceb57 100644 --- a/src/utils.c +++ b/src/utils.c @@ -1,6 +1,6 @@ #include "ft_ping.h" -uint16_t make_checksum(uint16_t *data, int len) +uint16_t make_checksum(const uint16_t *data, int len) { // make the checksum of data uint32_t checksum = 0;