-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmping.c
More file actions
623 lines (503 loc) · 14.2 KB
/
mping.c
File metadata and controls
623 lines (503 loc) · 14.2 KB
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/*
xkrambler's Massive PING v0.4
03/Sep/2013 Pablo Rodriguez Rey (mr -at- xkr -dot- es)
Last update: 18/Jun/2025
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <netdb.h>
#include <getopt.h>
#include <errno.h>
#include <malloc.h>
#include <pthread.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip_icmp.h>
//#include "debug.h" // debug
#include "list.h"
#define false 0
#define true !0
#define MPING_VERSION "mping v0.4 Jun 18 2025 by mr.xkr - Massive PING"
#define PING_START_SEQ 10000
#define PING_IPV4_MAXHDRSIZE 60
#define PING_PACKETSIZE 32
#define PING_TTL 255
#define PING_MAX_RECVSIZE (PING_PACKETSIZE + PING_IPV4_MAXHDRSIZE)
#define PING_TIMEOUT 1000
#define PING_MSG "P!NG" // no more than (PING_PACKETSIZE - sizeof(struct icmphdr) - sizeof(IPv4address)) = 9 bytes
#define BUFFER_SIZE 4096
// host data
typedef struct {
unsigned int seq;
char *hostname;
char *ip;
int ok;
int shown;
struct timespec tvi;
struct timespec tvf;
} Host;
// ICMP packet
typedef struct {
struct icmphdr hdr;
char msg[PING_PACKETSIZE - sizeof(struct icmphdr)];
} PacketICMP;
// 16 bit one's complement (RFC 792)
unsigned short ping_checksum(void *b, int len) {
unsigned short *buf=b;
unsigned int sum=0;
unsigned short result;
for (sum=0; len > 1; len-=2) sum+=*buf++;
if (len == 1) sum+=*(unsigned char*)buf;
sum=(sum >> 16) + (sum & 0xFFFF);
sum+=(sum >> 16);
result=~sum;
return result;
}
// resolve hostname, return IP string and resolved sockaddr_in
int resolve(const char *address, char **ip, struct sockaddr_in *resolved_addr) {
struct addrinfo hints, *res;
char ipstr[INET_ADDRSTRLEN];
// initialize hints
memset(&hints, 0, sizeof(hints));
hints.ai_family=AF_INET;
hints.ai_socktype=SOCK_RAW;
// resolve
if (getaddrinfo(address, NULL, &hints, &res) != 0) return 0;
// extract IP address
struct sockaddr_in *addr=(struct sockaddr_in *)res->ai_addr;
inet_ntop(AF_INET, &addr->sin_addr, ipstr, sizeof(ipstr));
if (!(*ip)) *ip=strdup(ipstr);
// copy resolved address
if (resolved_addr) memcpy(resolved_addr, addr, sizeof(struct sockaddr_in));
// free info
freeaddrinfo(res);
// ok
return true;
}
// send ICMP ECHO packet
double ping_send(int sd, Host *host) {
PacketICMP packet;
struct sockaddr_in addr_ping;
// resolve hostname
if (!resolve(host->hostname, &host->ip, &addr_ping)) return false;
// prepare packet
bzero(&packet, sizeof(packet));
packet.hdr.type=ICMP_ECHO;
packet.hdr.un.echo.id=getpid();
packet.hdr.un.echo.sequence=host->seq;
// copy message and IP address in the message
strcpy(packet.msg, PING_MSG);
strcat(packet.msg, host->ip);
// update checksum
packet.hdr.checksum=ping_checksum(&packet, sizeof(packet));
#ifdef __DEBUG
printf("--- PING %s %s %s %s\n", host->hostname, host->ip, inet_ntoa(addr_ping.sin_addr), packet.msg);
#endif
// start timming and send PING
clock_gettime(CLOCK_MONOTONIC, &host->tvi);
if (sendto(sd, &packet, sizeof(packet), 0, (struct sockaddr*)&addr_ping, sizeof(addr_ping)) <= 0) return false;
// all fine
return true;
}
// receive ICMP ECHO packet
double ping_recv(int sd, PacketICMP *packet, struct sockaddr_in *addr, int timeout) {
char *buffer=malloc(sizeof(char)*PING_MAX_RECVSIZE);
// receive ping
unsigned int len=sizeof(struct sockaddr_in);
if (recvfrom(sd, buffer, PING_MAX_RECVSIZE, 0, (struct sockaddr*)addr, &len) > 0) {
int ip_len=((buffer[0] & 0x0F)<<2); // get ip header length, we need discard it
memcpy(packet, buffer+ip_len, sizeof(PacketICMP));
free(buffer);
return true;
}
// timeout
free(buffer);
return false;
}
// main program
int main(int argc, char *argv[]) {
int i;
int sd;
unsigned int retry=1;
unsigned int ping_seq=PING_START_SEQ;
unsigned int ping_timeout=PING_TIMEOUT;
unsigned char ping_ttl=PING_TTL;
unsigned int show_hostnames=true;
unsigned int only_summary=false;
unsigned int follow=false;
struct protoent *proto=NULL;
unsigned int content_start=0;
size_t content_size;
char buffer[BUFFER_SIZE];
unsigned int loading_hosts=true;
struct timespec time_start, time_follow, time_recv, time_end;
pthread_mutex_t running_thread=PTHREAD_MUTEX_INITIALIZER;
List *hosts;
// aux: search host node by sequence number and IP address
Node *host_search(List *hosts, char *ip, unsigned int seq) {
Node *n=hosts->first;
while (n) {
Host *host=(Host *)n->e;
if (!host->ok && host->ip && host->seq == seq && (strcmp(host->ip, ip) == 0)) return n;
n=n->sig;
}
return NULL;
}
// aux: add a host to the list
Host *host_add(char *hostname) {
Host *host=malloc(sizeof(Host));
host->hostname=strdup(hostname);
host->ip=NULL;
//host->he=NULL;
host->ok=false;
host->shown=false;
listNodeAdd(hosts, host);
return host;
}
// aux: delete host from list
void host_del(List *hosts, Node *n) {
Host *host=(Host *)n->e;
if (host) {
if (host->hostname) free(host->hostname);
if (host->ip) free(host->ip);
free(host);
}
listNodeDel(hosts, n);
}
// aux: convert timespec interval to time difference (in miliseconds)
double time_diff(struct timespec begin, struct timespec end) {
double s=(end.tv_sec - begin.tv_sec)*1000;
double ms=(end.tv_nsec - begin.tv_nsec)/1000000.0;
return s+ms;
}
// aux: sequence w/o increment
unsigned int sequence(int inc) {
ping_seq=(ping_seq+inc)%65535;
return ping_seq;
}
// aux: ping to host
int ping_host(Host *host) {
if (!host->ip) host->seq=sequence(+1);
if (ping_send(sd, host) < 0) {
perror("Failed to send ping");
return false;
}
return true;
}
// aux: display syntax and usage
void help() {
printf(MPING_VERSION);
printf("\n");
printf("Usage: %s [options] [target ...]\n", argv[0]);
printf(" target Targets can be Hosts or IPs to be resolved. Also can be entered as input pipe.\n");
printf(" -t timeout Timeout in miliseconds\n");
printf(" -r retries Number of retries for down hosts\n");
printf(" -f Continue retrying pinging hosts\n");
printf(" -n Do not show hostnames, only IP addresses\n");
printf(" -s Only show summary\n");
printf(" -v Show version\n");
printf(" -h Show this help\n");
printf("\n");
}
// option list and parse
const char* const optstring="hfnsvr:t:";
const struct option longopts[]={
{"help", 0, NULL, 'h'},
{"follow", 0, NULL, 'f'},
{"version", 0, NULL, 'v'},
{"nohosts", 0, NULL, 'n'},
{"summary", 0, NULL, 's'},
{"retry", 1, NULL, 'r'},
{"timeout", 1, NULL, 't'},
{0,0,0,0}
};
while (true) {
int r=getopt_long(argc, argv, optstring, longopts, NULL);
if (r < 0) break;
switch (r) {
case 'h':
help();
return 0;
case 'v':
printf(MPING_VERSION);
printf("\n");
return 0;
case 'n':
show_hostnames=false;
break;
case 's':
only_summary=true;
break;
case 'f':
follow=true;
break;
case 't':
if (!optarg) {
printf("ERROR: -t option does not specify timeout\n");
return 1;
}
ping_timeout=atoi(optarg);
if (ping_timeout < 1) ping_timeout=1;
break;
case 'r':
if (!optarg) {
printf("ERROR: -r option does not specify retry times\n");
return 1;
}
retry=atoi(optarg);
if (retry < 1) retry=1;
break;
default:
return 1;
}
}
// frame reception thread
void *thread_recv(void *none) {
// declarations
PacketICMP *packet=malloc(sizeof(PacketICMP));
struct sockaddr_in r_addr;
struct timespec time_actual;
// do the work!
int hosts_ok=0;
do {
// while we have hosts, we search for ping
if (hosts->num) while (ping_recv(sd, packet, &r_addr, ping_timeout)) {
#ifdef __DEBUG
printf("--- addr=%s id=%d seq=%d msg=%s ---\n", inet_ntoa(r_addr.sin_addr), packet->hdr.un.echo.id, packet->hdr.un.echo.sequence, packet->msg);
debug((void *)packet, sizeof(PacketICMP));
#endif
// search sequence correspondence
Node *n=host_search(hosts, inet_ntoa(r_addr.sin_addr), packet->hdr.un.echo.sequence);
if (n) {
Host *host=(Host *)n->e;
clock_gettime(CLOCK_MONOTONIC, &host->tvf);
host->ok=true;
hosts_ok++;
}
}
// no more hosts pending, finish
if (hosts_ok >= hosts->num) break;
// if no more pending hosts, start count and finish loop when timeout expires
if (!loading_hosts) {
// timeout reached
clock_gettime(CLOCK_MONOTONIC, &time_actual);
if (time_diff(time_recv, time_actual) >= ping_timeout) {
#ifdef __DEBUG
printf("TIMEOUT %dms\n", ping_timeout);
#endif
break;
}
}
// wait for more hosts to be loaded
usleep(1000);
} while (true);
// free ICMP packet
free(packet);
// thread sync and finished
pthread_exit((void *)0);
}
// application timming
clock_gettime(CLOCK_MONOTONIC, &time_start);
clock_gettime(CLOCK_MONOTONIC, &time_recv);
// set start sequence (limit 16-bits)
sequence(time_start.tv_sec);
// start mark
printf("START retry=%d timeout=%dms seq=%d\n", retry, ping_timeout, ping_seq);
// new hosts list
hosts=listNew();
// obtain hostnames from parameters...
if (optind < argc) {
while (optind < argc) {
host_add(argv[optind++]);
}
// ...or get them from stdin
} else {
// allocate content
content_size=0;
char *content=malloc(0);
if (content == NULL) {
perror("Failed to allocate content");
return 1;
}
// read from stdin
int buffer_len;
while ((int)(buffer_len=fread(buffer, 1, BUFFER_SIZE, stdin))) {
if (buffer_len < 0) break;
// allocate in the buffer
char *old=content;
content_size+=buffer_len;
content=realloc(content, content_size);
if (content == NULL) {
perror("Failed to reallocate content");
free(old);
return 3;
}
memcpy(content+content_size-buffer_len, buffer, buffer_len);
// process buffer lines
for (i=content_start; i < content_size; i++) {
if (content[i] == '\r') content[i]=0;
else if (content[i] == '\n') {
content[i]=0;
// line found
if (strlen(content+content_start) > 0) {
host_add(content+content_start);
}
content_start=i+1;
}
}
}
// free content buffer
free(content);
// check stdin
if (ferror(stdin)) {
perror("Error reading from stdin");
return 4;
}
}
// if we have hosts to ping
if (hosts->num) {
// make ICMP socket (requires root)
proto=getprotobyname("ICMP");
sd=socket(PF_INET, SOCK_RAW, proto->p_proto);
if (sd < 0) {
perror("Could not create RAW socket, are you root?");
return 4;
}
if (setsockopt(sd, SOL_IP, IP_TTL, &ping_ttl, sizeof(ping_ttl)) != 0) {
perror("Could not set socket TTL");
return 5;
}
if (fcntl(sd, F_SETFL, O_NONBLOCK) != 0) {
perror("Could not set nonblock I/O");
return 6;
}
// follow
do {
// follow start
clock_gettime(CLOCK_MONOTONIC, &time_follow);
int num_hosts_ok=0;
// retry loop
for (i=0; i < retry; i++) {
// if there are pending hosts
#ifdef __DEBUG
printf("RETRY %d/%d\n", i+1, retry);
#endif
// we are going to load hosts
loading_hosts=true;
pthread_mutex_lock(&running_thread);
// start timeout counter again
clock_gettime(CLOCK_MONOTONIC, &time_recv);
// create receive thread
pthread_t thread;
if (pthread_create((pthread_t *)&thread, NULL, &thread_recv, NULL)) {
perror("Cannot create receive thread");
return 7;
}
// ping hosts
{
Node *n=hosts->first;
while (n) {
Host *host=(Host *)n->e;
if (!host->ok) {
if (!ping_host(host)) return 8;
}
n=n->sig;
}
}
// sync thread
loading_hosts=false;
pthread_mutex_unlock(&running_thread);
pthread_join(thread, NULL);
// check OK
num_hosts_ok=0;
{
Node *n=hosts->first;
while (n) {
Host *host=(Host *)n->e;
if (host->ok) {
num_hosts_ok++;
if (!host->shown) {
host->shown=true;
double ms=time_diff(host->tvi, host->tvf);
if (!only_summary) {
if (show_hostnames) {
printf("OK %s\t%s\t%5.4fms\n", host->hostname, (host->ip?host->ip:""), ms);
} else {
printf("OK %s\t%5.4fms\n", (host->ip?host->ip:host->hostname), ms);
}
}
}
}
n=n->sig;
}
}
// no more retries needed
if (num_hosts_ok >= hosts->num) break;
}
// remaining host list are timeout or host could not be resolved
int num_hosts_ko=0;
{
Node *n=hosts->first;
while (n) {
Host *host=(Host *)n->e;
if (!host->ok) {
num_hosts_ko++;
if (!only_summary) {
if (host->ip) {
if (show_hostnames) {
printf("TIMEOUT %s\t%s\n", host->hostname, (host->ip?host->ip:""));
} else {
printf("TIMEOUT %s\n", (host->ip?host->ip:host->hostname));
}
} else {
printf("UNKNOWN %s\n", host->hostname);
}
}
}
host->shown=false; // reiniciar
host->ok=false; // reiniciar
n=n->sig;
}
}
// show total timming
clock_gettime(CLOCK_MONOTONIC, &time_end);
printf("%s %5.2fms hosts=%d ok=%d ko=%d\n", (follow?"STATS":"END"), time_diff(time_start, time_end), hosts->num, num_hosts_ok, num_hosts_ko);
// if follow, wait at least timeout to repeat
if (follow) {
struct timespec time_actual;
clock_gettime(CLOCK_MONOTONIC, &time_actual);
double ping_ms=time_diff(time_follow, time_actual);
if (ping_ms < ping_timeout) usleep((ping_timeout-ping_ms)*1000);
}
// repeat if follow
} while (follow);
// close socket
close(sd);
}
// free all host nodes
{
Node *n=hosts->first;
while (n) {
host_del(hosts, n);
n=hosts->first;
}
}
// free list
listFree(hosts);
// everything was fine
return 0;
}