blob: 91dd5310329ab2bcbf446722f902fea376c4eaa7 [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
69 if ( nlsr->debugging )
70 printf("%s %s %s\n", ccn_charbuf_as_string(uri), hexL, hexR);
Syed Obaid Amin3a3864b2013-01-09 03:05:50 -060071 fflush(stdout);
72 free(hexL);
73 free(hexR);
74 ccn_charbuf_destroy(&uri);
akmhoqued6cd61d2013-01-17 10:31:15 -060075
76
77 //--Doing ourthing from here
akmhoqueb29edd82013-01-14 20:54:11 -060078 struct ccn_indexbuf cid={0};
Syed Obaid Amin3a3864b2013-01-09 03:05:50 -060079
80 struct ccn_indexbuf *components=&cid;
81 ccn_name_split (name, components);
82 ccn_name_chop(name,components,-3);
83
84 process_content_from_sync(name,components);
85
86
87
88 return(0);
89}
90//test method
akmhoque8fdd6412012-12-04 15:05:33 -060091
92int
93get_lsa_position(struct ccn_charbuf * ccnb, struct ccn_indexbuf *comps)
94{
95
96
97
98 int res,i;
99 int lsa_position=0;
100 int name_comps=(int)comps->n;
101
102 for(i=0;i<name_comps;i++)
103 {
104 res=ccn_name_comp_strcmp(ccnb->buf,comps,i,"LSA");
105 if( res == 0)
106 {
107 lsa_position=i;
108 break;
109 }
110 }
111
112 return lsa_position;
113
114}
115
116void
117get_name_part(struct name_prefix *name_part,struct ccn_charbuf * interest_ccnb, struct ccn_indexbuf *interest_comps, int offset)
118{
119
120
121
122 int res,i;
123 int lsa_position=0;
124 int len=0;
125
126 lsa_position=get_lsa_position(interest_ccnb,interest_comps);
127
128 const unsigned char *comp_ptr1;
129 size_t comp_size;
130 for(i=lsa_position+1+offset;i<interest_comps->n-1;i++)
131 {
132 res=ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1, &comp_size);
133 len+=1;
134 len+=(int)comp_size;
135 }
136 len++;
137
138 char *neighbor=(char *)malloc(len);
139 memset(neighbor,0,len);
140
141 for(i=lsa_position+1+offset; i<interest_comps->n-1;i++)
142 {
143 res=ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1, &comp_size);
144 memcpy(neighbor+strlen(neighbor),"/",1);
145 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,strlen((char *)comp_ptr1));
146
147 }
148
149 name_part->name=(char *)malloc(strlen(neighbor)+1);
150 memset(name_part->name,0,strlen(neighbor)+1);
151 memcpy(name_part->name,neighbor,strlen(neighbor)+1);
152 name_part->length=strlen(neighbor)+1;
153
154
155}
156
akmhoque09c0afa2012-12-14 09:27:00 -0600157void
158get_host_name_from_command_string(struct name_prefix *name_part,char *nbr_name_uri, int offset)
159{
160
161
162
163 int res,i;
164 int len=0;
165 const unsigned char *comp_ptr1;
166 size_t comp_size;
167
168 struct ccn_charbuf *name=ccn_charbuf_create();
169 name = ccn_charbuf_create();
170 res = ccn_name_from_uri(name,nbr_name_uri);
171 if (res < 0) {
172 fprintf(stderr, "Bad ccn URI: %s\n", nbr_name_uri);
173 exit(1);
174 }
175
176 struct ccn_indexbuf cid={0};
177
178 struct ccn_indexbuf *components=&cid;
179 ccn_name_split (name, components);
180
181 for(i=components->n-2;i> (0+offset);i--)
182 {
183 res=ccn_name_comp_get(name->buf, components,i,&comp_ptr1, &comp_size);
184 len+=1;
185 len+=(int)comp_size;
186 }
187 len++;
188
189 char *neighbor=(char *)malloc(len);
190 memset(neighbor,0,len);
191
192 for(i=components->n-2;i> (0+offset);i--)
193 {
194 res=ccn_name_comp_get(name->buf, components,i,&comp_ptr1, &comp_size);
195 if ( i != components->n-2)
196 memcpy(neighbor+strlen(neighbor),".",1);
197 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,strlen((char *)comp_ptr1));
198
199 }
200
201 name_part->name=(char *)malloc(strlen(neighbor)+1);
202 memset(name_part->name,0,strlen(neighbor)+1);
203 memcpy(name_part->name,neighbor,strlen(neighbor)+1);
204 name_part->length=strlen(neighbor)+1;
205
206
207}
208
akmhoque8fdd6412012-12-04 15:05:33 -0600209
soamin15043622013-01-15 23:10:26 -0600210//char *
211void
Obaid Amine63022a2013-01-15 23:36:40 -0600212get_content_by_content_name(char *content_name, unsigned char **content_data)
akmhoque8fdd6412012-12-04 15:05:33 -0600213{
214
soamin15043622013-01-15 23:10:26 -0600215 struct ccn_charbuf *name = NULL;
216 struct ccn_charbuf *templ = NULL;
217 struct ccn_charbuf *resultbuf = NULL;
218 struct ccn_parsed_ContentObject pcobuf = { 0 };
219 int res;
220 int allow_stale = 0;
221 int content_only = 1;
222 int scope = -1;
223 const unsigned char *ptr;
224 size_t length;
225 int resolve_version = CCN_V_HIGHEST;
226 int timeout_ms = 3000;
227 const unsigned lifetime_default = CCN_INTEREST_LIFETIME_SEC << 12;
228 unsigned lifetime_l12 = lifetime_default;
229 int get_flags = 0;
akmhoque8fdd6412012-12-04 15:05:33 -0600230
soamin15043622013-01-15 23:10:26 -0600231 name = ccn_charbuf_create();
232 res = ccn_name_from_uri(name,content_name);
233 if (res < 0) {
234 fprintf(stderr, "Bad ccn URI: %s\n", content_name);
235 exit(1);
236 }
237
akmhoque8fdd6412012-12-04 15:05:33 -0600238 if (allow_stale || lifetime_l12 != lifetime_default || scope != -1) {
soamin15043622013-01-15 23:10:26 -0600239 templ = ccn_charbuf_create();
240 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
241 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
242 ccn_charbuf_append_closer(templ); /* </Name> */
akmhoque8fdd6412012-12-04 15:05:33 -0600243 if (allow_stale) {
244 ccn_charbuf_append_tt(templ, CCN_DTAG_AnswerOriginKind, CCN_DTAG);
245 ccnb_append_number(templ,
soamin15043622013-01-15 23:10:26 -0600246 CCN_AOK_DEFAULT | CCN_AOK_STALE);
akmhoque8fdd6412012-12-04 15:05:33 -0600247 ccn_charbuf_append_closer(templ); /* </AnswerOriginKind> */
248 }
soamin15043622013-01-15 23:10:26 -0600249 if (scope != -1) {
250 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
251 }
akmhoque8fdd6412012-12-04 15:05:33 -0600252 if (lifetime_l12 != lifetime_default) {
253 /*
254 * Choose the interest lifetime so there are at least 3
255 * expressions (in the unsatisfied case).
256 */
257 unsigned char buf[3] = { 0 };
258 int i;
259 for (i = sizeof(buf) - 1; i >= 0; i--, lifetime_l12 >>= 8)
260 buf[i] = lifetime_l12 & 0xff;
261 ccnb_append_tagged_blob(templ, CCN_DTAG_InterestLifetime, buf, sizeof(buf));
262 }
soamin15043622013-01-15 23:10:26 -0600263 ccn_charbuf_append_closer(templ); /* </Interest> */
264 }
265 resultbuf = ccn_charbuf_create();
266 if (resolve_version != 0) {
267 res = ccn_resolve_version(nlsr->ccn, name, resolve_version, 500);
268 if (res >= 0) {
269 ccn_uri_append(resultbuf, name->buf, name->length, 1);
270 //fprintf(stderr, "== %s\n",ccn_charbuf_as_string(resultbuf));
271 resultbuf->length = 0;
272 }
273 }
274 res = ccn_get(nlsr->ccn, name, templ, timeout_ms, resultbuf, &pcobuf, NULL, get_flags);
275 if (res >= 0) {
276 ptr = resultbuf->buf;
277 length = resultbuf->length;
278 if (content_only){
279 ccn_content_get_value(ptr, length, &pcobuf, &ptr, &length);
Obaid Amine63022a2013-01-15 23:36:40 -0600280 *content_data = (unsigned char *) calloc(length, sizeof(char *));
281 memcpy (*content_data, ptr, length);
soamin15043622013-01-15 23:10:26 -0600282 }
283 }
284 ccn_charbuf_destroy(&resultbuf);
285 ccn_charbuf_destroy(&templ);
286 ccn_charbuf_destroy(&name);
287 //return (unsigned char *)ptr;
akmhoque8fdd6412012-12-04 15:05:33 -0600288}
289
akmhoque4ae16942012-12-10 11:50:43 -0600290void
soamin15043622013-01-15 23:10:26 -0600291process_incoming_sync_content_lsa( unsigned char *content_data)
akmhoque4ae16942012-12-10 11:50:43 -0600292{
293
294
295 if ( nlsr->debugging )
296 printf("process_incoming_sync_content_lsa called \n");
297 //if ( nlsr->detailed_logging )
298 //writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_content_lsa called \n");
299
300 char *sep="|";
301 char *rem;
302 char *orig_router;
303 char *orl;
304 int orig_router_length;
305 char *lst;
306 int ls_type;
307 char *lsid;
308 long int ls_id;
309 char *isvld;
310 int isValid;
311 char *num_link;
312 int no_link;
313 char *np;
314 char *np_length;
315 int name_length;
316 char *data;
317 char *orig_time;
318
319
320 if ( nlsr->debugging )
321 printf("LSA Data \n");
322 //if ( nlsr->detailed_logging )
323 // writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Data\n");
324
soamin15043622013-01-15 23:10:26 -0600325 if( strlen((char *)content_data ) > 0 )
akmhoque4ae16942012-12-10 11:50:43 -0600326 {
327
328 orig_router=strtok_r((char *)content_data,sep,&rem);
329 orl=strtok_r(NULL,sep,&rem);
330 orig_router_length=atoi(orl);
331
332 if ( nlsr->debugging )
333 {
334 printf(" Orig Router Name : %s\n",orig_router);
335 printf(" Orig Router Length: %d\n",orig_router_length);
336 }
337
338 lst=strtok_r(NULL,sep,&rem);
339 ls_type=atoi(lst);
340
341 if ( nlsr->debugging )
342 printf(" LS Type : %d\n",ls_type);
343
344 if ( ls_type == LS_TYPE_NAME )
345 {
346 lsid=strtok_r(NULL,sep,&rem);
347 ls_id=atoi(lsid);
348 orig_time=strtok_r(NULL,sep,&rem);
349 isvld=strtok_r(NULL,sep,&rem);
350 isValid=atoi(isvld);
351 np=strtok_r(NULL,sep,&rem);
352 np_length=strtok_r(NULL,sep,&rem);
353 name_length=atoi(np_length);
354 if ( nlsr->debugging )
355 {
356 printf(" LS ID : %ld\n",ls_id);
357 printf(" isValid : %d\n",isValid);
358 printf(" Name Prefix : %s\n",np);
359 printf(" Orig Time : %s\n",orig_time);
360 printf(" Name Prefix length: %d\n",name_length);
361 }
362
363 build_and_install_others_name_lsa(orig_router,ls_type,ls_id,orig_time,isValid,np);
364
365 print_name_lsdb();
366
367 }
368 else if ( ls_type == LS_TYPE_ADJ )
369 {
370 orig_time=strtok_r(NULL,sep,&rem);
371 num_link=strtok_r(NULL,sep,&rem);
372 no_link=atoi(num_link);
373 data=rem;
374
375 if ( nlsr->debugging )
376 {
377 printf(" No Link : %d\n",no_link);
378 printf(" Data : %s\n",data);
379 }
380 build_and_install_others_adj_lsa(orig_router,ls_type,orig_time,no_link,data);
381 }
382 }
383}
384
akmhoque8fdd6412012-12-04 15:05:33 -0600385void
386process_content_from_sync(struct ccn_charbuf *content_name, struct ccn_indexbuf *components)
387{
388 int lsa_position;
389 int res;
390 size_t comp_size;
391 const unsigned char *lst;
392 const unsigned char *lsid;
393 const unsigned char *origtime;
394
395 int ls_type;
396 long int ls_id=0;
397
soamin15043622013-01-15 23:10:26 -0600398 unsigned char *content_data = NULL;
399
akmhoque4ae16942012-12-10 11:50:43 -0600400 char *time_stamp=(char *)malloc(20);
401 memset(time_stamp,0,20);
402 get_current_timestamp_micro(time_stamp);
403
akmhoque8fdd6412012-12-04 15:05:33 -0600404 struct ccn_charbuf *uri = ccn_charbuf_create();
405 ccn_uri_append(uri, content_name->buf, content_name->length, 0);
406
407 struct name_prefix *orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix));
408
409 lsa_position=get_lsa_position(content_name, components);
410
411 res=ccn_name_comp_get(content_name->buf, components,lsa_position+1,&lst, &comp_size);
412
akmhoque4ae16942012-12-10 11:50:43 -0600413 //printf("Ls Type: %s\n",lst);
akmhoque8fdd6412012-12-04 15:05:33 -0600414 ls_type=atoi((char *)lst);
415 if(ls_type == LS_TYPE_NAME)
416 {
akmhoque4ae16942012-12-10 11:50:43 -0600417
akmhoque8fdd6412012-12-04 15:05:33 -0600418 res=ccn_name_comp_get(content_name->buf, components,lsa_position+2,&lsid, &comp_size);
419 ls_id=atoi((char *)lsid);
420 res=ccn_name_comp_get(content_name->buf, components,lsa_position+3,&origtime, &comp_size);
421 get_name_part(orig_router,content_name,components,3);
akmhoque8fdd6412012-12-04 15:05:33 -0600422
akmhoque4ae16942012-12-10 11:50:43 -0600423 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
424
425 //printf("Ls ID: %s\nOrig Time: %s\nOrig Router: %s\n",lsid,origtime,orig_router->name);
426
427 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 -0600428 {
akmhoque4ae16942012-12-10 11:50:43 -0600429 int is_new_name_lsa=check_is_new_name_lsa(orig_router->name,(char *)lst,(char *)lsid,(char *)origtime);
430 if ( is_new_name_lsa == 1 )
431 {
a64adam04be6482013-01-17 11:29:32 -0600432 if ( nlsr->debugging )
433 printf("New NAME LSA.....\n");
soamin15043622013-01-15 23:10:26 -0600434 //content_data=get_content_by_content_name(ccn_charbuf_as_string(uri));
Obaid Amine63022a2013-01-15 23:36:40 -0600435 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
a64adamf7b051d2013-01-17 11:39:58 -0600436 if ( nlsr->debugging )
437 printf("Content Data: %s \n",content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600438 process_incoming_sync_content_lsa(content_data);
439 }
440 else
441 {
a64adam04be6482013-01-17 11:29:32 -0600442 if ( nlsr->debugging )
443 printf("Name LSA / Newer Name LSA already xists in LSDB\n");
soamin15043622013-01-15 23:10:26 -0600444 //content_data=get_content_by_content_name(ccn_charbuf_as_string(uri));
Obaid Amine63022a2013-01-15 23:36:40 -0600445 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
a64adam04be6482013-01-17 11:29:32 -0600446
447 if ( nlsr->debugging )
448 printf("Content Data: %s \n",content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600449 }
akmhoque8fdd6412012-12-04 15:05:33 -0600450 }
451 else
452 {
a64adam04be6482013-01-17 11:29:32 -0600453 if ( nlsr->debugging )
454 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
akmhoque8fdd6412012-12-04 15:05:33 -0600455 }
456 }
457 else if(ls_type == LS_TYPE_ADJ)
458 {
459 res=ccn_name_comp_get(content_name->buf, components,lsa_position+2,&origtime, &comp_size);
460 get_name_part(orig_router,content_name,components,2);
a64adam04be6482013-01-17 11:29:32 -0600461
462 if ( nlsr->debugging )
463 printf("Orig Time: %s\nOrig Router: %s\n",origtime,orig_router->name);
akmhoque8fdd6412012-12-04 15:05:33 -0600464
akmhoque4ae16942012-12-10 11:50:43 -0600465 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque8fdd6412012-12-04 15:05:33 -0600466
akmhoque4ae16942012-12-10 11:50:43 -0600467 //printf("Ls ID: %s\nOrig Time: %s\nOrig Router: %s\n",lsid,origtime,orig_router->name);
468
469 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) )
470 {
471 int is_new_adj_lsa=check_is_new_adj_lsa(orig_router->name,(char *)lst,(char *)origtime);
472 if ( is_new_adj_lsa == 1 )
473 {
a64adam04be6482013-01-17 11:29:32 -0600474 if ( nlsr->debugging )
475 printf("New Adj LSA.....\n");
soamin15043622013-01-15 23:10:26 -0600476 //content_data=get_content_by_content_name(ccn_charbuf_as_string(uri));
Obaid Amine63022a2013-01-15 23:36:40 -0600477 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
a64adam04be6482013-01-17 11:29:32 -0600478
479 if ( nlsr->debugging )
480 printf("Content Data: %s \n",content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600481 process_incoming_sync_content_lsa(content_data);
482 }
483 else
484 {
a64adam04be6482013-01-17 11:29:32 -0600485 if ( nlsr->debugging )
486 printf("Adj LSA / Newer Adj LSA already exists in LSDB\n");
Obaid Amine63022a2013-01-15 23:36:40 -0600487 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
a64adam04be6482013-01-17 11:29:32 -0600488 if ( nlsr->debugging )
489 printf("Content Data: %s \n",content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600490 }
491 }
492 else
493 {
a64adam04be6482013-01-17 11:29:32 -0600494 if ( nlsr->debugging )
495 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
akmhoque4ae16942012-12-10 11:50:43 -0600496 }
akmhoque8fdd6412012-12-04 15:05:33 -0600497 }
498
Obaid Amine63022a2013-01-15 23:36:40 -0600499 if (content_data != NULL)
500 free(content_data);
akmhoque8fdd6412012-12-04 15:05:33 -0600501 ccn_charbuf_destroy(&uri);
akmhoque8fdd6412012-12-04 15:05:33 -0600502}
503
504int
505sync_callback(struct ccns_name_closure *nc,
506 struct ccn_charbuf *lhash,
507 struct ccn_charbuf *rhash,
508 struct ccn_charbuf *name)
509{
510
511
512 struct ccn_indexbuf cid={0};
513
514 struct ccn_indexbuf *components=&cid;
515 ccn_name_split (name, components);
516 ccn_name_chop(name,components,-3);
517
518 process_content_from_sync(name,components);
519
520 return(0);
521}
522
523
524void
525sync_monitor(char *topo_prefix, char *slice_prefix)
526{
527
528 static struct ccns_name_closure nc={0};
529
530 nlsr->closure = &nc;
531 struct ccn_charbuf *prefix = ccn_charbuf_create();
532 struct ccn_charbuf *roothash = NULL;
533 struct ccn_charbuf *topo = ccn_charbuf_create();
534 nlsr->slice = ccns_slice_create();
535 ccn_charbuf_reset(prefix);
536 ccn_charbuf_reset(topo);
537
538 ccn_charbuf_reset(prefix);
539 ccn_name_from_uri(prefix, slice_prefix);
540
541 ccn_charbuf_reset(topo);
542 ccn_name_from_uri(topo, topo_prefix);
543
544
545 ccns_slice_set_topo_prefix(nlsr->slice, topo, prefix);
Syed Obaid Amin3a3864b2013-01-09 03:05:50 -0600546 nlsr->closure->callback = &sync_cb;
547 //nlsr->closure->callback = &sync_callback;
akmhoque8fdd6412012-12-04 15:05:33 -0600548 nlsr->ccns = ccns_open(nlsr->ccn, nlsr->slice, nlsr->closure, roothash, NULL);
akmhoque8fdd6412012-12-04 15:05:33 -0600549}
550
551struct ccn_charbuf *
552make_template(int scope)
553{
554 struct ccn_charbuf *templ = NULL;
555 templ = ccn_charbuf_create();
556 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
557 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
558 ccn_charbuf_append_closer(templ); /* </Name> */
559 if (0 <= scope && scope <= 2)
560 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
561 ccn_charbuf_append_closer(templ); /* </Interest> */
562 return(templ);
563}
564
565void
566write_data_to_repo(char *data, char *name_prefix)
567{
akmhoque260698b2013-01-17 03:54:09 -0600568 if ( nlsr->debugging )
569 {
570 printf("write_data_to_repo called\n");
akmhoque47d19d12013-01-17 04:00:32 -0600571 printf("Content Name: %s \n",name_prefix);
akmhoque260698b2013-01-17 03:54:09 -0600572 printf("Content Data: %s \n",data);
573 }
akmhoque8fdd6412012-12-04 15:05:33 -0600574
575 struct ccn_charbuf *name = NULL;
576 struct ccn_seqwriter *w = NULL;
577 int blocksize = 1024;
578 int freshness = -1;
579 int torepo = 1;
580 int scope = 1;
581 int res;
582 size_t blockread;
583 struct ccn_charbuf *templ;
584
585 name = ccn_charbuf_create();
586 res = ccn_name_from_uri(name, name_prefix);
587 if (res < 0) {
588 fprintf(stderr, "bad CCN URI: %s\n",name_prefix);
589 exit(1);
590 }
591
592
593 w = ccn_seqw_create(nlsr->ccn, name);
594 if (w == NULL) {
595 fprintf(stderr, "ccn_seqw_create failed\n");
596 exit(1);
597 }
598 ccn_seqw_set_block_limits(w, blocksize, blocksize);
599 if (freshness > -1)
600 ccn_seqw_set_freshness(w, freshness);
601 if (torepo) {
602 struct ccn_charbuf *name_v = ccn_charbuf_create();
603 ccn_seqw_get_name(w, name_v);
604 ccn_name_from_uri(name_v, "%C1.R.sw");
605 ccn_name_append_nonce(name_v);
606 templ = make_template(scope);
607 res = ccn_get(nlsr->ccn, name_v, templ, 60000, NULL, NULL, NULL, 0);
608 ccn_charbuf_destroy(&templ);
609 ccn_charbuf_destroy(&name_v);
610 if (res < 0) {
611 fprintf(stderr, "No response from repository\n");
612 exit(1);
613 }
614 }
615
616
617
618
619 blockread = 0;
akmhoque260698b2013-01-17 03:54:09 -0600620
akmhoque8fdd6412012-12-04 15:05:33 -0600621
622 blockread=strlen(data);
623
624 if (blockread > 0) {
akmhoqued6cd61d2013-01-17 10:31:15 -0600625 //ccn_run(nlsr->ccn, 100);
akmhoque8fdd6412012-12-04 15:05:33 -0600626 res = ccn_seqw_write(w, data, blockread);
akmhoque260698b2013-01-17 03:54:09 -0600627 while (res == -1) {
akmhoqued6cd61d2013-01-17 10:31:15 -0600628 //ccn_run(nlsr->ccn, 100);
akmhoque260698b2013-01-17 03:54:09 -0600629 res = ccn_seqw_write(w, data, blockread);
630 }
akmhoque8fdd6412012-12-04 15:05:33 -0600631 }
632
akmhoque8fdd6412012-12-04 15:05:33 -0600633 ccn_seqw_close(w);
akmhoque260698b2013-01-17 03:54:09 -0600634 //ccn_run(nlsr->ccn, 1);
akmhoque8fdd6412012-12-04 15:05:33 -0600635 ccn_charbuf_destroy(&name);
636}
637
638
639int
640create_sync_slice(char *topo_prefix, char *slice_prefix)
641{
642 int res;
643 struct ccns_slice *slice;
644 struct ccn_charbuf *prefix = ccn_charbuf_create();
645 struct ccn_charbuf *topo = ccn_charbuf_create();
646 struct ccn_charbuf *clause = ccn_charbuf_create();
647 struct ccn_charbuf *slice_name = ccn_charbuf_create();
648 struct ccn_charbuf *slice_uri = ccn_charbuf_create();
649
650 if (prefix == NULL || topo == NULL || clause == NULL ||
651 slice_name == NULL || slice_uri == NULL) {
652 fprintf(stderr, "Unable to allocate required memory.\n");
653 exit(1);
654 }
655
656
657 slice = ccns_slice_create();
658
659 ccn_charbuf_reset(topo);
660 ccn_name_from_uri(topo, topo_prefix);
661 ccn_charbuf_reset(prefix);
662 ccn_name_from_uri(prefix,slice_prefix );
663 ccns_slice_set_topo_prefix(slice, topo, prefix);
664
665
666 res = ccns_write_slice(nlsr->ccn, slice, slice_name);
Syed Obaid Amin83eea122013-01-08 12:21:37 -0600667 /*
668// Obaid: commenting out the following lines to resolve a bug.
669// If commenting them can resolve the issue, then we
670// need to call them before terminating the program.
akmhoque8fdd6412012-12-04 15:05:33 -0600671 ccns_slice_destroy(&slice);
672 ccn_charbuf_destroy(&prefix);
673 ccn_charbuf_destroy(&topo);
674 ccn_charbuf_destroy(&clause);
675 ccn_charbuf_destroy(&slice_name);
676 ccn_charbuf_destroy(&slice_uri);
Syed Obaid Amin83eea122013-01-08 12:21:37 -0600677*/
akmhoque8fdd6412012-12-04 15:05:33 -0600678 return 0;
679}
680