blob: b60a5ed9dae43cbeec1d102be2501d9bd1b2b323 [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
Obaid Amin806df812013-02-21 13:30:26 -0600144 void
akmhoqueccb33e92013-02-20 11:44:28 -0600145get_name_part(struct name_prefix *name_part,struct ccn_charbuf * interest_ccnb,
Obaid Amin806df812013-02-21 13:30:26 -0600146 struct ccn_indexbuf *interest_comps, int offset)
akmhoquea0e71152013-02-11 09:47:59 -0600147{
148
akmhoquea0e71152013-02-11 09:47:59 -0600149
akmhoquea0e71152013-02-11 09:47:59 -0600150
Obaid Amin806df812013-02-21 13:30:26 -0600151 int i;
152 int lsa_position=0;
153 int len=0;
akmhoquea0e71152013-02-11 09:47:59 -0600154
akmhoquea0e71152013-02-11 09:47:59 -0600155
Obaid Amin806df812013-02-21 13:30:26 -0600156 struct ccn_indexbuf cid={0};
157 struct ccn_indexbuf *components=&cid;
158 struct ccn_charbuf *name=ccn_charbuf_create();
159 ccn_name_from_uri(name,nlsr->slice_prefix);
160 ccn_name_split (name, components);
161 lsa_position=components->n-2;
akmhoquea0e71152013-02-11 09:47:59 -0600162
Obaid Amin806df812013-02-21 13:30:26 -0600163 ccn_charbuf_destroy(&name);
akmhoquea0e71152013-02-11 09:47:59 -0600164
akmhoquea0e71152013-02-11 09:47:59 -0600165
Obaid Amin806df812013-02-21 13:30:26 -0600166 const unsigned char *comp_ptr1;
167 size_t comp_size;
168 for(i=lsa_position+1+offset;i<interest_comps->n-1;i++)
169 {
170 ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1,
171 &comp_size);
172 len+=1;
173 len+=(int)comp_size;
174 }
175 len++;
akmhoquea0e71152013-02-11 09:47:59 -0600176
Obaid Amin806df812013-02-21 13:30:26 -0600177 char *neighbor=(char *)malloc(len);
178 memset(neighbor,0,len);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600179
Obaid Amin806df812013-02-21 13:30:26 -0600180 for(i=lsa_position+1+offset; i<interest_comps->n-1;i++)
181 {
182 ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1,
183 &comp_size);
184 memcpy(neighbor+strlen(neighbor),"/",1);
185 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,
186 strlen((char *)comp_ptr1));
Obaid Aminbb8ac422013-02-20 17:08:48 -0600187
Obaid Amin806df812013-02-21 13:30:26 -0600188 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600189
Obaid Amin806df812013-02-21 13:30:26 -0600190 name_part->name=(char *)malloc(strlen(neighbor)+1);
191 memset(name_part->name,0,strlen(neighbor)+1);
192 memcpy(name_part->name,neighbor,strlen(neighbor)+1);
193 name_part->length=strlen(neighbor)+1;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600194
Obaid Amin806df812013-02-21 13:30:26 -0600195 // Add 01/31/2013
196 free(neighbor);
akmhoquea0e71152013-02-11 09:47:59 -0600197}
198
akmhoquea0e71152013-02-11 09:47:59 -0600199
akmhoquea0e71152013-02-11 09:47:59 -0600200
201
202
Obaid Amin806df812013-02-21 13:30:26 -0600203 void
akmhoquea0e71152013-02-11 09:47:59 -0600204get_content_by_content_name(char *content_name, unsigned char **content_data)
205{
206
Obaid Amin806df812013-02-21 13:30:26 -0600207 struct ccn_charbuf *name = NULL;
208 struct ccn_charbuf *templ = NULL;
209 struct ccn_charbuf *resultbuf = NULL;
210 struct ccn_parsed_ContentObject pcobuf = { 0 };
211 int res;
212 int allow_stale = 0;
213 int content_only = 1;
214 int scope = -1;
215 const unsigned char *ptr;
216 size_t length;
217 int resolve_version = CCN_V_HIGHEST;
218 int timeout_ms = 3000;
219 const unsigned lifetime_default = CCN_INTEREST_LIFETIME_SEC << 12;
220 unsigned lifetime_l12 = lifetime_default;
221 int get_flags = 0;
akmhoquea0e71152013-02-11 09:47:59 -0600222
Obaid Amin806df812013-02-21 13:30:26 -0600223 name = ccn_charbuf_create();
224 res = ccn_name_from_uri(name,content_name);
225 if (res < 0) {
226 fprintf(stderr, "Bad ccn URI: %s\n", content_name);
227 exit(1);
228 }
akmhoquea0e71152013-02-11 09:47:59 -0600229
Obaid Amin806df812013-02-21 13:30:26 -0600230 if (allow_stale || lifetime_l12 != lifetime_default || scope != -1) {
231 templ = ccn_charbuf_create();
232 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
233 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
234 ccn_charbuf_append_closer(templ); /* </Name> */
235 if (allow_stale) {
236 ccn_charbuf_append_tt(templ, CCN_DTAG_AnswerOriginKind, CCN_DTAG);
237 ccnb_append_number(templ,
238 CCN_AOK_DEFAULT | CCN_AOK_STALE);
239 ccn_charbuf_append_closer(templ); /* </AnswerOriginKind> */
240 }
241 if (scope != -1) {
242 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
243 }
244 if (lifetime_l12 != lifetime_default) {
245 /*
246 * Choose the interest lifetime so there are at least 3
247 * expressions (in the unsatisfied case).
248 */
249 unsigned char buf[3] = { 0 };
250 int i;
251 for (i = sizeof(buf) - 1; i >= 0; i--, lifetime_l12 >>= 8)
252 buf[i] = lifetime_l12 & 0xff;
253 ccnb_append_tagged_blob(templ, CCN_DTAG_InterestLifetime, buf,
254 sizeof(buf));
255 }
256 ccn_charbuf_append_closer(templ); /* </Interest> */
akmhoquea0e71152013-02-11 09:47:59 -0600257 }
Obaid Amin806df812013-02-21 13:30:26 -0600258 resultbuf = ccn_charbuf_create();
259 if (resolve_version != 0) {
260 res = ccn_resolve_version(nlsr->ccn, name, resolve_version, 500);
261 if (res >= 0) {
262 ccn_uri_append(resultbuf, name->buf, name->length, 1);
263 resultbuf->length = 0;
264 }
akmhoquea0e71152013-02-11 09:47:59 -0600265 }
Obaid Amin806df812013-02-21 13:30:26 -0600266 res = ccn_get(nlsr->ccn, name, templ, timeout_ms, resultbuf, &pcobuf, NULL,
267 get_flags);
akmhoquea0e71152013-02-11 09:47:59 -0600268 if (res >= 0) {
Obaid Amin806df812013-02-21 13:30:26 -0600269 ptr = resultbuf->buf;
270 length = resultbuf->length;
271 if (content_only){
272 ccn_content_get_value(ptr, length, &pcobuf, &ptr, &length);
273 *content_data = (unsigned char *) calloc(length, sizeof(char *));
274 memcpy (*content_data, ptr, length);
275 }
akmhoquea0e71152013-02-11 09:47:59 -0600276 }
Obaid Amin806df812013-02-21 13:30:26 -0600277 ccn_charbuf_destroy(&resultbuf);
278 ccn_charbuf_destroy(&templ);
279 ccn_charbuf_destroy(&name);
akmhoquea0e71152013-02-11 09:47:59 -0600280}
281
Obaid Amin806df812013-02-21 13:30:26 -0600282 void
akmhoquea0e71152013-02-11 09:47:59 -0600283process_incoming_sync_content_lsa( unsigned char *content_data)
284{
285
286
Obaid Amin806df812013-02-21 13:30:26 -0600287 if ( nlsr->debugging )
288 printf("process_incoming_sync_content_lsa called \n");
Obaid Aminbb8ac422013-02-20 17:08:48 -0600289
Obaid Amin806df812013-02-21 13:30:26 -0600290 char *sep="|";
291 char *rem;
292 char *orig_router;
293 char *orl;
294 int orig_router_length;
295 char *lst;
296 int ls_type;
297 char *lsid;
298 long int ls_id;
299 char *isvld;
300 int isValid;
301 char *num_link;
302 int no_link;
303 char *np;
304 char *np_length;
305 int name_length;
306 char *data;
307 char *orig_time;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600308
309
akmhoquea0e71152013-02-11 09:47:59 -0600310 if ( nlsr->debugging )
Obaid Amin806df812013-02-21 13:30:26 -0600311 printf("LSA Data \n");
312
313 if( strlen((char *)content_data ) > 0 )
akmhoquea0e71152013-02-11 09:47:59 -0600314 {
Obaid Aminbb8ac422013-02-20 17:08:48 -0600315
Obaid Amin806df812013-02-21 13:30:26 -0600316 orig_router=strtok_r((char *)content_data,sep,&rem);
317 orl=strtok_r(NULL,sep,&rem);
318 orig_router_length=atoi(orl);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600319
Obaid Amin806df812013-02-21 13:30:26 -0600320 if ( nlsr->debugging )
321 {
322 printf(" Orig Router Name : %s\n",orig_router);
323 printf(" Orig Router Length: %d\n",orig_router_length);
324 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600325
Obaid Amin806df812013-02-21 13:30:26 -0600326 lst=strtok_r(NULL,sep,&rem);
327 ls_type=atoi(lst);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600328
Obaid Amin806df812013-02-21 13:30:26 -0600329 if ( nlsr->debugging )
330 printf(" LS Type : %d\n",ls_type);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600331
Obaid Amin806df812013-02-21 13:30:26 -0600332 if ( ls_type == LS_TYPE_NAME )
333 {
334 lsid=strtok_r(NULL,sep,&rem);
335 ls_id=atoi(lsid);
336 orig_time=strtok_r(NULL,sep,&rem);
337 isvld=strtok_r(NULL,sep,&rem);
338 isValid=atoi(isvld);
339 np=strtok_r(NULL,sep,&rem);
340 np_length=strtok_r(NULL,sep,&rem);
341 name_length=atoi(np_length);
342 if ( nlsr->debugging )
343 {
344 printf(" LS ID : %ld\n",ls_id);
345 printf(" isValid : %d\n",isValid);
346 printf(" Name Prefix : %s\n",np);
347 printf(" Orig Time : %s\n",orig_time);
348 printf(" Name Prefix length: %d\n",name_length);
349 }
350
351 build_and_install_others_name_lsa(orig_router,ls_type,ls_id,orig_time,isValid,np);
352
353 print_name_lsdb();
354
355 }
356 else if ( ls_type == LS_TYPE_ADJ )
357 {
358 orig_time=strtok_r(NULL,sep,&rem);
359 num_link=strtok_r(NULL,sep,&rem);
360 no_link=atoi(num_link);
361 data=rem;
362
363 if ( nlsr->debugging )
364 {
365 printf(" Orig Time : %s\n",orig_time);
366 printf(" No Link : %d\n",no_link);
367 printf(" Data : %s\n",data);
368 }
369 build_and_install_others_adj_lsa(orig_router,ls_type,orig_time,no_link,data);
370 }
371 else if ( ls_type == LS_TYPE_COR )
372 {
373 orig_time=strtok_r(NULL,sep,&rem);
374 char *cor_r=strtok_r(NULL,sep,&rem);
375 char *cor_theta=strtok_r(NULL,sep,&rem);
376
377 double r, theta;
378 r=strtof(cor_r,NULL);
379 theta=strtof(cor_theta,NULL);
380
381 if ( nlsr->debugging )
382 {
383 printf(" Orig Time : %s\n",orig_time);
384 printf(" Cor R : %f\n",r);
385 printf(" Cor Theta : %f\n",theta);
386 }
387 build_and_install_others_cor_lsa(orig_router,ls_type,orig_time, (double)r, (double)theta);
388 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600389
390 }
akmhoquea0e71152013-02-11 09:47:59 -0600391}
392
Obaid Amin806df812013-02-21 13:30:26 -0600393 void
akmhoquea0e71152013-02-11 09:47:59 -0600394process_content_from_sync(struct ccn_charbuf *content_name, struct ccn_indexbuf *components)
395{
Obaid Amin806df812013-02-21 13:30:26 -0600396 size_t comp_size;
397 char *lst;
398 char *lsid;
399 const unsigned char *second_last_comp;
400 const unsigned char *third_last_comp;
401 const unsigned char *origtime;
402 char *sep=".";
403 char *rem;
404 char *second_comp_type;
akmhoquea0e71152013-02-11 09:47:59 -0600405
Obaid Amin806df812013-02-21 13:30:26 -0600406 int ls_type;
407 long int ls_id=0;
akmhoquea0e71152013-02-11 09:47:59 -0600408
Obaid Amin806df812013-02-21 13:30:26 -0600409 unsigned char *content_data = NULL;
akmhoquea0e71152013-02-11 09:47:59 -0600410
Obaid Amin806df812013-02-21 13:30:26 -0600411 char *time_stamp=(char *)malloc(20);
412 memset(time_stamp,0,20);
413 get_current_timestamp_micro(time_stamp);
akmhoquea0e71152013-02-11 09:47:59 -0600414
Obaid Amin806df812013-02-21 13:30:26 -0600415 struct ccn_charbuf *uri = ccn_charbuf_create();
416 ccn_uri_append(uri, content_name->buf, content_name->length, 0);
akmhoquea0e71152013-02-11 09:47:59 -0600417
Obaid Amin806df812013-02-21 13:30:26 -0600418 struct name_prefix *orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix));
akmhoquea0e71152013-02-11 09:47:59 -0600419
akmhoquea0e71152013-02-11 09:47:59 -0600420
akmhoquea0e71152013-02-11 09:47:59 -0600421
Obaid Amin806df812013-02-21 13:30:26 -0600422 ccn_name_comp_get(content_name->buf, components,components->n-1-2,&second_last_comp, &comp_size);
423 if (nlsr->debugging)
424 printf("2nd Last Component: %s \n",second_last_comp);
akmhoquea0e71152013-02-11 09:47:59 -0600425
Obaid Amin806df812013-02-21 13:30:26 -0600426 second_comp_type=strtok_r((char *)second_last_comp,sep,&rem);
427 if ( strcmp( second_comp_type, "lsId" ) == 0 )
428 {
429 lsid=rem;
430 ls_id=atoi(rem);
431 ccn_name_comp_get(content_name->buf, components,components->n-2-2,&third_last_comp, &comp_size);
432 lst=strtok_r((char *)third_last_comp,sep,&rem);
433 lst=rem;
434 ls_type=atoi(lst);
435 ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size);
436 ccn_name_chop(content_name,components,-3);
437 get_name_part(orig_router,content_name,components,0);
akmhoquea0e71152013-02-11 09:47:59 -0600438
Obaid Aminbb8ac422013-02-20 17:08:48 -0600439 if ( nlsr->debugging )
Obaid Amin806df812013-02-21 13:30:26 -0600440 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 -0600441
Obaid Amin806df812013-02-21 13:30:26 -0600442 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600443
Obaid Amin806df812013-02-21 13:30:26 -0600444 if ( (strcmp(orig_router->name,nlsr->router_name) == 0 && lsa_life_time < nlsr->lsa_refresh_time)
445 || (strcmp(orig_router->name,nlsr->router_name) != 0 && lsa_life_time < nlsr->router_dead_interval) )
Obaid Aminbb8ac422013-02-20 17:08:48 -0600446 {
Obaid Amin806df812013-02-21 13:30:26 -0600447 int is_new_name_lsa=check_is_new_name_lsa(orig_router->name,(char *)lst,(char *)lsid,(char *)origtime);
448 if ( is_new_name_lsa == 1 )
449 {
450 if ( nlsr->debugging )
451 printf("New NAME LSA.....\n");
452 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
453 if ( nlsr->debugging )
454 printf("Content Data: %s \n",content_data);
455 process_incoming_sync_content_lsa(content_data);
456 }
457 else
458 {
459 if ( nlsr->debugging )
460 printf("Name LSA / Newer Name LSA already xists in LSDB\n");
461 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600462
Obaid Amin806df812013-02-21 13:30:26 -0600463 if ( nlsr->debugging )
464 printf("Content Data: %s \n",content_data);
465 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600466 }
Obaid Amin806df812013-02-21 13:30:26 -0600467 else
Obaid Aminbb8ac422013-02-20 17:08:48 -0600468 {
Obaid Amin806df812013-02-21 13:30:26 -0600469 if ( nlsr->debugging )
470 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
Obaid Aminbb8ac422013-02-20 17:08:48 -0600471 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600472 }
Obaid Amin806df812013-02-21 13:30:26 -0600473 else
Obaid Aminbb8ac422013-02-20 17:08:48 -0600474 {
Obaid Amin806df812013-02-21 13:30:26 -0600475 ls_type=atoi(rem);
476 lst=rem;
477 if(ls_type == LS_TYPE_ADJ)
Obaid Aminbb8ac422013-02-20 17:08:48 -0600478 {
Obaid Amin806df812013-02-21 13:30:26 -0600479 ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size);
480 ccn_name_chop(content_name,components,-2);
481 get_name_part(orig_router,content_name,components,0);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600482
Obaid Amin806df812013-02-21 13:30:26 -0600483 if ( nlsr->debugging )
484 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",orig_router->name,ls_type,origtime);
485
486 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
487 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) )
488 {
489 int is_new_adj_lsa=check_is_new_adj_lsa(orig_router->name,(char *)lst,(char *)origtime);
490 if ( is_new_adj_lsa == 1 )
491 {
492 if ( nlsr->debugging )
493 printf("New Adj LSA.....\n");
494 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
495
496 if ( nlsr->debugging )
497 printf("Content Data: %s \n",content_data);
498 process_incoming_sync_content_lsa(content_data);
499 }
500 else
501 {
502 if ( nlsr->debugging )
503 printf("Adj LSA / Newer Adj LSA already exists in LSDB\n");
504 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
505 if ( nlsr->debugging )
506 printf("Content Data: %s \n",content_data);
507 }
508 }
509 else
510 {
511 if ( nlsr->debugging )
512 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
513 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600514 }
Obaid Amin806df812013-02-21 13:30:26 -0600515 else if(ls_type == LS_TYPE_COR)
Obaid Aminbb8ac422013-02-20 17:08:48 -0600516 {
Obaid Amin806df812013-02-21 13:30:26 -0600517 ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size);
518 ccn_name_chop(content_name,components,-2);
519 get_name_part(orig_router,content_name,components,0);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600520
Obaid Amin806df812013-02-21 13:30:26 -0600521 if ( nlsr->debugging )
522 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",orig_router->name,ls_type,origtime);
523
524 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
525 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) )
526 {
527 int is_new_cor_lsa=check_is_new_cor_lsa(orig_router->name,(char *)lst,(char *)origtime);
528 if ( is_new_cor_lsa == 1 )
529 {
530 if ( nlsr->debugging )
531 printf("New Cor LSA.....\n");
532 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
533
534 if ( nlsr->debugging )
535 printf("Content Data: %s \n",content_data);
536 process_incoming_sync_content_lsa(content_data);
537 }
538 else
539 {
540 if ( nlsr->debugging )
541 printf("Cor LSA / Newer Cor LSA already exists in LSDB\n");
542 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
543 if ( nlsr->debugging )
544 printf("Content Data: %s \n",content_data);
545 }
546 }
547 else
548 {
549 if ( nlsr->debugging )
550 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
551 }
552
553 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600554 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600555
Obaid Amin806df812013-02-21 13:30:26 -0600556 free(orig_router);
557 ccn_charbuf_destroy(&uri);
558 //01/31/2013
559 free(time_stamp);
akmhoquea0e71152013-02-11 09:47:59 -0600560}
561
Obaid Amin806df812013-02-21 13:30:26 -0600562 int
akmhoquea0e71152013-02-11 09:47:59 -0600563sync_monitor(char *topo_prefix, char *slice_prefix)
564{
565
Obaid Amin806df812013-02-21 13:30:26 -0600566 static struct ccns_name_closure nc={0};
567 nlsr->closure = &nc;
568 struct ccn_charbuf *prefix = ccn_charbuf_create();
569 struct ccn_charbuf *roothash = NULL;
570 struct ccn_charbuf *topo = ccn_charbuf_create();
571 nlsr->slice = ccns_slice_create();
572 ccn_charbuf_reset(prefix);
573 ccn_charbuf_reset(topo);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600574
Obaid Amin806df812013-02-21 13:30:26 -0600575 ccn_charbuf_reset(prefix);
576 ccn_name_from_uri(prefix, slice_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600577
Obaid Amin806df812013-02-21 13:30:26 -0600578 ccn_charbuf_reset(topo);
579 ccn_name_from_uri(topo, topo_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600580
Obaid Amin806df812013-02-21 13:30:26 -0600581 ccns_slice_set_topo_prefix(nlsr->slice, topo, prefix);
582 nlsr->closure->callback = &sync_cb;
583 nlsr->ccns = ccns_open(nlsr->ccn, nlsr->slice, nlsr->closure, roothash, NULL);
akmhoquea0e71152013-02-11 09:47:59 -0600584
Obaid Amin806df812013-02-21 13:30:26 -0600585 //01/31/2013
586 ccn_charbuf_destroy(&prefix);
587 ccn_charbuf_destroy(&topo);
588 ccn_charbuf_destroy(&roothash);
589 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600590}
591
Obaid Amin806df812013-02-21 13:30:26 -0600592 struct ccn_charbuf *
akmhoquea0e71152013-02-11 09:47:59 -0600593make_template(int scope)
594{
Obaid Amin806df812013-02-21 13:30:26 -0600595 struct ccn_charbuf *templ = NULL;
596 templ = ccn_charbuf_create();
597 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
598 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
599 ccn_charbuf_append_closer(templ); /* </Name> */
600 if (0 <= scope && scope <= 2)
601 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
602 ccn_charbuf_append_closer(templ); /* </Interest> */
603 return(templ);
akmhoquea0e71152013-02-11 09:47:59 -0600604}
605
Obaid Amin806df812013-02-21 13:30:26 -0600606 int
akmhoquea0e71152013-02-11 09:47:59 -0600607write_data_to_repo(char *data, char *name_prefix)
608{
Obaid Amin806df812013-02-21 13:30:26 -0600609 if ( nlsr->debugging )
610 {
611 printf("write_data_to_repo called\n");
612 printf("Content Name: %s \n",name_prefix);
613 printf("Content Data: %s \n",data);
614 }
akmhoquea0e71152013-02-11 09:47:59 -0600615
Obaid Amin806df812013-02-21 13:30:26 -0600616 struct ccn *temp_ccn;
617 temp_ccn=ccn_create();
618 int ccn_fd=ccn_connect(temp_ccn, NULL);
619 if(ccn_fd == -1)
620 {
621 fprintf(stderr,"Could not connect to ccnd for Data Writing\n");
622 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd for Data Writing\n");
623 return -1;
624 }
625 struct ccn_charbuf *name = NULL;
626 struct ccn_seqwriter *w = NULL;
627 int blocksize = 4096;
628 int freshness = -1;
629 int torepo = 1;
630 int scope = 1;
631 int res;
632 size_t blockread;
633 struct ccn_charbuf *templ;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600634
Obaid Amin806df812013-02-21 13:30:26 -0600635 name = ccn_charbuf_create();
636 res = ccn_name_from_uri(name, name_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600637 if (res < 0) {
Obaid Amin806df812013-02-21 13:30:26 -0600638 fprintf(stderr, "bad CCN URI: %s\n",name_prefix);
639 return -1;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600640 }
Obaid Amin806df812013-02-21 13:30:26 -0600641
642
643 w = ccn_seqw_create(temp_ccn, name);
644 if (w == NULL) {
645 fprintf(stderr, "ccn_seqw_create failed\n");
646 return -1;
647 }
648 ccn_seqw_set_block_limits(w, blocksize, blocksize);
649 if (freshness > -1)
650 ccn_seqw_set_freshness(w, freshness);
651 if (torepo) {
652 struct ccn_charbuf *name_v = ccn_charbuf_create();
653 ccn_seqw_get_name(w, name_v);
654 ccn_name_from_uri(name_v, "%C1.R.sw");
655 ccn_name_append_nonce(name_v);
656 templ = make_template(scope);
657 res = ccn_get(temp_ccn, name_v, templ, 60000, NULL, NULL, NULL, 0);
658 ccn_charbuf_destroy(&templ);
659 ccn_charbuf_destroy(&name_v);
660 if (res < 0) {
661 fprintf(stderr, "No response from repository\n");
662 return -1;
663 }
664 }
akmhoquea0e71152013-02-11 09:47:59 -0600665
666
667
668
Obaid Amin806df812013-02-21 13:30:26 -0600669 blockread = 0;
akmhoquea0e71152013-02-11 09:47:59 -0600670
671
Obaid Amin806df812013-02-21 13:30:26 -0600672 blockread=strlen(data);
akmhoquea0e71152013-02-11 09:47:59 -0600673
Obaid Amin806df812013-02-21 13:30:26 -0600674 if (blockread > 0) {
675 ccn_run(temp_ccn, 100);
676 res = ccn_seqw_write(w, data, blockread);
677 while (res == -1) {
678 ccn_run(temp_ccn, 100);
679 res = ccn_seqw_write(w, data, blockread);
680 }
681 }
682
683 ccn_seqw_close(w);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600684 ccn_run(temp_ccn, 100);
Obaid Amin806df812013-02-21 13:30:26 -0600685 ccn_charbuf_destroy(&name);
686 ccn_destroy(&temp_ccn);
akmhoquea0e71152013-02-11 09:47:59 -0600687
Obaid Amin806df812013-02-21 13:30:26 -0600688 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600689}
690
691
Obaid Amin806df812013-02-21 13:30:26 -0600692 int
akmhoquea0e71152013-02-11 09:47:59 -0600693create_sync_slice(char *topo_prefix, char *slice_prefix)
694{
Obaid Amin806df812013-02-21 13:30:26 -0600695 int res;
696 struct ccn *handle;
697 struct ccns_slice *slice;
698 struct ccn_charbuf *prefix = ccn_charbuf_create();
699 struct ccn_charbuf *topo = ccn_charbuf_create();
700 struct ccn_charbuf *clause = ccn_charbuf_create();
701 struct ccn_charbuf *slice_name = ccn_charbuf_create();
702 struct ccn_charbuf *slice_uri = ccn_charbuf_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600703
Obaid Amin806df812013-02-21 13:30:26 -0600704 if (prefix == NULL || topo == NULL || clause == NULL ||
705 slice_name == NULL || slice_uri == NULL) {
706 fprintf(stderr, "Unable to allocate required memory.\n");
707 return -1;
708 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600709
Obaid Amin806df812013-02-21 13:30:26 -0600710 handle = ccn_create();
711 res = ccn_connect(handle, NULL);
712 if (0 > res) {
713 fprintf(stderr, "Unable to connect to ccnd.\n");
714 return -1;
715 }
akmhoque866c2222013-02-12 10:49:33 -0600716
Obaid Amin806df812013-02-21 13:30:26 -0600717 slice = ccns_slice_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600718
Obaid Amin806df812013-02-21 13:30:26 -0600719 ccn_charbuf_reset(topo);
720 ccn_name_from_uri(topo, topo_prefix);
721 ccn_charbuf_reset(prefix);
722 ccn_name_from_uri(prefix,slice_prefix );
723 ccns_slice_set_topo_prefix(slice, topo, prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600724
725
Obaid Amin806df812013-02-21 13:30:26 -0600726 res = ccns_write_slice(handle, slice, slice_name);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600727
Obaid Amin806df812013-02-21 13:30:26 -0600728 //01/31/2013
729 ccns_slice_destroy(&slice);
730 ccn_destroy(&handle);
731 ccn_charbuf_destroy(&prefix);
732 ccn_charbuf_destroy(&topo);
733 ccn_charbuf_destroy(&clause);
734 ccn_charbuf_destroy(&slice_name);
735 ccn_charbuf_destroy(&slice_uri);
akmhoquea0e71152013-02-11 09:47:59 -0600736
Obaid Amin806df812013-02-21 13:30:26 -0600737 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600738}
739