blob: 9c938726dfedab02cac9b0dd8cdb4ab9d949a867 [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
120 ccn_charbuf_append_closer(sp.template_ccnb); // SignedInfo closer
akmhoquedbf6acc2013-03-22 05:36:54 -0500121
akmhoque7c64d802013-03-05 10:18:37 -0600122
123 sp.sp_flags |= CCN_SP_TEMPL_KEY_LOCATOR;
124 sp.sp_flags |= CCN_SP_FINAL_BLOCK;
akmhoqueced50e92013-03-22 05:40:18 -0500125 sp.freshness = freshness;
akmhoque7c64d802013-03-05 10:18:37 -0600126
127
128 if (pubid_out->length != sizeof(sp.pubid)){
akmhoque7adb2772013-03-05 16:30:59 -0600129 if ( nlsr->debugging )
130 printf("Size of pubid and sp.pubid is not equal");
akmhoque7c64d802013-03-05 10:18:37 -0600131 ccn_charbuf_destroy(&keyname);
132 ccn_charbuf_destroy(&pubid_out);
133 free(baseuri);
134 return -1;
135 }
136
137 memcpy(sp.pubid, pubid_out->buf, pubid_out->length);
138
139
140
141 res=ccn_sign_content(nlsr->ccn,resultbuf,content_name,&sp,data,data_size);
142 if( res < 0 ){
akmhoque7adb2772013-03-05 16:30:59 -0600143 if ( nlsr->debugging )
144 printf("Content signing error \n");
akmhoque7c64d802013-03-05 10:18:37 -0600145 ccn_charbuf_destroy(&sp.template_ccnb);
146 ccn_charbuf_destroy(&keyid);
147 ccn_charbuf_destroy(&keyname);
148 ccn_charbuf_destroy(&pubid_out);
149 free(baseuri);
150 return -1;
151 }
152
akmhoque284056b2013-03-15 01:20:16 -0500153 ccn_keystore_destroy(&keystore);
akmhoque7c64d802013-03-05 10:18:37 -0600154 ccn_charbuf_destroy(&sp.template_ccnb);
155 ccn_charbuf_destroy(&keyid);
156 ccn_charbuf_destroy(&keyname);
157 ccn_charbuf_destroy(&pubid_out);
akmhoque284056b2013-03-15 01:20:16 -0500158
akmhoque7c64d802013-03-05 10:18:37 -0600159 free(baseuri);
160 return 0;
161}
162
163
akmhoqueb8dbba82013-03-11 11:34:17 -0500164char *
165get_orig_router_from_lsa_name(struct ccn_charbuf * content_name)
166{
167 int start=0;
168
169 size_t comp_size;
170 const unsigned char *second_last_comp;
171 char *second_comp_type;
172 char *sep=".";
173 char *rem;
174
175 struct ccn_indexbuf *components=ccn_indexbuf_create();
176 struct ccn_charbuf *name=ccn_charbuf_create();
akmhoqueb7958182013-03-11 12:03:54 -0500177 ccn_name_from_uri(name,nlsr->slice_prefix);
akmhoqueb8dbba82013-03-11 11:34:17 -0500178 ccn_name_split (name, components);
179 start=components->n-2;
180 ccn_charbuf_destroy(&name);
181 ccn_indexbuf_destroy(&components);
182
183 struct ccn_indexbuf *comps=ccn_indexbuf_create();
184 ccn_name_split (content_name, comps);
185 ccn_name_comp_get( content_name->buf, comps,
186 comps->n-1-2, &second_last_comp, &comp_size);
187
188 second_comp_type=strtok_r((char *)second_last_comp, sep, &rem);
189 if ( strcmp( second_comp_type, "lsId" ) == 0 ){
190 ccn_name_chop(content_name,comps,-3);
191 }
192 else{
193 ccn_name_chop(content_name,comps,-2);
194 }
195
196
197 struct ccn_charbuf *temp=ccn_charbuf_create();
198 ccn_name_init(temp);
199 ccn_name_append_components( temp, content_name->buf,
200 comps->buf[start+1],
201 comps->buf[comps->n - 1]);
202
203 struct ccn_charbuf *temp1=ccn_charbuf_create();
204 ccn_uri_append(temp1, temp->buf, temp->length, 0);
205
206 char *orig_router=(char *)calloc(strlen(ccn_charbuf_as_string(temp1))+1,
207 sizeof(char));
208 memcpy(orig_router,ccn_charbuf_as_string(temp1),
209 strlen(ccn_charbuf_as_string(temp1)));
210 orig_router[strlen(orig_router)]='\0';
211
212 ccn_charbuf_destroy(&temp);
213 ccn_charbuf_destroy(&temp1);
214 ccn_indexbuf_destroy(&comps);
215 return orig_router;
216
217
218}
219
220
akmhoqueb7958182013-03-11 12:03:54 -0500221char *
222get_orig_router_from_info_content_name(struct ccn_charbuf * content_name)
223{
224 int start,end;
225
226 start=0;
227
228 struct ccn_indexbuf *comps=ccn_indexbuf_create();
229 ccn_name_split (content_name, comps);
230
231 end=check_for_name_component_in_name(content_name,comps,"nlsr");
232
233
234 struct ccn_charbuf *temp=ccn_charbuf_create();
235 ccn_name_init(temp);
236 ccn_name_append_components( temp, content_name->buf,
237 comps->buf[start],
238 comps->buf[end]);
239
240 struct ccn_charbuf *temp1=ccn_charbuf_create();
241 ccn_uri_append(temp1, temp->buf, temp->length, 0);
242
243 char *orig_router=(char *)calloc(strlen(ccn_charbuf_as_string(temp1))+1,
244 sizeof(char));
245 memcpy(orig_router,ccn_charbuf_as_string(temp1),
246 strlen(ccn_charbuf_as_string(temp1)));
247 orig_router[strlen(orig_router)]='\0';
248
249 ccn_charbuf_destroy(&temp);
250 ccn_charbuf_destroy(&temp1);
251 ccn_indexbuf_destroy(&comps);
252 return orig_router;
253
254
255}
256
akmhoqueb8dbba82013-03-11 11:34:17 -0500257
258int
259check_key_name_hierarchy(const unsigned char *ccnb,
260 struct ccn_parsed_ContentObject *pco,
akmhoqueb7958182013-03-11 12:03:54 -0500261 int key_type, int content_type){
akmhoquee2901222013-03-15 00:59:54 -0500262 if ( nlsr->debugging )
263 printf("check_key_name_hierarchy called\n");
akmhoqueb8dbba82013-03-11 11:34:17 -0500264 if (key_type == UNKNOWN_KEY ){
265 return 1;
266 }
akmhoque5c68a642013-03-15 00:28:16 -0500267 //int res;
akmhoqueb8dbba82013-03-11 11:34:17 -0500268 struct ccn_charbuf *key_name=get_key_name(ccnb, pco);
269
270 struct ccn_charbuf *key_uri = ccn_charbuf_create();
271 ccn_uri_append(key_uri, key_name->buf, key_name->length, 0);
akmhoquee2901222013-03-15 00:59:54 -0500272 if ( nlsr->debugging )
273 printf("Key Name: %s\n",ccn_charbuf_as_string(key_uri));
akmhoqueb8dbba82013-03-11 11:34:17 -0500274 ccn_charbuf_destroy(&key_uri);
275
276 struct ccn_charbuf *content_name=ccn_charbuf_create();
akmhoquea33345f2013-03-15 00:30:03 -0500277 ccn_charbuf_append(content_name, ccnb + pco->offset[CCN_PCO_B_Name],
akmhoqueb8dbba82013-03-11 11:34:17 -0500278 pco->offset[CCN_PCO_E_Name] - pco->offset[CCN_PCO_B_Name]);
279
280 struct ccn_charbuf *content_uri = ccn_charbuf_create();
281 ccn_uri_append(content_uri, content_name->buf, content_name->length, 0);
akmhoquee2901222013-03-15 00:59:54 -0500282 if ( nlsr->debugging )
283 printf("Content Name: %s\n",ccn_charbuf_as_string(content_uri));
akmhoqueb8dbba82013-03-11 11:34:17 -0500284 ccn_charbuf_destroy(&content_uri);
285
286 if ( key_type == NLSR_KEY){
287 char *orig_router_key_name=get_orig_router_from_key_name(key_name,0,0);
akmhoqueb7958182013-03-11 12:03:54 -0500288 char *orig_router_content_name;
289 if ( content_type == 1 ){
290 orig_router_content_name=get_orig_router_from_lsa_name(content_name);
291 }
292 else if ( content_type == 0 ){
293 orig_router_content_name=get_orig_router_from_info_content_name(content_name);
294 }
akmhoquee2901222013-03-15 00:59:54 -0500295 if ( nlsr->debugging ){
296 printf("Orig Router (Key Name):%s\n",orig_router_key_name);
297 printf("Orig Router (Content Name):%s\n",orig_router_content_name);
298 }
akmhoqueb8dbba82013-03-11 11:34:17 -0500299
300 if (strcmp(orig_router_key_name,orig_router_content_name) == 0 ){
301 free(orig_router_key_name);
302 free(orig_router_content_name);
303 ccn_charbuf_destroy(&key_name);
304 ccn_charbuf_destroy(&content_name);
305 return 1;
306 }
307 }
308
309 if ( key_type == ROUTING_KEY){
310 char *orig_router_key_name=get_orig_router_from_key_name(key_name,1,0);
311 char *orig_router_content_name=get_orig_router_from_key_name(content_name,1,1);
akmhoquee2901222013-03-15 00:59:54 -0500312 if ( nlsr->debugging ){
313 printf("Orig Router (Key Name):%s\n",orig_router_key_name);
314 printf("Orig Router (Content Name):%s\n",orig_router_content_name);
315 }
akmhoqueb8dbba82013-03-11 11:34:17 -0500316
317 if (strcmp(orig_router_key_name,orig_router_content_name) == 0 ){
318 free(orig_router_key_name);
319 free(orig_router_content_name);
320 ccn_charbuf_destroy(&key_name);
321 ccn_charbuf_destroy(&content_name);
322 return 1;
323 }
324 }
325 if ( key_type == OPERATOR_KEY){
326 struct ccn_indexbuf *key_name_comps;
327 key_name_comps = ccn_indexbuf_create();
akmhoquea33345f2013-03-15 00:30:03 -0500328 ccn_name_split(key_name, key_name_comps);
akmhoqueb8dbba82013-03-11 11:34:17 -0500329 int last_indx=check_for_tag_component_in_name(key_name,key_name_comps,"O.N.Start");
330 char *site_key_prefix_key=get_name_segments_from_name(key_name,0,last_indx);
akmhoquee2901222013-03-15 00:59:54 -0500331 if ( nlsr->debugging )
332 printf("Site key prefix(key Name):%s\n",site_key_prefix_key);
akmhoqueb8dbba82013-03-11 11:34:17 -0500333 ccn_indexbuf_destroy(&key_name_comps);
334
335 struct ccn_indexbuf *content_name_comps;
336 content_name_comps = ccn_indexbuf_create();
akmhoquea33345f2013-03-15 00:30:03 -0500337 ccn_name_split(content_name, content_name_comps);
akmhoqueb8dbba82013-03-11 11:34:17 -0500338 int last_indx_rtr=check_for_tag_component_in_name(content_name,content_name_comps,"R.N.Start");
339 char *site_key_prefix_content=get_name_segments_from_name(key_name,0,last_indx_rtr);
akmhoquee2901222013-03-15 00:59:54 -0500340 if ( nlsr->debugging )
341 printf("Site key prefix(Content Name):%s\n",site_key_prefix_content);
akmhoqueb8dbba82013-03-11 11:34:17 -0500342 ccn_indexbuf_destroy(&content_name_comps);
343
344 if( strcmp(site_key_prefix_key,site_key_prefix_content) == 0 ){
345 free(site_key_prefix_key);
346 free(site_key_prefix_content);
347 ccn_charbuf_destroy(&key_name);
348 ccn_charbuf_destroy(&content_name);
349 return 1;
350 }
351
352 }
353
354 if ( key_type == SITE_KEY){
355 struct ccn_indexbuf *key_name_comps;
356 key_name_comps = ccn_indexbuf_create();
akmhoquea33345f2013-03-15 00:30:03 -0500357 ccn_name_split(key_name, key_name_comps);
akmhoqueb8dbba82013-03-11 11:34:17 -0500358 int last_indx=check_for_tag_component_in_name(key_name,key_name_comps,"M.K");
359 char *site_key_prefix_key=get_name_segments_from_name(key_name,0,last_indx);
akmhoquee2901222013-03-15 00:59:54 -0500360 if ( nlsr->debugging )
361 printf("Site key prefix(key Name):%s\n",site_key_prefix_key);
akmhoqueb8dbba82013-03-11 11:34:17 -0500362 ccn_indexbuf_destroy(&key_name_comps);
363
364 struct ccn_indexbuf *content_name_comps;
365 content_name_comps = ccn_indexbuf_create();
akmhoquea33345f2013-03-15 00:30:03 -0500366 ccn_name_split(content_name, content_name_comps);
akmhoqueb8dbba82013-03-11 11:34:17 -0500367 int last_indx_rtr=check_for_tag_component_in_name(content_name,content_name_comps,"O.N.Start");
368 char *site_key_prefix_content=get_name_segments_from_name(key_name,0,last_indx_rtr);
akmhoquee2901222013-03-15 00:59:54 -0500369 if ( nlsr->debugging )
370 printf("Site key prefix(Content Name):%s\n",site_key_prefix_content);
akmhoqueb8dbba82013-03-11 11:34:17 -0500371 ccn_indexbuf_destroy(&content_name_comps);
372
373 if( strcmp(site_key_prefix_key,site_key_prefix_content) == 0 ){
374 free(site_key_prefix_key);
375 free(site_key_prefix_content);
376 ccn_charbuf_destroy(&key_name);
377 ccn_charbuf_destroy(&content_name);
378 return 1;
379 }
380
381 }
382
383 if ( key_type == ROOT_KEY){
384 ccn_charbuf_destroy(&key_name);
385 ccn_charbuf_destroy(&content_name);
386 return 1;
387 }
388
389 ccn_charbuf_destroy(&key_name);
390 ccn_charbuf_destroy(&content_name);
391 return 0;
392}
393
akmhoque7c64d802013-03-05 10:18:37 -0600394int
akmhoque6e2ba842013-03-05 19:35:26 -0600395verify_key(const unsigned char *ccnb,
akmhoqueb7958182013-03-11 12:03:54 -0500396 struct ccn_parsed_ContentObject *pco,
397 int content_type){
akmhoque7adb2772013-03-05 16:30:59 -0600398 if ( nlsr->debugging )
399 printf("verify key called\n");
akmhoque7c64d802013-03-05 10:18:37 -0600400 int ret=-1;
akmhoqueb8dbba82013-03-11 11:34:17 -0500401
akmhoque7c64d802013-03-05 10:18:37 -0600402 if ( contain_key_name(ccnb, pco) == 1){
403
404 struct ccn_charbuf *key_name=get_key_name(ccnb, pco);
405 struct ccn_charbuf *key_uri = ccn_charbuf_create();
406 ccn_uri_append(key_uri, key_name->buf, key_name->length, 0);
akmhoque7adb2772013-03-05 16:30:59 -0600407 if ( nlsr->debugging )
408 printf("Key Name from Incoming Content: %s\n",ccn_charbuf_as_string(key_uri));
akmhoqueb8dbba82013-03-11 11:34:17 -0500409 int key_type=get_key_type_from_key_name(key_name);
akmhoque7adb2772013-03-05 16:30:59 -0600410 if ( nlsr->debugging )
akmhoqueb8dbba82013-03-11 11:34:17 -0500411 printf("Key Type: %d \n",key_type);
akmhoque7c64d802013-03-05 10:18:37 -0600412
413 struct ccn_charbuf *result = ccn_charbuf_create();
414 struct ccn_parsed_ContentObject temp_pco = {0};
415 int get_flags = 0;
416 get_flags |= CCN_GET_NOKEYWAIT;
417 int counter = 0;
418 while(ccn_get(nlsr->ccn, key_name, NULL, 500, result, &temp_pco, NULL,
419 get_flags) < 0 && counter < 3) counter++;
420
421 int chk_verify=ccn_verify_content(nlsr->ccn,ccnb,pco);
422
423 if ( chk_verify == 0 ){
akmhoque7adb2772013-03-05 16:30:59 -0600424 if ( nlsr->debugging )
akmhoqueb7958182013-03-11 12:03:54 -0500425 printf("Content verification Successful :)\n");
akmhoque7c64d802013-03-05 10:18:37 -0600426
427 if ( counter == 3){
akmhoque7adb2772013-03-05 16:30:59 -0600428 if ( nlsr->debugging )
429 printf("Could not retrieve key by name !!!\n");
akmhoque7c64d802013-03-05 10:18:37 -0600430 }
431 else{
akmhoqueb8dbba82013-03-11 11:34:17 -0500432 if ( key_type == ROOT_KEY ){
akmhoque7c64d802013-03-05 10:18:37 -0600433 ret=0;
434 }
435 else{
akmhoqueb8dbba82013-03-11 11:34:17 -0500436 if ( nlsr->isStrictHierchicalKeyCheck ){
akmhoqueb7958182013-03-11 12:03:54 -0500437 int key_name_test=check_key_name_hierarchy(ccnb,
438 pco,
439 key_type,
440 content_type);
akmhoqueb8dbba82013-03-11 11:34:17 -0500441 if ( key_name_test == 1){
akmhoqueb7958182013-03-11 12:03:54 -0500442 ret=verify_key(result->buf,&temp_pco,content_type);
akmhoqueb8dbba82013-03-11 11:34:17 -0500443 }
444 }
445 else{
akmhoqueb7958182013-03-11 12:03:54 -0500446 ret=verify_key(result->buf,&temp_pco,content_type);
akmhoqueb8dbba82013-03-11 11:34:17 -0500447 }
akmhoque7c64d802013-03-05 10:18:37 -0600448 }
449 }
450 }
451 ccn_charbuf_destroy(&result);
452 ccn_charbuf_destroy(&key_uri);
453 ccn_charbuf_destroy(&key_name);
454 return ret;
455 }
456
457 return ret;
458}
459
akmhoque237239c2013-03-18 10:29:26 -0500460void
461destroy_keys(void)
462{
463 int i, key_element;
464 struct nlsr_key *key;
465
466 struct hashtb_enumerator ee;
467 struct hashtb_enumerator *e = &ee;
468
469 hashtb_start(nlsr->keys, e);
470 key_element=hashtb_n(nlsr->keys);
471
472 for(i=0;i<key_element;i++)
473 {
474 key=e->data;
475 free(key->key_name);
476 hashtb_next(e);
477 }
478
479 hashtb_end(e);
480
481 if( nlsr->keys )
482 hashtb_destroy(&nlsr->keys);
483
484}
485
486void
487print_keys(void){
488
489 if ( nlsr->debugging )
490 printf("print_keys called \n");
491 if ( nlsr->detailed_logging )
492 writeLogg(__FILE__,__FUNCTION__,__LINE__,"print_keys called \n");
493
494 int i, key_element;
495 struct nlsr_key *key;
496
497 struct hashtb_enumerator ee;
498 struct hashtb_enumerator *e = &ee;
499
500 hashtb_start(nlsr->keys, e);
501 key_element=hashtb_n(nlsr->keys);
502
503 for(i=0;i<key_element;i++)
504 {
505 key=e->data;
506
507 if ( nlsr->debugging )
508 printf("Key : %s \n",key->key_name);
509
510 hashtb_next(e);
511 }
512
513 hashtb_end(e);
514
515}
516
517int
518does_key_exist(char *keyname){
519 if (nlsr->debugging)
520 {
521 printf("does_key_exist called\n");
522 printf("Keyname : %s \n",keyname);
523 }
524
525 int ret=0;
526
527 unsigned *v;
528 v = hashtb_lookup(nlsr->keys, keyname, strlen(keyname));
529 if (v != NULL){
530 ret = 1;
531 if (nlsr->debugging)
532 printf("Key Found\n");
533 }
534
535 return ret;
536}
537
538void
539add_key(char *keyname){
540 if (nlsr->debugging)
541 {
542 printf("add_key called\n");
543 printf("Keyname : %s \n",keyname);
544 }
545
546 struct nlsr_key *key;
547
548 struct hashtb_enumerator ee;
549 struct hashtb_enumerator *e = &ee;
550 int res;
551
552 hashtb_start(nlsr->keys, e);
553 res = hashtb_seek(e, keyname, strlen(keyname), 0);
554
555 if(res == HT_NEW_ENTRY )
556 {
557 key=e->data;
558 key->key_name=(char *)calloc(strlen(keyname)+1,sizeof(char));
559 memcpy(key->key_name,keyname,strlen(keyname)+1);
560 }
561
562 if (nlsr->debugging)
563 print_keys();
564}