Commit 9450e4d3 authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Implement -x and -X.

parent a12be898
......@@ -125,7 +125,8 @@ main(int argc, char **argv)
} else if(strcmp(*arg, "-p") == 0) {
SHIFTE();
protocol_port = atoi(*arg);
} else if(strcmp(*arg, "-n") == 0) {
} else if(strcmp(*arg, "-x") == 0 || strcmp(*arg, "-X") == 0) {
int force = (strcmp(*arg, "-X") == 0);
if(nummyxroutes >= MAXMYXROUTES) {
fprintf(stderr, "Too many network routes.\n");
exit(1);
......@@ -144,6 +145,7 @@ main(int argc, char **argv)
if(myxroutes[nummyxroutes].cost < 0 ||
myxroutes[nummyxroutes].cost > INFINITY)
goto syntax;
myxroutes[nummyxroutes].installed = force ? 0 : 2;
nummyxroutes++;
} else if(strcmp(*arg, "-h") == 0) {
SHIFTE();
......@@ -346,6 +348,7 @@ main(int argc, char **argv)
}
init_signals();
check_myxroutes();
for(i = 0; i < numnets; i++) {
send_hello(&nets[i]);
......@@ -493,7 +496,7 @@ main(int argc, char **argv)
" "
"[-u update_interval] [-k metric] [-s] [-P] [-c cost]\n"
" "
"[-d level] [-n net cost]... address interface...\n",
"[-d level] [-x net cost] [-X net cost]... address interface...\n",
argv[0]);
exit(1);
......@@ -625,7 +628,13 @@ add_network(char *ifname, int ifindex, int mtu,
void
expire_routes(void)
{
int i;
int rc, i;
rc = check_myxroutes();
if(rc > 0)
send_self_update(NULL);
else if(rc < 0)
fprintf(stderr, "Warning: couldn't check installed routes.\n");
for(i = 0; i < numneighs; i++) {
if(neighs[i].id[0] == 0)
......
......@@ -359,6 +359,8 @@ flushupdates(void)
if(buffered_updates[i] == NULL) {
start_message(net, MIN(20 + 20 * nummyxroutes, 1000));
for(j = 0; j < nummyxroutes; j++) {
if(!myxroutes[j].installed)
continue;
if(net->bufsize - net->buffered < 40)
/* We cannot just call start_message, as this would
split the xroutes from the update. Bail out
......
......@@ -52,6 +52,20 @@ find_installed_xroute(unsigned char *prefix, unsigned short plen)
return NULL;
}
static struct xroute *
find_installed_myxroute(unsigned char *prefix, unsigned short plen)
{
int i;
for(i = 0; i < nummyxroutes; i++) {
if(myxroutes[i].installed &&
myxroutes[i].plen == plen &&
memcmp(myxroutes[i].prefix, prefix, 16) == 0) {
return &xroutes[i];
}
}
return NULL;
}
static struct xroute *
find_best_xroute(unsigned char *prefix, unsigned short plen)
{
......@@ -146,9 +160,12 @@ consider_xroute(struct xroute *xroute)
if(find_installed_route(xroute->gateway) == NULL)
return;
installed = find_installed_xroute(xroute->prefix, xroute->plen);
if(!installed || installed->metric >= xroute->metric)
install_xroute(xroute);
installed = find_installed_myxroute(xroute->prefix, xroute->plen);
if(!installed) {
installed = find_installed_xroute(xroute->prefix, xroute->plen);
if(!installed || installed->metric >= xroute->metric)
install_xroute(xroute);
}
}
void
......@@ -283,3 +300,41 @@ update_xroute_metric(struct xroute *xroute, int cost)
}
}
}
int
check_myxroutes()
{
int i, j, n, change;
struct kernel_route routes[120];
n = -1;
for(i = 0; i < nummyxroutes; i++)
if(myxroutes[i].installed < 2)
n = MAX(n, myxroutes[i].plen);
if(n < 0)
return 0;
n = kernel_routes(n, routes, 120);
if(n < 0)
return -1;
change = 0;
for(i = 0; i < nummyxroutes; i++) {
int installed;
if(myxroutes[i].installed == 2)
continue;
installed = 0;
for(j = 0; j < n; j++) {
if(routes[j].plen == myxroutes[i].plen &&
memcmp(routes[j].prefix, myxroutes[i].prefix, 16) == 0) {
installed = 1;
break;
}
}
if(myxroutes[i].installed != installed) {
myxroutes[i].installed = installed;
change = 1;
}
}
return change;
}
......@@ -47,3 +47,4 @@ void flush_xroutes(struct destination *gateway,
struct xroute * update_xroute(const unsigned char *prefix, unsigned short plen,
struct destination *gateway, int cost);
void update_xroute_metric(struct xroute *xroute, int cost);
int check_myxroutes(void);
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