blob: 73449affbad328e39bf897bdb0b98a9a23a7a0db [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{
Obaid Amin806df812013-02-21 13:30:26 -0600123 int i;
124 int lsa_position=0;
125 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;
akmhoquea0e71152013-02-11 09:47:59 -0600133
Obaid Amin806df812013-02-21 13:30:26 -0600134 ccn_charbuf_destroy(&name);
akmhoquea0e71152013-02-11 09:47:59 -0600135
Obaid Amin806df812013-02-21 13:30:26 -0600136 const unsigned char *comp_ptr1;
137 size_t comp_size;
138 for(i=lsa_position+1+offset;i<interest_comps->n-1;i++)
139 {
140 ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1,
141 &comp_size);
142 len+=1;
143 len+=(int)comp_size;
144 }
145 len++;
akmhoquea0e71152013-02-11 09:47:59 -0600146
Obaid Amin806df812013-02-21 13:30:26 -0600147 char *neighbor=(char *)malloc(len);
148 memset(neighbor,0,len);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600149
Obaid Amin806df812013-02-21 13:30:26 -0600150 for(i=lsa_position+1+offset; i<interest_comps->n-1;i++)
151 {
152 ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1,
153 &comp_size);
154 memcpy(neighbor+strlen(neighbor),"/",1);
155 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,
156 strlen((char *)comp_ptr1));
Obaid Aminbb8ac422013-02-20 17:08:48 -0600157
Obaid Amin806df812013-02-21 13:30:26 -0600158 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600159
Obaid Amin806df812013-02-21 13:30:26 -0600160 name_part->name=(char *)malloc(strlen(neighbor)+1);
161 memset(name_part->name,0,strlen(neighbor)+1);
162 memcpy(name_part->name,neighbor,strlen(neighbor)+1);
163 name_part->length=strlen(neighbor)+1;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600164
Obaid Amin806df812013-02-21 13:30:26 -0600165 // Add 01/31/2013
166 free(neighbor);
akmhoquea0e71152013-02-11 09:47:59 -0600167}
168
akmhoquea0e71152013-02-11 09:47:59 -0600169
akmhoquea0e71152013-02-11 09:47:59 -0600170
171
172
Obaid Amin806df812013-02-21 13:30:26 -0600173 void
akmhoquea0e71152013-02-11 09:47:59 -0600174get_content_by_content_name(char *content_name, unsigned char **content_data)
175{
176
Obaid Amin806df812013-02-21 13:30:26 -0600177 struct ccn_charbuf *name = NULL;
178 struct ccn_charbuf *templ = NULL;
179 struct ccn_charbuf *resultbuf = NULL;
180 struct ccn_parsed_ContentObject pcobuf = { 0 };
181 int res;
182 int allow_stale = 0;
183 int content_only = 1;
184 int scope = -1;
185 const unsigned char *ptr;
186 size_t length;
187 int resolve_version = CCN_V_HIGHEST;
188 int timeout_ms = 3000;
189 const unsigned lifetime_default = CCN_INTEREST_LIFETIME_SEC << 12;
190 unsigned lifetime_l12 = lifetime_default;
191 int get_flags = 0;
akmhoquea0e71152013-02-11 09:47:59 -0600192
Obaid Amin806df812013-02-21 13:30:26 -0600193 name = ccn_charbuf_create();
194 res = ccn_name_from_uri(name,content_name);
195 if (res < 0) {
196 fprintf(stderr, "Bad ccn URI: %s\n", content_name);
197 exit(1);
198 }
akmhoquea0e71152013-02-11 09:47:59 -0600199
Obaid Amin806df812013-02-21 13:30:26 -0600200 if (allow_stale || lifetime_l12 != lifetime_default || scope != -1) {
201 templ = ccn_charbuf_create();
202 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
203 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
204 ccn_charbuf_append_closer(templ); /* </Name> */
205 if (allow_stale) {
206 ccn_charbuf_append_tt(templ, CCN_DTAG_AnswerOriginKind, CCN_DTAG);
207 ccnb_append_number(templ,
208 CCN_AOK_DEFAULT | CCN_AOK_STALE);
209 ccn_charbuf_append_closer(templ); /* </AnswerOriginKind> */
210 }
211 if (scope != -1) {
212 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
213 }
214 if (lifetime_l12 != lifetime_default) {
215 /*
216 * Choose the interest lifetime so there are at least 3
217 * expressions (in the unsatisfied case).
218 */
219 unsigned char buf[3] = { 0 };
220 int i;
221 for (i = sizeof(buf) - 1; i >= 0; i--, lifetime_l12 >>= 8)
222 buf[i] = lifetime_l12 & 0xff;
223 ccnb_append_tagged_blob(templ, CCN_DTAG_InterestLifetime, buf,
224 sizeof(buf));
225 }
226 ccn_charbuf_append_closer(templ); /* </Interest> */
akmhoquea0e71152013-02-11 09:47:59 -0600227 }
Obaid Amin806df812013-02-21 13:30:26 -0600228 resultbuf = ccn_charbuf_create();
229 if (resolve_version != 0) {
230 res = ccn_resolve_version(nlsr->ccn, name, resolve_version, 500);
231 if (res >= 0) {
232 ccn_uri_append(resultbuf, name->buf, name->length, 1);
233 resultbuf->length = 0;
234 }
akmhoquea0e71152013-02-11 09:47:59 -0600235 }
Obaid Amin806df812013-02-21 13:30:26 -0600236 res = ccn_get(nlsr->ccn, name, templ, timeout_ms, resultbuf, &pcobuf, NULL,
237 get_flags);
akmhoquea0e71152013-02-11 09:47:59 -0600238 if (res >= 0) {
Obaid Amin806df812013-02-21 13:30:26 -0600239 ptr = resultbuf->buf;
240 length = resultbuf->length;
241 if (content_only){
242 ccn_content_get_value(ptr, length, &pcobuf, &ptr, &length);
243 *content_data = (unsigned char *) calloc(length, sizeof(char *));
244 memcpy (*content_data, ptr, length);
245 }
akmhoquea0e71152013-02-11 09:47:59 -0600246 }
Obaid Amin806df812013-02-21 13:30:26 -0600247 ccn_charbuf_destroy(&resultbuf);
248 ccn_charbuf_destroy(&templ);
249 ccn_charbuf_destroy(&name);
akmhoquea0e71152013-02-11 09:47:59 -0600250}
251
Obaid Amin806df812013-02-21 13:30:26 -0600252 void
akmhoquea0e71152013-02-11 09:47:59 -0600253process_incoming_sync_content_lsa( unsigned char *content_data)
254{
255
256
Obaid Amin806df812013-02-21 13:30:26 -0600257 if ( nlsr->debugging )
258 printf("process_incoming_sync_content_lsa called \n");
Obaid Aminbb8ac422013-02-20 17:08:48 -0600259
Obaid Amin806df812013-02-21 13:30:26 -0600260 char *sep="|";
261 char *rem;
262 char *orig_router;
263 char *orl;
264 int orig_router_length;
265 char *lst;
266 int ls_type;
267 char *lsid;
268 long int ls_id;
269 char *isvld;
270 int isValid;
271 char *num_link;
272 int no_link;
273 char *np;
274 char *np_length;
275 int name_length;
276 char *data;
277 char *orig_time;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600278
279
akmhoquea0e71152013-02-11 09:47:59 -0600280 if ( nlsr->debugging )
Obaid Amin806df812013-02-21 13:30:26 -0600281 printf("LSA Data \n");
282
283 if( strlen((char *)content_data ) > 0 )
akmhoquea0e71152013-02-11 09:47:59 -0600284 {
Obaid Aminbb8ac422013-02-20 17:08:48 -0600285
Obaid Amin806df812013-02-21 13:30:26 -0600286 orig_router=strtok_r((char *)content_data,sep,&rem);
287 orl=strtok_r(NULL,sep,&rem);
288 orig_router_length=atoi(orl);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600289
Obaid Amin806df812013-02-21 13:30:26 -0600290 if ( nlsr->debugging )
291 {
292 printf(" Orig Router Name : %s\n",orig_router);
293 printf(" Orig Router Length: %d\n",orig_router_length);
294 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600295
Obaid Amin806df812013-02-21 13:30:26 -0600296 lst=strtok_r(NULL,sep,&rem);
297 ls_type=atoi(lst);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600298
Obaid Amin806df812013-02-21 13:30:26 -0600299 if ( nlsr->debugging )
300 printf(" LS Type : %d\n",ls_type);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600301
Obaid Amin806df812013-02-21 13:30:26 -0600302 if ( ls_type == LS_TYPE_NAME )
303 {
304 lsid=strtok_r(NULL,sep,&rem);
305 ls_id=atoi(lsid);
306 orig_time=strtok_r(NULL,sep,&rem);
307 isvld=strtok_r(NULL,sep,&rem);
308 isValid=atoi(isvld);
309 np=strtok_r(NULL,sep,&rem);
310 np_length=strtok_r(NULL,sep,&rem);
311 name_length=atoi(np_length);
312 if ( nlsr->debugging )
313 {
314 printf(" LS ID : %ld\n",ls_id);
315 printf(" isValid : %d\n",isValid);
316 printf(" Name Prefix : %s\n",np);
317 printf(" Orig Time : %s\n",orig_time);
318 printf(" Name Prefix length: %d\n",name_length);
319 }
320
321 build_and_install_others_name_lsa(orig_router,ls_type,ls_id,orig_time,isValid,np);
322
323 print_name_lsdb();
324
325 }
326 else if ( ls_type == LS_TYPE_ADJ )
327 {
328 orig_time=strtok_r(NULL,sep,&rem);
329 num_link=strtok_r(NULL,sep,&rem);
330 no_link=atoi(num_link);
331 data=rem;
332
333 if ( nlsr->debugging )
334 {
335 printf(" Orig Time : %s\n",orig_time);
336 printf(" No Link : %d\n",no_link);
337 printf(" Data : %s\n",data);
338 }
339 build_and_install_others_adj_lsa(orig_router,ls_type,orig_time,no_link,data);
340 }
341 else if ( ls_type == LS_TYPE_COR )
342 {
343 orig_time=strtok_r(NULL,sep,&rem);
344 char *cor_r=strtok_r(NULL,sep,&rem);
345 char *cor_theta=strtok_r(NULL,sep,&rem);
346
347 double r, theta;
348 r=strtof(cor_r,NULL);
349 theta=strtof(cor_theta,NULL);
350
351 if ( nlsr->debugging )
352 {
353 printf(" Orig Time : %s\n",orig_time);
354 printf(" Cor R : %f\n",r);
355 printf(" Cor Theta : %f\n",theta);
356 }
357 build_and_install_others_cor_lsa(orig_router,ls_type,orig_time, (double)r, (double)theta);
358 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600359
360 }
akmhoquea0e71152013-02-11 09:47:59 -0600361}
362
Obaid Amin806df812013-02-21 13:30:26 -0600363 void
Obaid Amin7997ad82013-02-22 16:41:56 -0600364process_content_from_sync (struct ccn_charbuf *content_name,
365 struct ccn_indexbuf *components)
akmhoquea0e71152013-02-11 09:47:59 -0600366{
Obaid Amin806df812013-02-21 13:30:26 -0600367 size_t comp_size;
368 char *lst;
369 char *lsid;
370 const unsigned char *second_last_comp;
371 const unsigned char *third_last_comp;
372 const unsigned char *origtime;
373 char *sep=".";
374 char *rem;
375 char *second_comp_type;
akmhoquea0e71152013-02-11 09:47:59 -0600376
Obaid Amin806df812013-02-21 13:30:26 -0600377 int ls_type;
378 long int ls_id=0;
akmhoquea0e71152013-02-11 09:47:59 -0600379
Obaid Amin806df812013-02-21 13:30:26 -0600380 unsigned char *content_data = NULL;
akmhoquea0e71152013-02-11 09:47:59 -0600381
akmhoqueb31caa12013-02-22 08:42:09 -0600382 char *time_stamp=get_current_timestamp_micro_v2();
akmhoquea0e71152013-02-11 09:47:59 -0600383
Obaid Amin806df812013-02-21 13:30:26 -0600384 struct ccn_charbuf *uri = ccn_charbuf_create();
385 ccn_uri_append(uri, content_name->buf, content_name->length, 0);
akmhoquea0e71152013-02-11 09:47:59 -0600386
Obaid Amin7997ad82013-02-22 16:41:56 -0600387 struct name_prefix *orig_router=(struct name_prefix *)
388 calloc( 1, sizeof(struct name_prefix));
akmhoquea0e71152013-02-11 09:47:59 -0600389
Obaid Amin7997ad82013-02-22 16:41:56 -0600390 ccn_name_comp_get( content_name->buf, components,
391 components->n-1-2, &second_last_comp, &comp_size);
392
Obaid Amin806df812013-02-21 13:30:26 -0600393 if (nlsr->debugging)
Obaid Amin7997ad82013-02-22 16:41:56 -0600394 printf("2nd Last Component: %s \n", second_last_comp);
akmhoquea0e71152013-02-11 09:47:59 -0600395
Obaid Amin7997ad82013-02-22 16:41:56 -0600396 second_comp_type=strtok_r((char *)second_last_comp, sep, &rem);
397 if (second_comp_type == NULL || rem == NULL)
398 {
399 printf ("Error: unable to tokenize the string: %s, calling exit()\n",
400 (char *)second_last_comp);
401 exit(0);
402 }
403
Obaid Amin806df812013-02-21 13:30:26 -0600404 if ( strcmp( second_comp_type, "lsId" ) == 0 )
405 {
406 lsid=rem;
407 ls_id=atoi(rem);
408 ccn_name_comp_get(content_name->buf, components,components->n-2-2,&third_last_comp, &comp_size);
409 lst=strtok_r((char *)third_last_comp,sep,&rem);
410 lst=rem;
411 ls_type=atoi(lst);
412 ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size);
413 ccn_name_chop(content_name,components,-3);
414 get_name_part(orig_router,content_name,components,0);
akmhoquea0e71152013-02-11 09:47:59 -0600415
Obaid Aminbb8ac422013-02-20 17:08:48 -0600416 if ( nlsr->debugging )
Obaid Amin806df812013-02-21 13:30:26 -0600417 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 -0600418
Obaid Amin806df812013-02-21 13:30:26 -0600419 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600420
Obaid Amin806df812013-02-21 13:30:26 -0600421 if ( (strcmp(orig_router->name,nlsr->router_name) == 0 && lsa_life_time < nlsr->lsa_refresh_time)
422 || (strcmp(orig_router->name,nlsr->router_name) != 0 && lsa_life_time < nlsr->router_dead_interval) )
Obaid Aminbb8ac422013-02-20 17:08:48 -0600423 {
Obaid Amin806df812013-02-21 13:30:26 -0600424 int is_new_name_lsa=check_is_new_name_lsa(orig_router->name,(char *)lst,(char *)lsid,(char *)origtime);
425 if ( is_new_name_lsa == 1 )
426 {
427 if ( nlsr->debugging )
428 printf("New NAME LSA.....\n");
429 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
430 if ( nlsr->debugging )
431 printf("Content Data: %s \n",content_data);
432 process_incoming_sync_content_lsa(content_data);
433 }
434 else
435 {
436 if ( nlsr->debugging )
437 printf("Name LSA / Newer Name LSA already xists in LSDB\n");
438 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600439
Obaid Amin806df812013-02-21 13:30:26 -0600440 if ( nlsr->debugging )
441 printf("Content Data: %s \n",content_data);
442 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600443 }
Obaid Amin806df812013-02-21 13:30:26 -0600444 else
Obaid Aminbb8ac422013-02-20 17:08:48 -0600445 {
Obaid Amin806df812013-02-21 13:30:26 -0600446 if ( nlsr->debugging )
447 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
Obaid Aminbb8ac422013-02-20 17:08:48 -0600448 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600449 }
Obaid Amin806df812013-02-21 13:30:26 -0600450 else
Obaid Aminbb8ac422013-02-20 17:08:48 -0600451 {
Obaid Amin806df812013-02-21 13:30:26 -0600452 ls_type=atoi(rem);
453 lst=rem;
454 if(ls_type == LS_TYPE_ADJ)
Obaid Aminbb8ac422013-02-20 17:08:48 -0600455 {
Obaid Amin806df812013-02-21 13:30:26 -0600456 ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size);
457 ccn_name_chop(content_name,components,-2);
458 get_name_part(orig_router,content_name,components,0);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600459
Obaid Amin806df812013-02-21 13:30:26 -0600460 if ( nlsr->debugging )
461 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",orig_router->name,ls_type,origtime);
462
463 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
464 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) )
465 {
466 int is_new_adj_lsa=check_is_new_adj_lsa(orig_router->name,(char *)lst,(char *)origtime);
467 if ( is_new_adj_lsa == 1 )
468 {
469 if ( nlsr->debugging )
470 printf("New Adj LSA.....\n");
471 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
472
473 if ( nlsr->debugging )
474 printf("Content Data: %s \n",content_data);
475 process_incoming_sync_content_lsa(content_data);
476 }
477 else
478 {
479 if ( nlsr->debugging )
480 printf("Adj LSA / Newer Adj LSA already exists in LSDB\n");
481 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
482 if ( nlsr->debugging )
483 printf("Content Data: %s \n",content_data);
484 }
485 }
486 else
487 {
488 if ( nlsr->debugging )
489 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
490 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600491 }
Obaid Amin806df812013-02-21 13:30:26 -0600492 else if(ls_type == LS_TYPE_COR)
Obaid Aminbb8ac422013-02-20 17:08:48 -0600493 {
Obaid Amin806df812013-02-21 13:30:26 -0600494 ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size);
495 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 )
499 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",orig_router->name,ls_type,origtime);
500
501 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
502 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) )
503 {
504 int is_new_cor_lsa=check_is_new_cor_lsa(orig_router->name,(char *)lst,(char *)origtime);
505 if ( is_new_cor_lsa == 1 )
506 {
507 if ( nlsr->debugging )
508 printf("New Cor LSA.....\n");
509 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
510
511 if ( nlsr->debugging )
512 printf("Content Data: %s \n",content_data);
513 process_incoming_sync_content_lsa(content_data);
514 }
515 else
516 {
517 if ( nlsr->debugging )
518 printf("Cor LSA / Newer Cor LSA already exists in LSDB\n");
519 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
520 if ( nlsr->debugging )
521 printf("Content Data: %s \n",content_data);
522 }
523 }
524 else
525 {
526 if ( nlsr->debugging )
527 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
528 }
529
530 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600531 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600532
Obaid Amin05300892013-02-21 13:45:25 -0600533 if (orig_router)
534 free(orig_router);
Obaid Amin806df812013-02-21 13:30:26 -0600535 ccn_charbuf_destroy(&uri);
536 //01/31/2013
537 free(time_stamp);
akmhoquea0e71152013-02-11 09:47:59 -0600538}
539
Obaid Amin7793ba72013-02-22 00:43:58 -0600540 int
akmhoquea0e71152013-02-11 09:47:59 -0600541sync_monitor(char *topo_prefix, char *slice_prefix)
542{
543
Obaid Amin806df812013-02-21 13:30:26 -0600544 struct ccn_charbuf *prefix = ccn_charbuf_create();
Obaid Amin806df812013-02-21 13:30:26 -0600545 struct ccn_charbuf *topo = ccn_charbuf_create();
Obaid Amin7997ad82013-02-22 16:41:56 -0600546
547 nlsr->closure=(struct ccns_name_closure *)
548 calloc(1,sizeof(struct ccns_name_closure));
549
Obaid Amin806df812013-02-21 13:30:26 -0600550 nlsr->slice = ccns_slice_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600551
Obaid Amin806df812013-02-21 13:30:26 -0600552 ccn_charbuf_reset(prefix);
553 ccn_name_from_uri(prefix, slice_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600554
Obaid Amin806df812013-02-21 13:30:26 -0600555 ccn_charbuf_reset(topo);
556 ccn_name_from_uri(topo, topo_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600557
Obaid Amin806df812013-02-21 13:30:26 -0600558 ccns_slice_set_topo_prefix(nlsr->slice, topo, prefix);
559 nlsr->closure->callback = &sync_cb;
akmhoque54d86112013-02-21 16:42:34 -0600560 nlsr->ccns = ccns_open(nlsr->ccn, nlsr->slice, nlsr->closure, NULL, NULL);
akmhoquea0e71152013-02-11 09:47:59 -0600561
Obaid Amin7997ad82013-02-22 16:41:56 -0600562 ccn_charbuf_destroy(&prefix);
563 ccn_charbuf_destroy(&topo);
Obaid Amin806df812013-02-21 13:30:26 -0600564 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600565}
566
Obaid Amin7793ba72013-02-22 00:43:58 -0600567 struct ccn_charbuf *
akmhoquea0e71152013-02-11 09:47:59 -0600568make_template(int scope)
569{
Obaid Amin806df812013-02-21 13:30:26 -0600570 struct ccn_charbuf *templ = NULL;
571 templ = ccn_charbuf_create();
572 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
573 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
574 ccn_charbuf_append_closer(templ); /* </Name> */
575 if (0 <= scope && scope <= 2)
576 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
577 ccn_charbuf_append_closer(templ); /* </Interest> */
578 return(templ);
akmhoquea0e71152013-02-11 09:47:59 -0600579}
580
Obaid Amin7793ba72013-02-22 00:43:58 -0600581 int
akmhoquea0e71152013-02-11 09:47:59 -0600582write_data_to_repo(char *data, char *name_prefix)
583{
Obaid Amin806df812013-02-21 13:30:26 -0600584 if ( nlsr->debugging )
585 {
586 printf("write_data_to_repo called\n");
587 printf("Content Name: %s \n",name_prefix);
588 printf("Content Data: %s \n",data);
589 }
akmhoquea0e71152013-02-11 09:47:59 -0600590
Obaid Aminf210a322013-02-22 02:04:52 -0600591 struct ccn *temp_ccn;
Obaid Amin806df812013-02-21 13:30:26 -0600592 temp_ccn=ccn_create();
593 int ccn_fd=ccn_connect(temp_ccn, NULL);
594 if(ccn_fd == -1)
595 {
596 fprintf(stderr,"Could not connect to ccnd for Data Writing\n");
597 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd for Data Writing\n");
598 return -1;
599 }
Obaid Aminf210a322013-02-22 02:04:52 -0600600
Obaid Amin806df812013-02-21 13:30:26 -0600601 struct ccn_charbuf *name = NULL;
602 struct ccn_seqwriter *w = NULL;
603 int blocksize = 4096;
604 int freshness = -1;
Obaid Amin806df812013-02-21 13:30:26 -0600605 int scope = 1;
606 int res;
607 size_t blockread;
608 struct ccn_charbuf *templ;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600609
Obaid Amin806df812013-02-21 13:30:26 -0600610 name = ccn_charbuf_create();
611 res = ccn_name_from_uri(name, name_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600612 if (res < 0) {
Obaid Amin806df812013-02-21 13:30:26 -0600613 fprintf(stderr, "bad CCN URI: %s\n",name_prefix);
614 return -1;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600615 }
Obaid Amin806df812013-02-21 13:30:26 -0600616
Obaid Aminf210a322013-02-22 02:04:52 -0600617 w = ccn_seqw_create(temp_ccn, name);
Obaid Amin806df812013-02-21 13:30:26 -0600618 if (w == NULL) {
619 fprintf(stderr, "ccn_seqw_create failed\n");
620 return -1;
621 }
622 ccn_seqw_set_block_limits(w, blocksize, blocksize);
623 if (freshness > -1)
624 ccn_seqw_set_freshness(w, freshness);
akmhoquea0e71152013-02-11 09:47:59 -0600625
Obaid Amin7997ad82013-02-22 16:41:56 -0600626 struct ccn_charbuf *name_v = ccn_charbuf_create();
627 ccn_seqw_get_name(w, name_v);
628 ccn_name_from_uri(name_v, "%C1.R.sw");
629 ccn_name_append_nonce(name_v);
630 templ = make_template(scope);
631 res = ccn_get(temp_ccn, name_v, templ, 60000, NULL, NULL, NULL, 0);
632 ccn_charbuf_destroy(&templ);
633 ccn_charbuf_destroy(&name_v);
634 if (res < 0) {
635 fprintf(stderr, "No response from repository\n");
636 return -1;
637 }
akmhoquea0e71152013-02-11 09:47:59 -0600638
Obaid Amin806df812013-02-21 13:30:26 -0600639 blockread = 0;
akmhoquea0e71152013-02-11 09:47:59 -0600640
Obaid Amin806df812013-02-21 13:30:26 -0600641 blockread=strlen(data);
akmhoquea0e71152013-02-11 09:47:59 -0600642
Obaid Amin806df812013-02-21 13:30:26 -0600643 if (blockread > 0) {
Obaid Amin806df812013-02-21 13:30:26 -0600644 res = ccn_seqw_write(w, data, blockread);
645 while (res == -1) {
akmhoque54309a22013-02-22 15:38:25 -0600646 ccn_run(temp_ccn,1);
Obaid Amin806df812013-02-21 13:30:26 -0600647 res = ccn_seqw_write(w, data, blockread);
648 }
649 }
650
akmhoque54309a22013-02-22 15:38:25 -0600651
652 ccn_run(temp_ccn, 1);
Obaid Amin806df812013-02-21 13:30:26 -0600653 ccn_charbuf_destroy(&name);
akmhoque0ed6d982013-02-22 14:28:01 -0600654 ccn_destroy(&temp_ccn);
akmhoquea0e71152013-02-11 09:47:59 -0600655
Obaid Amin806df812013-02-21 13:30:26 -0600656 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600657}
Obaid Amin806df812013-02-21 13:30:26 -0600658 int
Obaid Amin7997ad82013-02-22 16:41:56 -0600659 create_sync_slice(char *topo_prefix, char *slice_prefix)
akmhoquea0e71152013-02-11 09:47:59 -0600660{
Obaid Amin806df812013-02-21 13:30:26 -0600661 int res;
akmhoque0ed6d982013-02-22 14:28:01 -0600662 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 -0600663 struct ccns_slice *slice;
664 struct ccn_charbuf *prefix = ccn_charbuf_create();
665 struct ccn_charbuf *topo = ccn_charbuf_create();
666 struct ccn_charbuf *clause = ccn_charbuf_create();
667 struct ccn_charbuf *slice_name = ccn_charbuf_create();
668 struct ccn_charbuf *slice_uri = ccn_charbuf_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600669
Obaid Amin806df812013-02-21 13:30:26 -0600670 if (prefix == NULL || topo == NULL || clause == NULL ||
671 slice_name == NULL || slice_uri == NULL) {
672 fprintf(stderr, "Unable to allocate required memory.\n");
673 return -1;
674 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600675
akmhoque0ed6d982013-02-22 14:28:01 -0600676 handle = ccn_create();
Obaid Amin806df812013-02-21 13:30:26 -0600677 res = ccn_connect(handle, NULL);
678 if (0 > res) {
679 fprintf(stderr, "Unable to connect to ccnd.\n");
680 return -1;
681 }
akmhoque0ed6d982013-02-22 14:28:01 -0600682
Obaid Amin806df812013-02-21 13:30:26 -0600683 slice = ccns_slice_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600684
Obaid Amin806df812013-02-21 13:30:26 -0600685 ccn_charbuf_reset(topo);
686 ccn_name_from_uri(topo, topo_prefix);
687 ccn_charbuf_reset(prefix);
688 ccn_name_from_uri(prefix,slice_prefix );
689 ccns_slice_set_topo_prefix(slice, topo, prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600690
691
akmhoque0ed6d982013-02-22 14:28:01 -0600692 res = ccns_write_slice(handle, slice, slice_name);
693 //res = ccns_write_slice(nlsr->ccn, slice, slice_name);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600694
Obaid Amin806df812013-02-21 13:30:26 -0600695 //01/31/2013
696 ccns_slice_destroy(&slice);
akmhoque0ed6d982013-02-22 14:28:01 -0600697 ccn_destroy(&handle);
Obaid Amin806df812013-02-21 13:30:26 -0600698 ccn_charbuf_destroy(&prefix);
699 ccn_charbuf_destroy(&topo);
700 ccn_charbuf_destroy(&clause);
701 ccn_charbuf_destroy(&slice_name);
702 ccn_charbuf_destroy(&slice_uri);
akmhoquea0e71152013-02-11 09:47:59 -0600703
Obaid Amin806df812013-02-21 13:30:26 -0600704 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600705}
706