akmhoque | 29c1db5 | 2012-09-07 14:47:43 -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 | |
| 13 | |
| 14 | #include <ccn/ccn.h> |
| 15 | #include <ccn/uri.h> |
| 16 | #include <ccn/keystore.h> |
| 17 | #include <ccn/signing.h> |
| 18 | #include <ccn/schedule.h> |
| 19 | #include <ccn/hashtb.h> |
| 20 | |
| 21 | |
| 22 | #include "nlsr.h" |
| 23 | #include "nlsr_route.h" |
| 24 | #include "nlsr_lsdb.h" |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 25 | #include "nlsr_npt.h" |
akmhoque | fbfd098 | 2012-09-09 20:59:03 -0500 | [diff] [blame] | 26 | #include "nlsr_adl.h" |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 27 | |
| 28 | int |
| 29 | route_calculate(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags) |
| 30 | { |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 31 | |
| 32 | if(flags == CCN_SCHEDULE_CANCEL) |
| 33 | { |
| 34 | return -1; |
| 35 | } |
| 36 | |
| 37 | nlsr_lock(); |
| 38 | |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 39 | printf("route_calculate called\n"); |
| 40 | |
| 41 | if( ! nlsr->is_build_adj_lsa_sheduled ) |
| 42 | { |
| 43 | /* Calculate Route here */ |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 44 | print_routing_table(); |
| 45 | print_npt(); |
| 46 | |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 47 | struct hashtb_param param_me = {0}; |
| 48 | nlsr->map = hashtb_create(sizeof(struct map_entry), ¶m_me); |
akmhoque | fbfd098 | 2012-09-09 20:59:03 -0500 | [diff] [blame] | 49 | nlsr->rev_map = hashtb_create(sizeof(struct map_entry), ¶m_me); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 50 | make_map(); |
| 51 | assign_mapping_number(); |
| 52 | print_map(); |
akmhoque | fbfd098 | 2012-09-09 20:59:03 -0500 | [diff] [blame] | 53 | print_rev_map(); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 54 | |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 55 | do_old_routing_table_updates(); |
| 56 | |
| 57 | |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 58 | int i; |
| 59 | int **adj_matrix; |
| 60 | int map_element=hashtb_n(nlsr->map); |
| 61 | adj_matrix=malloc(map_element * sizeof(int *)); |
| 62 | for(i = 0; i < map_element; i++) |
| 63 | { |
| 64 | adj_matrix[i] = malloc(map_element * sizeof(int)); |
| 65 | } |
| 66 | make_adj_matrix(adj_matrix,map_element); |
| 67 | print_adj_matrix(adj_matrix,map_element); |
| 68 | |
| 69 | long int source=get_mapping_no(nlsr->router_name); |
| 70 | long int *parent=(long int *)malloc(map_element * sizeof(long int)); |
| 71 | |
| 72 | calculate_path(adj_matrix,parent, map_element, source); |
| 73 | |
| 74 | print_all_path_from_source(parent,source); |
| 75 | |
akmhoque | fbfd098 | 2012-09-09 20:59:03 -0500 | [diff] [blame] | 76 | print_all_next_hop(parent,source); |
| 77 | |
| 78 | update_routing_table_with_new_route(parent,source); |
| 79 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 80 | print_routing_table(); |
| 81 | print_npt(); |
akmhoque | fbfd098 | 2012-09-09 20:59:03 -0500 | [diff] [blame] | 82 | |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 83 | for(i = 0; i < map_element; i++) |
| 84 | { |
| 85 | free(adj_matrix[i]); |
| 86 | } |
| 87 | free(parent); |
| 88 | free(adj_matrix); |
| 89 | hashtb_destroy(&nlsr->map); |
akmhoque | fbfd098 | 2012-09-09 20:59:03 -0500 | [diff] [blame] | 90 | hashtb_destroy(&nlsr->rev_map); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 91 | |
| 92 | } |
| 93 | nlsr->is_route_calculation_scheduled=0; |
| 94 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 95 | nlsr_unlock(); |
| 96 | |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | void |
| 101 | calculate_path(int **adj_matrix, long int *parent, long int V, long int S) |
| 102 | { |
| 103 | int i; |
| 104 | long int v,u; |
| 105 | long int *dist=(long int *)malloc(V * sizeof(long int)); |
| 106 | long int *Q=(long int *)malloc(V * sizeof(long int)); |
| 107 | long int head=0; |
| 108 | /* Initial the Parent */ |
| 109 | for (i = 0 ; i < V; i++) |
| 110 | { |
| 111 | parent[i]=EMPTY_PARENT; |
| 112 | dist[i]=INF_DISTANCE; |
| 113 | Q[i]=i; |
| 114 | } |
| 115 | |
| 116 | dist[S]=0; |
| 117 | sort_queue_by_distance(Q,dist,head,V); |
| 118 | |
| 119 | while (head < V ) |
| 120 | { |
| 121 | u=Q[head]; |
| 122 | if(dist[u] == INF_DISTANCE) |
| 123 | { |
| 124 | break; |
| 125 | } |
| 126 | |
| 127 | for(v=0 ; v <V; v++) |
| 128 | { |
| 129 | if( adj_matrix[u][v] > 0 ) // |
| 130 | { |
| 131 | if ( is_not_explored(Q,v,head+1,V) ) |
| 132 | { |
| 133 | |
| 134 | if( dist[u] + adj_matrix[u][v] < dist[v]) |
| 135 | { |
| 136 | dist[v]=dist[u] + adj_matrix[u][v] ; |
| 137 | parent[v]=u; |
| 138 | } |
| 139 | |
| 140 | } |
| 141 | |
| 142 | } |
| 143 | |
| 144 | } |
| 145 | |
| 146 | head++; |
| 147 | sort_queue_by_distance(Q,dist,head,V); |
| 148 | } |
| 149 | |
| 150 | free(Q); |
| 151 | free(dist); |
| 152 | } |
| 153 | |
| 154 | void |
| 155 | print_all_path_from_source(long int *parent,long int source) |
| 156 | { |
| 157 | int i, map_element; |
| 158 | struct map_entry *me; |
| 159 | |
| 160 | struct hashtb_enumerator ee; |
| 161 | struct hashtb_enumerator *e = ⅇ |
| 162 | |
| 163 | hashtb_start(nlsr->map, e); |
| 164 | map_element=hashtb_n(nlsr->map); |
| 165 | |
| 166 | for(i=0;i<map_element;i++) |
| 167 | { |
| 168 | me=e->data; |
| 169 | if(me->mapping != source) |
| 170 | { |
| 171 | print_path(parent,(long int)me->mapping); |
| 172 | printf("\n"); |
| 173 | } |
| 174 | hashtb_next(e); |
| 175 | } |
| 176 | |
| 177 | hashtb_end(e); |
| 178 | |
| 179 | } |
| 180 | |
akmhoque | fbfd098 | 2012-09-09 20:59:03 -0500 | [diff] [blame] | 181 | void |
| 182 | print_all_next_hop(long int *parent,long int source) |
| 183 | { |
| 184 | int i, map_element; |
| 185 | struct map_entry *me; |
| 186 | |
| 187 | struct hashtb_enumerator ee; |
| 188 | struct hashtb_enumerator *e = ⅇ |
| 189 | |
| 190 | hashtb_start(nlsr->map, e); |
| 191 | map_element=hashtb_n(nlsr->map); |
| 192 | |
| 193 | for(i=0;i<map_element;i++) |
| 194 | { |
| 195 | me=e->data; |
| 196 | if(me->mapping != source) |
| 197 | { |
| 198 | |
| 199 | printf("Dest: %d Next Hop: %ld\n",me->mapping,get_next_hop_from_calculation(parent,me->mapping,source)); |
| 200 | } |
| 201 | hashtb_next(e); |
| 202 | } |
| 203 | |
| 204 | hashtb_end(e); |
| 205 | |
| 206 | } |
| 207 | |
| 208 | long int |
| 209 | get_next_hop_from_calculation(long int *parent, long int dest,long int source) |
| 210 | { |
| 211 | long int next_hop; |
| 212 | while ( parent[dest] != EMPTY_PARENT ) |
| 213 | { |
| 214 | next_hop=dest; |
| 215 | dest=parent[dest]; |
| 216 | |
| 217 | } |
| 218 | |
| 219 | if ( dest != source ) |
| 220 | { |
| 221 | next_hop=NO_NEXT_HOP; |
| 222 | } |
| 223 | |
| 224 | return next_hop; |
| 225 | |
| 226 | } |
| 227 | |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 228 | void |
| 229 | print_path(long int *parent, long int dest) |
| 230 | { |
| 231 | if (parent[dest] != EMPTY_PARENT ) |
| 232 | print_path(parent,parent[dest]); |
| 233 | |
| 234 | printf(" %ld",dest); |
| 235 | } |
| 236 | |
| 237 | int |
| 238 | is_not_explored(long int *Q, long int u,long int start, long int element) |
| 239 | { |
| 240 | int ret=0; |
| 241 | long int i; |
| 242 | for(i=start; i< element; i++) |
| 243 | { |
| 244 | if ( Q[i] == u ) |
| 245 | { |
| 246 | ret=1; |
| 247 | break; |
| 248 | } |
| 249 | } |
| 250 | return ret; |
| 251 | } |
| 252 | |
| 253 | void |
| 254 | sort_queue_by_distance(long int *Q,long int *dist,long int start,long int element) |
| 255 | { |
| 256 | long int i,j; |
| 257 | long int temp_u; |
| 258 | |
| 259 | for ( i=start ; i < element ; i ++) |
| 260 | { |
| 261 | for( j=i+1; j<element; j ++) |
| 262 | { |
| 263 | if (dist[Q[j]] < dist[Q[i]]) |
| 264 | { |
| 265 | temp_u=Q[j]; |
| 266 | Q[j]=Q[i]; |
| 267 | Q[i]=temp_u; |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | } |
| 273 | |
| 274 | void |
| 275 | print_map(void) |
| 276 | { |
| 277 | int i, map_element; |
| 278 | struct map_entry *me; |
| 279 | |
| 280 | struct hashtb_enumerator ee; |
| 281 | struct hashtb_enumerator *e = ⅇ |
| 282 | |
| 283 | hashtb_start(nlsr->map, e); |
| 284 | map_element=hashtb_n(nlsr->map); |
| 285 | |
| 286 | for(i=0;i<map_element;i++) |
| 287 | { |
| 288 | me=e->data; |
| 289 | printf("Router: %s Mapping Number: %d \n",me->router,me->mapping); |
| 290 | hashtb_next(e); |
| 291 | } |
| 292 | |
| 293 | hashtb_end(e); |
| 294 | } |
| 295 | |
| 296 | |
| 297 | void |
| 298 | assign_mapping_number(void) |
| 299 | { |
| 300 | int i, map_element; |
| 301 | struct map_entry *me; |
| 302 | |
| 303 | struct hashtb_enumerator ee; |
| 304 | struct hashtb_enumerator *e = ⅇ |
| 305 | |
| 306 | hashtb_start(nlsr->map, e); |
| 307 | map_element=hashtb_n(nlsr->map); |
| 308 | |
| 309 | for(i=0;i<map_element;i++) |
| 310 | { |
| 311 | me=e->data; |
| 312 | me->mapping=i; |
akmhoque | fbfd098 | 2012-09-09 20:59:03 -0500 | [diff] [blame] | 313 | add_rev_map_entry(i,me->router); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 314 | hashtb_next(e); |
| 315 | } |
| 316 | |
| 317 | hashtb_end(e); |
| 318 | } |
| 319 | |
| 320 | void |
| 321 | make_map(void) |
| 322 | { |
| 323 | |
| 324 | |
| 325 | int i, adj_lsdb_element; |
| 326 | struct alsa *adj_lsa; |
| 327 | |
| 328 | struct hashtb_enumerator ee; |
| 329 | struct hashtb_enumerator *e = ⅇ |
| 330 | |
| 331 | hashtb_start(nlsr->lsdb->adj_lsdb, e); |
| 332 | adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb); |
| 333 | |
| 334 | for(i=0;i<adj_lsdb_element;i++) |
| 335 | { |
| 336 | adj_lsa=e->data; |
| 337 | add_adj_data_to_map(adj_lsa->header->orig_router->name,adj_lsa->body,adj_lsa->no_link); |
| 338 | hashtb_next(e); |
| 339 | } |
| 340 | |
| 341 | hashtb_end(e); |
| 342 | |
| 343 | } |
| 344 | |
| 345 | void |
| 346 | add_map_entry(char *router) |
| 347 | { |
| 348 | |
| 349 | struct map_entry *me=(struct map_entry*)malloc(sizeof(struct map_entry )); |
| 350 | |
| 351 | struct hashtb_enumerator ee; |
| 352 | struct hashtb_enumerator *e = ⅇ |
| 353 | int res; |
| 354 | |
| 355 | hashtb_start(nlsr->map, e); |
| 356 | res = hashtb_seek(e, router, strlen(router), 0); |
| 357 | |
| 358 | if(res == HT_NEW_ENTRY) |
| 359 | { |
| 360 | me=e->data; |
| 361 | me->router=(char *)malloc(strlen(router)+1); |
| 362 | memset(me->router,0,strlen(router)+1); |
| 363 | memcpy(me->router,router,strlen(router)); |
| 364 | me->mapping=0; |
| 365 | } |
| 366 | |
| 367 | hashtb_end(e); |
| 368 | |
| 369 | } |
| 370 | |
| 371 | |
| 372 | void |
| 373 | add_adj_data_to_map(char *orig_router, char *body, int no_link) |
| 374 | { |
| 375 | add_map_entry(orig_router); |
| 376 | |
| 377 | int i=0; |
| 378 | char *lsa_data=(char *)malloc(strlen(body)+1); |
| 379 | memset( lsa_data,0,strlen(body)+1); |
| 380 | memcpy(lsa_data,body,strlen(body)+1); |
| 381 | char *sep="|"; |
| 382 | char *rem; |
| 383 | char *rtr_id; |
| 384 | char *length; |
| 385 | char *face; |
| 386 | char *metric; |
| 387 | |
| 388 | if(no_link >0 ) |
| 389 | { |
| 390 | rtr_id=strtok_r(lsa_data,sep,&rem); |
| 391 | length=strtok_r(NULL,sep,&rem); |
| 392 | face=strtok_r(NULL,sep,&rem); |
| 393 | metric=strtok_r(NULL,sep,&rem); |
| 394 | |
| 395 | add_map_entry(rtr_id); |
| 396 | |
| 397 | for(i=1;i<no_link;i++) |
| 398 | { |
| 399 | rtr_id=strtok_r(NULL,sep,&rem); |
| 400 | length=strtok_r(NULL,sep,&rem); |
| 401 | face=strtok_r(NULL,sep,&rem); |
| 402 | metric=strtok_r(NULL,sep,&rem); |
| 403 | |
| 404 | add_map_entry(rtr_id); |
| 405 | |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | free(lsa_data); |
| 410 | } |
| 411 | |
| 412 | int |
| 413 | get_mapping_no(char *router) |
| 414 | { |
| 415 | struct map_entry *me; |
| 416 | |
| 417 | struct hashtb_enumerator ee; |
| 418 | struct hashtb_enumerator *e = ⅇ |
akmhoque | 0315f98 | 2012-09-09 11:28:05 -0500 | [diff] [blame] | 419 | int res; |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 420 | int ret; |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 421 | |
| 422 | hashtb_start(nlsr->map, e); |
| 423 | res = hashtb_seek(e, router, strlen(router), 0); |
| 424 | |
| 425 | if( res == HT_OLD_ENTRY ) |
| 426 | { |
| 427 | me=e->data; |
| 428 | ret=me->mapping; |
| 429 | } |
| 430 | else if(res == HT_NEW_ENTRY) |
| 431 | { |
| 432 | hashtb_delete(e); |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 433 | ret=NO_MAPPING_NUM; |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | hashtb_end(e); |
| 437 | |
| 438 | return ret; |
| 439 | |
| 440 | } |
| 441 | |
akmhoque | fbfd098 | 2012-09-09 20:59:03 -0500 | [diff] [blame] | 442 | |
| 443 | void |
| 444 | add_rev_map_entry(long int mapping_number, char *router) |
| 445 | { |
| 446 | |
| 447 | struct map_entry *me=(struct map_entry*)malloc(sizeof(struct map_entry )); |
| 448 | |
| 449 | struct hashtb_enumerator ee; |
| 450 | struct hashtb_enumerator *e = ⅇ |
| 451 | int res; |
| 452 | |
| 453 | hashtb_start(nlsr->rev_map, e); |
| 454 | res = hashtb_seek(e, &mapping_number, sizeof(mapping_number), 0); |
| 455 | |
| 456 | if(res == HT_NEW_ENTRY) |
| 457 | { |
| 458 | me=e->data; |
| 459 | me->router=(char *)malloc(strlen(router)+1); |
| 460 | memset(me->router,0,strlen(router)+1); |
| 461 | memcpy(me->router,router,strlen(router)); |
| 462 | me->mapping=mapping_number; |
| 463 | } |
| 464 | |
| 465 | hashtb_end(e); |
| 466 | |
| 467 | } |
| 468 | |
| 469 | |
| 470 | |
| 471 | char * |
| 472 | get_router_from_rev_map(long int mapping_number) |
| 473 | { |
| 474 | |
| 475 | struct map_entry *me; |
| 476 | |
| 477 | struct hashtb_enumerator ee; |
| 478 | struct hashtb_enumerator *e = ⅇ |
| 479 | int res; |
| 480 | |
| 481 | hashtb_start(nlsr->rev_map, e); |
| 482 | res = hashtb_seek(e, &mapping_number, sizeof(mapping_number), 0); |
| 483 | |
| 484 | if(res == HT_OLD_ENTRY) |
| 485 | { |
| 486 | me=e->data; |
| 487 | hashtb_end(e); |
| 488 | return me->router; |
| 489 | } |
| 490 | else if(res == HT_NEW_ENTRY) |
| 491 | { |
| 492 | hashtb_delete(e); |
| 493 | hashtb_end(e); |
| 494 | } |
| 495 | |
| 496 | return NULL; |
| 497 | } |
| 498 | |
| 499 | void |
| 500 | print_rev_map(void) |
| 501 | { |
| 502 | int i, map_element; |
| 503 | struct map_entry *me; |
| 504 | |
| 505 | struct hashtb_enumerator ee; |
| 506 | struct hashtb_enumerator *e = ⅇ |
| 507 | |
| 508 | hashtb_start(nlsr->map, e); |
| 509 | map_element=hashtb_n(nlsr->map); |
| 510 | |
| 511 | for(i=0;i<map_element;i++) |
| 512 | { |
| 513 | me=e->data; |
| 514 | printf("Mapping Number: %d Router: %s \n",me->mapping,me->router); |
| 515 | hashtb_next(e); |
| 516 | } |
| 517 | |
| 518 | hashtb_end(e); |
| 519 | } |
| 520 | |
| 521 | |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 522 | void |
| 523 | assign_adj_matrix_for_lsa(struct alsa *adj_lsa, int **adj_matrix) |
| 524 | { |
| 525 | int mapping_orig_router=get_mapping_no(adj_lsa->header->orig_router->name); |
| 526 | int mapping_nbr_router; |
| 527 | |
| 528 | int i; |
| 529 | char *lsa_data=(char *)malloc(strlen(adj_lsa->body)+1); |
| 530 | memset( lsa_data,0,strlen(adj_lsa->body)+1); |
| 531 | memcpy(lsa_data,adj_lsa->body,strlen(adj_lsa->body)+1); |
| 532 | char *sep="|"; |
| 533 | char *rem; |
| 534 | char *rtr_id; |
| 535 | char *length; |
| 536 | char *face; |
| 537 | char *metric; |
| 538 | |
| 539 | if(adj_lsa->no_link >0 ) |
| 540 | { |
| 541 | rtr_id=strtok_r(lsa_data,sep,&rem); |
| 542 | length=strtok_r(NULL,sep,&rem); |
| 543 | face=strtok_r(NULL,sep,&rem); |
| 544 | metric=strtok_r(NULL,sep,&rem); |
| 545 | |
| 546 | mapping_nbr_router=get_mapping_no(rtr_id); |
| 547 | adj_matrix[mapping_orig_router][mapping_nbr_router]=atoi(metric); |
| 548 | |
| 549 | for(i=1;i<adj_lsa->no_link;i++) |
| 550 | { |
| 551 | rtr_id=strtok_r(NULL,sep,&rem); |
| 552 | length=strtok_r(NULL,sep,&rem); |
| 553 | face=strtok_r(NULL,sep,&rem); |
| 554 | metric=strtok_r(NULL,sep,&rem); |
| 555 | |
| 556 | mapping_nbr_router=get_mapping_no(rtr_id); |
| 557 | adj_matrix[mapping_orig_router][mapping_nbr_router]=atoi(metric); |
| 558 | |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | free(lsa_data); |
| 563 | } |
| 564 | |
| 565 | void |
| 566 | make_adj_matrix(int **adj_matrix,int map_element) |
| 567 | { |
| 568 | |
| 569 | init_adj_matrix(adj_matrix,map_element); |
| 570 | |
| 571 | int i, adj_lsdb_element; |
| 572 | struct alsa *adj_lsa; |
| 573 | |
| 574 | struct hashtb_enumerator ee; |
| 575 | struct hashtb_enumerator *e = ⅇ |
| 576 | |
| 577 | hashtb_start(nlsr->lsdb->adj_lsdb, e); |
| 578 | adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb); |
| 579 | |
| 580 | for(i=0;i<adj_lsdb_element;i++) |
| 581 | { |
| 582 | adj_lsa=e->data; |
| 583 | assign_adj_matrix_for_lsa(adj_lsa,adj_matrix); |
| 584 | hashtb_next(e); |
| 585 | } |
| 586 | |
| 587 | hashtb_end(e); |
| 588 | |
| 589 | } |
| 590 | |
| 591 | void |
| 592 | init_adj_matrix(int **adj_matrix,int map_element) |
| 593 | { |
| 594 | int i, j; |
| 595 | for(i=0;i<map_element;i++) |
| 596 | for(j=0;j<map_element;j++) |
| 597 | adj_matrix[i][j]=0; |
| 598 | } |
| 599 | |
| 600 | void print_adj_matrix(int **adj_matrix, int map_element) |
| 601 | { |
| 602 | int i, j; |
| 603 | for(i=0;i<map_element;i++) |
| 604 | { |
| 605 | for(j=0;j<map_element;j++) |
| 606 | printf("%d ",adj_matrix[i][j]); |
| 607 | printf("\n"); |
| 608 | } |
| 609 | } |
| 610 | |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 611 | int |
| 612 | get_next_hop(char *dest_router) |
| 613 | { |
| 614 | struct routing_table_entry *rte; |
| 615 | |
| 616 | struct hashtb_enumerator ee; |
| 617 | struct hashtb_enumerator *e = ⅇ |
| 618 | int res,ret; |
| 619 | |
| 620 | hashtb_start(nlsr->routing_table, e); |
| 621 | res = hashtb_seek(e, dest_router, strlen(dest_router), 0); |
| 622 | |
| 623 | if( res == HT_OLD_ENTRY ) |
| 624 | { |
| 625 | rte=e->data; |
| 626 | ret=rte->next_hop_face; |
| 627 | } |
| 628 | else if(res == HT_NEW_ENTRY) |
| 629 | { |
| 630 | hashtb_delete(e); |
| 631 | ret=NO_NEXT_HOP; |
| 632 | } |
| 633 | |
| 634 | hashtb_end(e); |
| 635 | |
| 636 | return ret; |
| 637 | } |
| 638 | |
| 639 | void |
| 640 | add_next_hop_router(char *dest_router) |
| 641 | { |
| 642 | if ( strcmp(dest_router,nlsr->router_name)== 0) |
| 643 | { |
| 644 | return ; |
| 645 | } |
| 646 | |
| 647 | struct routing_table_entry *rte=(struct routing_table_entry *)malloc(sizeof(struct routing_table_entry)); |
| 648 | |
| 649 | struct hashtb_enumerator ee; |
| 650 | struct hashtb_enumerator *e = ⅇ |
| 651 | int res; |
| 652 | |
| 653 | hashtb_start(nlsr->routing_table, e); |
| 654 | res = hashtb_seek(e, dest_router, strlen(dest_router), 0); |
| 655 | |
| 656 | if( res == HT_NEW_ENTRY ) |
| 657 | { |
| 658 | rte=e->data; |
| 659 | rte->dest_router=(char *)malloc(strlen(dest_router)+1); |
| 660 | memset(rte->dest_router,0,strlen(dest_router)+1); |
| 661 | memcpy(rte->dest_router,dest_router,strlen(dest_router)); |
| 662 | rte->next_hop_face=NO_NEXT_HOP; |
| 663 | } |
| 664 | hashtb_end(e); |
| 665 | |
| 666 | } |
| 667 | |
| 668 | void |
| 669 | add_next_hop_from_lsa_adj_body(char *body, int no_link) |
| 670 | { |
| 671 | |
| 672 | int i=0; |
| 673 | char *lsa_data=(char *)malloc(strlen(body)+1); |
| 674 | memset( lsa_data,0,strlen(body)+1); |
| 675 | memcpy(lsa_data,body,strlen(body)+1); |
| 676 | char *sep="|"; |
| 677 | char *rem; |
| 678 | char *rtr_id; |
| 679 | char *length; |
| 680 | char *face; |
| 681 | char *metric; |
| 682 | |
| 683 | if(no_link >0 ) |
| 684 | { |
| 685 | rtr_id=strtok_r(lsa_data,sep,&rem); |
| 686 | length=strtok_r(NULL,sep,&rem); |
| 687 | face=strtok_r(NULL,sep,&rem); |
| 688 | metric=strtok_r(NULL,sep,&rem); |
| 689 | |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 690 | |
| 691 | add_next_hop_router(rtr_id); |
| 692 | |
| 693 | for(i=1;i<no_link;i++) |
| 694 | { |
| 695 | rtr_id=strtok_r(NULL,sep,&rem); |
| 696 | length=strtok_r(NULL,sep,&rem); |
| 697 | face=strtok_r(NULL,sep,&rem); |
| 698 | metric=strtok_r(NULL,sep,&rem); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 699 | |
| 700 | add_next_hop_router(rtr_id); |
| 701 | |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | free(lsa_data); |
| 706 | |
| 707 | |
| 708 | } |
| 709 | |
| 710 | void |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 711 | update_routing_table(char * dest_router,int next_hop_face) |
| 712 | { |
| 713 | int res; |
| 714 | struct routing_table_entry *rte; |
| 715 | |
| 716 | struct hashtb_enumerator ee; |
| 717 | struct hashtb_enumerator *e = ⅇ |
| 718 | |
| 719 | hashtb_start(nlsr->routing_table, e); |
| 720 | res = hashtb_seek(e, dest_router, strlen(dest_router), 0); |
| 721 | |
| 722 | if( res == HT_OLD_ENTRY ) |
| 723 | { |
| 724 | rte=e->data; |
| 725 | rte->next_hop_face=next_hop_face; |
| 726 | } |
| 727 | else if ( res == HT_OLD_ENTRY ) |
| 728 | { |
| 729 | hashtb_delete(e); |
| 730 | } |
| 731 | |
| 732 | hashtb_end(e); |
| 733 | |
| 734 | } |
| 735 | |
| 736 | void |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 737 | print_routing_table(void) |
| 738 | { |
| 739 | printf("\n"); |
| 740 | printf("print_routing_table called\n"); |
| 741 | int i, rt_element; |
| 742 | |
| 743 | struct routing_table_entry *rte; |
| 744 | |
| 745 | struct hashtb_enumerator ee; |
| 746 | struct hashtb_enumerator *e = ⅇ |
| 747 | |
| 748 | hashtb_start(nlsr->routing_table, e); |
| 749 | rt_element=hashtb_n(nlsr->routing_table); |
| 750 | |
| 751 | for(i=0;i<rt_element;i++) |
| 752 | { |
| 753 | printf("----------Routing Table Entry %d------------------\n",i+1); |
| 754 | rte=e->data; |
| 755 | printf(" Destination Router: %s \n",rte->dest_router); |
| 756 | 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); |
| 757 | hashtb_next(e); |
| 758 | } |
| 759 | |
| 760 | hashtb_end(e); |
| 761 | |
| 762 | printf("\n"); |
| 763 | } |
| 764 | |
| 765 | void |
| 766 | do_old_routing_table_updates() |
| 767 | { |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 768 | printf("do_old_routing_table_updates called\n"); |
| 769 | int i, rt_element; |
| 770 | int mapping_no; |
| 771 | |
| 772 | struct routing_table_entry *rte; |
| 773 | |
| 774 | struct hashtb_enumerator ee; |
| 775 | struct hashtb_enumerator *e = ⅇ |
| 776 | |
| 777 | hashtb_start(nlsr->routing_table, e); |
| 778 | rt_element=hashtb_n(nlsr->routing_table); |
| 779 | |
| 780 | for(i=0;i<rt_element;i++) |
| 781 | { |
| 782 | rte=e->data; |
| 783 | mapping_no=get_mapping_no(rte->dest_router); |
| 784 | if ( mapping_no == NO_MAPPING_NUM) |
| 785 | { |
| 786 | delete_orig_router_from_npt(rte->dest_router,rte->next_hop_face); |
| 787 | hashtb_delete(e); |
| 788 | } |
| 789 | hashtb_next(e); |
| 790 | } |
| 791 | |
| 792 | hashtb_end(e); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 793 | } |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 794 | |
akmhoque | fbfd098 | 2012-09-09 20:59:03 -0500 | [diff] [blame] | 795 | void |
| 796 | update_routing_table_with_new_route(long int *parent, long int source) |
| 797 | { |
| 798 | printf("update_routing_table_with_new_route called\n"); |
| 799 | int i, map_element; |
| 800 | struct map_entry *me; |
| 801 | |
| 802 | struct hashtb_enumerator ee; |
| 803 | struct hashtb_enumerator *e = ⅇ |
| 804 | |
| 805 | hashtb_start(nlsr->rev_map, e); |
| 806 | map_element=hashtb_n(nlsr->rev_map); |
| 807 | |
| 808 | for(i=0;i<map_element;i++) |
| 809 | { |
| 810 | me=e->data; |
| 811 | if(me->mapping != source) |
| 812 | { |
| 813 | |
| 814 | char *orig_router=get_router_from_rev_map(me->mapping); |
| 815 | if (orig_router != NULL ) |
| 816 | { |
| 817 | int next_hop_router_num=get_next_hop_from_calculation(parent,me->mapping,source); |
| 818 | //printf(" Next hop router Num: %d ",next_hop_router_num); |
| 819 | if ( next_hop_router_num == NO_NEXT_HOP ) |
| 820 | { |
| 821 | update_npt_with_new_route(orig_router,NO_FACE); |
| 822 | printf ("\nOrig_router: %s Next Hop Face: %d \n",orig_router,NO_FACE); |
| 823 | } |
| 824 | else |
| 825 | { |
| 826 | char *next_hop_router=get_router_from_rev_map(next_hop_router_num); |
| 827 | //printf("Next hop router name: %s \n",next_hop_router); |
| 828 | int next_hop_face=get_next_hop_face_from_adl(next_hop_router); |
| 829 | update_npt_with_new_route(orig_router,next_hop_face); |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 830 | update_routing_table(orig_router,next_hop_face); |
akmhoque | fbfd098 | 2012-09-09 20:59:03 -0500 | [diff] [blame] | 831 | printf ("Orig_router: %s Next Hop Face: %d \n",orig_router,next_hop_face); |
| 832 | |
| 833 | } |
| 834 | } |
| 835 | } |
| 836 | hashtb_next(e); |
| 837 | } |
| 838 | |
| 839 | hashtb_end(e); |
| 840 | } |
| 841 | |