blob: b8b56d63fc2de093a6d552f3ba94f88322b0645d [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
125
126 sp.sp_flags |= CCN_SP_TEMPL_KEY_LOCATOR;
127 sp.sp_flags |= CCN_SP_FINAL_BLOCK;
akmhoqueb9ab8db2013-03-22 05:27:16 -0500128 sp.freshness = freshness;
akmhoque7c64d802013-03-05 10:18:37 -0600129
130
131 if (pubid_out->length != sizeof(sp.pubid)){
akmhoque7adb2772013-03-05 16:30:59 -0600132 if ( nlsr->debugging )
133 printf("Size of pubid and sp.pubid is not equal");
akmhoque7c64d802013-03-05 10:18:37 -0600134 ccn_charbuf_destroy(&keyname);
135 ccn_charbuf_destroy(&pubid_out);
136 free(baseuri);
137 return -1;
138 }
139
140 memcpy(sp.pubid, pubid_out->buf, pubid_out->length);
141
142
143
144 res=ccn_sign_content(nlsr->ccn,resultbuf,content_name,&sp,data,data_size);
145 if( res < 0 ){
akmhoque7adb2772013-03-05 16:30:59 -0600146 if ( nlsr->debugging )
147 printf("Content signing error \n");
akmhoque7c64d802013-03-05 10:18:37 -0600148 ccn_charbuf_destroy(&sp.template_ccnb);
149 ccn_charbuf_destroy(&keyid);
150 ccn_charbuf_destroy(&keyname);
151 ccn_charbuf_destroy(&pubid_out);
152 free(baseuri);
153 return -1;
154 }
155
akmhoque284056b2013-03-15 01:20:16 -0500156 ccn_keystore_destroy(&keystore);
akmhoque7c64d802013-03-05 10:18:37 -0600157 ccn_charbuf_destroy(&sp.template_ccnb);
158 ccn_charbuf_destroy(&keyid);
159 ccn_charbuf_destroy(&keyname);
160 ccn_charbuf_destroy(&pubid_out);
akmhoque284056b2013-03-15 01:20:16 -0500161
akmhoque7c64d802013-03-05 10:18:37 -0600162 free(baseuri);
163 return 0;
164}
165
166
akmhoqueb8dbba82013-03-11 11:34:17 -0500167char *
168get_orig_router_from_lsa_name(struct ccn_charbuf * content_name)
169{
170 int start=0;
171
172 size_t comp_size;
173 const unsigned char *second_last_comp;
174 char *second_comp_type;
175 char *sep=".";
176 char *rem;
177
178 struct ccn_indexbuf *components=ccn_indexbuf_create();
179 struct ccn_charbuf *name=ccn_charbuf_create();
akmhoqueb7958182013-03-11 12:03:54 -0500180 ccn_name_from_uri(name,nlsr->slice_prefix);
akmhoqueb8dbba82013-03-11 11:34:17 -0500181 ccn_name_split (name, components);
182 start=components->n-2;
183 ccn_charbuf_destroy(&name);
184 ccn_indexbuf_destroy(&components);
185
186 struct ccn_indexbuf *comps=ccn_indexbuf_create();
187 ccn_name_split (content_name, comps);
188 ccn_name_comp_get( content_name->buf, comps,
189 comps->n-1-2, &second_last_comp, &comp_size);
190
191 second_comp_type=strtok_r((char *)second_last_comp, sep, &rem);
192 if ( strcmp( second_comp_type, "lsId" ) == 0 ){
193 ccn_name_chop(content_name,comps,-3);
194 }
195 else{
196 ccn_name_chop(content_name,comps,-2);
197 }
198
199
200 struct ccn_charbuf *temp=ccn_charbuf_create();
201 ccn_name_init(temp);
202 ccn_name_append_components( temp, content_name->buf,
203 comps->buf[start+1],
204 comps->buf[comps->n - 1]);
205
206 struct ccn_charbuf *temp1=ccn_charbuf_create();
207 ccn_uri_append(temp1, temp->buf, temp->length, 0);
208
209 char *orig_router=(char *)calloc(strlen(ccn_charbuf_as_string(temp1))+1,
210 sizeof(char));
211 memcpy(orig_router,ccn_charbuf_as_string(temp1),
212 strlen(ccn_charbuf_as_string(temp1)));
213 orig_router[strlen(orig_router)]='\0';
214
215 ccn_charbuf_destroy(&temp);
216 ccn_charbuf_destroy(&temp1);
217 ccn_indexbuf_destroy(&comps);
218 return orig_router;
219
220
221}
222
223
akmhoqueb7958182013-03-11 12:03:54 -0500224char *
225get_orig_router_from_info_content_name(struct ccn_charbuf * content_name)
226{
227 int start,end;
228
229 start=0;
230
231 struct ccn_indexbuf *comps=ccn_indexbuf_create();
232 ccn_name_split (content_name, comps);
233
234 end=check_for_name_component_in_name(content_name,comps,"nlsr");
235
236
237 struct ccn_charbuf *temp=ccn_charbuf_create();
238 ccn_name_init(temp);
239 ccn_name_append_components( temp, content_name->buf,
240 comps->buf[start],
241 comps->buf[end]);
242
243 struct ccn_charbuf *temp1=ccn_charbuf_create();
244 ccn_uri_append(temp1, temp->buf, temp->length, 0);
245
246 char *orig_router=(char *)calloc(strlen(ccn_charbuf_as_string(temp1))+1,
247 sizeof(char));
248 memcpy(orig_router,ccn_charbuf_as_string(temp1),
249 strlen(ccn_charbuf_as_string(temp1)));
250 orig_router[strlen(orig_router)]='\0';
251
252 ccn_charbuf_destroy(&temp);
253 ccn_charbuf_destroy(&temp1);
254 ccn_indexbuf_destroy(&comps);
255 return orig_router;
256
257
258}
259
akmhoqueb8dbba82013-03-11 11:34:17 -0500260
261int
262check_key_name_hierarchy(const unsigned char *ccnb,
263 struct ccn_parsed_ContentObject *pco,
akmhoqueb7958182013-03-11 12:03:54 -0500264 int key_type, int content_type){
akmhoquee2901222013-03-15 00:59:54 -0500265 if ( nlsr->debugging )
266 printf("check_key_name_hierarchy called\n");
akmhoqueb8dbba82013-03-11 11:34:17 -0500267 if (key_type == UNKNOWN_KEY ){
268 return 1;
269 }
akmhoque5c68a642013-03-15 00:28:16 -0500270 //int res;
akmhoqueb8dbba82013-03-11 11:34:17 -0500271 struct ccn_charbuf *key_name=get_key_name(ccnb, pco);
272
273 struct ccn_charbuf *key_uri = ccn_charbuf_create();
274 ccn_uri_append(key_uri, key_name->buf, key_name->length, 0);
akmhoquee2901222013-03-15 00:59:54 -0500275 if ( nlsr->debugging )
276 printf("Key Name: %s\n",ccn_charbuf_as_string(key_uri));
akmhoqueb8dbba82013-03-11 11:34:17 -0500277 ccn_charbuf_destroy(&key_uri);
278
279 struct ccn_charbuf *content_name=ccn_charbuf_create();
akmhoquea33345f2013-03-15 00:30:03 -0500280 ccn_charbuf_append(content_name, ccnb + pco->offset[CCN_PCO_B_Name],
akmhoqueb8dbba82013-03-11 11:34:17 -0500281 pco->offset[CCN_PCO_E_Name] - pco->offset[CCN_PCO_B_Name]);
282
283 struct ccn_charbuf *content_uri = ccn_charbuf_create();
284 ccn_uri_append(content_uri, content_name->buf, content_name->length, 0);
akmhoquee2901222013-03-15 00:59:54 -0500285 if ( nlsr->debugging )
286 printf("Content Name: %s\n",ccn_charbuf_as_string(content_uri));
akmhoqueb8dbba82013-03-11 11:34:17 -0500287 ccn_charbuf_destroy(&content_uri);
288
289 if ( key_type == NLSR_KEY){
290 char *orig_router_key_name=get_orig_router_from_key_name(key_name,0,0);
akmhoqueb7958182013-03-11 12:03:54 -0500291 char *orig_router_content_name;
292 if ( content_type == 1 ){
293 orig_router_content_name=get_orig_router_from_lsa_name(content_name);
294 }
295 else if ( content_type == 0 ){
296 orig_router_content_name=get_orig_router_from_info_content_name(content_name);
297 }
akmhoquee2901222013-03-15 00:59:54 -0500298 if ( nlsr->debugging ){
299 printf("Orig Router (Key Name):%s\n",orig_router_key_name);
300 printf("Orig Router (Content Name):%s\n",orig_router_content_name);
301 }
akmhoqueb8dbba82013-03-11 11:34:17 -0500302
303 if (strcmp(orig_router_key_name,orig_router_content_name) == 0 ){
304 free(orig_router_key_name);
305 free(orig_router_content_name);
306 ccn_charbuf_destroy(&key_name);
307 ccn_charbuf_destroy(&content_name);
308 return 1;
309 }
310 }
311
312 if ( key_type == ROUTING_KEY){
313 char *orig_router_key_name=get_orig_router_from_key_name(key_name,1,0);
314 char *orig_router_content_name=get_orig_router_from_key_name(content_name,1,1);
akmhoquee2901222013-03-15 00:59:54 -0500315 if ( nlsr->debugging ){
316 printf("Orig Router (Key Name):%s\n",orig_router_key_name);
317 printf("Orig Router (Content Name):%s\n",orig_router_content_name);
318 }
akmhoqueb8dbba82013-03-11 11:34:17 -0500319
320 if (strcmp(orig_router_key_name,orig_router_content_name) == 0 ){
321 free(orig_router_key_name);
322 free(orig_router_content_name);
323 ccn_charbuf_destroy(&key_name);
324 ccn_charbuf_destroy(&content_name);
325 return 1;
326 }
327 }
328 if ( key_type == OPERATOR_KEY){
329 struct ccn_indexbuf *key_name_comps;
330 key_name_comps = ccn_indexbuf_create();
akmhoquea33345f2013-03-15 00:30:03 -0500331 ccn_name_split(key_name, key_name_comps);
akmhoqueb8dbba82013-03-11 11:34:17 -0500332 int last_indx=check_for_tag_component_in_name(key_name,key_name_comps,"O.N.Start");
333 char *site_key_prefix_key=get_name_segments_from_name(key_name,0,last_indx);
akmhoquee2901222013-03-15 00:59:54 -0500334 if ( nlsr->debugging )
335 printf("Site key prefix(key Name):%s\n",site_key_prefix_key);
akmhoqueb8dbba82013-03-11 11:34:17 -0500336 ccn_indexbuf_destroy(&key_name_comps);
337
338 struct ccn_indexbuf *content_name_comps;
339 content_name_comps = ccn_indexbuf_create();
akmhoquea33345f2013-03-15 00:30:03 -0500340 ccn_name_split(content_name, content_name_comps);
akmhoqueb8dbba82013-03-11 11:34:17 -0500341 int last_indx_rtr=check_for_tag_component_in_name(content_name,content_name_comps,"R.N.Start");
342 char *site_key_prefix_content=get_name_segments_from_name(key_name,0,last_indx_rtr);
akmhoquee2901222013-03-15 00:59:54 -0500343 if ( nlsr->debugging )
344 printf("Site key prefix(Content Name):%s\n",site_key_prefix_content);
akmhoqueb8dbba82013-03-11 11:34:17 -0500345 ccn_indexbuf_destroy(&content_name_comps);
346
347 if( strcmp(site_key_prefix_key,site_key_prefix_content) == 0 ){
348 free(site_key_prefix_key);
349 free(site_key_prefix_content);
350 ccn_charbuf_destroy(&key_name);
351 ccn_charbuf_destroy(&content_name);
352 return 1;
353 }
354
355 }
356
357 if ( key_type == SITE_KEY){
358 struct ccn_indexbuf *key_name_comps;
359 key_name_comps = ccn_indexbuf_create();
akmhoquea33345f2013-03-15 00:30:03 -0500360 ccn_name_split(key_name, key_name_comps);
akmhoqueb8dbba82013-03-11 11:34:17 -0500361 int last_indx=check_for_tag_component_in_name(key_name,key_name_comps,"M.K");
362 char *site_key_prefix_key=get_name_segments_from_name(key_name,0,last_indx);
akmhoquee2901222013-03-15 00:59:54 -0500363 if ( nlsr->debugging )
364 printf("Site key prefix(key Name):%s\n",site_key_prefix_key);
akmhoqueb8dbba82013-03-11 11:34:17 -0500365 ccn_indexbuf_destroy(&key_name_comps);
366
367 struct ccn_indexbuf *content_name_comps;
368 content_name_comps = ccn_indexbuf_create();
akmhoquea33345f2013-03-15 00:30:03 -0500369 ccn_name_split(content_name, content_name_comps);
akmhoqueb8dbba82013-03-11 11:34:17 -0500370 int last_indx_rtr=check_for_tag_component_in_name(content_name,content_name_comps,"O.N.Start");
371 char *site_key_prefix_content=get_name_segments_from_name(key_name,0,last_indx_rtr);
akmhoquee2901222013-03-15 00:59:54 -0500372 if ( nlsr->debugging )
373 printf("Site key prefix(Content Name):%s\n",site_key_prefix_content);
akmhoqueb8dbba82013-03-11 11:34:17 -0500374 ccn_indexbuf_destroy(&content_name_comps);
375
376 if( strcmp(site_key_prefix_key,site_key_prefix_content) == 0 ){
377 free(site_key_prefix_key);
378 free(site_key_prefix_content);
379 ccn_charbuf_destroy(&key_name);
380 ccn_charbuf_destroy(&content_name);
381 return 1;
382 }
383
384 }
385
386 if ( key_type == ROOT_KEY){
387 ccn_charbuf_destroy(&key_name);
388 ccn_charbuf_destroy(&content_name);
389 return 1;
390 }
391
392 ccn_charbuf_destroy(&key_name);
393 ccn_charbuf_destroy(&content_name);
394 return 0;
395}
396
akmhoque7c64d802013-03-05 10:18:37 -0600397int
akmhoque6e2ba842013-03-05 19:35:26 -0600398verify_key(const unsigned char *ccnb,
akmhoqueb7958182013-03-11 12:03:54 -0500399 struct ccn_parsed_ContentObject *pco,
400 int content_type){
akmhoque7adb2772013-03-05 16:30:59 -0600401 if ( nlsr->debugging )
402 printf("verify key called\n");
akmhoque7c64d802013-03-05 10:18:37 -0600403 int ret=-1;
akmhoqueb8dbba82013-03-11 11:34:17 -0500404
akmhoque7c64d802013-03-05 10:18:37 -0600405 if ( contain_key_name(ccnb, pco) == 1){
406
407 struct ccn_charbuf *key_name=get_key_name(ccnb, pco);
408 struct ccn_charbuf *key_uri = ccn_charbuf_create();
409 ccn_uri_append(key_uri, key_name->buf, key_name->length, 0);
akmhoque7adb2772013-03-05 16:30:59 -0600410 if ( nlsr->debugging )
411 printf("Key Name from Incoming Content: %s\n",ccn_charbuf_as_string(key_uri));
akmhoqueb8dbba82013-03-11 11:34:17 -0500412 int key_type=get_key_type_from_key_name(key_name);
akmhoque7adb2772013-03-05 16:30:59 -0600413 if ( nlsr->debugging )
akmhoqueb8dbba82013-03-11 11:34:17 -0500414 printf("Key Type: %d \n",key_type);
akmhoque7c64d802013-03-05 10:18:37 -0600415
416 struct ccn_charbuf *result = ccn_charbuf_create();
417 struct ccn_parsed_ContentObject temp_pco = {0};
418 int get_flags = 0;
419 get_flags |= CCN_GET_NOKEYWAIT;
420 int counter = 0;
421 while(ccn_get(nlsr->ccn, key_name, NULL, 500, result, &temp_pco, NULL,
422 get_flags) < 0 && counter < 3) counter++;
423
424 int chk_verify=ccn_verify_content(nlsr->ccn,ccnb,pco);
425
426 if ( chk_verify == 0 ){
akmhoque7adb2772013-03-05 16:30:59 -0600427 if ( nlsr->debugging )
akmhoqueb7958182013-03-11 12:03:54 -0500428 printf("Content verification Successful :)\n");
akmhoque7c64d802013-03-05 10:18:37 -0600429
430 if ( counter == 3){
akmhoque7adb2772013-03-05 16:30:59 -0600431 if ( nlsr->debugging )
432 printf("Could not retrieve key by name !!!\n");
akmhoque7c64d802013-03-05 10:18:37 -0600433 }
434 else{
akmhoqueb8dbba82013-03-11 11:34:17 -0500435 if ( key_type == ROOT_KEY ){
akmhoque7c64d802013-03-05 10:18:37 -0600436 ret=0;
437 }
438 else{
akmhoqueb8dbba82013-03-11 11:34:17 -0500439 if ( nlsr->isStrictHierchicalKeyCheck ){
akmhoqueb7958182013-03-11 12:03:54 -0500440 int key_name_test=check_key_name_hierarchy(ccnb,
441 pco,
442 key_type,
443 content_type);
akmhoqueb8dbba82013-03-11 11:34:17 -0500444 if ( key_name_test == 1){
akmhoqueb7958182013-03-11 12:03:54 -0500445 ret=verify_key(result->buf,&temp_pco,content_type);
akmhoqueb8dbba82013-03-11 11:34:17 -0500446 }
447 }
448 else{
akmhoqueb7958182013-03-11 12:03:54 -0500449 ret=verify_key(result->buf,&temp_pco,content_type);
akmhoqueb8dbba82013-03-11 11:34:17 -0500450 }
akmhoque7c64d802013-03-05 10:18:37 -0600451 }
452 }
453 }
454 ccn_charbuf_destroy(&result);
455 ccn_charbuf_destroy(&key_uri);
456 ccn_charbuf_destroy(&key_name);
457 return ret;
458 }
459
460 return ret;
461}
462
akmhoque237239c2013-03-18 10:29:26 -0500463void
464destroy_keys(void)
465{
466 int i, key_element;
467 struct nlsr_key *key;
468
469 struct hashtb_enumerator ee;
470 struct hashtb_enumerator *e = &ee;
471
472 hashtb_start(nlsr->keys, e);
473 key_element=hashtb_n(nlsr->keys);
474
475 for(i=0;i<key_element;i++)
476 {
477 key=e->data;
478 free(key->key_name);
479 hashtb_next(e);
480 }
481
482 hashtb_end(e);
483
484 if( nlsr->keys )
485 hashtb_destroy(&nlsr->keys);
486
487}
488
489void
490print_keys(void){
491
492 if ( nlsr->debugging )
493 printf("print_keys called \n");
494 if ( nlsr->detailed_logging )
495 writeLogg(__FILE__,__FUNCTION__,__LINE__,"print_keys called \n");
496
497 int i, key_element;
498 struct nlsr_key *key;
499
500 struct hashtb_enumerator ee;
501 struct hashtb_enumerator *e = &ee;
502
503 hashtb_start(nlsr->keys, e);
504 key_element=hashtb_n(nlsr->keys);
505
506 for(i=0;i<key_element;i++)
507 {
508 key=e->data;
509
510 if ( nlsr->debugging )
511 printf("Key : %s \n",key->key_name);
512
513 hashtb_next(e);
514 }
515
516 hashtb_end(e);
517
518}
519
520int
521does_key_exist(char *keyname){
522 if (nlsr->debugging)
523 {
524 printf("does_key_exist called\n");
525 printf("Keyname : %s \n",keyname);
526 }
527
528 int ret=0;
529
530 unsigned *v;
531 v = hashtb_lookup(nlsr->keys, keyname, strlen(keyname));
532 if (v != NULL){
533 ret = 1;
534 if (nlsr->debugging)
535 printf("Key Found\n");
536 }
537
538 return ret;
539}
540
541void
542add_key(char *keyname){
543 if (nlsr->debugging)
544 {
545 printf("add_key called\n");
546 printf("Keyname : %s \n",keyname);
547 }
548
549 struct nlsr_key *key;
550
551 struct hashtb_enumerator ee;
552 struct hashtb_enumerator *e = &ee;
553 int res;
554
555 hashtb_start(nlsr->keys, e);
556 res = hashtb_seek(e, keyname, strlen(keyname), 0);
557
558 if(res == HT_NEW_ENTRY )
559 {
560 key=e->data;
561 key->key_name=(char *)calloc(strlen(keyname)+1,sizeof(char));
562 memcpy(key->key_name,keyname,strlen(keyname)+1);
563 }
564
565 if (nlsr->debugging)
566 print_keys();
567}