forked from NDN-Routing/NLSR0.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nlsr_face.c
410 lines (340 loc) · 11.6 KB
/
nlsr_face.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
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdarg.h>
#include <string.h>
#include <ccn/ccn.h>
#include <ccn/uri.h>
#include <ccn/face_mgmt.h>
#include <ccn/reg_mgmt.h>
#include <ccn/charbuf.h>
#include "nlsr_face.h"
static void
ccn_fib_warn(int lineno, const char *format, ...)
{
struct timeval t;
va_list ap;
va_start(ap, format);
gettimeofday(&t, NULL);
fprintf(stderr, "%d.%06d ccn_fib[%d]:%d: ", (int)t.tv_sec, (unsigned)t.tv_usec, (int)getpid(), lineno);
vfprintf(stderr, format, ap);
va_end(ap);
}
static void
ccn_fib_fatal(int lineno, const char *format, ...)
{
struct timeval t;
va_list ap;
va_start(ap, format);
gettimeofday(&t, NULL);
fprintf(stderr, "%d.%06d ccn_fib[%d]:%d: ", (int)t.tv_sec, (unsigned)t.tv_usec, (int)getpid(), lineno);
vfprintf(stderr, format, ap);
va_end(ap);
exit(1);
}
#define ON_ERROR_EXIT(resval, msg) on_error_exit((resval), __LINE__, msg)
static void
on_error_exit(int res, int lineno, const char *msg)
{
if (res >= 0)
return;
ccn_fib_fatal(lineno, "fatal error, res = %d, %s\n", res, msg);
}
#define ON_ERROR_CLEANUP(resval) \
{ \
if ((resval) < 0) { \
ccn_fib_warn (__LINE__, "OnError cleanup\n"); \
goto cleanup; \
} \
}
#define ON_NULL_CLEANUP(resval) \
{ \
if ((resval) == NULL) { \
ccn_fib_warn(__LINE__, "OnNull cleanup\n"); \
goto cleanup; \
} \
}
/**
*
* Bind a prefix to a face
*
*/
static int
register_unregister_prefix(struct ccn *h, struct ccn_charbuf *local_scope_template,
struct ccn_charbuf *no_name, struct ccn_charbuf *name_prefix,
struct ccn_face_instance *face_instance, int operation)
{
struct ccn_charbuf *temp = NULL;
struct ccn_charbuf *resultbuf = NULL;
struct ccn_charbuf *signed_info = NULL;
struct ccn_charbuf *name = NULL;
struct ccn_charbuf *prefixreg = NULL;
struct ccn_parsed_ContentObject pcobuf = {0};
struct ccn_forwarding_entry forwarding_entry_storage = {0};
struct ccn_forwarding_entry *forwarding_entry = &forwarding_entry_storage;
struct ccn_forwarding_entry *new_forwarding_entry;
const unsigned char *ptr = NULL;
size_t length = 0;
int res;
/* Register or unregister the prefix */
forwarding_entry->action = (operation == OP_REG) ? "prefixreg" : "unreg";
forwarding_entry->name_prefix = name_prefix;
forwarding_entry->ccnd_id = face_instance->ccnd_id;
forwarding_entry->ccnd_id_size = face_instance->ccnd_id_size;
forwarding_entry->faceid = face_instance->faceid;
forwarding_entry->flags = -1;
forwarding_entry->lifetime = (~0U) >> 1;
prefixreg = ccn_charbuf_create();
ccnb_append_forwarding_entry(prefixreg, forwarding_entry);
temp = ccn_charbuf_create();
res = ccn_sign_content(h, temp, no_name, NULL, prefixreg->buf, prefixreg->length);
resultbuf = ccn_charbuf_create();
/* construct Interest containing prefixreg request */
name = ccn_charbuf_create();
ccn_name_init(name);
ccn_name_append_str(name, "ccnx");
ccn_name_append(name, face_instance->ccnd_id, face_instance->ccnd_id_size);
ccn_name_append_str(name, (operation == OP_REG) ? "prefixreg" : "unreg");
ccn_name_append(name, temp->buf, temp->length);
/* send Interest, get Data */
res = ccn_get(h, name, local_scope_template, 1000, resultbuf, &pcobuf, NULL, 0);
ON_ERROR_CLEANUP(res);
res = ccn_content_get_value(resultbuf->buf, resultbuf->length, &pcobuf, &ptr, &length);
ON_ERROR_CLEANUP(res);
/* extract new forwarding entry from Data */
new_forwarding_entry = ccn_forwarding_entry_parse(ptr, length);
ON_NULL_CLEANUP(new_forwarding_entry);
res = new_forwarding_entry->faceid;
ccn_forwarding_entry_destroy(&new_forwarding_entry);
ccn_charbuf_destroy(&signed_info);
ccn_charbuf_destroy(&temp);
ccn_charbuf_destroy(&resultbuf);
ccn_charbuf_destroy(&name);
ccn_charbuf_destroy(&prefixreg);
return res;
cleanup:
ccn_forwarding_entry_destroy(&new_forwarding_entry);
ccn_charbuf_destroy(&signed_info);
ccn_charbuf_destroy(&temp);
ccn_charbuf_destroy(&resultbuf);
ccn_charbuf_destroy(&name);
ccn_charbuf_destroy(&prefixreg);
return -1;
}
/**
*
* Create new face by sending out a request Interest
* The actual new face instance is returned
*
*/
static
struct ccn_face_instance *create_face(struct ccn *h, struct ccn_charbuf *local_scope_template,
struct ccn_charbuf *no_name, struct ccn_face_instance *face_instance)
{
struct ccn_charbuf *newface = NULL;
struct ccn_charbuf *signed_info = NULL;
struct ccn_charbuf *temp = NULL;
struct ccn_charbuf *name = NULL;
struct ccn_charbuf *resultbuf = NULL;
struct ccn_parsed_ContentObject pcobuf = {0};
struct ccn_face_instance *new_face_instance = NULL;
const unsigned char *ptr = NULL;
size_t length = 0;
int res = 0;
/* Encode the given face instance */
newface = ccn_charbuf_create();
ccnb_append_face_instance(newface, face_instance);
temp = ccn_charbuf_create();
res = ccn_sign_content(h, temp, no_name, NULL, newface->buf, newface->length);
resultbuf = ccn_charbuf_create();
/* Construct the Interest name that will create the face */
name = ccn_charbuf_create();
ccn_name_init(name);
ccn_name_append_str(name, "ccnx");
ccn_name_append(name, face_instance->ccnd_id, face_instance->ccnd_id_size);
ccn_name_append_str(name, face_instance->action);
ccn_name_append(name, temp->buf, temp->length);
/* send Interest to retrieve Data that contains the newly created face */
res = ccn_get(h, name, local_scope_template, 1000, resultbuf, &pcobuf, NULL, 0);
ON_ERROR_CLEANUP(res);
/* decode Data to get the actual face instance */
res = ccn_content_get_value(resultbuf->buf, resultbuf->length, &pcobuf, &ptr, &length);
ON_ERROR_CLEANUP(res);
new_face_instance = ccn_face_instance_parse(ptr, length);
ccn_charbuf_destroy(&newface);
ccn_charbuf_destroy(&signed_info);
ccn_charbuf_destroy(&temp);
ccn_charbuf_destroy(&resultbuf);
ccn_charbuf_destroy(&name);
return new_face_instance;
cleanup:
ccn_charbuf_destroy(&newface);
ccn_charbuf_destroy(&signed_info);
ccn_charbuf_destroy(&temp);
ccn_charbuf_destroy(&resultbuf);
ccn_charbuf_destroy(&name);
return NULL;
}
/**
*
* Get ccnd id
*
*/
static int
get_ccndid(struct ccn *h, struct ccn_charbuf *local_scope_template,
unsigned char *ccndid)
{
struct ccn_charbuf *name = NULL;
struct ccn_charbuf *resultbuf = NULL;
struct ccn_parsed_ContentObject pcobuf = {0};
char ccndid_uri[] = "ccnx:/%C1.M.S.localhost/%C1.M.SRV/ccnd/KEY";
const unsigned char *ccndid_result;
static size_t ccndid_result_size;
int res;
name = ccn_charbuf_create();
resultbuf = ccn_charbuf_create();
res = ccn_name_from_uri(name, ccndid_uri);
ON_ERROR_EXIT(res, "Unable to parse service locator URI for ccnd key\n");
/* get Data */
res = ccn_get(h, name, local_scope_template, 4500, resultbuf, &pcobuf, NULL, 0);
ON_ERROR_EXIT(res, "Unable to get key from ccnd\n");
/* extract from Data */
res = ccn_ref_tagged_BLOB(CCN_DTAG_PublisherPublicKeyDigest,
resultbuf->buf,
pcobuf.offset[CCN_PCO_B_PublisherPublicKeyDigest],
pcobuf.offset[CCN_PCO_E_PublisherPublicKeyDigest],
&ccndid_result, &ccndid_result_size);
ON_ERROR_EXIT(res, "Unable to parse ccnd response for ccnd id\n");
memcpy((void *)ccndid, ccndid_result, ccndid_result_size);
ccn_charbuf_destroy(&name);
ccn_charbuf_destroy(&resultbuf);
return (ccndid_result_size);
}
/**
* Construct a new face instance based on the given address and port
* This face instance is only used to send new face request
*/
static struct
ccn_face_instance *construct_face(const unsigned char *ccndid, size_t ccndid_size,
const char *address, const char *port, unsigned int tunnel_proto)
{
struct ccn_face_instance *fi = calloc(1, sizeof(*fi));
char rhostnamebuf[NI_MAXHOST];
char rhostportbuf[NI_MAXSERV];
struct addrinfo hints = {.ai_family = AF_UNSPEC, .ai_flags = (AI_ADDRCONFIG),
.ai_socktype = SOCK_DGRAM};
struct addrinfo *raddrinfo = NULL;
struct ccn_charbuf *store = ccn_charbuf_create();
int host_off = -1;
int port_off = -1;
int res;
res = getaddrinfo(address, port, &hints, &raddrinfo);
if (res != 0 || raddrinfo == NULL)
{
fprintf(stderr, "Error: getaddrinfo\n");
return NULL;
}
res = getnameinfo(raddrinfo->ai_addr, raddrinfo->ai_addrlen,
rhostnamebuf, sizeof(rhostnamebuf),
rhostportbuf, sizeof(rhostportbuf),
NI_NUMERICHOST | NI_NUMERICSERV);
freeaddrinfo(raddrinfo);
if (res != 0)
{
fprintf(stderr, "Error: getnameinfo\n");
return NULL;
}
fi->store = store;
fi->descr.ipproto = tunnel_proto;
fi->descr.mcast_ttl = CCN_FIB_MCASTTTL;
fi->lifetime = CCN_FIB_LIFETIME;
ccn_charbuf_append(store, "newface", strlen("newface") + 1);
host_off = store->length;
ccn_charbuf_append(store, rhostnamebuf, strlen(rhostnamebuf) + 1);
port_off = store->length;
ccn_charbuf_append(store, rhostportbuf, strlen(rhostportbuf) + 1);
char *b = (char *)store->buf;
fi->action = b;
fi->descr.address = b + host_off;
fi->descr.port = b + port_off;
fi->descr.source_address = NULL;
fi->ccnd_id = ccndid;
fi->ccnd_id_size = ccndid_size;
return fi;
}
/**
* initialize local data
*/
static void
init_data(struct ccn_charbuf *local_scope_template,
struct ccn_charbuf *no_name)
{
ccn_charbuf_append_tt(local_scope_template, CCN_DTAG_Interest, CCN_DTAG);
ccn_charbuf_append_tt(local_scope_template, CCN_DTAG_Name, CCN_DTAG);
ccn_charbuf_append_closer(local_scope_template); /* </Name> */
ccnb_tagged_putf(local_scope_template, CCN_DTAG_Scope, "1");
ccn_charbuf_append_closer(local_scope_template); /* </Interest> */
ccn_name_init(no_name);
}
static int
add_delete_ccn_face(struct ccn *h, const char *uri, const char *address, const unsigned int p, int operation,unsigned int tunnel_proto)
{
struct ccn_charbuf *prefix;
char port[6];
struct ccn_charbuf *local_scope_template = ccn_charbuf_create();
struct ccn_charbuf *no_name = ccn_charbuf_create();
unsigned char ccndid_storage[32] = {0};
unsigned char *ccndid = ccndid_storage;
size_t ccndid_size = 0;
struct ccn_face_instance *fi;
struct ccn_face_instance *nfi;
int res;
prefix = ccn_charbuf_create();
res = ccn_name_from_uri(prefix, uri);
ON_ERROR_CLEANUP(res);
memset(port, 0, 6);
sprintf(port, "%d", p);
init_data(local_scope_template, no_name);
ccndid_size = get_ccndid(h, local_scope_template, ccndid);
if (ccndid_size != sizeof(ccndid_storage))
{
fprintf(stderr, "Incorrect size for ccnd id in response\n");
ON_ERROR_CLEANUP(-1);
}
/* construct a face instance for new face request */
fi = construct_face(ccndid, ccndid_size, address, port,tunnel_proto);
ON_NULL_CLEANUP(fi);
/* send new face request to actually create a new face */
nfi = create_face(h, local_scope_template, no_name, fi);
ON_NULL_CLEANUP(nfi);
res = register_unregister_prefix(h, local_scope_template, no_name, prefix, nfi, operation);
ON_ERROR_CLEANUP(res);
int faceid=nfi->faceid;
ccn_charbuf_destroy(&local_scope_template);
ccn_charbuf_destroy(&no_name);
ccn_face_instance_destroy(&fi);
ccn_face_instance_destroy(&nfi);
ccn_charbuf_destroy(&prefix);
return faceid;
cleanup:
ccn_charbuf_destroy(&prefix);
ccn_charbuf_destroy(&local_scope_template);
ccn_charbuf_destroy(&no_name);
ccn_face_instance_destroy(&fi);
ccn_face_instance_destroy(&nfi);
return -1;
}
int
add_ccn_face(struct ccn *h, const char *uri, const char *address, const unsigned int port, unsigned int tunnel_proto)
{
return add_delete_ccn_face(h, uri, address, port, OP_REG,tunnel_proto);
}
int
delete_ccn_face(struct ccn *h, const char *uri, const char *address, const unsigned int port,unsigned int tunnel_proto)
{
return add_delete_ccn_face(h, uri, address, port, OP_UNREG,tunnel_proto);
}