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