// uses both friend functions above ... friend StrInt operator + (const StrInt& a, const StrInt& b) { int len1 = a.snum.size(); int len2 = b.snum.size(); int ml = (len1 > len2 ? len1 : len2); StrInt s; s.snum.resize(ml); int carry = 0; while (len1 && len2) s.snum[--ml] = sumof(a, --len1, b, --len2, carry); if (len1) while (len1) s.snum[--ml] = sumof(a, --len1, carry); else if (len2) while (len2) s.snum[--ml] = sumof(b, --len2, carry); if (carry) s.snum = "1" + s.snum; return s; }
//function for updating cost by Bellmann ford equation int updaterouting(struct dv *dv1,struct message *sendmsg,struct message *revddata,struct details *neigh) { struct updating newcost[5]; char st[INET6_ADDRSTRLEN]; int i=0; int mincost; int32_t dest; int16_t cofxv; int16_t cofxy[5]; int32_t senderid; cofxy[0]=neigh[0].cost; cofxy[1]=neigh[1].cost; cofxy[2]=neigh[2].cost; cofxy[3]=neigh[3].cost; cofxy[4]=neigh[4].cost; struct sockaddr_in g; memcpy(&g.sin_addr,&revddata[0].senderip,sizeof(revddata[0].senderip)); inet_ntop(AF_INET,&g.sin_addr,st,INET_ADDRSTRLEN); for(i=0;i<dv1[0].n;i++) { if(revddata[0].senderip==neigh[i].nip) { cofxv=neigh[i].cost; senderid=neigh[i].id; //printf("\n sender id %d",senderid); } } if(revddata[0].senderip==neigh[0].nip) { newcost[0].to1=revddata[0].cost; newcost[0].to2=revddata[1].cost; newcost[0].to3=revddata[2].cost; newcost[0].to4=revddata[3].cost; newcost[0].to5=revddata[4].cost; } if(revddata[0].senderip==neigh[1].nip) { newcost[0].to1=revddata[0].cost; newcost[0].to2=revddata[1].cost; newcost[0].to3=revddata[2].cost; newcost[0].to4=revddata[3].cost; newcost[0].to5=revddata[4].cost; } if(revddata[0].senderip==neigh[2].nip) { newcost[0].to1=revddata[0].cost; newcost[0].to2=revddata[1].cost; newcost[0].to3=revddata[2].cost; newcost[0].to4=revddata[3].cost; newcost[0].to5=revddata[4].cost; } if(revddata[0].senderip==neigh[3].nip) { newcost[0].to1=revddata[0].cost; newcost[0].to2=revddata[1].cost; newcost[0].to3=revddata[2].cost; newcost[0].to4=revddata[3].cost; newcost[0].to5=revddata[4].cost; } if(revddata[0].senderip==neigh[4].nip) { newcost[0].to1=revddata[0].cost; newcost[0].to2=revddata[1].cost; newcost[0].to3=revddata[2].cost; newcost[0].to4=revddata[3].cost; newcost[0].to5=revddata[4].cost; } for(i=0;i<dv1[0].n;i++) { dest=neigh[i].id; if(dv1[0].presentnode==dest) { mincost=0; if(neigh[i].id == dv1[0].presentnode) { neigh[i].cost=mincost; } } else if(dest==neigh[0].id) { mincost=min(neigh[0].cost,sumof(cofxv,newcost[0].to1)); if(mincost != neigh[0].cost) dv1[0].hopto=senderid; neigh[0].cost=mincost; } else if(dest==neigh[1].id) { mincost=min(neigh[1].cost,sumof(cofxv,newcost[0].to2)); if(mincost != neigh[1].cost) dv1[1].hopto=senderid; neigh[1].cost=mincost; } else if(dest==neigh[2].id) { mincost=min(neigh[2].cost,sumof(cofxv,newcost[0].to3)); if(mincost != neigh[2].cost) dv1[2].hopto=senderid; neigh[2].cost=mincost; } else if(dest==neigh[3].id) { mincost=min(neigh[3].cost,sumof(cofxv,newcost[0].to4)); if(mincost != neigh[3].cost) dv1[3].hopto=senderid; neigh[3].cost=mincost; } else if(dest==neigh[4].id) { mincost=min(neigh[4].cost,sumof(cofxv,newcost[0].to5)); if(mincost != neigh[4].cost) dv1[4].hopto=senderid; neigh[4].cost=mincost; } } //for copying the data from the neighbour structure to the routing table for(i=0;i<dv1[0].n;i++) { dv1[i].sid=neigh[i].id; dv1[i].cost=neigh[i].cost; dv1[i].ip=neigh[i].nip; dv1[i].port=neigh[i].nport; }//for return 0; }