blob: 289cf394b09618fb73f048d8f2d3cbbbfbed6fc7 [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"
Syed Obaid Amin3a3864b2013-01-09 03:05:50 -060032//test method
33char *
34hex_string(unsigned char *s, size_t l)
35{
36 const char *hex_digits = "0123456789abcdef";
37 char *r;
38 int i;
39 r = calloc(1, 1 + 2 * l);
40 for (i = 0; i < l; i++) {
41 r[2*i] = hex_digits[(s[i]>>4) & 0xf];
42 r[1+2*i] = hex_digits[s[i] & 0xf];
43 }
44 return(r);
45}
46
47int
48sync_cb(struct ccns_name_closure *nc,
49 struct ccn_charbuf *lhash,
50 struct ccn_charbuf *rhash,
51 struct ccn_charbuf *name)
52{
53 char *hexL;
54 char *hexR;
55 struct ccn_charbuf *uri = ccn_charbuf_create();
56 if (lhash == NULL || lhash->length == 0) {
57 hexL = strdup("none");
58 } else
59 hexL = hex_string(lhash->buf, lhash->length);
60 if (rhash == NULL || rhash->length == 0) {
61 hexR = strdup("none");
62 } else
63 hexR = hex_string(rhash->buf, rhash->length);
64 if (name != NULL)
65 ccn_uri_append(uri, name->buf, name->length, 1);
66 else
67 ccn_charbuf_append_string(uri, "(null)");
a64adam04be6482013-01-17 11:29:32 -060068
akmhoquefc5176d2013-01-18 09:50:12 -060069 //if ( nlsr->debugging )
70 //printf("%s %s %s\n", ccn_charbuf_as_string(uri), hexL, hexR);
71
a64adam04be6482013-01-17 11:29:32 -060072 if ( nlsr->debugging )
akmhoquefc5176d2013-01-18 09:50:12 -060073 printf("Response from sync in the name: %s \n",ccn_charbuf_as_string(uri));
74
Syed Obaid Amin3a3864b2013-01-09 03:05:50 -060075 fflush(stdout);
76 free(hexL);
77 free(hexR);
78 ccn_charbuf_destroy(&uri);
akmhoqued6cd61d2013-01-17 10:31:15 -060079
80
81 //--Doing ourthing from here
akmhoqueb29edd82013-01-14 20:54:11 -060082 struct ccn_indexbuf cid={0};
Syed Obaid Amin3a3864b2013-01-09 03:05:50 -060083
84 struct ccn_indexbuf *components=&cid;
85 ccn_name_split (name, components);
86 ccn_name_chop(name,components,-3);
87
88 process_content_from_sync(name,components);
89
akmhoquefc5176d2013-01-18 09:50:12 -060090
Syed Obaid Amin3a3864b2013-01-09 03:05:50 -060091
92
93 return(0);
94}
95//test method
akmhoque8fdd6412012-12-04 15:05:33 -060096
97int
98get_lsa_position(struct ccn_charbuf * ccnb, struct ccn_indexbuf *comps)
99{
100
101
102
103 int res,i;
Obaid Amin512c08f2013-01-17 14:11:59 -0600104 int lsa_position=0; //Obaid: Hoque make it -1, 0 is also an index
105 // I didn't change it as it might break the code.
akmhoque8fdd6412012-12-04 15:05:33 -0600106 int name_comps=(int)comps->n;
107
108 for(i=0;i<name_comps;i++)
109 {
110 res=ccn_name_comp_strcmp(ccnb->buf,comps,i,"LSA");
111 if( res == 0)
112 {
113 lsa_position=i;
114 break;
115 }
116 }
117
118 return lsa_position;
119
120}
121
122void
123get_name_part(struct name_prefix *name_part,struct ccn_charbuf * interest_ccnb, struct ccn_indexbuf *interest_comps, int offset)
124{
125
126
127
128 int res,i;
129 int lsa_position=0;
130 int len=0;
131
akmhoque2ee18032013-01-22 10:01:23 -0600132 //lsa_position=get_lsa_position(interest_ccnb,interest_comps);
133
134 //printf("LSA Position: %d \n",lsa_position);
135
136
137 struct ccn_indexbuf cid={0};
138 struct ccn_indexbuf *components=&cid;
139 struct ccn_charbuf *name=ccn_charbuf_create();
140 ccn_name_from_uri(name,nlsr->slice_prefix);
141 ccn_name_split (name, components);
142 lsa_position=components->n-2;
143 //printf("LSA Position from Slice Prefix Component: %d \n",(int)components->n-2);
144 ccn_charbuf_destroy(&name);
145
akmhoque8fdd6412012-12-04 15:05:33 -0600146
147 const unsigned char *comp_ptr1;
148 size_t comp_size;
149 for(i=lsa_position+1+offset;i<interest_comps->n-1;i++)
150 {
151 res=ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1, &comp_size);
152 len+=1;
153 len+=(int)comp_size;
154 }
155 len++;
156
157 char *neighbor=(char *)malloc(len);
158 memset(neighbor,0,len);
159
160 for(i=lsa_position+1+offset; i<interest_comps->n-1;i++)
161 {
162 res=ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1, &comp_size);
163 memcpy(neighbor+strlen(neighbor),"/",1);
164 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,strlen((char *)comp_ptr1));
165
166 }
167
168 name_part->name=(char *)malloc(strlen(neighbor)+1);
169 memset(name_part->name,0,strlen(neighbor)+1);
170 memcpy(name_part->name,neighbor,strlen(neighbor)+1);
171 name_part->length=strlen(neighbor)+1;
172
173
174}
175
akmhoque09c0afa2012-12-14 09:27:00 -0600176void
177get_host_name_from_command_string(struct name_prefix *name_part,char *nbr_name_uri, int offset)
178{
179
180
181
182 int res,i;
183 int len=0;
184 const unsigned char *comp_ptr1;
185 size_t comp_size;
186
187 struct ccn_charbuf *name=ccn_charbuf_create();
188 name = ccn_charbuf_create();
189 res = ccn_name_from_uri(name,nbr_name_uri);
190 if (res < 0) {
191 fprintf(stderr, "Bad ccn URI: %s\n", nbr_name_uri);
192 exit(1);
193 }
194
195 struct ccn_indexbuf cid={0};
196
197 struct ccn_indexbuf *components=&cid;
198 ccn_name_split (name, components);
199
200 for(i=components->n-2;i> (0+offset);i--)
201 {
202 res=ccn_name_comp_get(name->buf, components,i,&comp_ptr1, &comp_size);
203 len+=1;
204 len+=(int)comp_size;
205 }
206 len++;
207
208 char *neighbor=(char *)malloc(len);
209 memset(neighbor,0,len);
210
211 for(i=components->n-2;i> (0+offset);i--)
212 {
213 res=ccn_name_comp_get(name->buf, components,i,&comp_ptr1, &comp_size);
214 if ( i != components->n-2)
215 memcpy(neighbor+strlen(neighbor),".",1);
216 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,strlen((char *)comp_ptr1));
217
218 }
219
220 name_part->name=(char *)malloc(strlen(neighbor)+1);
221 memset(name_part->name,0,strlen(neighbor)+1);
222 memcpy(name_part->name,neighbor,strlen(neighbor)+1);
223 name_part->length=strlen(neighbor)+1;
224
225
226}
227
akmhoque8fdd6412012-12-04 15:05:33 -0600228
akmhoqueed418f32013-01-30 12:25:04 -0600229
soamin15043622013-01-15 23:10:26 -0600230void
Obaid Amine63022a2013-01-15 23:36:40 -0600231get_content_by_content_name(char *content_name, unsigned char **content_data)
akmhoque8fdd6412012-12-04 15:05:33 -0600232{
233
soamin15043622013-01-15 23:10:26 -0600234 struct ccn_charbuf *name = NULL;
235 struct ccn_charbuf *templ = NULL;
236 struct ccn_charbuf *resultbuf = NULL;
237 struct ccn_parsed_ContentObject pcobuf = { 0 };
238 int res;
239 int allow_stale = 0;
240 int content_only = 1;
241 int scope = -1;
242 const unsigned char *ptr;
243 size_t length;
244 int resolve_version = CCN_V_HIGHEST;
245 int timeout_ms = 3000;
246 const unsigned lifetime_default = CCN_INTEREST_LIFETIME_SEC << 12;
247 unsigned lifetime_l12 = lifetime_default;
248 int get_flags = 0;
akmhoque8fdd6412012-12-04 15:05:33 -0600249
soamin15043622013-01-15 23:10:26 -0600250 name = ccn_charbuf_create();
251 res = ccn_name_from_uri(name,content_name);
252 if (res < 0) {
253 fprintf(stderr, "Bad ccn URI: %s\n", content_name);
254 exit(1);
255 }
256
akmhoque8fdd6412012-12-04 15:05:33 -0600257 if (allow_stale || lifetime_l12 != lifetime_default || scope != -1) {
soamin15043622013-01-15 23:10:26 -0600258 templ = ccn_charbuf_create();
259 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
260 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
261 ccn_charbuf_append_closer(templ); /* </Name> */
akmhoque8fdd6412012-12-04 15:05:33 -0600262 if (allow_stale) {
263 ccn_charbuf_append_tt(templ, CCN_DTAG_AnswerOriginKind, CCN_DTAG);
264 ccnb_append_number(templ,
soamin15043622013-01-15 23:10:26 -0600265 CCN_AOK_DEFAULT | CCN_AOK_STALE);
akmhoque8fdd6412012-12-04 15:05:33 -0600266 ccn_charbuf_append_closer(templ); /* </AnswerOriginKind> */
267 }
soamin15043622013-01-15 23:10:26 -0600268 if (scope != -1) {
269 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
270 }
akmhoque8fdd6412012-12-04 15:05:33 -0600271 if (lifetime_l12 != lifetime_default) {
272 /*
273 * Choose the interest lifetime so there are at least 3
274 * expressions (in the unsatisfied case).
275 */
276 unsigned char buf[3] = { 0 };
277 int i;
278 for (i = sizeof(buf) - 1; i >= 0; i--, lifetime_l12 >>= 8)
279 buf[i] = lifetime_l12 & 0xff;
280 ccnb_append_tagged_blob(templ, CCN_DTAG_InterestLifetime, buf, sizeof(buf));
281 }
soamin15043622013-01-15 23:10:26 -0600282 ccn_charbuf_append_closer(templ); /* </Interest> */
283 }
284 resultbuf = ccn_charbuf_create();
285 if (resolve_version != 0) {
286 res = ccn_resolve_version(nlsr->ccn, name, resolve_version, 500);
287 if (res >= 0) {
288 ccn_uri_append(resultbuf, name->buf, name->length, 1);
289 //fprintf(stderr, "== %s\n",ccn_charbuf_as_string(resultbuf));
290 resultbuf->length = 0;
291 }
292 }
293 res = ccn_get(nlsr->ccn, name, templ, timeout_ms, resultbuf, &pcobuf, NULL, get_flags);
294 if (res >= 0) {
295 ptr = resultbuf->buf;
296 length = resultbuf->length;
297 if (content_only){
298 ccn_content_get_value(ptr, length, &pcobuf, &ptr, &length);
Obaid Amine63022a2013-01-15 23:36:40 -0600299 *content_data = (unsigned char *) calloc(length, sizeof(char *));
300 memcpy (*content_data, ptr, length);
soamin15043622013-01-15 23:10:26 -0600301 }
302 }
303 ccn_charbuf_destroy(&resultbuf);
304 ccn_charbuf_destroy(&templ);
305 ccn_charbuf_destroy(&name);
306 //return (unsigned char *)ptr;
akmhoque8fdd6412012-12-04 15:05:33 -0600307}
308
akmhoque4ae16942012-12-10 11:50:43 -0600309void
soamin15043622013-01-15 23:10:26 -0600310process_incoming_sync_content_lsa( unsigned char *content_data)
akmhoque4ae16942012-12-10 11:50:43 -0600311{
312
313
314 if ( nlsr->debugging )
akmhoqueed418f32013-01-30 12:25:04 -0600315 printf("process_incoming_sync_content_lsa called \n");
akmhoque4ae16942012-12-10 11:50:43 -0600316
317 char *sep="|";
318 char *rem;
319 char *orig_router;
320 char *orl;
321 int orig_router_length;
322 char *lst;
323 int ls_type;
324 char *lsid;
325 long int ls_id;
326 char *isvld;
327 int isValid;
328 char *num_link;
329 int no_link;
330 char *np;
331 char *np_length;
332 int name_length;
333 char *data;
334 char *orig_time;
335
336
337 if ( nlsr->debugging )
338 printf("LSA Data \n");
akmhoque4ae16942012-12-10 11:50:43 -0600339
soamin15043622013-01-15 23:10:26 -0600340 if( strlen((char *)content_data ) > 0 )
akmhoque4ae16942012-12-10 11:50:43 -0600341 {
342
343 orig_router=strtok_r((char *)content_data,sep,&rem);
344 orl=strtok_r(NULL,sep,&rem);
345 orig_router_length=atoi(orl);
346
347 if ( nlsr->debugging )
348 {
349 printf(" Orig Router Name : %s\n",orig_router);
350 printf(" Orig Router Length: %d\n",orig_router_length);
351 }
352
353 lst=strtok_r(NULL,sep,&rem);
354 ls_type=atoi(lst);
355
356 if ( nlsr->debugging )
357 printf(" LS Type : %d\n",ls_type);
358
359 if ( ls_type == LS_TYPE_NAME )
360 {
361 lsid=strtok_r(NULL,sep,&rem);
362 ls_id=atoi(lsid);
363 orig_time=strtok_r(NULL,sep,&rem);
364 isvld=strtok_r(NULL,sep,&rem);
365 isValid=atoi(isvld);
366 np=strtok_r(NULL,sep,&rem);
367 np_length=strtok_r(NULL,sep,&rem);
368 name_length=atoi(np_length);
369 if ( nlsr->debugging )
370 {
371 printf(" LS ID : %ld\n",ls_id);
372 printf(" isValid : %d\n",isValid);
373 printf(" Name Prefix : %s\n",np);
374 printf(" Orig Time : %s\n",orig_time);
375 printf(" Name Prefix length: %d\n",name_length);
376 }
377
378 build_and_install_others_name_lsa(orig_router,ls_type,ls_id,orig_time,isValid,np);
379
380 print_name_lsdb();
381
382 }
383 else if ( ls_type == LS_TYPE_ADJ )
384 {
385 orig_time=strtok_r(NULL,sep,&rem);
386 num_link=strtok_r(NULL,sep,&rem);
387 no_link=atoi(num_link);
388 data=rem;
389
390 if ( nlsr->debugging )
391 {
akmhoqueed418f32013-01-30 12:25:04 -0600392 printf(" Orig Time : %s\n",orig_time);
akmhoque4ae16942012-12-10 11:50:43 -0600393 printf(" No Link : %d\n",no_link);
394 printf(" Data : %s\n",data);
395 }
396 build_and_install_others_adj_lsa(orig_router,ls_type,orig_time,no_link,data);
397 }
akmhoqueed418f32013-01-30 12:25:04 -0600398 else if ( ls_type == LS_TYPE_COR )
399 {
400 orig_time=strtok_r(NULL,sep,&rem);
401 char *cor_r=strtok_r(NULL,sep,&rem);
402 char *cor_theta=strtok_r(NULL,sep,&rem);
403
404 double r, theta;
405 r=strtof(cor_r,NULL);
406 theta=strtof(cor_theta,NULL);
407
408 if ( nlsr->debugging )
409 {
410 printf(" Orig Time : %s\n",orig_time);
411 printf(" Cor R : %f\n",r);
412 printf(" Cor Theta : %f\n",theta);
413 }
414 build_and_install_others_cor_lsa(orig_router,ls_type,orig_time, (double)r, (double)theta);
415 }
416
akmhoque4ae16942012-12-10 11:50:43 -0600417 }
418}
419
akmhoque8fdd6412012-12-04 15:05:33 -0600420void
421process_content_from_sync(struct ccn_charbuf *content_name, struct ccn_indexbuf *components)
422{
423 int lsa_position;
424 int res;
425 size_t comp_size;
426 const unsigned char *lst;
427 const unsigned char *lsid;
428 const unsigned char *origtime;
429
430 int ls_type;
431 long int ls_id=0;
432
soamin15043622013-01-15 23:10:26 -0600433 unsigned char *content_data = NULL;
434
akmhoque4ae16942012-12-10 11:50:43 -0600435 char *time_stamp=(char *)malloc(20);
436 memset(time_stamp,0,20);
437 get_current_timestamp_micro(time_stamp);
438
akmhoque8fdd6412012-12-04 15:05:33 -0600439 struct ccn_charbuf *uri = ccn_charbuf_create();
440 ccn_uri_append(uri, content_name->buf, content_name->length, 0);
441
442 struct name_prefix *orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix));
443
akmhoque2ee18032013-01-22 10:01:23 -0600444
445 struct ccn_indexbuf cid={0};
446 struct ccn_indexbuf *temp_components=&cid;
447 struct ccn_charbuf *name=ccn_charbuf_create();
448 ccn_name_from_uri(name,nlsr->slice_prefix);
449 ccn_name_split (name, temp_components);
450 lsa_position=temp_components->n-2;
akmhoque2ee18032013-01-22 10:01:23 -0600451 ccn_charbuf_destroy(&name);
452
akmhoque8fdd6412012-12-04 15:05:33 -0600453
454 res=ccn_name_comp_get(content_name->buf, components,lsa_position+1,&lst, &comp_size);
455
akmhoque8fdd6412012-12-04 15:05:33 -0600456 ls_type=atoi((char *)lst);
457 if(ls_type == LS_TYPE_NAME)
458 {
akmhoque4ae16942012-12-10 11:50:43 -0600459
akmhoque8fdd6412012-12-04 15:05:33 -0600460 res=ccn_name_comp_get(content_name->buf, components,lsa_position+2,&lsid, &comp_size);
461 ls_id=atoi((char *)lsid);
462 res=ccn_name_comp_get(content_name->buf, components,lsa_position+3,&origtime, &comp_size);
463 get_name_part(orig_router,content_name,components,3);
akmhoque8fdd6412012-12-04 15:05:33 -0600464
akmhoque4ae16942012-12-10 11:50:43 -0600465 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
466
akmhoque4ae16942012-12-10 11:50:43 -0600467 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 -0600468 {
akmhoque4ae16942012-12-10 11:50:43 -0600469 int is_new_name_lsa=check_is_new_name_lsa(orig_router->name,(char *)lst,(char *)lsid,(char *)origtime);
470 if ( is_new_name_lsa == 1 )
471 {
a64adam04be6482013-01-17 11:29:32 -0600472 if ( nlsr->debugging )
473 printf("New NAME LSA.....\n");
Obaid Amine63022a2013-01-15 23:36:40 -0600474 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
a64adamf7b051d2013-01-17 11:39:58 -0600475 if ( nlsr->debugging )
476 printf("Content Data: %s \n",content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600477 process_incoming_sync_content_lsa(content_data);
478 }
479 else
480 {
a64adam04be6482013-01-17 11:29:32 -0600481 if ( nlsr->debugging )
482 printf("Name LSA / Newer Name LSA already xists in LSDB\n");
Obaid Amine63022a2013-01-15 23:36:40 -0600483 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
a64adam04be6482013-01-17 11:29:32 -0600484
485 if ( nlsr->debugging )
486 printf("Content Data: %s \n",content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600487 }
akmhoque8fdd6412012-12-04 15:05:33 -0600488 }
489 else
490 {
a64adam04be6482013-01-17 11:29:32 -0600491 if ( nlsr->debugging )
492 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
akmhoque8fdd6412012-12-04 15:05:33 -0600493 }
494 }
495 else if(ls_type == LS_TYPE_ADJ)
496 {
497 res=ccn_name_comp_get(content_name->buf, components,lsa_position+2,&origtime, &comp_size);
498 get_name_part(orig_router,content_name,components,2);
a64adam04be6482013-01-17 11:29:32 -0600499
500 if ( nlsr->debugging )
501 printf("Orig Time: %s\nOrig Router: %s\n",origtime,orig_router->name);
akmhoque8fdd6412012-12-04 15:05:33 -0600502
akmhoque4ae16942012-12-10 11:50:43 -0600503 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque4ae16942012-12-10 11:50:43 -0600504 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) )
505 {
506 int is_new_adj_lsa=check_is_new_adj_lsa(orig_router->name,(char *)lst,(char *)origtime);
507 if ( is_new_adj_lsa == 1 )
508 {
a64adam04be6482013-01-17 11:29:32 -0600509 if ( nlsr->debugging )
510 printf("New Adj LSA.....\n");
Obaid Amine63022a2013-01-15 23:36:40 -0600511 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
a64adam04be6482013-01-17 11:29:32 -0600512
513 if ( nlsr->debugging )
514 printf("Content Data: %s \n",content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600515 process_incoming_sync_content_lsa(content_data);
516 }
517 else
518 {
a64adam04be6482013-01-17 11:29:32 -0600519 if ( nlsr->debugging )
520 printf("Adj LSA / Newer Adj LSA already exists in LSDB\n");
Obaid Amine63022a2013-01-15 23:36:40 -0600521 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
a64adam04be6482013-01-17 11:29:32 -0600522 if ( nlsr->debugging )
523 printf("Content Data: %s \n",content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600524 }
525 }
526 else
527 {
a64adam04be6482013-01-17 11:29:32 -0600528 if ( nlsr->debugging )
529 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
akmhoque4ae16942012-12-10 11:50:43 -0600530 }
akmhoque8fdd6412012-12-04 15:05:33 -0600531 }
akmhoqueed418f32013-01-30 12:25:04 -0600532 else if(ls_type == LS_TYPE_COR)
533 {
534 res=ccn_name_comp_get(content_name->buf, components,lsa_position+2,&origtime, &comp_size);
535 get_name_part(orig_router,content_name,components,2);
536
537 if ( nlsr->debugging )
538 printf("Orig Time: %s\nOrig Router: %s\n",origtime,orig_router->name);
539
540 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
541 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) )
542 {
543 int is_new_cor_lsa=check_is_new_cor_lsa(orig_router->name,(char *)lst,(char *)origtime);
544 if ( is_new_cor_lsa == 1 )
545 {
546 if ( nlsr->debugging )
547 printf("New Cor LSA.....\n");
548 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
549
550 if ( nlsr->debugging )
551 printf("Content Data: %s \n",content_data);
552 process_incoming_sync_content_lsa(content_data);
553 }
554 else
555 {
556 if ( nlsr->debugging )
557 printf("Cor LSA / Newer Cor LSA already exists in LSDB\n");
558 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
559 if ( nlsr->debugging )
560 printf("Content Data: %s \n",content_data);
561 }
562 }
563 else
564 {
565 if ( nlsr->debugging )
566 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
567 }
568
569 }
akmhoque8fdd6412012-12-04 15:05:33 -0600570
Obaid Amine63022a2013-01-15 23:36:40 -0600571 if (content_data != NULL)
572 free(content_data);
akmhoque8fdd6412012-12-04 15:05:33 -0600573 ccn_charbuf_destroy(&uri);
akmhoque8fdd6412012-12-04 15:05:33 -0600574}
575
576int
577sync_callback(struct ccns_name_closure *nc,
578 struct ccn_charbuf *lhash,
579 struct ccn_charbuf *rhash,
580 struct ccn_charbuf *name)
581{
582
583
584 struct ccn_indexbuf cid={0};
585
586 struct ccn_indexbuf *components=&cid;
587 ccn_name_split (name, components);
588 ccn_name_chop(name,components,-3);
589
590 process_content_from_sync(name,components);
591
592 return(0);
593}
594
595
596void
597sync_monitor(char *topo_prefix, char *slice_prefix)
598{
599
600 static struct ccns_name_closure nc={0};
601
602 nlsr->closure = &nc;
603 struct ccn_charbuf *prefix = ccn_charbuf_create();
604 struct ccn_charbuf *roothash = NULL;
605 struct ccn_charbuf *topo = ccn_charbuf_create();
606 nlsr->slice = ccns_slice_create();
607 ccn_charbuf_reset(prefix);
608 ccn_charbuf_reset(topo);
609
610 ccn_charbuf_reset(prefix);
611 ccn_name_from_uri(prefix, slice_prefix);
612
613 ccn_charbuf_reset(topo);
614 ccn_name_from_uri(topo, topo_prefix);
615
616
617 ccns_slice_set_topo_prefix(nlsr->slice, topo, prefix);
Syed Obaid Amin3a3864b2013-01-09 03:05:50 -0600618 nlsr->closure->callback = &sync_cb;
619 //nlsr->closure->callback = &sync_callback;
akmhoque8fdd6412012-12-04 15:05:33 -0600620 nlsr->ccns = ccns_open(nlsr->ccn, nlsr->slice, nlsr->closure, roothash, NULL);
akmhoque8fdd6412012-12-04 15:05:33 -0600621}
622
623struct ccn_charbuf *
624make_template(int scope)
625{
626 struct ccn_charbuf *templ = NULL;
627 templ = ccn_charbuf_create();
628 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
629 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
630 ccn_charbuf_append_closer(templ); /* </Name> */
631 if (0 <= scope && scope <= 2)
632 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
633 ccn_charbuf_append_closer(templ); /* </Interest> */
634 return(templ);
635}
636
akmhoquefc5176d2013-01-18 09:50:12 -0600637int
akmhoque8fdd6412012-12-04 15:05:33 -0600638write_data_to_repo(char *data, char *name_prefix)
639{
akmhoque260698b2013-01-17 03:54:09 -0600640 if ( nlsr->debugging )
641 {
642 printf("write_data_to_repo called\n");
akmhoque47d19d12013-01-17 04:00:32 -0600643 printf("Content Name: %s \n",name_prefix);
akmhoque260698b2013-01-17 03:54:09 -0600644 printf("Content Data: %s \n",data);
645 }
akmhoque8fdd6412012-12-04 15:05:33 -0600646
akmhoque2ee18032013-01-22 10:01:23 -0600647 struct ccn *temp_ccn;
648 temp_ccn=ccn_create();
649 int ccn_fd=ccn_connect(temp_ccn, NULL);
650 if(ccn_fd == -1)
651 {
652 fprintf(stderr,"Could not connect to ccnd for Data Writing\n");
653 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd for Data Writing\n");
654 return -1;
655 }
akmhoque8fdd6412012-12-04 15:05:33 -0600656 struct ccn_charbuf *name = NULL;
657 struct ccn_seqwriter *w = NULL;
akmhoque2ee18032013-01-22 10:01:23 -0600658 int blocksize = 4096;
akmhoque8fdd6412012-12-04 15:05:33 -0600659 int freshness = -1;
660 int torepo = 1;
661 int scope = 1;
662 int res;
663 size_t blockread;
664 struct ccn_charbuf *templ;
665
666 name = ccn_charbuf_create();
667 res = ccn_name_from_uri(name, name_prefix);
668 if (res < 0) {
669 fprintf(stderr, "bad CCN URI: %s\n",name_prefix);
akmhoquefc5176d2013-01-18 09:50:12 -0600670 return -1;
akmhoque8fdd6412012-12-04 15:05:33 -0600671 }
672
673
akmhoque2ee18032013-01-22 10:01:23 -0600674 w = ccn_seqw_create(temp_ccn, name);
akmhoque8fdd6412012-12-04 15:05:33 -0600675 if (w == NULL) {
676 fprintf(stderr, "ccn_seqw_create failed\n");
akmhoquefc5176d2013-01-18 09:50:12 -0600677 return -1;
akmhoque8fdd6412012-12-04 15:05:33 -0600678 }
679 ccn_seqw_set_block_limits(w, blocksize, blocksize);
680 if (freshness > -1)
681 ccn_seqw_set_freshness(w, freshness);
682 if (torepo) {
683 struct ccn_charbuf *name_v = ccn_charbuf_create();
684 ccn_seqw_get_name(w, name_v);
685 ccn_name_from_uri(name_v, "%C1.R.sw");
686 ccn_name_append_nonce(name_v);
687 templ = make_template(scope);
akmhoque2ee18032013-01-22 10:01:23 -0600688 res = ccn_get(temp_ccn, name_v, templ, 60000, NULL, NULL, NULL, 0);
akmhoque8fdd6412012-12-04 15:05:33 -0600689 ccn_charbuf_destroy(&templ);
690 ccn_charbuf_destroy(&name_v);
691 if (res < 0) {
692 fprintf(stderr, "No response from repository\n");
akmhoquefc5176d2013-01-18 09:50:12 -0600693 return -1;
akmhoque8fdd6412012-12-04 15:05:33 -0600694 }
695 }
696
697
698
699
700 blockread = 0;
akmhoque260698b2013-01-17 03:54:09 -0600701
akmhoque8fdd6412012-12-04 15:05:33 -0600702
703 blockread=strlen(data);
704
705 if (blockread > 0) {
akmhoque2ee18032013-01-22 10:01:23 -0600706 ccn_run(temp_ccn, 100);
akmhoque8fdd6412012-12-04 15:05:33 -0600707 res = ccn_seqw_write(w, data, blockread);
akmhoque260698b2013-01-17 03:54:09 -0600708 while (res == -1) {
akmhoque2ee18032013-01-22 10:01:23 -0600709 ccn_run(temp_ccn, 100);
akmhoque260698b2013-01-17 03:54:09 -0600710 res = ccn_seqw_write(w, data, blockread);
711 }
akmhoque8fdd6412012-12-04 15:05:33 -0600712 }
713
akmhoque8fdd6412012-12-04 15:05:33 -0600714 ccn_seqw_close(w);
akmhoque2ee18032013-01-22 10:01:23 -0600715 ccn_run(temp_ccn, 100);
akmhoque8fdd6412012-12-04 15:05:33 -0600716 ccn_charbuf_destroy(&name);
akmhoque2ee18032013-01-22 10:01:23 -0600717 ccn_destroy(&temp_ccn);
akmhoquefc5176d2013-01-18 09:50:12 -0600718
719 return 0;
akmhoque8fdd6412012-12-04 15:05:33 -0600720}
721
722
723int
724create_sync_slice(char *topo_prefix, char *slice_prefix)
725{
726 int res;
727 struct ccns_slice *slice;
728 struct ccn_charbuf *prefix = ccn_charbuf_create();
729 struct ccn_charbuf *topo = ccn_charbuf_create();
730 struct ccn_charbuf *clause = ccn_charbuf_create();
731 struct ccn_charbuf *slice_name = ccn_charbuf_create();
732 struct ccn_charbuf *slice_uri = ccn_charbuf_create();
733
734 if (prefix == NULL || topo == NULL || clause == NULL ||
735 slice_name == NULL || slice_uri == NULL) {
736 fprintf(stderr, "Unable to allocate required memory.\n");
akmhoquefc5176d2013-01-18 09:50:12 -0600737 return -1;
akmhoque8fdd6412012-12-04 15:05:33 -0600738 }
739
740
741 slice = ccns_slice_create();
742
743 ccn_charbuf_reset(topo);
744 ccn_name_from_uri(topo, topo_prefix);
745 ccn_charbuf_reset(prefix);
746 ccn_name_from_uri(prefix,slice_prefix );
747 ccns_slice_set_topo_prefix(slice, topo, prefix);
748
749
750 res = ccns_write_slice(nlsr->ccn, slice, slice_name);
Syed Obaid Amin83eea122013-01-08 12:21:37 -0600751 /*
752// Obaid: commenting out the following lines to resolve a bug.
753// If commenting them can resolve the issue, then we
754// need to call them before terminating the program.
akmhoque8fdd6412012-12-04 15:05:33 -0600755 ccns_slice_destroy(&slice);
756 ccn_charbuf_destroy(&prefix);
757 ccn_charbuf_destroy(&topo);
758 ccn_charbuf_destroy(&clause);
759 ccn_charbuf_destroy(&slice_name);
760 ccn_charbuf_destroy(&slice_uri);
akmhoque613446f2013-01-23 10:40:12 -0600761*/
akmhoque34425762013-01-23 09:33:29 -0600762
akmhoque8fdd6412012-12-04 15:05:33 -0600763 return 0;
764}
765