blob: caa886a59eb97ed34f54b0cfe2f3ec4bd4de792b [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)");
68 printf("%s %s %s\n", ccn_charbuf_as_string(uri), hexL, hexR);
69 fflush(stdout);
70 free(hexL);
71 free(hexR);
72 ccn_charbuf_destroy(&uri);
73//--Doing ourthing from here
akmhoqueb29edd82013-01-14 20:54:11 -060074 struct ccn_indexbuf cid={0};
Syed Obaid Amin3a3864b2013-01-09 03:05:50 -060075
76 struct ccn_indexbuf *components=&cid;
77 ccn_name_split (name, components);
78 ccn_name_chop(name,components,-3);
79
80 process_content_from_sync(name,components);
81
82
83
84 return(0);
85}
86//test method
akmhoque8fdd6412012-12-04 15:05:33 -060087
88int
89get_lsa_position(struct ccn_charbuf * ccnb, struct ccn_indexbuf *comps)
90{
91
92
93
94 int res,i;
95 int lsa_position=0;
96 int name_comps=(int)comps->n;
97
98 for(i=0;i<name_comps;i++)
99 {
100 res=ccn_name_comp_strcmp(ccnb->buf,comps,i,"LSA");
101 if( res == 0)
102 {
103 lsa_position=i;
104 break;
105 }
106 }
107
108 return lsa_position;
109
110}
111
112void
113get_name_part(struct name_prefix *name_part,struct ccn_charbuf * interest_ccnb, struct ccn_indexbuf *interest_comps, int offset)
114{
115
116
117
118 int res,i;
119 int lsa_position=0;
120 int len=0;
121
122 lsa_position=get_lsa_position(interest_ccnb,interest_comps);
123
124 const unsigned char *comp_ptr1;
125 size_t comp_size;
126 for(i=lsa_position+1+offset;i<interest_comps->n-1;i++)
127 {
128 res=ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1, &comp_size);
129 len+=1;
130 len+=(int)comp_size;
131 }
132 len++;
133
134 char *neighbor=(char *)malloc(len);
135 memset(neighbor,0,len);
136
137 for(i=lsa_position+1+offset; i<interest_comps->n-1;i++)
138 {
139 res=ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1, &comp_size);
140 memcpy(neighbor+strlen(neighbor),"/",1);
141 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,strlen((char *)comp_ptr1));
142
143 }
144
145 name_part->name=(char *)malloc(strlen(neighbor)+1);
146 memset(name_part->name,0,strlen(neighbor)+1);
147 memcpy(name_part->name,neighbor,strlen(neighbor)+1);
148 name_part->length=strlen(neighbor)+1;
149
150
151}
152
akmhoque09c0afa2012-12-14 09:27:00 -0600153void
154get_host_name_from_command_string(struct name_prefix *name_part,char *nbr_name_uri, int offset)
155{
156
157
158
159 int res,i;
160 int len=0;
161 const unsigned char *comp_ptr1;
162 size_t comp_size;
163
164 struct ccn_charbuf *name=ccn_charbuf_create();
165 name = ccn_charbuf_create();
166 res = ccn_name_from_uri(name,nbr_name_uri);
167 if (res < 0) {
168 fprintf(stderr, "Bad ccn URI: %s\n", nbr_name_uri);
169 exit(1);
170 }
171
172 struct ccn_indexbuf cid={0};
173
174 struct ccn_indexbuf *components=&cid;
175 ccn_name_split (name, components);
176
177 for(i=components->n-2;i> (0+offset);i--)
178 {
179 res=ccn_name_comp_get(name->buf, components,i,&comp_ptr1, &comp_size);
180 len+=1;
181 len+=(int)comp_size;
182 }
183 len++;
184
185 char *neighbor=(char *)malloc(len);
186 memset(neighbor,0,len);
187
188 for(i=components->n-2;i> (0+offset);i--)
189 {
190 res=ccn_name_comp_get(name->buf, components,i,&comp_ptr1, &comp_size);
191 if ( i != components->n-2)
192 memcpy(neighbor+strlen(neighbor),".",1);
193 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,strlen((char *)comp_ptr1));
194
195 }
196
197 name_part->name=(char *)malloc(strlen(neighbor)+1);
198 memset(name_part->name,0,strlen(neighbor)+1);
199 memcpy(name_part->name,neighbor,strlen(neighbor)+1);
200 name_part->length=strlen(neighbor)+1;
201
202
203}
204
akmhoque8fdd6412012-12-04 15:05:33 -0600205
soamin15043622013-01-15 23:10:26 -0600206//char *
207void
Obaid Amine63022a2013-01-15 23:36:40 -0600208get_content_by_content_name(char *content_name, unsigned char **content_data)
akmhoque8fdd6412012-12-04 15:05:33 -0600209{
210
soamin15043622013-01-15 23:10:26 -0600211 struct ccn_charbuf *name = NULL;
212 struct ccn_charbuf *templ = NULL;
213 struct ccn_charbuf *resultbuf = NULL;
214 struct ccn_parsed_ContentObject pcobuf = { 0 };
215 int res;
216 int allow_stale = 0;
217 int content_only = 1;
218 int scope = -1;
219 const unsigned char *ptr;
220 size_t length;
221 int resolve_version = CCN_V_HIGHEST;
222 int timeout_ms = 3000;
223 const unsigned lifetime_default = CCN_INTEREST_LIFETIME_SEC << 12;
224 unsigned lifetime_l12 = lifetime_default;
225 int get_flags = 0;
akmhoque8fdd6412012-12-04 15:05:33 -0600226
soamin15043622013-01-15 23:10:26 -0600227 name = ccn_charbuf_create();
228 res = ccn_name_from_uri(name,content_name);
229 if (res < 0) {
230 fprintf(stderr, "Bad ccn URI: %s\n", content_name);
231 exit(1);
232 }
233
akmhoque8fdd6412012-12-04 15:05:33 -0600234 if (allow_stale || lifetime_l12 != lifetime_default || scope != -1) {
soamin15043622013-01-15 23:10:26 -0600235 templ = ccn_charbuf_create();
236 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
237 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
238 ccn_charbuf_append_closer(templ); /* </Name> */
akmhoque8fdd6412012-12-04 15:05:33 -0600239 if (allow_stale) {
240 ccn_charbuf_append_tt(templ, CCN_DTAG_AnswerOriginKind, CCN_DTAG);
241 ccnb_append_number(templ,
soamin15043622013-01-15 23:10:26 -0600242 CCN_AOK_DEFAULT | CCN_AOK_STALE);
akmhoque8fdd6412012-12-04 15:05:33 -0600243 ccn_charbuf_append_closer(templ); /* </AnswerOriginKind> */
244 }
soamin15043622013-01-15 23:10:26 -0600245 if (scope != -1) {
246 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
247 }
akmhoque8fdd6412012-12-04 15:05:33 -0600248 if (lifetime_l12 != lifetime_default) {
249 /*
250 * Choose the interest lifetime so there are at least 3
251 * expressions (in the unsatisfied case).
252 */
253 unsigned char buf[3] = { 0 };
254 int i;
255 for (i = sizeof(buf) - 1; i >= 0; i--, lifetime_l12 >>= 8)
256 buf[i] = lifetime_l12 & 0xff;
257 ccnb_append_tagged_blob(templ, CCN_DTAG_InterestLifetime, buf, sizeof(buf));
258 }
soamin15043622013-01-15 23:10:26 -0600259 ccn_charbuf_append_closer(templ); /* </Interest> */
260 }
261 resultbuf = ccn_charbuf_create();
262 if (resolve_version != 0) {
263 res = ccn_resolve_version(nlsr->ccn, name, resolve_version, 500);
264 if (res >= 0) {
265 ccn_uri_append(resultbuf, name->buf, name->length, 1);
266 //fprintf(stderr, "== %s\n",ccn_charbuf_as_string(resultbuf));
267 resultbuf->length = 0;
268 }
269 }
270 res = ccn_get(nlsr->ccn, name, templ, timeout_ms, resultbuf, &pcobuf, NULL, get_flags);
271 if (res >= 0) {
272 ptr = resultbuf->buf;
273 length = resultbuf->length;
274 if (content_only){
275 ccn_content_get_value(ptr, length, &pcobuf, &ptr, &length);
Obaid Amine63022a2013-01-15 23:36:40 -0600276 *content_data = (unsigned char *) calloc(length, sizeof(char *));
277 memcpy (*content_data, ptr, length);
soamin15043622013-01-15 23:10:26 -0600278 }
279 }
280 ccn_charbuf_destroy(&resultbuf);
281 ccn_charbuf_destroy(&templ);
282 ccn_charbuf_destroy(&name);
283 //return (unsigned char *)ptr;
akmhoque8fdd6412012-12-04 15:05:33 -0600284}
285
akmhoque4ae16942012-12-10 11:50:43 -0600286void
soamin15043622013-01-15 23:10:26 -0600287process_incoming_sync_content_lsa( unsigned char *content_data)
akmhoque4ae16942012-12-10 11:50:43 -0600288{
289
290
291 if ( nlsr->debugging )
292 printf("process_incoming_sync_content_lsa called \n");
293 //if ( nlsr->detailed_logging )
294 //writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_content_lsa called \n");
295
296 char *sep="|";
297 char *rem;
298 char *orig_router;
299 char *orl;
300 int orig_router_length;
301 char *lst;
302 int ls_type;
303 char *lsid;
304 long int ls_id;
305 char *isvld;
306 int isValid;
307 char *num_link;
308 int no_link;
309 char *np;
310 char *np_length;
311 int name_length;
312 char *data;
313 char *orig_time;
314
315
316 if ( nlsr->debugging )
317 printf("LSA Data \n");
318 //if ( nlsr->detailed_logging )
319 // writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Data\n");
320
soamin15043622013-01-15 23:10:26 -0600321 if( strlen((char *)content_data ) > 0 )
akmhoque4ae16942012-12-10 11:50:43 -0600322 {
323
324 orig_router=strtok_r((char *)content_data,sep,&rem);
325 orl=strtok_r(NULL,sep,&rem);
326 orig_router_length=atoi(orl);
327
328 if ( nlsr->debugging )
329 {
330 printf(" Orig Router Name : %s\n",orig_router);
331 printf(" Orig Router Length: %d\n",orig_router_length);
332 }
333
334 lst=strtok_r(NULL,sep,&rem);
335 ls_type=atoi(lst);
336
337 if ( nlsr->debugging )
338 printf(" LS Type : %d\n",ls_type);
339
340 if ( ls_type == LS_TYPE_NAME )
341 {
342 lsid=strtok_r(NULL,sep,&rem);
343 ls_id=atoi(lsid);
344 orig_time=strtok_r(NULL,sep,&rem);
345 isvld=strtok_r(NULL,sep,&rem);
346 isValid=atoi(isvld);
347 np=strtok_r(NULL,sep,&rem);
348 np_length=strtok_r(NULL,sep,&rem);
349 name_length=atoi(np_length);
350 if ( nlsr->debugging )
351 {
352 printf(" LS ID : %ld\n",ls_id);
353 printf(" isValid : %d\n",isValid);
354 printf(" Name Prefix : %s\n",np);
355 printf(" Orig Time : %s\n",orig_time);
356 printf(" Name Prefix length: %d\n",name_length);
357 }
358
359 build_and_install_others_name_lsa(orig_router,ls_type,ls_id,orig_time,isValid,np);
360
361 print_name_lsdb();
362
363 }
364 else if ( ls_type == LS_TYPE_ADJ )
365 {
366 orig_time=strtok_r(NULL,sep,&rem);
367 num_link=strtok_r(NULL,sep,&rem);
368 no_link=atoi(num_link);
369 data=rem;
370
371 if ( nlsr->debugging )
372 {
373 printf(" No Link : %d\n",no_link);
374 printf(" Data : %s\n",data);
375 }
376 build_and_install_others_adj_lsa(orig_router,ls_type,orig_time,no_link,data);
377 }
378 }
379}
380
akmhoque8fdd6412012-12-04 15:05:33 -0600381void
382process_content_from_sync(struct ccn_charbuf *content_name, struct ccn_indexbuf *components)
383{
384 int lsa_position;
385 int res;
386 size_t comp_size;
387 const unsigned char *lst;
388 const unsigned char *lsid;
389 const unsigned char *origtime;
390
391 int ls_type;
392 long int ls_id=0;
393
soamin15043622013-01-15 23:10:26 -0600394 unsigned char *content_data = NULL;
395
akmhoque4ae16942012-12-10 11:50:43 -0600396 char *time_stamp=(char *)malloc(20);
397 memset(time_stamp,0,20);
398 get_current_timestamp_micro(time_stamp);
399
akmhoque8fdd6412012-12-04 15:05:33 -0600400 struct ccn_charbuf *uri = ccn_charbuf_create();
401 ccn_uri_append(uri, content_name->buf, content_name->length, 0);
402
403 struct name_prefix *orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix));
404
405 lsa_position=get_lsa_position(content_name, components);
406
407 res=ccn_name_comp_get(content_name->buf, components,lsa_position+1,&lst, &comp_size);
408
akmhoque4ae16942012-12-10 11:50:43 -0600409 //printf("Ls Type: %s\n",lst);
akmhoque8fdd6412012-12-04 15:05:33 -0600410 ls_type=atoi((char *)lst);
411 if(ls_type == LS_TYPE_NAME)
412 {
akmhoque4ae16942012-12-10 11:50:43 -0600413
akmhoque8fdd6412012-12-04 15:05:33 -0600414 res=ccn_name_comp_get(content_name->buf, components,lsa_position+2,&lsid, &comp_size);
415 ls_id=atoi((char *)lsid);
416 res=ccn_name_comp_get(content_name->buf, components,lsa_position+3,&origtime, &comp_size);
417 get_name_part(orig_router,content_name,components,3);
akmhoque8fdd6412012-12-04 15:05:33 -0600418
akmhoque4ae16942012-12-10 11:50:43 -0600419 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
420
421 //printf("Ls ID: %s\nOrig Time: %s\nOrig Router: %s\n",lsid,origtime,orig_router->name);
422
423 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 -0600424 {
akmhoque4ae16942012-12-10 11:50:43 -0600425 int is_new_name_lsa=check_is_new_name_lsa(orig_router->name,(char *)lst,(char *)lsid,(char *)origtime);
426 if ( is_new_name_lsa == 1 )
427 {
428 printf("New NAME LSA.....\n");
soamin15043622013-01-15 23:10:26 -0600429 //content_data=get_content_by_content_name(ccn_charbuf_as_string(uri));
Obaid Amine63022a2013-01-15 23:36:40 -0600430 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600431 printf("Content Data: %s \n",content_data);
432 process_incoming_sync_content_lsa(content_data);
433 }
434 else
435 {
436 printf("Name LSA / Newer Name LSA already xists in LSDB\n");
soamin15043622013-01-15 23:10:26 -0600437 //content_data=get_content_by_content_name(ccn_charbuf_as_string(uri));
Obaid Amine63022a2013-01-15 23:36:40 -0600438 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600439 printf("Content Data: %s \n",content_data);
440 }
akmhoque8fdd6412012-12-04 15:05:33 -0600441 }
442 else
443 {
akmhoque4ae16942012-12-10 11:50:43 -0600444 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
akmhoque8fdd6412012-12-04 15:05:33 -0600445 }
446 }
447 else if(ls_type == LS_TYPE_ADJ)
448 {
449 res=ccn_name_comp_get(content_name->buf, components,lsa_position+2,&origtime, &comp_size);
450 get_name_part(orig_router,content_name,components,2);
451 printf("Orig Time: %s\nOrig Router: %s\n",origtime,orig_router->name);
akmhoque8fdd6412012-12-04 15:05:33 -0600452
akmhoque4ae16942012-12-10 11:50:43 -0600453 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque8fdd6412012-12-04 15:05:33 -0600454
akmhoque4ae16942012-12-10 11:50:43 -0600455 //printf("Ls ID: %s\nOrig Time: %s\nOrig Router: %s\n",lsid,origtime,orig_router->name);
456
457 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) )
458 {
459 int is_new_adj_lsa=check_is_new_adj_lsa(orig_router->name,(char *)lst,(char *)origtime);
460 if ( is_new_adj_lsa == 1 )
461 {
462 printf("New Adj LSA.....\n");
soamin15043622013-01-15 23:10:26 -0600463 //content_data=get_content_by_content_name(ccn_charbuf_as_string(uri));
Obaid Amine63022a2013-01-15 23:36:40 -0600464 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600465 printf("Content Data: %s \n",content_data);
466 process_incoming_sync_content_lsa(content_data);
467 }
468 else
469 {
470
471 printf("Adj LSA / Newer Adj LSA already exists in LSDB\n");
Obaid Amine63022a2013-01-15 23:36:40 -0600472 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600473 printf("Content Data: %s \n",content_data);
474 }
475 }
476 else
477 {
478 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
479 }
akmhoque8fdd6412012-12-04 15:05:33 -0600480 }
481
Obaid Amine63022a2013-01-15 23:36:40 -0600482 if (content_data != NULL)
483 free(content_data);
akmhoque8fdd6412012-12-04 15:05:33 -0600484 ccn_charbuf_destroy(&uri);
akmhoque8fdd6412012-12-04 15:05:33 -0600485}
486
487int
488sync_callback(struct ccns_name_closure *nc,
489 struct ccn_charbuf *lhash,
490 struct ccn_charbuf *rhash,
491 struct ccn_charbuf *name)
492{
493
494
495 struct ccn_indexbuf cid={0};
496
497 struct ccn_indexbuf *components=&cid;
498 ccn_name_split (name, components);
499 ccn_name_chop(name,components,-3);
500
501 process_content_from_sync(name,components);
502
503 return(0);
504}
505
506
507void
508sync_monitor(char *topo_prefix, char *slice_prefix)
509{
510
511 static struct ccns_name_closure nc={0};
512
513 nlsr->closure = &nc;
514 struct ccn_charbuf *prefix = ccn_charbuf_create();
515 struct ccn_charbuf *roothash = NULL;
516 struct ccn_charbuf *topo = ccn_charbuf_create();
517 nlsr->slice = ccns_slice_create();
518 ccn_charbuf_reset(prefix);
519 ccn_charbuf_reset(topo);
520
521 ccn_charbuf_reset(prefix);
522 ccn_name_from_uri(prefix, slice_prefix);
523
524 ccn_charbuf_reset(topo);
525 ccn_name_from_uri(topo, topo_prefix);
526
527
528 ccns_slice_set_topo_prefix(nlsr->slice, topo, prefix);
Syed Obaid Amin3a3864b2013-01-09 03:05:50 -0600529 nlsr->closure->callback = &sync_cb;
530 //nlsr->closure->callback = &sync_callback;
akmhoque8fdd6412012-12-04 15:05:33 -0600531 nlsr->ccns = ccns_open(nlsr->ccn, nlsr->slice, nlsr->closure, roothash, NULL);
akmhoque8fdd6412012-12-04 15:05:33 -0600532}
533
534struct ccn_charbuf *
535make_template(int scope)
536{
537 struct ccn_charbuf *templ = NULL;
538 templ = ccn_charbuf_create();
539 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
540 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
541 ccn_charbuf_append_closer(templ); /* </Name> */
542 if (0 <= scope && scope <= 2)
543 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
544 ccn_charbuf_append_closer(templ); /* </Interest> */
545 return(templ);
546}
547
548void
549write_data_to_repo(char *data, char *name_prefix)
550{
akmhoque260698b2013-01-17 03:54:09 -0600551 if ( nlsr->debugging )
552 {
553 printf("write_data_to_repo called\n");
554 prtinf("Content Name: %s \n",name_prefix);
555 printf("Content Data: %s \n",data);
556 }
akmhoque8fdd6412012-12-04 15:05:33 -0600557
558 struct ccn_charbuf *name = NULL;
559 struct ccn_seqwriter *w = NULL;
560 int blocksize = 1024;
561 int freshness = -1;
562 int torepo = 1;
563 int scope = 1;
564 int res;
565 size_t blockread;
566 struct ccn_charbuf *templ;
567
568 name = ccn_charbuf_create();
569 res = ccn_name_from_uri(name, name_prefix);
570 if (res < 0) {
571 fprintf(stderr, "bad CCN URI: %s\n",name_prefix);
572 exit(1);
573 }
574
575
576 w = ccn_seqw_create(nlsr->ccn, name);
577 if (w == NULL) {
578 fprintf(stderr, "ccn_seqw_create failed\n");
579 exit(1);
580 }
581 ccn_seqw_set_block_limits(w, blocksize, blocksize);
582 if (freshness > -1)
583 ccn_seqw_set_freshness(w, freshness);
584 if (torepo) {
585 struct ccn_charbuf *name_v = ccn_charbuf_create();
586 ccn_seqw_get_name(w, name_v);
587 ccn_name_from_uri(name_v, "%C1.R.sw");
588 ccn_name_append_nonce(name_v);
589 templ = make_template(scope);
590 res = ccn_get(nlsr->ccn, name_v, templ, 60000, NULL, NULL, NULL, 0);
591 ccn_charbuf_destroy(&templ);
592 ccn_charbuf_destroy(&name_v);
593 if (res < 0) {
594 fprintf(stderr, "No response from repository\n");
595 exit(1);
596 }
597 }
598
599
600
601
602 blockread = 0;
akmhoque260698b2013-01-17 03:54:09 -0600603
akmhoque8fdd6412012-12-04 15:05:33 -0600604
605 blockread=strlen(data);
606
607 if (blockread > 0) {
akmhoqueb29edd82013-01-14 20:54:11 -0600608 ccn_run(nlsr->ccn, 100);
akmhoque8fdd6412012-12-04 15:05:33 -0600609 res = ccn_seqw_write(w, data, blockread);
akmhoque260698b2013-01-17 03:54:09 -0600610 while (res == -1) {
611 ccn_run(nlsr->ccn, 100);
612 res = ccn_seqw_write(w, data, blockread);
613 }
akmhoque8fdd6412012-12-04 15:05:33 -0600614 }
615
akmhoque8fdd6412012-12-04 15:05:33 -0600616 ccn_seqw_close(w);
akmhoque260698b2013-01-17 03:54:09 -0600617 //ccn_run(nlsr->ccn, 1);
akmhoque8fdd6412012-12-04 15:05:33 -0600618 ccn_charbuf_destroy(&name);
619}
620
621
622int
623create_sync_slice(char *topo_prefix, char *slice_prefix)
624{
625 int res;
626 struct ccns_slice *slice;
627 struct ccn_charbuf *prefix = ccn_charbuf_create();
628 struct ccn_charbuf *topo = ccn_charbuf_create();
629 struct ccn_charbuf *clause = ccn_charbuf_create();
630 struct ccn_charbuf *slice_name = ccn_charbuf_create();
631 struct ccn_charbuf *slice_uri = ccn_charbuf_create();
632
633 if (prefix == NULL || topo == NULL || clause == NULL ||
634 slice_name == NULL || slice_uri == NULL) {
635 fprintf(stderr, "Unable to allocate required memory.\n");
636 exit(1);
637 }
638
639
640 slice = ccns_slice_create();
641
642 ccn_charbuf_reset(topo);
643 ccn_name_from_uri(topo, topo_prefix);
644 ccn_charbuf_reset(prefix);
645 ccn_name_from_uri(prefix,slice_prefix );
646 ccns_slice_set_topo_prefix(slice, topo, prefix);
647
648
649 res = ccns_write_slice(nlsr->ccn, slice, slice_name);
Syed Obaid Amin83eea122013-01-08 12:21:37 -0600650 /*
651// Obaid: commenting out the following lines to resolve a bug.
652// If commenting them can resolve the issue, then we
653// need to call them before terminating the program.
akmhoque8fdd6412012-12-04 15:05:33 -0600654 ccns_slice_destroy(&slice);
655 ccn_charbuf_destroy(&prefix);
656 ccn_charbuf_destroy(&topo);
657 ccn_charbuf_destroy(&clause);
658 ccn_charbuf_destroy(&slice_name);
659 ccn_charbuf_destroy(&slice_uri);
Syed Obaid Amin83eea122013-01-08 12:21:37 -0600660*/
akmhoque8fdd6412012-12-04 15:05:33 -0600661 return 0;
662}
663