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