blob: 7b228c597652bd16c7b30da00318265c179ca712 [file] [log] [blame]
akmhoquea0e71152013-02-11 09:47:59 -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"
31#include "utility.h"
32
33
34char *
35hex_string(unsigned char *s, size_t l)
36{
37 const char *hex_digits = "0123456789abcdef";
38 char *r;
39 int i;
40 r = calloc(1, 1 + 2 * l);
41 for (i = 0; i < l; i++) {
42 r[2*i] = hex_digits[(s[i]>>4) & 0xf];
43 r[1+2*i] = hex_digits[s[i] & 0xf];
44 }
45 return(r);
46}
47
48int
49sync_cb(struct ccns_name_closure *nc,
50 struct ccn_charbuf *lhash,
51 struct ccn_charbuf *rhash,
52 struct ccn_charbuf *name)
53{
54 char *hexL;
55 char *hexR;
56 int res;
57 struct ccn_charbuf *uri = ccn_charbuf_create();
58 if (lhash == NULL || lhash->length == 0) {
59 hexL = strdup("none");
60 } else
61 hexL = hex_string(lhash->buf, lhash->length);
62 if (rhash == NULL || rhash->length == 0) {
63 hexR = strdup("none");
64 } else
65 hexR = hex_string(rhash->buf, rhash->length);
66 if (name != NULL)
67 ccn_uri_append(uri, name->buf, name->length, 1);
68 else
69 ccn_charbuf_append_string(uri, "(null)");
70
71
72 if ( nlsr->debugging )
73 printf("Response from sync in the name: %s \n",ccn_charbuf_as_string(uri));
74
75 fflush(stdout);
76 free(hexL);
77 free(hexR);
78 ccn_charbuf_destroy(&uri);
79
80
81 //--Doing our thing from here
82 struct ccn_indexbuf cid={0};
83
84 struct ccn_indexbuf *components=&cid;
85 ccn_name_split (name, components);
86 //ccn_name_chop(name,components,-3);
87 //process_content_from_sync(name,components);
88
89 struct ccn_charbuf *content_name = ccn_charbuf_create();
90 ccn_name_init(content_name);
91 res = ccn_name_append_components(content_name, name->buf, components->buf[0], components->buf[components->n - 1]);
92
93 // debugging purpose
94 struct ccn_charbuf *temp=ccn_charbuf_create();
95 ccn_uri_append(temp, content_name->buf, content_name->length, 0);
96 if ( nlsr->debugging )
97 printf("Name before chopping: %s \n",ccn_charbuf_as_string(temp));
98 ccn_charbuf_destroy(&temp);
99
100 struct ccn_indexbuf cid1={0};
101 struct ccn_indexbuf *components1=&cid1;
102 res=ccn_name_split (content_name, components1);
103 if ( nlsr->debugging )
104 {
105 printf("Number of components in name = %d \n",res);
106 printf("Number of components in name as indexbuf->n = %d \n",(int)components1->n);
107 }
108 ccn_name_chop(content_name,components1,-3);
109 if ( nlsr->debugging )
110 printf("Number of components in name as indexbuf->n after chopping= %d \n",(int)components1->n);
111
112 //debugging purpose
113 struct ccn_charbuf *temp1=ccn_charbuf_create();
114 ccn_uri_append(temp1, content_name->buf, content_name->length, 0);
115 if ( nlsr->debugging )
116 printf("Name after chopping: %s \n",ccn_charbuf_as_string(temp1));
117 ccn_charbuf_destroy(&temp1);
118
119 process_content_from_sync(content_name,components1);
120 ccn_charbuf_destroy(&content_name);
121
122 return(0);
123}
124
125
126int
127get_lsa_position(struct ccn_charbuf * ccnb, struct ccn_indexbuf *comps)
128{
129
130
131
132 int res,i;
133 int lsa_position=0;
134 int name_comps=(int)comps->n;
135
136 for(i=0;i<name_comps;i++)
137 {
138 res=ccn_name_comp_strcmp(ccnb->buf,comps,i,"LSA");
139 if( res == 0)
140 {
141 lsa_position=i;
142 break;
143 }
144 }
145
146 return lsa_position;
147
148}
149
150void
151get_name_part(struct name_prefix *name_part,struct ccn_charbuf * interest_ccnb, struct ccn_indexbuf *interest_comps, int offset)
152{
153
154
155
156 int res,i;
157 int lsa_position=0;
158 int len=0;
159
160
161 struct ccn_indexbuf cid={0};
162 struct ccn_indexbuf *components=&cid;
163 struct ccn_charbuf *name=ccn_charbuf_create();
164 ccn_name_from_uri(name,nlsr->slice_prefix);
165 ccn_name_split (name, components);
166 lsa_position=components->n-2;
167
168 ccn_charbuf_destroy(&name);
169
170
171 const unsigned char *comp_ptr1;
172 size_t comp_size;
173 for(i=lsa_position+1+offset;i<interest_comps->n-1;i++)
174 {
175 res=ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1, &comp_size);
176 len+=1;
177 len+=(int)comp_size;
178 }
179 len++;
180
181 char *neighbor=(char *)malloc(len);
182 memset(neighbor,0,len);
183
184 for(i=lsa_position+1+offset; i<interest_comps->n-1;i++)
185 {
186 res=ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1, &comp_size);
187 memcpy(neighbor+strlen(neighbor),"/",1);
188 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,strlen((char *)comp_ptr1));
189
190 }
191
192 name_part->name=(char *)malloc(strlen(neighbor)+1);
193 memset(name_part->name,0,strlen(neighbor)+1);
194 memcpy(name_part->name,neighbor,strlen(neighbor)+1);
195 name_part->length=strlen(neighbor)+1;
196
197 // Add 01/31/2013
198 free(neighbor);
199}
200
201void
202get_host_name_from_command_string(struct name_prefix *name_part,char *nbr_name_uri, int offset)
203{
204
205
206
207 int res,i;
208 int len=0;
209 const unsigned char *comp_ptr1;
210 size_t comp_size;
211
212 struct ccn_charbuf *name=ccn_charbuf_create();
213 name = ccn_charbuf_create();
214 res = ccn_name_from_uri(name,nbr_name_uri);
215 if (res < 0) {
216 fprintf(stderr, "Bad ccn URI: %s\n", nbr_name_uri);
217 exit(1);
218 }
219
220 struct ccn_indexbuf cid={0};
221
222 struct ccn_indexbuf *components=&cid;
223 ccn_name_split (name, components);
224
225 for(i=components->n-2;i> (0+offset);i--)
226 {
227 res=ccn_name_comp_get(name->buf, components,i,&comp_ptr1, &comp_size);
228 len+=1;
229 len+=(int)comp_size;
230 }
231 len++;
232
233 char *neighbor=(char *)malloc(len);
234 memset(neighbor,0,len);
235
236 for(i=components->n-2;i> (0+offset);i--)
237 {
238 res=ccn_name_comp_get(name->buf, components,i,&comp_ptr1, &comp_size);
239 if ( i != components->n-2)
240 memcpy(neighbor+strlen(neighbor),".",1);
241 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,strlen((char *)comp_ptr1));
242
243 }
244
245 name_part->name=(char *)malloc(strlen(neighbor)+1);
246 memset(name_part->name,0,strlen(neighbor)+1);
247 memcpy(name_part->name,neighbor,strlen(neighbor)+1);
248 name_part->length=strlen(neighbor)+1;
249
250 // 01/31/2013
251 free(neighbor);
252 ccn_charbuf_destroy(&name);
253}
254
255
256
257void
258get_content_by_content_name(char *content_name, unsigned char **content_data)
259{
260
261 struct ccn_charbuf *name = NULL;
262 struct ccn_charbuf *templ = NULL;
263 struct ccn_charbuf *resultbuf = NULL;
264 struct ccn_parsed_ContentObject pcobuf = { 0 };
265 int res;
266 int allow_stale = 0;
267 int content_only = 1;
268 int scope = -1;
269 const unsigned char *ptr;
270 size_t length;
271 int resolve_version = CCN_V_HIGHEST;
272 int timeout_ms = 3000;
273 const unsigned lifetime_default = CCN_INTEREST_LIFETIME_SEC << 12;
274 unsigned lifetime_l12 = lifetime_default;
275 int get_flags = 0;
276
277 name = ccn_charbuf_create();
278 res = ccn_name_from_uri(name,content_name);
279 if (res < 0) {
280 fprintf(stderr, "Bad ccn URI: %s\n", content_name);
281 exit(1);
282 }
283
284 if (allow_stale || lifetime_l12 != lifetime_default || scope != -1) {
285 templ = ccn_charbuf_create();
286 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
287 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
288 ccn_charbuf_append_closer(templ); /* </Name> */
289 if (allow_stale) {
290 ccn_charbuf_append_tt(templ, CCN_DTAG_AnswerOriginKind, CCN_DTAG);
291 ccnb_append_number(templ,
292 CCN_AOK_DEFAULT | CCN_AOK_STALE);
293 ccn_charbuf_append_closer(templ); /* </AnswerOriginKind> */
294 }
295 if (scope != -1) {
296 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
297 }
298 if (lifetime_l12 != lifetime_default) {
299 /*
300 * Choose the interest lifetime so there are at least 3
301 * expressions (in the unsatisfied case).
302 */
303 unsigned char buf[3] = { 0 };
304 int i;
305 for (i = sizeof(buf) - 1; i >= 0; i--, lifetime_l12 >>= 8)
306 buf[i] = lifetime_l12 & 0xff;
307 ccnb_append_tagged_blob(templ, CCN_DTAG_InterestLifetime, buf, sizeof(buf));
308 }
309 ccn_charbuf_append_closer(templ); /* </Interest> */
310 }
311 resultbuf = ccn_charbuf_create();
312 if (resolve_version != 0) {
313 res = ccn_resolve_version(nlsr->ccn, name, resolve_version, 500);
314 if (res >= 0) {
315 ccn_uri_append(resultbuf, name->buf, name->length, 1);
316 resultbuf->length = 0;
317 }
318 }
319 res = ccn_get(nlsr->ccn, name, templ, timeout_ms, resultbuf, &pcobuf, NULL, get_flags);
320 if (res >= 0) {
321 ptr = resultbuf->buf;
322 length = resultbuf->length;
323 if (content_only){
324 ccn_content_get_value(ptr, length, &pcobuf, &ptr, &length);
325 *content_data = (unsigned char *) calloc(length, sizeof(char *));
326 memcpy (*content_data, ptr, length);
327 }
328 }
329 ccn_charbuf_destroy(&resultbuf);
330 ccn_charbuf_destroy(&templ);
331 ccn_charbuf_destroy(&name);
332}
333
334void
335process_incoming_sync_content_lsa( unsigned char *content_data)
336{
337
338
339 if ( nlsr->debugging )
340 printf("process_incoming_sync_content_lsa called \n");
341
342 char *sep="|";
343 char *rem;
344 char *orig_router;
345 char *orl;
346 int orig_router_length;
347 char *lst;
348 int ls_type;
349 char *lsid;
350 long int ls_id;
351 char *isvld;
352 int isValid;
353 char *num_link;
354 int no_link;
355 char *np;
356 char *np_length;
357 int name_length;
358 char *data;
359 char *orig_time;
360
361
362 if ( nlsr->debugging )
363 printf("LSA Data \n");
364
365 if( strlen((char *)content_data ) > 0 )
366 {
367
368 orig_router=strtok_r((char *)content_data,sep,&rem);
369 orl=strtok_r(NULL,sep,&rem);
370 orig_router_length=atoi(orl);
371
372 if ( nlsr->debugging )
373 {
374 printf(" Orig Router Name : %s\n",orig_router);
375 printf(" Orig Router Length: %d\n",orig_router_length);
376 }
377
378 lst=strtok_r(NULL,sep,&rem);
379 ls_type=atoi(lst);
380
381 if ( nlsr->debugging )
382 printf(" LS Type : %d\n",ls_type);
383
384 if ( ls_type == LS_TYPE_NAME )
385 {
386 lsid=strtok_r(NULL,sep,&rem);
387 ls_id=atoi(lsid);
388 orig_time=strtok_r(NULL,sep,&rem);
389 isvld=strtok_r(NULL,sep,&rem);
390 isValid=atoi(isvld);
391 np=strtok_r(NULL,sep,&rem);
392 np_length=strtok_r(NULL,sep,&rem);
393 name_length=atoi(np_length);
394 if ( nlsr->debugging )
395 {
396 printf(" LS ID : %ld\n",ls_id);
397 printf(" isValid : %d\n",isValid);
398 printf(" Name Prefix : %s\n",np);
399 printf(" Orig Time : %s\n",orig_time);
400 printf(" Name Prefix length: %d\n",name_length);
401 }
402
403 build_and_install_others_name_lsa(orig_router,ls_type,ls_id,orig_time,isValid,np);
404
405 print_name_lsdb();
406
407 }
408 else if ( ls_type == LS_TYPE_ADJ )
409 {
410 orig_time=strtok_r(NULL,sep,&rem);
411 num_link=strtok_r(NULL,sep,&rem);
412 no_link=atoi(num_link);
413 data=rem;
414
415 if ( nlsr->debugging )
416 {
417 printf(" Orig Time : %s\n",orig_time);
418 printf(" No Link : %d\n",no_link);
419 printf(" Data : %s\n",data);
420 }
421 build_and_install_others_adj_lsa(orig_router,ls_type,orig_time,no_link,data);
422 }
423 else if ( ls_type == LS_TYPE_COR )
424 {
425 orig_time=strtok_r(NULL,sep,&rem);
426 char *cor_r=strtok_r(NULL,sep,&rem);
427 char *cor_theta=strtok_r(NULL,sep,&rem);
428
429 double r, theta;
430 r=strtof(cor_r,NULL);
431 theta=strtof(cor_theta,NULL);
432
433 if ( nlsr->debugging )
434 {
435 printf(" Orig Time : %s\n",orig_time);
436 printf(" Cor R : %f\n",r);
437 printf(" Cor Theta : %f\n",theta);
438 }
439 build_and_install_others_cor_lsa(orig_router,ls_type,orig_time, (double)r, (double)theta);
440 }
441
442 }
443}
444
445void
446process_content_from_sync(struct ccn_charbuf *content_name, struct ccn_indexbuf *components)
447{
448 //int lsa_position;
449 int res;
450 size_t comp_size;
451 char *lst;
452 char *lsid;
453 const unsigned char *second_last_comp;
454 const unsigned char *third_last_comp;
455 const unsigned char *origtime;
456 char *sep=".";
457 char *rem;
458 char *second_comp_type;
459
460 int ls_type;
461 long int ls_id=0;
462
463 unsigned char *content_data = NULL;
464
465 char *time_stamp=(char *)malloc(20);
466 memset(time_stamp,0,20);
467 get_current_timestamp_micro(time_stamp);
468
469 struct ccn_charbuf *uri = ccn_charbuf_create();
470 ccn_uri_append(uri, content_name->buf, content_name->length, 0);
471
472 struct name_prefix *orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix));
473
474
475 struct ccn_indexbuf cid={0};
476 struct ccn_indexbuf *temp_components=&cid;
477 struct ccn_charbuf *name=ccn_charbuf_create();
478 ccn_name_from_uri(name,nlsr->slice_prefix);
479 ccn_name_split (name, temp_components);
480 //lsa_position=temp_components->n-2;
481 ccn_charbuf_destroy(&name);
482
483
484 //res=ccn_name_comp_get(content_name->buf, components,lsa_position+1,&lst, &comp_size);
485 res=ccn_name_comp_get(content_name->buf, components,components->n-2-1,&second_last_comp, &comp_size);
486 //ls_type=atoi((char *)lst);
487
488 printf("2nd Last Component: %s \n",second_last_comp);
489 second_comp_type=strtok_r((char *)second_last_comp,sep,&rem);
490 if ( strcmp( second_comp_type, "lsId" ) == 0 )
491 {
492 lsid=rem;
493 ls_id=atoi(rem);
494 res=ccn_name_comp_get(content_name->buf, components,components->n-2-2,&third_last_comp, &comp_size);
495 lst=strtok_r((char *)third_last_comp,sep,&rem);
496 lst=rem;
497 ls_type=atoi(lst);
498 res=ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size);
499 ccn_name_chop(content_name,components,-3);
500 get_name_part(orig_router,content_name,components,0);
501
502 if ( nlsr->debugging )
503 printf("Orig Router: %s Ls Type: %d Ls id: %ld Orig Time: %s\n",orig_router->name,ls_type,ls_id,origtime);
504
505 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
506
507 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) )
508 {
509 int is_new_name_lsa=check_is_new_name_lsa(orig_router->name,(char *)lst,(char *)lsid,(char *)origtime);
510 if ( is_new_name_lsa == 1 )
511 {
512 if ( nlsr->debugging )
513 printf("New NAME LSA.....\n");
514 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
515 if ( nlsr->debugging )
516 printf("Content Data: %s \n",content_data);
517 process_incoming_sync_content_lsa(content_data);
518 }
519 else
520 {
521 if ( nlsr->debugging )
522 printf("Name LSA / Newer Name LSA already xists in LSDB\n");
523 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
524
525 if ( nlsr->debugging )
526 printf("Content Data: %s \n",content_data);
527 }
528 }
529 else
530 {
531 if ( nlsr->debugging )
532 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
533 }
534 }
535 else
536 {
537 ls_type=atoi(rem);
538 lst=rem;
539 if(ls_type == LS_TYPE_ADJ)
540 {
541 res=ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size);
542 ccn_name_chop(content_name,components,-2);
543 get_name_part(orig_router,content_name,components,0);
544
545 if ( nlsr->debugging )
546 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",orig_router->name,ls_type,origtime);
547
548 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
549 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) )
550 {
551 int is_new_adj_lsa=check_is_new_adj_lsa(orig_router->name,(char *)lst,(char *)origtime);
552 if ( is_new_adj_lsa == 1 )
553 {
554 if ( nlsr->debugging )
555 printf("New Adj LSA.....\n");
556 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
557
558 if ( nlsr->debugging )
559 printf("Content Data: %s \n",content_data);
560 process_incoming_sync_content_lsa(content_data);
561 }
562 else
563 {
564 if ( nlsr->debugging )
565 printf("Adj LSA / Newer Adj LSA already exists in LSDB\n");
566 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
567 if ( nlsr->debugging )
568 printf("Content Data: %s \n",content_data);
569 }
570 }
571 else
572 {
573 if ( nlsr->debugging )
574 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
575 }
576 }
577 else if(ls_type == LS_TYPE_COR)
578 {
579 res=ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size);
580 ccn_name_chop(content_name,components,-2);
581 get_name_part(orig_router,content_name,components,0);
582
583 if ( nlsr->debugging )
584 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",orig_router->name,ls_type,origtime);
585
586 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
587 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) )
588 {
589 int is_new_cor_lsa=check_is_new_cor_lsa(orig_router->name,(char *)lst,(char *)origtime);
590 if ( is_new_cor_lsa == 1 )
591 {
592 if ( nlsr->debugging )
593 printf("New Cor LSA.....\n");
594 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
595
596 if ( nlsr->debugging )
597 printf("Content Data: %s \n",content_data);
598 process_incoming_sync_content_lsa(content_data);
599 }
600 else
601 {
602 if ( nlsr->debugging )
603 printf("Cor LSA / Newer Cor LSA already exists in LSDB\n");
604 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
605 if ( nlsr->debugging )
606 printf("Content Data: %s \n",content_data);
607 }
608 }
609 else
610 {
611 if ( nlsr->debugging )
612 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
613 }
614
615 }
616 }
617
618 if (content_data != NULL)
619 free(content_data);
620 ccn_charbuf_destroy(&uri);
621 //01/31/2013
622 free(time_stamp);
623}
624
625void
626sync_monitor(char *topo_prefix, char *slice_prefix)
627{
628
629 static struct ccns_name_closure nc={0};
630
631 nlsr->closure = &nc;
632 struct ccn_charbuf *prefix = ccn_charbuf_create();
633 struct ccn_charbuf *roothash = NULL;
634 struct ccn_charbuf *topo = ccn_charbuf_create();
635 nlsr->slice = ccns_slice_create();
636 ccn_charbuf_reset(prefix);
637 ccn_charbuf_reset(topo);
638
639 ccn_charbuf_reset(prefix);
640 ccn_name_from_uri(prefix, slice_prefix);
641
642 ccn_charbuf_reset(topo);
643 ccn_name_from_uri(topo, topo_prefix);
644
645
646 ccns_slice_set_topo_prefix(nlsr->slice, topo, prefix);
647 nlsr->closure->callback = &sync_cb;
648 nlsr->ccns = ccns_open(nlsr->ccn, nlsr->slice, nlsr->closure, roothash, NULL);
649
650 //01/31/2013
651 ccn_charbuf_destroy(&prefix);
652 ccn_charbuf_destroy(&topo);
653}
654
655struct ccn_charbuf *
656make_template(int scope)
657{
658 struct ccn_charbuf *templ = NULL;
659 templ = ccn_charbuf_create();
660 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
661 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
662 ccn_charbuf_append_closer(templ); /* </Name> */
663 if (0 <= scope && scope <= 2)
664 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
665 ccn_charbuf_append_closer(templ); /* </Interest> */
666 return(templ);
667}
668
669int
670write_data_to_repo(char *data, char *name_prefix)
671{
672 if ( nlsr->debugging )
673 {
674 printf("write_data_to_repo called\n");
675 printf("Content Name: %s \n",name_prefix);
676 printf("Content Data: %s \n",data);
677 }
678
679 struct ccn *temp_ccn;
680 temp_ccn=ccn_create();
681 int ccn_fd=ccn_connect(temp_ccn, NULL);
682 if(ccn_fd == -1)
683 {
684 fprintf(stderr,"Could not connect to ccnd for Data Writing\n");
685 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd for Data Writing\n");
686 return -1;
687 }
688 struct ccn_charbuf *name = NULL;
689 struct ccn_seqwriter *w = NULL;
690 int blocksize = 4096;
691 int freshness = -1;
692 int torepo = 1;
693 int scope = 1;
694 int res;
695 size_t blockread;
696 struct ccn_charbuf *templ;
697
698 name = ccn_charbuf_create();
699 res = ccn_name_from_uri(name, name_prefix);
700 if (res < 0) {
701 fprintf(stderr, "bad CCN URI: %s\n",name_prefix);
702 return -1;
703 }
704
705
706 w = ccn_seqw_create(temp_ccn, name);
707 if (w == NULL) {
708 fprintf(stderr, "ccn_seqw_create failed\n");
709 return -1;
710 }
711 ccn_seqw_set_block_limits(w, blocksize, blocksize);
712 if (freshness > -1)
713 ccn_seqw_set_freshness(w, freshness);
714 if (torepo) {
715 struct ccn_charbuf *name_v = ccn_charbuf_create();
716 ccn_seqw_get_name(w, name_v);
717 ccn_name_from_uri(name_v, "%C1.R.sw");
718 ccn_name_append_nonce(name_v);
719 templ = make_template(scope);
720 res = ccn_get(temp_ccn, name_v, templ, 60000, NULL, NULL, NULL, 0);
721 ccn_charbuf_destroy(&templ);
722 ccn_charbuf_destroy(&name_v);
723 if (res < 0) {
724 fprintf(stderr, "No response from repository\n");
725 return -1;
726 }
727 }
728
729
730
731
732 blockread = 0;
733
734
735 blockread=strlen(data);
736
737 if (blockread > 0) {
738 ccn_run(temp_ccn, 100);
739 res = ccn_seqw_write(w, data, blockread);
740 while (res == -1) {
741 ccn_run(temp_ccn, 100);
742 res = ccn_seqw_write(w, data, blockread);
743 }
744 }
745
746 ccn_seqw_close(w);
747 ccn_run(temp_ccn, 100);
748 ccn_charbuf_destroy(&name);
749 ccn_destroy(&temp_ccn);
750
751 return 0;
752}
753
754
755int
756create_sync_slice(char *topo_prefix, char *slice_prefix)
757{
758 int res;
759 struct ccns_slice *slice;
760 struct ccn_charbuf *prefix = ccn_charbuf_create();
761 struct ccn_charbuf *topo = ccn_charbuf_create();
762 struct ccn_charbuf *clause = ccn_charbuf_create();
763 struct ccn_charbuf *slice_name = ccn_charbuf_create();
764 struct ccn_charbuf *slice_uri = ccn_charbuf_create();
765
766 if (prefix == NULL || topo == NULL || clause == NULL ||
767 slice_name == NULL || slice_uri == NULL) {
768 fprintf(stderr, "Unable to allocate required memory.\n");
769 return -1;
770 }
771
772
773 slice = ccns_slice_create();
774
775 ccn_charbuf_reset(topo);
776 ccn_name_from_uri(topo, topo_prefix);
777 ccn_charbuf_reset(prefix);
778 ccn_name_from_uri(prefix,slice_prefix );
779 ccns_slice_set_topo_prefix(slice, topo, prefix);
780
781
782 res = ccns_write_slice(nlsr->ccn, slice, slice_name);
783
784 //01/31/2013
785 ccns_slice_destroy(&slice);
786 ccn_charbuf_destroy(&prefix);
787 ccn_charbuf_destroy(&topo);
788 ccn_charbuf_destroy(&clause);
789 ccn_charbuf_destroy(&slice_name);
790 ccn_charbuf_destroy(&slice_uri);
791
792 return 0;
793}
794