blob: fb7e892ac875c5cc3c18334fce8ab1d551953bc9 [file] [log] [blame]
akmhoquea0e71152013-02-11 09:47:59 -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"
31#include "utility.h"
32
33
34char *
35hex_string(unsigned char *s, size_t l)
36{
37 const char *hex_digits = "0123456789abcdef";
38 char *r;
39 int i;
40 r = calloc(1, 1 + 2 * l);
41 for (i = 0; i < l; i++) {
42 r[2*i] = hex_digits[(s[i]>>4) & 0xf];
43 r[1+2*i] = hex_digits[s[i] & 0xf];
44 }
45 return(r);
46}
47
48int
49sync_cb(struct ccns_name_closure *nc,
50 struct ccn_charbuf *lhash,
51 struct ccn_charbuf *rhash,
52 struct ccn_charbuf *name)
53{
54 char *hexL;
55 char *hexR;
56 int res;
57 struct ccn_charbuf *uri = ccn_charbuf_create();
58 if (lhash == NULL || lhash->length == 0) {
59 hexL = strdup("none");
60 } else
61 hexL = hex_string(lhash->buf, lhash->length);
62 if (rhash == NULL || rhash->length == 0) {
63 hexR = strdup("none");
64 } else
65 hexR = hex_string(rhash->buf, rhash->length);
66 if (name != NULL)
67 ccn_uri_append(uri, name->buf, name->length, 1);
68 else
69 ccn_charbuf_append_string(uri, "(null)");
70
71
72 if ( nlsr->debugging )
73 printf("Response from sync in the name: %s \n",ccn_charbuf_as_string(uri));
74
75 fflush(stdout);
76 free(hexL);
77 free(hexR);
78 ccn_charbuf_destroy(&uri);
79
80
81 //--Doing our thing from here
akmhoque8f2a2c22013-02-12 15:10:18 -060082 //struct ccn_indexbuf cid={0};
akmhoquea0e71152013-02-11 09:47:59 -060083
akmhoque8f2a2c22013-02-12 15:10:18 -060084 //struct ccn_indexbuf *components=&cid;
85 struct ccn_indexbuf *components=ccn_indexbuf_create();
akmhoque018692c2013-02-11 11:33:39 -060086 res=ccn_name_split (name, components);
87 if ( res < 0 )
88 return 0;
akmhoquea0e71152013-02-11 09:47:59 -060089 //ccn_name_chop(name,components,-3);
90 //process_content_from_sync(name,components);
91
92 struct ccn_charbuf *content_name = ccn_charbuf_create();
93 ccn_name_init(content_name);
akmhoque018692c2013-02-11 11:33:39 -060094 if (components->n < 2)
95 return 0;
akmhoquea0e71152013-02-11 09:47:59 -060096 res = ccn_name_append_components(content_name, name->buf, components->buf[0], components->buf[components->n - 1]);
97
akmhoque018692c2013-02-11 11:33:39 -060098 if ( res < 0)
99 return 0;
100
101
akmhoquea0e71152013-02-11 09:47:59 -0600102 // debugging purpose
103 struct ccn_charbuf *temp=ccn_charbuf_create();
104 ccn_uri_append(temp, content_name->buf, content_name->length, 0);
105 if ( nlsr->debugging )
106 printf("Name before chopping: %s \n",ccn_charbuf_as_string(temp));
107 ccn_charbuf_destroy(&temp);
108
akmhoque8f2a2c22013-02-12 15:10:18 -0600109 //struct ccn_indexbuf cid1={0};
110 //struct ccn_indexbuf *components1=&cid1;
111 struct ccn_indexbuf *components1=ccn_indexbuf_create();
akmhoquea0e71152013-02-11 09:47:59 -0600112 res=ccn_name_split (content_name, components1);
akmhoque018692c2013-02-11 11:33:39 -0600113 if ( res < 0)
114 return 0;
115
akmhoquea0e71152013-02-11 09:47:59 -0600116 if ( nlsr->debugging )
117 {
118 printf("Number of components in name = %d \n",res);
119 printf("Number of components in name as indexbuf->n = %d \n",(int)components1->n);
120 }
121 ccn_name_chop(content_name,components1,-3);
122 if ( nlsr->debugging )
123 printf("Number of components in name as indexbuf->n after chopping= %d \n",(int)components1->n);
124
125 //debugging purpose
126 struct ccn_charbuf *temp1=ccn_charbuf_create();
127 ccn_uri_append(temp1, content_name->buf, content_name->length, 0);
128 if ( nlsr->debugging )
129 printf("Name after chopping: %s \n",ccn_charbuf_as_string(temp1));
130 ccn_charbuf_destroy(&temp1);
131
132 process_content_from_sync(content_name,components1);
133 ccn_charbuf_destroy(&content_name);
akmhoque8f2a2c22013-02-12 15:10:18 -0600134 ccn_indexbuf_destroy(&components);
135 ccn_indexbuf_destroy(&components1);
akmhoquea0e71152013-02-11 09:47:59 -0600136
137 return(0);
138}
139
140
141int
142get_lsa_position(struct ccn_charbuf * ccnb, struct ccn_indexbuf *comps)
143{
144
145
146
147 int res,i;
148 int lsa_position=0;
149 int name_comps=(int)comps->n;
150
151 for(i=0;i<name_comps;i++)
152 {
153 res=ccn_name_comp_strcmp(ccnb->buf,comps,i,"LSA");
154 if( res == 0)
155 {
156 lsa_position=i;
157 break;
158 }
159 }
160
161 return lsa_position;
162
163}
164
165void
166get_name_part(struct name_prefix *name_part,struct ccn_charbuf * interest_ccnb, struct ccn_indexbuf *interest_comps, int offset)
167{
168
169
170
akmhoqueb28579d2013-02-12 11:15:52 -0600171 int i;
akmhoquea0e71152013-02-11 09:47:59 -0600172 int lsa_position=0;
173 int len=0;
174
175
176 struct ccn_indexbuf cid={0};
177 struct ccn_indexbuf *components=&cid;
178 struct ccn_charbuf *name=ccn_charbuf_create();
179 ccn_name_from_uri(name,nlsr->slice_prefix);
180 ccn_name_split (name, components);
181 lsa_position=components->n-2;
182
183 ccn_charbuf_destroy(&name);
184
185
186 const unsigned char *comp_ptr1;
187 size_t comp_size;
188 for(i=lsa_position+1+offset;i<interest_comps->n-1;i++)
189 {
akmhoqueb28579d2013-02-12 11:15:52 -0600190 ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1, &comp_size);
akmhoquea0e71152013-02-11 09:47:59 -0600191 len+=1;
192 len+=(int)comp_size;
193 }
194 len++;
195
196 char *neighbor=(char *)malloc(len);
197 memset(neighbor,0,len);
198
199 for(i=lsa_position+1+offset; i<interest_comps->n-1;i++)
200 {
akmhoqueb28579d2013-02-12 11:15:52 -0600201 ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1, &comp_size);
akmhoquea0e71152013-02-11 09:47:59 -0600202 memcpy(neighbor+strlen(neighbor),"/",1);
203 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,strlen((char *)comp_ptr1));
204
205 }
206
207 name_part->name=(char *)malloc(strlen(neighbor)+1);
208 memset(name_part->name,0,strlen(neighbor)+1);
209 memcpy(name_part->name,neighbor,strlen(neighbor)+1);
210 name_part->length=strlen(neighbor)+1;
211
212 // Add 01/31/2013
213 free(neighbor);
214}
215
216void
217get_host_name_from_command_string(struct name_prefix *name_part,char *nbr_name_uri, int offset)
218{
219
220
221
222 int res,i;
223 int len=0;
224 const unsigned char *comp_ptr1;
225 size_t comp_size;
226
227 struct ccn_charbuf *name=ccn_charbuf_create();
228 name = ccn_charbuf_create();
229 res = ccn_name_from_uri(name,nbr_name_uri);
230 if (res < 0) {
231 fprintf(stderr, "Bad ccn URI: %s\n", nbr_name_uri);
232 exit(1);
233 }
234
235 struct ccn_indexbuf cid={0};
236
237 struct ccn_indexbuf *components=&cid;
238 ccn_name_split (name, components);
239
240 for(i=components->n-2;i> (0+offset);i--)
241 {
242 res=ccn_name_comp_get(name->buf, components,i,&comp_ptr1, &comp_size);
243 len+=1;
244 len+=(int)comp_size;
245 }
246 len++;
247
248 char *neighbor=(char *)malloc(len);
249 memset(neighbor,0,len);
250
251 for(i=components->n-2;i> (0+offset);i--)
252 {
253 res=ccn_name_comp_get(name->buf, components,i,&comp_ptr1, &comp_size);
254 if ( i != components->n-2)
255 memcpy(neighbor+strlen(neighbor),".",1);
256 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,strlen((char *)comp_ptr1));
257
258 }
259
260 name_part->name=(char *)malloc(strlen(neighbor)+1);
261 memset(name_part->name,0,strlen(neighbor)+1);
262 memcpy(name_part->name,neighbor,strlen(neighbor)+1);
263 name_part->length=strlen(neighbor)+1;
264
265 // 01/31/2013
266 free(neighbor);
267 ccn_charbuf_destroy(&name);
268}
269
270
271
272void
273get_content_by_content_name(char *content_name, unsigned char **content_data)
274{
275
276 struct ccn_charbuf *name = NULL;
277 struct ccn_charbuf *templ = NULL;
278 struct ccn_charbuf *resultbuf = NULL;
279 struct ccn_parsed_ContentObject pcobuf = { 0 };
280 int res;
281 int allow_stale = 0;
282 int content_only = 1;
283 int scope = -1;
284 const unsigned char *ptr;
285 size_t length;
286 int resolve_version = CCN_V_HIGHEST;
287 int timeout_ms = 3000;
288 const unsigned lifetime_default = CCN_INTEREST_LIFETIME_SEC << 12;
289 unsigned lifetime_l12 = lifetime_default;
290 int get_flags = 0;
291
292 name = ccn_charbuf_create();
293 res = ccn_name_from_uri(name,content_name);
294 if (res < 0) {
295 fprintf(stderr, "Bad ccn URI: %s\n", content_name);
296 exit(1);
297 }
298
299 if (allow_stale || lifetime_l12 != lifetime_default || scope != -1) {
300 templ = ccn_charbuf_create();
301 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
302 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
303 ccn_charbuf_append_closer(templ); /* </Name> */
304 if (allow_stale) {
305 ccn_charbuf_append_tt(templ, CCN_DTAG_AnswerOriginKind, CCN_DTAG);
306 ccnb_append_number(templ,
307 CCN_AOK_DEFAULT | CCN_AOK_STALE);
308 ccn_charbuf_append_closer(templ); /* </AnswerOriginKind> */
309 }
310 if (scope != -1) {
311 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
312 }
313 if (lifetime_l12 != lifetime_default) {
314 /*
315 * Choose the interest lifetime so there are at least 3
316 * expressions (in the unsatisfied case).
317 */
318 unsigned char buf[3] = { 0 };
319 int i;
320 for (i = sizeof(buf) - 1; i >= 0; i--, lifetime_l12 >>= 8)
321 buf[i] = lifetime_l12 & 0xff;
322 ccnb_append_tagged_blob(templ, CCN_DTAG_InterestLifetime, buf, sizeof(buf));
323 }
324 ccn_charbuf_append_closer(templ); /* </Interest> */
325 }
326 resultbuf = ccn_charbuf_create();
327 if (resolve_version != 0) {
328 res = ccn_resolve_version(nlsr->ccn, name, resolve_version, 500);
329 if (res >= 0) {
330 ccn_uri_append(resultbuf, name->buf, name->length, 1);
331 resultbuf->length = 0;
332 }
333 }
334 res = ccn_get(nlsr->ccn, name, templ, timeout_ms, resultbuf, &pcobuf, NULL, get_flags);
335 if (res >= 0) {
336 ptr = resultbuf->buf;
337 length = resultbuf->length;
338 if (content_only){
339 ccn_content_get_value(ptr, length, &pcobuf, &ptr, &length);
340 *content_data = (unsigned char *) calloc(length, sizeof(char *));
341 memcpy (*content_data, ptr, length);
342 }
343 }
344 ccn_charbuf_destroy(&resultbuf);
345 ccn_charbuf_destroy(&templ);
346 ccn_charbuf_destroy(&name);
347}
348
349void
350process_incoming_sync_content_lsa( unsigned char *content_data)
351{
352
353
354 if ( nlsr->debugging )
355 printf("process_incoming_sync_content_lsa called \n");
356
357 char *sep="|";
358 char *rem;
359 char *orig_router;
360 char *orl;
361 int orig_router_length;
362 char *lst;
363 int ls_type;
364 char *lsid;
365 long int ls_id;
366 char *isvld;
367 int isValid;
368 char *num_link;
369 int no_link;
370 char *np;
371 char *np_length;
372 int name_length;
373 char *data;
374 char *orig_time;
375
376
377 if ( nlsr->debugging )
378 printf("LSA Data \n");
379
380 if( strlen((char *)content_data ) > 0 )
381 {
382
383 orig_router=strtok_r((char *)content_data,sep,&rem);
384 orl=strtok_r(NULL,sep,&rem);
385 orig_router_length=atoi(orl);
386
387 if ( nlsr->debugging )
388 {
389 printf(" Orig Router Name : %s\n",orig_router);
390 printf(" Orig Router Length: %d\n",orig_router_length);
391 }
392
393 lst=strtok_r(NULL,sep,&rem);
394 ls_type=atoi(lst);
395
396 if ( nlsr->debugging )
397 printf(" LS Type : %d\n",ls_type);
398
399 if ( ls_type == LS_TYPE_NAME )
400 {
401 lsid=strtok_r(NULL,sep,&rem);
402 ls_id=atoi(lsid);
403 orig_time=strtok_r(NULL,sep,&rem);
404 isvld=strtok_r(NULL,sep,&rem);
405 isValid=atoi(isvld);
406 np=strtok_r(NULL,sep,&rem);
407 np_length=strtok_r(NULL,sep,&rem);
408 name_length=atoi(np_length);
409 if ( nlsr->debugging )
410 {
411 printf(" LS ID : %ld\n",ls_id);
412 printf(" isValid : %d\n",isValid);
413 printf(" Name Prefix : %s\n",np);
414 printf(" Orig Time : %s\n",orig_time);
415 printf(" Name Prefix length: %d\n",name_length);
416 }
417
418 build_and_install_others_name_lsa(orig_router,ls_type,ls_id,orig_time,isValid,np);
419
420 print_name_lsdb();
421
422 }
423 else if ( ls_type == LS_TYPE_ADJ )
424 {
425 orig_time=strtok_r(NULL,sep,&rem);
426 num_link=strtok_r(NULL,sep,&rem);
427 no_link=atoi(num_link);
428 data=rem;
429
430 if ( nlsr->debugging )
431 {
432 printf(" Orig Time : %s\n",orig_time);
433 printf(" No Link : %d\n",no_link);
434 printf(" Data : %s\n",data);
435 }
436 build_and_install_others_adj_lsa(orig_router,ls_type,orig_time,no_link,data);
437 }
438 else if ( ls_type == LS_TYPE_COR )
439 {
440 orig_time=strtok_r(NULL,sep,&rem);
441 char *cor_r=strtok_r(NULL,sep,&rem);
442 char *cor_theta=strtok_r(NULL,sep,&rem);
443
444 double r, theta;
445 r=strtof(cor_r,NULL);
446 theta=strtof(cor_theta,NULL);
447
448 if ( nlsr->debugging )
449 {
450 printf(" Orig Time : %s\n",orig_time);
451 printf(" Cor R : %f\n",r);
452 printf(" Cor Theta : %f\n",theta);
453 }
454 build_and_install_others_cor_lsa(orig_router,ls_type,orig_time, (double)r, (double)theta);
455 }
456
457 }
458}
459
460void
461process_content_from_sync(struct ccn_charbuf *content_name, struct ccn_indexbuf *components)
462{
463 //int lsa_position;
akmhoqueb28579d2013-02-12 11:15:52 -0600464 //int res;
akmhoquea0e71152013-02-11 09:47:59 -0600465 size_t comp_size;
466 char *lst;
467 char *lsid;
468 const unsigned char *second_last_comp;
469 const unsigned char *third_last_comp;
470 const unsigned char *origtime;
471 char *sep=".";
472 char *rem;
473 char *second_comp_type;
474
475 int ls_type;
476 long int ls_id=0;
477
478 unsigned char *content_data = NULL;
479
480 char *time_stamp=(char *)malloc(20);
481 memset(time_stamp,0,20);
482 get_current_timestamp_micro(time_stamp);
483
484 struct ccn_charbuf *uri = ccn_charbuf_create();
485 ccn_uri_append(uri, content_name->buf, content_name->length, 0);
486
487 struct name_prefix *orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix));
488
489
akmhoque7ca519b2013-02-20 09:49:43 -0600490 //struct ccn_indexbuf cid={0};
491 //struct ccn_indexbuf *temp_components=&cid;
492 //struct ccn_charbuf *name=ccn_charbuf_create();
493 //ccn_name_from_uri(name,nlsr->slice_prefix);
494 //ccn_name_split (name, temp_components);
akmhoquea0e71152013-02-11 09:47:59 -0600495 //lsa_position=temp_components->n-2;
akmhoque7ca519b2013-02-20 09:49:43 -0600496 //ccn_charbuf_destroy(&name);
akmhoquea0e71152013-02-11 09:47:59 -0600497
498
499 //res=ccn_name_comp_get(content_name->buf, components,lsa_position+1,&lst, &comp_size);
akmhoqueb28579d2013-02-12 11:15:52 -0600500 ccn_name_comp_get(content_name->buf, components,components->n-2-1,&second_last_comp, &comp_size);
akmhoquea0e71152013-02-11 09:47:59 -0600501 //ls_type=atoi((char *)lst);
akmhoqueb28579d2013-02-12 11:15:52 -0600502 if (nlsr->debugging)
503 printf("2nd Last Component: %s \n",second_last_comp);
504
akmhoquea0e71152013-02-11 09:47:59 -0600505 second_comp_type=strtok_r((char *)second_last_comp,sep,&rem);
506 if ( strcmp( second_comp_type, "lsId" ) == 0 )
507 {
508 lsid=rem;
509 ls_id=atoi(rem);
akmhoqueb28579d2013-02-12 11:15:52 -0600510 ccn_name_comp_get(content_name->buf, components,components->n-2-2,&third_last_comp, &comp_size);
akmhoquea0e71152013-02-11 09:47:59 -0600511 lst=strtok_r((char *)third_last_comp,sep,&rem);
512 lst=rem;
513 ls_type=atoi(lst);
akmhoqueb28579d2013-02-12 11:15:52 -0600514 ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size);
akmhoquea0e71152013-02-11 09:47:59 -0600515 ccn_name_chop(content_name,components,-3);
516 get_name_part(orig_router,content_name,components,0);
517
518 if ( nlsr->debugging )
519 printf("Orig Router: %s Ls Type: %d Ls id: %ld Orig Time: %s\n",orig_router->name,ls_type,ls_id,origtime);
520
521 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
522
523 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) )
524 {
525 int is_new_name_lsa=check_is_new_name_lsa(orig_router->name,(char *)lst,(char *)lsid,(char *)origtime);
526 if ( is_new_name_lsa == 1 )
527 {
528 if ( nlsr->debugging )
529 printf("New NAME LSA.....\n");
530 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
531 if ( nlsr->debugging )
532 printf("Content Data: %s \n",content_data);
533 process_incoming_sync_content_lsa(content_data);
534 }
535 else
536 {
537 if ( nlsr->debugging )
538 printf("Name LSA / Newer Name LSA already xists in LSDB\n");
539 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
540
541 if ( nlsr->debugging )
542 printf("Content Data: %s \n",content_data);
543 }
544 }
545 else
546 {
547 if ( nlsr->debugging )
548 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
549 }
550 }
551 else
552 {
553 ls_type=atoi(rem);
554 lst=rem;
555 if(ls_type == LS_TYPE_ADJ)
556 {
akmhoqueb28579d2013-02-12 11:15:52 -0600557 ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size);
akmhoquea0e71152013-02-11 09:47:59 -0600558 ccn_name_chop(content_name,components,-2);
559 get_name_part(orig_router,content_name,components,0);
560
561 if ( nlsr->debugging )
562 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",orig_router->name,ls_type,origtime);
563
564 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque7ca519b2013-02-20 09:49:43 -0600565 if ( (strcmp((char *)orig_router->name,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) )
akmhoquea0e71152013-02-11 09:47:59 -0600566 {
567 int is_new_adj_lsa=check_is_new_adj_lsa(orig_router->name,(char *)lst,(char *)origtime);
568 if ( is_new_adj_lsa == 1 )
569 {
570 if ( nlsr->debugging )
571 printf("New Adj LSA.....\n");
572 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
573
574 if ( nlsr->debugging )
575 printf("Content Data: %s \n",content_data);
576 process_incoming_sync_content_lsa(content_data);
577 }
578 else
579 {
580 if ( nlsr->debugging )
581 printf("Adj LSA / Newer Adj LSA already exists in LSDB\n");
582 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
583 if ( nlsr->debugging )
584 printf("Content Data: %s \n",content_data);
585 }
586 }
587 else
588 {
589 if ( nlsr->debugging )
590 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
591 }
592 }
593 else if(ls_type == LS_TYPE_COR)
594 {
akmhoqueb28579d2013-02-12 11:15:52 -0600595 ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size);
akmhoquea0e71152013-02-11 09:47:59 -0600596 ccn_name_chop(content_name,components,-2);
597 get_name_part(orig_router,content_name,components,0);
598
599 if ( nlsr->debugging )
600 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",orig_router->name,ls_type,origtime);
601
602 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
603 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) )
604 {
605 int is_new_cor_lsa=check_is_new_cor_lsa(orig_router->name,(char *)lst,(char *)origtime);
606 if ( is_new_cor_lsa == 1 )
607 {
608 if ( nlsr->debugging )
609 printf("New Cor LSA.....\n");
610 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
611
612 if ( nlsr->debugging )
613 printf("Content Data: %s \n",content_data);
614 process_incoming_sync_content_lsa(content_data);
615 }
616 else
617 {
618 if ( nlsr->debugging )
619 printf("Cor LSA / Newer Cor LSA already exists in LSDB\n");
620 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
621 if ( nlsr->debugging )
622 printf("Content Data: %s \n",content_data);
623 }
624 }
625 else
626 {
627 if ( nlsr->debugging )
628 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
629 }
630
631 }
632 }
633
akmhoque866c2222013-02-12 10:49:33 -0600634 //if (content_data != NULL)
635 //free(content_data);
akmhoquea0e71152013-02-11 09:47:59 -0600636 ccn_charbuf_destroy(&uri);
637 //01/31/2013
638 free(time_stamp);
639}
640
akmhoque866c2222013-02-12 10:49:33 -0600641int
akmhoquea0e71152013-02-11 09:47:59 -0600642sync_monitor(char *topo_prefix, char *slice_prefix)
643{
644
akmhoque0678c5d2013-02-18 11:03:31 -0600645 static struct ccns_name_closure nc={0};
akmhoquea0e71152013-02-11 09:47:59 -0600646 nlsr->closure = &nc;
647 struct ccn_charbuf *prefix = ccn_charbuf_create();
648 struct ccn_charbuf *roothash = NULL;
649 struct ccn_charbuf *topo = ccn_charbuf_create();
650 nlsr->slice = ccns_slice_create();
651 ccn_charbuf_reset(prefix);
652 ccn_charbuf_reset(topo);
653
654 ccn_charbuf_reset(prefix);
655 ccn_name_from_uri(prefix, slice_prefix);
656
657 ccn_charbuf_reset(topo);
658 ccn_name_from_uri(topo, topo_prefix);
659
akmhoquea0e71152013-02-11 09:47:59 -0600660 ccns_slice_set_topo_prefix(nlsr->slice, topo, prefix);
661 nlsr->closure->callback = &sync_cb;
662 nlsr->ccns = ccns_open(nlsr->ccn, nlsr->slice, nlsr->closure, roothash, NULL);
663
664 //01/31/2013
665 ccn_charbuf_destroy(&prefix);
666 ccn_charbuf_destroy(&topo);
akmhoque866c2222013-02-12 10:49:33 -0600667 ccn_charbuf_destroy(&roothash);
668 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600669}
670
671struct ccn_charbuf *
672make_template(int scope)
673{
674 struct ccn_charbuf *templ = NULL;
675 templ = ccn_charbuf_create();
676 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
677 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
678 ccn_charbuf_append_closer(templ); /* </Name> */
679 if (0 <= scope && scope <= 2)
680 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
681 ccn_charbuf_append_closer(templ); /* </Interest> */
682 return(templ);
683}
684
685int
686write_data_to_repo(char *data, char *name_prefix)
687{
688 if ( nlsr->debugging )
689 {
690 printf("write_data_to_repo called\n");
691 printf("Content Name: %s \n",name_prefix);
692 printf("Content Data: %s \n",data);
693 }
694
695 struct ccn *temp_ccn;
696 temp_ccn=ccn_create();
697 int ccn_fd=ccn_connect(temp_ccn, NULL);
698 if(ccn_fd == -1)
699 {
700 fprintf(stderr,"Could not connect to ccnd for Data Writing\n");
701 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd for Data Writing\n");
702 return -1;
703 }
704 struct ccn_charbuf *name = NULL;
705 struct ccn_seqwriter *w = NULL;
706 int blocksize = 4096;
707 int freshness = -1;
708 int torepo = 1;
709 int scope = 1;
710 int res;
711 size_t blockread;
712 struct ccn_charbuf *templ;
713
714 name = ccn_charbuf_create();
715 res = ccn_name_from_uri(name, name_prefix);
716 if (res < 0) {
717 fprintf(stderr, "bad CCN URI: %s\n",name_prefix);
718 return -1;
719 }
720
721
722 w = ccn_seqw_create(temp_ccn, name);
723 if (w == NULL) {
724 fprintf(stderr, "ccn_seqw_create failed\n");
725 return -1;
726 }
727 ccn_seqw_set_block_limits(w, blocksize, blocksize);
728 if (freshness > -1)
729 ccn_seqw_set_freshness(w, freshness);
730 if (torepo) {
731 struct ccn_charbuf *name_v = ccn_charbuf_create();
732 ccn_seqw_get_name(w, name_v);
733 ccn_name_from_uri(name_v, "%C1.R.sw");
734 ccn_name_append_nonce(name_v);
735 templ = make_template(scope);
736 res = ccn_get(temp_ccn, name_v, templ, 60000, NULL, NULL, NULL, 0);
737 ccn_charbuf_destroy(&templ);
738 ccn_charbuf_destroy(&name_v);
739 if (res < 0) {
740 fprintf(stderr, "No response from repository\n");
741 return -1;
742 }
743 }
744
745
746
747
748 blockread = 0;
749
750
751 blockread=strlen(data);
752
753 if (blockread > 0) {
754 ccn_run(temp_ccn, 100);
755 res = ccn_seqw_write(w, data, blockread);
756 while (res == -1) {
757 ccn_run(temp_ccn, 100);
758 res = ccn_seqw_write(w, data, blockread);
759 }
760 }
761
762 ccn_seqw_close(w);
763 ccn_run(temp_ccn, 100);
764 ccn_charbuf_destroy(&name);
765 ccn_destroy(&temp_ccn);
766
767 return 0;
768}
769
770
771int
772create_sync_slice(char *topo_prefix, char *slice_prefix)
773{
774 int res;
akmhoque866c2222013-02-12 10:49:33 -0600775 struct ccn *handle;
akmhoquea0e71152013-02-11 09:47:59 -0600776 struct ccns_slice *slice;
777 struct ccn_charbuf *prefix = ccn_charbuf_create();
778 struct ccn_charbuf *topo = ccn_charbuf_create();
779 struct ccn_charbuf *clause = ccn_charbuf_create();
780 struct ccn_charbuf *slice_name = ccn_charbuf_create();
781 struct ccn_charbuf *slice_uri = ccn_charbuf_create();
782
783 if (prefix == NULL || topo == NULL || clause == NULL ||
784 slice_name == NULL || slice_uri == NULL) {
785 fprintf(stderr, "Unable to allocate required memory.\n");
786 return -1;
787 }
788
akmhoque866c2222013-02-12 10:49:33 -0600789 handle = ccn_create();
790 res = ccn_connect(handle, NULL);
791 if (0 > res) {
792 fprintf(stderr, "Unable to connect to ccnd.\n");
793 return -1;
794 }
795
akmhoquea0e71152013-02-11 09:47:59 -0600796 slice = ccns_slice_create();
797
798 ccn_charbuf_reset(topo);
799 ccn_name_from_uri(topo, topo_prefix);
800 ccn_charbuf_reset(prefix);
801 ccn_name_from_uri(prefix,slice_prefix );
802 ccns_slice_set_topo_prefix(slice, topo, prefix);
803
804
akmhoque866c2222013-02-12 10:49:33 -0600805 res = ccns_write_slice(handle, slice, slice_name);
akmhoquea0e71152013-02-11 09:47:59 -0600806
807 //01/31/2013
808 ccns_slice_destroy(&slice);
akmhoque866c2222013-02-12 10:49:33 -0600809 ccn_destroy(&handle);
akmhoquea0e71152013-02-11 09:47:59 -0600810 ccn_charbuf_destroy(&prefix);
811 ccn_charbuf_destroy(&topo);
812 ccn_charbuf_destroy(&clause);
813 ccn_charbuf_destroy(&slice_name);
814 ccn_charbuf_destroy(&slice_uri);
815
816 return 0;
817}
818