blob: e89fcb8f7835ae7eba8cbd12076bc58c3d2710c7 [file] [log] [blame]
akmhoque8fdd6412012-12-04 15:05:33 -06001#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4#include <unistd.h>
5#include <getopt.h>
6#include <sys/time.h>
7#include <sys/stat.h>
8#include <assert.h>
9#include <sys/types.h>
10#include <signal.h>
11#include <sys/socket.h>
12#include <sys/un.h>
13#include <fcntl.h>
14#include <sys/ioctl.h>
15#include <netinet/in.h>
16#include <netdb.h>
17#include <arpa/inet.h>
18
19#include <ccn/ccn.h>
20#include <ccn/uri.h>
21#include <ccn/keystore.h>
22#include <ccn/signing.h>
23#include <ccn/schedule.h>
24#include <ccn/hashtb.h>
25#include <ccn/sync.h>
26#include <ccn/seqwriter.h>
27
28#include "nlsr.h"
29#include "nlsr_sync.h"
30#include "nlsr_lsdb.h"
akmhoque4ae16942012-12-10 11:50:43 -060031#include "utility.h"
akmhoque8fdd6412012-12-04 15:05:33 -060032
33int
34get_lsa_position(struct ccn_charbuf * ccnb, struct ccn_indexbuf *comps)
35{
36
37
38
39 int res,i;
40 int lsa_position=0;
41 int name_comps=(int)comps->n;
42
43 for(i=0;i<name_comps;i++)
44 {
45 res=ccn_name_comp_strcmp(ccnb->buf,comps,i,"LSA");
46 if( res == 0)
47 {
48 lsa_position=i;
49 break;
50 }
51 }
52
53 return lsa_position;
54
55}
56
57void
58get_name_part(struct name_prefix *name_part,struct ccn_charbuf * interest_ccnb, struct ccn_indexbuf *interest_comps, int offset)
59{
60
61
62
63 int res,i;
64 int lsa_position=0;
65 int len=0;
66
67 lsa_position=get_lsa_position(interest_ccnb,interest_comps);
68
69 const unsigned char *comp_ptr1;
70 size_t comp_size;
71 for(i=lsa_position+1+offset;i<interest_comps->n-1;i++)
72 {
73 res=ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1, &comp_size);
74 len+=1;
75 len+=(int)comp_size;
76 }
77 len++;
78
79 char *neighbor=(char *)malloc(len);
80 memset(neighbor,0,len);
81
82 for(i=lsa_position+1+offset; i<interest_comps->n-1;i++)
83 {
84 res=ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1, &comp_size);
85 memcpy(neighbor+strlen(neighbor),"/",1);
86 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,strlen((char *)comp_ptr1));
87
88 }
89
90 name_part->name=(char *)malloc(strlen(neighbor)+1);
91 memset(name_part->name,0,strlen(neighbor)+1);
92 memcpy(name_part->name,neighbor,strlen(neighbor)+1);
93 name_part->length=strlen(neighbor)+1;
94
95
96}
97
98
99char *
100get_content_by_content_name(char *content_name)
101{
102
103 struct ccn_charbuf *name = NULL;
104 struct ccn_charbuf *templ = NULL;
105 struct ccn_charbuf *resultbuf = NULL;
106 struct ccn_parsed_ContentObject pcobuf = { 0 };
107 int res;
108 int allow_stale = 0;
109 int content_only = 1;
110 int scope = -1;
111 const unsigned char *ptr;
112 size_t length;
113 int resolve_version = CCN_V_HIGHEST;
114 int timeout_ms = 3000;
115 const unsigned lifetime_default = CCN_INTEREST_LIFETIME_SEC << 12;
116 unsigned lifetime_l12 = lifetime_default;
117 int get_flags = 0;
118
119
120 name = ccn_charbuf_create();
121 res = ccn_name_from_uri(name,content_name);
122 if (res < 0) {
123 fprintf(stderr, "Bad ccn URI: %s\n", content_name);
124 exit(1);
125 }
126
127
128 if (allow_stale || lifetime_l12 != lifetime_default || scope != -1) {
129 templ = ccn_charbuf_create();
130 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
131 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
132 ccn_charbuf_append_closer(templ); /* </Name> */
133 if (allow_stale) {
134 ccn_charbuf_append_tt(templ, CCN_DTAG_AnswerOriginKind, CCN_DTAG);
135 ccnb_append_number(templ,
136 CCN_AOK_DEFAULT | CCN_AOK_STALE);
137 ccn_charbuf_append_closer(templ); /* </AnswerOriginKind> */
138 }
139 if (scope != -1) {
140 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
141 }
142 if (lifetime_l12 != lifetime_default) {
143 /*
144 * Choose the interest lifetime so there are at least 3
145 * expressions (in the unsatisfied case).
146 */
147 unsigned char buf[3] = { 0 };
148 int i;
149 for (i = sizeof(buf) - 1; i >= 0; i--, lifetime_l12 >>= 8)
150 buf[i] = lifetime_l12 & 0xff;
151 ccnb_append_tagged_blob(templ, CCN_DTAG_InterestLifetime, buf, sizeof(buf));
152 }
153 ccn_charbuf_append_closer(templ); /* </Interest> */
154 }
155 resultbuf = ccn_charbuf_create();
156 if (resolve_version != 0) {
157 res = ccn_resolve_version(nlsr->ccn, name, resolve_version, 500);
158 if (res >= 0) {
159 ccn_uri_append(resultbuf, name->buf, name->length, 1);
160 //fprintf(stderr, "== %s\n",ccn_charbuf_as_string(resultbuf));
161 resultbuf->length = 0;
162 }
163 }
164 res = ccn_get(nlsr->ccn, name, templ, timeout_ms, resultbuf, &pcobuf, NULL, get_flags);
165 if (res >= 0) {
166 ptr = resultbuf->buf;
167 length = resultbuf->length;
168 if (content_only)
169 ccn_content_get_value(ptr, length, &pcobuf, &ptr, &length);
170 }
171 ccn_charbuf_destroy(&resultbuf);
172 ccn_charbuf_destroy(&templ);
173 ccn_charbuf_destroy(&name);
174 return (char *)ptr;
175}
176
akmhoque4ae16942012-12-10 11:50:43 -0600177void
178process_incoming_sync_content_lsa(char *content_data)
179{
180
181
182 if ( nlsr->debugging )
183 printf("process_incoming_sync_content_lsa called \n");
184 //if ( nlsr->detailed_logging )
185 //writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_content_lsa called \n");
186
187 char *sep="|";
188 char *rem;
189 char *orig_router;
190 char *orl;
191 int orig_router_length;
192 char *lst;
193 int ls_type;
194 char *lsid;
195 long int ls_id;
196 char *isvld;
197 int isValid;
198 char *num_link;
199 int no_link;
200 char *np;
201 char *np_length;
202 int name_length;
203 char *data;
204 char *orig_time;
205
206
207 if ( nlsr->debugging )
208 printf("LSA Data \n");
209 //if ( nlsr->detailed_logging )
210 // writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Data\n");
211
212 if( strlen(content_data ) > 0 )
213 {
214
215 orig_router=strtok_r((char *)content_data,sep,&rem);
216 orl=strtok_r(NULL,sep,&rem);
217 orig_router_length=atoi(orl);
218
219 if ( nlsr->debugging )
220 {
221 printf(" Orig Router Name : %s\n",orig_router);
222 printf(" Orig Router Length: %d\n",orig_router_length);
223 }
224
225 lst=strtok_r(NULL,sep,&rem);
226 ls_type=atoi(lst);
227
228 if ( nlsr->debugging )
229 printf(" LS Type : %d\n",ls_type);
230
231 if ( ls_type == LS_TYPE_NAME )
232 {
233 lsid=strtok_r(NULL,sep,&rem);
234 ls_id=atoi(lsid);
235 orig_time=strtok_r(NULL,sep,&rem);
236 isvld=strtok_r(NULL,sep,&rem);
237 isValid=atoi(isvld);
238 np=strtok_r(NULL,sep,&rem);
239 np_length=strtok_r(NULL,sep,&rem);
240 name_length=atoi(np_length);
241 if ( nlsr->debugging )
242 {
243 printf(" LS ID : %ld\n",ls_id);
244 printf(" isValid : %d\n",isValid);
245 printf(" Name Prefix : %s\n",np);
246 printf(" Orig Time : %s\n",orig_time);
247 printf(" Name Prefix length: %d\n",name_length);
248 }
249
250 build_and_install_others_name_lsa(orig_router,ls_type,ls_id,orig_time,isValid,np);
251
252 print_name_lsdb();
253
254 }
255 else if ( ls_type == LS_TYPE_ADJ )
256 {
257 orig_time=strtok_r(NULL,sep,&rem);
258 num_link=strtok_r(NULL,sep,&rem);
259 no_link=atoi(num_link);
260 data=rem;
261
262 if ( nlsr->debugging )
263 {
264 printf(" No Link : %d\n",no_link);
265 printf(" Data : %s\n",data);
266 }
267 build_and_install_others_adj_lsa(orig_router,ls_type,orig_time,no_link,data);
268 }
269 }
270}
271
akmhoque8fdd6412012-12-04 15:05:33 -0600272void
273process_content_from_sync(struct ccn_charbuf *content_name, struct ccn_indexbuf *components)
274{
275 int lsa_position;
276 int res;
277 size_t comp_size;
278 const unsigned char *lst;
279 const unsigned char *lsid;
280 const unsigned char *origtime;
281
282 int ls_type;
283 long int ls_id=0;
284
akmhoque4ae16942012-12-10 11:50:43 -0600285 char *time_stamp=(char *)malloc(20);
286 memset(time_stamp,0,20);
287 get_current_timestamp_micro(time_stamp);
288
akmhoque8fdd6412012-12-04 15:05:33 -0600289 struct ccn_charbuf *uri = ccn_charbuf_create();
290 ccn_uri_append(uri, content_name->buf, content_name->length, 0);
291
292 struct name_prefix *orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix));
293
294 lsa_position=get_lsa_position(content_name, components);
295
296 res=ccn_name_comp_get(content_name->buf, components,lsa_position+1,&lst, &comp_size);
297
akmhoque4ae16942012-12-10 11:50:43 -0600298 //printf("Ls Type: %s\n",lst);
akmhoque8fdd6412012-12-04 15:05:33 -0600299 ls_type=atoi((char *)lst);
300 if(ls_type == LS_TYPE_NAME)
301 {
akmhoque4ae16942012-12-10 11:50:43 -0600302
akmhoque8fdd6412012-12-04 15:05:33 -0600303 res=ccn_name_comp_get(content_name->buf, components,lsa_position+2,&lsid, &comp_size);
304 ls_id=atoi((char *)lsid);
305 res=ccn_name_comp_get(content_name->buf, components,lsa_position+3,&origtime, &comp_size);
306 get_name_part(orig_router,content_name,components,3);
akmhoque8fdd6412012-12-04 15:05:33 -0600307
akmhoque4ae16942012-12-10 11:50:43 -0600308 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
309
310 //printf("Ls ID: %s\nOrig Time: %s\nOrig Router: %s\n",lsid,origtime,orig_router->name);
311
312 if ( (strcmp((char *)orig_router,nlsr->router_name) == 0 && lsa_life_time < nlsr->lsa_refresh_time) || (strcmp((char *)orig_router,nlsr->router_name) != 0 && lsa_life_time < nlsr->router_dead_interval) )
akmhoque8fdd6412012-12-04 15:05:33 -0600313 {
akmhoque4ae16942012-12-10 11:50:43 -0600314 int is_new_name_lsa=check_is_new_name_lsa(orig_router->name,(char *)lst,(char *)lsid,(char *)origtime);
315 if ( is_new_name_lsa == 1 )
316 {
317 printf("New NAME LSA.....\n");
318 char *content_data=get_content_by_content_name(ccn_charbuf_as_string(uri));
319 printf("Content Data: %s \n",content_data);
320 process_incoming_sync_content_lsa(content_data);
321 }
322 else
323 {
324 printf("Name LSA / Newer Name LSA already xists in LSDB\n");
325 char *content_data=get_content_by_content_name(ccn_charbuf_as_string(uri));
326 printf("Content Data: %s \n",content_data);
327 }
akmhoque8fdd6412012-12-04 15:05:33 -0600328 }
329 else
330 {
akmhoque4ae16942012-12-10 11:50:43 -0600331 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
akmhoque8fdd6412012-12-04 15:05:33 -0600332 }
333 }
334 else if(ls_type == LS_TYPE_ADJ)
335 {
336 res=ccn_name_comp_get(content_name->buf, components,lsa_position+2,&origtime, &comp_size);
337 get_name_part(orig_router,content_name,components,2);
338 printf("Orig Time: %s\nOrig Router: %s\n",origtime,orig_router->name);
akmhoque8fdd6412012-12-04 15:05:33 -0600339
akmhoque4ae16942012-12-10 11:50:43 -0600340 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque8fdd6412012-12-04 15:05:33 -0600341
akmhoque4ae16942012-12-10 11:50:43 -0600342 //printf("Ls ID: %s\nOrig Time: %s\nOrig Router: %s\n",lsid,origtime,orig_router->name);
343
344 if ( (strcmp((char *)orig_router,nlsr->router_name) == 0 && lsa_life_time < nlsr->lsa_refresh_time) || (strcmp((char *)orig_router,nlsr->router_name) != 0 && lsa_life_time < nlsr->router_dead_interval) )
345 {
346 int is_new_adj_lsa=check_is_new_adj_lsa(orig_router->name,(char *)lst,(char *)origtime);
347 if ( is_new_adj_lsa == 1 )
348 {
349 printf("New Adj LSA.....\n");
350 char *content_data=get_content_by_content_name(ccn_charbuf_as_string(uri));
351 printf("Content Data: %s \n",content_data);
352 process_incoming_sync_content_lsa(content_data);
353 }
354 else
355 {
356
357 printf("Adj LSA / Newer Adj LSA already exists in LSDB\n");
358 char *content_data=get_content_by_content_name(ccn_charbuf_as_string(uri));
359 printf("Content Data: %s \n",content_data);
360 }
361 }
362 else
363 {
364 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
365 }
akmhoque8fdd6412012-12-04 15:05:33 -0600366 }
367
368 ccn_charbuf_destroy(&uri);
369
370}
371
372int
373sync_callback(struct ccns_name_closure *nc,
374 struct ccn_charbuf *lhash,
375 struct ccn_charbuf *rhash,
376 struct ccn_charbuf *name)
377{
378
379
380 struct ccn_indexbuf cid={0};
381
382 struct ccn_indexbuf *components=&cid;
383 ccn_name_split (name, components);
384 ccn_name_chop(name,components,-3);
385
386 process_content_from_sync(name,components);
387
388 return(0);
389}
390
391
392void
393sync_monitor(char *topo_prefix, char *slice_prefix)
394{
395
396 static struct ccns_name_closure nc={0};
397
398 nlsr->closure = &nc;
399 struct ccn_charbuf *prefix = ccn_charbuf_create();
400 struct ccn_charbuf *roothash = NULL;
401 struct ccn_charbuf *topo = ccn_charbuf_create();
402 nlsr->slice = ccns_slice_create();
403 ccn_charbuf_reset(prefix);
404 ccn_charbuf_reset(topo);
405
406 ccn_charbuf_reset(prefix);
407 ccn_name_from_uri(prefix, slice_prefix);
408
409 ccn_charbuf_reset(topo);
410 ccn_name_from_uri(topo, topo_prefix);
411
412
413 ccns_slice_set_topo_prefix(nlsr->slice, topo, prefix);
414 nlsr->closure->callback = &sync_callback;
415 nlsr->ccns = ccns_open(nlsr->ccn, nlsr->slice, nlsr->closure, roothash, NULL);
416
417}
418
419struct ccn_charbuf *
420make_template(int scope)
421{
422 struct ccn_charbuf *templ = NULL;
423 templ = ccn_charbuf_create();
424 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
425 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
426 ccn_charbuf_append_closer(templ); /* </Name> */
427 if (0 <= scope && scope <= 2)
428 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
429 ccn_charbuf_append_closer(templ); /* </Interest> */
430 return(templ);
431}
432
433void
434write_data_to_repo(char *data, char *name_prefix)
435{
436
437 struct ccn_charbuf *name = NULL;
438 struct ccn_seqwriter *w = NULL;
439 int blocksize = 1024;
440 int freshness = -1;
441 int torepo = 1;
442 int scope = 1;
443 int res;
444 size_t blockread;
445 struct ccn_charbuf *templ;
446
447 name = ccn_charbuf_create();
448 res = ccn_name_from_uri(name, name_prefix);
449 if (res < 0) {
450 fprintf(stderr, "bad CCN URI: %s\n",name_prefix);
451 exit(1);
452 }
453
454
455 w = ccn_seqw_create(nlsr->ccn, name);
456 if (w == NULL) {
457 fprintf(stderr, "ccn_seqw_create failed\n");
458 exit(1);
459 }
460 ccn_seqw_set_block_limits(w, blocksize, blocksize);
461 if (freshness > -1)
462 ccn_seqw_set_freshness(w, freshness);
463 if (torepo) {
464 struct ccn_charbuf *name_v = ccn_charbuf_create();
465 ccn_seqw_get_name(w, name_v);
466 ccn_name_from_uri(name_v, "%C1.R.sw");
467 ccn_name_append_nonce(name_v);
468 templ = make_template(scope);
469 res = ccn_get(nlsr->ccn, name_v, templ, 60000, NULL, NULL, NULL, 0);
470 ccn_charbuf_destroy(&templ);
471 ccn_charbuf_destroy(&name_v);
472 if (res < 0) {
473 fprintf(stderr, "No response from repository\n");
474 exit(1);
475 }
476 }
477
478
479
480
481 blockread = 0;
482 //struct ccn_charbuf *buf=ccn_charbuf_create();
483 //get_name_lsdb_summary(buf);
484 //blockread=buf->length;
485
486 blockread=strlen(data);
487
488 if (blockread > 0) {
489 //res = ccn_seqw_write(w, ccn_charbuf_as_string(buf), blockread);
490 res = ccn_seqw_write(w, data, blockread);
491 while (res == -1) {
492 ccn_run(nlsr->ccn, 100);
493 //res = ccn_seqw_write(w, ccn_charbuf_as_string(buf), blockread);
494 res = ccn_seqw_write(w, data, blockread);
495 }
496 }
497
498
499 ccn_run(nlsr->ccn, 1);
500
501 ccn_seqw_close(w);
502 //ccn_charbuf_destroy(&buf);
503 ccn_charbuf_destroy(&name);
504}
505
506
507int
508create_sync_slice(char *topo_prefix, char *slice_prefix)
509{
510 int res;
511 struct ccns_slice *slice;
512 struct ccn_charbuf *prefix = ccn_charbuf_create();
513 struct ccn_charbuf *topo = ccn_charbuf_create();
514 struct ccn_charbuf *clause = ccn_charbuf_create();
515 struct ccn_charbuf *slice_name = ccn_charbuf_create();
516 struct ccn_charbuf *slice_uri = ccn_charbuf_create();
517
518 if (prefix == NULL || topo == NULL || clause == NULL ||
519 slice_name == NULL || slice_uri == NULL) {
520 fprintf(stderr, "Unable to allocate required memory.\n");
521 exit(1);
522 }
523
524
525 slice = ccns_slice_create();
526
527 ccn_charbuf_reset(topo);
528 ccn_name_from_uri(topo, topo_prefix);
529 ccn_charbuf_reset(prefix);
530 ccn_name_from_uri(prefix,slice_prefix );
531 ccns_slice_set_topo_prefix(slice, topo, prefix);
532
533
534 res = ccns_write_slice(nlsr->ccn, slice, slice_name);
535
536 ccns_slice_destroy(&slice);
537 ccn_charbuf_destroy(&prefix);
538 ccn_charbuf_destroy(&topo);
539 ccn_charbuf_destroy(&clause);
540 ccn_charbuf_destroy(&slice_name);
541 ccn_charbuf_destroy(&slice_uri);
542
543 return 0;
544}
545