Skip to content

Commit f3f920f

Browse files
Theo SIGNORETheo SIGNORE
Theo SIGNORE
authored and
Theo SIGNORE
committed
05 - fixed A LOT of things
all syscall are protected refactoring done todo: last check, maybe advanced error handling ?
1 parent dd55806 commit f3f920f

11 files changed

+357
-267
lines changed

Makefile

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
SRCS := ft_ping.c \
22
sending_and_receiving.c \
3-
dgram_and_hdrs.c \
3+
dgram.c \
4+
icmphdr.c \
5+
iphdr.c \
46
msghdr.c \
7+
summary.c \
58
pings.c \
69
time_stuff.c \
710
utils.c
@@ -11,8 +14,8 @@ OBJS = ${SRCS:%.c=.%.o}
1114
DEP = ${SRCS:%.c=.%.d}
1215

1316
#FLAGS := -Wall -Wextra -Werror -MMD
14-
FLAGS := -Wall -Wextra -Werror -g -MMD
15-
#FLAGS := -Wall -Wextra -Werror -g -MMD -fsanitize=address
17+
#FLAGS := -Wall -Wextra -Werror -g -MMD
18+
FLAGS := -Wall -Wextra -Werror -g -MMD -fsanitize=address
1619

1720
COMPILO := gcc
1821

dgram.c

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "ft_ping.h"
2+
3+
dgram_t* create_dgram(sain_t* target)
4+
{
5+
dgram_t* res;
6+
7+
res = malloc(sizeof(dgram_t));
8+
if (res)
9+
{
10+
set_iphdr(&(res->ip_hdr), target);
11+
set_icmp(&(res->icmp_hdr), res->data);
12+
}
13+
else
14+
perror("ft_ping: malloc()");
15+
return (res);
16+
}
17+
18+
void dgram_dump(dgram_t* dgram, size_t size)
19+
{
20+
hexdump_iphdr(&(dgram->ip_hdr));
21+
print_iphdr(&(dgram->ip_hdr));
22+
print_icmp(&(dgram->icmp_hdr), size);
23+
}

ft_ping.c

+58-44
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "ft_ping.h"
22

3-
char sig = 0;
3+
int sig = 0;
44

55
static int get_target(char* arg, target_t* target)
66
{
@@ -12,7 +12,7 @@ static int get_target(char* arg, target_t* target)
1212
ret = getaddrinfo(arg, NULL, NULL, &addr_nfo);
1313
if (ret != 0)
1414
{
15-
dprintf(STDERR_FILENO, "ping: %s: %s\n", arg, gai_strerror(ret));
15+
dprintf(STDERR_FILENO, "ft_ping: %s: %s\n", arg, gai_strerror(ret));
1616
return (1);
1717
}
1818
target->fqdn = arg;
@@ -22,59 +22,58 @@ static int get_target(char* arg, target_t* target)
2222
}
2323
else
2424
{
25-
mmcpy(arg, target->ip, 12);
25+
mmcpy(arg, target->ip, ft_strlen(arg));
2626
target->fqdn = NULL;
2727
}
2828
return (0);
2929
}
3030

31-
int icmp_socket(void)
31+
static int icmp_socket(void)
3232
{
3333
int res;
3434
int one = 1;
3535
int suckit = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
3636
if (suckit == -1)
3737
{
38-
perror("ping: socket()");
38+
perror("ft_ping: socket()");
3939
return (-1);
4040
}
4141
res = setsockopt(suckit, IPPROTO_IP, IP_HDRINCL, &one, sizeof(one));
4242
if (!res)
4343
{
4444
res = setsockopt(suckit, IPPROTO_IP, IP_RECVERR, &one, sizeof(one));
4545
if (res)
46-
perror("ping: setsockopt(,,IP_RECVERR,,)");
46+
perror("ft_ping: setsockopt(,,IP_RECVERR,,)");
4747
}
4848
else
49-
perror("ping: setsockopt(,,IP_HDRINCL,,)");
49+
perror("ft_ping: setsockopt(,,IP_HDRINCL,,)");
5050
if (res == 0)
5151
return (suckit);
5252
close(suckit);
5353
return (-1);
5454
}
5555

56-
void handolo(int fuck)
56+
void signal_handler(int signum)
5757
{
58-
(void)fuck;
59-
sig = 1;
60-
}
61-
62-
void sendsig(int fuck)
63-
{
64-
(void)fuck;
65-
send_icmp_echo();
66-
if (!sig)
67-
alarm(1);
58+
if (signum == SIGINT)
59+
sig = 1;
60+
else
61+
{
62+
send_icmp_echo();
63+
if (!sig)
64+
alarm(1);
65+
}
6866
}
6967

