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