akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [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" |
| 26 | #include "nlsr_route.h" |
| 27 | #include "nlsr_adl.h" |
| 28 | #include "utility.h" |
| 29 | |
| 30 | int |
| 31 | add_npt_entry(char *orig_router, char *name_prefix, int num_face, int *faces, int *route_costs) |
| 32 | { |
| 33 | if ( strcmp(orig_router,nlsr->router_name)== 0) |
| 34 | { |
| 35 | return -1; |
| 36 | } |
| 37 | |
| 38 | struct npt_entry *ne=(struct npt_entry*)malloc(sizeof(struct npt_entry )); |
| 39 | |
| 40 | int res,res_nle,res_fle; |
| 41 | struct hashtb_enumerator ee; |
| 42 | struct hashtb_enumerator *e = ⅇ |
| 43 | |
| 44 | |
| 45 | hashtb_start(nlsr->npt, e); |
| 46 | res = hashtb_seek(e, orig_router, strlen(orig_router), 0); |
| 47 | |
| 48 | if(res == HT_NEW_ENTRY) |
| 49 | { |
| 50 | ne=e->data; |
| 51 | |
| 52 | ne->orig_router=(char *)malloc(strlen(orig_router)+1); |
| 53 | memset(ne->orig_router,0,strlen(orig_router)+1); |
| 54 | memcpy(ne->orig_router,orig_router,strlen(orig_router)); |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | struct hashtb_param param_nle = {0}; |
| 60 | ne->name_list= hashtb_create(sizeof(struct name_list_entry ), ¶m_nle); |
| 61 | |
| 62 | struct hashtb_enumerator eenle; |
| 63 | struct hashtb_enumerator *enle = &eenle; |
| 64 | |
| 65 | hashtb_start(ne->name_list, enle); |
| 66 | res_nle = hashtb_seek(enle, name_prefix, strlen(name_prefix), 0); |
| 67 | |
| 68 | if(res_nle == HT_NEW_ENTRY ) |
| 69 | { |
| 70 | struct name_list_entry *nle=(struct name_list_entry *)malloc(sizeof(struct name_list_entry)); |
| 71 | nle=enle->data; |
| 72 | nle->name=(char *)malloc(strlen(name_prefix)+1); |
| 73 | memset(nle->name,0,strlen(name_prefix)+1); |
| 74 | memcpy(nle->name,name_prefix,strlen(name_prefix)); |
| 75 | |
| 76 | |
| 77 | |
| 78 | } |
| 79 | hashtb_end(enle); |
| 80 | |
| 81 | struct hashtb_param param_fle = {0}; |
| 82 | ne->face_list=hashtb_create(sizeof(struct face_list_entry), ¶m_fle); |
| 83 | |
| 84 | if ( num_face > 0 ) |
| 85 | { |
| 86 | struct hashtb_enumerator eef; |
| 87 | struct hashtb_enumerator *ef = &eef; |
| 88 | |
| 89 | hashtb_start(ne->face_list, ef); |
| 90 | int i; |
| 91 | |
| 92 | for ( i=0; i < num_face ; i++) |
| 93 | { |
| 94 | int face=faces[i]; |
| 95 | if ( face != NO_FACE && face != ZERO_FACE) |
| 96 | { |
| 97 | res_fle = hashtb_seek(ef, &face, sizeof(face), 0); |
| 98 | |
| 99 | if ( res_fle == HT_NEW_ENTRY ) |
| 100 | { |
| 101 | struct face_list_entry *fle=(struct face_list_entry *)malloc(sizeof(struct face_list_entry)); |
| 102 | fle=ef->data; |
| 103 | fle->next_hop_face=face; |
| 104 | fle->route_cost=route_costs[i]; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | } |
| 109 | hashtb_end(ef); |
| 110 | } |
| 111 | |
| 112 | } |
| 113 | else if (res == HT_OLD_ENTRY) |
| 114 | { |
| 115 | free(ne); |
| 116 | struct npt_entry *one; |
| 117 | |
| 118 | one=e->data; |
| 119 | |
| 120 | struct hashtb_enumerator eenle; |
| 121 | struct hashtb_enumerator *enle = &eenle; |
| 122 | |
| 123 | hashtb_start(one->name_list, enle); |
| 124 | res_nle = hashtb_seek(enle, name_prefix, strlen(name_prefix), 0); |
| 125 | |
| 126 | if(res_nle == HT_NEW_ENTRY ) |
| 127 | { |
| 128 | struct name_list_entry *nle=(struct name_list_entry *)malloc(sizeof(struct name_list_entry)); |
| 129 | nle=enle->data; |
| 130 | nle->name=(char *)malloc(strlen(name_prefix)+1); |
| 131 | memset(nle->name,0,strlen(name_prefix)+1); |
| 132 | memcpy(nle->name,name_prefix,strlen(name_prefix)); |
| 133 | } |
| 134 | else if(res_nle == HT_OLD_ENTRY ) |
| 135 | { |
| 136 | |
| 137 | } |
| 138 | hashtb_end(enle); |
| 139 | |
| 140 | if ( num_face > 0 ) |
| 141 | { |
| 142 | struct hashtb_enumerator eef; |
| 143 | struct hashtb_enumerator *ef = &eef; |
| 144 | |
| 145 | hashtb_start(one->face_list, ef); |
| 146 | int i; |
| 147 | |
| 148 | for ( i=0; i< num_face ; i ++) |
| 149 | { |
| 150 | int face=faces[i]; |
| 151 | if ( face != NO_FACE && face != ZERO_FACE) |
| 152 | { |
| 153 | res_fle = hashtb_seek(ef, &face, sizeof(face), 0); |
| 154 | |
| 155 | if ( res_fle == HT_NEW_ENTRY ) |
| 156 | { |
| 157 | struct face_list_entry *fle=(struct face_list_entry *)malloc(sizeof(struct face_list_entry)); |
| 158 | fle=ef->data; |
| 159 | fle->next_hop_face=face; |
| 160 | fle->route_cost=route_costs[i]; |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | hashtb_end(ef); |
| 165 | } |
| 166 | |
| 167 | |
| 168 | } |
| 169 | hashtb_end(e); |
| 170 | |
| 171 | update_ccnd_fib_for_orig_router(orig_router); |
| 172 | |
| 173 | return res; |
| 174 | } |
| 175 | |
| 176 | void |
| 177 | update_ccnd_fib_for_orig_router(char *orig_router) |
| 178 | { |
| 179 | |
| 180 | int res; |
| 181 | struct hashtb_enumerator ee; |
| 182 | struct hashtb_enumerator *e = ⅇ |
| 183 | |
| 184 | |
| 185 | hashtb_start(nlsr->npt, e); |
| 186 | res = hashtb_seek(e, orig_router, strlen(orig_router), 0); |
| 187 | |
| 188 | if(res == HT_NEW_ENTRY) |
| 189 | { |
| 190 | hashtb_delete(e); |
| 191 | } |
| 192 | else if ( res == HT_OLD_ENTRY ) |
| 193 | { |
| 194 | struct npt_entry *ne; |
| 195 | ne=e->data; |
| 196 | int num_face=hashtb_n(ne->face_list); |
| 197 | int last_face,first_face; |
| 198 | |
| 199 | int *faces=(int *)malloc(num_face*sizeof(int)); |
| 200 | int *route_costs=(int *)malloc(num_face*sizeof(int)); |
| 201 | |
| 202 | get_all_faces_for_orig_router_from_npt(orig_router,faces,route_costs,num_face); |
| 203 | sort_faces_by_distance(faces,route_costs,0,num_face); |
| 204 | |
| 205 | |
| 206 | first_face=num_face-1; |
| 207 | |
| 208 | if ( nlsr->multi_path_face_num == 0 ) |
| 209 | { |
| 210 | last_face=first_face; |
| 211 | } |
| 212 | else |
| 213 | { |
| 214 | if ( num_face <= nlsr->multi_path_face_num) |
| 215 | { |
| 216 | last_face=0; |
| 217 | } |
| 218 | else if ( nlsr->multi_path_face_num == 0) |
| 219 | { |
| 220 | last_face=num_face-nlsr->multi_path_face_num; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | int i,j, nl_element; |
| 225 | struct name_list_entry *nle; |
| 226 | struct hashtb_enumerator eenle; |
| 227 | struct hashtb_enumerator *enle = &eenle; |
| 228 | |
| 229 | hashtb_start(ne->name_list, enle); |
| 230 | nl_element=hashtb_n(ne->name_list); |
| 231 | |
| 232 | for (i=0;i<nl_element;i++) |
| 233 | { |
| 234 | nle=enle->data; |
| 235 | |
| 236 | for( j=first_face; j>= last_face; j--) |
| 237 | { |
| 238 | //printf("Adding face: Name:%s Face: %d\n",nle->name,faces[j]); |
| 239 | //printf("Orig Router: %s \n",orig_router); |
| 240 | //printf("Name Prefix: %s \n",nle->name); |
| 241 | //printf("Is neighbor Orig Router: %d \n",is_neighbor(orig_router)); |
| 242 | //printf("Is neighbor Name Prefix: %d \n",is_neighbor(nle->name)); |
| 243 | |
| 244 | if ( is_neighbor(orig_router) == 0 ) |
| 245 | { |
| 246 | if ( nlsr->debugging ) |
| 247 | printf("Adding face: Name:%s Face: %d\n",nle->name,faces[j]); |
| 248 | if ( nlsr->detailed_logging ) |
| 249 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"Adding face: Name:%s Face: %d\n",nle->name,faces[j]); |
| 250 | //printf("Adding face: Name:%s Face: %d\n",nle->name,faces[j]); |
| 251 | add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_REG, faces[j]); |
| 252 | } |
| 253 | else |
| 254 | { |
| 255 | if ( j == last_face && is_neighbor(nle->name)==0) |
| 256 | { |
| 257 | if ( nlsr->debugging ) |
| 258 | printf("Adding face: Name:%s Face: %d\n",nle->name,faces[j]); |
| 259 | if ( nlsr->detailed_logging ) |
| 260 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"Adding face: Name:%s Face: %d\n",nle->name,faces[j]); |
| 261 | //printf("Adding face: Name:%s Face: %d\n",nle->name,faces[j]); |
| 262 | add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_REG, faces[j]); |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | |
| 268 | hashtb_next(enle); |
| 269 | } |
| 270 | hashtb_end(enle); |
| 271 | |
| 272 | |
| 273 | |
| 274 | free(faces); |
| 275 | free(route_costs); |
| 276 | |
| 277 | } |
| 278 | hashtb_end(e); |
| 279 | |
| 280 | } |
| 281 | |
| 282 | int |
| 283 | delete_npt_entry_by_router_and_name_prefix(char *orig_router, char *name_prefix) |
| 284 | { |
| 285 | if ( strcmp(orig_router,nlsr->router_name)== 0) |
| 286 | { |
| 287 | return -1; |
| 288 | } |
| 289 | |
| 290 | struct npt_entry *ne; |
| 291 | |
| 292 | int res,res_nle; |
| 293 | struct hashtb_enumerator ee; |
| 294 | struct hashtb_enumerator *e = ⅇ |
| 295 | |
| 296 | |
| 297 | hashtb_start(nlsr->npt, e); |
| 298 | res = hashtb_seek(e, orig_router, strlen(orig_router), 0); |
| 299 | |
| 300 | if(res == HT_NEW_ENTRY) |
| 301 | { |
| 302 | hashtb_delete(e); |
| 303 | return -1; |
| 304 | } |
| 305 | else if (res == HT_OLD_ENTRY) |
| 306 | { |
| 307 | ne=e->data; |
| 308 | |
| 309 | struct hashtb_enumerator eenle; |
| 310 | struct hashtb_enumerator *enle = &eenle; |
| 311 | |
| 312 | hashtb_start(ne->name_list, enle); |
| 313 | res_nle = hashtb_seek(enle, name_prefix, strlen(name_prefix), 0); |
| 314 | |
| 315 | if(res_nle == HT_NEW_ENTRY ) |
| 316 | { |
| 317 | hashtb_delete(enle); |
| 318 | } |
| 319 | else if(res_nle == HT_OLD_ENTRY ) |
| 320 | { |
| 321 | struct name_list_entry *nle; |
| 322 | |
| 323 | nle=enle->data; |
| 324 | |
| 325 | int j; |
| 326 | int num_face=hashtb_n(ne->face_list); |
| 327 | int last_face,first_face; |
| 328 | |
| 329 | int *faces=(int *)malloc(num_face*sizeof(int)); |
| 330 | int *route_costs=(int *)malloc(num_face*sizeof(int)); |
| 331 | |
| 332 | get_all_faces_for_orig_router_from_npt(orig_router,faces,route_costs,num_face); |
| 333 | sort_faces_by_distance(faces,route_costs,0,num_face); |
| 334 | |
| 335 | |
| 336 | first_face=num_face-1; |
| 337 | |
| 338 | if ( nlsr->multi_path_face_num == 0 ) |
| 339 | { |
| 340 | last_face=first_face; |
| 341 | } |
| 342 | else |
| 343 | { |
| 344 | if ( num_face <= nlsr->multi_path_face_num) |
| 345 | { |
| 346 | last_face=0; |
| 347 | } |
| 348 | else if ( nlsr->multi_path_face_num == 0) |
| 349 | { |
| 350 | last_face=num_face-nlsr->multi_path_face_num; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | for( j=first_face; j>= last_face; j--) |
| 355 | { |
| 356 | |
| 357 | if ( is_neighbor(orig_router) == 0 ) |
| 358 | { |
| 359 | if ( nlsr->debugging ) |
| 360 | printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]); |
| 361 | if ( nlsr->detailed_logging ) |
| 362 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]); |
| 363 | add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]); |
| 364 | } |
| 365 | else |
| 366 | { |
| 367 | if ( j == last_face && is_neighbor(nle->name)==0) |
| 368 | { |
| 369 | if ( nlsr->debugging ) |
| 370 | printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]); |
| 371 | if ( nlsr->detailed_logging ) |
| 372 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]); |
| 373 | add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | } |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | //hashtb_delete(enle); has to delete later |
| 383 | } |
| 384 | |
| 385 | hashtb_end(enle); |
| 386 | } |
| 387 | |
| 388 | hashtb_end(e); |
| 389 | |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | void |
| 394 | print_npt(void) |
| 395 | { |
| 396 | |
| 397 | if ( nlsr->debugging ) |
| 398 | { |
| 399 | printf("\n"); |
| 400 | printf("print_npt called\n"); |
| 401 | } |
| 402 | if ( nlsr->detailed_logging ) |
| 403 | { |
| 404 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n"); |
| 405 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"print_npt called\n"); |
| 406 | } |
| 407 | int i, npt_element; |
| 408 | |
| 409 | struct npt_entry *ne; |
| 410 | |
| 411 | struct hashtb_enumerator ee; |
| 412 | struct hashtb_enumerator *e = ⅇ |
| 413 | |
| 414 | hashtb_start(nlsr->npt, e); |
| 415 | npt_element=hashtb_n(nlsr->npt); |
| 416 | |
| 417 | for(i=0;i<npt_element;i++) |
| 418 | { |
| 419 | if ( nlsr->debugging ) |
| 420 | { |
| 421 | printf("\n"); |
| 422 | printf("----------NPT ENTRY %d------------------\n",i+1); |
| 423 | } |
| 424 | if ( nlsr->detailed_logging ) |
| 425 | { |
| 426 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n"); |
| 427 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"----------NPT ENTRY %d------------------\n",i+1); |
| 428 | } |
| 429 | ne=e->data; |
| 430 | if ( nlsr->debugging ) |
| 431 | printf(" Origination Router: %s \n",ne->orig_router); |
| 432 | if ( nlsr->detailed_logging ) |
| 433 | writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router: %s \n",ne->orig_router); |
| 434 | //ne->next_hop_face == NO_FACE ? printf(" Next Hop Face: NO_NEXT_HOP \n") : printf(" Next Hop Face: %d \n", ne->next_hop_face); |
| 435 | |
| 436 | int j, nl_element,face_list_element; |
| 437 | struct name_list_entry *nle; |
| 438 | struct hashtb_enumerator eenle; |
| 439 | struct hashtb_enumerator *enle = &eenle; |
| 440 | |
| 441 | hashtb_start(ne->name_list, enle); |
| 442 | nl_element=hashtb_n(ne->name_list); |
| 443 | |
| 444 | for (j=0;j<nl_element;j++) |
| 445 | { |
| 446 | nle=enle->data; |
| 447 | if ( nlsr->debugging ) |
| 448 | printf(" Name Prefix: %s \n",nle->name); |
| 449 | if ( nlsr->detailed_logging ) |
| 450 | writeLogg(__FILE__,__FUNCTION__,__LINE__," Name Prefix: %s \n",nle->name); |
| 451 | hashtb_next(enle); |
| 452 | } |
| 453 | hashtb_end(enle); |
| 454 | |
| 455 | struct face_list_entry *fle; |
| 456 | |
| 457 | struct hashtb_enumerator eef; |
| 458 | struct hashtb_enumerator *ef = &eef; |
| 459 | |
| 460 | hashtb_start(ne->face_list, ef); |
| 461 | face_list_element=hashtb_n(ne->face_list); |
| 462 | if ( face_list_element <= 0 ) |
| 463 | { |
| 464 | if ( nlsr->debugging ) |
| 465 | printf(" Face: No Face \n"); |
| 466 | if ( nlsr->detailed_logging ) |
| 467 | writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: No Face \n"); |
| 468 | |
| 469 | } |
| 470 | else |
| 471 | { |
| 472 | for(j=0;j<face_list_element;j++) |
| 473 | { |
| 474 | fle=ef->data; |
| 475 | if ( nlsr->debugging ) |
| 476 | printf(" Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost); |
| 477 | if ( nlsr->detailed_logging ) |
| 478 | writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost); |
| 479 | hashtb_next(ef); |
| 480 | } |
| 481 | } |
| 482 | hashtb_end(ef); |
| 483 | |
| 484 | |
| 485 | hashtb_next(e); |
| 486 | } |
| 487 | |
| 488 | hashtb_end(e); |
| 489 | |
| 490 | if ( nlsr->debugging ) |
| 491 | printf("\n"); |
| 492 | if ( nlsr->detailed_logging ) |
| 493 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n"); |
| 494 | } |
| 495 | |
| 496 | void |
| 497 | delete_orig_router_from_npt(char *orig_router) |
| 498 | { |
| 499 | int res,num_face,num_prefix; |
| 500 | struct npt_entry *ne; |
| 501 | |
| 502 | struct hashtb_enumerator ee; |
| 503 | struct hashtb_enumerator *e = ⅇ |
| 504 | |
| 505 | hashtb_start(nlsr->npt, e); |
| 506 | res = hashtb_seek(e, orig_router, strlen(orig_router), 0); |
| 507 | |
| 508 | if ( res == HT_OLD_ENTRY ) |
| 509 | { |
| 510 | ne=e->data; |
| 511 | num_prefix=hashtb_n(ne->name_list); |
| 512 | if ( num_prefix > 0 ) |
| 513 | { |
| 514 | num_face=hashtb_n(ne->face_list); |
| 515 | |
| 516 | if ( num_face > 0 ) |
| 517 | { |
| 518 | int j, nl_element; |
| 519 | struct name_list_entry *nle; |
| 520 | struct hashtb_enumerator eenle; |
| 521 | struct hashtb_enumerator *enle = &eenle; |
| 522 | |
| 523 | hashtb_start(ne->name_list, enle); |
| 524 | nl_element=hashtb_n(ne->name_list); |
| 525 | |
| 526 | for (j=0;j<nl_element;j++) |
| 527 | { |
| 528 | nle=enle->data; |
| 529 | delete_npt_entry_by_router_and_name_prefix(ne->orig_router,nle->name); |
| 530 | hashtb_next(enle); |
| 531 | } |
| 532 | hashtb_end(enle); |
| 533 | |
| 534 | } |
| 535 | |
| 536 | } |
| 537 | hashtb_destroy(&ne->name_list); |
| 538 | hashtb_destroy(&ne->face_list); |
| 539 | hashtb_delete(e); |
| 540 | } |
| 541 | else if ( res == HT_NEW_ENTRY ) |
| 542 | { |
| 543 | hashtb_delete(e); |
| 544 | } |
| 545 | hashtb_end(e); |
| 546 | } |
| 547 | |
| 548 | |
| 549 | void |
| 550 | add_face_to_npt_by_face_id(char *dest_router, int face_id, int route_cost) |
| 551 | { |
| 552 | if ( nlsr->debugging ) |
| 553 | { |
| 554 | printf("add_face_to_npt_by_face_id called\n"); |
| 555 | printf("Dest Router: %s Face: %d Route_cost: %d \n",dest_router, face_id, route_cost); |
| 556 | } |
| 557 | if ( nlsr->detailed_logging ) |
| 558 | { |
| 559 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"add_face_to_npt_by_face_id called\n"); |
| 560 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"Dest Router: %s Face: %d Route_cost: %d \n",dest_router, face_id, route_cost); |
| 561 | } |
| 562 | |
akmhoque | e49af7a | 2013-01-22 14:58:37 -0600 | [diff] [blame^] | 563 | print_routing_table(); |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 564 | |
| 565 | int res,res1; |
| 566 | struct npt_entry *ne; |
| 567 | |
| 568 | struct hashtb_enumerator ee; |
| 569 | struct hashtb_enumerator *e = ⅇ |
| 570 | |
| 571 | hashtb_start(nlsr->npt, e); |
| 572 | res = hashtb_seek(e, dest_router, strlen(dest_router), 0); |
| 573 | |
| 574 | if ( res == HT_OLD_ENTRY ) |
| 575 | { |
| 576 | if ( nlsr->debugging ) |
| 577 | printf("Dest Router Found \n"); |
| 578 | if ( nlsr->detailed_logging ) |
| 579 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"Dest Router Found \n"); |
| 580 | |
| 581 | ne=e->data; |
| 582 | |
| 583 | struct hashtb_enumerator eef; |
| 584 | struct hashtb_enumerator *ef = &eef; |
| 585 | |
| 586 | hashtb_start(ne->face_list, ef); |
| 587 | res1=hashtb_seek(ef, &face_id, sizeof(face_id), 0); |
| 588 | |
| 589 | if ( res1 == HT_OLD_ENTRY ) |
| 590 | { |
| 591 | if ( nlsr->debugging ) |
| 592 | printf("Face Found \n"); |
| 593 | if ( nlsr->detailed_logging ) |
| 594 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"Face Found \n"); |
| 595 | struct face_list_entry *fle;//=(struct face_list_entry *)malloc(sizeof(struct face_list_entry)); |
| 596 | fle=ef->data; |
| 597 | fle->next_hop_face=face_id; |
| 598 | fle->route_cost=route_cost; |
| 599 | } |
| 600 | else if ( res1 == HT_NEW_ENTRY ) |
| 601 | { |
| 602 | if ( nlsr->debugging ) |
| 603 | printf("Face Not Found \n"); |
| 604 | if ( nlsr->detailed_logging ) |
| 605 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"Face Not Found \n"); |
| 606 | struct face_list_entry *fle=(struct face_list_entry *)malloc(sizeof(struct face_list_entry)); |
| 607 | fle=ef->data; |
| 608 | fle->next_hop_face=face_id; |
| 609 | fle->route_cost=route_cost; |
| 610 | //hashtb_delete(ef); |
| 611 | } |
| 612 | hashtb_end(ef); |
| 613 | } |
| 614 | else if (res == HT_NEW_ENTRY) |
| 615 | { |
| 616 | hashtb_delete(e); |
| 617 | } |
| 618 | |
| 619 | hashtb_end(e); |
| 620 | } |
| 621 | |
| 622 | |
| 623 | void |
| 624 | add_new_fib_entries_to_npt(void) |
| 625 | { |
| 626 | if ( nlsr->debugging ) |
| 627 | printf("add_new_fib_entries_to_npt called\n"); |
| 628 | if ( nlsr->detailed_logging ) |
| 629 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"add_new_fib_entries_to_npt called\n"); |
| 630 | int i,j, rt_element,face_list_element; |
| 631 | |
| 632 | struct routing_table_entry *rte; |
| 633 | |
| 634 | struct hashtb_enumerator ee; |
| 635 | struct hashtb_enumerator *e = ⅇ |
| 636 | |
| 637 | hashtb_start(nlsr->routing_table, e); |
| 638 | rt_element=hashtb_n(nlsr->routing_table); |
| 639 | |
| 640 | for(i=0;i<rt_element;i++) |
| 641 | { |
| 642 | rte=e->data; |
| 643 | |
| 644 | struct face_list_entry *fle; |
| 645 | |
| 646 | struct hashtb_enumerator eef; |
| 647 | struct hashtb_enumerator *ef = &eef; |
| 648 | |
| 649 | hashtb_start(rte->face_list, ef); |
| 650 | face_list_element=hashtb_n(rte->face_list); |
| 651 | if ( face_list_element <= 0 ) |
| 652 | { |
| 653 | if ( nlsr->debugging ) |
akmhoque | e19e3ee | 2013-01-22 14:38:45 -0600 | [diff] [blame] | 654 | printf(" Face: No Face \n"); |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 655 | if ( nlsr->detailed_logging ) |
akmhoque | e19e3ee | 2013-01-22 14:38:45 -0600 | [diff] [blame] | 656 | writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: No Face \n"); |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 657 | } |
| 658 | else |
| 659 | { |
| 660 | for(j=0;j<face_list_element;j++) |
| 661 | { |
| 662 | fle=ef->data; |
akmhoque | e19e3ee | 2013-01-22 14:38:45 -0600 | [diff] [blame] | 663 | if (fle->next_hop_face > 0 ) |
| 664 | add_face_to_npt_by_face_id(rte->dest_router,fle->next_hop_face,fle->route_cost); |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 665 | hashtb_next(ef); |
| 666 | } |
| 667 | } |
| 668 | hashtb_end(ef); |
| 669 | |
| 670 | hashtb_next(e); |
| 671 | } |
| 672 | |
| 673 | hashtb_end(e); |
| 674 | |
| 675 | } |
| 676 | |
| 677 | |
| 678 | void |
| 679 | delete_face_from_npt_by_face_id(char *dest_router, int face_id) |
| 680 | { |
| 681 | if ( nlsr->debugging ) |
| 682 | printf("delete_face_from_npt_by_face_id\n"); |
| 683 | if ( nlsr->detailed_logging ) |
| 684 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_face_from_npt_by_face_id\n"); |
| 685 | |
| 686 | int res,res1; |
| 687 | struct npt_entry *ne; |
| 688 | |
| 689 | struct hashtb_enumerator ee; |
| 690 | struct hashtb_enumerator *e = ⅇ |
| 691 | |
| 692 | hashtb_start(nlsr->npt, e); |
| 693 | res = hashtb_seek(e, dest_router, strlen(dest_router), 0); |
| 694 | |
| 695 | if ( res == HT_OLD_ENTRY ) |
| 696 | { |
| 697 | ne=e->data; |
| 698 | |
| 699 | struct hashtb_enumerator eef; |
| 700 | struct hashtb_enumerator *ef = &eef; |
| 701 | |
| 702 | hashtb_start(ne->face_list, ef); |
| 703 | res1=hashtb_seek(ef, &face_id, sizeof(face_id), 0); |
| 704 | |
| 705 | if ( res1 == HT_OLD_ENTRY ) |
| 706 | { |
| 707 | hashtb_delete(ef); |
| 708 | } |
| 709 | else if ( res1 == HT_NEW_ENTRY ) |
| 710 | { |
| 711 | hashtb_delete(ef); |
| 712 | } |
| 713 | hashtb_end(ef); |
| 714 | } |
| 715 | else if (res == HT_NEW_ENTRY) |
| 716 | { |
| 717 | hashtb_delete(e); |
| 718 | } |
| 719 | |
| 720 | hashtb_end(e); |
| 721 | } |
| 722 | |
| 723 | int |
| 724 | delete_old_face_from_npt(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags) |
| 725 | { |
| 726 | if(flags == CCN_SCHEDULE_CANCEL) |
| 727 | { |
| 728 | return -1; |
| 729 | } |
| 730 | |
| 731 | nlsr_lock(); |
| 732 | |
| 733 | if ( nlsr->debugging ) |
| 734 | printf("delete_old_face_from_npt\n"); |
| 735 | if ( nlsr->detailed_logging ) |
| 736 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_old_face_from_npt\n"); |
| 737 | |
| 738 | if ( ev->evdata != NULL ) |
| 739 | { |
| 740 | if ( nlsr->debugging ) |
| 741 | printf("Event Data: %s \n",(char *)ev->evdata); |
| 742 | if ( nlsr->detailed_logging ) |
| 743 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"Event Data: %s \n",(char *)ev->evdata); |
| 744 | char *sep="|"; |
| 745 | char *rem; |
| 746 | char *orig_router; |
| 747 | char *faceid; |
| 748 | int face_id; |
| 749 | |
| 750 | char *face_data=(char *)malloc(strlen((char *)ev->evdata)+1); |
| 751 | memset(face_data,0,strlen((char *)ev->evdata)+1); |
| 752 | memcpy(face_data+strlen(face_data),(char *)ev->evdata,strlen((char *)ev->evdata)); |
| 753 | |
| 754 | orig_router=strtok_r(face_data,sep,&rem); |
| 755 | faceid=strtok_r(NULL,sep,&rem); |
| 756 | face_id=atoi(faceid); |
| 757 | |
| 758 | if ( nlsr->debugging ) |
| 759 | printf("Orig Router: %s Face: %d \n",orig_router,face_id); |
| 760 | if ( nlsr->detailed_logging ) |
| 761 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"Orig Router: %s Face: %d \n",orig_router,face_id); |
| 762 | |
| 763 | delete_face_from_npt_by_face_id(orig_router,face_id); |
| 764 | } |
| 765 | |
| 766 | nlsr_unlock(); |
| 767 | |
| 768 | return 0; |
| 769 | } |
| 770 | |
| 771 | void |
| 772 | clean_old_fib_entries_from_npt(void) |
| 773 | { |
| 774 | |
| 775 | |
| 776 | if ( nlsr->debugging ) |
| 777 | printf("clean_old_fib_entries_from_npt called\n\n"); |
| 778 | if ( nlsr->detailed_logging ) |
| 779 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"clean_old_fib_entries_from_npt called\n\n"); |
| 780 | int i, npt_element; |
| 781 | |
| 782 | struct npt_entry *ne; |
| 783 | |
| 784 | struct hashtb_enumerator ee; |
| 785 | struct hashtb_enumerator *e = ⅇ |
| 786 | |
| 787 | hashtb_start(nlsr->npt, e); |
| 788 | npt_element=hashtb_n(nlsr->npt); |
| 789 | |
| 790 | for(i=0;i<npt_element;i++) |
| 791 | { |
| 792 | ne=e->data; |
| 793 | |
| 794 | int j,k, nl_element,face_list_element; |
| 795 | struct face_list_entry *fle; |
| 796 | |
| 797 | struct hashtb_enumerator eef; |
| 798 | struct hashtb_enumerator *ef = &eef; |
| 799 | |
| 800 | hashtb_start(ne->face_list, ef); |
| 801 | face_list_element=hashtb_n(ne->face_list); |
| 802 | if ( face_list_element <= 0 ) |
| 803 | { |
| 804 | if ( nlsr->debugging ) |
akmhoque | e49af7a | 2013-01-22 14:58:37 -0600 | [diff] [blame^] | 805 | printf(" Face: No Face \n"); |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 806 | if ( nlsr->detailed_logging ) |
| 807 | writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: No Face \n"); |
| 808 | |
| 809 | } |
| 810 | else |
| 811 | { |
| 812 | for(j=0;j<face_list_element;j++) |
| 813 | { |
| 814 | fle=ef->data; |
| 815 | int check=does_face_exist_for_router(ne->orig_router,fle->next_hop_face); |
| 816 | if ( check == 0 ) |
| 817 | { |
| 818 | struct name_list_entry *nle; |
| 819 | struct hashtb_enumerator eenle; |
| 820 | struct hashtb_enumerator *enle = &eenle; |
| 821 | |
| 822 | hashtb_start(ne->name_list, enle); |
| 823 | nl_element=hashtb_n(ne->name_list); |
| 824 | |
| 825 | for (k=0;k<nl_element;k++) |
| 826 | { |
| 827 | nle=enle->data; |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 828 | if( is_neighbor(nle->name) == 0 ) |
| 829 | { |
| 830 | if ( nlsr->debugging ) |
| 831 | printf("Deleting face: Name:%s Face: %d\n",nle->name,fle->next_hop_face); |
| 832 | if ( nlsr->detailed_logging ) |
| 833 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,fle->next_hop_face); |
| 834 | add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, fle->next_hop_face); |
| 835 | } |
| 836 | |
| 837 | |
| 838 | hashtb_next(enle); |
| 839 | } |
| 840 | hashtb_end(enle); |
| 841 | |
| 842 | char faceid[20]; |
| 843 | memset(faceid,0,20); |
| 844 | sprintf(faceid,"%d",fle->next_hop_face); |
| 845 | char *evdata=(char *)malloc(strlen(ne->orig_router)+strlen(faceid)+2); |
| 846 | memset(evdata,0,strlen(ne->orig_router)+strlen(faceid)+2); |
| 847 | memcpy(evdata+strlen(evdata),ne->orig_router,strlen(ne->orig_router)); |
| 848 | memcpy(evdata+strlen(evdata),"|",1); |
| 849 | memcpy(evdata+strlen(evdata),faceid,strlen(faceid)); |
| 850 | |
akmhoque | e19e3ee | 2013-01-22 14:38:45 -0600 | [diff] [blame] | 851 | nlsr->event = ccn_schedule_event(nlsr->sched, 1, &delete_old_face_from_npt, (void *)evdata, 0); |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 852 | |
| 853 | } |
| 854 | |
| 855 | hashtb_next(ef); |
| 856 | } |
| 857 | } |
| 858 | hashtb_end(ef); |
| 859 | |
| 860 | |
| 861 | hashtb_next(e); |
| 862 | } |
| 863 | |
| 864 | hashtb_end(e); |
| 865 | |
| 866 | } |
| 867 | |
| 868 | void |
| 869 | update_npt_with_new_route(void) |
| 870 | { |
| 871 | clean_old_fib_entries_from_npt(); |
| 872 | add_new_fib_entries_to_npt(); |
| 873 | |
| 874 | int i, npt_element; |
| 875 | |
| 876 | struct npt_entry *ne; |
| 877 | |
| 878 | struct hashtb_enumerator ee; |
| 879 | struct hashtb_enumerator *e = ⅇ |
| 880 | |
| 881 | hashtb_start(nlsr->npt, e); |
| 882 | npt_element=hashtb_n(nlsr->npt); |
| 883 | |
| 884 | for(i=0;i<npt_element;i++) |
| 885 | { |
| 886 | |
| 887 | ne=e->data; |
| 888 | update_ccnd_fib_for_orig_router(ne->orig_router); |
| 889 | hashtb_next(e); |
| 890 | } |
| 891 | |
| 892 | hashtb_end(e); |
| 893 | } |
| 894 | |
| 895 | |
| 896 | |
| 897 | void |
| 898 | sort_faces_by_distance(int *faces,int *route_costs,int start,int element) |
| 899 | { |
| 900 | int i,j; |
| 901 | int temp_cost; |
| 902 | int temp_face; |
| 903 | |
| 904 | for ( i=start ; i < element ; i ++) |
| 905 | { |
| 906 | for( j=i+1; j<element; j ++) |
| 907 | { |
| 908 | if (route_costs[j] < route_costs[i] ) |
| 909 | { |
| 910 | temp_cost=route_costs[j]; |
| 911 | route_costs[j]=route_costs[i]; |
| 912 | route_costs[i]=temp_cost; |
| 913 | |
| 914 | temp_face=faces[j]; |
| 915 | faces[j]=faces[i]; |
| 916 | faces[i]=temp_face; |
| 917 | } |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | } |
| 922 | |
| 923 | void |
| 924 | get_all_faces_for_orig_router_from_npt(char *orig_router, int *faces, int *route_costs, int num_faces) |
| 925 | { |
| 926 | |
| 927 | int res,face_list_element,j; |
| 928 | struct hashtb_enumerator ee; |
| 929 | struct hashtb_enumerator *e = ⅇ |
| 930 | |
| 931 | |
| 932 | hashtb_start(nlsr->npt, e); |
| 933 | res = hashtb_seek(e, orig_router, strlen(orig_router), 0); |
| 934 | |
| 935 | if(res == HT_NEW_ENTRY) |
| 936 | { |
| 937 | hashtb_delete(e); |
| 938 | } |
| 939 | else if ( res == HT_OLD_ENTRY ) |
| 940 | { |
| 941 | struct npt_entry *ne; |
| 942 | ne=e->data; |
| 943 | |
| 944 | struct face_list_entry *fle; |
| 945 | |
| 946 | struct hashtb_enumerator eef; |
| 947 | struct hashtb_enumerator *ef = &eef; |
| 948 | |
| 949 | hashtb_start(ne->face_list, ef); |
| 950 | face_list_element=hashtb_n(ne->face_list); |
| 951 | for(j=0;j<face_list_element;j++) |
| 952 | { |
| 953 | fle=ef->data; |
| 954 | faces[j]=fle->next_hop_face; |
| 955 | route_costs[j]=fle->route_cost; |
| 956 | hashtb_next(ef); |
| 957 | } |
| 958 | hashtb_end(ef); |
| 959 | |
| 960 | |
| 961 | } |
| 962 | hashtb_end(e); |
| 963 | |
| 964 | } |
| 965 | |
| 966 | void |
| 967 | destroy_faces_by_orig_router(char *orig_router) |
| 968 | { |
| 969 | |
| 970 | int res; |
| 971 | struct hashtb_enumerator ee; |
| 972 | struct hashtb_enumerator *e = ⅇ |
| 973 | |
| 974 | |
| 975 | hashtb_start(nlsr->npt, e); |
| 976 | res = hashtb_seek(e, orig_router, strlen(orig_router), 0); |
| 977 | |
| 978 | if(res == HT_NEW_ENTRY) |
| 979 | { |
| 980 | hashtb_delete(e); |
| 981 | } |
| 982 | else if ( res == HT_OLD_ENTRY ) |
| 983 | { |
| 984 | struct npt_entry *ne; |
| 985 | ne=e->data; |
| 986 | int num_face=hashtb_n(ne->face_list); |
| 987 | int last_face,first_face; |
| 988 | |
| 989 | int *faces=(int *)malloc(num_face*sizeof(int)); |
| 990 | int *route_costs=(int *)malloc(num_face*sizeof(int)); |
| 991 | |
| 992 | get_all_faces_for_orig_router_from_npt(orig_router,faces,route_costs,num_face); |
| 993 | sort_faces_by_distance(faces,route_costs,0,num_face); |
| 994 | |
| 995 | |
| 996 | first_face=num_face-1; |
| 997 | |
| 998 | if ( nlsr->multi_path_face_num == 0 ) |
| 999 | { |
| 1000 | last_face=first_face; |
| 1001 | } |
| 1002 | else |
| 1003 | { |
| 1004 | if ( num_face <= nlsr->multi_path_face_num) |
| 1005 | { |
| 1006 | last_face=0; |
| 1007 | } |
| 1008 | else if ( nlsr->multi_path_face_num == 0) |
| 1009 | { |
| 1010 | last_face=num_face-nlsr->multi_path_face_num; |
| 1011 | } |
| 1012 | } |
| 1013 | |
| 1014 | int i,j, nl_element; |
| 1015 | struct name_list_entry *nle; |
| 1016 | struct hashtb_enumerator eenle; |
| 1017 | struct hashtb_enumerator *enle = &eenle; |
| 1018 | |
| 1019 | hashtb_start(ne->name_list, enle); |
| 1020 | nl_element=hashtb_n(ne->name_list); |
| 1021 | |
| 1022 | for (i=0;i<nl_element;i++) |
| 1023 | { |
| 1024 | nle=enle->data; |
| 1025 | |
| 1026 | for( j=first_face; j>= last_face; j--) |
| 1027 | { |
| 1028 | if ( is_neighbor(orig_router) == 0 ) |
| 1029 | { |
| 1030 | if ( nlsr->debugging ) |
| 1031 | printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]); |
| 1032 | if ( nlsr->detailed_logging ) |
| 1033 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]); |
| 1034 | add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]); |
| 1035 | } |
| 1036 | else |
| 1037 | { |
| 1038 | if ( j == last_face && is_neighbor(nle->name)==0) |
| 1039 | { |
| 1040 | if ( nlsr->debugging ) |
| 1041 | printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]); |
| 1042 | if ( nlsr->detailed_logging ) |
| 1043 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]); |
| 1044 | add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j]); |
| 1045 | } |
| 1046 | } |
| 1047 | } |
| 1048 | |
| 1049 | hashtb_next(enle); |
| 1050 | } |
| 1051 | hashtb_end(enle); |
| 1052 | |
| 1053 | |
| 1054 | |
| 1055 | free(faces); |
| 1056 | free(route_costs); |
| 1057 | |
| 1058 | } |
| 1059 | hashtb_end(e); |
| 1060 | |
| 1061 | } |
| 1062 | |
| 1063 | void |
| 1064 | destroy_all_face_by_nlsr(void) |
| 1065 | { |
| 1066 | int i, npt_element; |
| 1067 | |
| 1068 | struct npt_entry *ne; |
| 1069 | |
| 1070 | struct hashtb_enumerator ee; |
| 1071 | struct hashtb_enumerator *e = ⅇ |
| 1072 | |
| 1073 | hashtb_start(nlsr->npt, e); |
| 1074 | npt_element=hashtb_n(nlsr->npt); |
| 1075 | |
| 1076 | for(i=0;i<npt_element;i++) |
| 1077 | { |
| 1078 | ne=e->data; |
| 1079 | destroy_faces_by_orig_router(ne->orig_router); |
| 1080 | hashtb_next(e); |
| 1081 | } |
| 1082 | |
| 1083 | hashtb_end(e); |
| 1084 | |
| 1085 | if ( nlsr->debugging ) |
| 1086 | printf("\n"); |
| 1087 | if ( nlsr->detailed_logging ) |
| 1088 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n"); |
| 1089 | } |