blob: 53112d07cf3f539b9d2e149324f85ab07d67d3d1 [file] [log] [blame]
akmhoquea0e71152013-02-11 09:47:59 -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"
31#include "utility.h"
32
33
Obaid Amin806df812013-02-21 13:30:26 -060034 char *
akmhoquea0e71152013-02-11 09:47:59 -060035hex_string(unsigned char *s, size_t l)
36{
Obaid Amin806df812013-02-21 13:30:26 -060037 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);
akmhoquea0e71152013-02-11 09:47:59 -060046}
47
Obaid Amin806df812013-02-21 13:30:26 -060048 int
akmhoquea0e71152013-02-11 09:47:59 -060049sync_cb(struct ccns_name_closure *nc,
Obaid Amin806df812013-02-21 13:30:26 -060050 struct ccn_charbuf *lhash,
51 struct ccn_charbuf *rhash,
52 struct ccn_charbuf *name)
akmhoquea0e71152013-02-11 09:47:59 -060053{
Obaid Amin806df812013-02-21 13:30:26 -060054 int res;
Obaid Amin806df812013-02-21 13:30:26 -060055 //--Doing our thing from here
56 //struct ccn_indexbuf cid={0};
57
58 //struct ccn_indexbuf *components=&cid;
59 struct ccn_indexbuf *components=ccn_indexbuf_create();
60 res=ccn_name_split (name, components);
61 if ( res < 0 )
62 return 0;
63 //ccn_name_chop(name,components,-3);
64 //process_content_from_sync(name,components);
65
66 struct ccn_charbuf *content_name = ccn_charbuf_create();
67 ccn_name_init(content_name);
68 if (components->n < 2)
69 return 0;
70 res = ccn_name_append_components(content_name, name->buf, components->buf[0]
71 , components->buf[components->n - 1]);
72
73 if ( res < 0)
74 return 0;
akmhoquea0e71152013-02-11 09:47:59 -060075
Obaid Aminbb8ac422013-02-20 17:08:48 -060076
Obaid Amin806df812013-02-21 13:30:26 -060077 // debugging purpose
78 struct ccn_charbuf *temp=ccn_charbuf_create();
79 ccn_uri_append(temp, content_name->buf, content_name->length, 0);
80 if ( nlsr->debugging )
81 printf("Name before chopping: %s \n",ccn_charbuf_as_string(temp));
82 ccn_charbuf_destroy(&temp);
akmhoquea0e71152013-02-11 09:47:59 -060083
Obaid Amin806df812013-02-21 13:30:26 -060084 //struct ccn_indexbuf cid1={0};
85 //struct ccn_indexbuf *components1=&cid1;
86 struct ccn_indexbuf *components1=ccn_indexbuf_create();
87 res=ccn_name_split (content_name, components1);
88 if ( res < 0)
89 return 0;
akmhoquea0e71152013-02-11 09:47:59 -060090
Obaid Amin806df812013-02-21 13:30:26 -060091 if ( nlsr->debugging )
92 {
93 printf("Number of components in name = %d \n",res);
94 printf("Number of components in name as indexbuf->n = %d \n",
95 (int)components1->n);
96 }
97 ccn_name_chop(content_name,components1,-3);
98 if ( nlsr->debugging )
99 printf("Number of components in name as indexbuf->n after chopping= %d \n"
100 ,(int)components1->n);
akmhoquea0e71152013-02-11 09:47:59 -0600101
Obaid Amin806df812013-02-21 13:30:26 -0600102 //debugging purpose
103 struct ccn_charbuf *temp1=ccn_charbuf_create();
104 ccn_uri_append(temp1, content_name->buf, content_name->length, 0);
105 if ( nlsr->debugging )
106 printf("Name after chopping: %s \n",ccn_charbuf_as_string(temp1));
107 ccn_charbuf_destroy(&temp1);
akmhoquea0e71152013-02-11 09:47:59 -0600108
Obaid Amin806df812013-02-21 13:30:26 -0600109 process_content_from_sync(content_name,components1);
110 ccn_charbuf_destroy(&content_name);
111 ccn_indexbuf_destroy(&components);
112 ccn_indexbuf_destroy(&components1);
akmhoquea0e71152013-02-11 09:47:59 -0600113
Obaid Amin806df812013-02-21 13:30:26 -0600114 return(0);
akmhoquea0e71152013-02-11 09:47:59 -0600115}
116
117
akmhoqueccb33e92013-02-20 11:44:28 -0600118
Obaid Amin7793ba72013-02-22 00:43:58 -0600119 void
akmhoqueccb33e92013-02-20 11:44:28 -0600120get_name_part(struct name_prefix *name_part,struct ccn_charbuf * interest_ccnb,
Obaid Amin806df812013-02-21 13:30:26 -0600121 struct ccn_indexbuf *interest_comps, int offset)
akmhoquea0e71152013-02-11 09:47:59 -0600122{
Obaid Amin806df812013-02-21 13:30:26 -0600123 int i;
124 int lsa_position=0;
125 int len=0;
akmhoquea0e71152013-02-11 09:47:59 -0600126
Obaid Amin806df812013-02-21 13:30:26 -0600127 struct ccn_indexbuf cid={0};
128 struct ccn_indexbuf *components=&cid;
129 struct ccn_charbuf *name=ccn_charbuf_create();
130 ccn_name_from_uri(name,nlsr->slice_prefix);
131 ccn_name_split (name, components);
132 lsa_position=components->n-2;
akmhoquea0e71152013-02-11 09:47:59 -0600133
Obaid Amin806df812013-02-21 13:30:26 -0600134 ccn_charbuf_destroy(&name);
akmhoquea0e71152013-02-11 09:47:59 -0600135
Obaid Amin806df812013-02-21 13:30:26 -0600136 const unsigned char *comp_ptr1;
137 size_t comp_size;
138 for(i=lsa_position+1+offset;i<interest_comps->n-1;i++)
139 {
140 ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1,
141 &comp_size);
142 len+=1;
143 len+=(int)comp_size;
144 }
145 len++;
akmhoquea0e71152013-02-11 09:47:59 -0600146
Obaid Amin806df812013-02-21 13:30:26 -0600147 char *neighbor=(char *)malloc(len);
148 memset(neighbor,0,len);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600149
Obaid Amin806df812013-02-21 13:30:26 -0600150 for(i=lsa_position+1+offset; i<interest_comps->n-1;i++)
151 {
152 ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1,
153 &comp_size);
154 memcpy(neighbor+strlen(neighbor),"/",1);
155 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,
156 strlen((char *)comp_ptr1));
Obaid Aminbb8ac422013-02-20 17:08:48 -0600157
Obaid Amin806df812013-02-21 13:30:26 -0600158 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600159
Obaid Amin806df812013-02-21 13:30:26 -0600160 name_part->name=(char *)malloc(strlen(neighbor)+1);
161 memset(name_part->name,0,strlen(neighbor)+1);
162 memcpy(name_part->name,neighbor,strlen(neighbor)+1);
163 name_part->length=strlen(neighbor)+1;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600164
Obaid Amin806df812013-02-21 13:30:26 -0600165 // Add 01/31/2013
166 free(neighbor);
akmhoquea0e71152013-02-11 09:47:59 -0600167}
168
akmhoquea0e71152013-02-11 09:47:59 -0600169
akmhoquea0e71152013-02-11 09:47:59 -0600170
171
172
Obaid Amin806df812013-02-21 13:30:26 -0600173 void
akmhoquea0e71152013-02-11 09:47:59 -0600174get_content_by_content_name(char *content_name, unsigned char **content_data)
175{
176
Obaid Amin806df812013-02-21 13:30:26 -0600177 struct ccn_charbuf *name = NULL;
178 struct ccn_charbuf *templ = NULL;
179 struct ccn_charbuf *resultbuf = NULL;
180 struct ccn_parsed_ContentObject pcobuf = { 0 };
181 int res;
182 int allow_stale = 0;
183 int content_only = 1;
184 int scope = -1;
185 const unsigned char *ptr;
186 size_t length;
187 int resolve_version = CCN_V_HIGHEST;
188 int timeout_ms = 3000;
189 const unsigned lifetime_default = CCN_INTEREST_LIFETIME_SEC << 12;
190 unsigned lifetime_l12 = lifetime_default;
191 int get_flags = 0;
akmhoquea0e71152013-02-11 09:47:59 -0600192
Obaid Amin806df812013-02-21 13:30:26 -0600193 name = ccn_charbuf_create();
194 res = ccn_name_from_uri(name,content_name);
195 if (res < 0) {
196 fprintf(stderr, "Bad ccn URI: %s\n", content_name);
197 exit(1);
198 }
akmhoquea0e71152013-02-11 09:47:59 -0600199
Obaid Amin806df812013-02-21 13:30:26 -0600200 if (allow_stale || lifetime_l12 != lifetime_default || scope != -1) {
201 templ = ccn_charbuf_create();
202 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
203 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
204 ccn_charbuf_append_closer(templ); /* </Name> */
205 if (allow_stale) {
206 ccn_charbuf_append_tt(templ, CCN_DTAG_AnswerOriginKind, CCN_DTAG);
207 ccnb_append_number(templ,
208 CCN_AOK_DEFAULT | CCN_AOK_STALE);
209 ccn_charbuf_append_closer(templ); /* </AnswerOriginKind> */
210 }
211 if (scope != -1) {
212 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
213 }
214 if (lifetime_l12 != lifetime_default) {
215 /*
216 * Choose the interest lifetime so there are at least 3
217 * expressions (in the unsatisfied case).
218 */
219 unsigned char buf[3] = { 0 };
220 int i;
221 for (i = sizeof(buf) - 1; i >= 0; i--, lifetime_l12 >>= 8)
222 buf[i] = lifetime_l12 & 0xff;
223 ccnb_append_tagged_blob(templ, CCN_DTAG_InterestLifetime, buf,
224 sizeof(buf));
225 }
226 ccn_charbuf_append_closer(templ); /* </Interest> */
akmhoquea0e71152013-02-11 09:47:59 -0600227 }
Obaid Amin806df812013-02-21 13:30:26 -0600228 resultbuf = ccn_charbuf_create();
229 if (resolve_version != 0) {
230 res = ccn_resolve_version(nlsr->ccn, name, resolve_version, 500);
231 if (res >= 0) {
232 ccn_uri_append(resultbuf, name->buf, name->length, 1);
233 resultbuf->length = 0;
234 }
akmhoquea0e71152013-02-11 09:47:59 -0600235 }
Obaid Amin806df812013-02-21 13:30:26 -0600236 res = ccn_get(nlsr->ccn, name, templ, timeout_ms, resultbuf, &pcobuf, NULL,
237 get_flags);
akmhoquea0e71152013-02-11 09:47:59 -0600238 if (res >= 0) {
Obaid Amin806df812013-02-21 13:30:26 -0600239 ptr = resultbuf->buf;
240 length = resultbuf->length;
241 if (content_only){
242 ccn_content_get_value(ptr, length, &pcobuf, &ptr, &length);
243 *content_data = (unsigned char *) calloc(length, sizeof(char *));
244 memcpy (*content_data, ptr, length);
245 }
akmhoquea0e71152013-02-11 09:47:59 -0600246 }
Obaid Amin806df812013-02-21 13:30:26 -0600247 ccn_charbuf_destroy(&resultbuf);
248 ccn_charbuf_destroy(&templ);
249 ccn_charbuf_destroy(&name);
akmhoquea0e71152013-02-11 09:47:59 -0600250}
251
Obaid Amin806df812013-02-21 13:30:26 -0600252 void
akmhoquea0e71152013-02-11 09:47:59 -0600253process_incoming_sync_content_lsa( unsigned char *content_data)
254{
255
256
Obaid Amin806df812013-02-21 13:30:26 -0600257 if ( nlsr->debugging )
258 printf("process_incoming_sync_content_lsa called \n");
Obaid Aminbb8ac422013-02-20 17:08:48 -0600259
Obaid Amin806df812013-02-21 13:30:26 -0600260 char *sep="|";
261 char *rem;
262 char *orig_router;
263 char *orl;
264 int orig_router_length;
265 char *lst;
266 int ls_type;
267 char *lsid;
268 long int ls_id;
269 char *isvld;
270 int isValid;
271 char *num_link;
272 int no_link;
273 char *np;
274 char *np_length;
275 int name_length;
276 char *data;
277 char *orig_time;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600278
279
akmhoquea0e71152013-02-11 09:47:59 -0600280 if ( nlsr->debugging )
Obaid Amin806df812013-02-21 13:30:26 -0600281 printf("LSA Data \n");
282
283 if( strlen((char *)content_data ) > 0 )
akmhoquea0e71152013-02-11 09:47:59 -0600284 {
Obaid Aminbb8ac422013-02-20 17:08:48 -0600285
Obaid Amin806df812013-02-21 13:30:26 -0600286 orig_router=strtok_r((char *)content_data,sep,&rem);
287 orl=strtok_r(NULL,sep,&rem);
288 orig_router_length=atoi(orl);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600289
Obaid Amin806df812013-02-21 13:30:26 -0600290 if ( nlsr->debugging )
291 {
292 printf(" Orig Router Name : %s\n",orig_router);
293 printf(" Orig Router Length: %d\n",orig_router_length);
294 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600295
Obaid Amin806df812013-02-21 13:30:26 -0600296 lst=strtok_r(NULL,sep,&rem);
297 ls_type=atoi(lst);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600298
Obaid Amin806df812013-02-21 13:30:26 -0600299 if ( nlsr->debugging )
300 printf(" LS Type : %d\n",ls_type);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600301
Obaid Amin806df812013-02-21 13:30:26 -0600302 if ( ls_type == LS_TYPE_NAME )
303 {
304 lsid=strtok_r(NULL,sep,&rem);
305 ls_id=atoi(lsid);
306 orig_time=strtok_r(NULL,sep,&rem);
307 isvld=strtok_r(NULL,sep,&rem);
308 isValid=atoi(isvld);
309 np=strtok_r(NULL,sep,&rem);
310 np_length=strtok_r(NULL,sep,&rem);
311 name_length=atoi(np_length);
312 if ( nlsr->debugging )
313 {
314 printf(" LS ID : %ld\n",ls_id);
315 printf(" isValid : %d\n",isValid);
316 printf(" Name Prefix : %s\n",np);
317 printf(" Orig Time : %s\n",orig_time);
318 printf(" Name Prefix length: %d\n",name_length);
319 }
320
321 build_and_install_others_name_lsa(orig_router,ls_type,ls_id,orig_time,isValid,np);
322
323 print_name_lsdb();
324
325 }
326 else if ( ls_type == LS_TYPE_ADJ )
327 {
328 orig_time=strtok_r(NULL,sep,&rem);
329 num_link=strtok_r(NULL,sep,&rem);
330 no_link=atoi(num_link);
331 data=rem;
332
333 if ( nlsr->debugging )
334 {
335 printf(" Orig Time : %s\n",orig_time);
336 printf(" No Link : %d\n",no_link);
337 printf(" Data : %s\n",data);
338 }
339 build_and_install_others_adj_lsa(orig_router,ls_type,orig_time,no_link,data);
340 }
341 else if ( ls_type == LS_TYPE_COR )
342 {
343 orig_time=strtok_r(NULL,sep,&rem);
344 char *cor_r=strtok_r(NULL,sep,&rem);
345 char *cor_theta=strtok_r(NULL,sep,&rem);
346
347 double r, theta;
348 r=strtof(cor_r,NULL);
349 theta=strtof(cor_theta,NULL);
350
351 if ( nlsr->debugging )
352 {
353 printf(" Orig Time : %s\n",orig_time);
354 printf(" Cor R : %f\n",r);
355 printf(" Cor Theta : %f\n",theta);
356 }
357 build_and_install_others_cor_lsa(orig_router,ls_type,orig_time, (double)r, (double)theta);
358 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600359
360 }
akmhoquea0e71152013-02-11 09:47:59 -0600361}
362
Obaid Amin806df812013-02-21 13:30:26 -0600363 void
akmhoquea0e71152013-02-11 09:47:59 -0600364process_content_from_sync(struct ccn_charbuf *content_name, struct ccn_indexbuf *components)
365{
Obaid Amin806df812013-02-21 13:30:26 -0600366 size_t comp_size;
367 char *lst;
368 char *lsid;
369 const unsigned char *second_last_comp;
370 const unsigned char *third_last_comp;
371 const unsigned char *origtime;
372 char *sep=".";
373 char *rem;
374 char *second_comp_type;
akmhoquea0e71152013-02-11 09:47:59 -0600375
Obaid Amin806df812013-02-21 13:30:26 -0600376 int ls_type;
377 long int ls_id=0;
akmhoquea0e71152013-02-11 09:47:59 -0600378
Obaid Amin806df812013-02-21 13:30:26 -0600379 unsigned char *content_data = NULL;
akmhoquea0e71152013-02-11 09:47:59 -0600380
Obaid Amin806df812013-02-21 13:30:26 -0600381 char *time_stamp=(char *)malloc(20);
382 memset(time_stamp,0,20);
383 get_current_timestamp_micro(time_stamp);
akmhoquea0e71152013-02-11 09:47:59 -0600384
Obaid Amin806df812013-02-21 13:30:26 -0600385 struct ccn_charbuf *uri = ccn_charbuf_create();
386 ccn_uri_append(uri, content_name->buf, content_name->length, 0);
akmhoquea0e71152013-02-11 09:47:59 -0600387
Obaid Amin806df812013-02-21 13:30:26 -0600388 struct name_prefix *orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix));
akmhoquea0e71152013-02-11 09:47:59 -0600389
akmhoquea0e71152013-02-11 09:47:59 -0600390
akmhoquea0e71152013-02-11 09:47:59 -0600391
Obaid Amin806df812013-02-21 13:30:26 -0600392 ccn_name_comp_get(content_name->buf, components,components->n-1-2,&second_last_comp, &comp_size);
393 if (nlsr->debugging)
394 printf("2nd Last Component: %s \n",second_last_comp);
akmhoquea0e71152013-02-11 09:47:59 -0600395
Obaid Amin806df812013-02-21 13:30:26 -0600396 second_comp_type=strtok_r((char *)second_last_comp,sep,&rem);
397 if ( strcmp( second_comp_type, "lsId" ) == 0 )
398 {
399 lsid=rem;
400 ls_id=atoi(rem);
401 ccn_name_comp_get(content_name->buf, components,components->n-2-2,&third_last_comp, &comp_size);
402 lst=strtok_r((char *)third_last_comp,sep,&rem);
403 lst=rem;
404 ls_type=atoi(lst);
405 ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size);
406 ccn_name_chop(content_name,components,-3);
407 get_name_part(orig_router,content_name,components,0);
akmhoquea0e71152013-02-11 09:47:59 -0600408
Obaid Aminbb8ac422013-02-20 17:08:48 -0600409 if ( nlsr->debugging )
Obaid Amin806df812013-02-21 13:30:26 -0600410 printf("Orig Router: %s Ls Type: %d Ls id: %ld Orig Time: %s\n",orig_router->name,ls_type,ls_id,origtime);
akmhoquea0e71152013-02-11 09:47:59 -0600411
Obaid Amin806df812013-02-21 13:30:26 -0600412 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600413
Obaid Amin806df812013-02-21 13:30:26 -0600414 if ( (strcmp(orig_router->name,nlsr->router_name) == 0 && lsa_life_time < nlsr->lsa_refresh_time)
415 || (strcmp(orig_router->name,nlsr->router_name) != 0 && lsa_life_time < nlsr->router_dead_interval) )
Obaid Aminbb8ac422013-02-20 17:08:48 -0600416 {
Obaid Amin806df812013-02-21 13:30:26 -0600417 int is_new_name_lsa=check_is_new_name_lsa(orig_router->name,(char *)lst,(char *)lsid,(char *)origtime);
418 if ( is_new_name_lsa == 1 )
419 {
420 if ( nlsr->debugging )
421 printf("New NAME LSA.....\n");
422 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
423 if ( nlsr->debugging )
424 printf("Content Data: %s \n",content_data);
425 process_incoming_sync_content_lsa(content_data);
426 }
427 else
428 {
429 if ( nlsr->debugging )
430 printf("Name LSA / Newer Name LSA already xists in LSDB\n");
431 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600432
Obaid Amin806df812013-02-21 13:30:26 -0600433 if ( nlsr->debugging )
434 printf("Content Data: %s \n",content_data);
435 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600436 }
Obaid Amin806df812013-02-21 13:30:26 -0600437 else
Obaid Aminbb8ac422013-02-20 17:08:48 -0600438 {
Obaid Amin806df812013-02-21 13:30:26 -0600439 if ( nlsr->debugging )
440 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
Obaid Aminbb8ac422013-02-20 17:08:48 -0600441 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600442 }
Obaid Amin806df812013-02-21 13:30:26 -0600443 else
Obaid Aminbb8ac422013-02-20 17:08:48 -0600444 {
Obaid Amin806df812013-02-21 13:30:26 -0600445 ls_type=atoi(rem);
446 lst=rem;
447 if(ls_type == LS_TYPE_ADJ)
Obaid Aminbb8ac422013-02-20 17:08:48 -0600448 {
Obaid Amin806df812013-02-21 13:30:26 -0600449 ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size);
450 ccn_name_chop(content_name,components,-2);
451 get_name_part(orig_router,content_name,components,0);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600452
Obaid Amin806df812013-02-21 13:30:26 -0600453 if ( nlsr->debugging )
454 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",orig_router->name,ls_type,origtime);
455
456 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
457 if ( (strcmp(orig_router->name,nlsr->router_name) == 0 && lsa_life_time < nlsr->lsa_refresh_time) || (strcmp(orig_router->name,nlsr->router_name) != 0 && lsa_life_time < nlsr->router_dead_interval) )
458 {
459 int is_new_adj_lsa=check_is_new_adj_lsa(orig_router->name,(char *)lst,(char *)origtime);
460 if ( is_new_adj_lsa == 1 )
461 {
462 if ( nlsr->debugging )
463 printf("New Adj LSA.....\n");
464 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
465
466 if ( nlsr->debugging )
467 printf("Content Data: %s \n",content_data);
468 process_incoming_sync_content_lsa(content_data);
469 }
470 else
471 {
472 if ( nlsr->debugging )
473 printf("Adj LSA / Newer Adj LSA already exists in LSDB\n");
474 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
475 if ( nlsr->debugging )
476 printf("Content Data: %s \n",content_data);
477 }
478 }
479 else
480 {
481 if ( nlsr->debugging )
482 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
483 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600484 }
Obaid Amin806df812013-02-21 13:30:26 -0600485 else if(ls_type == LS_TYPE_COR)
Obaid Aminbb8ac422013-02-20 17:08:48 -0600486 {
Obaid Amin806df812013-02-21 13:30:26 -0600487 ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size);
488 ccn_name_chop(content_name,components,-2);
489 get_name_part(orig_router,content_name,components,0);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600490
Obaid Amin806df812013-02-21 13:30:26 -0600491 if ( nlsr->debugging )
492 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",orig_router->name,ls_type,origtime);
493
494 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
495 if ( (strcmp(orig_router->name,nlsr->router_name) == 0 && lsa_life_time < nlsr->lsa_refresh_time) || (strcmp(orig_router->name,nlsr->router_name) != 0 && lsa_life_time < nlsr->router_dead_interval) )
496 {
497 int is_new_cor_lsa=check_is_new_cor_lsa(orig_router->name,(char *)lst,(char *)origtime);
498 if ( is_new_cor_lsa == 1 )
499 {
500 if ( nlsr->debugging )
501 printf("New Cor LSA.....\n");
502 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
503
504 if ( nlsr->debugging )
505 printf("Content Data: %s \n",content_data);
506 process_incoming_sync_content_lsa(content_data);
507 }
508 else
509 {
510 if ( nlsr->debugging )
511 printf("Cor LSA / Newer Cor LSA already exists in LSDB\n");
512 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
513 if ( nlsr->debugging )
514 printf("Content Data: %s \n",content_data);
515 }
516 }
517 else
518 {
519 if ( nlsr->debugging )
520 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
521 }
522
523 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600524 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600525
Obaid Amin05300892013-02-21 13:45:25 -0600526 if (orig_router)
527 free(orig_router);
Obaid Amin806df812013-02-21 13:30:26 -0600528 ccn_charbuf_destroy(&uri);
529 //01/31/2013
530 free(time_stamp);
akmhoquea0e71152013-02-11 09:47:59 -0600531}
532
Obaid Amin7793ba72013-02-22 00:43:58 -0600533 int
akmhoquea0e71152013-02-11 09:47:59 -0600534sync_monitor(char *topo_prefix, char *slice_prefix)
535{
536
akmhoque54d86112013-02-21 16:42:34 -0600537 //static struct ccns_name_closure nc={0};
538 //nlsr->closure = &nc;
539 nlsr->closure=(struct ccns_name_closure *)calloc(1,sizeof(struct ccns_name_closure));
Obaid Amin806df812013-02-21 13:30:26 -0600540 struct ccn_charbuf *prefix = ccn_charbuf_create();
akmhoque54d86112013-02-21 16:42:34 -0600541 //struct ccn_charbuf *roothash = NULL;
Obaid Amin806df812013-02-21 13:30:26 -0600542 struct ccn_charbuf *topo = ccn_charbuf_create();
543 nlsr->slice = ccns_slice_create();
akmhoque54d86112013-02-21 16:42:34 -0600544 //ccn_charbuf_reset(prefix);
545 //ccn_charbuf_reset(topo);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600546
Obaid Amin806df812013-02-21 13:30:26 -0600547 ccn_charbuf_reset(prefix);
548 ccn_name_from_uri(prefix, slice_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600549
Obaid Amin806df812013-02-21 13:30:26 -0600550 ccn_charbuf_reset(topo);
551 ccn_name_from_uri(topo, topo_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600552
Obaid Amin806df812013-02-21 13:30:26 -0600553 ccns_slice_set_topo_prefix(nlsr->slice, topo, prefix);
554 nlsr->closure->callback = &sync_cb;
akmhoque54d86112013-02-21 16:42:34 -0600555 nlsr->ccns = ccns_open(nlsr->ccn, nlsr->slice, nlsr->closure, NULL, NULL);
akmhoquea0e71152013-02-11 09:47:59 -0600556
Obaid Amin806df812013-02-21 13:30:26 -0600557 //01/31/2013
akmhoque54d86112013-02-21 16:42:34 -0600558 //ccn_charbuf_destroy(&prefix);
559 //ccn_charbuf_destroy(&topo);
560 //ccn_charbuf_destroy(&roothash);
Obaid Amin806df812013-02-21 13:30:26 -0600561 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600562}
563
Obaid Amin7793ba72013-02-22 00:43:58 -0600564 struct ccn_charbuf *
akmhoquea0e71152013-02-11 09:47:59 -0600565make_template(int scope)
566{
Obaid Amin806df812013-02-21 13:30:26 -0600567 struct ccn_charbuf *templ = NULL;
568 templ = ccn_charbuf_create();
569 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
570 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
571 ccn_charbuf_append_closer(templ); /* </Name> */
572 if (0 <= scope && scope <= 2)
573 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
574 ccn_charbuf_append_closer(templ); /* </Interest> */
575 return(templ);
akmhoquea0e71152013-02-11 09:47:59 -0600576}
577
Obaid Amin7793ba72013-02-22 00:43:58 -0600578 int
akmhoquea0e71152013-02-11 09:47:59 -0600579write_data_to_repo(char *data, char *name_prefix)
580{
Obaid Amin806df812013-02-21 13:30:26 -0600581 if ( nlsr->debugging )
582 {
583 printf("write_data_to_repo called\n");
584 printf("Content Name: %s \n",name_prefix);
585 printf("Content Data: %s \n",data);
586 }
akmhoquea0e71152013-02-11 09:47:59 -0600587
Obaid Amin50255052013-02-22 01:54:45 -0600588/* struct ccn *temp_ccn;
Obaid Amin806df812013-02-21 13:30:26 -0600589 temp_ccn=ccn_create();
590 int ccn_fd=ccn_connect(temp_ccn, NULL);
591 if(ccn_fd == -1)
592 {
593 fprintf(stderr,"Could not connect to ccnd for Data Writing\n");
594 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd for Data Writing\n");
595 return -1;
596 }
Obaid Amin50255052013-02-22 01:54:45 -0600597*/
Obaid Amin806df812013-02-21 13:30:26 -0600598 struct ccn_charbuf *name = NULL;
599 struct ccn_seqwriter *w = NULL;
600 int blocksize = 4096;
601 int freshness = -1;
602 int torepo = 1;
603 int scope = 1;
604 int res;
605 size_t blockread;
606 struct ccn_charbuf *templ;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600607
Obaid Amin806df812013-02-21 13:30:26 -0600608 name = ccn_charbuf_create();
609 res = ccn_name_from_uri(name, name_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600610 if (res < 0) {
Obaid Amin806df812013-02-21 13:30:26 -0600611 fprintf(stderr, "bad CCN URI: %s\n",name_prefix);
612 return -1;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600613 }
Obaid Amin806df812013-02-21 13:30:26 -0600614
615
Obaid Amin50255052013-02-22 01:54:45 -0600616 //w = ccn_seqw_create(temp_ccn, name);
617 w = ccn_seqw_create(nlsr->ccn, name);
Obaid Amin806df812013-02-21 13:30:26 -0600618 if (w == NULL) {
619 fprintf(stderr, "ccn_seqw_create failed\n");
620 return -1;
621 }
622 ccn_seqw_set_block_limits(w, blocksize, blocksize);
623 if (freshness > -1)
624 ccn_seqw_set_freshness(w, freshness);
625 if (torepo) {
626 struct ccn_charbuf *name_v = ccn_charbuf_create();
627 ccn_seqw_get_name(w, name_v);
628 ccn_name_from_uri(name_v, "%C1.R.sw");
629 ccn_name_append_nonce(name_v);
630 templ = make_template(scope);
Obaid Amin50255052013-02-22 01:54:45 -0600631 //res = ccn_get(temp_ccn, name_v, templ, 60000, NULL, NULL, NULL, 0);
632 res = ccn_get(nlsr->ccn, name_v, templ, 60000, NULL, NULL, NULL, 0);
Obaid Amin806df812013-02-21 13:30:26 -0600633 ccn_charbuf_destroy(&templ);
634 ccn_charbuf_destroy(&name_v);
635 if (res < 0) {
636 fprintf(stderr, "No response from repository\n");
637 return -1;
638 }
639 }
akmhoquea0e71152013-02-11 09:47:59 -0600640
641
Obaid Amin806df812013-02-21 13:30:26 -0600642 blockread = 0;
akmhoquea0e71152013-02-11 09:47:59 -0600643
Obaid Amin806df812013-02-21 13:30:26 -0600644 blockread=strlen(data);
akmhoquea0e71152013-02-11 09:47:59 -0600645
Obaid Amin806df812013-02-21 13:30:26 -0600646 if (blockread > 0) {
Obaid Amin50255052013-02-22 01:54:45 -0600647 //ccn_run(temp_ccn, 100);
648 ccn_run(nlsr->ccn, 1);
Obaid Amin806df812013-02-21 13:30:26 -0600649 res = ccn_seqw_write(w, data, blockread);
650 while (res == -1) {
Obaid Amin50255052013-02-22 01:54:45 -0600651 //ccn_run(temp_ccn, 100);
652 ccn_run(nlsr->ccn, 1);
Obaid Amin806df812013-02-21 13:30:26 -0600653 res = ccn_seqw_write(w, data, blockread);
654 }
655 }
656
657 ccn_seqw_close(w);
Obaid Amin50255052013-02-22 01:54:45 -0600658 //ccn_run(temp_ccn, 100);
659 ccn_run(nlsr->ccn, 1);
Obaid Amin806df812013-02-21 13:30:26 -0600660 ccn_charbuf_destroy(&name);
Obaid Amin50255052013-02-22 01:54:45 -0600661 //ccn_destroy(&temp_ccn);
akmhoquea0e71152013-02-11 09:47:59 -0600662
Obaid Amin806df812013-02-21 13:30:26 -0600663 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600664}
665
666
Obaid Amin806df812013-02-21 13:30:26 -0600667 int
akmhoquea0e71152013-02-11 09:47:59 -0600668create_sync_slice(char *topo_prefix, char *slice_prefix)
669{
Obaid Amin806df812013-02-21 13:30:26 -0600670 int res;
Obaid Amin7793ba72013-02-22 00:43:58 -0600671 //struct ccn *handle; //obaid: probably we don't need it use the same handle i.e. nlsr->ccn
Obaid Amin806df812013-02-21 13:30:26 -0600672 struct ccns_slice *slice;
673 struct ccn_charbuf *prefix = ccn_charbuf_create();
674 struct ccn_charbuf *topo = ccn_charbuf_create();
675 struct ccn_charbuf *clause = ccn_charbuf_create();
676 struct ccn_charbuf *slice_name = ccn_charbuf_create();
677 struct ccn_charbuf *slice_uri = ccn_charbuf_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600678
Obaid Amin806df812013-02-21 13:30:26 -0600679 if (prefix == NULL || topo == NULL || clause == NULL ||
680 slice_name == NULL || slice_uri == NULL) {
681 fprintf(stderr, "Unable to allocate required memory.\n");
682 return -1;
683 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600684
Obaid Amin7793ba72013-02-22 00:43:58 -0600685 /*handle = ccn_create();
Obaid Amin806df812013-02-21 13:30:26 -0600686 res = ccn_connect(handle, NULL);
687 if (0 > res) {
688 fprintf(stderr, "Unable to connect to ccnd.\n");
689 return -1;
690 }
Obaid Amin7793ba72013-02-22 00:43:58 -0600691*/
Obaid Amin806df812013-02-21 13:30:26 -0600692 slice = ccns_slice_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600693
Obaid Amin806df812013-02-21 13:30:26 -0600694 ccn_charbuf_reset(topo);
695 ccn_name_from_uri(topo, topo_prefix);
696 ccn_charbuf_reset(prefix);
697 ccn_name_from_uri(prefix,slice_prefix );
698 ccns_slice_set_topo_prefix(slice, topo, prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600699
700
Obaid Amin7793ba72013-02-22 00:43:58 -0600701 //res = ccns_write_slice(handle, slice, slice_name);
702 res = ccns_write_slice(nlsr->ccn, slice, slice_name);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600703
Obaid Amin806df812013-02-21 13:30:26 -0600704 //01/31/2013
705 ccns_slice_destroy(&slice);
Obaid Amin7793ba72013-02-22 00:43:58 -0600706 //ccn_destroy(&handle);
Obaid Amin806df812013-02-21 13:30:26 -0600707 ccn_charbuf_destroy(&prefix);
708 ccn_charbuf_destroy(&topo);
709 ccn_charbuf_destroy(&clause);
710 ccn_charbuf_destroy(&slice_name);
711 ccn_charbuf_destroy(&slice_uri);
akmhoquea0e71152013-02-11 09:47:59 -0600712
Obaid Amin806df812013-02-21 13:30:26 -0600713 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600714}
715