Commit 6563584a authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Use a linked list for neighbours.

parent e45020f7
......@@ -910,8 +910,6 @@ dump_tables(FILE *out)
fprintf(out, "My id %s seqno %d\n", format_address(myid), myseqno);
FOR_ALL_NEIGHBOURS(neigh) {
if(!neighbour_valid(neigh))
continue;
fprintf(out, "Neighbour %s ", format_address(neigh->id));
fprintf(out, "at %s dev %s reach %04x rxcost %d txcost %d%s.\n",
format_address(neigh->address),
......
......@@ -21,7 +21,6 @@ THE SOFTWARE.
*/
#define MAXSRCS 256
#define MAXNEIGHBOURS 64
#define INFINITY ((unsigned short)(~0))
......
......@@ -1022,8 +1022,6 @@ send_ihu(struct neighbour *neigh, struct network *net)
if(neigh == NULL) {
struct neighbour *ngh;
FOR_ALL_NEIGHBOURS(ngh) {
if(!neighbour_valid(ngh))
continue;
if(ngh->network == net)
send_ihu(ngh, net);
}
......@@ -1056,7 +1054,7 @@ send_marginal_ihu(struct network *net)
{
struct neighbour *neigh;
FOR_ALL_NEIGHBOURS(neigh) {
if(!neighbour_valid(neigh) || (net && neigh->network != net))
if(net && neigh->network != net)
continue;
if(neigh->txcost >= 384 || (neigh->reach & 0xF000) != 0xF000)
send_ihu(neigh, net);
......
......@@ -34,13 +34,18 @@ THE SOFTWARE.
#include "route.h"
#include "message.h"
struct neighbour neighs[MAXNEIGHBOURS];
int numneighs = 0;
struct neighbour *neighs = NULL;
int
neighbour_valid(struct neighbour *neigh)
struct neighbour *
find_neighbour(const unsigned char *address, struct network *net)
{
return neigh->hello_seqno != -2;
struct neighbour *neigh;
FOR_ALL_NEIGHBOURS(neigh) {
if(memcmp(address, neigh->address, 16) == 0 &&
neigh->network == net)
return neigh;
}
return NULL;
}
void
......@@ -49,35 +54,23 @@ flush_neighbour(struct neighbour *neigh)
flush_neighbour_routes(neigh);
if(unicast_neighbour == neigh)
flush_unicast(1);
memset(neigh, 0, sizeof(*neigh));
VALGRIND_MAKE_MEM_UNDEFINED(neigh, sizeof(*neigh));
neigh->hello_seqno = -2;
while(numneighs > 0 && !neighbour_valid(&neighs[numneighs - 1])) {
numneighs--;
VALGRIND_MAKE_MEM_UNDEFINED(&neighs[numneighs],
sizeof(neighs[numneighs]));
}
}
struct neighbour *
find_neighbour(const unsigned char *address, struct network *net)
{
struct neighbour *neigh;
FOR_ALL_NEIGHBOURS(neigh) {
if(!neighbour_valid(neigh))
continue;
if(memcmp(address, neigh->address, 16) == 0 &&
neigh->network == net)
return neigh;
if(neighs == neigh) {
neighs = neigh->next;
} else {
struct neighbour *previous = neighs;
while(previous->next != neigh)
previous = previous->next;
previous->next = neigh->next;
}
return NULL;
free(neigh);
}
struct neighbour *
add_neighbour(const unsigned char *id, const unsigned char *address,
struct network *net)
{
struct neighbour *neigh, *ngh;
struct neighbour *neigh;
const struct timeval zero = {0, 0};
neigh = find_neighbour(address, net);
......@@ -93,17 +86,13 @@ add_neighbour(const unsigned char *id, const unsigned char *address,
}
debugf("Creating neighbour %s (%s).\n",
format_address(id), format_address(address));
FOR_ALL_NEIGHBOURS(ngh) {
if(!neighbour_valid(ngh))
neigh = ngh;
}
if(!neigh) {
if(numneighs >= MAXNEIGHBOURS) {
fprintf(stderr, "Too many neighbours.\n");
return NULL;
}
neigh = &neighs[numneighs++];
neigh = malloc(sizeof(struct neighbour));
if(neigh == NULL) {
perror("malloc(neighbour)");
return NULL;
}
neigh->hello_seqno = -1;
memcpy(neigh->id, id, 16);
memcpy(neigh->address, address, 16);
......@@ -114,6 +103,8 @@ add_neighbour(const unsigned char *id, const unsigned char *address,
neigh->hello_interval = 0;
neigh->ihu_interval = 0;
neigh->network = net;
neigh->next = neighs;
neighs = neigh;
send_hello(net);
return neigh;
}
......@@ -240,16 +231,16 @@ check_neighbours()
debugf("Checking neighbours.\n");
FOR_ALL_NEIGHBOURS(neigh) {
if(!neighbour_valid(neigh))
continue;
neigh = neighs;
while(neigh) {
changed = update_neighbour(neigh, -1, 0);
if(neigh->reach == 0 ||
neigh->hello_time.tv_sec > now.tv_sec || /* clock stepped */
timeval_minus_msec(&now, &neigh->hello_time) > 300000) {
flush_neighbour(neigh);
struct neighbour *old = neigh;
neigh = neigh->next;
flush_neighbour(old);
continue;
}
......@@ -264,6 +255,7 @@ check_neighbours()
msecs = MIN(msecs, neigh->hello_interval * 10);
if(neigh->ihu_interval > 0)
msecs = MIN(msecs, neigh->ihu_interval * 10);
neigh = neigh->next;
}
return msecs;
......
......@@ -21,8 +21,8 @@ THE SOFTWARE.
*/
struct neighbour {
/* This is -1 when unknown, and -2 for an invalid neighbour,
so don't make it unsigned */
struct neighbour *next;
/* This is -1 when unknown, so don't make it unsigned */
int hello_seqno;
unsigned char id[16];
unsigned char address[16];
......@@ -35,11 +35,10 @@ struct neighbour {
struct network *network;
};
extern struct neighbour neighs[MAXNEIGHBOURS];
extern int numneighs;
extern struct neighbour *neighs;
#define FOR_ALL_NEIGHBOURS(_neigh) \
for(_neigh = neighs; _neigh < neighs + numneighs; _neigh++)
for(_neigh = neighs; _neigh; _neigh = _neigh->next)
int neighbour_valid(struct neighbour *neigh);
void flush_neighbour(struct neighbour *neigh);
......
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