blob: f3ad3c447380df0fce27ce442b7eab947cf59ee8 [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"
akmhoque3b6c66d2013-01-31 15:30:23 -060032
33
Syed Obaid Amin3a3864b2013-01-09 03:05:50 -060034char *
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 struct ccn_charbuf *uri = ccn_charbuf_create();
57 if (lhash == NULL || lhash->length == 0) {
58 hexL = strdup("none");
59 } else
60 hexL = hex_string(lhash->buf, lhash->length);
61 if (rhash == NULL || rhash->length == 0) {
62 hexR = strdup("none");
63 } else
64 hexR = hex_string(rhash->buf, rhash->length);
65 if (name != NULL)
66 ccn_uri_append(uri, name->buf, name->length, 1);
67 else
68 ccn_charbuf_append_string(uri, "(null)");
a64adam04be6482013-01-17 11:29:32 -060069
akmhoque3b6c66d2013-01-31 15:30:23 -060070
a64adam04be6482013-01-17 11:29:32 -060071 if ( nlsr->debugging )
akmhoquefc5176d2013-01-18 09:50:12 -060072 printf("Response from sync in the name: %s \n",ccn_charbuf_as_string(uri));
73
Syed Obaid Amin3a3864b2013-01-09 03:05:50 -060074 fflush(stdout);
75 free(hexL);
76 free(hexR);
akmhoque3b6c66d2013-01-31 15:30:23 -060077 //ccn_charbuf_destroy(&uri);
akmhoqued6cd61d2013-01-17 10:31:15 -060078
79
akmhoque3b6c66d2013-01-31 15:30:23 -060080 //--Doing our thing from here
akmhoqueb29edd82013-01-14 20:54:11 -060081 struct ccn_indexbuf cid={0};
Syed Obaid Amin3a3864b2013-01-09 03:05:50 -060082
83 struct ccn_indexbuf *components=&cid;
akmhoque3b6c66d2013-01-31 15:30:23 -060084 ccn_name_split (uri, components);
85 ccn_name_chop(uri,components,-3);
Syed Obaid Amin3a3864b2013-01-09 03:05:50 -060086
akmhoque3b6c66d2013-01-31 15:30:23 -060087 process_content_from_sync(uri,components);
Syed Obaid Amin3a3864b2013-01-09 03:05:50 -060088
akmhoquefc5176d2013-01-18 09:50:12 -060089
akmhoque3b6c66d2013-01-31 15:30:23 -060090 ccn_charbuf_destroy(&uri);
Syed Obaid Amin3a3864b2013-01-09 03:05:50 -060091
92 return(0);
93}
akmhoque3b6c66d2013-01-31 15:30:23 -060094
akmhoque8fdd6412012-12-04 15:05:33 -060095
96int
97get_lsa_position(struct ccn_charbuf * ccnb, struct ccn_indexbuf *comps)
98{
99
100
101
102 int res,i;
akmhoque3b6c66d2013-01-31 15:30:23 -0600103 int lsa_position=0;
akmhoque8fdd6412012-12-04 15:05:33 -0600104 int name_comps=(int)comps->n;
105
106 for(i=0;i<name_comps;i++)
107 {
108 res=ccn_name_comp_strcmp(ccnb->buf,comps,i,"LSA");
109 if( res == 0)
110 {
111 lsa_position=i;
112 break;
113 }
114 }
115
116 return lsa_position;
117
118}
119
120void
121get_name_part(struct name_prefix *name_part,struct ccn_charbuf * interest_ccnb, struct ccn_indexbuf *interest_comps, int offset)
122{
123
124
125
126 int res,i;
127 int lsa_position=0;
128 int len=0;
129
akmhoque2ee18032013-01-22 10:01:23 -0600130
131 struct ccn_indexbuf cid={0};
132 struct ccn_indexbuf *components=&cid;
133 struct ccn_charbuf *name=ccn_charbuf_create();
134 ccn_name_from_uri(name,nlsr->slice_prefix);
135 ccn_name_split (name, components);
136 lsa_position=components->n-2;
akmhoque3b6c66d2013-01-31 15:30:23 -0600137
akmhoque2ee18032013-01-22 10:01:23 -0600138 ccn_charbuf_destroy(&name);
139
akmhoque8fdd6412012-12-04 15:05:33 -0600140
141 const unsigned char *comp_ptr1;
142 size_t comp_size;
143 for(i=lsa_position+1+offset;i<interest_comps->n-1;i++)
144 {
145 res=ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1, &comp_size);
146 len+=1;
147 len+=(int)comp_size;
148 }
149 len++;
150
151 char *neighbor=(char *)malloc(len);
152 memset(neighbor,0,len);
153
154 for(i=lsa_position+1+offset; i<interest_comps->n-1;i++)
155 {
156 res=ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1, &comp_size);
157 memcpy(neighbor+strlen(neighbor),"/",1);
158 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,strlen((char *)comp_ptr1));
159
160 }
161
162 name_part->name=(char *)malloc(strlen(neighbor)+1);
163 memset(name_part->name,0,strlen(neighbor)+1);
164 memcpy(name_part->name,neighbor,strlen(neighbor)+1);
165 name_part->length=strlen(neighbor)+1;
166
akmhoque3b6c66d2013-01-31 15:30:23 -0600167 // Add 01/31/2013
168 free(neighbor);
akmhoque8fdd6412012-12-04 15:05:33 -0600169}
170
akmhoque09c0afa2012-12-14 09:27:00 -0600171void
172get_host_name_from_command_string(struct name_prefix *name_part,char *nbr_name_uri, int offset)
173{
174
175
176
177 int res,i;
178 int len=0;
179 const unsigned char *comp_ptr1;
180 size_t comp_size;
181
182 struct ccn_charbuf *name=ccn_charbuf_create();
183 name = ccn_charbuf_create();
184 res = ccn_name_from_uri(name,nbr_name_uri);
185 if (res < 0) {
186 fprintf(stderr, "Bad ccn URI: %s\n", nbr_name_uri);
187 exit(1);
188 }
189
190 struct ccn_indexbuf cid={0};
191
192 struct ccn_indexbuf *components=&cid;
193 ccn_name_split (name, components);
194
195 for(i=components->n-2;i> (0+offset);i--)
196 {
197 res=ccn_name_comp_get(name->buf, components,i,&comp_ptr1, &comp_size);
198 len+=1;
199 len+=(int)comp_size;
200 }
201 len++;
202
203 char *neighbor=(char *)malloc(len);
204 memset(neighbor,0,len);
205
206 for(i=components->n-2;i> (0+offset);i--)
207 {
208 res=ccn_name_comp_get(name->buf, components,i,&comp_ptr1, &comp_size);
209 if ( i != components->n-2)
210 memcpy(neighbor+strlen(neighbor),".",1);
211 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,strlen((char *)comp_ptr1));
212
213 }
214
215 name_part->name=(char *)malloc(strlen(neighbor)+1);
216 memset(name_part->name,0,strlen(neighbor)+1);
217 memcpy(name_part->name,neighbor,strlen(neighbor)+1);
218 name_part->length=strlen(neighbor)+1;
219
akmhoque3b6c66d2013-01-31 15:30:23 -0600220 // 01/31/2013
221 free(neighbor);
222 ccn_charbuf_destroy(&name);
akmhoque09c0afa2012-12-14 09:27:00 -0600223}
224
akmhoque8fdd6412012-12-04 15:05:33 -0600225
akmhoqueed418f32013-01-30 12:25:04 -0600226
soamin15043622013-01-15 23:10:26 -0600227void
Obaid Amine63022a2013-01-15 23:36:40 -0600228get_content_by_content_name(char *content_name, unsigned char **content_data)
akmhoque8fdd6412012-12-04 15:05:33 -0600229{
230
soamin15043622013-01-15 23:10:26 -0600231 struct ccn_charbuf *name = NULL;
232 struct ccn_charbuf *templ = NULL;
233 struct ccn_charbuf *resultbuf = NULL;
234 struct ccn_parsed_ContentObject pcobuf = { 0 };
235 int res;
236 int allow_stale = 0;
237 int content_only = 1;
238 int scope = -1;
239 const unsigned char *ptr;
240 size_t length;
241 int resolve_version = CCN_V_HIGHEST;
242 int timeout_ms = 3000;
243 const unsigned lifetime_default = CCN_INTEREST_LIFETIME_SEC << 12;
244 unsigned lifetime_l12 = lifetime_default;
245 int get_flags = 0;
akmhoque8fdd6412012-12-04 15:05:33 -0600246
soamin15043622013-01-15 23:10:26 -0600247 name = ccn_charbuf_create();
248 res = ccn_name_from_uri(name,content_name);
249 if (res < 0) {
250 fprintf(stderr, "Bad ccn URI: %s\n", content_name);
251 exit(1);
252 }
253
akmhoque8fdd6412012-12-04 15:05:33 -0600254 if (allow_stale || lifetime_l12 != lifetime_default || scope != -1) {
soamin15043622013-01-15 23:10:26 -0600255 templ = ccn_charbuf_create();
256 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
257 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
258 ccn_charbuf_append_closer(templ); /* </Name> */
akmhoque8fdd6412012-12-04 15:05:33 -0600259 if (allow_stale) {
260 ccn_charbuf_append_tt(templ, CCN_DTAG_AnswerOriginKind, CCN_DTAG);
261 ccnb_append_number(templ,
soamin15043622013-01-15 23:10:26 -0600262 CCN_AOK_DEFAULT | CCN_AOK_STALE);
akmhoque8fdd6412012-12-04 15:05:33 -0600263 ccn_charbuf_append_closer(templ); /* </AnswerOriginKind> */
264 }
soamin15043622013-01-15 23:10:26 -0600265 if (scope != -1) {
266 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
267 }
akmhoque8fdd6412012-12-04 15:05:33 -0600268 if (lifetime_l12 != lifetime_default) {
269 /*
270 * Choose the interest lifetime so there are at least 3
271 * expressions (in the unsatisfied case).
272 */
273 unsigned char buf[3] = { 0 };
274 int i;
275 for (i = sizeof(buf) - 1; i >= 0; i--, lifetime_l12 >>= 8)
276 buf[i] = lifetime_l12 & 0xff;
277 ccnb_append_tagged_blob(templ, CCN_DTAG_InterestLifetime, buf, sizeof(buf));
278 }
soamin15043622013-01-15 23:10:26 -0600279 ccn_charbuf_append_closer(templ); /* </Interest> */
280 }
281 resultbuf = ccn_charbuf_create();
282 if (resolve_version != 0) {
283 res = ccn_resolve_version(nlsr->ccn, name, resolve_version, 500);
284 if (res >= 0) {
285 ccn_uri_append(resultbuf, name->buf, name->length, 1);
soamin15043622013-01-15 23:10:26 -0600286 resultbuf->length = 0;
287 }
288 }
289 res = ccn_get(nlsr->ccn, name, templ, timeout_ms, resultbuf, &pcobuf, NULL, get_flags);
290 if (res >= 0) {
291 ptr = resultbuf->buf;
292 length = resultbuf->length;
293 if (content_only){
294 ccn_content_get_value(ptr, length, &pcobuf, &ptr, &length);
Obaid Amine63022a2013-01-15 23:36:40 -0600295 *content_data = (unsigned char *) calloc(length, sizeof(char *));
296 memcpy (*content_data, ptr, length);
soamin15043622013-01-15 23:10:26 -0600297 }
298 }
299 ccn_charbuf_destroy(&resultbuf);
300 ccn_charbuf_destroy(&templ);
301 ccn_charbuf_destroy(&name);
akmhoque8fdd6412012-12-04 15:05:33 -0600302}
303
akmhoque4ae16942012-12-10 11:50:43 -0600304void
soamin15043622013-01-15 23:10:26 -0600305process_incoming_sync_content_lsa( unsigned char *content_data)
akmhoque4ae16942012-12-10 11:50:43 -0600306{
307
308
309 if ( nlsr->debugging )
akmhoqueed418f32013-01-30 12:25:04 -0600310 printf("process_incoming_sync_content_lsa called \n");
akmhoque4ae16942012-12-10 11:50:43 -0600311
312 char *sep="|";
313 char *rem;
314 char *orig_router;
315 char *orl;
316 int orig_router_length;
317 char *lst;
318 int ls_type;
319 char *lsid;
320 long int ls_id;
321 char *isvld;
322 int isValid;
323 char *num_link;
324 int no_link;
325 char *np;
326 char *np_length;
327 int name_length;
328 char *data;
329 char *orig_time;
330
331
332 if ( nlsr->debugging )
333 printf("LSA Data \n");
akmhoque4ae16942012-12-10 11:50:43 -0600334
soamin15043622013-01-15 23:10:26 -0600335 if( strlen((char *)content_data ) > 0 )
akmhoque4ae16942012-12-10 11:50:43 -0600336 {
337
338 orig_router=strtok_r((char *)content_data,sep,&rem);
339 orl=strtok_r(NULL,sep,&rem);
340 orig_router_length=atoi(orl);
341
342 if ( nlsr->debugging )
343 {
344 printf(" Orig Router Name : %s\n",orig_router);
345 printf(" Orig Router Length: %d\n",orig_router_length);
346 }
347
348 lst=strtok_r(NULL,sep,&rem);
349 ls_type=atoi(lst);
350
351 if ( nlsr->debugging )
352 printf(" LS Type : %d\n",ls_type);
353
354 if ( ls_type == LS_TYPE_NAME )
355 {
356 lsid=strtok_r(NULL,sep,&rem);
357 ls_id=atoi(lsid);
358 orig_time=strtok_r(NULL,sep,&rem);
359 isvld=strtok_r(NULL,sep,&rem);
360 isValid=atoi(isvld);
361 np=strtok_r(NULL,sep,&rem);
362 np_length=strtok_r(NULL,sep,&rem);
363 name_length=atoi(np_length);
364 if ( nlsr->debugging )
365 {
366 printf(" LS ID : %ld\n",ls_id);
367 printf(" isValid : %d\n",isValid);
368 printf(" Name Prefix : %s\n",np);
369 printf(" Orig Time : %s\n",orig_time);
370 printf(" Name Prefix length: %d\n",name_length);
371 }
372
373 build_and_install_others_name_lsa(orig_router,ls_type,ls_id,orig_time,isValid,np);
374
375 print_name_lsdb();
376
377 }
378 else if ( ls_type == LS_TYPE_ADJ )
379 {
380 orig_time=strtok_r(NULL,sep,&rem);
381 num_link=strtok_r(NULL,sep,&rem);
382 no_link=atoi(num_link);
383 data=rem;
384
385 if ( nlsr->debugging )
386 {
akmhoqueed418f32013-01-30 12:25:04 -0600387 printf(" Orig Time : %s\n",orig_time);
akmhoque4ae16942012-12-10 11:50:43 -0600388 printf(" No Link : %d\n",no_link);
389 printf(" Data : %s\n",data);
390 }
391 build_and_install_others_adj_lsa(orig_router,ls_type,orig_time,no_link,data);
392 }
akmhoqueed418f32013-01-30 12:25:04 -0600393 else if ( ls_type == LS_TYPE_COR )
394 {
395 orig_time=strtok_r(NULL,sep,&rem);
396 char *cor_r=strtok_r(NULL,sep,&rem);
397 char *cor_theta=strtok_r(NULL,sep,&rem);
398
399 double r, theta;
400 r=strtof(cor_r,NULL);
401 theta=strtof(cor_theta,NULL);
402
403 if ( nlsr->debugging )
404 {
405 printf(" Orig Time : %s\n",orig_time);
406 printf(" Cor R : %f\n",r);
407 printf(" Cor Theta : %f\n",theta);
408 }
409 build_and_install_others_cor_lsa(orig_router,ls_type,orig_time, (double)r, (double)theta);
410 }
411
akmhoque4ae16942012-12-10 11:50:43 -0600412 }
413}
414
akmhoque8fdd6412012-12-04 15:05:33 -0600415void
416process_content_from_sync(struct ccn_charbuf *content_name, struct ccn_indexbuf *components)
417{
418 int lsa_position;
419 int res;
420 size_t comp_size;
421 const unsigned char *lst;
422 const unsigned char *lsid;
423 const unsigned char *origtime;
424
425 int ls_type;
426 long int ls_id=0;
427
soamin15043622013-01-15 23:10:26 -0600428 unsigned char *content_data = NULL;
429
akmhoque4ae16942012-12-10 11:50:43 -0600430 char *time_stamp=(char *)malloc(20);
431 memset(time_stamp,0,20);
432 get_current_timestamp_micro(time_stamp);
433
akmhoque8fdd6412012-12-04 15:05:33 -0600434 struct ccn_charbuf *uri = ccn_charbuf_create();
435 ccn_uri_append(uri, content_name->buf, content_name->length, 0);
436
437 struct name_prefix *orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix));
438
akmhoque2ee18032013-01-22 10:01:23 -0600439
440 struct ccn_indexbuf cid={0};
441 struct ccn_indexbuf *temp_components=&cid;
442 struct ccn_charbuf *name=ccn_charbuf_create();
443 ccn_name_from_uri(name,nlsr->slice_prefix);
444 ccn_name_split (name, temp_components);
445 lsa_position=temp_components->n-2;
akmhoque2ee18032013-01-22 10:01:23 -0600446 ccn_charbuf_destroy(&name);
447
akmhoque8fdd6412012-12-04 15:05:33 -0600448
449 res=ccn_name_comp_get(content_name->buf, components,lsa_position+1,&lst, &comp_size);
450
akmhoque8fdd6412012-12-04 15:05:33 -0600451 ls_type=atoi((char *)lst);
452 if(ls_type == LS_TYPE_NAME)
453 {
akmhoque4ae16942012-12-10 11:50:43 -0600454
akmhoque8fdd6412012-12-04 15:05:33 -0600455 res=ccn_name_comp_get(content_name->buf, components,lsa_position+2,&lsid, &comp_size);
456 ls_id=atoi((char *)lsid);
457 res=ccn_name_comp_get(content_name->buf, components,lsa_position+3,&origtime, &comp_size);
458 get_name_part(orig_router,content_name,components,3);
akmhoque8fdd6412012-12-04 15:05:33 -0600459
akmhoque4ae16942012-12-10 11:50:43 -0600460 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
461
akmhoque4ae16942012-12-10 11:50:43 -0600462 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 -0600463 {
akmhoque4ae16942012-12-10 11:50:43 -0600464 int is_new_name_lsa=check_is_new_name_lsa(orig_router->name,(char *)lst,(char *)lsid,(char *)origtime);
465 if ( is_new_name_lsa == 1 )
466 {
a64adam04be6482013-01-17 11:29:32 -0600467 if ( nlsr->debugging )
468 printf("New NAME LSA.....\n");
Obaid Amine63022a2013-01-15 23:36:40 -0600469 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
a64adamf7b051d2013-01-17 11:39:58 -0600470 if ( nlsr->debugging )
471 printf("Content Data: %s \n",content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600472 process_incoming_sync_content_lsa(content_data);
473 }
474 else
475 {
a64adam04be6482013-01-17 11:29:32 -0600476 if ( nlsr->debugging )
477 printf("Name LSA / Newer Name LSA already xists in LSDB\n");
Obaid Amine63022a2013-01-15 23:36:40 -0600478 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
a64adam04be6482013-01-17 11:29:32 -0600479
480 if ( nlsr->debugging )
481 printf("Content Data: %s \n",content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600482 }
akmhoque8fdd6412012-12-04 15:05:33 -0600483 }
484 else
485 {
a64adam04be6482013-01-17 11:29:32 -0600486 if ( nlsr->debugging )
487 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
akmhoque8fdd6412012-12-04 15:05:33 -0600488 }
489 }
490 else if(ls_type == LS_TYPE_ADJ)
491 {
492 res=ccn_name_comp_get(content_name->buf, components,lsa_position+2,&origtime, &comp_size);
493 get_name_part(orig_router,content_name,components,2);
a64adam04be6482013-01-17 11:29:32 -0600494
495 if ( nlsr->debugging )
496 printf("Orig Time: %s\nOrig Router: %s\n",origtime,orig_router->name);
akmhoque8fdd6412012-12-04 15:05:33 -0600497
akmhoque4ae16942012-12-10 11:50:43 -0600498 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque4ae16942012-12-10 11:50:43 -0600499 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) )
500 {
501 int is_new_adj_lsa=check_is_new_adj_lsa(orig_router->name,(char *)lst,(char *)origtime);
502 if ( is_new_adj_lsa == 1 )
503 {
a64adam04be6482013-01-17 11:29:32 -0600504 if ( nlsr->debugging )
505 printf("New Adj LSA.....\n");
Obaid Amine63022a2013-01-15 23:36:40 -0600506 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
a64adam04be6482013-01-17 11:29:32 -0600507
508 if ( nlsr->debugging )
509 printf("Content Data: %s \n",content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600510 process_incoming_sync_content_lsa(content_data);
511 }
512 else
513 {
a64adam04be6482013-01-17 11:29:32 -0600514 if ( nlsr->debugging )
515 printf("Adj LSA / Newer Adj LSA already exists in LSDB\n");
Obaid Amine63022a2013-01-15 23:36:40 -0600516 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
a64adam04be6482013-01-17 11:29:32 -0600517 if ( nlsr->debugging )
518 printf("Content Data: %s \n",content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600519 }
520 }
521 else
522 {
a64adam04be6482013-01-17 11:29:32 -0600523 if ( nlsr->debugging )
524 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
akmhoque4ae16942012-12-10 11:50:43 -0600525 }
akmhoque8fdd6412012-12-04 15:05:33 -0600526 }
akmhoqueed418f32013-01-30 12:25:04 -0600527 else if(ls_type == LS_TYPE_COR)
528 {
529 res=ccn_name_comp_get(content_name->buf, components,lsa_position+2,&origtime, &comp_size);
530 get_name_part(orig_router,content_name,components,2);
531
532 if ( nlsr->debugging )
533 printf("Orig Time: %s\nOrig Router: %s\n",origtime,orig_router->name);
534
535 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
536 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) )
537 {
538 int is_new_cor_lsa=check_is_new_cor_lsa(orig_router->name,(char *)lst,(char *)origtime);
539 if ( is_new_cor_lsa == 1 )
540 {
541 if ( nlsr->debugging )
542 printf("New Cor LSA.....\n");
543 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
544
545 if ( nlsr->debugging )
546 printf("Content Data: %s \n",content_data);
547 process_incoming_sync_content_lsa(content_data);
548 }
549 else
550 {
551 if ( nlsr->debugging )
552 printf("Cor LSA / Newer Cor LSA already exists in LSDB\n");
553 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
554 if ( nlsr->debugging )
555 printf("Content Data: %s \n",content_data);
556 }
557 }
558 else
559 {
560 if ( nlsr->debugging )
561 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
562 }
563
564 }
akmhoque8fdd6412012-12-04 15:05:33 -0600565
Obaid Amine63022a2013-01-15 23:36:40 -0600566 if (content_data != NULL)
567 free(content_data);
akmhoque8fdd6412012-12-04 15:05:33 -0600568 ccn_charbuf_destroy(&uri);
akmhoque3b6c66d2013-01-31 15:30:23 -0600569 //01/31/2013
570 free(time_stamp);
akmhoque8fdd6412012-12-04 15:05:33 -0600571}
572
akmhoque8fdd6412012-12-04 15:05:33 -0600573void
574sync_monitor(char *topo_prefix, char *slice_prefix)
575{
576
577 static struct ccns_name_closure nc={0};
578
579 nlsr->closure = &nc;
580 struct ccn_charbuf *prefix = ccn_charbuf_create();
581 struct ccn_charbuf *roothash = NULL;
582 struct ccn_charbuf *topo = ccn_charbuf_create();
583 nlsr->slice = ccns_slice_create();
584 ccn_charbuf_reset(prefix);
585 ccn_charbuf_reset(topo);
586
587 ccn_charbuf_reset(prefix);
588 ccn_name_from_uri(prefix, slice_prefix);
589
590 ccn_charbuf_reset(topo);
591 ccn_name_from_uri(topo, topo_prefix);
592
593
594 ccns_slice_set_topo_prefix(nlsr->slice, topo, prefix);
Syed Obaid Amin3a3864b2013-01-09 03:05:50 -0600595 nlsr->closure->callback = &sync_cb;
akmhoque8fdd6412012-12-04 15:05:33 -0600596 nlsr->ccns = ccns_open(nlsr->ccn, nlsr->slice, nlsr->closure, roothash, NULL);
akmhoque3b6c66d2013-01-31 15:30:23 -0600597
598 //01/31/2013
599 ccn_charbuf_destroy(&prefix);
600 ccn_charbuf_destroy(&topo);
akmhoque8fdd6412012-12-04 15:05:33 -0600601}
602
603struct ccn_charbuf *
604make_template(int scope)
605{
606 struct ccn_charbuf *templ = NULL;
607 templ = ccn_charbuf_create();
608 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
609 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
610 ccn_charbuf_append_closer(templ); /* </Name> */
611 if (0 <= scope && scope <= 2)
612 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
613 ccn_charbuf_append_closer(templ); /* </Interest> */
614 return(templ);
615}
616
akmhoquefc5176d2013-01-18 09:50:12 -0600617int
akmhoque8fdd6412012-12-04 15:05:33 -0600618write_data_to_repo(char *data, char *name_prefix)
619{
akmhoque260698b2013-01-17 03:54:09 -0600620 if ( nlsr->debugging )
621 {
622 printf("write_data_to_repo called\n");
akmhoque47d19d12013-01-17 04:00:32 -0600623 printf("Content Name: %s \n",name_prefix);
akmhoque260698b2013-01-17 03:54:09 -0600624 printf("Content Data: %s \n",data);
625 }
akmhoque8fdd6412012-12-04 15:05:33 -0600626
akmhoque2ee18032013-01-22 10:01:23 -0600627 struct ccn *temp_ccn;
628 temp_ccn=ccn_create();
629 int ccn_fd=ccn_connect(temp_ccn, NULL);
630 if(ccn_fd == -1)
631 {
632 fprintf(stderr,"Could not connect to ccnd for Data Writing\n");
633 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd for Data Writing\n");
634 return -1;
635 }
akmhoque8fdd6412012-12-04 15:05:33 -0600636 struct ccn_charbuf *name = NULL;
637 struct ccn_seqwriter *w = NULL;
akmhoque2ee18032013-01-22 10:01:23 -0600638 int blocksize = 4096;
akmhoque8fdd6412012-12-04 15:05:33 -0600639 int freshness = -1;
640 int torepo = 1;
641 int scope = 1;
642 int res;
643 size_t blockread;
644 struct ccn_charbuf *templ;
645
646 name = ccn_charbuf_create();
647 res = ccn_name_from_uri(name, name_prefix);
648 if (res < 0) {
649 fprintf(stderr, "bad CCN URI: %s\n",name_prefix);
akmhoquefc5176d2013-01-18 09:50:12 -0600650 return -1;
akmhoque8fdd6412012-12-04 15:05:33 -0600651 }
652
653
akmhoque2ee18032013-01-22 10:01:23 -0600654 w = ccn_seqw_create(temp_ccn, name);
akmhoque8fdd6412012-12-04 15:05:33 -0600655 if (w == NULL) {
656 fprintf(stderr, "ccn_seqw_create failed\n");
akmhoquefc5176d2013-01-18 09:50:12 -0600657 return -1;
akmhoque8fdd6412012-12-04 15:05:33 -0600658 }
659 ccn_seqw_set_block_limits(w, blocksize, blocksize);
660 if (freshness > -1)
661 ccn_seqw_set_freshness(w, freshness);
662 if (torepo) {
663 struct ccn_charbuf *name_v = ccn_charbuf_create();
664 ccn_seqw_get_name(w, name_v);
665 ccn_name_from_uri(name_v, "%C1.R.sw");
666 ccn_name_append_nonce(name_v);
667 templ = make_template(scope);
akmhoque2ee18032013-01-22 10:01:23 -0600668 res = ccn_get(temp_ccn, name_v, templ, 60000, NULL, NULL, NULL, 0);
akmhoque8fdd6412012-12-04 15:05:33 -0600669 ccn_charbuf_destroy(&templ);
670 ccn_charbuf_destroy(&name_v);
671 if (res < 0) {
672 fprintf(stderr, "No response from repository\n");
akmhoquefc5176d2013-01-18 09:50:12 -0600673 return -1;
akmhoque8fdd6412012-12-04 15:05:33 -0600674 }
675 }
676
677
678
679
680 blockread = 0;
akmhoque260698b2013-01-17 03:54:09 -0600681
akmhoque8fdd6412012-12-04 15:05:33 -0600682
683 blockread=strlen(data);
684
685 if (blockread > 0) {
akmhoque2ee18032013-01-22 10:01:23 -0600686 ccn_run(temp_ccn, 100);
akmhoque8fdd6412012-12-04 15:05:33 -0600687 res = ccn_seqw_write(w, data, blockread);
akmhoque260698b2013-01-17 03:54:09 -0600688 while (res == -1) {
akmhoque2ee18032013-01-22 10:01:23 -0600689 ccn_run(temp_ccn, 100);
akmhoque260698b2013-01-17 03:54:09 -0600690 res = ccn_seqw_write(w, data, blockread);
691 }
akmhoque8fdd6412012-12-04 15:05:33 -0600692 }
693
akmhoque8fdd6412012-12-04 15:05:33 -0600694 ccn_seqw_close(w);
akmhoque2ee18032013-01-22 10:01:23 -0600695 ccn_run(temp_ccn, 100);
akmhoque8fdd6412012-12-04 15:05:33 -0600696 ccn_charbuf_destroy(&name);
akmhoque2ee18032013-01-22 10:01:23 -0600697 ccn_destroy(&temp_ccn);
akmhoquefc5176d2013-01-18 09:50:12 -0600698
699 return 0;
akmhoque8fdd6412012-12-04 15:05:33 -0600700}
701
702
703int
704create_sync_slice(char *topo_prefix, char *slice_prefix)
705{
706 int res;
707 struct ccns_slice *slice;
708 struct ccn_charbuf *prefix = ccn_charbuf_create();
709 struct ccn_charbuf *topo = ccn_charbuf_create();
710 struct ccn_charbuf *clause = ccn_charbuf_create();
711 struct ccn_charbuf *slice_name = ccn_charbuf_create();
712 struct ccn_charbuf *slice_uri = ccn_charbuf_create();
713
714 if (prefix == NULL || topo == NULL || clause == NULL ||
715 slice_name == NULL || slice_uri == NULL) {
716 fprintf(stderr, "Unable to allocate required memory.\n");
akmhoquefc5176d2013-01-18 09:50:12 -0600717 return -1;
akmhoque8fdd6412012-12-04 15:05:33 -0600718 }
719
720
721 slice = ccns_slice_create();
722
723 ccn_charbuf_reset(topo);
724 ccn_name_from_uri(topo, topo_prefix);
725 ccn_charbuf_reset(prefix);
726 ccn_name_from_uri(prefix,slice_prefix );
727 ccns_slice_set_topo_prefix(slice, topo, prefix);
728
729
730 res = ccns_write_slice(nlsr->ccn, slice, slice_name);
akmhoque3b6c66d2013-01-31 15:30:23 -0600731
732 //01/31/2013
akmhoque8fdd6412012-12-04 15:05:33 -0600733 ccns_slice_destroy(&slice);
734 ccn_charbuf_destroy(&prefix);
735 ccn_charbuf_destroy(&topo);
736 ccn_charbuf_destroy(&clause);
737 ccn_charbuf_destroy(&slice_name);
738 ccn_charbuf_destroy(&slice_uri);
akmhoque34425762013-01-23 09:33:29 -0600739
akmhoque8fdd6412012-12-04 15:05:33 -0600740 return 0;
741}
742