-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
106 lines (90 loc) · 2.3 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
/ Daniel Dubiel 291111
*/
#include "netinet/ip.h"
#include "netinet/ip_icmp.h"
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include "inttypes.h"
#include "stdio.h"
#include "defines.h"
#include "receive.h"
#include "send.h"
#include "utils.h"
static int check_input(int argc, char* argv[]);
static void print_results(int ttl, reply_data (*replies)[PACKES_PER_TTL]);
int main(int argc, char* argv[])
{
if(!check_input(argc, argv))
{
printf("Incorrect ip adress format\n");
return EXIT_FAILURE;
}
LOG("MY PID %d", getppid());
int socketfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if(socketfd < 0)
{
EXIT_ERR("socket error");
}
reply_data replies[PACKES_PER_TTL];
for(int ttl = 1, reached = 0; ttl < MAX_TTL && reached == 0; ttl++)
{
send_pings(socketfd, argv[1], ttl);
LOG("");
memset(replies, 0, sizeof(replies));
reached = receive(socketfd, ttl, &replies);
LOG("");
print_results(ttl, &replies);
}
return 0;
}
static int check_input(int argc, char* argv[])
{
if(argc < 2)
perror("No arguments, ip adress required");
else if(argc > 2)
perror("To many arguments");
else
{
struct sockaddr_in sa;
int res = inet_pton(AF_INET, argv[1], &(sa.sin_addr));
return res != 0;
}
return 1;
}
static void print_results(int ttl, reply_data (*replies)[PACKES_PER_TTL])
{
int8_t is_reply = 0, no_avg = 0;
int16_t avg = 0;
for(int i = 0; i < PACKES_PER_TTL; i++)
{
is_reply += (*replies)[i].recived;
no_avg += (*replies)[i].recived == 0;
avg += (*replies)[i].ms;
LOG("%d ", (*replies)[i].recived);
}
avg /= PACKES_PER_TTL;
printf("%.2d. ", ttl);
if(!is_reply)
{
printf("*\n");
return;
}
for(int i = 0; i < PACKES_PER_TTL; i++)
{
if((*replies)[i].recived)
printf("%s ", (*replies)[i].ip_addr);
for(int j = PACKES_PER_TTL - 1; j > i; j--)
{
if(strcmp((*replies)[i].ip_addr, (*replies)[j].ip_addr) == 0)
(*replies)[j].recived = 0;
}
}
if(no_avg)
printf("%s\n", "???");
else
printf("%dms\n", avg);
}