blob: f799873004bcd88552e523043a5c6473aa26fb52 [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
18#include "nlsr.h"
19#include "nlsr_km_util.h"
20#include "nlsr_km.h"
21
22int
23appendLifetime(struct ccn_charbuf *cb, int lifetime)
24{
25 unsigned char buf[sizeof(int32_t)];
26 int32_t dreck = lifetime << 12;
27 int pos = sizeof(int32_t);
28 int res = 0;
29 while (dreck > 0 && pos > 0)
30 {
31 pos--;
32 buf[pos] = dreck & 255;
33 dreck = dreck >> 8;
34 }
35 res |= ccnb_append_tagged_blob(cb, CCN_DTAG_InterestLifetime, buf+pos,
36 sizeof(buf)-pos);
37 return res;
38}
39
40int
41contain_key_name(const unsigned char *ccnb, struct ccn_parsed_ContentObject *pco)
42{
43 if (pco->offset[CCN_PCO_B_KeyLocator] == pco->offset[CCN_PCO_E_KeyLocator])
44 return -1;
45
46 struct ccn_buf_decoder decoder;
47 struct ccn_buf_decoder *d;
48 d = ccn_buf_decoder_start(&decoder, ccnb +
49 pco->offset[CCN_PCO_B_Key_Certificate_KeyName],
50 pco->offset[CCN_PCO_E_Key_Certificate_KeyName] -
51 pco->offset[CCN_PCO_B_Key_Certificate_KeyName]);
52 if (ccn_buf_match_dtag(d, CCN_DTAG_KeyName))
53 return 1;
54
55 return -1;
56}
57
58struct ccn_charbuf *
59get_key_name(const unsigned char *ccnb, struct ccn_parsed_ContentObject *pco)
60{
61 struct ccn_charbuf *key_name = ccn_charbuf_create();
62 ccn_charbuf_append(key_name, ccnb + pco->offset[CCN_PCO_B_KeyName_Name],
63 pco->offset[CCN_PCO_E_KeyName_Name] - pco->offset[CCN_PCO_B_KeyName_Name]);
64
65 return key_name;
66}
67
akmhoque7adb2772013-03-05 16:30:59 -060068
akmhoque7c64d802013-03-05 10:18:37 -060069int
70check_for_name_component_in_name(const struct ccn_charbuf *name,
71 const struct ccn_indexbuf *indx,
72 const char *component){
73
74 int res,i;
75 int result_position=0;
76 int name_comps=(int)indx->n;
77
78 for(i=0;i<name_comps;i++){
79 res=ccn_name_comp_strcmp(name->buf,indx,i,component);
80 if( res == 0){
81
82 result_position=i;
83 break;
84 }
85 }
86
87 return result_position;
88}
89
90
91int
92check_for_tag_component_in_name(const struct ccn_charbuf *name,
93 const struct ccn_indexbuf *indx,
94 const char *component){
95
96 int res,i;
97 int result_position=0;
98 int name_comps=(int)indx->n;
99
100 for(i=0;i<name_comps;i++){
101 const unsigned char *comp_ptr;
102 size_t comp_size;
103 res=ccn_name_comp_get(name->buf, indx,i,&comp_ptr, &comp_size);
104 if( res == 0){
105 if ( strstr((char *)comp_ptr,component) != NULL ){
106 result_position=i;
107 break;
108 }
109 }
110 }
111
112 return result_position;
113}
114
115enum key_type
116get_key_type_from_key_name(struct ccn_charbuf *keyname)
117{
118 printf("get_key_type_from_key_name called\n");
119
120 int res;
121 int return_key=UNKNOWN_KEY;
122
123 struct ccn_indexbuf *indx=ccn_indexbuf_create();
124 if ( indx == NULL ){
125 printf("Error in creating index for key name \n");
126 return UNKNOWN_KEY;
127 }
128
129 res=ccn_name_split(keyname,indx);
130 if ( res < 0 ){
131 printf("Error in parsing key name \n");
132 ccn_indexbuf_destroy(&indx);
133 return UNKNOWN_KEY;
134 }
135 else if ( res == 3){
136 int chk_ndn=check_for_name_component_in_name(keyname,indx,"ndn");
137 int chk_key=check_for_name_component_in_name(keyname,indx,"keys");
138 if ( chk_ndn == 0 && chk_key == 1)
139 return_key=ROOT_KEY;
140 }
141 else{
142 int check_op,check_rt;
143 check_op=check_for_tag_component_in_name(keyname,indx,
144 "O.N.Start");
145 check_rt=check_for_tag_component_in_name(keyname,indx,
146 "R.N.Start");
147 if ( check_op > 0){
148 return_key=OPERATOR_KEY;
149 }
150 else if(check_rt >0){
151 int check_nlsr;
152 check_nlsr=check_for_name_component_in_name(keyname,indx,
153 "nlsr");
154 if ( check_rt > 0 ){
155 if ( check_nlsr > 0){
156 return_key=NLSR_KEY;
157 }
158 else{
159 return_key=ROUTING_KEY;
160 }
161 }
162 }
163 else if ( check_rt == 0 && check_op == 0 && res > 3){
164 return_key=SITE_KEY;
165 }
166 }
167
168 ccn_indexbuf_destroy(&indx);
169 return return_key;
170}
akmhoqueb8dbba82013-03-11 11:34:17 -0500171
172
173char *
174get_name_segments_from_name(struct ccn_charbuf *name, int start_indx, int end_indx)
175{
176 int res;
177 struct ccn_indexbuf *name_comps;
178 struct ccn_charbuf *orig_router;
179 char *name_seg=NULL;
180
181 name_comps = ccn_indexbuf_create();
182 res = ccn_name_split(name, name_comps);
183 if ( res < 0 ){
184 ccn_indexbuf_destroy(&name_comps);
185 return name_seg;
186 }
187 else{
188 orig_router=ccn_charbuf_create();
189 ccn_name_init(orig_router);
190 ccn_name_append_components(orig_router,name->buf,
191 name_comps->buf[start_indx],
192 name_comps->buf[end_indx]);
193 struct ccn_charbuf *temp1=ccn_charbuf_create();
194 ccn_uri_append(temp1, orig_router->buf, orig_router->length, 0);
195
196 name_seg=(char *)calloc(strlen(ccn_charbuf_as_string(temp1))+1,
197 sizeof(char));
198 memcpy(name_seg,ccn_charbuf_as_string(temp1),
199 strlen(ccn_charbuf_as_string(temp1)));
200 name_seg[strlen(name_seg)]='\0';
201 ccn_charbuf_destroy(&orig_router);
202 ccn_charbuf_destroy(&temp1);
203
204 }
205
206 ccn_indexbuf_destroy(&name_comps);
207 return name_seg;
208}
209
210
211char *
212get_orig_router_from_key_name(struct ccn_charbuf *name, int more, int type)
213{
214 int res;
215 struct ccn_indexbuf *name_comps;
216 struct ccn_charbuf *orig_router;
217 char *router=NULL;
218
219 name_comps = ccn_indexbuf_create();
220 res = ccn_name_split(name, name_comps);
221 if ( res < 0 ){
222 ccn_indexbuf_destroy(&name_comps);
223 return router;
224 }
225 else{
226 res=ccn_name_chop(name, name_comps, -(2-more));
227 if ( more > 0 && type==1)
228 res=ccn_name_chop(name, name_comps, -3);
229 if ( res < 0 ){
230 ccn_indexbuf_destroy(&name_comps);
231 return NULL;
232 }
233 else{
234 res=check_for_tag_component_in_name(name,name_comps,"R.N.Start");
235 if ( res > 0 ){
236 orig_router=ccn_charbuf_create();
237 ccn_name_init(orig_router);
238 ccn_name_append_components(orig_router,name->buf,
239 name_comps->buf[res+1],
240 name_comps->buf[name_comps->n - 1]);
241 struct ccn_charbuf *temp1=ccn_charbuf_create();
242 ccn_uri_append(temp1, orig_router->buf, orig_router->length, 0);
243
244 router=(char *)calloc(strlen(ccn_charbuf_as_string(temp1))+1,
245 sizeof(char));
246 memcpy(router,ccn_charbuf_as_string(temp1),
247 strlen(ccn_charbuf_as_string(temp1)));
248 router[strlen(router)]='\0';
249 }
250 else{
251 ccn_indexbuf_destroy(&name_comps);
252 return NULL;
253 }
254 }
255 }
256
257 ccn_indexbuf_destroy(&name_comps);
258 return router;
259}
260