blob: a9865239e18ae142f2ee9826c853d378586bfc44 [file] [log] [blame]
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <sys/time.h>
#include <assert.h>
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <sys/types.h>
#include <signal.h>
#include <ccn/ccn.h>
#include <ccn/uri.h>
#include <ccn/keystore.h>
#include <ccn/signing.h>
#include <ccn/schedule.h>
#include <ccn/hashtb.h>
#include "nlsr.h"
#include "nlsr_npt.h"
#include "nlsr_fib.h"
#include "nlsr_route.h"
#include "nlsr_adl.h"
#include "utility.h"
int
add_npt_entry(char *orig_router, char *name_prefix, int num_face, int *faces, int *route_costs)
{
if ( strcmp(orig_router,nlsr->router_name)== 0)
{
return -1;
}
struct npt_entry *ne;//=(struct npt_entry*)calloc(1,sizeof(struct npt_entry ));
int res,res_nle,res_fle;
struct hashtb_enumerator ee;
struct hashtb_enumerator *e = &ee;
hashtb_start(nlsr->npt, e);
res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
if(res == HT_NEW_ENTRY)
{
ne=e->data;
ne->orig_router=(char *)calloc(strlen(orig_router)+1,sizeof(char));
memcpy(ne->orig_router,orig_router,strlen(orig_router)+1);
//struct hashtb_param param_nle = {0};
ne->name_list= hashtb_create(sizeof(struct name_list_entry ),NULL);
struct hashtb_enumerator eenle;
struct hashtb_enumerator *enle = &eenle;
hashtb_start(ne->name_list, enle);
res_nle = hashtb_seek(enle, name_prefix, strlen(name_prefix), 0);
if(res_nle == HT_NEW_ENTRY )
{
struct name_list_entry *nle;//=(struct name_list_entry *)calloc(1,sizeof(struct name_list_entry));
nle=enle->data;
nle->name=(char *)calloc(strlen(name_prefix)+1,sizeof(char));
//memset(nle->name,0,strlen(name_prefix)+1);
memcpy(nle->name,name_prefix,strlen(name_prefix)+1);
}
hashtb_end(enle);
//struct hashtb_param param_fle = {0};
ne->face_list=hashtb_create(sizeof(struct face_list_entry), NULL);
if ( num_face > 0 )
{
struct hashtb_enumerator eef;
struct hashtb_enumerator *ef = &eef;
hashtb_start(ne->face_list, ef);
int i;
for ( i=0; i < num_face ; i++)
{
int face=faces[i];
if ( face != NO_FACE && face != ZERO_FACE)
{
res_fle = hashtb_seek(ef, &face, sizeof(face), 0);
if ( res_fle == HT_NEW_ENTRY )
{
struct face_list_entry *fle;//=(struct face_list_entry *)calloc(1,sizeof(struct face_list_entry));
fle=ef->data;
fle->next_hop_face=face;
fle->route_cost=route_costs[i];
}
}
}
hashtb_end(ef);
}
}
else if (res == HT_OLD_ENTRY)
{
struct npt_entry *one;
one=e->data;
struct hashtb_enumerator eenle;
struct hashtb_enumerator *enle = &eenle;
hashtb_start(one->name_list, enle);
res_nle = hashtb_seek(enle, name_prefix, strlen(name_prefix), 0);
if(res_nle == HT_NEW_ENTRY )
{
struct name_list_entry *nle;//=(struct name_list_entry *)calloc(1,sizeof(struct name_list_entry));
nle=enle->data;
nle->name=(char *)malloc(strlen(name_prefix)+1);
memset(nle->name,0,strlen(name_prefix)+1);
memcpy(nle->name,name_prefix,strlen(name_prefix));
}
else if(res_nle == HT_OLD_ENTRY )
{
}
hashtb_end(enle);
if ( num_face > 0 )
{
struct hashtb_enumerator eef;
struct hashtb_enumerator *ef = &eef;
hashtb_start(one->face_list, ef);
int i;
for ( i=0; i< num_face ; i ++)
{
int face=faces[i];
if ( face != NO_FACE && face != ZERO_FACE)
{
res_fle = hashtb_seek(ef, &face, sizeof(face), 0);
if ( res_fle == HT_NEW_ENTRY )
{
struct face_list_entry *fle;//=(struct face_list_entry *)calloc(1,sizeof(struct face_list_entry));
fle=ef->data;
fle->next_hop_face=face;
fle->route_cost=route_costs[i];
}
}
}
hashtb_end(ef);
}
}
hashtb_end(e);
update_ccnd_fib_for_orig_router(orig_router);
return res;
}
void
update_ccnd_fib_for_orig_router(char *orig_router)
{
if ( nlsr->debugging )
{
printf("update_ccnd_fib_for_orig_router called\n");
}
int res;
struct hashtb_enumerator ee;
struct hashtb_enumerator *e = &ee;
hashtb_start(nlsr->npt, e);
res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
if(res == HT_NEW_ENTRY)
{
hashtb_delete(e);
}
else if ( res == HT_OLD_ENTRY )
{
struct npt_entry *ne;
ne=e->data;
int num_face=hashtb_n(ne->face_list);
int last_face,first_face;
int *faces=(int *)malloc(num_face*sizeof(int));
int *route_costs=(int *)malloc(num_face*sizeof(int));
get_all_faces_for_orig_router_from_npt(orig_router,faces,route_costs,num_face);
sort_faces_by_distance(faces,route_costs,0,num_face);
if ( nlsr->debugging )
{
int m;
for ( m =0 ; m< num_face ; m++)
{
printf("Dest_router: %s Next_Hop_Face: %d Route_cost: %d \n",orig_router,faces[m],route_costs[m]);
}
}
last_face=0;
if ( nlsr->max_faces_per_prefix == 0) // add all faces available in routing table
{
first_face=num_face-1;
}
else if( nlsr->max_faces_per_prefix > 0)
{
if ( nlsr->max_faces_per_prefix >= num_face)
{
first_face=num_face-1;
}
else if ( nlsr->max_faces_per_prefix < num_face)
{
first_face=nlsr->max_faces_per_prefix-1;
}
}
int i,j, nl_element;
struct name_list_entry *nle;
struct hashtb_enumerator eenle;
struct hashtb_enumerator *enle = &eenle;
hashtb_start(ne->name_list, enle);
nl_element=hashtb_n(ne->name_list);
for (i=0;i<nl_element;i++)
{
nle=enle->data;
for( j=num_face-1; j>= 0; j--)
{
if ( !( is_neighbor(nle->name) == 1 && get_next_hop_face_from_adl(nle->name) == faces[j] ) )
{
add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j],nlsr->router_dead_interval);
}
}
hashtb_next(enle);
}
hashtb_end(enle);
if ( nlsr->debugging )
{
printf("First Face Index: %d Last Face Index: %d\n",first_face,last_face);
}
hashtb_start(ne->name_list, enle);
nl_element=hashtb_n(ne->name_list);
for (i=0;i<nl_element;i++)
{
nle=enle->data;
for( j=first_face; j>= last_face; j--)
{
if ( nlsr->debugging )
{
printf("Possible FIB Entry name: %s face: %d route cost: %d \n",nle->name,faces[j],route_costs[j]);
}
if ( is_active_neighbor(orig_router) == 0 )
{
if ( nlsr->debugging )
printf("Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
if ( nlsr->detailed_logging )
writeLogg(__FILE__,__FUNCTION__,__LINE__,"Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_REG, faces[j],nlsr->router_dead_interval);
}
else
{
if ( j == last_face && is_active_neighbor(nle->name)==0)
{
if ( nlsr->debugging )
printf("Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
if ( nlsr->detailed_logging )
writeLogg(__FILE__,__FUNCTION__,__LINE__,"Adding face: Name:%s Face: %d\n",nle->name,faces[j]);
add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_REG, faces[j],nlsr->router_dead_interval);
}
}
}
hashtb_next(enle);
}
hashtb_end(enle);
free(faces);
free(route_costs);
}
hashtb_end(e);
}
int
delete_npt_entry_by_router_and_name_prefix(char *orig_router, char *name_prefix)
{
if ( strcmp(orig_router,nlsr->router_name)== 0)
{
return -1;
}
struct npt_entry *ne;
int res,res_nle;
struct hashtb_enumerator ee;
struct hashtb_enumerator *e = &ee;
hashtb_start(nlsr->npt, e);
res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
if(res == HT_NEW_ENTRY)
{
hashtb_delete(e);
return -1;
}
else if (res == HT_OLD_ENTRY)
{
ne=e->data;
struct hashtb_enumerator eenle;
struct hashtb_enumerator *enle = &eenle;
hashtb_start(ne->name_list, enle);
res_nle = hashtb_seek(enle, name_prefix, strlen(name_prefix), 0);
if(res_nle == HT_NEW_ENTRY )
{
hashtb_delete(enle);
}
else if(res_nle == HT_OLD_ENTRY )
{
struct name_list_entry *nle;
nle=enle->data;
int j;
int num_face=hashtb_n(ne->face_list);
int last_face,first_face;
int *faces=(int *)malloc(num_face*sizeof(int));
int *route_costs=(int *)malloc(num_face*sizeof(int));
get_all_faces_for_orig_router_from_npt(orig_router,faces,route_costs,num_face);
sort_faces_by_distance(faces,route_costs,0,num_face);
last_face=0;
if ( nlsr->max_faces_per_prefix == 0) // add all faces available in routing table
{
first_face=num_face-1;
}
else if( nlsr->max_faces_per_prefix > 0)
{
if ( nlsr->max_faces_per_prefix >= num_face)
{
first_face=num_face-1;
}
else if ( nlsr->max_faces_per_prefix < num_face)
{
first_face=nlsr->max_faces_per_prefix-1;
}
}
for( j=first_face; j>= last_face; j--)
{
if ( is_active_neighbor(orig_router) == 0 )
{
if ( nlsr->debugging )
printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
if ( nlsr->detailed_logging )
writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j],nlsr->router_dead_interval);
}
else
{
if ( j == last_face && is_active_neighbor(nle->name)==0)
{
if ( nlsr->debugging )
printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
if ( nlsr->detailed_logging )
writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j],nlsr->router_dead_interval);
}
}
}
}
hashtb_end(enle);
}
hashtb_end(e);
return 0;
}
void
print_npt(void)
{
if ( nlsr->debugging )
{
printf("\n");
printf("print_npt called\n");
}
if ( nlsr->detailed_logging )
{
writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
writeLogg(__FILE__,__FUNCTION__,__LINE__,"print_npt called\n");
}
int i, npt_element;
struct npt_entry *ne;
struct hashtb_enumerator ee;
struct hashtb_enumerator *e = &ee;
hashtb_start(nlsr->npt, e);
npt_element=hashtb_n(nlsr->npt);
for(i=0;i<npt_element;i++)
{
if ( nlsr->debugging )
{
printf("\n");
printf("----------NPT ENTRY %d------------------\n",i+1);
}
if ( nlsr->detailed_logging )
{
writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
writeLogg(__FILE__,__FUNCTION__,__LINE__,"----------NPT ENTRY %d------------------\n",i+1);
}
ne=e->data;
if ( nlsr->debugging )
printf(" Origination Router: %s \n",ne->orig_router);
if ( nlsr->detailed_logging )
writeLogg(__FILE__,__FUNCTION__,__LINE__," Origination Router: %s \n",ne->orig_router);
int j, nl_element,face_list_element;
struct name_list_entry *nle;
struct hashtb_enumerator eenle;
struct hashtb_enumerator *enle = &eenle;
hashtb_start(ne->name_list, enle);
nl_element=hashtb_n(ne->name_list);
for (j=0;j<nl_element;j++)
{
nle=enle->data;
if ( nlsr->debugging )
printf(" Name Prefix: %s \n",nle->name);
if ( nlsr->detailed_logging )
writeLogg(__FILE__,__FUNCTION__,__LINE__," Name Prefix: %s \n",nle->name);
hashtb_next(enle);
}
hashtb_end(enle);
struct face_list_entry *fle;
struct hashtb_enumerator eef;
struct hashtb_enumerator *ef = &eef;
hashtb_start(ne->face_list, ef);
face_list_element=hashtb_n(ne->face_list);
if ( face_list_element <= 0 )
{
if ( nlsr->debugging )
printf(" Face: No Face \n");
if ( nlsr->detailed_logging )
writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: No Face \n");
}
else
{
for(j=0;j<face_list_element;j++)
{
fle=ef->data;
if ( nlsr->debugging )
printf(" Face: %d Route_Cost: %f \n",fle->next_hop_face,fle->route_cost);
if ( nlsr->detailed_logging )
writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
hashtb_next(ef);
}
}
hashtb_end(ef);
hashtb_next(e);
}
hashtb_end(e);
if ( nlsr->debugging )
printf("\n");
if ( nlsr->detailed_logging )
writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
}
void
delete_orig_router_from_npt(char *orig_router)
{
int res,num_face,num_prefix;
struct npt_entry *ne;
struct hashtb_enumerator ee;
struct hashtb_enumerator *e = &ee;
hashtb_start(nlsr->npt, e);
res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
if ( res == HT_OLD_ENTRY )
{
ne=e->data;
num_prefix=hashtb_n(ne->name_list);
if ( num_prefix > 0 )
{
num_face=hashtb_n(ne->face_list);
if ( num_face > 0 )
{
int j, nl_element;
struct name_list_entry *nle;
struct hashtb_enumerator eenle;
struct hashtb_enumerator *enle = &eenle;
hashtb_start(ne->name_list, enle);
nl_element=hashtb_n(ne->name_list);
for (j=0;j<nl_element;j++)
{
nle=enle->data;
delete_npt_entry_by_router_and_name_prefix(ne->orig_router,nle->name);
hashtb_next(enle);
}
hashtb_end(enle);
}
}
//hashtb_destroy(&ne->name_list);
//hashtb_destroy(&ne->face_list);
free(ne->orig_router);
destroy_name_list(ne->name_list);
destroy_face_list(ne->face_list);
hashtb_delete(e);
}
else if ( res == HT_NEW_ENTRY )
{
hashtb_delete(e);
}
hashtb_end(e);
}
void
add_face_to_npt_by_face_id(char *dest_router, int face_id, int route_cost)
{
if ( nlsr->debugging )
{
printf("add_face_to_npt_by_face_id called\n");
printf("Dest Router: %s Face: %d Route_cost: %d \n",dest_router, face_id, route_cost);
}
if ( nlsr->detailed_logging )
{
writeLogg(__FILE__,__FUNCTION__,__LINE__,"add_face_to_npt_by_face_id called\n");
writeLogg(__FILE__,__FUNCTION__,__LINE__,"Dest Router: %s Face: %d Route_cost: %d \n",dest_router, face_id, route_cost);
}
int res,res1;
struct npt_entry *ne;
struct hashtb_enumerator ee;
struct hashtb_enumerator *e = &ee;
hashtb_start(nlsr->npt, e);
res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
if ( res == HT_OLD_ENTRY )
{
if ( nlsr->debugging )
printf("Dest Router Found \n");
if ( nlsr->detailed_logging )
writeLogg(__FILE__,__FUNCTION__,__LINE__,"Dest Router Found \n");
ne=e->data;
struct hashtb_enumerator eef;
struct hashtb_enumerator *ef = &eef;
hashtb_start(ne->face_list, ef);
res1=hashtb_seek(ef, &face_id, sizeof(face_id), 0);
if ( res1 == HT_OLD_ENTRY )
{
if ( nlsr->debugging )
printf("Face Found \n");
if ( nlsr->detailed_logging )
writeLogg(__FILE__,__FUNCTION__,__LINE__,"Face Found \n");
struct face_list_entry *fle;
fle=ef->data;
fle->next_hop_face=face_id;
fle->route_cost=route_cost;
}
else if ( res1 == HT_NEW_ENTRY )
{
if ( nlsr->debugging )
printf("Face Not Found \n");
if ( nlsr->detailed_logging )
writeLogg(__FILE__,__FUNCTION__,__LINE__,"Face Not Found \n");
struct face_list_entry *fle;//=(struct face_list_entry *)malloc(sizeof(struct face_list_entry));
fle=ef->data;
fle->next_hop_face=face_id;
fle->route_cost=route_cost;
}
hashtb_end(ef);
}
else if (res == HT_NEW_ENTRY)
{
hashtb_delete(e);
}
hashtb_end(e);
}
void
add_new_fib_entries_to_npt(void)
{
if ( nlsr->debugging )
printf("add_new_fib_entries_to_npt called\n");
if ( nlsr->detailed_logging )
writeLogg(__FILE__,__FUNCTION__,__LINE__,"add_new_fib_entries_to_npt called\n");
int i,j, rt_element,face_list_element;
struct routing_table_entry *rte;
struct hashtb_enumerator ee;
struct hashtb_enumerator *e = &ee;
hashtb_start(nlsr->routing_table, e);
rt_element=hashtb_n(nlsr->routing_table);
for(i=0;i<rt_element;i++)
{
rte=e->data;
struct face_list_entry *fle;
struct hashtb_enumerator eef;
struct hashtb_enumerator *ef = &eef;
hashtb_start(rte->face_list, ef);
face_list_element=hashtb_n(rte->face_list);
if ( face_list_element <= 0 )
{
if ( nlsr->debugging )
printf(" Face: No Face \n");
if ( nlsr->detailed_logging )
writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: No Face \n");
}
else
{
for(j=0;j<face_list_element;j++)
{
fle=ef->data;
add_face_to_npt_by_face_id(rte->dest_router,fle->next_hop_face,fle->route_cost);
hashtb_next(ef);
}
}
hashtb_end(ef);
hashtb_next(e);
}
hashtb_end(e);
}
void
delete_face_from_npt_by_face_id(char *dest_router, int face_id)
{
if ( nlsr->debugging )
printf("delete_face_from_npt_by_face_id\n");
if ( nlsr->detailed_logging )
writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_face_from_npt_by_face_id\n");
int res,res1;
struct npt_entry *ne;
struct hashtb_enumerator ee;
struct hashtb_enumerator *e = &ee;
hashtb_start(nlsr->npt, e);
res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
if ( res == HT_OLD_ENTRY )
{
ne=e->data;
struct hashtb_enumerator eef;
struct hashtb_enumerator *ef = &eef;
hashtb_start(ne->face_list, ef);
res1=hashtb_seek(ef, &face_id, sizeof(face_id), 0);
if ( res1 == HT_OLD_ENTRY )
{
hashtb_delete(ef);
}
else if ( res1 == HT_NEW_ENTRY )
{
hashtb_delete(ef);
}
hashtb_end(ef);
}
else if (res == HT_NEW_ENTRY)
{
hashtb_delete(e);
}
hashtb_end(e);
}
void
clean_old_fib_entries_from_npt(void)
{
if ( nlsr->debugging )
printf("clean_old_fib_entries_from_npt called\n\n");
if ( nlsr->detailed_logging )
writeLogg(__FILE__,__FUNCTION__,__LINE__,"clean_old_fib_entries_from_npt called\n\n");
int i, npt_element;
struct npt_entry *ne;
struct hashtb_enumerator ee;
struct hashtb_enumerator *e = &ee;
hashtb_start(nlsr->npt, e);
npt_element=hashtb_n(nlsr->npt);
for(i=0;i<npt_element;i++)
{
ne=e->data;
int j,k, nl_element,face_list_element;
struct face_list_entry *fle;
struct hashtb_enumerator eef;
struct hashtb_enumerator *ef = &eef;
hashtb_start(ne->face_list, ef);
face_list_element=hashtb_n(ne->face_list);
if ( face_list_element <= 0 )
{
if ( nlsr->debugging )
printf(" Face: No Face \n");
if ( nlsr->detailed_logging )
writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: No Face \n");
}
else
{
for(j=0;j<face_list_element;j++)
{
fle=ef->data;
int check=does_face_exist_for_router(ne->orig_router,fle->next_hop_face);
if ( check == 0 )
{
struct name_list_entry *nle;
struct hashtb_enumerator eenle;
struct hashtb_enumerator *enle = &eenle;
hashtb_start(ne->name_list, enle);
nl_element=hashtb_n(ne->name_list);
for (k=0;k<nl_element;k++)
{
nle=enle->data;
if( is_neighbor(nle->name) == 0 )
{
if ( nlsr->debugging )
printf("Deleting face: Name:%s Face: %d\n",nle->name,fle->next_hop_face);
if ( nlsr->detailed_logging )
writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,fle->next_hop_face);
add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, fle->next_hop_face,nlsr->router_dead_interval);
}
hashtb_next(enle);
}
hashtb_end(enle);
hashtb_delete(ef);
j++;
}
else
{
struct name_list_entry *nle;
struct hashtb_enumerator eenle;
struct hashtb_enumerator *enle = &eenle;
hashtb_start(ne->name_list, enle);
nl_element=hashtb_n(ne->name_list);
for (k=0;k<nl_element;k++)
{
nle=enle->data;
if( is_active_neighbor(ne->orig_router) && get_next_hop_face_from_adl( ne->orig_router ) != fle->next_hop_face )
{
if ( nlsr->debugging )
printf("Deleting face: Name:%s Face: %d\n",nle->name,fle->next_hop_face);
if ( nlsr->detailed_logging )
writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,fle->next_hop_face);
add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, fle->next_hop_face,nlsr->router_dead_interval);
}
hashtb_next(enle);
}
hashtb_end(enle);
hashtb_next(ef);
}
}
}
hashtb_end(ef);
hashtb_next(e);
}
hashtb_end(e);
}
void
update_npt_with_new_route(void)
{
if ( nlsr->debugging )
printf("update_npt_with_new_route called\n");
clean_old_fib_entries_from_npt();
print_npt();
add_new_fib_entries_to_npt();
print_npt();
int i, npt_element;
struct npt_entry *ne;
struct hashtb_enumerator ee;
struct hashtb_enumerator *e = &ee;
hashtb_start(nlsr->npt, e);
npt_element=hashtb_n(nlsr->npt);
for(i=0;i<npt_element;i++)
{
ne=e->data;
update_ccnd_fib_for_orig_router(ne->orig_router);
hashtb_next(e);
}
hashtb_end(e);
}
void
sort_faces_by_distance(int *faces,int *route_costs,int start,int element)
{
int i,j;
int temp_cost;
int temp_face;
for ( i=start ; i < element ; i ++)
{
for( j=i+1; j<element; j ++)
{
if (route_costs[j] < route_costs[i] )
{
temp_cost=route_costs[j];
route_costs[j]=route_costs[i];
route_costs[i]=temp_cost;
temp_face=faces[j];
faces[j]=faces[i];
faces[i]=temp_face;
}
}
}
}
void
get_all_faces_for_orig_router_from_npt(char *orig_router, int *faces, int *route_costs, int num_faces)
{
int res,face_list_element,j;
struct hashtb_enumerator ee;
struct hashtb_enumerator *e = &ee;
hashtb_start(nlsr->npt, e);
res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
if(res == HT_NEW_ENTRY)
{
hashtb_delete(e);
}
else if ( res == HT_OLD_ENTRY )
{
struct npt_entry *ne;
ne=e->data;
struct face_list_entry *fle;
struct hashtb_enumerator eef;
struct hashtb_enumerator *ef = &eef;
hashtb_start(ne->face_list, ef);
face_list_element=hashtb_n(ne->face_list);
for(j=0;j<face_list_element;j++)
{
fle=ef->data;
faces[j]=fle->next_hop_face;
route_costs[j]=fle->route_cost;
hashtb_next(ef);
}
hashtb_end(ef);
}
hashtb_end(e);
}
void
destroy_faces_by_orig_router(char *orig_router)
{
int res;
struct hashtb_enumerator ee;
struct hashtb_enumerator *e = &ee;
hashtb_start(nlsr->npt, e);
res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
if(res == HT_NEW_ENTRY)
{
hashtb_delete(e);
}
else if ( res == HT_OLD_ENTRY )
{
struct npt_entry *ne;
ne=e->data;
int num_face=hashtb_n(ne->face_list);
int last_face,first_face;
int *faces=(int *)malloc(num_face*sizeof(int));
int *route_costs=(int *)malloc(num_face*sizeof(int));
get_all_faces_for_orig_router_from_npt(orig_router,faces,route_costs,num_face);
sort_faces_by_distance(faces,route_costs,0,num_face);
last_face=0;
if ( nlsr->max_faces_per_prefix == 0)
{
first_face=num_face-1;
}
else if( nlsr->max_faces_per_prefix > 0)
{
if ( nlsr->max_faces_per_prefix >= num_face)
{
first_face=num_face-1;
}
else if ( nlsr->max_faces_per_prefix < num_face)
{
first_face=nlsr->max_faces_per_prefix-1;
}
}
int i,j, nl_element;
struct name_list_entry *nle;
struct hashtb_enumerator eenle;
struct hashtb_enumerator *enle = &eenle;
hashtb_start(ne->name_list, enle);
nl_element=hashtb_n(ne->name_list);
for (i=0;i<nl_element;i++)
{
nle=enle->data;
for( j=first_face; j>= last_face; j--)
{
if ( is_active_neighbor(orig_router) == 0 )
{
if ( nlsr->debugging )
printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
if ( nlsr->detailed_logging )
writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j],nlsr->router_dead_interval);
}
else
{
if ( j == last_face && is_active_neighbor(nle->name)==0)
{
if ( nlsr->debugging )
printf("Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
if ( nlsr->detailed_logging )
writeLogg(__FILE__,__FUNCTION__,__LINE__,"Deleting face: Name:%s Face: %d\n",nle->name,faces[j]);
add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG, faces[j],nlsr->router_dead_interval);
}
}
}
hashtb_next(enle);
}
hashtb_end(enle);
free(faces);
free(route_costs);
}
hashtb_end(e);
}
void
destroy_all_face_by_nlsr(void)
{
int i, npt_element;
struct npt_entry *ne;
struct hashtb_enumerator ee;
struct hashtb_enumerator *e = &ee;
hashtb_start(nlsr->npt, e);
npt_element=hashtb_n(nlsr->npt);
for(i=0;i<npt_element;i++)
{
ne=e->data;
destroy_faces_by_orig_router(ne->orig_router);
hashtb_next(e);
}
hashtb_end(e);
if ( nlsr->debugging )
printf("\n");
if ( nlsr->detailed_logging )
writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
}
void
destroy_name_list(struct hashtb *name_list)
{
int j,nl_element;
struct name_list_entry *nle;
struct hashtb_enumerator eenle;
struct hashtb_enumerator *enle = &eenle;
hashtb_start(name_list, enle);
nl_element=hashtb_n(name_list);
for (j=0;j<nl_element;j++)
{
nle=enle->data;
free(nle->name);
hashtb_next(enle);
}
hashtb_end(enle);
hashtb_destroy(&name_list);
}
void
destroy_face_list(struct hashtb *face_list)
{
hashtb_destroy(&face_list);
}
void
destroy_npt(void)
{
int i, npt_element;
struct npt_entry *ne;
struct hashtb_enumerator ee;
struct hashtb_enumerator *e = &ee;
hashtb_start(nlsr->npt, e);
npt_element=hashtb_n(nlsr->npt);
for(i=0;i<npt_element;i++)
{
ne=e->data;
destroy_name_list(ne->name_list);
destroy_face_list(ne->face_list);
hashtb_next(e);
}
hashtb_end(e);
hashtb_destroy(&nlsr->npt);
}