blob: f745d914df6c96623538371e674686577f61409b [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,
31 char *router_name){
32
akmhoque7adb2772013-03-05 16:30:59 -060033 if ( nlsr->debugging )
34 printf("sign_content_with_user_defined_keystore called\n");
akmhoque7c64d802013-03-05 10:18:37 -060035
36
37 int res;
38
39
40 struct ccn_charbuf * pubid_out=ccn_charbuf_create();
41 struct ccn_charbuf * keyname;
42
43
44 struct ccn_keystore *keystore = NULL;
45 keystore=ccn_keystore_create();
46 res=ccn_keystore_init(keystore, keystore_path,keystore_passphrase );
47 if ( res < 0 ){
akmhoque7adb2772013-03-05 16:30:59 -060048 if ( nlsr->debugging )
49 printf("Error in initiating keystore :(\n");
akmhoque7c64d802013-03-05 10:18:37 -060050 ccn_keystore_destroy(&keystore);
51 return -1;
52 }
53
54
55 res=ccn_load_private_key (nlsr->ccn,
56 keystore_path,
akmhoque7adb2772013-03-05 16:30:59 -060057 keystore_passphrase,
akmhoque7c64d802013-03-05 10:18:37 -060058 pubid_out);
59
60 if(res < 0 ){
akmhoque7adb2772013-03-05 16:30:59 -060061 if ( nlsr->debugging )
62 printf("Error in loading keystore :( \n");
akmhoque7c64d802013-03-05 10:18:37 -060063 ccn_charbuf_destroy(&pubid_out);
64 return -1;
65 }
66
67 char *baseuri=(char *)calloc(strlen(key_repo_name)+strlen(site_name)+
68 strlen(router_name)+strlen("/%C1.R.N.Start")+5,sizeof(char));
69 memcpy(baseuri,key_repo_name,strlen(key_repo_name)+1);
akmhoque7adb2772013-03-05 16:30:59 -060070 if ( site_name[0] != '/')
71 memcpy(baseuri+strlen(baseuri),"/",1);
akmhoque7c64d802013-03-05 10:18:37 -060072 memcpy(baseuri+strlen(baseuri),site_name,strlen(site_name)+1);
73 memcpy(baseuri+strlen(baseuri),"/%C1.R.N.Start",strlen("/%C1.R.N.Start"));
74 memcpy(baseuri+strlen(baseuri),router_name,strlen(router_name)+1);
75 baseuri[strlen(baseuri)]='\0';
76
77
78 keyname=ccn_charbuf_create();
79 if(keyname == NULL ){
80 ccn_charbuf_destroy(&pubid_out);
81 free(baseuri);
82 return -1;
83 }
84 ccn_name_from_uri(keyname,baseuri);
85 if ( res < 0 ){
akmhoque7adb2772013-03-05 16:30:59 -060086 if ( nlsr->debugging )
87 printf("Bad URI format: %s\n",baseuri);
akmhoque7c64d802013-03-05 10:18:37 -060088 ccn_charbuf_destroy(&pubid_out);
89 ccn_charbuf_destroy(&keyname);
90 free(baseuri);
91 return -1;
92 }
akmhoque3098a312013-03-06 07:31:01 -060093
akmhoque7c64d802013-03-05 10:18:37 -060094 ccn_name_append_str(keyname,"nlsr");
95 struct ccn_charbuf *keyid = ccn_charbuf_create();
96 ccn_charbuf_append_value(keyid, CCN_MARKER_CONTROL, 1);
97 ccn_charbuf_append_string(keyid, ".M.K");
98 ccn_charbuf_append_value(keyid, 0, 1);
99 ccn_charbuf_append_charbuf(keyid, pubid_out);
100 ccn_name_append(keyname, keyid->buf, keyid->length);
101
102
103
104 struct ccn_charbuf *uri = ccn_charbuf_create();
105 ccn_uri_append(uri, keyname->buf, keyname->length, 0);
akmhoque7adb2772013-03-05 16:30:59 -0600106 if ( nlsr->debugging )
107 printf("Key Name Included when processing content: %s\n", ccn_charbuf_as_string(uri));
akmhoque7c64d802013-03-05 10:18:37 -0600108 ccn_charbuf_destroy(&uri);
109
110 struct ccn_signing_params sp = CCN_SIGNING_PARAMS_INIT;
111 sp.type = CCN_CONTENT_DATA;
112 sp.template_ccnb = ccn_charbuf_create();
113 ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_SignedInfo, CCN_DTAG);
114 ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_KeyLocator, CCN_DTAG);
115 ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_KeyName, CCN_DTAG);
116 ccn_charbuf_append(sp.template_ccnb, keyname->buf, keyname->length);
117 ccn_charbuf_append_closer(sp.template_ccnb); // KeyName closer
118 ccn_charbuf_append_closer(sp.template_ccnb); // KeyLocator closer
119 ccn_charbuf_append_closer(sp.template_ccnb); // SignedInfo closer
120
121 sp.sp_flags |= CCN_SP_TEMPL_KEY_LOCATOR;
122 sp.sp_flags |= CCN_SP_FINAL_BLOCK;
123 sp.freshness = 60;
124
125
126 if (pubid_out->length != sizeof(sp.pubid)){
akmhoque7adb2772013-03-05 16:30:59 -0600127 if ( nlsr->debugging )
128 printf("Size of pubid and sp.pubid is not equal");
akmhoque7c64d802013-03-05 10:18:37 -0600129 ccn_charbuf_destroy(&keyname);
130 ccn_charbuf_destroy(&pubid_out);
131 free(baseuri);
132 return -1;
133 }
134
135 memcpy(sp.pubid, pubid_out->buf, pubid_out->length);
136
137
138
139 res=ccn_sign_content(nlsr->ccn,resultbuf,content_name,&sp,data,data_size);
140 if( res < 0 ){
akmhoque7adb2772013-03-05 16:30:59 -0600141 if ( nlsr->debugging )
142 printf("Content signing error \n");
akmhoque7c64d802013-03-05 10:18:37 -0600143 ccn_charbuf_destroy(&sp.template_ccnb);
144 ccn_charbuf_destroy(&keyid);
145 ccn_charbuf_destroy(&keyname);
146 ccn_charbuf_destroy(&pubid_out);
147 free(baseuri);
148 return -1;
149 }
150
akmhoque284056b2013-03-15 01:20:16 -0500151 ccn_keystore_destroy(&keystore);
akmhoque7c64d802013-03-05 10:18:37 -0600152 ccn_charbuf_destroy(&sp.template_ccnb);
153 ccn_charbuf_destroy(&keyid);
154 ccn_charbuf_destroy(&keyname);
155 ccn_charbuf_destroy(&pubid_out);
akmhoque284056b2013-03-15 01:20:16 -0500156
akmhoque7c64d802013-03-05 10:18:37 -0600157 free(baseuri);
158 return 0;
159}
160
161
akmhoqueb8dbba82013-03-11 11:34:17 -0500162char *
163get_orig_router_from_lsa_name(struct ccn_charbuf * content_name)
164{
165 int start=0;
166
167 size_t comp_size;
168 const unsigned char *second_last_comp;
169 char *second_comp_type;
170 char *sep=".";
171 char *rem;
172
173 struct ccn_indexbuf *components=ccn_indexbuf_create();
174 struct ccn_charbuf *name=ccn_charbuf_create();
akmhoqueb7958182013-03-11 12:03:54 -0500175 ccn_name_from_uri(name,nlsr->slice_prefix);
akmhoqueb8dbba82013-03-11 11:34:17 -0500176 ccn_name_split (name, components);
177 start=components->n-2;
178 ccn_charbuf_destroy(&name);
179 ccn_indexbuf_destroy(&components);
180
181 struct ccn_indexbuf *comps=ccn_indexbuf_create();
182 ccn_name_split (content_name, comps);
183 ccn_name_comp_get( content_name->buf, comps,
184 comps->n-1-2, &second_last_comp, &comp_size);
185
186 second_comp_type=strtok_r((char *)second_last_comp, sep, &rem);
187 if ( strcmp( second_comp_type, "lsId" ) == 0 ){
188 ccn_name_chop(content_name,comps,-3);
189 }
190 else{
191 ccn_name_chop(content_name,comps,-2);
192 }
193
194
195 struct ccn_charbuf *temp=ccn_charbuf_create();
196 ccn_name_init(temp);
197 ccn_name_append_components( temp, content_name->buf,
198 comps->buf[start+1],
199 comps->buf[comps->n - 1]);
200
201 struct ccn_charbuf *temp1=ccn_charbuf_create();
202 ccn_uri_append(temp1, temp->buf, temp->length, 0);
203
204 char *orig_router=(char *)calloc(strlen(ccn_charbuf_as_string(temp1))+1,
205 sizeof(char));
206 memcpy(orig_router,ccn_charbuf_as_string(temp1),
207 strlen(ccn_charbuf_as_string(temp1)));
208 orig_router[strlen(orig_router)]='\0';
209
210 ccn_charbuf_destroy(&temp);
211 ccn_charbuf_destroy(&temp1);
212 ccn_indexbuf_destroy(&comps);
213 return orig_router;
214
215
216}
217
218
akmhoqueb7958182013-03-11 12:03:54 -0500219char *
220get_orig_router_from_info_content_name(struct ccn_charbuf * content_name)
221{
222 int start,end;
223
224 start=0;
225
226 struct ccn_indexbuf *comps=ccn_indexbuf_create();
227 ccn_name_split (content_name, comps);
228
229 end=check_for_name_component_in_name(content_name,comps,"nlsr");
230
231
232 struct ccn_charbuf *temp=ccn_charbuf_create();
233 ccn_name_init(temp);
234 ccn_name_append_components( temp, content_name->buf,
235 comps->buf[start],
236 comps->buf[end]);
237
238 struct ccn_charbuf *temp1=ccn_charbuf_create();
239 ccn_uri_append(temp1, temp->buf, temp->length, 0);
240
241 char *orig_router=(char *)calloc(strlen(ccn_charbuf_as_string(temp1))+1,
242 sizeof(char));
243 memcpy(orig_router,ccn_charbuf_as_string(temp1),
244 strlen(ccn_charbuf_as_string(temp1)));
245 orig_router[strlen(orig_router)]='\0';
246
247 ccn_charbuf_destroy(&temp);
248 ccn_charbuf_destroy(&temp1);
249 ccn_indexbuf_destroy(&comps);
250 return orig_router;
251
252
253}
254
akmhoqueb8dbba82013-03-11 11:34:17 -0500255
256int
257check_key_name_hierarchy(const unsigned char *ccnb,
258 struct ccn_parsed_ContentObject *pco,
akmhoqueb7958182013-03-11 12:03:54 -0500259 int key_type, int content_type){
akmhoquee2901222013-03-15 00:59:54 -0500260 if ( nlsr->debugging )
261 printf("check_key_name_hierarchy called\n");
akmhoqueb8dbba82013-03-11 11:34:17 -0500262 if (key_type == UNKNOWN_KEY ){
263 return 1;
264 }
akmhoque5c68a642013-03-15 00:28:16 -0500265 //int res;
akmhoqueb8dbba82013-03-11 11:34:17 -0500266 struct ccn_charbuf *key_name=get_key_name(ccnb, pco);
267
268 struct ccn_charbuf *key_uri = ccn_charbuf_create();
269 ccn_uri_append(key_uri, key_name->buf, key_name->length, 0);
akmhoquee2901222013-03-15 00:59:54 -0500270 if ( nlsr->debugging )
271 printf("Key Name: %s\n",ccn_charbuf_as_string(key_uri));
akmhoqueb8dbba82013-03-11 11:34:17 -0500272 ccn_charbuf_destroy(&key_uri);
273
274 struct ccn_charbuf *content_name=ccn_charbuf_create();
akmhoquea33345f2013-03-15 00:30:03 -0500275 ccn_charbuf_append(content_name, ccnb + pco->offset[CCN_PCO_B_Name],
akmhoqueb8dbba82013-03-11 11:34:17 -0500276 pco->offset[CCN_PCO_E_Name] - pco->offset[CCN_PCO_B_Name]);
277
278 struct ccn_charbuf *content_uri = ccn_charbuf_create();
279 ccn_uri_append(content_uri, content_name->buf, content_name->length, 0);
akmhoquee2901222013-03-15 00:59:54 -0500280 if ( nlsr->debugging )
281 printf("Content Name: %s\n",ccn_charbuf_as_string(content_uri));
akmhoqueb8dbba82013-03-11 11:34:17 -0500282 ccn_charbuf_destroy(&content_uri);
283
284 if ( key_type == NLSR_KEY){
285 char *orig_router_key_name=get_orig_router_from_key_name(key_name,0,0);
akmhoqueb7958182013-03-11 12:03:54 -0500286 char *orig_router_content_name;
287 if ( content_type == 1 ){
288 orig_router_content_name=get_orig_router_from_lsa_name(content_name);
289 }
290 else if ( content_type == 0 ){
291 orig_router_content_name=get_orig_router_from_info_content_name(content_name);
292 }
akmhoquee2901222013-03-15 00:59:54 -0500293 if ( nlsr->debugging ){
294 printf("Orig Router (Key Name):%s\n",orig_router_key_name);
295 printf("Orig Router (Content Name):%s\n",orig_router_content_name);
296 }
akmhoqueb8dbba82013-03-11 11:34:17 -0500297
298 if (strcmp(orig_router_key_name,orig_router_content_name) == 0 ){
299 free(orig_router_key_name);
300 free(orig_router_content_name);
301 ccn_charbuf_destroy(&key_name);
302 ccn_charbuf_destroy(&content_name);
303 return 1;
304 }
305 }
306
307 if ( key_type == ROUTING_KEY){
308 char *orig_router_key_name=get_orig_router_from_key_name(key_name,1,0);
309 char *orig_router_content_name=get_orig_router_from_key_name(content_name,1,1);
akmhoquee2901222013-03-15 00:59:54 -0500310 if ( nlsr->debugging ){
311 printf("Orig Router (Key Name):%s\n",orig_router_key_name);
312 printf("Orig Router (Content Name):%s\n",orig_router_content_name);
313 }
akmhoqueb8dbba82013-03-11 11:34:17 -0500314
315 if (strcmp(orig_router_key_name,orig_router_content_name) == 0 ){
316 free(orig_router_key_name);
317 free(orig_router_content_name);
318 ccn_charbuf_destroy(&key_name);
319 ccn_charbuf_destroy(&content_name);
320 return 1;
321 }
322 }
323 if ( key_type == OPERATOR_KEY){
324 struct ccn_indexbuf *key_name_comps;
325 key_name_comps = ccn_indexbuf_create();
akmhoquea33345f2013-03-15 00:30:03 -0500326 ccn_name_split(key_name, key_name_comps);
akmhoqueb8dbba82013-03-11 11:34:17 -0500327 int last_indx=check_for_tag_component_in_name(key_name,key_name_comps,"O.N.Start");
328 char *site_key_prefix_key=get_name_segments_from_name(key_name,0,last_indx);
akmhoquee2901222013-03-15 00:59:54 -0500329 if ( nlsr->debugging )
330 printf("Site key prefix(key Name):%s\n",site_key_prefix_key);
akmhoqueb8dbba82013-03-11 11:34:17 -0500331 ccn_indexbuf_destroy(&key_name_comps);
332
333 struct ccn_indexbuf *content_name_comps;
334 content_name_comps = ccn_indexbuf_create();
akmhoquea33345f2013-03-15 00:30:03 -0500335 ccn_name_split(content_name, content_name_comps);
akmhoqueb8dbba82013-03-11 11:34:17 -0500336 int last_indx_rtr=check_for_tag_component_in_name(content_name,content_name_comps,"R.N.Start");
337 char *site_key_prefix_content=get_name_segments_from_name(key_name,0,last_indx_rtr);
akmhoquee2901222013-03-15 00:59:54 -0500338 if ( nlsr->debugging )
339 printf("Site key prefix(Content Name):%s\n",site_key_prefix_content);
akmhoqueb8dbba82013-03-11 11:34:17 -0500340 ccn_indexbuf_destroy(&content_name_comps);
341
342 if( strcmp(site_key_prefix_key,site_key_prefix_content) == 0 ){
343 free(site_key_prefix_key);
344 free(site_key_prefix_content);
345 ccn_charbuf_destroy(&key_name);
346 ccn_charbuf_destroy(&content_name);
347 return 1;
348 }
349
350 }
351
352 if ( key_type == SITE_KEY){
353 struct ccn_indexbuf *key_name_comps;
354 key_name_comps = ccn_indexbuf_create();
akmhoquea33345f2013-03-15 00:30:03 -0500355 ccn_name_split(key_name, key_name_comps);
akmhoqueb8dbba82013-03-11 11:34:17 -0500356 int last_indx=check_for_tag_component_in_name(key_name,key_name_comps,"M.K");
357 char *site_key_prefix_key=get_name_segments_from_name(key_name,0,last_indx);
akmhoquee2901222013-03-15 00:59:54 -0500358 if ( nlsr->debugging )
359 printf("Site key prefix(key Name):%s\n",site_key_prefix_key);
akmhoqueb8dbba82013-03-11 11:34:17 -0500360 ccn_indexbuf_destroy(&key_name_comps);
361
362 struct ccn_indexbuf *content_name_comps;
363 content_name_comps = ccn_indexbuf_create();
akmhoquea33345f2013-03-15 00:30:03 -0500364 ccn_name_split(content_name, content_name_comps);
akmhoqueb8dbba82013-03-11 11:34:17 -0500365 int last_indx_rtr=check_for_tag_component_in_name(content_name,content_name_comps,"O.N.Start");
366 char *site_key_prefix_content=get_name_segments_from_name(key_name,0,last_indx_rtr);
akmhoquee2901222013-03-15 00:59:54 -0500367 if ( nlsr->debugging )
368 printf("Site key prefix(Content Name):%s\n",site_key_prefix_content);
akmhoqueb8dbba82013-03-11 11:34:17 -0500369 ccn_indexbuf_destroy(&content_name_comps);
370
371 if( strcmp(site_key_prefix_key,site_key_prefix_content) == 0 ){
372 free(site_key_prefix_key);
373 free(site_key_prefix_content);
374 ccn_charbuf_destroy(&key_name);
375 ccn_charbuf_destroy(&content_name);
376 return 1;
377 }
378
379 }
380
381 if ( key_type == ROOT_KEY){
382 ccn_charbuf_destroy(&key_name);
383 ccn_charbuf_destroy(&content_name);
384 return 1;
385 }
386
387 ccn_charbuf_destroy(&key_name);
388 ccn_charbuf_destroy(&content_name);
389 return 0;
390}
391
akmhoque7c64d802013-03-05 10:18:37 -0600392int
akmhoque6e2ba842013-03-05 19:35:26 -0600393verify_key(const unsigned char *ccnb,
akmhoqueb7958182013-03-11 12:03:54 -0500394 struct ccn_parsed_ContentObject *pco,
395 int content_type){
akmhoque7adb2772013-03-05 16:30:59 -0600396 if ( nlsr->debugging )
397 printf("verify key called\n");
akmhoque7c64d802013-03-05 10:18:37 -0600398 int ret=-1;
akmhoqueb8dbba82013-03-11 11:34:17 -0500399
akmhoque7c64d802013-03-05 10:18:37 -0600400 if ( contain_key_name(ccnb, pco) == 1){
401
402 struct ccn_charbuf *key_name=get_key_name(ccnb, pco);
403 struct ccn_charbuf *key_uri = ccn_charbuf_create();
404 ccn_uri_append(key_uri, key_name->buf, key_name->length, 0);
akmhoque7adb2772013-03-05 16:30:59 -0600405 if ( nlsr->debugging )
406 printf("Key Name from Incoming Content: %s\n",ccn_charbuf_as_string(key_uri));
akmhoqueb8dbba82013-03-11 11:34:17 -0500407 int key_type=get_key_type_from_key_name(key_name);
akmhoque7adb2772013-03-05 16:30:59 -0600408 if ( nlsr->debugging )
akmhoqueb8dbba82013-03-11 11:34:17 -0500409 printf("Key Type: %d \n",key_type);
akmhoque7c64d802013-03-05 10:18:37 -0600410
411 struct ccn_charbuf *result = ccn_charbuf_create();
412 struct ccn_parsed_ContentObject temp_pco = {0};
413 int get_flags = 0;
414 get_flags |= CCN_GET_NOKEYWAIT;
415 int counter = 0;
416 while(ccn_get(nlsr->ccn, key_name, NULL, 500, result, &temp_pco, NULL,
417 get_flags) < 0 && counter < 3) counter++;
418
419 int chk_verify=ccn_verify_content(nlsr->ccn,ccnb,pco);
420
421 if ( chk_verify == 0 ){
akmhoque7adb2772013-03-05 16:30:59 -0600422 if ( nlsr->debugging )
akmhoqueb7958182013-03-11 12:03:54 -0500423 printf("Content verification Successful :)\n");
akmhoque7c64d802013-03-05 10:18:37 -0600424
425 if ( counter == 3){
akmhoque7adb2772013-03-05 16:30:59 -0600426 if ( nlsr->debugging )
427 printf("Could not retrieve key by name !!!\n");
akmhoque7c64d802013-03-05 10:18:37 -0600428 }
429 else{
akmhoqueb8dbba82013-03-11 11:34:17 -0500430 if ( key_type == ROOT_KEY ){
akmhoque7c64d802013-03-05 10:18:37 -0600431 ret=0;
432 }
433 else{
akmhoqueb8dbba82013-03-11 11:34:17 -0500434 if ( nlsr->isStrictHierchicalKeyCheck ){
akmhoqueb7958182013-03-11 12:03:54 -0500435 int key_name_test=check_key_name_hierarchy(ccnb,
436 pco,
437 key_type,
438 content_type);
akmhoqueb8dbba82013-03-11 11:34:17 -0500439 if ( key_name_test == 1){
akmhoqueb7958182013-03-11 12:03:54 -0500440 ret=verify_key(result->buf,&temp_pco,content_type);
akmhoqueb8dbba82013-03-11 11:34:17 -0500441 }
442 }
443 else{
akmhoqueb7958182013-03-11 12:03:54 -0500444 ret=verify_key(result->buf,&temp_pco,content_type);
akmhoqueb8dbba82013-03-11 11:34:17 -0500445 }
akmhoque7c64d802013-03-05 10:18:37 -0600446 }
447 }
448 }
449 ccn_charbuf_destroy(&result);
450 ccn_charbuf_destroy(&key_uri);
451 ccn_charbuf_destroy(&key_name);
452 return ret;
453 }
454
455 return ret;
456}
457
akmhoque237239c2013-03-18 10:29:26 -0500458void
459destroy_keys(void)
460{
461 int i, key_element;
462 struct nlsr_key *key;
463
464 struct hashtb_enumerator ee;
465 struct hashtb_enumerator *e = &ee;
466
467 hashtb_start(nlsr->keys, e);
468 key_element=hashtb_n(nlsr->keys);
469
470 for(i=0;i<key_element;i++)
471 {
472 key=e->data;
473 free(key->key_name);
474 hashtb_next(e);
475 }
476
477 hashtb_end(e);
478
479 if( nlsr->keys )
480 hashtb_destroy(&nlsr->keys);
481
482}
483
484void
485print_keys(void){
486
487 if ( nlsr->debugging )
488 printf("print_keys called \n");
489 if ( nlsr->detailed_logging )
490 writeLogg(__FILE__,__FUNCTION__,__LINE__,"print_keys called \n");
491
492 int i, key_element;
493 struct nlsr_key *key;
494
495 struct hashtb_enumerator ee;
496 struct hashtb_enumerator *e = &ee;
497
498 hashtb_start(nlsr->keys, e);
499 key_element=hashtb_n(nlsr->keys);
500
501 for(i=0;i<key_element;i++)
502 {
503 key=e->data;
504
505 if ( nlsr->debugging )
506 printf("Key : %s \n",key->key_name);
507
508 hashtb_next(e);
509 }
510
511 hashtb_end(e);
512
513}
514
515int
516does_key_exist(char *keyname){
517 if (nlsr->debugging)
518 {
519 printf("does_key_exist called\n");
520 printf("Keyname : %s \n",keyname);
521 }
522
523 int ret=0;
524
525 unsigned *v;
526 v = hashtb_lookup(nlsr->keys, keyname, strlen(keyname));
527 if (v != NULL){
528 ret = 1;
529 if (nlsr->debugging)
530 printf("Key Found\n");
531 }
532
533 return ret;
534}
535
536void
537add_key(char *keyname){
538 if (nlsr->debugging)
539 {
540 printf("add_key called\n");
541 printf("Keyname : %s \n",keyname);
542 }
543
544 struct nlsr_key *key;
545
546 struct hashtb_enumerator ee;
547 struct hashtb_enumerator *e = &ee;
548 int res;
549
550 hashtb_start(nlsr->keys, e);
551 res = hashtb_seek(e, keyname, strlen(keyname), 0);
552
553 if(res == HT_NEW_ENTRY )
554 {
555 key=e->data;
556 key->key_name=(char *)calloc(strlen(keyname)+1,sizeof(char));
557 memcpy(key->key_name,keyname,strlen(keyname)+1);
558 }
559
560 if (nlsr->debugging)
561 print_keys();
562}