blob: ee72d790466d758d95d5f644af52dbc8a6cbf834 [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
459 res=ccn_name_comp_get(content_name->buf, components,lsa_position+1,&lst, &comp_size);
460
akmhoque8fdd6412012-12-04 15:05:33 -0600461 ls_type=atoi((char *)lst);
462 if(ls_type == LS_TYPE_NAME)
463 {
akmhoque4ae16942012-12-10 11:50:43 -0600464
akmhoque8fdd6412012-12-04 15:05:33 -0600465 res=ccn_name_comp_get(content_name->buf, components,lsa_position+2,&lsid, &comp_size);
466 ls_id=atoi((char *)lsid);
467 res=ccn_name_comp_get(content_name->buf, components,lsa_position+3,&origtime, &comp_size);
468 get_name_part(orig_router,content_name,components,3);
akmhoque8fdd6412012-12-04 15:05:33 -0600469
akmhoque4ae16942012-12-10 11:50:43 -0600470 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
471
akmhoque4ae16942012-12-10 11:50:43 -0600472 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 -0600473 {
akmhoque4ae16942012-12-10 11:50:43 -0600474 int is_new_name_lsa=check_is_new_name_lsa(orig_router->name,(char *)lst,(char *)lsid,(char *)origtime);
475 if ( is_new_name_lsa == 1 )
476 {
a64adam04be6482013-01-17 11:29:32 -0600477 if ( nlsr->debugging )
478 printf("New NAME LSA.....\n");
Obaid Amine63022a2013-01-15 23:36:40 -0600479 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
a64adamf7b051d2013-01-17 11:39:58 -0600480 if ( nlsr->debugging )
481 printf("Content Data: %s \n",content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600482 process_incoming_sync_content_lsa(content_data);
483 }
484 else
485 {
a64adam04be6482013-01-17 11:29:32 -0600486 if ( nlsr->debugging )
487 printf("Name LSA / Newer Name LSA already xists in LSDB\n");
Obaid Amine63022a2013-01-15 23:36:40 -0600488 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
a64adam04be6482013-01-17 11:29:32 -0600489
490 if ( nlsr->debugging )
491 printf("Content Data: %s \n",content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600492 }
akmhoque8fdd6412012-12-04 15:05:33 -0600493 }
494 else
495 {
a64adam04be6482013-01-17 11:29:32 -0600496 if ( nlsr->debugging )
497 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
akmhoque8fdd6412012-12-04 15:05:33 -0600498 }
499 }
500 else if(ls_type == LS_TYPE_ADJ)
501 {
502 res=ccn_name_comp_get(content_name->buf, components,lsa_position+2,&origtime, &comp_size);
503 get_name_part(orig_router,content_name,components,2);
a64adam04be6482013-01-17 11:29:32 -0600504
505 if ( nlsr->debugging )
506 printf("Orig Time: %s\nOrig Router: %s\n",origtime,orig_router->name);
akmhoque8fdd6412012-12-04 15:05:33 -0600507
akmhoque4ae16942012-12-10 11:50:43 -0600508 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque4ae16942012-12-10 11:50:43 -0600509 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) )
510 {
511 int is_new_adj_lsa=check_is_new_adj_lsa(orig_router->name,(char *)lst,(char *)origtime);
512 if ( is_new_adj_lsa == 1 )
513 {
a64adam04be6482013-01-17 11:29:32 -0600514 if ( nlsr->debugging )
515 printf("New Adj LSA.....\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
518 if ( nlsr->debugging )
519 printf("Content Data: %s \n",content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600520 process_incoming_sync_content_lsa(content_data);
521 }
522 else
523 {
a64adam04be6482013-01-17 11:29:32 -0600524 if ( nlsr->debugging )
525 printf("Adj LSA / Newer Adj LSA already exists in LSDB\n");
Obaid Amine63022a2013-01-15 23:36:40 -0600526 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
a64adam04be6482013-01-17 11:29:32 -0600527 if ( nlsr->debugging )
528 printf("Content Data: %s \n",content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600529 }
530 }
531 else
532 {
a64adam04be6482013-01-17 11:29:32 -0600533 if ( nlsr->debugging )
534 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
akmhoque4ae16942012-12-10 11:50:43 -0600535 }
akmhoque8fdd6412012-12-04 15:05:33 -0600536 }
akmhoqueed418f32013-01-30 12:25:04 -0600537 else if(ls_type == LS_TYPE_COR)
538 {
539 res=ccn_name_comp_get(content_name->buf, components,lsa_position+2,&origtime, &comp_size);
540 get_name_part(orig_router,content_name,components,2);
541
542 if ( nlsr->debugging )
543 printf("Orig Time: %s\nOrig Router: %s\n",origtime,orig_router->name);
544
545 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
546 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) )
547 {
548 int is_new_cor_lsa=check_is_new_cor_lsa(orig_router->name,(char *)lst,(char *)origtime);
549 if ( is_new_cor_lsa == 1 )
550 {
551 if ( nlsr->debugging )
552 printf("New Cor LSA.....\n");
553 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
554
555 if ( nlsr->debugging )
556 printf("Content Data: %s \n",content_data);
557 process_incoming_sync_content_lsa(content_data);
558 }
559 else
560 {
561 if ( nlsr->debugging )
562 printf("Cor LSA / Newer Cor LSA already exists in LSDB\n");
563 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
564 if ( nlsr->debugging )
565 printf("Content Data: %s \n",content_data);
566 }
567 }
568 else
569 {
570 if ( nlsr->debugging )
571 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
572 }
573
574 }
akmhoque8fdd6412012-12-04 15:05:33 -0600575
Obaid Amine63022a2013-01-15 23:36:40 -0600576 if (content_data != NULL)
577 free(content_data);
akmhoque8fdd6412012-12-04 15:05:33 -0600578 ccn_charbuf_destroy(&uri);
akmhoque3b6c66d2013-01-31 15:30:23 -0600579 //01/31/2013
580 free(time_stamp);
akmhoque8fdd6412012-12-04 15:05:33 -0600581}
582
akmhoque8fdd6412012-12-04 15:05:33 -0600583void
584sync_monitor(char *topo_prefix, char *slice_prefix)
585{
586
587 static struct ccns_name_closure nc={0};
588
589 nlsr->closure = &nc;
590 struct ccn_charbuf *prefix = ccn_charbuf_create();
591 struct ccn_charbuf *roothash = NULL;
592 struct ccn_charbuf *topo = ccn_charbuf_create();
593 nlsr->slice = ccns_slice_create();
594 ccn_charbuf_reset(prefix);
595 ccn_charbuf_reset(topo);
596
597 ccn_charbuf_reset(prefix);
598 ccn_name_from_uri(prefix, slice_prefix);
599
600 ccn_charbuf_reset(topo);
601 ccn_name_from_uri(topo, topo_prefix);
602
603
604 ccns_slice_set_topo_prefix(nlsr->slice, topo, prefix);
Syed Obaid Amin3a3864b2013-01-09 03:05:50 -0600605 nlsr->closure->callback = &sync_cb;
akmhoque8fdd6412012-12-04 15:05:33 -0600606 nlsr->ccns = ccns_open(nlsr->ccn, nlsr->slice, nlsr->closure, roothash, NULL);
akmhoque3b6c66d2013-01-31 15:30:23 -0600607
608 //01/31/2013
609 ccn_charbuf_destroy(&prefix);
610 ccn_charbuf_destroy(&topo);
akmhoque8fdd6412012-12-04 15:05:33 -0600611}
612
613struct ccn_charbuf *
614make_template(int scope)
615{
616 struct ccn_charbuf *templ = NULL;
617 templ = ccn_charbuf_create();
618 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
619 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
620 ccn_charbuf_append_closer(templ); /* </Name> */
621 if (0 <= scope && scope <= 2)
622 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
623 ccn_charbuf_append_closer(templ); /* </Interest> */
624 return(templ);
625}
626
akmhoquefc5176d2013-01-18 09:50:12 -0600627int
akmhoque8fdd6412012-12-04 15:05:33 -0600628write_data_to_repo(char *data, char *name_prefix)
629{
akmhoque260698b2013-01-17 03:54:09 -0600630 if ( nlsr->debugging )
631 {
632 printf("write_data_to_repo called\n");
akmhoque47d19d12013-01-17 04:00:32 -0600633 printf("Content Name: %s \n",name_prefix);
akmhoque260698b2013-01-17 03:54:09 -0600634 printf("Content Data: %s \n",data);
635 }
akmhoque8fdd6412012-12-04 15:05:33 -0600636
akmhoque2ee18032013-01-22 10:01:23 -0600637 struct ccn *temp_ccn;
638 temp_ccn=ccn_create();
639 int ccn_fd=ccn_connect(temp_ccn, NULL);
640 if(ccn_fd == -1)
641 {
642 fprintf(stderr,"Could not connect to ccnd for Data Writing\n");
643 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd for Data Writing\n");
644 return -1;
645 }
akmhoque8fdd6412012-12-04 15:05:33 -0600646 struct ccn_charbuf *name = NULL;
647 struct ccn_seqwriter *w = NULL;
akmhoque2ee18032013-01-22 10:01:23 -0600648 int blocksize = 4096;
akmhoque8fdd6412012-12-04 15:05:33 -0600649 int freshness = -1;
650 int torepo = 1;
651 int scope = 1;
652 int res;
653 size_t blockread;
654 struct ccn_charbuf *templ;
655
656 name = ccn_charbuf_create();
657 res = ccn_name_from_uri(name, name_prefix);
658 if (res < 0) {
659 fprintf(stderr, "bad CCN URI: %s\n",name_prefix);
akmhoquefc5176d2013-01-18 09:50:12 -0600660 return -1;
akmhoque8fdd6412012-12-04 15:05:33 -0600661 }
662
663
akmhoque2ee18032013-01-22 10:01:23 -0600664 w = ccn_seqw_create(temp_ccn, name);
akmhoque8fdd6412012-12-04 15:05:33 -0600665 if (w == NULL) {
666 fprintf(stderr, "ccn_seqw_create failed\n");
akmhoquefc5176d2013-01-18 09:50:12 -0600667 return -1;
akmhoque8fdd6412012-12-04 15:05:33 -0600668 }
669 ccn_seqw_set_block_limits(w, blocksize, blocksize);
670 if (freshness > -1)
671 ccn_seqw_set_freshness(w, freshness);
672 if (torepo) {
673 struct ccn_charbuf *name_v = ccn_charbuf_create();
674 ccn_seqw_get_name(w, name_v);
675 ccn_name_from_uri(name_v, "%C1.R.sw");
676 ccn_name_append_nonce(name_v);
677 templ = make_template(scope);
akmhoque2ee18032013-01-22 10:01:23 -0600678 res = ccn_get(temp_ccn, name_v, templ, 60000, NULL, NULL, NULL, 0);
akmhoque8fdd6412012-12-04 15:05:33 -0600679 ccn_charbuf_destroy(&templ);
680 ccn_charbuf_destroy(&name_v);
681 if (res < 0) {
682 fprintf(stderr, "No response from repository\n");
akmhoquefc5176d2013-01-18 09:50:12 -0600683 return -1;
akmhoque8fdd6412012-12-04 15:05:33 -0600684 }
685 }
686
687
688
689
690 blockread = 0;
akmhoque260698b2013-01-17 03:54:09 -0600691
akmhoque8fdd6412012-12-04 15:05:33 -0600692
693 blockread=strlen(data);
694
695 if (blockread > 0) {
akmhoque2ee18032013-01-22 10:01:23 -0600696 ccn_run(temp_ccn, 100);
akmhoque8fdd6412012-12-04 15:05:33 -0600697 res = ccn_seqw_write(w, data, blockread);
akmhoque260698b2013-01-17 03:54:09 -0600698 while (res == -1) {
akmhoque2ee18032013-01-22 10:01:23 -0600699 ccn_run(temp_ccn, 100);
akmhoque260698b2013-01-17 03:54:09 -0600700 res = ccn_seqw_write(w, data, blockread);
701 }
akmhoque8fdd6412012-12-04 15:05:33 -0600702 }
703
akmhoque8fdd6412012-12-04 15:05:33 -0600704 ccn_seqw_close(w);
akmhoque2ee18032013-01-22 10:01:23 -0600705 ccn_run(temp_ccn, 100);
akmhoque8fdd6412012-12-04 15:05:33 -0600706 ccn_charbuf_destroy(&name);
akmhoque2ee18032013-01-22 10:01:23 -0600707 ccn_destroy(&temp_ccn);
akmhoquefc5176d2013-01-18 09:50:12 -0600708
709 return 0;
akmhoque8fdd6412012-12-04 15:05:33 -0600710}
711
712
713int
714create_sync_slice(char *topo_prefix, char *slice_prefix)
715{
716 int res;
717 struct ccns_slice *slice;
718 struct ccn_charbuf *prefix = ccn_charbuf_create();
719 struct ccn_charbuf *topo = ccn_charbuf_create();
720 struct ccn_charbuf *clause = ccn_charbuf_create();
721 struct ccn_charbuf *slice_name = ccn_charbuf_create();
722 struct ccn_charbuf *slice_uri = ccn_charbuf_create();
723
724 if (prefix == NULL || topo == NULL || clause == NULL ||
725 slice_name == NULL || slice_uri == NULL) {
726 fprintf(stderr, "Unable to allocate required memory.\n");
akmhoquefc5176d2013-01-18 09:50:12 -0600727 return -1;
akmhoque8fdd6412012-12-04 15:05:33 -0600728 }
729
730
731 slice = ccns_slice_create();
732
733 ccn_charbuf_reset(topo);
734 ccn_name_from_uri(topo, topo_prefix);
735 ccn_charbuf_reset(prefix);
736 ccn_name_from_uri(prefix,slice_prefix );
737 ccns_slice_set_topo_prefix(slice, topo, prefix);
738
739
740 res = ccns_write_slice(nlsr->ccn, slice, slice_name);
akmhoque3b6c66d2013-01-31 15:30:23 -0600741
742 //01/31/2013
akmhoque8fdd6412012-12-04 15:05:33 -0600743 ccns_slice_destroy(&slice);
744 ccn_charbuf_destroy(&prefix);
745 ccn_charbuf_destroy(&topo);
746 ccn_charbuf_destroy(&clause);
747 ccn_charbuf_destroy(&slice_name);
748 ccn_charbuf_destroy(&slice_uri);
akmhoque34425762013-01-23 09:33:29 -0600749
akmhoque8fdd6412012-12-04 15:05:33 -0600750 return 0;
751}
752