# How to calculate indirect trust value of each router involved in communication using NS2?

#### Description

Indirect trust value is calculated based on trust value reported by common neighbours of corresponding node.

#### Source Code

//Finding common neighbor for current path

int common_count=0;

int common_neighbor[100];

for (int p=0;p<neighbor_count[index];p++) {

for (int q=0;q<neighbor_count[path->nexthop];q++) {

//printf("n1=%d n2=%d\n",neighbor_id[index][p],neighbor_id[rt->rt_nexthop][q]);

if(neighbor_id[index][p]==neighbor_id[path->nexthop][q]) {

common_neighbor[common_count]=neighbor_id[index][p];

//printf("neighborid=%d\n",common_neighbor[common_count]);

if(malicious_status[neighbor_id[index][p]]==1) {

printf("Attacker %d is common neighbor \n",neighbor_id[index][p]);

}

common_count=common_count+1;

}

}

}

printf("Current_Path path->nexthop=%d \t common_count %d\n",path->nexthop,common_count);

float a[common_count];

float g_trust=0.0;

float avg_trust;

for (int r=0;r<common_count;r++) { a[r]=DT[common_neighbor[r]][path->nexthop];

printf("DT=%.2f for nexthop %d at node %d\n",a[r],path->nexthop,common_neighbor[r]);

g_trust = g_trust + a[r];

}

avg_trust = g_trust / common_count;

avg_trust is the average trust value of indirect neighbors.