diff --git a/.gitignore b/.gitignore index 722d5e7..2ec987a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,5 @@ .vscode +.cache +generate_json.py +ft_ping + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4549d0c --- /dev/null +++ b/Makefile @@ -0,0 +1,52 @@ +NAME = ./ft_ping + +SRCS = src/main.c \ + src/utils.c + + +OBJ_DIR = objs +OBJS = $(addprefix $(OBJ_DIR)/, $(SRCS:.c=.o)) + +CC = clang +CFLAGS = -Wall -Wextra -Werror -I includes + +DEBUG ?= false +MODE = "release" +TOOLCHAIN ?= clang + +ifeq ($(TOOLCHAIN), gcc) + CC = gcc +endif + +ifeq ($(DEBUG), true) + CFLAGS += -g -D DEBUG + MODE = "debug" +endif + +RM = rm -rf + +$(OBJ_DIR)/%.o: %.c + @echo "\e[1;32m[compiling "$(MODE)" {"$(CC)"}...]\e[1;00m "$< + @$(CC) $(CFLAGS) $(COPTS) -c $< -o $@ + +all: $(NAME) + + + +$(NAME): $(OBJ_DIR) $(OBJS) + @echo "\e[1;32m[linking "$(MODE)" {"$(CC)"}...]\e[1;00m "$@ + @$(CC) -o $(NAME) $(OBJS) -lm + @echo "\e[1;32m[build finished]\e[1;00m" + +$(OBJ_DIR): + @mkdir -p $(sort $(addprefix $(OBJ_DIR)/, $(dir $(SRCS)))) + +clean: + @$(RM) $(OBJ_DIR) + +fclean: clean + @$(RM) $(NAME) + +re: fclean all + +.PHONY: all clean fclean re diff --git a/compile_commands.json b/compile_commands.json new file mode 100644 index 0000000..963e993 --- /dev/null +++ b/compile_commands.json @@ -0,0 +1,12 @@ +[ + { + "directory": "/nfs/homes/vvaas/42/ft_ping", + "command": "clang++ -std=c++20 -I includes -c /nfs/homes/vvaas/42/ft_ping/src/main.c -o /nfs/homes/vvaas/42/ft_ping/objs/ma.o", + "file": "/nfs/homes/vvaas/42/ft_ping/src/main.c" + }, + { + "directory": "/nfs/homes/vvaas/42/ft_ping", + "command": "clang++ -std=c++20 -I includes -c /nfs/homes/vvaas/42/ft_ping/src/utils.c -o /nfs/homes/vvaas/42/ft_ping/objs/uti.o", + "file": "/nfs/homes/vvaas/42/ft_ping/src/utils.c" + } +] \ No newline at end of file diff --git a/ft_ping.h b/includes/ft_ping.h similarity index 92% rename from ft_ping.h rename to includes/ft_ping.h index d208e02..5d753ee 100644 --- a/ft_ping.h +++ b/includes/ft_ping.h @@ -15,6 +15,7 @@ #include #include #include +#include #define PACKET_SIZE 64 @@ -34,7 +35,7 @@ struct packet_stats double timestamp_array[65536]; }; -uint16_t calculate_checksum(uint16_t *data, int len); +uint16_t make_checksum(uint16_t *data, int len); double get_timestamp(); struct in_addr get_addr_by_hostname(char *hostname); double get_stddev(double *timestamp_array); diff --git a/objs/src/main.o b/objs/src/main.o new file mode 100644 index 0000000..bb24153 Binary files /dev/null and b/objs/src/main.o differ diff --git a/objs/src/utils.o b/objs/src/utils.o new file mode 100644 index 0000000..2fa6a00 Binary files /dev/null and b/objs/src/utils.o differ diff --git a/main.c b/src/main.c similarity index 95% rename from main.c rename to src/main.c index 9aca163..fabf241 100644 --- a/main.c +++ b/src/main.c @@ -14,7 +14,7 @@ int ft_ping(int sock, uint16_t seq, struct sockaddr_in *dst) icmp_hdr->code = 0; icmp_hdr->id = getpid(); icmp_hdr->seq = seq; - icmp_hdr->checksum = calculate_checksum((uint16_t *)icmp_hdr, sizeof(icmp_hdr)); + icmp_hdr->checksum = make_checksum((uint16_t *)icmp_hdr, sizeof(icmp_hdr)); if (sendto(sock, data, sizeof(data), 0, (struct sockaddr *)dst, sizeof(struct sockaddr_in)) == -1) { @@ -42,7 +42,7 @@ void ft_recv(int sock, uint16_t seq, char *ip, double start) time = (get_timestamp() - start) * 1000; checksum = icmp_hdr->checksum; icmp_hdr->checksum = 0; - if (icmp_hdr->seq != seq || calculate_checksum((uint16_t *)icmp_hdr, sizeof(*icmp_hdr)) != checksum) + if (icmp_hdr->seq != seq || make_checksum((uint16_t *)icmp_hdr, sizeof(*icmp_hdr)) != checksum) return; fill_timestamp_array(&stats, time); stats.n_packet_recv++; @@ -93,6 +93,7 @@ void print_recap(char *ip) printf("%d packed transmitted, %d received, %0.0f%% packet loss\n", stats.n_packet_sent, stats.n_packet_recv, (double)(100 - (stats.n_packet_recv / stats.n_packet_sent) * 100)); printf("round-trip min/avg/max/stddev = %5.3f/%5.3f/%5.3f/%5.3f ms\n", get_min(stats.timestamp_array), get_avg(stats.timestamp_array), get_max(stats.timestamp_array), get_stddev(stats.timestamp_array)); } + int main(int argc, char **argv) { int sock; diff --git a/utils.c b/src/utils.c similarity index 85% rename from utils.c rename to src/utils.c index 7bfb413..f680b1b 100644 --- a/utils.c +++ b/src/utils.c @@ -1,7 +1,8 @@ #include "ft_ping.h" -uint16_t calculate_checksum(uint16_t *data, int len) +uint16_t make_checksum(uint16_t *data, int len) { + // make the checksum of data uint32_t checksum = 0; int i = 0; @@ -43,6 +44,7 @@ void fill_timestamp_array(struct packet_stats *stats, double time) double get_min(double *timestamp_array) { + // get the smallest element of the timestamp_array double min = timestamp_array[0]; for (int i = 1; timestamp_array[i]; i++) { @@ -54,6 +56,7 @@ double get_min(double *timestamp_array) double get_max(double *timestamp_array) { + // get the biggest element of the timestamp_array double max = timestamp_array[0]; for (int i = 1; timestamp_array[i]; i++) { @@ -65,6 +68,7 @@ double get_max(double *timestamp_array) double get_avg(double *timestamp_array) { + // get the average of elements in timestamp_array double avg = 0; int i = 0; @@ -78,6 +82,7 @@ double get_avg(double *timestamp_array) double get_stddev(double *timestamp_array) { + // get the standard deviation of elements in timestamp_array float avg = get_avg(timestamp_array); float variance = 0; float variance_tmp; @@ -88,4 +93,4 @@ double get_stddev(double *timestamp_array) variance += variance_tmp * variance_tmp; } return (sqrt(variance)); -} \ No newline at end of file +}