blob: b3bc5863950ed0325c2879e9c66293d69806b1f1 [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);
akmhoque3c0c95d2013-01-31 16:46:24 -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
akmhoqueacef1e62013-01-31 17:15:08 -060081 /*struct ccn_indexbuf cid={0};
Syed Obaid Amin3a3864b2013-01-09 03:05:50 -060082
83 struct ccn_indexbuf *components=&cid;
akmhoque3c0c95d2013-01-31 16:46:24 -060084 ccn_name_split (name, components);
85 ccn_name_chop(name,components,-3);
Syed Obaid Amin3a3864b2013-01-09 03:05:50 -060086
akmhoque3c0c95d2013-01-31 16:46:24 -060087 process_content_from_sync(name,components);
Syed Obaid Amin3a3864b2013-01-09 03:05:50 -060088
akmhoqueacef1e62013-01-31 17:15:08 -060089 */
90 struct ccn_indexbuf cid={0};
91 struct ccn_indexbuf *components=&cid;
akmhoquefc5176d2013-01-18 09:50:12 -060092
akmhoqueacef1e62013-01-31 17:15:08 -060093 char *name_string=ccn_charbuf_as_string(name);
94 struct ccn_charbuf *content_name = ccn_charbuf_create();
95 int res=ccn_name_from_uri(content_name,name_string);
96
97 printf("Res = %d \n",res);
98
99 res=ccn_name_split (content_name, components);
100
101 printf("Res = %d \n",res);
102
103 ccn_charbuf_destroy(&content_name);
Syed Obaid Amin3a3864b2013-01-09 03:05:50 -0600104
105 return(0);
106}
akmhoque3b6c66d2013-01-31 15:30:23 -0600107
akmhoque8fdd6412012-12-04 15:05:33 -0600108
109int
110get_lsa_position(struct ccn_charbuf * ccnb, struct ccn_indexbuf *comps)
111{
112
113
114
115 int res,i;
akmhoque3b6c66d2013-01-31 15:30:23 -0600116 int lsa_position=0;
akmhoque8fdd6412012-12-04 15:05:33 -0600117 int name_comps=(int)comps->n;
118
119 for(i=0;i<name_comps;i++)
120 {
121 res=ccn_name_comp_strcmp(ccnb->buf,comps,i,"LSA");
122 if( res == 0)
123 {
124 lsa_position=i;
125 break;
126 }
127 }
128
129 return lsa_position;
130
131}
132
133void
134get_name_part(struct name_prefix *name_part,struct ccn_charbuf * interest_ccnb, struct ccn_indexbuf *interest_comps, int offset)
135{
136
137
138
139 int res,i;
140 int lsa_position=0;
141 int len=0;
142
akmhoque2ee18032013-01-22 10:01:23 -0600143
144 struct ccn_indexbuf cid={0};
145 struct ccn_indexbuf *components=&cid;
146 struct ccn_charbuf *name=ccn_charbuf_create();
147 ccn_name_from_uri(name,nlsr->slice_prefix);
148 ccn_name_split (name, components);
149 lsa_position=components->n-2;
akmhoque3b6c66d2013-01-31 15:30:23 -0600150
akmhoque2ee18032013-01-22 10:01:23 -0600151 ccn_charbuf_destroy(&name);
152
akmhoque8fdd6412012-12-04 15:05:33 -0600153
154 const unsigned char *comp_ptr1;
155 size_t comp_size;
156 for(i=lsa_position+1+offset;i<interest_comps->n-1;i++)
157 {
158 res=ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1, &comp_size);
159 len+=1;
160 len+=(int)comp_size;
161 }
162 len++;
163
164 char *neighbor=(char *)malloc(len);
165 memset(neighbor,0,len);
166
167 for(i=lsa_position+1+offset; i<interest_comps->n-1;i++)
168 {
169 res=ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1, &comp_size);
170 memcpy(neighbor+strlen(neighbor),"/",1);
171 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,strlen((char *)comp_ptr1));
172
173 }
174
175 name_part->name=(char *)malloc(strlen(neighbor)+1);
176 memset(name_part->name,0,strlen(neighbor)+1);
177 memcpy(name_part->name,neighbor,strlen(neighbor)+1);
178 name_part->length=strlen(neighbor)+1;
179
akmhoque3b6c66d2013-01-31 15:30:23 -0600180 // Add 01/31/2013
181 free(neighbor);
akmhoque8fdd6412012-12-04 15:05:33 -0600182}
183
akmhoque09c0afa2012-12-14 09:27:00 -0600184void
185get_host_name_from_command_string(struct name_prefix *name_part,char *nbr_name_uri, int offset)
186{
187
188
189
190 int res,i;
191 int len=0;
192 const unsigned char *comp_ptr1;
193 size_t comp_size;
194
195 struct ccn_charbuf *name=ccn_charbuf_create();
196 name = ccn_charbuf_create();
197 res = ccn_name_from_uri(name,nbr_name_uri);
198 if (res < 0) {
199 fprintf(stderr, "Bad ccn URI: %s\n", nbr_name_uri);
200 exit(1);
201 }
202
203 struct ccn_indexbuf cid={0};
204
205 struct ccn_indexbuf *components=&cid;
206 ccn_name_split (name, components);
207
208 for(i=components->n-2;i> (0+offset);i--)
209 {
210 res=ccn_name_comp_get(name->buf, components,i,&comp_ptr1, &comp_size);
211 len+=1;
212 len+=(int)comp_size;
213 }
214 len++;
215
216 char *neighbor=(char *)malloc(len);
217 memset(neighbor,0,len);
218
219 for(i=components->n-2;i> (0+offset);i--)
220 {
221 res=ccn_name_comp_get(name->buf, components,i,&comp_ptr1, &comp_size);
222 if ( i != components->n-2)
223 memcpy(neighbor+strlen(neighbor),".",1);
224 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,strlen((char *)comp_ptr1));
225
226 }
227
228 name_part->name=(char *)malloc(strlen(neighbor)+1);
229 memset(name_part->name,0,strlen(neighbor)+1);
230 memcpy(name_part->name,neighbor,strlen(neighbor)+1);
231 name_part->length=strlen(neighbor)+1;
232
akmhoque3b6c66d2013-01-31 15:30:23 -0600233 // 01/31/2013
234 free(neighbor);
235 ccn_charbuf_destroy(&name);
akmhoque09c0afa2012-12-14 09:27:00 -0600236}
237
akmhoque8fdd6412012-12-04 15:05:33 -0600238
akmhoqueed418f32013-01-30 12:25:04 -0600239
soamin15043622013-01-15 23:10:26 -0600240void
Obaid Amine63022a2013-01-15 23:36:40 -0600241get_content_by_content_name(char *content_name, unsigned char **content_data)
akmhoque8fdd6412012-12-04 15:05:33 -0600242{
243
soamin15043622013-01-15 23:10:26 -0600244 struct ccn_charbuf *name = NULL;
245 struct ccn_charbuf *templ = NULL;
246 struct ccn_charbuf *resultbuf = NULL;
247 struct ccn_parsed_ContentObject pcobuf = { 0 };
248 int res;
249 int allow_stale = 0;
250 int content_only = 1;
251 int scope = -1;
252 const unsigned char *ptr;
253 size_t length;
254 int resolve_version = CCN_V_HIGHEST;
255 int timeout_ms = 3000;
256 const unsigned lifetime_default = CCN_INTEREST_LIFETIME_SEC << 12;
257 unsigned lifetime_l12 = lifetime_default;
258 int get_flags = 0;
akmhoque8fdd6412012-12-04 15:05:33 -0600259
soamin15043622013-01-15 23:10:26 -0600260 name = ccn_charbuf_create();
261 res = ccn_name_from_uri(name,content_name);
262 if (res < 0) {
263 fprintf(stderr, "Bad ccn URI: %s\n", content_name);
264 exit(1);
265 }
266
akmhoque8fdd6412012-12-04 15:05:33 -0600267 if (allow_stale || lifetime_l12 != lifetime_default || scope != -1) {
soamin15043622013-01-15 23:10:26 -0600268 templ = ccn_charbuf_create();
269 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
270 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
271 ccn_charbuf_append_closer(templ); /* </Name> */
akmhoque8fdd6412012-12-04 15:05:33 -0600272 if (allow_stale) {
273 ccn_charbuf_append_tt(templ, CCN_DTAG_AnswerOriginKind, CCN_DTAG);
274 ccnb_append_number(templ,
soamin15043622013-01-15 23:10:26 -0600275 CCN_AOK_DEFAULT | CCN_AOK_STALE);
akmhoque8fdd6412012-12-04 15:05:33 -0600276 ccn_charbuf_append_closer(templ); /* </AnswerOriginKind> */
277 }
soamin15043622013-01-15 23:10:26 -0600278 if (scope != -1) {
279 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
280 }
akmhoque8fdd6412012-12-04 15:05:33 -0600281 if (lifetime_l12 != lifetime_default) {
282 /*
283 * Choose the interest lifetime so there are at least 3
284 * expressions (in the unsatisfied case).
285 */
286 unsigned char buf[3] = { 0 };
287 int i;
288 for (i = sizeof(buf) - 1; i >= 0; i--, lifetime_l12 >>= 8)
289 buf[i] = lifetime_l12 & 0xff;
290 ccnb_append_tagged_blob(templ, CCN_DTAG_InterestLifetime, buf, sizeof(buf));
291 }
soamin15043622013-01-15 23:10:26 -0600292 ccn_charbuf_append_closer(templ); /* </Interest> */
293 }
294 resultbuf = ccn_charbuf_create();
295 if (resolve_version != 0) {
296 res = ccn_resolve_version(nlsr->ccn, name, resolve_version, 500);
297 if (res >= 0) {
298 ccn_uri_append(resultbuf, name->buf, name->length, 1);
soamin15043622013-01-15 23:10:26 -0600299 resultbuf->length = 0;
300 }
301 }
302 res = ccn_get(nlsr->ccn, name, templ, timeout_ms, resultbuf, &pcobuf, NULL, get_flags);
303 if (res >= 0) {
304 ptr = resultbuf->buf;
305 length = resultbuf->length;
306 if (content_only){
307 ccn_content_get_value(ptr, length, &pcobuf, &ptr, &length);
Obaid Amine63022a2013-01-15 23:36:40 -0600308 *content_data = (unsigned char *) calloc(length, sizeof(char *));
309 memcpy (*content_data, ptr, length);
soamin15043622013-01-15 23:10:26 -0600310 }
311 }
312 ccn_charbuf_destroy(&resultbuf);
313 ccn_charbuf_destroy(&templ);
314 ccn_charbuf_destroy(&name);
akmhoque8fdd6412012-12-04 15:05:33 -0600315}
316
akmhoque4ae16942012-12-10 11:50:43 -0600317void
soamin15043622013-01-15 23:10:26 -0600318process_incoming_sync_content_lsa( unsigned char *content_data)
akmhoque4ae16942012-12-10 11:50:43 -0600319{
320
321
322 if ( nlsr->debugging )
akmhoqueed418f32013-01-30 12:25:04 -0600323 printf("process_incoming_sync_content_lsa called \n");
akmhoque4ae16942012-12-10 11:50:43 -0600324
325 char *sep="|";
326 char *rem;
327 char *orig_router;
328 char *orl;
329 int orig_router_length;
330 char *lst;
331 int ls_type;
332 char *lsid;
333 long int ls_id;
334 char *isvld;
335 int isValid;
336 char *num_link;
337 int no_link;
338 char *np;
339 char *np_length;
340 int name_length;
341 char *data;
342 char *orig_time;
343
344
345 if ( nlsr->debugging )
346 printf("LSA Data \n");
akmhoque4ae16942012-12-10 11:50:43 -0600347
soamin15043622013-01-15 23:10:26 -0600348 if( strlen((char *)content_data ) > 0 )
akmhoque4ae16942012-12-10 11:50:43 -0600349 {
350
351 orig_router=strtok_r((char *)content_data,sep,&rem);
352 orl=strtok_r(NULL,sep,&rem);
353 orig_router_length=atoi(orl);
354
355 if ( nlsr->debugging )
356 {
357 printf(" Orig Router Name : %s\n",orig_router);
358 printf(" Orig Router Length: %d\n",orig_router_length);
359 }
360
361 lst=strtok_r(NULL,sep,&rem);
362 ls_type=atoi(lst);
363
364 if ( nlsr->debugging )
365 printf(" LS Type : %d\n",ls_type);
366
367 if ( ls_type == LS_TYPE_NAME )
368 {
369 lsid=strtok_r(NULL,sep,&rem);
370 ls_id=atoi(lsid);
371 orig_time=strtok_r(NULL,sep,&rem);
372 isvld=strtok_r(NULL,sep,&rem);
373 isValid=atoi(isvld);
374 np=strtok_r(NULL,sep,&rem);
375 np_length=strtok_r(NULL,sep,&rem);
376 name_length=atoi(np_length);
377 if ( nlsr->debugging )
378 {
379 printf(" LS ID : %ld\n",ls_id);
380 printf(" isValid : %d\n",isValid);
381 printf(" Name Prefix : %s\n",np);
382 printf(" Orig Time : %s\n",orig_time);
383 printf(" Name Prefix length: %d\n",name_length);
384 }
385
386 build_and_install_others_name_lsa(orig_router,ls_type,ls_id,orig_time,isValid,np);
387
388 print_name_lsdb();
389
390 }
391 else if ( ls_type == LS_TYPE_ADJ )
392 {
393 orig_time=strtok_r(NULL,sep,&rem);
394 num_link=strtok_r(NULL,sep,&rem);
395 no_link=atoi(num_link);
396 data=rem;
397
398 if ( nlsr->debugging )
399 {
akmhoqueed418f32013-01-30 12:25:04 -0600400 printf(" Orig Time : %s\n",orig_time);
akmhoque4ae16942012-12-10 11:50:43 -0600401 printf(" No Link : %d\n",no_link);
402 printf(" Data : %s\n",data);
403 }
404 build_and_install_others_adj_lsa(orig_router,ls_type,orig_time,no_link,data);
405 }
akmhoqueed418f32013-01-30 12:25:04 -0600406 else if ( ls_type == LS_TYPE_COR )
407 {
408 orig_time=strtok_r(NULL,sep,&rem);
409 char *cor_r=strtok_r(NULL,sep,&rem);
410 char *cor_theta=strtok_r(NULL,sep,&rem);
411
412 double r, theta;
413 r=strtof(cor_r,NULL);
414 theta=strtof(cor_theta,NULL);
415
416 if ( nlsr->debugging )
417 {
418 printf(" Orig Time : %s\n",orig_time);
419 printf(" Cor R : %f\n",r);
420 printf(" Cor Theta : %f\n",theta);
421 }
422 build_and_install_others_cor_lsa(orig_router,ls_type,orig_time, (double)r, (double)theta);
423 }
424
akmhoque4ae16942012-12-10 11:50:43 -0600425 }
426}
427
akmhoque8fdd6412012-12-04 15:05:33 -0600428void
429process_content_from_sync(struct ccn_charbuf *content_name, struct ccn_indexbuf *components)
430{
431 int lsa_position;
432 int res;
433 size_t comp_size;
434 const unsigned char *lst;
435 const unsigned char *lsid;
436 const unsigned char *origtime;
437
438 int ls_type;
439 long int ls_id=0;
440
soamin15043622013-01-15 23:10:26 -0600441 unsigned char *content_data = NULL;
442
akmhoque4ae16942012-12-10 11:50:43 -0600443 char *time_stamp=(char *)malloc(20);
444 memset(time_stamp,0,20);
445 get_current_timestamp_micro(time_stamp);
446
akmhoque8fdd6412012-12-04 15:05:33 -0600447 struct ccn_charbuf *uri = ccn_charbuf_create();
448 ccn_uri_append(uri, content_name->buf, content_name->length, 0);
449
450 struct name_prefix *orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix));
451
akmhoque2ee18032013-01-22 10:01:23 -0600452
453 struct ccn_indexbuf cid={0};
454 struct ccn_indexbuf *temp_components=&cid;
455 struct ccn_charbuf *name=ccn_charbuf_create();
456 ccn_name_from_uri(name,nlsr->slice_prefix);
457 ccn_name_split (name, temp_components);
458 lsa_position=temp_components->n-2;
akmhoque2ee18032013-01-22 10:01:23 -0600459 ccn_charbuf_destroy(&name);
460
akmhoque8fdd6412012-12-04 15:05:33 -0600461
462 res=ccn_name_comp_get(content_name->buf, components,lsa_position+1,&lst, &comp_size);
463
akmhoque8fdd6412012-12-04 15:05:33 -0600464 ls_type=atoi((char *)lst);
465 if(ls_type == LS_TYPE_NAME)
466 {
akmhoque4ae16942012-12-10 11:50:43 -0600467
akmhoque8fdd6412012-12-04 15:05:33 -0600468 res=ccn_name_comp_get(content_name->buf, components,lsa_position+2,&lsid, &comp_size);
469 ls_id=atoi((char *)lsid);
470 res=ccn_name_comp_get(content_name->buf, components,lsa_position+3,&origtime, &comp_size);
471 get_name_part(orig_router,content_name,components,3);
akmhoque8fdd6412012-12-04 15:05:33 -0600472
akmhoque4ae16942012-12-10 11:50:43 -0600473 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
474
akmhoque4ae16942012-12-10 11:50:43 -0600475 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 -0600476 {
akmhoque4ae16942012-12-10 11:50:43 -0600477 int is_new_name_lsa=check_is_new_name_lsa(orig_router->name,(char *)lst,(char *)lsid,(char *)origtime);
478 if ( is_new_name_lsa == 1 )
479 {
a64adam04be6482013-01-17 11:29:32 -0600480 if ( nlsr->debugging )
481 printf("New NAME LSA.....\n");
Obaid Amine63022a2013-01-15 23:36:40 -0600482 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
a64adamf7b051d2013-01-17 11:39:58 -0600483 if ( nlsr->debugging )
484 printf("Content Data: %s \n",content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600485 process_incoming_sync_content_lsa(content_data);
486 }
487 else
488 {
a64adam04be6482013-01-17 11:29:32 -0600489 if ( nlsr->debugging )
490 printf("Name LSA / Newer Name LSA already xists in LSDB\n");
Obaid Amine63022a2013-01-15 23:36:40 -0600491 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
a64adam04be6482013-01-17 11:29:32 -0600492
493 if ( nlsr->debugging )
494 printf("Content Data: %s \n",content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600495 }
akmhoque8fdd6412012-12-04 15:05:33 -0600496 }
497 else
498 {
a64adam04be6482013-01-17 11:29:32 -0600499 if ( nlsr->debugging )
500 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
akmhoque8fdd6412012-12-04 15:05:33 -0600501 }
502 }
503 else if(ls_type == LS_TYPE_ADJ)
504 {
505 res=ccn_name_comp_get(content_name->buf, components,lsa_position+2,&origtime, &comp_size);
506 get_name_part(orig_router,content_name,components,2);
a64adam04be6482013-01-17 11:29:32 -0600507
508 if ( nlsr->debugging )
509 printf("Orig Time: %s\nOrig Router: %s\n",origtime,orig_router->name);
akmhoque8fdd6412012-12-04 15:05:33 -0600510
akmhoque4ae16942012-12-10 11:50:43 -0600511 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque4ae16942012-12-10 11:50:43 -0600512 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) )
513 {
514 int is_new_adj_lsa=check_is_new_adj_lsa(orig_router->name,(char *)lst,(char *)origtime);
515 if ( is_new_adj_lsa == 1 )
516 {
a64adam04be6482013-01-17 11:29:32 -0600517 if ( nlsr->debugging )
518 printf("New Adj LSA.....\n");
Obaid Amine63022a2013-01-15 23:36:40 -0600519 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
a64adam04be6482013-01-17 11:29:32 -0600520
521 if ( nlsr->debugging )
522 printf("Content Data: %s \n",content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600523 process_incoming_sync_content_lsa(content_data);
524 }
525 else
526 {
a64adam04be6482013-01-17 11:29:32 -0600527 if ( nlsr->debugging )
528 printf("Adj LSA / Newer Adj LSA already exists in LSDB\n");
Obaid Amine63022a2013-01-15 23:36:40 -0600529 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
a64adam04be6482013-01-17 11:29:32 -0600530 if ( nlsr->debugging )
531 printf("Content Data: %s \n",content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600532 }
533 }
534 else
535 {
a64adam04be6482013-01-17 11:29:32 -0600536 if ( nlsr->debugging )
537 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
akmhoque4ae16942012-12-10 11:50:43 -0600538 }
akmhoque8fdd6412012-12-04 15:05:33 -0600539 }
akmhoqueed418f32013-01-30 12:25:04 -0600540 else if(ls_type == LS_TYPE_COR)
541 {
542 res=ccn_name_comp_get(content_name->buf, components,lsa_position+2,&origtime, &comp_size);
543 get_name_part(orig_router,content_name,components,2);
544
545 if ( nlsr->debugging )
546 printf("Orig Time: %s\nOrig Router: %s\n",origtime,orig_router->name);
547
548 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
549 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) )
550 {
551 int is_new_cor_lsa=check_is_new_cor_lsa(orig_router->name,(char *)lst,(char *)origtime);
552 if ( is_new_cor_lsa == 1 )
553 {
554 if ( nlsr->debugging )
555 printf("New Cor LSA.....\n");
556 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
557
558 if ( nlsr->debugging )
559 printf("Content Data: %s \n",content_data);
560 process_incoming_sync_content_lsa(content_data);
561 }
562 else
563 {
564 if ( nlsr->debugging )
565 printf("Cor LSA / Newer Cor LSA already exists in LSDB\n");
566 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
567 if ( nlsr->debugging )
568 printf("Content Data: %s \n",content_data);
569 }
570 }
571 else
572 {
573 if ( nlsr->debugging )
574 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
575 }
576
577 }
akmhoque8fdd6412012-12-04 15:05:33 -0600578
Obaid Amine63022a2013-01-15 23:36:40 -0600579 if (content_data != NULL)
580 free(content_data);
akmhoque8fdd6412012-12-04 15:05:33 -0600581 ccn_charbuf_destroy(&uri);
akmhoque3b6c66d2013-01-31 15:30:23 -0600582 //01/31/2013
583 free(time_stamp);
akmhoque8fdd6412012-12-04 15:05:33 -0600584}
585
akmhoque8fdd6412012-12-04 15:05:33 -0600586void
587sync_monitor(char *topo_prefix, char *slice_prefix)
588{
589
590 static struct ccns_name_closure nc={0};
591
592 nlsr->closure = &nc;
593 struct ccn_charbuf *prefix = ccn_charbuf_create();
594 struct ccn_charbuf *roothash = NULL;
595 struct ccn_charbuf *topo = ccn_charbuf_create();
596 nlsr->slice = ccns_slice_create();
597 ccn_charbuf_reset(prefix);
598 ccn_charbuf_reset(topo);
599
600 ccn_charbuf_reset(prefix);
601 ccn_name_from_uri(prefix, slice_prefix);
602
603 ccn_charbuf_reset(topo);
604 ccn_name_from_uri(topo, topo_prefix);
605
606
607 ccns_slice_set_topo_prefix(nlsr->slice, topo, prefix);
Syed Obaid Amin3a3864b2013-01-09 03:05:50 -0600608 nlsr->closure->callback = &sync_cb;
akmhoque8fdd6412012-12-04 15:05:33 -0600609 nlsr->ccns = ccns_open(nlsr->ccn, nlsr->slice, nlsr->closure, roothash, NULL);
akmhoque3b6c66d2013-01-31 15:30:23 -0600610
611 //01/31/2013
612 ccn_charbuf_destroy(&prefix);
613 ccn_charbuf_destroy(&topo);
akmhoque8fdd6412012-12-04 15:05:33 -0600614}
615
616struct ccn_charbuf *
617make_template(int scope)
618{
619 struct ccn_charbuf *templ = NULL;
620 templ = ccn_charbuf_create();
621 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
622 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
623 ccn_charbuf_append_closer(templ); /* </Name> */
624 if (0 <= scope && scope <= 2)
625 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
626 ccn_charbuf_append_closer(templ); /* </Interest> */
627 return(templ);
628}
629
akmhoquefc5176d2013-01-18 09:50:12 -0600630int
akmhoque8fdd6412012-12-04 15:05:33 -0600631write_data_to_repo(char *data, char *name_prefix)
632{
akmhoque260698b2013-01-17 03:54:09 -0600633 if ( nlsr->debugging )
634 {
635 printf("write_data_to_repo called\n");
akmhoque47d19d12013-01-17 04:00:32 -0600636 printf("Content Name: %s \n",name_prefix);
akmhoque260698b2013-01-17 03:54:09 -0600637 printf("Content Data: %s \n",data);
638 }
akmhoque8fdd6412012-12-04 15:05:33 -0600639
akmhoque2ee18032013-01-22 10:01:23 -0600640 struct ccn *temp_ccn;
641 temp_ccn=ccn_create();
642 int ccn_fd=ccn_connect(temp_ccn, NULL);
643 if(ccn_fd == -1)
644 {
645 fprintf(stderr,"Could not connect to ccnd for Data Writing\n");
646 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd for Data Writing\n");
647 return -1;
648 }
akmhoque8fdd6412012-12-04 15:05:33 -0600649 struct ccn_charbuf *name = NULL;
650 struct ccn_seqwriter *w = NULL;
akmhoque2ee18032013-01-22 10:01:23 -0600651 int blocksize = 4096;
akmhoque8fdd6412012-12-04 15:05:33 -0600652 int freshness = -1;
653 int torepo = 1;
654 int scope = 1;
655 int res;
656 size_t blockread;
657 struct ccn_charbuf *templ;
658
659 name = ccn_charbuf_create();
660 res = ccn_name_from_uri(name, name_prefix);
661 if (res < 0) {
662 fprintf(stderr, "bad CCN URI: %s\n",name_prefix);
akmhoquefc5176d2013-01-18 09:50:12 -0600663 return -1;
akmhoque8fdd6412012-12-04 15:05:33 -0600664 }
665
666
akmhoque2ee18032013-01-22 10:01:23 -0600667 w = ccn_seqw_create(temp_ccn, name);
akmhoque8fdd6412012-12-04 15:05:33 -0600668 if (w == NULL) {
669 fprintf(stderr, "ccn_seqw_create failed\n");
akmhoquefc5176d2013-01-18 09:50:12 -0600670 return -1;
akmhoque8fdd6412012-12-04 15:05:33 -0600671 }
672 ccn_seqw_set_block_limits(w, blocksize, blocksize);
673 if (freshness > -1)
674 ccn_seqw_set_freshness(w, freshness);
675 if (torepo) {
676 struct ccn_charbuf *name_v = ccn_charbuf_create();
677 ccn_seqw_get_name(w, name_v);
678 ccn_name_from_uri(name_v, "%C1.R.sw");
679 ccn_name_append_nonce(name_v);
680 templ = make_template(scope);
akmhoque2ee18032013-01-22 10:01:23 -0600681 res = ccn_get(temp_ccn, name_v, templ, 60000, NULL, NULL, NULL, 0);
akmhoque8fdd6412012-12-04 15:05:33 -0600682 ccn_charbuf_destroy(&templ);
683 ccn_charbuf_destroy(&name_v);
684 if (res < 0) {
685 fprintf(stderr, "No response from repository\n");
akmhoquefc5176d2013-01-18 09:50:12 -0600686 return -1;
akmhoque8fdd6412012-12-04 15:05:33 -0600687 }
688 }
689
690
691
692
693 blockread = 0;
akmhoque260698b2013-01-17 03:54:09 -0600694
akmhoque8fdd6412012-12-04 15:05:33 -0600695
696 blockread=strlen(data);
697
698 if (blockread > 0) {
akmhoque2ee18032013-01-22 10:01:23 -0600699 ccn_run(temp_ccn, 100);
akmhoque8fdd6412012-12-04 15:05:33 -0600700 res = ccn_seqw_write(w, data, blockread);
akmhoque260698b2013-01-17 03:54:09 -0600701 while (res == -1) {
akmhoque2ee18032013-01-22 10:01:23 -0600702 ccn_run(temp_ccn, 100);
akmhoque260698b2013-01-17 03:54:09 -0600703 res = ccn_seqw_write(w, data, blockread);
704 }
akmhoque8fdd6412012-12-04 15:05:33 -0600705 }
706
akmhoque8fdd6412012-12-04 15:05:33 -0600707 ccn_seqw_close(w);
akmhoque2ee18032013-01-22 10:01:23 -0600708 ccn_run(temp_ccn, 100);
akmhoque8fdd6412012-12-04 15:05:33 -0600709 ccn_charbuf_destroy(&name);
akmhoque2ee18032013-01-22 10:01:23 -0600710 ccn_destroy(&temp_ccn);
akmhoquefc5176d2013-01-18 09:50:12 -0600711
712 return 0;
akmhoque8fdd6412012-12-04 15:05:33 -0600713}
714
715
716int
717create_sync_slice(char *topo_prefix, char *slice_prefix)
718{
719 int res;
720 struct ccns_slice *slice;
721 struct ccn_charbuf *prefix = ccn_charbuf_create();
722 struct ccn_charbuf *topo = ccn_charbuf_create();
723 struct ccn_charbuf *clause = ccn_charbuf_create();
724 struct ccn_charbuf *slice_name = ccn_charbuf_create();
725 struct ccn_charbuf *slice_uri = ccn_charbuf_create();
726
727 if (prefix == NULL || topo == NULL || clause == NULL ||
728 slice_name == NULL || slice_uri == NULL) {
729 fprintf(stderr, "Unable to allocate required memory.\n");
akmhoquefc5176d2013-01-18 09:50:12 -0600730 return -1;
akmhoque8fdd6412012-12-04 15:05:33 -0600731 }
732
733
734 slice = ccns_slice_create();
735
736 ccn_charbuf_reset(topo);
737 ccn_name_from_uri(topo, topo_prefix);
738 ccn_charbuf_reset(prefix);
739 ccn_name_from_uri(prefix,slice_prefix );
740 ccns_slice_set_topo_prefix(slice, topo, prefix);
741
742
743 res = ccns_write_slice(nlsr->ccn, slice, slice_name);
akmhoque3b6c66d2013-01-31 15:30:23 -0600744
745 //01/31/2013
akmhoque8fdd6412012-12-04 15:05:33 -0600746 ccns_slice_destroy(&slice);
747 ccn_charbuf_destroy(&prefix);
748 ccn_charbuf_destroy(&topo);
749 ccn_charbuf_destroy(&clause);
750 ccn_charbuf_destroy(&slice_name);
751 ccn_charbuf_destroy(&slice_uri);
akmhoque34425762013-01-23 09:33:29 -0600752
akmhoque8fdd6412012-12-04 15:05:33 -0600753 return 0;
754}
755