blob: f2d390b8642d3d3b902eceb3eade2f7a6e09efe6 [file] [log] [blame]
akmhoquea0e71152013-02-11 09:47:59 -06001#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4#include <unistd.h>
5#include <getopt.h>
6#include <sys/time.h>
7#include <sys/stat.h>
8#include <assert.h>
9#include <sys/types.h>
10#include <signal.h>
11#include <sys/socket.h>
12#include <sys/un.h>
13#include <fcntl.h>
14#include <sys/ioctl.h>
15#include <netinet/in.h>
16#include <netdb.h>
17#include <arpa/inet.h>
18
19#include <ccn/ccn.h>
20#include <ccn/uri.h>
21#include <ccn/keystore.h>
22#include <ccn/signing.h>
23#include <ccn/schedule.h>
24#include <ccn/hashtb.h>
25#include <ccn/sync.h>
26#include <ccn/seqwriter.h>
27
28#include "nlsr.h"
29#include "nlsr_sync.h"
30#include "nlsr_lsdb.h"
31#include "utility.h"
32
33
Obaid Amin806df812013-02-21 13:30:26 -060034 char *
akmhoquea0e71152013-02-11 09:47:59 -060035hex_string(unsigned char *s, size_t l)
36{
Obaid Amin806df812013-02-21 13:30:26 -060037 const char *hex_digits = "0123456789abcdef";
38 char *r;
39 int i;
40 r = calloc(1, 1 + 2 * l);
41 for (i = 0; i < l; i++) {
42 r[2*i] = hex_digits[(s[i]>>4) & 0xf];
43 r[1+2*i] = hex_digits[s[i] & 0xf];
44 }
45 return(r);
akmhoquea0e71152013-02-11 09:47:59 -060046}
47
Obaid Amin806df812013-02-21 13:30:26 -060048 int
akmhoquea0e71152013-02-11 09:47:59 -060049sync_cb(struct ccns_name_closure *nc,
Obaid Amin806df812013-02-21 13:30:26 -060050 struct ccn_charbuf *lhash,
51 struct ccn_charbuf *rhash,
52 struct ccn_charbuf *name)
akmhoquea0e71152013-02-11 09:47:59 -060053{
Obaid Amin806df812013-02-21 13:30:26 -060054 int res;
Obaid Amin7997ad82013-02-22 16:41:56 -060055 struct ccn_charbuf *content_name;
56 struct ccn_indexbuf *content_comps;
57 struct ccn_indexbuf *name_comps;
58
59 content_comps = ccn_indexbuf_create();
60 res = ccn_name_split(name, content_comps);
Obaid Amin806df812013-02-21 13:30:26 -060061 if ( res < 0 )
62 return 0;
Obaid Amin7997ad82013-02-22 16:41:56 -060063
64 if (content_comps->n < 2)
Obaid Amin806df812013-02-21 13:30:26 -060065 return 0;
Obaid Amin7997ad82013-02-22 16:41:56 -060066
67 content_name = ccn_charbuf_create();
68 ccn_name_init(content_name);
69
70 res = ccn_name_append_components( content_name, name->buf,
71 content_comps->buf[0], content_comps->buf[content_comps->n - 1]);
Obaid Amin806df812013-02-21 13:30:26 -060072
73 if ( res < 0)
74 return 0;
akmhoquea0e71152013-02-11 09:47:59 -060075
Obaid Amin7997ad82013-02-22 16:41:56 -060076 // for debugging
Obaid Amin806df812013-02-21 13:30:26 -060077 struct ccn_charbuf *temp=ccn_charbuf_create();
78 ccn_uri_append(temp, content_name->buf, content_name->length, 0);
79 if ( nlsr->debugging )
80 printf("Name before chopping: %s \n",ccn_charbuf_as_string(temp));
81 ccn_charbuf_destroy(&temp);
akmhoquea0e71152013-02-11 09:47:59 -060082
Obaid Amin7997ad82013-02-22 16:41:56 -060083 name_comps = ccn_indexbuf_create();
84 res=ccn_name_split (content_name, name_comps);
85 if (res < 0)
Obaid Amin806df812013-02-21 13:30:26 -060086 return 0;
akmhoquea0e71152013-02-11 09:47:59 -060087
Obaid Amin806df812013-02-21 13:30:26 -060088 if ( nlsr->debugging )
89 {
90 printf("Number of components in name = %d \n",res);
91 printf("Number of components in name as indexbuf->n = %d \n",
Obaid Amin7997ad82013-02-22 16:41:56 -060092 (int)name_comps->n);
Obaid Amin806df812013-02-21 13:30:26 -060093 }
Obaid Amin7997ad82013-02-22 16:41:56 -060094
95 ccn_name_chop(content_name, name_comps, -3);
Obaid Amin806df812013-02-21 13:30:26 -060096 if ( nlsr->debugging )
97 printf("Number of components in name as indexbuf->n after chopping= %d \n"
Obaid Amin7997ad82013-02-22 16:41:56 -060098 , (int)name_comps->n);
akmhoquea0e71152013-02-11 09:47:59 -060099
Obaid Amin7997ad82013-02-22 16:41:56 -0600100 //for debugging
Obaid Amin806df812013-02-21 13:30:26 -0600101 struct ccn_charbuf *temp1=ccn_charbuf_create();
102 ccn_uri_append(temp1, content_name->buf, content_name->length, 0);
103 if ( nlsr->debugging )
104 printf("Name after chopping: %s \n",ccn_charbuf_as_string(temp1));
105 ccn_charbuf_destroy(&temp1);
akmhoquea0e71152013-02-11 09:47:59 -0600106
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600107 //main method that processes contents from the sync
Obaid Amin7997ad82013-02-22 16:41:56 -0600108 process_content_from_sync(content_name, name_comps);
109
Obaid Amin806df812013-02-21 13:30:26 -0600110 ccn_charbuf_destroy(&content_name);
Obaid Amin7997ad82013-02-22 16:41:56 -0600111 ccn_indexbuf_destroy(&content_comps);
112 ccn_indexbuf_destroy(&name_comps);
akmhoquea0e71152013-02-11 09:47:59 -0600113
Obaid Amin806df812013-02-21 13:30:26 -0600114 return(0);
akmhoquea0e71152013-02-11 09:47:59 -0600115}
116
117
akmhoqueccb33e92013-02-20 11:44:28 -0600118
Obaid Amin7793ba72013-02-22 00:43:58 -0600119 void
akmhoqueccb33e92013-02-20 11:44:28 -0600120get_name_part(struct name_prefix *name_part,struct ccn_charbuf * interest_ccnb,
Obaid Amin806df812013-02-21 13:30:26 -0600121 struct ccn_indexbuf *interest_comps, int offset)
akmhoquea0e71152013-02-11 09:47:59 -0600122{
akmhoquef6773612013-02-23 09:53:00 -0600123 //int i;
Obaid Amin806df812013-02-21 13:30:26 -0600124 int lsa_position=0;
akmhoquef6773612013-02-23 09:53:00 -0600125 //int len=0;
akmhoquea0e71152013-02-11 09:47:59 -0600126
akmhoque0b60ba92013-02-25 17:55:35 -0600127 //struct ccn_indexbuf cid={0};
128 //struct ccn_indexbuf *components=&cid;
129 struct ccn_indexbuf *components=ccn_indexbuf_create();
Obaid Amin806df812013-02-21 13:30:26 -0600130 struct ccn_charbuf *name=ccn_charbuf_create();
131 ccn_name_from_uri(name,nlsr->slice_prefix);
132 ccn_name_split (name, components);
133 lsa_position=components->n-2;
Obaid Amin806df812013-02-21 13:30:26 -0600134 ccn_charbuf_destroy(&name);
akmhoquea0e71152013-02-11 09:47:59 -0600135
akmhoquef6773612013-02-23 09:53:00 -0600136 struct ccn_charbuf *temp=ccn_charbuf_create();
137 ccn_name_init(temp);
138 ccn_name_append_components( temp, interest_ccnb->buf,
139 interest_comps->buf[lsa_position+1], interest_comps->buf[interest_comps->n - 1]);
140
141 struct ccn_charbuf *temp1=ccn_charbuf_create();
142 ccn_uri_append(temp1, temp->buf, temp->length, 0);
143
144 name_part->name=(char *)calloc(strlen(ccn_charbuf_as_string(temp1))+1,sizeof(char));
145 memcpy(name_part->name,ccn_charbuf_as_string(temp1),strlen(ccn_charbuf_as_string(temp1)));
146 name_part->name[strlen(ccn_charbuf_as_string(temp1))]='\0';
147 name_part->length=strlen(ccn_charbuf_as_string(temp1))+1;
148
149 ccn_charbuf_destroy(&temp1);
150 ccn_charbuf_destroy(&temp);
akmhoque0b60ba92013-02-25 17:55:35 -0600151 ccn_indexbuf_destroy(&components);
akmhoquef6773612013-02-23 09:53:00 -0600152
153 if ( nlsr->debugging )
154 printf("Name Part: %s \n",name_part->name);
155
156
157 /*
Obaid Amin806df812013-02-21 13:30:26 -0600158 const unsigned char *comp_ptr1;
159 size_t comp_size;
160 for(i=lsa_position+1+offset;i<interest_comps->n-1;i++)
161 {
162 ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1,
163 &comp_size);
164 len+=1;
165 len+=(int)comp_size;
166 }
167 len++;
akmhoquea0e71152013-02-11 09:47:59 -0600168
Obaid Amin806df812013-02-21 13:30:26 -0600169 char *neighbor=(char *)malloc(len);
170 memset(neighbor,0,len);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600171
Obaid Amin806df812013-02-21 13:30:26 -0600172 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 memcpy(neighbor+strlen(neighbor),"/",1);
177 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,
178 strlen((char *)comp_ptr1));
Obaid Aminbb8ac422013-02-20 17:08:48 -0600179
Obaid Amin806df812013-02-21 13:30:26 -0600180 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600181
Obaid Amin806df812013-02-21 13:30:26 -0600182 name_part->name=(char *)malloc(strlen(neighbor)+1);
183 memset(name_part->name,0,strlen(neighbor)+1);
184 memcpy(name_part->name,neighbor,strlen(neighbor)+1);
185 name_part->length=strlen(neighbor)+1;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600186
Obaid Amin806df812013-02-21 13:30:26 -0600187 // Add 01/31/2013
188 free(neighbor);
akmhoquef6773612013-02-23 09:53:00 -0600189 */
akmhoquea0e71152013-02-11 09:47:59 -0600190}
191
akmhoquea0e71152013-02-11 09:47:59 -0600192
akmhoquea0e71152013-02-11 09:47:59 -0600193
194
195
Obaid Amin806df812013-02-21 13:30:26 -0600196 void
akmhoquea0e71152013-02-11 09:47:59 -0600197get_content_by_content_name(char *content_name, unsigned char **content_data)
198{
199
Obaid Amin806df812013-02-21 13:30:26 -0600200 struct ccn_charbuf *name = NULL;
201 struct ccn_charbuf *templ = NULL;
202 struct ccn_charbuf *resultbuf = NULL;
203 struct ccn_parsed_ContentObject pcobuf = { 0 };
204 int res;
205 int allow_stale = 0;
206 int content_only = 1;
207 int scope = -1;
208 const unsigned char *ptr;
209 size_t length;
210 int resolve_version = CCN_V_HIGHEST;
211 int timeout_ms = 3000;
212 const unsigned lifetime_default = CCN_INTEREST_LIFETIME_SEC << 12;
213 unsigned lifetime_l12 = lifetime_default;
214 int get_flags = 0;
akmhoquea0e71152013-02-11 09:47:59 -0600215
Obaid Amin806df812013-02-21 13:30:26 -0600216 name = ccn_charbuf_create();
217 res = ccn_name_from_uri(name,content_name);
218 if (res < 0) {
219 fprintf(stderr, "Bad ccn URI: %s\n", content_name);
220 exit(1);
221 }
akmhoquea0e71152013-02-11 09:47:59 -0600222
Obaid Amin806df812013-02-21 13:30:26 -0600223 if (allow_stale || lifetime_l12 != lifetime_default || scope != -1) {
224 templ = ccn_charbuf_create();
225 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
226 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
227 ccn_charbuf_append_closer(templ); /* </Name> */
228 if (allow_stale) {
229 ccn_charbuf_append_tt(templ, CCN_DTAG_AnswerOriginKind, CCN_DTAG);
230 ccnb_append_number(templ,
231 CCN_AOK_DEFAULT | CCN_AOK_STALE);
232 ccn_charbuf_append_closer(templ); /* </AnswerOriginKind> */
233 }
234 if (scope != -1) {
235 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
236 }
237 if (lifetime_l12 != lifetime_default) {
238 /*
239 * Choose the interest lifetime so there are at least 3
240 * expressions (in the unsatisfied case).
241 */
242 unsigned char buf[3] = { 0 };
243 int i;
244 for (i = sizeof(buf) - 1; i >= 0; i--, lifetime_l12 >>= 8)
245 buf[i] = lifetime_l12 & 0xff;
246 ccnb_append_tagged_blob(templ, CCN_DTAG_InterestLifetime, buf,
247 sizeof(buf));
248 }
249 ccn_charbuf_append_closer(templ); /* </Interest> */
akmhoquea0e71152013-02-11 09:47:59 -0600250 }
Obaid Amin806df812013-02-21 13:30:26 -0600251 resultbuf = ccn_charbuf_create();
252 if (resolve_version != 0) {
253 res = ccn_resolve_version(nlsr->ccn, name, resolve_version, 500);
254 if (res >= 0) {
255 ccn_uri_append(resultbuf, name->buf, name->length, 1);
256 resultbuf->length = 0;
257 }
akmhoquea0e71152013-02-11 09:47:59 -0600258 }
Obaid Amin806df812013-02-21 13:30:26 -0600259 res = ccn_get(nlsr->ccn, name, templ, timeout_ms, resultbuf, &pcobuf, NULL,
260 get_flags);
akmhoquea0e71152013-02-11 09:47:59 -0600261 if (res >= 0) {
Obaid Amin806df812013-02-21 13:30:26 -0600262 ptr = resultbuf->buf;
263 length = resultbuf->length;
264 if (content_only){
265 ccn_content_get_value(ptr, length, &pcobuf, &ptr, &length);
akmhoque2f1d11e2013-02-24 12:06:52 -0600266 *content_data = (unsigned char *) calloc(length, sizeof(char *));
Obaid Amin806df812013-02-21 13:30:26 -0600267 memcpy (*content_data, ptr, length);
268 }
akmhoquea0e71152013-02-11 09:47:59 -0600269 }
Obaid Amin806df812013-02-21 13:30:26 -0600270 ccn_charbuf_destroy(&resultbuf);
271 ccn_charbuf_destroy(&templ);
272 ccn_charbuf_destroy(&name);
akmhoquea0e71152013-02-11 09:47:59 -0600273}
274
Obaid Amin806df812013-02-21 13:30:26 -0600275 void
akmhoquea0e71152013-02-11 09:47:59 -0600276process_incoming_sync_content_lsa( unsigned char *content_data)
277{
278
279
Obaid Amin806df812013-02-21 13:30:26 -0600280 if ( nlsr->debugging )
281 printf("process_incoming_sync_content_lsa called \n");
Obaid Aminbb8ac422013-02-20 17:08:48 -0600282
Obaid Amin806df812013-02-21 13:30:26 -0600283 char *sep="|";
284 char *rem;
285 char *orig_router;
286 char *orl;
287 int orig_router_length;
288 char *lst;
289 int ls_type;
290 char *lsid;
291 long int ls_id;
292 char *isvld;
293 int isValid;
294 char *num_link;
295 int no_link;
296 char *np;
297 char *np_length;
298 int name_length;
299 char *data;
300 char *orig_time;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600301
302
akmhoquea0e71152013-02-11 09:47:59 -0600303 if ( nlsr->debugging )
Obaid Amin806df812013-02-21 13:30:26 -0600304 printf("LSA Data \n");
305
306 if( strlen((char *)content_data ) > 0 )
akmhoquea0e71152013-02-11 09:47:59 -0600307 {
Obaid Aminbb8ac422013-02-20 17:08:48 -0600308
Obaid Amin806df812013-02-21 13:30:26 -0600309 orig_router=strtok_r((char *)content_data,sep,&rem);
310 orl=strtok_r(NULL,sep,&rem);
311 orig_router_length=atoi(orl);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600312
Obaid Amin806df812013-02-21 13:30:26 -0600313 if ( nlsr->debugging )
314 {
315 printf(" Orig Router Name : %s\n",orig_router);
316 printf(" Orig Router Length: %d\n",orig_router_length);
317 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600318
Obaid Amin806df812013-02-21 13:30:26 -0600319 lst=strtok_r(NULL,sep,&rem);
320 ls_type=atoi(lst);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600321
Obaid Amin806df812013-02-21 13:30:26 -0600322 if ( nlsr->debugging )
323 printf(" LS Type : %d\n",ls_type);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600324
Obaid Amin806df812013-02-21 13:30:26 -0600325 if ( ls_type == LS_TYPE_NAME )
326 {
327 lsid=strtok_r(NULL,sep,&rem);
328 ls_id=atoi(lsid);
329 orig_time=strtok_r(NULL,sep,&rem);
330 isvld=strtok_r(NULL,sep,&rem);
331 isValid=atoi(isvld);
332 np=strtok_r(NULL,sep,&rem);
333 np_length=strtok_r(NULL,sep,&rem);
334 name_length=atoi(np_length);
335 if ( nlsr->debugging )
336 {
337 printf(" LS ID : %ld\n",ls_id);
338 printf(" isValid : %d\n",isValid);
339 printf(" Name Prefix : %s\n",np);
340 printf(" Orig Time : %s\n",orig_time);
341 printf(" Name Prefix length: %d\n",name_length);
342 }
343
344 build_and_install_others_name_lsa(orig_router,ls_type,ls_id,orig_time,isValid,np);
345
346 print_name_lsdb();
347
348 }
349 else if ( ls_type == LS_TYPE_ADJ )
350 {
351 orig_time=strtok_r(NULL,sep,&rem);
352 num_link=strtok_r(NULL,sep,&rem);
353 no_link=atoi(num_link);
354 data=rem;
355
356 if ( nlsr->debugging )
357 {
358 printf(" Orig Time : %s\n",orig_time);
359 printf(" No Link : %d\n",no_link);
360 printf(" Data : %s\n",data);
361 }
362 build_and_install_others_adj_lsa(orig_router,ls_type,orig_time,no_link,data);
363 }
364 else if ( ls_type == LS_TYPE_COR )
365 {
366 orig_time=strtok_r(NULL,sep,&rem);
367 char *cor_r=strtok_r(NULL,sep,&rem);
368 char *cor_theta=strtok_r(NULL,sep,&rem);
369
370 double r, theta;
akmhoque8034d502013-02-24 11:53:24 -0600371 r=strtod(cor_r,NULL);
372 theta=strtod(cor_theta,NULL);
Obaid Amin806df812013-02-21 13:30:26 -0600373
374 if ( nlsr->debugging )
375 {
376 printf(" Orig Time : %s\n",orig_time);
377 printf(" Cor R : %f\n",r);
378 printf(" Cor Theta : %f\n",theta);
379 }
380 build_and_install_others_cor_lsa(orig_router,ls_type,orig_time, (double)r, (double)theta);
381 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600382
383 }
akmhoquea0e71152013-02-11 09:47:59 -0600384}
385
Obaid Amin806df812013-02-21 13:30:26 -0600386 void
Obaid Amin7997ad82013-02-22 16:41:56 -0600387process_content_from_sync (struct ccn_charbuf *content_name,
388 struct ccn_indexbuf *components)
akmhoquea0e71152013-02-11 09:47:59 -0600389{
akmhoque8c5e2662013-02-24 06:37:14 -0600390 if (nlsr->debugging)
391 printf("process_content_from_sync called \n");
Obaid Amin806df812013-02-21 13:30:26 -0600392 size_t comp_size;
393 char *lst;
394 char *lsid;
395 const unsigned char *second_last_comp;
396 const unsigned char *third_last_comp;
397 const unsigned char *origtime;
398 char *sep=".";
399 char *rem;
400 char *second_comp_type;
akmhoquea0e71152013-02-11 09:47:59 -0600401
Obaid Amin806df812013-02-21 13:30:26 -0600402 int ls_type;
403 long int ls_id=0;
akmhoquea0e71152013-02-11 09:47:59 -0600404
Obaid Amin806df812013-02-21 13:30:26 -0600405 unsigned char *content_data = NULL;
akmhoquea0e71152013-02-11 09:47:59 -0600406
akmhoqueb31caa12013-02-22 08:42:09 -0600407 char *time_stamp=get_current_timestamp_micro_v2();
akmhoquea0e71152013-02-11 09:47:59 -0600408
Obaid Amin806df812013-02-21 13:30:26 -0600409 struct ccn_charbuf *uri = ccn_charbuf_create();
410 ccn_uri_append(uri, content_name->buf, content_name->length, 0);
akmhoquea0e71152013-02-11 09:47:59 -0600411
Obaid Amin7997ad82013-02-22 16:41:56 -0600412 struct name_prefix *orig_router=(struct name_prefix *)
413 calloc( 1, sizeof(struct name_prefix));
akmhoquea0e71152013-02-11 09:47:59 -0600414
Obaid Amin7997ad82013-02-22 16:41:56 -0600415 ccn_name_comp_get( content_name->buf, components,
416 components->n-1-2, &second_last_comp, &comp_size);
417
Obaid Amin806df812013-02-21 13:30:26 -0600418 if (nlsr->debugging)
Obaid Amin7997ad82013-02-22 16:41:56 -0600419 printf("2nd Last Component: %s \n", second_last_comp);
akmhoquea0e71152013-02-11 09:47:59 -0600420
Obaid Amin7997ad82013-02-22 16:41:56 -0600421 second_comp_type=strtok_r((char *)second_last_comp, sep, &rem);
422 if (second_comp_type == NULL || rem == NULL)
423 {
424 printf ("Error: unable to tokenize the string: %s, calling exit()\n",
425 (char *)second_last_comp);
426 exit(0);
427 }
428
Obaid Amin806df812013-02-21 13:30:26 -0600429 if ( strcmp( second_comp_type, "lsId" ) == 0 )
430 {
431 lsid=rem;
432 ls_id=atoi(rem);
433 ccn_name_comp_get(content_name->buf, components,components->n-2-2,&third_last_comp, &comp_size);
434 lst=strtok_r((char *)third_last_comp,sep,&rem);
435 lst=rem;
436 ls_type=atoi(lst);
437 ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size);
438 ccn_name_chop(content_name,components,-3);
439 get_name_part(orig_router,content_name,components,0);
akmhoquea0e71152013-02-11 09:47:59 -0600440
Obaid Aminbb8ac422013-02-20 17:08:48 -0600441 if ( nlsr->debugging )
Obaid Amin806df812013-02-21 13:30:26 -0600442 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 -0600443
Obaid Amin806df812013-02-21 13:30:26 -0600444 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque8c5e2662013-02-24 06:37:14 -0600445 if ( nlsr->debugging )
446 printf("LSA Life time: %d\n",lsa_life_time);
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 Amin4b0e4ec2013-02-24 22:15:01 -0600483 ccn_name_comp_get(content_name->buf, components,components->n-2,
484 &origtime, &comp_size);
Obaid Amin806df812013-02-21 13:30:26 -0600485 ccn_name_chop(content_name,components,-2);
486 get_name_part(orig_router,content_name,components,0);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600487
Obaid Amin806df812013-02-21 13:30:26 -0600488 if ( nlsr->debugging )
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600489 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",
490 orig_router->name,ls_type,origtime);
Obaid Amin806df812013-02-21 13:30:26 -0600491
492 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque8c5e2662013-02-24 06:37:14 -0600493 if ( nlsr->debugging )
494 printf("LSA Life time: %d\n",lsa_life_time);
495
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600496 if ( (strcmp(orig_router->name,nlsr->router_name) == 0 &&
497 lsa_life_time < nlsr->lsa_refresh_time) ||
498 (strcmp(orig_router->name,nlsr->router_name) != 0 &&
499 lsa_life_time < nlsr->router_dead_interval) )
Obaid Amin806df812013-02-21 13:30:26 -0600500 {
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600501 int is_new_adj_lsa = check_is_new_adj_lsa( orig_router->name,
502 (char *)lst, (char *)origtime);
Obaid Amin806df812013-02-21 13:30:26 -0600503 if ( is_new_adj_lsa == 1 )
504 {
505 if ( nlsr->debugging )
506 printf("New Adj LSA.....\n");
507 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
508
509 if ( nlsr->debugging )
510 printf("Content Data: %s \n",content_data);
511 process_incoming_sync_content_lsa(content_data);
512 }
513 else
514 {
515 if ( nlsr->debugging )
516 printf("Adj LSA / Newer Adj LSA already exists in LSDB\n");
517 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
518 if ( nlsr->debugging )
519 printf("Content Data: %s \n",content_data);
520 }
521 }
522 else
523 {
524 if ( nlsr->debugging )
525 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
526 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600527 }
Obaid Amin806df812013-02-21 13:30:26 -0600528 else if(ls_type == LS_TYPE_COR)
Obaid Aminbb8ac422013-02-20 17:08:48 -0600529 {
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600530 ccn_name_comp_get(content_name->buf, components, components->n-2,
531 &origtime, &comp_size);
Obaid Amin806df812013-02-21 13:30:26 -0600532 ccn_name_chop(content_name,components,-2);
533 get_name_part(orig_router,content_name,components,0);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600534
Obaid Amin806df812013-02-21 13:30:26 -0600535 if ( nlsr->debugging )
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600536 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",
537 orig_router->name,ls_type,origtime);
Obaid Amin806df812013-02-21 13:30:26 -0600538
539 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque8c5e2662013-02-24 06:37:14 -0600540 if ( nlsr->debugging )
541 printf("LSA Life time: %d\n",lsa_life_time);
542
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600543 if ( (strcmp(orig_router->name,nlsr->router_name) == 0 &&
544 lsa_life_time < nlsr->lsa_refresh_time) ||
545 (strcmp(orig_router->name,nlsr->router_name) != 0 &&
546 lsa_life_time < nlsr->router_dead_interval) )
Obaid Amin806df812013-02-21 13:30:26 -0600547 {
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600548 int is_new_cor_lsa=check_is_new_cor_lsa( orig_router->name,
549 (char *)lst,(char *) origtime);
Obaid Amin806df812013-02-21 13:30:26 -0600550 if ( is_new_cor_lsa == 1 )
551 {
552 if ( nlsr->debugging )
553 printf("New Cor LSA.....\n");
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600554 get_content_by_content_name(ccn_charbuf_as_string(uri),
555 &content_data);
Obaid Amin806df812013-02-21 13:30:26 -0600556
557 if ( nlsr->debugging )
558 printf("Content Data: %s \n",content_data);
559 process_incoming_sync_content_lsa(content_data);
560 }
561 else
562 {
563 if ( nlsr->debugging )
564 printf("Cor LSA / Newer Cor LSA already exists in LSDB\n");
565 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
566 if ( nlsr->debugging )
567 printf("Content Data: %s \n",content_data);
568 }
569 }
570 else
571 {
572 if ( nlsr->debugging )
573 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
574 }
575
576 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600577 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600578
akmhoquef6773612013-02-23 09:53:00 -0600579 if (orig_router->name)
580 free(orig_router->name);
Obaid Amin05300892013-02-21 13:45:25 -0600581 if (orig_router)
582 free(orig_router);
Obaid Amin806df812013-02-21 13:30:26 -0600583 ccn_charbuf_destroy(&uri);
584 //01/31/2013
585 free(time_stamp);
akmhoquea0e71152013-02-11 09:47:59 -0600586}
587
Obaid Amin7793ba72013-02-22 00:43:58 -0600588 int
akmhoquea0e71152013-02-11 09:47:59 -0600589sync_monitor(char *topo_prefix, char *slice_prefix)
590{
591
Obaid Amin806df812013-02-21 13:30:26 -0600592 struct ccn_charbuf *prefix = ccn_charbuf_create();
Obaid Amin806df812013-02-21 13:30:26 -0600593 struct ccn_charbuf *topo = ccn_charbuf_create();
Obaid Amin7997ad82013-02-22 16:41:56 -0600594
595 nlsr->closure=(struct ccns_name_closure *)
akmhoquececba942013-02-25 17:33:34 -0600596 calloc(1,sizeof(struct ccns_name_closure)); // leak
Obaid Amin7997ad82013-02-22 16:41:56 -0600597
Obaid Amin806df812013-02-21 13:30:26 -0600598 nlsr->slice = ccns_slice_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600599
Obaid Amin806df812013-02-21 13:30:26 -0600600 ccn_charbuf_reset(prefix);
601 ccn_name_from_uri(prefix, slice_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600602
Obaid Amin806df812013-02-21 13:30:26 -0600603 ccn_charbuf_reset(topo);
604 ccn_name_from_uri(topo, topo_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600605
Obaid Amin806df812013-02-21 13:30:26 -0600606 ccns_slice_set_topo_prefix(nlsr->slice, topo, prefix);
607 nlsr->closure->callback = &sync_cb;
akmhoque54d86112013-02-21 16:42:34 -0600608 nlsr->ccns = ccns_open(nlsr->ccn, nlsr->slice, nlsr->closure, NULL, NULL);
akmhoquea0e71152013-02-11 09:47:59 -0600609
Obaid Amin7997ad82013-02-22 16:41:56 -0600610 ccn_charbuf_destroy(&prefix);
611 ccn_charbuf_destroy(&topo);
Obaid Amin806df812013-02-21 13:30:26 -0600612 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600613}
614
Obaid Amin7793ba72013-02-22 00:43:58 -0600615 struct ccn_charbuf *
akmhoquea0e71152013-02-11 09:47:59 -0600616make_template(int scope)
617{
Obaid Amin806df812013-02-21 13:30:26 -0600618 struct ccn_charbuf *templ = NULL;
619 templ = ccn_charbuf_create();
620 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
621 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
622 ccn_charbuf_append_closer(templ); /* </Name> */
623 if (0 <= scope && scope <= 2)
624 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
625 ccn_charbuf_append_closer(templ); /* </Interest> */
626 return(templ);
akmhoquea0e71152013-02-11 09:47:59 -0600627}
628
Obaid Amin7793ba72013-02-22 00:43:58 -0600629 int
akmhoquea0e71152013-02-11 09:47:59 -0600630write_data_to_repo(char *data, char *name_prefix)
631{
Obaid Amin806df812013-02-21 13:30:26 -0600632 if ( nlsr->debugging )
633 {
634 printf("write_data_to_repo called\n");
635 printf("Content Name: %s \n",name_prefix);
636 printf("Content Data: %s \n",data);
637 }
akmhoquea0e71152013-02-11 09:47:59 -0600638
Obaid Aminf210a322013-02-22 02:04:52 -0600639 struct ccn *temp_ccn;
Obaid Amin806df812013-02-21 13:30:26 -0600640 temp_ccn=ccn_create();
641 int ccn_fd=ccn_connect(temp_ccn, NULL);
642 if(ccn_fd == -1)
643 {
644 fprintf(stderr,"Could not connect to ccnd for Data Writing\n");
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600645 writeLogg(__FILE__,__FUNCTION__,__LINE__,
646 "Could not connect to ccnd for Data Writing\n");
Obaid Amin806df812013-02-21 13:30:26 -0600647 return -1;
648 }
Obaid Aminf210a322013-02-22 02:04:52 -0600649
Obaid Amin806df812013-02-21 13:30:26 -0600650 struct ccn_charbuf *name = NULL;
651 struct ccn_seqwriter *w = NULL;
652 int blocksize = 4096;
653 int freshness = -1;
Obaid Amin806df812013-02-21 13:30:26 -0600654 int scope = 1;
655 int res;
656 size_t blockread;
657 struct ccn_charbuf *templ;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600658
Obaid Amin806df812013-02-21 13:30:26 -0600659 name = ccn_charbuf_create();
660 res = ccn_name_from_uri(name, name_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600661 if (res < 0) {
Obaid Amin806df812013-02-21 13:30:26 -0600662 fprintf(stderr, "bad CCN URI: %s\n",name_prefix);
663 return -1;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600664 }
Obaid Amin806df812013-02-21 13:30:26 -0600665
Obaid Aminf210a322013-02-22 02:04:52 -0600666 w = ccn_seqw_create(temp_ccn, name);
Obaid Amin806df812013-02-21 13:30:26 -0600667 if (w == NULL) {
668 fprintf(stderr, "ccn_seqw_create failed\n");
669 return -1;
670 }
671 ccn_seqw_set_block_limits(w, blocksize, blocksize);
672 if (freshness > -1)
673 ccn_seqw_set_freshness(w, freshness);
akmhoquea0e71152013-02-11 09:47:59 -0600674
Obaid Amin7997ad82013-02-22 16:41:56 -0600675 struct ccn_charbuf *name_v = ccn_charbuf_create();
676 ccn_seqw_get_name(w, name_v);
677 ccn_name_from_uri(name_v, "%C1.R.sw");
678 ccn_name_append_nonce(name_v);
679 templ = make_template(scope);
680 res = ccn_get(temp_ccn, name_v, templ, 60000, NULL, NULL, NULL, 0);
681 ccn_charbuf_destroy(&templ);
682 ccn_charbuf_destroy(&name_v);
683 if (res < 0) {
684 fprintf(stderr, "No response from repository\n");
685 return -1;
686 }
akmhoquea0e71152013-02-11 09:47:59 -0600687
Obaid Amin806df812013-02-21 13:30:26 -0600688 blockread = 0;
akmhoquea0e71152013-02-11 09:47:59 -0600689
akmhoquee407fc22013-02-24 11:57:17 -0600690 blockread=strlen(data)+1;
akmhoquea0e71152013-02-11 09:47:59 -0600691
Obaid Amin806df812013-02-21 13:30:26 -0600692 if (blockread > 0) {
Obaid Amin806df812013-02-21 13:30:26 -0600693 res = ccn_seqw_write(w, data, blockread);
694 while (res == -1) {
akmhoque8034d502013-02-24 11:53:24 -0600695 ccn_run(temp_ccn,100);
Obaid Amin806df812013-02-21 13:30:26 -0600696 res = ccn_seqw_write(w, data, blockread);
697 }
698 }
699
akmhoque8034d502013-02-24 11:53:24 -0600700 ccn_seqw_close(w);
701 ccn_run(temp_ccn, 100);
Obaid Amin806df812013-02-21 13:30:26 -0600702 ccn_charbuf_destroy(&name);
akmhoque0ed6d982013-02-22 14:28:01 -0600703 ccn_destroy(&temp_ccn);
akmhoquea0e71152013-02-11 09:47:59 -0600704
Obaid Amin806df812013-02-21 13:30:26 -0600705 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600706}
Obaid Amin806df812013-02-21 13:30:26 -0600707 int
Obaid Amin7997ad82013-02-22 16:41:56 -0600708 create_sync_slice(char *topo_prefix, char *slice_prefix)
akmhoquea0e71152013-02-11 09:47:59 -0600709{
Obaid Amin806df812013-02-21 13:30:26 -0600710 int res;
akmhoque0ed6d982013-02-22 14:28:01 -0600711 struct ccn *handle; //obaid: probably we don't need it use the same handle i.e. nlsr->ccn
Obaid Amin806df812013-02-21 13:30:26 -0600712 struct ccns_slice *slice;
713 struct ccn_charbuf *prefix = ccn_charbuf_create();
714 struct ccn_charbuf *topo = ccn_charbuf_create();
715 struct ccn_charbuf *clause = ccn_charbuf_create();
716 struct ccn_charbuf *slice_name = ccn_charbuf_create();
717 struct ccn_charbuf *slice_uri = ccn_charbuf_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600718
Obaid Amin806df812013-02-21 13:30:26 -0600719 if (prefix == NULL || topo == NULL || clause == NULL ||
720 slice_name == NULL || slice_uri == NULL) {
721 fprintf(stderr, "Unable to allocate required memory.\n");
722 return -1;
723 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600724
akmhoque0ed6d982013-02-22 14:28:01 -0600725 handle = ccn_create();
Obaid Amin806df812013-02-21 13:30:26 -0600726 res = ccn_connect(handle, NULL);
727 if (0 > res) {
728 fprintf(stderr, "Unable to connect to ccnd.\n");
729 return -1;
730 }
akmhoque0ed6d982013-02-22 14:28:01 -0600731
Obaid Amin806df812013-02-21 13:30:26 -0600732 slice = ccns_slice_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600733
Obaid Amin806df812013-02-21 13:30:26 -0600734 ccn_charbuf_reset(topo);
735 ccn_name_from_uri(topo, topo_prefix);
736 ccn_charbuf_reset(prefix);
737 ccn_name_from_uri(prefix,slice_prefix );
738 ccns_slice_set_topo_prefix(slice, topo, prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600739
740
akmhoque0ed6d982013-02-22 14:28:01 -0600741 res = ccns_write_slice(handle, slice, slice_name);
742 //res = ccns_write_slice(nlsr->ccn, slice, slice_name);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600743
Obaid Amin806df812013-02-21 13:30:26 -0600744 //01/31/2013
745 ccns_slice_destroy(&slice);
akmhoque0ed6d982013-02-22 14:28:01 -0600746 ccn_destroy(&handle);
Obaid Amin806df812013-02-21 13:30:26 -0600747 ccn_charbuf_destroy(&prefix);
748 ccn_charbuf_destroy(&topo);
749 ccn_charbuf_destroy(&clause);
750 ccn_charbuf_destroy(&slice_name);
751 ccn_charbuf_destroy(&slice_uri);
akmhoquea0e71152013-02-11 09:47:59 -0600752
Obaid Amin806df812013-02-21 13:30:26 -0600753 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600754}
755
akmhoque0eb8de22013-02-24 14:08:19 -0600756