Add Makefile
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -1 +1,5 @@
|
|||||||
.vscode
|
.vscode
|
||||||
|
.cache
|
||||||
|
generate_json.py
|
||||||
|
ft_ping
|
||||||
|
|
||||||
|
|||||||
52
Makefile
Normal file
52
Makefile
Normal file
@@ -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
|
||||||
12
compile_commands.json
Normal file
12
compile_commands.json
Normal file
@@ -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"
|
||||||
|
}
|
||||||
|
]
|
||||||
@@ -15,6 +15,7 @@
|
|||||||
#include <netinet/ip_icmp.h>
|
#include <netinet/ip_icmp.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
|
||||||
#define PACKET_SIZE 64
|
#define PACKET_SIZE 64
|
||||||
|
|
||||||
@@ -34,7 +35,7 @@ struct packet_stats
|
|||||||
double timestamp_array[65536];
|
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();
|
double get_timestamp();
|
||||||
struct in_addr get_addr_by_hostname(char *hostname);
|
struct in_addr get_addr_by_hostname(char *hostname);
|
||||||
double get_stddev(double *timestamp_array);
|
double get_stddev(double *timestamp_array);
|
||||||
BIN
objs/src/main.o
Normal file
BIN
objs/src/main.o
Normal file
Binary file not shown.
BIN
objs/src/utils.o
Normal file
BIN
objs/src/utils.o
Normal file
Binary file not shown.
@@ -14,7 +14,7 @@ int ft_ping(int sock, uint16_t seq, struct sockaddr_in *dst)
|
|||||||
icmp_hdr->code = 0;
|
icmp_hdr->code = 0;
|
||||||
icmp_hdr->id = getpid();
|
icmp_hdr->id = getpid();
|
||||||
icmp_hdr->seq = seq;
|
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)
|
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;
|
time = (get_timestamp() - start) * 1000;
|
||||||
checksum = icmp_hdr->checksum;
|
checksum = icmp_hdr->checksum;
|
||||||
icmp_hdr->checksum = 0;
|
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;
|
return;
|
||||||
fill_timestamp_array(&stats, time);
|
fill_timestamp_array(&stats, time);
|
||||||
stats.n_packet_recv++;
|
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("%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));
|
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 main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
int sock;
|
int sock;
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
#include "ft_ping.h"
|
#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;
|
uint32_t checksum = 0;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
||||||
@@ -43,6 +44,7 @@ void fill_timestamp_array(struct packet_stats *stats, double time)
|
|||||||
|
|
||||||
double get_min(double *timestamp_array)
|
double get_min(double *timestamp_array)
|
||||||
{
|
{
|
||||||
|
// get the smallest element of the timestamp_array
|
||||||
double min = timestamp_array[0];
|
double min = timestamp_array[0];
|
||||||
for (int i = 1; timestamp_array[i]; i++)
|
for (int i = 1; timestamp_array[i]; i++)
|
||||||
{
|
{
|
||||||
@@ -54,6 +56,7 @@ double get_min(double *timestamp_array)
|
|||||||
|
|
||||||
double get_max(double *timestamp_array)
|
double get_max(double *timestamp_array)
|
||||||
{
|
{
|
||||||
|
// get the biggest element of the timestamp_array
|
||||||
double max = timestamp_array[0];
|
double max = timestamp_array[0];
|
||||||
for (int i = 1; timestamp_array[i]; i++)
|
for (int i = 1; timestamp_array[i]; i++)
|
||||||
{
|
{
|
||||||
@@ -65,6 +68,7 @@ double get_max(double *timestamp_array)
|
|||||||
|
|
||||||
double get_avg(double *timestamp_array)
|
double get_avg(double *timestamp_array)
|
||||||
{
|
{
|
||||||
|
// get the average of elements in timestamp_array
|
||||||
double avg = 0;
|
double avg = 0;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
||||||
@@ -78,6 +82,7 @@ double get_avg(double *timestamp_array)
|
|||||||
|
|
||||||
double get_stddev(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 avg = get_avg(timestamp_array);
|
||||||
float variance = 0;
|
float variance = 0;
|
||||||
float variance_tmp;
|
float variance_tmp;
|
||||||
Reference in New Issue
Block a user