7068
int main(int ac, char** av)
7169
{
7270
if (ac < 2 || ac > 3)
7371
{
74-
dprintf(STDERR_FILENO, "Usage: ft_ping [-v] <IPv4 | FQDN>\n");
72+
dprintf(STDERR_FILENO, "ft_ping: invalid usage\nTry 'ft_ping -?' for more information\n");
7573
return (1);
7674
}
7775

76+
// I don't have in mind to add more options
7877
int vrb = 0;
7978
if (ac == 3)
8079
{
@@ -85,69 +84,84 @@ int main(int ac, char** av)
8584
}
8685
else
8786
{
88-
dprintf(STDERR_FILENO, "Unknown option: \"%s\"\nUsage: ft_ping [-v] <IPv4 | FQDN>\n", av[1]);
87+
dprintf(STDERR_FILENO, "ft_ping: invalid usage\nTry 'ft_ping -?' for more information\n");
8988
return (1);
9089
}
9190
}
91+
else if (ac == 2 && av[1][0] == '-' && av[1][1] == '?')
92+
{
93+
printf("-- ft_ping --\nSend ICMP ECHO_REQUEST packets to a network host.\nUsage:\n ft_ping HOST | standard usage\n ft_ping -v HOST | verbose mode\n ft_ping -? | display this help\n");
94+
return (0);
95+
}
96+
97+
//get info on targeted host
9298
target_t target;
9399
zerocalcare(&target, sizeof(target));
94100
int ret = get_target(av[1], &target);
95101
if (ret)
96102
return (1);
97103

104+
//opening and configuring socket
98105
int suckit = icmp_socket();
99106
if (suckit == -1)
100107
return (1);
101108

102-
ping_t* first_ping = NULL;
103-
104-
signal(SIGINT, &handolo);
105-
109+
//allocating and building datagram: IP and ICMP headers, and data
106110
dgram_t* dgram = create_dgram(&(target.addr));
107111
if (!dgram)
108112
{
109113
close(suckit);
110114
return (1);
111115
}
112116

117+
//pointer to the first ping of the ping list
118+
ping_t* first_ping = NULL;
119+
120+
//passing the values to initialise the statics in the sender function
113121
set_icmp_echo(suckit, dgram, &(target.addr), &first_ping);
114-
signal(SIGALRM, &sendsig);
115122

123+
//setting signal handlers (sigaction would be better but it's unauthorized)
124+
signal(SIGINT, &signal_handler);
125+
signal(SIGALRM, &signal_handler);
126+
127+
printf("PING %s (%s): %zu data bytes", av[1], target.ip, ICMP_ECHO_SIZE - sizeof(struct icmphdr));
116128
if (vrb)
117-
printf("PING %s (%s): %zu data bytes, id 0x%x = %i\n", av[1], target.ip, ICMP_ECHO_SIZE - sizeof(struct icmphdr), getpid(), getpid());
129+
printf(", id 0x%x = %i\n", getpid(), getpid());
118130
else
119-
printf("PING %s (%s): %zu data bytes.\n", av[1], target.ip, ICMP_ECHO_SIZE - sizeof(struct icmphdr));
120-
alarm(1);
131+
printf(".\n");
121132

133+
//allocating and setting message header for recvmsg()
122134
struct msghdr* msg_hdr = alloc_msghdr();
135+
if (!msg_hdr)
136+
{
137+
free(dgram);
138+
close(suckit);
139+
return (1);
140+
}
141+
142+
//starting the sender loop
143+
alarm(1);
144+
145+
//starting the listening loop
123146
int res = 0;
124147
while(!sig)
125148
{
126149
res = recvmsg(suckit, msg_hdr, MSG_DONTWAIT);
127150
if(res > 0)
128151
{
129-
receive_icmp_reply(msg_hdr, &first_ping, res, &target, vrb);
152+
receive_icmp_reply(msg_hdr, &first_ping, res, &target, vrb, dgram->icmp_hdr.un.echo.id);
130153
}
131154
}
132155
close(suckit);
156+
free(dgram);
133157
free(msg_hdr);
158+
159+
//computing summary with the ping list
134160
summary_t summary;
135161
get_summary(first_ping, &summary);
162+
136163
free_pings(first_ping);
137-
printf("--- %s ping statistics ---\n%zu packets transmitted, %zu received, %zu%% packet loss\n",
138-
av[1],
139-
summary.transmitted,
140-
summary.received,
141-
summary.loss
142-
);
143-
if (summary.received)
144-
{
145-
printf("round-trip min/avg/max/stddev = %.3f/%.3f/%.3f/%.3f ms\n",
146-
time_to_fms(&(summary.min)),
147-
time_to_fms(&(summary.avg)),
148-
time_to_fms(&(summary.max)),
149-
time_to_fms(&(summary.mdev))
150-
);
151-
}
164+
165+
print_summary(&summary, av[1]);
152166
return (0);
153167
}

