blob: c68e3bc6626535e1d8f0abdfae02db2e3d9b6b0d [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"
akmhoqueedb68d92013-03-05 10:18:16 -060032#include "nlsr_km.h"
akmhoque7adb2772013-03-05 16:30:59 -060033#include "nlsr_km_util.h"
akmhoquea0e71152013-02-11 09:47:59 -060034
35
akmhoqueedb68d92013-03-05 10:18:16 -060036char *
akmhoquea0e71152013-02-11 09:47:59 -060037hex_string(unsigned char *s, size_t l)
38{
Obaid Amin806df812013-02-21 13:30:26 -060039 const char *hex_digits = "0123456789abcdef";
40 char *r;
41 int i;
42 r = calloc(1, 1 + 2 * l);
43 for (i = 0; i < l; i++) {
44 r[2*i] = hex_digits[(s[i]>>4) & 0xf];
45 r[1+2*i] = hex_digits[s[i] & 0xf];
46 }
47 return(r);
akmhoquea0e71152013-02-11 09:47:59 -060048}
49
akmhoquec25f2242013-03-15 00:24:43 -050050/**
51* call back function from sync. Receive notification of updates in sync.
52* This function calls process_content_from_sync to handle incoming content
53* from sync.
54*/
55
akmhoqueedb68d92013-03-05 10:18:16 -060056int
akmhoquea0e71152013-02-11 09:47:59 -060057sync_cb(struct ccns_name_closure *nc,
Obaid Amin806df812013-02-21 13:30:26 -060058 struct ccn_charbuf *lhash,
59 struct ccn_charbuf *rhash,
60 struct ccn_charbuf *name)
akmhoquea0e71152013-02-11 09:47:59 -060061{
akmhoque483c1eb2013-03-08 00:51:09 -060062 nlsr_lock();
Obaid Amin806df812013-02-21 13:30:26 -060063 int res;
Obaid Amin7997ad82013-02-22 16:41:56 -060064 struct ccn_charbuf *content_name;
65 struct ccn_indexbuf *content_comps;
66 struct ccn_indexbuf *name_comps;
67
68 content_comps = ccn_indexbuf_create();
69 res = ccn_name_split(name, content_comps);
Obaid Amin806df812013-02-21 13:30:26 -060070 if ( res < 0 )
71 return 0;
Obaid Amin7997ad82013-02-22 16:41:56 -060072
73 if (content_comps->n < 2)
Obaid Amin806df812013-02-21 13:30:26 -060074 return 0;
Obaid Amin7997ad82013-02-22 16:41:56 -060075
76 content_name = ccn_charbuf_create();
77 ccn_name_init(content_name);
78
79 res = ccn_name_append_components( content_name, name->buf,
80 content_comps->buf[0], content_comps->buf[content_comps->n - 1]);
Obaid Amin806df812013-02-21 13:30:26 -060081
82 if ( res < 0)
83 return 0;
akmhoquea0e71152013-02-11 09:47:59 -060084
Obaid Amin7997ad82013-02-22 16:41:56 -060085 // for debugging
Obaid Amin806df812013-02-21 13:30:26 -060086 struct ccn_charbuf *temp=ccn_charbuf_create();
87 ccn_uri_append(temp, content_name->buf, content_name->length, 0);
88 if ( nlsr->debugging )
89 printf("Name before chopping: %s \n",ccn_charbuf_as_string(temp));
90 ccn_charbuf_destroy(&temp);
akmhoquea0e71152013-02-11 09:47:59 -060091
Obaid Amin7997ad82013-02-22 16:41:56 -060092 name_comps = ccn_indexbuf_create();
93 res=ccn_name_split (content_name, name_comps);
94 if (res < 0)
Obaid Amin806df812013-02-21 13:30:26 -060095 return 0;
akmhoquea0e71152013-02-11 09:47:59 -060096
Obaid Amin806df812013-02-21 13:30:26 -060097 if ( nlsr->debugging )
98 {
99 printf("Number of components in name = %d \n",res);
100 printf("Number of components in name as indexbuf->n = %d \n",
Obaid Amin7997ad82013-02-22 16:41:56 -0600101 (int)name_comps->n);
Obaid Amin806df812013-02-21 13:30:26 -0600102 }
Obaid Amin7997ad82013-02-22 16:41:56 -0600103
104 ccn_name_chop(content_name, name_comps, -3);
Obaid Amin806df812013-02-21 13:30:26 -0600105 if ( nlsr->debugging )
106 printf("Number of components in name as indexbuf->n after chopping= %d \n"
Obaid Amin7997ad82013-02-22 16:41:56 -0600107 , (int)name_comps->n);
akmhoquea0e71152013-02-11 09:47:59 -0600108
Obaid Amin7997ad82013-02-22 16:41:56 -0600109 //for debugging
Obaid Amin806df812013-02-21 13:30:26 -0600110 struct ccn_charbuf *temp1=ccn_charbuf_create();
111 ccn_uri_append(temp1, content_name->buf, content_name->length, 0);
112 if ( nlsr->debugging )
113 printf("Name after chopping: %s \n",ccn_charbuf_as_string(temp1));
114 ccn_charbuf_destroy(&temp1);
akmhoquea0e71152013-02-11 09:47:59 -0600115
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600116 //main method that processes contents from the sync
Obaid Amin7997ad82013-02-22 16:41:56 -0600117 process_content_from_sync(content_name, name_comps);
118
Obaid Amin806df812013-02-21 13:30:26 -0600119 ccn_charbuf_destroy(&content_name);
Obaid Amin7997ad82013-02-22 16:41:56 -0600120 ccn_indexbuf_destroy(&content_comps);
121 ccn_indexbuf_destroy(&name_comps);
akmhoquea0e71152013-02-11 09:47:59 -0600122
akmhoque483c1eb2013-03-08 00:51:09 -0600123 nlsr_unlock();
Obaid Amin806df812013-02-21 13:30:26 -0600124 return(0);
akmhoquea0e71152013-02-11 09:47:59 -0600125}
126
127
akmhoquec25f2242013-03-15 00:24:43 -0500128/**
129* this function retrieve part of name from interest name and put it in name_part
130*/
akmhoqueccb33e92013-02-20 11:44:28 -0600131
akmhoqueedb68d92013-03-05 10:18:16 -0600132void
akmhoqueccb33e92013-02-20 11:44:28 -0600133get_name_part(struct name_prefix *name_part,struct ccn_charbuf * interest_ccnb,
Obaid Amin806df812013-02-21 13:30:26 -0600134 struct ccn_indexbuf *interest_comps, int offset)
akmhoquea0e71152013-02-11 09:47:59 -0600135{
Obaid Amin806df812013-02-21 13:30:26 -0600136 int lsa_position=0;
akmhoqueedb68d92013-03-05 10:18:16 -0600137
akmhoque0b60ba92013-02-25 17:55:35 -0600138 struct ccn_indexbuf *components=ccn_indexbuf_create();
Obaid Amin806df812013-02-21 13:30:26 -0600139 struct ccn_charbuf *name=ccn_charbuf_create();
140 ccn_name_from_uri(name,nlsr->slice_prefix);
141 ccn_name_split (name, components);
142 lsa_position=components->n-2;
Obaid Amin806df812013-02-21 13:30:26 -0600143 ccn_charbuf_destroy(&name);
akmhoquea0e71152013-02-11 09:47:59 -0600144
akmhoquef6773612013-02-23 09:53:00 -0600145 struct ccn_charbuf *temp=ccn_charbuf_create();
146 ccn_name_init(temp);
147 ccn_name_append_components( temp, interest_ccnb->buf,
akmhoque7adb2772013-03-05 16:30:59 -0600148 interest_comps->buf[lsa_position+1],
149 interest_comps->buf[interest_comps->n - 1]);
akmhoquef6773612013-02-23 09:53:00 -0600150
151 struct ccn_charbuf *temp1=ccn_charbuf_create();
152 ccn_uri_append(temp1, temp->buf, temp->length, 0);
153
akmhoque7adb2772013-03-05 16:30:59 -0600154 name_part->name=(char *)calloc(strlen(ccn_charbuf_as_string(temp1))+1,
155 sizeof(char));
156 memcpy(name_part->name,ccn_charbuf_as_string(temp1),
157 strlen(ccn_charbuf_as_string(temp1)));
akmhoquef6773612013-02-23 09:53:00 -0600158 name_part->name[strlen(ccn_charbuf_as_string(temp1))]='\0';
159 name_part->length=strlen(ccn_charbuf_as_string(temp1))+1;
160
161 ccn_charbuf_destroy(&temp1);
162 ccn_charbuf_destroy(&temp);
akmhoque0b60ba92013-02-25 17:55:35 -0600163 ccn_indexbuf_destroy(&components);
akmhoquef6773612013-02-23 09:53:00 -0600164
165 if ( nlsr->debugging )
166 printf("Name Part: %s \n",name_part->name);
167
akmhoquea0e71152013-02-11 09:47:59 -0600168}
169
akmhoquea0e71152013-02-11 09:47:59 -0600170
akmhoquec25f2242013-03-15 00:24:43 -0500171/**
172* Get content value by content name and put the content value in content_data
173*/
akmhoquea0e71152013-02-11 09:47:59 -0600174
175
akmhoque7adb2772013-03-05 16:30:59 -0600176int
akmhoque5079b652013-03-15 19:48:49 -0500177get_content_by_content_name(char *content_name, unsigned char **content_data,
akmhoque7adb2772013-03-05 16:30:59 -0600178 char *orig_router)
akmhoquea0e71152013-02-11 09:47:59 -0600179{
akmhoque7adb2772013-03-05 16:30:59 -0600180
181 int ret=-1;
Obaid Amin806df812013-02-21 13:30:26 -0600182 struct ccn_charbuf *name = NULL;
183 struct ccn_charbuf *templ = NULL;
184 struct ccn_charbuf *resultbuf = NULL;
185 struct ccn_parsed_ContentObject pcobuf = { 0 };
186 int res;
akmhoque7adb2772013-03-05 16:30:59 -0600187 int allow_stale = 1;
Obaid Amin806df812013-02-21 13:30:26 -0600188 int content_only = 1;
189 int scope = -1;
akmhoquec90ca4d2013-03-15 09:30:17 -0500190 const unsigned char *ptr,*ptr_in;
191 size_t length,length_in;
Obaid Amin806df812013-02-21 13:30:26 -0600192 int resolve_version = CCN_V_HIGHEST;
193 int timeout_ms = 3000;
194 const unsigned lifetime_default = CCN_INTEREST_LIFETIME_SEC << 12;
195 unsigned lifetime_l12 = lifetime_default;
196 int get_flags = 0;
akmhoquea0e71152013-02-11 09:47:59 -0600197
Obaid Amin806df812013-02-21 13:30:26 -0600198 name = ccn_charbuf_create();
199 res = ccn_name_from_uri(name,content_name);
200 if (res < 0) {
201 fprintf(stderr, "Bad ccn URI: %s\n", content_name);
akmhoque7adb2772013-03-05 16:30:59 -0600202 ccn_charbuf_destroy(&name);
203 return ret;
Obaid Amin806df812013-02-21 13:30:26 -0600204 }
akmhoquea0e71152013-02-11 09:47:59 -0600205
Obaid Amin806df812013-02-21 13:30:26 -0600206 if (allow_stale || lifetime_l12 != lifetime_default || scope != -1) {
207 templ = ccn_charbuf_create();
208 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
209 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
210 ccn_charbuf_append_closer(templ); /* </Name> */
211 if (allow_stale) {
212 ccn_charbuf_append_tt(templ, CCN_DTAG_AnswerOriginKind, CCN_DTAG);
213 ccnb_append_number(templ,
214 CCN_AOK_DEFAULT | CCN_AOK_STALE);
215 ccn_charbuf_append_closer(templ); /* </AnswerOriginKind> */
216 }
217 if (scope != -1) {
218 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
219 }
220 if (lifetime_l12 != lifetime_default) {
221 /*
222 * Choose the interest lifetime so there are at least 3
223 * expressions (in the unsatisfied case).
224 */
225 unsigned char buf[3] = { 0 };
226 int i;
227 for (i = sizeof(buf) - 1; i >= 0; i--, lifetime_l12 >>= 8)
228 buf[i] = lifetime_l12 & 0xff;
229 ccnb_append_tagged_blob(templ, CCN_DTAG_InterestLifetime, buf,
230 sizeof(buf));
231 }
232 ccn_charbuf_append_closer(templ); /* </Interest> */
akmhoquea0e71152013-02-11 09:47:59 -0600233 }
Obaid Amin806df812013-02-21 13:30:26 -0600234 resultbuf = ccn_charbuf_create();
235 if (resolve_version != 0) {
236 res = ccn_resolve_version(nlsr->ccn, name, resolve_version, 500);
237 if (res >= 0) {
238 ccn_uri_append(resultbuf, name->buf, name->length, 1);
239 resultbuf->length = 0;
240 }
akmhoquea0e71152013-02-11 09:47:59 -0600241 }
Obaid Amin806df812013-02-21 13:30:26 -0600242 res = ccn_get(nlsr->ccn, name, templ, timeout_ms, resultbuf, &pcobuf, NULL,
243 get_flags);
akmhoquea0e71152013-02-11 09:47:59 -0600244 if (res >= 0) {
Obaid Amin806df812013-02-21 13:30:26 -0600245 ptr = resultbuf->buf;
246 length = resultbuf->length;
247 if (content_only){
248 ccn_content_get_value(ptr, length, &pcobuf, &ptr, &length);
akmhoque7adb2772013-03-05 16:30:59 -0600249 struct ccn_parsed_ContentObject pcobuf1 = { 0 };
250 int chk_cont=ccn_parse_ContentObject(ptr,length,&pcobuf1,NULL);
akmhoquee2901222013-03-15 00:59:54 -0500251 if ( nlsr->debugging )
252 printf("Content Parsing result: %d\n",chk_cont);
akmhoque7adb2772013-03-05 16:30:59 -0600253 if ( contain_key_name(ptr, &pcobuf1) == 1){
akmhoque237239c2013-03-18 10:29:26 -0500254 int res_verify=-1;
255 int key_exists=0;
256 struct ccn_charbuf *key_name=get_key_name(ptr, &pcobuf1);
257 struct ccn_charbuf *key_uri = ccn_charbuf_create();
258 ccn_uri_append(key_uri, key_name->buf, key_name->length, 0);
259 key_exists=does_key_exist(ccn_charbuf_as_string(key_uri));
260 int key_type=get_key_type_from_key_name(key_name);
261
262 if ( key_exists == 1 && key_type == NLSR_KEY ){
263 res_verify=0;
264 }
265 else{
266 res_verify=verify_key(ptr,&pcobuf1,1);
267 }
akmhoque7adb2772013-03-05 16:30:59 -0600268
akmhoqueb7958182013-03-11 12:03:54 -0500269 if ( res_verify != 0 ){
akmhoquee2901222013-03-15 00:59:54 -0500270 if ( nlsr->debugging )
akmhoqueb7958182013-03-11 12:03:54 -0500271 printf("Error in verfiying keys !! :( \n");
akmhoque237239c2013-03-18 10:29:26 -0500272 ccn_charbuf_destroy(&key_name);
273 ccn_charbuf_destroy(&key_uri);
akmhoqueb7958182013-03-11 12:03:54 -0500274 }
275 else{
akmhoque237239c2013-03-18 10:29:26 -0500276 if ( key_exists == 0 )
277 add_key(ccn_charbuf_as_string(key_uri));
278 ccn_charbuf_destroy(&key_name);
279 ccn_charbuf_destroy(&key_uri);
akmhoquee2901222013-03-15 00:59:54 -0500280 if ( nlsr->debugging )
281 printf("Key verification is successful :)\n");
akmhoquec90ca4d2013-03-15 09:30:17 -0500282 ptr_in=ptr;
283 length_in=length;
284 ccn_content_get_value(ptr_in, length_in, &pcobuf1,
285 &ptr_in, &length_in);
akmhoque3a89ec72013-03-15 19:56:05 -0500286 *content_data = (unsigned char *) calloc(length_in+1,
akmhoque96607ec2013-03-15 19:53:03 -0500287 sizeof(char *));
akmhoque5079b652013-03-15 19:48:49 -0500288 memcpy (*content_data, ptr_in, length_in);
akmhoqueb7958182013-03-11 12:03:54 -0500289 ret=0;
290 }
akmhoque7adb2772013-03-05 16:30:59 -0600291 }
Obaid Amin806df812013-02-21 13:30:26 -0600292 }
akmhoquea0e71152013-02-11 09:47:59 -0600293 }
akmhoque7adb2772013-03-05 16:30:59 -0600294
Obaid Amin806df812013-02-21 13:30:26 -0600295 ccn_charbuf_destroy(&resultbuf);
296 ccn_charbuf_destroy(&templ);
akmhoque7adb2772013-03-05 16:30:59 -0600297 ccn_charbuf_destroy(&name);
298
299 return ret;
akmhoquea0e71152013-02-11 09:47:59 -0600300}
301
akmhoquec25f2242013-03-15 00:24:43 -0500302/**
303* Handle incoming lsa content, Calls functions to install lsa into lsdb
304*/
305
akmhoqueedb68d92013-03-05 10:18:16 -0600306void
akmhoquea0e71152013-02-11 09:47:59 -0600307process_incoming_sync_content_lsa( unsigned char *content_data)
308{
309
310
Obaid Amin806df812013-02-21 13:30:26 -0600311 if ( nlsr->debugging )
312 printf("process_incoming_sync_content_lsa called \n");
Obaid Aminbb8ac422013-02-20 17:08:48 -0600313
Obaid Amin806df812013-02-21 13:30:26 -0600314 char *sep="|";
315 char *rem;
316 char *orig_router;
317 char *orl;
318 int orig_router_length;
319 char *lst;
320 int ls_type;
321 char *lsid;
322 long int ls_id;
323 char *isvld;
324 int isValid;
325 char *num_link;
326 int no_link;
327 char *np;
328 char *np_length;
329 int name_length;
330 char *data;
331 char *orig_time;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600332
333
akmhoquea0e71152013-02-11 09:47:59 -0600334 if ( nlsr->debugging )
Obaid Amin806df812013-02-21 13:30:26 -0600335 printf("LSA Data \n");
336
337 if( strlen((char *)content_data ) > 0 )
akmhoquea0e71152013-02-11 09:47:59 -0600338 {
Obaid Aminbb8ac422013-02-20 17:08:48 -0600339
Obaid Amin806df812013-02-21 13:30:26 -0600340 orig_router=strtok_r((char *)content_data,sep,&rem);
341 orl=strtok_r(NULL,sep,&rem);
342 orig_router_length=atoi(orl);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600343
Obaid Amin806df812013-02-21 13:30:26 -0600344 if ( nlsr->debugging )
345 {
346 printf(" Orig Router Name : %s\n",orig_router);
347 printf(" Orig Router Length: %d\n",orig_router_length);
348 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600349
Obaid Amin806df812013-02-21 13:30:26 -0600350 lst=strtok_r(NULL,sep,&rem);
351 ls_type=atoi(lst);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600352
Obaid Amin806df812013-02-21 13:30:26 -0600353 if ( nlsr->debugging )
354 printf(" LS Type : %d\n",ls_type);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600355
Obaid Amin806df812013-02-21 13:30:26 -0600356 if ( ls_type == LS_TYPE_NAME )
357 {
358 lsid=strtok_r(NULL,sep,&rem);
359 ls_id=atoi(lsid);
360 orig_time=strtok_r(NULL,sep,&rem);
361 isvld=strtok_r(NULL,sep,&rem);
362 isValid=atoi(isvld);
363 np=strtok_r(NULL,sep,&rem);
364 np_length=strtok_r(NULL,sep,&rem);
365 name_length=atoi(np_length);
366 if ( nlsr->debugging )
367 {
368 printf(" LS ID : %ld\n",ls_id);
369 printf(" isValid : %d\n",isValid);
370 printf(" Name Prefix : %s\n",np);
371 printf(" Orig Time : %s\n",orig_time);
372 printf(" Name Prefix length: %d\n",name_length);
373 }
374
akmhoque7adb2772013-03-05 16:30:59 -0600375 build_and_install_others_name_lsa(orig_router,ls_type,ls_id,
376 orig_time,isValid,np);
Obaid Amin806df812013-02-21 13:30:26 -0600377
378 print_name_lsdb();
379
380 }
381 else if ( ls_type == LS_TYPE_ADJ )
382 {
383 orig_time=strtok_r(NULL,sep,&rem);
384 num_link=strtok_r(NULL,sep,&rem);
385 no_link=atoi(num_link);
386 data=rem;
387
388 if ( nlsr->debugging )
389 {
390 printf(" Orig Time : %s\n",orig_time);
391 printf(" No Link : %d\n",no_link);
392 printf(" Data : %s\n",data);
393 }
akmhoque7adb2772013-03-05 16:30:59 -0600394 build_and_install_others_adj_lsa(orig_router,ls_type,orig_time,
395 no_link,data);
Obaid Amin806df812013-02-21 13:30:26 -0600396 }
397 else if ( ls_type == LS_TYPE_COR )
398 {
399 orig_time=strtok_r(NULL,sep,&rem);
400 char *cor_r=strtok_r(NULL,sep,&rem);
401 char *cor_theta=strtok_r(NULL,sep,&rem);
402
403 double r, theta;
akmhoque8034d502013-02-24 11:53:24 -0600404 r=strtod(cor_r,NULL);
405 theta=strtod(cor_theta,NULL);
Obaid Amin806df812013-02-21 13:30:26 -0600406
407 if ( nlsr->debugging )
408 {
409 printf(" Orig Time : %s\n",orig_time);
410 printf(" Cor R : %f\n",r);
411 printf(" Cor Theta : %f\n",theta);
412 }
akmhoque7adb2772013-03-05 16:30:59 -0600413 build_and_install_others_cor_lsa(orig_router,ls_type,orig_time,
414 (double)r, (double)theta);
Obaid Amin806df812013-02-21 13:30:26 -0600415 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600416
417 }
akmhoquea0e71152013-02-11 09:47:59 -0600418}
419
akmhoquec25f2242013-03-15 00:24:43 -0500420/**
421* Check LSA whether its new. If new retrieve the LSA content and call
422* process_incoming_sync_content_lsa with content_data
423*/
424
akmhoque7adb2772013-03-05 16:30:59 -0600425void
Obaid Amin7997ad82013-02-22 16:41:56 -0600426process_content_from_sync (struct ccn_charbuf *content_name,
427 struct ccn_indexbuf *components)
akmhoquea0e71152013-02-11 09:47:59 -0600428{
akmhoque8c5e2662013-02-24 06:37:14 -0600429 if (nlsr->debugging)
430 printf("process_content_from_sync called \n");
Obaid Amin806df812013-02-21 13:30:26 -0600431 size_t comp_size;
432 char *lst;
433 char *lsid;
434 const unsigned char *second_last_comp;
435 const unsigned char *third_last_comp;
436 const unsigned char *origtime;
437 char *sep=".";
438 char *rem;
439 char *second_comp_type;
akmhoquea0e71152013-02-11 09:47:59 -0600440
Obaid Amin806df812013-02-21 13:30:26 -0600441 int ls_type;
442 long int ls_id=0;
akmhoquea0e71152013-02-11 09:47:59 -0600443
Obaid Amin806df812013-02-21 13:30:26 -0600444 unsigned char *content_data = NULL;
akmhoquea0e71152013-02-11 09:47:59 -0600445
akmhoqueb31caa12013-02-22 08:42:09 -0600446 char *time_stamp=get_current_timestamp_micro_v2();
akmhoquea0e71152013-02-11 09:47:59 -0600447
Obaid Amin806df812013-02-21 13:30:26 -0600448 struct ccn_charbuf *uri = ccn_charbuf_create();
449 ccn_uri_append(uri, content_name->buf, content_name->length, 0);
akmhoquea0e71152013-02-11 09:47:59 -0600450
Obaid Amin7997ad82013-02-22 16:41:56 -0600451 struct name_prefix *orig_router=(struct name_prefix *)
452 calloc( 1, sizeof(struct name_prefix));
akmhoquea0e71152013-02-11 09:47:59 -0600453
Obaid Amin7997ad82013-02-22 16:41:56 -0600454 ccn_name_comp_get( content_name->buf, components,
455 components->n-1-2, &second_last_comp, &comp_size);
456
Obaid Amin806df812013-02-21 13:30:26 -0600457 if (nlsr->debugging)
Obaid Amin7997ad82013-02-22 16:41:56 -0600458 printf("2nd Last Component: %s \n", second_last_comp);
akmhoquea0e71152013-02-11 09:47:59 -0600459
Obaid Amin7997ad82013-02-22 16:41:56 -0600460 second_comp_type=strtok_r((char *)second_last_comp, sep, &rem);
461 if (second_comp_type == NULL || rem == NULL)
462 {
463 printf ("Error: unable to tokenize the string: %s, calling exit()\n",
464 (char *)second_last_comp);
465 exit(0);
466 }
467
Obaid Amin806df812013-02-21 13:30:26 -0600468 if ( strcmp( second_comp_type, "lsId" ) == 0 )
469 {
470 lsid=rem;
471 ls_id=atoi(rem);
akmhoque7adb2772013-03-05 16:30:59 -0600472 ccn_name_comp_get(content_name->buf, components,components->n-2-2,
473 &third_last_comp, &comp_size);
Obaid Amin806df812013-02-21 13:30:26 -0600474 lst=strtok_r((char *)third_last_comp,sep,&rem);
475 lst=rem;
476 ls_type=atoi(lst);
akmhoque7adb2772013-03-05 16:30:59 -0600477 ccn_name_comp_get(content_name->buf, components,components->n-2,
478 &origtime, &comp_size);
Obaid Amin806df812013-02-21 13:30:26 -0600479 ccn_name_chop(content_name,components,-3);
480 get_name_part(orig_router,content_name,components,0);
akmhoquea0e71152013-02-11 09:47:59 -0600481
Obaid Aminbb8ac422013-02-20 17:08:48 -0600482 if ( nlsr->debugging )
akmhoque7adb2772013-03-05 16:30:59 -0600483 printf("Orig Router: %s Ls Type: %d Ls id: %ld Orig Time: %s\n",
484 orig_router->name,ls_type,ls_id,origtime);
akmhoquea0e71152013-02-11 09:47:59 -0600485
Obaid Amin806df812013-02-21 13:30:26 -0600486 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque8c5e2662013-02-24 06:37:14 -0600487 if ( nlsr->debugging )
488 printf("LSA Life time: %d\n",lsa_life_time);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600489
akmhoque483c1eb2013-03-08 00:51:09 -0600490 if ( strcmp(orig_router->name,nlsr->router_name) != 0
491 && (lsa_life_time < nlsr->router_dead_interval) )
Obaid Aminbb8ac422013-02-20 17:08:48 -0600492 {
akmhoque7adb2772013-03-05 16:30:59 -0600493 int is_new_name_lsa=check_is_new_name_lsa(orig_router->name,
494 (char *)lst,(char *)lsid,(char *)origtime);
Obaid Amin806df812013-02-21 13:30:26 -0600495 if ( is_new_name_lsa == 1 )
496 {
497 if ( nlsr->debugging )
498 printf("New NAME LSA.....\n");
akmhoque7adb2772013-03-05 16:30:59 -0600499 int chk_con=get_content_by_content_name(ccn_charbuf_as_string(uri),
akmhoque5079b652013-03-15 19:48:49 -0500500 &content_data,orig_router->name);
akmhoque7adb2772013-03-05 16:30:59 -0600501 if ( chk_con == 0 ){
502 if ( nlsr->debugging )
503 printf("Content Data: %s \n",content_data);
504 process_incoming_sync_content_lsa(content_data);
505 }
506 else{
507 if ( nlsr->debugging )
508 printf("Verification failed. No content given back\n");
509 }
Obaid Amin806df812013-02-21 13:30:26 -0600510 }
511 else
512 {
513 if ( nlsr->debugging )
514 printf("Name LSA / Newer Name LSA already xists in LSDB\n");
akmhoque7adb2772013-03-05 16:30:59 -0600515 int chk_con=get_content_by_content_name(ccn_charbuf_as_string(uri)
akmhoque5079b652013-03-15 19:48:49 -0500516 , &content_data,orig_router->name);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600517
akmhoque7adb2772013-03-05 16:30:59 -0600518 if ( chk_con == 0 ){
519 if ( nlsr->debugging )
520 printf("Content Data: %s \n",content_data);
521 process_incoming_sync_content_lsa(content_data);
522 }
523 else{
524 if ( nlsr->debugging )
525 printf("Verification failed. No content given back\n");
526 }
Obaid Amin806df812013-02-21 13:30:26 -0600527 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600528 }
Obaid Amin806df812013-02-21 13:30:26 -0600529 else
Obaid Aminbb8ac422013-02-20 17:08:48 -0600530 {
Obaid Amin806df812013-02-21 13:30:26 -0600531 if ( nlsr->debugging )
532 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
Obaid Aminbb8ac422013-02-20 17:08:48 -0600533 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600534 }
Obaid Amin806df812013-02-21 13:30:26 -0600535 else
Obaid Aminbb8ac422013-02-20 17:08:48 -0600536 {
Obaid Amin806df812013-02-21 13:30:26 -0600537 ls_type=atoi(rem);
538 lst=rem;
539 if(ls_type == LS_TYPE_ADJ)
Obaid Aminbb8ac422013-02-20 17:08:48 -0600540 {
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600541 ccn_name_comp_get(content_name->buf, components,components->n-2,
542 &origtime, &comp_size);
Obaid Amin806df812013-02-21 13:30:26 -0600543 ccn_name_chop(content_name,components,-2);
544 get_name_part(orig_router,content_name,components,0);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600545
Obaid Amin806df812013-02-21 13:30:26 -0600546 if ( nlsr->debugging )
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600547 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",
548 orig_router->name,ls_type,origtime);
Obaid Amin806df812013-02-21 13:30:26 -0600549
550 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque8c5e2662013-02-24 06:37:14 -0600551 if ( nlsr->debugging )
552 printf("LSA Life time: %d\n",lsa_life_time);
553
akmhoque483c1eb2013-03-08 00:51:09 -0600554 if ( strcmp(orig_router->name,nlsr->router_name) != 0
555 && (lsa_life_time < nlsr->router_dead_interval) )
Obaid Amin806df812013-02-21 13:30:26 -0600556 {
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600557 int is_new_adj_lsa = check_is_new_adj_lsa( orig_router->name,
558 (char *)lst, (char *)origtime);
Obaid Amin806df812013-02-21 13:30:26 -0600559 if ( is_new_adj_lsa == 1 )
560 {
561 if ( nlsr->debugging )
562 printf("New Adj LSA.....\n");
akmhoque7adb2772013-03-05 16:30:59 -0600563 int chk_con=get_content_by_content_name(ccn_charbuf_as_string(uri),
akmhoque5079b652013-03-15 19:48:49 -0500564 &content_data,orig_router->name);
Obaid Amin806df812013-02-21 13:30:26 -0600565
akmhoque7adb2772013-03-05 16:30:59 -0600566 if ( chk_con == 0 ){
567 if ( nlsr->debugging )
568 printf("Content Data: %s \n",content_data);
569 process_incoming_sync_content_lsa(content_data);
570 }
571 else{
572 if ( nlsr->debugging )
573 printf("Verification failed. No content given back\n");
574 }
Obaid Amin806df812013-02-21 13:30:26 -0600575 }
576 else
577 {
578 if ( nlsr->debugging )
579 printf("Adj LSA / Newer Adj LSA already exists in LSDB\n");
akmhoque7adb2772013-03-05 16:30:59 -0600580 int chk_con=get_content_by_content_name(ccn_charbuf_as_string(uri),
akmhoque5079b652013-03-15 19:48:49 -0500581 &content_data,orig_router->name);
akmhoque7adb2772013-03-05 16:30:59 -0600582 if ( chk_con == 0 ){
583 if ( nlsr->debugging )
584 printf("Content Data: %s \n",content_data);
585 process_incoming_sync_content_lsa(content_data);
586 }
587 else{
588 if ( nlsr->debugging )
589 printf("Verification failed. No content given back\n");
590 }
Obaid Amin806df812013-02-21 13:30:26 -0600591 }
592 }
593 else
594 {
595 if ( nlsr->debugging )
596 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
597 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600598 }
Obaid Amin806df812013-02-21 13:30:26 -0600599 else if(ls_type == LS_TYPE_COR)
Obaid Aminbb8ac422013-02-20 17:08:48 -0600600 {
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600601 ccn_name_comp_get(content_name->buf, components, components->n-2,
602 &origtime, &comp_size);
Obaid Amin806df812013-02-21 13:30:26 -0600603 ccn_name_chop(content_name,components,-2);
604 get_name_part(orig_router,content_name,components,0);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600605
Obaid Amin806df812013-02-21 13:30:26 -0600606 if ( nlsr->debugging )
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600607 printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",
608 orig_router->name,ls_type,origtime);
Obaid Amin806df812013-02-21 13:30:26 -0600609
610 int lsa_life_time=get_time_diff(time_stamp,(char *)origtime);
akmhoque8c5e2662013-02-24 06:37:14 -0600611 if ( nlsr->debugging )
612 printf("LSA Life time: %d\n",lsa_life_time);
613
akmhoque483c1eb2013-03-08 00:51:09 -0600614 if ( strcmp(orig_router->name,nlsr->router_name) != 0
615 && (lsa_life_time < nlsr->router_dead_interval) )
Obaid Amin806df812013-02-21 13:30:26 -0600616 {
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600617 int is_new_cor_lsa=check_is_new_cor_lsa( orig_router->name,
618 (char *)lst,(char *) origtime);
Obaid Amin806df812013-02-21 13:30:26 -0600619 if ( is_new_cor_lsa == 1 )
620 {
621 if ( nlsr->debugging )
622 printf("New Cor LSA.....\n");
akmhoque7adb2772013-03-05 16:30:59 -0600623 int chk_con=get_content_by_content_name(ccn_charbuf_as_string(uri),
akmhoque5079b652013-03-15 19:48:49 -0500624 &content_data,
akmhoque7adb2772013-03-05 16:30:59 -0600625 orig_router->name);
Obaid Amin806df812013-02-21 13:30:26 -0600626
akmhoque7adb2772013-03-05 16:30:59 -0600627 if ( chk_con == 0 ){
628 if ( nlsr->debugging )
629 printf("Content Data: %s \n",content_data);
630 process_incoming_sync_content_lsa(content_data);
631 }
632 else{
633 if ( nlsr->debugging )
634 printf("Verification failed. No content given back\n");
635 }
Obaid Amin806df812013-02-21 13:30:26 -0600636 }
637 else
638 {
639 if ( nlsr->debugging )
640 printf("Cor LSA / Newer Cor LSA already exists in LSDB\n");
akmhoque7adb2772013-03-05 16:30:59 -0600641 int chk_con=get_content_by_content_name(ccn_charbuf_as_string(uri),
akmhoque5079b652013-03-15 19:48:49 -0500642 &content_data,orig_router->name);
akmhoque7adb2772013-03-05 16:30:59 -0600643 if ( chk_con == 0 ){
644 if ( nlsr->debugging )
645 printf("Content Data: %s \n",content_data);
646 process_incoming_sync_content_lsa(content_data);
647 }
648 else{
649 if ( nlsr->debugging )
650 printf("Verification failed. No content given back\n");
651 }
Obaid Amin806df812013-02-21 13:30:26 -0600652 }
653 }
654 else
655 {
656 if ( nlsr->debugging )
657 printf("Lsa is older than Router LSA refresh time/ Dead Interval\n");
658 }
659
660 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600661 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600662
akmhoquec25f2242013-03-15 00:24:43 -0500663 if(content_data)
664 free(content_data);
665
akmhoquef6773612013-02-23 09:53:00 -0600666 if (orig_router->name)
667 free(orig_router->name);
Obaid Amin05300892013-02-21 13:45:25 -0600668 if (orig_router)
669 free(orig_router);
Obaid Amin806df812013-02-21 13:30:26 -0600670 ccn_charbuf_destroy(&uri);
671 //01/31/2013
672 free(time_stamp);
akmhoquea0e71152013-02-11 09:47:59 -0600673}
674
akmhoquec25f2242013-03-15 00:24:43 -0500675/**
676* This function performs same functionality as ccnsyncwatch
677*/
678
679
680int
akmhoquea0e71152013-02-11 09:47:59 -0600681sync_monitor(char *topo_prefix, char *slice_prefix)
682{
683
Obaid Amin806df812013-02-21 13:30:26 -0600684 struct ccn_charbuf *prefix = ccn_charbuf_create();
Obaid Amin806df812013-02-21 13:30:26 -0600685 struct ccn_charbuf *topo = ccn_charbuf_create();
Obaid Amin7997ad82013-02-22 16:41:56 -0600686
687 nlsr->closure=(struct ccns_name_closure *)
akmhoquececba942013-02-25 17:33:34 -0600688 calloc(1,sizeof(struct ccns_name_closure)); // leak
Obaid Amin7997ad82013-02-22 16:41:56 -0600689
Obaid Amin806df812013-02-21 13:30:26 -0600690 nlsr->slice = ccns_slice_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600691
Obaid Amin806df812013-02-21 13:30:26 -0600692 ccn_charbuf_reset(prefix);
693 ccn_name_from_uri(prefix, slice_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600694
Obaid Amin806df812013-02-21 13:30:26 -0600695 ccn_charbuf_reset(topo);
696 ccn_name_from_uri(topo, topo_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600697
Obaid Amin806df812013-02-21 13:30:26 -0600698 ccns_slice_set_topo_prefix(nlsr->slice, topo, prefix);
699 nlsr->closure->callback = &sync_cb;
akmhoque54d86112013-02-21 16:42:34 -0600700 nlsr->ccns = ccns_open(nlsr->ccn, nlsr->slice, nlsr->closure, NULL, NULL);
akmhoquea0e71152013-02-11 09:47:59 -0600701
Obaid Amin7997ad82013-02-22 16:41:56 -0600702 ccn_charbuf_destroy(&prefix);
703 ccn_charbuf_destroy(&topo);
Obaid Amin806df812013-02-21 13:30:26 -0600704 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600705}
706
akmhoquead6d5692013-03-11 19:43:33 -0500707struct ccn_charbuf *
akmhoquea0e71152013-02-11 09:47:59 -0600708make_template(int scope)
709{
Obaid Amin806df812013-02-21 13:30:26 -0600710 struct ccn_charbuf *templ = NULL;
711 templ = ccn_charbuf_create();
712 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
713 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
714 ccn_charbuf_append_closer(templ); /* </Name> */
715 if (0 <= scope && scope <= 2)
716 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
717 ccn_charbuf_append_closer(templ); /* </Interest> */
718 return(templ);
akmhoquea0e71152013-02-11 09:47:59 -0600719}
720
akmhoquec25f2242013-03-15 00:24:43 -0500721/**
722* Write signed data to repo
723*/
724
725
726int
akmhoquea0e71152013-02-11 09:47:59 -0600727write_data_to_repo(char *data, char *name_prefix)
728{
akmhoque483c1eb2013-03-08 00:51:09 -0600729
730 nlsr_lock();
Obaid Amin806df812013-02-21 13:30:26 -0600731 if ( nlsr->debugging )
732 {
733 printf("write_data_to_repo called\n");
734 printf("Content Name: %s \n",name_prefix);
735 printf("Content Data: %s \n",data);
736 }
akmhoquea0e71152013-02-11 09:47:59 -0600737
Obaid Aminf210a322013-02-22 02:04:52 -0600738 struct ccn *temp_ccn;
Obaid Amin806df812013-02-21 13:30:26 -0600739 temp_ccn=ccn_create();
740 int ccn_fd=ccn_connect(temp_ccn, NULL);
741 if(ccn_fd == -1)
742 {
743 fprintf(stderr,"Could not connect to ccnd for Data Writing\n");
Obaid Amin4b0e4ec2013-02-24 22:15:01 -0600744 writeLogg(__FILE__,__FUNCTION__,__LINE__,
745 "Could not connect to ccnd for Data Writing\n");
Obaid Amin806df812013-02-21 13:30:26 -0600746 return -1;
747 }
Obaid Aminf210a322013-02-22 02:04:52 -0600748
Obaid Amin806df812013-02-21 13:30:26 -0600749 struct ccn_charbuf *name = NULL;
750 struct ccn_seqwriter *w = NULL;
751 int blocksize = 4096;
752 int freshness = -1;
Obaid Amin806df812013-02-21 13:30:26 -0600753 int scope = 1;
754 int res;
755 size_t blockread;
756 struct ccn_charbuf *templ;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600757
Obaid Amin806df812013-02-21 13:30:26 -0600758 name = ccn_charbuf_create();
759 res = ccn_name_from_uri(name, name_prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600760 if (res < 0) {
Obaid Amin806df812013-02-21 13:30:26 -0600761 fprintf(stderr, "bad CCN URI: %s\n",name_prefix);
762 return -1;
Obaid Aminbb8ac422013-02-20 17:08:48 -0600763 }
Obaid Amin806df812013-02-21 13:30:26 -0600764
Obaid Aminf210a322013-02-22 02:04:52 -0600765 w = ccn_seqw_create(temp_ccn, name);
Obaid Amin806df812013-02-21 13:30:26 -0600766 if (w == NULL) {
767 fprintf(stderr, "ccn_seqw_create failed\n");
768 return -1;
769 }
770 ccn_seqw_set_block_limits(w, blocksize, blocksize);
771 if (freshness > -1)
772 ccn_seqw_set_freshness(w, freshness);
akmhoquea0e71152013-02-11 09:47:59 -0600773
Obaid Amin7997ad82013-02-22 16:41:56 -0600774 struct ccn_charbuf *name_v = ccn_charbuf_create();
775 ccn_seqw_get_name(w, name_v);
776 ccn_name_from_uri(name_v, "%C1.R.sw");
777 ccn_name_append_nonce(name_v);
778 templ = make_template(scope);
779 res = ccn_get(temp_ccn, name_v, templ, 60000, NULL, NULL, NULL, 0);
780 ccn_charbuf_destroy(&templ);
781 ccn_charbuf_destroy(&name_v);
782 if (res < 0) {
783 fprintf(stderr, "No response from repository\n");
784 return -1;
785 }
akmhoquea0e71152013-02-11 09:47:59 -0600786
akmhoque7adb2772013-03-05 16:30:59 -0600787 struct ccn_charbuf *resultbuf=ccn_charbuf_create();
akmhoquea0e71152013-02-11 09:47:59 -0600788
akmhoque7adb2772013-03-05 16:30:59 -0600789 sign_content_with_user_defined_keystore(name,
790 resultbuf,
791 data,
792 strlen(data),
793 nlsr->keystore_path,
794 nlsr->keystore_passphrase,
795 nlsr->root_key_prefix,
796 nlsr->site_name,
akmhoque2fafaa52013-03-22 05:10:52 -0500797 nlsr->router_name,
798 60);
akmhoque7adb2772013-03-05 16:30:59 -0600799
800
akmhoque7adb2772013-03-05 16:30:59 -0600801 blockread=resultbuf->length;
akmhoquea0e71152013-02-11 09:47:59 -0600802
Obaid Amin806df812013-02-21 13:30:26 -0600803 if (blockread > 0) {
akmhoque7adb2772013-03-05 16:30:59 -0600804 res = ccn_seqw_write(w, resultbuf->buf, resultbuf->length);
Obaid Amin806df812013-02-21 13:30:26 -0600805 while (res == -1) {
akmhoquec25f2242013-03-15 00:24:43 -0500806 ccn_run(temp_ccn,1);
akmhoque7adb2772013-03-05 16:30:59 -0600807 res = ccn_seqw_write(w, resultbuf->buf, resultbuf->length);
Obaid Amin806df812013-02-21 13:30:26 -0600808 }
809 }
810
akmhoque8034d502013-02-24 11:53:24 -0600811 ccn_seqw_close(w);
akmhoquec25f2242013-03-15 00:24:43 -0500812 ccn_run(temp_ccn, 1);
Obaid Amin806df812013-02-21 13:30:26 -0600813 ccn_charbuf_destroy(&name);
akmhoque0ed6d982013-02-22 14:28:01 -0600814 ccn_destroy(&temp_ccn);
akmhoque6e2ba842013-03-05 19:35:26 -0600815 ccn_charbuf_destroy(&resultbuf);
akmhoquea0e71152013-02-11 09:47:59 -0600816
akmhoque483c1eb2013-03-08 00:51:09 -0600817 nlsr_unlock();
Obaid Amin806df812013-02-21 13:30:26 -0600818 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600819}
akmhoquec25f2242013-03-15 00:24:43 -0500820
821/**
822* Create slice for sync/repo
823*/
824
825
826int
827create_sync_slice(char *topo_prefix, char *slice_prefix)
akmhoquea0e71152013-02-11 09:47:59 -0600828{
Obaid Amin806df812013-02-21 13:30:26 -0600829 int res;
akmhoque7adb2772013-03-05 16:30:59 -0600830 struct ccn *handle;
Obaid Amin806df812013-02-21 13:30:26 -0600831 struct ccns_slice *slice;
832 struct ccn_charbuf *prefix = ccn_charbuf_create();
833 struct ccn_charbuf *topo = ccn_charbuf_create();
834 struct ccn_charbuf *clause = ccn_charbuf_create();
835 struct ccn_charbuf *slice_name = ccn_charbuf_create();
836 struct ccn_charbuf *slice_uri = ccn_charbuf_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600837
Obaid Amin806df812013-02-21 13:30:26 -0600838 if (prefix == NULL || topo == NULL || clause == NULL ||
839 slice_name == NULL || slice_uri == NULL) {
840 fprintf(stderr, "Unable to allocate required memory.\n");
841 return -1;
842 }
Obaid Aminbb8ac422013-02-20 17:08:48 -0600843
akmhoque0ed6d982013-02-22 14:28:01 -0600844 handle = ccn_create();
Obaid Amin806df812013-02-21 13:30:26 -0600845 res = ccn_connect(handle, NULL);
846 if (0 > res) {
847 fprintf(stderr, "Unable to connect to ccnd.\n");
848 return -1;
849 }
akmhoque0ed6d982013-02-22 14:28:01 -0600850
Obaid Amin806df812013-02-21 13:30:26 -0600851 slice = ccns_slice_create();
Obaid Aminbb8ac422013-02-20 17:08:48 -0600852
Obaid Amin806df812013-02-21 13:30:26 -0600853 ccn_charbuf_reset(topo);
854 ccn_name_from_uri(topo, topo_prefix);
855 ccn_charbuf_reset(prefix);
856 ccn_name_from_uri(prefix,slice_prefix );
857 ccns_slice_set_topo_prefix(slice, topo, prefix);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600858
859
akmhoque0ed6d982013-02-22 14:28:01 -0600860 res = ccns_write_slice(handle, slice, slice_name);
Obaid Aminbb8ac422013-02-20 17:08:48 -0600861
Obaid Amin806df812013-02-21 13:30:26 -0600862 //01/31/2013
863 ccns_slice_destroy(&slice);
akmhoque0ed6d982013-02-22 14:28:01 -0600864 ccn_destroy(&handle);
Obaid Amin806df812013-02-21 13:30:26 -0600865 ccn_charbuf_destroy(&prefix);
866 ccn_charbuf_destroy(&topo);
867 ccn_charbuf_destroy(&clause);
868 ccn_charbuf_destroy(&slice_name);
869 ccn_charbuf_destroy(&slice_uri);
akmhoquea0e71152013-02-11 09:47:59 -0600870
Obaid Amin806df812013-02-21 13:30:26 -0600871 return 0;
akmhoquea0e71152013-02-11 09:47:59 -0600872}
873
akmhoque0eb8de22013-02-24 14:08:19 -0600874