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 | |
| 69 | int |
| 70 | get_orig_router_from_key_name(struct ccn_charbuf *orig_router ,struct ccn_charbuf *name) |
| 71 | { |
| 72 | int res; |
| 73 | struct ccn_indexbuf *name_comps; |
| 74 | |
| 75 | name_comps = ccn_indexbuf_create(); |
| 76 | res = ccn_name_split(name, name_comps); |
| 77 | if ( res < 0 ){ |
| 78 | ccn_indexbuf_destroy(&name_comps); |
| 79 | return res; |
| 80 | } |
| 81 | else{ |
akmhoque | bfc882e | 2013-03-08 01:16:09 -0600 | [diff] [blame] | 82 | res=ccn_name_chop(name, name_comps, -2); |
akmhoque | 7adb277 | 2013-03-05 16:30:59 -0600 | [diff] [blame] | 83 | if ( res < 0 ){ |
| 84 | ccn_indexbuf_destroy(&name_comps); |
| 85 | return res; |
| 86 | } |
| 87 | else{ |
| 88 | res=check_for_tag_component_in_name(name,name_comps,"R.N.Start"); |
| 89 | if ( res > 0 ){ |
| 90 | ccn_name_init(orig_router); |
| 91 | ccn_name_append_components(orig_router,name->buf, |
| 92 | name_comps->buf[res+1], |
| 93 | name_comps->buf[name_comps->n - 1]); |
| 94 | } |
| 95 | else{ |
| 96 | ccn_indexbuf_destroy(&name_comps); |
| 97 | return -1; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | ccn_indexbuf_destroy(&name_comps); |
| 103 | return 0; |
| 104 | } |
| 105 | |
akmhoque | 7c64d80 | 2013-03-05 10:18:37 -0600 | [diff] [blame] | 106 | int |
| 107 | check_for_name_component_in_name(const struct ccn_charbuf *name, |
| 108 | const struct ccn_indexbuf *indx, |
| 109 | const char *component){ |
| 110 | |
| 111 | int res,i; |
| 112 | int result_position=0; |
| 113 | int name_comps=(int)indx->n; |
| 114 | |
| 115 | for(i=0;i<name_comps;i++){ |
| 116 | res=ccn_name_comp_strcmp(name->buf,indx,i,component); |
| 117 | if( res == 0){ |
| 118 | |
| 119 | result_position=i; |
| 120 | break; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | return result_position; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | int |
| 129 | check_for_tag_component_in_name(const struct ccn_charbuf *name, |
| 130 | const struct ccn_indexbuf *indx, |
| 131 | const char *component){ |
| 132 | |
| 133 | int res,i; |
| 134 | int result_position=0; |
| 135 | int name_comps=(int)indx->n; |
| 136 | |
| 137 | for(i=0;i<name_comps;i++){ |
| 138 | const unsigned char *comp_ptr; |
| 139 | size_t comp_size; |
| 140 | res=ccn_name_comp_get(name->buf, indx,i,&comp_ptr, &comp_size); |
| 141 | if( res == 0){ |
| 142 | if ( strstr((char *)comp_ptr,component) != NULL ){ |
| 143 | result_position=i; |
| 144 | break; |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | return result_position; |
| 150 | } |
| 151 | |
| 152 | enum key_type |
| 153 | get_key_type_from_key_name(struct ccn_charbuf *keyname) |
| 154 | { |
| 155 | printf("get_key_type_from_key_name called\n"); |
| 156 | |
| 157 | int res; |
| 158 | int return_key=UNKNOWN_KEY; |
| 159 | |
| 160 | struct ccn_indexbuf *indx=ccn_indexbuf_create(); |
| 161 | if ( indx == NULL ){ |
| 162 | printf("Error in creating index for key name \n"); |
| 163 | return UNKNOWN_KEY; |
| 164 | } |
| 165 | |
| 166 | res=ccn_name_split(keyname,indx); |
| 167 | if ( res < 0 ){ |
| 168 | printf("Error in parsing key name \n"); |
| 169 | ccn_indexbuf_destroy(&indx); |
| 170 | return UNKNOWN_KEY; |
| 171 | } |
| 172 | else if ( res == 3){ |
| 173 | int chk_ndn=check_for_name_component_in_name(keyname,indx,"ndn"); |
| 174 | int chk_key=check_for_name_component_in_name(keyname,indx,"keys"); |
| 175 | if ( chk_ndn == 0 && chk_key == 1) |
| 176 | return_key=ROOT_KEY; |
| 177 | } |
| 178 | else{ |
| 179 | int check_op,check_rt; |
| 180 | check_op=check_for_tag_component_in_name(keyname,indx, |
| 181 | "O.N.Start"); |
| 182 | check_rt=check_for_tag_component_in_name(keyname,indx, |
| 183 | "R.N.Start"); |
| 184 | if ( check_op > 0){ |
| 185 | return_key=OPERATOR_KEY; |
| 186 | } |
| 187 | else if(check_rt >0){ |
| 188 | int check_nlsr; |
| 189 | check_nlsr=check_for_name_component_in_name(keyname,indx, |
| 190 | "nlsr"); |
| 191 | if ( check_rt > 0 ){ |
| 192 | if ( check_nlsr > 0){ |
| 193 | return_key=NLSR_KEY; |
| 194 | } |
| 195 | else{ |
| 196 | return_key=ROUTING_KEY; |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | else if ( check_rt == 0 && check_op == 0 && res > 3){ |
| 201 | return_key=SITE_KEY; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | ccn_indexbuf_destroy(&indx); |
| 206 | return return_key; |
| 207 | } |