ft_ping.h

+42-23
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,9 @@
2929
#define MSGHDR_CONTROL_OFFSET (MSGHDR_NAME_OFFSET + MSGHDR_NAMELEN)
3030
#define MSGHDR_IOVBASE_OFFSET (MSGHDR_CONTROL_OFFSET + MSGHDR_CONTROLLEN)
3131

32-
33-
void mmcpy(const void* src, void* dst, size_t size);
34-
void zerocalcare(void* ptr, size_t size);
35-
size_t ft_strlen(const char* str);
36-
size_t ipv4_ntoa(uint32_t s_addr, char* dst);
37-
3832
typedef struct timeval tv_t;
3933
typedef struct sockaddr_in sain_t;
4034

41-
void time_diff(tv_t* a, tv_t* b, tv_t* res);
42-
void time_div(tv_t* a, size_t nb, tv_t* c);
43-
void time_sum(tv_t* a, tv_t* b, tv_t* res);
44-
int time_grt(tv_t* a, tv_t* b);
45-
int time_lwr(tv_t* a, tv_t* b);
46-
size_t time_to_ims(tv_t*);
47-
double time_to_fms(tv_t*);
48-
void time_cpy(tv_t* a, tv_t* b);
49-
5035
typedef struct s_ping
5136
{
5237
size_t seq;
@@ -81,18 +66,52 @@ typedef struct s_target
8166
sain_t addr;
8267
} target_t;
8368

69+
extern int sig;
8470

85-
struct msghdr* alloc_msghdr(void);
86-
71+
//dgram.c
8772
dgram_t* create_dgram(sain_t* target);
8873
void dgram_dump(dgram_t* dgram, size_t size);
89-
void set_icmp(struct icmphdr* icmp_hdr, char* data);
9074

91-
void set_icmp_echo(int socket, dgram_t* dgram, sain_t* targetptr, ping_t** pings);
92-
int send_icmp_echo(void);
93-
int receive_icmp_reply(struct msghdr* msg_hdr, ping_t** pings, int res, target_t* target, char vrb);
75+
//icmphdr.c
76+
int verify_checksum(struct icmphdr* icmp_hdr);
77+
void set_icmp(struct icmphdr* icmp_hdr, char* data);
78+
void print_icmp(const struct icmphdr* icmp_hdr, size_t size);
79+
80+
//iphdr.c
81+
void set_iphdr(struct iphdr* ip_hdr, const sain_t* target);
82+
void hexdump_iphdr(const struct iphdr* ip_hdr);
83+
void print_iphdr(struct iphdr* ip_hdr);
84+
85+
//msghdr.c
86+
struct msghdr* alloc_msghdr(void);
9487

95-
void add_ping(ping_t** first, int seq, time_t seconds, suseconds_t micro);
88+
//pings.c
89+
int add_ping(ping_t** first, int seq, time_t seconds, suseconds_t micro);
9690
ping_t* note_reply(ping_t* first, size_t sequence, time_t seconds, suseconds_t micro);
97-
void get_summary(ping_t* first, summary_t* summary);
9891
void free_pings(ping_t* first_ping);
92+
93+
//sending_and_receiving
94+
void set_icmp_echo(int socket, dgram_t* dgram, sain_t* targetptr, ping_t** pings);
95+
int send_icmp_echo(void);
96+
int receive_icmp_reply(struct msghdr* msg_hdr, ping_t** pings, int res, target_t* target, char vrb, uint16_t);
97+
98+
//summary.c
99+
void get_summary(ping_t* first, summary_t* summary);
100+
void print_summary(summary_t* summary, const char* arg);
101+
102+
//time_stuff.c
103+
void time_diff(tv_t* a, tv_t* b, tv_t* res);
104+
void time_div(tv_t* a, size_t nb, tv_t* c);
105+
void time_sum(tv_t* a, tv_t* b, tv_t* res);
106+
int time_grt(tv_t* a, tv_t* b);
107+
int time_lwr(tv_t* a, tv_t* b);
108+
size_t time_to_ims(tv_t*);
109+
double time_to_fms(tv_t*);
110+
void time_cpy(tv_t* a, tv_t* b);
111+
112+
//utils.c
113+
void mmcpy(const void* src, void* dst, size_t size);
114+
void zerocalcare(void* ptr, size_t size);
115+
size_t ft_strlen(const char* str);
116+
size_t ipv4_ntoa(uint32_t s_addr, char* dst);
117+
uint16_t invert_bytes(uint16_t n);

0 commit comments

Comments
 (0)