blob: f528ba173a3bdde8ccb2c43c49d4cc2bb68cc6b4 [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 Aminbb8ac422013-02-20 17:08:48 -060034 char *
akmhoquea0e71152013-02-11 09:47:59 -060035hex_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++) {
Obaid Aminbb8ac422013-02-20 17:08:48 -060042 r[2*i] = hex_digits[(s[i]>>4) & 0xf];
43 r[1+2*i] = hex_digits[s[i] & 0xf];
akmhoquea0e71152013-02-11 09:47:59 -060044 }
45 return(r);
46}
47
Obaid Aminbb8ac422013-02-20 17:08:48 -060048 int
akmhoquea0e71152013-02-11 09:47:59 -060049sync_cb(struct ccns_name_closure *nc,
Obaid Aminbb8ac422013-02-20 17:08:48 -060050 struct ccn_charbuf *lhash,
51 struct ccn_charbuf *rhash,
52 struct ccn_charbuf *name)
akmhoquea0e71152013-02-11 09:47:59 -060053{
54 char *hexL;
55 char *hexR;
56 int res;
57 struct ccn_charbuf *uri = ccn_charbuf_create();
58 if (lhash == NULL || lhash->length == 0) {
Obaid Aminbb8ac422013-02-20 17:08:48 -060059 hexL = strdup("none");
akmhoquea0e71152013-02-11 09:47:59 -060060 } else
Obaid Aminbb8ac422013-02-20 17:08:48 -060061 hexL = hex_string(lhash->buf, lhash->length);
akmhoquea0e71152013-02-11 09:47:59 -060062 if (rhash == NULL || rhash->length == 0) {
Obaid Aminbb8ac422013-02-20 17:08:48 -060063 hexR = strdup("none");
akmhoquea0e71152013-02-11 09:47:59 -060064 } else
Obaid Aminbb8ac422013-02-20 17:08:48 -060065 hexR = hex_string(rhash->buf, rhash->length);
akmhoquea0e71152013-02-11 09:47:59 -060066 if (name != NULL)
Obaid Aminbb8ac422013-02-20 17:08:48 -060067 ccn_uri_append(uri, name->buf, name->length, 1);
akmhoquea0e71152013-02-11 09:47:59 -060068 else
Obaid Aminbb8ac422013-02-20 17:08:48 -060069 ccn_charbuf_append_string(uri, "(null)");
akmhoquea0e71152013-02-11 09:47:59 -060070
Obaid Aminbb8ac422013-02-20 17:08:48 -060071
akmhoquea0e71152013-02-11 09:47:59 -060072 if ( nlsr->debugging )
73 printf("Response from sync in the name: %s \n",ccn_charbuf_as_string(uri));
74
75 fflush(stdout);
76 free(hexL);
77 free(hexR);
78 ccn_charbuf_destroy(&uri);
79
80
Obaid Aminbb8ac422013-02-20 17:08:48 -060081 //--Doing our thing from here
82 //struct ccn_indexbuf cid={0};
akmhoquea0e71152013-02-11 09:47:59 -060083
Obaid Aminbb8ac422013-02-20 17:08:48 -060084 //struct ccn_indexbuf *components=&cid;
85 struct ccn_indexbuf *components=ccn_indexbuf_create();
86 res=ccn_name_split (name, components);
87 if ( res < 0 )
88 return 0;
89 //ccn_name_chop(name,components,-3);
90 //process_content_from_sync(name,components);
akmhoquea0e71152013-02-11 09:47:59 -060091
Obaid Aminbb8ac422013-02-20 17:08:48 -060092 struct ccn_charbuf *content_name = ccn_charbuf_create();
93 ccn_name_init(content_name);
94 if (components->n < 2)
95 return 0;
96 res = ccn_name_append_components(content_name, name->buf, components->buf[0]
97 , components->buf[components->n - 1]);
akmhoquea0e71152013-02-11 09:47:59 -060098
Obaid Aminbb8ac422013-02-20 17:08:48 -060099 if ( res < 0)
100 return 0;
akmhoque018692c2013-02-11 11:33:39 -0600101
102
Obaid Aminbb8ac422013-02-20 17:08:48 -0600103 // debugging purpose
104 struct ccn_charbuf *temp=ccn_charbuf_create();
105 ccn_uri_append(temp, content_name->buf, content_name->length, 0);
106 if ( nlsr->debugging )
107 printf("Name before chopping: %s \n",ccn_charbuf_as_string(temp));
108 ccn_charbuf_destroy(&temp);
akmhoquea0e71152013-02-11 09:47:59 -0600109
Obaid Aminbb8ac422013-02-20 17:08:48 -0600110 //struct ccn_indexbuf cid1={0};
111 //struct ccn_indexbuf *components1=&cid1;
112 struct ccn_indexbuf *components1=ccn_indexbuf_create();
113 res=ccn_name_split (content_name, components1);
114 if ( res < 0)
115 return 0;
akmhoque018692c2013-02-11 11:33:39 -0600116
Obaid Aminbb8ac422013-02-20 17:08:48 -0600117 if ( nlsr->debugging )
118 {
119 printf("Number of components in name = %d \n",res);
120 printf("Number of components in name as indexbuf->n = %d \n",
121 (int)components1->n);
122 }
123 ccn_name_chop(content_name,components1,-3);
124 if ( nlsr->debugging )
125 printf("Number of components in name as indexbuf->n after chopping= %d \n"
126 ,(int)components1->n);
akmhoquea0e71152013-02-11 09:47:59 -0600127
Obaid Aminbb8ac422013-02-20 17:08:48 -0600128 //debugging purpose
129 struct ccn_charbuf *temp1=ccn_charbuf_create();
130 ccn_uri_append(temp1, content_name->buf, content_name->length, 0);
131 if ( nlsr->debugging )
132 printf("Name after chopping: %s \n",ccn_charbuf_as_string(temp1));
133 ccn_charbuf_destroy(&temp1);
akmhoquea0e71152013-02-11 09:47:59 -0600134
Obaid Aminbb8ac422013-02-20 17:08:48 -0600135 process_content_from_sync(content_name,components1);
136 ccn_charbuf_destroy(&content_name);
137 ccn_indexbuf_destroy(&components);
138 ccn_indexbuf_destroy(&components1);
akmhoquea0e71152013-02-11 09:47:59 -0600139
Obaid Aminbb8ac422013-02-20 17:08:48 -0600140 return(0);
akmhoquea0e71152013-02-11 09:47:59 -0600141}
142
143
akmhoqueccb33e92013-02-20 11:44:28 -0600144
Obaid Aminbb8ac422013-02-20 17:08:48 -0600145 void
akmhoqueccb33e92013-02-20 11:44:28 -0600146get_name_part(struct name_prefix *name_part,struct ccn_charbuf * interest_ccnb,
Obaid Aminbb8ac422013-02-20 17:08:48 -0600147 struct ccn_indexbuf *interest_comps, int offset)
akmhoquea0e71152013-02-11 09:47:59 -0600148{
149
akmhoquea0e71152013-02-11 09:47:59 -0600150
akmhoquea0e71152013-02-11 09:47:59 -0600151
Obaid Aminbb8ac422013-02-20 17:08:48 -0600152 int i;
153 int lsa_position=0;
154 int len=0;
akmhoquea0e71152013-02-11 09:47:59 -0600155
akmhoquea0e71152013-02-11 09:47:59 -0600156
Obaid Aminbb8ac422013-02-20 17:08:48 -0600157 struct ccn_indexbuf cid={0};
158 struct ccn_indexbuf *components=&cid;
159 struct ccn_charbuf *name=ccn_charbuf_create();
160 ccn_name_from_uri(name,nlsr->slice_prefix);
161 ccn_name_split (name, components);
162 lsa_position=components->n-2;
akmhoquea0e71152013-02-11 09:47:59 -0600163
Obaid Aminbb8ac422013-02-20 17:08:48 -0600164 ccn_charbuf_destroy(&name);
akmhoquea0e71152013-02-11 09:47:59 -0600165
akmhoquea0e71152013-02-11 09:47:59 -0600166
Obaid Aminbb8ac422013-02-20 17:08:48 -0600167 const unsigned char *comp_ptr1;
168 size_t comp_size;
169 for(i=lsa_position+1+offset;i<interest_comps->n-1;i++)
170 {
171 ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1,
172 &comp_size);
173 len+=1;
174 len+=(int)comp_size;
175 }
176 len++;
akmhoquea0e71152013-02-11 09:47:59 -0600177
Obaid Aminbb8ac422013-02-20 17:08:48 -0600178 char *neighbor=(char *)malloc(len);
179 memset(neighbor,0,len);
180
181 for(i=lsa_position+1+offset; i<interest_comps->n-1;i++)
182 {
183 ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1,
184 &comp_size);
185 memcpy(neighbor+strlen(neighbor),"/",1);
186 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,
187 strlen((char *)comp_ptr1));
188
189 }
190
191 name_part->name=(char *)malloc(strlen(neighbor)+1);
192 memset(name_part->name,0,strlen(neighbor)+1);
193 memcpy(name_part->name,neighbor,strlen(neighbor)+1);
194 name_part->length=strlen(neighbor)+1;
195
196 // Add 01/31/2013
197 free(neighbor);
akmhoquea0e71152013-02-11 09:47:59 -0600198}
199
akmhoquea0e71152013-02-11 09:47:59 -0600200
akmhoquea0e71152013-02-11 09:47:59 -0600201
202
203
Obaid Aminbb8ac422013-02-20 17:08:48 -0600204 void
akmhoquea0e71152013-02-11 09:47:59 -0600205get_content_by_content_name(char *content_name, unsigned char **content_data)
206{
207
Obaid Aminbb8ac422013-02-20 17:08:48 -0600208 struct ccn_charbuf *name = NULL;
209 struct ccn_charbuf *templ = NULL;
210 struct ccn_charbuf *resultbuf = NULL;
211 struct ccn_parsed_ContentObject pcobuf = { 0 };
212 int res;
213 int allow_stale = 0;
214 int content_only = 1;
215 int scope = -1;
216 const unsigned char *ptr;
217 size_t length;
218 int resolve_version = CCN_V_HIGHEST;
219 int timeout_ms = 3000;
220 const unsigned lifetime_default = CCN_INTEREST_LIFETIME_SEC << 12;
221 unsigned lifetime_l12 = lifetime_default;
222 int get_flags = 0;
akmhoquea0e71152013-02-11 09:47:59 -0600223
Obaid Aminbb8ac422013-02-20 17:08:48 -0600224 name = ccn_charbuf_create();
225 res = ccn_name_from_uri(name,content_name);
226 if (res < 0) {
227 fprintf(stderr, "Bad ccn URI: %s\n", content_name);
228 exit(1);
229 }
akmhoquea0e71152013-02-11 09:47:59 -0600230
Obaid Aminbb8ac422013-02-20 17:08:48 -0600231 if (allow_stale || lifetime_l12 != lifetime_default || scope != -1) {
232 templ = ccn_charbuf_create();
233 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
234 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
235 ccn_charbuf_append_closer(templ); /* </Name> */
236 if (allow_stale) {
237 ccn_charbuf_append_tt(templ, CCN_DTAG_AnswerOriginKind, CCN_DTAG);
238 ccnb_append_number(templ,
239 CCN_AOK_DEFAULT | CCN_AOK_STALE);
240 ccn_charbuf_append_closer(templ); /* </AnswerOriginKind> */
akmhoquea0e71152013-02-11 09:47:59 -0600241 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600242 if (scope != -1) {
243 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
akmhoquea0e71152013-02-11 09:47:59 -0600244 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600245 if (lifetime_l12 != lifetime_default) {
246 /*
247 * Choose the interest lifetime so there are at least 3
248 * expressions (in the unsatisfied case).
249 */
250 unsigned char buf[3] = { 0 };
251 int i;
252 for (i = sizeof(buf) - 1; i >= 0; i--, lifetime_l12 >>= 8)
253 buf[i] = lifetime_l12 & 0xff;
254 ccnb_append_tagged_blob(templ, CCN_DTAG_InterestLifetime, buf,
255 sizeof(buf));
256 }
257 ccn_charbuf_append_closer(templ); /* </Interest> */
258 }
259 resultbuf = ccn_charbuf_create();
260 if (resolve_version != 0) {
261 res = ccn_resolve_version(nlsr->ccn, name, resolve_version, 500);
akmhoquea0e71152013-02-11 09:47:59 -0600262 if (res >= 0) {
Obaid Aminbb8ac422013-02-20 17:08:48 -0600263 ccn_uri_append(resultbuf, name->buf, name->length, 1);
264 resultbuf->length = 0;
akmhoquea0e71152013-02-11 09:47:59 -0600265 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600266 }
267 res = ccn_get(nlsr->ccn, name, templ, timeout_ms, resultbuf, &pcobuf, NULL,
268 get_flags);
269 if (res >= 0) {
270 ptr = resultbuf->buf;
271 length = resultbuf->length;
272 if (content_only){
273 ccn_content_get_value(ptr, length, &pcobuf, &ptr, &length);
274 *content_data = (unsigned char *) calloc(length, sizeof(char *));
275 memcpy (*content_data, ptr, length);
276 }
277 }
278 ccn_charbuf_destroy(&resultbuf);
279 ccn_charbuf_destroy(&templ);
280 ccn_charbuf_destroy(&name);
akmhoquea0e71152013-02-11 09:47:59 -0600281}
282
Obaid Aminbb8ac422013-02-20 17:08:48 -0600283 void
akmhoquea0e71152013-02-11 09:47:59 -0600284process_incoming_sync_content_lsa( unsigned char *content_data)
285{
286
287
Obaid Aminbb8ac422013-02-20 17:08:48 -0600288 if ( nlsr->debugging )
289 printf("process_incoming_sync_content_lsa called \n");
290
291 char *sep="|";
292 char *rem;
293 char *orig_router;
294 char *orl;
295 int orig_router_length;
296 char *lst;
297 int ls_type;
298 char *lsid;
299 long int ls_id;
300 char *isvld;
301 int isValid;
302 char *num_link;
303 int no_link;
304 char *np;
305 char *np_length;
306 int name_length;
307 char *data;
308 char *orig_time;
309
310
311 if ( nlsr->debugging )
312 printf("LSA Data \n");
313
314 if( strlen((char *)content_data ) > 0 )
315 {
316
317 orig_router=strtok_r((char *)content_data,sep,&rem);
318 orl=strtok_r(NULL,sep,&rem);
319 orig_router_length=atoi(orl);
320
akmhoquea0e71152013-02-11 09:47:59 -0600321 if ( nlsr->debugging )
akmhoquea0e71152013-02-11 09:47:59 -0600322 {
Obaid Aminbb8ac422013-02-20 17:08:48 -0600323 printf(" Orig Router Name : %s\n",orig_router);
324 printf(" Orig Router Length: %d\n",orig_router_length);
akmhoquea0e71152013-02-11 09:47:59 -0600325 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600326
327 lst=strtok_r(NULL,sep,&rem);
328 ls_type=atoi(lst);
329
330 if ( nlsr->debugging )
331 printf(" LS Type : %d\n",ls_type);
332
333 if ( ls_type == LS_TYPE_NAME )
334 {
335 lsid=strtok_r(NULL,sep,&rem);
336 ls_id=atoi(lsid);
337 orig_time=strtok_r(NULL,sep,&rem);
338 isvld=strtok_r(NULL,sep,&rem);
339 isValid=atoi(isvld);
340 np=strtok_r(NULL,sep,&rem);
341 np_length=strtok_r(NULL,sep,&rem);
342 name_length=atoi(np_length);
343 if ( nlsr->debugging )
344 {
345 printf(" LS ID : %ld\n",ls_id);
346 printf(" isValid : %d\n",isValid);
347 printf(" Name Prefix : %s\n",np);
348 printf(" Orig Time : %s\n",orig_time);
349 printf(" Name Prefix length: %d\n",name_length);
350 }
351
352 build_and_install_others_name_lsa(orig_router,ls_type,ls_id,orig_time,isValid,np);
353
354 print_name_lsdb();
355
356 }
357 else if ( ls_type == LS_TYPE_ADJ )
358 {
359 orig_time=strtok_r(NULL,sep,&rem);
360 num_link=strtok_r(NULL,sep,&rem);
361 no_link=atoi(num_link);
362 data=rem;
363
364 if ( nlsr->debugging )
365 {
366 printf(" Orig Time : %s\n",orig_time);
367 printf(" No Link : %d\n",no_link);
368 printf(" Data : %s\n",data);
369 }
370 build_and_install_others_adj_lsa(orig_router,ls_type,orig_time,no_link,data);
371 }
372 else if ( ls_type == LS_TYPE_COR )
373 {
374 orig_time=strtok_r(NULL,sep,&rem);
375 char *cor_r=strtok_r(NULL,sep,&rem);
376 char *cor_theta=strtok_r(NULL,sep,&rem);
377
378 double r, theta;
379 r=strtof(cor_r,NULL);
380 theta=strtof(cor_theta,NULL);
381
382 if ( nlsr->debugging )
383 {
384 printf(" Orig Time : %s\n",orig_time);
385 printf(" Cor R : %f\n",r);
386 printf(" Cor Theta : %f\n",theta);
387 }
388 build_and_install_others_cor_lsa(orig_router,ls_type,orig_time, (double)r, (double)theta);
389 }
390
391 }
akmhoquea0e71152013-02-11 09:47:59 -0600392}
393
Obaid Aminbb8ac422013-02-20 17:08:48 -0600394 void
akmhoquea0e71152013-02-11 09:47:59 -0600395process_content_from_sync(struct ccn_charbuf *content_name, struct ccn_indexbuf *components)
396{
Obaid Aminbb8ac422013-02-20 17:08:48 -0600397 size_t comp_size;
398 char *lst;
399 char *lsid;
400 const unsigned char *second_last_comp;
401 const unsigned char *third_last_comp;
402 const unsigned char *origtime;
403 char *sep=".";
404 char *rem;
405 char *second_comp_type;
akmhoquea0e71152013-02-11 09:47:59 -0600406
Obaid Aminbb8ac422013-02-20 17:08:48 -0600407 int ls_type;
408 long int ls_id=0;
akmhoquea0e71152013-02-11 09:47:59 -0600409
Obaid Aminbb8ac422013-02-20 17:08:48 -0600410 unsigned char *content_data = NULL;
akmhoquea0e71152013-02-11 09:47:59 -0600411
Obaid Aminbb8ac422013-02-20 17:08:48 -0600412 char *time_stamp=(char *)malloc(20);
413 memset(time_stamp,0,20);
414 get_current_timestamp_micro(time_stamp);
akmhoquea0e71152013-02-11 09:47:59 -0600415
Obaid Aminbb8ac422013-02-20 17:08:48 -0600416 struct ccn_charbuf *uri = ccn_charbuf_create();
417 ccn_uri_append(uri, content_name->buf, content_name->length, 0);
akmhoquea0e71152013-02-11 09:47:59 -0600418
Obaid Aminbb8ac422013-02-20 17:08:48 -0600419 struct name_prefix *orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix));
akmhoquea0e71152013-02-11 09:47:59 -0600420
akmhoquea0e71152013-02-11 09:47:59 -0600421
akmhoquea0e71152013-02-11 09:47:59 -0600422
Obaid Aminbb8ac422013-02-20 17:08:48 -0600423 ccn_name_comp_get(content_name->buf, components,components->n-1-2,&second_last_comp, &comp_size);
424 if (nlsr->debugging)
425 printf("2nd Last Component: %s \n",second_last_comp);
akmhoquea0e71152013-02-11 09:47:59 -0600426
Obaid Aminbb8ac422013-02-20 17:08:48 -0600427 second_comp_type=strtok_r((char *)second_last_comp,sep,&rem);
428 if ( strcmp( second_comp_type, "lsId" ) == 0 )
429 {
430 lsid=rem;
431 ls_id=atoi(rem);
432 ccn_name_comp_get(content_name->buf, components,components->n-2-2,&third_last_comp, &comp_size);
433 lst=strtok_r((char *)third_last_comp,sep,&rem);
434 lst=rem;
435 ls_type=atoi(lst);
436 ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size);
437 ccn_name_chop(content_name,components,-3);
438 get_name_part(orig_router,content_name,components,0);
439
440 if ( nlsr->debugging )
441 printf("Orig Router: %s Ls Type: %d Ls id: %ld Orig Time: %s\n",orig_router->name,ls_type,ls_id,origtime);
442
443 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
444
akmhoque0ab71642013-02-21 10:10:33 -0600445 if ( (strcmp(orig_router->name,nlsr->router_name) == 0 && lsa_life_time < nlsr->lsa_refresh_time)
446 || (strcmp(orig_router->name,nlsr->router_name) != 0 && lsa_life_time < nlsr->router_dead_interval) )
akmhoquea0e71152013-02-11 09:47:59 -0600447 {
Obaid Aminbb8ac422013-02-20 17:08:48 -0600448 int is_new_name_lsa=check_is_new_name_lsa(orig_router->name,(char *)lst,(char *)lsid,(char *)origtime);
449 if ( is_new_name_lsa == 1 )
450 {
451 if ( nlsr->debugging )
452 printf("New NAME LSA.....\n");
453 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
454 if ( nlsr->debugging )
455 printf("Content Data: %s \n",content_data);
456 process_incoming_sync_content_lsa(content_data);
457 }
458 else
459 {
460 if ( nlsr->debugging )
461 printf("Name LSA / Newer Name LSA already xists in LSDB\n");
462 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
akmhoquea0e71152013-02-11 09:47:59 -0600463
Obaid Aminbb8ac422013-02-20 17:08:48 -0600464 if ( nlsr->debugging )
465 printf("Content Data: %s \n",content_data);
466 }
akmhoquea0e71152013-02-11 09:47:59 -0600467 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600468 else
469 {
470 if ( nlsr->debugging )
471 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
472 }
473 }
474 else
475 {
476 ls_type=atoi(rem);
477 lst=rem;
478 if(ls_type == LS_TYPE_ADJ)
479 {
480 ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size);
481 ccn_name_chop(content_name,components,-2);
482 get_name_part(orig_router,content_name,components,0);
akmhoquea0e71152013-02-11 09:47:59 -0600483
Obaid Aminbb8ac422013-02-20 17:08:48 -0600484 if ( nlsr->debugging )
485 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",orig_router->name,ls_type,origtime);
486
487 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
488 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) )
489 {
490 int is_new_adj_lsa=check_is_new_adj_lsa(orig_router->name,(char *)lst,(char *)origtime);
491 if ( is_new_adj_lsa == 1 )
492 {
493 if ( nlsr->debugging )
494 printf("New Adj LSA.....\n");
495 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
496
497 if ( nlsr->debugging )
498 printf("Content Data: %s \n",content_data);
499 process_incoming_sync_content_lsa(content_data);
500 }
501 else
502 {
503 if ( nlsr->debugging )
504 printf("Adj LSA / Newer Adj LSA already exists in LSDB\n");
505 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
506 if ( nlsr->debugging )
507 printf("Content Data: %s \n",content_data);
508 }
509 }
510 else
511 {
512 if ( nlsr->debugging )
513 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
514 }
515 }
516 else if(ls_type == LS_TYPE_COR)
517 {
518 ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size);
519 ccn_name_chop(content_name,components,-2);
520 get_name_part(orig_router,content_name,components,0);
521
522 if ( nlsr->debugging )
523 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",orig_router->name,ls_type,origtime);
524
525 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
526 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) )
527 {
528 int is_new_cor_lsa=check_is_new_cor_lsa(orig_router->name,(char *)lst,(char *)origtime);
529 if ( is_new_cor_lsa == 1 )
530 {
531 if ( nlsr->debugging )
532 printf("New Cor LSA.....\n");
533 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
534
535 if ( nlsr->debugging )
536 printf("Content Data: %s \n",content_data);
537 process_incoming_sync_content_lsa(content_data);
538 }
539 else
540 {
541 if ( nlsr->debugging )
542 printf("Cor LSA / Newer Cor LSA already exists in LSDB\n");
543 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
544 if ( nlsr->debugging )
545 printf("Content Data: %s \n",content_data);
546 }
547 }
548 else
549 {
550 if ( nlsr->debugging )
551 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
552 }
553
554 }
555 }
556
Obaid Aminbb8ac422013-02-20 17:08:48 -0600557 free(orig_router);
558 ccn_charbuf_destroy(&uri);
559 //01/31/2013
560 free(time_stamp);
akmhoquea0e71152013-02-11 09:47:59 -0600561}
562
Obaid Aminbb8ac422013-02-20 17:08:48 -0600563 int
akmhoquea0e71152013-02-11 09:47:59 -0600564sync_monitor(char *topo_prefix, char *slice_prefix)
565{
566
akmhoque0678c5d2013-02-18 11:03:31 -0600567 static struct ccns_name_closure nc={0};
akmhoquea0e71152013-02-11 09:47:59 -0600568 nlsr->closure = &nc;
569 struct ccn_charbuf *prefix = ccn_charbuf_create();
570 struct ccn_charbuf *roothash = NULL;
571 struct ccn_charbuf *topo = ccn_charbuf_create();
572 nlsr->slice = ccns_slice_create();
573 ccn_charbuf_reset(prefix);
574 ccn_charbuf_reset(topo);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600575
akmhoquea0e71152013-02-11 09:47:59 -0600576 ccn_charbuf_reset(prefix);
577 ccn_name_from_uri(prefix, slice_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600578
akmhoquea0e71152013-02-11 09:47:59 -0600579 ccn_charbuf_reset(topo);
580 ccn_name_from_uri(topo, topo_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600581
akmhoquea0e71152013-02-11 09:47:59 -0600582 ccns_slice_set_topo_prefix(nlsr->slice, topo, prefix);
583 nlsr->closure->callback = &sync_cb;
584 nlsr->ccns = ccns_open(nlsr->ccn, nlsr->slice, nlsr->closure, roothash, NULL);
585
586 //01/31/2013
587 ccn_charbuf_destroy(&prefix);
588 ccn_charbuf_destroy(&topo);
akmhoque866c2222013-02-12 10:49:33 -0600589 ccn_charbuf_destroy(&roothash);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600590 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600591}
592
Obaid Aminbb8ac422013-02-20 17:08:48 -0600593 struct ccn_charbuf *
akmhoquea0e71152013-02-11 09:47:59 -0600594make_template(int scope)
595{
596 struct ccn_charbuf *templ = NULL;
597 templ = ccn_charbuf_create();
598 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
599 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
600 ccn_charbuf_append_closer(templ); /* </Name> */
601 if (0 <= scope && scope <= 2)
Obaid Aminbb8ac422013-02-20 17:08:48 -0600602 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
akmhoquea0e71152013-02-11 09:47:59 -0600603 ccn_charbuf_append_closer(templ); /* </Interest> */
604 return(templ);
605}
606
Obaid Aminbb8ac422013-02-20 17:08:48 -0600607 int
akmhoquea0e71152013-02-11 09:47:59 -0600608write_data_to_repo(char *data, char *name_prefix)
609{
Obaid Aminbb8ac422013-02-20 17:08:48 -0600610 if ( nlsr->debugging )
611 {
612 printf("write_data_to_repo called\n");
613 printf("Content Name: %s \n",name_prefix);
614 printf("Content Data: %s \n",data);
615 }
akmhoquea0e71152013-02-11 09:47:59 -0600616
Obaid Aminbb8ac422013-02-20 17:08:48 -0600617 struct ccn *temp_ccn;
618 temp_ccn=ccn_create();
619 int ccn_fd=ccn_connect(temp_ccn, NULL);
620 if(ccn_fd == -1)
621 {
622 fprintf(stderr,"Could not connect to ccnd for Data Writing\n");
623 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd for Data Writing\n");
624 return -1;
625 }
akmhoquea0e71152013-02-11 09:47:59 -0600626 struct ccn_charbuf *name = NULL;
627 struct ccn_seqwriter *w = NULL;
628 int blocksize = 4096;
629 int freshness = -1;
630 int torepo = 1;
631 int scope = 1;
632 int res;
633 size_t blockread;
634 struct ccn_charbuf *templ;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600635
akmhoquea0e71152013-02-11 09:47:59 -0600636 name = ccn_charbuf_create();
637 res = ccn_name_from_uri(name, name_prefix);
638 if (res < 0) {
Obaid Aminbb8ac422013-02-20 17:08:48 -0600639 fprintf(stderr, "bad CCN URI: %s\n",name_prefix);
akmhoquea0e71152013-02-11 09:47:59 -0600640 return -1;
641 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600642
643
akmhoquea0e71152013-02-11 09:47:59 -0600644 w = ccn_seqw_create(temp_ccn, name);
645 if (w == NULL) {
Obaid Aminbb8ac422013-02-20 17:08:48 -0600646 fprintf(stderr, "ccn_seqw_create failed\n");
akmhoquea0e71152013-02-11 09:47:59 -0600647 return -1;
648 }
649 ccn_seqw_set_block_limits(w, blocksize, blocksize);
650 if (freshness > -1)
Obaid Aminbb8ac422013-02-20 17:08:48 -0600651 ccn_seqw_set_freshness(w, freshness);
akmhoquea0e71152013-02-11 09:47:59 -0600652 if (torepo) {
Obaid Aminbb8ac422013-02-20 17:08:48 -0600653 struct ccn_charbuf *name_v = ccn_charbuf_create();
654 ccn_seqw_get_name(w, name_v);
655 ccn_name_from_uri(name_v, "%C1.R.sw");
656 ccn_name_append_nonce(name_v);
657 templ = make_template(scope);
658 res = ccn_get(temp_ccn, name_v, templ, 60000, NULL, NULL, NULL, 0);
659 ccn_charbuf_destroy(&templ);
660 ccn_charbuf_destroy(&name_v);
661 if (res < 0) {
662 fprintf(stderr, "No response from repository\n");
akmhoquea0e71152013-02-11 09:47:59 -0600663 return -1;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600664 }
akmhoquea0e71152013-02-11 09:47:59 -0600665 }
666
667
668
669
Obaid Aminbb8ac422013-02-20 17:08:48 -0600670 blockread = 0;
akmhoquea0e71152013-02-11 09:47:59 -0600671
672
Obaid Aminbb8ac422013-02-20 17:08:48 -0600673 blockread=strlen(data);
akmhoquea0e71152013-02-11 09:47:59 -0600674
Obaid Aminbb8ac422013-02-20 17:08:48 -0600675 if (blockread > 0) {
676 ccn_run(temp_ccn, 100);
677 res = ccn_seqw_write(w, data, blockread);
678 while (res == -1) {
679 ccn_run(temp_ccn, 100);
680 res = ccn_seqw_write(w, data, blockread);
681 }
682 }
akmhoquea0e71152013-02-11 09:47:59 -0600683
684 ccn_seqw_close(w);
685 ccn_run(temp_ccn, 100);
686 ccn_charbuf_destroy(&name);
687 ccn_destroy(&temp_ccn);
688
Obaid Aminbb8ac422013-02-20 17:08:48 -0600689 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600690}
691
692
Obaid Aminbb8ac422013-02-20 17:08:48 -0600693 int
akmhoquea0e71152013-02-11 09:47:59 -0600694create_sync_slice(char *topo_prefix, char *slice_prefix)
695{
696 int res;
akmhoque866c2222013-02-12 10:49:33 -0600697 struct ccn *handle;
akmhoquea0e71152013-02-11 09:47:59 -0600698 struct ccns_slice *slice;
699 struct ccn_charbuf *prefix = ccn_charbuf_create();
700 struct ccn_charbuf *topo = ccn_charbuf_create();
701 struct ccn_charbuf *clause = ccn_charbuf_create();
702 struct ccn_charbuf *slice_name = ccn_charbuf_create();
703 struct ccn_charbuf *slice_uri = ccn_charbuf_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600704
akmhoquea0e71152013-02-11 09:47:59 -0600705 if (prefix == NULL || topo == NULL || clause == NULL ||
Obaid Aminbb8ac422013-02-20 17:08:48 -0600706 slice_name == NULL || slice_uri == NULL) {
707 fprintf(stderr, "Unable to allocate required memory.\n");
akmhoquea0e71152013-02-11 09:47:59 -0600708 return -1;
709 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600710
711 handle = ccn_create();
712 res = ccn_connect(handle, NULL);
713 if (0 > res) {
714 fprintf(stderr, "Unable to connect to ccnd.\n");
715 return -1;
716 }
akmhoque866c2222013-02-12 10:49:33 -0600717
akmhoquea0e71152013-02-11 09:47:59 -0600718 slice = ccns_slice_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600719
akmhoquea0e71152013-02-11 09:47:59 -0600720 ccn_charbuf_reset(topo);
721 ccn_name_from_uri(topo, topo_prefix);
722 ccn_charbuf_reset(prefix);
723 ccn_name_from_uri(prefix,slice_prefix );
724 ccns_slice_set_topo_prefix(slice, topo, prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600725
726
akmhoque866c2222013-02-12 10:49:33 -0600727 res = ccns_write_slice(handle, slice, slice_name);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600728
akmhoquea0e71152013-02-11 09:47:59 -0600729 //01/31/2013
730 ccns_slice_destroy(&slice);
akmhoque866c2222013-02-12 10:49:33 -0600731 ccn_destroy(&handle);
akmhoquea0e71152013-02-11 09:47:59 -0600732 ccn_charbuf_destroy(&prefix);
733 ccn_charbuf_destroy(&topo);
734 ccn_charbuf_destroy(&clause);
735 ccn_charbuf_destroy(&slice_name);
736 ccn_charbuf_destroy(&slice_uri);
737
738 return 0;
739}
740