blob: 65caed2c51f66ee3ac833f24fc9181bc644ddda7 [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"
32
33
Obaid Amin806df812013-02-21 13:30:26 -060034 char *
akmhoquea0e71152013-02-11 09:47:59 -060035hex_string(unsigned char *s, size_t l)
36{
Obaid Amin806df812013-02-21 13:30:26 -060037 const char *hex_digits = "0123456789abcdef";
38 char *r;
39 int i;
40 r = calloc(1, 1 + 2 * l);
41 for (i = 0; i < l; i++) {
42 r[2*i] = hex_digits[(s[i]>>4) & 0xf];
43 r[1+2*i] = hex_digits[s[i] & 0xf];
44 }
45 return(r);
akmhoquea0e71152013-02-11 09:47:59 -060046}
47
Obaid Amin806df812013-02-21 13:30:26 -060048 int
akmhoquea0e71152013-02-11 09:47:59 -060049sync_cb(struct ccns_name_closure *nc,
Obaid Amin806df812013-02-21 13:30:26 -060050 struct ccn_charbuf *lhash,
51 struct ccn_charbuf *rhash,
52 struct ccn_charbuf *name)
akmhoquea0e71152013-02-11 09:47:59 -060053{
Obaid Amin806df812013-02-21 13:30:26 -060054 int res;
Obaid Amin7997ad82013-02-22 16:41:56 -060055 struct ccn_charbuf *content_name;
56 struct ccn_indexbuf *content_comps;
57 struct ccn_indexbuf *name_comps;
58
59 content_comps = ccn_indexbuf_create();
60 res = ccn_name_split(name, content_comps);
Obaid Amin806df812013-02-21 13:30:26 -060061 if ( res < 0 )
62 return 0;
Obaid Amin7997ad82013-02-22 16:41:56 -060063
64 if (content_comps->n < 2)
Obaid Amin806df812013-02-21 13:30:26 -060065 return 0;
Obaid Amin7997ad82013-02-22 16:41:56 -060066
67 content_name = ccn_charbuf_create();
68 ccn_name_init(content_name);
69
70 res = ccn_name_append_components( content_name, name->buf,
71 content_comps->buf[0], content_comps->buf[content_comps->n - 1]);
Obaid Amin806df812013-02-21 13:30:26 -060072
73 if ( res < 0)
74 return 0;
akmhoquea0e71152013-02-11 09:47:59 -060075
Obaid Amin7997ad82013-02-22 16:41:56 -060076 // for debugging
Obaid Amin806df812013-02-21 13:30:26 -060077 struct ccn_charbuf *temp=ccn_charbuf_create();
78 ccn_uri_append(temp, content_name->buf, content_name->length, 0);
79 if ( nlsr->debugging )
80 printf("Name before chopping: %s \n",ccn_charbuf_as_string(temp));
81 ccn_charbuf_destroy(&temp);
akmhoquea0e71152013-02-11 09:47:59 -060082
Obaid Amin7997ad82013-02-22 16:41:56 -060083 name_comps = ccn_indexbuf_create();
84 res=ccn_name_split (content_name, name_comps);
85 if (res < 0)
Obaid Amin806df812013-02-21 13:30:26 -060086 return 0;
akmhoquea0e71152013-02-11 09:47:59 -060087
Obaid Amin806df812013-02-21 13:30:26 -060088 if ( nlsr->debugging )
89 {
90 printf("Number of components in name = %d \n",res);
91 printf("Number of components in name as indexbuf->n = %d \n",
Obaid Amin7997ad82013-02-22 16:41:56 -060092 (int)name_comps->n);
Obaid Amin806df812013-02-21 13:30:26 -060093 }
Obaid Amin7997ad82013-02-22 16:41:56 -060094
95 ccn_name_chop(content_name, name_comps, -3);
Obaid Amin806df812013-02-21 13:30:26 -060096 if ( nlsr->debugging )
97 printf("Number of components in name as indexbuf->n after chopping= %d \n"
Obaid Amin7997ad82013-02-22 16:41:56 -060098 , (int)name_comps->n);
akmhoquea0e71152013-02-11 09:47:59 -060099
Obaid Amin7997ad82013-02-22 16:41:56 -0600100 //for debugging
Obaid Amin806df812013-02-21 13:30:26 -0600101 struct ccn_charbuf *temp1=ccn_charbuf_create();
102 ccn_uri_append(temp1, content_name->buf, content_name->length, 0);
103 if ( nlsr->debugging )
104 printf("Name after chopping: %s \n",ccn_charbuf_as_string(temp1));
105 ccn_charbuf_destroy(&temp1);
akmhoquea0e71152013-02-11 09:47:59 -0600106
Obaid Amin7997ad82013-02-22 16:41:56 -0600107 //main method which process contents from the sync.
108 process_content_from_sync(content_name, name_comps);
109
Obaid Amin806df812013-02-21 13:30:26 -0600110 ccn_charbuf_destroy(&content_name);
Obaid Amin7997ad82013-02-22 16:41:56 -0600111 ccn_indexbuf_destroy(&content_comps);
112 ccn_indexbuf_destroy(&name_comps);
akmhoquea0e71152013-02-11 09:47:59 -0600113
Obaid Amin806df812013-02-21 13:30:26 -0600114 return(0);
akmhoquea0e71152013-02-11 09:47:59 -0600115}
116
117
akmhoqueccb33e92013-02-20 11:44:28 -0600118
Obaid Amin7793ba72013-02-22 00:43:58 -0600119 void
akmhoqueccb33e92013-02-20 11:44:28 -0600120get_name_part(struct name_prefix *name_part,struct ccn_charbuf * interest_ccnb,
Obaid Amin806df812013-02-21 13:30:26 -0600121 struct ccn_indexbuf *interest_comps, int offset)
akmhoquea0e71152013-02-11 09:47:59 -0600122{
akmhoquef6773612013-02-23 09:53:00 -0600123 //int i;
Obaid Amin806df812013-02-21 13:30:26 -0600124 int lsa_position=0;
akmhoquef6773612013-02-23 09:53:00 -0600125 //int len=0;
akmhoquea0e71152013-02-11 09:47:59 -0600126
Obaid Amin806df812013-02-21 13:30:26 -0600127 struct ccn_indexbuf cid={0};
128 struct ccn_indexbuf *components=&cid;
129 struct ccn_charbuf *name=ccn_charbuf_create();
130 ccn_name_from_uri(name,nlsr->slice_prefix);
131 ccn_name_split (name, components);
132 lsa_position=components->n-2;
Obaid Amin806df812013-02-21 13:30:26 -0600133 ccn_charbuf_destroy(&name);
akmhoquea0e71152013-02-11 09:47:59 -0600134
akmhoquef6773612013-02-23 09:53:00 -0600135 struct ccn_charbuf *temp=ccn_charbuf_create();
136 ccn_name_init(temp);
137 ccn_name_append_components( temp, interest_ccnb->buf,
138 interest_comps->buf[lsa_position+1], interest_comps->buf[interest_comps->n - 1]);
139
140 struct ccn_charbuf *temp1=ccn_charbuf_create();
141 ccn_uri_append(temp1, temp->buf, temp->length, 0);
142
143 name_part->name=(char *)calloc(strlen(ccn_charbuf_as_string(temp1))+1,sizeof(char));
144 memcpy(name_part->name,ccn_charbuf_as_string(temp1),strlen(ccn_charbuf_as_string(temp1)));
145 name_part->name[strlen(ccn_charbuf_as_string(temp1))]='\0';
146 name_part->length=strlen(ccn_charbuf_as_string(temp1))+1;
147
148 ccn_charbuf_destroy(&temp1);
149 ccn_charbuf_destroy(&temp);
150
151 if ( nlsr->debugging )
152 printf("Name Part: %s \n",name_part->name);
153
154
155 /*
Obaid Amin806df812013-02-21 13:30:26 -0600156 const unsigned char *comp_ptr1;
157 size_t comp_size;
158 for(i=lsa_position+1+offset;i<interest_comps->n-1;i++)
159 {
160 ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1,
161 &comp_size);
162 len+=1;
163 len+=(int)comp_size;
164 }
165 len++;
akmhoquea0e71152013-02-11 09:47:59 -0600166
Obaid Amin806df812013-02-21 13:30:26 -0600167 char *neighbor=(char *)malloc(len);
168 memset(neighbor,0,len);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600169
Obaid Amin806df812013-02-21 13:30:26 -0600170 for(i=lsa_position+1+offset; i<interest_comps->n-1;i++)
171 {
172 ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1,
173 &comp_size);
174 memcpy(neighbor+strlen(neighbor),"/",1);
175 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,
176 strlen((char *)comp_ptr1));
Obaid Aminbb8ac422013-02-20 17:08:48 -0600177
Obaid Amin806df812013-02-21 13:30:26 -0600178 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600179
Obaid Amin806df812013-02-21 13:30:26 -0600180 name_part->name=(char *)malloc(strlen(neighbor)+1);
181 memset(name_part->name,0,strlen(neighbor)+1);
182 memcpy(name_part->name,neighbor,strlen(neighbor)+1);
183 name_part->length=strlen(neighbor)+1;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600184
Obaid Amin806df812013-02-21 13:30:26 -0600185 // Add 01/31/2013
186 free(neighbor);
akmhoquef6773612013-02-23 09:53:00 -0600187 */
akmhoquea0e71152013-02-11 09:47:59 -0600188}
189
akmhoquea0e71152013-02-11 09:47:59 -0600190
akmhoquea0e71152013-02-11 09:47:59 -0600191
192
193
Obaid Amin806df812013-02-21 13:30:26 -0600194 void
akmhoquea0e71152013-02-11 09:47:59 -0600195get_content_by_content_name(char *content_name, unsigned char **content_data)
196{
197
Obaid Amin806df812013-02-21 13:30:26 -0600198 struct ccn_charbuf *name = NULL;
199 struct ccn_charbuf *templ = NULL;
200 struct ccn_charbuf *resultbuf = NULL;
201 struct ccn_parsed_ContentObject pcobuf = { 0 };
202 int res;
203 int allow_stale = 0;
204 int content_only = 1;
205 int scope = -1;
206 const unsigned char *ptr;
207 size_t length;
208 int resolve_version = CCN_V_HIGHEST;
209 int timeout_ms = 3000;
210 const unsigned lifetime_default = CCN_INTEREST_LIFETIME_SEC << 12;
211 unsigned lifetime_l12 = lifetime_default;
212 int get_flags = 0;
akmhoquea0e71152013-02-11 09:47:59 -0600213
Obaid Amin806df812013-02-21 13:30:26 -0600214 name = ccn_charbuf_create();
215 res = ccn_name_from_uri(name,content_name);
216 if (res < 0) {
217 fprintf(stderr, "Bad ccn URI: %s\n", content_name);
218 exit(1);
219 }
akmhoquea0e71152013-02-11 09:47:59 -0600220
Obaid Amin806df812013-02-21 13:30:26 -0600221 if (allow_stale || lifetime_l12 != lifetime_default || scope != -1) {
222 templ = ccn_charbuf_create();
223 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
224 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
225 ccn_charbuf_append_closer(templ); /* </Name> */
226 if (allow_stale) {
227 ccn_charbuf_append_tt(templ, CCN_DTAG_AnswerOriginKind, CCN_DTAG);
228 ccnb_append_number(templ,
229 CCN_AOK_DEFAULT | CCN_AOK_STALE);
230 ccn_charbuf_append_closer(templ); /* </AnswerOriginKind> */
231 }
232 if (scope != -1) {
233 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
234 }
235 if (lifetime_l12 != lifetime_default) {
236 /*
237 * Choose the interest lifetime so there are at least 3
238 * expressions (in the unsatisfied case).
239 */
240 unsigned char buf[3] = { 0 };
241 int i;
242 for (i = sizeof(buf) - 1; i >= 0; i--, lifetime_l12 >>= 8)
243 buf[i] = lifetime_l12 & 0xff;
244 ccnb_append_tagged_blob(templ, CCN_DTAG_InterestLifetime, buf,
245 sizeof(buf));
246 }
247 ccn_charbuf_append_closer(templ); /* </Interest> */
akmhoquea0e71152013-02-11 09:47:59 -0600248 }
Obaid Amin806df812013-02-21 13:30:26 -0600249 resultbuf = ccn_charbuf_create();
250 if (resolve_version != 0) {
251 res = ccn_resolve_version(nlsr->ccn, name, resolve_version, 500);
252 if (res >= 0) {
253 ccn_uri_append(resultbuf, name->buf, name->length, 1);
254 resultbuf->length = 0;
255 }
akmhoquea0e71152013-02-11 09:47:59 -0600256 }
Obaid Amin806df812013-02-21 13:30:26 -0600257 res = ccn_get(nlsr->ccn, name, templ, timeout_ms, resultbuf, &pcobuf, NULL,
258 get_flags);
akmhoquea0e71152013-02-11 09:47:59 -0600259 if (res >= 0) {
Obaid Amin806df812013-02-21 13:30:26 -0600260 ptr = resultbuf->buf;
261 length = resultbuf->length;
262 if (content_only){
263 ccn_content_get_value(ptr, length, &pcobuf, &ptr, &length);
akmhoque2d63d6c2013-02-24 12:03:10 -0600264 *content_data = (unsigned char *) calloc(length+1, sizeof(char *));
Obaid Amin806df812013-02-21 13:30:26 -0600265 memcpy (*content_data, ptr, length);
akmhoque2d63d6c2013-02-24 12:03:10 -0600266 *content_data[strlen((char *)*content_data)]='\0';
Obaid Amin806df812013-02-21 13:30:26 -0600267 }
akmhoquea0e71152013-02-11 09:47:59 -0600268 }
Obaid Amin806df812013-02-21 13:30:26 -0600269 ccn_charbuf_destroy(&resultbuf);
270 ccn_charbuf_destroy(&templ);
271 ccn_charbuf_destroy(&name);
akmhoquea0e71152013-02-11 09:47:59 -0600272}
273
Obaid Amin806df812013-02-21 13:30:26 -0600274 void
akmhoquea0e71152013-02-11 09:47:59 -0600275process_incoming_sync_content_lsa( unsigned char *content_data)
276{
277
278
Obaid Amin806df812013-02-21 13:30:26 -0600279 if ( nlsr->debugging )
280 printf("process_incoming_sync_content_lsa called \n");
Obaid Aminbb8ac422013-02-20 17:08:48 -0600281
Obaid Amin806df812013-02-21 13:30:26 -0600282 char *sep="|";
283 char *rem;
284 char *orig_router;
285 char *orl;
286 int orig_router_length;
287 char *lst;
288 int ls_type;
289 char *lsid;
290 long int ls_id;
291 char *isvld;
292 int isValid;
293 char *num_link;
294 int no_link;
295 char *np;
296 char *np_length;
297 int name_length;
298 char *data;
299 char *orig_time;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600300
301
akmhoquea0e71152013-02-11 09:47:59 -0600302 if ( nlsr->debugging )
Obaid Amin806df812013-02-21 13:30:26 -0600303 printf("LSA Data \n");
304
305 if( strlen((char *)content_data ) > 0 )
akmhoquea0e71152013-02-11 09:47:59 -0600306 {
Obaid Aminbb8ac422013-02-20 17:08:48 -0600307
Obaid Amin806df812013-02-21 13:30:26 -0600308 orig_router=strtok_r((char *)content_data,sep,&rem);
309 orl=strtok_r(NULL,sep,&rem);
310 orig_router_length=atoi(orl);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600311
Obaid Amin806df812013-02-21 13:30:26 -0600312 if ( nlsr->debugging )
313 {
314 printf(" Orig Router Name : %s\n",orig_router);
315 printf(" Orig Router Length: %d\n",orig_router_length);
316 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600317
Obaid Amin806df812013-02-21 13:30:26 -0600318 lst=strtok_r(NULL,sep,&rem);
319 ls_type=atoi(lst);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600320
Obaid Amin806df812013-02-21 13:30:26 -0600321 if ( nlsr->debugging )
322 printf(" LS Type : %d\n",ls_type);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600323
Obaid Amin806df812013-02-21 13:30:26 -0600324 if ( ls_type == LS_TYPE_NAME )
325 {
326 lsid=strtok_r(NULL,sep,&rem);
327 ls_id=atoi(lsid);
328 orig_time=strtok_r(NULL,sep,&rem);
329 isvld=strtok_r(NULL,sep,&rem);
330 isValid=atoi(isvld);
331 np=strtok_r(NULL,sep,&rem);
332 np_length=strtok_r(NULL,sep,&rem);
333 name_length=atoi(np_length);
334 if ( nlsr->debugging )
335 {
336 printf(" LS ID : %ld\n",ls_id);
337 printf(" isValid : %d\n",isValid);
338 printf(" Name Prefix : %s\n",np);
339 printf(" Orig Time : %s\n",orig_time);
340 printf(" Name Prefix length: %d\n",name_length);
341 }
342
343 build_and_install_others_name_lsa(orig_router,ls_type,ls_id,orig_time,isValid,np);
344
345 print_name_lsdb();
346
347 }
348 else if ( ls_type == LS_TYPE_ADJ )
349 {
350 orig_time=strtok_r(NULL,sep,&rem);
351 num_link=strtok_r(NULL,sep,&rem);
352 no_link=atoi(num_link);
353 data=rem;
354
355 if ( nlsr->debugging )
356 {
357 printf(" Orig Time : %s\n",orig_time);
358 printf(" No Link : %d\n",no_link);
359 printf(" Data : %s\n",data);
360 }
361 build_and_install_others_adj_lsa(orig_router,ls_type,orig_time,no_link,data);
362 }
363 else if ( ls_type == LS_TYPE_COR )
364 {
365 orig_time=strtok_r(NULL,sep,&rem);
366 char *cor_r=strtok_r(NULL,sep,&rem);
367 char *cor_theta=strtok_r(NULL,sep,&rem);
368
369 double r, theta;
akmhoque8034d502013-02-24 11:53:24 -0600370 r=strtod(cor_r,NULL);
371 theta=strtod(cor_theta,NULL);
Obaid Amin806df812013-02-21 13:30:26 -0600372
373 if ( nlsr->debugging )
374 {
375 printf(" Orig Time : %s\n",orig_time);
376 printf(" Cor R : %f\n",r);
377 printf(" Cor Theta : %f\n",theta);
378 }
379 build_and_install_others_cor_lsa(orig_router,ls_type,orig_time, (double)r, (double)theta);
380 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600381
382 }
akmhoquea0e71152013-02-11 09:47:59 -0600383}
384
Obaid Amin806df812013-02-21 13:30:26 -0600385 void
Obaid Amin7997ad82013-02-22 16:41:56 -0600386process_content_from_sync (struct ccn_charbuf *content_name,
387 struct ccn_indexbuf *components)
akmhoquea0e71152013-02-11 09:47:59 -0600388{
akmhoque8c5e2662013-02-24 06:37:14 -0600389 if (nlsr->debugging)
390 printf("process_content_from_sync called \n");
Obaid Amin806df812013-02-21 13:30:26 -0600391 size_t comp_size;
392 char *lst;
393 char *lsid;
394 const unsigned char *second_last_comp;
395 const unsigned char *third_last_comp;
396 const unsigned char *origtime;
397 char *sep=".";
398 char *rem;
399 char *second_comp_type;
akmhoquea0e71152013-02-11 09:47:59 -0600400
Obaid Amin806df812013-02-21 13:30:26 -0600401 int ls_type;
402 long int ls_id=0;
akmhoquea0e71152013-02-11 09:47:59 -0600403
Obaid Amin806df812013-02-21 13:30:26 -0600404 unsigned char *content_data = NULL;
akmhoquea0e71152013-02-11 09:47:59 -0600405
akmhoqueb31caa12013-02-22 08:42:09 -0600406 char *time_stamp=get_current_timestamp_micro_v2();
akmhoquea0e71152013-02-11 09:47:59 -0600407
Obaid Amin806df812013-02-21 13:30:26 -0600408 struct ccn_charbuf *uri = ccn_charbuf_create();
409 ccn_uri_append(uri, content_name->buf, content_name->length, 0);
akmhoquea0e71152013-02-11 09:47:59 -0600410
Obaid Amin7997ad82013-02-22 16:41:56 -0600411 struct name_prefix *orig_router=(struct name_prefix *)
412 calloc( 1, sizeof(struct name_prefix));
akmhoquea0e71152013-02-11 09:47:59 -0600413
Obaid Amin7997ad82013-02-22 16:41:56 -0600414 ccn_name_comp_get( content_name->buf, components,
415 components->n-1-2, &second_last_comp, &comp_size);
416
Obaid Amin806df812013-02-21 13:30:26 -0600417 if (nlsr->debugging)
Obaid Amin7997ad82013-02-22 16:41:56 -0600418 printf("2nd Last Component: %s \n", second_last_comp);
akmhoquea0e71152013-02-11 09:47:59 -0600419
Obaid Amin7997ad82013-02-22 16:41:56 -0600420 second_comp_type=strtok_r((char *)second_last_comp, sep, &rem);
421 if (second_comp_type == NULL || rem == NULL)
422 {
423 printf ("Error: unable to tokenize the string: %s, calling exit()\n",
424 (char *)second_last_comp);
425 exit(0);
426 }
427
Obaid Amin806df812013-02-21 13:30:26 -0600428 if ( strcmp( second_comp_type, "lsId" ) == 0 )
429 {
430 lsid=rem;
431 ls_id=atoi(rem);
432 ccn_name_comp_get(content_name->buf, components,components->n-2-2,&third_last_comp, &comp_size);
433 lst=strtok_r((char *)third_last_comp,sep,&rem);
434 lst=rem;
435 ls_type=atoi(lst);
436 ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size);
437 ccn_name_chop(content_name,components,-3);
438 get_name_part(orig_router,content_name,components,0);
akmhoquea0e71152013-02-11 09:47:59 -0600439
Obaid Aminbb8ac422013-02-20 17:08:48 -0600440 if ( nlsr->debugging )
Obaid Amin806df812013-02-21 13:30:26 -0600441 printf("Orig Router: %s Ls Type: %d Ls id: %ld Orig Time: %s\n",orig_router->name,ls_type,ls_id,origtime);
akmhoquea0e71152013-02-11 09:47:59 -0600442
Obaid Amin806df812013-02-21 13:30:26 -0600443 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque8c5e2662013-02-24 06:37:14 -0600444 if ( nlsr->debugging )
445 printf("LSA Life time: %d\n",lsa_life_time);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600446
Obaid Amin806df812013-02-21 13:30:26 -0600447 if ( (strcmp(orig_router->name,nlsr->router_name) == 0 && lsa_life_time < nlsr->lsa_refresh_time)
448 || (strcmp(orig_router->name,nlsr->router_name) != 0 && lsa_life_time < nlsr->router_dead_interval) )
Obaid Aminbb8ac422013-02-20 17:08:48 -0600449 {
Obaid Amin806df812013-02-21 13:30:26 -0600450 int is_new_name_lsa=check_is_new_name_lsa(orig_router->name,(char *)lst,(char *)lsid,(char *)origtime);
451 if ( is_new_name_lsa == 1 )
452 {
453 if ( nlsr->debugging )
454 printf("New NAME LSA.....\n");
455 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
456 if ( nlsr->debugging )
457 printf("Content Data: %s \n",content_data);
458 process_incoming_sync_content_lsa(content_data);
459 }
460 else
461 {
462 if ( nlsr->debugging )
463 printf("Name LSA / Newer Name LSA already xists in LSDB\n");
464 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600465
Obaid Amin806df812013-02-21 13:30:26 -0600466 if ( nlsr->debugging )
467 printf("Content Data: %s \n",content_data);
468 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600469 }
Obaid Amin806df812013-02-21 13:30:26 -0600470 else
Obaid Aminbb8ac422013-02-20 17:08:48 -0600471 {
Obaid Amin806df812013-02-21 13:30:26 -0600472 if ( nlsr->debugging )
473 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
Obaid Aminbb8ac422013-02-20 17:08:48 -0600474 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600475 }
Obaid Amin806df812013-02-21 13:30:26 -0600476 else
Obaid Aminbb8ac422013-02-20 17:08:48 -0600477 {
Obaid Amin806df812013-02-21 13:30:26 -0600478 ls_type=atoi(rem);
479 lst=rem;
480 if(ls_type == LS_TYPE_ADJ)
Obaid Aminbb8ac422013-02-20 17:08:48 -0600481 {
Obaid Amin806df812013-02-21 13:30:26 -0600482 ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size);
483 ccn_name_chop(content_name,components,-2);
484 get_name_part(orig_router,content_name,components,0);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600485
Obaid Amin806df812013-02-21 13:30:26 -0600486 if ( nlsr->debugging )
487 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",orig_router->name,ls_type,origtime);
488
489 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque8c5e2662013-02-24 06:37:14 -0600490 if ( nlsr->debugging )
491 printf("LSA Life time: %d\n",lsa_life_time);
492
Obaid Amin806df812013-02-21 13:30:26 -0600493 if ( (strcmp(orig_router->name,nlsr->router_name) == 0 && lsa_life_time < nlsr->lsa_refresh_time) || (strcmp(orig_router->name,nlsr->router_name) != 0 && lsa_life_time < nlsr->router_dead_interval) )
494 {
495 int is_new_adj_lsa=check_is_new_adj_lsa(orig_router->name,(char *)lst,(char *)origtime);
496 if ( is_new_adj_lsa == 1 )
497 {
498 if ( nlsr->debugging )
499 printf("New Adj LSA.....\n");
500 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
501
502 if ( nlsr->debugging )
503 printf("Content Data: %s \n",content_data);
504 process_incoming_sync_content_lsa(content_data);
505 }
506 else
507 {
508 if ( nlsr->debugging )
509 printf("Adj LSA / Newer Adj LSA already exists in LSDB\n");
510 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
511 if ( nlsr->debugging )
512 printf("Content Data: %s \n",content_data);
513 }
514 }
515 else
516 {
517 if ( nlsr->debugging )
518 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
519 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600520 }
Obaid Amin806df812013-02-21 13:30:26 -0600521 else if(ls_type == LS_TYPE_COR)
Obaid Aminbb8ac422013-02-20 17:08:48 -0600522 {
Obaid Amin806df812013-02-21 13:30:26 -0600523 ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size);
524 ccn_name_chop(content_name,components,-2);
525 get_name_part(orig_router,content_name,components,0);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600526
Obaid Amin806df812013-02-21 13:30:26 -0600527 if ( nlsr->debugging )
528 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",orig_router->name,ls_type,origtime);
529
530 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque8c5e2662013-02-24 06:37:14 -0600531 if ( nlsr->debugging )
532 printf("LSA Life time: %d\n",lsa_life_time);
533
Obaid Amin806df812013-02-21 13:30:26 -0600534 if ( (strcmp(orig_router->name,nlsr->router_name) == 0 && lsa_life_time < nlsr->lsa_refresh_time) || (strcmp(orig_router->name,nlsr->router_name) != 0 && lsa_life_time < nlsr->router_dead_interval) )
535 {
536 int is_new_cor_lsa=check_is_new_cor_lsa(orig_router->name,(char *)lst,(char *)origtime);
537 if ( is_new_cor_lsa == 1 )
538 {
539 if ( nlsr->debugging )
540 printf("New Cor LSA.....\n");
541 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
542
543 if ( nlsr->debugging )
544 printf("Content Data: %s \n",content_data);
545 process_incoming_sync_content_lsa(content_data);
546 }
547 else
548 {
549 if ( nlsr->debugging )
550 printf("Cor LSA / Newer Cor LSA already exists in LSDB\n");
551 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
552 if ( nlsr->debugging )
553 printf("Content Data: %s \n",content_data);
554 }
555 }
556 else
557 {
558 if ( nlsr->debugging )
559 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
560 }
561
562 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600563 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600564
akmhoquef6773612013-02-23 09:53:00 -0600565 if (orig_router->name)
566 free(orig_router->name);
Obaid Amin05300892013-02-21 13:45:25 -0600567 if (orig_router)
568 free(orig_router);
Obaid Amin806df812013-02-21 13:30:26 -0600569 ccn_charbuf_destroy(&uri);
570 //01/31/2013
571 free(time_stamp);
akmhoquea0e71152013-02-11 09:47:59 -0600572}
573
Obaid Amin7793ba72013-02-22 00:43:58 -0600574 int
akmhoquea0e71152013-02-11 09:47:59 -0600575sync_monitor(char *topo_prefix, char *slice_prefix)
576{
577
Obaid Amin806df812013-02-21 13:30:26 -0600578 struct ccn_charbuf *prefix = ccn_charbuf_create();
Obaid Amin806df812013-02-21 13:30:26 -0600579 struct ccn_charbuf *topo = ccn_charbuf_create();
Obaid Amin7997ad82013-02-22 16:41:56 -0600580
581 nlsr->closure=(struct ccns_name_closure *)
582 calloc(1,sizeof(struct ccns_name_closure));
583
Obaid Amin806df812013-02-21 13:30:26 -0600584 nlsr->slice = ccns_slice_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600585
Obaid Amin806df812013-02-21 13:30:26 -0600586 ccn_charbuf_reset(prefix);
587 ccn_name_from_uri(prefix, slice_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600588
Obaid Amin806df812013-02-21 13:30:26 -0600589 ccn_charbuf_reset(topo);
590 ccn_name_from_uri(topo, topo_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600591
Obaid Amin806df812013-02-21 13:30:26 -0600592 ccns_slice_set_topo_prefix(nlsr->slice, topo, prefix);
593 nlsr->closure->callback = &sync_cb;
akmhoque54d86112013-02-21 16:42:34 -0600594 nlsr->ccns = ccns_open(nlsr->ccn, nlsr->slice, nlsr->closure, NULL, NULL);
akmhoquea0e71152013-02-11 09:47:59 -0600595
Obaid Amin7997ad82013-02-22 16:41:56 -0600596 ccn_charbuf_destroy(&prefix);
597 ccn_charbuf_destroy(&topo);
Obaid Amin806df812013-02-21 13:30:26 -0600598 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600599}
600
Obaid Amin7793ba72013-02-22 00:43:58 -0600601 struct ccn_charbuf *
akmhoquea0e71152013-02-11 09:47:59 -0600602make_template(int scope)
603{
Obaid Amin806df812013-02-21 13:30:26 -0600604 struct ccn_charbuf *templ = NULL;
605 templ = ccn_charbuf_create();
606 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
607 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
608 ccn_charbuf_append_closer(templ); /* </Name> */
609 if (0 <= scope && scope <= 2)
610 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
611 ccn_charbuf_append_closer(templ); /* </Interest> */
612 return(templ);
akmhoquea0e71152013-02-11 09:47:59 -0600613}
614
Obaid Amin7793ba72013-02-22 00:43:58 -0600615 int
akmhoquea0e71152013-02-11 09:47:59 -0600616write_data_to_repo(char *data, char *name_prefix)
617{
Obaid Amin806df812013-02-21 13:30:26 -0600618 if ( nlsr->debugging )
619 {
620 printf("write_data_to_repo called\n");
621 printf("Content Name: %s \n",name_prefix);
622 printf("Content Data: %s \n",data);
623 }
akmhoquea0e71152013-02-11 09:47:59 -0600624
Obaid Aminf210a322013-02-22 02:04:52 -0600625 struct ccn *temp_ccn;
Obaid Amin806df812013-02-21 13:30:26 -0600626 temp_ccn=ccn_create();
627 int ccn_fd=ccn_connect(temp_ccn, NULL);
628 if(ccn_fd == -1)
629 {
630 fprintf(stderr,"Could not connect to ccnd for Data Writing\n");
631 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd for Data Writing\n");
632 return -1;
633 }
Obaid Aminf210a322013-02-22 02:04:52 -0600634
Obaid Amin806df812013-02-21 13:30:26 -0600635 struct ccn_charbuf *name = NULL;
636 struct ccn_seqwriter *w = NULL;
637 int blocksize = 4096;
638 int freshness = -1;
Obaid Amin806df812013-02-21 13:30:26 -0600639 int scope = 1;
640 int res;
641 size_t blockread;
642 struct ccn_charbuf *templ;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600643
Obaid Amin806df812013-02-21 13:30:26 -0600644 name = ccn_charbuf_create();
645 res = ccn_name_from_uri(name, name_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600646 if (res < 0) {
Obaid Amin806df812013-02-21 13:30:26 -0600647 fprintf(stderr, "bad CCN URI: %s\n",name_prefix);
648 return -1;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600649 }
Obaid Amin806df812013-02-21 13:30:26 -0600650
Obaid Aminf210a322013-02-22 02:04:52 -0600651 w = ccn_seqw_create(temp_ccn, name);
Obaid Amin806df812013-02-21 13:30:26 -0600652 if (w == NULL) {
653 fprintf(stderr, "ccn_seqw_create failed\n");
654 return -1;
655 }
656 ccn_seqw_set_block_limits(w, blocksize, blocksize);
657 if (freshness > -1)
658 ccn_seqw_set_freshness(w, freshness);
akmhoquea0e71152013-02-11 09:47:59 -0600659
Obaid Amin7997ad82013-02-22 16:41:56 -0600660 struct ccn_charbuf *name_v = ccn_charbuf_create();
661 ccn_seqw_get_name(w, name_v);
662 ccn_name_from_uri(name_v, "%C1.R.sw");
663 ccn_name_append_nonce(name_v);
664 templ = make_template(scope);
665 res = ccn_get(temp_ccn, name_v, templ, 60000, NULL, NULL, NULL, 0);
666 ccn_charbuf_destroy(&templ);
667 ccn_charbuf_destroy(&name_v);
668 if (res < 0) {
669 fprintf(stderr, "No response from repository\n");
670 return -1;
671 }
akmhoquea0e71152013-02-11 09:47:59 -0600672
Obaid Amin806df812013-02-21 13:30:26 -0600673 blockread = 0;
akmhoquea0e71152013-02-11 09:47:59 -0600674
akmhoquee407fc22013-02-24 11:57:17 -0600675 blockread=strlen(data)+1;
akmhoquea0e71152013-02-11 09:47:59 -0600676
Obaid Amin806df812013-02-21 13:30:26 -0600677 if (blockread > 0) {
Obaid Amin806df812013-02-21 13:30:26 -0600678 res = ccn_seqw_write(w, data, blockread);
679 while (res == -1) {
akmhoque8034d502013-02-24 11:53:24 -0600680 ccn_run(temp_ccn,100);
Obaid Amin806df812013-02-21 13:30:26 -0600681 res = ccn_seqw_write(w, data, blockread);
682 }
683 }
684
akmhoque8034d502013-02-24 11:53:24 -0600685 ccn_seqw_close(w);
686 ccn_run(temp_ccn, 100);
Obaid Amin806df812013-02-21 13:30:26 -0600687 ccn_charbuf_destroy(&name);
akmhoque0ed6d982013-02-22 14:28:01 -0600688 ccn_destroy(&temp_ccn);
akmhoquea0e71152013-02-11 09:47:59 -0600689
Obaid Amin806df812013-02-21 13:30:26 -0600690 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600691}
Obaid Amin806df812013-02-21 13:30:26 -0600692 int
Obaid Amin7997ad82013-02-22 16:41:56 -0600693 create_sync_slice(char *topo_prefix, char *slice_prefix)
akmhoquea0e71152013-02-11 09:47:59 -0600694{
Obaid Amin806df812013-02-21 13:30:26 -0600695 int res;
akmhoque0ed6d982013-02-22 14:28:01 -0600696 struct ccn *handle; //obaid: probably we don't need it use the same handle i.e. nlsr->ccn
Obaid Amin806df812013-02-21 13:30:26 -0600697 struct ccns_slice *slice;
698 struct ccn_charbuf *prefix = ccn_charbuf_create();
699 struct ccn_charbuf *topo = ccn_charbuf_create();
700 struct ccn_charbuf *clause = ccn_charbuf_create();
701 struct ccn_charbuf *slice_name = ccn_charbuf_create();
702 struct ccn_charbuf *slice_uri = ccn_charbuf_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600703
Obaid Amin806df812013-02-21 13:30:26 -0600704 if (prefix == NULL || topo == NULL || clause == NULL ||
705 slice_name == NULL || slice_uri == NULL) {
706 fprintf(stderr, "Unable to allocate required memory.\n");
707 return -1;
708 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600709
akmhoque0ed6d982013-02-22 14:28:01 -0600710 handle = ccn_create();
Obaid Amin806df812013-02-21 13:30:26 -0600711 res = ccn_connect(handle, NULL);
712 if (0 > res) {
713 fprintf(stderr, "Unable to connect to ccnd.\n");
714 return -1;
715 }
akmhoque0ed6d982013-02-22 14:28:01 -0600716
Obaid Amin806df812013-02-21 13:30:26 -0600717 slice = ccns_slice_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600718
Obaid Amin806df812013-02-21 13:30:26 -0600719 ccn_charbuf_reset(topo);
720 ccn_name_from_uri(topo, topo_prefix);
721 ccn_charbuf_reset(prefix);
722 ccn_name_from_uri(prefix,slice_prefix );
723 ccns_slice_set_topo_prefix(slice, topo, prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600724
725
akmhoque0ed6d982013-02-22 14:28:01 -0600726 res = ccns_write_slice(handle, slice, slice_name);
727 //res = ccns_write_slice(nlsr->ccn, slice, slice_name);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600728
Obaid Amin806df812013-02-21 13:30:26 -0600729 //01/31/2013
730 ccns_slice_destroy(&slice);
akmhoque0ed6d982013-02-22 14:28:01 -0600731 ccn_destroy(&handle);
Obaid Amin806df812013-02-21 13:30:26 -0600732 ccn_charbuf_destroy(&prefix);
733 ccn_charbuf_destroy(&topo);
734 ccn_charbuf_destroy(&clause);
735 ccn_charbuf_destroy(&slice_name);
736 ccn_charbuf_destroy(&slice_uri);
akmhoquea0e71152013-02-11 09:47:59 -0600737
Obaid Amin806df812013-02-21 13:30:26 -0600738 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600739}
740