akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 1 | #include<stdio.h> |
| 2 | #include<string.h> |
| 3 | #include<stdlib.h> |
| 4 | #include <unistd.h> |
| 5 | #include <getopt.h> |
| 6 | #include <sys/time.h> |
| 7 | #include <assert.h> |
| 8 | #ifdef HAVE_CONFIG_H |
| 9 | #include <config.h> |
| 10 | #endif |
| 11 | #include <sys/types.h> |
| 12 | #include <signal.h> |
| 13 | |
| 14 | |
| 15 | |
| 16 | #include <ccn/ccn.h> |
| 17 | #include <ccn/uri.h> |
| 18 | #include <ccn/keystore.h> |
| 19 | #include <ccn/signing.h> |
| 20 | #include <ccn/schedule.h> |
| 21 | #include <ccn/hashtb.h> |
| 22 | |
| 23 | #include "nlsr.h" |
| 24 | #include "nlsr_npt.h" |
| 25 | #include "nlsr_fib.h" |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 26 | #include "nlsr_route.h" |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 27 | |
| 28 | int |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 29 | add_npt_entry(char *orig_router, char *name_prefix, int num_face, int *faces, int *route_costs) |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 30 | { |
| 31 | if ( strcmp(orig_router,nlsr->router_name)== 0) |
| 32 | { |
| 33 | return -1; |
| 34 | } |
| 35 | |
| 36 | struct npt_entry *ne=(struct npt_entry*)malloc(sizeof(struct npt_entry )); |
| 37 | |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 38 | int res,res_nle,res_fle; |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 39 | struct hashtb_enumerator ee; |
| 40 | struct hashtb_enumerator *e = ⅇ |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 41 | |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 42 | |
| 43 | hashtb_start(nlsr->npt, e); |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 44 | res = hashtb_seek(e, orig_router, strlen(orig_router), 0); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 45 | |
| 46 | if(res == HT_NEW_ENTRY) |
| 47 | { |
| 48 | ne=e->data; |
| 49 | |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 50 | ne->orig_router=(char *)malloc(strlen(orig_router)+1); |
| 51 | memset(ne->orig_router,0,strlen(orig_router)+1); |
| 52 | memcpy(ne->orig_router,orig_router,strlen(orig_router)); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 53 | |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 54 | |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 55 | |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 56 | |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 57 | struct hashtb_param param_nle = {0}; |
| 58 | ne->name_list= hashtb_create(sizeof(struct name_list_entry ), ¶m_nle); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 59 | |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 60 | struct hashtb_enumerator eenle; |
| 61 | struct hashtb_enumerator *enle = &eenle; |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 62 | |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 63 | hashtb_start(ne->name_list, enle); |
| 64 | res_nle = hashtb_seek(enle, name_prefix, strlen(name_prefix), 0); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 65 | |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 66 | if(res_nle == HT_NEW_ENTRY ) |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 67 | { |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 68 | struct name_list_entry *nle=(struct name_list_entry *)malloc(sizeof(struct name_list_entry)); |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 69 | nle=enle->data; |
| 70 | nle->name=(char *)malloc(strlen(name_prefix)+1); |
| 71 | memset(nle->name,0,strlen(name_prefix)+1); |
| 72 | memcpy(nle->name,name_prefix,strlen(name_prefix)); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 73 | |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 74 | |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 75 | |
| 76 | } |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 77 | hashtb_end(enle); |
| 78 | |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 79 | struct hashtb_param param_fle = {0}; |
| 80 | ne->face_list=hashtb_create(sizeof(struct face_list_entry), ¶m_fle); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 81 | |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 82 | if ( num_face > 0 ) |
| 83 | { |
| 84 | struct hashtb_enumerator eef; |
| 85 | struct hashtb_enumerator *ef = &eef; |
| 86 | |
| 87 | hashtb_start(ne->face_list, ef); |
| 88 | int i; |
| 89 | |
| 90 | for ( i=0; i< num_face ; i ++) |
| 91 | { |
| 92 | int face=faces[i]; |
| 93 | res_fle = hashtb_seek(ef, &face, sizeof(face), 0); |
| 94 | |
| 95 | if ( res_fle == HT_NEW_ENTRY ) |
| 96 | { |
| 97 | struct face_list_entry *fle=(struct face_list_entry *)malloc(sizeof(struct face_list_entry)); |
| 98 | fle=ef->data; |
| 99 | fle->next_hop_face=face; |
| 100 | fle->route_cost=route_costs[i]; |
| 101 | } |
| 102 | |
| 103 | } |
| 104 | hashtb_end(ef); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | //ne->next_hop_face=face; |
| 109 | |
| 110 | /* |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 111 | if ( face != NO_FACE ) |
| 112 | { |
| 113 | add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)name_prefix, OP_REG, face); |
| 114 | } |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 115 | */ |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 116 | } |
| 117 | else if (res == HT_OLD_ENTRY) |
| 118 | { |
| 119 | free(ne); |
| 120 | struct npt_entry *one; |
| 121 | |
| 122 | one=e->data; |
| 123 | |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 124 | struct hashtb_enumerator eenle; |
| 125 | struct hashtb_enumerator *enle = &eenle; |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 126 | |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 127 | hashtb_start(one->name_list, enle); |
| 128 | res_nle = hashtb_seek(enle, name_prefix, strlen(name_prefix), 0); |
| 129 | |
| 130 | if(res_nle == HT_NEW_ENTRY ) |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 131 | { |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 132 | struct name_list_entry *nle=(struct name_list_entry *)malloc(sizeof(struct name_list_entry)); |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 133 | nle=enle->data; |
| 134 | nle->name=(char *)malloc(strlen(name_prefix)+1); |
| 135 | memset(nle->name,0,strlen(name_prefix)+1); |
| 136 | memcpy(nle->name,name_prefix,strlen(name_prefix)); |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 137 | /* |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 138 | if ( face != NO_FACE ) |
| 139 | { |
| 140 | add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)name_prefix, OP_REG, face); |
| 141 | } |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 142 | */ |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 143 | } |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 144 | else if(res_nle == HT_OLD_ENTRY ) |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 145 | { |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 146 | |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 147 | } |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 148 | hashtb_end(enle); |
| 149 | |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 150 | if ( num_face > 0 ) |
| 151 | { |
| 152 | struct hashtb_enumerator eef; |
| 153 | struct hashtb_enumerator *ef = &eef; |
| 154 | |
| 155 | hashtb_start(one->face_list, ef); |
| 156 | int i; |
| 157 | |
| 158 | for ( i=0; i< num_face ; i ++) |
| 159 | { |
| 160 | int face=faces[i]; |
| 161 | res_fle = hashtb_seek(ef, &face, sizeof(face), 0); |
| 162 | |
| 163 | if ( res_fle == HT_NEW_ENTRY ) |
| 164 | { |
| 165 | struct face_list_entry *fle=(struct face_list_entry *)malloc(sizeof(struct face_list_entry)); |
| 166 | fle=ef->data; |
| 167 | fle->next_hop_face=face; |
| 168 | fle->route_cost=route_costs[i]; |
| 169 | } |
| 170 | |
| 171 | } |
| 172 | hashtb_end(ef); |
| 173 | } |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 174 | |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 175 | |
| 176 | } |
| 177 | hashtb_end(e); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 178 | return res; |
| 179 | } |
| 180 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 181 | int |
| 182 | delete_npt_entry(char *orig_router, char *name_prefix) |
| 183 | { |
| 184 | if ( strcmp(orig_router,nlsr->router_name)== 0) |
| 185 | { |
| 186 | return -1; |
| 187 | } |
| 188 | |
| 189 | struct npt_entry *ne; |
| 190 | |
| 191 | int res,res_nle; |
| 192 | struct hashtb_enumerator ee; |
| 193 | struct hashtb_enumerator *e = ⅇ |
| 194 | |
| 195 | |
| 196 | hashtb_start(nlsr->npt, e); |
| 197 | res = hashtb_seek(e, orig_router, strlen(orig_router), 0); |
| 198 | |
| 199 | if(res == HT_NEW_ENTRY) |
| 200 | { |
| 201 | hashtb_delete(e); |
| 202 | return -1; |
| 203 | } |
| 204 | else if (res == HT_OLD_ENTRY) |
| 205 | { |
| 206 | ne=e->data; |
| 207 | |
| 208 | struct hashtb_enumerator eenle; |
| 209 | struct hashtb_enumerator *enle = &eenle; |
| 210 | |
| 211 | hashtb_start(ne->name_list, enle); |
| 212 | res_nle = hashtb_seek(enle, name_prefix, strlen(name_prefix), 0); |
| 213 | |
| 214 | if(res_nle == HT_NEW_ENTRY ) |
| 215 | { |
| 216 | hashtb_delete(enle); |
| 217 | } |
| 218 | else if(res_nle == HT_OLD_ENTRY ) |
| 219 | { |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 220 | /* |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 221 | if (ne->next_hop_face != NO_FACE ) |
| 222 | { |
| 223 | add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)name_prefix, OP_UNREG, ne->next_hop_face); |
| 224 | } |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 225 | */ |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 226 | hashtb_delete(enle); |
| 227 | } |
| 228 | |
| 229 | hashtb_end(enle); |
akmhoque | da5b683 | 2012-09-13 22:33:55 -0500 | [diff] [blame] | 230 | |
| 231 | if ( hashtb_n(ne->name_list) == 0 ) |
| 232 | { |
| 233 | hashtb_delete(e); |
| 234 | } |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | hashtb_end(e); |
| 238 | |
| 239 | return 0; |
| 240 | } |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 241 | |
| 242 | void |
| 243 | print_npt(void) |
| 244 | { |
| 245 | printf("\n"); |
| 246 | printf("print_npt called\n\n"); |
| 247 | int i, npt_element; |
| 248 | |
| 249 | struct npt_entry *ne; |
| 250 | |
| 251 | struct hashtb_enumerator ee; |
| 252 | struct hashtb_enumerator *e = ⅇ |
| 253 | |
| 254 | hashtb_start(nlsr->npt, e); |
| 255 | npt_element=hashtb_n(nlsr->npt); |
| 256 | |
| 257 | for(i=0;i<npt_element;i++) |
| 258 | { |
| 259 | printf("\n"); |
| 260 | printf("----------NPT ENTRY %d------------------\n",i+1); |
| 261 | ne=e->data; |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 262 | printf(" Origination Router: %s \n",ne->orig_router); |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 263 | //ne->next_hop_face == NO_FACE ? printf(" Next Hop Face: NO_NEXT_HOP \n") : printf(" Next Hop Face: %d \n", ne->next_hop_face); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 264 | |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 265 | int j, nl_element,face_list_element; |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 266 | struct name_list_entry *nle; |
| 267 | struct hashtb_enumerator eenle; |
| 268 | struct hashtb_enumerator *enle = &eenle; |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 269 | |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 270 | hashtb_start(ne->name_list, enle); |
| 271 | nl_element=hashtb_n(ne->name_list); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 272 | |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 273 | for (j=0;j<nl_element;j++) |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 274 | { |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 275 | nle=enle->data; |
| 276 | printf(" Name Prefix: %s \n",nle->name); |
| 277 | hashtb_next(enle); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 278 | } |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 279 | hashtb_end(enle); |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 280 | |
| 281 | struct face_list_entry *fle; |
| 282 | |
| 283 | struct hashtb_enumerator eef; |
| 284 | struct hashtb_enumerator *ef = &eef; |
| 285 | |
| 286 | hashtb_start(ne->face_list, ef); |
| 287 | face_list_element=hashtb_n(ne->face_list); |
| 288 | if ( face_list_element <= 0 ) |
| 289 | { |
| 290 | printf(" Face: No Face \n"); |
| 291 | } |
| 292 | else |
| 293 | { |
| 294 | for(j=0;j<face_list_element;j++) |
| 295 | { |
| 296 | fle=ef->data; |
| 297 | printf(" Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost); |
| 298 | hashtb_next(ef); |
| 299 | } |
| 300 | } |
| 301 | hashtb_end(ef); |
| 302 | |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 303 | |
| 304 | hashtb_next(e); |
| 305 | } |
| 306 | |
| 307 | hashtb_end(e); |
| 308 | |
| 309 | printf("\n"); |
| 310 | } |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 311 | |
| 312 | void |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 313 | delete_orig_router_from_npt(char *orig_router) |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 314 | { |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 315 | int res,num_face,num_prefix; |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 316 | struct npt_entry *ne; |
| 317 | |
| 318 | struct hashtb_enumerator ee; |
| 319 | struct hashtb_enumerator *e = ⅇ |
| 320 | |
| 321 | hashtb_start(nlsr->npt, e); |
| 322 | res = hashtb_seek(e, orig_router, strlen(orig_router), 0); |
| 323 | |
| 324 | if ( res == HT_OLD_ENTRY ) |
| 325 | { |
| 326 | ne=e->data; |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 327 | num_prefix=hashtb_n(ne->name_list); |
| 328 | if ( num_prefix > 0 ) |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 329 | { |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 330 | num_face=hashtb_n(ne->face_list); |
| 331 | |
| 332 | if ( num_face > 0 ) |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 333 | { |
| 334 | int j, nl_element; |
| 335 | struct name_list_entry *nle; |
| 336 | struct hashtb_enumerator eenle; |
| 337 | struct hashtb_enumerator *enle = &eenle; |
| 338 | |
| 339 | hashtb_start(ne->name_list, enle); |
| 340 | nl_element=hashtb_n(ne->name_list); |
| 341 | |
| 342 | for (j=0;j<nl_element;j++) |
| 343 | { |
| 344 | nle=enle->data; |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 345 | |
| 346 | int k,face_list_element; |
| 347 | struct face_list_entry *fle; |
| 348 | |
| 349 | struct hashtb_enumerator eef; |
| 350 | struct hashtb_enumerator *ef = &eef; |
| 351 | |
| 352 | hashtb_start(ne->face_list, ef); |
| 353 | face_list_element=hashtb_n(ne->face_list); |
| 354 | for(k=0;k<face_list_element;k++) |
| 355 | { |
| 356 | fle=ef->data; |
| 357 | //printf(" Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost); |
| 358 | //add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG , next_hop_face); |
| 359 | hashtb_next(ef); |
| 360 | } |
| 361 | hashtb_end(ef); |
| 362 | |
| 363 | //add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG , next_hop_face); |
| 364 | |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 365 | hashtb_next(enle); |
| 366 | } |
| 367 | hashtb_end(enle); |
| 368 | |
| 369 | } |
| 370 | |
| 371 | } |
| 372 | hashtb_destroy(&ne->name_list); |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 373 | hashtb_destroy(&ne->face_list); |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 374 | hashtb_delete(e); |
| 375 | } |
| 376 | else if ( res == HT_NEW_ENTRY ) |
| 377 | { |
| 378 | hashtb_delete(e); |
| 379 | } |
| 380 | hashtb_end(e); |
| 381 | } |
akmhoque | fbfd098 | 2012-09-09 20:59:03 -0500 | [diff] [blame] | 382 | |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 383 | /* |
akmhoque | fbfd098 | 2012-09-09 20:59:03 -0500 | [diff] [blame] | 384 | |
| 385 | void |
| 386 | update_npt_with_new_route(char * orig_router,int next_hop_face) |
| 387 | { |
| 388 | int res; |
| 389 | struct npt_entry *ne; |
| 390 | |
| 391 | struct hashtb_enumerator ee; |
| 392 | struct hashtb_enumerator *e = ⅇ |
| 393 | |
| 394 | hashtb_start(nlsr->npt, e); |
| 395 | res = hashtb_seek(e, orig_router, strlen(orig_router), 0); |
| 396 | |
| 397 | if ( res == HT_OLD_ENTRY ) |
| 398 | { |
| 399 | ne=e->data; |
| 400 | |
| 401 | if ( next_hop_face != ne->next_hop_face ) |
| 402 | { |
| 403 | int j, nl_element; |
| 404 | struct name_list_entry *nle; |
| 405 | struct hashtb_enumerator eenle; |
| 406 | struct hashtb_enumerator *enle = &eenle; |
| 407 | |
| 408 | hashtb_start(ne->name_list, enle); |
| 409 | nl_element=hashtb_n(ne->name_list); |
| 410 | |
| 411 | for (j=0;j<nl_element;j++) |
| 412 | { |
| 413 | nle=enle->data; |
| 414 | if (ne->next_hop_face != NO_FACE ) |
| 415 | { |
| 416 | add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG , ne->next_hop_face); |
| 417 | } |
| 418 | if (next_hop_face != NO_FACE ) |
| 419 | { |
| 420 | add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_REG , next_hop_face); |
| 421 | } |
| 422 | hashtb_next(enle); |
| 423 | } |
| 424 | hashtb_end(enle); |
| 425 | ne->next_hop_face=next_hop_face; |
| 426 | } |
| 427 | |
| 428 | } |
| 429 | else if ( res == HT_NEW_ENTRY ) |
| 430 | { |
| 431 | hashtb_delete(e); |
| 432 | } |
| 433 | hashtb_end(e); |
| 434 | } |
| 435 | |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 436 | */ |
| 437 | |
| 438 | |
| 439 | void |
| 440 | add_face_to_npt_by_face_id(char *dest_router, int face_id, int route_cost) |
| 441 | { |
| 442 | printf("add_face_to_npt_by_face_id called\n"); |
| 443 | |
| 444 | printf("Dest Router: %s Face: %d Route_cost: %d \n",dest_router, face_id, route_cost); |
| 445 | int res,res1; |
| 446 | struct npt_entry *ne; |
| 447 | |
| 448 | struct hashtb_enumerator ee; |
| 449 | struct hashtb_enumerator *e = ⅇ |
| 450 | |
| 451 | hashtb_start(nlsr->npt, e); |
| 452 | res = hashtb_seek(e, dest_router, strlen(dest_router), 0); |
| 453 | |
| 454 | if ( res == HT_OLD_ENTRY ) |
| 455 | { |
| 456 | printf("Dest Router Found \n"); |
| 457 | ne=e->data; |
| 458 | |
| 459 | struct hashtb_enumerator eef; |
| 460 | struct hashtb_enumerator *ef = &eef; |
| 461 | |
| 462 | hashtb_start(ne->face_list, ef); |
| 463 | res1=hashtb_seek(ef, &face_id, sizeof(face_id), 0); |
| 464 | |
| 465 | if ( res1 == HT_OLD_ENTRY ) |
| 466 | { |
| 467 | printf("Face Found \n"); |
| 468 | struct face_list_entry *fle;//=(struct face_list_entry *)malloc(sizeof(struct face_list_entry)); |
| 469 | fle=ef->data; |
| 470 | fle->next_hop_face=face_id; |
| 471 | fle->route_cost=route_cost; |
| 472 | } |
| 473 | else if ( res1 == HT_NEW_ENTRY ) |
| 474 | { |
| 475 | printf("Face Not Found \n"); |
| 476 | struct face_list_entry *fle=(struct face_list_entry *)malloc(sizeof(struct face_list_entry)); |
| 477 | fle=ef->data; |
| 478 | fle->next_hop_face=face_id; |
| 479 | fle->route_cost=route_cost; |
| 480 | //hashtb_delete(ef); |
| 481 | } |
| 482 | hashtb_end(ef); |
| 483 | } |
| 484 | else if (res == HT_NEW_ENTRY) |
| 485 | { |
| 486 | hashtb_delete(e); |
| 487 | } |
| 488 | |
| 489 | hashtb_end(e); |
| 490 | } |
| 491 | |
| 492 | |
| 493 | void |
| 494 | add_new_fib_entries_to_npt() |
| 495 | { |
| 496 | printf("add_new_fib_entries_to_npt called\n"); |
| 497 | int i,j, rt_element,face_list_element; |
| 498 | |
| 499 | struct routing_table_entry *rte; |
| 500 | |
| 501 | struct hashtb_enumerator ee; |
| 502 | struct hashtb_enumerator *e = ⅇ |
| 503 | |
| 504 | hashtb_start(nlsr->routing_table, e); |
| 505 | rt_element=hashtb_n(nlsr->routing_table); |
| 506 | |
| 507 | for(i=0;i<rt_element;i++) |
| 508 | { |
| 509 | //printf("----------Routing Table Entry %d------------------\n",i+1); |
| 510 | rte=e->data; |
| 511 | //printf(" Destination Router: %s \n",rte->dest_router); |
| 512 | //rte->next_hop_face == NO_NEXT_HOP ? printf(" Next Hop Face: NO_NEXT_HOP \n") : printf(" Next Hop Face: %d \n", rte->next_hop_face); |
| 513 | |
| 514 | struct face_list_entry *fle; |
| 515 | |
| 516 | struct hashtb_enumerator eef; |
| 517 | struct hashtb_enumerator *ef = &eef; |
| 518 | |
| 519 | hashtb_start(rte->face_list, ef); |
| 520 | face_list_element=hashtb_n(rte->face_list); |
| 521 | if ( face_list_element <= 0 ) |
| 522 | { |
| 523 | printf(" Face: No Face \n"); |
| 524 | } |
| 525 | else |
| 526 | { |
| 527 | for(j=0;j<face_list_element;j++) |
| 528 | { |
| 529 | fle=ef->data; |
| 530 | //printf(" Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost); |
| 531 | add_face_to_npt_by_face_id(rte->dest_router,fle->next_hop_face,fle->route_cost); |
| 532 | hashtb_next(ef); |
| 533 | } |
| 534 | } |
| 535 | hashtb_end(ef); |
| 536 | |
| 537 | hashtb_next(e); |
| 538 | } |
| 539 | |
| 540 | hashtb_end(e); |
| 541 | |
| 542 | } |
| 543 | |
| 544 | |
| 545 | void |
| 546 | delete_face_from_npt_by_face_id(char *dest_router, int face_id) |
| 547 | { |
| 548 | printf("delete_face_from_npt_by_face_id called\n"); |
| 549 | |
| 550 | int res,res1; |
| 551 | struct npt_entry *ne; |
| 552 | |
| 553 | struct hashtb_enumerator ee; |
| 554 | struct hashtb_enumerator *e = ⅇ |
| 555 | |
| 556 | hashtb_start(nlsr->npt, e); |
| 557 | res = hashtb_seek(e, dest_router, strlen(dest_router), 0); |
| 558 | |
| 559 | if ( res == HT_OLD_ENTRY ) |
| 560 | { |
| 561 | ne=e->data; |
| 562 | |
| 563 | struct hashtb_enumerator eef; |
| 564 | struct hashtb_enumerator *ef = &eef; |
| 565 | |
| 566 | hashtb_start(ne->face_list, ef); |
| 567 | res1=hashtb_seek(ef, &face_id, sizeof(face_id), 0); |
| 568 | |
| 569 | if ( res1 == HT_OLD_ENTRY ) |
| 570 | { |
| 571 | hashtb_delete(ef); |
| 572 | } |
| 573 | else if ( res1 == HT_NEW_ENTRY ) |
| 574 | { |
| 575 | hashtb_delete(ef); |
| 576 | } |
| 577 | hashtb_end(ef); |
| 578 | } |
| 579 | else if (res == HT_NEW_ENTRY) |
| 580 | { |
| 581 | hashtb_delete(e); |
| 582 | } |
| 583 | |
| 584 | hashtb_end(e); |
| 585 | } |
| 586 | |
| 587 | int |
| 588 | delete_old_face_from_npt(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags) |
| 589 | { |
| 590 | if(flags == CCN_SCHEDULE_CANCEL) |
| 591 | { |
| 592 | return -1; |
| 593 | } |
| 594 | |
| 595 | nlsr_lock(); |
| 596 | |
| 597 | printf("delete_old_face_from_npt called\n"); |
| 598 | if ( ev->evdata != NULL ) |
| 599 | { |
| 600 | printf("Event Data: %s \n",(char *)ev->evdata); |
| 601 | char *sep="|"; |
| 602 | char *rem; |
| 603 | char *orig_router; |
| 604 | char *faceid; |
| 605 | int face_id; |
| 606 | |
| 607 | char *face_data=(char *)malloc(strlen((char *)ev->evdata)+1); |
| 608 | memset(face_data,0,strlen((char *)ev->evdata)+1); |
| 609 | memcpy(face_data+strlen(face_data),(char *)ev->evdata,strlen((char *)ev->evdata)); |
| 610 | |
| 611 | orig_router=strtok_r(face_data,sep,&rem); |
| 612 | faceid=strtok_r(NULL,sep,&rem); |
| 613 | face_id=atoi(faceid); |
| 614 | printf("Orig Router: %s Face: %d \n",orig_router,face_id); |
| 615 | |
| 616 | delete_face_from_npt_by_face_id(orig_router,face_id); |
| 617 | } |
| 618 | |
| 619 | nlsr_unlock(); |
| 620 | |
| 621 | return 0; |
| 622 | } |
| 623 | |
| 624 | void |
| 625 | clean_old_fib_entries_from_npt() |
| 626 | { |
| 627 | |
| 628 | printf("clean_old_fib_entries_from_npt called\n\n"); |
| 629 | int i, npt_element; |
| 630 | |
| 631 | struct npt_entry *ne; |
| 632 | |
| 633 | struct hashtb_enumerator ee; |
| 634 | struct hashtb_enumerator *e = ⅇ |
| 635 | |
| 636 | hashtb_start(nlsr->npt, e); |
| 637 | npt_element=hashtb_n(nlsr->npt); |
| 638 | |
| 639 | for(i=0;i<npt_element;i++) |
| 640 | { |
| 641 | ne=e->data; |
| 642 | |
| 643 | int j,k, nl_element,face_list_element; |
| 644 | struct face_list_entry *fle; |
| 645 | |
| 646 | struct hashtb_enumerator eef; |
| 647 | struct hashtb_enumerator *ef = &eef; |
| 648 | |
| 649 | hashtb_start(ne->face_list, ef); |
| 650 | face_list_element=hashtb_n(ne->face_list); |
| 651 | if ( face_list_element <= 0 ) |
| 652 | { |
| 653 | printf(" Face: No Face \n"); |
| 654 | } |
| 655 | else |
| 656 | { |
| 657 | for(j=0;j<face_list_element;j++) |
| 658 | { |
| 659 | fle=ef->data; |
| 660 | int check=does_face_exist_for_router(ne->orig_router,fle->next_hop_face); |
| 661 | if ( check == 0 ) |
| 662 | { |
| 663 | struct name_list_entry *nle; |
| 664 | struct hashtb_enumerator eenle; |
| 665 | struct hashtb_enumerator *enle = &eenle; |
| 666 | |
| 667 | hashtb_start(ne->name_list, enle); |
| 668 | nl_element=hashtb_n(ne->name_list); |
| 669 | |
| 670 | for (k=0;k<nl_element;k++) |
| 671 | { |
| 672 | nle=enle->data; |
| 673 | |
| 674 | //delete all the fib entries here |
| 675 | |
| 676 | hashtb_next(enle); |
| 677 | } |
| 678 | hashtb_end(enle); |
| 679 | |
| 680 | char faceid[20]; |
| 681 | memset(faceid,0,20); |
| 682 | sprintf(faceid,"%d",fle->next_hop_face); |
| 683 | char *evdata=(char *)malloc(strlen(ne->orig_router)+strlen(faceid)+2); |
| 684 | memset(evdata,0,strlen(ne->orig_router)+strlen(faceid)+2); |
| 685 | memcpy(evdata+strlen(evdata),ne->orig_router,strlen(ne->orig_router)); |
| 686 | memcpy(evdata+strlen(evdata),"|",1); |
| 687 | memcpy(evdata+strlen(evdata),faceid,strlen(faceid)); |
| 688 | |
| 689 | nlsr->event = ccn_schedule_event(nlsr->sched, 1, &delete_old_face_from_npt, (void *)evdata, 0); |
| 690 | |
| 691 | } |
| 692 | |
| 693 | hashtb_next(ef); |
| 694 | } |
| 695 | } |
| 696 | hashtb_end(ef); |
| 697 | |
| 698 | |
| 699 | hashtb_next(e); |
| 700 | } |
| 701 | |
| 702 | hashtb_end(e); |
| 703 | |
| 704 | } |
| 705 | |
| 706 | void |
| 707 | update_npt_with_new_route() |
| 708 | { |
| 709 | clean_old_fib_entries_from_npt(); |
| 710 | add_new_fib_entries_to_npt(); |
| 711 | } |
| 712 | |
akmhoque | fbfd098 | 2012-09-09 20:59:03 -0500 | [diff] [blame] | 713 | void |
| 714 | destroy_all_face_by_nlsr(void) |
| 715 | { |
| 716 | int i, npt_element; |
| 717 | |
| 718 | struct npt_entry *ne; |
| 719 | |
| 720 | struct hashtb_enumerator ee; |
| 721 | struct hashtb_enumerator *e = ⅇ |
| 722 | |
| 723 | hashtb_start(nlsr->npt, e); |
| 724 | npt_element=hashtb_n(nlsr->npt); |
| 725 | |
| 726 | for(i=0;i<npt_element;i++) |
| 727 | { |
akmhoque | fbfd098 | 2012-09-09 20:59:03 -0500 | [diff] [blame] | 728 | ne=e->data; |
akmhoque | fbfd098 | 2012-09-09 20:59:03 -0500 | [diff] [blame] | 729 | |
| 730 | int j, nl_element; |
| 731 | struct name_list_entry *nle; |
| 732 | struct hashtb_enumerator eenle; |
| 733 | struct hashtb_enumerator *enle = &eenle; |
| 734 | |
| 735 | hashtb_start(ne->name_list, enle); |
| 736 | nl_element=hashtb_n(ne->name_list); |
| 737 | |
| 738 | for (j=0;j<nl_element;j++) |
| 739 | { |
| 740 | nle=enle->data; |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 741 | /* |
akmhoque | fbfd098 | 2012-09-09 20:59:03 -0500 | [diff] [blame] | 742 | if ( ne->next_hop_face != NO_FACE) |
| 743 | { |
| 744 | add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG , ne->next_hop_face); |
| 745 | } |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 746 | */ |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 747 | //printf(" Name Prefix: %s Face: %d \n",nle->name,ne->next_hop_face); |
akmhoque | fbfd098 | 2012-09-09 20:59:03 -0500 | [diff] [blame] | 748 | hashtb_next(enle); |
| 749 | } |
| 750 | hashtb_end(enle); |
| 751 | |
| 752 | hashtb_next(e); |
| 753 | } |
| 754 | |
| 755 | hashtb_end(e); |
| 756 | |
| 757 | printf("\n"); |
| 758 | } |