blob: 2a77331b8c79924052d3af6bf6a58b096a247639 [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;
Obaid Amin512c08f2013-01-17 14:11:59 -060099 int lsa_position=0; //Obaid: Hoque make it -1, 0 is also an index
100 // I didn't change it as it might break the code.
akmhoque8fdd6412012-12-04 15:05:33 -0600101 int name_comps=(int)comps->n;
102
103 for(i=0;i<name_comps;i++)
104 {
105 res=ccn_name_comp_strcmp(ccnb->buf,comps,i,"LSA");
106 if( res == 0)
107 {
108 lsa_position=i;
109 break;
110 }
111 }
112
113 return lsa_position;
114
115}
116
117void
118get_name_part(struct name_prefix *name_part,struct ccn_charbuf * interest_ccnb, struct ccn_indexbuf *interest_comps, int offset)
119{
120
121
122
123 int res,i;
124 int lsa_position=0;
125 int len=0;
126
127 lsa_position=get_lsa_position(interest_ccnb,interest_comps);
128
129 const unsigned char *comp_ptr1;
130 size_t comp_size;
131 for(i=lsa_position+1+offset;i<interest_comps->n-1;i++)
132 {
133 res=ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1, &comp_size);
134 len+=1;
135 len+=(int)comp_size;
136 }
137 len++;
138
139 char *neighbor=(char *)malloc(len);
140 memset(neighbor,0,len);
141
142 for(i=lsa_position+1+offset; i<interest_comps->n-1;i++)
143 {
144 res=ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1, &comp_size);
145 memcpy(neighbor+strlen(neighbor),"/",1);
146 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,strlen((char *)comp_ptr1));
147
148 }
149
150 name_part->name=(char *)malloc(strlen(neighbor)+1);
151 memset(name_part->name,0,strlen(neighbor)+1);
152 memcpy(name_part->name,neighbor,strlen(neighbor)+1);
153 name_part->length=strlen(neighbor)+1;
154
155
156}
157
akmhoque09c0afa2012-12-14 09:27:00 -0600158void
159get_host_name_from_command_string(struct name_prefix *name_part,char *nbr_name_uri, int offset)
160{
161
162
163
164 int res,i;
165 int len=0;
166 const unsigned char *comp_ptr1;
167 size_t comp_size;
168
169 struct ccn_charbuf *name=ccn_charbuf_create();
170 name = ccn_charbuf_create();
171 res = ccn_name_from_uri(name,nbr_name_uri);
172 if (res < 0) {
173 fprintf(stderr, "Bad ccn URI: %s\n", nbr_name_uri);
174 exit(1);
175 }
176
177 struct ccn_indexbuf cid={0};
178
179 struct ccn_indexbuf *components=&cid;
180 ccn_name_split (name, components);
181
182 for(i=components->n-2;i> (0+offset);i--)
183 {
184 res=ccn_name_comp_get(name->buf, components,i,&comp_ptr1, &comp_size);
185 len+=1;
186 len+=(int)comp_size;
187 }
188 len++;
189
190 char *neighbor=(char *)malloc(len);
191 memset(neighbor,0,len);
192
193 for(i=components->n-2;i> (0+offset);i--)
194 {
195 res=ccn_name_comp_get(name->buf, components,i,&comp_ptr1, &comp_size);
196 if ( i != components->n-2)
197 memcpy(neighbor+strlen(neighbor),".",1);
198 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,strlen((char *)comp_ptr1));
199
200 }
201
202 name_part->name=(char *)malloc(strlen(neighbor)+1);
203 memset(name_part->name,0,strlen(neighbor)+1);
204 memcpy(name_part->name,neighbor,strlen(neighbor)+1);
205 name_part->length=strlen(neighbor)+1;
206
207
208}
209
akmhoque8fdd6412012-12-04 15:05:33 -0600210
soamin15043622013-01-15 23:10:26 -0600211//char *
212void
Obaid Amine63022a2013-01-15 23:36:40 -0600213get_content_by_content_name(char *content_name, unsigned char **content_data)
akmhoque8fdd6412012-12-04 15:05:33 -0600214{
215
soamin15043622013-01-15 23:10:26 -0600216 struct ccn_charbuf *name = NULL;
217 struct ccn_charbuf *templ = NULL;
218 struct ccn_charbuf *resultbuf = NULL;
219 struct ccn_parsed_ContentObject pcobuf = { 0 };
220 int res;
221 int allow_stale = 0;
222 int content_only = 1;
223 int scope = -1;
224 const unsigned char *ptr;
225 size_t length;
226 int resolve_version = CCN_V_HIGHEST;
227 int timeout_ms = 3000;
228 const unsigned lifetime_default = CCN_INTEREST_LIFETIME_SEC << 12;
229 unsigned lifetime_l12 = lifetime_default;
230 int get_flags = 0;
akmhoque8fdd6412012-12-04 15:05:33 -0600231
soamin15043622013-01-15 23:10:26 -0600232 name = ccn_charbuf_create();
233 res = ccn_name_from_uri(name,content_name);
234 if (res < 0) {
235 fprintf(stderr, "Bad ccn URI: %s\n", content_name);
236 exit(1);
237 }
238
akmhoque8fdd6412012-12-04 15:05:33 -0600239 if (allow_stale || lifetime_l12 != lifetime_default || scope != -1) {
soamin15043622013-01-15 23:10:26 -0600240 templ = ccn_charbuf_create();
241 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
242 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
243 ccn_charbuf_append_closer(templ); /* </Name> */
akmhoque8fdd6412012-12-04 15:05:33 -0600244 if (allow_stale) {
245 ccn_charbuf_append_tt(templ, CCN_DTAG_AnswerOriginKind, CCN_DTAG);
246 ccnb_append_number(templ,
soamin15043622013-01-15 23:10:26 -0600247 CCN_AOK_DEFAULT | CCN_AOK_STALE);
akmhoque8fdd6412012-12-04 15:05:33 -0600248 ccn_charbuf_append_closer(templ); /* </AnswerOriginKind> */
249 }
soamin15043622013-01-15 23:10:26 -0600250 if (scope != -1) {
251 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
252 }
akmhoque8fdd6412012-12-04 15:05:33 -0600253 if (lifetime_l12 != lifetime_default) {
254 /*
255 * Choose the interest lifetime so there are at least 3
256 * expressions (in the unsatisfied case).
257 */
258 unsigned char buf[3] = { 0 };
259 int i;
260 for (i = sizeof(buf) - 1; i >= 0; i--, lifetime_l12 >>= 8)
261 buf[i] = lifetime_l12 & 0xff;
262 ccnb_append_tagged_blob(templ, CCN_DTAG_InterestLifetime, buf, sizeof(buf));
263 }
soamin15043622013-01-15 23:10:26 -0600264 ccn_charbuf_append_closer(templ); /* </Interest> */
265 }
266 resultbuf = ccn_charbuf_create();
267 if (resolve_version != 0) {
268 res = ccn_resolve_version(nlsr->ccn, name, resolve_version, 500);
269 if (res >= 0) {
270 ccn_uri_append(resultbuf, name->buf, name->length, 1);
271 //fprintf(stderr, "== %s\n",ccn_charbuf_as_string(resultbuf));
272 resultbuf->length = 0;
273 }
274 }
275 res = ccn_get(nlsr->ccn, name, templ, timeout_ms, resultbuf, &pcobuf, NULL, get_flags);
276 if (res >= 0) {
277 ptr = resultbuf->buf;
278 length = resultbuf->length;
279 if (content_only){
280 ccn_content_get_value(ptr, length, &pcobuf, &ptr, &length);
Obaid Amine63022a2013-01-15 23:36:40 -0600281 *content_data = (unsigned char *) calloc(length, sizeof(char *));
282 memcpy (*content_data, ptr, length);
soamin15043622013-01-15 23:10:26 -0600283 }
284 }
285 ccn_charbuf_destroy(&resultbuf);
286 ccn_charbuf_destroy(&templ);
287 ccn_charbuf_destroy(&name);
288 //return (unsigned char *)ptr;
akmhoque8fdd6412012-12-04 15:05:33 -0600289}
290
akmhoque4ae16942012-12-10 11:50:43 -0600291void
soamin15043622013-01-15 23:10:26 -0600292process_incoming_sync_content_lsa( unsigned char *content_data)
akmhoque4ae16942012-12-10 11:50:43 -0600293{
294
295
296 if ( nlsr->debugging )
297 printf("process_incoming_sync_content_lsa called \n");
298 //if ( nlsr->detailed_logging )
299 //writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_content_lsa called \n");
300
301 char *sep="|";
302 char *rem;
303 char *orig_router;
304 char *orl;
305 int orig_router_length;
306 char *lst;
307 int ls_type;
308 char *lsid;
309 long int ls_id;
310 char *isvld;
311 int isValid;
312 char *num_link;
313 int no_link;
314 char *np;
315 char *np_length;
316 int name_length;
317 char *data;
318 char *orig_time;
319
320
321 if ( nlsr->debugging )
322 printf("LSA Data \n");
323 //if ( nlsr->detailed_logging )
324 // writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Data\n");
325
soamin15043622013-01-15 23:10:26 -0600326 if( strlen((char *)content_data ) > 0 )
akmhoque4ae16942012-12-10 11:50:43 -0600327 {
328
329 orig_router=strtok_r((char *)content_data,sep,&rem);
330 orl=strtok_r(NULL,sep,&rem);
331 orig_router_length=atoi(orl);
332
333 if ( nlsr->debugging )
334 {
335 printf(" Orig Router Name : %s\n",orig_router);
336 printf(" Orig Router Length: %d\n",orig_router_length);
337 }
338
339 lst=strtok_r(NULL,sep,&rem);
340 ls_type=atoi(lst);
341
342 if ( nlsr->debugging )
343 printf(" LS Type : %d\n",ls_type);
344
345 if ( ls_type == LS_TYPE_NAME )
346 {
347 lsid=strtok_r(NULL,sep,&rem);
348 ls_id=atoi(lsid);
349 orig_time=strtok_r(NULL,sep,&rem);
350 isvld=strtok_r(NULL,sep,&rem);
351 isValid=atoi(isvld);
352 np=strtok_r(NULL,sep,&rem);
353 np_length=strtok_r(NULL,sep,&rem);
354 name_length=atoi(np_length);
355 if ( nlsr->debugging )
356 {
357 printf(" LS ID : %ld\n",ls_id);
358 printf(" isValid : %d\n",isValid);
359 printf(" Name Prefix : %s\n",np);
360 printf(" Orig Time : %s\n",orig_time);
361 printf(" Name Prefix length: %d\n",name_length);
362 }
363
364 build_and_install_others_name_lsa(orig_router,ls_type,ls_id,orig_time,isValid,np);
365
366 print_name_lsdb();
367
368 }
369 else if ( ls_type == LS_TYPE_ADJ )
370 {
371 orig_time=strtok_r(NULL,sep,&rem);
372 num_link=strtok_r(NULL,sep,&rem);
373 no_link=atoi(num_link);
374 data=rem;
375
376 if ( nlsr->debugging )
377 {
378 printf(" No Link : %d\n",no_link);
379 printf(" Data : %s\n",data);
380 }
381 build_and_install_others_adj_lsa(orig_router,ls_type,orig_time,no_link,data);
382 }
383 }
384}
385
akmhoque8fdd6412012-12-04 15:05:33 -0600386void
387process_content_from_sync(struct ccn_charbuf *content_name, struct ccn_indexbuf *components)
388{
389 int lsa_position;
390 int res;
391 size_t comp_size;
392 const unsigned char *lst;
393 const unsigned char *lsid;
394 const unsigned char *origtime;
395
396 int ls_type;
397 long int ls_id=0;
398
soamin15043622013-01-15 23:10:26 -0600399 unsigned char *content_data = NULL;
400
akmhoque4ae16942012-12-10 11:50:43 -0600401 char *time_stamp=(char *)malloc(20);
402 memset(time_stamp,0,20);
403 get_current_timestamp_micro(time_stamp);
404
akmhoque8fdd6412012-12-04 15:05:33 -0600405 struct ccn_charbuf *uri = ccn_charbuf_create();
406 ccn_uri_append(uri, content_name->buf, content_name->length, 0);
407
408 struct name_prefix *orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix));
409
410 lsa_position=get_lsa_position(content_name, components);
411
412 res=ccn_name_comp_get(content_name->buf, components,lsa_position+1,&lst, &comp_size);
413
akmhoque4ae16942012-12-10 11:50:43 -0600414 //printf("Ls Type: %s\n",lst);
akmhoque8fdd6412012-12-04 15:05:33 -0600415 ls_type=atoi((char *)lst);
416 if(ls_type == LS_TYPE_NAME)
417 {
akmhoque4ae16942012-12-10 11:50:43 -0600418
akmhoque8fdd6412012-12-04 15:05:33 -0600419 res=ccn_name_comp_get(content_name->buf, components,lsa_position+2,&lsid, &comp_size);
420 ls_id=atoi((char *)lsid);
421 res=ccn_name_comp_get(content_name->buf, components,lsa_position+3,&origtime, &comp_size);
422 get_name_part(orig_router,content_name,components,3);
akmhoque8fdd6412012-12-04 15:05:33 -0600423
akmhoque4ae16942012-12-10 11:50:43 -0600424 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
425
426 //printf("Ls ID: %s\nOrig Time: %s\nOrig Router: %s\n",lsid,origtime,orig_router->name);
427
428 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 -0600429 {
akmhoque4ae16942012-12-10 11:50:43 -0600430 int is_new_name_lsa=check_is_new_name_lsa(orig_router->name,(char *)lst,(char *)lsid,(char *)origtime);
431 if ( is_new_name_lsa == 1 )
432 {
a64adam04be6482013-01-17 11:29:32 -0600433 if ( nlsr->debugging )
434 printf("New NAME LSA.....\n");
soamin15043622013-01-15 23:10:26 -0600435 //content_data=get_content_by_content_name(ccn_charbuf_as_string(uri));
Obaid Amine63022a2013-01-15 23:36:40 -0600436 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
a64adamf7b051d2013-01-17 11:39:58 -0600437 if ( nlsr->debugging )
438 printf("Content Data: %s \n",content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600439 process_incoming_sync_content_lsa(content_data);
440 }
441 else
442 {
a64adam04be6482013-01-17 11:29:32 -0600443 if ( nlsr->debugging )
444 printf("Name LSA / Newer Name LSA already xists in LSDB\n");
soamin15043622013-01-15 23:10:26 -0600445 //content_data=get_content_by_content_name(ccn_charbuf_as_string(uri));
Obaid Amine63022a2013-01-15 23:36:40 -0600446 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
a64adam04be6482013-01-17 11:29:32 -0600447
448 if ( nlsr->debugging )
449 printf("Content Data: %s \n",content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600450 }
akmhoque8fdd6412012-12-04 15:05:33 -0600451 }
452 else
453 {
a64adam04be6482013-01-17 11:29:32 -0600454 if ( nlsr->debugging )
455 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
akmhoque8fdd6412012-12-04 15:05:33 -0600456 }
457 }
458 else if(ls_type == LS_TYPE_ADJ)
459 {
460 res=ccn_name_comp_get(content_name->buf, components,lsa_position+2,&origtime, &comp_size);
461 get_name_part(orig_router,content_name,components,2);
a64adam04be6482013-01-17 11:29:32 -0600462
463 if ( nlsr->debugging )
464 printf("Orig Time: %s\nOrig Router: %s\n",origtime,orig_router->name);
akmhoque8fdd6412012-12-04 15:05:33 -0600465
akmhoque4ae16942012-12-10 11:50:43 -0600466 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque8fdd6412012-12-04 15:05:33 -0600467
akmhoque4ae16942012-12-10 11:50:43 -0600468 //printf("Ls ID: %s\nOrig Time: %s\nOrig Router: %s\n",lsid,origtime,orig_router->name);
469
470 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) )
471 {
472 int is_new_adj_lsa=check_is_new_adj_lsa(orig_router->name,(char *)lst,(char *)origtime);
473 if ( is_new_adj_lsa == 1 )
474 {
a64adam04be6482013-01-17 11:29:32 -0600475 if ( nlsr->debugging )
476 printf("New Adj LSA.....\n");
soamin15043622013-01-15 23:10:26 -0600477 //content_data=get_content_by_content_name(ccn_charbuf_as_string(uri));
Obaid Amine63022a2013-01-15 23:36:40 -0600478 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
a64adam04be6482013-01-17 11:29:32 -0600479
480 if ( nlsr->debugging )
481 printf("Content Data: %s \n",content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600482 process_incoming_sync_content_lsa(content_data);
483 }
484 else
485 {
a64adam04be6482013-01-17 11:29:32 -0600486 if ( nlsr->debugging )
487 printf("Adj LSA / Newer Adj LSA already exists in LSDB\n");
Obaid Amine63022a2013-01-15 23:36:40 -0600488 get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data);
a64adam04be6482013-01-17 11:29:32 -0600489 if ( nlsr->debugging )
490 printf("Content Data: %s \n",content_data);
akmhoque4ae16942012-12-10 11:50:43 -0600491 }
492 }
493 else
494 {
a64adam04be6482013-01-17 11:29:32 -0600495 if ( nlsr->debugging )
496 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
akmhoque4ae16942012-12-10 11:50:43 -0600497 }
akmhoque8fdd6412012-12-04 15:05:33 -0600498 }
499
Obaid Amine63022a2013-01-15 23:36:40 -0600500 if (content_data != NULL)
501 free(content_data);
akmhoque8fdd6412012-12-04 15:05:33 -0600502 ccn_charbuf_destroy(&uri);
akmhoque8fdd6412012-12-04 15:05:33 -0600503}
504
505int
506sync_callback(struct ccns_name_closure *nc,
507 struct ccn_charbuf *lhash,
508 struct ccn_charbuf *rhash,
509 struct ccn_charbuf *name)
510{
511
512
513 struct ccn_indexbuf cid={0};
514
515 struct ccn_indexbuf *components=&cid;
516 ccn_name_split (name, components);
517 ccn_name_chop(name,components,-3);
518
519 process_content_from_sync(name,components);
520
521 return(0);
522}
523
524
525void
526sync_monitor(char *topo_prefix, char *slice_prefix)
527{
528
529 static struct ccns_name_closure nc={0};
530
531 nlsr->closure = &nc;
532 struct ccn_charbuf *prefix = ccn_charbuf_create();
533 struct ccn_charbuf *roothash = NULL;
534 struct ccn_charbuf *topo = ccn_charbuf_create();
535 nlsr->slice = ccns_slice_create();
536 ccn_charbuf_reset(prefix);
537 ccn_charbuf_reset(topo);
538
539 ccn_charbuf_reset(prefix);
540 ccn_name_from_uri(prefix, slice_prefix);
541
542 ccn_charbuf_reset(topo);
543 ccn_name_from_uri(topo, topo_prefix);
544
545
546 ccns_slice_set_topo_prefix(nlsr->slice, topo, prefix);
Syed Obaid Amin3a3864b2013-01-09 03:05:50 -0600547 nlsr->closure->callback = &sync_cb;
548 //nlsr->closure->callback = &sync_callback;
akmhoque8fdd6412012-12-04 15:05:33 -0600549 nlsr->ccns = ccns_open(nlsr->ccn, nlsr->slice, nlsr->closure, roothash, NULL);
akmhoque8fdd6412012-12-04 15:05:33 -0600550}
551
552struct ccn_charbuf *
553make_template(int scope)
554{
555 struct ccn_charbuf *templ = NULL;
556 templ = ccn_charbuf_create();
557 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
558 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
559 ccn_charbuf_append_closer(templ); /* </Name> */
560 if (0 <= scope && scope <= 2)
561 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
562 ccn_charbuf_append_closer(templ); /* </Interest> */
563 return(templ);
564}
565
566void
567write_data_to_repo(char *data, char *name_prefix)
568{
akmhoque260698b2013-01-17 03:54:09 -0600569 if ( nlsr->debugging )
570 {
571 printf("write_data_to_repo called\n");
akmhoque47d19d12013-01-17 04:00:32 -0600572 printf("Content Name: %s \n",name_prefix);
akmhoque260698b2013-01-17 03:54:09 -0600573 printf("Content Data: %s \n",data);
574 }
akmhoque8fdd6412012-12-04 15:05:33 -0600575
576 struct ccn_charbuf *name = NULL;
577 struct ccn_seqwriter *w = NULL;
578 int blocksize = 1024;
579 int freshness = -1;
580 int torepo = 1;
581 int scope = 1;
582 int res;
583 size_t blockread;
584 struct ccn_charbuf *templ;
585
586 name = ccn_charbuf_create();
587 res = ccn_name_from_uri(name, name_prefix);
588 if (res < 0) {
589 fprintf(stderr, "bad CCN URI: %s\n",name_prefix);
590 exit(1);
591 }
592
593
594 w = ccn_seqw_create(nlsr->ccn, name);
595 if (w == NULL) {
596 fprintf(stderr, "ccn_seqw_create failed\n");
597 exit(1);
598 }
599 ccn_seqw_set_block_limits(w, blocksize, blocksize);
600 if (freshness > -1)
601 ccn_seqw_set_freshness(w, freshness);
602 if (torepo) {
603 struct ccn_charbuf *name_v = ccn_charbuf_create();
604 ccn_seqw_get_name(w, name_v);
605 ccn_name_from_uri(name_v, "%C1.R.sw");
606 ccn_name_append_nonce(name_v);
607 templ = make_template(scope);
608 res = ccn_get(nlsr->ccn, name_v, templ, 60000, NULL, NULL, NULL, 0);
609 ccn_charbuf_destroy(&templ);
610 ccn_charbuf_destroy(&name_v);
611 if (res < 0) {
612 fprintf(stderr, "No response from repository\n");
613 exit(1);
614 }
615 }
616
617
618
619
620 blockread = 0;
akmhoque260698b2013-01-17 03:54:09 -0600621
akmhoque8fdd6412012-12-04 15:05:33 -0600622
623 blockread=strlen(data);
624
625 if (blockread > 0) {
akmhoqued6cd61d2013-01-17 10:31:15 -0600626 //ccn_run(nlsr->ccn, 100);
akmhoque8fdd6412012-12-04 15:05:33 -0600627 res = ccn_seqw_write(w, data, blockread);
akmhoque260698b2013-01-17 03:54:09 -0600628 while (res == -1) {
akmhoqued6cd61d2013-01-17 10:31:15 -0600629 //ccn_run(nlsr->ccn, 100);
akmhoque260698b2013-01-17 03:54:09 -0600630 res = ccn_seqw_write(w, data, blockread);
631 }
akmhoque8fdd6412012-12-04 15:05:33 -0600632 }
633
akmhoque8fdd6412012-12-04 15:05:33 -0600634 ccn_seqw_close(w);
akmhoque260698b2013-01-17 03:54:09 -0600635 //ccn_run(nlsr->ccn, 1);
akmhoque8fdd6412012-12-04 15:05:33 -0600636 ccn_charbuf_destroy(&name);
637}
638
639
640int
641create_sync_slice(char *topo_prefix, char *slice_prefix)
642{
643 int res;
644 struct ccns_slice *slice;
645 struct ccn_charbuf *prefix = ccn_charbuf_create();
646 struct ccn_charbuf *topo = ccn_charbuf_create();
647 struct ccn_charbuf *clause = ccn_charbuf_create();
648 struct ccn_charbuf *slice_name = ccn_charbuf_create();
649 struct ccn_charbuf *slice_uri = ccn_charbuf_create();
650
651 if (prefix == NULL || topo == NULL || clause == NULL ||
652 slice_name == NULL || slice_uri == NULL) {
653 fprintf(stderr, "Unable to allocate required memory.\n");
654 exit(1);
655 }
656
657
658 slice = ccns_slice_create();
659
660 ccn_charbuf_reset(topo);
661 ccn_name_from_uri(topo, topo_prefix);
662 ccn_charbuf_reset(prefix);
663 ccn_name_from_uri(prefix,slice_prefix );
664 ccns_slice_set_topo_prefix(slice, topo, prefix);
665
666
667 res = ccns_write_slice(nlsr->ccn, slice, slice_name);
Syed Obaid Amin83eea122013-01-08 12:21:37 -0600668 /*
669// Obaid: commenting out the following lines to resolve a bug.
670// If commenting them can resolve the issue, then we
671// need to call them before terminating the program.
akmhoque8fdd6412012-12-04 15:05:33 -0600672 ccns_slice_destroy(&slice);
673 ccn_charbuf_destroy(&prefix);
674 ccn_charbuf_destroy(&topo);
675 ccn_charbuf_destroy(&clause);
676 ccn_charbuf_destroy(&slice_name);
677 ccn_charbuf_destroy(&slice_uri);
Syed Obaid Amin83eea122013-01-08 12:21:37 -0600678*/
akmhoque8fdd6412012-12-04 15:05:33 -0600679 return 0;
680}
681