blob: 4578f98ad95c2c628edf5db25d67b09e1c84bd81 [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);
akmhoque1458b1d2013-01-31 17:48:32 -060096 ccn_name_chop(content_name,components1,-3);
97 process_content_from_sync(content_name,components1);
akmhoqueacef1e62013-01-31 17:15:08 -060098 printf("Res = %d \n",res);
akmhoqueacef1e62013-01-31 17:15:08 -060099 ccn_charbuf_destroy(&content_name);
Syed Obaid Amin3a3864b2013-01-09 03:05:50 -0600100
101 return(0);
102}
akmhoque3b6c66d2013-01-31 15:30:23 -0600103
akmhoque8fdd6412012-12-04 15:05:33 -0600104
105int
106get_lsa_position(struct ccn_charbuf * ccnb, struct ccn_indexbuf *comps)
107{
108
109
110
111 int res,i;
akmhoque3b6c66d2013-01-31 15:30:23 -0600112 int lsa_position=0;
akmhoque8fdd6412012-12-04 15:05:33 -0600113 int name_comps=(int)comps->n;
114
115 for(i=0;i<name_comps;i++)
116 {
117 res=ccn_name_comp_strcmp(ccnb->buf,comps,i,"LSA");
118 if( res == 0)
119 {
120 lsa_position=i;
121 break;
122 }
123 }
124
125 return lsa_position;
126
127}
128
129void
130get_name_part(struct name_prefix *name_part,struct ccn_charbuf * interest_ccnb, struct ccn_indexbuf *interest_comps, int offset)
131{
132
133
134
135 int res,i;
136 int lsa_position=0;
137 int len=0;
138
akmhoque2ee18032013-01-22 10:01:23 -0600139
140 struct ccn_indexbuf cid={0};
141 struct ccn_indexbuf *components=&cid;
142 struct ccn_charbuf *name=ccn_charbuf_create();
143 ccn_name_from_uri(name,nlsr->slice_prefix);
144 ccn_name_split (name, components);
145 lsa_position=components->n-2;
akmhoque3b6c66d2013-01-31 15:30:23 -0600146
akmhoque2ee18032013-01-22 10:01:23 -0600147 ccn_charbuf_destroy(&name);
148
akmhoque8fdd6412012-12-04 15:05:33 -0600149
150 const unsigned char *comp_ptr1;
151 size_t comp_size;
152 for(i=lsa_position+1+offset;i<interest_comps->n-1;i++)
153 {
154 res=ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1, &comp_size);
155 len+=1;
156 len+=(int)comp_size;
157 }
158 len++;
159
160 char *neighbor=(char *)malloc(len);
161 memset(neighbor,0,len);
162
163 for(i=lsa_position+1+offset; i<interest_comps->n-1;i++)
164 {
165 res=ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1, &comp_size);
166 memcpy(neighbor+strlen(neighbor),"/",1);
167 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,strlen((char *)comp_ptr1));
168
169 }
170
171 name_part->name=(char *)malloc(strlen(neighbor)+1);
172 memset(name_part->name,0,strlen(neighbor)+1);
173 memcpy(name_part->name,neighbor,strlen(neighbor)+1);
174 name_part->length=strlen(neighbor)+1;
175
akmhoque3b6c66d2013-01-31 15:30:23 -0600176 // Add 01/31/2013
177 free(neighbor);
akmhoque8fdd6412012-12-04 15:05:33 -0600178}
179
akmhoque09c0afa2012-12-14 09:27:00 -0600180void
181get_host_name_from_command_string(struct name_prefix *name_part,char *nbr_name_uri, int offset)
182{
183
184
185
186 int res,i;
187 int len=0;
188 const unsigned char *comp_ptr1;
189 size_t comp_size;
190
191 struct ccn_charbuf *name=ccn_charbuf_create();
192 name = ccn_charbuf_create();
193 res = ccn_name_from_uri(name,nbr_name_uri);
194 if (res < 0) {
195 fprintf(stderr, "Bad ccn URI: %s\n", nbr_name_uri);
196 exit(1);
197 }
198
199 struct ccn_indexbuf cid={0};
200
201 struct ccn_indexbuf *components=&cid;
202 ccn_name_split (name, components);
203
204 for(i=components->n-2;i> (0+offset);i--)
205 {
206 res=ccn_name_comp_get(name->buf, components,i,&comp_ptr1, &comp_size);
207 len+=1;
208 len+=(int)comp_size;
209 }
210 len++;
211
212 char *neighbor=(char *)malloc(len);
213 memset(neighbor,0,len);
214
215 for(i=components->n-2;i> (0+offset);i--)
216 {
217 res=ccn_name_comp_get(name->buf, components,i,&comp_ptr1, &comp_size);
218 if ( i != components->n-2)
219 memcpy(neighbor+strlen(neighbor),".",1);
220 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,strlen((char *)comp_ptr1));
221
222 }
223
224 name_part->name=(char *)malloc(strlen(neighbor)+1);
225 memset(name_part->name,0,strlen(neighbor)+1);
226 memcpy(name_part->name,neighbor,strlen(neighbor)+1);
227 name_part->length=strlen(neighbor)+1;
228
akmhoque3b6c66d2013-01-31 15:30:23 -0600229 // 01/31/2013
230 free(neighbor);
231 ccn_charbuf_destroy(&name);
akmhoque09c0afa2012-12-14 09:27:00 -0600232}
233
akmhoque8fdd6412012-12-04 15:05:33 -0600234
akmhoqueed418f32013-01-30 12:25:04 -0600235
soamin15043622013-01-15 23:10:26 -0600236void
Obaid Amine63022a2013-01-15 23:36:40 -0600237get_content_by_content_name(char *content_name, unsigned char **content_data)
akmhoque8fdd6412012-12-04 15:05:33 -0600238{
239
soamin15043622013-01-15 23:10:26 -0600240 struct ccn_charbuf *name = NULL;
241 struct ccn_charbuf *templ = NULL;
242 struct ccn_charbuf *resultbuf = NULL;
243 struct ccn_parsed_ContentObject pcobuf = { 0 };
244 int res;
245 int allow_stale = 0;
246 int content_only = 1;
247 int scope = -1;
248 const unsigned char *ptr;
249 size_t length;
250 int resolve_version = CCN_V_HIGHEST;
251 int timeout_ms = 3000;
252 const unsigned lifetime_default = CCN_INTEREST_LIFETIME_SEC << 12;
253 unsigned lifetime_l12 = lifetime_default;
254 int get_flags = 0;
akmhoque8fdd6412012-12-04 15:05:33 -0600255
soamin15043622013-01-15 23:10:26 -0600256 name = ccn_charbuf_create();
257 res = ccn_name_from_uri(name,content_name);
258 if (res < 0) {
259 fprintf(stderr, "Bad ccn URI: %s\n", content_name);
260 exit(1);
261 }
262
akmhoque8fdd6412012-12-04 15:05:33 -0600263 if (allow_stale || lifetime_l12 != lifetime_default || scope != -1) {
soamin15043622013-01-15 23:10:26 -0600264 templ = ccn_charbuf_create();
265 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
266 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
267 ccn_charbuf_append_closer(templ); /* </Name> */
akmhoque8fdd6412012-12-04 15:05:33 -0600268 if (allow_stale) {
269 ccn_charbuf_append_tt(templ, CCN_DTAG_AnswerOriginKind, CCN_DTAG);
270 ccnb_append_number(templ,
soamin15043622013-01-15 23:10:26 -0600271 CCN_AOK_DEFAULT | CCN_AOK_STALE);
akmhoque8fdd6412012-12-04 15:05:33 -0600272 ccn_charbuf_append_closer(templ); /* </AnswerOriginKind> */
273 }
soamin15043622013-01-15 23:10:26 -0600274 if (scope != -1) {
275 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
276 }
akmhoque8fdd6412012-12-04 15:05:33 -0600277 if (lifetime_l12 != lifetime_default) {
278 /*
279 * Choose the interest lifetime so there are at least 3
280 * expressions (in the unsatisfied case).
281 */
282 unsigned char buf[3] = { 0 };
283 int i;
284 for (i = sizeof(buf) - 1; i >= 0; i--, lifetime_l12 >>= 8)
285 buf[i] = lifetime_l12 & 0xff;
286 ccnb_append_tagged_blob(templ, CCN_DTAG_InterestLifetime, buf, sizeof(buf));
287 }
soamin15043622013-01-15 23:10:26 -0600288 ccn_charbuf_append_closer(templ); /* </Interest> */
289 }
290 resultbuf = ccn_charbuf_create();
291 if (resolve_version != 0) {
292 res = ccn_resolve_version(nlsr->ccn, name, resolve_version, 500);
293 if (res >= 0) {
294 ccn_uri_append(resultbuf, name->buf, name->length, 1);
soamin15043622013-01-15 23:10:26 -0600295 resultbuf->length = 0;
296 }
297 }
298 res = ccn_get(nlsr->ccn, name, templ, timeout_ms, resultbuf, &pcobuf, NULL, get_flags);
299 if (res >= 0) {
300 ptr = resultbuf->buf;
301 length = resultbuf->length;
302 if (content_only){
303 ccn_content_get_value(ptr, length, &pcobuf, &ptr, &length);
Obaid Amine63022a2013-01-15 23:36:40 -0600304 *content_data = (unsigned char *) calloc(length, sizeof(char *));
305 memcpy (*content_data, ptr, length);
soamin15043622013-01-15 23:10:26 -0600306 }
307 }
308 ccn_charbuf_destroy(&resultbuf);
309 ccn_charbuf_destroy(&templ);
310 ccn_charbuf_destroy(&name);
akmhoque8fdd6412012-12-04 15:05:33 -0600311}
312
akmhoque4ae16942012-12-10 11:50:43 -0600313void
soamin15043622013-01-15 23:10:26 -0600314process_incoming_sync_content_lsa( unsigned char *content_data)
akmhoque4ae16942012-12-10 11:50:43 -0600315{
316
317
318 if ( nlsr->debugging )
akmhoqueed418f32013-01-30 12:25:04 -0600319 printf("process_incoming_sync_content_lsa called \n");
akmhoque4ae16942012-12-10 11:50:43 -0600320
321 char *sep="|";
322 char *rem;
323 char *orig_router;
324 char *orl;
325 int orig_router_length;
326 char *lst;
327 int ls_type;
328 char *lsid;
329 long int ls_id;
330 char *isvld;
331 int isValid;
332 char *num_link;
333 int no_link;
334 char *np;
335 char *np_length;
336 int name_length;
337 char *data;
338 char *orig_time;
339
340
341 if ( nlsr->debugging )
342 printf("LSA Data \n");
akmhoque4ae16942012-12-10 11:50:43 -0600343
soamin15043622013-01-15 23:10:26 -0600344 if( strlen((char *)content_data ) > 0 )
akmhoque4ae16942012-12-10 11:50:43 -0600345 {
346
347 orig_router=strtok_r((char *)content_data,sep,&rem);
348 orl=strtok_r(NULL,sep,&rem);
349 orig_router_length=atoi(orl);
350
351 if ( nlsr->debugging )
352 {
353 printf(" Orig Router Name : %s\n",orig_router);
354 printf(" Orig Router Length: %d\n",orig_router_length);
355 }
356
357 lst=strtok_r(NULL,sep,&rem);
358 ls_type=atoi(lst);
359
360 if ( nlsr->debugging )
361 printf(" LS Type : %d\n",ls_type);
362
363 if ( ls_type == LS_TYPE_NAME )
364 {
365 lsid=strtok_r(NULL,sep,&rem);
366 ls_id=atoi(lsid);
367 orig_time=strtok_r(NULL,sep,&rem);
368 isvld=strtok_r(NULL,sep,&rem);
369 isValid=atoi(isvld);
370 np=strtok_r(NULL,sep,&rem);
371 np_length=strtok_r(NULL,sep,&rem);
372 name_length=atoi(np_length);
373 if ( nlsr->debugging )
374 {
375 printf(" LS ID : %ld\n",ls_id);
376 printf(" isValid : %d\n",isValid);
377 printf(" Name Prefix : %s\n",np);
378 printf(" Orig Time : %s\n",orig_time);
379 printf(" Name Prefix length: %d\n",name_length);
380 }
381
382 build_and_install_others_name_lsa(orig_router,ls_type,ls_id,orig_time,isValid,np);
383
384 print_name_lsdb();
385
386 }
387 else if ( ls_type == LS_TYPE_ADJ )
388 {
389 orig_time=strtok_r(NULL,sep,&rem);
390 num_link=strtok_r(NULL,sep,&rem);
391 no_link=atoi(num_link);
392 data=rem;
393
394 if ( nlsr->debugging )
395 {
akmhoqueed418f32013-01-30 12:25:04 -0600396 printf(" Orig Time : %s\n",orig_time);
akmhoque4ae16942012-12-10 11:50:43 -0600397 printf(" No Link : %d\n",no_link);
398 printf(" Data : %s\n",data);
399 }
400 build_and_install_others_adj_lsa(orig_router,ls_type,orig_time,no_link,data);
401 }
akmhoqueed418f32013-01-30 12:25:04 -0600402 else if ( ls_type == LS_TYPE_COR )
403 {
404 orig_time=strtok_r(NULL,sep,&rem);
405 char *cor_r=strtok_r(NULL,sep,&rem);
406 char *cor_theta=strtok_r(NULL,sep,&rem);
407
408 double r, theta;
409 r=strtof(cor_r,NULL);
410 theta=strtof(cor_theta,NULL);
411
412 if ( nlsr->debugging )
413 {
414 printf(" Orig Time : %s\n",orig_time);
415 printf(" Cor R : %f\n",r);
416 printf(" Cor Theta : %f\n",theta);
417 }
418 build_and_install_others_cor_lsa(orig_router,ls_type,orig_time, (double)r, (double)theta);
419 }
420
akmhoque4ae16942012-12-10 11:50:43 -0600421 }
422}
423
akmhoque8fdd6412012-12-04 15:05:33 -0600424void
425process_content_from_sync(struct ccn_charbuf *content_name, struct ccn_indexbuf *components)
426{
427 int lsa_position;
428 int res;
429 size_t comp_size;
430 const unsigned char *lst;
431 const unsigned char *lsid;
432 const unsigned char *origtime;
433
434 int ls_type;
435 long int ls_id=0;
436
soamin15043622013-01-15 23:10:26 -0600437 unsigned char *content_data = NULL;
438
akmhoque4ae16942012-12-10 11:50:43 -0600439 char *time_stamp=(char *)malloc(20);
440 memset(time_stamp,0,20);
441 get_current_timestamp_micro(time_stamp);
442
akmhoque8fdd6412012-12-04 15:05:33 -0600443 struct ccn_charbuf *uri = ccn_charbuf_create();
444 ccn_uri_append(uri, content_name->buf, content_name->length, 0);
445
446 struct name_prefix *orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix));
447
akmhoque2ee18032013-01-22 10:01:23 -0600448
449 struct ccn_indexbuf cid={0};
450 struct ccn_indexbuf *temp_components=&cid;
451 struct ccn_charbuf *name=ccn_charbuf_create();
452 ccn_name_from_uri(name,nlsr->slice_prefix);
453 ccn_name_split (name, temp_components);
454 lsa_position=temp_components->n-2;
akmhoque2ee18032013-01-22 10:01:23 -0600455 ccn_charbuf_destroy(&name);
456
akmhoque8fdd6412012-12-04 15:05:33 -0600457
458 res=ccn_name_comp_get(content_name->buf, components,lsa_position+1,&lst, &comp_size);
459
akmhoque8fdd6412012-12-04 15:05:33 -0600460 ls_type=atoi((char *)lst);
461 if(ls_type == LS_TYPE_NAME)
462 {
akmhoque4ae16942012-12-10 11:50:43 -0600463
akmhoque8fdd6412012-12-04 15:05:33 -0600464 res=ccn_name_comp_get(content_name->buf, components,lsa_position+2,&lsid, &comp_size);
465 ls_id=atoi((char *)lsid);
466 res=ccn_name_comp_get(content_name->buf, components,lsa_position+3,&origtime, &comp_size);
467 get_name_part(orig_router,content_name,components,3);
akmhoque8fdd6412012-12-04 15:05:33 -0600468
akmhoque4ae16942012-12-10 11:50:43 -0600469 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
470
akmhoque4ae16942012-12-10 11:50:43 -0600471 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 -0600472 {
akmhoque4ae16942012-12-10 11:50:43 -0600473 int is_new_name_lsa=check_is_new_name_lsa(orig_router->name,(char *)lst,(char *)lsid,(char *)origtime);
474 if ( is_new_name_lsa == 1 )
475 {
a64adam04be6482013-01-17 11:29:32 -0600476 if ( nlsr->debugging )
477 printf("New NAME LSA.....\n");
Obaid Amine63022a2013-01-15 23:36:40 -0600478 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
a64adamf7b051d2013-01-17 11:39:58 -0600479 if ( nlsr->debugging )
480 printf("Content Data: %s \n",content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600481 process_incoming_sync_content_lsa(content_data);
482 }
483 else
484 {
a64adam04be6482013-01-17 11:29:32 -0600485 if ( nlsr->debugging )
486 printf("Name LSA / Newer Name LSA already xists in LSDB\n");
Obaid Amine63022a2013-01-15 23:36:40 -0600487 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
a64adam04be6482013-01-17 11:29:32 -0600488
489 if ( nlsr->debugging )
490 printf("Content Data: %s \n",content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600491 }
akmhoque8fdd6412012-12-04 15:05:33 -0600492 }
493 else
494 {
a64adam04be6482013-01-17 11:29:32 -0600495 if ( nlsr->debugging )
496 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
akmhoque8fdd6412012-12-04 15:05:33 -0600497 }
498 }
499 else if(ls_type == LS_TYPE_ADJ)
500 {
501 res=ccn_name_comp_get(content_name->buf, components,lsa_position+2,&origtime, &comp_size);
502 get_name_part(orig_router,content_name,components,2);
a64adam04be6482013-01-17 11:29:32 -0600503
504 if ( nlsr->debugging )
505 printf("Orig Time: %s\nOrig Router: %s\n",origtime,orig_router->name);
akmhoque8fdd6412012-12-04 15:05:33 -0600506
akmhoque4ae16942012-12-10 11:50:43 -0600507 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque4ae16942012-12-10 11:50:43 -0600508 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) )
509 {
510 int is_new_adj_lsa=check_is_new_adj_lsa(orig_router->name,(char *)lst,(char *)origtime);
511 if ( is_new_adj_lsa == 1 )
512 {
a64adam04be6482013-01-17 11:29:32 -0600513 if ( nlsr->debugging )
514 printf("New Adj LSA.....\n");
Obaid Amine63022a2013-01-15 23:36:40 -0600515 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
a64adam04be6482013-01-17 11:29:32 -0600516
517 if ( nlsr->debugging )
518 printf("Content Data: %s \n",content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600519 process_incoming_sync_content_lsa(content_data);
520 }
521 else
522 {
a64adam04be6482013-01-17 11:29:32 -0600523 if ( nlsr->debugging )
524 printf("Adj LSA / Newer Adj LSA already exists in LSDB\n");
Obaid Amine63022a2013-01-15 23:36:40 -0600525 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
a64adam04be6482013-01-17 11:29:32 -0600526 if ( nlsr->debugging )
527 printf("Content Data: %s \n",content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600528 }
529 }
530 else
531 {
a64adam04be6482013-01-17 11:29:32 -0600532 if ( nlsr->debugging )
533 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
akmhoque4ae16942012-12-10 11:50:43 -0600534 }
akmhoque8fdd6412012-12-04 15:05:33 -0600535 }
akmhoqueed418f32013-01-30 12:25:04 -0600536 else if(ls_type == LS_TYPE_COR)
537 {
538 res=ccn_name_comp_get(content_name->buf, components,lsa_position+2,&origtime, &comp_size);
539 get_name_part(orig_router,content_name,components,2);
540
541 if ( nlsr->debugging )
542 printf("Orig Time: %s\nOrig Router: %s\n",origtime,orig_router->name);
543
544 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
545 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) )
546 {
547 int is_new_cor_lsa=check_is_new_cor_lsa(orig_router->name,(char *)lst,(char *)origtime);
548 if ( is_new_cor_lsa == 1 )
549 {
550 if ( nlsr->debugging )
551 printf("New Cor LSA.....\n");
552 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
553
554 if ( nlsr->debugging )
555 printf("Content Data: %s \n",content_data);
556 process_incoming_sync_content_lsa(content_data);
557 }
558 else
559 {
560 if ( nlsr->debugging )
561 printf("Cor LSA / Newer Cor LSA already exists in LSDB\n");
562 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
563 if ( nlsr->debugging )
564 printf("Content Data: %s \n",content_data);
565 }
566 }
567 else
568 {
569 if ( nlsr->debugging )
570 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
571 }
572
573 }
akmhoque8fdd6412012-12-04 15:05:33 -0600574
Obaid Amine63022a2013-01-15 23:36:40 -0600575 if (content_data != NULL)
576 free(content_data);
akmhoque8fdd6412012-12-04 15:05:33 -0600577 ccn_charbuf_destroy(&uri);
akmhoque3b6c66d2013-01-31 15:30:23 -0600578 //01/31/2013
579 free(time_stamp);
akmhoque8fdd6412012-12-04 15:05:33 -0600580}
581
akmhoque8fdd6412012-12-04 15:05:33 -0600582void
583sync_monitor(char *topo_prefix, char *slice_prefix)
584{
585
586 static struct ccns_name_closure nc={0};
587
588 nlsr->closure = &nc;
589 struct ccn_charbuf *prefix = ccn_charbuf_create();
590 struct ccn_charbuf *roothash = NULL;
591 struct ccn_charbuf *topo = ccn_charbuf_create();
592 nlsr->slice = ccns_slice_create();
593 ccn_charbuf_reset(prefix);
594 ccn_charbuf_reset(topo);
595
596 ccn_charbuf_reset(prefix);
597 ccn_name_from_uri(prefix, slice_prefix);
598
599 ccn_charbuf_reset(topo);
600 ccn_name_from_uri(topo, topo_prefix);
601
602
603 ccns_slice_set_topo_prefix(nlsr->slice, topo, prefix);
Syed Obaid Amin3a3864b2013-01-09 03:05:50 -0600604 nlsr->closure->callback = &sync_cb;
akmhoque8fdd6412012-12-04 15:05:33 -0600605 nlsr->ccns = ccns_open(nlsr->ccn, nlsr->slice, nlsr->closure, roothash, NULL);
akmhoque3b6c66d2013-01-31 15:30:23 -0600606
607 //01/31/2013
608 ccn_charbuf_destroy(&prefix);
609 ccn_charbuf_destroy(&topo);
akmhoque8fdd6412012-12-04 15:05:33 -0600610}
611
612struct ccn_charbuf *
613make_template(int scope)
614{
615 struct ccn_charbuf *templ = NULL;
616 templ = ccn_charbuf_create();
617 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
618 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
619 ccn_charbuf_append_closer(templ); /* </Name> */
620 if (0 <= scope && scope <= 2)
621 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
622 ccn_charbuf_append_closer(templ); /* </Interest> */
623 return(templ);
624}
625
akmhoquefc5176d2013-01-18 09:50:12 -0600626int
akmhoque8fdd6412012-12-04 15:05:33 -0600627write_data_to_repo(char *data, char *name_prefix)
628{
akmhoque260698b2013-01-17 03:54:09 -0600629 if ( nlsr->debugging )
630 {
631 printf("write_data_to_repo called\n");
akmhoque47d19d12013-01-17 04:00:32 -0600632 printf("Content Name: %s \n",name_prefix);
akmhoque260698b2013-01-17 03:54:09 -0600633 printf("Content Data: %s \n",data);
634 }
akmhoque8fdd6412012-12-04 15:05:33 -0600635
akmhoque2ee18032013-01-22 10:01:23 -0600636 struct ccn *temp_ccn;
637 temp_ccn=ccn_create();
638 int ccn_fd=ccn_connect(temp_ccn, NULL);
639 if(ccn_fd == -1)
640 {
641 fprintf(stderr,"Could not connect to ccnd for Data Writing\n");
642 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd for Data Writing\n");
643 return -1;
644 }
akmhoque8fdd6412012-12-04 15:05:33 -0600645 struct ccn_charbuf *name = NULL;
646 struct ccn_seqwriter *w = NULL;
akmhoque2ee18032013-01-22 10:01:23 -0600647 int blocksize = 4096;
akmhoque8fdd6412012-12-04 15:05:33 -0600648 int freshness = -1;
649 int torepo = 1;
650 int scope = 1;
651 int res;
652 size_t blockread;
653 struct ccn_charbuf *templ;
654
655 name = ccn_charbuf_create();
656 res = ccn_name_from_uri(name, name_prefix);
657 if (res < 0) {
658 fprintf(stderr, "bad CCN URI: %s\n",name_prefix);
akmhoquefc5176d2013-01-18 09:50:12 -0600659 return -1;
akmhoque8fdd6412012-12-04 15:05:33 -0600660 }
661
662
akmhoque2ee18032013-01-22 10:01:23 -0600663 w = ccn_seqw_create(temp_ccn, name);
akmhoque8fdd6412012-12-04 15:05:33 -0600664 if (w == NULL) {
665 fprintf(stderr, "ccn_seqw_create failed\n");
akmhoquefc5176d2013-01-18 09:50:12 -0600666 return -1;
akmhoque8fdd6412012-12-04 15:05:33 -0600667 }
668 ccn_seqw_set_block_limits(w, blocksize, blocksize);
669 if (freshness > -1)
670 ccn_seqw_set_freshness(w, freshness);
671 if (torepo) {
672 struct ccn_charbuf *name_v = ccn_charbuf_create();
673 ccn_seqw_get_name(w, name_v);
674 ccn_name_from_uri(name_v, "%C1.R.sw");
675 ccn_name_append_nonce(name_v);
676 templ = make_template(scope);
akmhoque2ee18032013-01-22 10:01:23 -0600677 res = ccn_get(temp_ccn, name_v, templ, 60000, NULL, NULL, NULL, 0);
akmhoque8fdd6412012-12-04 15:05:33 -0600678 ccn_charbuf_destroy(&templ);
679 ccn_charbuf_destroy(&name_v);
680 if (res < 0) {
681 fprintf(stderr, "No response from repository\n");
akmhoquefc5176d2013-01-18 09:50:12 -0600682 return -1;
akmhoque8fdd6412012-12-04 15:05:33 -0600683 }
684 }
685
686
687
688
689 blockread = 0;
akmhoque260698b2013-01-17 03:54:09 -0600690
akmhoque8fdd6412012-12-04 15:05:33 -0600691
692 blockread=strlen(data);
693
694 if (blockread > 0) {
akmhoque2ee18032013-01-22 10:01:23 -0600695 ccn_run(temp_ccn, 100);
akmhoque8fdd6412012-12-04 15:05:33 -0600696 res = ccn_seqw_write(w, data, blockread);
akmhoque260698b2013-01-17 03:54:09 -0600697 while (res == -1) {
akmhoque2ee18032013-01-22 10:01:23 -0600698 ccn_run(temp_ccn, 100);
akmhoque260698b2013-01-17 03:54:09 -0600699 res = ccn_seqw_write(w, data, blockread);
700 }
akmhoque8fdd6412012-12-04 15:05:33 -0600701 }
702
akmhoque8fdd6412012-12-04 15:05:33 -0600703 ccn_seqw_close(w);
akmhoque2ee18032013-01-22 10:01:23 -0600704 ccn_run(temp_ccn, 100);
akmhoque8fdd6412012-12-04 15:05:33 -0600705 ccn_charbuf_destroy(&name);
akmhoque2ee18032013-01-22 10:01:23 -0600706 ccn_destroy(&temp_ccn);
akmhoquefc5176d2013-01-18 09:50:12 -0600707
708 return 0;
akmhoque8fdd6412012-12-04 15:05:33 -0600709}
710
711
712int
713create_sync_slice(char *topo_prefix, char *slice_prefix)
714{
715 int res;
716 struct ccns_slice *slice;
717 struct ccn_charbuf *prefix = ccn_charbuf_create();
718 struct ccn_charbuf *topo = ccn_charbuf_create();
719 struct ccn_charbuf *clause = ccn_charbuf_create();
720 struct ccn_charbuf *slice_name = ccn_charbuf_create();
721 struct ccn_charbuf *slice_uri = ccn_charbuf_create();
722
723 if (prefix == NULL || topo == NULL || clause == NULL ||
724 slice_name == NULL || slice_uri == NULL) {
725 fprintf(stderr, "Unable to allocate required memory.\n");
akmhoquefc5176d2013-01-18 09:50:12 -0600726 return -1;
akmhoque8fdd6412012-12-04 15:05:33 -0600727 }
728
729
730 slice = ccns_slice_create();
731
732 ccn_charbuf_reset(topo);
733 ccn_name_from_uri(topo, topo_prefix);
734 ccn_charbuf_reset(prefix);
735 ccn_name_from_uri(prefix,slice_prefix );
736 ccns_slice_set_topo_prefix(slice, topo, prefix);
737
738
739 res = ccns_write_slice(nlsr->ccn, slice, slice_name);
akmhoque3b6c66d2013-01-31 15:30:23 -0600740
741 //01/31/2013
akmhoque8fdd6412012-12-04 15:05:33 -0600742 ccns_slice_destroy(&slice);
743 ccn_charbuf_destroy(&prefix);
744 ccn_charbuf_destroy(&topo);
745 ccn_charbuf_destroy(&clause);
746 ccn_charbuf_destroy(&slice_name);
747 ccn_charbuf_destroy(&slice_uri);
akmhoque34425762013-01-23 09:33:29 -0600748
akmhoque8fdd6412012-12-04 15:05:33 -0600749 return 0;
750}
751