blob: 70e0b0dbb9ef9ee93228d3e5a2a70325b6af3bac [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
akmhoquec25f2242013-03-15 00:24:43 -050050/**
51* call back function from sync. Receive notification of updates in sync.
52* This function calls process_content_from_sync to handle incoming content
53* from sync.
54*/
55
akmhoqueedb68d92013-03-05 10:18:16 -060056int
akmhoquea0e71152013-02-11 09:47:59 -060057sync_cb(struct ccns_name_closure *nc,
Obaid Amin806df812013-02-21 13:30:26 -060058 struct ccn_charbuf *lhash,
59 struct ccn_charbuf *rhash,
60 struct ccn_charbuf *name)
akmhoquea0e71152013-02-11 09:47:59 -060061{
akmhoque483c1eb2013-03-08 00:51:09 -060062 nlsr_lock();
Obaid Amin806df812013-02-21 13:30:26 -060063 int res;
Obaid Amin7997ad82013-02-22 16:41:56 -060064 struct ccn_charbuf *content_name;
65 struct ccn_indexbuf *content_comps;
66 struct ccn_indexbuf *name_comps;
67
68 content_comps = ccn_indexbuf_create();
69 res = ccn_name_split(name, content_comps);
Obaid Amin806df812013-02-21 13:30:26 -060070 if ( res < 0 )
71 return 0;
Obaid Amin7997ad82013-02-22 16:41:56 -060072
73 if (content_comps->n < 2)
Obaid Amin806df812013-02-21 13:30:26 -060074 return 0;
Obaid Amin7997ad82013-02-22 16:41:56 -060075
76 content_name = ccn_charbuf_create();
77 ccn_name_init(content_name);
78
79 res = ccn_name_append_components( content_name, name->buf,
80 content_comps->buf[0], content_comps->buf[content_comps->n - 1]);
Obaid Amin806df812013-02-21 13:30:26 -060081
82 if ( res < 0)
83 return 0;
akmhoquea0e71152013-02-11 09:47:59 -060084
Obaid Amin7997ad82013-02-22 16:41:56 -060085 // for debugging
Obaid Amin806df812013-02-21 13:30:26 -060086 struct ccn_charbuf *temp=ccn_charbuf_create();
87 ccn_uri_append(temp, content_name->buf, content_name->length, 0);
88 if ( nlsr->debugging )
89 printf("Name before chopping: %s \n",ccn_charbuf_as_string(temp));
90 ccn_charbuf_destroy(&temp);
akmhoquea0e71152013-02-11 09:47:59 -060091
Obaid Amin7997ad82013-02-22 16:41:56 -060092 name_comps = ccn_indexbuf_create();
93 res=ccn_name_split (content_name, name_comps);
94 if (res < 0)
Obaid Amin806df812013-02-21 13:30:26 -060095 return 0;
akmhoquea0e71152013-02-11 09:47:59 -060096
Obaid Amin806df812013-02-21 13:30:26 -060097 if ( nlsr->debugging )
98 {
99 printf("Number of components in name = %d \n",res);
100 printf("Number of components in name as indexbuf->n = %d \n",
Obaid Amin7997ad82013-02-22 16:41:56 -0600101 (int)name_comps->n);
Obaid Amin806df812013-02-21 13:30:26 -0600102 }
Obaid Amin7997ad82013-02-22 16:41:56 -0600103
104 ccn_name_chop(content_name, name_comps, -3);
Obaid Amin806df812013-02-21 13:30:26 -0600105 if ( nlsr->debugging )
106 printf("Number of components in name as indexbuf->n after chopping= %d \n"
Obaid Amin7997ad82013-02-22 16:41:56 -0600107 , (int)name_comps->n);
akmhoquea0e71152013-02-11 09:47:59 -0600108
Obaid Amin7997ad82013-02-22 16:41:56 -0600109 //for debugging
Obaid Amin806df812013-02-21 13:30:26 -0600110 struct ccn_charbuf *temp1=ccn_charbuf_create();
111 ccn_uri_append(temp1, content_name->buf, content_name->length, 0);
112 if ( nlsr->debugging )
113 printf("Name after chopping: %s \n",ccn_charbuf_as_string(temp1));
114 ccn_charbuf_destroy(&temp1);
akmhoquea0e71152013-02-11 09:47:59 -0600115
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600116 //main method that processes contents from the sync
Obaid Amin7997ad82013-02-22 16:41:56 -0600117 process_content_from_sync(content_name, name_comps);
118
Obaid Amin806df812013-02-21 13:30:26 -0600119 ccn_charbuf_destroy(&content_name);
Obaid Amin7997ad82013-02-22 16:41:56 -0600120 ccn_indexbuf_destroy(&content_comps);
121 ccn_indexbuf_destroy(&name_comps);
akmhoquea0e71152013-02-11 09:47:59 -0600122
akmhoque483c1eb2013-03-08 00:51:09 -0600123 nlsr_unlock();
Obaid Amin806df812013-02-21 13:30:26 -0600124 return(0);
akmhoquea0e71152013-02-11 09:47:59 -0600125}
126
127
akmhoquec25f2242013-03-15 00:24:43 -0500128/**
129* this function retrieve part of name from interest name and put it in name_part
130*/
akmhoqueccb33e92013-02-20 11:44:28 -0600131
akmhoqueedb68d92013-03-05 10:18:16 -0600132void
akmhoqueccb33e92013-02-20 11:44:28 -0600133get_name_part(struct name_prefix *name_part,struct ccn_charbuf * interest_ccnb,
Obaid Amin806df812013-02-21 13:30:26 -0600134 struct ccn_indexbuf *interest_comps, int offset)
akmhoquea0e71152013-02-11 09:47:59 -0600135{
Obaid Amin806df812013-02-21 13:30:26 -0600136 int lsa_position=0;
akmhoqueedb68d92013-03-05 10:18:16 -0600137
akmhoque0b60ba92013-02-25 17:55:35 -0600138 struct ccn_indexbuf *components=ccn_indexbuf_create();
Obaid Amin806df812013-02-21 13:30:26 -0600139 struct ccn_charbuf *name=ccn_charbuf_create();
140 ccn_name_from_uri(name,nlsr->slice_prefix);
141 ccn_name_split (name, components);
142 lsa_position=components->n-2;
Obaid Amin806df812013-02-21 13:30:26 -0600143 ccn_charbuf_destroy(&name);
akmhoquea0e71152013-02-11 09:47:59 -0600144
akmhoquef6773612013-02-23 09:53:00 -0600145 struct ccn_charbuf *temp=ccn_charbuf_create();
146 ccn_name_init(temp);
147 ccn_name_append_components( temp, interest_ccnb->buf,
akmhoque7adb2772013-03-05 16:30:59 -0600148 interest_comps->buf[lsa_position+1],
149 interest_comps->buf[interest_comps->n - 1]);
akmhoquef6773612013-02-23 09:53:00 -0600150
151 struct ccn_charbuf *temp1=ccn_charbuf_create();
152 ccn_uri_append(temp1, temp->buf, temp->length, 0);
153
akmhoque7adb2772013-03-05 16:30:59 -0600154 name_part->name=(char *)calloc(strlen(ccn_charbuf_as_string(temp1))+1,
155 sizeof(char));
156 memcpy(name_part->name,ccn_charbuf_as_string(temp1),
157 strlen(ccn_charbuf_as_string(temp1)));
akmhoquef6773612013-02-23 09:53:00 -0600158 name_part->name[strlen(ccn_charbuf_as_string(temp1))]='\0';
159 name_part->length=strlen(ccn_charbuf_as_string(temp1))+1;
160
161 ccn_charbuf_destroy(&temp1);
162 ccn_charbuf_destroy(&temp);
akmhoque0b60ba92013-02-25 17:55:35 -0600163 ccn_indexbuf_destroy(&components);
akmhoquef6773612013-02-23 09:53:00 -0600164
165 if ( nlsr->debugging )
166 printf("Name Part: %s \n",name_part->name);
167
akmhoquea0e71152013-02-11 09:47:59 -0600168}
169
akmhoquea0e71152013-02-11 09:47:59 -0600170
akmhoquec25f2242013-03-15 00:24:43 -0500171/**
172* Get content value by content name and put the content value in content_data
173*/
akmhoquea0e71152013-02-11 09:47:59 -0600174
175
akmhoque7adb2772013-03-05 16:30:59 -0600176int
177get_content_by_content_name(char *content_name, unsigned char **content_data,
178 char *orig_router)
akmhoquea0e71152013-02-11 09:47:59 -0600179{
akmhoque7adb2772013-03-05 16:30:59 -0600180
181 int ret=-1;
Obaid Amin806df812013-02-21 13:30:26 -0600182 struct ccn_charbuf *name = NULL;
183 struct ccn_charbuf *templ = NULL;
184 struct ccn_charbuf *resultbuf = NULL;
185 struct ccn_parsed_ContentObject pcobuf = { 0 };
186 int res;
akmhoque7adb2772013-03-05 16:30:59 -0600187 int allow_stale = 1;
Obaid Amin806df812013-02-21 13:30:26 -0600188 int content_only = 1;
189 int scope = -1;
akmhoque7adb2772013-03-05 16:30:59 -0600190 const unsigned char *ptr,*ptr_in;
191 size_t length,length_in;
Obaid Amin806df812013-02-21 13:30:26 -0600192 int resolve_version = CCN_V_HIGHEST;
193 int timeout_ms = 3000;
194 const unsigned lifetime_default = CCN_INTEREST_LIFETIME_SEC << 12;
195 unsigned lifetime_l12 = lifetime_default;
196 int get_flags = 0;
akmhoquea0e71152013-02-11 09:47:59 -0600197
Obaid Amin806df812013-02-21 13:30:26 -0600198 name = ccn_charbuf_create();
199 res = ccn_name_from_uri(name,content_name);
200 if (res < 0) {
201 fprintf(stderr, "Bad ccn URI: %s\n", content_name);
akmhoque7adb2772013-03-05 16:30:59 -0600202 ccn_charbuf_destroy(&name);
203 return ret;
Obaid Amin806df812013-02-21 13:30:26 -0600204 }
akmhoquea0e71152013-02-11 09:47:59 -0600205
Obaid Amin806df812013-02-21 13:30:26 -0600206 if (allow_stale || lifetime_l12 != lifetime_default || scope != -1) {
207 templ = ccn_charbuf_create();
208 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
209 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
210 ccn_charbuf_append_closer(templ); /* </Name> */
211 if (allow_stale) {
212 ccn_charbuf_append_tt(templ, CCN_DTAG_AnswerOriginKind, CCN_DTAG);
213 ccnb_append_number(templ,
214 CCN_AOK_DEFAULT | CCN_AOK_STALE);
215 ccn_charbuf_append_closer(templ); /* </AnswerOriginKind> */
216 }
217 if (scope != -1) {
218 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
219 }
220 if (lifetime_l12 != lifetime_default) {
221 /*
222 * Choose the interest lifetime so there are at least 3
223 * expressions (in the unsatisfied case).
224 */
225 unsigned char buf[3] = { 0 };
226 int i;
227 for (i = sizeof(buf) - 1; i >= 0; i--, lifetime_l12 >>= 8)
228 buf[i] = lifetime_l12 & 0xff;
229 ccnb_append_tagged_blob(templ, CCN_DTAG_InterestLifetime, buf,
230 sizeof(buf));
231 }
232 ccn_charbuf_append_closer(templ); /* </Interest> */
akmhoquea0e71152013-02-11 09:47:59 -0600233 }
Obaid Amin806df812013-02-21 13:30:26 -0600234 resultbuf = ccn_charbuf_create();
235 if (resolve_version != 0) {
236 res = ccn_resolve_version(nlsr->ccn, name, resolve_version, 500);
237 if (res >= 0) {
238 ccn_uri_append(resultbuf, name->buf, name->length, 1);
239 resultbuf->length = 0;
240 }
akmhoquea0e71152013-02-11 09:47:59 -0600241 }
Obaid Amin806df812013-02-21 13:30:26 -0600242 res = ccn_get(nlsr->ccn, name, templ, timeout_ms, resultbuf, &pcobuf, NULL,
243 get_flags);
akmhoquea0e71152013-02-11 09:47:59 -0600244 if (res >= 0) {
Obaid Amin806df812013-02-21 13:30:26 -0600245 ptr = resultbuf->buf;
246 length = resultbuf->length;
247 if (content_only){
248 ccn_content_get_value(ptr, length, &pcobuf, &ptr, &length);
akmhoque7adb2772013-03-05 16:30:59 -0600249 struct ccn_parsed_ContentObject pcobuf1 = { 0 };
250 int chk_cont=ccn_parse_ContentObject(ptr,length,&pcobuf1,NULL);
251 printf("Content Parsing result: %d\n",chk_cont);
252 if ( contain_key_name(ptr, &pcobuf1) == 1){
akmhoque7adb2772013-03-05 16:30:59 -0600253
akmhoqueb7958182013-03-11 12:03:54 -0500254 int res_verify=verify_key(ptr,&pcobuf1,1);
akmhoque7adb2772013-03-05 16:30:59 -0600255
akmhoqueb7958182013-03-11 12:03:54 -0500256 if ( res_verify != 0 ){
257 printf("Error in verfiying keys !! :( \n");
258 }
259 else{
260 printf("Key verification is successful :)\n");
261 ptr_in=ptr;
262 length_in=length;
263 ccn_content_get_value(ptr_in, length_in, &pcobuf1,
akmhoque7adb2772013-03-05 16:30:59 -0600264 &ptr_in, &length_in);
akmhoqueb7958182013-03-11 12:03:54 -0500265 *content_data = (unsigned char *) calloc(length_in,
akmhoque7adb2772013-03-05 16:30:59 -0600266 sizeof(char *));
akmhoqueb7958182013-03-11 12:03:54 -0500267 memcpy (*content_data, ptr_in, length_in);
268 ret=0;
269 }
akmhoque7adb2772013-03-05 16:30:59 -0600270 }
Obaid Amin806df812013-02-21 13:30:26 -0600271 }
akmhoquea0e71152013-02-11 09:47:59 -0600272 }
akmhoque7adb2772013-03-05 16:30:59 -0600273
Obaid Amin806df812013-02-21 13:30:26 -0600274 ccn_charbuf_destroy(&resultbuf);
275 ccn_charbuf_destroy(&templ);
akmhoque7adb2772013-03-05 16:30:59 -0600276 ccn_charbuf_destroy(&name);
277
278 return ret;
akmhoquea0e71152013-02-11 09:47:59 -0600279}
280
akmhoquec25f2242013-03-15 00:24:43 -0500281/**
282* Handle incoming lsa content, Calls functions to install lsa into lsdb
283*/
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
akmhoquec25f2242013-03-15 00:24:43 -0500399/**
400* Check LSA whether its new. If new retrieve the LSA content and call
401* process_incoming_sync_content_lsa with content_data
402*/
403
akmhoque7adb2772013-03-05 16:30:59 -0600404void
Obaid Amin7997ad82013-02-22 16:41:56 -0600405process_content_from_sync (struct ccn_charbuf *content_name,
406 struct ccn_indexbuf *components)
akmhoquea0e71152013-02-11 09:47:59 -0600407{
akmhoque8c5e2662013-02-24 06:37:14 -0600408 if (nlsr->debugging)
409 printf("process_content_from_sync called \n");
Obaid Amin806df812013-02-21 13:30:26 -0600410 size_t comp_size;
411 char *lst;
412 char *lsid;
413 const unsigned char *second_last_comp;
414 const unsigned char *third_last_comp;
415 const unsigned char *origtime;
416 char *sep=".";
417 char *rem;
418 char *second_comp_type;
akmhoquea0e71152013-02-11 09:47:59 -0600419
Obaid Amin806df812013-02-21 13:30:26 -0600420 int ls_type;
421 long int ls_id=0;
akmhoquea0e71152013-02-11 09:47:59 -0600422
Obaid Amin806df812013-02-21 13:30:26 -0600423 unsigned char *content_data = NULL;
akmhoquea0e71152013-02-11 09:47:59 -0600424
akmhoqueb31caa12013-02-22 08:42:09 -0600425 char *time_stamp=get_current_timestamp_micro_v2();
akmhoquea0e71152013-02-11 09:47:59 -0600426
Obaid Amin806df812013-02-21 13:30:26 -0600427 struct ccn_charbuf *uri = ccn_charbuf_create();
428 ccn_uri_append(uri, content_name->buf, content_name->length, 0);
akmhoquea0e71152013-02-11 09:47:59 -0600429
Obaid Amin7997ad82013-02-22 16:41:56 -0600430 struct name_prefix *orig_router=(struct name_prefix *)
431 calloc( 1, sizeof(struct name_prefix));
akmhoquea0e71152013-02-11 09:47:59 -0600432
Obaid Amin7997ad82013-02-22 16:41:56 -0600433 ccn_name_comp_get( content_name->buf, components,
434 components->n-1-2, &second_last_comp, &comp_size);
435
Obaid Amin806df812013-02-21 13:30:26 -0600436 if (nlsr->debugging)
Obaid Amin7997ad82013-02-22 16:41:56 -0600437 printf("2nd Last Component: %s \n", second_last_comp);
akmhoquea0e71152013-02-11 09:47:59 -0600438
Obaid Amin7997ad82013-02-22 16:41:56 -0600439 second_comp_type=strtok_r((char *)second_last_comp, sep, &rem);
440 if (second_comp_type == NULL || rem == NULL)
441 {
442 printf ("Error: unable to tokenize the string: %s, calling exit()\n",
443 (char *)second_last_comp);
444 exit(0);
445 }
446
Obaid Amin806df812013-02-21 13:30:26 -0600447 if ( strcmp( second_comp_type, "lsId" ) == 0 )
448 {
449 lsid=rem;
450 ls_id=atoi(rem);
akmhoque7adb2772013-03-05 16:30:59 -0600451 ccn_name_comp_get(content_name->buf, components,components->n-2-2,
452 &third_last_comp, &comp_size);
Obaid Amin806df812013-02-21 13:30:26 -0600453 lst=strtok_r((char *)third_last_comp,sep,&rem);
454 lst=rem;
455 ls_type=atoi(lst);
akmhoque7adb2772013-03-05 16:30:59 -0600456 ccn_name_comp_get(content_name->buf, components,components->n-2,
457 &origtime, &comp_size);
Obaid Amin806df812013-02-21 13:30:26 -0600458 ccn_name_chop(content_name,components,-3);
459 get_name_part(orig_router,content_name,components,0);
akmhoquea0e71152013-02-11 09:47:59 -0600460
Obaid Aminbb8ac422013-02-20 17:08:48 -0600461 if ( nlsr->debugging )
akmhoque7adb2772013-03-05 16:30:59 -0600462 printf("Orig Router: %s Ls Type: %d Ls id: %ld Orig Time: %s\n",
463 orig_router->name,ls_type,ls_id,origtime);
akmhoquea0e71152013-02-11 09:47:59 -0600464
Obaid Amin806df812013-02-21 13:30:26 -0600465 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque8c5e2662013-02-24 06:37:14 -0600466 if ( nlsr->debugging )
467 printf("LSA Life time: %d\n",lsa_life_time);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600468
akmhoque483c1eb2013-03-08 00:51:09 -0600469 if ( strcmp(orig_router->name,nlsr->router_name) != 0
470 && (lsa_life_time < nlsr->router_dead_interval) )
Obaid Aminbb8ac422013-02-20 17:08:48 -0600471 {
akmhoque7adb2772013-03-05 16:30:59 -0600472 int is_new_name_lsa=check_is_new_name_lsa(orig_router->name,
473 (char *)lst,(char *)lsid,(char *)origtime);
Obaid Amin806df812013-02-21 13:30:26 -0600474 if ( is_new_name_lsa == 1 )
475 {
476 if ( nlsr->debugging )
477 printf("New NAME LSA.....\n");
akmhoque7adb2772013-03-05 16:30:59 -0600478 int chk_con=get_content_by_content_name(ccn_charbuf_as_string(uri),
479 &content_data,orig_router->name);
480 if ( chk_con == 0 ){
481 if ( nlsr->debugging )
482 printf("Content Data: %s \n",content_data);
483 process_incoming_sync_content_lsa(content_data);
484 }
485 else{
486 if ( nlsr->debugging )
487 printf("Verification failed. No content given back\n");
488 }
Obaid Amin806df812013-02-21 13:30:26 -0600489 }
490 else
491 {
492 if ( nlsr->debugging )
493 printf("Name LSA / Newer Name LSA already xists in LSDB\n");
akmhoque7adb2772013-03-05 16:30:59 -0600494 int chk_con=get_content_by_content_name(ccn_charbuf_as_string(uri)
495 , &content_data,orig_router->name);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600496
akmhoque7adb2772013-03-05 16:30:59 -0600497 if ( chk_con == 0 ){
498 if ( nlsr->debugging )
499 printf("Content Data: %s \n",content_data);
500 process_incoming_sync_content_lsa(content_data);
501 }
502 else{
503 if ( nlsr->debugging )
504 printf("Verification failed. No content given back\n");
505 }
Obaid Amin806df812013-02-21 13:30:26 -0600506 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600507 }
Obaid Amin806df812013-02-21 13:30:26 -0600508 else
Obaid Aminbb8ac422013-02-20 17:08:48 -0600509 {
Obaid Amin806df812013-02-21 13:30:26 -0600510 if ( nlsr->debugging )
511 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
Obaid Aminbb8ac422013-02-20 17:08:48 -0600512 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600513 }
Obaid Amin806df812013-02-21 13:30:26 -0600514 else
Obaid Aminbb8ac422013-02-20 17:08:48 -0600515 {
Obaid Amin806df812013-02-21 13:30:26 -0600516 ls_type=atoi(rem);
517 lst=rem;
518 if(ls_type == LS_TYPE_ADJ)
Obaid Aminbb8ac422013-02-20 17:08:48 -0600519 {
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600520 ccn_name_comp_get(content_name->buf, components,components->n-2,
521 &origtime, &comp_size);
Obaid Amin806df812013-02-21 13:30:26 -0600522 ccn_name_chop(content_name,components,-2);
523 get_name_part(orig_router,content_name,components,0);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600524
Obaid Amin806df812013-02-21 13:30:26 -0600525 if ( nlsr->debugging )
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600526 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",
527 orig_router->name,ls_type,origtime);
Obaid Amin806df812013-02-21 13:30:26 -0600528
529 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque8c5e2662013-02-24 06:37:14 -0600530 if ( nlsr->debugging )
531 printf("LSA Life time: %d\n",lsa_life_time);
532
akmhoque483c1eb2013-03-08 00:51:09 -0600533 if ( strcmp(orig_router->name,nlsr->router_name) != 0
534 && (lsa_life_time < nlsr->router_dead_interval) )
Obaid Amin806df812013-02-21 13:30:26 -0600535 {
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600536 int is_new_adj_lsa = check_is_new_adj_lsa( orig_router->name,
537 (char *)lst, (char *)origtime);
Obaid Amin806df812013-02-21 13:30:26 -0600538 if ( is_new_adj_lsa == 1 )
539 {
540 if ( nlsr->debugging )
541 printf("New Adj LSA.....\n");
akmhoque7adb2772013-03-05 16:30:59 -0600542 int chk_con=get_content_by_content_name(ccn_charbuf_as_string(uri),
543 &content_data,orig_router->name);
Obaid Amin806df812013-02-21 13:30:26 -0600544
akmhoque7adb2772013-03-05 16:30:59 -0600545 if ( chk_con == 0 ){
546 if ( nlsr->debugging )
547 printf("Content Data: %s \n",content_data);
548 process_incoming_sync_content_lsa(content_data);
549 }
550 else{
551 if ( nlsr->debugging )
552 printf("Verification failed. No content given back\n");
553 }
Obaid Amin806df812013-02-21 13:30:26 -0600554 }
555 else
556 {
557 if ( nlsr->debugging )
558 printf("Adj LSA / Newer Adj LSA already exists in LSDB\n");
akmhoque7adb2772013-03-05 16:30:59 -0600559 int chk_con=get_content_by_content_name(ccn_charbuf_as_string(uri),
560 &content_data,orig_router->name);
561 if ( chk_con == 0 ){
562 if ( nlsr->debugging )
563 printf("Content Data: %s \n",content_data);
564 process_incoming_sync_content_lsa(content_data);
565 }
566 else{
567 if ( nlsr->debugging )
568 printf("Verification failed. No content given back\n");
569 }
Obaid Amin806df812013-02-21 13:30:26 -0600570 }
571 }
572 else
573 {
574 if ( nlsr->debugging )
575 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
576 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600577 }
Obaid Amin806df812013-02-21 13:30:26 -0600578 else if(ls_type == LS_TYPE_COR)
Obaid Aminbb8ac422013-02-20 17:08:48 -0600579 {
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600580 ccn_name_comp_get(content_name->buf, components, components->n-2,
581 &origtime, &comp_size);
Obaid Amin806df812013-02-21 13:30:26 -0600582 ccn_name_chop(content_name,components,-2);
583 get_name_part(orig_router,content_name,components,0);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600584
Obaid Amin806df812013-02-21 13:30:26 -0600585 if ( nlsr->debugging )
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600586 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",
587 orig_router->name,ls_type,origtime);
Obaid Amin806df812013-02-21 13:30:26 -0600588
589 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque8c5e2662013-02-24 06:37:14 -0600590 if ( nlsr->debugging )
591 printf("LSA Life time: %d\n",lsa_life_time);
592
akmhoque483c1eb2013-03-08 00:51:09 -0600593 if ( strcmp(orig_router->name,nlsr->router_name) != 0
594 && (lsa_life_time < nlsr->router_dead_interval) )
Obaid Amin806df812013-02-21 13:30:26 -0600595 {
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600596 int is_new_cor_lsa=check_is_new_cor_lsa( orig_router->name,
597 (char *)lst,(char *) origtime);
Obaid Amin806df812013-02-21 13:30:26 -0600598 if ( is_new_cor_lsa == 1 )
599 {
600 if ( nlsr->debugging )
601 printf("New Cor LSA.....\n");
akmhoque7adb2772013-03-05 16:30:59 -0600602 int chk_con=get_content_by_content_name(ccn_charbuf_as_string(uri),
603 &content_data,
604 orig_router->name);
Obaid Amin806df812013-02-21 13:30:26 -0600605
akmhoque7adb2772013-03-05 16:30:59 -0600606 if ( chk_con == 0 ){
607 if ( nlsr->debugging )
608 printf("Content Data: %s \n",content_data);
609 process_incoming_sync_content_lsa(content_data);
610 }
611 else{
612 if ( nlsr->debugging )
613 printf("Verification failed. No content given back\n");
614 }
Obaid Amin806df812013-02-21 13:30:26 -0600615 }
616 else
617 {
618 if ( nlsr->debugging )
619 printf("Cor LSA / Newer Cor LSA already exists in LSDB\n");
akmhoque7adb2772013-03-05 16:30:59 -0600620 int chk_con=get_content_by_content_name(ccn_charbuf_as_string(uri),
621 &content_data,orig_router->name);
622 if ( chk_con == 0 ){
623 if ( nlsr->debugging )
624 printf("Content Data: %s \n",content_data);
625 process_incoming_sync_content_lsa(content_data);
626 }
627 else{
628 if ( nlsr->debugging )
629 printf("Verification failed. No content given back\n");
630 }
Obaid Amin806df812013-02-21 13:30:26 -0600631 }
632 }
633 else
634 {
635 if ( nlsr->debugging )
636 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
637 }
638
639 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600640 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600641
akmhoquec25f2242013-03-15 00:24:43 -0500642 if(content_data)
643 free(content_data);
644
akmhoquef6773612013-02-23 09:53:00 -0600645 if (orig_router->name)
646 free(orig_router->name);
Obaid Amin05300892013-02-21 13:45:25 -0600647 if (orig_router)
648 free(orig_router);
Obaid Amin806df812013-02-21 13:30:26 -0600649 ccn_charbuf_destroy(&uri);
650 //01/31/2013
651 free(time_stamp);
akmhoquea0e71152013-02-11 09:47:59 -0600652}
653
akmhoquec25f2242013-03-15 00:24:43 -0500654/**
655* This function performs same functionality as ccnsyncwatch
656*/
657
658
659int
akmhoquea0e71152013-02-11 09:47:59 -0600660sync_monitor(char *topo_prefix, char *slice_prefix)
661{
662
Obaid Amin806df812013-02-21 13:30:26 -0600663 struct ccn_charbuf *prefix = ccn_charbuf_create();
Obaid Amin806df812013-02-21 13:30:26 -0600664 struct ccn_charbuf *topo = ccn_charbuf_create();
Obaid Amin7997ad82013-02-22 16:41:56 -0600665
666 nlsr->closure=(struct ccns_name_closure *)
akmhoquececba942013-02-25 17:33:34 -0600667 calloc(1,sizeof(struct ccns_name_closure)); // leak
Obaid Amin7997ad82013-02-22 16:41:56 -0600668
Obaid Amin806df812013-02-21 13:30:26 -0600669 nlsr->slice = ccns_slice_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600670
Obaid Amin806df812013-02-21 13:30:26 -0600671 ccn_charbuf_reset(prefix);
672 ccn_name_from_uri(prefix, slice_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600673
Obaid Amin806df812013-02-21 13:30:26 -0600674 ccn_charbuf_reset(topo);
675 ccn_name_from_uri(topo, topo_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600676
Obaid Amin806df812013-02-21 13:30:26 -0600677 ccns_slice_set_topo_prefix(nlsr->slice, topo, prefix);
678 nlsr->closure->callback = &sync_cb;
akmhoque54d86112013-02-21 16:42:34 -0600679 nlsr->ccns = ccns_open(nlsr->ccn, nlsr->slice, nlsr->closure, NULL, NULL);
akmhoquea0e71152013-02-11 09:47:59 -0600680
Obaid Amin7997ad82013-02-22 16:41:56 -0600681 ccn_charbuf_destroy(&prefix);
682 ccn_charbuf_destroy(&topo);
Obaid Amin806df812013-02-21 13:30:26 -0600683 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600684}
685
akmhoquead6d5692013-03-11 19:43:33 -0500686struct ccn_charbuf *
akmhoquea0e71152013-02-11 09:47:59 -0600687make_template(int scope)
688{
Obaid Amin806df812013-02-21 13:30:26 -0600689 struct ccn_charbuf *templ = NULL;
690 templ = ccn_charbuf_create();
691 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
692 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
693 ccn_charbuf_append_closer(templ); /* </Name> */
694 if (0 <= scope && scope <= 2)
695 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
696 ccn_charbuf_append_closer(templ); /* </Interest> */
697 return(templ);
akmhoquea0e71152013-02-11 09:47:59 -0600698}
699
akmhoquec25f2242013-03-15 00:24:43 -0500700/**
701* Write signed data to repo
702*/
703
704
705int
akmhoquea0e71152013-02-11 09:47:59 -0600706write_data_to_repo(char *data, char *name_prefix)
707{
akmhoque483c1eb2013-03-08 00:51:09 -0600708
709 nlsr_lock();
Obaid Amin806df812013-02-21 13:30:26 -0600710 if ( nlsr->debugging )
711 {
712 printf("write_data_to_repo called\n");
713 printf("Content Name: %s \n",name_prefix);
714 printf("Content Data: %s \n",data);
715 }
akmhoquea0e71152013-02-11 09:47:59 -0600716
Obaid Aminf210a322013-02-22 02:04:52 -0600717 struct ccn *temp_ccn;
Obaid Amin806df812013-02-21 13:30:26 -0600718 temp_ccn=ccn_create();
719 int ccn_fd=ccn_connect(temp_ccn, NULL);
720 if(ccn_fd == -1)
721 {
722 fprintf(stderr,"Could not connect to ccnd for Data Writing\n");
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600723 writeLogg(__FILE__,__FUNCTION__,__LINE__,
724 "Could not connect to ccnd for Data Writing\n");
Obaid Amin806df812013-02-21 13:30:26 -0600725 return -1;
726 }
Obaid Aminf210a322013-02-22 02:04:52 -0600727
Obaid Amin806df812013-02-21 13:30:26 -0600728 struct ccn_charbuf *name = NULL;
729 struct ccn_seqwriter *w = NULL;
730 int blocksize = 4096;
731 int freshness = -1;
Obaid Amin806df812013-02-21 13:30:26 -0600732 int scope = 1;
733 int res;
734 size_t blockread;
735 struct ccn_charbuf *templ;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600736
Obaid Amin806df812013-02-21 13:30:26 -0600737 name = ccn_charbuf_create();
738 res = ccn_name_from_uri(name, name_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600739 if (res < 0) {
Obaid Amin806df812013-02-21 13:30:26 -0600740 fprintf(stderr, "bad CCN URI: %s\n",name_prefix);
741 return -1;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600742 }
Obaid Amin806df812013-02-21 13:30:26 -0600743
Obaid Aminf210a322013-02-22 02:04:52 -0600744 w = ccn_seqw_create(temp_ccn, name);
Obaid Amin806df812013-02-21 13:30:26 -0600745 if (w == NULL) {
746 fprintf(stderr, "ccn_seqw_create failed\n");
747 return -1;
748 }
749 ccn_seqw_set_block_limits(w, blocksize, blocksize);
750 if (freshness > -1)
751 ccn_seqw_set_freshness(w, freshness);
akmhoquea0e71152013-02-11 09:47:59 -0600752
Obaid Amin7997ad82013-02-22 16:41:56 -0600753 struct ccn_charbuf *name_v = ccn_charbuf_create();
754 ccn_seqw_get_name(w, name_v);
755 ccn_name_from_uri(name_v, "%C1.R.sw");
756 ccn_name_append_nonce(name_v);
757 templ = make_template(scope);
758 res = ccn_get(temp_ccn, name_v, templ, 60000, NULL, NULL, NULL, 0);
759 ccn_charbuf_destroy(&templ);
760 ccn_charbuf_destroy(&name_v);
761 if (res < 0) {
762 fprintf(stderr, "No response from repository\n");
763 return -1;
764 }
akmhoquea0e71152013-02-11 09:47:59 -0600765
akmhoque7adb2772013-03-05 16:30:59 -0600766 struct ccn_charbuf *resultbuf=ccn_charbuf_create();
akmhoquea0e71152013-02-11 09:47:59 -0600767
akmhoque7adb2772013-03-05 16:30:59 -0600768 sign_content_with_user_defined_keystore(name,
769 resultbuf,
770 data,
771 strlen(data),
772 nlsr->keystore_path,
773 nlsr->keystore_passphrase,
774 nlsr->root_key_prefix,
775 nlsr->site_name,
776 nlsr->router_name);
777
778
akmhoque7adb2772013-03-05 16:30:59 -0600779 blockread=resultbuf->length;
akmhoquea0e71152013-02-11 09:47:59 -0600780
Obaid Amin806df812013-02-21 13:30:26 -0600781 if (blockread > 0) {
akmhoque7adb2772013-03-05 16:30:59 -0600782 res = ccn_seqw_write(w, resultbuf->buf, resultbuf->length);
Obaid Amin806df812013-02-21 13:30:26 -0600783 while (res == -1) {
akmhoquec25f2242013-03-15 00:24:43 -0500784 ccn_run(temp_ccn,1);
akmhoque7adb2772013-03-05 16:30:59 -0600785 res = ccn_seqw_write(w, resultbuf->buf, resultbuf->length);
Obaid Amin806df812013-02-21 13:30:26 -0600786 }
787 }
788
akmhoque8034d502013-02-24 11:53:24 -0600789 ccn_seqw_close(w);
akmhoquec25f2242013-03-15 00:24:43 -0500790 ccn_run(temp_ccn, 1);
Obaid Amin806df812013-02-21 13:30:26 -0600791 ccn_charbuf_destroy(&name);
akmhoque0ed6d982013-02-22 14:28:01 -0600792 ccn_destroy(&temp_ccn);
akmhoque6e2ba842013-03-05 19:35:26 -0600793 ccn_charbuf_destroy(&resultbuf);
akmhoquea0e71152013-02-11 09:47:59 -0600794
akmhoque483c1eb2013-03-08 00:51:09 -0600795 nlsr_unlock();
Obaid Amin806df812013-02-21 13:30:26 -0600796 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600797}
akmhoquec25f2242013-03-15 00:24:43 -0500798
799/**
800* Create slice for sync/repo
801*/
802
803
804int
805create_sync_slice(char *topo_prefix, char *slice_prefix)
akmhoquea0e71152013-02-11 09:47:59 -0600806{
Obaid Amin806df812013-02-21 13:30:26 -0600807 int res;
akmhoque7adb2772013-03-05 16:30:59 -0600808 struct ccn *handle;
Obaid Amin806df812013-02-21 13:30:26 -0600809 struct ccns_slice *slice;
810 struct ccn_charbuf *prefix = ccn_charbuf_create();
811 struct ccn_charbuf *topo = ccn_charbuf_create();
812 struct ccn_charbuf *clause = ccn_charbuf_create();
813 struct ccn_charbuf *slice_name = ccn_charbuf_create();
814 struct ccn_charbuf *slice_uri = ccn_charbuf_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600815
Obaid Amin806df812013-02-21 13:30:26 -0600816 if (prefix == NULL || topo == NULL || clause == NULL ||
817 slice_name == NULL || slice_uri == NULL) {
818 fprintf(stderr, "Unable to allocate required memory.\n");
819 return -1;
820 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600821
akmhoque0ed6d982013-02-22 14:28:01 -0600822 handle = ccn_create();
Obaid Amin806df812013-02-21 13:30:26 -0600823 res = ccn_connect(handle, NULL);
824 if (0 > res) {
825 fprintf(stderr, "Unable to connect to ccnd.\n");
826 return -1;
827 }
akmhoque0ed6d982013-02-22 14:28:01 -0600828
Obaid Amin806df812013-02-21 13:30:26 -0600829 slice = ccns_slice_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600830
Obaid Amin806df812013-02-21 13:30:26 -0600831 ccn_charbuf_reset(topo);
832 ccn_name_from_uri(topo, topo_prefix);
833 ccn_charbuf_reset(prefix);
834 ccn_name_from_uri(prefix,slice_prefix );
835 ccns_slice_set_topo_prefix(slice, topo, prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600836
837
akmhoque0ed6d982013-02-22 14:28:01 -0600838 res = ccns_write_slice(handle, slice, slice_name);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600839
Obaid Amin806df812013-02-21 13:30:26 -0600840 //01/31/2013
841 ccns_slice_destroy(&slice);
akmhoque0ed6d982013-02-22 14:28:01 -0600842 ccn_destroy(&handle);
Obaid Amin806df812013-02-21 13:30:26 -0600843 ccn_charbuf_destroy(&prefix);
844 ccn_charbuf_destroy(&topo);
845 ccn_charbuf_destroy(&clause);
846 ccn_charbuf_destroy(&slice_name);
847 ccn_charbuf_destroy(&slice_uri);
akmhoquea0e71152013-02-11 09:47:59 -0600848
Obaid Amin806df812013-02-21 13:30:26 -0600849 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600850}
851
akmhoque0eb8de22013-02-24 14:08:19 -0600852