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