blob: 094180f0e3362bfde9765e02d4847d00a714812a [file] [log] [blame]
akmhoque8fdd6412012-12-04 15:05:33 -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"
akmhoque4ae16942012-12-10 11:50:43 -060031#include "utility.h"
Syed Obaid Amin3a3864b2013-01-09 03:05:50 -060032//test method
33char *
34hex_string(unsigned char *s, size_t l)
35{
36 const char *hex_digits = "0123456789abcdef";
37 char *r;
38 int i;
39 r = calloc(1, 1 + 2 * l);
40 for (i = 0; i < l; i++) {
41 r[2*i] = hex_digits[(s[i]>>4) & 0xf];
42 r[1+2*i] = hex_digits[s[i] & 0xf];
43 }
44 return(r);
45}
46
47int
48sync_cb(struct ccns_name_closure *nc,
49 struct ccn_charbuf *lhash,
50 struct ccn_charbuf *rhash,
51 struct ccn_charbuf *name)
52{
53 char *hexL;
54 char *hexR;
55 struct ccn_charbuf *uri = ccn_charbuf_create();
56 if (lhash == NULL || lhash->length == 0) {
57 hexL = strdup("none");
58 } else
59 hexL = hex_string(lhash->buf, lhash->length);
60 if (rhash == NULL || rhash->length == 0) {
61 hexR = strdup("none");
62 } else
63 hexR = hex_string(rhash->buf, rhash->length);
64 if (name != NULL)
65 ccn_uri_append(uri, name->buf, name->length, 1);
66 else
67 ccn_charbuf_append_string(uri, "(null)");
a64adam04be6482013-01-17 11:29:32 -060068
akmhoquefc5176d2013-01-18 09:50:12 -060069 //if ( nlsr->debugging )
70 //printf("%s %s %s\n", ccn_charbuf_as_string(uri), hexL, hexR);
71
a64adam04be6482013-01-17 11:29:32 -060072 if ( nlsr->debugging )
akmhoquefc5176d2013-01-18 09:50:12 -060073 printf("Response from sync in the name: %s \n",ccn_charbuf_as_string(uri));
74
Syed Obaid Amin3a3864b2013-01-09 03:05:50 -060075 fflush(stdout);
76 free(hexL);
77 free(hexR);
78 ccn_charbuf_destroy(&uri);
akmhoqued6cd61d2013-01-17 10:31:15 -060079
80
81 //--Doing ourthing from here
akmhoqueb29edd82013-01-14 20:54:11 -060082 struct ccn_indexbuf cid={0};
Syed Obaid Amin3a3864b2013-01-09 03:05:50 -060083
84 struct ccn_indexbuf *components=&cid;
85 ccn_name_split (name, components);
86 ccn_name_chop(name,components,-3);
87
88 process_content_from_sync(name,components);
89
akmhoquefc5176d2013-01-18 09:50:12 -060090
Syed Obaid Amin3a3864b2013-01-09 03:05:50 -060091
92
93 return(0);
94}
95//test method
akmhoque8fdd6412012-12-04 15:05:33 -060096
97int
98get_lsa_position(struct ccn_charbuf * ccnb, struct ccn_indexbuf *comps)
99{
100
101
102
103 int res,i;
Obaid Amin512c08f2013-01-17 14:11:59 -0600104 int lsa_position=0; //Obaid: Hoque make it -1, 0 is also an index
105 // I didn't change it as it might break the code.
akmhoque8fdd6412012-12-04 15:05:33 -0600106 int name_comps=(int)comps->n;
107
108 for(i=0;i<name_comps;i++)
109 {
110 res=ccn_name_comp_strcmp(ccnb->buf,comps,i,"LSA");
111 if( res == 0)
112 {
113 lsa_position=i;
114 break;
115 }
116 }
117
118 return lsa_position;
119
120}
121
122void
123get_name_part(struct name_prefix *name_part,struct ccn_charbuf * interest_ccnb, struct ccn_indexbuf *interest_comps, int offset)
124{
125
126
127
128 int res,i;
129 int lsa_position=0;
130 int len=0;
131
132 lsa_position=get_lsa_position(interest_ccnb,interest_comps);
133
134 const unsigned char *comp_ptr1;
135 size_t comp_size;
136 for(i=lsa_position+1+offset;i<interest_comps->n-1;i++)
137 {
138 res=ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1, &comp_size);
139 len+=1;
140 len+=(int)comp_size;
141 }
142 len++;
143
144 char *neighbor=(char *)malloc(len);
145 memset(neighbor,0,len);
146
147 for(i=lsa_position+1+offset; i<interest_comps->n-1;i++)
148 {
149 res=ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1, &comp_size);
150 memcpy(neighbor+strlen(neighbor),"/",1);
151 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,strlen((char *)comp_ptr1));
152
153 }
154
155 name_part->name=(char *)malloc(strlen(neighbor)+1);
156 memset(name_part->name,0,strlen(neighbor)+1);
157 memcpy(name_part->name,neighbor,strlen(neighbor)+1);
158 name_part->length=strlen(neighbor)+1;
159
160
161}
162
akmhoque09c0afa2012-12-14 09:27:00 -0600163void
164get_host_name_from_command_string(struct name_prefix *name_part,char *nbr_name_uri, int offset)
165{
166
167
168
169 int res,i;
170 int len=0;
171 const unsigned char *comp_ptr1;
172 size_t comp_size;
173
174 struct ccn_charbuf *name=ccn_charbuf_create();
175 name = ccn_charbuf_create();
176 res = ccn_name_from_uri(name,nbr_name_uri);
177 if (res < 0) {
178 fprintf(stderr, "Bad ccn URI: %s\n", nbr_name_uri);
179 exit(1);
180 }
181
182 struct ccn_indexbuf cid={0};
183
184 struct ccn_indexbuf *components=&cid;
185 ccn_name_split (name, components);
186
187 for(i=components->n-2;i> (0+offset);i--)
188 {
189 res=ccn_name_comp_get(name->buf, components,i,&comp_ptr1, &comp_size);
190 len+=1;
191 len+=(int)comp_size;
192 }
193 len++;
194
195 char *neighbor=(char *)malloc(len);
196 memset(neighbor,0,len);
197
198 for(i=components->n-2;i> (0+offset);i--)
199 {
200 res=ccn_name_comp_get(name->buf, components,i,&comp_ptr1, &comp_size);
201 if ( i != components->n-2)
202 memcpy(neighbor+strlen(neighbor),".",1);
203 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,strlen((char *)comp_ptr1));
204
205 }
206
207 name_part->name=(char *)malloc(strlen(neighbor)+1);
208 memset(name_part->name,0,strlen(neighbor)+1);
209 memcpy(name_part->name,neighbor,strlen(neighbor)+1);
210 name_part->length=strlen(neighbor)+1;
211
212
213}
214
akmhoque8fdd6412012-12-04 15:05:33 -0600215
soamin15043622013-01-15 23:10:26 -0600216//char *
217void
Obaid Amine63022a2013-01-15 23:36:40 -0600218get_content_by_content_name(char *content_name, unsigned char **content_data)
akmhoque8fdd6412012-12-04 15:05:33 -0600219{
220
soamin15043622013-01-15 23:10:26 -0600221 struct ccn_charbuf *name = NULL;
222 struct ccn_charbuf *templ = NULL;
223 struct ccn_charbuf *resultbuf = NULL;
224 struct ccn_parsed_ContentObject pcobuf = { 0 };
225 int res;
226 int allow_stale = 0;
227 int content_only = 1;
228 int scope = -1;
229 const unsigned char *ptr;
230 size_t length;
231 int resolve_version = CCN_V_HIGHEST;
232 int timeout_ms = 3000;
233 const unsigned lifetime_default = CCN_INTEREST_LIFETIME_SEC << 12;
234 unsigned lifetime_l12 = lifetime_default;
235 int get_flags = 0;
akmhoque8fdd6412012-12-04 15:05:33 -0600236
soamin15043622013-01-15 23:10:26 -0600237 name = ccn_charbuf_create();
238 res = ccn_name_from_uri(name,content_name);
239 if (res < 0) {
240 fprintf(stderr, "Bad ccn URI: %s\n", content_name);
241 exit(1);
242 }
243
akmhoque8fdd6412012-12-04 15:05:33 -0600244 if (allow_stale || lifetime_l12 != lifetime_default || scope != -1) {
soamin15043622013-01-15 23:10:26 -0600245 templ = ccn_charbuf_create();
246 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
247 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
248 ccn_charbuf_append_closer(templ); /* </Name> */
akmhoque8fdd6412012-12-04 15:05:33 -0600249 if (allow_stale) {
250 ccn_charbuf_append_tt(templ, CCN_DTAG_AnswerOriginKind, CCN_DTAG);
251 ccnb_append_number(templ,
soamin15043622013-01-15 23:10:26 -0600252 CCN_AOK_DEFAULT | CCN_AOK_STALE);
akmhoque8fdd6412012-12-04 15:05:33 -0600253 ccn_charbuf_append_closer(templ); /* </AnswerOriginKind> */
254 }
soamin15043622013-01-15 23:10:26 -0600255 if (scope != -1) {
256 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
257 }
akmhoque8fdd6412012-12-04 15:05:33 -0600258 if (lifetime_l12 != lifetime_default) {
259 /*
260 * Choose the interest lifetime so there are at least 3
261 * expressions (in the unsatisfied case).
262 */
263 unsigned char buf[3] = { 0 };
264 int i;
265 for (i = sizeof(buf) - 1; i >= 0; i--, lifetime_l12 >>= 8)
266 buf[i] = lifetime_l12 & 0xff;
267 ccnb_append_tagged_blob(templ, CCN_DTAG_InterestLifetime, buf, sizeof(buf));
268 }
soamin15043622013-01-15 23:10:26 -0600269 ccn_charbuf_append_closer(templ); /* </Interest> */
270 }
271 resultbuf = ccn_charbuf_create();
272 if (resolve_version != 0) {
273 res = ccn_resolve_version(nlsr->ccn, name, resolve_version, 500);
274 if (res >= 0) {
275 ccn_uri_append(resultbuf, name->buf, name->length, 1);
276 //fprintf(stderr, "== %s\n",ccn_charbuf_as_string(resultbuf));
277 resultbuf->length = 0;
278 }
279 }
280 res = ccn_get(nlsr->ccn, name, templ, timeout_ms, resultbuf, &pcobuf, NULL, get_flags);
281 if (res >= 0) {
282 ptr = resultbuf->buf;
283 length = resultbuf->length;
284 if (content_only){
285 ccn_content_get_value(ptr, length, &pcobuf, &ptr, &length);
Obaid Amine63022a2013-01-15 23:36:40 -0600286 *content_data = (unsigned char *) calloc(length, sizeof(char *));
287 memcpy (*content_data, ptr, length);
soamin15043622013-01-15 23:10:26 -0600288 }
289 }
290 ccn_charbuf_destroy(&resultbuf);
291 ccn_charbuf_destroy(&templ);
292 ccn_charbuf_destroy(&name);
293 //return (unsigned char *)ptr;
akmhoque8fdd6412012-12-04 15:05:33 -0600294}
295
akmhoque4ae16942012-12-10 11:50:43 -0600296void
soamin15043622013-01-15 23:10:26 -0600297process_incoming_sync_content_lsa( unsigned char *content_data)
akmhoque4ae16942012-12-10 11:50:43 -0600298{
299
300
301 if ( nlsr->debugging )
302 printf("process_incoming_sync_content_lsa called \n");
303 //if ( nlsr->detailed_logging )
304 //writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_content_lsa called \n");
305
306 char *sep="|";
307 char *rem;
308 char *orig_router;
309 char *orl;
310 int orig_router_length;
311 char *lst;
312 int ls_type;
313 char *lsid;
314 long int ls_id;
315 char *isvld;
316 int isValid;
317 char *num_link;
318 int no_link;
319 char *np;
320 char *np_length;
321 int name_length;
322 char *data;
323 char *orig_time;
324
325
326 if ( nlsr->debugging )
327 printf("LSA Data \n");
akmhoque4ae16942012-12-10 11:50:43 -0600328
soamin15043622013-01-15 23:10:26 -0600329 if( strlen((char *)content_data ) > 0 )
akmhoque4ae16942012-12-10 11:50:43 -0600330 {
331
332 orig_router=strtok_r((char *)content_data,sep,&rem);
333 orl=strtok_r(NULL,sep,&rem);
334 orig_router_length=atoi(orl);
335
336 if ( nlsr->debugging )
337 {
338 printf(" Orig Router Name : %s\n",orig_router);
339 printf(" Orig Router Length: %d\n",orig_router_length);
340 }
341
342 lst=strtok_r(NULL,sep,&rem);
343 ls_type=atoi(lst);
344
345 if ( nlsr->debugging )
346 printf(" LS Type : %d\n",ls_type);
347
348 if ( ls_type == LS_TYPE_NAME )
349 {
350 lsid=strtok_r(NULL,sep,&rem);
351 ls_id=atoi(lsid);
352 orig_time=strtok_r(NULL,sep,&rem);
353 isvld=strtok_r(NULL,sep,&rem);
354 isValid=atoi(isvld);
355 np=strtok_r(NULL,sep,&rem);
356 np_length=strtok_r(NULL,sep,&rem);
357 name_length=atoi(np_length);
358 if ( nlsr->debugging )
359 {
360 printf(" LS ID : %ld\n",ls_id);
361 printf(" isValid : %d\n",isValid);
362 printf(" Name Prefix : %s\n",np);
363 printf(" Orig Time : %s\n",orig_time);
364 printf(" Name Prefix length: %d\n",name_length);
365 }
366
367 build_and_install_others_name_lsa(orig_router,ls_type,ls_id,orig_time,isValid,np);
368
369 print_name_lsdb();
370
371 }
372 else if ( ls_type == LS_TYPE_ADJ )
373 {
374 orig_time=strtok_r(NULL,sep,&rem);
375 num_link=strtok_r(NULL,sep,&rem);
376 no_link=atoi(num_link);
377 data=rem;
378
379 if ( nlsr->debugging )
380 {
381 printf(" No Link : %d\n",no_link);
382 printf(" Data : %s\n",data);
383 }
384 build_and_install_others_adj_lsa(orig_router,ls_type,orig_time,no_link,data);
385 }
386 }
387}
388
akmhoque8fdd6412012-12-04 15:05:33 -0600389void
390process_content_from_sync(struct ccn_charbuf *content_name, struct ccn_indexbuf *components)
391{
392 int lsa_position;
393 int res;
394 size_t comp_size;
395 const unsigned char *lst;
396 const unsigned char *lsid;
397 const unsigned char *origtime;
398
399 int ls_type;
400 long int ls_id=0;
401
soamin15043622013-01-15 23:10:26 -0600402 unsigned char *content_data = NULL;
403
akmhoque4ae16942012-12-10 11:50:43 -0600404 char *time_stamp=(char *)malloc(20);
405 memset(time_stamp,0,20);
406 get_current_timestamp_micro(time_stamp);
407
akmhoque8fdd6412012-12-04 15:05:33 -0600408 struct ccn_charbuf *uri = ccn_charbuf_create();
409 ccn_uri_append(uri, content_name->buf, content_name->length, 0);
410
411 struct name_prefix *orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix));
412
413 lsa_position=get_lsa_position(content_name, components);
414
415 res=ccn_name_comp_get(content_name->buf, components,lsa_position+1,&lst, &comp_size);
416
akmhoque4ae16942012-12-10 11:50:43 -0600417 //printf("Ls Type: %s\n",lst);
akmhoque8fdd6412012-12-04 15:05:33 -0600418 ls_type=atoi((char *)lst);
419 if(ls_type == LS_TYPE_NAME)
420 {
akmhoque4ae16942012-12-10 11:50:43 -0600421
akmhoque8fdd6412012-12-04 15:05:33 -0600422 res=ccn_name_comp_get(content_name->buf, components,lsa_position+2,&lsid, &comp_size);
423 ls_id=atoi((char *)lsid);
424 res=ccn_name_comp_get(content_name->buf, components,lsa_position+3,&origtime, &comp_size);
425 get_name_part(orig_router,content_name,components,3);
akmhoque8fdd6412012-12-04 15:05:33 -0600426
akmhoque4ae16942012-12-10 11:50:43 -0600427 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
428
429 //printf("Ls ID: %s\nOrig Time: %s\nOrig Router: %s\n",lsid,origtime,orig_router->name);
430
431 if ( (strcmp((char *)orig_router,nlsr->router_name) == 0 && lsa_life_time < nlsr->lsa_refresh_time) || (strcmp((char *)orig_router,nlsr->router_name) != 0 && lsa_life_time < nlsr->router_dead_interval) )
akmhoque8fdd6412012-12-04 15:05:33 -0600432 {
akmhoque4ae16942012-12-10 11:50:43 -0600433 int is_new_name_lsa=check_is_new_name_lsa(orig_router->name,(char *)lst,(char *)lsid,(char *)origtime);
434 if ( is_new_name_lsa == 1 )
435 {
a64adam04be6482013-01-17 11:29:32 -0600436 if ( nlsr->debugging )
437 printf("New NAME LSA.....\n");
soamin15043622013-01-15 23:10:26 -0600438 //content_data=get_content_by_content_name(ccn_charbuf_as_string(uri));
Obaid Amine63022a2013-01-15 23:36:40 -0600439 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
a64adamf7b051d2013-01-17 11:39:58 -0600440 if ( nlsr->debugging )
441 printf("Content Data: %s \n",content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600442 process_incoming_sync_content_lsa(content_data);
443 }
444 else
445 {
a64adam04be6482013-01-17 11:29:32 -0600446 if ( nlsr->debugging )
447 printf("Name LSA / Newer Name LSA already xists in LSDB\n");
soamin15043622013-01-15 23:10:26 -0600448 //content_data=get_content_by_content_name(ccn_charbuf_as_string(uri));
Obaid Amine63022a2013-01-15 23:36:40 -0600449 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
a64adam04be6482013-01-17 11:29:32 -0600450
451 if ( nlsr->debugging )
452 printf("Content Data: %s \n",content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600453 }
akmhoque8fdd6412012-12-04 15:05:33 -0600454 }
455 else
456 {
a64adam04be6482013-01-17 11:29:32 -0600457 if ( nlsr->debugging )
458 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
akmhoque8fdd6412012-12-04 15:05:33 -0600459 }
460 }
461 else if(ls_type == LS_TYPE_ADJ)
462 {
463 res=ccn_name_comp_get(content_name->buf, components,lsa_position+2,&origtime, &comp_size);
464 get_name_part(orig_router,content_name,components,2);
a64adam04be6482013-01-17 11:29:32 -0600465
466 if ( nlsr->debugging )
467 printf("Orig Time: %s\nOrig Router: %s\n",origtime,orig_router->name);
akmhoque8fdd6412012-12-04 15:05:33 -0600468
akmhoque4ae16942012-12-10 11:50:43 -0600469 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque8fdd6412012-12-04 15:05:33 -0600470
akmhoque4ae16942012-12-10 11:50:43 -0600471 //printf("Ls ID: %s\nOrig Time: %s\nOrig Router: %s\n",lsid,origtime,orig_router->name);
472
473 if ( (strcmp((char *)orig_router,nlsr->router_name) == 0 && lsa_life_time < nlsr->lsa_refresh_time) || (strcmp((char *)orig_router,nlsr->router_name) != 0 && lsa_life_time < nlsr->router_dead_interval) )
474 {
475 int is_new_adj_lsa=check_is_new_adj_lsa(orig_router->name,(char *)lst,(char *)origtime);
476 if ( is_new_adj_lsa == 1 )
477 {
a64adam04be6482013-01-17 11:29:32 -0600478 if ( nlsr->debugging )
479 printf("New Adj LSA.....\n");
soamin15043622013-01-15 23:10:26 -0600480 //content_data=get_content_by_content_name(ccn_charbuf_as_string(uri));
Obaid Amine63022a2013-01-15 23:36:40 -0600481 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
a64adam04be6482013-01-17 11:29:32 -0600482
483 if ( nlsr->debugging )
484 printf("Content Data: %s \n",content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600485 process_incoming_sync_content_lsa(content_data);
486 }
487 else
488 {
a64adam04be6482013-01-17 11:29:32 -0600489 if ( nlsr->debugging )
490 printf("Adj LSA / Newer Adj LSA already exists in LSDB\n");
Obaid Amine63022a2013-01-15 23:36:40 -0600491 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
a64adam04be6482013-01-17 11:29:32 -0600492 if ( nlsr->debugging )
493 printf("Content Data: %s \n",content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600494 }
495 }
496 else
497 {
a64adam04be6482013-01-17 11:29:32 -0600498 if ( nlsr->debugging )
499 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
akmhoque4ae16942012-12-10 11:50:43 -0600500 }
akmhoque8fdd6412012-12-04 15:05:33 -0600501 }
502
Obaid Amine63022a2013-01-15 23:36:40 -0600503 if (content_data != NULL)
504 free(content_data);
akmhoque8fdd6412012-12-04 15:05:33 -0600505 ccn_charbuf_destroy(&uri);
akmhoque8fdd6412012-12-04 15:05:33 -0600506}
507
508int
509sync_callback(struct ccns_name_closure *nc,
510 struct ccn_charbuf *lhash,
511 struct ccn_charbuf *rhash,
512 struct ccn_charbuf *name)
513{
514
515
516 struct ccn_indexbuf cid={0};
517
518 struct ccn_indexbuf *components=&cid;
519 ccn_name_split (name, components);
520 ccn_name_chop(name,components,-3);
521
522 process_content_from_sync(name,components);
523
524 return(0);
525}
526
527
528void
529sync_monitor(char *topo_prefix, char *slice_prefix)
530{
531
532 static struct ccns_name_closure nc={0};
533
534 nlsr->closure = &nc;
535 struct ccn_charbuf *prefix = ccn_charbuf_create();
536 struct ccn_charbuf *roothash = NULL;
537 struct ccn_charbuf *topo = ccn_charbuf_create();
538 nlsr->slice = ccns_slice_create();
539 ccn_charbuf_reset(prefix);
540 ccn_charbuf_reset(topo);
541
542 ccn_charbuf_reset(prefix);
543 ccn_name_from_uri(prefix, slice_prefix);
544
545 ccn_charbuf_reset(topo);
546 ccn_name_from_uri(topo, topo_prefix);
547
548
549 ccns_slice_set_topo_prefix(nlsr->slice, topo, prefix);
Syed Obaid Amin3a3864b2013-01-09 03:05:50 -0600550 nlsr->closure->callback = &sync_cb;
551 //nlsr->closure->callback = &sync_callback;
akmhoque8fdd6412012-12-04 15:05:33 -0600552 nlsr->ccns = ccns_open(nlsr->ccn, nlsr->slice, nlsr->closure, roothash, NULL);
akmhoque8fdd6412012-12-04 15:05:33 -0600553}
554
555struct ccn_charbuf *
556make_template(int scope)
557{
558 struct ccn_charbuf *templ = NULL;
559 templ = ccn_charbuf_create();
560 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
561 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
562 ccn_charbuf_append_closer(templ); /* </Name> */
563 if (0 <= scope && scope <= 2)
564 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
565 ccn_charbuf_append_closer(templ); /* </Interest> */
566 return(templ);
567}
568
akmhoquefc5176d2013-01-18 09:50:12 -0600569int
akmhoque8fdd6412012-12-04 15:05:33 -0600570write_data_to_repo(char *data, char *name_prefix)
571{
akmhoque260698b2013-01-17 03:54:09 -0600572 if ( nlsr->debugging )
573 {
574 printf("write_data_to_repo called\n");
akmhoque47d19d12013-01-17 04:00:32 -0600575 printf("Content Name: %s \n",name_prefix);
akmhoque260698b2013-01-17 03:54:09 -0600576 printf("Content Data: %s \n",data);
577 }
akmhoque8fdd6412012-12-04 15:05:33 -0600578
579 struct ccn_charbuf *name = NULL;
580 struct ccn_seqwriter *w = NULL;
581 int blocksize = 1024;
582 int freshness = -1;
583 int torepo = 1;
584 int scope = 1;
585 int res;
586 size_t blockread;
587 struct ccn_charbuf *templ;
588
589 name = ccn_charbuf_create();
590 res = ccn_name_from_uri(name, name_prefix);
591 if (res < 0) {
592 fprintf(stderr, "bad CCN URI: %s\n",name_prefix);
akmhoquefc5176d2013-01-18 09:50:12 -0600593 //exit(1);
594 return -1;
akmhoque8fdd6412012-12-04 15:05:33 -0600595 }
596
597
598 w = ccn_seqw_create(nlsr->ccn, name);
599 if (w == NULL) {
600 fprintf(stderr, "ccn_seqw_create failed\n");
akmhoquefc5176d2013-01-18 09:50:12 -0600601 //exit(1);
602 return -1;
akmhoque8fdd6412012-12-04 15:05:33 -0600603 }
604 ccn_seqw_set_block_limits(w, blocksize, blocksize);
605 if (freshness > -1)
606 ccn_seqw_set_freshness(w, freshness);
607 if (torepo) {
608 struct ccn_charbuf *name_v = ccn_charbuf_create();
609 ccn_seqw_get_name(w, name_v);
610 ccn_name_from_uri(name_v, "%C1.R.sw");
611 ccn_name_append_nonce(name_v);
612 templ = make_template(scope);
613 res = ccn_get(nlsr->ccn, name_v, templ, 60000, NULL, NULL, NULL, 0);
614 ccn_charbuf_destroy(&templ);
615 ccn_charbuf_destroy(&name_v);
616 if (res < 0) {
617 fprintf(stderr, "No response from repository\n");
akmhoquefc5176d2013-01-18 09:50:12 -0600618 //exit(1);
619 return -1;
akmhoque8fdd6412012-12-04 15:05:33 -0600620 }
621 }
622
623
624
625
626 blockread = 0;
akmhoque260698b2013-01-17 03:54:09 -0600627
akmhoque8fdd6412012-12-04 15:05:33 -0600628
629 blockread=strlen(data);
630
631 if (blockread > 0) {
akmhoqued6cd61d2013-01-17 10:31:15 -0600632 //ccn_run(nlsr->ccn, 100);
akmhoque8fdd6412012-12-04 15:05:33 -0600633 res = ccn_seqw_write(w, data, blockread);
akmhoque260698b2013-01-17 03:54:09 -0600634 while (res == -1) {
akmhoqued6cd61d2013-01-17 10:31:15 -0600635 //ccn_run(nlsr->ccn, 100);
akmhoque260698b2013-01-17 03:54:09 -0600636 res = ccn_seqw_write(w, data, blockread);
637 }
akmhoque8fdd6412012-12-04 15:05:33 -0600638 }
639
akmhoque8fdd6412012-12-04 15:05:33 -0600640 ccn_seqw_close(w);
akmhoque260698b2013-01-17 03:54:09 -0600641 //ccn_run(nlsr->ccn, 1);
akmhoque8fdd6412012-12-04 15:05:33 -0600642 ccn_charbuf_destroy(&name);
akmhoquefc5176d2013-01-18 09:50:12 -0600643
644 return 0;
akmhoque8fdd6412012-12-04 15:05:33 -0600645}
646
647
648int
649create_sync_slice(char *topo_prefix, char *slice_prefix)
650{
651 int res;
652 struct ccns_slice *slice;
653 struct ccn_charbuf *prefix = ccn_charbuf_create();
654 struct ccn_charbuf *topo = ccn_charbuf_create();
655 struct ccn_charbuf *clause = ccn_charbuf_create();
656 struct ccn_charbuf *slice_name = ccn_charbuf_create();
657 struct ccn_charbuf *slice_uri = ccn_charbuf_create();
658
659 if (prefix == NULL || topo == NULL || clause == NULL ||
660 slice_name == NULL || slice_uri == NULL) {
661 fprintf(stderr, "Unable to allocate required memory.\n");
akmhoquefc5176d2013-01-18 09:50:12 -0600662 //exit(1);
663 return -1;
akmhoque8fdd6412012-12-04 15:05:33 -0600664 }
665
666
667 slice = ccns_slice_create();
668
669 ccn_charbuf_reset(topo);
670 ccn_name_from_uri(topo, topo_prefix);
671 ccn_charbuf_reset(prefix);
672 ccn_name_from_uri(prefix,slice_prefix );
673 ccns_slice_set_topo_prefix(slice, topo, prefix);
674
675
676 res = ccns_write_slice(nlsr->ccn, slice, slice_name);
Syed Obaid Amin83eea122013-01-08 12:21:37 -0600677 /*
678// Obaid: commenting out the following lines to resolve a bug.
679// If commenting them can resolve the issue, then we
680// need to call them before terminating the program.
akmhoque8fdd6412012-12-04 15:05:33 -0600681 ccns_slice_destroy(&slice);
682 ccn_charbuf_destroy(&prefix);
683 ccn_charbuf_destroy(&topo);
684 ccn_charbuf_destroy(&clause);
685 ccn_charbuf_destroy(&slice_name);
686 ccn_charbuf_destroy(&slice_uri);
Syed Obaid Amin83eea122013-01-08 12:21:37 -0600687*/
akmhoque8fdd6412012-12-04 15:05:33 -0600688 return 0;
689}
690