blob: 7b4279bde39d7149124a749aaa88a06eeab62a44 [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
akmhoque09c0afa2012-12-14 09:27:00 -060098void
99get_host_name_from_command_string(struct name_prefix *name_part,char *nbr_name_uri, int offset)
100{
101
102
103
104 int res,i;
105 int len=0;
106 const unsigned char *comp_ptr1;
107 size_t comp_size;
108
109 struct ccn_charbuf *name=ccn_charbuf_create();
110 name = ccn_charbuf_create();
111 res = ccn_name_from_uri(name,nbr_name_uri);
112 if (res < 0) {
113 fprintf(stderr, "Bad ccn URI: %s\n", nbr_name_uri);
114 exit(1);
115 }
116
117 struct ccn_indexbuf cid={0};
118
119 struct ccn_indexbuf *components=&cid;
120 ccn_name_split (name, components);
121
122 for(i=components->n-2;i> (0+offset);i--)
123 {
124 res=ccn_name_comp_get(name->buf, components,i,&comp_ptr1, &comp_size);
125 len+=1;
126 len+=(int)comp_size;
127 }
128 len++;
129
130 char *neighbor=(char *)malloc(len);
131 memset(neighbor,0,len);
132
133 for(i=components->n-2;i> (0+offset);i--)
134 {
135 res=ccn_name_comp_get(name->buf, components,i,&comp_ptr1, &comp_size);
136 if ( i != components->n-2)
137 memcpy(neighbor+strlen(neighbor),".",1);
138 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,strlen((char *)comp_ptr1));
139
140 }
141
142 name_part->name=(char *)malloc(strlen(neighbor)+1);
143 memset(name_part->name,0,strlen(neighbor)+1);
144 memcpy(name_part->name,neighbor,strlen(neighbor)+1);
145 name_part->length=strlen(neighbor)+1;
146
147
148}
149
akmhoque8fdd6412012-12-04 15:05:33 -0600150
151char *
152get_content_by_content_name(char *content_name)
153{
154
155 struct ccn_charbuf *name = NULL;
156 struct ccn_charbuf *templ = NULL;
157 struct ccn_charbuf *resultbuf = NULL;
158 struct ccn_parsed_ContentObject pcobuf = { 0 };
159 int res;
160 int allow_stale = 0;
161 int content_only = 1;
162 int scope = -1;
163 const unsigned char *ptr;
164 size_t length;
165 int resolve_version = CCN_V_HIGHEST;
166 int timeout_ms = 3000;
167 const unsigned lifetime_default = CCN_INTEREST_LIFETIME_SEC << 12;
168 unsigned lifetime_l12 = lifetime_default;
169 int get_flags = 0;
170
171
172 name = ccn_charbuf_create();
173 res = ccn_name_from_uri(name,content_name);
174 if (res < 0) {
175 fprintf(stderr, "Bad ccn URI: %s\n", content_name);
176 exit(1);
177 }
178
179
180 if (allow_stale || lifetime_l12 != lifetime_default || scope != -1) {
181 templ = ccn_charbuf_create();
182 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
183 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
184 ccn_charbuf_append_closer(templ); /* </Name> */
185 if (allow_stale) {
186 ccn_charbuf_append_tt(templ, CCN_DTAG_AnswerOriginKind, CCN_DTAG);
187 ccnb_append_number(templ,
188 CCN_AOK_DEFAULT | CCN_AOK_STALE);
189 ccn_charbuf_append_closer(templ); /* </AnswerOriginKind> */
190 }
191 if (scope != -1) {
192 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
193 }
194 if (lifetime_l12 != lifetime_default) {
195 /*
196 * Choose the interest lifetime so there are at least 3
197 * expressions (in the unsatisfied case).
198 */
199 unsigned char buf[3] = { 0 };
200 int i;
201 for (i = sizeof(buf) - 1; i >= 0; i--, lifetime_l12 >>= 8)
202 buf[i] = lifetime_l12 & 0xff;
203 ccnb_append_tagged_blob(templ, CCN_DTAG_InterestLifetime, buf, sizeof(buf));
204 }
205 ccn_charbuf_append_closer(templ); /* </Interest> */
206 }
207 resultbuf = ccn_charbuf_create();
208 if (resolve_version != 0) {
209 res = ccn_resolve_version(nlsr->ccn, name, resolve_version, 500);
210 if (res >= 0) {
211 ccn_uri_append(resultbuf, name->buf, name->length, 1);
212 //fprintf(stderr, "== %s\n",ccn_charbuf_as_string(resultbuf));
213 resultbuf->length = 0;
214 }
215 }
216 res = ccn_get(nlsr->ccn, name, templ, timeout_ms, resultbuf, &pcobuf, NULL, get_flags);
217 if (res >= 0) {
218 ptr = resultbuf->buf;
219 length = resultbuf->length;
220 if (content_only)
221 ccn_content_get_value(ptr, length, &pcobuf, &ptr, &length);
222 }
223 ccn_charbuf_destroy(&resultbuf);
224 ccn_charbuf_destroy(&templ);
225 ccn_charbuf_destroy(&name);
226 return (char *)ptr;
227}
228
akmhoque4ae16942012-12-10 11:50:43 -0600229void
230process_incoming_sync_content_lsa(char *content_data)
231{
232
233
234 if ( nlsr->debugging )
235 printf("process_incoming_sync_content_lsa called \n");
236 //if ( nlsr->detailed_logging )
237 //writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_content_lsa called \n");
238
239 char *sep="|";
240 char *rem;
241 char *orig_router;
242 char *orl;
243 int orig_router_length;
244 char *lst;
245 int ls_type;
246 char *lsid;
247 long int ls_id;
248 char *isvld;
249 int isValid;
250 char *num_link;
251 int no_link;
252 char *np;
253 char *np_length;
254 int name_length;
255 char *data;
256 char *orig_time;
257
258
259 if ( nlsr->debugging )
260 printf("LSA Data \n");
261 //if ( nlsr->detailed_logging )
262 // writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Data\n");
263
264 if( strlen(content_data ) > 0 )
265 {
266
267 orig_router=strtok_r((char *)content_data,sep,&rem);
268 orl=strtok_r(NULL,sep,&rem);
269 orig_router_length=atoi(orl);
270
271 if ( nlsr->debugging )
272 {
273 printf(" Orig Router Name : %s\n",orig_router);
274 printf(" Orig Router Length: %d\n",orig_router_length);
275 }
276
277 lst=strtok_r(NULL,sep,&rem);
278 ls_type=atoi(lst);
279
280 if ( nlsr->debugging )
281 printf(" LS Type : %d\n",ls_type);
282
283 if ( ls_type == LS_TYPE_NAME )
284 {
285 lsid=strtok_r(NULL,sep,&rem);
286 ls_id=atoi(lsid);
287 orig_time=strtok_r(NULL,sep,&rem);
288 isvld=strtok_r(NULL,sep,&rem);
289 isValid=atoi(isvld);
290 np=strtok_r(NULL,sep,&rem);
291 np_length=strtok_r(NULL,sep,&rem);
292 name_length=atoi(np_length);
293 if ( nlsr->debugging )
294 {
295 printf(" LS ID : %ld\n",ls_id);
296 printf(" isValid : %d\n",isValid);
297 printf(" Name Prefix : %s\n",np);
298 printf(" Orig Time : %s\n",orig_time);
299 printf(" Name Prefix length: %d\n",name_length);
300 }
301
302 build_and_install_others_name_lsa(orig_router,ls_type,ls_id,orig_time,isValid,np);
303
304 print_name_lsdb();
305
306 }
307 else if ( ls_type == LS_TYPE_ADJ )
308 {
309 orig_time=strtok_r(NULL,sep,&rem);
310 num_link=strtok_r(NULL,sep,&rem);
311 no_link=atoi(num_link);
312 data=rem;
313
314 if ( nlsr->debugging )
315 {
316 printf(" No Link : %d\n",no_link);
317 printf(" Data : %s\n",data);
318 }
319 build_and_install_others_adj_lsa(orig_router,ls_type,orig_time,no_link,data);
320 }
321 }
322}
323
akmhoque8fdd6412012-12-04 15:05:33 -0600324void
325process_content_from_sync(struct ccn_charbuf *content_name, struct ccn_indexbuf *components)
326{
327 int lsa_position;
328 int res;
329 size_t comp_size;
330 const unsigned char *lst;
331 const unsigned char *lsid;
332 const unsigned char *origtime;
333
334 int ls_type;
335 long int ls_id=0;
336
akmhoque4ae16942012-12-10 11:50:43 -0600337 char *time_stamp=(char *)malloc(20);
338 memset(time_stamp,0,20);
339 get_current_timestamp_micro(time_stamp);
340
akmhoque8fdd6412012-12-04 15:05:33 -0600341 struct ccn_charbuf *uri = ccn_charbuf_create();
342 ccn_uri_append(uri, content_name->buf, content_name->length, 0);
343
344 struct name_prefix *orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix));
345
346 lsa_position=get_lsa_position(content_name, components);
347
348 res=ccn_name_comp_get(content_name->buf, components,lsa_position+1,&lst, &comp_size);
349
akmhoque4ae16942012-12-10 11:50:43 -0600350 //printf("Ls Type: %s\n",lst);
akmhoque8fdd6412012-12-04 15:05:33 -0600351 ls_type=atoi((char *)lst);
352 if(ls_type == LS_TYPE_NAME)
353 {
akmhoque4ae16942012-12-10 11:50:43 -0600354
akmhoque8fdd6412012-12-04 15:05:33 -0600355 res=ccn_name_comp_get(content_name->buf, components,lsa_position+2,&lsid, &comp_size);
356 ls_id=atoi((char *)lsid);
357 res=ccn_name_comp_get(content_name->buf, components,lsa_position+3,&origtime, &comp_size);
358 get_name_part(orig_router,content_name,components,3);
akmhoque8fdd6412012-12-04 15:05:33 -0600359
akmhoque4ae16942012-12-10 11:50:43 -0600360 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
361
362 //printf("Ls ID: %s\nOrig Time: %s\nOrig Router: %s\n",lsid,origtime,orig_router->name);
363
364 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 -0600365 {
akmhoque4ae16942012-12-10 11:50:43 -0600366 int is_new_name_lsa=check_is_new_name_lsa(orig_router->name,(char *)lst,(char *)lsid,(char *)origtime);
367 if ( is_new_name_lsa == 1 )
368 {
369 printf("New NAME LSA.....\n");
370 char *content_data=get_content_by_content_name(ccn_charbuf_as_string(uri));
371 printf("Content Data: %s \n",content_data);
372 process_incoming_sync_content_lsa(content_data);
373 }
374 else
375 {
376 printf("Name LSA / Newer Name LSA already xists in LSDB\n");
377 char *content_data=get_content_by_content_name(ccn_charbuf_as_string(uri));
378 printf("Content Data: %s \n",content_data);
379 }
akmhoque8fdd6412012-12-04 15:05:33 -0600380 }
381 else
382 {
akmhoque4ae16942012-12-10 11:50:43 -0600383 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
akmhoque8fdd6412012-12-04 15:05:33 -0600384 }
385 }
386 else if(ls_type == LS_TYPE_ADJ)
387 {
388 res=ccn_name_comp_get(content_name->buf, components,lsa_position+2,&origtime, &comp_size);
389 get_name_part(orig_router,content_name,components,2);
390 printf("Orig Time: %s\nOrig Router: %s\n",origtime,orig_router->name);
akmhoque8fdd6412012-12-04 15:05:33 -0600391
akmhoque4ae16942012-12-10 11:50:43 -0600392 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque8fdd6412012-12-04 15:05:33 -0600393
akmhoque4ae16942012-12-10 11:50:43 -0600394 //printf("Ls ID: %s\nOrig Time: %s\nOrig Router: %s\n",lsid,origtime,orig_router->name);
395
396 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) )
397 {
398 int is_new_adj_lsa=check_is_new_adj_lsa(orig_router->name,(char *)lst,(char *)origtime);
399 if ( is_new_adj_lsa == 1 )
400 {
401 printf("New Adj LSA.....\n");
402 char *content_data=get_content_by_content_name(ccn_charbuf_as_string(uri));
403 printf("Content Data: %s \n",content_data);
404 process_incoming_sync_content_lsa(content_data);
405 }
406 else
407 {
408
409 printf("Adj LSA / Newer Adj LSA already exists in LSDB\n");
410 char *content_data=get_content_by_content_name(ccn_charbuf_as_string(uri));
411 printf("Content Data: %s \n",content_data);
412 }
413 }
414 else
415 {
416 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
417 }
akmhoque8fdd6412012-12-04 15:05:33 -0600418 }
419
420 ccn_charbuf_destroy(&uri);
421
422}
423
424int
425sync_callback(struct ccns_name_closure *nc,
426 struct ccn_charbuf *lhash,
427 struct ccn_charbuf *rhash,
428 struct ccn_charbuf *name)
429{
430
431
432 struct ccn_indexbuf cid={0};
433
434 struct ccn_indexbuf *components=&cid;
435 ccn_name_split (name, components);
436 ccn_name_chop(name,components,-3);
437
438 process_content_from_sync(name,components);
439
440 return(0);
441}
442
443
444void
445sync_monitor(char *topo_prefix, char *slice_prefix)
446{
447
448 static struct ccns_name_closure nc={0};
449
450 nlsr->closure = &nc;
451 struct ccn_charbuf *prefix = ccn_charbuf_create();
452 struct ccn_charbuf *roothash = NULL;
453 struct ccn_charbuf *topo = ccn_charbuf_create();
454 nlsr->slice = ccns_slice_create();
455 ccn_charbuf_reset(prefix);
456 ccn_charbuf_reset(topo);
457
458 ccn_charbuf_reset(prefix);
459 ccn_name_from_uri(prefix, slice_prefix);
460
461 ccn_charbuf_reset(topo);
462 ccn_name_from_uri(topo, topo_prefix);
463
464
465 ccns_slice_set_topo_prefix(nlsr->slice, topo, prefix);
466 nlsr->closure->callback = &sync_callback;
467 nlsr->ccns = ccns_open(nlsr->ccn, nlsr->slice, nlsr->closure, roothash, NULL);
468
469}
470
471struct ccn_charbuf *
472make_template(int scope)
473{
474 struct ccn_charbuf *templ = NULL;
475 templ = ccn_charbuf_create();
476 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
477 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
478 ccn_charbuf_append_closer(templ); /* </Name> */
479 if (0 <= scope && scope <= 2)
480 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
481 ccn_charbuf_append_closer(templ); /* </Interest> */
482 return(templ);
483}
484
485void
486write_data_to_repo(char *data, char *name_prefix)
487{
488
489 struct ccn_charbuf *name = NULL;
490 struct ccn_seqwriter *w = NULL;
491 int blocksize = 1024;
492 int freshness = -1;
493 int torepo = 1;
494 int scope = 1;
495 int res;
496 size_t blockread;
497 struct ccn_charbuf *templ;
498
499 name = ccn_charbuf_create();
500 res = ccn_name_from_uri(name, name_prefix);
501 if (res < 0) {
502 fprintf(stderr, "bad CCN URI: %s\n",name_prefix);
503 exit(1);
504 }
505
506
507 w = ccn_seqw_create(nlsr->ccn, name);
508 if (w == NULL) {
509 fprintf(stderr, "ccn_seqw_create failed\n");
510 exit(1);
511 }
512 ccn_seqw_set_block_limits(w, blocksize, blocksize);
513 if (freshness > -1)
514 ccn_seqw_set_freshness(w, freshness);
515 if (torepo) {
516 struct ccn_charbuf *name_v = ccn_charbuf_create();
517 ccn_seqw_get_name(w, name_v);
518 ccn_name_from_uri(name_v, "%C1.R.sw");
519 ccn_name_append_nonce(name_v);
520 templ = make_template(scope);
521 res = ccn_get(nlsr->ccn, name_v, templ, 60000, NULL, NULL, NULL, 0);
522 ccn_charbuf_destroy(&templ);
523 ccn_charbuf_destroy(&name_v);
524 if (res < 0) {
525 fprintf(stderr, "No response from repository\n");
526 exit(1);
527 }
528 }
529
530
531
532
533 blockread = 0;
534 //struct ccn_charbuf *buf=ccn_charbuf_create();
535 //get_name_lsdb_summary(buf);
536 //blockread=buf->length;
537
538 blockread=strlen(data);
539
540 if (blockread > 0) {
541 //res = ccn_seqw_write(w, ccn_charbuf_as_string(buf), blockread);
542 res = ccn_seqw_write(w, data, blockread);
543 while (res == -1) {
544 ccn_run(nlsr->ccn, 100);
545 //res = ccn_seqw_write(w, ccn_charbuf_as_string(buf), blockread);
546 res = ccn_seqw_write(w, data, blockread);
547 }
548 }
549
550
551 ccn_run(nlsr->ccn, 1);
552
553 ccn_seqw_close(w);
554 //ccn_charbuf_destroy(&buf);
555 ccn_charbuf_destroy(&name);
556}
557
558
559int
560create_sync_slice(char *topo_prefix, char *slice_prefix)
561{
562 int res;
563 struct ccns_slice *slice;
564 struct ccn_charbuf *prefix = ccn_charbuf_create();
565 struct ccn_charbuf *topo = ccn_charbuf_create();
566 struct ccn_charbuf *clause = ccn_charbuf_create();
567 struct ccn_charbuf *slice_name = ccn_charbuf_create();
568 struct ccn_charbuf *slice_uri = ccn_charbuf_create();
569
570 if (prefix == NULL || topo == NULL || clause == NULL ||
571 slice_name == NULL || slice_uri == NULL) {
572 fprintf(stderr, "Unable to allocate required memory.\n");
573 exit(1);
574 }
575
576
577 slice = ccns_slice_create();
578
579 ccn_charbuf_reset(topo);
580 ccn_name_from_uri(topo, topo_prefix);
581 ccn_charbuf_reset(prefix);
582 ccn_name_from_uri(prefix,slice_prefix );
583 ccns_slice_set_topo_prefix(slice, topo, prefix);
584
585
586 res = ccns_write_slice(nlsr->ccn, slice, slice_name);
587
588 ccns_slice_destroy(&slice);
589 ccn_charbuf_destroy(&prefix);
590 ccn_charbuf_destroy(&topo);
591 ccn_charbuf_destroy(&clause);
592 ccn_charbuf_destroy(&slice_name);
593 ccn_charbuf_destroy(&slice_uri);
594
595 return 0;
596}
597