Add: more commentary & clean code

This commit is contained in:
vauden
2024-11-19 19:22:38 +01:00
parent 0058189d72
commit 0811342010
4 changed files with 14 additions and 7 deletions

View File

@@ -52,23 +52,30 @@ struct flags
}; };
// PACKET UTILS // 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); struct in_addr get_addr_by_hostname(char *hostname);
bool parse_packet(struct net_packet packet, uint16_t seq, uint16_t checksum); bool parse_packet(struct net_packet packet, uint16_t seq, uint16_t checksum);
// MATH // MATH
/* Returns the standard deviation of timestamp_array*/
double get_stddev(const double *timestamp_array); double get_stddev(const double *timestamp_array);
/* Returns the average value of timestamp_array*/
double get_avg(const double *timestamp_array); double get_avg(const double *timestamp_array);
/* Returns the smallest value of timestamp_array*/
double get_min(const double *timestamp_array); double get_min(const double *timestamp_array);
/* Returns the biggest value of timestamp_array*/
double get_max(const double *timestamp_array); double get_max(const double *timestamp_array);
long ft_atoi(const char *nptr); long ft_atoi(const char *nptr);
// VERBOSE // VERBOSE
void print_help(void);
bool parse_opt(int argc, char **argv, struct flags *flags); bool parse_opt(int argc, char **argv, struct flags *flags);
void print_packet_error(struct net_packet packet); void print_packet_error(struct net_packet packet);
// STATS // STATS
void print_recap(char *ip, const struct packet_stats 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); void fill_timestamp_array(struct packet_stats *sockstats, double time);
/* Returns the current time*/
double get_timestamp(); double get_timestamp();

View File

@@ -15,7 +15,7 @@ const char *flag_list[] =
NULL, NULL,
}; };
void print_help(void) static void print_help(void)
{ {
for (int i = 0; flag_list[i]; i++) for (int i = 0; flag_list[i]; i++)
printf("%s\n", flag_list[i]); printf("%s\n", flag_list[i]);

View File

@@ -1,7 +1,7 @@
#include "ft_ping.h" #include "ft_ping.h"
#include "icmp_codes.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) switch(type)
{ {
@@ -14,7 +14,7 @@ static const char *get_error_message(uint8_t type, uint8_t code)
} }
return (NULL); 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) if (packet.icmp_hdr->seq != seq || make_checksum((uint16_t *)packet.icmp_hdr, sizeof(*packet.icmp_hdr)) != checksum)
return (false); return (false);
@@ -23,7 +23,7 @@ bool parse_packet(struct net_packet packet, uint16_t seq, uint16_t checksum)
return (true); 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)); 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));
} }

View File

@@ -1,6 +1,6 @@
#include "ft_ping.h" #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 // make the checksum of data
uint32_t checksum = 0; uint32_t checksum = 0;