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