blob: 80b0175aca19109f08a0528d469366514e27a2f2 [file] [log] [blame]
akmhoquea0e71152013-02-11 09:47:59 -06001#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4#include <unistd.h>
5#include <getopt.h>
6#include <sys/time.h>
7#include <sys/stat.h>
8#include <assert.h>
9#include <sys/types.h>
10#include <signal.h>
11#include <sys/socket.h>
12#include <sys/un.h>
13#include <fcntl.h>
14#include <sys/ioctl.h>
15#include <netinet/in.h>
16#include <netdb.h>
17#include <arpa/inet.h>
18
19#include <ccn/ccn.h>
20#include <ccn/uri.h>
21#include <ccn/keystore.h>
22#include <ccn/signing.h>
23#include <ccn/schedule.h>
24#include <ccn/hashtb.h>
25#include <ccn/sync.h>
26#include <ccn/seqwriter.h>
27
28#include "nlsr.h"
29#include "nlsr_sync.h"
30#include "nlsr_lsdb.h"
31#include "utility.h"
akmhoqueedb68d92013-03-05 10:18:16 -060032#include "nlsr_km.h"
akmhoque7adb2772013-03-05 16:30:59 -060033#include "nlsr_km_util.h"
akmhoquea0e71152013-02-11 09:47:59 -060034
35
akmhoqueedb68d92013-03-05 10:18:16 -060036char *
akmhoquea0e71152013-02-11 09:47:59 -060037hex_string(unsigned char *s, size_t l)
38{
Obaid Amin806df812013-02-21 13:30:26 -060039 const char *hex_digits = "0123456789abcdef";
40 char *r;
41 int i;
42 r = calloc(1, 1 + 2 * l);
43 for (i = 0; i < l; i++) {
44 r[2*i] = hex_digits[(s[i]>>4) & 0xf];
45 r[1+2*i] = hex_digits[s[i] & 0xf];
46 }
47 return(r);
akmhoquea0e71152013-02-11 09:47:59 -060048}
49
akmhoqueedb68d92013-03-05 10:18:16 -060050int
akmhoquea0e71152013-02-11 09:47:59 -060051sync_cb(struct ccns_name_closure *nc,
Obaid Amin806df812013-02-21 13:30:26 -060052 struct ccn_charbuf *lhash,
53 struct ccn_charbuf *rhash,
54 struct ccn_charbuf *name)
akmhoquea0e71152013-02-11 09:47:59 -060055{
Obaid Amin806df812013-02-21 13:30:26 -060056 int res;
Obaid Amin7997ad82013-02-22 16:41:56 -060057 struct ccn_charbuf *content_name;
58 struct ccn_indexbuf *content_comps;
59 struct ccn_indexbuf *name_comps;
60
61 content_comps = ccn_indexbuf_create();
62 res = ccn_name_split(name, content_comps);
Obaid Amin806df812013-02-21 13:30:26 -060063 if ( res < 0 )
64 return 0;
Obaid Amin7997ad82013-02-22 16:41:56 -060065
66 if (content_comps->n < 2)
Obaid Amin806df812013-02-21 13:30:26 -060067 return 0;
Obaid Amin7997ad82013-02-22 16:41:56 -060068
69 content_name = ccn_charbuf_create();
70 ccn_name_init(content_name);
71
72 res = ccn_name_append_components( content_name, name->buf,
73 content_comps->buf[0], content_comps->buf[content_comps->n - 1]);
Obaid Amin806df812013-02-21 13:30:26 -060074
75 if ( res < 0)
76 return 0;
akmhoquea0e71152013-02-11 09:47:59 -060077
Obaid Amin7997ad82013-02-22 16:41:56 -060078 // for debugging
Obaid Amin806df812013-02-21 13:30:26 -060079 struct ccn_charbuf *temp=ccn_charbuf_create();
80 ccn_uri_append(temp, content_name->buf, content_name->length, 0);
81 if ( nlsr->debugging )
82 printf("Name before chopping: %s \n",ccn_charbuf_as_string(temp));
83 ccn_charbuf_destroy(&temp);
akmhoquea0e71152013-02-11 09:47:59 -060084
Obaid Amin7997ad82013-02-22 16:41:56 -060085 name_comps = ccn_indexbuf_create();
86 res=ccn_name_split (content_name, name_comps);
87 if (res < 0)
Obaid Amin806df812013-02-21 13:30:26 -060088 return 0;
akmhoquea0e71152013-02-11 09:47:59 -060089
Obaid Amin806df812013-02-21 13:30:26 -060090 if ( nlsr->debugging )
91 {
92 printf("Number of components in name = %d \n",res);
93 printf("Number of components in name as indexbuf->n = %d \n",
Obaid Amin7997ad82013-02-22 16:41:56 -060094 (int)name_comps->n);
Obaid Amin806df812013-02-21 13:30:26 -060095 }
Obaid Amin7997ad82013-02-22 16:41:56 -060096
97 ccn_name_chop(content_name, name_comps, -3);
Obaid Amin806df812013-02-21 13:30:26 -060098 if ( nlsr->debugging )
99 printf("Number of components in name as indexbuf->n after chopping= %d \n"
Obaid Amin7997ad82013-02-22 16:41:56 -0600100 , (int)name_comps->n);
akmhoquea0e71152013-02-11 09:47:59 -0600101
Obaid Amin7997ad82013-02-22 16:41:56 -0600102 //for debugging
Obaid Amin806df812013-02-21 13:30:26 -0600103 struct ccn_charbuf *temp1=ccn_charbuf_create();
104 ccn_uri_append(temp1, content_name->buf, content_name->length, 0);
105 if ( nlsr->debugging )
106 printf("Name after chopping: %s \n",ccn_charbuf_as_string(temp1));
107 ccn_charbuf_destroy(&temp1);
akmhoquea0e71152013-02-11 09:47:59 -0600108
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600109 //main method that processes contents from the sync
Obaid Amin7997ad82013-02-22 16:41:56 -0600110 process_content_from_sync(content_name, name_comps);
111
Obaid Amin806df812013-02-21 13:30:26 -0600112 ccn_charbuf_destroy(&content_name);
Obaid Amin7997ad82013-02-22 16:41:56 -0600113 ccn_indexbuf_destroy(&content_comps);
114 ccn_indexbuf_destroy(&name_comps);
akmhoquea0e71152013-02-11 09:47:59 -0600115
Obaid Amin806df812013-02-21 13:30:26 -0600116 return(0);
akmhoquea0e71152013-02-11 09:47:59 -0600117}
118
119
akmhoqueccb33e92013-02-20 11:44:28 -0600120
akmhoqueedb68d92013-03-05 10:18:16 -0600121void
akmhoqueccb33e92013-02-20 11:44:28 -0600122get_name_part(struct name_prefix *name_part,struct ccn_charbuf * interest_ccnb,
Obaid Amin806df812013-02-21 13:30:26 -0600123 struct ccn_indexbuf *interest_comps, int offset)
akmhoquea0e71152013-02-11 09:47:59 -0600124{
Obaid Amin806df812013-02-21 13:30:26 -0600125 int lsa_position=0;
akmhoqueedb68d92013-03-05 10:18:16 -0600126
akmhoque0b60ba92013-02-25 17:55:35 -0600127 struct ccn_indexbuf *components=ccn_indexbuf_create();
Obaid Amin806df812013-02-21 13:30:26 -0600128 struct ccn_charbuf *name=ccn_charbuf_create();
129 ccn_name_from_uri(name,nlsr->slice_prefix);
130 ccn_name_split (name, components);
131 lsa_position=components->n-2;
Obaid Amin806df812013-02-21 13:30:26 -0600132 ccn_charbuf_destroy(&name);
akmhoquea0e71152013-02-11 09:47:59 -0600133
akmhoquef6773612013-02-23 09:53:00 -0600134 struct ccn_charbuf *temp=ccn_charbuf_create();
135 ccn_name_init(temp);
136 ccn_name_append_components( temp, interest_ccnb->buf,
akmhoque7adb2772013-03-05 16:30:59 -0600137 interest_comps->buf[lsa_position+1],
138 interest_comps->buf[interest_comps->n - 1]);
akmhoquef6773612013-02-23 09:53:00 -0600139
140 struct ccn_charbuf *temp1=ccn_charbuf_create();
141 ccn_uri_append(temp1, temp->buf, temp->length, 0);
142
akmhoque7adb2772013-03-05 16:30:59 -0600143 name_part->name=(char *)calloc(strlen(ccn_charbuf_as_string(temp1))+1,
144 sizeof(char));
145 memcpy(name_part->name,ccn_charbuf_as_string(temp1),
146 strlen(ccn_charbuf_as_string(temp1)));
akmhoquef6773612013-02-23 09:53:00 -0600147 name_part->name[strlen(ccn_charbuf_as_string(temp1))]='\0';
148 name_part->length=strlen(ccn_charbuf_as_string(temp1))+1;
149
150 ccn_charbuf_destroy(&temp1);
151 ccn_charbuf_destroy(&temp);
akmhoque0b60ba92013-02-25 17:55:35 -0600152 ccn_indexbuf_destroy(&components);
akmhoquef6773612013-02-23 09:53:00 -0600153
154 if ( nlsr->debugging )
155 printf("Name Part: %s \n",name_part->name);
156
akmhoquea0e71152013-02-11 09:47:59 -0600157}
158
akmhoquea0e71152013-02-11 09:47:59 -0600159
akmhoquea0e71152013-02-11 09:47:59 -0600160
161
162
akmhoque7adb2772013-03-05 16:30:59 -0600163int
164get_content_by_content_name(char *content_name, unsigned char **content_data,
165 char *orig_router)
akmhoquea0e71152013-02-11 09:47:59 -0600166{
akmhoque7adb2772013-03-05 16:30:59 -0600167
168 int ret=-1;
Obaid Amin806df812013-02-21 13:30:26 -0600169 struct ccn_charbuf *name = NULL;
170 struct ccn_charbuf *templ = NULL;
171 struct ccn_charbuf *resultbuf = NULL;
172 struct ccn_parsed_ContentObject pcobuf = { 0 };
173 int res;
akmhoque7adb2772013-03-05 16:30:59 -0600174 int allow_stale = 1;
Obaid Amin806df812013-02-21 13:30:26 -0600175 int content_only = 1;
176 int scope = -1;
akmhoque7adb2772013-03-05 16:30:59 -0600177 const unsigned char *ptr,*ptr_in;
178 size_t length,length_in;
Obaid Amin806df812013-02-21 13:30:26 -0600179 int resolve_version = CCN_V_HIGHEST;
180 int timeout_ms = 3000;
181 const unsigned lifetime_default = CCN_INTEREST_LIFETIME_SEC << 12;
182 unsigned lifetime_l12 = lifetime_default;
183 int get_flags = 0;
akmhoquea0e71152013-02-11 09:47:59 -0600184
Obaid Amin806df812013-02-21 13:30:26 -0600185 name = ccn_charbuf_create();
186 res = ccn_name_from_uri(name,content_name);
187 if (res < 0) {
188 fprintf(stderr, "Bad ccn URI: %s\n", content_name);
akmhoque7adb2772013-03-05 16:30:59 -0600189 ccn_charbuf_destroy(&name);
190 return ret;
Obaid Amin806df812013-02-21 13:30:26 -0600191 }
akmhoquea0e71152013-02-11 09:47:59 -0600192
Obaid Amin806df812013-02-21 13:30:26 -0600193 if (allow_stale || lifetime_l12 != lifetime_default || scope != -1) {
194 templ = ccn_charbuf_create();
195 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
196 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
197 ccn_charbuf_append_closer(templ); /* </Name> */
198 if (allow_stale) {
199 ccn_charbuf_append_tt(templ, CCN_DTAG_AnswerOriginKind, CCN_DTAG);
200 ccnb_append_number(templ,
201 CCN_AOK_DEFAULT | CCN_AOK_STALE);
202 ccn_charbuf_append_closer(templ); /* </AnswerOriginKind> */
203 }
204 if (scope != -1) {
205 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
206 }
207 if (lifetime_l12 != lifetime_default) {
208 /*
209 * Choose the interest lifetime so there are at least 3
210 * expressions (in the unsatisfied case).
211 */
212 unsigned char buf[3] = { 0 };
213 int i;
214 for (i = sizeof(buf) - 1; i >= 0; i--, lifetime_l12 >>= 8)
215 buf[i] = lifetime_l12 & 0xff;
216 ccnb_append_tagged_blob(templ, CCN_DTAG_InterestLifetime, buf,
217 sizeof(buf));
218 }
219 ccn_charbuf_append_closer(templ); /* </Interest> */
akmhoquea0e71152013-02-11 09:47:59 -0600220 }
Obaid Amin806df812013-02-21 13:30:26 -0600221 resultbuf = ccn_charbuf_create();
222 if (resolve_version != 0) {
223 res = ccn_resolve_version(nlsr->ccn, name, resolve_version, 500);
224 if (res >= 0) {
225 ccn_uri_append(resultbuf, name->buf, name->length, 1);
226 resultbuf->length = 0;
227 }
akmhoquea0e71152013-02-11 09:47:59 -0600228 }
Obaid Amin806df812013-02-21 13:30:26 -0600229 res = ccn_get(nlsr->ccn, name, templ, timeout_ms, resultbuf, &pcobuf, NULL,
230 get_flags);
akmhoquea0e71152013-02-11 09:47:59 -0600231 if (res >= 0) {
Obaid Amin806df812013-02-21 13:30:26 -0600232 ptr = resultbuf->buf;
233 length = resultbuf->length;
234 if (content_only){
235 ccn_content_get_value(ptr, length, &pcobuf, &ptr, &length);
akmhoque7adb2772013-03-05 16:30:59 -0600236 struct ccn_parsed_ContentObject pcobuf1 = { 0 };
237 int chk_cont=ccn_parse_ContentObject(ptr,length,&pcobuf1,NULL);
238 printf("Content Parsing result: %d\n",chk_cont);
239 if ( contain_key_name(ptr, &pcobuf1) == 1){
240 struct ccn_charbuf *key_name=get_key_name(ptr, &pcobuf1);
241 struct ccn_charbuf *orig_router_kn=ccn_charbuf_create();
242 res=get_orig_router_from_key_name(orig_router_kn,key_name);
243 if( res == 0){
244 struct ccn_charbuf *rtr_uri = ccn_charbuf_create();
245 ccn_uri_append(rtr_uri, orig_router_kn->buf,
246 orig_router_kn->length, 0);
247 printf("Orig Router from Key name: %s\n",
248 ccn_charbuf_as_string(rtr_uri));
249
250 if( strcmp(orig_router,ccn_charbuf_as_string(rtr_uri)) == 0){
251
akmhoque6e2ba842013-03-05 19:35:26 -0600252 int res_verify=verify_key(ptr,&pcobuf1);
akmhoque7adb2772013-03-05 16:30:59 -0600253
254 if ( res_verify != 0 ){
255 printf("Error in verfiying keys !! :( \n");
256 }
257 else{
258 printf("Key verification is successful :)\n");
259 ptr_in=ptr;
260 length_in=length;
261 ccn_content_get_value(ptr_in, length_in, &pcobuf1,
262 &ptr_in, &length_in);
263 *content_data = (unsigned char *) calloc(length_in,
264 sizeof(char *));
265 memcpy (*content_data, ptr_in, length_in);
266 ret=0;
267 }
268 }
269 ccn_charbuf_destroy(&rtr_uri);
270 }
271 ccn_charbuf_destroy(&key_name);
272 ccn_charbuf_destroy(&orig_router_kn);
273 }
Obaid Amin806df812013-02-21 13:30:26 -0600274 }
akmhoquea0e71152013-02-11 09:47:59 -0600275 }
akmhoque7adb2772013-03-05 16:30:59 -0600276
Obaid Amin806df812013-02-21 13:30:26 -0600277 ccn_charbuf_destroy(&resultbuf);
278 ccn_charbuf_destroy(&templ);
akmhoque7adb2772013-03-05 16:30:59 -0600279 ccn_charbuf_destroy(&name);
280
281 return ret;
akmhoquea0e71152013-02-11 09:47:59 -0600282}
283
akmhoqueedb68d92013-03-05 10:18:16 -0600284void
akmhoquea0e71152013-02-11 09:47:59 -0600285process_incoming_sync_content_lsa( unsigned char *content_data)
286{
287
288
Obaid Amin806df812013-02-21 13:30:26 -0600289 if ( nlsr->debugging )
290 printf("process_incoming_sync_content_lsa called \n");
Obaid Aminbb8ac422013-02-20 17:08:48 -0600291
Obaid Amin806df812013-02-21 13:30:26 -0600292 char *sep="|";
293 char *rem;
294 char *orig_router;
295 char *orl;
296 int orig_router_length;
297 char *lst;
298 int ls_type;
299 char *lsid;
300 long int ls_id;
301 char *isvld;
302 int isValid;
303 char *num_link;
304 int no_link;
305 char *np;
306 char *np_length;
307 int name_length;
308 char *data;
309 char *orig_time;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600310
311
akmhoquea0e71152013-02-11 09:47:59 -0600312 if ( nlsr->debugging )
Obaid Amin806df812013-02-21 13:30:26 -0600313 printf("LSA Data \n");
314
315 if( strlen((char *)content_data ) > 0 )
akmhoquea0e71152013-02-11 09:47:59 -0600316 {
Obaid Aminbb8ac422013-02-20 17:08:48 -0600317
Obaid Amin806df812013-02-21 13:30:26 -0600318 orig_router=strtok_r((char *)content_data,sep,&rem);
319 orl=strtok_r(NULL,sep,&rem);
320 orig_router_length=atoi(orl);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600321
Obaid Amin806df812013-02-21 13:30:26 -0600322 if ( nlsr->debugging )
323 {
324 printf(" Orig Router Name : %s\n",orig_router);
325 printf(" Orig Router Length: %d\n",orig_router_length);
326 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600327
Obaid Amin806df812013-02-21 13:30:26 -0600328 lst=strtok_r(NULL,sep,&rem);
329 ls_type=atoi(lst);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600330
Obaid Amin806df812013-02-21 13:30:26 -0600331 if ( nlsr->debugging )
332 printf(" LS Type : %d\n",ls_type);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600333
Obaid Amin806df812013-02-21 13:30:26 -0600334 if ( ls_type == LS_TYPE_NAME )
335 {
336 lsid=strtok_r(NULL,sep,&rem);
337 ls_id=atoi(lsid);
338 orig_time=strtok_r(NULL,sep,&rem);
339 isvld=strtok_r(NULL,sep,&rem);
340 isValid=atoi(isvld);
341 np=strtok_r(NULL,sep,&rem);
342 np_length=strtok_r(NULL,sep,&rem);
343 name_length=atoi(np_length);
344 if ( nlsr->debugging )
345 {
346 printf(" LS ID : %ld\n",ls_id);
347 printf(" isValid : %d\n",isValid);
348 printf(" Name Prefix : %s\n",np);
349 printf(" Orig Time : %s\n",orig_time);
350 printf(" Name Prefix length: %d\n",name_length);
351 }
352
akmhoque7adb2772013-03-05 16:30:59 -0600353 build_and_install_others_name_lsa(orig_router,ls_type,ls_id,
354 orig_time,isValid,np);
Obaid Amin806df812013-02-21 13:30:26 -0600355
356 print_name_lsdb();
357
358 }
359 else if ( ls_type == LS_TYPE_ADJ )
360 {
361 orig_time=strtok_r(NULL,sep,&rem);
362 num_link=strtok_r(NULL,sep,&rem);
363 no_link=atoi(num_link);
364 data=rem;
365
366 if ( nlsr->debugging )
367 {
368 printf(" Orig Time : %s\n",orig_time);
369 printf(" No Link : %d\n",no_link);
370 printf(" Data : %s\n",data);
371 }
akmhoque7adb2772013-03-05 16:30:59 -0600372 build_and_install_others_adj_lsa(orig_router,ls_type,orig_time,
373 no_link,data);
Obaid Amin806df812013-02-21 13:30:26 -0600374 }
375 else if ( ls_type == LS_TYPE_COR )
376 {
377 orig_time=strtok_r(NULL,sep,&rem);
378 char *cor_r=strtok_r(NULL,sep,&rem);
379 char *cor_theta=strtok_r(NULL,sep,&rem);
380
381 double r, theta;
akmhoque8034d502013-02-24 11:53:24 -0600382 r=strtod(cor_r,NULL);
383 theta=strtod(cor_theta,NULL);
Obaid Amin806df812013-02-21 13:30:26 -0600384
385 if ( nlsr->debugging )
386 {
387 printf(" Orig Time : %s\n",orig_time);
388 printf(" Cor R : %f\n",r);
389 printf(" Cor Theta : %f\n",theta);
390 }
akmhoque7adb2772013-03-05 16:30:59 -0600391 build_and_install_others_cor_lsa(orig_router,ls_type,orig_time,
392 (double)r, (double)theta);
Obaid Amin806df812013-02-21 13:30:26 -0600393 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600394
395 }
akmhoquea0e71152013-02-11 09:47:59 -0600396}
397
akmhoque7adb2772013-03-05 16:30:59 -0600398void
Obaid Amin7997ad82013-02-22 16:41:56 -0600399process_content_from_sync (struct ccn_charbuf *content_name,
400 struct ccn_indexbuf *components)
akmhoquea0e71152013-02-11 09:47:59 -0600401{
akmhoque8c5e2662013-02-24 06:37:14 -0600402 if (nlsr->debugging)
403 printf("process_content_from_sync called \n");
Obaid Amin806df812013-02-21 13:30:26 -0600404 size_t comp_size;
405 char *lst;
406 char *lsid;
407 const unsigned char *second_last_comp;
408 const unsigned char *third_last_comp;
409 const unsigned char *origtime;
410 char *sep=".";
411 char *rem;
412 char *second_comp_type;
akmhoquea0e71152013-02-11 09:47:59 -0600413
Obaid Amin806df812013-02-21 13:30:26 -0600414 int ls_type;
415 long int ls_id=0;
akmhoquea0e71152013-02-11 09:47:59 -0600416
Obaid Amin806df812013-02-21 13:30:26 -0600417 unsigned char *content_data = NULL;
akmhoquea0e71152013-02-11 09:47:59 -0600418
akmhoqueb31caa12013-02-22 08:42:09 -0600419 char *time_stamp=get_current_timestamp_micro_v2();
akmhoquea0e71152013-02-11 09:47:59 -0600420
Obaid Amin806df812013-02-21 13:30:26 -0600421 struct ccn_charbuf *uri = ccn_charbuf_create();
422 ccn_uri_append(uri, content_name->buf, content_name->length, 0);
akmhoquea0e71152013-02-11 09:47:59 -0600423
Obaid Amin7997ad82013-02-22 16:41:56 -0600424 struct name_prefix *orig_router=(struct name_prefix *)
425 calloc( 1, sizeof(struct name_prefix));
akmhoquea0e71152013-02-11 09:47:59 -0600426
Obaid Amin7997ad82013-02-22 16:41:56 -0600427 ccn_name_comp_get( content_name->buf, components,
428 components->n-1-2, &second_last_comp, &comp_size);
429
Obaid Amin806df812013-02-21 13:30:26 -0600430 if (nlsr->debugging)
Obaid Amin7997ad82013-02-22 16:41:56 -0600431 printf("2nd Last Component: %s \n", second_last_comp);
akmhoquea0e71152013-02-11 09:47:59 -0600432
Obaid Amin7997ad82013-02-22 16:41:56 -0600433 second_comp_type=strtok_r((char *)second_last_comp, sep, &rem);
434 if (second_comp_type == NULL || rem == NULL)
435 {
436 printf ("Error: unable to tokenize the string: %s, calling exit()\n",
437 (char *)second_last_comp);
438 exit(0);
439 }
440
Obaid Amin806df812013-02-21 13:30:26 -0600441 if ( strcmp( second_comp_type, "lsId" ) == 0 )
442 {
443 lsid=rem;
444 ls_id=atoi(rem);
akmhoque7adb2772013-03-05 16:30:59 -0600445 ccn_name_comp_get(content_name->buf, components,components->n-2-2,
446 &third_last_comp, &comp_size);
Obaid Amin806df812013-02-21 13:30:26 -0600447 lst=strtok_r((char *)third_last_comp,sep,&rem);
448 lst=rem;
449 ls_type=atoi(lst);
akmhoque7adb2772013-03-05 16:30:59 -0600450 ccn_name_comp_get(content_name->buf, components,components->n-2,
451 &origtime, &comp_size);
Obaid Amin806df812013-02-21 13:30:26 -0600452 ccn_name_chop(content_name,components,-3);
453 get_name_part(orig_router,content_name,components,0);
akmhoquea0e71152013-02-11 09:47:59 -0600454
Obaid Aminbb8ac422013-02-20 17:08:48 -0600455 if ( nlsr->debugging )
akmhoque7adb2772013-03-05 16:30:59 -0600456 printf("Orig Router: %s Ls Type: %d Ls id: %ld Orig Time: %s\n",
457 orig_router->name,ls_type,ls_id,origtime);
akmhoquea0e71152013-02-11 09:47:59 -0600458
Obaid Amin806df812013-02-21 13:30:26 -0600459 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque8c5e2662013-02-24 06:37:14 -0600460 if ( nlsr->debugging )
461 printf("LSA Life time: %d\n",lsa_life_time);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600462
akmhoque7adb2772013-03-05 16:30:59 -0600463 if ( (strcmp(orig_router->name,nlsr->router_name) == 0 &&
464 lsa_life_time < nlsr->lsa_refresh_time)
465 || (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
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600529 if ( (strcmp(orig_router->name,nlsr->router_name) == 0 &&
530 lsa_life_time < nlsr->lsa_refresh_time) ||
531 (strcmp(orig_router->name,nlsr->router_name) != 0 &&
532 lsa_life_time < nlsr->router_dead_interval) )
Obaid Amin806df812013-02-21 13:30:26 -0600533 {
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600534 int is_new_adj_lsa = check_is_new_adj_lsa( orig_router->name,
535 (char *)lst, (char *)origtime);
Obaid Amin806df812013-02-21 13:30:26 -0600536 if ( is_new_adj_lsa == 1 )
537 {
538 if ( nlsr->debugging )
539 printf("New Adj LSA.....\n");
akmhoque7adb2772013-03-05 16:30:59 -0600540 int chk_con=get_content_by_content_name(ccn_charbuf_as_string(uri),
541 &content_data,orig_router->name);
Obaid Amin806df812013-02-21 13:30:26 -0600542
akmhoque7adb2772013-03-05 16:30:59 -0600543 if ( chk_con == 0 ){
544 if ( nlsr->debugging )
545 printf("Content Data: %s \n",content_data);
546 process_incoming_sync_content_lsa(content_data);
547 }
548 else{
549 if ( nlsr->debugging )
550 printf("Verification failed. No content given back\n");
551 }
Obaid Amin806df812013-02-21 13:30:26 -0600552 }
553 else
554 {
555 if ( nlsr->debugging )
556 printf("Adj LSA / Newer Adj LSA already exists in LSDB\n");
akmhoque7adb2772013-03-05 16:30:59 -0600557 int chk_con=get_content_by_content_name(ccn_charbuf_as_string(uri),
558 &content_data,orig_router->name);
559 if ( chk_con == 0 ){
560 if ( nlsr->debugging )
561 printf("Content Data: %s \n",content_data);
562 process_incoming_sync_content_lsa(content_data);
563 }
564 else{
565 if ( nlsr->debugging )
566 printf("Verification failed. No content given back\n");
567 }
Obaid Amin806df812013-02-21 13:30:26 -0600568 }
569 }
570 else
571 {
572 if ( nlsr->debugging )
573 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
574 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600575 }
Obaid Amin806df812013-02-21 13:30:26 -0600576 else if(ls_type == LS_TYPE_COR)
Obaid Aminbb8ac422013-02-20 17:08:48 -0600577 {
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600578 ccn_name_comp_get(content_name->buf, components, components->n-2,
579 &origtime, &comp_size);
Obaid Amin806df812013-02-21 13:30:26 -0600580 ccn_name_chop(content_name,components,-2);
581 get_name_part(orig_router,content_name,components,0);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600582
Obaid Amin806df812013-02-21 13:30:26 -0600583 if ( nlsr->debugging )
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600584 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",
585 orig_router->name,ls_type,origtime);
Obaid Amin806df812013-02-21 13:30:26 -0600586
587 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque8c5e2662013-02-24 06:37:14 -0600588 if ( nlsr->debugging )
589 printf("LSA Life time: %d\n",lsa_life_time);
590
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600591 if ( (strcmp(orig_router->name,nlsr->router_name) == 0 &&
592 lsa_life_time < nlsr->lsa_refresh_time) ||
593 (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
akmhoquef6773612013-02-23 09:53:00 -0600642 if (orig_router->name)
643 free(orig_router->name);
Obaid Amin05300892013-02-21 13:45:25 -0600644 if (orig_router)
645 free(orig_router);
Obaid Amin806df812013-02-21 13:30:26 -0600646 ccn_charbuf_destroy(&uri);
647 //01/31/2013
648 free(time_stamp);
akmhoquea0e71152013-02-11 09:47:59 -0600649}
650
Obaid Amin7793ba72013-02-22 00:43:58 -0600651 int
akmhoquea0e71152013-02-11 09:47:59 -0600652sync_monitor(char *topo_prefix, char *slice_prefix)
653{
654
Obaid Amin806df812013-02-21 13:30:26 -0600655 struct ccn_charbuf *prefix = ccn_charbuf_create();
Obaid Amin806df812013-02-21 13:30:26 -0600656 struct ccn_charbuf *topo = ccn_charbuf_create();
Obaid Amin7997ad82013-02-22 16:41:56 -0600657
658 nlsr->closure=(struct ccns_name_closure *)
akmhoquececba942013-02-25 17:33:34 -0600659 calloc(1,sizeof(struct ccns_name_closure)); // leak
Obaid Amin7997ad82013-02-22 16:41:56 -0600660
Obaid Amin806df812013-02-21 13:30:26 -0600661 nlsr->slice = ccns_slice_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600662
Obaid Amin806df812013-02-21 13:30:26 -0600663 ccn_charbuf_reset(prefix);
664 ccn_name_from_uri(prefix, slice_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600665
Obaid Amin806df812013-02-21 13:30:26 -0600666 ccn_charbuf_reset(topo);
667 ccn_name_from_uri(topo, topo_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600668
Obaid Amin806df812013-02-21 13:30:26 -0600669 ccns_slice_set_topo_prefix(nlsr->slice, topo, prefix);
670 nlsr->closure->callback = &sync_cb;
akmhoque54d86112013-02-21 16:42:34 -0600671 nlsr->ccns = ccns_open(nlsr->ccn, nlsr->slice, nlsr->closure, NULL, NULL);
akmhoquea0e71152013-02-11 09:47:59 -0600672
Obaid Amin7997ad82013-02-22 16:41:56 -0600673 ccn_charbuf_destroy(&prefix);
674 ccn_charbuf_destroy(&topo);
Obaid Amin806df812013-02-21 13:30:26 -0600675 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600676}
677
Obaid Amin7793ba72013-02-22 00:43:58 -0600678 struct ccn_charbuf *
akmhoquea0e71152013-02-11 09:47:59 -0600679make_template(int scope)
680{
Obaid Amin806df812013-02-21 13:30:26 -0600681 struct ccn_charbuf *templ = NULL;
682 templ = ccn_charbuf_create();
683 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
684 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
685 ccn_charbuf_append_closer(templ); /* </Name> */
686 if (0 <= scope && scope <= 2)
687 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
688 ccn_charbuf_append_closer(templ); /* </Interest> */
689 return(templ);
akmhoquea0e71152013-02-11 09:47:59 -0600690}
691
Obaid Amin7793ba72013-02-22 00:43:58 -0600692 int
akmhoquea0e71152013-02-11 09:47:59 -0600693write_data_to_repo(char *data, char *name_prefix)
694{
Obaid Amin806df812013-02-21 13:30:26 -0600695 if ( nlsr->debugging )
696 {
697 printf("write_data_to_repo called\n");
698 printf("Content Name: %s \n",name_prefix);
699 printf("Content Data: %s \n",data);
700 }
akmhoquea0e71152013-02-11 09:47:59 -0600701
Obaid Aminf210a322013-02-22 02:04:52 -0600702 struct ccn *temp_ccn;
Obaid Amin806df812013-02-21 13:30:26 -0600703 temp_ccn=ccn_create();
704 int ccn_fd=ccn_connect(temp_ccn, NULL);
705 if(ccn_fd == -1)
706 {
707 fprintf(stderr,"Could not connect to ccnd for Data Writing\n");
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600708 writeLogg(__FILE__,__FUNCTION__,__LINE__,
709 "Could not connect to ccnd for Data Writing\n");
Obaid Amin806df812013-02-21 13:30:26 -0600710 return -1;
711 }
Obaid Aminf210a322013-02-22 02:04:52 -0600712
Obaid Amin806df812013-02-21 13:30:26 -0600713 struct ccn_charbuf *name = NULL;
714 struct ccn_seqwriter *w = NULL;
715 int blocksize = 4096;
716 int freshness = -1;
Obaid Amin806df812013-02-21 13:30:26 -0600717 int scope = 1;
718 int res;
719 size_t blockread;
720 struct ccn_charbuf *templ;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600721
Obaid Amin806df812013-02-21 13:30:26 -0600722 name = ccn_charbuf_create();
723 res = ccn_name_from_uri(name, name_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600724 if (res < 0) {
Obaid Amin806df812013-02-21 13:30:26 -0600725 fprintf(stderr, "bad CCN URI: %s\n",name_prefix);
726 return -1;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600727 }
Obaid Amin806df812013-02-21 13:30:26 -0600728
Obaid Aminf210a322013-02-22 02:04:52 -0600729 w = ccn_seqw_create(temp_ccn, name);
Obaid Amin806df812013-02-21 13:30:26 -0600730 if (w == NULL) {
731 fprintf(stderr, "ccn_seqw_create failed\n");
732 return -1;
733 }
734 ccn_seqw_set_block_limits(w, blocksize, blocksize);
735 if (freshness > -1)
736 ccn_seqw_set_freshness(w, freshness);
akmhoquea0e71152013-02-11 09:47:59 -0600737
Obaid Amin7997ad82013-02-22 16:41:56 -0600738 struct ccn_charbuf *name_v = ccn_charbuf_create();
739 ccn_seqw_get_name(w, name_v);
740 ccn_name_from_uri(name_v, "%C1.R.sw");
741 ccn_name_append_nonce(name_v);
742 templ = make_template(scope);
743 res = ccn_get(temp_ccn, name_v, templ, 60000, NULL, NULL, NULL, 0);
744 ccn_charbuf_destroy(&templ);
745 ccn_charbuf_destroy(&name_v);
746 if (res < 0) {
747 fprintf(stderr, "No response from repository\n");
748 return -1;
749 }
akmhoquea0e71152013-02-11 09:47:59 -0600750
akmhoque7adb2772013-03-05 16:30:59 -0600751 struct ccn_charbuf *resultbuf=ccn_charbuf_create();
akmhoquea0e71152013-02-11 09:47:59 -0600752
akmhoque7adb2772013-03-05 16:30:59 -0600753 sign_content_with_user_defined_keystore(name,
754 resultbuf,
755 data,
756 strlen(data),
757 nlsr->keystore_path,
758 nlsr->keystore_passphrase,
759 nlsr->root_key_prefix,
760 nlsr->site_name,
761 nlsr->router_name);
762
763
764 //blockread = 0;
765 //blockread=strlen(data)+1;
766 blockread=resultbuf->length;
akmhoquea0e71152013-02-11 09:47:59 -0600767
Obaid Amin806df812013-02-21 13:30:26 -0600768 if (blockread > 0) {
akmhoque7adb2772013-03-05 16:30:59 -0600769 //res = ccn_seqw_write(w, data, blockread);
770 res = ccn_seqw_write(w, resultbuf->buf, resultbuf->length);
Obaid Amin806df812013-02-21 13:30:26 -0600771 while (res == -1) {
akmhoque8034d502013-02-24 11:53:24 -0600772 ccn_run(temp_ccn,100);
akmhoque7adb2772013-03-05 16:30:59 -0600773 //res = ccn_seqw_write(w, data, blockread);
774 res = ccn_seqw_write(w, resultbuf->buf, resultbuf->length);
Obaid Amin806df812013-02-21 13:30:26 -0600775 }
776 }
777
akmhoque8034d502013-02-24 11:53:24 -0600778 ccn_seqw_close(w);
779 ccn_run(temp_ccn, 100);
Obaid Amin806df812013-02-21 13:30:26 -0600780 ccn_charbuf_destroy(&name);
akmhoque0ed6d982013-02-22 14:28:01 -0600781 ccn_destroy(&temp_ccn);
akmhoque6e2ba842013-03-05 19:35:26 -0600782 ccn_charbuf_destroy(&resultbuf);
akmhoquea0e71152013-02-11 09:47:59 -0600783
Obaid Amin806df812013-02-21 13:30:26 -0600784 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600785}
Obaid Amin806df812013-02-21 13:30:26 -0600786 int
Obaid Amin7997ad82013-02-22 16:41:56 -0600787 create_sync_slice(char *topo_prefix, char *slice_prefix)
akmhoquea0e71152013-02-11 09:47:59 -0600788{
Obaid Amin806df812013-02-21 13:30:26 -0600789 int res;
akmhoque7adb2772013-03-05 16:30:59 -0600790 struct ccn *handle;
Obaid Amin806df812013-02-21 13:30:26 -0600791 struct ccns_slice *slice;
792 struct ccn_charbuf *prefix = ccn_charbuf_create();
793 struct ccn_charbuf *topo = ccn_charbuf_create();
794 struct ccn_charbuf *clause = ccn_charbuf_create();
795 struct ccn_charbuf *slice_name = ccn_charbuf_create();
796 struct ccn_charbuf *slice_uri = ccn_charbuf_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600797
Obaid Amin806df812013-02-21 13:30:26 -0600798 if (prefix == NULL || topo == NULL || clause == NULL ||
799 slice_name == NULL || slice_uri == NULL) {
800 fprintf(stderr, "Unable to allocate required memory.\n");
801 return -1;
802 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600803
akmhoque0ed6d982013-02-22 14:28:01 -0600804 handle = ccn_create();
Obaid Amin806df812013-02-21 13:30:26 -0600805 res = ccn_connect(handle, NULL);
806 if (0 > res) {
807 fprintf(stderr, "Unable to connect to ccnd.\n");
808 return -1;
809 }
akmhoque0ed6d982013-02-22 14:28:01 -0600810
Obaid Amin806df812013-02-21 13:30:26 -0600811 slice = ccns_slice_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600812
Obaid Amin806df812013-02-21 13:30:26 -0600813 ccn_charbuf_reset(topo);
814 ccn_name_from_uri(topo, topo_prefix);
815 ccn_charbuf_reset(prefix);
816 ccn_name_from_uri(prefix,slice_prefix );
817 ccns_slice_set_topo_prefix(slice, topo, prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600818
819
akmhoque0ed6d982013-02-22 14:28:01 -0600820 res = ccns_write_slice(handle, slice, slice_name);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600821
Obaid Amin806df812013-02-21 13:30:26 -0600822 //01/31/2013
823 ccns_slice_destroy(&slice);
akmhoque0ed6d982013-02-22 14:28:01 -0600824 ccn_destroy(&handle);
Obaid Amin806df812013-02-21 13:30:26 -0600825 ccn_charbuf_destroy(&prefix);
826 ccn_charbuf_destroy(&topo);
827 ccn_charbuf_destroy(&clause);
828 ccn_charbuf_destroy(&slice_name);
829 ccn_charbuf_destroy(&slice_uri);
akmhoquea0e71152013-02-11 09:47:59 -0600830
Obaid Amin806df812013-02-21 13:30:26 -0600831 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600832}
833
akmhoque0eb8de22013-02-24 14:08:19 -0600834