blob: 4130b9fd9255798c1c1d04481c6cf5190955cfb9 [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
akmhoquefd439252013-02-21 13:38:23 -0600144void
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{
Obaid Amin806df812013-02-21 13:30:26 -0600148 int i;
149 int lsa_position=0;
150 int len=0;
akmhoquea0e71152013-02-11 09:47:59 -0600151
Obaid Amin806df812013-02-21 13:30:26 -0600152 struct ccn_indexbuf cid={0};
153 struct ccn_indexbuf *components=&cid;
154 struct ccn_charbuf *name=ccn_charbuf_create();
155 ccn_name_from_uri(name,nlsr->slice_prefix);
156 ccn_name_split (name, components);
157 lsa_position=components->n-2;
akmhoquea0e71152013-02-11 09:47:59 -0600158
Obaid Amin806df812013-02-21 13:30:26 -0600159 ccn_charbuf_destroy(&name);
akmhoquea0e71152013-02-11 09:47:59 -0600160
Obaid Amin806df812013-02-21 13:30:26 -0600161 const unsigned char *comp_ptr1;
162 size_t comp_size;
163 for(i=lsa_position+1+offset;i<interest_comps->n-1;i++)
164 {
165 ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1,
166 &comp_size);
167 len+=1;
168 len+=(int)comp_size;
169 }
170 len++;
akmhoquea0e71152013-02-11 09:47:59 -0600171
Obaid Amin806df812013-02-21 13:30:26 -0600172 char *neighbor=(char *)malloc(len);
173 memset(neighbor,0,len);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600174
Obaid Amin806df812013-02-21 13:30:26 -0600175 for(i=lsa_position+1+offset; i<interest_comps->n-1;i++)
176 {
177 ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1,
178 &comp_size);
179 memcpy(neighbor+strlen(neighbor),"/",1);
180 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,
181 strlen((char *)comp_ptr1));
Obaid Aminbb8ac422013-02-20 17:08:48 -0600182
Obaid Amin806df812013-02-21 13:30:26 -0600183 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600184
Obaid Amin806df812013-02-21 13:30:26 -0600185 name_part->name=(char *)malloc(strlen(neighbor)+1);
186 memset(name_part->name,0,strlen(neighbor)+1);
187 memcpy(name_part->name,neighbor,strlen(neighbor)+1);
188 name_part->length=strlen(neighbor)+1;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600189
Obaid Amin806df812013-02-21 13:30:26 -0600190 // Add 01/31/2013
191 free(neighbor);
akmhoquea0e71152013-02-11 09:47:59 -0600192}
193
akmhoquea0e71152013-02-11 09:47:59 -0600194
akmhoquea0e71152013-02-11 09:47:59 -0600195
196
197
Obaid Amin806df812013-02-21 13:30:26 -0600198 void
akmhoquea0e71152013-02-11 09:47:59 -0600199get_content_by_content_name(char *content_name, unsigned char **content_data)
200{
201
Obaid Amin806df812013-02-21 13:30:26 -0600202 struct ccn_charbuf *name = NULL;
203 struct ccn_charbuf *templ = NULL;
204 struct ccn_charbuf *resultbuf = NULL;
205 struct ccn_parsed_ContentObject pcobuf = { 0 };
206 int res;
207 int allow_stale = 0;
208 int content_only = 1;
209 int scope = -1;
210 const unsigned char *ptr;
211 size_t length;
212 int resolve_version = CCN_V_HIGHEST;
213 int timeout_ms = 3000;
214 const unsigned lifetime_default = CCN_INTEREST_LIFETIME_SEC << 12;
215 unsigned lifetime_l12 = lifetime_default;
216 int get_flags = 0;
akmhoquea0e71152013-02-11 09:47:59 -0600217
Obaid Amin806df812013-02-21 13:30:26 -0600218 name = ccn_charbuf_create();
219 res = ccn_name_from_uri(name,content_name);
220 if (res < 0) {
221 fprintf(stderr, "Bad ccn URI: %s\n", content_name);
222 exit(1);
223 }
akmhoquea0e71152013-02-11 09:47:59 -0600224
Obaid Amin806df812013-02-21 13:30:26 -0600225 if (allow_stale || lifetime_l12 != lifetime_default || scope != -1) {
226 templ = ccn_charbuf_create();
227 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
228 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
229 ccn_charbuf_append_closer(templ); /* </Name> */
230 if (allow_stale) {
231 ccn_charbuf_append_tt(templ, CCN_DTAG_AnswerOriginKind, CCN_DTAG);
232 ccnb_append_number(templ,
233 CCN_AOK_DEFAULT | CCN_AOK_STALE);
234 ccn_charbuf_append_closer(templ); /* </AnswerOriginKind> */
235 }
236 if (scope != -1) {
237 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
238 }
239 if (lifetime_l12 != lifetime_default) {
240 /*
241 * Choose the interest lifetime so there are at least 3
242 * expressions (in the unsatisfied case).
243 */
244 unsigned char buf[3] = { 0 };
245 int i;
246 for (i = sizeof(buf) - 1; i >= 0; i--, lifetime_l12 >>= 8)
247 buf[i] = lifetime_l12 & 0xff;
248 ccnb_append_tagged_blob(templ, CCN_DTAG_InterestLifetime, buf,
249 sizeof(buf));
250 }
251 ccn_charbuf_append_closer(templ); /* </Interest> */
akmhoquea0e71152013-02-11 09:47:59 -0600252 }
Obaid Amin806df812013-02-21 13:30:26 -0600253 resultbuf = ccn_charbuf_create();
254 if (resolve_version != 0) {
255 res = ccn_resolve_version(nlsr->ccn, name, resolve_version, 500);
256 if (res >= 0) {
257 ccn_uri_append(resultbuf, name->buf, name->length, 1);
258 resultbuf->length = 0;
259 }
akmhoquea0e71152013-02-11 09:47:59 -0600260 }
Obaid Amin806df812013-02-21 13:30:26 -0600261 res = ccn_get(nlsr->ccn, name, templ, timeout_ms, resultbuf, &pcobuf, NULL,
262 get_flags);
akmhoquea0e71152013-02-11 09:47:59 -0600263 if (res >= 0) {
Obaid Amin806df812013-02-21 13:30:26 -0600264 ptr = resultbuf->buf;
265 length = resultbuf->length;
266 if (content_only){
267 ccn_content_get_value(ptr, length, &pcobuf, &ptr, &length);
268 *content_data = (unsigned char *) calloc(length, sizeof(char *));
269 memcpy (*content_data, ptr, length);
270 }
akmhoquea0e71152013-02-11 09:47:59 -0600271 }
Obaid Amin806df812013-02-21 13:30:26 -0600272 ccn_charbuf_destroy(&resultbuf);
273 ccn_charbuf_destroy(&templ);
274 ccn_charbuf_destroy(&name);
akmhoquea0e71152013-02-11 09:47:59 -0600275}
276
Obaid Amin806df812013-02-21 13:30:26 -0600277 void
akmhoquea0e71152013-02-11 09:47:59 -0600278process_incoming_sync_content_lsa( unsigned char *content_data)
279{
280
281
Obaid Amin806df812013-02-21 13:30:26 -0600282 if ( nlsr->debugging )
283 printf("process_incoming_sync_content_lsa called \n");
Obaid Aminbb8ac422013-02-20 17:08:48 -0600284
Obaid Amin806df812013-02-21 13:30:26 -0600285 char *sep="|";
286 char *rem;
287 char *orig_router;
288 char *orl;
289 int orig_router_length;
290 char *lst;
291 int ls_type;
292 char *lsid;
293 long int ls_id;
294 char *isvld;
295 int isValid;
296 char *num_link;
297 int no_link;
298 char *np;
299 char *np_length;
300 int name_length;
301 char *data;
302 char *orig_time;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600303
304
akmhoquea0e71152013-02-11 09:47:59 -0600305 if ( nlsr->debugging )
Obaid Amin806df812013-02-21 13:30:26 -0600306 printf("LSA Data \n");
307
308 if( strlen((char *)content_data ) > 0 )
akmhoquea0e71152013-02-11 09:47:59 -0600309 {
Obaid Aminbb8ac422013-02-20 17:08:48 -0600310
Obaid Amin806df812013-02-21 13:30:26 -0600311 orig_router=strtok_r((char *)content_data,sep,&rem);
312 orl=strtok_r(NULL,sep,&rem);
313 orig_router_length=atoi(orl);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600314
Obaid Amin806df812013-02-21 13:30:26 -0600315 if ( nlsr->debugging )
316 {
317 printf(" Orig Router Name : %s\n",orig_router);
318 printf(" Orig Router Length: %d\n",orig_router_length);
319 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600320
Obaid Amin806df812013-02-21 13:30:26 -0600321 lst=strtok_r(NULL,sep,&rem);
322 ls_type=atoi(lst);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600323
Obaid Amin806df812013-02-21 13:30:26 -0600324 if ( nlsr->debugging )
325 printf(" LS Type : %d\n",ls_type);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600326
Obaid Amin806df812013-02-21 13:30:26 -0600327 if ( ls_type == LS_TYPE_NAME )
328 {
329 lsid=strtok_r(NULL,sep,&rem);
330 ls_id=atoi(lsid);
331 orig_time=strtok_r(NULL,sep,&rem);
332 isvld=strtok_r(NULL,sep,&rem);
333 isValid=atoi(isvld);
334 np=strtok_r(NULL,sep,&rem);
335 np_length=strtok_r(NULL,sep,&rem);
336 name_length=atoi(np_length);
337 if ( nlsr->debugging )
338 {
339 printf(" LS ID : %ld\n",ls_id);
340 printf(" isValid : %d\n",isValid);
341 printf(" Name Prefix : %s\n",np);
342 printf(" Orig Time : %s\n",orig_time);
343 printf(" Name Prefix length: %d\n",name_length);
344 }
345
346 build_and_install_others_name_lsa(orig_router,ls_type,ls_id,orig_time,isValid,np);
347
348 print_name_lsdb();
349
350 }
351 else if ( ls_type == LS_TYPE_ADJ )
352 {
353 orig_time=strtok_r(NULL,sep,&rem);
354 num_link=strtok_r(NULL,sep,&rem);
355 no_link=atoi(num_link);
356 data=rem;
357
358 if ( nlsr->debugging )
359 {
360 printf(" Orig Time : %s\n",orig_time);
361 printf(" No Link : %d\n",no_link);
362 printf(" Data : %s\n",data);
363 }
364 build_and_install_others_adj_lsa(orig_router,ls_type,orig_time,no_link,data);
365 }
366 else if ( ls_type == LS_TYPE_COR )
367 {
368 orig_time=strtok_r(NULL,sep,&rem);
369 char *cor_r=strtok_r(NULL,sep,&rem);
370 char *cor_theta=strtok_r(NULL,sep,&rem);
371
372 double r, theta;
373 r=strtof(cor_r,NULL);
374 theta=strtof(cor_theta,NULL);
375
376 if ( nlsr->debugging )
377 {
378 printf(" Orig Time : %s\n",orig_time);
379 printf(" Cor R : %f\n",r);
380 printf(" Cor Theta : %f\n",theta);
381 }
382 build_and_install_others_cor_lsa(orig_router,ls_type,orig_time, (double)r, (double)theta);
383 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600384
385 }
akmhoquea0e71152013-02-11 09:47:59 -0600386}
387
Obaid Amin806df812013-02-21 13:30:26 -0600388 void
akmhoquea0e71152013-02-11 09:47:59 -0600389process_content_from_sync(struct ccn_charbuf *content_name, struct ccn_indexbuf *components)
390{
Obaid Amin806df812013-02-21 13:30:26 -0600391 size_t comp_size;
392 char *lst;
393 char *lsid;
394 const unsigned char *second_last_comp;
395 const unsigned char *third_last_comp;
396 const unsigned char *origtime;
397 char *sep=".";
398 char *rem;
399 char *second_comp_type;
akmhoquea0e71152013-02-11 09:47:59 -0600400
Obaid Amin806df812013-02-21 13:30:26 -0600401 int ls_type;
402 long int ls_id=0;
akmhoquea0e71152013-02-11 09:47:59 -0600403
Obaid Amin806df812013-02-21 13:30:26 -0600404 unsigned char *content_data = NULL;
akmhoquea0e71152013-02-11 09:47:59 -0600405
Obaid Amin806df812013-02-21 13:30:26 -0600406 char *time_stamp=(char *)malloc(20);
407 memset(time_stamp,0,20);
408 get_current_timestamp_micro(time_stamp);
akmhoquea0e71152013-02-11 09:47:59 -0600409
Obaid Amin806df812013-02-21 13:30:26 -0600410 struct ccn_charbuf *uri = ccn_charbuf_create();
411 ccn_uri_append(uri, content_name->buf, content_name->length, 0);
akmhoquea0e71152013-02-11 09:47:59 -0600412
Obaid Amin806df812013-02-21 13:30:26 -0600413 struct name_prefix *orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix));
akmhoquea0e71152013-02-11 09:47:59 -0600414
akmhoquea0e71152013-02-11 09:47:59 -0600415
akmhoquea0e71152013-02-11 09:47:59 -0600416
Obaid Amin806df812013-02-21 13:30:26 -0600417 ccn_name_comp_get(content_name->buf, components,components->n-1-2,&second_last_comp, &comp_size);
418 if (nlsr->debugging)
419 printf("2nd Last Component: %s \n",second_last_comp);
akmhoquea0e71152013-02-11 09:47:59 -0600420
Obaid Amin806df812013-02-21 13:30:26 -0600421 second_comp_type=strtok_r((char *)second_last_comp,sep,&rem);
422 if ( strcmp( second_comp_type, "lsId" ) == 0 )
423 {
424 lsid=rem;
425 ls_id=atoi(rem);
426 ccn_name_comp_get(content_name->buf, components,components->n-2-2,&third_last_comp, &comp_size);
427 lst=strtok_r((char *)third_last_comp,sep,&rem);
428 lst=rem;
429 ls_type=atoi(lst);
430 ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size);
431 ccn_name_chop(content_name,components,-3);
432 get_name_part(orig_router,content_name,components,0);
akmhoquea0e71152013-02-11 09:47:59 -0600433
Obaid Aminbb8ac422013-02-20 17:08:48 -0600434 if ( nlsr->debugging )
Obaid Amin806df812013-02-21 13:30:26 -0600435 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 -0600436
Obaid Amin806df812013-02-21 13:30:26 -0600437 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600438
Obaid Amin806df812013-02-21 13:30:26 -0600439 if ( (strcmp(orig_router->name,nlsr->router_name) == 0 && lsa_life_time < nlsr->lsa_refresh_time)
440 || (strcmp(orig_router->name,nlsr->router_name) != 0 && lsa_life_time < nlsr->router_dead_interval) )
Obaid Aminbb8ac422013-02-20 17:08:48 -0600441 {
Obaid Amin806df812013-02-21 13:30:26 -0600442 int is_new_name_lsa=check_is_new_name_lsa(orig_router->name,(char *)lst,(char *)lsid,(char *)origtime);
443 if ( is_new_name_lsa == 1 )
444 {
445 if ( nlsr->debugging )
446 printf("New NAME LSA.....\n");
447 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
448 if ( nlsr->debugging )
449 printf("Content Data: %s \n",content_data);
450 process_incoming_sync_content_lsa(content_data);
451 }
452 else
453 {
454 if ( nlsr->debugging )
455 printf("Name LSA / Newer Name LSA already xists in LSDB\n");
456 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600457
Obaid Amin806df812013-02-21 13:30:26 -0600458 if ( nlsr->debugging )
459 printf("Content Data: %s \n",content_data);
460 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600461 }
Obaid Amin806df812013-02-21 13:30:26 -0600462 else
Obaid Aminbb8ac422013-02-20 17:08:48 -0600463 {
Obaid Amin806df812013-02-21 13:30:26 -0600464 if ( nlsr->debugging )
465 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
Obaid Aminbb8ac422013-02-20 17:08:48 -0600466 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600467 }
Obaid Amin806df812013-02-21 13:30:26 -0600468 else
Obaid Aminbb8ac422013-02-20 17:08:48 -0600469 {
Obaid Amin806df812013-02-21 13:30:26 -0600470 ls_type=atoi(rem);
471 lst=rem;
472 if(ls_type == LS_TYPE_ADJ)
Obaid Aminbb8ac422013-02-20 17:08:48 -0600473 {
Obaid Amin806df812013-02-21 13:30:26 -0600474 ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size);
475 ccn_name_chop(content_name,components,-2);
476 get_name_part(orig_router,content_name,components,0);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600477
Obaid Amin806df812013-02-21 13:30:26 -0600478 if ( nlsr->debugging )
479 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",orig_router->name,ls_type,origtime);
480
481 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
482 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) )
483 {
484 int is_new_adj_lsa=check_is_new_adj_lsa(orig_router->name,(char *)lst,(char *)origtime);
485 if ( is_new_adj_lsa == 1 )
486 {
487 if ( nlsr->debugging )
488 printf("New Adj LSA.....\n");
489 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
490
491 if ( nlsr->debugging )
492 printf("Content Data: %s \n",content_data);
493 process_incoming_sync_content_lsa(content_data);
494 }
495 else
496 {
497 if ( nlsr->debugging )
498 printf("Adj LSA / Newer Adj LSA already exists in LSDB\n");
499 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
500 if ( nlsr->debugging )
501 printf("Content Data: %s \n",content_data);
502 }
503 }
504 else
505 {
506 if ( nlsr->debugging )
507 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
508 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600509 }
Obaid Amin806df812013-02-21 13:30:26 -0600510 else if(ls_type == LS_TYPE_COR)
Obaid Aminbb8ac422013-02-20 17:08:48 -0600511 {
Obaid Amin806df812013-02-21 13:30:26 -0600512 ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size);
513 ccn_name_chop(content_name,components,-2);
514 get_name_part(orig_router,content_name,components,0);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600515
Obaid Amin806df812013-02-21 13:30:26 -0600516 if ( nlsr->debugging )
517 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",orig_router->name,ls_type,origtime);
518
519 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
520 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) )
521 {
522 int is_new_cor_lsa=check_is_new_cor_lsa(orig_router->name,(char *)lst,(char *)origtime);
523 if ( is_new_cor_lsa == 1 )
524 {
525 if ( nlsr->debugging )
526 printf("New Cor LSA.....\n");
527 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
528
529 if ( nlsr->debugging )
530 printf("Content Data: %s \n",content_data);
531 process_incoming_sync_content_lsa(content_data);
532 }
533 else
534 {
535 if ( nlsr->debugging )
536 printf("Cor LSA / Newer Cor LSA already exists in LSDB\n");
537 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
538 if ( nlsr->debugging )
539 printf("Content Data: %s \n",content_data);
540 }
541 }
542 else
543 {
544 if ( nlsr->debugging )
545 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
546 }
547
548 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600549 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600550
Obaid Amin05300892013-02-21 13:45:25 -0600551 if (orig_router)
552 free(orig_router);
Obaid Amin806df812013-02-21 13:30:26 -0600553 ccn_charbuf_destroy(&uri);
554 //01/31/2013
555 free(time_stamp);
akmhoquea0e71152013-02-11 09:47:59 -0600556}
557
Obaid Amin806df812013-02-21 13:30:26 -0600558 int
akmhoquea0e71152013-02-11 09:47:59 -0600559sync_monitor(char *topo_prefix, char *slice_prefix)
560{
561
Obaid Amin806df812013-02-21 13:30:26 -0600562 static struct ccns_name_closure nc={0};
563 nlsr->closure = &nc;
564 struct ccn_charbuf *prefix = ccn_charbuf_create();
565 struct ccn_charbuf *roothash = NULL;
566 struct ccn_charbuf *topo = ccn_charbuf_create();
567 nlsr->slice = ccns_slice_create();
568 ccn_charbuf_reset(prefix);
569 ccn_charbuf_reset(topo);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600570
Obaid Amin806df812013-02-21 13:30:26 -0600571 ccn_charbuf_reset(prefix);
572 ccn_name_from_uri(prefix, slice_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600573
Obaid Amin806df812013-02-21 13:30:26 -0600574 ccn_charbuf_reset(topo);
575 ccn_name_from_uri(topo, topo_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600576
Obaid Amin806df812013-02-21 13:30:26 -0600577 ccns_slice_set_topo_prefix(nlsr->slice, topo, prefix);
578 nlsr->closure->callback = &sync_cb;
579 nlsr->ccns = ccns_open(nlsr->ccn, nlsr->slice, nlsr->closure, roothash, NULL);
akmhoquea0e71152013-02-11 09:47:59 -0600580
Obaid Amin806df812013-02-21 13:30:26 -0600581 //01/31/2013
582 ccn_charbuf_destroy(&prefix);
583 ccn_charbuf_destroy(&topo);
584 ccn_charbuf_destroy(&roothash);
585 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600586}
587
Obaid Amin806df812013-02-21 13:30:26 -0600588 struct ccn_charbuf *
akmhoquea0e71152013-02-11 09:47:59 -0600589make_template(int scope)
590{
Obaid Amin806df812013-02-21 13:30:26 -0600591 struct ccn_charbuf *templ = NULL;
592 templ = ccn_charbuf_create();
593 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
594 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
595 ccn_charbuf_append_closer(templ); /* </Name> */
596 if (0 <= scope && scope <= 2)
597 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
598 ccn_charbuf_append_closer(templ); /* </Interest> */
599 return(templ);
akmhoquea0e71152013-02-11 09:47:59 -0600600}
601
Obaid Amin806df812013-02-21 13:30:26 -0600602 int
akmhoquea0e71152013-02-11 09:47:59 -0600603write_data_to_repo(char *data, char *name_prefix)
604{
Obaid Amin806df812013-02-21 13:30:26 -0600605 if ( nlsr->debugging )
606 {
607 printf("write_data_to_repo called\n");
608 printf("Content Name: %s \n",name_prefix);
609 printf("Content Data: %s \n",data);
610 }
akmhoquea0e71152013-02-11 09:47:59 -0600611
Obaid Amin806df812013-02-21 13:30:26 -0600612 struct ccn *temp_ccn;
613 temp_ccn=ccn_create();
614 int ccn_fd=ccn_connect(temp_ccn, NULL);
615 if(ccn_fd == -1)
616 {
617 fprintf(stderr,"Could not connect to ccnd for Data Writing\n");
618 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd for Data Writing\n");
619 return -1;
620 }
621 struct ccn_charbuf *name = NULL;
622 struct ccn_seqwriter *w = NULL;
623 int blocksize = 4096;
624 int freshness = -1;
625 int torepo = 1;
626 int scope = 1;
627 int res;
628 size_t blockread;
629 struct ccn_charbuf *templ;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600630
Obaid Amin806df812013-02-21 13:30:26 -0600631 name = ccn_charbuf_create();
632 res = ccn_name_from_uri(name, name_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600633 if (res < 0) {
Obaid Amin806df812013-02-21 13:30:26 -0600634 fprintf(stderr, "bad CCN URI: %s\n",name_prefix);
635 return -1;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600636 }
Obaid Amin806df812013-02-21 13:30:26 -0600637
638
639 w = ccn_seqw_create(temp_ccn, name);
640 if (w == NULL) {
641 fprintf(stderr, "ccn_seqw_create failed\n");
642 return -1;
643 }
644 ccn_seqw_set_block_limits(w, blocksize, blocksize);
645 if (freshness > -1)
646 ccn_seqw_set_freshness(w, freshness);
647 if (torepo) {
648 struct ccn_charbuf *name_v = ccn_charbuf_create();
649 ccn_seqw_get_name(w, name_v);
650 ccn_name_from_uri(name_v, "%C1.R.sw");
651 ccn_name_append_nonce(name_v);
652 templ = make_template(scope);
653 res = ccn_get(temp_ccn, name_v, templ, 60000, NULL, NULL, NULL, 0);
654 ccn_charbuf_destroy(&templ);
655 ccn_charbuf_destroy(&name_v);
656 if (res < 0) {
657 fprintf(stderr, "No response from repository\n");
658 return -1;
659 }
660 }
akmhoquea0e71152013-02-11 09:47:59 -0600661
662
663
664
Obaid Amin806df812013-02-21 13:30:26 -0600665 blockread = 0;
akmhoquea0e71152013-02-11 09:47:59 -0600666
667
Obaid Amin806df812013-02-21 13:30:26 -0600668 blockread=strlen(data);
akmhoquea0e71152013-02-11 09:47:59 -0600669
Obaid Amin806df812013-02-21 13:30:26 -0600670 if (blockread > 0) {
671 ccn_run(temp_ccn, 100);
672 res = ccn_seqw_write(w, data, blockread);
673 while (res == -1) {
674 ccn_run(temp_ccn, 100);
675 res = ccn_seqw_write(w, data, blockread);
676 }
677 }
678
679 ccn_seqw_close(w);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600680 ccn_run(temp_ccn, 100);
Obaid Amin806df812013-02-21 13:30:26 -0600681 ccn_charbuf_destroy(&name);
682 ccn_destroy(&temp_ccn);
akmhoquea0e71152013-02-11 09:47:59 -0600683
Obaid Amin806df812013-02-21 13:30:26 -0600684 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600685}
686
687
Obaid Amin806df812013-02-21 13:30:26 -0600688 int
akmhoquea0e71152013-02-11 09:47:59 -0600689create_sync_slice(char *topo_prefix, char *slice_prefix)
690{
Obaid Amin806df812013-02-21 13:30:26 -0600691 int res;
692 struct ccn *handle;
693 struct ccns_slice *slice;
694 struct ccn_charbuf *prefix = ccn_charbuf_create();
695 struct ccn_charbuf *topo = ccn_charbuf_create();
696 struct ccn_charbuf *clause = ccn_charbuf_create();
697 struct ccn_charbuf *slice_name = ccn_charbuf_create();
698 struct ccn_charbuf *slice_uri = ccn_charbuf_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600699
Obaid Amin806df812013-02-21 13:30:26 -0600700 if (prefix == NULL || topo == NULL || clause == NULL ||
701 slice_name == NULL || slice_uri == NULL) {
702 fprintf(stderr, "Unable to allocate required memory.\n");
703 return -1;
704 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600705
Obaid Amin806df812013-02-21 13:30:26 -0600706 handle = ccn_create();
707 res = ccn_connect(handle, NULL);
708 if (0 > res) {
709 fprintf(stderr, "Unable to connect to ccnd.\n");
710 return -1;
711 }
akmhoque866c2222013-02-12 10:49:33 -0600712
Obaid Amin806df812013-02-21 13:30:26 -0600713 slice = ccns_slice_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600714
Obaid Amin806df812013-02-21 13:30:26 -0600715 ccn_charbuf_reset(topo);
716 ccn_name_from_uri(topo, topo_prefix);
717 ccn_charbuf_reset(prefix);
718 ccn_name_from_uri(prefix,slice_prefix );
719 ccns_slice_set_topo_prefix(slice, topo, prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600720
721
Obaid Amin806df812013-02-21 13:30:26 -0600722 res = ccns_write_slice(handle, slice, slice_name);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600723
Obaid Amin806df812013-02-21 13:30:26 -0600724 //01/31/2013
725 ccns_slice_destroy(&slice);
726 ccn_destroy(&handle);
727 ccn_charbuf_destroy(&prefix);
728 ccn_charbuf_destroy(&topo);
729 ccn_charbuf_destroy(&clause);
730 ccn_charbuf_destroy(&slice_name);
731 ccn_charbuf_destroy(&slice_uri);
akmhoquea0e71152013-02-11 09:47:59 -0600732
Obaid Amin806df812013-02-21 13:30:26 -0600733 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600734}
735