blob: d523bedc0e2f6b06eaea8f447e070fe175b32621 [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;
55 /*char *hexL;
56 char *hexR;
57 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)");
70
71 if ( nlsr->debugging )
72 printf("Response from sync in the name: %s \n",ccn_charbuf_as_string(uri));
73
74 fflush(stdout);
75 free(hexL);
76 free(hexR);
77 ccn_charbuf_destroy(&uri);
78 */
79
80 //--Doing our thing from here
81 //struct ccn_indexbuf cid={0};
82
83 //struct ccn_indexbuf *components=&cid;
84 struct ccn_indexbuf *components=ccn_indexbuf_create();
85 res=ccn_name_split (name, components);
86 if ( res < 0 )
87 return 0;
88 //ccn_name_chop(name,components,-3);
89 //process_content_from_sync(name,components);
90
91 struct ccn_charbuf *content_name = ccn_charbuf_create();
92 ccn_name_init(content_name);
93 if (components->n < 2)
94 return 0;
95 res = ccn_name_append_components(content_name, name->buf, components->buf[0]
96 , components->buf[components->n - 1]);
97
98 if ( res < 0)
99 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600100
Obaid Aminbb8ac422013-02-20 17:08:48 -0600101
Obaid Amin806df812013-02-21 13:30:26 -0600102 // debugging purpose
103 struct ccn_charbuf *temp=ccn_charbuf_create();
104 ccn_uri_append(temp, content_name->buf, content_name->length, 0);
105 if ( nlsr->debugging )
106 printf("Name before chopping: %s \n",ccn_charbuf_as_string(temp));
107 ccn_charbuf_destroy(&temp);
akmhoquea0e71152013-02-11 09:47:59 -0600108
Obaid Amin806df812013-02-21 13:30:26 -0600109 //struct ccn_indexbuf cid1={0};
110 //struct ccn_indexbuf *components1=&cid1;
111 struct ccn_indexbuf *components1=ccn_indexbuf_create();
112 res=ccn_name_split (content_name, components1);
113 if ( res < 0)
114 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600115
Obaid Amin806df812013-02-21 13:30:26 -0600116 if ( nlsr->debugging )
117 {
118 printf("Number of components in name = %d \n",res);
119 printf("Number of components in name as indexbuf->n = %d \n",
120 (int)components1->n);
121 }
122 ccn_name_chop(content_name,components1,-3);
123 if ( nlsr->debugging )
124 printf("Number of components in name as indexbuf->n after chopping= %d \n"
125 ,(int)components1->n);
akmhoquea0e71152013-02-11 09:47:59 -0600126
Obaid Amin806df812013-02-21 13:30:26 -0600127 //debugging purpose
128 struct ccn_charbuf *temp1=ccn_charbuf_create();
129 ccn_uri_append(temp1, content_name->buf, content_name->length, 0);
130 if ( nlsr->debugging )
131 printf("Name after chopping: %s \n",ccn_charbuf_as_string(temp1));
132 ccn_charbuf_destroy(&temp1);
akmhoquea0e71152013-02-11 09:47:59 -0600133
Obaid Amin806df812013-02-21 13:30:26 -0600134 process_content_from_sync(content_name,components1);
135 ccn_charbuf_destroy(&content_name);
136 ccn_indexbuf_destroy(&components);
137 ccn_indexbuf_destroy(&components1);
akmhoquea0e71152013-02-11 09:47:59 -0600138
Obaid Amin806df812013-02-21 13:30:26 -0600139 return(0);
akmhoquea0e71152013-02-11 09:47:59 -0600140}
141
142
akmhoqueccb33e92013-02-20 11:44:28 -0600143
akmhoque0c267012013-02-21 13:37:06 -0600144<<<<<<< HEAD
akmhoque8876e982013-02-21 13:35:46 -0600145void
akmhoque0c267012013-02-21 13:37:06 -0600146=======
Obaid Amin806df812013-02-21 13:30:26 -0600147 void
akmhoque0c267012013-02-21 13:37:06 -0600148>>>>>>> 806df81f471eddcdef48b2ab64eaec8aaca2e69c
akmhoqueccb33e92013-02-20 11:44:28 -0600149get_name_part(struct name_prefix *name_part,struct ccn_charbuf * interest_ccnb,
Obaid Amin806df812013-02-21 13:30:26 -0600150 struct ccn_indexbuf *interest_comps, int offset)
akmhoquea0e71152013-02-11 09:47:59 -0600151{
152
akmhoquea0e71152013-02-11 09:47:59 -0600153
akmhoquea0e71152013-02-11 09:47:59 -0600154
Obaid Amin806df812013-02-21 13:30:26 -0600155 int i;
156 int lsa_position=0;
157 int len=0;
akmhoquea0e71152013-02-11 09:47:59 -0600158
akmhoquea0e71152013-02-11 09:47:59 -0600159
Obaid Amin806df812013-02-21 13:30:26 -0600160 struct ccn_indexbuf cid={0};
161 struct ccn_indexbuf *components=&cid;
162 struct ccn_charbuf *name=ccn_charbuf_create();
163 ccn_name_from_uri(name,nlsr->slice_prefix);
164 ccn_name_split (name, components);
165 lsa_position=components->n-2;
akmhoquea0e71152013-02-11 09:47:59 -0600166
Obaid Amin806df812013-02-21 13:30:26 -0600167 ccn_charbuf_destroy(&name);
akmhoquea0e71152013-02-11 09:47:59 -0600168
akmhoquea0e71152013-02-11 09:47:59 -0600169
Obaid Amin806df812013-02-21 13:30:26 -0600170 const unsigned char *comp_ptr1;
171 size_t comp_size;
172 for(i=lsa_position+1+offset;i<interest_comps->n-1;i++)
173 {
174 ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1,
175 &comp_size);
176 len+=1;
177 len+=(int)comp_size;
178 }
179 len++;
akmhoquea0e71152013-02-11 09:47:59 -0600180
Obaid Amin806df812013-02-21 13:30:26 -0600181 char *neighbor=(char *)malloc(len);
182 memset(neighbor,0,len);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600183
Obaid Amin806df812013-02-21 13:30:26 -0600184 for(i=lsa_position+1+offset; i<interest_comps->n-1;i++)
185 {
186 ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1,
187 &comp_size);
188 memcpy(neighbor+strlen(neighbor),"/",1);
189 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,
190 strlen((char *)comp_ptr1));
Obaid Aminbb8ac422013-02-20 17:08:48 -0600191
Obaid Amin806df812013-02-21 13:30:26 -0600192 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600193
Obaid Amin806df812013-02-21 13:30:26 -0600194 name_part->name=(char *)malloc(strlen(neighbor)+1);
195 memset(name_part->name,0,strlen(neighbor)+1);
196 memcpy(name_part->name,neighbor,strlen(neighbor)+1);
197 name_part->length=strlen(neighbor)+1;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600198
Obaid Amin806df812013-02-21 13:30:26 -0600199 // Add 01/31/2013
200 free(neighbor);
akmhoquea0e71152013-02-11 09:47:59 -0600201}
202
akmhoquea0e71152013-02-11 09:47:59 -0600203
akmhoquea0e71152013-02-11 09:47:59 -0600204
205
206
Obaid Amin806df812013-02-21 13:30:26 -0600207 void
akmhoquea0e71152013-02-11 09:47:59 -0600208get_content_by_content_name(char *content_name, unsigned char **content_data)
209{
210
Obaid Amin806df812013-02-21 13:30:26 -0600211 struct ccn_charbuf *name = NULL;
212 struct ccn_charbuf *templ = NULL;
213 struct ccn_charbuf *resultbuf = NULL;
214 struct ccn_parsed_ContentObject pcobuf = { 0 };
215 int res;
216 int allow_stale = 0;
217 int content_only = 1;
218 int scope = -1;
219 const unsigned char *ptr;
220 size_t length;
221 int resolve_version = CCN_V_HIGHEST;
222 int timeout_ms = 3000;
223 const unsigned lifetime_default = CCN_INTEREST_LIFETIME_SEC << 12;
224 unsigned lifetime_l12 = lifetime_default;
225 int get_flags = 0;
akmhoquea0e71152013-02-11 09:47:59 -0600226
Obaid Amin806df812013-02-21 13:30:26 -0600227 name = ccn_charbuf_create();
228 res = ccn_name_from_uri(name,content_name);
229 if (res < 0) {
230 fprintf(stderr, "Bad ccn URI: %s\n", content_name);
231 exit(1);
232 }
akmhoquea0e71152013-02-11 09:47:59 -0600233
Obaid Amin806df812013-02-21 13:30:26 -0600234 if (allow_stale || lifetime_l12 != lifetime_default || scope != -1) {
235 templ = ccn_charbuf_create();
236 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
237 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
238 ccn_charbuf_append_closer(templ); /* </Name> */
239 if (allow_stale) {
240 ccn_charbuf_append_tt(templ, CCN_DTAG_AnswerOriginKind, CCN_DTAG);
241 ccnb_append_number(templ,
242 CCN_AOK_DEFAULT | CCN_AOK_STALE);
243 ccn_charbuf_append_closer(templ); /* </AnswerOriginKind> */
244 }
245 if (scope != -1) {
246 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
247 }
248 if (lifetime_l12 != lifetime_default) {
249 /*
250 * Choose the interest lifetime so there are at least 3
251 * expressions (in the unsatisfied case).
252 */
253 unsigned char buf[3] = { 0 };
254 int i;
255 for (i = sizeof(buf) - 1; i >= 0; i--, lifetime_l12 >>= 8)
256 buf[i] = lifetime_l12 & 0xff;
257 ccnb_append_tagged_blob(templ, CCN_DTAG_InterestLifetime, buf,
258 sizeof(buf));
259 }
260 ccn_charbuf_append_closer(templ); /* </Interest> */
akmhoquea0e71152013-02-11 09:47:59 -0600261 }
Obaid Amin806df812013-02-21 13:30:26 -0600262 resultbuf = ccn_charbuf_create();
263 if (resolve_version != 0) {
264 res = ccn_resolve_version(nlsr->ccn, name, resolve_version, 500);
265 if (res >= 0) {
266 ccn_uri_append(resultbuf, name->buf, name->length, 1);
267 resultbuf->length = 0;
268 }
akmhoquea0e71152013-02-11 09:47:59 -0600269 }
Obaid Amin806df812013-02-21 13:30:26 -0600270 res = ccn_get(nlsr->ccn, name, templ, timeout_ms, resultbuf, &pcobuf, NULL,
271 get_flags);
akmhoquea0e71152013-02-11 09:47:59 -0600272 if (res >= 0) {
Obaid Amin806df812013-02-21 13:30:26 -0600273 ptr = resultbuf->buf;
274 length = resultbuf->length;
275 if (content_only){
276 ccn_content_get_value(ptr, length, &pcobuf, &ptr, &length);
277 *content_data = (unsigned char *) calloc(length, sizeof(char *));
278 memcpy (*content_data, ptr, length);
279 }
akmhoquea0e71152013-02-11 09:47:59 -0600280 }
Obaid Amin806df812013-02-21 13:30:26 -0600281 ccn_charbuf_destroy(&resultbuf);
282 ccn_charbuf_destroy(&templ);
283 ccn_charbuf_destroy(&name);
akmhoquea0e71152013-02-11 09:47:59 -0600284}
285
Obaid Amin806df812013-02-21 13:30:26 -0600286 void
akmhoquea0e71152013-02-11 09:47:59 -0600287process_incoming_sync_content_lsa( unsigned char *content_data)
288{
289
290
Obaid Amin806df812013-02-21 13:30:26 -0600291 if ( nlsr->debugging )
292 printf("process_incoming_sync_content_lsa called \n");
Obaid Aminbb8ac422013-02-20 17:08:48 -0600293
Obaid Amin806df812013-02-21 13:30:26 -0600294 char *sep="|";
295 char *rem;
296 char *orig_router;
297 char *orl;
298 int orig_router_length;
299 char *lst;
300 int ls_type;
301 char *lsid;
302 long int ls_id;
303 char *isvld;
304 int isValid;
305 char *num_link;
306 int no_link;
307 char *np;
308 char *np_length;
309 int name_length;
310 char *data;
311 char *orig_time;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600312
313
akmhoquea0e71152013-02-11 09:47:59 -0600314 if ( nlsr->debugging )
Obaid Amin806df812013-02-21 13:30:26 -0600315 printf("LSA Data \n");
316
317 if( strlen((char *)content_data ) > 0 )
akmhoquea0e71152013-02-11 09:47:59 -0600318 {
Obaid Aminbb8ac422013-02-20 17:08:48 -0600319
Obaid Amin806df812013-02-21 13:30:26 -0600320 orig_router=strtok_r((char *)content_data,sep,&rem);
321 orl=strtok_r(NULL,sep,&rem);
322 orig_router_length=atoi(orl);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600323
Obaid Amin806df812013-02-21 13:30:26 -0600324 if ( nlsr->debugging )
325 {
326 printf(" Orig Router Name : %s\n",orig_router);
327 printf(" Orig Router Length: %d\n",orig_router_length);
328 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600329
Obaid Amin806df812013-02-21 13:30:26 -0600330 lst=strtok_r(NULL,sep,&rem);
331 ls_type=atoi(lst);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600332
Obaid Amin806df812013-02-21 13:30:26 -0600333 if ( nlsr->debugging )
334 printf(" LS Type : %d\n",ls_type);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600335
Obaid Amin806df812013-02-21 13:30:26 -0600336 if ( ls_type == LS_TYPE_NAME )
337 {
338 lsid=strtok_r(NULL,sep,&rem);
339 ls_id=atoi(lsid);
340 orig_time=strtok_r(NULL,sep,&rem);
341 isvld=strtok_r(NULL,sep,&rem);
342 isValid=atoi(isvld);
343 np=strtok_r(NULL,sep,&rem);
344 np_length=strtok_r(NULL,sep,&rem);
345 name_length=atoi(np_length);
346 if ( nlsr->debugging )
347 {
348 printf(" LS ID : %ld\n",ls_id);
349 printf(" isValid : %d\n",isValid);
350 printf(" Name Prefix : %s\n",np);
351 printf(" Orig Time : %s\n",orig_time);
352 printf(" Name Prefix length: %d\n",name_length);
353 }
354
355 build_and_install_others_name_lsa(orig_router,ls_type,ls_id,orig_time,isValid,np);
356
357 print_name_lsdb();
358
359 }
360 else if ( ls_type == LS_TYPE_ADJ )
361 {
362 orig_time=strtok_r(NULL,sep,&rem);
363 num_link=strtok_r(NULL,sep,&rem);
364 no_link=atoi(num_link);
365 data=rem;
366
367 if ( nlsr->debugging )
368 {
369 printf(" Orig Time : %s\n",orig_time);
370 printf(" No Link : %d\n",no_link);
371 printf(" Data : %s\n",data);
372 }
373 build_and_install_others_adj_lsa(orig_router,ls_type,orig_time,no_link,data);
374 }
375 else if ( ls_type == LS_TYPE_COR )
376 {
377 orig_time=strtok_r(NULL,sep,&rem);
378 char *cor_r=strtok_r(NULL,sep,&rem);
379 char *cor_theta=strtok_r(NULL,sep,&rem);
380
381 double r, theta;
382 r=strtof(cor_r,NULL);
383 theta=strtof(cor_theta,NULL);
384
385 if ( nlsr->debugging )
386 {
387 printf(" Orig Time : %s\n",orig_time);
388 printf(" Cor R : %f\n",r);
389 printf(" Cor Theta : %f\n",theta);
390 }
391 build_and_install_others_cor_lsa(orig_router,ls_type,orig_time, (double)r, (double)theta);
392 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600393
394 }
akmhoquea0e71152013-02-11 09:47:59 -0600395}
396
Obaid Amin806df812013-02-21 13:30:26 -0600397 void
akmhoquea0e71152013-02-11 09:47:59 -0600398process_content_from_sync(struct ccn_charbuf *content_name, struct ccn_indexbuf *components)
399{
Obaid Amin806df812013-02-21 13:30:26 -0600400 size_t comp_size;
401 char *lst;
402 char *lsid;
403 const unsigned char *second_last_comp;
404 const unsigned char *third_last_comp;
405 const unsigned char *origtime;
406 char *sep=".";
407 char *rem;
408 char *second_comp_type;
akmhoquea0e71152013-02-11 09:47:59 -0600409
Obaid Amin806df812013-02-21 13:30:26 -0600410 int ls_type;
411 long int ls_id=0;
akmhoquea0e71152013-02-11 09:47:59 -0600412
Obaid Amin806df812013-02-21 13:30:26 -0600413 unsigned char *content_data = NULL;
akmhoquea0e71152013-02-11 09:47:59 -0600414
Obaid Amin806df812013-02-21 13:30:26 -0600415 char *time_stamp=(char *)malloc(20);
416 memset(time_stamp,0,20);
417 get_current_timestamp_micro(time_stamp);
akmhoquea0e71152013-02-11 09:47:59 -0600418
Obaid Amin806df812013-02-21 13:30:26 -0600419 struct ccn_charbuf *uri = ccn_charbuf_create();
420 ccn_uri_append(uri, content_name->buf, content_name->length, 0);
akmhoquea0e71152013-02-11 09:47:59 -0600421
Obaid Amin806df812013-02-21 13:30:26 -0600422 struct name_prefix *orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix));
akmhoquea0e71152013-02-11 09:47:59 -0600423
akmhoquea0e71152013-02-11 09:47:59 -0600424
akmhoquea0e71152013-02-11 09:47:59 -0600425
Obaid Amin806df812013-02-21 13:30:26 -0600426 ccn_name_comp_get(content_name->buf, components,components->n-1-2,&second_last_comp, &comp_size);
427 if (nlsr->debugging)
428 printf("2nd Last Component: %s \n",second_last_comp);
akmhoquea0e71152013-02-11 09:47:59 -0600429
Obaid Amin806df812013-02-21 13:30:26 -0600430 second_comp_type=strtok_r((char *)second_last_comp,sep,&rem);
431 if ( strcmp( second_comp_type, "lsId" ) == 0 )
432 {
433 lsid=rem;
434 ls_id=atoi(rem);
435 ccn_name_comp_get(content_name->buf, components,components->n-2-2,&third_last_comp, &comp_size);
436 lst=strtok_r((char *)third_last_comp,sep,&rem);
437 lst=rem;
438 ls_type=atoi(lst);
439 ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size);
440 ccn_name_chop(content_name,components,-3);
441 get_name_part(orig_router,content_name,components,0);
akmhoquea0e71152013-02-11 09:47:59 -0600442
Obaid Aminbb8ac422013-02-20 17:08:48 -0600443 if ( nlsr->debugging )
Obaid Amin806df812013-02-21 13:30:26 -0600444 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 -0600445
Obaid Amin806df812013-02-21 13:30:26 -0600446 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600447
Obaid Amin806df812013-02-21 13:30:26 -0600448 if ( (strcmp(orig_router->name,nlsr->router_name) == 0 && lsa_life_time < nlsr->lsa_refresh_time)
449 || (strcmp(orig_router->name,nlsr->router_name) != 0 && lsa_life_time < nlsr->router_dead_interval) )
Obaid Aminbb8ac422013-02-20 17:08:48 -0600450 {
Obaid Amin806df812013-02-21 13:30:26 -0600451 int is_new_name_lsa=check_is_new_name_lsa(orig_router->name,(char *)lst,(char *)lsid,(char *)origtime);
452 if ( is_new_name_lsa == 1 )
453 {
454 if ( nlsr->debugging )
455 printf("New NAME LSA.....\n");
456 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
457 if ( nlsr->debugging )
458 printf("Content Data: %s \n",content_data);
459 process_incoming_sync_content_lsa(content_data);
460 }
461 else
462 {
463 if ( nlsr->debugging )
464 printf("Name LSA / Newer Name LSA already xists in LSDB\n");
465 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600466
Obaid Amin806df812013-02-21 13:30:26 -0600467 if ( nlsr->debugging )
468 printf("Content Data: %s \n",content_data);
469 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600470 }
Obaid Amin806df812013-02-21 13:30:26 -0600471 else
Obaid Aminbb8ac422013-02-20 17:08:48 -0600472 {
Obaid Amin806df812013-02-21 13:30:26 -0600473 if ( nlsr->debugging )
474 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
Obaid Aminbb8ac422013-02-20 17:08:48 -0600475 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600476 }
Obaid Amin806df812013-02-21 13:30:26 -0600477 else
Obaid Aminbb8ac422013-02-20 17:08:48 -0600478 {
Obaid Amin806df812013-02-21 13:30:26 -0600479 ls_type=atoi(rem);
480 lst=rem;
481 if(ls_type == LS_TYPE_ADJ)
Obaid Aminbb8ac422013-02-20 17:08:48 -0600482 {
Obaid Amin806df812013-02-21 13:30:26 -0600483 ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size);
484 ccn_name_chop(content_name,components,-2);
485 get_name_part(orig_router,content_name,components,0);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600486
Obaid Amin806df812013-02-21 13:30:26 -0600487 if ( nlsr->debugging )
488 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",orig_router->name,ls_type,origtime);
489
490 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
491 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) )
492 {
493 int is_new_adj_lsa=check_is_new_adj_lsa(orig_router->name,(char *)lst,(char *)origtime);
494 if ( is_new_adj_lsa == 1 )
495 {
496 if ( nlsr->debugging )
497 printf("New Adj LSA.....\n");
498 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
499
500 if ( nlsr->debugging )
501 printf("Content Data: %s \n",content_data);
502 process_incoming_sync_content_lsa(content_data);
503 }
504 else
505 {
506 if ( nlsr->debugging )
507 printf("Adj LSA / Newer Adj LSA already exists in LSDB\n");
508 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
509 if ( nlsr->debugging )
510 printf("Content Data: %s \n",content_data);
511 }
512 }
513 else
514 {
515 if ( nlsr->debugging )
516 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
517 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600518 }
Obaid Amin806df812013-02-21 13:30:26 -0600519 else if(ls_type == LS_TYPE_COR)
Obaid Aminbb8ac422013-02-20 17:08:48 -0600520 {
Obaid Amin806df812013-02-21 13:30:26 -0600521 ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size);
522 ccn_name_chop(content_name,components,-2);
523 get_name_part(orig_router,content_name,components,0);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600524
Obaid Amin806df812013-02-21 13:30:26 -0600525 if ( nlsr->debugging )
526 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",orig_router->name,ls_type,origtime);
527
528 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
529 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) )
530 {
531 int is_new_cor_lsa=check_is_new_cor_lsa(orig_router->name,(char *)lst,(char *)origtime);
532 if ( is_new_cor_lsa == 1 )
533 {
534 if ( nlsr->debugging )
535 printf("New Cor LSA.....\n");
536 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
537
538 if ( nlsr->debugging )
539 printf("Content Data: %s \n",content_data);
540 process_incoming_sync_content_lsa(content_data);
541 }
542 else
543 {
544 if ( nlsr->debugging )
545 printf("Cor LSA / Newer Cor LSA already exists in LSDB\n");
546 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
547 if ( nlsr->debugging )
548 printf("Content Data: %s \n",content_data);
549 }
550 }
551 else
552 {
553 if ( nlsr->debugging )
554 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
555 }
556
557 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600558 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600559
Obaid Amin806df812013-02-21 13:30:26 -0600560 free(orig_router);
561 ccn_charbuf_destroy(&uri);
562 //01/31/2013
563 free(time_stamp);
akmhoquea0e71152013-02-11 09:47:59 -0600564}
565
Obaid Amin806df812013-02-21 13:30:26 -0600566 int
akmhoquea0e71152013-02-11 09:47:59 -0600567sync_monitor(char *topo_prefix, char *slice_prefix)
568{
569
Obaid Amin806df812013-02-21 13:30:26 -0600570 static struct ccns_name_closure nc={0};
571 nlsr->closure = &nc;
572 struct ccn_charbuf *prefix = ccn_charbuf_create();
573 struct ccn_charbuf *roothash = NULL;
574 struct ccn_charbuf *topo = ccn_charbuf_create();
575 nlsr->slice = ccns_slice_create();
576 ccn_charbuf_reset(prefix);
577 ccn_charbuf_reset(topo);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600578
Obaid Amin806df812013-02-21 13:30:26 -0600579 ccn_charbuf_reset(prefix);
580 ccn_name_from_uri(prefix, slice_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600581
Obaid Amin806df812013-02-21 13:30:26 -0600582 ccn_charbuf_reset(topo);
583 ccn_name_from_uri(topo, topo_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600584
Obaid Amin806df812013-02-21 13:30:26 -0600585 ccns_slice_set_topo_prefix(nlsr->slice, topo, prefix);
586 nlsr->closure->callback = &sync_cb;
587 nlsr->ccns = ccns_open(nlsr->ccn, nlsr->slice, nlsr->closure, roothash, NULL);
akmhoquea0e71152013-02-11 09:47:59 -0600588
Obaid Amin806df812013-02-21 13:30:26 -0600589 //01/31/2013
590 ccn_charbuf_destroy(&prefix);
591 ccn_charbuf_destroy(&topo);
592 ccn_charbuf_destroy(&roothash);
593 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600594}
595
Obaid Amin806df812013-02-21 13:30:26 -0600596 struct ccn_charbuf *
akmhoquea0e71152013-02-11 09:47:59 -0600597make_template(int scope)
598{
Obaid Amin806df812013-02-21 13:30:26 -0600599 struct ccn_charbuf *templ = NULL;
600 templ = ccn_charbuf_create();
601 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
602 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
603 ccn_charbuf_append_closer(templ); /* </Name> */
604 if (0 <= scope && scope <= 2)
605 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
606 ccn_charbuf_append_closer(templ); /* </Interest> */
607 return(templ);
akmhoquea0e71152013-02-11 09:47:59 -0600608}
609
Obaid Amin806df812013-02-21 13:30:26 -0600610 int
akmhoquea0e71152013-02-11 09:47:59 -0600611write_data_to_repo(char *data, char *name_prefix)
612{
Obaid Amin806df812013-02-21 13:30:26 -0600613 if ( nlsr->debugging )
614 {
615 printf("write_data_to_repo called\n");
616 printf("Content Name: %s \n",name_prefix);
617 printf("Content Data: %s \n",data);
618 }
akmhoquea0e71152013-02-11 09:47:59 -0600619
Obaid Amin806df812013-02-21 13:30:26 -0600620 struct ccn *temp_ccn;
621 temp_ccn=ccn_create();
622 int ccn_fd=ccn_connect(temp_ccn, NULL);
623 if(ccn_fd == -1)
624 {
625 fprintf(stderr,"Could not connect to ccnd for Data Writing\n");
626 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd for Data Writing\n");
627 return -1;
628 }
629 struct ccn_charbuf *name = NULL;
630 struct ccn_seqwriter *w = NULL;
631 int blocksize = 4096;
632 int freshness = -1;
633 int torepo = 1;
634 int scope = 1;
635 int res;
636 size_t blockread;
637 struct ccn_charbuf *templ;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600638
Obaid Amin806df812013-02-21 13:30:26 -0600639 name = ccn_charbuf_create();
640 res = ccn_name_from_uri(name, name_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600641 if (res < 0) {
Obaid Amin806df812013-02-21 13:30:26 -0600642 fprintf(stderr, "bad CCN URI: %s\n",name_prefix);
643 return -1;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600644 }
Obaid Amin806df812013-02-21 13:30:26 -0600645
646
647 w = ccn_seqw_create(temp_ccn, name);
648 if (w == NULL) {
649 fprintf(stderr, "ccn_seqw_create failed\n");
650 return -1;
651 }
652 ccn_seqw_set_block_limits(w, blocksize, blocksize);
653 if (freshness > -1)
654 ccn_seqw_set_freshness(w, freshness);
655 if (torepo) {
656 struct ccn_charbuf *name_v = ccn_charbuf_create();
657 ccn_seqw_get_name(w, name_v);
658 ccn_name_from_uri(name_v, "%C1.R.sw");
659 ccn_name_append_nonce(name_v);
660 templ = make_template(scope);
661 res = ccn_get(temp_ccn, name_v, templ, 60000, NULL, NULL, NULL, 0);
662 ccn_charbuf_destroy(&templ);
663 ccn_charbuf_destroy(&name_v);
664 if (res < 0) {
665 fprintf(stderr, "No response from repository\n");
666 return -1;
667 }
668 }
akmhoquea0e71152013-02-11 09:47:59 -0600669
670
671
672
Obaid Amin806df812013-02-21 13:30:26 -0600673 blockread = 0;
akmhoquea0e71152013-02-11 09:47:59 -0600674
675
Obaid Amin806df812013-02-21 13:30:26 -0600676 blockread=strlen(data);
akmhoquea0e71152013-02-11 09:47:59 -0600677
Obaid Amin806df812013-02-21 13:30:26 -0600678 if (blockread > 0) {
679 ccn_run(temp_ccn, 100);
680 res = ccn_seqw_write(w, data, blockread);
681 while (res == -1) {
682 ccn_run(temp_ccn, 100);
683 res = ccn_seqw_write(w, data, blockread);
684 }
685 }
686
687 ccn_seqw_close(w);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600688 ccn_run(temp_ccn, 100);
Obaid Amin806df812013-02-21 13:30:26 -0600689 ccn_charbuf_destroy(&name);
690 ccn_destroy(&temp_ccn);
akmhoquea0e71152013-02-11 09:47:59 -0600691
Obaid Amin806df812013-02-21 13:30:26 -0600692 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600693}
694
695
Obaid Amin806df812013-02-21 13:30:26 -0600696 int
akmhoquea0e71152013-02-11 09:47:59 -0600697create_sync_slice(char *topo_prefix, char *slice_prefix)
698{
Obaid Amin806df812013-02-21 13:30:26 -0600699 int res;
700 struct ccn *handle;
701 struct ccns_slice *slice;
702 struct ccn_charbuf *prefix = ccn_charbuf_create();
703 struct ccn_charbuf *topo = ccn_charbuf_create();
704 struct ccn_charbuf *clause = ccn_charbuf_create();
705 struct ccn_charbuf *slice_name = ccn_charbuf_create();
706 struct ccn_charbuf *slice_uri = ccn_charbuf_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600707
Obaid Amin806df812013-02-21 13:30:26 -0600708 if (prefix == NULL || topo == NULL || clause == NULL ||
709 slice_name == NULL || slice_uri == NULL) {
710 fprintf(stderr, "Unable to allocate required memory.\n");
711 return -1;
712 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600713
Obaid Amin806df812013-02-21 13:30:26 -0600714 handle = ccn_create();
715 res = ccn_connect(handle, NULL);
716 if (0 > res) {
717 fprintf(stderr, "Unable to connect to ccnd.\n");
718 return -1;
719 }
akmhoque866c2222013-02-12 10:49:33 -0600720
Obaid Amin806df812013-02-21 13:30:26 -0600721 slice = ccns_slice_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600722
Obaid Amin806df812013-02-21 13:30:26 -0600723 ccn_charbuf_reset(topo);
724 ccn_name_from_uri(topo, topo_prefix);
725 ccn_charbuf_reset(prefix);
726 ccn_name_from_uri(prefix,slice_prefix );
727 ccns_slice_set_topo_prefix(slice, topo, prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600728
729
Obaid Amin806df812013-02-21 13:30:26 -0600730 res = ccns_write_slice(handle, slice, slice_name);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600731
Obaid Amin806df812013-02-21 13:30:26 -0600732 //01/31/2013
733 ccns_slice_destroy(&slice);
734 ccn_destroy(&handle);
735 ccn_charbuf_destroy(&prefix);
736 ccn_charbuf_destroy(&topo);
737 ccn_charbuf_destroy(&clause);
738 ccn_charbuf_destroy(&slice_name);
739 ccn_charbuf_destroy(&slice_uri);
akmhoquea0e71152013-02-11 09:47:59 -0600740
Obaid Amin806df812013-02-21 13:30:26 -0600741 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600742}
743