-
Notifications
You must be signed in to change notification settings - Fork 2k
/
sc_icmpv6_echo.c
351 lines (295 loc) · 10.1 KB
/
sc_icmpv6_echo.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
/*
* Copyright (C) 2015 Martine Lenders <mlenders@inf.fu-berlin.de>
*
* This file is subject to the terms and conditions of the GNU Lesser General
* Public License v2.1. See the file LICENSE in the top level directory for
* more details.
*/
/**
* @ingroup sys_shell_commands
* @{
*
* @file
*
* @author Martine Lenders <mlenders@inf.fu-berlin.de>
*/
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#ifdef MODULE_GNRC_ICMPV6
#include "byteorder.h"
#include "net/gnrc/icmpv6.h"
#include "net/ipv6/addr.h"
#ifdef MODULE_GNRC_IPV6_NIB
#include "net/gnrc/ipv6/nib/nc.h"
#endif
#include "net/gnrc/ipv6/hdr.h"
#include "net/gnrc/netif.h"
#include "net/gnrc.h"
#include "thread.h"
#include "utlist.h"
#include "xtimer.h"
static uint16_t id = 0x53;
static uint16_t min_seq_expected = 0;
static uint16_t max_seq_expected = 0;
static char ipv6_str[IPV6_ADDR_MAX_STR_LEN];
static void usage(char **argv)
{
printf("%s [<count>] <ipv6 addr>[%%<interface>] [<payload_len>] [<delay in ms>] [<stats interval>]\n", argv[0]);
puts("defaults:");
puts(" count = 3");
puts(" interface = first interface if only one present, only needed for link-local addresses");
puts(" payload_len = 4");
puts(" delay = 1000");
puts(" stats interval = count");
}
void _set_payload(icmpv6_echo_t *hdr, size_t payload_len)
{
size_t i = 0;
uint8_t *payload = (uint8_t *)(hdr + 1);
while (i < payload_len) {
if (id > 0xff) {
payload[i] = (uint8_t)(id >> 8);
payload[i + 1] = (uint8_t)(id & 0xff);
i += 2;
}
else {
payload[i++] = (uint8_t)(id & 0xff);
}
}
if (i > payload_len) { /* when id > 0xff and payload_len is odd */
payload[payload_len - 1] = id >> 8;
}
}
static inline bool _expected_seq(uint16_t seq)
{
/* take integer overflows in account */
if (min_seq_expected > max_seq_expected) {
return (seq >= min_seq_expected) || (seq <= max_seq_expected);
}
else {
return (seq >= min_seq_expected) && (seq <= max_seq_expected);
}
}
int _handle_reply(gnrc_pktsnip_t *pkt, uint32_t time)
{
gnrc_pktsnip_t *ipv6, *icmpv6;
ipv6_hdr_t *ipv6_hdr;
icmpv6_echo_t *icmpv6_hdr;
uint16_t seq;
ipv6 = gnrc_pktsnip_search_type(pkt, GNRC_NETTYPE_IPV6);
icmpv6 = gnrc_pktsnip_search_type(pkt, GNRC_NETTYPE_ICMPV6);
if ((ipv6 == NULL) || (icmpv6 == NULL)) {
puts("error: IPv6 header or ICMPv6 header not found in reply");
return 0;
}
ipv6_hdr = ipv6->data;
icmpv6_hdr = icmpv6->data;
seq = byteorder_ntohs(icmpv6_hdr->seq);
if ((byteorder_ntohs(icmpv6_hdr->id) == id) && _expected_seq(seq)) {
if (seq <= min_seq_expected) {
min_seq_expected++;
}
printf("%u bytes from %s: id=%" PRIu16 " seq=%" PRIu16 " hop limit=%u time = %"
PRIu32 ".%03" PRIu32 " ms\n", (unsigned) icmpv6->size,
ipv6_addr_to_str(ipv6_str, &(ipv6_hdr->src), sizeof(ipv6_str)),
byteorder_ntohs(icmpv6_hdr->id), seq, (unsigned)ipv6_hdr->hl,
time / US_PER_MS, time % US_PER_MS);
#ifdef MODULE_GNRC_IPV6_NIB
gnrc_ipv6_nib_nc_mark_reachable(&ipv6_hdr->src);
#endif
}
else {
puts("error: unexpected parameters");
return 0;
}
return 1;
}
static void _print_stats(char *addr_str, int success, int count, uint64_t total_time,
uint64_t sum_rtt, uint32_t min_rtt, uint32_t max_rtt)
{
printf("--- %s ping statistics ---\n", addr_str);
if (success > 0) {
uint32_t avg_rtt = (uint32_t)(sum_rtt / success); /* get average */
printf("%d packets transmitted, %d received, %d%% packet loss, time %"
PRIu32 ".06%" PRIu32 " s\n", count, success,
(100 - ((success * 100) / count)),
(uint32_t)total_time / US_PER_SEC, (uint32_t)total_time % US_PER_SEC);
printf("rtt min/avg/max = "
"%" PRIu32 ".%03" PRIu32 "/"
"%" PRIu32 ".%03" PRIu32 "/"
"%" PRIu32 ".%03" PRIu32 " ms\n",
min_rtt / US_PER_MS, min_rtt % US_PER_MS,
avg_rtt / US_PER_MS, avg_rtt % US_PER_MS,
max_rtt / US_PER_MS, max_rtt % US_PER_MS);
}
else {
printf("%d packets transmitted, 0 received, 100%% packet loss\n", count);
}
}
int _icmpv6_ping(int argc, char **argv)
{
int count = 3, success = 0, remaining, stat_interval = 0, stat_counter = 0;
size_t payload_len = 4;
uint32_t delay = 1 * MS_PER_SEC;
char *addr_str;
ipv6_addr_t addr;
kernel_pid_t src_iface;
msg_t msg;
gnrc_netreg_entry_t my_entry = GNRC_NETREG_ENTRY_INIT_PID(ICMPV6_ECHO_REP,
sched_active_pid);
uint32_t min_rtt = UINT32_MAX, max_rtt = 0;
uint64_t sum_rtt = 0;
uint64_t ping_start;
int param_offset = 0;
if (argc < 2) {
usage(argv);
return 1;
}
else if ((argc > 2) && ((count = atoi(argv[1])) > 0)) {
param_offset = 1;
}
else {
count = 3;
}
stat_interval = count;
addr_str = argv[1 + param_offset];
if (argc > (2 + param_offset)) {
payload_len = atoi(argv[2 + param_offset]);
}
if (argc > (3 + param_offset)) {
delay = atoi(argv[3 + param_offset]);
}
if (argc > (4 + param_offset)) {
stat_interval = atoi(argv[4 + param_offset]);
}
if ((int)payload_len < 0) {
usage(argv);
return 1;
}
src_iface = ipv6_addr_split_iface(addr_str);
if (src_iface == -1) {
src_iface = KERNEL_PID_UNDEF;
}
if (ipv6_addr_from_str(&addr, addr_str) == NULL) {
puts("error: malformed address");
return 1;
}
if (ipv6_addr_is_link_local(&addr) || (src_iface != KERNEL_PID_UNDEF)) {
size_t ifnum = gnrc_netif_numof();
if (src_iface == KERNEL_PID_UNDEF) {
if (ifnum == 1) {
src_iface = gnrc_netif_iter(NULL)->pid;
}
else {
puts("error: link local target needs interface parameter (use \"<address>%<ifnum>\")\n");
return 1;
}
}
else {
if (gnrc_netif_get_by_pid(src_iface) == NULL) {
printf("error: %"PRIkernel_pid" is not a valid interface.\n", src_iface);
return 1;
}
}
}
if (gnrc_netreg_register(GNRC_NETTYPE_ICMPV6, &my_entry) < 0) {
puts("error: network registry is full");
return 1;
}
remaining = count;
ping_start = xtimer_now_usec64();
while ((remaining--) > 0) {
gnrc_pktsnip_t *pkt, *payload;
uint32_t start, timeout = 1 * US_PER_SEC;
payload = gnrc_icmpv6_echo_build(ICMPV6_ECHO_REQ, id,
++max_seq_expected,
NULL, payload_len);
if (payload == NULL) {
puts("error: packet buffer full");
continue;
}
_set_payload(payload->data, payload_len);
pkt = gnrc_ipv6_hdr_build(payload, NULL, &addr);
if (pkt == NULL) {
puts("error: packet buffer full");
gnrc_pktbuf_release(payload);
continue;
}
if (src_iface != KERNEL_PID_UNDEF) {
payload = pkt;
pkt = gnrc_pktbuf_add(payload, NULL, sizeof(gnrc_netif_hdr_t),
GNRC_NETTYPE_NETIF);
if (pkt == NULL) {
puts("error: packet buffer full");
gnrc_pktbuf_release(payload);
continue;
}
gnrc_netif_hdr_init(((gnrc_netif_hdr_t *)pkt->data), 0, 0);
((gnrc_netif_hdr_t *)pkt->data)->if_pid = src_iface;
}
start = xtimer_now_usec();
if (!gnrc_netapi_dispatch_send(GNRC_NETTYPE_IPV6, GNRC_NETREG_DEMUX_CTX_ALL, pkt)) {
puts("error: unable to send ICMPv6 echo request\n");
gnrc_pktbuf_release(pkt);
continue;
}
/* TODO: replace when #4219 was fixed */
if (xtimer_msg_receive_timeout64(&msg, (uint64_t)timeout) >= 0) {
uint32_t stop;
switch (msg.type) {
case GNRC_NETAPI_MSG_TYPE_RCV:
stop = xtimer_now_usec() - start;
gnrc_pktsnip_t *pkt = msg.content.ptr;
success += _handle_reply(pkt, stop);
gnrc_pktbuf_release(pkt);
if (stop > max_rtt) {
max_rtt = stop;
}
if (stop < min_rtt) {
min_rtt = stop;
}
sum_rtt += stop;
break;
default:
/* requeue wrong packets */
msg_send(&msg, thread_getpid());
break;
}
}
else {
puts("ping timeout");
}
while (msg_try_receive(&msg) > 0) {
if (msg.type == GNRC_NETAPI_MSG_TYPE_RCV) {
printf("dropping additional response packet (probably caused by duplicates)\n");
gnrc_pktsnip_t *pkt = msg.content.ptr;
gnrc_pktbuf_release(pkt);
}
}
if (remaining > 0) {
xtimer_usleep64(delay * US_PER_MS);
}
if ((++stat_counter == stat_interval) || (remaining == 0)) {
uint64_t total_time = xtimer_now_usec64() - ping_start;
_print_stats(addr_str, success, (count - remaining), total_time, sum_rtt, min_rtt,
max_rtt);
stat_counter = 0;
}
}
max_seq_expected = 0;
id++;
gnrc_netreg_unregister(GNRC_NETTYPE_ICMPV6, &my_entry);
while (msg_try_receive(&msg) > 0) {
if (msg.type == GNRC_NETAPI_MSG_TYPE_RCV) {
printf("dropping additional response packet (probably caused by duplicates)\n");
gnrc_pktsnip_t *pkt = msg.content.ptr;
gnrc_pktbuf_release(pkt);
}
}
return success ? 0 : 1;
}
#endif
/**
* @}
*/