-
Notifications
You must be signed in to change notification settings - Fork 0
/
ping.c
381 lines (331 loc) · 10.2 KB
/
ping.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <netinet/ip_icmp.h>
#include <time.h>
#include <stdbool.h>
#include <signal.h>
#include <float.h>
#include <assert.h>
#include <pthread.h>
#include <getopt.h>
#define DEFAULT_TTL 64;
#define DEFAULT_RECV_TIMEOUT 1
// Automatic port number
#define PORT_NO 0
#define DEFAULT_PACKET_SIZE 64
#define PING_SLEEP_RATE 1000000
// time to live value - how long the packet can be left alive in a network.
int ttl_val = DEFAULT_TTL;
int timeout = DEFAULT_RECV_TIMEOUT;
// ping packet size
int packet_size = DEFAULT_PACKET_SIZE;
bool continue_pinging = true;
char *ip_addr;
long double min_rtt = LDBL_MAX, max_rtt = 0;
const static struct option long_options[] = {
{"help", no_argument, 0, 0x1},
{"size", required_argument, 0, 's'},
{"timeout", required_argument, 0, 't'},
{0, 0, 0, 0}};
// ping packet structure
struct ping_pkt
{
struct icmphdr header;
char *msg;
};
struct thread_args
{
int ping_sockfd;
struct sockaddr_in *ping_addr;
char *ping_ip;
};
void usage(char *app);
bool parse_argv(int argc, char *argv[]);
int socket_creation();
struct in_addr **dns_lookup(char *, struct hostent *);
unsigned short checksum(void *, int);
void *send_ping(void *);
// Interrupt handler
void intHandler(int dummy);
int needed_thread_NO(int, int, char *[]);
int main(int argc, char *argv[])
{
//catching interrupt
signal(SIGINT, intHandler);
if (!parse_argv(argc, argv))
{
usage(argv[0]);
return 1;
}
int i, j, index = 0;
int num_thread = needed_thread_NO(optind, argc, argv);
pthread_t *pthreadArray = malloc(sizeof(pthread_t) * num_thread);
for (j = optind; j < argc; j++)
{
printf("non-option arg: %s\n", argv[j]);
struct hostent host_entity;
struct in_addr **ip_list = dns_lookup(argv[j], &host_entity);
if (ip_list == NULL)
{
printf("Resolving failed.\n");
exit(EXIT_FAILURE);
}
for (i = 0; ip_list[i] != NULL; i++)
{
}
int counter = i;
for (i = 0; i < counter; i++)
{
struct sockaddr_in server_addr;
server_addr.sin_family = host_entity.h_addrtype;
// The ICMP packet does not have port numbers because
// it was designed to communicate network-layer information
// not between application layer processes
server_addr.sin_addr.s_addr = *(long *)ip_list[i];
int sockfd = socket_creation();
if (sockfd < 0)
{
printf("Socket() failed.\n");
free(pthreadArray);
exit(EXIT_FAILURE);
}
ip_addr = (char *)malloc(NI_MAXHOST * sizeof(char));
//filling up address structure
strcpy(ip_addr, inet_ntoa(*(struct in_addr *)ip_list[i]));
struct thread_args *args_p = malloc(sizeof(struct thread_args));
args_p->ping_sockfd = sockfd;
args_p->ping_addr = &server_addr;
args_p->ping_ip = ip_addr;
printf("Host<%s> added for being pinged...\n", ip_addr);
pthread_create(&pthreadArray[index++], NULL, send_ping, args_p);
}
}
for (i = 0; i < num_thread; i++)
{
int result_code = pthread_join(pthreadArray[i], NULL);
assert(!result_code);
}
return 0;
}
int needed_thread_NO(int optind, int argc, char *argv[])
{
int no = 0;
for (int i = optind; i < argc; ++i)
{
struct hostent host_entity;
struct in_addr **ip_list = dns_lookup(argv[i], &host_entity);
if (ip_list == NULL)
{
printf("Resolving failed.\n");
exit(EXIT_FAILURE);
}
int j;
for (j = 0; ip_list[j] != NULL; j++)
{
}
no += j;
}
return no;
}
// get all Resolutions - DNS lookup
// output => list of all ip address , host_entity
struct in_addr **dns_lookup(char *addr_host, struct hostent *host_entity)
{
struct in_addr **addr_list;
if ((host_entity = gethostbyname(addr_host)) == NULL)
{
//The routine herror() can be used to print an error message describing the failure : HOST_NOT_FOUND - TRY_AGAIN - NO_RECOVERY - NO_DATA
// do some error checking
herror("gethostbyname() failed"); // herror(), NOT perror()
return NULL;
}
addr_list = (struct in_addr **)host_entity->h_addr_list;
return addr_list;
}
// return file descriptor of created socket
int socket_creation()
{
int fd; // file descriptor
fd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if (fd < 0)
{
perror("socket() failed.");
return fd;
}
struct timeval tv_out;
tv_out.tv_sec = timeout;
tv_out.tv_usec = 0;
// set socket options at ip to TTL
if (setsockopt(fd, SOL_IP, IP_TTL, &ttl_val, sizeof(ttl_val)) != 0)
{
printf("Setting socket options to TTL failed!\n");
if (fd > -1)
{
close(fd);
fd = -1;
}
}
// setting timeout of recv setting
if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv_out, sizeof(tv_out)) < 0)
{
perror("Setting socket options to Timeout failed!\n");
if (fd > -1)
{
close(fd);
fd = -1;
}
}
return fd;
}
// ping request
void *send_ping(void *args)
{
struct thread_args *args_p = (struct thread_args *)args;
int ping_sockfd = args_p->ping_sockfd;
struct sockaddr_in *ping_addr = args_p->ping_addr;
char *ping_ip = args_p->ping_ip;
int msg_count = 0, i, addr_len, msg_received_count = 0;
bool packet_sent = true;
struct ping_pkt packet;
struct sockaddr_in recv_addr;
struct timespec time_start, time_end;
long double rtt = 0;
// send icmp packet in an infinite loop
while (continue_pinging)
{
bzero(&packet, sizeof(packet));
packet_sent = true;
packet.header.type = ICMP_ECHO;
packet.header.un.echo.id = getpid(); // id of current process
packet.msg = (char *)malloc((packet_size - sizeof(struct icmphdr)) * sizeof(char));
// filling packet by '1234...'
for (i = 0; i < sizeof(packet.msg) - 1; i++)
packet.msg[i] = i + '0';
packet.msg[i] = 0;
packet.header.un.echo.sequence = msg_count++;
packet.header.checksum = checksum(&packet, sizeof(packet));
usleep(PING_SLEEP_RATE);
//send packet
clock_gettime(CLOCK_MONOTONIC, &time_start);
//sendto => Send packet on ping_socket to peer at ping_addr
//returns the number sent, or -1 for errors.
if (sendto(ping_sockfd, &packet, sizeof(packet), 0, (struct sockaddr *)ping_addr, sizeof(*ping_addr)) <= 0)
{
packet_sent = false;
printf("sendto() failed.\n");
}
addr_len = sizeof(recv_addr);
//receive packet
if (recvfrom(ping_sockfd, &packet, sizeof(packet), 0, (struct sockaddr *)&recv_addr, &addr_len) <= 0 &&
msg_count > 1)
{
printf("recvfrom() failed!\n");
}
else
{
// calculate RTT
clock_gettime(CLOCK_MONOTONIC, &time_end);
double timeElapsed = ((double)(time_end.tv_nsec - time_start.tv_nsec)) / 1000000.0;
rtt = (time_end.tv_sec - time_start.tv_sec) * 1000.0 + timeElapsed;
// if packet was not sent, don't receive
if (packet_sent)
{
if (continue_pinging)
{
if ((packet.header.type == 69 && packet.header.code == 0))
{
printf("Reply from IP<%s> in %Lfms seq=%d.\n", ping_ip, rtt, msg_count);
msg_received_count++;
if (rtt < min_rtt)
min_rtt = rtt;
if (rtt > max_rtt)
max_rtt = rtt;
}
else
{
printf("Error..ICMP type %d, code %d\n", packet.header.type, packet.header.code);
}
}
else
{
msg_count--;
}
}
}
}
if (msg_received_count > 0)
{
float loss = ((msg_count - msg_received_count) / (float)msg_count) * 100.0;
printf("for IP<%s> <%d> packet(s) sent and <%d> packet(s) received, loss = %f%%.\n", ping_ip, msg_count, msg_received_count, loss);
}
return NULL;
}
// Calculate checksum bit
unsigned short checksum(void *b, int len)
{
unsigned short *buf = b;
unsigned int sum = 0;
for (sum = 0; len > 1; len -= 2)
sum += *buf++;
if (len == 1)
sum += *(unsigned char *)buf;
sum = (sum >> 16) + (sum & 0xFFFF);
sum += (sum >> 16);
// One's Complement
return ~sum;
}
// Interrupt handler
void intHandler(int dummy)
{
continue_pinging = false;
printf("\n------------statistics------------\n");
printf("MINIMUM RTT=<%Lf>ms MAXIMUM RTT=<%Lf>ms.\n", min_rtt, max_rtt);
}
bool parse_argv(int argc, char *argv[])
{
if (argc == 1)
return 0;
int c;
int opt_index = 0;
while (c != -1)
{
c = getopt_long(argc, argv, "s:t:", long_options, &opt_index);
if (c == -1)
break;
switch (c)
{
case 0x1:
return false;
break;
// size of packet
case 's':
packet_size = atoi(optarg);
printf("size : %d\n", packet_size);
break;
// timeout
case 't':
timeout = atoi(optarg);
printf("timeout : %d\n", timeout);
break;
default:
printf("\n");
return false;
break;
}
}
return (argc - optind > 0);
}
void usage(char *app)
{
printf("Usage: sudo %s [options] <host(s)>\n", app);
printf(" Options:\n");
printf(" -s|--size <num>|Size of packet (default: %d))\n", DEFAULT_PACKET_SIZE);
printf(" -t|--timeout <num>|Timeout is sec (default: %d)\n", DEFAULT_RECV_TIMEOUT);
}