blob: 34ef71a6604ac33c914f539d54c2f4fb19e1dc33 [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);
akmhoque4ae16942012-12-10 11:50:43 -0600436 printf("Content Data: %s \n",content_data);
437 process_incoming_sync_content_lsa(content_data);
438 }
439 else
440 {
a64adam04be6482013-01-17 11:29:32 -0600441 if ( nlsr->debugging )
442 printf("Name LSA / Newer Name LSA already xists in LSDB\n");
soamin15043622013-01-15 23:10:26 -0600443 //content_data=get_content_by_content_name(ccn_charbuf_as_string(uri));
Obaid Amine63022a2013-01-15 23:36:40 -0600444 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
a64adam04be6482013-01-17 11:29:32 -0600445
446 if ( nlsr->debugging )
447 printf("Content Data: %s \n",content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600448 }
akmhoque8fdd6412012-12-04 15:05:33 -0600449 }
450 else
451 {
a64adam04be6482013-01-17 11:29:32 -0600452 if ( nlsr->debugging )
453 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
akmhoque8fdd6412012-12-04 15:05:33 -0600454 }
455 }
456 else if(ls_type == LS_TYPE_ADJ)
457 {
458 res=ccn_name_comp_get(content_name->buf, components,lsa_position+2,&origtime, &comp_size);
459 get_name_part(orig_router,content_name,components,2);
a64adam04be6482013-01-17 11:29:32 -0600460
461 if ( nlsr->debugging )
462 printf("Orig Time: %s\nOrig Router: %s\n",origtime,orig_router->name);
akmhoque8fdd6412012-12-04 15:05:33 -0600463
akmhoque4ae16942012-12-10 11:50:43 -0600464 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque8fdd6412012-12-04 15:05:33 -0600465
akmhoque4ae16942012-12-10 11:50:43 -0600466 //printf("Ls ID: %s\nOrig Time: %s\nOrig Router: %s\n",lsid,origtime,orig_router->name);
467
468 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) )
469 {
470 int is_new_adj_lsa=check_is_new_adj_lsa(orig_router->name,(char *)lst,(char *)origtime);
471 if ( is_new_adj_lsa == 1 )
472 {
a64adam04be6482013-01-17 11:29:32 -0600473 if ( nlsr->debugging )
474 printf("New Adj LSA.....\n");
soamin15043622013-01-15 23:10:26 -0600475 //content_data=get_content_by_content_name(ccn_charbuf_as_string(uri));
Obaid Amine63022a2013-01-15 23:36:40 -0600476 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
a64adam04be6482013-01-17 11:29:32 -0600477
478 if ( nlsr->debugging )
479 printf("Content Data: %s \n",content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600480 process_incoming_sync_content_lsa(content_data);
481 }
482 else
483 {
a64adam04be6482013-01-17 11:29:32 -0600484 if ( nlsr->debugging )
485 printf("Adj LSA / Newer Adj LSA already exists in LSDB\n");
Obaid Amine63022a2013-01-15 23:36:40 -0600486 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
a64adam04be6482013-01-17 11:29:32 -0600487 if ( nlsr->debugging )
488 printf("Content Data: %s \n",content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600489 }
490 }
491 else
492 {
a64adam04be6482013-01-17 11:29:32 -0600493 if ( nlsr->debugging )
494 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
akmhoque4ae16942012-12-10 11:50:43 -0600495 }
akmhoque8fdd6412012-12-04 15:05:33 -0600496 }
497
Obaid Amine63022a2013-01-15 23:36:40 -0600498 if (content_data != NULL)
499 free(content_data);
akmhoque8fdd6412012-12-04 15:05:33 -0600500 ccn_charbuf_destroy(&uri);
akmhoque8fdd6412012-12-04 15:05:33 -0600501}
502
503int
504sync_callback(struct ccns_name_closure *nc,
505 struct ccn_charbuf *lhash,
506 struct ccn_charbuf *rhash,
507 struct ccn_charbuf *name)
508{
509
510
511 struct ccn_indexbuf cid={0};
512
513 struct ccn_indexbuf *components=&cid;
514 ccn_name_split (name, components);
515 ccn_name_chop(name,components,-3);
516
517 process_content_from_sync(name,components);
518
519 return(0);
520}
521
522
523void
524sync_monitor(char *topo_prefix, char *slice_prefix)
525{
526
527 static struct ccns_name_closure nc={0};
528
529 nlsr->closure = &nc;
530 struct ccn_charbuf *prefix = ccn_charbuf_create();
531 struct ccn_charbuf *roothash = NULL;
532 struct ccn_charbuf *topo = ccn_charbuf_create();
533 nlsr->slice = ccns_slice_create();
534 ccn_charbuf_reset(prefix);
535 ccn_charbuf_reset(topo);
536
537 ccn_charbuf_reset(prefix);
538 ccn_name_from_uri(prefix, slice_prefix);
539
540 ccn_charbuf_reset(topo);
541 ccn_name_from_uri(topo, topo_prefix);
542
543
544 ccns_slice_set_topo_prefix(nlsr->slice, topo, prefix);
Syed Obaid Amin3a3864b2013-01-09 03:05:50 -0600545 nlsr->closure->callback = &sync_cb;
546 //nlsr->closure->callback = &sync_callback;
akmhoque8fdd6412012-12-04 15:05:33 -0600547 nlsr->ccns = ccns_open(nlsr->ccn, nlsr->slice, nlsr->closure, roothash, NULL);
akmhoque8fdd6412012-12-04 15:05:33 -0600548}
549
550struct ccn_charbuf *
551make_template(int scope)
552{
553 struct ccn_charbuf *templ = NULL;
554 templ = ccn_charbuf_create();
555 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
556 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
557 ccn_charbuf_append_closer(templ); /* </Name> */
558 if (0 <= scope && scope <= 2)
559 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
560 ccn_charbuf_append_closer(templ); /* </Interest> */
561 return(templ);
562}
563
564void
565write_data_to_repo(char *data, char *name_prefix)
566{
akmhoque260698b2013-01-17 03:54:09 -0600567 if ( nlsr->debugging )
568 {
569 printf("write_data_to_repo called\n");
akmhoque47d19d12013-01-17 04:00:32 -0600570 printf("Content Name: %s \n",name_prefix);
akmhoque260698b2013-01-17 03:54:09 -0600571 printf("Content Data: %s \n",data);
572 }
akmhoque8fdd6412012-12-04 15:05:33 -0600573
574 struct ccn_charbuf *name = NULL;
575 struct ccn_seqwriter *w = NULL;
576 int blocksize = 1024;
577 int freshness = -1;
578 int torepo = 1;
579 int scope = 1;
580 int res;
581 size_t blockread;
582 struct ccn_charbuf *templ;
583
584 name = ccn_charbuf_create();
585 res = ccn_name_from_uri(name, name_prefix);
586 if (res < 0) {
587 fprintf(stderr, "bad CCN URI: %s\n",name_prefix);
588 exit(1);
589 }
590
591
592 w = ccn_seqw_create(nlsr->ccn, name);
593 if (w == NULL) {
594 fprintf(stderr, "ccn_seqw_create failed\n");
595 exit(1);
596 }
597 ccn_seqw_set_block_limits(w, blocksize, blocksize);
598 if (freshness > -1)
599 ccn_seqw_set_freshness(w, freshness);
600 if (torepo) {
601 struct ccn_charbuf *name_v = ccn_charbuf_create();
602 ccn_seqw_get_name(w, name_v);
603 ccn_name_from_uri(name_v, "%C1.R.sw");
604 ccn_name_append_nonce(name_v);
605 templ = make_template(scope);
606 res = ccn_get(nlsr->ccn, name_v, templ, 60000, NULL, NULL, NULL, 0);
607 ccn_charbuf_destroy(&templ);
608 ccn_charbuf_destroy(&name_v);
609 if (res < 0) {
610 fprintf(stderr, "No response from repository\n");
611 exit(1);
612 }
613 }
614
615
616
617
618 blockread = 0;
akmhoque260698b2013-01-17 03:54:09 -0600619
akmhoque8fdd6412012-12-04 15:05:33 -0600620
621 blockread=strlen(data);
622
623 if (blockread > 0) {
akmhoqued6cd61d2013-01-17 10:31:15 -0600624 //ccn_run(nlsr->ccn, 100);
akmhoque8fdd6412012-12-04 15:05:33 -0600625 res = ccn_seqw_write(w, data, blockread);
akmhoque260698b2013-01-17 03:54:09 -0600626 while (res == -1) {
akmhoqued6cd61d2013-01-17 10:31:15 -0600627 //ccn_run(nlsr->ccn, 100);
akmhoque260698b2013-01-17 03:54:09 -0600628 res = ccn_seqw_write(w, data, blockread);
629 }
akmhoque8fdd6412012-12-04 15:05:33 -0600630 }
631
akmhoque8fdd6412012-12-04 15:05:33 -0600632 ccn_seqw_close(w);
akmhoque260698b2013-01-17 03:54:09 -0600633 //ccn_run(nlsr->ccn, 1);
akmhoque8fdd6412012-12-04 15:05:33 -0600634 ccn_charbuf_destroy(&name);
635}
636
637
638int
639create_sync_slice(char *topo_prefix, char *slice_prefix)
640{
641 int res;
642 struct ccns_slice *slice;
643 struct ccn_charbuf *prefix = ccn_charbuf_create();
644 struct ccn_charbuf *topo = ccn_charbuf_create();
645 struct ccn_charbuf *clause = ccn_charbuf_create();
646 struct ccn_charbuf *slice_name = ccn_charbuf_create();
647 struct ccn_charbuf *slice_uri = ccn_charbuf_create();
648
649 if (prefix == NULL || topo == NULL || clause == NULL ||
650 slice_name == NULL || slice_uri == NULL) {
651 fprintf(stderr, "Unable to allocate required memory.\n");
652 exit(1);
653 }
654
655
656 slice = ccns_slice_create();
657
658 ccn_charbuf_reset(topo);
659 ccn_name_from_uri(topo, topo_prefix);
660 ccn_charbuf_reset(prefix);
661 ccn_name_from_uri(prefix,slice_prefix );
662 ccns_slice_set_topo_prefix(slice, topo, prefix);
663
664
665 res = ccns_write_slice(nlsr->ccn, slice, slice_name);
Syed Obaid Amin83eea122013-01-08 12:21:37 -0600666 /*
667// Obaid: commenting out the following lines to resolve a bug.
668// If commenting them can resolve the issue, then we
669// need to call them before terminating the program.
akmhoque8fdd6412012-12-04 15:05:33 -0600670 ccns_slice_destroy(&slice);
671 ccn_charbuf_destroy(&prefix);
672 ccn_charbuf_destroy(&topo);
673 ccn_charbuf_destroy(&clause);
674 ccn_charbuf_destroy(&slice_name);
675 ccn_charbuf_destroy(&slice_uri);
Syed Obaid Amin83eea122013-01-08 12:21:37 -0600676*/
akmhoque8fdd6412012-12-04 15:05:33 -0600677 return 0;
678}
679