blob: c6732591aacdc9a5db18498c159d129c94d9df88 [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{
akmhoque483c1eb2013-03-08 00:51:09 -060056 nlsr_lock();
Obaid Amin806df812013-02-21 13:30:26 -060057 int res;
Obaid Amin7997ad82013-02-22 16:41:56 -060058 struct ccn_charbuf *content_name;
59 struct ccn_indexbuf *content_comps;
60 struct ccn_indexbuf *name_comps;
61
62 content_comps = ccn_indexbuf_create();
63 res = ccn_name_split(name, content_comps);
Obaid Amin806df812013-02-21 13:30:26 -060064 if ( res < 0 )
65 return 0;
Obaid Amin7997ad82013-02-22 16:41:56 -060066
67 if (content_comps->n < 2)
Obaid Amin806df812013-02-21 13:30:26 -060068 return 0;
Obaid Amin7997ad82013-02-22 16:41:56 -060069
70 content_name = ccn_charbuf_create();
71 ccn_name_init(content_name);
72
73 res = ccn_name_append_components( content_name, name->buf,
74 content_comps->buf[0], content_comps->buf[content_comps->n - 1]);
Obaid Amin806df812013-02-21 13:30:26 -060075
76 if ( res < 0)
77 return 0;
akmhoquea0e71152013-02-11 09:47:59 -060078
Obaid Amin7997ad82013-02-22 16:41:56 -060079 // for debugging
Obaid Amin806df812013-02-21 13:30:26 -060080 struct ccn_charbuf *temp=ccn_charbuf_create();
81 ccn_uri_append(temp, content_name->buf, content_name->length, 0);
82 if ( nlsr->debugging )
83 printf("Name before chopping: %s \n",ccn_charbuf_as_string(temp));
84 ccn_charbuf_destroy(&temp);
akmhoquea0e71152013-02-11 09:47:59 -060085
Obaid Amin7997ad82013-02-22 16:41:56 -060086 name_comps = ccn_indexbuf_create();
87 res=ccn_name_split (content_name, name_comps);
88 if (res < 0)
Obaid Amin806df812013-02-21 13:30:26 -060089 return 0;
akmhoquea0e71152013-02-11 09:47:59 -060090
Obaid Amin806df812013-02-21 13:30:26 -060091 if ( nlsr->debugging )
92 {
93 printf("Number of components in name = %d \n",res);
94 printf("Number of components in name as indexbuf->n = %d \n",
Obaid Amin7997ad82013-02-22 16:41:56 -060095 (int)name_comps->n);
Obaid Amin806df812013-02-21 13:30:26 -060096 }
Obaid Amin7997ad82013-02-22 16:41:56 -060097
98 ccn_name_chop(content_name, name_comps, -3);
Obaid Amin806df812013-02-21 13:30:26 -060099 if ( nlsr->debugging )
100 printf("Number of components in name as indexbuf->n after chopping= %d \n"
Obaid Amin7997ad82013-02-22 16:41:56 -0600101 , (int)name_comps->n);
akmhoquea0e71152013-02-11 09:47:59 -0600102
Obaid Amin7997ad82013-02-22 16:41:56 -0600103 //for debugging
Obaid Amin806df812013-02-21 13:30:26 -0600104 struct ccn_charbuf *temp1=ccn_charbuf_create();
105 ccn_uri_append(temp1, content_name->buf, content_name->length, 0);
106 if ( nlsr->debugging )
107 printf("Name after chopping: %s \n",ccn_charbuf_as_string(temp1));
108 ccn_charbuf_destroy(&temp1);
akmhoquea0e71152013-02-11 09:47:59 -0600109
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600110 //main method that processes contents from the sync
Obaid Amin7997ad82013-02-22 16:41:56 -0600111 process_content_from_sync(content_name, name_comps);
112
Obaid Amin806df812013-02-21 13:30:26 -0600113 ccn_charbuf_destroy(&content_name);
Obaid Amin7997ad82013-02-22 16:41:56 -0600114 ccn_indexbuf_destroy(&content_comps);
115 ccn_indexbuf_destroy(&name_comps);
akmhoquea0e71152013-02-11 09:47:59 -0600116
akmhoque483c1eb2013-03-08 00:51:09 -0600117 nlsr_unlock();
Obaid Amin806df812013-02-21 13:30:26 -0600118 return(0);
akmhoquea0e71152013-02-11 09:47:59 -0600119}
120
121
akmhoqueccb33e92013-02-20 11:44:28 -0600122
akmhoqueedb68d92013-03-05 10:18:16 -0600123void
akmhoqueccb33e92013-02-20 11:44:28 -0600124get_name_part(struct name_prefix *name_part,struct ccn_charbuf * interest_ccnb,
Obaid Amin806df812013-02-21 13:30:26 -0600125 struct ccn_indexbuf *interest_comps, int offset)
akmhoquea0e71152013-02-11 09:47:59 -0600126{
Obaid Amin806df812013-02-21 13:30:26 -0600127 int lsa_position=0;
akmhoqueedb68d92013-03-05 10:18:16 -0600128
akmhoque0b60ba92013-02-25 17:55:35 -0600129 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,
akmhoque7adb2772013-03-05 16:30:59 -0600139 interest_comps->buf[lsa_position+1],
140 interest_comps->buf[interest_comps->n - 1]);
akmhoquef6773612013-02-23 09:53:00 -0600141
142 struct ccn_charbuf *temp1=ccn_charbuf_create();
143 ccn_uri_append(temp1, temp->buf, temp->length, 0);
144
akmhoque7adb2772013-03-05 16:30:59 -0600145 name_part->name=(char *)calloc(strlen(ccn_charbuf_as_string(temp1))+1,
146 sizeof(char));
147 memcpy(name_part->name,ccn_charbuf_as_string(temp1),
148 strlen(ccn_charbuf_as_string(temp1)));
akmhoquef6773612013-02-23 09:53:00 -0600149 name_part->name[strlen(ccn_charbuf_as_string(temp1))]='\0';
150 name_part->length=strlen(ccn_charbuf_as_string(temp1))+1;
151
152 ccn_charbuf_destroy(&temp1);
153 ccn_charbuf_destroy(&temp);
akmhoque0b60ba92013-02-25 17:55:35 -0600154 ccn_indexbuf_destroy(&components);
akmhoquef6773612013-02-23 09:53:00 -0600155
156 if ( nlsr->debugging )
157 printf("Name Part: %s \n",name_part->name);
158
akmhoquea0e71152013-02-11 09:47:59 -0600159}
160
akmhoquea0e71152013-02-11 09:47:59 -0600161
akmhoquea0e71152013-02-11 09:47:59 -0600162
163
164
akmhoque7adb2772013-03-05 16:30:59 -0600165int
166get_content_by_content_name(char *content_name, unsigned char **content_data,
167 char *orig_router)
akmhoquea0e71152013-02-11 09:47:59 -0600168{
akmhoque7adb2772013-03-05 16:30:59 -0600169
170 int ret=-1;
Obaid Amin806df812013-02-21 13:30:26 -0600171 struct ccn_charbuf *name = NULL;
172 struct ccn_charbuf *templ = NULL;
173 struct ccn_charbuf *resultbuf = NULL;
174 struct ccn_parsed_ContentObject pcobuf = { 0 };
175 int res;
akmhoque7adb2772013-03-05 16:30:59 -0600176 int allow_stale = 1;
Obaid Amin806df812013-02-21 13:30:26 -0600177 int content_only = 1;
178 int scope = -1;
akmhoque7adb2772013-03-05 16:30:59 -0600179 const unsigned char *ptr,*ptr_in;
180 size_t length,length_in;
Obaid Amin806df812013-02-21 13:30:26 -0600181 int resolve_version = CCN_V_HIGHEST;
182 int timeout_ms = 3000;
183 const unsigned lifetime_default = CCN_INTEREST_LIFETIME_SEC << 12;
184 unsigned lifetime_l12 = lifetime_default;
185 int get_flags = 0;
akmhoquea0e71152013-02-11 09:47:59 -0600186
Obaid Amin806df812013-02-21 13:30:26 -0600187 name = ccn_charbuf_create();
188 res = ccn_name_from_uri(name,content_name);
189 if (res < 0) {
190 fprintf(stderr, "Bad ccn URI: %s\n", content_name);
akmhoque7adb2772013-03-05 16:30:59 -0600191 ccn_charbuf_destroy(&name);
192 return ret;
Obaid Amin806df812013-02-21 13:30:26 -0600193 }
akmhoquea0e71152013-02-11 09:47:59 -0600194
Obaid Amin806df812013-02-21 13:30:26 -0600195 if (allow_stale || lifetime_l12 != lifetime_default || scope != -1) {
196 templ = ccn_charbuf_create();
197 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
198 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
199 ccn_charbuf_append_closer(templ); /* </Name> */
200 if (allow_stale) {
201 ccn_charbuf_append_tt(templ, CCN_DTAG_AnswerOriginKind, CCN_DTAG);
202 ccnb_append_number(templ,
203 CCN_AOK_DEFAULT | CCN_AOK_STALE);
204 ccn_charbuf_append_closer(templ); /* </AnswerOriginKind> */
205 }
206 if (scope != -1) {
207 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
208 }
209 if (lifetime_l12 != lifetime_default) {
210 /*
211 * Choose the interest lifetime so there are at least 3
212 * expressions (in the unsatisfied case).
213 */
214 unsigned char buf[3] = { 0 };
215 int i;
216 for (i = sizeof(buf) - 1; i >= 0; i--, lifetime_l12 >>= 8)
217 buf[i] = lifetime_l12 & 0xff;
218 ccnb_append_tagged_blob(templ, CCN_DTAG_InterestLifetime, buf,
219 sizeof(buf));
220 }
221 ccn_charbuf_append_closer(templ); /* </Interest> */
akmhoquea0e71152013-02-11 09:47:59 -0600222 }
Obaid Amin806df812013-02-21 13:30:26 -0600223 resultbuf = ccn_charbuf_create();
224 if (resolve_version != 0) {
225 res = ccn_resolve_version(nlsr->ccn, name, resolve_version, 500);
226 if (res >= 0) {
227 ccn_uri_append(resultbuf, name->buf, name->length, 1);
228 resultbuf->length = 0;
229 }
akmhoquea0e71152013-02-11 09:47:59 -0600230 }
Obaid Amin806df812013-02-21 13:30:26 -0600231 res = ccn_get(nlsr->ccn, name, templ, timeout_ms, resultbuf, &pcobuf, NULL,
232 get_flags);
akmhoquea0e71152013-02-11 09:47:59 -0600233 if (res >= 0) {
Obaid Amin806df812013-02-21 13:30:26 -0600234 ptr = resultbuf->buf;
235 length = resultbuf->length;
236 if (content_only){
237 ccn_content_get_value(ptr, length, &pcobuf, &ptr, &length);
akmhoque7adb2772013-03-05 16:30:59 -0600238 struct ccn_parsed_ContentObject pcobuf1 = { 0 };
239 int chk_cont=ccn_parse_ContentObject(ptr,length,&pcobuf1,NULL);
240 printf("Content Parsing result: %d\n",chk_cont);
241 if ( contain_key_name(ptr, &pcobuf1) == 1){
akmhoque7adb2772013-03-05 16:30:59 -0600242
akmhoqueb7958182013-03-11 12:03:54 -0500243 int res_verify=verify_key(ptr,&pcobuf1,1);
akmhoque7adb2772013-03-05 16:30:59 -0600244
akmhoqueb7958182013-03-11 12:03:54 -0500245 if ( res_verify != 0 ){
246 printf("Error in verfiying keys !! :( \n");
247 }
248 else{
249 printf("Key verification is successful :)\n");
250 ptr_in=ptr;
251 length_in=length;
252 ccn_content_get_value(ptr_in, length_in, &pcobuf1,
akmhoque7adb2772013-03-05 16:30:59 -0600253 &ptr_in, &length_in);
akmhoqueb7958182013-03-11 12:03:54 -0500254 *content_data = (unsigned char *) calloc(length_in,
akmhoque7adb2772013-03-05 16:30:59 -0600255 sizeof(char *));
akmhoqueb7958182013-03-11 12:03:54 -0500256 memcpy (*content_data, ptr_in, length_in);
257 ret=0;
258 }
akmhoque7adb2772013-03-05 16:30:59 -0600259 }
Obaid Amin806df812013-02-21 13:30:26 -0600260 }
akmhoquea0e71152013-02-11 09:47:59 -0600261 }
akmhoque7adb2772013-03-05 16:30:59 -0600262
Obaid Amin806df812013-02-21 13:30:26 -0600263 ccn_charbuf_destroy(&resultbuf);
264 ccn_charbuf_destroy(&templ);
akmhoque7adb2772013-03-05 16:30:59 -0600265 ccn_charbuf_destroy(&name);
266
267 return ret;
akmhoquea0e71152013-02-11 09:47:59 -0600268}
269
akmhoqueedb68d92013-03-05 10:18:16 -0600270void
akmhoquea0e71152013-02-11 09:47:59 -0600271process_incoming_sync_content_lsa( unsigned char *content_data)
272{
273
274
Obaid Amin806df812013-02-21 13:30:26 -0600275 if ( nlsr->debugging )
276 printf("process_incoming_sync_content_lsa called \n");
Obaid Aminbb8ac422013-02-20 17:08:48 -0600277
Obaid Amin806df812013-02-21 13:30:26 -0600278 char *sep="|";
279 char *rem;
280 char *orig_router;
281 char *orl;
282 int orig_router_length;
283 char *lst;
284 int ls_type;
285 char *lsid;
286 long int ls_id;
287 char *isvld;
288 int isValid;
289 char *num_link;
290 int no_link;
291 char *np;
292 char *np_length;
293 int name_length;
294 char *data;
295 char *orig_time;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600296
297
akmhoquea0e71152013-02-11 09:47:59 -0600298 if ( nlsr->debugging )
Obaid Amin806df812013-02-21 13:30:26 -0600299 printf("LSA Data \n");
300
301 if( strlen((char *)content_data ) > 0 )
akmhoquea0e71152013-02-11 09:47:59 -0600302 {
Obaid Aminbb8ac422013-02-20 17:08:48 -0600303
Obaid Amin806df812013-02-21 13:30:26 -0600304 orig_router=strtok_r((char *)content_data,sep,&rem);
305 orl=strtok_r(NULL,sep,&rem);
306 orig_router_length=atoi(orl);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600307
Obaid Amin806df812013-02-21 13:30:26 -0600308 if ( nlsr->debugging )
309 {
310 printf(" Orig Router Name : %s\n",orig_router);
311 printf(" Orig Router Length: %d\n",orig_router_length);
312 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600313
Obaid Amin806df812013-02-21 13:30:26 -0600314 lst=strtok_r(NULL,sep,&rem);
315 ls_type=atoi(lst);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600316
Obaid Amin806df812013-02-21 13:30:26 -0600317 if ( nlsr->debugging )
318 printf(" LS Type : %d\n",ls_type);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600319
Obaid Amin806df812013-02-21 13:30:26 -0600320 if ( ls_type == LS_TYPE_NAME )
321 {
322 lsid=strtok_r(NULL,sep,&rem);
323 ls_id=atoi(lsid);
324 orig_time=strtok_r(NULL,sep,&rem);
325 isvld=strtok_r(NULL,sep,&rem);
326 isValid=atoi(isvld);
327 np=strtok_r(NULL,sep,&rem);
328 np_length=strtok_r(NULL,sep,&rem);
329 name_length=atoi(np_length);
330 if ( nlsr->debugging )
331 {
332 printf(" LS ID : %ld\n",ls_id);
333 printf(" isValid : %d\n",isValid);
334 printf(" Name Prefix : %s\n",np);
335 printf(" Orig Time : %s\n",orig_time);
336 printf(" Name Prefix length: %d\n",name_length);
337 }
338
akmhoque7adb2772013-03-05 16:30:59 -0600339 build_and_install_others_name_lsa(orig_router,ls_type,ls_id,
340 orig_time,isValid,np);
Obaid Amin806df812013-02-21 13:30:26 -0600341
342 print_name_lsdb();
343
344 }
345 else if ( ls_type == LS_TYPE_ADJ )
346 {
347 orig_time=strtok_r(NULL,sep,&rem);
348 num_link=strtok_r(NULL,sep,&rem);
349 no_link=atoi(num_link);
350 data=rem;
351
352 if ( nlsr->debugging )
353 {
354 printf(" Orig Time : %s\n",orig_time);
355 printf(" No Link : %d\n",no_link);
356 printf(" Data : %s\n",data);
357 }
akmhoque7adb2772013-03-05 16:30:59 -0600358 build_and_install_others_adj_lsa(orig_router,ls_type,orig_time,
359 no_link,data);
Obaid Amin806df812013-02-21 13:30:26 -0600360 }
361 else if ( ls_type == LS_TYPE_COR )
362 {
363 orig_time=strtok_r(NULL,sep,&rem);
364 char *cor_r=strtok_r(NULL,sep,&rem);
365 char *cor_theta=strtok_r(NULL,sep,&rem);
366
367 double r, theta;
akmhoque8034d502013-02-24 11:53:24 -0600368 r=strtod(cor_r,NULL);
369 theta=strtod(cor_theta,NULL);
Obaid Amin806df812013-02-21 13:30:26 -0600370
371 if ( nlsr->debugging )
372 {
373 printf(" Orig Time : %s\n",orig_time);
374 printf(" Cor R : %f\n",r);
375 printf(" Cor Theta : %f\n",theta);
376 }
akmhoque7adb2772013-03-05 16:30:59 -0600377 build_and_install_others_cor_lsa(orig_router,ls_type,orig_time,
378 (double)r, (double)theta);
Obaid Amin806df812013-02-21 13:30:26 -0600379 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600380
381 }
akmhoquea0e71152013-02-11 09:47:59 -0600382}
383
akmhoque7adb2772013-03-05 16:30:59 -0600384void
Obaid Amin7997ad82013-02-22 16:41:56 -0600385process_content_from_sync (struct ccn_charbuf *content_name,
386 struct ccn_indexbuf *components)
akmhoquea0e71152013-02-11 09:47:59 -0600387{
akmhoque8c5e2662013-02-24 06:37:14 -0600388 if (nlsr->debugging)
389 printf("process_content_from_sync called \n");
Obaid Amin806df812013-02-21 13:30:26 -0600390 size_t comp_size;
391 char *lst;
392 char *lsid;
393 const unsigned char *second_last_comp;
394 const unsigned char *third_last_comp;
395 const unsigned char *origtime;
396 char *sep=".";
397 char *rem;
398 char *second_comp_type;
akmhoquea0e71152013-02-11 09:47:59 -0600399
Obaid Amin806df812013-02-21 13:30:26 -0600400 int ls_type;
401 long int ls_id=0;
akmhoquea0e71152013-02-11 09:47:59 -0600402
Obaid Amin806df812013-02-21 13:30:26 -0600403 unsigned char *content_data = NULL;
akmhoquea0e71152013-02-11 09:47:59 -0600404
akmhoqueb31caa12013-02-22 08:42:09 -0600405 char *time_stamp=get_current_timestamp_micro_v2();
akmhoquea0e71152013-02-11 09:47:59 -0600406
Obaid Amin806df812013-02-21 13:30:26 -0600407 struct ccn_charbuf *uri = ccn_charbuf_create();
408 ccn_uri_append(uri, content_name->buf, content_name->length, 0);
akmhoquea0e71152013-02-11 09:47:59 -0600409
Obaid Amin7997ad82013-02-22 16:41:56 -0600410 struct name_prefix *orig_router=(struct name_prefix *)
411 calloc( 1, sizeof(struct name_prefix));
akmhoquea0e71152013-02-11 09:47:59 -0600412
Obaid Amin7997ad82013-02-22 16:41:56 -0600413 ccn_name_comp_get( content_name->buf, components,
414 components->n-1-2, &second_last_comp, &comp_size);
415
Obaid Amin806df812013-02-21 13:30:26 -0600416 if (nlsr->debugging)
Obaid Amin7997ad82013-02-22 16:41:56 -0600417 printf("2nd Last Component: %s \n", second_last_comp);
akmhoquea0e71152013-02-11 09:47:59 -0600418
Obaid Amin7997ad82013-02-22 16:41:56 -0600419 second_comp_type=strtok_r((char *)second_last_comp, sep, &rem);
420 if (second_comp_type == NULL || rem == NULL)
421 {
422 printf ("Error: unable to tokenize the string: %s, calling exit()\n",
423 (char *)second_last_comp);
424 exit(0);
425 }
426
Obaid Amin806df812013-02-21 13:30:26 -0600427 if ( strcmp( second_comp_type, "lsId" ) == 0 )
428 {
429 lsid=rem;
430 ls_id=atoi(rem);
akmhoque7adb2772013-03-05 16:30:59 -0600431 ccn_name_comp_get(content_name->buf, components,components->n-2-2,
432 &third_last_comp, &comp_size);
Obaid Amin806df812013-02-21 13:30:26 -0600433 lst=strtok_r((char *)third_last_comp,sep,&rem);
434 lst=rem;
435 ls_type=atoi(lst);
akmhoque7adb2772013-03-05 16:30:59 -0600436 ccn_name_comp_get(content_name->buf, components,components->n-2,
437 &origtime, &comp_size);
Obaid Amin806df812013-02-21 13:30:26 -0600438 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 )
akmhoque7adb2772013-03-05 16:30:59 -0600442 printf("Orig Router: %s Ls Type: %d Ls id: %ld Orig Time: %s\n",
443 orig_router->name,ls_type,ls_id,origtime);
akmhoquea0e71152013-02-11 09:47:59 -0600444
Obaid Amin806df812013-02-21 13:30:26 -0600445 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque8c5e2662013-02-24 06:37:14 -0600446 if ( nlsr->debugging )
447 printf("LSA Life time: %d\n",lsa_life_time);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600448
akmhoque483c1eb2013-03-08 00:51:09 -0600449 if ( strcmp(orig_router->name,nlsr->router_name) != 0
450 && (lsa_life_time < nlsr->router_dead_interval) )
Obaid Aminbb8ac422013-02-20 17:08:48 -0600451 {
akmhoque7adb2772013-03-05 16:30:59 -0600452 int is_new_name_lsa=check_is_new_name_lsa(orig_router->name,
453 (char *)lst,(char *)lsid,(char *)origtime);
Obaid Amin806df812013-02-21 13:30:26 -0600454 if ( is_new_name_lsa == 1 )
455 {
456 if ( nlsr->debugging )
457 printf("New NAME LSA.....\n");
akmhoque7adb2772013-03-05 16:30:59 -0600458 int chk_con=get_content_by_content_name(ccn_charbuf_as_string(uri),
459 &content_data,orig_router->name);
460 if ( chk_con == 0 ){
461 if ( nlsr->debugging )
462 printf("Content Data: %s \n",content_data);
463 process_incoming_sync_content_lsa(content_data);
464 }
465 else{
466 if ( nlsr->debugging )
467 printf("Verification failed. No content given back\n");
468 }
Obaid Amin806df812013-02-21 13:30:26 -0600469 }
470 else
471 {
472 if ( nlsr->debugging )
473 printf("Name LSA / Newer Name LSA already xists in LSDB\n");
akmhoque7adb2772013-03-05 16:30:59 -0600474 int chk_con=get_content_by_content_name(ccn_charbuf_as_string(uri)
475 , &content_data,orig_router->name);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600476
akmhoque7adb2772013-03-05 16:30:59 -0600477 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 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600487 }
Obaid Amin806df812013-02-21 13:30:26 -0600488 else
Obaid Aminbb8ac422013-02-20 17:08:48 -0600489 {
Obaid Amin806df812013-02-21 13:30:26 -0600490 if ( nlsr->debugging )
491 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
Obaid Aminbb8ac422013-02-20 17:08:48 -0600492 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600493 }
Obaid Amin806df812013-02-21 13:30:26 -0600494 else
Obaid Aminbb8ac422013-02-20 17:08:48 -0600495 {
Obaid Amin806df812013-02-21 13:30:26 -0600496 ls_type=atoi(rem);
497 lst=rem;
498 if(ls_type == LS_TYPE_ADJ)
Obaid Aminbb8ac422013-02-20 17:08:48 -0600499 {
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600500 ccn_name_comp_get(content_name->buf, components,components->n-2,
501 &origtime, &comp_size);
Obaid Amin806df812013-02-21 13:30:26 -0600502 ccn_name_chop(content_name,components,-2);
503 get_name_part(orig_router,content_name,components,0);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600504
Obaid Amin806df812013-02-21 13:30:26 -0600505 if ( nlsr->debugging )
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600506 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",
507 orig_router->name,ls_type,origtime);
Obaid Amin806df812013-02-21 13:30:26 -0600508
509 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque8c5e2662013-02-24 06:37:14 -0600510 if ( nlsr->debugging )
511 printf("LSA Life time: %d\n",lsa_life_time);
512
akmhoque483c1eb2013-03-08 00:51:09 -0600513 if ( strcmp(orig_router->name,nlsr->router_name) != 0
514 && (lsa_life_time < nlsr->router_dead_interval) )
Obaid Amin806df812013-02-21 13:30:26 -0600515 {
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600516 int is_new_adj_lsa = check_is_new_adj_lsa( orig_router->name,
517 (char *)lst, (char *)origtime);
Obaid Amin806df812013-02-21 13:30:26 -0600518 if ( is_new_adj_lsa == 1 )
519 {
520 if ( nlsr->debugging )
521 printf("New Adj LSA.....\n");
akmhoque7adb2772013-03-05 16:30:59 -0600522 int chk_con=get_content_by_content_name(ccn_charbuf_as_string(uri),
523 &content_data,orig_router->name);
Obaid Amin806df812013-02-21 13:30:26 -0600524
akmhoque7adb2772013-03-05 16:30:59 -0600525 if ( chk_con == 0 ){
526 if ( nlsr->debugging )
527 printf("Content Data: %s \n",content_data);
528 process_incoming_sync_content_lsa(content_data);
529 }
530 else{
531 if ( nlsr->debugging )
532 printf("Verification failed. No content given back\n");
533 }
Obaid Amin806df812013-02-21 13:30:26 -0600534 }
535 else
536 {
537 if ( nlsr->debugging )
538 printf("Adj LSA / Newer Adj LSA already exists in LSDB\n");
akmhoque7adb2772013-03-05 16:30:59 -0600539 int chk_con=get_content_by_content_name(ccn_charbuf_as_string(uri),
540 &content_data,orig_router->name);
541 if ( chk_con == 0 ){
542 if ( nlsr->debugging )
543 printf("Content Data: %s \n",content_data);
544 process_incoming_sync_content_lsa(content_data);
545 }
546 else{
547 if ( nlsr->debugging )
548 printf("Verification failed. No content given back\n");
549 }
Obaid Amin806df812013-02-21 13:30:26 -0600550 }
551 }
552 else
553 {
554 if ( nlsr->debugging )
555 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
556 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600557 }
Obaid Amin806df812013-02-21 13:30:26 -0600558 else if(ls_type == LS_TYPE_COR)
Obaid Aminbb8ac422013-02-20 17:08:48 -0600559 {
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600560 ccn_name_comp_get(content_name->buf, components, components->n-2,
561 &origtime, &comp_size);
Obaid Amin806df812013-02-21 13:30:26 -0600562 ccn_name_chop(content_name,components,-2);
563 get_name_part(orig_router,content_name,components,0);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600564
Obaid Amin806df812013-02-21 13:30:26 -0600565 if ( nlsr->debugging )
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600566 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",
567 orig_router->name,ls_type,origtime);
Obaid Amin806df812013-02-21 13:30:26 -0600568
569 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque8c5e2662013-02-24 06:37:14 -0600570 if ( nlsr->debugging )
571 printf("LSA Life time: %d\n",lsa_life_time);
572
akmhoque483c1eb2013-03-08 00:51:09 -0600573 if ( strcmp(orig_router->name,nlsr->router_name) != 0
574 && (lsa_life_time < nlsr->router_dead_interval) )
Obaid Amin806df812013-02-21 13:30:26 -0600575 {
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600576 int is_new_cor_lsa=check_is_new_cor_lsa( orig_router->name,
577 (char *)lst,(char *) origtime);
Obaid Amin806df812013-02-21 13:30:26 -0600578 if ( is_new_cor_lsa == 1 )
579 {
580 if ( nlsr->debugging )
581 printf("New Cor LSA.....\n");
akmhoque7adb2772013-03-05 16:30:59 -0600582 int chk_con=get_content_by_content_name(ccn_charbuf_as_string(uri),
583 &content_data,
584 orig_router->name);
Obaid Amin806df812013-02-21 13:30:26 -0600585
akmhoque7adb2772013-03-05 16:30:59 -0600586 if ( chk_con == 0 ){
587 if ( nlsr->debugging )
588 printf("Content Data: %s \n",content_data);
589 process_incoming_sync_content_lsa(content_data);
590 }
591 else{
592 if ( nlsr->debugging )
593 printf("Verification failed. No content given back\n");
594 }
Obaid Amin806df812013-02-21 13:30:26 -0600595 }
596 else
597 {
598 if ( nlsr->debugging )
599 printf("Cor LSA / Newer Cor LSA already exists in LSDB\n");
akmhoque7adb2772013-03-05 16:30:59 -0600600 int chk_con=get_content_by_content_name(ccn_charbuf_as_string(uri),
601 &content_data,orig_router->name);
602 if ( chk_con == 0 ){
603 if ( nlsr->debugging )
604 printf("Content Data: %s \n",content_data);
605 process_incoming_sync_content_lsa(content_data);
606 }
607 else{
608 if ( nlsr->debugging )
609 printf("Verification failed. No content given back\n");
610 }
Obaid Amin806df812013-02-21 13:30:26 -0600611 }
612 }
613 else
614 {
615 if ( nlsr->debugging )
616 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
617 }
618
619 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600620 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600621
akmhoquef6773612013-02-23 09:53:00 -0600622 if (orig_router->name)
623 free(orig_router->name);
Obaid Amin05300892013-02-21 13:45:25 -0600624 if (orig_router)
625 free(orig_router);
Obaid Amin806df812013-02-21 13:30:26 -0600626 ccn_charbuf_destroy(&uri);
627 //01/31/2013
628 free(time_stamp);
akmhoquea0e71152013-02-11 09:47:59 -0600629}
630
Obaid Amin7793ba72013-02-22 00:43:58 -0600631 int
akmhoquea0e71152013-02-11 09:47:59 -0600632sync_monitor(char *topo_prefix, char *slice_prefix)
633{
634
Obaid Amin806df812013-02-21 13:30:26 -0600635 struct ccn_charbuf *prefix = ccn_charbuf_create();
Obaid Amin806df812013-02-21 13:30:26 -0600636 struct ccn_charbuf *topo = ccn_charbuf_create();
Obaid Amin7997ad82013-02-22 16:41:56 -0600637
638 nlsr->closure=(struct ccns_name_closure *)
akmhoquececba942013-02-25 17:33:34 -0600639 calloc(1,sizeof(struct ccns_name_closure)); // leak
Obaid Amin7997ad82013-02-22 16:41:56 -0600640
Obaid Amin806df812013-02-21 13:30:26 -0600641 nlsr->slice = ccns_slice_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600642
Obaid Amin806df812013-02-21 13:30:26 -0600643 ccn_charbuf_reset(prefix);
644 ccn_name_from_uri(prefix, slice_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600645
Obaid Amin806df812013-02-21 13:30:26 -0600646 ccn_charbuf_reset(topo);
647 ccn_name_from_uri(topo, topo_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600648
Obaid Amin806df812013-02-21 13:30:26 -0600649 ccns_slice_set_topo_prefix(nlsr->slice, topo, prefix);
650 nlsr->closure->callback = &sync_cb;
akmhoque54d86112013-02-21 16:42:34 -0600651 nlsr->ccns = ccns_open(nlsr->ccn, nlsr->slice, nlsr->closure, NULL, NULL);
akmhoquea0e71152013-02-11 09:47:59 -0600652
Obaid Amin7997ad82013-02-22 16:41:56 -0600653 ccn_charbuf_destroy(&prefix);
654 ccn_charbuf_destroy(&topo);
Obaid Amin806df812013-02-21 13:30:26 -0600655 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600656}
657
Obaid Amin7793ba72013-02-22 00:43:58 -0600658 struct ccn_charbuf *
akmhoquea0e71152013-02-11 09:47:59 -0600659make_template(int scope)
660{
Obaid Amin806df812013-02-21 13:30:26 -0600661 struct ccn_charbuf *templ = NULL;
662 templ = ccn_charbuf_create();
663 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
664 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
665 ccn_charbuf_append_closer(templ); /* </Name> */
666 if (0 <= scope && scope <= 2)
667 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
668 ccn_charbuf_append_closer(templ); /* </Interest> */
669 return(templ);
akmhoquea0e71152013-02-11 09:47:59 -0600670}
671
Obaid Amin7793ba72013-02-22 00:43:58 -0600672 int
akmhoquea0e71152013-02-11 09:47:59 -0600673write_data_to_repo(char *data, char *name_prefix)
674{
akmhoque483c1eb2013-03-08 00:51:09 -0600675
676 nlsr_lock();
Obaid Amin806df812013-02-21 13:30:26 -0600677 if ( nlsr->debugging )
678 {
679 printf("write_data_to_repo called\n");
680 printf("Content Name: %s \n",name_prefix);
681 printf("Content Data: %s \n",data);
682 }
akmhoquea0e71152013-02-11 09:47:59 -0600683
Obaid Aminf210a322013-02-22 02:04:52 -0600684 struct ccn *temp_ccn;
Obaid Amin806df812013-02-21 13:30:26 -0600685 temp_ccn=ccn_create();
686 int ccn_fd=ccn_connect(temp_ccn, NULL);
687 if(ccn_fd == -1)
688 {
689 fprintf(stderr,"Could not connect to ccnd for Data Writing\n");
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600690 writeLogg(__FILE__,__FUNCTION__,__LINE__,
691 "Could not connect to ccnd for Data Writing\n");
Obaid Amin806df812013-02-21 13:30:26 -0600692 return -1;
693 }
Obaid Aminf210a322013-02-22 02:04:52 -0600694
Obaid Amin806df812013-02-21 13:30:26 -0600695 struct ccn_charbuf *name = NULL;
696 struct ccn_seqwriter *w = NULL;
697 int blocksize = 4096;
698 int freshness = -1;
Obaid Amin806df812013-02-21 13:30:26 -0600699 int scope = 1;
700 int res;
701 size_t blockread;
702 struct ccn_charbuf *templ;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600703
Obaid Amin806df812013-02-21 13:30:26 -0600704 name = ccn_charbuf_create();
705 res = ccn_name_from_uri(name, name_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600706 if (res < 0) {
Obaid Amin806df812013-02-21 13:30:26 -0600707 fprintf(stderr, "bad CCN URI: %s\n",name_prefix);
708 return -1;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600709 }
Obaid Amin806df812013-02-21 13:30:26 -0600710
Obaid Aminf210a322013-02-22 02:04:52 -0600711 w = ccn_seqw_create(temp_ccn, name);
Obaid Amin806df812013-02-21 13:30:26 -0600712 if (w == NULL) {
713 fprintf(stderr, "ccn_seqw_create failed\n");
714 return -1;
715 }
716 ccn_seqw_set_block_limits(w, blocksize, blocksize);
717 if (freshness > -1)
718 ccn_seqw_set_freshness(w, freshness);
akmhoquea0e71152013-02-11 09:47:59 -0600719
Obaid Amin7997ad82013-02-22 16:41:56 -0600720 struct ccn_charbuf *name_v = ccn_charbuf_create();
721 ccn_seqw_get_name(w, name_v);
722 ccn_name_from_uri(name_v, "%C1.R.sw");
723 ccn_name_append_nonce(name_v);
724 templ = make_template(scope);
725 res = ccn_get(temp_ccn, name_v, templ, 60000, NULL, NULL, NULL, 0);
726 ccn_charbuf_destroy(&templ);
727 ccn_charbuf_destroy(&name_v);
728 if (res < 0) {
729 fprintf(stderr, "No response from repository\n");
730 return -1;
731 }
akmhoquea0e71152013-02-11 09:47:59 -0600732
akmhoque7adb2772013-03-05 16:30:59 -0600733 struct ccn_charbuf *resultbuf=ccn_charbuf_create();
akmhoquea0e71152013-02-11 09:47:59 -0600734
akmhoque7adb2772013-03-05 16:30:59 -0600735 sign_content_with_user_defined_keystore(name,
736 resultbuf,
737 data,
738 strlen(data),
739 nlsr->keystore_path,
740 nlsr->keystore_passphrase,
741 nlsr->root_key_prefix,
742 nlsr->site_name,
743 nlsr->router_name);
744
745
746 //blockread = 0;
747 //blockread=strlen(data)+1;
748 blockread=resultbuf->length;
akmhoquea0e71152013-02-11 09:47:59 -0600749
Obaid Amin806df812013-02-21 13:30:26 -0600750 if (blockread > 0) {
akmhoque7adb2772013-03-05 16:30:59 -0600751 //res = ccn_seqw_write(w, data, blockread);
752 res = ccn_seqw_write(w, resultbuf->buf, resultbuf->length);
Obaid Amin806df812013-02-21 13:30:26 -0600753 while (res == -1) {
akmhoque8034d502013-02-24 11:53:24 -0600754 ccn_run(temp_ccn,100);
akmhoque7adb2772013-03-05 16:30:59 -0600755 //res = ccn_seqw_write(w, data, blockread);
756 res = ccn_seqw_write(w, resultbuf->buf, resultbuf->length);
Obaid Amin806df812013-02-21 13:30:26 -0600757 }
758 }
759
akmhoque8034d502013-02-24 11:53:24 -0600760 ccn_seqw_close(w);
761 ccn_run(temp_ccn, 100);
Obaid Amin806df812013-02-21 13:30:26 -0600762 ccn_charbuf_destroy(&name);
akmhoque0ed6d982013-02-22 14:28:01 -0600763 ccn_destroy(&temp_ccn);
akmhoque6e2ba842013-03-05 19:35:26 -0600764 ccn_charbuf_destroy(&resultbuf);
akmhoquea0e71152013-02-11 09:47:59 -0600765
akmhoque483c1eb2013-03-08 00:51:09 -0600766 nlsr_unlock();
Obaid Amin806df812013-02-21 13:30:26 -0600767 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600768}
Obaid Amin806df812013-02-21 13:30:26 -0600769 int
Obaid Amin7997ad82013-02-22 16:41:56 -0600770 create_sync_slice(char *topo_prefix, char *slice_prefix)
akmhoquea0e71152013-02-11 09:47:59 -0600771{
Obaid Amin806df812013-02-21 13:30:26 -0600772 int res;
akmhoque7adb2772013-03-05 16:30:59 -0600773 struct ccn *handle;
Obaid Amin806df812013-02-21 13:30:26 -0600774 struct ccns_slice *slice;
775 struct ccn_charbuf *prefix = ccn_charbuf_create();
776 struct ccn_charbuf *topo = ccn_charbuf_create();
777 struct ccn_charbuf *clause = ccn_charbuf_create();
778 struct ccn_charbuf *slice_name = ccn_charbuf_create();
779 struct ccn_charbuf *slice_uri = ccn_charbuf_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600780
Obaid Amin806df812013-02-21 13:30:26 -0600781 if (prefix == NULL || topo == NULL || clause == NULL ||
782 slice_name == NULL || slice_uri == NULL) {
783 fprintf(stderr, "Unable to allocate required memory.\n");
784 return -1;
785 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600786
akmhoque0ed6d982013-02-22 14:28:01 -0600787 handle = ccn_create();
Obaid Amin806df812013-02-21 13:30:26 -0600788 res = ccn_connect(handle, NULL);
789 if (0 > res) {
790 fprintf(stderr, "Unable to connect to ccnd.\n");
791 return -1;
792 }
akmhoque0ed6d982013-02-22 14:28:01 -0600793
Obaid Amin806df812013-02-21 13:30:26 -0600794 slice = ccns_slice_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600795
Obaid Amin806df812013-02-21 13:30:26 -0600796 ccn_charbuf_reset(topo);
797 ccn_name_from_uri(topo, topo_prefix);
798 ccn_charbuf_reset(prefix);
799 ccn_name_from_uri(prefix,slice_prefix );
800 ccns_slice_set_topo_prefix(slice, topo, prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600801
802
akmhoque0ed6d982013-02-22 14:28:01 -0600803 res = ccns_write_slice(handle, slice, slice_name);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600804
Obaid Amin806df812013-02-21 13:30:26 -0600805 //01/31/2013
806 ccns_slice_destroy(&slice);
akmhoque0ed6d982013-02-22 14:28:01 -0600807 ccn_destroy(&handle);
Obaid Amin806df812013-02-21 13:30:26 -0600808 ccn_charbuf_destroy(&prefix);
809 ccn_charbuf_destroy(&topo);
810 ccn_charbuf_destroy(&clause);
811 ccn_charbuf_destroy(&slice_name);
812 ccn_charbuf_destroy(&slice_uri);
akmhoquea0e71152013-02-11 09:47:59 -0600813
Obaid Amin806df812013-02-21 13:30:26 -0600814 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600815}
816
akmhoque0eb8de22013-02-24 14:08:19 -0600817