-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathccnpeek.c
192 lines (189 loc) · 6.5 KB
/
ccnpeek.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
/**
* @file ccnpeek.c
* Get one content item matching the name prefix and write it to stdout.
* Written as test for ccn_get, but probably useful for debugging.
*
* A CCNx command-line utility.
*
* Copyright (C) 2009-2013 Palo Alto Research Center, Inc.
*
* This work is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License version 2 as published by the
* Free Software Foundation.
* This work 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.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <ccn/bloom.h>
#include <ccn/ccn.h>
#include <ccn/charbuf.h>
#include <ccn/uri.h>
static void
usage(const char *progname)
{
fprintf(stderr,
"%s [-a] [-c] [-l lifetime] [-s scope] [-u] [-v] [-w timeout] ccnx:/a/b\n"
" Get one content item matching the name prefix and write it to stdout"
"\n"
" -a - allow stale data\n"
" -c - content only, not full ccnb\n"
" -l x - lifetime (seconds) of interest. 0.00012 < x <= 30.0000, Default 4.\n"
" -s {0,1,2} - scope of interest. Default none.\n"
" -u - allow unverified content\n"
" -v - resolve version number\n"
" -w x - wait time (seconds) for response. 0.001 <= timeout <= 60.000, Default 3.0\n",
progname);
exit(1);
}
int
main(int argc, char **argv)
{
struct ccn *h = NULL;
struct ccn_charbuf *name = NULL;
struct ccn_charbuf *templ = NULL;
struct ccn_charbuf *resultbuf = NULL;
const char *arg = NULL;
struct ccn_parsed_ContentObject pcobuf = { 0 };
int res;
int opt;
int allow_stale = 0;
int content_only = 0;
int scope = -1;
const unsigned char *ptr;
size_t length;
int resolve_version = 0;
int timeout_ms = 3000;
const unsigned lifetime_default = CCN_INTEREST_LIFETIME_SEC << 12;
unsigned lifetime_l12 = lifetime_default;
double lifetime;
int get_flags = 0;
while ((opt = getopt(argc, argv, "acl:s:uvw:h")) != -1) {
switch (opt) {
case 'a':
allow_stale = 1;
break;
case 'c':
content_only = 1;
break;
case 'l':
errno = 0;
lifetime = strtod(optarg, NULL);
if (errno != 0) {
perror(optarg);
exit(1);
}
lifetime_l12 = 4096 * (lifetime + 1.0/8192.0);
if (lifetime_l12 == 0 || lifetime_l12 > (30 << 12)) {
fprintf(stderr, "%.5f: invalid lifetime. %.5f < lifetime <= 30.0\n", lifetime, 1.0/8192.0);
exit(1);
}
break;
case 's':
scope = atoi(optarg);
if (scope < 0 || scope > 2) {
fprintf(stderr, "%d: invalid scope. 0 <= scope <= 2\n", scope);
exit(1);
}
case 'u':
get_flags |= CCN_GET_NOKEYWAIT;
break;
case 'v':
if (resolve_version == 0)
resolve_version = CCN_V_HIGHEST;
else
resolve_version = CCN_V_HIGH;
break;
case 'w':
timeout_ms = strtod(optarg, NULL) * 1000;
if (timeout_ms <= 0 || timeout_ms > 60000) {
fprintf(stderr, "%s: invalid timeout. 0.001 <= timeout <= 60.000\n", optarg);
exit(1);
}
break;
case 'h':
default:
usage(argv[0]);
}
}
arg = argv[optind];
if (arg == NULL)
usage(argv[0]);
name = ccn_charbuf_create();
res = ccn_name_from_uri(name, arg);
if (res < 0) {
fprintf(stderr, "%s: bad ccn URI: %s\n", argv[0], arg);
exit(1);
}
if (argv[optind + 1] != NULL)
fprintf(stderr, "%s warning: extra arguments ignored\n", argv[0]);
h = ccn_create();
res = ccn_connect(h, NULL);
if (res < 0) {
ccn_perror(h, "ccn_connect");
exit(1);
}
if (res < 0) {
fprintf(stderr, "%s: bad ccn URI: %s\n", argv[0], arg);
exit(1);
}
if (allow_stale || lifetime_l12 != lifetime_default || scope != -1) {
templ = ccn_charbuf_create();
ccnb_element_begin(templ, CCN_DTAG_Interest);
ccnb_element_begin(templ, CCN_DTAG_Name);
ccnb_element_end(templ); /* </Name> */
if (allow_stale) {
ccnb_element_begin(templ, CCN_DTAG_AnswerOriginKind);
ccnb_append_number(templ,
CCN_AOK_DEFAULT | CCN_AOK_STALE);
ccnb_element_end(templ); /* </AnswerOriginKind> */
}
if (scope != -1) {
ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
}
if (lifetime_l12 != lifetime_default) {
/*
* Choose the interest lifetime so there are at least 3
* expressions (in the unsatisfied case).
*/
unsigned char buf[3] = { 0 };
int i;
for (i = sizeof(buf) - 1; i >= 0; i--, lifetime_l12 >>= 8)
buf[i] = lifetime_l12 & 0xff;
ccnb_append_tagged_blob(templ, CCN_DTAG_InterestLifetime, buf, sizeof(buf));
}
ccnb_element_end(templ); /* </Interest> */
}
resultbuf = ccn_charbuf_create();
if (resolve_version != 0) {
res = ccn_resolve_version(h, name, resolve_version, 500);
if (res >= 0) {
ccn_uri_append(resultbuf, name->buf, name->length, 1);
fprintf(stderr, "== %s\n",
ccn_charbuf_as_string(resultbuf));
resultbuf->length = 0;
}
}
res = ccn_get(h, name, templ, timeout_ms, resultbuf, &pcobuf, NULL, get_flags);
if (res >= 0) {
ptr = resultbuf->buf;
length = resultbuf->length;
if (content_only)
ccn_content_get_value(ptr, length, &pcobuf, &ptr, &length);
if (length > 0)
res = fwrite(ptr, length, 1, stdout) - 1;
}
ccn_charbuf_destroy(&resultbuf);
ccn_charbuf_destroy(&templ);
ccn_charbuf_destroy(&name);
ccn_destroy(&h);
exit(res < 0);
}