blob: fbc679be6c5527d1f3cd01b762bddb7f6478161c [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 Amin4b0e4ec2013-02-24 22:15:01 -0600107 //main method that processes contents from the sync
Obaid Amin7997ad82013-02-22 16:41:56 -0600108 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);
akmhoque2f1d11e2013-02-24 12:06:52 -0600264 *content_data = (unsigned char *) calloc(length, sizeof(char *));
Obaid Amin806df812013-02-21 13:30:26 -0600265 memcpy (*content_data, ptr, length);
266 }
akmhoquea0e71152013-02-11 09:47:59 -0600267 }
Obaid Amin806df812013-02-21 13:30:26 -0600268 ccn_charbuf_destroy(&resultbuf);
269 ccn_charbuf_destroy(&templ);
270 ccn_charbuf_destroy(&name);
akmhoquea0e71152013-02-11 09:47:59 -0600271}
272
Obaid Amin806df812013-02-21 13:30:26 -0600273 void
akmhoquea0e71152013-02-11 09:47:59 -0600274process_incoming_sync_content_lsa( unsigned char *content_data)
275{
276
277
Obaid Amin806df812013-02-21 13:30:26 -0600278 if ( nlsr->debugging )
279 printf("process_incoming_sync_content_lsa called \n");
Obaid Aminbb8ac422013-02-20 17:08:48 -0600280
Obaid Amin806df812013-02-21 13:30:26 -0600281 char *sep="|";
282 char *rem;
283 char *orig_router;
284 char *orl;
285 int orig_router_length;
286 char *lst;
287 int ls_type;
288 char *lsid;
289 long int ls_id;
290 char *isvld;
291 int isValid;
292 char *num_link;
293 int no_link;
294 char *np;
295 char *np_length;
296 int name_length;
297 char *data;
298 char *orig_time;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600299
300
akmhoquea0e71152013-02-11 09:47:59 -0600301 if ( nlsr->debugging )
Obaid Amin806df812013-02-21 13:30:26 -0600302 printf("LSA Data \n");
303
304 if( strlen((char *)content_data ) > 0 )
akmhoquea0e71152013-02-11 09:47:59 -0600305 {
Obaid Aminbb8ac422013-02-20 17:08:48 -0600306
Obaid Amin806df812013-02-21 13:30:26 -0600307 orig_router=strtok_r((char *)content_data,sep,&rem);
308 orl=strtok_r(NULL,sep,&rem);
309 orig_router_length=atoi(orl);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600310
Obaid Amin806df812013-02-21 13:30:26 -0600311 if ( nlsr->debugging )
312 {
313 printf(" Orig Router Name : %s\n",orig_router);
314 printf(" Orig Router Length: %d\n",orig_router_length);
315 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600316
Obaid Amin806df812013-02-21 13:30:26 -0600317 lst=strtok_r(NULL,sep,&rem);
318 ls_type=atoi(lst);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600319
Obaid Amin806df812013-02-21 13:30:26 -0600320 if ( nlsr->debugging )
321 printf(" LS Type : %d\n",ls_type);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600322
Obaid Amin806df812013-02-21 13:30:26 -0600323 if ( ls_type == LS_TYPE_NAME )
324 {
325 lsid=strtok_r(NULL,sep,&rem);
326 ls_id=atoi(lsid);
327 orig_time=strtok_r(NULL,sep,&rem);
328 isvld=strtok_r(NULL,sep,&rem);
329 isValid=atoi(isvld);
330 np=strtok_r(NULL,sep,&rem);
331 np_length=strtok_r(NULL,sep,&rem);
332 name_length=atoi(np_length);
333 if ( nlsr->debugging )
334 {
335 printf(" LS ID : %ld\n",ls_id);
336 printf(" isValid : %d\n",isValid);
337 printf(" Name Prefix : %s\n",np);
338 printf(" Orig Time : %s\n",orig_time);
339 printf(" Name Prefix length: %d\n",name_length);
340 }
341
342 build_and_install_others_name_lsa(orig_router,ls_type,ls_id,orig_time,isValid,np);
343
344 print_name_lsdb();
345
346 }
347 else if ( ls_type == LS_TYPE_ADJ )
348 {
349 orig_time=strtok_r(NULL,sep,&rem);
350 num_link=strtok_r(NULL,sep,&rem);
351 no_link=atoi(num_link);
352 data=rem;
353
354 if ( nlsr->debugging )
355 {
356 printf(" Orig Time : %s\n",orig_time);
357 printf(" No Link : %d\n",no_link);
358 printf(" Data : %s\n",data);
359 }
360 build_and_install_others_adj_lsa(orig_router,ls_type,orig_time,no_link,data);
361 }
362 else if ( ls_type == LS_TYPE_COR )
363 {
364 orig_time=strtok_r(NULL,sep,&rem);
365 char *cor_r=strtok_r(NULL,sep,&rem);
366 char *cor_theta=strtok_r(NULL,sep,&rem);
367
368 double r, theta;
akmhoque8034d502013-02-24 11:53:24 -0600369 r=strtod(cor_r,NULL);
370 theta=strtod(cor_theta,NULL);
Obaid Amin806df812013-02-21 13:30:26 -0600371
372 if ( nlsr->debugging )
373 {
374 printf(" Orig Time : %s\n",orig_time);
375 printf(" Cor R : %f\n",r);
376 printf(" Cor Theta : %f\n",theta);
377 }
378 build_and_install_others_cor_lsa(orig_router,ls_type,orig_time, (double)r, (double)theta);
379 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600380
381 }
akmhoquea0e71152013-02-11 09:47:59 -0600382}
383
Obaid Amin806df812013-02-21 13:30:26 -0600384 void
Obaid Amin7997ad82013-02-22 16:41:56 -0600385process_content_from_sync (struct ccn_charbuf *content_name,
386 struct ccn_indexbuf *components)
akmhoquea0e71152013-02-11 09:47:59 -0600387{
akmhoque8c5e2662013-02-24 06:37:14 -0600388 if (nlsr->debugging)
389 printf("process_content_from_sync called \n");
Obaid Amin806df812013-02-21 13:30:26 -0600390 size_t comp_size;
391 char *lst;
392 char *lsid;
393 const unsigned char *second_last_comp;
394 const unsigned char *third_last_comp;
395 const unsigned char *origtime;
396 char *sep=".";
397 char *rem;
398 char *second_comp_type;
akmhoquea0e71152013-02-11 09:47:59 -0600399
Obaid Amin806df812013-02-21 13:30:26 -0600400 int ls_type;
401 long int ls_id=0;
akmhoquea0e71152013-02-11 09:47:59 -0600402
Obaid Amin806df812013-02-21 13:30:26 -0600403 unsigned char *content_data = NULL;
akmhoquea0e71152013-02-11 09:47:59 -0600404
akmhoqueb31caa12013-02-22 08:42:09 -0600405 char *time_stamp=get_current_timestamp_micro_v2();
akmhoquea0e71152013-02-11 09:47:59 -0600406
Obaid Amin806df812013-02-21 13:30:26 -0600407 struct ccn_charbuf *uri = ccn_charbuf_create();
408 ccn_uri_append(uri, content_name->buf, content_name->length, 0);
akmhoquea0e71152013-02-11 09:47:59 -0600409
Obaid Amin7997ad82013-02-22 16:41:56 -0600410 struct name_prefix *orig_router=(struct name_prefix *)
411 calloc( 1, sizeof(struct name_prefix));
akmhoquea0e71152013-02-11 09:47:59 -0600412
Obaid Amin7997ad82013-02-22 16:41:56 -0600413 ccn_name_comp_get( content_name->buf, components,
414 components->n-1-2, &second_last_comp, &comp_size);
415
Obaid Amin806df812013-02-21 13:30:26 -0600416 if (nlsr->debugging)
Obaid Amin7997ad82013-02-22 16:41:56 -0600417 printf("2nd Last Component: %s \n", second_last_comp);
akmhoquea0e71152013-02-11 09:47:59 -0600418
Obaid Amin7997ad82013-02-22 16:41:56 -0600419 second_comp_type=strtok_r((char *)second_last_comp, sep, &rem);
420 if (second_comp_type == NULL || rem == NULL)
421 {
422 printf ("Error: unable to tokenize the string: %s, calling exit()\n",
423 (char *)second_last_comp);
424 exit(0);
425 }
426
Obaid Amin806df812013-02-21 13:30:26 -0600427 if ( strcmp( second_comp_type, "lsId" ) == 0 )
428 {
429 lsid=rem;
430 ls_id=atoi(rem);
431 ccn_name_comp_get(content_name->buf, components,components->n-2-2,&third_last_comp, &comp_size);
432 lst=strtok_r((char *)third_last_comp,sep,&rem);
433 lst=rem;
434 ls_type=atoi(lst);
435 ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size);
436 ccn_name_chop(content_name,components,-3);
437 get_name_part(orig_router,content_name,components,0);
akmhoquea0e71152013-02-11 09:47:59 -0600438
Obaid Aminbb8ac422013-02-20 17:08:48 -0600439 if ( nlsr->debugging )
Obaid Amin806df812013-02-21 13:30:26 -0600440 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 -0600441
Obaid Amin806df812013-02-21 13:30:26 -0600442 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque8c5e2662013-02-24 06:37:14 -0600443 if ( nlsr->debugging )
444 printf("LSA Life time: %d\n",lsa_life_time);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600445
Obaid Amin806df812013-02-21 13:30:26 -0600446 if ( (strcmp(orig_router->name,nlsr->router_name) == 0 && lsa_life_time < nlsr->lsa_refresh_time)
447 || (strcmp(orig_router->name,nlsr->router_name) != 0 && lsa_life_time < nlsr->router_dead_interval) )
Obaid Aminbb8ac422013-02-20 17:08:48 -0600448 {
Obaid Amin806df812013-02-21 13:30:26 -0600449 int is_new_name_lsa=check_is_new_name_lsa(orig_router->name,(char *)lst,(char *)lsid,(char *)origtime);
450 if ( is_new_name_lsa == 1 )
451 {
452 if ( nlsr->debugging )
453 printf("New NAME LSA.....\n");
454 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
455 if ( nlsr->debugging )
456 printf("Content Data: %s \n",content_data);
457 process_incoming_sync_content_lsa(content_data);
458 }
459 else
460 {
461 if ( nlsr->debugging )
462 printf("Name LSA / Newer Name LSA already xists in LSDB\n");
463 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600464
Obaid Amin806df812013-02-21 13:30:26 -0600465 if ( nlsr->debugging )
466 printf("Content Data: %s \n",content_data);
467 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600468 }
Obaid Amin806df812013-02-21 13:30:26 -0600469 else
Obaid Aminbb8ac422013-02-20 17:08:48 -0600470 {
Obaid Amin806df812013-02-21 13:30:26 -0600471 if ( nlsr->debugging )
472 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
Obaid Aminbb8ac422013-02-20 17:08:48 -0600473 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600474 }
Obaid Amin806df812013-02-21 13:30:26 -0600475 else
Obaid Aminbb8ac422013-02-20 17:08:48 -0600476 {
Obaid Amin806df812013-02-21 13:30:26 -0600477 ls_type=atoi(rem);
478 lst=rem;
479 if(ls_type == LS_TYPE_ADJ)
Obaid Aminbb8ac422013-02-20 17:08:48 -0600480 {
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600481 ccn_name_comp_get(content_name->buf, components,components->n-2,
482 &origtime, &comp_size);
Obaid Amin806df812013-02-21 13:30:26 -0600483 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 )
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600487 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",
488 orig_router->name,ls_type,origtime);
Obaid Amin806df812013-02-21 13:30:26 -0600489
490 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque8c5e2662013-02-24 06:37:14 -0600491 if ( nlsr->debugging )
492 printf("LSA Life time: %d\n",lsa_life_time);
493
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600494 if ( (strcmp(orig_router->name,nlsr->router_name) == 0 &&
495 lsa_life_time < nlsr->lsa_refresh_time) ||
496 (strcmp(orig_router->name,nlsr->router_name) != 0 &&
497 lsa_life_time < nlsr->router_dead_interval) )
Obaid Amin806df812013-02-21 13:30:26 -0600498 {
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600499 int is_new_adj_lsa = check_is_new_adj_lsa( orig_router->name,
500 (char *)lst, (char *)origtime);
Obaid Amin806df812013-02-21 13:30:26 -0600501 if ( is_new_adj_lsa == 1 )
502 {
503 if ( nlsr->debugging )
504 printf("New Adj LSA.....\n");
505 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
506
507 if ( nlsr->debugging )
508 printf("Content Data: %s \n",content_data);
509 process_incoming_sync_content_lsa(content_data);
510 }
511 else
512 {
513 if ( nlsr->debugging )
514 printf("Adj LSA / Newer Adj LSA already exists in LSDB\n");
515 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
516 if ( nlsr->debugging )
517 printf("Content Data: %s \n",content_data);
518 }
519 }
520 else
521 {
522 if ( nlsr->debugging )
523 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
524 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600525 }
Obaid Amin806df812013-02-21 13:30:26 -0600526 else if(ls_type == LS_TYPE_COR)
Obaid Aminbb8ac422013-02-20 17:08:48 -0600527 {
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600528 ccn_name_comp_get(content_name->buf, components, components->n-2,
529 &origtime, &comp_size);
Obaid Amin806df812013-02-21 13:30:26 -0600530 ccn_name_chop(content_name,components,-2);
531 get_name_part(orig_router,content_name,components,0);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600532
Obaid Amin806df812013-02-21 13:30:26 -0600533 if ( nlsr->debugging )
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600534 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",
535 orig_router->name,ls_type,origtime);
Obaid Amin806df812013-02-21 13:30:26 -0600536
537 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque8c5e2662013-02-24 06:37:14 -0600538 if ( nlsr->debugging )
539 printf("LSA Life time: %d\n",lsa_life_time);
540
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600541 if ( (strcmp(orig_router->name,nlsr->router_name) == 0 &&
542 lsa_life_time < nlsr->lsa_refresh_time) ||
543 (strcmp(orig_router->name,nlsr->router_name) != 0 &&
544 lsa_life_time < nlsr->router_dead_interval) )
Obaid Amin806df812013-02-21 13:30:26 -0600545 {
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600546 int is_new_cor_lsa=check_is_new_cor_lsa( orig_router->name,
547 (char *)lst,(char *) origtime);
Obaid Amin806df812013-02-21 13:30:26 -0600548 if ( is_new_cor_lsa == 1 )
549 {
550 if ( nlsr->debugging )
551 printf("New Cor LSA.....\n");
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600552 get_content_by_content_name(ccn_charbuf_as_string(uri),
553 &content_data);
Obaid Amin806df812013-02-21 13:30:26 -0600554
555 if ( nlsr->debugging )
556 printf("Content Data: %s \n",content_data);
557 process_incoming_sync_content_lsa(content_data);
558 }
559 else
560 {
561 if ( nlsr->debugging )
562 printf("Cor LSA / Newer Cor LSA already exists in LSDB\n");
563 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
564 if ( nlsr->debugging )
565 printf("Content Data: %s \n",content_data);
566 }
567 }
568 else
569 {
570 if ( nlsr->debugging )
571 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
572 }
573
574 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600575 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600576
akmhoquef6773612013-02-23 09:53:00 -0600577 if (orig_router->name)
578 free(orig_router->name);
Obaid Amin05300892013-02-21 13:45:25 -0600579 if (orig_router)
580 free(orig_router);
Obaid Amin806df812013-02-21 13:30:26 -0600581 ccn_charbuf_destroy(&uri);
582 //01/31/2013
583 free(time_stamp);
akmhoquea0e71152013-02-11 09:47:59 -0600584}
585
Obaid Amin7793ba72013-02-22 00:43:58 -0600586 int
akmhoquea0e71152013-02-11 09:47:59 -0600587sync_monitor(char *topo_prefix, char *slice_prefix)
588{
589
Obaid Amin806df812013-02-21 13:30:26 -0600590 struct ccn_charbuf *prefix = ccn_charbuf_create();
Obaid Amin806df812013-02-21 13:30:26 -0600591 struct ccn_charbuf *topo = ccn_charbuf_create();
Obaid Amin7997ad82013-02-22 16:41:56 -0600592
593 nlsr->closure=(struct ccns_name_closure *)
594 calloc(1,sizeof(struct ccns_name_closure));
595
Obaid Amin806df812013-02-21 13:30:26 -0600596 nlsr->slice = ccns_slice_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600597
Obaid Amin806df812013-02-21 13:30:26 -0600598 ccn_charbuf_reset(prefix);
599 ccn_name_from_uri(prefix, slice_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600600
Obaid Amin806df812013-02-21 13:30:26 -0600601 ccn_charbuf_reset(topo);
602 ccn_name_from_uri(topo, topo_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600603
Obaid Amin806df812013-02-21 13:30:26 -0600604 ccns_slice_set_topo_prefix(nlsr->slice, topo, prefix);
605 nlsr->closure->callback = &sync_cb;
akmhoque54d86112013-02-21 16:42:34 -0600606 nlsr->ccns = ccns_open(nlsr->ccn, nlsr->slice, nlsr->closure, NULL, NULL);
akmhoquea0e71152013-02-11 09:47:59 -0600607
Obaid Amin7997ad82013-02-22 16:41:56 -0600608 ccn_charbuf_destroy(&prefix);
609 ccn_charbuf_destroy(&topo);
Obaid Amin806df812013-02-21 13:30:26 -0600610 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600611}
612
Obaid Amin7793ba72013-02-22 00:43:58 -0600613 struct ccn_charbuf *
akmhoquea0e71152013-02-11 09:47:59 -0600614make_template(int scope)
615{
Obaid Amin806df812013-02-21 13:30:26 -0600616 struct ccn_charbuf *templ = NULL;
617 templ = ccn_charbuf_create();
618 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
619 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
620 ccn_charbuf_append_closer(templ); /* </Name> */
621 if (0 <= scope && scope <= 2)
622 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
623 ccn_charbuf_append_closer(templ); /* </Interest> */
624 return(templ);
akmhoquea0e71152013-02-11 09:47:59 -0600625}
626
Obaid Amin7793ba72013-02-22 00:43:58 -0600627 int
akmhoquea0e71152013-02-11 09:47:59 -0600628write_data_to_repo(char *data, char *name_prefix)
629{
Obaid Amin806df812013-02-21 13:30:26 -0600630 if ( nlsr->debugging )
631 {
632 printf("write_data_to_repo called\n");
633 printf("Content Name: %s \n",name_prefix);
634 printf("Content Data: %s \n",data);
635 }
akmhoquea0e71152013-02-11 09:47:59 -0600636
Obaid Aminf210a322013-02-22 02:04:52 -0600637 struct ccn *temp_ccn;
Obaid Amin806df812013-02-21 13:30:26 -0600638 temp_ccn=ccn_create();
639 int ccn_fd=ccn_connect(temp_ccn, NULL);
640 if(ccn_fd == -1)
641 {
642 fprintf(stderr,"Could not connect to ccnd for Data Writing\n");
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600643 writeLogg(__FILE__,__FUNCTION__,__LINE__,
644 "Could not connect to ccnd for Data Writing\n");
Obaid Amin806df812013-02-21 13:30:26 -0600645 return -1;
646 }
Obaid Aminf210a322013-02-22 02:04:52 -0600647
Obaid Amin806df812013-02-21 13:30:26 -0600648 struct ccn_charbuf *name = NULL;
649 struct ccn_seqwriter *w = NULL;
650 int blocksize = 4096;
651 int freshness = -1;
Obaid Amin806df812013-02-21 13:30:26 -0600652 int scope = 1;
653 int res;
654 size_t blockread;
655 struct ccn_charbuf *templ;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600656
Obaid Amin806df812013-02-21 13:30:26 -0600657 name = ccn_charbuf_create();
658 res = ccn_name_from_uri(name, name_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600659 if (res < 0) {
Obaid Amin806df812013-02-21 13:30:26 -0600660 fprintf(stderr, "bad CCN URI: %s\n",name_prefix);
661 return -1;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600662 }
Obaid Amin806df812013-02-21 13:30:26 -0600663
Obaid Aminf210a322013-02-22 02:04:52 -0600664 w = ccn_seqw_create(temp_ccn, name);
Obaid Amin806df812013-02-21 13:30:26 -0600665 if (w == NULL) {
666 fprintf(stderr, "ccn_seqw_create failed\n");
667 return -1;
668 }
669 ccn_seqw_set_block_limits(w, blocksize, blocksize);
670 if (freshness > -1)
671 ccn_seqw_set_freshness(w, freshness);
akmhoquea0e71152013-02-11 09:47:59 -0600672
Obaid Amin7997ad82013-02-22 16:41:56 -0600673 struct ccn_charbuf *name_v = ccn_charbuf_create();
674 ccn_seqw_get_name(w, name_v);
675 ccn_name_from_uri(name_v, "%C1.R.sw");
676 ccn_name_append_nonce(name_v);
677 templ = make_template(scope);
678 res = ccn_get(temp_ccn, name_v, templ, 60000, NULL, NULL, NULL, 0);
679 ccn_charbuf_destroy(&templ);
680 ccn_charbuf_destroy(&name_v);
681 if (res < 0) {
682 fprintf(stderr, "No response from repository\n");
683 return -1;
684 }
akmhoquea0e71152013-02-11 09:47:59 -0600685
Obaid Amin806df812013-02-21 13:30:26 -0600686 blockread = 0;
akmhoquea0e71152013-02-11 09:47:59 -0600687
akmhoquee407fc22013-02-24 11:57:17 -0600688 blockread=strlen(data)+1;
akmhoquea0e71152013-02-11 09:47:59 -0600689
Obaid Amin806df812013-02-21 13:30:26 -0600690 if (blockread > 0) {
Obaid Amin806df812013-02-21 13:30:26 -0600691 res = ccn_seqw_write(w, data, blockread);
692 while (res == -1) {
akmhoque8034d502013-02-24 11:53:24 -0600693 ccn_run(temp_ccn,100);
Obaid Amin806df812013-02-21 13:30:26 -0600694 res = ccn_seqw_write(w, data, blockread);
695 }
696 }
697
akmhoque8034d502013-02-24 11:53:24 -0600698 ccn_seqw_close(w);
699 ccn_run(temp_ccn, 100);
Obaid Amin806df812013-02-21 13:30:26 -0600700 ccn_charbuf_destroy(&name);
akmhoque0ed6d982013-02-22 14:28:01 -0600701 ccn_destroy(&temp_ccn);
akmhoquea0e71152013-02-11 09:47:59 -0600702
Obaid Amin806df812013-02-21 13:30:26 -0600703 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600704}
Obaid Amin806df812013-02-21 13:30:26 -0600705 int
Obaid Amin7997ad82013-02-22 16:41:56 -0600706 create_sync_slice(char *topo_prefix, char *slice_prefix)
akmhoquea0e71152013-02-11 09:47:59 -0600707{
Obaid Amin806df812013-02-21 13:30:26 -0600708 int res;
akmhoque0ed6d982013-02-22 14:28:01 -0600709 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 -0600710 struct ccns_slice *slice;
711 struct ccn_charbuf *prefix = ccn_charbuf_create();
712 struct ccn_charbuf *topo = ccn_charbuf_create();
713 struct ccn_charbuf *clause = ccn_charbuf_create();
714 struct ccn_charbuf *slice_name = ccn_charbuf_create();
715 struct ccn_charbuf *slice_uri = ccn_charbuf_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600716
Obaid Amin806df812013-02-21 13:30:26 -0600717 if (prefix == NULL || topo == NULL || clause == NULL ||
718 slice_name == NULL || slice_uri == NULL) {
719 fprintf(stderr, "Unable to allocate required memory.\n");
720 return -1;
721 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600722
akmhoque0ed6d982013-02-22 14:28:01 -0600723 handle = ccn_create();
Obaid Amin806df812013-02-21 13:30:26 -0600724 res = ccn_connect(handle, NULL);
725 if (0 > res) {
726 fprintf(stderr, "Unable to connect to ccnd.\n");
727 return -1;
728 }
akmhoque0ed6d982013-02-22 14:28:01 -0600729
Obaid Amin806df812013-02-21 13:30:26 -0600730 slice = ccns_slice_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600731
Obaid Amin806df812013-02-21 13:30:26 -0600732 ccn_charbuf_reset(topo);
733 ccn_name_from_uri(topo, topo_prefix);
734 ccn_charbuf_reset(prefix);
735 ccn_name_from_uri(prefix,slice_prefix );
736 ccns_slice_set_topo_prefix(slice, topo, prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600737
738
akmhoque0ed6d982013-02-22 14:28:01 -0600739 res = ccns_write_slice(handle, slice, slice_name);
740 //res = ccns_write_slice(nlsr->ccn, slice, slice_name);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600741
Obaid Amin806df812013-02-21 13:30:26 -0600742 //01/31/2013
743 ccns_slice_destroy(&slice);
akmhoque0ed6d982013-02-22 14:28:01 -0600744 ccn_destroy(&handle);
Obaid Amin806df812013-02-21 13:30:26 -0600745 ccn_charbuf_destroy(&prefix);
746 ccn_charbuf_destroy(&topo);
747 ccn_charbuf_destroy(&clause);
748 ccn_charbuf_destroy(&slice_name);
749 ccn_charbuf_destroy(&slice_uri);
akmhoquea0e71152013-02-11 09:47:59 -0600750
Obaid Amin806df812013-02-21 13:30:26 -0600751 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600752}
753
akmhoque0eb8de22013-02-24 14:08:19 -0600754