akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <math.h> |
| 4 | #include <stdlib.h> |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 5 | #include <unistd.h> |
| 6 | #include <getopt.h> |
| 7 | #include <sys/time.h> |
| 8 | #include <assert.h> |
| 9 | #ifdef HAVE_CONFIG_H |
| 10 | #include <config.h> |
| 11 | #endif |
| 12 | #include <sys/types.h> |
| 13 | |
| 14 | |
| 15 | #include <ccn/ccn.h> |
| 16 | #include <ccn/uri.h> |
| 17 | #include <ccn/keystore.h> |
| 18 | #include <ccn/signing.h> |
| 19 | #include <ccn/schedule.h> |
| 20 | #include <ccn/hashtb.h> |
| 21 | |
| 22 | |
| 23 | #include "nlsr.h" |
| 24 | #include "nlsr_route.h" |
| 25 | #include "nlsr_lsdb.h" |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 26 | #include "nlsr_npt.h" |
akmhoque | fbfd098 | 2012-09-09 20:59:03 -0500 | [diff] [blame] | 27 | #include "nlsr_adl.h" |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 28 | #include "nlsr_fib.h" |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 29 | #include "utility.h" |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 30 | |
| 31 | int |
| 32 | route_calculate(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags) |
| 33 | { |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 34 | |
| 35 | if(flags == CCN_SCHEDULE_CANCEL) |
| 36 | { |
| 37 | return -1; |
| 38 | } |
| 39 | |
| 40 | nlsr_lock(); |
| 41 | |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 42 | if ( nlsr->debugging ) |
| 43 | printf("route_calculate called\n"); |
| 44 | if ( nlsr->detailed_logging ) |
| 45 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"route_calculate called\n"); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 46 | |
| 47 | if( ! nlsr->is_build_adj_lsa_sheduled ) |
| 48 | { |
| 49 | /* Calculate Route here */ |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 50 | print_routing_table(); |
akmhoque | ae38886 | 2013-03-15 01:46:22 -0500 | [diff] [blame] | 51 | print_npt(); |
akmhoque | 0b60ba9 | 2013-02-25 17:55:35 -0600 | [diff] [blame] | 52 | nlsr->map = hashtb_create(sizeof(struct map_entry), NULL); |
| 53 | nlsr->rev_map = hashtb_create(sizeof(struct map_entry), NULL); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 54 | make_map(); |
| 55 | assign_mapping_number(); |
| 56 | print_map(); |
akmhoque | fbfd098 | 2012-09-09 20:59:03 -0500 | [diff] [blame] | 57 | print_rev_map(); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 58 | |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 59 | do_old_routing_table_updates(); |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 60 | clear_old_routing_table(); |
| 61 | print_routing_table(); |
akmhoque | a30cb77 | 2012-10-07 09:50:34 -0500 | [diff] [blame] | 62 | print_npt(); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 63 | |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 64 | int i; |
| 65 | int **adj_matrix; |
| 66 | int map_element=hashtb_n(nlsr->map); |
| 67 | adj_matrix=malloc(map_element * sizeof(int *)); |
| 68 | for(i = 0; i < map_element; i++) |
| 69 | { |
| 70 | adj_matrix[i] = malloc(map_element * sizeof(int)); |
| 71 | } |
| 72 | make_adj_matrix(adj_matrix,map_element); |
akmhoque | 3171d65 | 2012-11-13 11:44:33 -0600 | [diff] [blame] | 73 | if ( nlsr->debugging ) |
| 74 | print_adj_matrix(adj_matrix,map_element); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 75 | |
| 76 | long int source=get_mapping_no(nlsr->router_name); |
akmhoque | 9fe296b | 2012-09-24 09:52:08 -0500 | [diff] [blame] | 77 | int num_link=get_no_link_from_adj_matrix(adj_matrix, map_element ,source); |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame] | 78 | |
| 79 | if ( nlsr->is_hyperbolic_calc == 1) |
akmhoque | 9fe296b | 2012-09-24 09:52:08 -0500 | [diff] [blame] | 80 | { |
| 81 | long int *links=(long int *)malloc(num_link*sizeof(long int)); |
| 82 | long int *link_costs=(long int *)malloc(num_link*sizeof(long int)); |
| 83 | get_links_from_adj_matrix(adj_matrix, map_element , links, link_costs, source); |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame] | 84 | |
| 85 | struct hashtb_enumerator ee; |
| 86 | struct hashtb_enumerator *e = ⅇ |
| 87 | for (hashtb_start(nlsr->rev_map, e); e->key != NULL; hashtb_next(e)) |
akmhoque | 9fe296b | 2012-09-24 09:52:08 -0500 | [diff] [blame] | 88 | { |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame] | 89 | struct map_entry *me=e->data; |
| 90 | if ( me->mapping != source ) |
| 91 | { |
| 92 | long int *faces=(long int *)calloc(num_link,sizeof(long int)); |
| 93 | double *nbr_dist=(double *)calloc(num_link,sizeof(double)); |
| 94 | double *nbr_to_dest=(double *)calloc(num_link,sizeof(double)); |
| 95 | for ( i=0 ; i < num_link; i++) |
| 96 | { |
| 97 | int face=get_next_hop_face_from_adl(get_router_from_rev_map(links[i])); |
| 98 | double dist_to_nbr=get_hyperbolic_distance(source,links[i]); |
| 99 | double dist_to_dest_from_nbr=get_hyperbolic_distance(links[i],me->mapping); |
| 100 | faces[i]=face; |
| 101 | nbr_dist[i]=dist_to_nbr; |
| 102 | nbr_to_dest[i]= dist_to_dest_from_nbr; |
| 103 | |
| 104 | |
| 105 | } |
| 106 | sort_hyperbolic_route(nbr_to_dest,nbr_dist, faces,0,num_link); |
| 107 | if (nlsr->max_faces_per_prefix == 0 ) |
| 108 | { |
| 109 | for ( i=0 ; i < num_link; i++) |
| 110 | { |
| 111 | update_routing_table_with_new_hyperbolic_route(me->mapping,faces[i],nbr_to_dest[i]); |
| 112 | } |
| 113 | } |
| 114 | else if ( nlsr->max_faces_per_prefix > 0 ) |
| 115 | { |
| 116 | if ( num_link <= nlsr->max_faces_per_prefix ) |
| 117 | { |
| 118 | for ( i=0 ; i < num_link; i++) |
| 119 | { |
| 120 | update_routing_table_with_new_hyperbolic_route(me->mapping,faces[i],nbr_to_dest[i]); |
| 121 | } |
| 122 | } |
| 123 | else if (num_link > nlsr->max_faces_per_prefix) |
| 124 | { |
| 125 | for ( i=0 ; i < nlsr->max_faces_per_prefix; i++) |
| 126 | { |
| 127 | update_routing_table_with_new_hyperbolic_route(me->mapping,faces[i],nbr_to_dest[i]); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | } |
| 132 | free(faces); |
| 133 | free(nbr_dist); |
| 134 | free(nbr_to_dest); |
| 135 | } |
| 136 | } |
| 137 | hashtb_end(e); |
| 138 | |
| 139 | |
| 140 | free(links); |
| 141 | free(link_costs); |
| 142 | } |
| 143 | else if (nlsr->is_hyperbolic_calc == 0 ) |
| 144 | { |
| 145 | |
| 146 | long int *parent=(long int *)malloc(map_element * sizeof(long int)); |
| 147 | long int *dist=(long int *)malloc(map_element * sizeof(long int)); |
| 148 | |
| 149 | |
| 150 | if ( (num_link == 0) || (nlsr->max_faces_per_prefix == 1 ) ) |
| 151 | { |
akmhoque | 9fe296b | 2012-09-24 09:52:08 -0500 | [diff] [blame] | 152 | calculate_path(adj_matrix,parent,dist, map_element, source); |
| 153 | print_all_path_from_source(parent,source); |
| 154 | print_all_next_hop(parent,source); |
| 155 | update_routing_table_with_new_route(parent, dist,source); |
| 156 | } |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame] | 157 | else if ( (num_link != 0) && (nlsr->max_faces_per_prefix == 0 || nlsr->max_faces_per_prefix > 1 ) ) |
| 158 | { |
| 159 | long int *links=(long int *)malloc(num_link*sizeof(long int)); |
| 160 | long int *link_costs=(long int *)malloc(num_link*sizeof(long int)); |
| 161 | get_links_from_adj_matrix(adj_matrix, map_element , links, link_costs, source); |
| 162 | for ( i=0 ; i < num_link; i++) |
| 163 | { |
| 164 | adjust_adj_matrix(adj_matrix, map_element,source,links[i],link_costs[i]); |
| 165 | calculate_path(adj_matrix,parent,dist, map_element, source); |
| 166 | print_all_path_from_source(parent,source); |
| 167 | print_all_next_hop(parent,source); |
| 168 | update_routing_table_with_new_route(parent, dist,source); |
| 169 | } |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 170 | |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame] | 171 | free(links); |
| 172 | free(link_costs); |
| 173 | } |
| 174 | free(parent); |
| 175 | free(dist); |
akmhoque | 9fe296b | 2012-09-24 09:52:08 -0500 | [diff] [blame] | 176 | } |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame] | 177 | |
| 178 | print_routing_table(); |
| 179 | print_npt(); |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 180 | |
| 181 | update_npt_with_new_route(); |
akmhoque | fbfd098 | 2012-09-09 20:59:03 -0500 | [diff] [blame] | 182 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 183 | print_routing_table(); |
| 184 | print_npt(); |
akmhoque | fbfd098 | 2012-09-09 20:59:03 -0500 | [diff] [blame] | 185 | |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame] | 186 | |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 187 | for(i = 0; i < map_element; i++) |
| 188 | { |
| 189 | free(adj_matrix[i]); |
| 190 | } |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame] | 191 | |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 192 | free(adj_matrix); |
akmhoque | 54a14f4 | 2013-02-24 06:16:20 -0600 | [diff] [blame] | 193 | destroy_map(); |
| 194 | destroy_rev_map(); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 195 | |
| 196 | } |
| 197 | nlsr->is_route_calculation_scheduled=0; |
| 198 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 199 | nlsr_unlock(); |
| 200 | |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | void |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 205 | 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] | 206 | { |
| 207 | int i; |
| 208 | long int v,u; |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 209 | //long int *dist=(long int *)malloc(V * sizeof(long int)); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 210 | long int *Q=(long int *)malloc(V * sizeof(long int)); |
| 211 | long int head=0; |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame] | 212 | /* Initiate the Parent */ |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 213 | for (i = 0 ; i < V; i++) |
| 214 | { |
| 215 | parent[i]=EMPTY_PARENT; |
| 216 | dist[i]=INF_DISTANCE; |
| 217 | Q[i]=i; |
| 218 | } |
| 219 | |
akmhoque | 9fe296b | 2012-09-24 09:52:08 -0500 | [diff] [blame] | 220 | if ( S != NO_MAPPING_NUM ) |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 221 | { |
akmhoque | 9fe296b | 2012-09-24 09:52:08 -0500 | [diff] [blame] | 222 | dist[S]=0; |
| 223 | sort_queue_by_distance(Q,dist,head,V); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 224 | |
akmhoque | 9fe296b | 2012-09-24 09:52:08 -0500 | [diff] [blame] | 225 | while (head < V ) |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 226 | { |
akmhoque | 9fe296b | 2012-09-24 09:52:08 -0500 | [diff] [blame] | 227 | u=Q[head]; |
| 228 | if(dist[u] == INF_DISTANCE) |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 229 | { |
akmhoque | 9fe296b | 2012-09-24 09:52:08 -0500 | [diff] [blame] | 230 | break; |
| 231 | } |
| 232 | |
| 233 | for(v=0 ; v <V; v++) |
| 234 | { |
| 235 | if( adj_matrix[u][v] > 0 ) // |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 236 | { |
akmhoque | 9fe296b | 2012-09-24 09:52:08 -0500 | [diff] [blame] | 237 | if ( is_not_explored(Q,v,head+1,V) ) |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 238 | { |
akmhoque | 9fe296b | 2012-09-24 09:52:08 -0500 | [diff] [blame] | 239 | |
| 240 | if( dist[u] + adj_matrix[u][v] < dist[v]) |
| 241 | { |
| 242 | dist[v]=dist[u] + adj_matrix[u][v] ; |
| 243 | parent[v]=u; |
| 244 | } |
| 245 | |
| 246 | } |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 247 | |
| 248 | } |
| 249 | |
| 250 | } |
| 251 | |
akmhoque | 9fe296b | 2012-09-24 09:52:08 -0500 | [diff] [blame] | 252 | head++; |
| 253 | sort_queue_by_distance(Q,dist,head,V); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 254 | } |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 255 | } |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame] | 256 | free(Q); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | void |
| 260 | print_all_path_from_source(long int *parent,long int source) |
| 261 | { |
| 262 | int i, map_element; |
| 263 | struct map_entry *me; |
| 264 | |
| 265 | struct hashtb_enumerator ee; |
| 266 | struct hashtb_enumerator *e = ⅇ |
| 267 | |
| 268 | hashtb_start(nlsr->map, e); |
| 269 | map_element=hashtb_n(nlsr->map); |
| 270 | |
akmhoque | 9fe296b | 2012-09-24 09:52:08 -0500 | [diff] [blame] | 271 | if ( source != NO_MAPPING_NUM) |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 272 | { |
akmhoque | 9fe296b | 2012-09-24 09:52:08 -0500 | [diff] [blame] | 273 | for(i=0;i<map_element;i++) |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 274 | { |
akmhoque | 9fe296b | 2012-09-24 09:52:08 -0500 | [diff] [blame] | 275 | me=e->data; |
| 276 | if(me->mapping != source) |
| 277 | { |
akmhoque | 3171d65 | 2012-11-13 11:44:33 -0600 | [diff] [blame] | 278 | |
| 279 | if ( nlsr->debugging ) |
| 280 | { |
| 281 | print_path(parent,(long int)me->mapping); |
| 282 | printf("\n"); |
| 283 | } |
| 284 | if ( nlsr->detailed_logging ) |
| 285 | { |
| 286 | print_path(parent,(long int)me->mapping); |
| 287 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n"); |
| 288 | } |
| 289 | |
akmhoque | 9fe296b | 2012-09-24 09:52:08 -0500 | [diff] [blame] | 290 | } |
| 291 | hashtb_next(e); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 292 | } |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 293 | } |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 294 | hashtb_end(e); |
| 295 | |
| 296 | } |
| 297 | |
akmhoque | fbfd098 | 2012-09-09 20:59:03 -0500 | [diff] [blame] | 298 | void |
| 299 | print_all_next_hop(long int *parent,long int source) |
| 300 | { |
| 301 | int i, map_element; |
| 302 | struct map_entry *me; |
| 303 | |
| 304 | struct hashtb_enumerator ee; |
| 305 | struct hashtb_enumerator *e = ⅇ |
| 306 | |
| 307 | hashtb_start(nlsr->map, e); |
| 308 | map_element=hashtb_n(nlsr->map); |
| 309 | |
| 310 | for(i=0;i<map_element;i++) |
| 311 | { |
| 312 | me=e->data; |
| 313 | if(me->mapping != source) |
| 314 | { |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 315 | if ( nlsr->debugging ) |
| 316 | printf("Dest: %d Next Hop: %ld\n",me->mapping,get_next_hop_from_calculation(parent,me->mapping,source)); |
| 317 | if ( nlsr->detailed_logging ) |
| 318 | 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] | 319 | } |
| 320 | hashtb_next(e); |
| 321 | } |
| 322 | |
| 323 | hashtb_end(e); |
| 324 | |
| 325 | } |
| 326 | |
| 327 | long int |
| 328 | get_next_hop_from_calculation(long int *parent, long int dest,long int source) |
| 329 | { |
| 330 | long int next_hop; |
| 331 | while ( parent[dest] != EMPTY_PARENT ) |
| 332 | { |
| 333 | next_hop=dest; |
| 334 | dest=parent[dest]; |
| 335 | |
| 336 | } |
| 337 | |
| 338 | if ( dest != source ) |
| 339 | { |
| 340 | next_hop=NO_NEXT_HOP; |
| 341 | } |
| 342 | |
| 343 | return next_hop; |
| 344 | |
| 345 | } |
| 346 | |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 347 | void |
| 348 | print_path(long int *parent, long int dest) |
| 349 | { |
| 350 | if (parent[dest] != EMPTY_PARENT ) |
| 351 | print_path(parent,parent[dest]); |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame] | 352 | |
| 353 | if ( nlsr->debugging ) |
| 354 | printf(" %ld",dest); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | int |
| 358 | is_not_explored(long int *Q, long int u,long int start, long int element) |
| 359 | { |
| 360 | int ret=0; |
| 361 | long int i; |
| 362 | for(i=start; i< element; i++) |
| 363 | { |
| 364 | if ( Q[i] == u ) |
| 365 | { |
| 366 | ret=1; |
| 367 | break; |
| 368 | } |
| 369 | } |
| 370 | return ret; |
| 371 | } |
| 372 | |
| 373 | void |
| 374 | sort_queue_by_distance(long int *Q,long int *dist,long int start,long int element) |
| 375 | { |
| 376 | long int i,j; |
| 377 | long int temp_u; |
| 378 | |
| 379 | for ( i=start ; i < element ; i ++) |
| 380 | { |
| 381 | for( j=i+1; j<element; j ++) |
| 382 | { |
| 383 | if (dist[Q[j]] < dist[Q[i]]) |
| 384 | { |
| 385 | temp_u=Q[j]; |
| 386 | Q[j]=Q[i]; |
| 387 | Q[i]=temp_u; |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | } |
| 393 | |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame] | 394 | void |
| 395 | sort_hyperbolic_route(double *dist_dest,double *dist_nbr, long int *faces,long int start,long int element) |
| 396 | { |
| 397 | long int i,j; |
| 398 | double temp_dist; |
| 399 | long int temp_face; |
| 400 | |
| 401 | for ( i=start ; i < element ; i ++) |
| 402 | { |
| 403 | for( j=i+1; j<element; j ++) |
| 404 | { |
| 405 | if (dist_dest[i] < dist_dest[j]) |
| 406 | { |
| 407 | temp_dist=dist_dest[j]; |
| 408 | dist_dest[j]=dist_dest[i]; |
| 409 | dist_dest[i]=temp_dist; |
| 410 | |
| 411 | temp_dist=dist_nbr[j]; |
| 412 | dist_nbr[j]=dist_nbr[i]; |
| 413 | dist_nbr[i]=temp_dist; |
| 414 | |
| 415 | temp_face=faces[j]; |
| 416 | faces[j]=faces[i]; |
| 417 | faces[i]=temp_face; |
| 418 | } |
| 419 | if ( (dist_dest[i] == dist_dest[j]) && (dist_nbr[i] < dist_nbr[j]) ) |
| 420 | { |
| 421 | temp_dist=dist_dest[j]; |
| 422 | dist_dest[j]=dist_dest[i]; |
| 423 | dist_dest[i]=temp_dist; |
| 424 | |
| 425 | temp_dist=dist_nbr[j]; |
| 426 | dist_nbr[j]=dist_nbr[i]; |
| 427 | dist_nbr[i]=temp_dist; |
| 428 | |
| 429 | temp_face=faces[j]; |
| 430 | faces[j]=faces[i]; |
| 431 | faces[i]=temp_face; |
| 432 | } |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | } |
| 437 | |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 438 | void |
| 439 | print_map(void) |
| 440 | { |
| 441 | int i, map_element; |
| 442 | struct map_entry *me; |
| 443 | |
| 444 | struct hashtb_enumerator ee; |
| 445 | struct hashtb_enumerator *e = ⅇ |
| 446 | |
| 447 | hashtb_start(nlsr->map, e); |
| 448 | map_element=hashtb_n(nlsr->map); |
| 449 | |
| 450 | for(i=0;i<map_element;i++) |
| 451 | { |
| 452 | me=e->data; |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 453 | if ( nlsr->debugging ) |
| 454 | printf("Router: %s Mapping Number: %d \n",me->router,me->mapping); |
| 455 | if ( nlsr->detailed_logging ) |
| 456 | 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] | 457 | hashtb_next(e); |
| 458 | } |
| 459 | |
| 460 | hashtb_end(e); |
| 461 | } |
| 462 | |
| 463 | |
| 464 | void |
| 465 | assign_mapping_number(void) |
| 466 | { |
| 467 | int i, map_element; |
| 468 | struct map_entry *me; |
| 469 | |
| 470 | struct hashtb_enumerator ee; |
| 471 | struct hashtb_enumerator *e = ⅇ |
| 472 | |
| 473 | hashtb_start(nlsr->map, e); |
| 474 | map_element=hashtb_n(nlsr->map); |
| 475 | |
| 476 | for(i=0;i<map_element;i++) |
| 477 | { |
| 478 | me=e->data; |
| 479 | me->mapping=i; |
akmhoque | fbfd098 | 2012-09-09 20:59:03 -0500 | [diff] [blame] | 480 | add_rev_map_entry(i,me->router); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 481 | hashtb_next(e); |
| 482 | } |
| 483 | |
| 484 | hashtb_end(e); |
| 485 | } |
| 486 | |
| 487 | void |
| 488 | make_map(void) |
| 489 | { |
| 490 | |
| 491 | |
| 492 | int i, adj_lsdb_element; |
| 493 | struct alsa *adj_lsa; |
| 494 | |
| 495 | struct hashtb_enumerator ee; |
| 496 | struct hashtb_enumerator *e = ⅇ |
| 497 | |
| 498 | hashtb_start(nlsr->lsdb->adj_lsdb, e); |
| 499 | adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb); |
| 500 | |
akmhoque | 9fe296b | 2012-09-24 09:52:08 -0500 | [diff] [blame] | 501 | add_map_entry(nlsr->router_name); |
| 502 | |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 503 | for(i=0;i<adj_lsdb_element;i++) |
| 504 | { |
| 505 | adj_lsa=e->data; |
| 506 | add_adj_data_to_map(adj_lsa->header->orig_router->name,adj_lsa->body,adj_lsa->no_link); |
| 507 | hashtb_next(e); |
| 508 | } |
| 509 | |
| 510 | hashtb_end(e); |
| 511 | |
| 512 | } |
| 513 | |
| 514 | void |
| 515 | add_map_entry(char *router) |
| 516 | { |
| 517 | |
akmhoque | cecba94 | 2013-02-25 17:33:34 -0600 | [diff] [blame] | 518 | struct map_entry *me;//=(struct map_entry*)malloc(sizeof(struct map_entry )); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 519 | |
| 520 | struct hashtb_enumerator ee; |
| 521 | struct hashtb_enumerator *e = ⅇ |
| 522 | int res; |
| 523 | |
| 524 | hashtb_start(nlsr->map, e); |
| 525 | res = hashtb_seek(e, router, strlen(router), 0); |
| 526 | |
| 527 | if(res == HT_NEW_ENTRY) |
| 528 | { |
| 529 | me=e->data; |
| 530 | me->router=(char *)malloc(strlen(router)+1); |
| 531 | memset(me->router,0,strlen(router)+1); |
| 532 | memcpy(me->router,router,strlen(router)); |
| 533 | me->mapping=0; |
| 534 | } |
| 535 | |
| 536 | hashtb_end(e); |
| 537 | |
| 538 | } |
| 539 | |
| 540 | |
| 541 | void |
| 542 | add_adj_data_to_map(char *orig_router, char *body, int no_link) |
| 543 | { |
| 544 | add_map_entry(orig_router); |
| 545 | |
| 546 | int i=0; |
| 547 | char *lsa_data=(char *)malloc(strlen(body)+1); |
| 548 | memset( lsa_data,0,strlen(body)+1); |
| 549 | memcpy(lsa_data,body,strlen(body)+1); |
| 550 | char *sep="|"; |
| 551 | char *rem; |
| 552 | char *rtr_id; |
| 553 | char *length; |
akmhoque | 866c222 | 2013-02-12 10:49:33 -0600 | [diff] [blame] | 554 | //char *face; |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 555 | char *metric; |
| 556 | |
| 557 | if(no_link >0 ) |
| 558 | { |
| 559 | rtr_id=strtok_r(lsa_data,sep,&rem); |
| 560 | length=strtok_r(NULL,sep,&rem); |
akmhoque | 866c222 | 2013-02-12 10:49:33 -0600 | [diff] [blame] | 561 | //face=strtok_r(NULL,sep,&rem); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 562 | metric=strtok_r(NULL,sep,&rem); |
| 563 | |
akmhoque | b28579d | 2013-02-12 11:15:52 -0600 | [diff] [blame] | 564 | if ( !nlsr->debugging && nlsr->debugging) |
| 565 | { |
| 566 | printf("Metric: %s Length:%s\n",metric,length); |
| 567 | } |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 568 | add_map_entry(rtr_id); |
| 569 | |
| 570 | for(i=1;i<no_link;i++) |
| 571 | { |
| 572 | rtr_id=strtok_r(NULL,sep,&rem); |
| 573 | length=strtok_r(NULL,sep,&rem); |
akmhoque | 866c222 | 2013-02-12 10:49:33 -0600 | [diff] [blame] | 574 | //face=strtok_r(NULL,sep,&rem); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 575 | metric=strtok_r(NULL,sep,&rem); |
akmhoque | b28579d | 2013-02-12 11:15:52 -0600 | [diff] [blame] | 576 | if ( !nlsr->debugging && nlsr->debugging) |
| 577 | { |
| 578 | printf("Metric: %s Length:%s\n",metric,length); |
| 579 | } |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 580 | |
| 581 | add_map_entry(rtr_id); |
| 582 | |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | free(lsa_data); |
| 587 | } |
| 588 | |
| 589 | int |
| 590 | get_mapping_no(char *router) |
| 591 | { |
| 592 | struct map_entry *me; |
| 593 | |
| 594 | struct hashtb_enumerator ee; |
| 595 | struct hashtb_enumerator *e = ⅇ |
akmhoque | 0315f98 | 2012-09-09 11:28:05 -0500 | [diff] [blame] | 596 | int res; |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 597 | int ret; |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 598 | |
akmhoque | 63152c6 | 2012-09-18 08:43:42 -0500 | [diff] [blame] | 599 | int n = hashtb_n(nlsr->map); |
| 600 | |
| 601 | if ( n < 1) |
| 602 | { |
| 603 | return NO_MAPPING_NUM; |
| 604 | } |
| 605 | |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 606 | hashtb_start(nlsr->map, e); |
| 607 | res = hashtb_seek(e, router, strlen(router), 0); |
| 608 | |
| 609 | if( res == HT_OLD_ENTRY ) |
| 610 | { |
| 611 | me=e->data; |
| 612 | ret=me->mapping; |
| 613 | } |
| 614 | else if(res == HT_NEW_ENTRY) |
| 615 | { |
| 616 | hashtb_delete(e); |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 617 | ret=NO_MAPPING_NUM; |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | hashtb_end(e); |
| 621 | |
| 622 | return ret; |
| 623 | |
| 624 | } |
| 625 | |
akmhoque | fbfd098 | 2012-09-09 20:59:03 -0500 | [diff] [blame] | 626 | |
| 627 | void |
| 628 | add_rev_map_entry(long int mapping_number, char *router) |
| 629 | { |
| 630 | |
akmhoque | cecba94 | 2013-02-25 17:33:34 -0600 | [diff] [blame] | 631 | struct map_entry *me;//=(struct map_entry*)malloc(sizeof(struct map_entry )); |
akmhoque | fbfd098 | 2012-09-09 20:59:03 -0500 | [diff] [blame] | 632 | |
| 633 | struct hashtb_enumerator ee; |
| 634 | struct hashtb_enumerator *e = ⅇ |
| 635 | int res; |
| 636 | |
| 637 | hashtb_start(nlsr->rev_map, e); |
| 638 | res = hashtb_seek(e, &mapping_number, sizeof(mapping_number), 0); |
| 639 | |
| 640 | if(res == HT_NEW_ENTRY) |
| 641 | { |
| 642 | me=e->data; |
| 643 | me->router=(char *)malloc(strlen(router)+1); |
| 644 | memset(me->router,0,strlen(router)+1); |
| 645 | memcpy(me->router,router,strlen(router)); |
| 646 | me->mapping=mapping_number; |
| 647 | } |
| 648 | |
| 649 | hashtb_end(e); |
| 650 | |
| 651 | } |
| 652 | |
| 653 | |
| 654 | |
| 655 | char * |
| 656 | get_router_from_rev_map(long int mapping_number) |
| 657 | { |
| 658 | |
| 659 | struct map_entry *me; |
| 660 | |
| 661 | struct hashtb_enumerator ee; |
| 662 | struct hashtb_enumerator *e = ⅇ |
| 663 | int res; |
| 664 | |
| 665 | hashtb_start(nlsr->rev_map, e); |
| 666 | res = hashtb_seek(e, &mapping_number, sizeof(mapping_number), 0); |
| 667 | |
| 668 | if(res == HT_OLD_ENTRY) |
| 669 | { |
| 670 | me=e->data; |
| 671 | hashtb_end(e); |
| 672 | return me->router; |
| 673 | } |
| 674 | else if(res == HT_NEW_ENTRY) |
| 675 | { |
| 676 | hashtb_delete(e); |
| 677 | hashtb_end(e); |
| 678 | } |
| 679 | |
| 680 | return NULL; |
| 681 | } |
| 682 | |
| 683 | void |
| 684 | print_rev_map(void) |
| 685 | { |
| 686 | int i, map_element; |
| 687 | struct map_entry *me; |
| 688 | |
| 689 | struct hashtb_enumerator ee; |
| 690 | struct hashtb_enumerator *e = ⅇ |
| 691 | |
| 692 | hashtb_start(nlsr->map, e); |
| 693 | map_element=hashtb_n(nlsr->map); |
| 694 | |
| 695 | for(i=0;i<map_element;i++) |
| 696 | { |
| 697 | me=e->data; |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 698 | if ( nlsr->debugging ) |
| 699 | printf("Mapping Number: %d Router: %s \n",me->mapping,me->router); |
| 700 | if ( nlsr->detailed_logging ) |
| 701 | 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] | 702 | hashtb_next(e); |
| 703 | } |
| 704 | |
| 705 | hashtb_end(e); |
| 706 | } |
| 707 | |
| 708 | |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 709 | void |
| 710 | assign_adj_matrix_for_lsa(struct alsa *adj_lsa, int **adj_matrix) |
| 711 | { |
| 712 | int mapping_orig_router=get_mapping_no(adj_lsa->header->orig_router->name); |
| 713 | int mapping_nbr_router; |
| 714 | |
| 715 | int i; |
| 716 | char *lsa_data=(char *)malloc(strlen(adj_lsa->body)+1); |
| 717 | memset( lsa_data,0,strlen(adj_lsa->body)+1); |
| 718 | memcpy(lsa_data,adj_lsa->body,strlen(adj_lsa->body)+1); |
| 719 | char *sep="|"; |
| 720 | char *rem; |
| 721 | char *rtr_id; |
| 722 | char *length; |
akmhoque | 866c222 | 2013-02-12 10:49:33 -0600 | [diff] [blame] | 723 | //char *face; |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 724 | char *metric; |
| 725 | |
| 726 | if(adj_lsa->no_link >0 ) |
| 727 | { |
| 728 | rtr_id=strtok_r(lsa_data,sep,&rem); |
| 729 | length=strtok_r(NULL,sep,&rem); |
akmhoque | 866c222 | 2013-02-12 10:49:33 -0600 | [diff] [blame] | 730 | //face=strtok_r(NULL,sep,&rem); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 731 | metric=strtok_r(NULL,sep,&rem); |
akmhoque | b28579d | 2013-02-12 11:15:52 -0600 | [diff] [blame] | 732 | |
| 733 | if ( !nlsr->debugging && nlsr->debugging) |
| 734 | { |
| 735 | printf("Metric: %s Length:%s\n",metric,length); |
| 736 | } |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 737 | |
| 738 | mapping_nbr_router=get_mapping_no(rtr_id); |
| 739 | adj_matrix[mapping_orig_router][mapping_nbr_router]=atoi(metric); |
| 740 | |
| 741 | for(i=1;i<adj_lsa->no_link;i++) |
| 742 | { |
| 743 | rtr_id=strtok_r(NULL,sep,&rem); |
| 744 | length=strtok_r(NULL,sep,&rem); |
akmhoque | 866c222 | 2013-02-12 10:49:33 -0600 | [diff] [blame] | 745 | //face=strtok_r(NULL,sep,&rem); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 746 | metric=strtok_r(NULL,sep,&rem); |
| 747 | |
akmhoque | b28579d | 2013-02-12 11:15:52 -0600 | [diff] [blame] | 748 | if ( !nlsr->debugging && nlsr->debugging) |
| 749 | { |
| 750 | printf("Metric: %s Length:%s\n",metric,length); |
| 751 | } |
| 752 | |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 753 | mapping_nbr_router=get_mapping_no(rtr_id); |
| 754 | adj_matrix[mapping_orig_router][mapping_nbr_router]=atoi(metric); |
| 755 | |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | free(lsa_data); |
| 760 | } |
| 761 | |
| 762 | void |
| 763 | make_adj_matrix(int **adj_matrix,int map_element) |
| 764 | { |
| 765 | |
| 766 | init_adj_matrix(adj_matrix,map_element); |
| 767 | |
| 768 | int i, adj_lsdb_element; |
| 769 | struct alsa *adj_lsa; |
| 770 | |
| 771 | struct hashtb_enumerator ee; |
| 772 | struct hashtb_enumerator *e = ⅇ |
| 773 | |
| 774 | hashtb_start(nlsr->lsdb->adj_lsdb, e); |
| 775 | adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb); |
| 776 | |
| 777 | for(i=0;i<adj_lsdb_element;i++) |
| 778 | { |
| 779 | adj_lsa=e->data; |
| 780 | assign_adj_matrix_for_lsa(adj_lsa,adj_matrix); |
| 781 | hashtb_next(e); |
| 782 | } |
| 783 | |
| 784 | hashtb_end(e); |
| 785 | |
| 786 | } |
| 787 | |
| 788 | void |
| 789 | init_adj_matrix(int **adj_matrix,int map_element) |
| 790 | { |
| 791 | int i, j; |
| 792 | for(i=0;i<map_element;i++) |
| 793 | for(j=0;j<map_element;j++) |
| 794 | adj_matrix[i][j]=0; |
| 795 | } |
| 796 | |
| 797 | void print_adj_matrix(int **adj_matrix, int map_element) |
| 798 | { |
| 799 | int i, j; |
| 800 | for(i=0;i<map_element;i++) |
| 801 | { |
| 802 | for(j=0;j<map_element;j++) |
| 803 | printf("%d ",adj_matrix[i][j]); |
| 804 | printf("\n"); |
| 805 | } |
| 806 | } |
| 807 | |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 808 | |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 809 | int |
akmhoque | 9fe296b | 2012-09-24 09:52:08 -0500 | [diff] [blame] | 810 | get_no_link_from_adj_matrix(int **adj_matrix,long int V, long int S) |
| 811 | { |
| 812 | int no_link=0; |
| 813 | int i; |
| 814 | |
| 815 | for(i=0;i<V;i++) |
| 816 | { |
| 817 | if ( adj_matrix[S][i] > 0 ) |
| 818 | { |
| 819 | no_link++; |
| 820 | } |
| 821 | } |
| 822 | return no_link; |
| 823 | } |
| 824 | |
| 825 | void |
| 826 | get_links_from_adj_matrix(int **adj_matrix, long int V ,long int *links, long int *link_costs,long int S) |
| 827 | { |
| 828 | int i,j; |
| 829 | j=0; |
| 830 | for (i=0; i <V; i++) |
| 831 | { |
| 832 | if ( adj_matrix[S][i] > 0 ) |
| 833 | { |
| 834 | links[j]=i; |
| 835 | link_costs[j]=adj_matrix[S][i]; |
| 836 | j++; |
| 837 | } |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | void adjust_adj_matrix(int **adj_matrix, long int V, long int S, long int link,long int link_cost) |
| 842 | { |
| 843 | int i; |
| 844 | for ( i = 0; i < V; i++ ) |
| 845 | { |
| 846 | if ( i == link ) |
| 847 | { |
| 848 | adj_matrix[S][i]=link_cost; |
| 849 | } |
| 850 | else |
| 851 | { |
| 852 | adj_matrix[S][i]=0; |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | } |
| 857 | |
| 858 | int |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 859 | get_number_of_next_hop(char *dest_router) |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 860 | { |
| 861 | struct routing_table_entry *rte; |
| 862 | |
| 863 | struct hashtb_enumerator ee; |
| 864 | struct hashtb_enumerator *e = ⅇ |
| 865 | int res,ret; |
| 866 | |
| 867 | hashtb_start(nlsr->routing_table, e); |
| 868 | res = hashtb_seek(e, dest_router, strlen(dest_router), 0); |
| 869 | |
| 870 | if( res == HT_OLD_ENTRY ) |
| 871 | { |
| 872 | rte=e->data; |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 873 | ret=hashtb_n(rte->face_list); |
| 874 | //nhl=rte->face_list; |
| 875 | } |
| 876 | else if(res == HT_NEW_ENTRY) |
| 877 | { |
| 878 | hashtb_delete(e); |
| 879 | ret=NO_NEXT_HOP; |
| 880 | } |
| 881 | |
| 882 | hashtb_end(e); |
| 883 | |
| 884 | return ret; |
| 885 | } |
| 886 | |
| 887 | |
| 888 | int |
| 889 | get_next_hop(char *dest_router,int *faces, int *route_costs) |
| 890 | { |
| 891 | struct routing_table_entry *rte; |
| 892 | |
| 893 | struct hashtb_enumerator ee; |
| 894 | struct hashtb_enumerator *e = ⅇ |
| 895 | int res,ret; |
| 896 | |
| 897 | hashtb_start(nlsr->routing_table, e); |
| 898 | res = hashtb_seek(e, dest_router, strlen(dest_router), 0); |
| 899 | |
| 900 | if( res == HT_OLD_ENTRY ) |
| 901 | { |
| 902 | rte=e->data; |
| 903 | ret=hashtb_n(rte->face_list); |
| 904 | //nhl=rte->face_list; |
| 905 | int j,face_list_element; |
| 906 | struct face_list_entry *fle; |
| 907 | |
| 908 | struct hashtb_enumerator eef; |
| 909 | struct hashtb_enumerator *ef = &eef; |
| 910 | |
| 911 | hashtb_start(rte->face_list, ef); |
| 912 | face_list_element=hashtb_n(rte->face_list); |
| 913 | for(j=0;j<face_list_element;j++) |
| 914 | { |
| 915 | fle=ef->data; |
| 916 | //printf(" Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost); |
| 917 | faces[j]=fle->next_hop_face; |
| 918 | route_costs[j]=fle->route_cost; |
| 919 | hashtb_next(ef); |
| 920 | } |
| 921 | hashtb_end(ef); |
| 922 | |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 923 | } |
| 924 | else if(res == HT_NEW_ENTRY) |
| 925 | { |
| 926 | hashtb_delete(e); |
| 927 | ret=NO_NEXT_HOP; |
| 928 | } |
| 929 | |
| 930 | hashtb_end(e); |
| 931 | |
| 932 | return ret; |
| 933 | } |
| 934 | |
| 935 | void |
| 936 | add_next_hop_router(char *dest_router) |
| 937 | { |
| 938 | if ( strcmp(dest_router,nlsr->router_name)== 0) |
| 939 | { |
| 940 | return ; |
| 941 | } |
| 942 | |
akmhoque | cecba94 | 2013-02-25 17:33:34 -0600 | [diff] [blame] | 943 | struct routing_table_entry *rte;//=(struct routing_table_entry *)malloc(sizeof(struct routing_table_entry)); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 944 | |
| 945 | struct hashtb_enumerator ee; |
| 946 | struct hashtb_enumerator *e = ⅇ |
| 947 | int res; |
| 948 | |
| 949 | hashtb_start(nlsr->routing_table, e); |
| 950 | res = hashtb_seek(e, dest_router, strlen(dest_router), 0); |
| 951 | |
| 952 | if( res == HT_NEW_ENTRY ) |
| 953 | { |
| 954 | rte=e->data; |
| 955 | rte->dest_router=(char *)malloc(strlen(dest_router)+1); |
| 956 | memset(rte->dest_router,0,strlen(dest_router)+1); |
| 957 | memcpy(rte->dest_router,dest_router,strlen(dest_router)); |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 958 | //rte->next_hop_face=NO_NEXT_HOP; |
| 959 | struct hashtb_param param_fle = {0}; |
| 960 | rte->face_list=hashtb_create(sizeof(struct face_list_entry), ¶m_fle); |
| 961 | |
| 962 | add_npt_entry(dest_router, dest_router, 0, NULL, NULL); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 963 | } |
| 964 | hashtb_end(e); |
| 965 | |
| 966 | } |
| 967 | |
| 968 | void |
| 969 | add_next_hop_from_lsa_adj_body(char *body, int no_link) |
| 970 | { |
| 971 | |
| 972 | int i=0; |
| 973 | char *lsa_data=(char *)malloc(strlen(body)+1); |
| 974 | memset( lsa_data,0,strlen(body)+1); |
| 975 | memcpy(lsa_data,body,strlen(body)+1); |
| 976 | char *sep="|"; |
| 977 | char *rem; |
| 978 | char *rtr_id; |
| 979 | char *length; |
akmhoque | 866c222 | 2013-02-12 10:49:33 -0600 | [diff] [blame] | 980 | //char *face; |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 981 | char *metric; |
| 982 | |
| 983 | if(no_link >0 ) |
| 984 | { |
| 985 | rtr_id=strtok_r(lsa_data,sep,&rem); |
| 986 | length=strtok_r(NULL,sep,&rem); |
akmhoque | 866c222 | 2013-02-12 10:49:33 -0600 | [diff] [blame] | 987 | //face=strtok_r(NULL,sep,&rem); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 988 | metric=strtok_r(NULL,sep,&rem); |
akmhoque | b28579d | 2013-02-12 11:15:52 -0600 | [diff] [blame] | 989 | if ( !nlsr->debugging && nlsr->debugging) |
| 990 | { |
| 991 | printf("Metric: %s Length:%s\n",metric,length); |
| 992 | } |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 993 | |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 994 | |
| 995 | add_next_hop_router(rtr_id); |
| 996 | |
| 997 | for(i=1;i<no_link;i++) |
| 998 | { |
| 999 | rtr_id=strtok_r(NULL,sep,&rem); |
| 1000 | length=strtok_r(NULL,sep,&rem); |
akmhoque | 866c222 | 2013-02-12 10:49:33 -0600 | [diff] [blame] | 1001 | //face=strtok_r(NULL,sep,&rem); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 1002 | metric=strtok_r(NULL,sep,&rem); |
akmhoque | b28579d | 2013-02-12 11:15:52 -0600 | [diff] [blame] | 1003 | if ( !nlsr->debugging && nlsr->debugging) |
| 1004 | { |
| 1005 | printf("Metric: %s Length:%s\n",metric,length); |
| 1006 | } |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 1007 | |
| 1008 | add_next_hop_router(rtr_id); |
| 1009 | |
| 1010 | } |
| 1011 | } |
| 1012 | |
| 1013 | free(lsa_data); |
| 1014 | |
| 1015 | |
| 1016 | } |
| 1017 | |
| 1018 | void |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 1019 | update_routing_table(char * dest_router,int next_hop_face, int route_cost) |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 1020 | { |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame] | 1021 | if ( nlsr->debugging ) |
| 1022 | { |
| 1023 | printf("update_routing_table called \n"); |
| 1024 | printf("Dest Router: %s Next Hop face: %d Route Cost: %d \n",dest_router,next_hop_face,route_cost); |
| 1025 | } |
| 1026 | |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 1027 | int res,res1; |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 1028 | struct routing_table_entry *rte; |
| 1029 | |
| 1030 | struct hashtb_enumerator ee; |
| 1031 | struct hashtb_enumerator *e = ⅇ |
| 1032 | |
| 1033 | hashtb_start(nlsr->routing_table, e); |
| 1034 | res = hashtb_seek(e, dest_router, strlen(dest_router), 0); |
| 1035 | |
| 1036 | if( res == HT_OLD_ENTRY ) |
| 1037 | { |
| 1038 | rte=e->data; |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 1039 | |
| 1040 | struct hashtb_enumerator eef; |
| 1041 | struct hashtb_enumerator *ef = &eef; |
| 1042 | |
| 1043 | hashtb_start(rte->face_list, ef); |
| 1044 | res1 = hashtb_seek(ef, &next_hop_face, sizeof(next_hop_face), 0); |
| 1045 | if( res1 == HT_NEW_ENTRY) |
| 1046 | { |
akmhoque | d91401d | 2013-03-04 11:05:27 -0600 | [diff] [blame] | 1047 | struct face_list_entry *fle; |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 1048 | fle=ef->data; |
| 1049 | fle->next_hop_face=next_hop_face; |
| 1050 | fle->route_cost=route_cost; |
| 1051 | } |
| 1052 | else if ( res1 == HT_OLD_ENTRY ) |
| 1053 | { |
| 1054 | struct face_list_entry *fle; |
| 1055 | fle=ef->data; |
| 1056 | fle->route_cost=route_cost; |
| 1057 | } |
| 1058 | hashtb_end(ef); |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 1059 | } |
akmhoque | 54a14f4 | 2013-02-24 06:16:20 -0600 | [diff] [blame] | 1060 | else if ( res == HT_NEW_ENTRY ) |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 1061 | { |
| 1062 | hashtb_delete(e); |
| 1063 | } |
| 1064 | |
| 1065 | hashtb_end(e); |
| 1066 | |
| 1067 | } |
| 1068 | |
| 1069 | void |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 1070 | print_routing_table(void) |
| 1071 | { |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 1072 | if ( nlsr->debugging ) |
| 1073 | printf("print_routing_table called\n"); |
| 1074 | if ( nlsr->detailed_logging ) |
| 1075 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"print_routing_table called\n"); |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 1076 | int i,j, rt_element,face_list_element; |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 1077 | |
| 1078 | struct routing_table_entry *rte; |
| 1079 | |
| 1080 | struct hashtb_enumerator ee; |
| 1081 | struct hashtb_enumerator *e = ⅇ |
| 1082 | |
| 1083 | hashtb_start(nlsr->routing_table, e); |
| 1084 | rt_element=hashtb_n(nlsr->routing_table); |
| 1085 | |
| 1086 | for(i=0;i<rt_element;i++) |
| 1087 | { |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 1088 | if ( nlsr->debugging ) |
| 1089 | printf("----------Routing Table Entry %d------------------\n",i+1); |
| 1090 | if ( nlsr->detailed_logging ) |
| 1091 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"----------Routing Table Entry %d------------------\n",i+1); |
| 1092 | |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 1093 | rte=e->data; |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 1094 | |
| 1095 | if ( nlsr->debugging ) |
| 1096 | printf(" Destination Router: %s \n",rte->dest_router); |
| 1097 | if ( nlsr->detailed_logging ) |
| 1098 | writeLogg(__FILE__,__FUNCTION__,__LINE__," Destination Router: %s \n",rte->dest_router); |
| 1099 | |
| 1100 | |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 1101 | //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); |
| 1102 | |
| 1103 | struct face_list_entry *fle; |
| 1104 | |
| 1105 | struct hashtb_enumerator eef; |
| 1106 | struct hashtb_enumerator *ef = &eef; |
| 1107 | |
| 1108 | hashtb_start(rte->face_list, ef); |
| 1109 | face_list_element=hashtb_n(rte->face_list); |
| 1110 | if ( face_list_element <= 0 ) |
| 1111 | { |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 1112 | if ( nlsr->debugging ) |
| 1113 | printf(" Face: No Face \n"); |
| 1114 | if ( nlsr->detailed_logging ) |
| 1115 | writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: No Face \n"); |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 1116 | } |
| 1117 | else |
| 1118 | { |
| 1119 | for(j=0;j<face_list_element;j++) |
| 1120 | { |
| 1121 | fle=ef->data; |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 1122 | if ( nlsr->debugging ) |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame] | 1123 | printf(" Face: %d Route_Cost: %f \n",fle->next_hop_face,fle->route_cost); |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 1124 | if ( nlsr->detailed_logging ) |
| 1125 | 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] | 1126 | hashtb_next(ef); |
| 1127 | } |
| 1128 | } |
| 1129 | hashtb_end(ef); |
| 1130 | |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 1131 | hashtb_next(e); |
| 1132 | } |
| 1133 | |
| 1134 | hashtb_end(e); |
| 1135 | |
akmhoque | 3171d65 | 2012-11-13 11:44:33 -0600 | [diff] [blame] | 1136 | if ( nlsr->debugging ) |
| 1137 | printf("\n"); |
| 1138 | if ( nlsr->detailed_logging ) |
| 1139 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n"); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 1140 | } |
| 1141 | |
akmhoque | 866c222 | 2013-02-12 10:49:33 -0600 | [diff] [blame] | 1142 | /* |
akmhoque | 63152c6 | 2012-09-18 08:43:42 -0500 | [diff] [blame] | 1143 | int |
| 1144 | delete_empty_rte(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags) |
| 1145 | { |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 1146 | if ( nlsr->debugging ) |
| 1147 | { |
| 1148 | printf("delete_empty_rte called\n"); |
| 1149 | printf("Router: %s \n",(char *)ev->evdata); |
| 1150 | } |
| 1151 | if ( nlsr->detailed_logging ) |
| 1152 | { |
| 1153 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_empty_rte called\n"); |
| 1154 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router: %s \n",(char *)ev->evdata); |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 1155 | } |
| 1156 | |
akmhoque | 63152c6 | 2012-09-18 08:43:42 -0500 | [diff] [blame] | 1157 | if(flags == CCN_SCHEDULE_CANCEL) |
| 1158 | { |
| 1159 | return -1; |
| 1160 | } |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame] | 1161 | |
| 1162 | nlsr_lock(); |
akmhoque | 63152c6 | 2012-09-18 08:43:42 -0500 | [diff] [blame] | 1163 | int res; |
| 1164 | struct hashtb_enumerator ee; |
| 1165 | struct hashtb_enumerator *e = ⅇ |
| 1166 | |
| 1167 | hashtb_start(nlsr->routing_table, e); |
| 1168 | res = hashtb_seek(e, (char *)ev->evdata, strlen((char *)ev->evdata), 0); |
| 1169 | |
| 1170 | if ( res == HT_OLD_ENTRY ) |
| 1171 | { |
| 1172 | hashtb_delete(e); |
| 1173 | } |
| 1174 | else if ( res == HT_NEW_ENTRY ) |
| 1175 | { |
| 1176 | hashtb_delete(e); |
| 1177 | } |
| 1178 | |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 1179 | print_routing_table(); |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame] | 1180 | |
| 1181 | nlsr_unlock(); |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 1182 | |
akmhoque | 63152c6 | 2012-09-18 08:43:42 -0500 | [diff] [blame] | 1183 | return 0; |
| 1184 | } |
akmhoque | 866c222 | 2013-02-12 10:49:33 -0600 | [diff] [blame] | 1185 | */ |
akmhoque | 63152c6 | 2012-09-18 08:43:42 -0500 | [diff] [blame] | 1186 | |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 1187 | void |
akmhoque | 3cced64 | 2012-09-24 16:20:20 -0500 | [diff] [blame] | 1188 | clear_old_routing_table(void) |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 1189 | { |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 1190 | if ( nlsr->debugging ) |
| 1191 | printf("clear_old_routing_table called\n"); |
| 1192 | if ( nlsr->detailed_logging ) |
| 1193 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"clear_old_routing_table called\n"); |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 1194 | int i,rt_element; |
| 1195 | |
| 1196 | struct routing_table_entry *rte; |
| 1197 | |
| 1198 | struct hashtb_enumerator ee; |
| 1199 | struct hashtb_enumerator *e = ⅇ |
| 1200 | |
| 1201 | hashtb_start(nlsr->routing_table, e); |
| 1202 | rt_element=hashtb_n(nlsr->routing_table); |
| 1203 | |
| 1204 | for(i=0;i<rt_element;i++) |
| 1205 | { |
| 1206 | rte=e->data; |
| 1207 | hashtb_destroy(&rte->face_list); |
| 1208 | struct hashtb_param param_fle = {0}; |
| 1209 | rte->face_list=hashtb_create(sizeof(struct face_list_entry), ¶m_fle); |
| 1210 | |
| 1211 | hashtb_next(e); |
| 1212 | } |
| 1213 | |
| 1214 | hashtb_end(e); |
| 1215 | } |
| 1216 | |
akmhoque | 63152c6 | 2012-09-18 08:43:42 -0500 | [diff] [blame] | 1217 | |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 1218 | void |
akmhoque | 3cced64 | 2012-09-24 16:20:20 -0500 | [diff] [blame] | 1219 | do_old_routing_table_updates(void) |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 1220 | { |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 1221 | if ( nlsr->debugging ) |
| 1222 | printf("do_old_routing_table_updates called\n"); |
| 1223 | if ( nlsr->detailed_logging ) |
| 1224 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"do_old_routing_table_updates called\n"); |
| 1225 | |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 1226 | int i, rt_element; |
| 1227 | int mapping_no; |
| 1228 | |
| 1229 | struct routing_table_entry *rte; |
| 1230 | |
| 1231 | struct hashtb_enumerator ee; |
| 1232 | struct hashtb_enumerator *e = ⅇ |
| 1233 | |
| 1234 | hashtb_start(nlsr->routing_table, e); |
| 1235 | rt_element=hashtb_n(nlsr->routing_table); |
| 1236 | |
| 1237 | for(i=0;i<rt_element;i++) |
| 1238 | { |
| 1239 | rte=e->data; |
| 1240 | mapping_no=get_mapping_no(rte->dest_router); |
| 1241 | if ( mapping_no == NO_MAPPING_NUM) |
| 1242 | { |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 1243 | delete_orig_router_from_npt(rte->dest_router); |
akmhoque | 866c222 | 2013-02-12 10:49:33 -0600 | [diff] [blame] | 1244 | /* |
akmhoque | 63152c6 | 2012-09-18 08:43:42 -0500 | [diff] [blame] | 1245 | char *router=(char *)malloc(strlen(rte->dest_router)+1); |
| 1246 | memset(router,0,strlen(rte->dest_router)+1); |
| 1247 | memcpy(router,rte->dest_router,strlen(rte->dest_router)+1); |
| 1248 | nlsr->event = ccn_schedule_event(nlsr->sched, 1, &delete_empty_rte, (void *)router , 0); |
akmhoque | 866c222 | 2013-02-12 10:49:33 -0600 | [diff] [blame] | 1249 | */ |
akmhoque | 54a14f4 | 2013-02-24 06:16:20 -0600 | [diff] [blame] | 1250 | destroy_routing_table_entry_comp(rte); |
akmhoque | 866c222 | 2013-02-12 10:49:33 -0600 | [diff] [blame] | 1251 | hashtb_delete(e); |
| 1252 | i++; |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 1253 | } |
akmhoque | 866c222 | 2013-02-12 10:49:33 -0600 | [diff] [blame] | 1254 | else |
| 1255 | { |
| 1256 | hashtb_next(e); |
| 1257 | } |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 1258 | } |
| 1259 | |
| 1260 | hashtb_end(e); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 1261 | } |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 1262 | |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame] | 1263 | void |
| 1264 | update_routing_table_with_new_hyperbolic_route(long int dest_router_rev_map_index, long int face, double nbr_to_dest_dist) |
| 1265 | { |
| 1266 | if ( nlsr->debugging ) |
| 1267 | printf("update_routing_table_with_new_hyperbolic_route called\n"); |
| 1268 | if ( nlsr->detailed_logging ) |
| 1269 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"update_routing_table_with_new_hyperbolic_route called\n"); |
| 1270 | |
| 1271 | char *orig_router=get_router_from_rev_map(dest_router_rev_map_index); |
| 1272 | |
| 1273 | if (face != NO_NEXT_HOP && face != NO_FACE ) |
| 1274 | { |
| 1275 | update_routing_table(orig_router,face,nbr_to_dest_dist); |
| 1276 | if ( nlsr->debugging ) |
| 1277 | printf ("Orig_router: %s Next Hop Face: %ld \n",orig_router,face); |
| 1278 | if ( nlsr->detailed_logging ) |
| 1279 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"Orig_router: %s Next Hop Face: %ld \n",orig_router,face); |
| 1280 | |
| 1281 | } |
| 1282 | |
| 1283 | |
| 1284 | } |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 1285 | |
| 1286 | |
akmhoque | fbfd098 | 2012-09-09 20:59:03 -0500 | [diff] [blame] | 1287 | void |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 1288 | 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] | 1289 | { |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 1290 | if ( nlsr->debugging ) |
| 1291 | printf("update_routing_table_with_new_route called\n"); |
| 1292 | if ( nlsr->detailed_logging ) |
| 1293 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"update_routing_table_with_new_route called\n"); |
akmhoque | fbfd098 | 2012-09-09 20:59:03 -0500 | [diff] [blame] | 1294 | int i, map_element; |
| 1295 | struct map_entry *me; |
| 1296 | |
| 1297 | struct hashtb_enumerator ee; |
| 1298 | struct hashtb_enumerator *e = ⅇ |
| 1299 | |
| 1300 | hashtb_start(nlsr->rev_map, e); |
| 1301 | map_element=hashtb_n(nlsr->rev_map); |
| 1302 | |
| 1303 | for(i=0;i<map_element;i++) |
| 1304 | { |
| 1305 | me=e->data; |
| 1306 | if(me->mapping != source) |
| 1307 | { |
| 1308 | |
| 1309 | char *orig_router=get_router_from_rev_map(me->mapping); |
| 1310 | if (orig_router != NULL ) |
| 1311 | { |
| 1312 | int next_hop_router_num=get_next_hop_from_calculation(parent,me->mapping,source); |
akmhoque | fbfd098 | 2012-09-09 20:59:03 -0500 | [diff] [blame] | 1313 | if ( next_hop_router_num == NO_NEXT_HOP ) |
| 1314 | { |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 1315 | if ( nlsr->debugging ) |
| 1316 | printf ("Orig_router: %s Next Hop Face: %d \n",orig_router,NO_FACE); |
| 1317 | if ( nlsr->detailed_logging ) |
| 1318 | 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] | 1319 | } |
| 1320 | else |
| 1321 | { |
| 1322 | char *next_hop_router=get_router_from_rev_map(next_hop_router_num); |
akmhoque | fbfd098 | 2012-09-09 20:59:03 -0500 | [diff] [blame] | 1323 | int next_hop_face=get_next_hop_face_from_adl(next_hop_router); |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 1324 | update_routing_table(orig_router,next_hop_face,dist[me->mapping]); |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 1325 | if ( nlsr->debugging ) |
| 1326 | printf ("Orig_router: %s Next Hop Face: %d \n",orig_router,next_hop_face); |
| 1327 | if ( nlsr->detailed_logging ) |
| 1328 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"Orig_router: %s Next Hop Face: %d \n",orig_router,next_hop_face); |
| 1329 | |
akmhoque | fbfd098 | 2012-09-09 20:59:03 -0500 | [diff] [blame] | 1330 | |
| 1331 | } |
| 1332 | } |
| 1333 | } |
| 1334 | hashtb_next(e); |
| 1335 | } |
| 1336 | |
| 1337 | hashtb_end(e); |
| 1338 | } |
| 1339 | |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 1340 | int |
| 1341 | does_face_exist_for_router(char *dest_router, int face_id) |
| 1342 | { |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame] | 1343 | if (nlsr->debugging) |
| 1344 | { |
| 1345 | printf("does_face_exist_for_router called\n"); |
| 1346 | printf("Dest Router: %s and Face id: %d \n",dest_router, face_id); |
| 1347 | } |
| 1348 | |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 1349 | int ret=0; |
| 1350 | |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame] | 1351 | int res; |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 1352 | struct routing_table_entry *rte; |
| 1353 | |
| 1354 | struct hashtb_enumerator ee; |
| 1355 | struct hashtb_enumerator *e = ⅇ |
| 1356 | |
| 1357 | hashtb_start(nlsr->routing_table, e); |
| 1358 | res = hashtb_seek(e, dest_router, strlen(dest_router), 0); |
| 1359 | |
| 1360 | if( res == HT_OLD_ENTRY ) |
| 1361 | { |
| 1362 | rte=e->data; |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame] | 1363 | unsigned *v; |
| 1364 | v = hashtb_lookup(rte->face_list, &face_id, sizeof(face_id)); |
| 1365 | if (v != NULL) |
| 1366 | ret = 1; |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 1367 | } |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame] | 1368 | else |
akmhoque | de61ba9 | 2012-09-20 22:19:12 -0500 | [diff] [blame] | 1369 | { |
| 1370 | hashtb_delete(e); |
| 1371 | } |
| 1372 | |
| 1373 | hashtb_end(e); |
| 1374 | |
| 1375 | return ret; |
| 1376 | } |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame] | 1377 | |
| 1378 | double |
| 1379 | get_hyperbolic_distance(long int source, long int dest) |
| 1380 | { |
| 1381 | double distance; |
| 1382 | char *src_router=get_router_from_rev_map(source); |
| 1383 | char *dest_router=get_router_from_rev_map(dest); |
| 1384 | |
| 1385 | double src_r=get_hyperbolic_r(src_router); |
| 1386 | double src_theta=get_hyperbolic_r(src_router); |
| 1387 | |
| 1388 | double dest_r=get_hyperbolic_r(dest_router); |
| 1389 | double dest_theta=get_hyperbolic_r(dest_router); |
| 1390 | if ( src_r != -1 && dest_r != -1 ) |
| 1391 | { |
| 1392 | distance=acosh(cosh(src_r)*cosh(dest_r)-sinh(src_r)*sinh(dest_r)*cos(src_theta-dest_theta)); |
| 1393 | } |
| 1394 | else |
| 1395 | { |
| 1396 | distance= -1.0; |
| 1397 | } |
| 1398 | |
| 1399 | return distance; |
| 1400 | } |
akmhoque | 54a14f4 | 2013-02-24 06:16:20 -0600 | [diff] [blame] | 1401 | |
| 1402 | void |
| 1403 | destroy_routing_table_entry_comp(struct routing_table_entry *rte) |
| 1404 | { |
| 1405 | |
| 1406 | free(rte->dest_router); |
| 1407 | hashtb_destroy(&rte->face_list); |
| 1408 | |
| 1409 | } |
| 1410 | |
| 1411 | void |
| 1412 | destroy_routing_table_entry(struct routing_table_entry *rte) |
| 1413 | { |
| 1414 | |
| 1415 | destroy_routing_table_entry_comp(rte); |
| 1416 | free(rte); |
| 1417 | } |
| 1418 | |
| 1419 | void |
| 1420 | destroy_routing_table(void) |
| 1421 | { |
| 1422 | int i, rt_element; |
| 1423 | struct hashtb_enumerator ee; |
| 1424 | struct hashtb_enumerator *e = ⅇ |
| 1425 | |
| 1426 | struct routing_table_entry *rte; |
| 1427 | hashtb_start(nlsr->routing_table, e); |
| 1428 | rt_element=hashtb_n(nlsr->routing_table); |
| 1429 | for(i=0;i<rt_element;i++) |
| 1430 | { |
| 1431 | rte=e->data; |
| 1432 | free(rte->dest_router); |
| 1433 | hashtb_destroy(&rte->face_list); |
| 1434 | hashtb_next(e); |
| 1435 | } |
| 1436 | hashtb_end(e); |
| 1437 | hashtb_destroy(&nlsr->routing_table); |
| 1438 | |
| 1439 | } |
| 1440 | |
| 1441 | void |
| 1442 | destroy_map(void) |
| 1443 | { |
| 1444 | |
| 1445 | int i, map_element; |
| 1446 | struct map_entry *me; |
| 1447 | |
| 1448 | struct hashtb_enumerator ee; |
| 1449 | struct hashtb_enumerator *e = ⅇ |
| 1450 | |
| 1451 | hashtb_start(nlsr->map, e); |
| 1452 | map_element=hashtb_n(nlsr->map); |
| 1453 | |
| 1454 | for(i=0;i<map_element;i++) |
| 1455 | { |
| 1456 | me=e->data; |
| 1457 | free(me->router); |
| 1458 | hashtb_next(e); |
| 1459 | } |
| 1460 | |
| 1461 | hashtb_end(e); |
| 1462 | |
akmhoque | ae38886 | 2013-03-15 01:46:22 -0500 | [diff] [blame] | 1463 | hashtb_destroy(&nlsr->map); |
| 1464 | |
akmhoque | 54a14f4 | 2013-02-24 06:16:20 -0600 | [diff] [blame] | 1465 | } |
| 1466 | |
| 1467 | void |
| 1468 | destroy_rev_map(void) |
| 1469 | { |
| 1470 | int i, map_element; |
| 1471 | struct map_entry *me; |
| 1472 | |
| 1473 | struct hashtb_enumerator ee; |
| 1474 | struct hashtb_enumerator *e = ⅇ |
| 1475 | |
| 1476 | hashtb_start(nlsr->rev_map, e); |
| 1477 | map_element=hashtb_n(nlsr->rev_map); |
| 1478 | |
| 1479 | for(i=0;i<map_element;i++) |
| 1480 | { |
| 1481 | me=e->data; |
| 1482 | free(me->router); |
| 1483 | hashtb_next(e); |
| 1484 | } |
| 1485 | |
| 1486 | hashtb_end(e); |
akmhoque | ae38886 | 2013-03-15 01:46:22 -0500 | [diff] [blame] | 1487 | |
| 1488 | hashtb_destroy(&nlsr->rev_map); |
akmhoque | 54a14f4 | 2013-02-24 06:16:20 -0600 | [diff] [blame] | 1489 | } |