This repository has been archived by the owner on Mar 29, 2018. It is now read-only.
/
ndnping.c
469 lines (390 loc) · 14.2 KB
/
ndnping.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
/*
* ndnping sends ping Interests towards a name prefix to test connectivity.
* Copyright (C) 2011 University of Arizona
*
* 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.
*
* Author: Cheng Yi <yic@email.arizona.edu>
*/
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#include <ndn/ndn.h>
#include <ndn/uri.h>
#include <ndn/schedule.h>
#include <ndn/hashtb.h>
#define PING_COMPONENT "ping"
#define PING_MIN_INTERVAL 0.1
struct ndn_ping_client {
char *original_prefix; // Name prefix given by command line.
char *identifier; // Identifier added to the Interest names
// before the numbers.
struct ndn_charbuf *prefix; // Name prefix to ping.
double interval; // Interval between pings in seconds.
int sent; // Number of interest sent.
int received; // Number of content or timeout received.
int total; // Total number of pings to send.
long int number; // The number used in ping Interest name,
// number < 0 means random.
int print_timestamp; // Whether to print timestamp.
int allow_caching; // Whether routers are allowed to return
// ping Data from cache.
struct ndn *h;
struct ndn_schedule *sched;
struct ndn_scheduled_event *event;
struct ndn_closure *closure;
struct hashtb *ndn_ping_table;
};
struct ndn_ping_entry {
long int number;
struct timeval send_time;
};
struct ndn_ping_statistics {
char *prefix;
int sent;
int received;
struct timeval start;
double min;
double max;
double tsum;
double tsum2;
};
struct sigaction osa;
struct ndn_ping_statistics sta;
static void ndn_ping_gettime(const struct ndn_gettime *self, struct ndn_timeval *result)
{
struct timeval now;
gettimeofday(&now, 0);
result->s = now.tv_sec;
result->micros = now.tv_usec;
}
static struct ndn_gettime ndn_ping_ticker = {
"timer",
&ndn_ping_gettime,
1000000,
NULL
};
static void usage(const char *progname)
{
fprintf(stderr,
"Usage: %s ndnx:/name/prefix [options]\n"
"Ping a NDN name prefix using Interests with name ndnx:/name/prefix/ping/number.\n"
"The numbers in the Interests are randomly generated unless specified.\n"
" [-i interval] - set ping interval in seconds (minimum %.2f second)\n"
" [-c count] - set total number of pings\n"
" [-n number] - set the starting number, the number is increamented by 1"
" after each Interest\n"
" [-p identifier] - add identifier to the Interest names before the numbers"
" to avoid conflict\n"
" [-a] - allow routers to return ping Data from cache (allowed by default if"
" NDNx version < 0.8.0)\n"
" [-t] - print timestamp\n"
" [-h] - print this message and exit\n",
progname, PING_MIN_INTERVAL);
exit(1);
}
static struct ndn_ping_entry *get_ndn_ping_entry(struct ndn_ping_client *client,
const unsigned char *interest_msg, const struct ndn_parsed_interest *pi)
{
struct hashtb_enumerator ee;
struct hashtb_enumerator *e = ⅇ
struct ndn_ping_entry *entry;
int res;
hashtb_start(client->ndn_ping_table, e);
res = hashtb_seek(e, interest_msg + pi->offset[NDN_PI_B_Component0],
pi->offset[NDN_PI_E_LastPrefixComponent] - pi->offset[NDN_PI_B_Component0], 0);
assert(res == HT_OLD_ENTRY);
entry = e->data;
hashtb_end(e);
return entry;
}
static void remove_ndn_ping_entry(struct ndn_ping_client *client,
const unsigned char *interest_msg, const struct ndn_parsed_interest *pi)
{
struct hashtb_enumerator ee;
struct hashtb_enumerator *e = ⅇ
int res;
hashtb_start(client->ndn_ping_table, e);
res = hashtb_seek(e, interest_msg + pi->offset[NDN_PI_B_Component0],
pi->offset[NDN_PI_E_LastPrefixComponent] - pi->offset[NDN_PI_B_Component0], 0);
assert(res == HT_OLD_ENTRY);
hashtb_delete(e);
hashtb_end(e);
}
static void add_ndn_ping_entry(struct ndn_ping_client *client,
struct ndn_charbuf *name, long int number)
{
struct hashtb_enumerator ee;
struct hashtb_enumerator *e = ⅇ
struct ndn_ping_entry *entry;
int res;
hashtb_start(client->ndn_ping_table, e);
res = hashtb_seek(e, name->buf + 1, name->length - 2, 0);
assert(res == HT_NEW_ENTRY);
entry = e->data;
entry->number = number;
gettimeofday(&entry->send_time, 0);
hashtb_end(e);
}
void print_log(int print_timestamp, const char *format, ...)
{
if (print_timestamp) {
struct timeval now;
gettimeofday(&now, 0);
printf("%ld.%06u: ", (long)now.tv_sec, (unsigned)now.tv_usec);
}
va_list arglist;
va_start(arglist, format);
vprintf(format, arglist);
va_end(arglist);
}
static enum ndn_upcall_res incoming_content(struct ndn_closure* selfp,
enum ndn_upcall_kind kind, struct ndn_upcall_info* info)
{
struct ndn_ping_client *client = selfp->data;
struct ndn_ping_entry *entry;
double rtt;
struct timeval now;
assert(client->closure == selfp);
gettimeofday(&now, 0);
switch(kind) {
case NDN_UPCALL_FINAL:
break;
case NDN_UPCALL_CONTENT:
client->received ++;
sta.received ++;
entry = get_ndn_ping_entry(client,
info->interest_ndnb, info->pi);
rtt = (double)(now.tv_sec - entry->send_time.tv_sec) * 1000 +
(double)(now.tv_usec - entry->send_time.tv_usec) / 1000;
if (rtt < sta.min)
sta.min = rtt;
if (rtt > sta.max)
sta.max = rtt;
sta.tsum += rtt;
sta.tsum2 += rtt * rtt;
print_log(client->print_timestamp, "content from %s: number = %ld %2s\trtt = %.3f ms\n",
client->original_prefix, entry->number, "", rtt);
remove_ndn_ping_entry(client, info->interest_ndnb, info->pi);
break;
case NDN_UPCALL_INTEREST_TIMED_OUT:
entry = get_ndn_ping_entry(client,
info->interest_ndnb, info->pi);
print_log(client->print_timestamp, "timeout from %s: number = %ld\n",
client->original_prefix, entry->number);
remove_ndn_ping_entry(client, info->interest_ndnb, info->pi);
break;
default:
fprintf(stderr, "Unexpected response of kind %d\n", kind);
return NDN_UPCALL_RESULT_ERR;
}
return NDN_UPCALL_RESULT_OK;
}
struct ndn_charbuf *make_template(int allow_caching) {
if (NDN_API_VERSION >= 8000 && !allow_caching) {
struct ndn_charbuf *templ = ndn_charbuf_create();
ndn_charbuf_append_tt(templ, NDN_DTAG_Interest, NDN_DTAG);
ndn_charbuf_append_tt(templ, NDN_DTAG_Name, NDN_DTAG);
ndn_charbuf_append_closer(templ); // </Name>
ndn_charbuf_append_tt(templ, NDN_DTAG_AnswerOriginKind, NDN_DTAG);
ndnb_append_number(templ, NDN_AOK_NEW);
ndn_charbuf_append_closer(templ); // </AnswerOriginKind>
ndn_charbuf_append_closer(templ); // </Interest>
return(templ);
}
return NULL;
}
static int do_ping(struct ndn_schedule *sched, void *clienth,
struct ndn_scheduled_event *ev, int flags)
{
struct ndn_ping_client *client = clienth;
if (client->total >= 0 && client->sent >= client->total)
return 0;
struct ndn_charbuf *name = ndn_charbuf_create();
struct ndn_charbuf *templ = NULL;
long int rnum;
char rnumstr[20];
int res;
ndn_charbuf_append(name, client->prefix->buf, client->prefix->length);
if (client->number < 0)
rnum = random();
else {
rnum = client->number;
client->number ++;
}
memset(&rnumstr, 0, 20);
sprintf(rnumstr, "%ld", rnum);
ndn_name_append_str(name, rnumstr);
templ = make_template(client->allow_caching);
res = ndn_express_interest(client->h, name, client->closure, templ);
add_ndn_ping_entry(client, name, rnum);
client->sent ++;
sta.sent ++;
ndn_charbuf_destroy(&templ);
ndn_charbuf_destroy(&name);
if (res < 0)
print_log(client->print_timestamp, "failed to express Interest to %s: number = %ld\n",
client->original_prefix, rnum);
return client->interval * 1000000;
}
void print_statistics(void)
{
printf("\n--- %s ndnping statistics ---\n", sta.prefix);
if (sta.sent > 0) {
double lost = (double)(sta.sent - sta.received) * 100 / sta.sent;
struct timeval now;
gettimeofday(&now, 0);
int time = (double)(now.tv_sec - sta.start.tv_sec) * 1000 +
(double)(now.tv_usec - sta.start.tv_usec) / 1000;
printf("%d Interests transmitted, %d Data received, %.1f%% packet loss, time %d ms\n",
sta.sent, sta.received, lost, time);
}
if (sta.received > 0) {
double avg = sta.tsum / sta.received;
double mdev = sqrt(sta.tsum2 / sta.received - avg * avg);
printf("rtt min/avg/max/mdev = %.3f/%.3f/%.3f/%.3f ms\n", sta.min, avg, sta.max, mdev);
}
}
void handle_interrupt(int sig_no)
{
print_statistics();
sigaction(SIGINT, &osa, NULL);
kill(0, SIGINT);
}
int is_valid_identifier(char *identifier) {
if (strlen(identifier) == 0)
return 0;
while (*identifier != '\0') {
if (*identifier < 'A' || (*identifier > 'Z' && *identifier < 'a') || *identifier > 'z')
return 0;
identifier ++;
}
return 1;
}
int main(int argc, char *argv[])
{
const char *progname = argv[0];
struct ndn_ping_client client = {.identifier = 0, .interval = 1, .sent = 0,
.received = 0, .total = -1, .number = -1, .print_timestamp = 0, .allow_caching = 0};
struct ndn_closure in_content = {.p = &incoming_content};
struct hashtb_param param = {0};
int res;
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = &handle_interrupt;
sigaction(SIGINT, &sa, &osa);
memset(&sta, 0, sizeof(sta));
gettimeofday(&sta.start, 0);
sta.min = INT_MAX;
while ((res = getopt(argc, argv, "htai:c:n:p:")) != -1) {
switch (res) {
case 'c':
client.total = atol(optarg);
if (client.total <= 0)
usage(progname);
break;
case 'i':
client.interval = atof(optarg);
if (client.interval < PING_MIN_INTERVAL)
usage(progname);
break;
case 'n':
client.number = atol(optarg);
if (client.number < 0)
usage(progname);
break;
case 'p':
client.identifier = optarg;
if (!is_valid_identifier(client.identifier))
usage(progname);
break;
case 't':
client.print_timestamp = 1;
break;
case 'a':
client.allow_caching = 1;
break;
case 'h':
default:
usage(progname);
break;
}
}
if (client.number < 0)
srandom(time(NULL) * getpid());
argc -= optind;
argv += optind;
if (argv[0] == NULL)
usage(progname);
sta.prefix = argv[0];
client.original_prefix = argv[0];
client.prefix = ndn_charbuf_create();
res = ndn_name_from_uri(client.prefix, argv[0]);
if (res < 0) {
fprintf(stderr, "%s: bad ndn URI: %s\n", progname, argv[0]);
exit(1);
}
if (argv[1] != NULL)
fprintf(stderr, "%s warning: extra arguments ignored\n", progname);
// Append "/ping" to the given name prefix.
res = ndn_name_append_str(client.prefix, PING_COMPONENT);
if (res < 0) {
fprintf(stderr, "%s: error constructing ndn URI: %s/%s\n",
progname, argv[0], PING_COMPONENT);
exit(1);
}
// Append identifier if not empty.
if (client.identifier) {
res = ndn_name_append_str(client.prefix, client.identifier);
if (res < 0) {
fprintf(stderr, "%s: error constructing ndn URI: %s/%s/%s\n",
progname, argv[0], PING_COMPONENT, client.identifier);
exit(1);
}
}
// Connect to ndnd.
client.h = ndn_create();
if (ndn_connect(client.h, NULL) == -1) {
perror("Could not connect to ndnd");
exit(1);
}
client.closure = &in_content;
in_content.data = &client;
client.ndn_ping_table = hashtb_create(sizeof(struct ndn_ping_entry), ¶m);
client.sched = ndn_schedule_create(&client, &ndn_ping_ticker);
client.event = ndn_schedule_event(client.sched, 0, &do_ping, NULL, 0);
print_log(client.print_timestamp, "NDNPING %s\n", client.original_prefix);
res = 0;
while (res >= 0 && (client.total <= 0 || client.sent < client.total ||
hashtb_n(client.ndn_ping_table) > 0))
{
if (client.total <= 0 || client.sent < client.total)
ndn_schedule_run(client.sched);
res = ndn_run(client.h, 10);
}
ndn_schedule_destroy(&client.sched);
ndn_destroy(&client.h);
ndn_charbuf_destroy(&client.prefix);
print_statistics();
return 0;
}