blob: c1263b4d864847f3551617c6423f65e7aafd93bd [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);
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 Amin806df812013-02-21 13:30:26 -0600481 ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size);
482 ccn_name_chop(content_name,components,-2);
483 get_name_part(orig_router,content_name,components,0);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600484
Obaid Amin806df812013-02-21 13:30:26 -0600485 if ( nlsr->debugging )
486 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",orig_router->name,ls_type,origtime);
487
488 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque8c5e2662013-02-24 06:37:14 -0600489 if ( nlsr->debugging )
490 printf("LSA Life time: %d\n",lsa_life_time);
491
Obaid Amin806df812013-02-21 13:30:26 -0600492 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) )
493 {
494 int is_new_adj_lsa=check_is_new_adj_lsa(orig_router->name,(char *)lst,(char *)origtime);
495 if ( is_new_adj_lsa == 1 )
496 {
497 if ( nlsr->debugging )
498 printf("New Adj LSA.....\n");
499 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
500
501 if ( nlsr->debugging )
502 printf("Content Data: %s \n",content_data);
503 process_incoming_sync_content_lsa(content_data);
504 }
505 else
506 {
507 if ( nlsr->debugging )
508 printf("Adj LSA / Newer Adj LSA already exists in LSDB\n");
509 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
510 if ( nlsr->debugging )
511 printf("Content Data: %s \n",content_data);
512 }
513 }
514 else
515 {
516 if ( nlsr->debugging )
517 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
518 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600519 }
Obaid Amin806df812013-02-21 13:30:26 -0600520 else if(ls_type == LS_TYPE_COR)
Obaid Aminbb8ac422013-02-20 17:08:48 -0600521 {
Obaid Amin806df812013-02-21 13:30:26 -0600522 ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size);
523 ccn_name_chop(content_name,components,-2);
524 get_name_part(orig_router,content_name,components,0);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600525
Obaid Amin806df812013-02-21 13:30:26 -0600526 if ( nlsr->debugging )
527 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",orig_router->name,ls_type,origtime);
528
529 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque8c5e2662013-02-24 06:37:14 -0600530 if ( nlsr->debugging )
531 printf("LSA Life time: %d\n",lsa_life_time);
532
Obaid Amin806df812013-02-21 13:30:26 -0600533 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) )
534 {
535 int is_new_cor_lsa=check_is_new_cor_lsa(orig_router->name,(char *)lst,(char *)origtime);
536 if ( is_new_cor_lsa == 1 )
537 {
538 if ( nlsr->debugging )
539 printf("New Cor LSA.....\n");
540 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
541
542 if ( nlsr->debugging )
543 printf("Content Data: %s \n",content_data);
544 process_incoming_sync_content_lsa(content_data);
545 }
546 else
547 {
548 if ( nlsr->debugging )
549 printf("Cor LSA / Newer Cor LSA already exists in LSDB\n");
550 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
551 if ( nlsr->debugging )
552 printf("Content Data: %s \n",content_data);
553 }
554 }
555 else
556 {
557 if ( nlsr->debugging )
558 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
559 }
560
561 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600562 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600563
akmhoquef6773612013-02-23 09:53:00 -0600564 if (orig_router->name)
565 free(orig_router->name);
Obaid Amin05300892013-02-21 13:45:25 -0600566 if (orig_router)
567 free(orig_router);
Obaid Amin806df812013-02-21 13:30:26 -0600568 ccn_charbuf_destroy(&uri);
569 //01/31/2013
570 free(time_stamp);
akmhoquea0e71152013-02-11 09:47:59 -0600571}
572
Obaid Amin7793ba72013-02-22 00:43:58 -0600573 int
akmhoquea0e71152013-02-11 09:47:59 -0600574sync_monitor(char *topo_prefix, char *slice_prefix)
575{
576
Obaid Amin806df812013-02-21 13:30:26 -0600577 struct ccn_charbuf *prefix = ccn_charbuf_create();
Obaid Amin806df812013-02-21 13:30:26 -0600578 struct ccn_charbuf *topo = ccn_charbuf_create();
Obaid Amin7997ad82013-02-22 16:41:56 -0600579
580 nlsr->closure=(struct ccns_name_closure *)
581 calloc(1,sizeof(struct ccns_name_closure));
582
Obaid Amin806df812013-02-21 13:30:26 -0600583 nlsr->slice = ccns_slice_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600584
Obaid Amin806df812013-02-21 13:30:26 -0600585 ccn_charbuf_reset(prefix);
586 ccn_name_from_uri(prefix, slice_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600587
Obaid Amin806df812013-02-21 13:30:26 -0600588 ccn_charbuf_reset(topo);
589 ccn_name_from_uri(topo, topo_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600590
Obaid Amin806df812013-02-21 13:30:26 -0600591 ccns_slice_set_topo_prefix(nlsr->slice, topo, prefix);
592 nlsr->closure->callback = &sync_cb;
akmhoque54d86112013-02-21 16:42:34 -0600593 nlsr->ccns = ccns_open(nlsr->ccn, nlsr->slice, nlsr->closure, NULL, NULL);
akmhoquea0e71152013-02-11 09:47:59 -0600594
Obaid Amin7997ad82013-02-22 16:41:56 -0600595 ccn_charbuf_destroy(&prefix);
596 ccn_charbuf_destroy(&topo);
Obaid Amin806df812013-02-21 13:30:26 -0600597 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600598}
599
Obaid Amin7793ba72013-02-22 00:43:58 -0600600 struct ccn_charbuf *
akmhoquea0e71152013-02-11 09:47:59 -0600601make_template(int scope)
602{
Obaid Amin806df812013-02-21 13:30:26 -0600603 struct ccn_charbuf *templ = NULL;
604 templ = ccn_charbuf_create();
605 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
606 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
607 ccn_charbuf_append_closer(templ); /* </Name> */
608 if (0 <= scope && scope <= 2)
609 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
610 ccn_charbuf_append_closer(templ); /* </Interest> */
611 return(templ);
akmhoquea0e71152013-02-11 09:47:59 -0600612}
613
Obaid Amin7793ba72013-02-22 00:43:58 -0600614 int
akmhoquea0e71152013-02-11 09:47:59 -0600615write_data_to_repo(char *data, char *name_prefix)
616{
Obaid Amin806df812013-02-21 13:30:26 -0600617 if ( nlsr->debugging )
618 {
619 printf("write_data_to_repo called\n");
620 printf("Content Name: %s \n",name_prefix);
621 printf("Content Data: %s \n",data);
622 }
akmhoquea0e71152013-02-11 09:47:59 -0600623
Obaid Aminf210a322013-02-22 02:04:52 -0600624 struct ccn *temp_ccn;
Obaid Amin806df812013-02-21 13:30:26 -0600625 temp_ccn=ccn_create();
626 int ccn_fd=ccn_connect(temp_ccn, NULL);
627 if(ccn_fd == -1)
628 {
629 fprintf(stderr,"Could not connect to ccnd for Data Writing\n");
630 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd for Data Writing\n");
631 return -1;
632 }
Obaid Aminf210a322013-02-22 02:04:52 -0600633
Obaid Amin806df812013-02-21 13:30:26 -0600634 struct ccn_charbuf *name = NULL;
635 struct ccn_seqwriter *w = NULL;
636 int blocksize = 4096;
637 int freshness = -1;
Obaid Amin806df812013-02-21 13:30:26 -0600638 int scope = 1;
639 int res;
640 size_t blockread;
641 struct ccn_charbuf *templ;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600642
Obaid Amin806df812013-02-21 13:30:26 -0600643 name = ccn_charbuf_create();
644 res = ccn_name_from_uri(name, name_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600645 if (res < 0) {
Obaid Amin806df812013-02-21 13:30:26 -0600646 fprintf(stderr, "bad CCN URI: %s\n",name_prefix);
647 return -1;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600648 }
Obaid Amin806df812013-02-21 13:30:26 -0600649
Obaid Aminf210a322013-02-22 02:04:52 -0600650 w = ccn_seqw_create(temp_ccn, name);
Obaid Amin806df812013-02-21 13:30:26 -0600651 if (w == NULL) {
652 fprintf(stderr, "ccn_seqw_create failed\n");
653 return -1;
654 }
655 ccn_seqw_set_block_limits(w, blocksize, blocksize);
656 if (freshness > -1)
657 ccn_seqw_set_freshness(w, freshness);
akmhoquea0e71152013-02-11 09:47:59 -0600658
Obaid Amin7997ad82013-02-22 16:41:56 -0600659 struct ccn_charbuf *name_v = ccn_charbuf_create();
660 ccn_seqw_get_name(w, name_v);
661 ccn_name_from_uri(name_v, "%C1.R.sw");
662 ccn_name_append_nonce(name_v);
663 templ = make_template(scope);
664 res = ccn_get(temp_ccn, name_v, templ, 60000, NULL, NULL, NULL, 0);
665 ccn_charbuf_destroy(&templ);
666 ccn_charbuf_destroy(&name_v);
667 if (res < 0) {
668 fprintf(stderr, "No response from repository\n");
669 return -1;
670 }
akmhoquea0e71152013-02-11 09:47:59 -0600671
Obaid Amin806df812013-02-21 13:30:26 -0600672 blockread = 0;
akmhoquea0e71152013-02-11 09:47:59 -0600673
akmhoquee407fc22013-02-24 11:57:17 -0600674 blockread=strlen(data)+1;
akmhoquea0e71152013-02-11 09:47:59 -0600675
Obaid Amin806df812013-02-21 13:30:26 -0600676 if (blockread > 0) {
Obaid Amin806df812013-02-21 13:30:26 -0600677 res = ccn_seqw_write(w, data, blockread);
678 while (res == -1) {
akmhoque8034d502013-02-24 11:53:24 -0600679 ccn_run(temp_ccn,100);
Obaid Amin806df812013-02-21 13:30:26 -0600680 res = ccn_seqw_write(w, data, blockread);
681 }
682 }
683
akmhoque8034d502013-02-24 11:53:24 -0600684 ccn_seqw_close(w);
685 ccn_run(temp_ccn, 100);
Obaid Amin806df812013-02-21 13:30:26 -0600686 ccn_charbuf_destroy(&name);
akmhoque0ed6d982013-02-22 14:28:01 -0600687 ccn_destroy(&temp_ccn);
akmhoquea0e71152013-02-11 09:47:59 -0600688
Obaid Amin806df812013-02-21 13:30:26 -0600689 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600690}
Obaid Amin806df812013-02-21 13:30:26 -0600691 int
Obaid Amin7997ad82013-02-22 16:41:56 -0600692 create_sync_slice(char *topo_prefix, char *slice_prefix)
akmhoquea0e71152013-02-11 09:47:59 -0600693{
Obaid Amin806df812013-02-21 13:30:26 -0600694 int res;
akmhoque0ed6d982013-02-22 14:28:01 -0600695 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 -0600696 struct ccns_slice *slice;
697 struct ccn_charbuf *prefix = ccn_charbuf_create();
698 struct ccn_charbuf *topo = ccn_charbuf_create();
699 struct ccn_charbuf *clause = ccn_charbuf_create();
700 struct ccn_charbuf *slice_name = ccn_charbuf_create();
701 struct ccn_charbuf *slice_uri = ccn_charbuf_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600702
Obaid Amin806df812013-02-21 13:30:26 -0600703 if (prefix == NULL || topo == NULL || clause == NULL ||
704 slice_name == NULL || slice_uri == NULL) {
705 fprintf(stderr, "Unable to allocate required memory.\n");
706 return -1;
707 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600708
akmhoque0ed6d982013-02-22 14:28:01 -0600709 handle = ccn_create();
Obaid Amin806df812013-02-21 13:30:26 -0600710 res = ccn_connect(handle, NULL);
711 if (0 > res) {
712 fprintf(stderr, "Unable to connect to ccnd.\n");
713 return -1;
714 }
akmhoque0ed6d982013-02-22 14:28:01 -0600715
Obaid Amin806df812013-02-21 13:30:26 -0600716 slice = ccns_slice_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600717
Obaid Amin806df812013-02-21 13:30:26 -0600718 ccn_charbuf_reset(topo);
719 ccn_name_from_uri(topo, topo_prefix);
720 ccn_charbuf_reset(prefix);
721 ccn_name_from_uri(prefix,slice_prefix );
722 ccns_slice_set_topo_prefix(slice, topo, prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600723
724
akmhoque0ed6d982013-02-22 14:28:01 -0600725 res = ccns_write_slice(handle, slice, slice_name);
726 //res = ccns_write_slice(nlsr->ccn, slice, slice_name);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600727
Obaid Amin806df812013-02-21 13:30:26 -0600728 //01/31/2013
729 ccns_slice_destroy(&slice);
akmhoque0ed6d982013-02-22 14:28:01 -0600730 ccn_destroy(&handle);
Obaid Amin806df812013-02-21 13:30:26 -0600731 ccn_charbuf_destroy(&prefix);
732 ccn_charbuf_destroy(&topo);
733 ccn_charbuf_destroy(&clause);
734 ccn_charbuf_destroy(&slice_name);
735 ccn_charbuf_destroy(&slice_uri);
akmhoquea0e71152013-02-11 09:47:59 -0600736
Obaid Amin806df812013-02-21 13:30:26 -0600737 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600738}
739