akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 1 | #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 | |
| 22 | int |
| 23 | appendLifetime(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 | |
| 40 | int |
| 41 | contain_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 | |
| 58 | struct ccn_charbuf * |
| 59 | get_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 | |
akmhoque | 7adb277 | 2013-03-05 16:30:59 -0600 | [diff] [blame] | 68 | |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 69 | int |
| 70 | check_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 | |
| 91 | int |
| 92 | check_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 | |
| 115 | enum key_type |
| 116 | get_key_type_from_key_name(struct ccn_charbuf *keyname) |
| 117 | { |
akmhoque | e290122 | 2013-03-15 00:59:54 -0500 | [diff] [blame] | 118 | if ( nlsr->debugging ) |
| 119 | printf("get_key_type_from_key_name called\n"); |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 120 | |
| 121 | int res; |
| 122 | int return_key=UNKNOWN_KEY; |
| 123 | |
| 124 | struct ccn_indexbuf *indx=ccn_indexbuf_create(); |
| 125 | if ( indx == NULL ){ |
akmhoque | e290122 | 2013-03-15 00:59:54 -0500 | [diff] [blame] | 126 | if ( nlsr->debugging ) |
| 127 | printf("Error in creating index for key name \n"); |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 128 | return UNKNOWN_KEY; |
| 129 | } |
| 130 | |
| 131 | res=ccn_name_split(keyname,indx); |
| 132 | if ( res < 0 ){ |
akmhoque | e290122 | 2013-03-15 00:59:54 -0500 | [diff] [blame] | 133 | if ( nlsr->debugging ) |
| 134 | printf("Error in parsing key name \n"); |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 135 | ccn_indexbuf_destroy(&indx); |
| 136 | return UNKNOWN_KEY; |
| 137 | } |
| 138 | else if ( res == 3){ |
| 139 | int chk_ndn=check_for_name_component_in_name(keyname,indx,"ndn"); |
| 140 | int chk_key=check_for_name_component_in_name(keyname,indx,"keys"); |
| 141 | if ( chk_ndn == 0 && chk_key == 1) |
| 142 | return_key=ROOT_KEY; |
| 143 | } |
| 144 | else{ |
| 145 | int check_op,check_rt; |
| 146 | check_op=check_for_tag_component_in_name(keyname,indx, |
| 147 | "O.N.Start"); |
| 148 | check_rt=check_for_tag_component_in_name(keyname,indx, |
| 149 | "R.N.Start"); |
| 150 | if ( check_op > 0){ |
| 151 | return_key=OPERATOR_KEY; |
| 152 | } |
| 153 | else if(check_rt >0){ |
| 154 | int check_nlsr; |
| 155 | check_nlsr=check_for_name_component_in_name(keyname,indx, |
| 156 | "nlsr"); |
| 157 | if ( check_rt > 0 ){ |
| 158 | if ( check_nlsr > 0){ |
| 159 | return_key=NLSR_KEY; |
| 160 | } |
| 161 | else{ |
| 162 | return_key=ROUTING_KEY; |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | else if ( check_rt == 0 && check_op == 0 && res > 3){ |
| 167 | return_key=SITE_KEY; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | ccn_indexbuf_destroy(&indx); |
| 172 | return return_key; |
| 173 | } |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 174 | |
| 175 | |
| 176 | char * |
| 177 | get_name_segments_from_name(struct ccn_charbuf *name, int start_indx, int end_indx) |
| 178 | { |
| 179 | int res; |
| 180 | struct ccn_indexbuf *name_comps; |
| 181 | struct ccn_charbuf *orig_router; |
| 182 | char *name_seg=NULL; |
| 183 | |
| 184 | name_comps = ccn_indexbuf_create(); |
| 185 | res = ccn_name_split(name, name_comps); |
| 186 | if ( res < 0 ){ |
| 187 | ccn_indexbuf_destroy(&name_comps); |
| 188 | return name_seg; |
| 189 | } |
| 190 | else{ |
| 191 | orig_router=ccn_charbuf_create(); |
| 192 | ccn_name_init(orig_router); |
| 193 | ccn_name_append_components(orig_router,name->buf, |
| 194 | name_comps->buf[start_indx], |
| 195 | name_comps->buf[end_indx]); |
| 196 | struct ccn_charbuf *temp1=ccn_charbuf_create(); |
| 197 | ccn_uri_append(temp1, orig_router->buf, orig_router->length, 0); |
| 198 | |
| 199 | name_seg=(char *)calloc(strlen(ccn_charbuf_as_string(temp1))+1, |
| 200 | sizeof(char)); |
| 201 | memcpy(name_seg,ccn_charbuf_as_string(temp1), |
| 202 | strlen(ccn_charbuf_as_string(temp1))); |
| 203 | name_seg[strlen(name_seg)]='\0'; |
| 204 | ccn_charbuf_destroy(&orig_router); |
| 205 | ccn_charbuf_destroy(&temp1); |
| 206 | |
| 207 | } |
| 208 | |
| 209 | ccn_indexbuf_destroy(&name_comps); |
| 210 | return name_seg; |
| 211 | } |
| 212 | |
| 213 | |
| 214 | char * |
| 215 | get_orig_router_from_key_name(struct ccn_charbuf *name, int more, int type) |
| 216 | { |
| 217 | int res; |
| 218 | struct ccn_indexbuf *name_comps; |
| 219 | struct ccn_charbuf *orig_router; |
| 220 | char *router=NULL; |
| 221 | |
| 222 | name_comps = ccn_indexbuf_create(); |
| 223 | res = ccn_name_split(name, name_comps); |
| 224 | if ( res < 0 ){ |
| 225 | ccn_indexbuf_destroy(&name_comps); |
| 226 | return router; |
| 227 | } |
| 228 | else{ |
| 229 | res=ccn_name_chop(name, name_comps, -(2-more)); |
| 230 | if ( more > 0 && type==1) |
| 231 | res=ccn_name_chop(name, name_comps, -3); |
| 232 | if ( res < 0 ){ |
| 233 | ccn_indexbuf_destroy(&name_comps); |
| 234 | return NULL; |
| 235 | } |
| 236 | else{ |
| 237 | res=check_for_tag_component_in_name(name,name_comps,"R.N.Start"); |
| 238 | if ( res > 0 ){ |
| 239 | orig_router=ccn_charbuf_create(); |
| 240 | ccn_name_init(orig_router); |
| 241 | ccn_name_append_components(orig_router,name->buf, |
| 242 | name_comps->buf[res+1], |
| 243 | name_comps->buf[name_comps->n - 1]); |
| 244 | struct ccn_charbuf *temp1=ccn_charbuf_create(); |
| 245 | ccn_uri_append(temp1, orig_router->buf, orig_router->length, 0); |
| 246 | |
| 247 | router=(char *)calloc(strlen(ccn_charbuf_as_string(temp1))+1, |
| 248 | sizeof(char)); |
| 249 | memcpy(router,ccn_charbuf_as_string(temp1), |
| 250 | strlen(ccn_charbuf_as_string(temp1))); |
| 251 | router[strlen(router)]='\0'; |
akmhoque | 284056b | 2013-03-15 01:20:16 -0500 | [diff] [blame] | 252 | ccn_charbuf_destroy(&temp1); |
| 253 | ccn_charbuf_destroy(&orig_router); |
akmhoque | b8dbba8 | 2013-03-11 11:34:17 -0500 | [diff] [blame] | 254 | } |
| 255 | else{ |
| 256 | ccn_indexbuf_destroy(&name_comps); |
| 257 | return NULL; |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | ccn_indexbuf_destroy(&name_comps); |
| 263 | return router; |
| 264 | } |
| 265 | |