blob: 313b1b97eda2c2f077f72a5c5a1fdf70e7619db9 [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);
akmhoqued6cd61d2013-01-17 10:31:15 -060073
74
75 //--Doing ourthing from here
akmhoqueb29edd82013-01-14 20:54:11 -060076 struct ccn_indexbuf cid={0};
Syed Obaid Amin3a3864b2013-01-09 03:05:50 -060077
78 struct ccn_indexbuf *components=&cid;
79 ccn_name_split (name, components);
80 ccn_name_chop(name,components,-3);
81
82 process_content_from_sync(name,components);
83
84
85
86 return(0);
87}
88//test method
akmhoque8fdd6412012-12-04 15:05:33 -060089
90int
91get_lsa_position(struct ccn_charbuf * ccnb, struct ccn_indexbuf *comps)
92{
93
94
95
96 int res,i;
97 int lsa_position=0;
98 int name_comps=(int)comps->n;
99
100 for(i=0;i<name_comps;i++)
101 {
102 res=ccn_name_comp_strcmp(ccnb->buf,comps,i,"LSA");
103 if( res == 0)
104 {
105 lsa_position=i;
106 break;
107 }
108 }
109
110 return lsa_position;
111
112}
113
114void
115get_name_part(struct name_prefix *name_part,struct ccn_charbuf * interest_ccnb, struct ccn_indexbuf *interest_comps, int offset)
116{
117
118
119
120 int res,i;
121 int lsa_position=0;
122 int len=0;
123
124 lsa_position=get_lsa_position(interest_ccnb,interest_comps);
125
126 const unsigned char *comp_ptr1;
127 size_t comp_size;
128 for(i=lsa_position+1+offset;i<interest_comps->n-1;i++)
129 {
130 res=ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1, &comp_size);
131 len+=1;
132 len+=(int)comp_size;
133 }
134 len++;
135
136 char *neighbor=(char *)malloc(len);
137 memset(neighbor,0,len);
138
139 for(i=lsa_position+1+offset; i<interest_comps->n-1;i++)
140 {
141 res=ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1, &comp_size);
142 memcpy(neighbor+strlen(neighbor),"/",1);
143 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,strlen((char *)comp_ptr1));
144
145 }
146
147 name_part->name=(char *)malloc(strlen(neighbor)+1);
148 memset(name_part->name,0,strlen(neighbor)+1);
149 memcpy(name_part->name,neighbor,strlen(neighbor)+1);
150 name_part->length=strlen(neighbor)+1;
151
152
153}
154
akmhoque09c0afa2012-12-14 09:27:00 -0600155void
156get_host_name_from_command_string(struct name_prefix *name_part,char *nbr_name_uri, int offset)
157{
158
159
160
161 int res,i;
162 int len=0;
163 const unsigned char *comp_ptr1;
164 size_t comp_size;
165
166 struct ccn_charbuf *name=ccn_charbuf_create();
167 name = ccn_charbuf_create();
168 res = ccn_name_from_uri(name,nbr_name_uri);
169 if (res < 0) {
170 fprintf(stderr, "Bad ccn URI: %s\n", nbr_name_uri);
171 exit(1);
172 }
173
174 struct ccn_indexbuf cid={0};
175
176 struct ccn_indexbuf *components=&cid;
177 ccn_name_split (name, components);
178
179 for(i=components->n-2;i> (0+offset);i--)
180 {
181 res=ccn_name_comp_get(name->buf, components,i,&comp_ptr1, &comp_size);
182 len+=1;
183 len+=(int)comp_size;
184 }
185 len++;
186
187 char *neighbor=(char *)malloc(len);
188 memset(neighbor,0,len);
189
190 for(i=components->n-2;i> (0+offset);i--)
191 {
192 res=ccn_name_comp_get(name->buf, components,i,&comp_ptr1, &comp_size);
193 if ( i != components->n-2)
194 memcpy(neighbor+strlen(neighbor),".",1);
195 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,strlen((char *)comp_ptr1));
196
197 }
198
199 name_part->name=(char *)malloc(strlen(neighbor)+1);
200 memset(name_part->name,0,strlen(neighbor)+1);
201 memcpy(name_part->name,neighbor,strlen(neighbor)+1);
202 name_part->length=strlen(neighbor)+1;
203
204
205}
206
akmhoque8fdd6412012-12-04 15:05:33 -0600207
soamin15043622013-01-15 23:10:26 -0600208//char *
209void
Obaid Amine63022a2013-01-15 23:36:40 -0600210get_content_by_content_name(char *content_name, unsigned char **content_data)
akmhoque8fdd6412012-12-04 15:05:33 -0600211{
212
soamin15043622013-01-15 23:10:26 -0600213 struct ccn_charbuf *name = NULL;
214 struct ccn_charbuf *templ = NULL;
215 struct ccn_charbuf *resultbuf = NULL;
216 struct ccn_parsed_ContentObject pcobuf = { 0 };
217 int res;
218 int allow_stale = 0;
219 int content_only = 1;
220 int scope = -1;
221 const unsigned char *ptr;
222 size_t length;
223 int resolve_version = CCN_V_HIGHEST;
224 int timeout_ms = 3000;
225 const unsigned lifetime_default = CCN_INTEREST_LIFETIME_SEC << 12;
226 unsigned lifetime_l12 = lifetime_default;
227 int get_flags = 0;
akmhoque8fdd6412012-12-04 15:05:33 -0600228
soamin15043622013-01-15 23:10:26 -0600229 name = ccn_charbuf_create();
230 res = ccn_name_from_uri(name,content_name);
231 if (res < 0) {
232 fprintf(stderr, "Bad ccn URI: %s\n", content_name);
233 exit(1);
234 }
235
akmhoque8fdd6412012-12-04 15:05:33 -0600236 if (allow_stale || lifetime_l12 != lifetime_default || scope != -1) {
soamin15043622013-01-15 23:10:26 -0600237 templ = ccn_charbuf_create();
238 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
239 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
240 ccn_charbuf_append_closer(templ); /* </Name> */
akmhoque8fdd6412012-12-04 15:05:33 -0600241 if (allow_stale) {
242 ccn_charbuf_append_tt(templ, CCN_DTAG_AnswerOriginKind, CCN_DTAG);
243 ccnb_append_number(templ,
soamin15043622013-01-15 23:10:26 -0600244 CCN_AOK_DEFAULT | CCN_AOK_STALE);
akmhoque8fdd6412012-12-04 15:05:33 -0600245 ccn_charbuf_append_closer(templ); /* </AnswerOriginKind> */
246 }
soamin15043622013-01-15 23:10:26 -0600247 if (scope != -1) {
248 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
249 }
akmhoque8fdd6412012-12-04 15:05:33 -0600250 if (lifetime_l12 != lifetime_default) {
251 /*
252 * Choose the interest lifetime so there are at least 3
253 * expressions (in the unsatisfied case).
254 */
255 unsigned char buf[3] = { 0 };
256 int i;
257 for (i = sizeof(buf) - 1; i >= 0; i--, lifetime_l12 >>= 8)
258 buf[i] = lifetime_l12 & 0xff;
259 ccnb_append_tagged_blob(templ, CCN_DTAG_InterestLifetime, buf, sizeof(buf));
260 }
soamin15043622013-01-15 23:10:26 -0600261 ccn_charbuf_append_closer(templ); /* </Interest> */
262 }
263 resultbuf = ccn_charbuf_create();
264 if (resolve_version != 0) {
265 res = ccn_resolve_version(nlsr->ccn, name, resolve_version, 500);
266 if (res >= 0) {
267 ccn_uri_append(resultbuf, name->buf, name->length, 1);
268 //fprintf(stderr, "== %s\n",ccn_charbuf_as_string(resultbuf));
269 resultbuf->length = 0;
270 }
271 }
272 res = ccn_get(nlsr->ccn, name, templ, timeout_ms, resultbuf, &pcobuf, NULL, get_flags);
273 if (res >= 0) {
274 ptr = resultbuf->buf;
275 length = resultbuf->length;
276 if (content_only){
277 ccn_content_get_value(ptr, length, &pcobuf, &ptr, &length);
Obaid Amine63022a2013-01-15 23:36:40 -0600278 *content_data = (unsigned char *) calloc(length, sizeof(char *));
279 memcpy (*content_data, ptr, length);
soamin15043622013-01-15 23:10:26 -0600280 }
281 }
282 ccn_charbuf_destroy(&resultbuf);
283 ccn_charbuf_destroy(&templ);
284 ccn_charbuf_destroy(&name);
285 //return (unsigned char *)ptr;
akmhoque8fdd6412012-12-04 15:05:33 -0600286}
287
akmhoque4ae16942012-12-10 11:50:43 -0600288void
soamin15043622013-01-15 23:10:26 -0600289process_incoming_sync_content_lsa( unsigned char *content_data)
akmhoque4ae16942012-12-10 11:50:43 -0600290{
291
292
293 if ( nlsr->debugging )
294 printf("process_incoming_sync_content_lsa called \n");
295 //if ( nlsr->detailed_logging )
296 //writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_content_lsa called \n");
297
298 char *sep="|";
299 char *rem;
300 char *orig_router;
301 char *orl;
302 int orig_router_length;
303 char *lst;
304 int ls_type;
305 char *lsid;
306 long int ls_id;
307 char *isvld;
308 int isValid;
309 char *num_link;
310 int no_link;
311 char *np;
312 char *np_length;
313 int name_length;
314 char *data;
315 char *orig_time;
316
317
318 if ( nlsr->debugging )
319 printf("LSA Data \n");
320 //if ( nlsr->detailed_logging )
321 // writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Data\n");
322
soamin15043622013-01-15 23:10:26 -0600323 if( strlen((char *)content_data ) > 0 )
akmhoque4ae16942012-12-10 11:50:43 -0600324 {
325
326 orig_router=strtok_r((char *)content_data,sep,&rem);
327 orl=strtok_r(NULL,sep,&rem);
328 orig_router_length=atoi(orl);
329
330 if ( nlsr->debugging )
331 {
332 printf(" Orig Router Name : %s\n",orig_router);
333 printf(" Orig Router Length: %d\n",orig_router_length);
334 }
335
336 lst=strtok_r(NULL,sep,&rem);
337 ls_type=atoi(lst);
338
339 if ( nlsr->debugging )
340 printf(" LS Type : %d\n",ls_type);
341
342 if ( ls_type == LS_TYPE_NAME )
343 {
344 lsid=strtok_r(NULL,sep,&rem);
345 ls_id=atoi(lsid);
346 orig_time=strtok_r(NULL,sep,&rem);
347 isvld=strtok_r(NULL,sep,&rem);
348 isValid=atoi(isvld);
349 np=strtok_r(NULL,sep,&rem);
350 np_length=strtok_r(NULL,sep,&rem);
351 name_length=atoi(np_length);
352 if ( nlsr->debugging )
353 {
354 printf(" LS ID : %ld\n",ls_id);
355 printf(" isValid : %d\n",isValid);
356 printf(" Name Prefix : %s\n",np);
357 printf(" Orig Time : %s\n",orig_time);
358 printf(" Name Prefix length: %d\n",name_length);
359 }
360
361 build_and_install_others_name_lsa(orig_router,ls_type,ls_id,orig_time,isValid,np);
362
363 print_name_lsdb();
364
365 }
366 else if ( ls_type == LS_TYPE_ADJ )
367 {
368 orig_time=strtok_r(NULL,sep,&rem);
369 num_link=strtok_r(NULL,sep,&rem);
370 no_link=atoi(num_link);
371 data=rem;
372
373 if ( nlsr->debugging )
374 {
375 printf(" No Link : %d\n",no_link);
376 printf(" Data : %s\n",data);
377 }
378 build_and_install_others_adj_lsa(orig_router,ls_type,orig_time,no_link,data);
379 }
380 }
381}
382
akmhoque8fdd6412012-12-04 15:05:33 -0600383void
384process_content_from_sync(struct ccn_charbuf *content_name, struct ccn_indexbuf *components)
385{
386 int lsa_position;
387 int res;
388 size_t comp_size;
389 const unsigned char *lst;
390 const unsigned char *lsid;
391 const unsigned char *origtime;
392
393 int ls_type;
394 long int ls_id=0;
395
soamin15043622013-01-15 23:10:26 -0600396 unsigned char *content_data = NULL;
397
akmhoque4ae16942012-12-10 11:50:43 -0600398 char *time_stamp=(char *)malloc(20);
399 memset(time_stamp,0,20);
400 get_current_timestamp_micro(time_stamp);
401
akmhoque8fdd6412012-12-04 15:05:33 -0600402 struct ccn_charbuf *uri = ccn_charbuf_create();
403 ccn_uri_append(uri, content_name->buf, content_name->length, 0);
404
405 struct name_prefix *orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix));
406
407 lsa_position=get_lsa_position(content_name, components);
408
409 res=ccn_name_comp_get(content_name->buf, components,lsa_position+1,&lst, &comp_size);
410
akmhoque4ae16942012-12-10 11:50:43 -0600411 //printf("Ls Type: %s\n",lst);
akmhoque8fdd6412012-12-04 15:05:33 -0600412 ls_type=atoi((char *)lst);
413 if(ls_type == LS_TYPE_NAME)
414 {
akmhoque4ae16942012-12-10 11:50:43 -0600415
akmhoque8fdd6412012-12-04 15:05:33 -0600416 res=ccn_name_comp_get(content_name->buf, components,lsa_position+2,&lsid, &comp_size);
417 ls_id=atoi((char *)lsid);
418 res=ccn_name_comp_get(content_name->buf, components,lsa_position+3,&origtime, &comp_size);
419 get_name_part(orig_router,content_name,components,3);
akmhoque8fdd6412012-12-04 15:05:33 -0600420
akmhoque4ae16942012-12-10 11:50:43 -0600421 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
422
423 //printf("Ls ID: %s\nOrig Time: %s\nOrig Router: %s\n",lsid,origtime,orig_router->name);
424
425 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 -0600426 {
akmhoque4ae16942012-12-10 11:50:43 -0600427 int is_new_name_lsa=check_is_new_name_lsa(orig_router->name,(char *)lst,(char *)lsid,(char *)origtime);
428 if ( is_new_name_lsa == 1 )
429 {
430 printf("New NAME LSA.....\n");
soamin15043622013-01-15 23:10:26 -0600431 //content_data=get_content_by_content_name(ccn_charbuf_as_string(uri));
Obaid Amine63022a2013-01-15 23:36:40 -0600432 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600433 printf("Content Data: %s \n",content_data);
434 process_incoming_sync_content_lsa(content_data);
435 }
436 else
437 {
438 printf("Name LSA / Newer Name LSA already xists in LSDB\n");
soamin15043622013-01-15 23:10:26 -0600439 //content_data=get_content_by_content_name(ccn_charbuf_as_string(uri));
Obaid Amine63022a2013-01-15 23:36:40 -0600440 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600441 printf("Content Data: %s \n",content_data);
442 }
akmhoque8fdd6412012-12-04 15:05:33 -0600443 }
444 else
445 {
akmhoque4ae16942012-12-10 11:50:43 -0600446 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
akmhoque8fdd6412012-12-04 15:05:33 -0600447 }
448 }
449 else if(ls_type == LS_TYPE_ADJ)
450 {
451 res=ccn_name_comp_get(content_name->buf, components,lsa_position+2,&origtime, &comp_size);
452 get_name_part(orig_router,content_name,components,2);
453 printf("Orig Time: %s\nOrig Router: %s\n",origtime,orig_router->name);
akmhoque8fdd6412012-12-04 15:05:33 -0600454
akmhoque4ae16942012-12-10 11:50:43 -0600455 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque8fdd6412012-12-04 15:05:33 -0600456
akmhoque4ae16942012-12-10 11:50:43 -0600457 //printf("Ls ID: %s\nOrig Time: %s\nOrig Router: %s\n",lsid,origtime,orig_router->name);
458
459 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) )
460 {
461 int is_new_adj_lsa=check_is_new_adj_lsa(orig_router->name,(char *)lst,(char *)origtime);
462 if ( is_new_adj_lsa == 1 )
463 {
464 printf("New Adj LSA.....\n");
soamin15043622013-01-15 23:10:26 -0600465 //content_data=get_content_by_content_name(ccn_charbuf_as_string(uri));
Obaid Amine63022a2013-01-15 23:36:40 -0600466 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600467 printf("Content Data: %s \n",content_data);
468 process_incoming_sync_content_lsa(content_data);
469 }
470 else
471 {
472
473 printf("Adj LSA / Newer Adj LSA already exists in LSDB\n");
Obaid Amine63022a2013-01-15 23:36:40 -0600474 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600475 printf("Content Data: %s \n",content_data);
476 }
477 }
478 else
479 {
480 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
481 }
akmhoque8fdd6412012-12-04 15:05:33 -0600482 }
483
Obaid Amine63022a2013-01-15 23:36:40 -0600484 if (content_data != NULL)
485 free(content_data);
akmhoque8fdd6412012-12-04 15:05:33 -0600486 ccn_charbuf_destroy(&uri);
akmhoque8fdd6412012-12-04 15:05:33 -0600487}
488
489int
490sync_callback(struct ccns_name_closure *nc,
491 struct ccn_charbuf *lhash,
492 struct ccn_charbuf *rhash,
493 struct ccn_charbuf *name)
494{
495
496
497 struct ccn_indexbuf cid={0};
498
499 struct ccn_indexbuf *components=&cid;
500 ccn_name_split (name, components);
501 ccn_name_chop(name,components,-3);
502
503 process_content_from_sync(name,components);
504
505 return(0);
506}
507
508
509void
510sync_monitor(char *topo_prefix, char *slice_prefix)
511{
512
513 static struct ccns_name_closure nc={0};
514
515 nlsr->closure = &nc;
516 struct ccn_charbuf *prefix = ccn_charbuf_create();
517 struct ccn_charbuf *roothash = NULL;
518 struct ccn_charbuf *topo = ccn_charbuf_create();
519 nlsr->slice = ccns_slice_create();
520 ccn_charbuf_reset(prefix);
521 ccn_charbuf_reset(topo);
522
523 ccn_charbuf_reset(prefix);
524 ccn_name_from_uri(prefix, slice_prefix);
525
526 ccn_charbuf_reset(topo);
527 ccn_name_from_uri(topo, topo_prefix);
528
529
530 ccns_slice_set_topo_prefix(nlsr->slice, topo, prefix);
Syed Obaid Amin3a3864b2013-01-09 03:05:50 -0600531 nlsr->closure->callback = &sync_cb;
532 //nlsr->closure->callback = &sync_callback;
akmhoque8fdd6412012-12-04 15:05:33 -0600533 nlsr->ccns = ccns_open(nlsr->ccn, nlsr->slice, nlsr->closure, roothash, NULL);
akmhoque8fdd6412012-12-04 15:05:33 -0600534}
535
536struct ccn_charbuf *
537make_template(int scope)
538{
539 struct ccn_charbuf *templ = NULL;
540 templ = ccn_charbuf_create();
541 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
542 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
543 ccn_charbuf_append_closer(templ); /* </Name> */
544 if (0 <= scope && scope <= 2)
545 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
546 ccn_charbuf_append_closer(templ); /* </Interest> */
547 return(templ);
548}
549
550void
551write_data_to_repo(char *data, char *name_prefix)
552{
akmhoque260698b2013-01-17 03:54:09 -0600553 if ( nlsr->debugging )
554 {
555 printf("write_data_to_repo called\n");
akmhoque47d19d12013-01-17 04:00:32 -0600556 printf("Content Name: %s \n",name_prefix);
akmhoque260698b2013-01-17 03:54:09 -0600557 printf("Content Data: %s \n",data);
558 }
akmhoque8fdd6412012-12-04 15:05:33 -0600559
560 struct ccn_charbuf *name = NULL;
561 struct ccn_seqwriter *w = NULL;
562 int blocksize = 1024;
563 int freshness = -1;
564 int torepo = 1;
565 int scope = 1;
566 int res;
567 size_t blockread;
568 struct ccn_charbuf *templ;
569
570 name = ccn_charbuf_create();
571 res = ccn_name_from_uri(name, name_prefix);
572 if (res < 0) {
573 fprintf(stderr, "bad CCN URI: %s\n",name_prefix);
574 exit(1);
575 }
576
577
578 w = ccn_seqw_create(nlsr->ccn, name);
579 if (w == NULL) {
580 fprintf(stderr, "ccn_seqw_create failed\n");
581 exit(1);
582 }
583 ccn_seqw_set_block_limits(w, blocksize, blocksize);
584 if (freshness > -1)
585 ccn_seqw_set_freshness(w, freshness);
586 if (torepo) {
587 struct ccn_charbuf *name_v = ccn_charbuf_create();
588 ccn_seqw_get_name(w, name_v);
589 ccn_name_from_uri(name_v, "%C1.R.sw");
590 ccn_name_append_nonce(name_v);
591 templ = make_template(scope);
592 res = ccn_get(nlsr->ccn, name_v, templ, 60000, NULL, NULL, NULL, 0);
593 ccn_charbuf_destroy(&templ);
594 ccn_charbuf_destroy(&name_v);
595 if (res < 0) {
596 fprintf(stderr, "No response from repository\n");
597 exit(1);
598 }
599 }
600
601
602
603
604 blockread = 0;
akmhoque260698b2013-01-17 03:54:09 -0600605
akmhoque8fdd6412012-12-04 15:05:33 -0600606
607 blockread=strlen(data);
608
609 if (blockread > 0) {
akmhoqued6cd61d2013-01-17 10:31:15 -0600610 //ccn_run(nlsr->ccn, 100);
akmhoque8fdd6412012-12-04 15:05:33 -0600611 res = ccn_seqw_write(w, data, blockread);
akmhoque260698b2013-01-17 03:54:09 -0600612 while (res == -1) {
akmhoqued6cd61d2013-01-17 10:31:15 -0600613 //ccn_run(nlsr->ccn, 100);
akmhoque260698b2013-01-17 03:54:09 -0600614 res = ccn_seqw_write(w, data, blockread);
615 }
akmhoque8fdd6412012-12-04 15:05:33 -0600616 }
617
akmhoque8fdd6412012-12-04 15:05:33 -0600618 ccn_seqw_close(w);
akmhoque260698b2013-01-17 03:54:09 -0600619 //ccn_run(nlsr->ccn, 1);
akmhoque8fdd6412012-12-04 15:05:33 -0600620 ccn_charbuf_destroy(&name);
621}
622
623
624int
625create_sync_slice(char *topo_prefix, char *slice_prefix)
626{
627 int res;
628 struct ccns_slice *slice;
629 struct ccn_charbuf *prefix = ccn_charbuf_create();
630 struct ccn_charbuf *topo = ccn_charbuf_create();
631 struct ccn_charbuf *clause = ccn_charbuf_create();
632 struct ccn_charbuf *slice_name = ccn_charbuf_create();
633 struct ccn_charbuf *slice_uri = ccn_charbuf_create();
634
635 if (prefix == NULL || topo == NULL || clause == NULL ||
636 slice_name == NULL || slice_uri == NULL) {
637 fprintf(stderr, "Unable to allocate required memory.\n");
638 exit(1);
639 }
640
641
642 slice = ccns_slice_create();
643
644 ccn_charbuf_reset(topo);
645 ccn_name_from_uri(topo, topo_prefix);
646 ccn_charbuf_reset(prefix);
647 ccn_name_from_uri(prefix,slice_prefix );
648 ccns_slice_set_topo_prefix(slice, topo, prefix);
649
650
651 res = ccns_write_slice(nlsr->ccn, slice, slice_name);
Syed Obaid Amin83eea122013-01-08 12:21:37 -0600652 /*
653// Obaid: commenting out the following lines to resolve a bug.
654// If commenting them can resolve the issue, then we
655// need to call them before terminating the program.
akmhoque8fdd6412012-12-04 15:05:33 -0600656 ccns_slice_destroy(&slice);
657 ccn_charbuf_destroy(&prefix);
658 ccn_charbuf_destroy(&topo);
659 ccn_charbuf_destroy(&clause);
660 ccn_charbuf_destroy(&slice_name);
661 ccn_charbuf_destroy(&slice_uri);
Syed Obaid Amin83eea122013-01-08 12:21:37 -0600662*/
akmhoque8fdd6412012-12-04 15:05:33 -0600663 return 0;
664}
665