Commit b48ed3e3 authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Factorise xroute destination comparison into a separate function.

parent cbd0670a
...@@ -42,16 +42,20 @@ int nummyxroutes = 0; ...@@ -42,16 +42,20 @@ int nummyxroutes = 0;
int xroute_gc_delay = 180; int xroute_gc_delay = 180;
int xroute_hold_delay = 45; int xroute_hold_delay = 45;
static int
xroute_prefix(struct xroute *xroute, const unsigned char *prefix, int plen)
{
return (xroute->plen == plen &&
memcmp(xroute->prefix, prefix, 16) == 0);
}
static struct xroute * static struct xroute *
find_installed_xroute(unsigned char *prefix, unsigned short plen) find_installed_xroute(unsigned char *prefix, unsigned short plen)
{ {
int i; int i;
for(i = 0; i < numxroutes; i++) { for(i = 0; i < numxroutes; i++) {
if(xroutes[i].installed && if(xroutes[i].installed && xroute_prefix(&xroutes[i], prefix, plen))
xroutes[i].plen == plen &&
memcmp(xroutes[i].prefix, prefix, 16) == 0) {
return &xroutes[i]; return &xroutes[i];
}
} }
return NULL; return NULL;
} }
...@@ -61,11 +65,8 @@ find_installed_myxroute(unsigned char *prefix, unsigned short plen) ...@@ -61,11 +65,8 @@ find_installed_myxroute(unsigned char *prefix, unsigned short plen)
{ {
int i; int i;
for(i = 0; i < nummyxroutes; i++) { for(i = 0; i < nummyxroutes; i++) {
if(myxroutes[i].installed && if(myxroutes[i].installed && xroute_prefix(&myxroutes[i], prefix, plen))
myxroutes[i].plen == plen &&
memcmp(myxroutes[i].prefix, prefix, 16) == 0) {
return &myxroutes[i]; return &myxroutes[i];
}
} }
return NULL; return NULL;
} }
...@@ -80,8 +81,7 @@ find_best_xroute(unsigned char *prefix, unsigned short plen) ...@@ -80,8 +81,7 @@ find_best_xroute(unsigned char *prefix, unsigned short plen)
for(i = 0; i < numxroutes; i++) { for(i = 0; i < numxroutes; i++) {
if(xroutes[i].metric >= INFINITY && xroutes[i].cost < INFINITY) if(xroutes[i].metric >= INFINITY && xroutes[i].cost < INFINITY)
continue; continue;
if(xroutes[i].plen != plen || if(!xroute_prefix(&xroutes[i], prefix, plen))
memcmp(xroutes[i].prefix, prefix, 16) != 0)
continue; continue;
route = find_installed_route(xroutes[i].gateway); route = find_installed_route(xroutes[i].gateway);
if(route->nexthop != xroutes[i].nexthop) if(route->nexthop != xroutes[i].nexthop)
...@@ -251,8 +251,7 @@ retract_xroutes(struct destination *gateway, struct neighbour *nexthop, ...@@ -251,8 +251,7 @@ retract_xroutes(struct destination *gateway, struct neighbour *nexthop,
if(xroutes[i].cost < INFINITY && xroutes[i].gateway == gateway && if(xroutes[i].cost < INFINITY && xroutes[i].gateway == gateway &&
xroutes[i].nexthop == nexthop) { xroutes[i].nexthop == nexthop) {
for(j = 0; j < numexcept; j++) { for(j = 0; j < numexcept; j++) {
if(memcmp(xroutes[i].prefix, except[j].prefix, 16) == 0 && if(xroute_prefix(&xroutes[i], except[j].prefix, except[j].plen))
xroutes[i].plen == except[j].plen)
goto skip; goto skip;
} }
update_xroute_metric(&xroutes[i], INFINITY); update_xroute_metric(&xroutes[i], INFINITY);
...@@ -282,7 +281,7 @@ update_xroute(const unsigned char *prefix, unsigned short plen, ...@@ -282,7 +281,7 @@ update_xroute(const unsigned char *prefix, unsigned short plen,
for(i = 0; i < numxroutes; i++) { for(i = 0; i < numxroutes; i++) {
xroute = &xroutes[i]; xroute = &xroutes[i];
if(xroute->gateway == gateway && xroute->nexthop == nexthop && if(xroute->gateway == gateway && xroute->nexthop == nexthop &&
memcmp(xroute->prefix, prefix, 16) == 0 && xroute->plen == plen) { xroute_prefix(xroute, prefix, plen)) {
update_xroute_metric(xroute, cost); update_xroute_metric(xroute, cost);
xroute->time = now.tv_sec; xroute->time = now.tv_sec;
return xroute; return xroute;
...@@ -378,8 +377,8 @@ check_myxroutes() ...@@ -378,8 +377,8 @@ check_myxroutes()
continue; continue;
installed = 0; installed = 0;
for(j = 0; j < n; j++) { for(j = 0; j < n; j++) {
if(routes[j].plen == myxroutes[i].plen && if(xroute_prefix(&myxroutes[i],
memcmp(routes[j].prefix, myxroutes[i].prefix, 16) == 0) { routes[j].prefix, routes[j].plen)) {
installed = 1; installed = 1;
break; break;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment