blob: e48f7d708973d4d9b7c262e05e57680148c6861b [file] [log] [blame]
akmhoque7c64d802013-03-05 10:18:37 -06001#include<stdio.h>
2#include<string.h>
3#include<stdlib.h>
4#include <unistd.h>
5#ifdef HAVE_CONFIG_H
6#include <config.h>
7#endif
8
9
10#include <ccn/ccn.h>
11#include <ccn/uri.h>
12#include <ccn/keystore.h>
13#include <ccn/signing.h>
14#include <ccn/schedule.h>
15#include <ccn/hashtb.h>
16
17#include "nlsr.h"
18#include "nlsr_km.h"
19#include "nlsr_km_util.h"
akmhoque237239c2013-03-18 10:29:26 -050020#include "utility.h"
akmhoque7c64d802013-03-05 10:18:37 -060021
22int
23sign_content_with_user_defined_keystore(struct ccn_charbuf *content_name,
24 struct ccn_charbuf *resultbuf,
25 const void *data,
26 size_t data_size,
27 char *keystore_path,
28 char *keystore_passphrase,
29 char *key_repo_name,
30 char *site_name,
akmhoque2fafaa52013-03-22 05:10:52 -050031 char *router_name,
32 long int freshness){
akmhoque7c64d802013-03-05 10:18:37 -060033
akmhoque7adb2772013-03-05 16:30:59 -060034 if ( nlsr->debugging )
35 printf("sign_content_with_user_defined_keystore called\n");
akmhoque7c64d802013-03-05 10:18:37 -060036
37
38 int res;
39
40
41 struct ccn_charbuf * pubid_out=ccn_charbuf_create();
42 struct ccn_charbuf * keyname;
43
44
45 struct ccn_keystore *keystore = NULL;
46 keystore=ccn_keystore_create();
47 res=ccn_keystore_init(keystore, keystore_path,keystore_passphrase );
48 if ( res < 0 ){
akmhoque7adb2772013-03-05 16:30:59 -060049 if ( nlsr->debugging )
50 printf("Error in initiating keystore :(\n");
akmhoque7c64d802013-03-05 10:18:37 -060051 ccn_keystore_destroy(&keystore);
52 return -1;
53 }
54
55
56 res=ccn_load_private_key (nlsr->ccn,
57 keystore_path,
akmhoque7adb2772013-03-05 16:30:59 -060058 keystore_passphrase,
akmhoque7c64d802013-03-05 10:18:37 -060059 pubid_out);
60
61 if(res < 0 ){
akmhoque7adb2772013-03-05 16:30:59 -060062 if ( nlsr->debugging )
63 printf("Error in loading keystore :( \n");
akmhoque7c64d802013-03-05 10:18:37 -060064 ccn_charbuf_destroy(&pubid_out);
65 return -1;
66 }
67
68 char *baseuri=(char *)calloc(strlen(key_repo_name)+strlen(site_name)+
69 strlen(router_name)+strlen("/%C1.R.N.Start")+5,sizeof(char));
70 memcpy(baseuri,key_repo_name,strlen(key_repo_name)+1);
akmhoque7adb2772013-03-05 16:30:59 -060071 if ( site_name[0] != '/')
72 memcpy(baseuri+strlen(baseuri),"/",1);
akmhoque7c64d802013-03-05 10:18:37 -060073 memcpy(baseuri+strlen(baseuri),site_name,strlen(site_name)+1);
74 memcpy(baseuri+strlen(baseuri),"/%C1.R.N.Start",strlen("/%C1.R.N.Start"));
75 memcpy(baseuri+strlen(baseuri),router_name,strlen(router_name)+1);
76 baseuri[strlen(baseuri)]='\0';
77
78
79 keyname=ccn_charbuf_create();
80 if(keyname == NULL ){
81 ccn_charbuf_destroy(&pubid_out);
82 free(baseuri);
83 return -1;
84 }
85 ccn_name_from_uri(keyname,baseuri);
86 if ( res < 0 ){
akmhoque7adb2772013-03-05 16:30:59 -060087 if ( nlsr->debugging )
88 printf("Bad URI format: %s\n",baseuri);
akmhoque7c64d802013-03-05 10:18:37 -060089 ccn_charbuf_destroy(&pubid_out);
90 ccn_charbuf_destroy(&keyname);
91 free(baseuri);
92 return -1;
93 }
akmhoque3098a312013-03-06 07:31:01 -060094
akmhoque7c64d802013-03-05 10:18:37 -060095 ccn_name_append_str(keyname,"nlsr");
96 struct ccn_charbuf *keyid = ccn_charbuf_create();
97 ccn_charbuf_append_value(keyid, CCN_MARKER_CONTROL, 1);
98 ccn_charbuf_append_string(keyid, ".M.K");
99 ccn_charbuf_append_value(keyid, 0, 1);
100 ccn_charbuf_append_charbuf(keyid, pubid_out);
101 ccn_name_append(keyname, keyid->buf, keyid->length);
102
103
104
105 struct ccn_charbuf *uri = ccn_charbuf_create();
106 ccn_uri_append(uri, keyname->buf, keyname->length, 0);
akmhoque7adb2772013-03-05 16:30:59 -0600107 if ( nlsr->debugging )
108 printf("Key Name Included when processing content: %s\n", ccn_charbuf_as_string(uri));
akmhoque7c64d802013-03-05 10:18:37 -0600109 ccn_charbuf_destroy(&uri);
110
111 struct ccn_signing_params sp = CCN_SIGNING_PARAMS_INIT;
112 sp.type = CCN_CONTENT_DATA;
113 sp.template_ccnb = ccn_charbuf_create();
114 ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_SignedInfo, CCN_DTAG);
115 ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_KeyLocator, CCN_DTAG);
116 ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_KeyName, CCN_DTAG);
117 ccn_charbuf_append(sp.template_ccnb, keyname->buf, keyname->length);
118 ccn_charbuf_append_closer(sp.template_ccnb); // KeyName closer
119 ccn_charbuf_append_closer(sp.template_ccnb); // KeyLocator closer
akmhoque2fafaa52013-03-22 05:10:52 -0500120
akmhoqueb9ab8db2013-03-22 05:27:16 -0500121 //ccnb_tagged_putf(sp.template_ccnb, CCN_DTAG_FreshnessSeconds, "%ld", freshness);
122 //sp.sp_flags |= CCN_SP_TEMPL_FRESHNESS;
akmhoque2fafaa52013-03-22 05:10:52 -0500123
akmhoque7c64d802013-03-05 10:18:37 -0600124 ccn_charbuf_append_closer(sp.template_ccnb); // SignedInfo closer
akmhoquedbf6acc2013-03-22 05:36:54 -0500125
126 ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_SignedInfo, CCN_DTAG);
127 ccnb_tagged_putf(sp.template_ccnb, CCN_DTAG_FreshnessSeconds, "%ld", freshness);
128 sp.sp_flags |= CCN_SP_TEMPL_FRESHNESS;
129 ccn_charbuf_append_closer(sp.template_ccnb);
akmhoque7c64d802013-03-05 10:18:37 -0600130
131 sp.sp_flags |= CCN_SP_TEMPL_KEY_LOCATOR;
132 sp.sp_flags |= CCN_SP_FINAL_BLOCK;
akmhoquedbf6acc2013-03-22 05:36:54 -0500133 //sp.freshness = freshness;
akmhoque7c64d802013-03-05 10:18:37 -0600134
135
136 if (pubid_out->length != sizeof(sp.pubid)){
akmhoque7adb2772013-03-05 16:30:59 -0600137 if ( nlsr->debugging )
138 printf("Size of pubid and sp.pubid is not equal");
akmhoque7c64d802013-03-05 10:18:37 -0600139 ccn_charbuf_destroy(&keyname);
140 ccn_charbuf_destroy(&pubid_out);
141 free(baseuri);
142 return -1;
143 }
144
145 memcpy(sp.pubid, pubid_out->buf, pubid_out->length);
146
147
148
149 res=ccn_sign_content(nlsr->ccn,resultbuf,content_name,&sp,data,data_size);
150 if( res < 0 ){
akmhoque7adb2772013-03-05 16:30:59 -0600151 if ( nlsr->debugging )
152 printf("Content signing error \n");
akmhoque7c64d802013-03-05 10:18:37 -0600153 ccn_charbuf_destroy(&sp.template_ccnb);
154 ccn_charbuf_destroy(&keyid);
155 ccn_charbuf_destroy(&keyname);
156 ccn_charbuf_destroy(&pubid_out);
157 free(baseuri);
158 return -1;
159 }
160
akmhoque284056b2013-03-15 01:20:16 -0500161 ccn_keystore_destroy(&keystore);
akmhoque7c64d802013-03-05 10:18:37 -0600162 ccn_charbuf_destroy(&sp.template_ccnb);
163 ccn_charbuf_destroy(&keyid);
164 ccn_charbuf_destroy(&keyname);
165 ccn_charbuf_destroy(&pubid_out);
akmhoque284056b2013-03-15 01:20:16 -0500166
akmhoque7c64d802013-03-05 10:18:37 -0600167 free(baseuri);
168 return 0;
169}
170
171
akmhoqueb8dbba82013-03-11 11:34:17 -0500172char *
173get_orig_router_from_lsa_name(struct ccn_charbuf * content_name)
174{
175 int start=0;
176
177 size_t comp_size;
178 const unsigned char *second_last_comp;
179 char *second_comp_type;
180 char *sep=".";
181 char *rem;
182
183 struct ccn_indexbuf *components=ccn_indexbuf_create();
184 struct ccn_charbuf *name=ccn_charbuf_create();
akmhoqueb7958182013-03-11 12:03:54 -0500185 ccn_name_from_uri(name,nlsr->slice_prefix);
akmhoqueb8dbba82013-03-11 11:34:17 -0500186 ccn_name_split (name, components);
187 start=components->n-2;
188 ccn_charbuf_destroy(&name);
189 ccn_indexbuf_destroy(&components);
190
191 struct ccn_indexbuf *comps=ccn_indexbuf_create();
192 ccn_name_split (content_name, comps);
193 ccn_name_comp_get( content_name->buf, comps,
194 comps->n-1-2, &second_last_comp, &comp_size);
195
196 second_comp_type=strtok_r((char *)second_last_comp, sep, &rem);
197 if ( strcmp( second_comp_type, "lsId" ) == 0 ){
198 ccn_name_chop(content_name,comps,-3);
199 }
200 else{
201 ccn_name_chop(content_name,comps,-2);
202 }
203
204
205 struct ccn_charbuf *temp=ccn_charbuf_create();
206 ccn_name_init(temp);
207 ccn_name_append_components( temp, content_name->buf,
208 comps->buf[start+1],
209 comps->buf[comps->n - 1]);
210
211 struct ccn_charbuf *temp1=ccn_charbuf_create();
212 ccn_uri_append(temp1, temp->buf, temp->length, 0);
213
214 char *orig_router=(char *)calloc(strlen(ccn_charbuf_as_string(temp1))+1,
215 sizeof(char));
216 memcpy(orig_router,ccn_charbuf_as_string(temp1),
217 strlen(ccn_charbuf_as_string(temp1)));
218 orig_router[strlen(orig_router)]='\0';
219
220 ccn_charbuf_destroy(&temp);
221 ccn_charbuf_destroy(&temp1);
222 ccn_indexbuf_destroy(&comps);
223 return orig_router;
224
225
226}
227
228
akmhoqueb7958182013-03-11 12:03:54 -0500229char *
230get_orig_router_from_info_content_name(struct ccn_charbuf * content_name)
231{
232 int start,end;
233
234 start=0;
235
236 struct ccn_indexbuf *comps=ccn_indexbuf_create();
237 ccn_name_split (content_name, comps);
238
239 end=check_for_name_component_in_name(content_name,comps,"nlsr");
240
241
242 struct ccn_charbuf *temp=ccn_charbuf_create();
243 ccn_name_init(temp);
244 ccn_name_append_components( temp, content_name->buf,
245 comps->buf[start],
246 comps->buf[end]);
247
248 struct ccn_charbuf *temp1=ccn_charbuf_create();
249 ccn_uri_append(temp1, temp->buf, temp->length, 0);
250
251 char *orig_router=(char *)calloc(strlen(ccn_charbuf_as_string(temp1))+1,
252 sizeof(char));
253 memcpy(orig_router,ccn_charbuf_as_string(temp1),
254 strlen(ccn_charbuf_as_string(temp1)));
255 orig_router[strlen(orig_router)]='\0';
256
257 ccn_charbuf_destroy(&temp);
258 ccn_charbuf_destroy(&temp1);
259 ccn_indexbuf_destroy(&comps);
260 return orig_router;
261
262
263}
264
akmhoqueb8dbba82013-03-11 11:34:17 -0500265
266int
267check_key_name_hierarchy(const unsigned char *ccnb,
268 struct ccn_parsed_ContentObject *pco,
akmhoqueb7958182013-03-11 12:03:54 -0500269 int key_type, int content_type){
akmhoquee2901222013-03-15 00:59:54 -0500270 if ( nlsr->debugging )
271 printf("check_key_name_hierarchy called\n");
akmhoqueb8dbba82013-03-11 11:34:17 -0500272 if (key_type == UNKNOWN_KEY ){
273 return 1;
274 }
akmhoque5c68a642013-03-15 00:28:16 -0500275 //int res;
akmhoqueb8dbba82013-03-11 11:34:17 -0500276 struct ccn_charbuf *key_name=get_key_name(ccnb, pco);
277
278 struct ccn_charbuf *key_uri = ccn_charbuf_create();
279 ccn_uri_append(key_uri, key_name->buf, key_name->length, 0);
akmhoquee2901222013-03-15 00:59:54 -0500280 if ( nlsr->debugging )
281 printf("Key Name: %s\n",ccn_charbuf_as_string(key_uri));
akmhoqueb8dbba82013-03-11 11:34:17 -0500282 ccn_charbuf_destroy(&key_uri);
283
284 struct ccn_charbuf *content_name=ccn_charbuf_create();
akmhoquea33345f2013-03-15 00:30:03 -0500285 ccn_charbuf_append(content_name, ccnb + pco->offset[CCN_PCO_B_Name],
akmhoqueb8dbba82013-03-11 11:34:17 -0500286 pco->offset[CCN_PCO_E_Name] - pco->offset[CCN_PCO_B_Name]);
287
288 struct ccn_charbuf *content_uri = ccn_charbuf_create();
289 ccn_uri_append(content_uri, content_name->buf, content_name->length, 0);
akmhoquee2901222013-03-15 00:59:54 -0500290 if ( nlsr->debugging )
291 printf("Content Name: %s\n",ccn_charbuf_as_string(content_uri));
akmhoqueb8dbba82013-03-11 11:34:17 -0500292 ccn_charbuf_destroy(&content_uri);
293
294 if ( key_type == NLSR_KEY){
295 char *orig_router_key_name=get_orig_router_from_key_name(key_name,0,0);
akmhoqueb7958182013-03-11 12:03:54 -0500296 char *orig_router_content_name;
297 if ( content_type == 1 ){
298 orig_router_content_name=get_orig_router_from_lsa_name(content_name);
299 }
300 else if ( content_type == 0 ){
301 orig_router_content_name=get_orig_router_from_info_content_name(content_name);
302 }
akmhoquee2901222013-03-15 00:59:54 -0500303 if ( nlsr->debugging ){
304 printf("Orig Router (Key Name):%s\n",orig_router_key_name);
305 printf("Orig Router (Content Name):%s\n",orig_router_content_name);
306 }
akmhoqueb8dbba82013-03-11 11:34:17 -0500307
308 if (strcmp(orig_router_key_name,orig_router_content_name) == 0 ){
309 free(orig_router_key_name);
310 free(orig_router_content_name);
311 ccn_charbuf_destroy(&key_name);
312 ccn_charbuf_destroy(&content_name);
313 return 1;
314 }
315 }
316
317 if ( key_type == ROUTING_KEY){
318 char *orig_router_key_name=get_orig_router_from_key_name(key_name,1,0);
319 char *orig_router_content_name=get_orig_router_from_key_name(content_name,1,1);
akmhoquee2901222013-03-15 00:59:54 -0500320 if ( nlsr->debugging ){
321 printf("Orig Router (Key Name):%s\n",orig_router_key_name);
322 printf("Orig Router (Content Name):%s\n",orig_router_content_name);
323 }
akmhoqueb8dbba82013-03-11 11:34:17 -0500324
325 if (strcmp(orig_router_key_name,orig_router_content_name) == 0 ){
326 free(orig_router_key_name);
327 free(orig_router_content_name);
328 ccn_charbuf_destroy(&key_name);
329 ccn_charbuf_destroy(&content_name);
330 return 1;
331 }
332 }
333 if ( key_type == OPERATOR_KEY){
334 struct ccn_indexbuf *key_name_comps;
335 key_name_comps = ccn_indexbuf_create();
akmhoquea33345f2013-03-15 00:30:03 -0500336 ccn_name_split(key_name, key_name_comps);
akmhoqueb8dbba82013-03-11 11:34:17 -0500337 int last_indx=check_for_tag_component_in_name(key_name,key_name_comps,"O.N.Start");
338 char *site_key_prefix_key=get_name_segments_from_name(key_name,0,last_indx);
akmhoquee2901222013-03-15 00:59:54 -0500339 if ( nlsr->debugging )
340 printf("Site key prefix(key Name):%s\n",site_key_prefix_key);
akmhoqueb8dbba82013-03-11 11:34:17 -0500341 ccn_indexbuf_destroy(&key_name_comps);
342
343 struct ccn_indexbuf *content_name_comps;
344 content_name_comps = ccn_indexbuf_create();
akmhoquea33345f2013-03-15 00:30:03 -0500345 ccn_name_split(content_name, content_name_comps);
akmhoqueb8dbba82013-03-11 11:34:17 -0500346 int last_indx_rtr=check_for_tag_component_in_name(content_name,content_name_comps,"R.N.Start");
347 char *site_key_prefix_content=get_name_segments_from_name(key_name,0,last_indx_rtr);
akmhoquee2901222013-03-15 00:59:54 -0500348 if ( nlsr->debugging )
349 printf("Site key prefix(Content Name):%s\n",site_key_prefix_content);
akmhoqueb8dbba82013-03-11 11:34:17 -0500350 ccn_indexbuf_destroy(&content_name_comps);
351
352 if( strcmp(site_key_prefix_key,site_key_prefix_content) == 0 ){
353 free(site_key_prefix_key);
354 free(site_key_prefix_content);
355 ccn_charbuf_destroy(&key_name);
356 ccn_charbuf_destroy(&content_name);
357 return 1;
358 }
359
360 }
361
362 if ( key_type == SITE_KEY){
363 struct ccn_indexbuf *key_name_comps;
364 key_name_comps = ccn_indexbuf_create();
akmhoquea33345f2013-03-15 00:30:03 -0500365 ccn_name_split(key_name, key_name_comps);
akmhoqueb8dbba82013-03-11 11:34:17 -0500366 int last_indx=check_for_tag_component_in_name(key_name,key_name_comps,"M.K");
367 char *site_key_prefix_key=get_name_segments_from_name(key_name,0,last_indx);
akmhoquee2901222013-03-15 00:59:54 -0500368 if ( nlsr->debugging )
369 printf("Site key prefix(key Name):%s\n",site_key_prefix_key);
akmhoqueb8dbba82013-03-11 11:34:17 -0500370 ccn_indexbuf_destroy(&key_name_comps);
371
372 struct ccn_indexbuf *content_name_comps;
373 content_name_comps = ccn_indexbuf_create();
akmhoquea33345f2013-03-15 00:30:03 -0500374 ccn_name_split(content_name, content_name_comps);
akmhoqueb8dbba82013-03-11 11:34:17 -0500375 int last_indx_rtr=check_for_tag_component_in_name(content_name,content_name_comps,"O.N.Start");
376 char *site_key_prefix_content=get_name_segments_from_name(key_name,0,last_indx_rtr);
akmhoquee2901222013-03-15 00:59:54 -0500377 if ( nlsr->debugging )
378 printf("Site key prefix(Content Name):%s\n",site_key_prefix_content);
akmhoqueb8dbba82013-03-11 11:34:17 -0500379 ccn_indexbuf_destroy(&content_name_comps);
380
381 if( strcmp(site_key_prefix_key,site_key_prefix_content) == 0 ){
382 free(site_key_prefix_key);
383 free(site_key_prefix_content);
384 ccn_charbuf_destroy(&key_name);
385 ccn_charbuf_destroy(&content_name);
386 return 1;
387 }
388
389 }
390
391 if ( key_type == ROOT_KEY){
392 ccn_charbuf_destroy(&key_name);
393 ccn_charbuf_destroy(&content_name);
394 return 1;
395 }
396
397 ccn_charbuf_destroy(&key_name);
398 ccn_charbuf_destroy(&content_name);
399 return 0;
400}
401
akmhoque7c64d802013-03-05 10:18:37 -0600402int
akmhoque6e2ba842013-03-05 19:35:26 -0600403verify_key(const unsigned char *ccnb,
akmhoqueb7958182013-03-11 12:03:54 -0500404 struct ccn_parsed_ContentObject *pco,
405 int content_type){
akmhoque7adb2772013-03-05 16:30:59 -0600406 if ( nlsr->debugging )
407 printf("verify key called\n");
akmhoque7c64d802013-03-05 10:18:37 -0600408 int ret=-1;
akmhoqueb8dbba82013-03-11 11:34:17 -0500409
akmhoque7c64d802013-03-05 10:18:37 -0600410 if ( contain_key_name(ccnb, pco) == 1){
411
412 struct ccn_charbuf *key_name=get_key_name(ccnb, pco);
413 struct ccn_charbuf *key_uri = ccn_charbuf_create();
414 ccn_uri_append(key_uri, key_name->buf, key_name->length, 0);
akmhoque7adb2772013-03-05 16:30:59 -0600415 if ( nlsr->debugging )
416 printf("Key Name from Incoming Content: %s\n",ccn_charbuf_as_string(key_uri));
akmhoqueb8dbba82013-03-11 11:34:17 -0500417 int key_type=get_key_type_from_key_name(key_name);
akmhoque7adb2772013-03-05 16:30:59 -0600418 if ( nlsr->debugging )
akmhoqueb8dbba82013-03-11 11:34:17 -0500419 printf("Key Type: %d \n",key_type);
akmhoque7c64d802013-03-05 10:18:37 -0600420
421 struct ccn_charbuf *result = ccn_charbuf_create();
422 struct ccn_parsed_ContentObject temp_pco = {0};
423 int get_flags = 0;
424 get_flags |= CCN_GET_NOKEYWAIT;
425 int counter = 0;
426 while(ccn_get(nlsr->ccn, key_name, NULL, 500, result, &temp_pco, NULL,
427 get_flags) < 0 && counter < 3) counter++;
428
429 int chk_verify=ccn_verify_content(nlsr->ccn,ccnb,pco);
430
431 if ( chk_verify == 0 ){
akmhoque7adb2772013-03-05 16:30:59 -0600432 if ( nlsr->debugging )
akmhoqueb7958182013-03-11 12:03:54 -0500433 printf("Content verification Successful :)\n");
akmhoque7c64d802013-03-05 10:18:37 -0600434
435 if ( counter == 3){
akmhoque7adb2772013-03-05 16:30:59 -0600436 if ( nlsr->debugging )
437 printf("Could not retrieve key by name !!!\n");
akmhoque7c64d802013-03-05 10:18:37 -0600438 }
439 else{
akmhoqueb8dbba82013-03-11 11:34:17 -0500440 if ( key_type == ROOT_KEY ){
akmhoque7c64d802013-03-05 10:18:37 -0600441 ret=0;
442 }
443 else{
akmhoqueb8dbba82013-03-11 11:34:17 -0500444 if ( nlsr->isStrictHierchicalKeyCheck ){
akmhoqueb7958182013-03-11 12:03:54 -0500445 int key_name_test=check_key_name_hierarchy(ccnb,
446 pco,
447 key_type,
448 content_type);
akmhoqueb8dbba82013-03-11 11:34:17 -0500449 if ( key_name_test == 1){
akmhoqueb7958182013-03-11 12:03:54 -0500450 ret=verify_key(result->buf,&temp_pco,content_type);
akmhoqueb8dbba82013-03-11 11:34:17 -0500451 }
452 }
453 else{
akmhoqueb7958182013-03-11 12:03:54 -0500454 ret=verify_key(result->buf,&temp_pco,content_type);
akmhoqueb8dbba82013-03-11 11:34:17 -0500455 }
akmhoque7c64d802013-03-05 10:18:37 -0600456 }
457 }
458 }
459 ccn_charbuf_destroy(&result);
460 ccn_charbuf_destroy(&key_uri);
461 ccn_charbuf_destroy(&key_name);
462 return ret;
463 }
464
465 return ret;
466}
467
akmhoque237239c2013-03-18 10:29:26 -0500468void
469destroy_keys(void)
470{
471 int i, key_element;
472 struct nlsr_key *key;
473
474 struct hashtb_enumerator ee;
475 struct hashtb_enumerator *e = &ee;
476
477 hashtb_start(nlsr->keys, e);
478 key_element=hashtb_n(nlsr->keys);
479
480 for(i=0;i<key_element;i++)
481 {
482 key=e->data;
483 free(key->key_name);
484 hashtb_next(e);
485 }
486
487 hashtb_end(e);
488
489 if( nlsr->keys )
490 hashtb_destroy(&nlsr->keys);
491
492}
493
494void
495print_keys(void){
496
497 if ( nlsr->debugging )
498 printf("print_keys called \n");
499 if ( nlsr->detailed_logging )
500 writeLogg(__FILE__,__FUNCTION__,__LINE__,"print_keys called \n");
501
502 int i, key_element;
503 struct nlsr_key *key;
504
505 struct hashtb_enumerator ee;
506 struct hashtb_enumerator *e = &ee;
507
508 hashtb_start(nlsr->keys, e);
509 key_element=hashtb_n(nlsr->keys);
510
511 for(i=0;i<key_element;i++)
512 {
513 key=e->data;
514
515 if ( nlsr->debugging )
516 printf("Key : %s \n",key->key_name);
517
518 hashtb_next(e);
519 }
520
521 hashtb_end(e);
522
523}
524
525int
526does_key_exist(char *keyname){
527 if (nlsr->debugging)
528 {
529 printf("does_key_exist called\n");
530 printf("Keyname : %s \n",keyname);
531 }
532
533 int ret=0;
534
535 unsigned *v;
536 v = hashtb_lookup(nlsr->keys, keyname, strlen(keyname));
537 if (v != NULL){
538 ret = 1;
539 if (nlsr->debugging)
540 printf("Key Found\n");
541 }
542
543 return ret;
544}
545
546void
547add_key(char *keyname){
548 if (nlsr->debugging)
549 {
550 printf("add_key called\n");
551 printf("Keyname : %s \n",keyname);
552 }
553
554 struct nlsr_key *key;
555
556 struct hashtb_enumerator ee;
557 struct hashtb_enumerator *e = &ee;
558 int res;
559
560 hashtb_start(nlsr->keys, e);
561 res = hashtb_seek(e, keyname, strlen(keyname), 0);
562
563 if(res == HT_NEW_ENTRY )
564 {
565 key=e->data;
566 key->key_name=(char *)calloc(strlen(keyname)+1,sizeof(char));
567 memcpy(key->key_name,keyname,strlen(keyname)+1);
568 }
569
570 if (nlsr->debugging)
571 print_keys();
572}