-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsend-echo-request.c
566 lines (464 loc) · 16.4 KB
/
send-echo-request.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
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
/***********************************************************************
* send-echo-request - send ICMP(v6) echo request without waiting for answers
* Copyright (C) 2014,2020 Hans Ulrich Niedermann
*
* 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; either version 2 of the License, or
* (at your option) any later version.
*
* 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
**********************************************************************/
/*
* Usage: send-echo-request [OPTIONS]... ADDR...
*
* Send ICMPv6 (or ICMP) echo requests to all ADDRs given with a
* constant delay of 0.5s between each packet sent.
*
* If no ADDRs are given at all, send-echo-request aborts.
*
* Options:
* -n --dry-run do not actually send any packets
* --loop loop after sending packet to last addr in list
* -q --quiet operate without generating output
* -v --verbose generate more output (repeat for more output)
*
* --help print this usage message and exit
* --version print program version information and exit
*
* Address examples:
* 127.0.0.1 Numerical IPv4 address
* ::1 Numerical IPv6 address
*
* Verbosity levels:
* -qq no output whatsoever until program crashes
* -q report only packet send errors
* <default> also report current loop iteration number
* -v also report each address packet is sent to
* -vv also report address details
*
* Exit status:
* 0 problem free operation
* non-0 if there have been any errors sending packets or otherwise
* <none> in --loop mode, send-echo-request does not exit.
*/
#define PROG "send-echo-request"
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <netinet/ip6.h>
#include <netinet/icmp6.h>
#include <errno.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <time.h>
#include <unistd.h>
#include <stdio.h>
#include "git-version.h"
#include "usage-msg.h"
/***********************************************************************
* Compiler infrastructure
***********************************************************************/
#define IGNORE_RESULT(statement) while (0) { if (statement) {} }
/***********************************************************************
* Global settings, can be changed by command line options
***********************************************************************/
typedef enum {
VERB_MUTE = 0,
VERB_QUIET = 1,
VERB_NORMAL = 2,
VERB_VERBOSE = 3,
VERB_VERY = 4
} verbosity_T;
/* These globals can be set via cmdline options */
static bool dry_run = false;
static bool do_loop = false;
static verbosity_T verbosity = VERB_NORMAL;
/***********************************************************************
* Output messages for --help and --version
***********************************************************************/
static
void print_usage()
{
IGNORE_RESULT(write(STDOUT_FILENO, USAGE_MSG, strlen(USAGE_MSG)));
}
#define VERSION_MSG "" \
PROG " " GIT_VERSION_STR "\n" \
"Copyright (C) 2014 Hans Ulrich Niedermann\n" \
"License GPLv2+: GNU GPL version 2 or later\n" \
""
static
void print_version()
{
IGNORE_RESULT(write(STDOUT_FILENO, VERSION_MSG, strlen(VERSION_MSG)));
}
/***********************************************************************
* Output more or less verbose messages to user
***********************************************************************/
#define normalf(fmt, ...) \
messagef_i(VERB_NORMAL, "* " fmt "\n", ##__VA_ARGS__)
#define verbosef(fmt, ...) \
messagef_i(VERB_VERBOSE, " * " fmt "\n", ##__VA_ARGS__)
#define veryverbosef(fmt, ...) \
messagef_i(VERB_VERY, " * " fmt "\n", ##__VA_ARGS__)
static
void messagef_i(const verbosity_T msg_verb, const char *const format, ...)
__attribute__(( unused ))
__attribute__(( format(printf, 2, 3) ));
static
void messagef_i(const verbosity_T msg_verb, const char *const format, ...)
{
va_list ap;
if (verbosity >= msg_verb) {
va_start(ap, format);
vfprintf(stdout, format, ap);
fflush(stdout);
va_end(ap);
}
}
#define quietfe(MSG) quietfe_i(strlen(PROG ": " MSG ": "), \
PROG ": " MSG ": ")
static
void quietfe_i(const size_t msglen, const char *const msg)
{
if (verbosity >= VERB_QUIET) {
const int local_errno = errno; /* read global var errno */
const char *errmsg = strerror(local_errno);
IGNORE_RESULT(write(STDERR_FILENO, msg, msglen));
IGNORE_RESULT(write(STDERR_FILENO, errmsg, strlen(errmsg)));
IGNORE_RESULT(write(STDERR_FILENO, "\n", 1));
}
}
/***********************************************************************
* Output error message and exit program
***********************************************************************/
#define error_exit(MSG) error_exit_i(strlen(PROG ": " MSG "\n"), \
PROG ": " MSG "\n")
static
void error_exit_i(const size_t msglen, const char *const msg)
__attribute__((noreturn));
/* this should even work when realloc or malloc have failed */
static
void error_exit_i(const size_t msglen, const char *const msg)
{
IGNORE_RESULT(write(STDERR_FILENO, msg, msglen));
exit(EXIT_FAILURE);
}
#define error_exitf(fmt, ...) \
do { \
fprintf(stderr, PROG ": " fmt "\n", ##__VA_ARGS__); \
exit(EXIT_FAILURE); \
} while (0)
/***********************************************************************
* Prepare echo request packets and send them
***********************************************************************/
static
uint16_t icmp_checksum(const uint16_t *const data, const size_t byte_sz)
{
if (0 != (byte_sz & 1)) {
error_exitf("icmp_checksum: number of bytes %zu must be even",
byte_sz);
}
uint32_t accu = 0;
for (size_t i=0; i < (byte_sz >> 1); ++i) {
const uint32_t val32 = data[i];
accu = accu + val32;
}
/* Fold 32-bit sum to 16 bits */
while (accu >> 16) {
accu = (accu & 0xffff) + (accu >> 16);
}
const uint16_t checksum = ~accu;
return checksum;
}
#define IDENTIFIER 0x2342
static
int send_ping4(struct sockaddr_in *dest_addr,
const uint16_t sequenceno, const char *const dest_str)
{
verbosef("send_ping4 %s", dest_str);
veryverbosef("%-13s %d", "sin_family", dest_addr->sin_family);
veryverbosef("%-13s %u.%u.%u.%u", "sin_addr",
(ntohl(dest_addr->sin_addr.s_addr) & 0xff000000) >> 24,
(ntohl(dest_addr->sin_addr.s_addr) & 0x00ff0000) >> 16,
(ntohl(dest_addr->sin_addr.s_addr) & 0x0000ff00) >> 8,
(ntohl(dest_addr->sin_addr.s_addr) & 0x000000ff) >> 0
);
veryverbosef("%-13s %d", "sin_port", ntohs(dest_addr->sin_port));
if (dry_run) {
return 0;
}
const int sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if (sock < 0) {
quietfe("socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)");
return 1;
}
/* compose ICMP packet */
struct icmphdr hdr;
bzero(&hdr, sizeof(hdr));
hdr.type = ICMP_ECHO; /* ICMP echo request */
hdr.code = 0;
hdr.checksum = 0; /* required for actual checksum calculation */
hdr.un.echo.id = htons(IDENTIFIER); /* identifier */
hdr.un.echo.sequence = htons(sequenceno); /* sequence no */
/* Fill in the ICMP checksum */
hdr.checksum = icmp_checksum((uint16_t *)&hdr, sizeof(hdr));
/* send the echo request packet */
const ssize_t sent_bytes =
sendto(sock,
&hdr, sizeof(hdr),
0 /* no MSG_* flags */,
(struct sockaddr *)dest_addr, sizeof(*dest_addr));
if (sent_bytes != sizeof(hdr)) {
error_exit("sendto");
}
close(sock);
return 0;
}
static
int send_ping6(struct sockaddr_in6 *dest_addr,
const uint16_t sequenceno, const char *const dest_str)
{
verbosef("send_ping6 %s", dest_str);
veryverbosef("%-13s %d", "sin6_family", dest_addr->sin6_family);
veryverbosef("%-13s %d", "sin6_port", ntohs(dest_addr->sin6_port));
veryverbosef("%-13s 0x%08x", "sin6_flowinfo", dest_addr->sin6_flowinfo);
veryverbosef("%-13s "
"%02x%02x:%02x%02x:%02x%02x:%02x%02x:"
"%02x%02x:%02x%02x:%02x%02x:%02x%02x",
"sin6_addr",
dest_addr->sin6_addr.s6_addr[0x0],
dest_addr->sin6_addr.s6_addr[0x1],
dest_addr->sin6_addr.s6_addr[0x2],
dest_addr->sin6_addr.s6_addr[0x3],
dest_addr->sin6_addr.s6_addr[0x4],
dest_addr->sin6_addr.s6_addr[0x5],
dest_addr->sin6_addr.s6_addr[0x6],
dest_addr->sin6_addr.s6_addr[0x7],
dest_addr->sin6_addr.s6_addr[0x8],
dest_addr->sin6_addr.s6_addr[0x9],
dest_addr->sin6_addr.s6_addr[0xa],
dest_addr->sin6_addr.s6_addr[0xb],
dest_addr->sin6_addr.s6_addr[0xc],
dest_addr->sin6_addr.s6_addr[0xd],
dest_addr->sin6_addr.s6_addr[0xe],
dest_addr->sin6_addr.s6_addr[0xf]
);
veryverbosef("%-13s 0x%08x", "sin6_scope_id", dest_addr->sin6_scope_id);
if (dry_run) {
return 0;
}
const int sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
if (sock < 0) {
quietfe("socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)");
return 1;
}
/* compose ICMPv6 packet */
struct icmp6_hdr hdr;
bzero(&hdr, sizeof(hdr));
hdr.icmp6_type = ICMP6_ECHO_REQUEST;
hdr.icmp6_code = 0;
hdr.icmp6_dataun.icmp6_un_data16[0] = htons(IDENTIFIER); /* identifier */
hdr.icmp6_dataun.icmp6_un_data16[1] = htons(sequenceno); /* sequence no */
/* Have the system fill in hdr.icmp6_cksum for us */
int cksum_ofs = offsetof(struct icmp6_hdr, icmp6_cksum);
const socklen_t cksum_ofs_sz = sizeof(cksum_ofs);
const int sso_ret = setsockopt(sock, SOL_RAW, IPV6_CHECKSUM,
&cksum_ofs, cksum_ofs_sz);
if (sso_ret < 0) {
quietfe("setsockopt(SOL_RAW, IPV6_CHECKSUM)");
return 1;
}
/* send the echo request packet */
const ssize_t sent_bytes =
sendto(sock,
&hdr, sizeof(hdr),
0 /* no MSG_* flags */,
(struct sockaddr *)dest_addr, sizeof(*dest_addr));
if (sent_bytes < 0) {
quietfe("sendto");
return 1;
} else if (((size_t)sent_bytes) < sizeof(hdr)) {
error_exitf("sendto only sent %zu bytes of %zu",
sent_bytes, sizeof(hdr));
}
close(sock);
return 0;
}
/***********************************************************************
* main program
***********************************************************************/
typedef struct {
const char *addr_str;
struct sockaddr_storage sas;
struct timespec delay;
} task_T;
static
void main_loop(const task_T *const tasks, const size_t task_cnt)
{
/* endless main loop unless !do_loop */
for (uint16_t sequenceno=0; ; ++sequenceno) {
normalf("sequenceno %u", sequenceno);
size_t ping_errors = 0;
for (size_t i=0; i<task_cnt; ++i) {
switch (tasks[i].sas.ss_family) {
case AF_INET:
if (send_ping4((struct sockaddr_in *) &tasks[i].sas,
sequenceno, tasks[i].addr_str)) {
++ping_errors;
}
break;
case AF_INET6:
if (send_ping6((struct sockaddr_in6 *) &tasks[i].sas,
sequenceno, tasks[i].addr_str)) {
++ping_errors;
}
break;
default:
error_exitf("Invalid ss_family %u", tasks[i].sas.ss_family);
}
struct timespec rem_ts;
nanosleep(&tasks[i].delay, &rem_ts);
/* ignore rem_ts value */
}
if (ping_errors) {
normalf("ping_errors %zu/%zu", ping_errors, task_cnt);
}
if (!do_loop) {
if (ping_errors) {
exit(EXIT_FAILURE);
}
break;
}
}
}
int main(int argc, char *argv[])
{
size_t task_cnt = 0;
task_T *tasks = NULL;
/*** parse command line ***/
#define enlarge_array(addrstr) \
do { \
const size_t factor_a = sizeof(tasks[0]); \
const size_t factor_b = task_cnt + 1; \
const size_t new_size = factor_a * factor_b; \
if ((new_size < factor_a) || (new_size < factor_b)) { \
error_exit("realloc size overflow"); \
} \
tasks = realloc(tasks, new_size); \
if (!tasks) { \
quietfe("realloc"); \
exit(EXIT_FAILURE); \
} \
bzero(&tasks[task_cnt], sizeof(tasks[task_cnt])); \
tasks[task_cnt].addr_str = addrstr; \
} while (0)
for (int i=1; i<argc; ++i) {
const char *const arg = argv[i];
union {
struct sockaddr_in sin;
struct sockaddr_in6 sin6;
} u;
bzero(&u, sizeof(u));
/* These blocks must either
* exit to exit the program
* return to exit the program
* <do nothing> when having added a task (causing appropriate handling)
* continue when not having added a task
*/
if (0 == strcmp("--help", arg)) {
print_usage();
return 0;
} else if (0 == strcmp("--version", arg)) {
print_version();
return 0;
} else if ((0 == strcmp("--dry-run", arg)) || (0 == strcmp("-n", arg))) {
dry_run = true;
continue;
} else if (0 == strcmp("--loop", arg)) {
do_loop = true;
continue;
} else if (0 == strcmp("-qq", arg)) {
switch (verbosity) {
case VERB_NORMAL:
verbosity = VERB_MUTE;
break;
default:
error_exit("Illegal use of -qq");
}
continue;
} else if ((0 == strcmp("--quiet", arg)) || (0 == strcmp("-q", arg))) {
switch (verbosity) {
case VERB_NORMAL:
verbosity = VERB_QUIET;
break;
case VERB_QUIET:
verbosity = VERB_MUTE;
break;
default:
error_exit("Illegal use of --quiet/-q");
}
continue;
} else if (0 == strcmp("-vv", arg)) {
switch (verbosity) {
case VERB_NORMAL:
verbosity = VERB_VERY;
break;
default:
error_exit("Illegal use of -vv");
}
continue;
} else if ((0 == strcmp("--verbose", arg)) || (0 == strcmp("-v", arg))) {
switch (verbosity) {
case VERB_NORMAL:
verbosity = VERB_VERBOSE;
break;
case VERB_VERBOSE:
verbosity = VERB_VERY;
break;
default:
error_exit("Illegal use of --verbose/-v");
}
continue;
} else if (1 == inet_pton(AF_INET6, arg, &u.sin6.sin6_addr)) {
enlarge_array(arg);
u.sin6.sin6_family = AF_INET6;
memcpy(&tasks[task_cnt].sas, &u.sin6, sizeof(u.sin6));
} else if (1 == inet_pton(AF_INET, arg, &u.sin.sin_addr)) {
enlarge_array(arg);
u.sin.sin_family = AF_INET;
memcpy(&tasks[task_cnt].sas, &u.sin, sizeof(u.sin));
} else {
error_exitf("Cannot parse address: %s", arg);
}
tasks[task_cnt].delay.tv_sec = 0;
tasks[task_cnt].delay.tv_nsec = 500000000;
++task_cnt;
}
#undef enlarge_array
if (task_cnt == 0) {
error_exit("No adress(es) given");
}
/*** main loop ***/
main_loop(tasks, task_cnt);
return 0;
}
/*
* Local Variables:
* indent-tabs-mode: nil
* End:
*/