1
1
#include "ft_ping.h"
2
2
3
- char sig = 0 ;
3
+ int sig = 0 ;
4
4
5
5
static int get_target (char * arg , target_t * target )
6
6
{
@@ -12,7 +12,7 @@ static int get_target(char* arg, target_t* target)
12
12
ret = getaddrinfo (arg , NULL , NULL , & addr_nfo );
13
13
if (ret != 0 )
14
14
{
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 ));
16
16
return (1 );
17
17
}
18
18
target -> fqdn = arg ;
@@ -22,59 +22,58 @@ static int get_target(char* arg, target_t* target)
22
22
}
23
23
else
24
24
{
25
- mmcpy (arg , target -> ip , 12 );
25
+ mmcpy (arg , target -> ip , ft_strlen ( arg ) );
26
26
target -> fqdn = NULL ;
27
27
}
28
28
return (0 );
29
29
}
30
30
31
- int icmp_socket (void )
31
+ static int icmp_socket (void )
32
32
{
33
33
int res ;
34
34
int one = 1 ;
35
35
int suckit = socket (AF_INET , SOCK_RAW , IPPROTO_ICMP );
36
36
if (suckit == -1 )
37
37
{
38
- perror ("ping : socket()" );
38
+ perror ("ft_ping : socket()" );
39
39
return (-1 );
40
40
}
41
41
res = setsockopt (suckit , IPPROTO_IP , IP_HDRINCL , & one , sizeof (one ));
42
42
if (!res )
43
43
{
44
44
res = setsockopt (suckit , IPPROTO_IP , IP_RECVERR , & one , sizeof (one ));
45
45
if (res )
46
- perror ("ping : setsockopt(,,IP_RECVERR,,)" );
46
+ perror ("ft_ping : setsockopt(,,IP_RECVERR,,)" );
47
47
}
48
48
else
49
- perror ("ping : setsockopt(,,IP_HDRINCL,,)" );
49
+ perror ("ft_ping : setsockopt(,,IP_HDRINCL,,)" );
50
50
if (res == 0 )
51
51
return (suckit );
52
52
close (suckit );
53
53
return (-1 );
54
54
}
55
55
56
- void handolo (int fuck )
56
+ void signal_handler (int signum )
57
57
{
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
+ }
68
66
}
69
67
70
68
int main (int ac , char * * av )
71
69
{
72
70
if (ac < 2 || ac > 3 )
73
71
{
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" );
75
73
return (1 );
76
74
}
77
75
76
+ // I don't have in mind to add more options
78
77
int vrb = 0 ;
79
78
if (ac == 3 )
80
79
{
@@ -85,69 +84,84 @@ int main(int ac, char** av)
85
84
}
86
85
else
87
86
{
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" );
89
88
return (1 );
90
89
}
91
90
}
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
92
98
target_t target ;
93
99
zerocalcare (& target , sizeof (target ));
94
100
int ret = get_target (av [1 ], & target );
95
101
if (ret )
96
102
return (1 );
97
103
104
+ //opening and configuring socket
98
105
int suckit = icmp_socket ();
99
106
if (suckit == -1 )
100
107
return (1 );
101
108
102
- ping_t * first_ping = NULL ;
103
-
104
- signal (SIGINT , & handolo );
105
-
109
+ //allocating and building datagram: IP and ICMP headers, and data
106
110
dgram_t * dgram = create_dgram (& (target .addr ));
107
111
if (!dgram )
108
112
{
109
113
close (suckit );
110
114
return (1 );
111
115
}
112
116
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
113
121
set_icmp_echo (suckit , dgram , & (target .addr ), & first_ping );
114
- signal (SIGALRM , & sendsig );
115
122
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 ));
116
128
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 ());
118
130
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" );
121
132
133
+ //allocating and setting message header for recvmsg()
122
134
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
123
146
int res = 0 ;
124
147
while (!sig )
125
148
{
126
149
res = recvmsg (suckit , msg_hdr , MSG_DONTWAIT );
127
150
if (res > 0 )
128
151
{
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 );
130
153
}
131
154
}
132
155
close (suckit );
156
+ free (dgram );
133
157
free (msg_hdr );
158
+
159
+ //computing summary with the ping list
134
160
summary_t summary ;
135
161
get_summary (first_ping , & summary );
162
+
136
163
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 ]);
152
166
return (0 );
153
167
}
0 commit comments