blob: 548890ec0c83c1aec4c70aa62fc90859b040c004 [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
akmhoqueb31caa12013-02-22 08:42:09 -0600381 char *time_stamp=get_current_timestamp_micro_v2();
akmhoquea0e71152013-02-11 09:47:59 -0600382
Obaid Amin806df812013-02-21 13:30:26 -0600383 struct ccn_charbuf *uri = ccn_charbuf_create();
384 ccn_uri_append(uri, content_name->buf, content_name->length, 0);
akmhoquea0e71152013-02-11 09:47:59 -0600385
Obaid Amin806df812013-02-21 13:30:26 -0600386 struct name_prefix *orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix));
akmhoquea0e71152013-02-11 09:47:59 -0600387
akmhoquea0e71152013-02-11 09:47:59 -0600388
akmhoquea0e71152013-02-11 09:47:59 -0600389
Obaid Amin806df812013-02-21 13:30:26 -0600390 ccn_name_comp_get(content_name->buf, components,components->n-1-2,&second_last_comp, &comp_size);
391 if (nlsr->debugging)
392 printf("2nd Last Component: %s \n",second_last_comp);
akmhoquea0e71152013-02-11 09:47:59 -0600393
Obaid Amin806df812013-02-21 13:30:26 -0600394 second_comp_type=strtok_r((char *)second_last_comp,sep,&rem);
395 if ( strcmp( second_comp_type, "lsId" ) == 0 )
396 {
397 lsid=rem;
398 ls_id=atoi(rem);
399 ccn_name_comp_get(content_name->buf, components,components->n-2-2,&third_last_comp, &comp_size);
400 lst=strtok_r((char *)third_last_comp,sep,&rem);
401 lst=rem;
402 ls_type=atoi(lst);
403 ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size);
404 ccn_name_chop(content_name,components,-3);
405 get_name_part(orig_router,content_name,components,0);
akmhoquea0e71152013-02-11 09:47:59 -0600406
Obaid Aminbb8ac422013-02-20 17:08:48 -0600407 if ( nlsr->debugging )
Obaid Amin806df812013-02-21 13:30:26 -0600408 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 -0600409
Obaid Amin806df812013-02-21 13:30:26 -0600410 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600411
Obaid Amin806df812013-02-21 13:30:26 -0600412 if ( (strcmp(orig_router->name,nlsr->router_name) == 0 && lsa_life_time < nlsr->lsa_refresh_time)
413 || (strcmp(orig_router->name,nlsr->router_name) != 0 && lsa_life_time < nlsr->router_dead_interval) )
Obaid Aminbb8ac422013-02-20 17:08:48 -0600414 {
Obaid Amin806df812013-02-21 13:30:26 -0600415 int is_new_name_lsa=check_is_new_name_lsa(orig_router->name,(char *)lst,(char *)lsid,(char *)origtime);
416 if ( is_new_name_lsa == 1 )
417 {
418 if ( nlsr->debugging )
419 printf("New NAME LSA.....\n");
420 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
421 if ( nlsr->debugging )
422 printf("Content Data: %s \n",content_data);
423 process_incoming_sync_content_lsa(content_data);
424 }
425 else
426 {
427 if ( nlsr->debugging )
428 printf("Name LSA / Newer Name LSA already xists in LSDB\n");
429 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600430
Obaid Amin806df812013-02-21 13:30:26 -0600431 if ( nlsr->debugging )
432 printf("Content Data: %s \n",content_data);
433 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600434 }
Obaid Amin806df812013-02-21 13:30:26 -0600435 else
Obaid Aminbb8ac422013-02-20 17:08:48 -0600436 {
Obaid Amin806df812013-02-21 13:30:26 -0600437 if ( nlsr->debugging )
438 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
Obaid Aminbb8ac422013-02-20 17:08:48 -0600439 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600440 }
Obaid Amin806df812013-02-21 13:30:26 -0600441 else
Obaid Aminbb8ac422013-02-20 17:08:48 -0600442 {
Obaid Amin806df812013-02-21 13:30:26 -0600443 ls_type=atoi(rem);
444 lst=rem;
445 if(ls_type == LS_TYPE_ADJ)
Obaid Aminbb8ac422013-02-20 17:08:48 -0600446 {
Obaid Amin806df812013-02-21 13:30:26 -0600447 ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size);
448 ccn_name_chop(content_name,components,-2);
449 get_name_part(orig_router,content_name,components,0);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600450
Obaid Amin806df812013-02-21 13:30:26 -0600451 if ( nlsr->debugging )
452 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",orig_router->name,ls_type,origtime);
453
454 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
455 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) )
456 {
457 int is_new_adj_lsa=check_is_new_adj_lsa(orig_router->name,(char *)lst,(char *)origtime);
458 if ( is_new_adj_lsa == 1 )
459 {
460 if ( nlsr->debugging )
461 printf("New Adj LSA.....\n");
462 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
463
464 if ( nlsr->debugging )
465 printf("Content Data: %s \n",content_data);
466 process_incoming_sync_content_lsa(content_data);
467 }
468 else
469 {
470 if ( nlsr->debugging )
471 printf("Adj LSA / Newer Adj LSA already exists in LSDB\n");
472 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
473 if ( nlsr->debugging )
474 printf("Content Data: %s \n",content_data);
475 }
476 }
477 else
478 {
479 if ( nlsr->debugging )
480 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
481 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600482 }
Obaid Amin806df812013-02-21 13:30:26 -0600483 else if(ls_type == LS_TYPE_COR)
Obaid Aminbb8ac422013-02-20 17:08:48 -0600484 {
Obaid Amin806df812013-02-21 13:30:26 -0600485 ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size);
486 ccn_name_chop(content_name,components,-2);
487 get_name_part(orig_router,content_name,components,0);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600488
Obaid Amin806df812013-02-21 13:30:26 -0600489 if ( nlsr->debugging )
490 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",orig_router->name,ls_type,origtime);
491
492 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
493 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) )
494 {
495 int is_new_cor_lsa=check_is_new_cor_lsa(orig_router->name,(char *)lst,(char *)origtime);
496 if ( is_new_cor_lsa == 1 )
497 {
498 if ( nlsr->debugging )
499 printf("New Cor LSA.....\n");
500 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
501
502 if ( nlsr->debugging )
503 printf("Content Data: %s \n",content_data);
504 process_incoming_sync_content_lsa(content_data);
505 }
506 else
507 {
508 if ( nlsr->debugging )
509 printf("Cor LSA / Newer Cor LSA already exists in LSDB\n");
510 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
511 if ( nlsr->debugging )
512 printf("Content Data: %s \n",content_data);
513 }
514 }
515 else
516 {
517 if ( nlsr->debugging )
518 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
519 }
520
521 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600522 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600523
Obaid Amin05300892013-02-21 13:45:25 -0600524 if (orig_router)
525 free(orig_router);
Obaid Amin806df812013-02-21 13:30:26 -0600526 ccn_charbuf_destroy(&uri);
527 //01/31/2013
528 free(time_stamp);
akmhoquea0e71152013-02-11 09:47:59 -0600529}
530
Obaid Amin7793ba72013-02-22 00:43:58 -0600531 int
akmhoquea0e71152013-02-11 09:47:59 -0600532sync_monitor(char *topo_prefix, char *slice_prefix)
533{
534
akmhoque54d86112013-02-21 16:42:34 -0600535 //static struct ccns_name_closure nc={0};
536 //nlsr->closure = &nc;
537 nlsr->closure=(struct ccns_name_closure *)calloc(1,sizeof(struct ccns_name_closure));
Obaid Amin806df812013-02-21 13:30:26 -0600538 struct ccn_charbuf *prefix = ccn_charbuf_create();
akmhoque54d86112013-02-21 16:42:34 -0600539 //struct ccn_charbuf *roothash = NULL;
Obaid Amin806df812013-02-21 13:30:26 -0600540 struct ccn_charbuf *topo = ccn_charbuf_create();
541 nlsr->slice = ccns_slice_create();
akmhoque54d86112013-02-21 16:42:34 -0600542 //ccn_charbuf_reset(prefix);
543 //ccn_charbuf_reset(topo);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600544
Obaid Amin806df812013-02-21 13:30:26 -0600545 ccn_charbuf_reset(prefix);
546 ccn_name_from_uri(prefix, slice_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600547
Obaid Amin806df812013-02-21 13:30:26 -0600548 ccn_charbuf_reset(topo);
549 ccn_name_from_uri(topo, topo_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600550
Obaid Amin806df812013-02-21 13:30:26 -0600551 ccns_slice_set_topo_prefix(nlsr->slice, topo, prefix);
552 nlsr->closure->callback = &sync_cb;
akmhoque54d86112013-02-21 16:42:34 -0600553 nlsr->ccns = ccns_open(nlsr->ccn, nlsr->slice, nlsr->closure, NULL, NULL);
akmhoquea0e71152013-02-11 09:47:59 -0600554
Obaid Amin806df812013-02-21 13:30:26 -0600555 //01/31/2013
akmhoque54d86112013-02-21 16:42:34 -0600556 //ccn_charbuf_destroy(&prefix);
557 //ccn_charbuf_destroy(&topo);
558 //ccn_charbuf_destroy(&roothash);
Obaid Amin806df812013-02-21 13:30:26 -0600559 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600560}
561
Obaid Amin7793ba72013-02-22 00:43:58 -0600562 struct ccn_charbuf *
akmhoquea0e71152013-02-11 09:47:59 -0600563make_template(int scope)
564{
Obaid Amin806df812013-02-21 13:30:26 -0600565 struct ccn_charbuf *templ = NULL;
566 templ = ccn_charbuf_create();
567 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
568 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
569 ccn_charbuf_append_closer(templ); /* </Name> */
570 if (0 <= scope && scope <= 2)
571 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
572 ccn_charbuf_append_closer(templ); /* </Interest> */
573 return(templ);
akmhoquea0e71152013-02-11 09:47:59 -0600574}
575
Obaid Amin7793ba72013-02-22 00:43:58 -0600576 int
akmhoquea0e71152013-02-11 09:47:59 -0600577write_data_to_repo(char *data, char *name_prefix)
578{
Obaid Amin806df812013-02-21 13:30:26 -0600579 if ( nlsr->debugging )
580 {
581 printf("write_data_to_repo called\n");
582 printf("Content Name: %s \n",name_prefix);
583 printf("Content Data: %s \n",data);
584 }
akmhoquea0e71152013-02-11 09:47:59 -0600585
Obaid Aminf210a322013-02-22 02:04:52 -0600586 struct ccn *temp_ccn;
Obaid Amin806df812013-02-21 13:30:26 -0600587 temp_ccn=ccn_create();
588 int ccn_fd=ccn_connect(temp_ccn, NULL);
589 if(ccn_fd == -1)
590 {
591 fprintf(stderr,"Could not connect to ccnd for Data Writing\n");
592 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd for Data Writing\n");
593 return -1;
594 }
Obaid Aminf210a322013-02-22 02:04:52 -0600595
Obaid Amin806df812013-02-21 13:30:26 -0600596 struct ccn_charbuf *name = NULL;
597 struct ccn_seqwriter *w = NULL;
598 int blocksize = 4096;
599 int freshness = -1;
600 int torepo = 1;
601 int scope = 1;
602 int res;
603 size_t blockread;
604 struct ccn_charbuf *templ;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600605
Obaid Amin806df812013-02-21 13:30:26 -0600606 name = ccn_charbuf_create();
607 res = ccn_name_from_uri(name, name_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600608 if (res < 0) {
Obaid Amin806df812013-02-21 13:30:26 -0600609 fprintf(stderr, "bad CCN URI: %s\n",name_prefix);
610 return -1;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600611 }
Obaid Amin806df812013-02-21 13:30:26 -0600612
613
Obaid Aminf210a322013-02-22 02:04:52 -0600614 w = ccn_seqw_create(temp_ccn, name);
Obaid Amin806df812013-02-21 13:30:26 -0600615 if (w == NULL) {
616 fprintf(stderr, "ccn_seqw_create failed\n");
617 return -1;
618 }
619 ccn_seqw_set_block_limits(w, blocksize, blocksize);
620 if (freshness > -1)
621 ccn_seqw_set_freshness(w, freshness);
622 if (torepo) {
623 struct ccn_charbuf *name_v = ccn_charbuf_create();
624 ccn_seqw_get_name(w, name_v);
625 ccn_name_from_uri(name_v, "%C1.R.sw");
626 ccn_name_append_nonce(name_v);
627 templ = make_template(scope);
Obaid Aminf210a322013-02-22 02:04:52 -0600628 res = ccn_get(temp_ccn, name_v, templ, 60000, NULL, NULL, NULL, 0);
Obaid Amin806df812013-02-21 13:30:26 -0600629 ccn_charbuf_destroy(&templ);
630 ccn_charbuf_destroy(&name_v);
631 if (res < 0) {
632 fprintf(stderr, "No response from repository\n");
633 return -1;
634 }
635 }
akmhoquea0e71152013-02-11 09:47:59 -0600636
637
Obaid Amin806df812013-02-21 13:30:26 -0600638 blockread = 0;
akmhoquea0e71152013-02-11 09:47:59 -0600639
Obaid Amin806df812013-02-21 13:30:26 -0600640 blockread=strlen(data);
akmhoquea0e71152013-02-11 09:47:59 -0600641
Obaid Amin806df812013-02-21 13:30:26 -0600642 if (blockread > 0) {
Obaid Amin806df812013-02-21 13:30:26 -0600643 res = ccn_seqw_write(w, data, blockread);
644 while (res == -1) {
akmhoque54309a22013-02-22 15:38:25 -0600645 ccn_run(temp_ccn,1);
Obaid Amin806df812013-02-21 13:30:26 -0600646 res = ccn_seqw_write(w, data, blockread);
647 }
648 }
649
akmhoque54309a22013-02-22 15:38:25 -0600650
651 ccn_run(temp_ccn, 1);
Obaid Amin806df812013-02-21 13:30:26 -0600652 ccn_charbuf_destroy(&name);
akmhoque0ed6d982013-02-22 14:28:01 -0600653 ccn_destroy(&temp_ccn);
akmhoquea0e71152013-02-11 09:47:59 -0600654
Obaid Amin806df812013-02-21 13:30:26 -0600655 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600656}
657
658
Obaid Amin806df812013-02-21 13:30:26 -0600659 int
akmhoquea0e71152013-02-11 09:47:59 -0600660create_sync_slice(char *topo_prefix, char *slice_prefix)
661{
Obaid Amin806df812013-02-21 13:30:26 -0600662 int res;
akmhoque0ed6d982013-02-22 14:28:01 -0600663 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 -0600664 struct ccns_slice *slice;
665 struct ccn_charbuf *prefix = ccn_charbuf_create();
666 struct ccn_charbuf *topo = ccn_charbuf_create();
667 struct ccn_charbuf *clause = ccn_charbuf_create();
668 struct ccn_charbuf *slice_name = ccn_charbuf_create();
669 struct ccn_charbuf *slice_uri = ccn_charbuf_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600670
Obaid Amin806df812013-02-21 13:30:26 -0600671 if (prefix == NULL || topo == NULL || clause == NULL ||
672 slice_name == NULL || slice_uri == NULL) {
673 fprintf(stderr, "Unable to allocate required memory.\n");
674 return -1;
675 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600676
akmhoque0ed6d982013-02-22 14:28:01 -0600677 handle = ccn_create();
Obaid Amin806df812013-02-21 13:30:26 -0600678 res = ccn_connect(handle, NULL);
679 if (0 > res) {
680 fprintf(stderr, "Unable to connect to ccnd.\n");
681 return -1;
682 }
akmhoque0ed6d982013-02-22 14:28:01 -0600683
Obaid Amin806df812013-02-21 13:30:26 -0600684 slice = ccns_slice_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600685
Obaid Amin806df812013-02-21 13:30:26 -0600686 ccn_charbuf_reset(topo);
687 ccn_name_from_uri(topo, topo_prefix);
688 ccn_charbuf_reset(prefix);
689 ccn_name_from_uri(prefix,slice_prefix );
690 ccns_slice_set_topo_prefix(slice, topo, prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600691
692
akmhoque0ed6d982013-02-22 14:28:01 -0600693 res = ccns_write_slice(handle, slice, slice_name);
694 //res = ccns_write_slice(nlsr->ccn, slice, slice_name);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600695
Obaid Amin806df812013-02-21 13:30:26 -0600696 //01/31/2013
697 ccns_slice_destroy(&slice);
akmhoque0ed6d982013-02-22 14:28:01 -0600698 ccn_destroy(&handle);
Obaid Amin806df812013-02-21 13:30:26 -0600699 ccn_charbuf_destroy(&prefix);
700 ccn_charbuf_destroy(&topo);
701 ccn_charbuf_destroy(&clause);
702 ccn_charbuf_destroy(&slice_name);
703 ccn_charbuf_destroy(&slice_uri);
akmhoquea0e71152013-02-11 09:47:59 -0600704
Obaid Amin806df812013-02-21 13:30:26 -0600705 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600706}
707