blob: 52f9ca443c5f50d51936d6a7cef5050747f828ee [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;
akmhoque6c2f60a2013-01-31 17:29:11 -060056 int res;
Syed Obaid Amin3a3864b2013-01-09 03:05:50 -060057 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)");
a64adam04be6482013-01-17 11:29:32 -060070
akmhoque3b6c66d2013-01-31 15:30:23 -060071
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);
akmhoque3c0c95d2013-01-31 16:46:24 -060078 ccn_charbuf_destroy(&uri);
akmhoqued6cd61d2013-01-17 10:31:15 -060079
80
akmhoque3b6c66d2013-01-31 15:30:23 -060081 //--Doing our thing from here
akmhoque6c2f60a2013-01-31 17:29:11 -060082 struct ccn_indexbuf cid={0};
Syed Obaid Amin3a3864b2013-01-09 03:05:50 -060083
84 struct ccn_indexbuf *components=&cid;
akmhoque566517f2013-01-31 17:44:28 -060085 ccn_name_split (name, components);
akmhoque6c2f60a2013-01-31 17:29:11 -060086 //ccn_name_chop(name,components,-3);
87 //process_content_from_sync(name,components);
Syed Obaid Amin3a3864b2013-01-09 03:05:50 -060088
akmhoqueacef1e62013-01-31 17:15:08 -060089 struct ccn_charbuf *content_name = ccn_charbuf_create();
akmhoque566517f2013-01-31 17:44:28 -060090 ccn_name_init(content_name);
91 res = ccn_name_append_components(content_name, name->buf, components->buf[0], components->buf[components->n - 1]);
92
93 struct ccn_indexbuf cid1={0};
94 struct ccn_indexbuf *components1=&cid1;
95 res=ccn_name_split (content_name, components1);
akmhoqueaaad9702013-01-31 18:12:36 -060096 if ( nlsr->debugging )
97 printf("Number of components in name = %d \n",res);
akmhoque1458b1d2013-01-31 17:48:32 -060098 ccn_name_chop(content_name,components1,-3);
99 process_content_from_sync(content_name,components1);
akmhoqueacef1e62013-01-31 17:15:08 -0600100 ccn_charbuf_destroy(&content_name);
Syed Obaid Amin3a3864b2013-01-09 03:05:50 -0600101
102 return(0);
103}
akmhoque3b6c66d2013-01-31 15:30:23 -0600104
akmhoque8fdd6412012-12-04 15:05:33 -0600105
106int
107get_lsa_position(struct ccn_charbuf * ccnb, struct ccn_indexbuf *comps)
108{
109
110
111
112 int res,i;
akmhoque3b6c66d2013-01-31 15:30:23 -0600113 int lsa_position=0;
akmhoque8fdd6412012-12-04 15:05:33 -0600114 int name_comps=(int)comps->n;
115
116 for(i=0;i<name_comps;i++)
117 {
118 res=ccn_name_comp_strcmp(ccnb->buf,comps,i,"LSA");
119 if( res == 0)
120 {
121 lsa_position=i;
122 break;
123 }
124 }
125
126 return lsa_position;
127
128}
129
130void
131get_name_part(struct name_prefix *name_part,struct ccn_charbuf * interest_ccnb, struct ccn_indexbuf *interest_comps, int offset)
132{
133
134
135
136 int res,i;
137 int lsa_position=0;
138 int len=0;
139
akmhoque2ee18032013-01-22 10:01:23 -0600140
141 struct ccn_indexbuf cid={0};
142 struct ccn_indexbuf *components=&cid;
143 struct ccn_charbuf *name=ccn_charbuf_create();
144 ccn_name_from_uri(name,nlsr->slice_prefix);
145 ccn_name_split (name, components);
146 lsa_position=components->n-2;
akmhoque3b6c66d2013-01-31 15:30:23 -0600147
akmhoque2ee18032013-01-22 10:01:23 -0600148 ccn_charbuf_destroy(&name);
149
akmhoque8fdd6412012-12-04 15:05:33 -0600150
151 const unsigned char *comp_ptr1;
152 size_t comp_size;
153 for(i=lsa_position+1+offset;i<interest_comps->n-1;i++)
154 {
155 res=ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1, &comp_size);
156 len+=1;
157 len+=(int)comp_size;
158 }
159 len++;
160
161 char *neighbor=(char *)malloc(len);
162 memset(neighbor,0,len);
163
164 for(i=lsa_position+1+offset; i<interest_comps->n-1;i++)
165 {
166 res=ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1, &comp_size);
167 memcpy(neighbor+strlen(neighbor),"/",1);
168 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,strlen((char *)comp_ptr1));
169
170 }
171
172 name_part->name=(char *)malloc(strlen(neighbor)+1);
173 memset(name_part->name,0,strlen(neighbor)+1);
174 memcpy(name_part->name,neighbor,strlen(neighbor)+1);
175 name_part->length=strlen(neighbor)+1;
176
akmhoque3b6c66d2013-01-31 15:30:23 -0600177 // Add 01/31/2013
178 free(neighbor);
akmhoque8fdd6412012-12-04 15:05:33 -0600179}
180
akmhoque09c0afa2012-12-14 09:27:00 -0600181void
182get_host_name_from_command_string(struct name_prefix *name_part,char *nbr_name_uri, int offset)
183{
184
185
186
187 int res,i;
188 int len=0;
189 const unsigned char *comp_ptr1;
190 size_t comp_size;
191
192 struct ccn_charbuf *name=ccn_charbuf_create();
193 name = ccn_charbuf_create();
194 res = ccn_name_from_uri(name,nbr_name_uri);
195 if (res < 0) {
196 fprintf(stderr, "Bad ccn URI: %s\n", nbr_name_uri);
197 exit(1);
198 }
199
200 struct ccn_indexbuf cid={0};
201
202 struct ccn_indexbuf *components=&cid;
203 ccn_name_split (name, components);
204
205 for(i=components->n-2;i> (0+offset);i--)
206 {
207 res=ccn_name_comp_get(name->buf, components,i,&comp_ptr1, &comp_size);
208 len+=1;
209 len+=(int)comp_size;
210 }
211 len++;
212
213 char *neighbor=(char *)malloc(len);
214 memset(neighbor,0,len);
215
216 for(i=components->n-2;i> (0+offset);i--)
217 {
218 res=ccn_name_comp_get(name->buf, components,i,&comp_ptr1, &comp_size);
219 if ( i != components->n-2)
220 memcpy(neighbor+strlen(neighbor),".",1);
221 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,strlen((char *)comp_ptr1));
222
223 }
224
225 name_part->name=(char *)malloc(strlen(neighbor)+1);
226 memset(name_part->name,0,strlen(neighbor)+1);
227 memcpy(name_part->name,neighbor,strlen(neighbor)+1);
228 name_part->length=strlen(neighbor)+1;
229
akmhoque3b6c66d2013-01-31 15:30:23 -0600230 // 01/31/2013
231 free(neighbor);
232 ccn_charbuf_destroy(&name);
akmhoque09c0afa2012-12-14 09:27:00 -0600233}
234
akmhoque8fdd6412012-12-04 15:05:33 -0600235
akmhoqueed418f32013-01-30 12:25:04 -0600236
soamin15043622013-01-15 23:10:26 -0600237void
Obaid Amine63022a2013-01-15 23:36:40 -0600238get_content_by_content_name(char *content_name, unsigned char **content_data)
akmhoque8fdd6412012-12-04 15:05:33 -0600239{
240
soamin15043622013-01-15 23:10:26 -0600241 struct ccn_charbuf *name = NULL;
242 struct ccn_charbuf *templ = NULL;
243 struct ccn_charbuf *resultbuf = NULL;
244 struct ccn_parsed_ContentObject pcobuf = { 0 };
245 int res;
246 int allow_stale = 0;
247 int content_only = 1;
248 int scope = -1;
249 const unsigned char *ptr;
250 size_t length;
251 int resolve_version = CCN_V_HIGHEST;
252 int timeout_ms = 3000;
253 const unsigned lifetime_default = CCN_INTEREST_LIFETIME_SEC << 12;
254 unsigned lifetime_l12 = lifetime_default;
255 int get_flags = 0;
akmhoque8fdd6412012-12-04 15:05:33 -0600256
soamin15043622013-01-15 23:10:26 -0600257 name = ccn_charbuf_create();
258 res = ccn_name_from_uri(name,content_name);
259 if (res < 0) {
260 fprintf(stderr, "Bad ccn URI: %s\n", content_name);
261 exit(1);
262 }
263
akmhoque8fdd6412012-12-04 15:05:33 -0600264 if (allow_stale || lifetime_l12 != lifetime_default || scope != -1) {
soamin15043622013-01-15 23:10:26 -0600265 templ = ccn_charbuf_create();
266 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
267 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
268 ccn_charbuf_append_closer(templ); /* </Name> */
akmhoque8fdd6412012-12-04 15:05:33 -0600269 if (allow_stale) {
270 ccn_charbuf_append_tt(templ, CCN_DTAG_AnswerOriginKind, CCN_DTAG);
271 ccnb_append_number(templ,
soamin15043622013-01-15 23:10:26 -0600272 CCN_AOK_DEFAULT | CCN_AOK_STALE);
akmhoque8fdd6412012-12-04 15:05:33 -0600273 ccn_charbuf_append_closer(templ); /* </AnswerOriginKind> */
274 }
soamin15043622013-01-15 23:10:26 -0600275 if (scope != -1) {
276 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
277 }
akmhoque8fdd6412012-12-04 15:05:33 -0600278 if (lifetime_l12 != lifetime_default) {
279 /*
280 * Choose the interest lifetime so there are at least 3
281 * expressions (in the unsatisfied case).
282 */
283 unsigned char buf[3] = { 0 };
284 int i;
285 for (i = sizeof(buf) - 1; i >= 0; i--, lifetime_l12 >>= 8)
286 buf[i] = lifetime_l12 & 0xff;
287 ccnb_append_tagged_blob(templ, CCN_DTAG_InterestLifetime, buf, sizeof(buf));
288 }
soamin15043622013-01-15 23:10:26 -0600289 ccn_charbuf_append_closer(templ); /* </Interest> */
290 }
291 resultbuf = ccn_charbuf_create();
292 if (resolve_version != 0) {
293 res = ccn_resolve_version(nlsr->ccn, name, resolve_version, 500);
294 if (res >= 0) {
295 ccn_uri_append(resultbuf, name->buf, name->length, 1);
soamin15043622013-01-15 23:10:26 -0600296 resultbuf->length = 0;
297 }
298 }
299 res = ccn_get(nlsr->ccn, name, templ, timeout_ms, resultbuf, &pcobuf, NULL, get_flags);
300 if (res >= 0) {
301 ptr = resultbuf->buf;
302 length = resultbuf->length;
303 if (content_only){
304 ccn_content_get_value(ptr, length, &pcobuf, &ptr, &length);
Obaid Amine63022a2013-01-15 23:36:40 -0600305 *content_data = (unsigned char *) calloc(length, sizeof(char *));
306 memcpy (*content_data, ptr, length);
soamin15043622013-01-15 23:10:26 -0600307 }
308 }
309 ccn_charbuf_destroy(&resultbuf);
310 ccn_charbuf_destroy(&templ);
311 ccn_charbuf_destroy(&name);
akmhoque8fdd6412012-12-04 15:05:33 -0600312}
313
akmhoque4ae16942012-12-10 11:50:43 -0600314void
soamin15043622013-01-15 23:10:26 -0600315process_incoming_sync_content_lsa( unsigned char *content_data)
akmhoque4ae16942012-12-10 11:50:43 -0600316{
317
318
319 if ( nlsr->debugging )
akmhoqueed418f32013-01-30 12:25:04 -0600320 printf("process_incoming_sync_content_lsa called \n");
akmhoque4ae16942012-12-10 11:50:43 -0600321
322 char *sep="|";
323 char *rem;
324 char *orig_router;
325 char *orl;
326 int orig_router_length;
327 char *lst;
328 int ls_type;
329 char *lsid;
330 long int ls_id;
331 char *isvld;
332 int isValid;
333 char *num_link;
334 int no_link;
335 char *np;
336 char *np_length;
337 int name_length;
338 char *data;
339 char *orig_time;
340
341
342 if ( nlsr->debugging )
343 printf("LSA Data \n");
akmhoque4ae16942012-12-10 11:50:43 -0600344
soamin15043622013-01-15 23:10:26 -0600345 if( strlen((char *)content_data ) > 0 )
akmhoque4ae16942012-12-10 11:50:43 -0600346 {
347
348 orig_router=strtok_r((char *)content_data,sep,&rem);
349 orl=strtok_r(NULL,sep,&rem);
350 orig_router_length=atoi(orl);
351
352 if ( nlsr->debugging )
353 {
354 printf(" Orig Router Name : %s\n",orig_router);
355 printf(" Orig Router Length: %d\n",orig_router_length);
356 }
357
358 lst=strtok_r(NULL,sep,&rem);
359 ls_type=atoi(lst);
360
361 if ( nlsr->debugging )
362 printf(" LS Type : %d\n",ls_type);
363
364 if ( ls_type == LS_TYPE_NAME )
365 {
366 lsid=strtok_r(NULL,sep,&rem);
367 ls_id=atoi(lsid);
368 orig_time=strtok_r(NULL,sep,&rem);
369 isvld=strtok_r(NULL,sep,&rem);
370 isValid=atoi(isvld);
371 np=strtok_r(NULL,sep,&rem);
372 np_length=strtok_r(NULL,sep,&rem);
373 name_length=atoi(np_length);
374 if ( nlsr->debugging )
375 {
376 printf(" LS ID : %ld\n",ls_id);
377 printf(" isValid : %d\n",isValid);
378 printf(" Name Prefix : %s\n",np);
379 printf(" Orig Time : %s\n",orig_time);
380 printf(" Name Prefix length: %d\n",name_length);
381 }
382
383 build_and_install_others_name_lsa(orig_router,ls_type,ls_id,orig_time,isValid,np);
384
385 print_name_lsdb();
386
387 }
388 else if ( ls_type == LS_TYPE_ADJ )
389 {
390 orig_time=strtok_r(NULL,sep,&rem);
391 num_link=strtok_r(NULL,sep,&rem);
392 no_link=atoi(num_link);
393 data=rem;
394
395 if ( nlsr->debugging )
396 {
akmhoqueed418f32013-01-30 12:25:04 -0600397 printf(" Orig Time : %s\n",orig_time);
akmhoque4ae16942012-12-10 11:50:43 -0600398 printf(" No Link : %d\n",no_link);
399 printf(" Data : %s\n",data);
400 }
401 build_and_install_others_adj_lsa(orig_router,ls_type,orig_time,no_link,data);
402 }
akmhoqueed418f32013-01-30 12:25:04 -0600403 else if ( ls_type == LS_TYPE_COR )
404 {
405 orig_time=strtok_r(NULL,sep,&rem);
406 char *cor_r=strtok_r(NULL,sep,&rem);
407 char *cor_theta=strtok_r(NULL,sep,&rem);
408
409 double r, theta;
410 r=strtof(cor_r,NULL);
411 theta=strtof(cor_theta,NULL);
412
413 if ( nlsr->debugging )
414 {
415 printf(" Orig Time : %s\n",orig_time);
416 printf(" Cor R : %f\n",r);
417 printf(" Cor Theta : %f\n",theta);
418 }
419 build_and_install_others_cor_lsa(orig_router,ls_type,orig_time, (double)r, (double)theta);
420 }
421
akmhoque4ae16942012-12-10 11:50:43 -0600422 }
423}
424
akmhoque8fdd6412012-12-04 15:05:33 -0600425void
426process_content_from_sync(struct ccn_charbuf *content_name, struct ccn_indexbuf *components)
427{
428 int lsa_position;
429 int res;
430 size_t comp_size;
431 const unsigned char *lst;
432 const unsigned char *lsid;
433 const unsigned char *origtime;
434
435 int ls_type;
436 long int ls_id=0;
437
soamin15043622013-01-15 23:10:26 -0600438 unsigned char *content_data = NULL;
439
akmhoque4ae16942012-12-10 11:50:43 -0600440 char *time_stamp=(char *)malloc(20);
441 memset(time_stamp,0,20);
442 get_current_timestamp_micro(time_stamp);
443
akmhoque8fdd6412012-12-04 15:05:33 -0600444 struct ccn_charbuf *uri = ccn_charbuf_create();
445 ccn_uri_append(uri, content_name->buf, content_name->length, 0);
446
447 struct name_prefix *orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix));
448
akmhoque2ee18032013-01-22 10:01:23 -0600449
450 struct ccn_indexbuf cid={0};
451 struct ccn_indexbuf *temp_components=&cid;
452 struct ccn_charbuf *name=ccn_charbuf_create();
453 ccn_name_from_uri(name,nlsr->slice_prefix);
454 ccn_name_split (name, temp_components);
455 lsa_position=temp_components->n-2;
akmhoque2ee18032013-01-22 10:01:23 -0600456 ccn_charbuf_destroy(&name);
457
akmhoque8fdd6412012-12-04 15:05:33 -0600458
akmhoque496c88f2013-02-08 05:24:18 -0600459 //res=ccn_name_comp_get(content_name->buf, components,lsa_position+1,&lst, &comp_size);
460 res=ccn_name_comp_get(content_name->buf, components,components->n-2-1,&lst, &comp_size);
akmhoque8fdd6412012-12-04 15:05:33 -0600461 ls_type=atoi((char *)lst);
akmhoque496c88f2013-02-08 05:24:18 -0600462
akmhoque8fdd6412012-12-04 15:05:33 -0600463 if(ls_type == LS_TYPE_NAME)
464 {
akmhoque4ae16942012-12-10 11:50:43 -0600465
akmhoque496c88f2013-02-08 05:24:18 -0600466 res=ccn_name_comp_get(content_name->buf, components,components->n-2-2,&lsid, &comp_size);
akmhoque8fdd6412012-12-04 15:05:33 -0600467 ls_id=atoi((char *)lsid);
akmhoque496c88f2013-02-08 05:24:18 -0600468 res=ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size);
469 ccn_name_chop(content_name,components,-3);
470 get_name_part(orig_router,content_name,components,0);
471
472 if ( nlsr->debugging )
473 printf("Orig Router: %s Ls Type: %d Ls id: %ld Orig Time: %s\n",orig_router->name,ls_type,ls_id,origtime);
akmhoque8fdd6412012-12-04 15:05:33 -0600474
akmhoque4ae16942012-12-10 11:50:43 -0600475 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
476
akmhoque4ae16942012-12-10 11:50:43 -0600477 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 -0600478 {
akmhoque4ae16942012-12-10 11:50:43 -0600479 int is_new_name_lsa=check_is_new_name_lsa(orig_router->name,(char *)lst,(char *)lsid,(char *)origtime);
480 if ( is_new_name_lsa == 1 )
481 {
a64adam04be6482013-01-17 11:29:32 -0600482 if ( nlsr->debugging )
483 printf("New NAME LSA.....\n");
Obaid Amine63022a2013-01-15 23:36:40 -0600484 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
a64adamf7b051d2013-01-17 11:39:58 -0600485 if ( nlsr->debugging )
486 printf("Content Data: %s \n",content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600487 process_incoming_sync_content_lsa(content_data);
488 }
489 else
490 {
a64adam04be6482013-01-17 11:29:32 -0600491 if ( nlsr->debugging )
492 printf("Name LSA / Newer Name LSA already xists in LSDB\n");
Obaid Amine63022a2013-01-15 23:36:40 -0600493 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
a64adam04be6482013-01-17 11:29:32 -0600494
495 if ( nlsr->debugging )
496 printf("Content Data: %s \n",content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600497 }
akmhoque8fdd6412012-12-04 15:05:33 -0600498 }
499 else
500 {
a64adam04be6482013-01-17 11:29:32 -0600501 if ( nlsr->debugging )
502 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
akmhoque8fdd6412012-12-04 15:05:33 -0600503 }
504 }
505 else if(ls_type == LS_TYPE_ADJ)
506 {
akmhoque496c88f2013-02-08 05:24:18 -0600507 res=ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size);
508 ccn_name_chop(content_name,components,-2);
509 get_name_part(orig_router,content_name,components,0);
a64adam04be6482013-01-17 11:29:32 -0600510
511 if ( nlsr->debugging )
akmhoque496c88f2013-02-08 05:24:18 -0600512 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",orig_router->name,ls_type,origtime);
akmhoque8fdd6412012-12-04 15:05:33 -0600513
akmhoque4ae16942012-12-10 11:50:43 -0600514 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque4ae16942012-12-10 11:50:43 -0600515 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) )
516 {
517 int is_new_adj_lsa=check_is_new_adj_lsa(orig_router->name,(char *)lst,(char *)origtime);
518 if ( is_new_adj_lsa == 1 )
519 {
a64adam04be6482013-01-17 11:29:32 -0600520 if ( nlsr->debugging )
521 printf("New Adj LSA.....\n");
Obaid Amine63022a2013-01-15 23:36:40 -0600522 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
a64adam04be6482013-01-17 11:29:32 -0600523
524 if ( nlsr->debugging )
525 printf("Content Data: %s \n",content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600526 process_incoming_sync_content_lsa(content_data);
527 }
528 else
529 {
a64adam04be6482013-01-17 11:29:32 -0600530 if ( nlsr->debugging )
531 printf("Adj LSA / Newer Adj LSA already exists in LSDB\n");
Obaid Amine63022a2013-01-15 23:36:40 -0600532 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
a64adam04be6482013-01-17 11:29:32 -0600533 if ( nlsr->debugging )
534 printf("Content Data: %s \n",content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600535 }
536 }
537 else
538 {
a64adam04be6482013-01-17 11:29:32 -0600539 if ( nlsr->debugging )
540 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
akmhoque4ae16942012-12-10 11:50:43 -0600541 }
akmhoque8fdd6412012-12-04 15:05:33 -0600542 }
akmhoqueed418f32013-01-30 12:25:04 -0600543 else if(ls_type == LS_TYPE_COR)
544 {
akmhoque496c88f2013-02-08 05:24:18 -0600545 res=ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size);
546 ccn_name_chop(content_name,components,-2);
547 get_name_part(orig_router,content_name,components,0);
akmhoqueed418f32013-01-30 12:25:04 -0600548
549 if ( nlsr->debugging )
akmhoque496c88f2013-02-08 05:24:18 -0600550 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",orig_router->name,ls_type,origtime);
akmhoqueed418f32013-01-30 12:25:04 -0600551
552 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
553 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) )
554 {
555 int is_new_cor_lsa=check_is_new_cor_lsa(orig_router->name,(char *)lst,(char *)origtime);
556 if ( is_new_cor_lsa == 1 )
557 {
558 if ( nlsr->debugging )
559 printf("New Cor LSA.....\n");
560 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
561
562 if ( nlsr->debugging )
563 printf("Content Data: %s \n",content_data);
564 process_incoming_sync_content_lsa(content_data);
565 }
566 else
567 {
568 if ( nlsr->debugging )
569 printf("Cor LSA / Newer Cor LSA already exists in LSDB\n");
570 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
571 if ( nlsr->debugging )
572 printf("Content Data: %s \n",content_data);
573 }
574 }
575 else
576 {
577 if ( nlsr->debugging )
578 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
579 }
580
581 }
akmhoque8fdd6412012-12-04 15:05:33 -0600582
Obaid Amine63022a2013-01-15 23:36:40 -0600583 if (content_data != NULL)
584 free(content_data);
akmhoque8fdd6412012-12-04 15:05:33 -0600585 ccn_charbuf_destroy(&uri);
akmhoque3b6c66d2013-01-31 15:30:23 -0600586 //01/31/2013
587 free(time_stamp);
akmhoque8fdd6412012-12-04 15:05:33 -0600588}
589
akmhoque8fdd6412012-12-04 15:05:33 -0600590void
591sync_monitor(char *topo_prefix, char *slice_prefix)
592{
593
594 static struct ccns_name_closure nc={0};
595
596 nlsr->closure = &nc;
597 struct ccn_charbuf *prefix = ccn_charbuf_create();
598 struct ccn_charbuf *roothash = NULL;
599 struct ccn_charbuf *topo = ccn_charbuf_create();
600 nlsr->slice = ccns_slice_create();
601 ccn_charbuf_reset(prefix);
602 ccn_charbuf_reset(topo);
603
604 ccn_charbuf_reset(prefix);
605 ccn_name_from_uri(prefix, slice_prefix);
606
607 ccn_charbuf_reset(topo);
608 ccn_name_from_uri(topo, topo_prefix);
609
610
611 ccns_slice_set_topo_prefix(nlsr->slice, topo, prefix);
Syed Obaid Amin3a3864b2013-01-09 03:05:50 -0600612 nlsr->closure->callback = &sync_cb;
akmhoque8fdd6412012-12-04 15:05:33 -0600613 nlsr->ccns = ccns_open(nlsr->ccn, nlsr->slice, nlsr->closure, roothash, NULL);
akmhoque3b6c66d2013-01-31 15:30:23 -0600614
615 //01/31/2013
616 ccn_charbuf_destroy(&prefix);
617 ccn_charbuf_destroy(&topo);
akmhoque8fdd6412012-12-04 15:05:33 -0600618}
619
620struct ccn_charbuf *
621make_template(int scope)
622{
623 struct ccn_charbuf *templ = NULL;
624 templ = ccn_charbuf_create();
625 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
626 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
627 ccn_charbuf_append_closer(templ); /* </Name> */
628 if (0 <= scope && scope <= 2)
629 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
630 ccn_charbuf_append_closer(templ); /* </Interest> */
631 return(templ);
632}
633
akmhoquefc5176d2013-01-18 09:50:12 -0600634int
akmhoque8fdd6412012-12-04 15:05:33 -0600635write_data_to_repo(char *data, char *name_prefix)
636{
akmhoque260698b2013-01-17 03:54:09 -0600637 if ( nlsr->debugging )
638 {
639 printf("write_data_to_repo called\n");
akmhoque47d19d12013-01-17 04:00:32 -0600640 printf("Content Name: %s \n",name_prefix);
akmhoque260698b2013-01-17 03:54:09 -0600641 printf("Content Data: %s \n",data);
642 }
akmhoque8fdd6412012-12-04 15:05:33 -0600643
akmhoque2ee18032013-01-22 10:01:23 -0600644 struct ccn *temp_ccn;
645 temp_ccn=ccn_create();
646 int ccn_fd=ccn_connect(temp_ccn, NULL);
647 if(ccn_fd == -1)
648 {
649 fprintf(stderr,"Could not connect to ccnd for Data Writing\n");
650 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd for Data Writing\n");
651 return -1;
652 }
akmhoque8fdd6412012-12-04 15:05:33 -0600653 struct ccn_charbuf *name = NULL;
654 struct ccn_seqwriter *w = NULL;
akmhoque2ee18032013-01-22 10:01:23 -0600655 int blocksize = 4096;
akmhoque8fdd6412012-12-04 15:05:33 -0600656 int freshness = -1;
657 int torepo = 1;
658 int scope = 1;
659 int res;
660 size_t blockread;
661 struct ccn_charbuf *templ;
662
663 name = ccn_charbuf_create();
664 res = ccn_name_from_uri(name, name_prefix);
665 if (res < 0) {
666 fprintf(stderr, "bad CCN URI: %s\n",name_prefix);
akmhoquefc5176d2013-01-18 09:50:12 -0600667 return -1;
akmhoque8fdd6412012-12-04 15:05:33 -0600668 }
669
670
akmhoque2ee18032013-01-22 10:01:23 -0600671 w = ccn_seqw_create(temp_ccn, name);
akmhoque8fdd6412012-12-04 15:05:33 -0600672 if (w == NULL) {
673 fprintf(stderr, "ccn_seqw_create failed\n");
akmhoquefc5176d2013-01-18 09:50:12 -0600674 return -1;
akmhoque8fdd6412012-12-04 15:05:33 -0600675 }
676 ccn_seqw_set_block_limits(w, blocksize, blocksize);
677 if (freshness > -1)
678 ccn_seqw_set_freshness(w, freshness);
679 if (torepo) {
680 struct ccn_charbuf *name_v = ccn_charbuf_create();
681 ccn_seqw_get_name(w, name_v);
682 ccn_name_from_uri(name_v, "%C1.R.sw");
683 ccn_name_append_nonce(name_v);
684 templ = make_template(scope);
akmhoque2ee18032013-01-22 10:01:23 -0600685 res = ccn_get(temp_ccn, name_v, templ, 60000, NULL, NULL, NULL, 0);
akmhoque8fdd6412012-12-04 15:05:33 -0600686 ccn_charbuf_destroy(&templ);
687 ccn_charbuf_destroy(&name_v);
688 if (res < 0) {
689 fprintf(stderr, "No response from repository\n");
akmhoquefc5176d2013-01-18 09:50:12 -0600690 return -1;
akmhoque8fdd6412012-12-04 15:05:33 -0600691 }
692 }
693
694
695
696
697 blockread = 0;
akmhoque260698b2013-01-17 03:54:09 -0600698
akmhoque8fdd6412012-12-04 15:05:33 -0600699
700 blockread=strlen(data);
701
702 if (blockread > 0) {
akmhoque2ee18032013-01-22 10:01:23 -0600703 ccn_run(temp_ccn, 100);
akmhoque8fdd6412012-12-04 15:05:33 -0600704 res = ccn_seqw_write(w, data, blockread);
akmhoque260698b2013-01-17 03:54:09 -0600705 while (res == -1) {
akmhoque2ee18032013-01-22 10:01:23 -0600706 ccn_run(temp_ccn, 100);
akmhoque260698b2013-01-17 03:54:09 -0600707 res = ccn_seqw_write(w, data, blockread);
708 }
akmhoque8fdd6412012-12-04 15:05:33 -0600709 }
710
akmhoque8fdd6412012-12-04 15:05:33 -0600711 ccn_seqw_close(w);
akmhoque2ee18032013-01-22 10:01:23 -0600712 ccn_run(temp_ccn, 100);
akmhoque8fdd6412012-12-04 15:05:33 -0600713 ccn_charbuf_destroy(&name);
akmhoque2ee18032013-01-22 10:01:23 -0600714 ccn_destroy(&temp_ccn);
akmhoquefc5176d2013-01-18 09:50:12 -0600715
716 return 0;
akmhoque8fdd6412012-12-04 15:05:33 -0600717}
718
719
720int
721create_sync_slice(char *topo_prefix, char *slice_prefix)
722{
723 int res;
724 struct ccns_slice *slice;
725 struct ccn_charbuf *prefix = ccn_charbuf_create();
726 struct ccn_charbuf *topo = ccn_charbuf_create();
727 struct ccn_charbuf *clause = ccn_charbuf_create();
728 struct ccn_charbuf *slice_name = ccn_charbuf_create();
729 struct ccn_charbuf *slice_uri = ccn_charbuf_create();
730
731 if (prefix == NULL || topo == NULL || clause == NULL ||
732 slice_name == NULL || slice_uri == NULL) {
733 fprintf(stderr, "Unable to allocate required memory.\n");
akmhoquefc5176d2013-01-18 09:50:12 -0600734 return -1;
akmhoque8fdd6412012-12-04 15:05:33 -0600735 }
736
737
738 slice = ccns_slice_create();
739
740 ccn_charbuf_reset(topo);
741 ccn_name_from_uri(topo, topo_prefix);
742 ccn_charbuf_reset(prefix);
743 ccn_name_from_uri(prefix,slice_prefix );
744 ccns_slice_set_topo_prefix(slice, topo, prefix);
745
746
747 res = ccns_write_slice(nlsr->ccn, slice, slice_name);
akmhoque3b6c66d2013-01-31 15:30:23 -0600748
749 //01/31/2013
akmhoque8fdd6412012-12-04 15:05:33 -0600750 ccns_slice_destroy(&slice);
751 ccn_charbuf_destroy(&prefix);
752 ccn_charbuf_destroy(&topo);
753 ccn_charbuf_destroy(&clause);
754 ccn_charbuf_destroy(&slice_name);
755 ccn_charbuf_destroy(&slice_uri);
akmhoque34425762013-01-23 09:33:29 -0600756
akmhoque8fdd6412012-12-04 15:05:33 -0600757 return 0;
758}
759