blob: 4ae6c82cf71e7834c22558e1845c13c7ebaf2704 [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){
242 struct ccn_charbuf *key_name=get_key_name(ptr, &pcobuf1);
243 struct ccn_charbuf *orig_router_kn=ccn_charbuf_create();
244 res=get_orig_router_from_key_name(orig_router_kn,key_name);
245 if( res == 0){
246 struct ccn_charbuf *rtr_uri = ccn_charbuf_create();
247 ccn_uri_append(rtr_uri, orig_router_kn->buf,
248 orig_router_kn->length, 0);
249 printf("Orig Router from Key name: %s\n",
250 ccn_charbuf_as_string(rtr_uri));
251
252 if( strcmp(orig_router,ccn_charbuf_as_string(rtr_uri)) == 0){
253
akmhoque6e2ba842013-03-05 19:35:26 -0600254 int res_verify=verify_key(ptr,&pcobuf1);
akmhoque7adb2772013-03-05 16:30:59 -0600255
256 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,
264 &ptr_in, &length_in);
265 *content_data = (unsigned char *) calloc(length_in,
266 sizeof(char *));
267 memcpy (*content_data, ptr_in, length_in);
268 ret=0;
269 }
270 }
271 ccn_charbuf_destroy(&rtr_uri);
272 }
273 ccn_charbuf_destroy(&key_name);
274 ccn_charbuf_destroy(&orig_router_kn);
275 }
Obaid Amin806df812013-02-21 13:30:26 -0600276 }
akmhoquea0e71152013-02-11 09:47:59 -0600277 }
akmhoque7adb2772013-03-05 16:30:59 -0600278
Obaid Amin806df812013-02-21 13:30:26 -0600279 ccn_charbuf_destroy(&resultbuf);
280 ccn_charbuf_destroy(&templ);
akmhoque7adb2772013-03-05 16:30:59 -0600281 ccn_charbuf_destroy(&name);
282
283 return ret;
akmhoquea0e71152013-02-11 09:47:59 -0600284}
285
akmhoqueedb68d92013-03-05 10:18:16 -0600286void
akmhoquea0e71152013-02-11 09:47:59 -0600287process_incoming_sync_content_lsa( unsigned char *content_data)
288{
289
290
Obaid Amin806df812013-02-21 13:30:26 -0600291 if ( nlsr->debugging )
292 printf("process_incoming_sync_content_lsa called \n");
Obaid Aminbb8ac422013-02-20 17:08:48 -0600293
Obaid Amin806df812013-02-21 13:30:26 -0600294 char *sep="|";
295 char *rem;
296 char *orig_router;
297 char *orl;
298 int orig_router_length;
299 char *lst;
300 int ls_type;
301 char *lsid;
302 long int ls_id;
303 char *isvld;
304 int isValid;
305 char *num_link;
306 int no_link;
307 char *np;
308 char *np_length;
309 int name_length;
310 char *data;
311 char *orig_time;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600312
313
akmhoquea0e71152013-02-11 09:47:59 -0600314 if ( nlsr->debugging )
Obaid Amin806df812013-02-21 13:30:26 -0600315 printf("LSA Data \n");
316
317 if( strlen((char *)content_data ) > 0 )
akmhoquea0e71152013-02-11 09:47:59 -0600318 {
Obaid Aminbb8ac422013-02-20 17:08:48 -0600319
Obaid Amin806df812013-02-21 13:30:26 -0600320 orig_router=strtok_r((char *)content_data,sep,&rem);
321 orl=strtok_r(NULL,sep,&rem);
322 orig_router_length=atoi(orl);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600323
Obaid Amin806df812013-02-21 13:30:26 -0600324 if ( nlsr->debugging )
325 {
326 printf(" Orig Router Name : %s\n",orig_router);
327 printf(" Orig Router Length: %d\n",orig_router_length);
328 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600329
Obaid Amin806df812013-02-21 13:30:26 -0600330 lst=strtok_r(NULL,sep,&rem);
331 ls_type=atoi(lst);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600332
Obaid Amin806df812013-02-21 13:30:26 -0600333 if ( nlsr->debugging )
334 printf(" LS Type : %d\n",ls_type);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600335
Obaid Amin806df812013-02-21 13:30:26 -0600336 if ( ls_type == LS_TYPE_NAME )
337 {
338 lsid=strtok_r(NULL,sep,&rem);
339 ls_id=atoi(lsid);
340 orig_time=strtok_r(NULL,sep,&rem);
341 isvld=strtok_r(NULL,sep,&rem);
342 isValid=atoi(isvld);
343 np=strtok_r(NULL,sep,&rem);
344 np_length=strtok_r(NULL,sep,&rem);
345 name_length=atoi(np_length);
346 if ( nlsr->debugging )
347 {
348 printf(" LS ID : %ld\n",ls_id);
349 printf(" isValid : %d\n",isValid);
350 printf(" Name Prefix : %s\n",np);
351 printf(" Orig Time : %s\n",orig_time);
352 printf(" Name Prefix length: %d\n",name_length);
353 }
354
akmhoque7adb2772013-03-05 16:30:59 -0600355 build_and_install_others_name_lsa(orig_router,ls_type,ls_id,
356 orig_time,isValid,np);
Obaid Amin806df812013-02-21 13:30:26 -0600357
358 print_name_lsdb();
359
360 }
361 else if ( ls_type == LS_TYPE_ADJ )
362 {
363 orig_time=strtok_r(NULL,sep,&rem);
364 num_link=strtok_r(NULL,sep,&rem);
365 no_link=atoi(num_link);
366 data=rem;
367
368 if ( nlsr->debugging )
369 {
370 printf(" Orig Time : %s\n",orig_time);
371 printf(" No Link : %d\n",no_link);
372 printf(" Data : %s\n",data);
373 }
akmhoque7adb2772013-03-05 16:30:59 -0600374 build_and_install_others_adj_lsa(orig_router,ls_type,orig_time,
375 no_link,data);
Obaid Amin806df812013-02-21 13:30:26 -0600376 }
377 else if ( ls_type == LS_TYPE_COR )
378 {
379 orig_time=strtok_r(NULL,sep,&rem);
380 char *cor_r=strtok_r(NULL,sep,&rem);
381 char *cor_theta=strtok_r(NULL,sep,&rem);
382
383 double r, theta;
akmhoque8034d502013-02-24 11:53:24 -0600384 r=strtod(cor_r,NULL);
385 theta=strtod(cor_theta,NULL);
Obaid Amin806df812013-02-21 13:30:26 -0600386
387 if ( nlsr->debugging )
388 {
389 printf(" Orig Time : %s\n",orig_time);
390 printf(" Cor R : %f\n",r);
391 printf(" Cor Theta : %f\n",theta);
392 }
akmhoque7adb2772013-03-05 16:30:59 -0600393 build_and_install_others_cor_lsa(orig_router,ls_type,orig_time,
394 (double)r, (double)theta);
Obaid Amin806df812013-02-21 13:30:26 -0600395 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600396
397 }
akmhoquea0e71152013-02-11 09:47:59 -0600398}
399
akmhoque7adb2772013-03-05 16:30:59 -0600400void
Obaid Amin7997ad82013-02-22 16:41:56 -0600401process_content_from_sync (struct ccn_charbuf *content_name,
402 struct ccn_indexbuf *components)
akmhoquea0e71152013-02-11 09:47:59 -0600403{
akmhoque8c5e2662013-02-24 06:37:14 -0600404 if (nlsr->debugging)
405 printf("process_content_from_sync called \n");
Obaid Amin806df812013-02-21 13:30:26 -0600406 size_t comp_size;
407 char *lst;
408 char *lsid;
409 const unsigned char *second_last_comp;
410 const unsigned char *third_last_comp;
411 const unsigned char *origtime;
412 char *sep=".";
413 char *rem;
414 char *second_comp_type;
akmhoquea0e71152013-02-11 09:47:59 -0600415
Obaid Amin806df812013-02-21 13:30:26 -0600416 int ls_type;
417 long int ls_id=0;
akmhoquea0e71152013-02-11 09:47:59 -0600418
Obaid Amin806df812013-02-21 13:30:26 -0600419 unsigned char *content_data = NULL;
akmhoquea0e71152013-02-11 09:47:59 -0600420
akmhoqueb31caa12013-02-22 08:42:09 -0600421 char *time_stamp=get_current_timestamp_micro_v2();
akmhoquea0e71152013-02-11 09:47:59 -0600422
Obaid Amin806df812013-02-21 13:30:26 -0600423 struct ccn_charbuf *uri = ccn_charbuf_create();
424 ccn_uri_append(uri, content_name->buf, content_name->length, 0);
akmhoquea0e71152013-02-11 09:47:59 -0600425
Obaid Amin7997ad82013-02-22 16:41:56 -0600426 struct name_prefix *orig_router=(struct name_prefix *)
427 calloc( 1, sizeof(struct name_prefix));
akmhoquea0e71152013-02-11 09:47:59 -0600428
Obaid Amin7997ad82013-02-22 16:41:56 -0600429 ccn_name_comp_get( content_name->buf, components,
430 components->n-1-2, &second_last_comp, &comp_size);
431
Obaid Amin806df812013-02-21 13:30:26 -0600432 if (nlsr->debugging)
Obaid Amin7997ad82013-02-22 16:41:56 -0600433 printf("2nd Last Component: %s \n", second_last_comp);
akmhoquea0e71152013-02-11 09:47:59 -0600434
Obaid Amin7997ad82013-02-22 16:41:56 -0600435 second_comp_type=strtok_r((char *)second_last_comp, sep, &rem);
436 if (second_comp_type == NULL || rem == NULL)
437 {
438 printf ("Error: unable to tokenize the string: %s, calling exit()\n",
439 (char *)second_last_comp);
440 exit(0);
441 }
442
Obaid Amin806df812013-02-21 13:30:26 -0600443 if ( strcmp( second_comp_type, "lsId" ) == 0 )
444 {
445 lsid=rem;
446 ls_id=atoi(rem);
akmhoque7adb2772013-03-05 16:30:59 -0600447 ccn_name_comp_get(content_name->buf, components,components->n-2-2,
448 &third_last_comp, &comp_size);
Obaid Amin806df812013-02-21 13:30:26 -0600449 lst=strtok_r((char *)third_last_comp,sep,&rem);
450 lst=rem;
451 ls_type=atoi(lst);
akmhoque7adb2772013-03-05 16:30:59 -0600452 ccn_name_comp_get(content_name->buf, components,components->n-2,
453 &origtime, &comp_size);
Obaid Amin806df812013-02-21 13:30:26 -0600454 ccn_name_chop(content_name,components,-3);
455 get_name_part(orig_router,content_name,components,0);
akmhoquea0e71152013-02-11 09:47:59 -0600456
Obaid Aminbb8ac422013-02-20 17:08:48 -0600457 if ( nlsr->debugging )
akmhoque7adb2772013-03-05 16:30:59 -0600458 printf("Orig Router: %s Ls Type: %d Ls id: %ld Orig Time: %s\n",
459 orig_router->name,ls_type,ls_id,origtime);
akmhoquea0e71152013-02-11 09:47:59 -0600460
Obaid Amin806df812013-02-21 13:30:26 -0600461 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque8c5e2662013-02-24 06:37:14 -0600462 if ( nlsr->debugging )
463 printf("LSA Life time: %d\n",lsa_life_time);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600464
akmhoque483c1eb2013-03-08 00:51:09 -0600465 if ( strcmp(orig_router->name,nlsr->router_name) != 0
466 && (lsa_life_time < nlsr->router_dead_interval) )
Obaid Aminbb8ac422013-02-20 17:08:48 -0600467 {
akmhoque7adb2772013-03-05 16:30:59 -0600468 int is_new_name_lsa=check_is_new_name_lsa(orig_router->name,
469 (char *)lst,(char *)lsid,(char *)origtime);
Obaid Amin806df812013-02-21 13:30:26 -0600470 if ( is_new_name_lsa == 1 )
471 {
472 if ( nlsr->debugging )
473 printf("New NAME LSA.....\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);
476 if ( chk_con == 0 ){
477 if ( nlsr->debugging )
478 printf("Content Data: %s \n",content_data);
479 process_incoming_sync_content_lsa(content_data);
480 }
481 else{
482 if ( nlsr->debugging )
483 printf("Verification failed. No content given back\n");
484 }
Obaid Amin806df812013-02-21 13:30:26 -0600485 }
486 else
487 {
488 if ( nlsr->debugging )
489 printf("Name LSA / Newer Name LSA already xists in LSDB\n");
akmhoque7adb2772013-03-05 16:30:59 -0600490 int chk_con=get_content_by_content_name(ccn_charbuf_as_string(uri)
491 , &content_data,orig_router->name);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600492
akmhoque7adb2772013-03-05 16:30:59 -0600493 if ( chk_con == 0 ){
494 if ( nlsr->debugging )
495 printf("Content Data: %s \n",content_data);
496 process_incoming_sync_content_lsa(content_data);
497 }
498 else{
499 if ( nlsr->debugging )
500 printf("Verification failed. No content given back\n");
501 }
Obaid Amin806df812013-02-21 13:30:26 -0600502 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600503 }
Obaid Amin806df812013-02-21 13:30:26 -0600504 else
Obaid Aminbb8ac422013-02-20 17:08:48 -0600505 {
Obaid Amin806df812013-02-21 13:30:26 -0600506 if ( nlsr->debugging )
507 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
Obaid Aminbb8ac422013-02-20 17:08:48 -0600508 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600509 }
Obaid Amin806df812013-02-21 13:30:26 -0600510 else
Obaid Aminbb8ac422013-02-20 17:08:48 -0600511 {
Obaid Amin806df812013-02-21 13:30:26 -0600512 ls_type=atoi(rem);
513 lst=rem;
514 if(ls_type == LS_TYPE_ADJ)
Obaid Aminbb8ac422013-02-20 17:08:48 -0600515 {
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600516 ccn_name_comp_get(content_name->buf, components,components->n-2,
517 &origtime, &comp_size);
Obaid Amin806df812013-02-21 13:30:26 -0600518 ccn_name_chop(content_name,components,-2);
519 get_name_part(orig_router,content_name,components,0);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600520
Obaid Amin806df812013-02-21 13:30:26 -0600521 if ( nlsr->debugging )
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600522 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",
523 orig_router->name,ls_type,origtime);
Obaid Amin806df812013-02-21 13:30:26 -0600524
525 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque8c5e2662013-02-24 06:37:14 -0600526 if ( nlsr->debugging )
527 printf("LSA Life time: %d\n",lsa_life_time);
528
akmhoque483c1eb2013-03-08 00:51:09 -0600529 if ( strcmp(orig_router->name,nlsr->router_name) != 0
530 && (lsa_life_time < nlsr->router_dead_interval) )
Obaid Amin806df812013-02-21 13:30:26 -0600531 {
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600532 int is_new_adj_lsa = check_is_new_adj_lsa( orig_router->name,
533 (char *)lst, (char *)origtime);
Obaid Amin806df812013-02-21 13:30:26 -0600534 if ( is_new_adj_lsa == 1 )
535 {
536 if ( nlsr->debugging )
537 printf("New Adj LSA.....\n");
akmhoque7adb2772013-03-05 16:30:59 -0600538 int chk_con=get_content_by_content_name(ccn_charbuf_as_string(uri),
539 &content_data,orig_router->name);
Obaid Amin806df812013-02-21 13:30:26 -0600540
akmhoque7adb2772013-03-05 16:30:59 -0600541 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 else
552 {
553 if ( nlsr->debugging )
554 printf("Adj LSA / Newer Adj LSA already exists in LSDB\n");
akmhoque7adb2772013-03-05 16:30:59 -0600555 int chk_con=get_content_by_content_name(ccn_charbuf_as_string(uri),
556 &content_data,orig_router->name);
557 if ( chk_con == 0 ){
558 if ( nlsr->debugging )
559 printf("Content Data: %s \n",content_data);
560 process_incoming_sync_content_lsa(content_data);
561 }
562 else{
563 if ( nlsr->debugging )
564 printf("Verification failed. No content given back\n");
565 }
Obaid Amin806df812013-02-21 13:30:26 -0600566 }
567 }
568 else
569 {
570 if ( nlsr->debugging )
571 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
572 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600573 }
Obaid Amin806df812013-02-21 13:30:26 -0600574 else if(ls_type == LS_TYPE_COR)
Obaid Aminbb8ac422013-02-20 17:08:48 -0600575 {
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600576 ccn_name_comp_get(content_name->buf, components, components->n-2,
577 &origtime, &comp_size);
Obaid Amin806df812013-02-21 13:30:26 -0600578 ccn_name_chop(content_name,components,-2);
579 get_name_part(orig_router,content_name,components,0);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600580
Obaid Amin806df812013-02-21 13:30:26 -0600581 if ( nlsr->debugging )
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600582 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",
583 orig_router->name,ls_type,origtime);
Obaid Amin806df812013-02-21 13:30:26 -0600584
585 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque8c5e2662013-02-24 06:37:14 -0600586 if ( nlsr->debugging )
587 printf("LSA Life time: %d\n",lsa_life_time);
588
akmhoque483c1eb2013-03-08 00:51:09 -0600589 if ( strcmp(orig_router->name,nlsr->router_name) != 0
590 && (lsa_life_time < nlsr->router_dead_interval) )
Obaid Amin806df812013-02-21 13:30:26 -0600591 {
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600592 int is_new_cor_lsa=check_is_new_cor_lsa( orig_router->name,
593 (char *)lst,(char *) origtime);
Obaid Amin806df812013-02-21 13:30:26 -0600594 if ( is_new_cor_lsa == 1 )
595 {
596 if ( nlsr->debugging )
597 printf("New Cor LSA.....\n");
akmhoque7adb2772013-03-05 16:30:59 -0600598 int chk_con=get_content_by_content_name(ccn_charbuf_as_string(uri),
599 &content_data,
600 orig_router->name);
Obaid Amin806df812013-02-21 13:30:26 -0600601
akmhoque7adb2772013-03-05 16:30:59 -0600602 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 else
613 {
614 if ( nlsr->debugging )
615 printf("Cor LSA / Newer Cor LSA already exists in LSDB\n");
akmhoque7adb2772013-03-05 16:30:59 -0600616 int chk_con=get_content_by_content_name(ccn_charbuf_as_string(uri),
617 &content_data,orig_router->name);
618 if ( chk_con == 0 ){
619 if ( nlsr->debugging )
620 printf("Content Data: %s \n",content_data);
621 process_incoming_sync_content_lsa(content_data);
622 }
623 else{
624 if ( nlsr->debugging )
625 printf("Verification failed. No content given back\n");
626 }
Obaid Amin806df812013-02-21 13:30:26 -0600627 }
628 }
629 else
630 {
631 if ( nlsr->debugging )
632 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
633 }
634
635 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600636 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600637
akmhoquef6773612013-02-23 09:53:00 -0600638 if (orig_router->name)
639 free(orig_router->name);
Obaid Amin05300892013-02-21 13:45:25 -0600640 if (orig_router)
641 free(orig_router);
Obaid Amin806df812013-02-21 13:30:26 -0600642 ccn_charbuf_destroy(&uri);
643 //01/31/2013
644 free(time_stamp);
akmhoquea0e71152013-02-11 09:47:59 -0600645}
646
Obaid Amin7793ba72013-02-22 00:43:58 -0600647 int
akmhoquea0e71152013-02-11 09:47:59 -0600648sync_monitor(char *topo_prefix, char *slice_prefix)
649{
650
Obaid Amin806df812013-02-21 13:30:26 -0600651 struct ccn_charbuf *prefix = ccn_charbuf_create();
Obaid Amin806df812013-02-21 13:30:26 -0600652 struct ccn_charbuf *topo = ccn_charbuf_create();
Obaid Amin7997ad82013-02-22 16:41:56 -0600653
654 nlsr->closure=(struct ccns_name_closure *)
akmhoquececba942013-02-25 17:33:34 -0600655 calloc(1,sizeof(struct ccns_name_closure)); // leak
Obaid Amin7997ad82013-02-22 16:41:56 -0600656
Obaid Amin806df812013-02-21 13:30:26 -0600657 nlsr->slice = ccns_slice_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600658
Obaid Amin806df812013-02-21 13:30:26 -0600659 ccn_charbuf_reset(prefix);
660 ccn_name_from_uri(prefix, slice_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600661
Obaid Amin806df812013-02-21 13:30:26 -0600662 ccn_charbuf_reset(topo);
663 ccn_name_from_uri(topo, topo_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600664
Obaid Amin806df812013-02-21 13:30:26 -0600665 ccns_slice_set_topo_prefix(nlsr->slice, topo, prefix);
666 nlsr->closure->callback = &sync_cb;
akmhoque54d86112013-02-21 16:42:34 -0600667 nlsr->ccns = ccns_open(nlsr->ccn, nlsr->slice, nlsr->closure, NULL, NULL);
akmhoquea0e71152013-02-11 09:47:59 -0600668
Obaid Amin7997ad82013-02-22 16:41:56 -0600669 ccn_charbuf_destroy(&prefix);
670 ccn_charbuf_destroy(&topo);
Obaid Amin806df812013-02-21 13:30:26 -0600671 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600672}
673
Obaid Amin7793ba72013-02-22 00:43:58 -0600674 struct ccn_charbuf *
akmhoquea0e71152013-02-11 09:47:59 -0600675make_template(int scope)
676{
Obaid Amin806df812013-02-21 13:30:26 -0600677 struct ccn_charbuf *templ = NULL;
678 templ = ccn_charbuf_create();
679 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
680 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
681 ccn_charbuf_append_closer(templ); /* </Name> */
682 if (0 <= scope && scope <= 2)
683 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
684 ccn_charbuf_append_closer(templ); /* </Interest> */
685 return(templ);
akmhoquea0e71152013-02-11 09:47:59 -0600686}
687
Obaid Amin7793ba72013-02-22 00:43:58 -0600688 int
akmhoquea0e71152013-02-11 09:47:59 -0600689write_data_to_repo(char *data, char *name_prefix)
690{
akmhoque483c1eb2013-03-08 00:51:09 -0600691
692 nlsr_lock();
Obaid Amin806df812013-02-21 13:30:26 -0600693 if ( nlsr->debugging )
694 {
695 printf("write_data_to_repo called\n");
696 printf("Content Name: %s \n",name_prefix);
697 printf("Content Data: %s \n",data);
698 }
akmhoquea0e71152013-02-11 09:47:59 -0600699
Obaid Aminf210a322013-02-22 02:04:52 -0600700 struct ccn *temp_ccn;
Obaid Amin806df812013-02-21 13:30:26 -0600701 temp_ccn=ccn_create();
702 int ccn_fd=ccn_connect(temp_ccn, NULL);
703 if(ccn_fd == -1)
704 {
705 fprintf(stderr,"Could not connect to ccnd for Data Writing\n");
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600706 writeLogg(__FILE__,__FUNCTION__,__LINE__,
707 "Could not connect to ccnd for Data Writing\n");
Obaid Amin806df812013-02-21 13:30:26 -0600708 return -1;
709 }
Obaid Aminf210a322013-02-22 02:04:52 -0600710
Obaid Amin806df812013-02-21 13:30:26 -0600711 struct ccn_charbuf *name = NULL;
712 struct ccn_seqwriter *w = NULL;
713 int blocksize = 4096;
714 int freshness = -1;
Obaid Amin806df812013-02-21 13:30:26 -0600715 int scope = 1;
716 int res;
717 size_t blockread;
718 struct ccn_charbuf *templ;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600719
Obaid Amin806df812013-02-21 13:30:26 -0600720 name = ccn_charbuf_create();
721 res = ccn_name_from_uri(name, name_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600722 if (res < 0) {
Obaid Amin806df812013-02-21 13:30:26 -0600723 fprintf(stderr, "bad CCN URI: %s\n",name_prefix);
724 return -1;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600725 }
Obaid Amin806df812013-02-21 13:30:26 -0600726
Obaid Aminf210a322013-02-22 02:04:52 -0600727 w = ccn_seqw_create(temp_ccn, name);
Obaid Amin806df812013-02-21 13:30:26 -0600728 if (w == NULL) {
729 fprintf(stderr, "ccn_seqw_create failed\n");
730 return -1;
731 }
732 ccn_seqw_set_block_limits(w, blocksize, blocksize);
733 if (freshness > -1)
734 ccn_seqw_set_freshness(w, freshness);
akmhoquea0e71152013-02-11 09:47:59 -0600735
Obaid Amin7997ad82013-02-22 16:41:56 -0600736 struct ccn_charbuf *name_v = ccn_charbuf_create();
737 ccn_seqw_get_name(w, name_v);
738 ccn_name_from_uri(name_v, "%C1.R.sw");
739 ccn_name_append_nonce(name_v);
740 templ = make_template(scope);
741 res = ccn_get(temp_ccn, name_v, templ, 60000, NULL, NULL, NULL, 0);
742 ccn_charbuf_destroy(&templ);
743 ccn_charbuf_destroy(&name_v);
744 if (res < 0) {
745 fprintf(stderr, "No response from repository\n");
746 return -1;
747 }
akmhoquea0e71152013-02-11 09:47:59 -0600748
akmhoque7adb2772013-03-05 16:30:59 -0600749 struct ccn_charbuf *resultbuf=ccn_charbuf_create();
akmhoquea0e71152013-02-11 09:47:59 -0600750
akmhoque7adb2772013-03-05 16:30:59 -0600751 sign_content_with_user_defined_keystore(name,
752 resultbuf,
753 data,
754 strlen(data),
755 nlsr->keystore_path,
756 nlsr->keystore_passphrase,
757 nlsr->root_key_prefix,
758 nlsr->site_name,
759 nlsr->router_name);
760
761
762 //blockread = 0;
763 //blockread=strlen(data)+1;
764 blockread=resultbuf->length;
akmhoquea0e71152013-02-11 09:47:59 -0600765
Obaid Amin806df812013-02-21 13:30:26 -0600766 if (blockread > 0) {
akmhoque7adb2772013-03-05 16:30:59 -0600767 //res = ccn_seqw_write(w, data, blockread);
768 res = ccn_seqw_write(w, resultbuf->buf, resultbuf->length);
Obaid Amin806df812013-02-21 13:30:26 -0600769 while (res == -1) {
akmhoque8034d502013-02-24 11:53:24 -0600770 ccn_run(temp_ccn,100);
akmhoque7adb2772013-03-05 16:30:59 -0600771 //res = ccn_seqw_write(w, data, blockread);
772 res = ccn_seqw_write(w, resultbuf->buf, resultbuf->length);
Obaid Amin806df812013-02-21 13:30:26 -0600773 }
774 }
775
akmhoque8034d502013-02-24 11:53:24 -0600776 ccn_seqw_close(w);
777 ccn_run(temp_ccn, 100);
Obaid Amin806df812013-02-21 13:30:26 -0600778 ccn_charbuf_destroy(&name);
akmhoque0ed6d982013-02-22 14:28:01 -0600779 ccn_destroy(&temp_ccn);
akmhoque6e2ba842013-03-05 19:35:26 -0600780 ccn_charbuf_destroy(&resultbuf);
akmhoquea0e71152013-02-11 09:47:59 -0600781
akmhoque483c1eb2013-03-08 00:51:09 -0600782 nlsr_unlock();
Obaid Amin806df812013-02-21 13:30:26 -0600783 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600784}
Obaid Amin806df812013-02-21 13:30:26 -0600785 int
Obaid Amin7997ad82013-02-22 16:41:56 -0600786 create_sync_slice(char *topo_prefix, char *slice_prefix)
akmhoquea0e71152013-02-11 09:47:59 -0600787{
Obaid Amin806df812013-02-21 13:30:26 -0600788 int res;
akmhoque7adb2772013-03-05 16:30:59 -0600789 struct ccn *handle;
Obaid Amin806df812013-02-21 13:30:26 -0600790 struct ccns_slice *slice;
791 struct ccn_charbuf *prefix = ccn_charbuf_create();
792 struct ccn_charbuf *topo = ccn_charbuf_create();
793 struct ccn_charbuf *clause = ccn_charbuf_create();
794 struct ccn_charbuf *slice_name = ccn_charbuf_create();
795 struct ccn_charbuf *slice_uri = ccn_charbuf_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600796
Obaid Amin806df812013-02-21 13:30:26 -0600797 if (prefix == NULL || topo == NULL || clause == NULL ||
798 slice_name == NULL || slice_uri == NULL) {
799 fprintf(stderr, "Unable to allocate required memory.\n");
800 return -1;
801 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600802
akmhoque0ed6d982013-02-22 14:28:01 -0600803 handle = ccn_create();
Obaid Amin806df812013-02-21 13:30:26 -0600804 res = ccn_connect(handle, NULL);
805 if (0 > res) {
806 fprintf(stderr, "Unable to connect to ccnd.\n");
807 return -1;
808 }
akmhoque0ed6d982013-02-22 14:28:01 -0600809
Obaid Amin806df812013-02-21 13:30:26 -0600810 slice = ccns_slice_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600811
Obaid Amin806df812013-02-21 13:30:26 -0600812 ccn_charbuf_reset(topo);
813 ccn_name_from_uri(topo, topo_prefix);
814 ccn_charbuf_reset(prefix);
815 ccn_name_from_uri(prefix,slice_prefix );
816 ccns_slice_set_topo_prefix(slice, topo, prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600817
818
akmhoque0ed6d982013-02-22 14:28:01 -0600819 res = ccns_write_slice(handle, slice, slice_name);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600820
Obaid Amin806df812013-02-21 13:30:26 -0600821 //01/31/2013
822 ccns_slice_destroy(&slice);
akmhoque0ed6d982013-02-22 14:28:01 -0600823 ccn_destroy(&handle);
Obaid Amin806df812013-02-21 13:30:26 -0600824 ccn_charbuf_destroy(&prefix);
825 ccn_charbuf_destroy(&topo);
826 ccn_charbuf_destroy(&clause);
827 ccn_charbuf_destroy(&slice_name);
828 ccn_charbuf_destroy(&slice_uri);
akmhoquea0e71152013-02-11 09:47:59 -0600829
Obaid Amin806df812013-02-21 13:30:26 -0600830 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600831}
832
akmhoque0eb8de22013-02-24 14:08:19 -0600833