blob: 53652b50be5ecfd2616d77710e615a9d1a46bf35 [file] [log] [blame]
akmhoquea0e71152013-02-11 09:47:59 -06001#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4#include <unistd.h>
5#include <getopt.h>
6#include <sys/time.h>
7#include <sys/stat.h>
8#include <assert.h>
9#include <sys/types.h>
10#include <signal.h>
11#include <sys/socket.h>
12#include <sys/un.h>
13#include <fcntl.h>
14#include <sys/ioctl.h>
15#include <netinet/in.h>
16#include <netdb.h>
17#include <arpa/inet.h>
18
19#include <ccn/ccn.h>
20#include <ccn/uri.h>
21#include <ccn/keystore.h>
22#include <ccn/signing.h>
23#include <ccn/schedule.h>
24#include <ccn/hashtb.h>
25#include <ccn/sync.h>
26#include <ccn/seqwriter.h>
27
28#include "nlsr.h"
29#include "nlsr_sync.h"
30#include "nlsr_lsdb.h"
31#include "utility.h"
akmhoqueedb68d92013-03-05 10:18:16 -060032#include "nlsr_km.h"
akmhoquea0e71152013-02-11 09:47:59 -060033
34
akmhoqueedb68d92013-03-05 10:18:16 -060035char *
akmhoquea0e71152013-02-11 09:47:59 -060036hex_string(unsigned char *s, size_t l)
37{
Obaid Amin806df812013-02-21 13:30:26 -060038 const char *hex_digits = "0123456789abcdef";
39 char *r;
40 int i;
41 r = calloc(1, 1 + 2 * l);
42 for (i = 0; i < l; i++) {
43 r[2*i] = hex_digits[(s[i]>>4) & 0xf];
44 r[1+2*i] = hex_digits[s[i] & 0xf];
45 }
46 return(r);
akmhoquea0e71152013-02-11 09:47:59 -060047}
48
akmhoqueedb68d92013-03-05 10:18:16 -060049int
akmhoquea0e71152013-02-11 09:47:59 -060050sync_cb(struct ccns_name_closure *nc,
Obaid Amin806df812013-02-21 13:30:26 -060051 struct ccn_charbuf *lhash,
52 struct ccn_charbuf *rhash,
53 struct ccn_charbuf *name)
akmhoquea0e71152013-02-11 09:47:59 -060054{
Obaid Amin806df812013-02-21 13:30:26 -060055 int res;
Obaid Amin7997ad82013-02-22 16:41:56 -060056 struct ccn_charbuf *content_name;
57 struct ccn_indexbuf *content_comps;
58 struct ccn_indexbuf *name_comps;
59
60 content_comps = ccn_indexbuf_create();
61 res = ccn_name_split(name, content_comps);
Obaid Amin806df812013-02-21 13:30:26 -060062 if ( res < 0 )
63 return 0;
Obaid Amin7997ad82013-02-22 16:41:56 -060064
65 if (content_comps->n < 2)
Obaid Amin806df812013-02-21 13:30:26 -060066 return 0;
Obaid Amin7997ad82013-02-22 16:41:56 -060067
68 content_name = ccn_charbuf_create();
69 ccn_name_init(content_name);
70
71 res = ccn_name_append_components( content_name, name->buf,
72 content_comps->buf[0], content_comps->buf[content_comps->n - 1]);
Obaid Amin806df812013-02-21 13:30:26 -060073
74 if ( res < 0)
75 return 0;
akmhoquea0e71152013-02-11 09:47:59 -060076
Obaid Amin7997ad82013-02-22 16:41:56 -060077 // for debugging
Obaid Amin806df812013-02-21 13:30:26 -060078 struct ccn_charbuf *temp=ccn_charbuf_create();
79 ccn_uri_append(temp, content_name->buf, content_name->length, 0);
80 if ( nlsr->debugging )
81 printf("Name before chopping: %s \n",ccn_charbuf_as_string(temp));
82 ccn_charbuf_destroy(&temp);
akmhoquea0e71152013-02-11 09:47:59 -060083
Obaid Amin7997ad82013-02-22 16:41:56 -060084 name_comps = ccn_indexbuf_create();
85 res=ccn_name_split (content_name, name_comps);
86 if (res < 0)
Obaid Amin806df812013-02-21 13:30:26 -060087 return 0;
akmhoquea0e71152013-02-11 09:47:59 -060088
Obaid Amin806df812013-02-21 13:30:26 -060089 if ( nlsr->debugging )
90 {
91 printf("Number of components in name = %d \n",res);
92 printf("Number of components in name as indexbuf->n = %d \n",
Obaid Amin7997ad82013-02-22 16:41:56 -060093 (int)name_comps->n);
Obaid Amin806df812013-02-21 13:30:26 -060094 }
Obaid Amin7997ad82013-02-22 16:41:56 -060095
96 ccn_name_chop(content_name, name_comps, -3);
Obaid Amin806df812013-02-21 13:30:26 -060097 if ( nlsr->debugging )
98 printf("Number of components in name as indexbuf->n after chopping= %d \n"
Obaid Amin7997ad82013-02-22 16:41:56 -060099 , (int)name_comps->n);
akmhoquea0e71152013-02-11 09:47:59 -0600100
Obaid Amin7997ad82013-02-22 16:41:56 -0600101 //for debugging
Obaid Amin806df812013-02-21 13:30:26 -0600102 struct ccn_charbuf *temp1=ccn_charbuf_create();
103 ccn_uri_append(temp1, content_name->buf, content_name->length, 0);
104 if ( nlsr->debugging )
105 printf("Name after chopping: %s \n",ccn_charbuf_as_string(temp1));
106 ccn_charbuf_destroy(&temp1);
akmhoquea0e71152013-02-11 09:47:59 -0600107
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600108 //main method that processes contents from the sync
Obaid Amin7997ad82013-02-22 16:41:56 -0600109 process_content_from_sync(content_name, name_comps);
110
Obaid Amin806df812013-02-21 13:30:26 -0600111 ccn_charbuf_destroy(&content_name);
Obaid Amin7997ad82013-02-22 16:41:56 -0600112 ccn_indexbuf_destroy(&content_comps);
113 ccn_indexbuf_destroy(&name_comps);
akmhoquea0e71152013-02-11 09:47:59 -0600114
Obaid Amin806df812013-02-21 13:30:26 -0600115 return(0);
akmhoquea0e71152013-02-11 09:47:59 -0600116}
117
118
akmhoqueccb33e92013-02-20 11:44:28 -0600119
akmhoqueedb68d92013-03-05 10:18:16 -0600120void
akmhoqueccb33e92013-02-20 11:44:28 -0600121get_name_part(struct name_prefix *name_part,struct ccn_charbuf * interest_ccnb,
Obaid Amin806df812013-02-21 13:30:26 -0600122 struct ccn_indexbuf *interest_comps, int offset)
akmhoquea0e71152013-02-11 09:47:59 -0600123{
Obaid Amin806df812013-02-21 13:30:26 -0600124 int lsa_position=0;
akmhoqueedb68d92013-03-05 10:18:16 -0600125
akmhoque0b60ba92013-02-25 17:55:35 -0600126 struct ccn_indexbuf *components=ccn_indexbuf_create();
Obaid Amin806df812013-02-21 13:30:26 -0600127 struct ccn_charbuf *name=ccn_charbuf_create();
128 ccn_name_from_uri(name,nlsr->slice_prefix);
129 ccn_name_split (name, components);
130 lsa_position=components->n-2;
Obaid Amin806df812013-02-21 13:30:26 -0600131 ccn_charbuf_destroy(&name);
akmhoquea0e71152013-02-11 09:47:59 -0600132
akmhoquef6773612013-02-23 09:53:00 -0600133 struct ccn_charbuf *temp=ccn_charbuf_create();
134 ccn_name_init(temp);
135 ccn_name_append_components( temp, interest_ccnb->buf,
136 interest_comps->buf[lsa_position+1], interest_comps->buf[interest_comps->n - 1]);
137
138 struct ccn_charbuf *temp1=ccn_charbuf_create();
139 ccn_uri_append(temp1, temp->buf, temp->length, 0);
140
141 name_part->name=(char *)calloc(strlen(ccn_charbuf_as_string(temp1))+1,sizeof(char));
142 memcpy(name_part->name,ccn_charbuf_as_string(temp1),strlen(ccn_charbuf_as_string(temp1)));
143 name_part->name[strlen(ccn_charbuf_as_string(temp1))]='\0';
144 name_part->length=strlen(ccn_charbuf_as_string(temp1))+1;
145
146 ccn_charbuf_destroy(&temp1);
147 ccn_charbuf_destroy(&temp);
akmhoque0b60ba92013-02-25 17:55:35 -0600148 ccn_indexbuf_destroy(&components);
akmhoquef6773612013-02-23 09:53:00 -0600149
150 if ( nlsr->debugging )
151 printf("Name Part: %s \n",name_part->name);
152
akmhoquea0e71152013-02-11 09:47:59 -0600153}
154
akmhoquea0e71152013-02-11 09:47:59 -0600155
akmhoquea0e71152013-02-11 09:47:59 -0600156
157
158
Obaid Amin806df812013-02-21 13:30:26 -0600159 void
akmhoquea0e71152013-02-11 09:47:59 -0600160get_content_by_content_name(char *content_name, unsigned char **content_data)
161{
162
Obaid Amin806df812013-02-21 13:30:26 -0600163 struct ccn_charbuf *name = NULL;
164 struct ccn_charbuf *templ = NULL;
165 struct ccn_charbuf *resultbuf = NULL;
166 struct ccn_parsed_ContentObject pcobuf = { 0 };
167 int res;
168 int allow_stale = 0;
169 int content_only = 1;
170 int scope = -1;
171 const unsigned char *ptr;
172 size_t length;
173 int resolve_version = CCN_V_HIGHEST;
174 int timeout_ms = 3000;
175 const unsigned lifetime_default = CCN_INTEREST_LIFETIME_SEC << 12;
176 unsigned lifetime_l12 = lifetime_default;
177 int get_flags = 0;
akmhoquea0e71152013-02-11 09:47:59 -0600178
Obaid Amin806df812013-02-21 13:30:26 -0600179 name = ccn_charbuf_create();
180 res = ccn_name_from_uri(name,content_name);
181 if (res < 0) {
182 fprintf(stderr, "Bad ccn URI: %s\n", content_name);
183 exit(1);
184 }
akmhoquea0e71152013-02-11 09:47:59 -0600185
Obaid Amin806df812013-02-21 13:30:26 -0600186 if (allow_stale || lifetime_l12 != lifetime_default || scope != -1) {
187 templ = ccn_charbuf_create();
188 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
189 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
190 ccn_charbuf_append_closer(templ); /* </Name> */
191 if (allow_stale) {
192 ccn_charbuf_append_tt(templ, CCN_DTAG_AnswerOriginKind, CCN_DTAG);
193 ccnb_append_number(templ,
194 CCN_AOK_DEFAULT | CCN_AOK_STALE);
195 ccn_charbuf_append_closer(templ); /* </AnswerOriginKind> */
196 }
197 if (scope != -1) {
198 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
199 }
200 if (lifetime_l12 != lifetime_default) {
201 /*
202 * Choose the interest lifetime so there are at least 3
203 * expressions (in the unsatisfied case).
204 */
205 unsigned char buf[3] = { 0 };
206 int i;
207 for (i = sizeof(buf) - 1; i >= 0; i--, lifetime_l12 >>= 8)
208 buf[i] = lifetime_l12 & 0xff;
209 ccnb_append_tagged_blob(templ, CCN_DTAG_InterestLifetime, buf,
210 sizeof(buf));
211 }
212 ccn_charbuf_append_closer(templ); /* </Interest> */
akmhoquea0e71152013-02-11 09:47:59 -0600213 }
Obaid Amin806df812013-02-21 13:30:26 -0600214 resultbuf = ccn_charbuf_create();
215 if (resolve_version != 0) {
216 res = ccn_resolve_version(nlsr->ccn, name, resolve_version, 500);
217 if (res >= 0) {
218 ccn_uri_append(resultbuf, name->buf, name->length, 1);
219 resultbuf->length = 0;
220 }
akmhoquea0e71152013-02-11 09:47:59 -0600221 }
Obaid Amin806df812013-02-21 13:30:26 -0600222 res = ccn_get(nlsr->ccn, name, templ, timeout_ms, resultbuf, &pcobuf, NULL,
223 get_flags);
akmhoquea0e71152013-02-11 09:47:59 -0600224 if (res >= 0) {
Obaid Amin806df812013-02-21 13:30:26 -0600225 ptr = resultbuf->buf;
226 length = resultbuf->length;
227 if (content_only){
228 ccn_content_get_value(ptr, length, &pcobuf, &ptr, &length);
akmhoque2f1d11e2013-02-24 12:06:52 -0600229 *content_data = (unsigned char *) calloc(length, sizeof(char *));
Obaid Amin806df812013-02-21 13:30:26 -0600230 memcpy (*content_data, ptr, length);
231 }
akmhoquea0e71152013-02-11 09:47:59 -0600232 }
Obaid Amin806df812013-02-21 13:30:26 -0600233 ccn_charbuf_destroy(&resultbuf);
234 ccn_charbuf_destroy(&templ);
235 ccn_charbuf_destroy(&name);
akmhoquea0e71152013-02-11 09:47:59 -0600236}
237
akmhoqueedb68d92013-03-05 10:18:16 -0600238void
akmhoquea0e71152013-02-11 09:47:59 -0600239process_incoming_sync_content_lsa( unsigned char *content_data)
240{
241
242
Obaid Amin806df812013-02-21 13:30:26 -0600243 if ( nlsr->debugging )
244 printf("process_incoming_sync_content_lsa called \n");
Obaid Aminbb8ac422013-02-20 17:08:48 -0600245
Obaid Amin806df812013-02-21 13:30:26 -0600246 char *sep="|";
247 char *rem;
248 char *orig_router;
249 char *orl;
250 int orig_router_length;
251 char *lst;
252 int ls_type;
253 char *lsid;
254 long int ls_id;
255 char *isvld;
256 int isValid;
257 char *num_link;
258 int no_link;
259 char *np;
260 char *np_length;
261 int name_length;
262 char *data;
263 char *orig_time;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600264
265
akmhoquea0e71152013-02-11 09:47:59 -0600266 if ( nlsr->debugging )
Obaid Amin806df812013-02-21 13:30:26 -0600267 printf("LSA Data \n");
268
269 if( strlen((char *)content_data ) > 0 )
akmhoquea0e71152013-02-11 09:47:59 -0600270 {
Obaid Aminbb8ac422013-02-20 17:08:48 -0600271
Obaid Amin806df812013-02-21 13:30:26 -0600272 orig_router=strtok_r((char *)content_data,sep,&rem);
273 orl=strtok_r(NULL,sep,&rem);
274 orig_router_length=atoi(orl);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600275
Obaid Amin806df812013-02-21 13:30:26 -0600276 if ( nlsr->debugging )
277 {
278 printf(" Orig Router Name : %s\n",orig_router);
279 printf(" Orig Router Length: %d\n",orig_router_length);
280 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600281
Obaid Amin806df812013-02-21 13:30:26 -0600282 lst=strtok_r(NULL,sep,&rem);
283 ls_type=atoi(lst);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600284
Obaid Amin806df812013-02-21 13:30:26 -0600285 if ( nlsr->debugging )
286 printf(" LS Type : %d\n",ls_type);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600287
Obaid Amin806df812013-02-21 13:30:26 -0600288 if ( ls_type == LS_TYPE_NAME )
289 {
290 lsid=strtok_r(NULL,sep,&rem);
291 ls_id=atoi(lsid);
292 orig_time=strtok_r(NULL,sep,&rem);
293 isvld=strtok_r(NULL,sep,&rem);
294 isValid=atoi(isvld);
295 np=strtok_r(NULL,sep,&rem);
296 np_length=strtok_r(NULL,sep,&rem);
297 name_length=atoi(np_length);
298 if ( nlsr->debugging )
299 {
300 printf(" LS ID : %ld\n",ls_id);
301 printf(" isValid : %d\n",isValid);
302 printf(" Name Prefix : %s\n",np);
303 printf(" Orig Time : %s\n",orig_time);
304 printf(" Name Prefix length: %d\n",name_length);
305 }
306
307 build_and_install_others_name_lsa(orig_router,ls_type,ls_id,orig_time,isValid,np);
308
309 print_name_lsdb();
310
311 }
312 else if ( ls_type == LS_TYPE_ADJ )
313 {
314 orig_time=strtok_r(NULL,sep,&rem);
315 num_link=strtok_r(NULL,sep,&rem);
316 no_link=atoi(num_link);
317 data=rem;
318
319 if ( nlsr->debugging )
320 {
321 printf(" Orig Time : %s\n",orig_time);
322 printf(" No Link : %d\n",no_link);
323 printf(" Data : %s\n",data);
324 }
325 build_and_install_others_adj_lsa(orig_router,ls_type,orig_time,no_link,data);
326 }
327 else if ( ls_type == LS_TYPE_COR )
328 {
329 orig_time=strtok_r(NULL,sep,&rem);
330 char *cor_r=strtok_r(NULL,sep,&rem);
331 char *cor_theta=strtok_r(NULL,sep,&rem);
332
333 double r, theta;
akmhoque8034d502013-02-24 11:53:24 -0600334 r=strtod(cor_r,NULL);
335 theta=strtod(cor_theta,NULL);
Obaid Amin806df812013-02-21 13:30:26 -0600336
337 if ( nlsr->debugging )
338 {
339 printf(" Orig Time : %s\n",orig_time);
340 printf(" Cor R : %f\n",r);
341 printf(" Cor Theta : %f\n",theta);
342 }
343 build_and_install_others_cor_lsa(orig_router,ls_type,orig_time, (double)r, (double)theta);
344 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600345
346 }
akmhoquea0e71152013-02-11 09:47:59 -0600347}
348
Obaid Amin806df812013-02-21 13:30:26 -0600349 void
Obaid Amin7997ad82013-02-22 16:41:56 -0600350process_content_from_sync (struct ccn_charbuf *content_name,
351 struct ccn_indexbuf *components)
akmhoquea0e71152013-02-11 09:47:59 -0600352{
akmhoque8c5e2662013-02-24 06:37:14 -0600353 if (nlsr->debugging)
354 printf("process_content_from_sync called \n");
Obaid Amin806df812013-02-21 13:30:26 -0600355 size_t comp_size;
356 char *lst;
357 char *lsid;
358 const unsigned char *second_last_comp;
359 const unsigned char *third_last_comp;
360 const unsigned char *origtime;
361 char *sep=".";
362 char *rem;
363 char *second_comp_type;
akmhoquea0e71152013-02-11 09:47:59 -0600364
Obaid Amin806df812013-02-21 13:30:26 -0600365 int ls_type;
366 long int ls_id=0;
akmhoquea0e71152013-02-11 09:47:59 -0600367
Obaid Amin806df812013-02-21 13:30:26 -0600368 unsigned char *content_data = NULL;
akmhoquea0e71152013-02-11 09:47:59 -0600369
akmhoqueb31caa12013-02-22 08:42:09 -0600370 char *time_stamp=get_current_timestamp_micro_v2();
akmhoquea0e71152013-02-11 09:47:59 -0600371
Obaid Amin806df812013-02-21 13:30:26 -0600372 struct ccn_charbuf *uri = ccn_charbuf_create();
373 ccn_uri_append(uri, content_name->buf, content_name->length, 0);
akmhoquea0e71152013-02-11 09:47:59 -0600374
Obaid Amin7997ad82013-02-22 16:41:56 -0600375 struct name_prefix *orig_router=(struct name_prefix *)
376 calloc( 1, sizeof(struct name_prefix));
akmhoquea0e71152013-02-11 09:47:59 -0600377
Obaid Amin7997ad82013-02-22 16:41:56 -0600378 ccn_name_comp_get( content_name->buf, components,
379 components->n-1-2, &second_last_comp, &comp_size);
380
Obaid Amin806df812013-02-21 13:30:26 -0600381 if (nlsr->debugging)
Obaid Amin7997ad82013-02-22 16:41:56 -0600382 printf("2nd Last Component: %s \n", second_last_comp);
akmhoquea0e71152013-02-11 09:47:59 -0600383
Obaid Amin7997ad82013-02-22 16:41:56 -0600384 second_comp_type=strtok_r((char *)second_last_comp, sep, &rem);
385 if (second_comp_type == NULL || rem == NULL)
386 {
387 printf ("Error: unable to tokenize the string: %s, calling exit()\n",
388 (char *)second_last_comp);
389 exit(0);
390 }
391
Obaid Amin806df812013-02-21 13:30:26 -0600392 if ( strcmp( second_comp_type, "lsId" ) == 0 )
393 {
394 lsid=rem;
395 ls_id=atoi(rem);
396 ccn_name_comp_get(content_name->buf, components,components->n-2-2,&third_last_comp, &comp_size);
397 lst=strtok_r((char *)third_last_comp,sep,&rem);
398 lst=rem;
399 ls_type=atoi(lst);
400 ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size);
401 ccn_name_chop(content_name,components,-3);
402 get_name_part(orig_router,content_name,components,0);
akmhoquea0e71152013-02-11 09:47:59 -0600403
Obaid Aminbb8ac422013-02-20 17:08:48 -0600404 if ( nlsr->debugging )
Obaid Amin806df812013-02-21 13:30:26 -0600405 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 -0600406
Obaid Amin806df812013-02-21 13:30:26 -0600407 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque8c5e2662013-02-24 06:37:14 -0600408 if ( nlsr->debugging )
409 printf("LSA Life time: %d\n",lsa_life_time);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600410
Obaid Amin806df812013-02-21 13:30:26 -0600411 if ( (strcmp(orig_router->name,nlsr->router_name) == 0 && lsa_life_time < nlsr->lsa_refresh_time)
412 || (strcmp(orig_router->name,nlsr->router_name) != 0 && lsa_life_time < nlsr->router_dead_interval) )
Obaid Aminbb8ac422013-02-20 17:08:48 -0600413 {
Obaid Amin806df812013-02-21 13:30:26 -0600414 int is_new_name_lsa=check_is_new_name_lsa(orig_router->name,(char *)lst,(char *)lsid,(char *)origtime);
415 if ( is_new_name_lsa == 1 )
416 {
417 if ( nlsr->debugging )
418 printf("New NAME LSA.....\n");
419 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
420 if ( nlsr->debugging )
421 printf("Content Data: %s \n",content_data);
422 process_incoming_sync_content_lsa(content_data);
423 }
424 else
425 {
426 if ( nlsr->debugging )
427 printf("Name LSA / Newer Name LSA already xists in LSDB\n");
428 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600429
Obaid Amin806df812013-02-21 13:30:26 -0600430 if ( nlsr->debugging )
431 printf("Content Data: %s \n",content_data);
432 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600433 }
Obaid Amin806df812013-02-21 13:30:26 -0600434 else
Obaid Aminbb8ac422013-02-20 17:08:48 -0600435 {
Obaid Amin806df812013-02-21 13:30:26 -0600436 if ( nlsr->debugging )
437 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
Obaid Aminbb8ac422013-02-20 17:08:48 -0600438 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600439 }
Obaid Amin806df812013-02-21 13:30:26 -0600440 else
Obaid Aminbb8ac422013-02-20 17:08:48 -0600441 {
Obaid Amin806df812013-02-21 13:30:26 -0600442 ls_type=atoi(rem);
443 lst=rem;
444 if(ls_type == LS_TYPE_ADJ)
Obaid Aminbb8ac422013-02-20 17:08:48 -0600445 {
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600446 ccn_name_comp_get(content_name->buf, components,components->n-2,
447 &origtime, &comp_size);
Obaid Amin806df812013-02-21 13:30:26 -0600448 ccn_name_chop(content_name,components,-2);
449 get_name_part(orig_router,content_name,components,0);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600450
Obaid Amin806df812013-02-21 13:30:26 -0600451 if ( nlsr->debugging )
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600452 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",
453 orig_router->name,ls_type,origtime);
Obaid Amin806df812013-02-21 13:30:26 -0600454
455 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque8c5e2662013-02-24 06:37:14 -0600456 if ( nlsr->debugging )
457 printf("LSA Life time: %d\n",lsa_life_time);
458
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600459 if ( (strcmp(orig_router->name,nlsr->router_name) == 0 &&
460 lsa_life_time < nlsr->lsa_refresh_time) ||
461 (strcmp(orig_router->name,nlsr->router_name) != 0 &&
462 lsa_life_time < nlsr->router_dead_interval) )
Obaid Amin806df812013-02-21 13:30:26 -0600463 {
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600464 int is_new_adj_lsa = check_is_new_adj_lsa( orig_router->name,
465 (char *)lst, (char *)origtime);
Obaid Amin806df812013-02-21 13:30:26 -0600466 if ( is_new_adj_lsa == 1 )
467 {
468 if ( nlsr->debugging )
469 printf("New Adj LSA.....\n");
470 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
471
472 if ( nlsr->debugging )
473 printf("Content Data: %s \n",content_data);
474 process_incoming_sync_content_lsa(content_data);
475 }
476 else
477 {
478 if ( nlsr->debugging )
479 printf("Adj LSA / Newer Adj LSA already exists in LSDB\n");
480 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
481 if ( nlsr->debugging )
482 printf("Content Data: %s \n",content_data);
483 }
484 }
485 else
486 {
487 if ( nlsr->debugging )
488 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
489 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600490 }
Obaid Amin806df812013-02-21 13:30:26 -0600491 else if(ls_type == LS_TYPE_COR)
Obaid Aminbb8ac422013-02-20 17:08:48 -0600492 {
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600493 ccn_name_comp_get(content_name->buf, components, components->n-2,
494 &origtime, &comp_size);
Obaid Amin806df812013-02-21 13:30:26 -0600495 ccn_name_chop(content_name,components,-2);
496 get_name_part(orig_router,content_name,components,0);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600497
Obaid Amin806df812013-02-21 13:30:26 -0600498 if ( nlsr->debugging )
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600499 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",
500 orig_router->name,ls_type,origtime);
Obaid Amin806df812013-02-21 13:30:26 -0600501
502 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque8c5e2662013-02-24 06:37:14 -0600503 if ( nlsr->debugging )
504 printf("LSA Life time: %d\n",lsa_life_time);
505
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600506 if ( (strcmp(orig_router->name,nlsr->router_name) == 0 &&
507 lsa_life_time < nlsr->lsa_refresh_time) ||
508 (strcmp(orig_router->name,nlsr->router_name) != 0 &&
509 lsa_life_time < nlsr->router_dead_interval) )
Obaid Amin806df812013-02-21 13:30:26 -0600510 {
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600511 int is_new_cor_lsa=check_is_new_cor_lsa( orig_router->name,
512 (char *)lst,(char *) origtime);
Obaid Amin806df812013-02-21 13:30:26 -0600513 if ( is_new_cor_lsa == 1 )
514 {
515 if ( nlsr->debugging )
516 printf("New Cor LSA.....\n");
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600517 get_content_by_content_name(ccn_charbuf_as_string(uri),
518 &content_data);
Obaid Amin806df812013-02-21 13:30:26 -0600519
520 if ( nlsr->debugging )
521 printf("Content Data: %s \n",content_data);
522 process_incoming_sync_content_lsa(content_data);
523 }
524 else
525 {
526 if ( nlsr->debugging )
527 printf("Cor LSA / Newer Cor LSA already exists in LSDB\n");
528 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
529 if ( nlsr->debugging )
530 printf("Content Data: %s \n",content_data);
531 }
532 }
533 else
534 {
535 if ( nlsr->debugging )
536 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
537 }
538
539 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600540 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600541
akmhoquef6773612013-02-23 09:53:00 -0600542 if (orig_router->name)
543 free(orig_router->name);
Obaid Amin05300892013-02-21 13:45:25 -0600544 if (orig_router)
545 free(orig_router);
Obaid Amin806df812013-02-21 13:30:26 -0600546 ccn_charbuf_destroy(&uri);
547 //01/31/2013
548 free(time_stamp);
akmhoquea0e71152013-02-11 09:47:59 -0600549}
550
Obaid Amin7793ba72013-02-22 00:43:58 -0600551 int
akmhoquea0e71152013-02-11 09:47:59 -0600552sync_monitor(char *topo_prefix, char *slice_prefix)
553{
554
Obaid Amin806df812013-02-21 13:30:26 -0600555 struct ccn_charbuf *prefix = ccn_charbuf_create();
Obaid Amin806df812013-02-21 13:30:26 -0600556 struct ccn_charbuf *topo = ccn_charbuf_create();
Obaid Amin7997ad82013-02-22 16:41:56 -0600557
558 nlsr->closure=(struct ccns_name_closure *)
akmhoquececba942013-02-25 17:33:34 -0600559 calloc(1,sizeof(struct ccns_name_closure)); // leak
Obaid Amin7997ad82013-02-22 16:41:56 -0600560
Obaid Amin806df812013-02-21 13:30:26 -0600561 nlsr->slice = ccns_slice_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600562
Obaid Amin806df812013-02-21 13:30:26 -0600563 ccn_charbuf_reset(prefix);
564 ccn_name_from_uri(prefix, slice_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600565
Obaid Amin806df812013-02-21 13:30:26 -0600566 ccn_charbuf_reset(topo);
567 ccn_name_from_uri(topo, topo_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600568
Obaid Amin806df812013-02-21 13:30:26 -0600569 ccns_slice_set_topo_prefix(nlsr->slice, topo, prefix);
570 nlsr->closure->callback = &sync_cb;
akmhoque54d86112013-02-21 16:42:34 -0600571 nlsr->ccns = ccns_open(nlsr->ccn, nlsr->slice, nlsr->closure, NULL, NULL);
akmhoquea0e71152013-02-11 09:47:59 -0600572
Obaid Amin7997ad82013-02-22 16:41:56 -0600573 ccn_charbuf_destroy(&prefix);
574 ccn_charbuf_destroy(&topo);
Obaid Amin806df812013-02-21 13:30:26 -0600575 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600576}
577
Obaid Amin7793ba72013-02-22 00:43:58 -0600578 struct ccn_charbuf *
akmhoquea0e71152013-02-11 09:47:59 -0600579make_template(int scope)
580{
Obaid Amin806df812013-02-21 13:30:26 -0600581 struct ccn_charbuf *templ = NULL;
582 templ = ccn_charbuf_create();
583 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
584 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
585 ccn_charbuf_append_closer(templ); /* </Name> */
586 if (0 <= scope && scope <= 2)
587 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
588 ccn_charbuf_append_closer(templ); /* </Interest> */
589 return(templ);
akmhoquea0e71152013-02-11 09:47:59 -0600590}
591
Obaid Amin7793ba72013-02-22 00:43:58 -0600592 int
akmhoquea0e71152013-02-11 09:47:59 -0600593write_data_to_repo(char *data, char *name_prefix)
594{
Obaid Amin806df812013-02-21 13:30:26 -0600595 if ( nlsr->debugging )
596 {
597 printf("write_data_to_repo called\n");
598 printf("Content Name: %s \n",name_prefix);
599 printf("Content Data: %s \n",data);
600 }
akmhoquea0e71152013-02-11 09:47:59 -0600601
Obaid Aminf210a322013-02-22 02:04:52 -0600602 struct ccn *temp_ccn;
Obaid Amin806df812013-02-21 13:30:26 -0600603 temp_ccn=ccn_create();
604 int ccn_fd=ccn_connect(temp_ccn, NULL);
605 if(ccn_fd == -1)
606 {
607 fprintf(stderr,"Could not connect to ccnd for Data Writing\n");
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600608 writeLogg(__FILE__,__FUNCTION__,__LINE__,
609 "Could not connect to ccnd for Data Writing\n");
Obaid Amin806df812013-02-21 13:30:26 -0600610 return -1;
611 }
Obaid Aminf210a322013-02-22 02:04:52 -0600612
Obaid Amin806df812013-02-21 13:30:26 -0600613 struct ccn_charbuf *name = NULL;
614 struct ccn_seqwriter *w = NULL;
615 int blocksize = 4096;
616 int freshness = -1;
Obaid Amin806df812013-02-21 13:30:26 -0600617 int scope = 1;
618 int res;
619 size_t blockread;
620 struct ccn_charbuf *templ;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600621
Obaid Amin806df812013-02-21 13:30:26 -0600622 name = ccn_charbuf_create();
623 res = ccn_name_from_uri(name, name_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600624 if (res < 0) {
Obaid Amin806df812013-02-21 13:30:26 -0600625 fprintf(stderr, "bad CCN URI: %s\n",name_prefix);
626 return -1;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600627 }
Obaid Amin806df812013-02-21 13:30:26 -0600628
Obaid Aminf210a322013-02-22 02:04:52 -0600629 w = ccn_seqw_create(temp_ccn, name);
Obaid Amin806df812013-02-21 13:30:26 -0600630 if (w == NULL) {
631 fprintf(stderr, "ccn_seqw_create failed\n");
632 return -1;
633 }
634 ccn_seqw_set_block_limits(w, blocksize, blocksize);
635 if (freshness > -1)
636 ccn_seqw_set_freshness(w, freshness);
akmhoquea0e71152013-02-11 09:47:59 -0600637
Obaid Amin7997ad82013-02-22 16:41:56 -0600638 struct ccn_charbuf *name_v = ccn_charbuf_create();
639 ccn_seqw_get_name(w, name_v);
640 ccn_name_from_uri(name_v, "%C1.R.sw");
641 ccn_name_append_nonce(name_v);
642 templ = make_template(scope);
643 res = ccn_get(temp_ccn, name_v, templ, 60000, NULL, NULL, NULL, 0);
644 ccn_charbuf_destroy(&templ);
645 ccn_charbuf_destroy(&name_v);
646 if (res < 0) {
647 fprintf(stderr, "No response from repository\n");
648 return -1;
649 }
akmhoquea0e71152013-02-11 09:47:59 -0600650
Obaid Amin806df812013-02-21 13:30:26 -0600651 blockread = 0;
akmhoquea0e71152013-02-11 09:47:59 -0600652
akmhoquee407fc22013-02-24 11:57:17 -0600653 blockread=strlen(data)+1;
akmhoquea0e71152013-02-11 09:47:59 -0600654
Obaid Amin806df812013-02-21 13:30:26 -0600655 if (blockread > 0) {
Obaid Amin806df812013-02-21 13:30:26 -0600656 res = ccn_seqw_write(w, data, blockread);
657 while (res == -1) {
akmhoque8034d502013-02-24 11:53:24 -0600658 ccn_run(temp_ccn,100);
Obaid Amin806df812013-02-21 13:30:26 -0600659 res = ccn_seqw_write(w, data, blockread);
660 }
661 }
662
akmhoque8034d502013-02-24 11:53:24 -0600663 ccn_seqw_close(w);
664 ccn_run(temp_ccn, 100);
Obaid Amin806df812013-02-21 13:30:26 -0600665 ccn_charbuf_destroy(&name);
akmhoque0ed6d982013-02-22 14:28:01 -0600666 ccn_destroy(&temp_ccn);
akmhoquea0e71152013-02-11 09:47:59 -0600667
Obaid Amin806df812013-02-21 13:30:26 -0600668 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600669}
Obaid Amin806df812013-02-21 13:30:26 -0600670 int
Obaid Amin7997ad82013-02-22 16:41:56 -0600671 create_sync_slice(char *topo_prefix, char *slice_prefix)
akmhoquea0e71152013-02-11 09:47:59 -0600672{
Obaid Amin806df812013-02-21 13:30:26 -0600673 int res;
akmhoque0ed6d982013-02-22 14:28:01 -0600674 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 -0600675 struct ccns_slice *slice;
676 struct ccn_charbuf *prefix = ccn_charbuf_create();
677 struct ccn_charbuf *topo = ccn_charbuf_create();
678 struct ccn_charbuf *clause = ccn_charbuf_create();
679 struct ccn_charbuf *slice_name = ccn_charbuf_create();
680 struct ccn_charbuf *slice_uri = ccn_charbuf_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600681
Obaid Amin806df812013-02-21 13:30:26 -0600682 if (prefix == NULL || topo == NULL || clause == NULL ||
683 slice_name == NULL || slice_uri == NULL) {
684 fprintf(stderr, "Unable to allocate required memory.\n");
685 return -1;
686 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600687
akmhoque0ed6d982013-02-22 14:28:01 -0600688 handle = ccn_create();
Obaid Amin806df812013-02-21 13:30:26 -0600689 res = ccn_connect(handle, NULL);
690 if (0 > res) {
691 fprintf(stderr, "Unable to connect to ccnd.\n");
692 return -1;
693 }
akmhoque0ed6d982013-02-22 14:28:01 -0600694
Obaid Amin806df812013-02-21 13:30:26 -0600695 slice = ccns_slice_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600696
Obaid Amin806df812013-02-21 13:30:26 -0600697 ccn_charbuf_reset(topo);
698 ccn_name_from_uri(topo, topo_prefix);
699 ccn_charbuf_reset(prefix);
700 ccn_name_from_uri(prefix,slice_prefix );
701 ccns_slice_set_topo_prefix(slice, topo, prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600702
703
akmhoque0ed6d982013-02-22 14:28:01 -0600704 res = ccns_write_slice(handle, slice, slice_name);
705 //res = ccns_write_slice(nlsr->ccn, slice, slice_name);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600706
Obaid Amin806df812013-02-21 13:30:26 -0600707 //01/31/2013
708 ccns_slice_destroy(&slice);
akmhoque0ed6d982013-02-22 14:28:01 -0600709 ccn_destroy(&handle);
Obaid Amin806df812013-02-21 13:30:26 -0600710 ccn_charbuf_destroy(&prefix);
711 ccn_charbuf_destroy(&topo);
712 ccn_charbuf_destroy(&clause);
713 ccn_charbuf_destroy(&slice_name);
714 ccn_charbuf_destroy(&slice_uri);
akmhoquea0e71152013-02-11 09:47:59 -0600715
Obaid Amin806df812013-02-21 13:30:26 -0600716 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600717}
718
akmhoque0eb8de22013-02-24 14:08:19 -0600719