blob: fd884cb1c5d40e605f8f598151ef46ee8a1d4ba2 [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"
akmhoqueedb68d92013-03-05 10:18:16 -060032#include "nlsr_km.h"
akmhoque7adb2772013-03-05 16:30:59 -060033#include "nlsr_km_util.h"
akmhoquea0e71152013-02-11 09:47:59 -060034
35
akmhoqueedb68d92013-03-05 10:18:16 -060036char *
akmhoquea0e71152013-02-11 09:47:59 -060037hex_string(unsigned char *s, size_t l)
38{
Obaid Amin806df812013-02-21 13:30:26 -060039 const char *hex_digits = "0123456789abcdef";
40 char *r;
41 int i;
42 r = calloc(1, 1 + 2 * l);
43 for (i = 0; i < l; i++) {
44 r[2*i] = hex_digits[(s[i]>>4) & 0xf];
45 r[1+2*i] = hex_digits[s[i] & 0xf];
46 }
47 return(r);
akmhoquea0e71152013-02-11 09:47:59 -060048}
49
akmhoqueedb68d92013-03-05 10:18:16 -060050int
akmhoquea0e71152013-02-11 09:47:59 -060051sync_cb(struct ccns_name_closure *nc,
Obaid Amin806df812013-02-21 13:30:26 -060052 struct ccn_charbuf *lhash,
53 struct ccn_charbuf *rhash,
54 struct ccn_charbuf *name)
akmhoquea0e71152013-02-11 09:47:59 -060055{
Obaid Amin806df812013-02-21 13:30:26 -060056 int res;
Obaid Amin7997ad82013-02-22 16:41:56 -060057 struct ccn_charbuf *content_name;
58 struct ccn_indexbuf *content_comps;
59 struct ccn_indexbuf *name_comps;
60
61 content_comps = ccn_indexbuf_create();
62 res = ccn_name_split(name, content_comps);
Obaid Amin806df812013-02-21 13:30:26 -060063 if ( res < 0 )
64 return 0;
Obaid Amin7997ad82013-02-22 16:41:56 -060065
66 if (content_comps->n < 2)
Obaid Amin806df812013-02-21 13:30:26 -060067 return 0;
Obaid Amin7997ad82013-02-22 16:41:56 -060068
69 content_name = ccn_charbuf_create();
70 ccn_name_init(content_name);
71
72 res = ccn_name_append_components( content_name, name->buf,
73 content_comps->buf[0], content_comps->buf[content_comps->n - 1]);
Obaid Amin806df812013-02-21 13:30:26 -060074
75 if ( res < 0)
76 return 0;
akmhoquea0e71152013-02-11 09:47:59 -060077
Obaid Amin7997ad82013-02-22 16:41:56 -060078 // for debugging
Obaid Amin806df812013-02-21 13:30:26 -060079 struct ccn_charbuf *temp=ccn_charbuf_create();
80 ccn_uri_append(temp, content_name->buf, content_name->length, 0);
81 if ( nlsr->debugging )
82 printf("Name before chopping: %s \n",ccn_charbuf_as_string(temp));
83 ccn_charbuf_destroy(&temp);
akmhoquea0e71152013-02-11 09:47:59 -060084
Obaid Amin7997ad82013-02-22 16:41:56 -060085 name_comps = ccn_indexbuf_create();
86 res=ccn_name_split (content_name, name_comps);
87 if (res < 0)
Obaid Amin806df812013-02-21 13:30:26 -060088 return 0;
akmhoquea0e71152013-02-11 09:47:59 -060089
Obaid Amin806df812013-02-21 13:30:26 -060090 if ( nlsr->debugging )
91 {
92 printf("Number of components in name = %d \n",res);
93 printf("Number of components in name as indexbuf->n = %d \n",
Obaid Amin7997ad82013-02-22 16:41:56 -060094 (int)name_comps->n);
Obaid Amin806df812013-02-21 13:30:26 -060095 }
Obaid Amin7997ad82013-02-22 16:41:56 -060096
97 ccn_name_chop(content_name, name_comps, -3);
Obaid Amin806df812013-02-21 13:30:26 -060098 if ( nlsr->debugging )
99 printf("Number of components in name as indexbuf->n after chopping= %d \n"
Obaid Amin7997ad82013-02-22 16:41:56 -0600100 , (int)name_comps->n);
akmhoquea0e71152013-02-11 09:47:59 -0600101
Obaid Amin7997ad82013-02-22 16:41:56 -0600102 //for debugging
Obaid Amin806df812013-02-21 13:30:26 -0600103 struct ccn_charbuf *temp1=ccn_charbuf_create();
104 ccn_uri_append(temp1, content_name->buf, content_name->length, 0);
105 if ( nlsr->debugging )
106 printf("Name after chopping: %s \n",ccn_charbuf_as_string(temp1));
107 ccn_charbuf_destroy(&temp1);
akmhoquea0e71152013-02-11 09:47:59 -0600108
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600109 //main method that processes contents from the sync
Obaid Amin7997ad82013-02-22 16:41:56 -0600110 process_content_from_sync(content_name, name_comps);
111
Obaid Amin806df812013-02-21 13:30:26 -0600112 ccn_charbuf_destroy(&content_name);
Obaid Amin7997ad82013-02-22 16:41:56 -0600113 ccn_indexbuf_destroy(&content_comps);
114 ccn_indexbuf_destroy(&name_comps);
akmhoquea0e71152013-02-11 09:47:59 -0600115
Obaid Amin806df812013-02-21 13:30:26 -0600116 return(0);
akmhoquea0e71152013-02-11 09:47:59 -0600117}
118
119
akmhoqueccb33e92013-02-20 11:44:28 -0600120
akmhoqueedb68d92013-03-05 10:18:16 -0600121void
akmhoqueccb33e92013-02-20 11:44:28 -0600122get_name_part(struct name_prefix *name_part,struct ccn_charbuf * interest_ccnb,
Obaid Amin806df812013-02-21 13:30:26 -0600123 struct ccn_indexbuf *interest_comps, int offset)
akmhoquea0e71152013-02-11 09:47:59 -0600124{
Obaid Amin806df812013-02-21 13:30:26 -0600125 int lsa_position=0;
akmhoqueedb68d92013-03-05 10:18:16 -0600126
akmhoque0b60ba92013-02-25 17:55:35 -0600127 struct ccn_indexbuf *components=ccn_indexbuf_create();
Obaid Amin806df812013-02-21 13:30:26 -0600128 struct ccn_charbuf *name=ccn_charbuf_create();
129 ccn_name_from_uri(name,nlsr->slice_prefix);
130 ccn_name_split (name, components);
131 lsa_position=components->n-2;
Obaid Amin806df812013-02-21 13:30:26 -0600132 ccn_charbuf_destroy(&name);
akmhoquea0e71152013-02-11 09:47:59 -0600133
akmhoquef6773612013-02-23 09:53:00 -0600134 struct ccn_charbuf *temp=ccn_charbuf_create();
135 ccn_name_init(temp);
136 ccn_name_append_components( temp, interest_ccnb->buf,
akmhoque7adb2772013-03-05 16:30:59 -0600137 interest_comps->buf[lsa_position+1],
138 interest_comps->buf[interest_comps->n - 1]);
akmhoquef6773612013-02-23 09:53:00 -0600139
140 struct ccn_charbuf *temp1=ccn_charbuf_create();
141 ccn_uri_append(temp1, temp->buf, temp->length, 0);
142
akmhoque7adb2772013-03-05 16:30:59 -0600143 name_part->name=(char *)calloc(strlen(ccn_charbuf_as_string(temp1))+1,
144 sizeof(char));
145 memcpy(name_part->name,ccn_charbuf_as_string(temp1),
146 strlen(ccn_charbuf_as_string(temp1)));
akmhoquef6773612013-02-23 09:53:00 -0600147 name_part->name[strlen(ccn_charbuf_as_string(temp1))]='\0';
148 name_part->length=strlen(ccn_charbuf_as_string(temp1))+1;
149
150 ccn_charbuf_destroy(&temp1);
151 ccn_charbuf_destroy(&temp);
akmhoque0b60ba92013-02-25 17:55:35 -0600152 ccn_indexbuf_destroy(&components);
akmhoquef6773612013-02-23 09:53:00 -0600153
154 if ( nlsr->debugging )
155 printf("Name Part: %s \n",name_part->name);
156
akmhoquea0e71152013-02-11 09:47:59 -0600157}
158
akmhoquea0e71152013-02-11 09:47:59 -0600159
akmhoquea0e71152013-02-11 09:47:59 -0600160
161
162
akmhoque7adb2772013-03-05 16:30:59 -0600163int
164get_content_by_content_name(char *content_name, unsigned char **content_data,
165 char *orig_router)
akmhoquea0e71152013-02-11 09:47:59 -0600166{
akmhoque7adb2772013-03-05 16:30:59 -0600167
168 int ret=-1;
Obaid Amin806df812013-02-21 13:30:26 -0600169 struct ccn_charbuf *name = NULL;
170 struct ccn_charbuf *templ = NULL;
171 struct ccn_charbuf *resultbuf = NULL;
172 struct ccn_parsed_ContentObject pcobuf = { 0 };
173 int res;
akmhoque7adb2772013-03-05 16:30:59 -0600174 int allow_stale = 1;
Obaid Amin806df812013-02-21 13:30:26 -0600175 int content_only = 1;
176 int scope = -1;
akmhoque7adb2772013-03-05 16:30:59 -0600177 const unsigned char *ptr,*ptr_in;
178 size_t length,length_in;
Obaid Amin806df812013-02-21 13:30:26 -0600179 int resolve_version = CCN_V_HIGHEST;
180 int timeout_ms = 3000;
181 const unsigned lifetime_default = CCN_INTEREST_LIFETIME_SEC << 12;
182 unsigned lifetime_l12 = lifetime_default;
183 int get_flags = 0;
akmhoquea0e71152013-02-11 09:47:59 -0600184
Obaid Amin806df812013-02-21 13:30:26 -0600185 name = ccn_charbuf_create();
186 res = ccn_name_from_uri(name,content_name);
187 if (res < 0) {
188 fprintf(stderr, "Bad ccn URI: %s\n", content_name);
akmhoque7adb2772013-03-05 16:30:59 -0600189 ccn_charbuf_destroy(&name);
190 return ret;
Obaid Amin806df812013-02-21 13:30:26 -0600191 }
akmhoquea0e71152013-02-11 09:47:59 -0600192
Obaid Amin806df812013-02-21 13:30:26 -0600193 if (allow_stale || lifetime_l12 != lifetime_default || scope != -1) {
194 templ = ccn_charbuf_create();
195 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
196 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
197 ccn_charbuf_append_closer(templ); /* </Name> */
198 if (allow_stale) {
199 ccn_charbuf_append_tt(templ, CCN_DTAG_AnswerOriginKind, CCN_DTAG);
200 ccnb_append_number(templ,
201 CCN_AOK_DEFAULT | CCN_AOK_STALE);
202 ccn_charbuf_append_closer(templ); /* </AnswerOriginKind> */
203 }
204 if (scope != -1) {
205 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
206 }
207 if (lifetime_l12 != lifetime_default) {
208 /*
209 * Choose the interest lifetime so there are at least 3
210 * expressions (in the unsatisfied case).
211 */
212 unsigned char buf[3] = { 0 };
213 int i;
214 for (i = sizeof(buf) - 1; i >= 0; i--, lifetime_l12 >>= 8)
215 buf[i] = lifetime_l12 & 0xff;
216 ccnb_append_tagged_blob(templ, CCN_DTAG_InterestLifetime, buf,
217 sizeof(buf));
218 }
219 ccn_charbuf_append_closer(templ); /* </Interest> */
akmhoquea0e71152013-02-11 09:47:59 -0600220 }
Obaid Amin806df812013-02-21 13:30:26 -0600221 resultbuf = ccn_charbuf_create();
222 if (resolve_version != 0) {
223 res = ccn_resolve_version(nlsr->ccn, name, resolve_version, 500);
224 if (res >= 0) {
225 ccn_uri_append(resultbuf, name->buf, name->length, 1);
226 resultbuf->length = 0;
227 }
akmhoquea0e71152013-02-11 09:47:59 -0600228 }
Obaid Amin806df812013-02-21 13:30:26 -0600229 res = ccn_get(nlsr->ccn, name, templ, timeout_ms, resultbuf, &pcobuf, NULL,
230 get_flags);
akmhoquea0e71152013-02-11 09:47:59 -0600231 if (res >= 0) {
Obaid Amin806df812013-02-21 13:30:26 -0600232 ptr = resultbuf->buf;
233 length = resultbuf->length;
234 if (content_only){
235 ccn_content_get_value(ptr, length, &pcobuf, &ptr, &length);
akmhoque7adb2772013-03-05 16:30:59 -0600236 struct ccn_parsed_ContentObject pcobuf1 = { 0 };
237 int chk_cont=ccn_parse_ContentObject(ptr,length,&pcobuf1,NULL);
238 printf("Content Parsing result: %d\n",chk_cont);
239 if ( contain_key_name(ptr, &pcobuf1) == 1){
240 struct ccn_charbuf *key_name=get_key_name(ptr, &pcobuf1);
241 struct ccn_charbuf *orig_router_kn=ccn_charbuf_create();
242 res=get_orig_router_from_key_name(orig_router_kn,key_name);
243 if( res == 0){
244 struct ccn_charbuf *rtr_uri = ccn_charbuf_create();
245 ccn_uri_append(rtr_uri, orig_router_kn->buf,
246 orig_router_kn->length, 0);
247 printf("Orig Router from Key name: %s\n",
248 ccn_charbuf_as_string(rtr_uri));
249
250 if( strcmp(orig_router,ccn_charbuf_as_string(rtr_uri)) == 0){
251
252 int res_verify=verify_key(ptr,pcobuf1.offset[CCN_PCO_E],
253 &pcobuf1);
254
255 if ( res_verify != 0 ){
256 printf("Error in verfiying keys !! :( \n");
257 }
258 else{
259 printf("Key verification is successful :)\n");
260 ptr_in=ptr;
261 length_in=length;
262 ccn_content_get_value(ptr_in, length_in, &pcobuf1,
263 &ptr_in, &length_in);
264 *content_data = (unsigned char *) calloc(length_in,
265 sizeof(char *));
266 memcpy (*content_data, ptr_in, length_in);
267 ret=0;
268 }
269 }
270 ccn_charbuf_destroy(&rtr_uri);
271 }
272 ccn_charbuf_destroy(&key_name);
273 ccn_charbuf_destroy(&orig_router_kn);
274 }
Obaid Amin806df812013-02-21 13:30:26 -0600275 }
akmhoquea0e71152013-02-11 09:47:59 -0600276 }
akmhoque7adb2772013-03-05 16:30:59 -0600277
Obaid Amin806df812013-02-21 13:30:26 -0600278 ccn_charbuf_destroy(&resultbuf);
279 ccn_charbuf_destroy(&templ);
akmhoque7adb2772013-03-05 16:30:59 -0600280 ccn_charbuf_destroy(&name);
281
282 return ret;
akmhoquea0e71152013-02-11 09:47:59 -0600283}
284
akmhoqueedb68d92013-03-05 10:18:16 -0600285void
akmhoquea0e71152013-02-11 09:47:59 -0600286process_incoming_sync_content_lsa( unsigned char *content_data)
287{
288
289
Obaid Amin806df812013-02-21 13:30:26 -0600290 if ( nlsr->debugging )
291 printf("process_incoming_sync_content_lsa called \n");
Obaid Aminbb8ac422013-02-20 17:08:48 -0600292
Obaid Amin806df812013-02-21 13:30:26 -0600293 char *sep="|";
294 char *rem;
295 char *orig_router;
296 char *orl;
297 int orig_router_length;
298 char *lst;
299 int ls_type;
300 char *lsid;
301 long int ls_id;
302 char *isvld;
303 int isValid;
304 char *num_link;
305 int no_link;
306 char *np;
307 char *np_length;
308 int name_length;
309 char *data;
310 char *orig_time;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600311
312
akmhoquea0e71152013-02-11 09:47:59 -0600313 if ( nlsr->debugging )
Obaid Amin806df812013-02-21 13:30:26 -0600314 printf("LSA Data \n");
315
316 if( strlen((char *)content_data ) > 0 )
akmhoquea0e71152013-02-11 09:47:59 -0600317 {
Obaid Aminbb8ac422013-02-20 17:08:48 -0600318
Obaid Amin806df812013-02-21 13:30:26 -0600319 orig_router=strtok_r((char *)content_data,sep,&rem);
320 orl=strtok_r(NULL,sep,&rem);
321 orig_router_length=atoi(orl);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600322
Obaid Amin806df812013-02-21 13:30:26 -0600323 if ( nlsr->debugging )
324 {
325 printf(" Orig Router Name : %s\n",orig_router);
326 printf(" Orig Router Length: %d\n",orig_router_length);
327 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600328
Obaid Amin806df812013-02-21 13:30:26 -0600329 lst=strtok_r(NULL,sep,&rem);
330 ls_type=atoi(lst);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600331
Obaid Amin806df812013-02-21 13:30:26 -0600332 if ( nlsr->debugging )
333 printf(" LS Type : %d\n",ls_type);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600334
Obaid Amin806df812013-02-21 13:30:26 -0600335 if ( ls_type == LS_TYPE_NAME )
336 {
337 lsid=strtok_r(NULL,sep,&rem);
338 ls_id=atoi(lsid);
339 orig_time=strtok_r(NULL,sep,&rem);
340 isvld=strtok_r(NULL,sep,&rem);
341 isValid=atoi(isvld);
342 np=strtok_r(NULL,sep,&rem);
343 np_length=strtok_r(NULL,sep,&rem);
344 name_length=atoi(np_length);
345 if ( nlsr->debugging )
346 {
347 printf(" LS ID : %ld\n",ls_id);
348 printf(" isValid : %d\n",isValid);
349 printf(" Name Prefix : %s\n",np);
350 printf(" Orig Time : %s\n",orig_time);
351 printf(" Name Prefix length: %d\n",name_length);
352 }
353
akmhoque7adb2772013-03-05 16:30:59 -0600354 build_and_install_others_name_lsa(orig_router,ls_type,ls_id,
355 orig_time,isValid,np);
Obaid Amin806df812013-02-21 13:30:26 -0600356
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 }
akmhoque7adb2772013-03-05 16:30:59 -0600373 build_and_install_others_adj_lsa(orig_router,ls_type,orig_time,
374 no_link,data);
Obaid Amin806df812013-02-21 13:30:26 -0600375 }
376 else if ( ls_type == LS_TYPE_COR )
377 {
378 orig_time=strtok_r(NULL,sep,&rem);
379 char *cor_r=strtok_r(NULL,sep,&rem);
380 char *cor_theta=strtok_r(NULL,sep,&rem);
381
382 double r, theta;
akmhoque8034d502013-02-24 11:53:24 -0600383 r=strtod(cor_r,NULL);
384 theta=strtod(cor_theta,NULL);
Obaid Amin806df812013-02-21 13:30:26 -0600385
386 if ( nlsr->debugging )
387 {
388 printf(" Orig Time : %s\n",orig_time);
389 printf(" Cor R : %f\n",r);
390 printf(" Cor Theta : %f\n",theta);
391 }
akmhoque7adb2772013-03-05 16:30:59 -0600392 build_and_install_others_cor_lsa(orig_router,ls_type,orig_time,
393 (double)r, (double)theta);
Obaid Amin806df812013-02-21 13:30:26 -0600394 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600395
396 }
akmhoquea0e71152013-02-11 09:47:59 -0600397}
398
akmhoque7adb2772013-03-05 16:30:59 -0600399void
Obaid Amin7997ad82013-02-22 16:41:56 -0600400process_content_from_sync (struct ccn_charbuf *content_name,
401 struct ccn_indexbuf *components)
akmhoquea0e71152013-02-11 09:47:59 -0600402{
akmhoque8c5e2662013-02-24 06:37:14 -0600403 if (nlsr->debugging)
404 printf("process_content_from_sync called \n");
Obaid Amin806df812013-02-21 13:30:26 -0600405 size_t comp_size;
406 char *lst;
407 char *lsid;
408 const unsigned char *second_last_comp;
409 const unsigned char *third_last_comp;
410 const unsigned char *origtime;
411 char *sep=".";
412 char *rem;
413 char *second_comp_type;
akmhoquea0e71152013-02-11 09:47:59 -0600414
Obaid Amin806df812013-02-21 13:30:26 -0600415 int ls_type;
416 long int ls_id=0;
akmhoquea0e71152013-02-11 09:47:59 -0600417
Obaid Amin806df812013-02-21 13:30:26 -0600418 unsigned char *content_data = NULL;
akmhoquea0e71152013-02-11 09:47:59 -0600419
akmhoqueb31caa12013-02-22 08:42:09 -0600420 char *time_stamp=get_current_timestamp_micro_v2();
akmhoquea0e71152013-02-11 09:47:59 -0600421
Obaid Amin806df812013-02-21 13:30:26 -0600422 struct ccn_charbuf *uri = ccn_charbuf_create();
423 ccn_uri_append(uri, content_name->buf, content_name->length, 0);
akmhoquea0e71152013-02-11 09:47:59 -0600424
Obaid Amin7997ad82013-02-22 16:41:56 -0600425 struct name_prefix *orig_router=(struct name_prefix *)
426 calloc( 1, sizeof(struct name_prefix));
akmhoquea0e71152013-02-11 09:47:59 -0600427
Obaid Amin7997ad82013-02-22 16:41:56 -0600428 ccn_name_comp_get( content_name->buf, components,
429 components->n-1-2, &second_last_comp, &comp_size);
430
Obaid Amin806df812013-02-21 13:30:26 -0600431 if (nlsr->debugging)
Obaid Amin7997ad82013-02-22 16:41:56 -0600432 printf("2nd Last Component: %s \n", second_last_comp);
akmhoquea0e71152013-02-11 09:47:59 -0600433
Obaid Amin7997ad82013-02-22 16:41:56 -0600434 second_comp_type=strtok_r((char *)second_last_comp, sep, &rem);
435 if (second_comp_type == NULL || rem == NULL)
436 {
437 printf ("Error: unable to tokenize the string: %s, calling exit()\n",
438 (char *)second_last_comp);
439 exit(0);
440 }
441
Obaid Amin806df812013-02-21 13:30:26 -0600442 if ( strcmp( second_comp_type, "lsId" ) == 0 )
443 {
444 lsid=rem;
445 ls_id=atoi(rem);
akmhoque7adb2772013-03-05 16:30:59 -0600446 ccn_name_comp_get(content_name->buf, components,components->n-2-2,
447 &third_last_comp, &comp_size);
Obaid Amin806df812013-02-21 13:30:26 -0600448 lst=strtok_r((char *)third_last_comp,sep,&rem);
449 lst=rem;
450 ls_type=atoi(lst);
akmhoque7adb2772013-03-05 16:30:59 -0600451 ccn_name_comp_get(content_name->buf, components,components->n-2,
452 &origtime, &comp_size);
Obaid Amin806df812013-02-21 13:30:26 -0600453 ccn_name_chop(content_name,components,-3);
454 get_name_part(orig_router,content_name,components,0);
akmhoquea0e71152013-02-11 09:47:59 -0600455
Obaid Aminbb8ac422013-02-20 17:08:48 -0600456 if ( nlsr->debugging )
akmhoque7adb2772013-03-05 16:30:59 -0600457 printf("Orig Router: %s Ls Type: %d Ls id: %ld Orig Time: %s\n",
458 orig_router->name,ls_type,ls_id,origtime);
akmhoquea0e71152013-02-11 09:47:59 -0600459
Obaid Amin806df812013-02-21 13:30:26 -0600460 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque8c5e2662013-02-24 06:37:14 -0600461 if ( nlsr->debugging )
462 printf("LSA Life time: %d\n",lsa_life_time);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600463
akmhoque7adb2772013-03-05 16:30:59 -0600464 if ( (strcmp(orig_router->name,nlsr->router_name) == 0 &&
465 lsa_life_time < nlsr->lsa_refresh_time)
466 || (strcmp(orig_router->name,nlsr->router_name) != 0
467 && lsa_life_time < nlsr->router_dead_interval) )
Obaid Aminbb8ac422013-02-20 17:08:48 -0600468 {
akmhoque7adb2772013-03-05 16:30:59 -0600469 int is_new_name_lsa=check_is_new_name_lsa(orig_router->name,
470 (char *)lst,(char *)lsid,(char *)origtime);
Obaid Amin806df812013-02-21 13:30:26 -0600471 if ( is_new_name_lsa == 1 )
472 {
473 if ( nlsr->debugging )
474 printf("New NAME LSA.....\n");
akmhoque7adb2772013-03-05 16:30:59 -0600475 int chk_con=get_content_by_content_name(ccn_charbuf_as_string(uri),
476 &content_data,orig_router->name);
477 if ( chk_con == 0 ){
478 if ( nlsr->debugging )
479 printf("Content Data: %s \n",content_data);
480 process_incoming_sync_content_lsa(content_data);
481 }
482 else{
483 if ( nlsr->debugging )
484 printf("Verification failed. No content given back\n");
485 }
Obaid Amin806df812013-02-21 13:30:26 -0600486 }
487 else
488 {
489 if ( nlsr->debugging )
490 printf("Name LSA / Newer Name LSA already xists in LSDB\n");
akmhoque7adb2772013-03-05 16:30:59 -0600491 int chk_con=get_content_by_content_name(ccn_charbuf_as_string(uri)
492 , &content_data,orig_router->name);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600493
akmhoque7adb2772013-03-05 16:30:59 -0600494 if ( chk_con == 0 ){
495 if ( nlsr->debugging )
496 printf("Content Data: %s \n",content_data);
497 process_incoming_sync_content_lsa(content_data);
498 }
499 else{
500 if ( nlsr->debugging )
501 printf("Verification failed. No content given back\n");
502 }
Obaid Amin806df812013-02-21 13:30:26 -0600503 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600504 }
Obaid Amin806df812013-02-21 13:30:26 -0600505 else
Obaid Aminbb8ac422013-02-20 17:08:48 -0600506 {
Obaid Amin806df812013-02-21 13:30:26 -0600507 if ( nlsr->debugging )
508 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
Obaid Aminbb8ac422013-02-20 17:08:48 -0600509 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600510 }
Obaid Amin806df812013-02-21 13:30:26 -0600511 else
Obaid Aminbb8ac422013-02-20 17:08:48 -0600512 {
Obaid Amin806df812013-02-21 13:30:26 -0600513 ls_type=atoi(rem);
514 lst=rem;
515 if(ls_type == LS_TYPE_ADJ)
Obaid Aminbb8ac422013-02-20 17:08:48 -0600516 {
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600517 ccn_name_comp_get(content_name->buf, components,components->n-2,
518 &origtime, &comp_size);
Obaid Amin806df812013-02-21 13:30:26 -0600519 ccn_name_chop(content_name,components,-2);
520 get_name_part(orig_router,content_name,components,0);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600521
Obaid Amin806df812013-02-21 13:30:26 -0600522 if ( nlsr->debugging )
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600523 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",
524 orig_router->name,ls_type,origtime);
Obaid Amin806df812013-02-21 13:30:26 -0600525
526 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque8c5e2662013-02-24 06:37:14 -0600527 if ( nlsr->debugging )
528 printf("LSA Life time: %d\n",lsa_life_time);
529
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600530 if ( (strcmp(orig_router->name,nlsr->router_name) == 0 &&
531 lsa_life_time < nlsr->lsa_refresh_time) ||
532 (strcmp(orig_router->name,nlsr->router_name) != 0 &&
533 lsa_life_time < nlsr->router_dead_interval) )
Obaid Amin806df812013-02-21 13:30:26 -0600534 {
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600535 int is_new_adj_lsa = check_is_new_adj_lsa( orig_router->name,
536 (char *)lst, (char *)origtime);
Obaid Amin806df812013-02-21 13:30:26 -0600537 if ( is_new_adj_lsa == 1 )
538 {
539 if ( nlsr->debugging )
540 printf("New Adj LSA.....\n");
akmhoque7adb2772013-03-05 16:30:59 -0600541 int chk_con=get_content_by_content_name(ccn_charbuf_as_string(uri),
542 &content_data,orig_router->name);
Obaid Amin806df812013-02-21 13:30:26 -0600543
akmhoque7adb2772013-03-05 16:30:59 -0600544 if ( chk_con == 0 ){
545 if ( nlsr->debugging )
546 printf("Content Data: %s \n",content_data);
547 process_incoming_sync_content_lsa(content_data);
548 }
549 else{
550 if ( nlsr->debugging )
551 printf("Verification failed. No content given back\n");
552 }
Obaid Amin806df812013-02-21 13:30:26 -0600553 }
554 else
555 {
556 if ( nlsr->debugging )
557 printf("Adj LSA / Newer Adj LSA already exists in LSDB\n");
akmhoque7adb2772013-03-05 16:30:59 -0600558 int chk_con=get_content_by_content_name(ccn_charbuf_as_string(uri),
559 &content_data,orig_router->name);
560 if ( chk_con == 0 ){
561 if ( nlsr->debugging )
562 printf("Content Data: %s \n",content_data);
563 process_incoming_sync_content_lsa(content_data);
564 }
565 else{
566 if ( nlsr->debugging )
567 printf("Verification failed. No content given back\n");
568 }
Obaid Amin806df812013-02-21 13:30:26 -0600569 }
570 }
571 else
572 {
573 if ( nlsr->debugging )
574 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
575 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600576 }
Obaid Amin806df812013-02-21 13:30:26 -0600577 else if(ls_type == LS_TYPE_COR)
Obaid Aminbb8ac422013-02-20 17:08:48 -0600578 {
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600579 ccn_name_comp_get(content_name->buf, components, components->n-2,
580 &origtime, &comp_size);
Obaid Amin806df812013-02-21 13:30:26 -0600581 ccn_name_chop(content_name,components,-2);
582 get_name_part(orig_router,content_name,components,0);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600583
Obaid Amin806df812013-02-21 13:30:26 -0600584 if ( nlsr->debugging )
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600585 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",
586 orig_router->name,ls_type,origtime);
Obaid Amin806df812013-02-21 13:30:26 -0600587
588 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque8c5e2662013-02-24 06:37:14 -0600589 if ( nlsr->debugging )
590 printf("LSA Life time: %d\n",lsa_life_time);
591
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600592 if ( (strcmp(orig_router->name,nlsr->router_name) == 0 &&
593 lsa_life_time < nlsr->lsa_refresh_time) ||
594 (strcmp(orig_router->name,nlsr->router_name) != 0 &&
595 lsa_life_time < nlsr->router_dead_interval) )
Obaid Amin806df812013-02-21 13:30:26 -0600596 {
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600597 int is_new_cor_lsa=check_is_new_cor_lsa( orig_router->name,
598 (char *)lst,(char *) origtime);
Obaid Amin806df812013-02-21 13:30:26 -0600599 if ( is_new_cor_lsa == 1 )
600 {
601 if ( nlsr->debugging )
602 printf("New Cor LSA.....\n");
akmhoque7adb2772013-03-05 16:30:59 -0600603 int chk_con=get_content_by_content_name(ccn_charbuf_as_string(uri),
604 &content_data,
605 orig_router->name);
Obaid Amin806df812013-02-21 13:30:26 -0600606
akmhoque7adb2772013-03-05 16:30:59 -0600607 if ( chk_con == 0 ){
608 if ( nlsr->debugging )
609 printf("Content Data: %s \n",content_data);
610 process_incoming_sync_content_lsa(content_data);
611 }
612 else{
613 if ( nlsr->debugging )
614 printf("Verification failed. No content given back\n");
615 }
Obaid Amin806df812013-02-21 13:30:26 -0600616 }
617 else
618 {
619 if ( nlsr->debugging )
620 printf("Cor LSA / Newer Cor LSA already exists in LSDB\n");
akmhoque7adb2772013-03-05 16:30:59 -0600621 int chk_con=get_content_by_content_name(ccn_charbuf_as_string(uri),
622 &content_data,orig_router->name);
623 if ( chk_con == 0 ){
624 if ( nlsr->debugging )
625 printf("Content Data: %s \n",content_data);
626 process_incoming_sync_content_lsa(content_data);
627 }
628 else{
629 if ( nlsr->debugging )
630 printf("Verification failed. No content given back\n");
631 }
Obaid Amin806df812013-02-21 13:30:26 -0600632 }
633 }
634 else
635 {
636 if ( nlsr->debugging )
637 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
638 }
639
640 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600641 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600642
akmhoquef6773612013-02-23 09:53:00 -0600643 if (orig_router->name)
644 free(orig_router->name);
Obaid Amin05300892013-02-21 13:45:25 -0600645 if (orig_router)
646 free(orig_router);
Obaid Amin806df812013-02-21 13:30:26 -0600647 ccn_charbuf_destroy(&uri);
648 //01/31/2013
649 free(time_stamp);
akmhoquea0e71152013-02-11 09:47:59 -0600650}
651
Obaid Amin7793ba72013-02-22 00:43:58 -0600652 int
akmhoquea0e71152013-02-11 09:47:59 -0600653sync_monitor(char *topo_prefix, char *slice_prefix)
654{
655
Obaid Amin806df812013-02-21 13:30:26 -0600656 struct ccn_charbuf *prefix = ccn_charbuf_create();
Obaid Amin806df812013-02-21 13:30:26 -0600657 struct ccn_charbuf *topo = ccn_charbuf_create();
Obaid Amin7997ad82013-02-22 16:41:56 -0600658
659 nlsr->closure=(struct ccns_name_closure *)
akmhoquececba942013-02-25 17:33:34 -0600660 calloc(1,sizeof(struct ccns_name_closure)); // leak
Obaid Amin7997ad82013-02-22 16:41:56 -0600661
Obaid Amin806df812013-02-21 13:30:26 -0600662 nlsr->slice = ccns_slice_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600663
Obaid Amin806df812013-02-21 13:30:26 -0600664 ccn_charbuf_reset(prefix);
665 ccn_name_from_uri(prefix, slice_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600666
Obaid Amin806df812013-02-21 13:30:26 -0600667 ccn_charbuf_reset(topo);
668 ccn_name_from_uri(topo, topo_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600669
Obaid Amin806df812013-02-21 13:30:26 -0600670 ccns_slice_set_topo_prefix(nlsr->slice, topo, prefix);
671 nlsr->closure->callback = &sync_cb;
akmhoque54d86112013-02-21 16:42:34 -0600672 nlsr->ccns = ccns_open(nlsr->ccn, nlsr->slice, nlsr->closure, NULL, NULL);
akmhoquea0e71152013-02-11 09:47:59 -0600673
Obaid Amin7997ad82013-02-22 16:41:56 -0600674 ccn_charbuf_destroy(&prefix);
675 ccn_charbuf_destroy(&topo);
Obaid Amin806df812013-02-21 13:30:26 -0600676 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600677}
678
Obaid Amin7793ba72013-02-22 00:43:58 -0600679 struct ccn_charbuf *
akmhoquea0e71152013-02-11 09:47:59 -0600680make_template(int scope)
681{
Obaid Amin806df812013-02-21 13:30:26 -0600682 struct ccn_charbuf *templ = NULL;
683 templ = ccn_charbuf_create();
684 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
685 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
686 ccn_charbuf_append_closer(templ); /* </Name> */
687 if (0 <= scope && scope <= 2)
688 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
689 ccn_charbuf_append_closer(templ); /* </Interest> */
690 return(templ);
akmhoquea0e71152013-02-11 09:47:59 -0600691}
692
Obaid Amin7793ba72013-02-22 00:43:58 -0600693 int
akmhoquea0e71152013-02-11 09:47:59 -0600694write_data_to_repo(char *data, char *name_prefix)
695{
Obaid Amin806df812013-02-21 13:30:26 -0600696 if ( nlsr->debugging )
697 {
698 printf("write_data_to_repo called\n");
699 printf("Content Name: %s \n",name_prefix);
700 printf("Content Data: %s \n",data);
701 }
akmhoquea0e71152013-02-11 09:47:59 -0600702
Obaid Aminf210a322013-02-22 02:04:52 -0600703 struct ccn *temp_ccn;
Obaid Amin806df812013-02-21 13:30:26 -0600704 temp_ccn=ccn_create();
705 int ccn_fd=ccn_connect(temp_ccn, NULL);
706 if(ccn_fd == -1)
707 {
708 fprintf(stderr,"Could not connect to ccnd for Data Writing\n");
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600709 writeLogg(__FILE__,__FUNCTION__,__LINE__,
710 "Could not connect to ccnd for Data Writing\n");
Obaid Amin806df812013-02-21 13:30:26 -0600711 return -1;
712 }
Obaid Aminf210a322013-02-22 02:04:52 -0600713
Obaid Amin806df812013-02-21 13:30:26 -0600714 struct ccn_charbuf *name = NULL;
715 struct ccn_seqwriter *w = NULL;
716 int blocksize = 4096;
717 int freshness = -1;
Obaid Amin806df812013-02-21 13:30:26 -0600718 int scope = 1;
719 int res;
720 size_t blockread;
721 struct ccn_charbuf *templ;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600722
Obaid Amin806df812013-02-21 13:30:26 -0600723 name = ccn_charbuf_create();
724 res = ccn_name_from_uri(name, name_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600725 if (res < 0) {
Obaid Amin806df812013-02-21 13:30:26 -0600726 fprintf(stderr, "bad CCN URI: %s\n",name_prefix);
727 return -1;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600728 }
Obaid Amin806df812013-02-21 13:30:26 -0600729
Obaid Aminf210a322013-02-22 02:04:52 -0600730 w = ccn_seqw_create(temp_ccn, name);
Obaid Amin806df812013-02-21 13:30:26 -0600731 if (w == NULL) {
732 fprintf(stderr, "ccn_seqw_create failed\n");
733 return -1;
734 }
735 ccn_seqw_set_block_limits(w, blocksize, blocksize);
736 if (freshness > -1)
737 ccn_seqw_set_freshness(w, freshness);
akmhoquea0e71152013-02-11 09:47:59 -0600738
Obaid Amin7997ad82013-02-22 16:41:56 -0600739 struct ccn_charbuf *name_v = ccn_charbuf_create();
740 ccn_seqw_get_name(w, name_v);
741 ccn_name_from_uri(name_v, "%C1.R.sw");
742 ccn_name_append_nonce(name_v);
743 templ = make_template(scope);
744 res = ccn_get(temp_ccn, name_v, templ, 60000, NULL, NULL, NULL, 0);
745 ccn_charbuf_destroy(&templ);
746 ccn_charbuf_destroy(&name_v);
747 if (res < 0) {
748 fprintf(stderr, "No response from repository\n");
749 return -1;
750 }
akmhoquea0e71152013-02-11 09:47:59 -0600751
akmhoque7adb2772013-03-05 16:30:59 -0600752 struct ccn_charbuf *resultbuf=ccn_charbuf_create();
akmhoquea0e71152013-02-11 09:47:59 -0600753
akmhoque7adb2772013-03-05 16:30:59 -0600754 sign_content_with_user_defined_keystore(name,
755 resultbuf,
756 data,
757 strlen(data),
758 nlsr->keystore_path,
759 nlsr->keystore_passphrase,
760 nlsr->root_key_prefix,
761 nlsr->site_name,
762 nlsr->router_name);
763
764
765 //blockread = 0;
766 //blockread=strlen(data)+1;
767 blockread=resultbuf->length;
akmhoquea0e71152013-02-11 09:47:59 -0600768
Obaid Amin806df812013-02-21 13:30:26 -0600769 if (blockread > 0) {
akmhoque7adb2772013-03-05 16:30:59 -0600770 //res = ccn_seqw_write(w, data, blockread);
771 res = ccn_seqw_write(w, resultbuf->buf, resultbuf->length);
Obaid Amin806df812013-02-21 13:30:26 -0600772 while (res == -1) {
akmhoque8034d502013-02-24 11:53:24 -0600773 ccn_run(temp_ccn,100);
akmhoque7adb2772013-03-05 16:30:59 -0600774 //res = ccn_seqw_write(w, data, blockread);
775 res = ccn_seqw_write(w, resultbuf->buf, resultbuf->length);
Obaid Amin806df812013-02-21 13:30:26 -0600776 }
777 }
778
akmhoque8034d502013-02-24 11:53:24 -0600779 ccn_seqw_close(w);
780 ccn_run(temp_ccn, 100);
Obaid Amin806df812013-02-21 13:30:26 -0600781 ccn_charbuf_destroy(&name);
akmhoque0ed6d982013-02-22 14:28:01 -0600782 ccn_destroy(&temp_ccn);
akmhoquea0e71152013-02-11 09:47:59 -0600783
Obaid Amin806df812013-02-21 13:30:26 -0600784 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600785}
Obaid Amin806df812013-02-21 13:30:26 -0600786 int
Obaid Amin7997ad82013-02-22 16:41:56 -0600787 create_sync_slice(char *topo_prefix, char *slice_prefix)
akmhoquea0e71152013-02-11 09:47:59 -0600788{
Obaid Amin806df812013-02-21 13:30:26 -0600789 int res;
akmhoque7adb2772013-03-05 16:30:59 -0600790 struct ccn *handle;
Obaid Amin806df812013-02-21 13:30:26 -0600791 struct ccns_slice *slice;
792 struct ccn_charbuf *prefix = ccn_charbuf_create();
793 struct ccn_charbuf *topo = ccn_charbuf_create();
794 struct ccn_charbuf *clause = ccn_charbuf_create();
795 struct ccn_charbuf *slice_name = ccn_charbuf_create();
796 struct ccn_charbuf *slice_uri = ccn_charbuf_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600797
Obaid Amin806df812013-02-21 13:30:26 -0600798 if (prefix == NULL || topo == NULL || clause == NULL ||
799 slice_name == NULL || slice_uri == NULL) {
800 fprintf(stderr, "Unable to allocate required memory.\n");
801 return -1;
802 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600803
akmhoque0ed6d982013-02-22 14:28:01 -0600804 handle = ccn_create();
Obaid Amin806df812013-02-21 13:30:26 -0600805 res = ccn_connect(handle, NULL);
806 if (0 > res) {
807 fprintf(stderr, "Unable to connect to ccnd.\n");
808 return -1;
809 }
akmhoque0ed6d982013-02-22 14:28:01 -0600810
Obaid Amin806df812013-02-21 13:30:26 -0600811 slice = ccns_slice_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600812
Obaid Amin806df812013-02-21 13:30:26 -0600813 ccn_charbuf_reset(topo);
814 ccn_name_from_uri(topo, topo_prefix);
815 ccn_charbuf_reset(prefix);
816 ccn_name_from_uri(prefix,slice_prefix );
817 ccns_slice_set_topo_prefix(slice, topo, prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600818
819
akmhoque0ed6d982013-02-22 14:28:01 -0600820 res = ccns_write_slice(handle, slice, slice_name);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600821
Obaid Amin806df812013-02-21 13:30:26 -0600822 //01/31/2013
823 ccns_slice_destroy(&slice);
akmhoque0ed6d982013-02-22 14:28:01 -0600824 ccn_destroy(&handle);
Obaid Amin806df812013-02-21 13:30:26 -0600825 ccn_charbuf_destroy(&prefix);
826 ccn_charbuf_destroy(&topo);
827 ccn_charbuf_destroy(&clause);
828 ccn_charbuf_destroy(&slice_name);
829 ccn_charbuf_destroy(&slice_uri);
akmhoquea0e71152013-02-11 09:47:59 -0600830
Obaid Amin806df812013-02-21 13:30:26 -0600831 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600832}
833
akmhoque0eb8de22013-02-24 14:08:19 -0600834