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