blob: 58f546bd0bc95f316083e9af298914ad6b7efcc1 [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);
264 *content_data = (unsigned char *) calloc(length, sizeof(char *));
265 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;
369 r=strtof(cor_r,NULL);
370 theta=strtof(cor_theta,NULL);
371
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{
Obaid Amin806df812013-02-21 13:30:26 -0600388 size_t comp_size;
389 char *lst;
390 char *lsid;
391 const unsigned char *second_last_comp;
392 const unsigned char *third_last_comp;
393 const unsigned char *origtime;
394 char *sep=".";
395 char *rem;
396 char *second_comp_type;
akmhoquea0e71152013-02-11 09:47:59 -0600397
Obaid Amin806df812013-02-21 13:30:26 -0600398 int ls_type;
399 long int ls_id=0;
akmhoquea0e71152013-02-11 09:47:59 -0600400
Obaid Amin806df812013-02-21 13:30:26 -0600401 unsigned char *content_data = NULL;
akmhoquea0e71152013-02-11 09:47:59 -0600402
akmhoqueb31caa12013-02-22 08:42:09 -0600403 char *time_stamp=get_current_timestamp_micro_v2();
akmhoquea0e71152013-02-11 09:47:59 -0600404
Obaid Amin806df812013-02-21 13:30:26 -0600405 struct ccn_charbuf *uri = ccn_charbuf_create();
406 ccn_uri_append(uri, content_name->buf, content_name->length, 0);
akmhoquea0e71152013-02-11 09:47:59 -0600407
Obaid Amin7997ad82013-02-22 16:41:56 -0600408 struct name_prefix *orig_router=(struct name_prefix *)
409 calloc( 1, sizeof(struct name_prefix));
akmhoquea0e71152013-02-11 09:47:59 -0600410
Obaid Amin7997ad82013-02-22 16:41:56 -0600411 ccn_name_comp_get( content_name->buf, components,
412 components->n-1-2, &second_last_comp, &comp_size);
413
Obaid Amin806df812013-02-21 13:30:26 -0600414 if (nlsr->debugging)
Obaid Amin7997ad82013-02-22 16:41:56 -0600415 printf("2nd Last Component: %s \n", second_last_comp);
akmhoquea0e71152013-02-11 09:47:59 -0600416
Obaid Amin7997ad82013-02-22 16:41:56 -0600417 second_comp_type=strtok_r((char *)second_last_comp, sep, &rem);
418 if (second_comp_type == NULL || rem == NULL)
419 {
420 printf ("Error: unable to tokenize the string: %s, calling exit()\n",
421 (char *)second_last_comp);
422 exit(0);
423 }
424
Obaid Amin806df812013-02-21 13:30:26 -0600425 if ( strcmp( second_comp_type, "lsId" ) == 0 )
426 {
427 lsid=rem;
428 ls_id=atoi(rem);
429 ccn_name_comp_get(content_name->buf, components,components->n-2-2,&third_last_comp, &comp_size);
430 lst=strtok_r((char *)third_last_comp,sep,&rem);
431 lst=rem;
432 ls_type=atoi(lst);
433 ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size);
434 ccn_name_chop(content_name,components,-3);
435 get_name_part(orig_router,content_name,components,0);
akmhoquea0e71152013-02-11 09:47:59 -0600436
Obaid Aminbb8ac422013-02-20 17:08:48 -0600437 if ( nlsr->debugging )
Obaid Amin806df812013-02-21 13:30:26 -0600438 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 -0600439
Obaid Amin806df812013-02-21 13:30:26 -0600440 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600441
Obaid Amin806df812013-02-21 13:30:26 -0600442 if ( (strcmp(orig_router->name,nlsr->router_name) == 0 && lsa_life_time < nlsr->lsa_refresh_time)
443 || (strcmp(orig_router->name,nlsr->router_name) != 0 && lsa_life_time < nlsr->router_dead_interval) )
Obaid Aminbb8ac422013-02-20 17:08:48 -0600444 {
Obaid Amin806df812013-02-21 13:30:26 -0600445 int is_new_name_lsa=check_is_new_name_lsa(orig_router->name,(char *)lst,(char *)lsid,(char *)origtime);
446 if ( is_new_name_lsa == 1 )
447 {
448 if ( nlsr->debugging )
449 printf("New NAME LSA.....\n");
450 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
451 if ( nlsr->debugging )
452 printf("Content Data: %s \n",content_data);
453 process_incoming_sync_content_lsa(content_data);
454 }
455 else
456 {
457 if ( nlsr->debugging )
458 printf("Name LSA / Newer Name LSA already xists in LSDB\n");
459 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600460
Obaid Amin806df812013-02-21 13:30:26 -0600461 if ( nlsr->debugging )
462 printf("Content Data: %s \n",content_data);
463 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600464 }
Obaid Amin806df812013-02-21 13:30:26 -0600465 else
Obaid Aminbb8ac422013-02-20 17:08:48 -0600466 {
Obaid Amin806df812013-02-21 13:30:26 -0600467 if ( nlsr->debugging )
468 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
Obaid Aminbb8ac422013-02-20 17:08:48 -0600469 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600470 }
Obaid Amin806df812013-02-21 13:30:26 -0600471 else
Obaid Aminbb8ac422013-02-20 17:08:48 -0600472 {
Obaid Amin806df812013-02-21 13:30:26 -0600473 ls_type=atoi(rem);
474 lst=rem;
475 if(ls_type == LS_TYPE_ADJ)
Obaid Aminbb8ac422013-02-20 17:08:48 -0600476 {
Obaid Amin806df812013-02-21 13:30:26 -0600477 ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size);
478 ccn_name_chop(content_name,components,-2);
479 get_name_part(orig_router,content_name,components,0);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600480
Obaid Amin806df812013-02-21 13:30:26 -0600481 if ( nlsr->debugging )
482 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",orig_router->name,ls_type,origtime);
483
484 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
485 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) )
486 {
487 int is_new_adj_lsa=check_is_new_adj_lsa(orig_router->name,(char *)lst,(char *)origtime);
488 if ( is_new_adj_lsa == 1 )
489 {
490 if ( nlsr->debugging )
491 printf("New Adj LSA.....\n");
492 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
493
494 if ( nlsr->debugging )
495 printf("Content Data: %s \n",content_data);
496 process_incoming_sync_content_lsa(content_data);
497 }
498 else
499 {
500 if ( nlsr->debugging )
501 printf("Adj LSA / Newer Adj LSA already exists in LSDB\n");
502 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
503 if ( nlsr->debugging )
504 printf("Content Data: %s \n",content_data);
505 }
506 }
507 else
508 {
509 if ( nlsr->debugging )
510 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
511 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600512 }
Obaid Amin806df812013-02-21 13:30:26 -0600513 else if(ls_type == LS_TYPE_COR)
Obaid Aminbb8ac422013-02-20 17:08:48 -0600514 {
Obaid Amin806df812013-02-21 13:30:26 -0600515 ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size);
516 ccn_name_chop(content_name,components,-2);
517 get_name_part(orig_router,content_name,components,0);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600518
Obaid Amin806df812013-02-21 13:30:26 -0600519 if ( nlsr->debugging )
520 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",orig_router->name,ls_type,origtime);
521
522 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
523 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) )
524 {
525 int is_new_cor_lsa=check_is_new_cor_lsa(orig_router->name,(char *)lst,(char *)origtime);
526 if ( is_new_cor_lsa == 1 )
527 {
528 if ( nlsr->debugging )
529 printf("New Cor LSA.....\n");
530 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
531
532 if ( nlsr->debugging )
533 printf("Content Data: %s \n",content_data);
534 process_incoming_sync_content_lsa(content_data);
535 }
536 else
537 {
538 if ( nlsr->debugging )
539 printf("Cor LSA / Newer Cor LSA already exists in LSDB\n");
540 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
541 if ( nlsr->debugging )
542 printf("Content Data: %s \n",content_data);
543 }
544 }
545 else
546 {
547 if ( nlsr->debugging )
548 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
549 }
550
551 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600552 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600553
akmhoquef6773612013-02-23 09:53:00 -0600554 if (orig_router->name)
555 free(orig_router->name);
Obaid Amin05300892013-02-21 13:45:25 -0600556 if (orig_router)
557 free(orig_router);
Obaid Amin806df812013-02-21 13:30:26 -0600558 ccn_charbuf_destroy(&uri);
559 //01/31/2013
560 free(time_stamp);
akmhoquea0e71152013-02-11 09:47:59 -0600561}
562
Obaid Amin7793ba72013-02-22 00:43:58 -0600563 int
akmhoquea0e71152013-02-11 09:47:59 -0600564sync_monitor(char *topo_prefix, char *slice_prefix)
565{
566
Obaid Amin806df812013-02-21 13:30:26 -0600567 struct ccn_charbuf *prefix = ccn_charbuf_create();
Obaid Amin806df812013-02-21 13:30:26 -0600568 struct ccn_charbuf *topo = ccn_charbuf_create();
Obaid Amin7997ad82013-02-22 16:41:56 -0600569
570 nlsr->closure=(struct ccns_name_closure *)
571 calloc(1,sizeof(struct ccns_name_closure));
572
Obaid Amin806df812013-02-21 13:30:26 -0600573 nlsr->slice = ccns_slice_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600574
Obaid Amin806df812013-02-21 13:30:26 -0600575 ccn_charbuf_reset(prefix);
576 ccn_name_from_uri(prefix, slice_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600577
Obaid Amin806df812013-02-21 13:30:26 -0600578 ccn_charbuf_reset(topo);
579 ccn_name_from_uri(topo, topo_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600580
Obaid Amin806df812013-02-21 13:30:26 -0600581 ccns_slice_set_topo_prefix(nlsr->slice, topo, prefix);
582 nlsr->closure->callback = &sync_cb;
akmhoque54d86112013-02-21 16:42:34 -0600583 nlsr->ccns = ccns_open(nlsr->ccn, nlsr->slice, nlsr->closure, NULL, NULL);
akmhoquea0e71152013-02-11 09:47:59 -0600584
Obaid Amin7997ad82013-02-22 16:41:56 -0600585 ccn_charbuf_destroy(&prefix);
586 ccn_charbuf_destroy(&topo);
Obaid Amin806df812013-02-21 13:30:26 -0600587 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600588}
589
Obaid Amin7793ba72013-02-22 00:43:58 -0600590 struct ccn_charbuf *
akmhoquea0e71152013-02-11 09:47:59 -0600591make_template(int scope)
592{
Obaid Amin806df812013-02-21 13:30:26 -0600593 struct ccn_charbuf *templ = NULL;
594 templ = ccn_charbuf_create();
595 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
596 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
597 ccn_charbuf_append_closer(templ); /* </Name> */
598 if (0 <= scope && scope <= 2)
599 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
600 ccn_charbuf_append_closer(templ); /* </Interest> */
601 return(templ);
akmhoquea0e71152013-02-11 09:47:59 -0600602}
603
Obaid Amin7793ba72013-02-22 00:43:58 -0600604 int
akmhoquea0e71152013-02-11 09:47:59 -0600605write_data_to_repo(char *data, char *name_prefix)
606{
Obaid Amin806df812013-02-21 13:30:26 -0600607 if ( nlsr->debugging )
608 {
609 printf("write_data_to_repo called\n");
610 printf("Content Name: %s \n",name_prefix);
611 printf("Content Data: %s \n",data);
612 }
akmhoquea0e71152013-02-11 09:47:59 -0600613
Obaid Aminf210a322013-02-22 02:04:52 -0600614 struct ccn *temp_ccn;
Obaid Amin806df812013-02-21 13:30:26 -0600615 temp_ccn=ccn_create();
616 int ccn_fd=ccn_connect(temp_ccn, NULL);
617 if(ccn_fd == -1)
618 {
619 fprintf(stderr,"Could not connect to ccnd for Data Writing\n");
620 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd for Data Writing\n");
621 return -1;
622 }
Obaid Aminf210a322013-02-22 02:04:52 -0600623
Obaid Amin806df812013-02-21 13:30:26 -0600624 struct ccn_charbuf *name = NULL;
625 struct ccn_seqwriter *w = NULL;
626 int blocksize = 4096;
627 int freshness = -1;
Obaid Amin806df812013-02-21 13:30:26 -0600628 int scope = 1;
629 int res;
630 size_t blockread;
631 struct ccn_charbuf *templ;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600632
Obaid Amin806df812013-02-21 13:30:26 -0600633 name = ccn_charbuf_create();
634 res = ccn_name_from_uri(name, name_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600635 if (res < 0) {
Obaid Amin806df812013-02-21 13:30:26 -0600636 fprintf(stderr, "bad CCN URI: %s\n",name_prefix);
637 return -1;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600638 }
Obaid Amin806df812013-02-21 13:30:26 -0600639
Obaid Aminf210a322013-02-22 02:04:52 -0600640 w = ccn_seqw_create(temp_ccn, name);
Obaid Amin806df812013-02-21 13:30:26 -0600641 if (w == NULL) {
642 fprintf(stderr, "ccn_seqw_create failed\n");
643 return -1;
644 }
645 ccn_seqw_set_block_limits(w, blocksize, blocksize);
646 if (freshness > -1)
647 ccn_seqw_set_freshness(w, freshness);
akmhoquea0e71152013-02-11 09:47:59 -0600648
Obaid Amin7997ad82013-02-22 16:41:56 -0600649 struct ccn_charbuf *name_v = ccn_charbuf_create();
650 ccn_seqw_get_name(w, name_v);
651 ccn_name_from_uri(name_v, "%C1.R.sw");
652 ccn_name_append_nonce(name_v);
653 templ = make_template(scope);
654 res = ccn_get(temp_ccn, name_v, templ, 60000, NULL, NULL, NULL, 0);
655 ccn_charbuf_destroy(&templ);
656 ccn_charbuf_destroy(&name_v);
657 if (res < 0) {
658 fprintf(stderr, "No response from repository\n");
659 return -1;
660 }
akmhoquea0e71152013-02-11 09:47:59 -0600661
Obaid Amin806df812013-02-21 13:30:26 -0600662 blockread = 0;
akmhoquea0e71152013-02-11 09:47:59 -0600663
Obaid Amin806df812013-02-21 13:30:26 -0600664 blockread=strlen(data);
akmhoquea0e71152013-02-11 09:47:59 -0600665
Obaid Amin806df812013-02-21 13:30:26 -0600666 if (blockread > 0) {
Obaid Amin806df812013-02-21 13:30:26 -0600667 res = ccn_seqw_write(w, data, blockread);
668 while (res == -1) {
akmhoque54309a22013-02-22 15:38:25 -0600669 ccn_run(temp_ccn,1);
Obaid Amin806df812013-02-21 13:30:26 -0600670 res = ccn_seqw_write(w, data, blockread);
671 }
672 }
673
akmhoque54309a22013-02-22 15:38:25 -0600674
675 ccn_run(temp_ccn, 1);
Obaid Amin806df812013-02-21 13:30:26 -0600676 ccn_charbuf_destroy(&name);
akmhoque0ed6d982013-02-22 14:28:01 -0600677 ccn_destroy(&temp_ccn);
akmhoquea0e71152013-02-11 09:47:59 -0600678
Obaid Amin806df812013-02-21 13:30:26 -0600679 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600680}
Obaid Amin806df812013-02-21 13:30:26 -0600681 int
Obaid Amin7997ad82013-02-22 16:41:56 -0600682 create_sync_slice(char *topo_prefix, char *slice_prefix)
akmhoquea0e71152013-02-11 09:47:59 -0600683{
Obaid Amin806df812013-02-21 13:30:26 -0600684 int res;
akmhoque0ed6d982013-02-22 14:28:01 -0600685 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 -0600686 struct ccns_slice *slice;
687 struct ccn_charbuf *prefix = ccn_charbuf_create();
688 struct ccn_charbuf *topo = ccn_charbuf_create();
689 struct ccn_charbuf *clause = ccn_charbuf_create();
690 struct ccn_charbuf *slice_name = ccn_charbuf_create();
691 struct ccn_charbuf *slice_uri = ccn_charbuf_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600692
Obaid Amin806df812013-02-21 13:30:26 -0600693 if (prefix == NULL || topo == NULL || clause == NULL ||
694 slice_name == NULL || slice_uri == NULL) {
695 fprintf(stderr, "Unable to allocate required memory.\n");
696 return -1;
697 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600698
akmhoque0ed6d982013-02-22 14:28:01 -0600699 handle = ccn_create();
Obaid Amin806df812013-02-21 13:30:26 -0600700 res = ccn_connect(handle, NULL);
701 if (0 > res) {
702 fprintf(stderr, "Unable to connect to ccnd.\n");
703 return -1;
704 }
akmhoque0ed6d982013-02-22 14:28:01 -0600705
Obaid Amin806df812013-02-21 13:30:26 -0600706 slice = ccns_slice_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600707
Obaid Amin806df812013-02-21 13:30:26 -0600708 ccn_charbuf_reset(topo);
709 ccn_name_from_uri(topo, topo_prefix);
710 ccn_charbuf_reset(prefix);
711 ccn_name_from_uri(prefix,slice_prefix );
712 ccns_slice_set_topo_prefix(slice, topo, prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600713
714
akmhoque0ed6d982013-02-22 14:28:01 -0600715 res = ccns_write_slice(handle, slice, slice_name);
716 //res = ccns_write_slice(nlsr->ccn, slice, slice_name);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600717
Obaid Amin806df812013-02-21 13:30:26 -0600718 //01/31/2013
719 ccns_slice_destroy(&slice);
akmhoque0ed6d982013-02-22 14:28:01 -0600720 ccn_destroy(&handle);
Obaid Amin806df812013-02-21 13:30:26 -0600721 ccn_charbuf_destroy(&prefix);
722 ccn_charbuf_destroy(&topo);
723 ccn_charbuf_destroy(&clause);
724 ccn_charbuf_destroy(&slice_name);
725 ccn_charbuf_destroy(&slice_uri);
akmhoquea0e71152013-02-11 09:47:59 -0600726
Obaid Amin806df812013-02-21 13:30:26 -0600727 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600728}
729