Commit 21b9773f authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Make sources table into a linked list.

parent c9b1416d
...@@ -20,8 +20,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN ...@@ -20,8 +20,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. THE SOFTWARE.
*/ */
#define MAXSRCS 256
#define INFINITY ((unsigned short)(~0)) #define INFINITY ((unsigned short)(~0))
#ifndef RTPROT_BABEL #ifndef RTPROT_BABEL
......
...@@ -30,61 +30,42 @@ THE SOFTWARE. ...@@ -30,61 +30,42 @@ THE SOFTWARE.
#include "network.h" #include "network.h"
#include "route.h" #include "route.h"
struct source srcs[MAXSRCS]; struct source *srcs = NULL;
int numsrcs = 0;
struct source * struct source*
find_source(const unsigned char *id, const unsigned char *p, unsigned char plen, find_source(const unsigned char *id, const unsigned char *p, unsigned char plen,
int create, unsigned short seqno) int create, unsigned short seqno)
{ {
struct source *src; struct source *src;
int i, rc;
for(i = 0; i < numsrcs; i++) { for(src = srcs; src; src = src->next) {
if(!srcs[i].valid)
continue;
/* This should really be a hash table. For now, check the /* This should really be a hash table. For now, check the
last byte first. */ last byte first. */
if(srcs[i].id[15] != id[15]) if(src->id[15] != id[15])
continue; continue;
if(memcmp(srcs[i].id, id, 16) != 0) if(memcmp(src->id, id, 16) != 0)
continue; continue;
if(source_match(&srcs[i], p, plen)) if(source_match(src, p, plen))
return &srcs[i]; return src;
} }
if(!create) if(!create)
return NULL; return NULL;
again: src = malloc(sizeof(struct source));
if(src == NULL) {
src = NULL; perror("malloc(source)");
for(i = 0; i < numsrcs; i++) {
if(srcs[i].valid == 0) {
src = &srcs[i];
break;
}
}
if(!src) {
if(numsrcs >= MAXSRCS) {
rc = expire_sources();
if(rc)
goto again;
fprintf(stderr, "Too many sources.\n");
return NULL; return NULL;
} }
src = &srcs[numsrcs++];
}
src->valid = 1;
memcpy(src->id, id, 16); memcpy(src->id, id, 16);
memcpy(src->prefix, p, 16); memcpy(src->prefix, p, 16);
src->plen = plen; src->plen = plen;
src->seqno = seqno; src->seqno = seqno;
src->metric = INFINITY; src->metric = INFINITY;
src->time = now.tv_sec; src->time = now.tv_sec;
src->next = srcs;
srcs = src;
return src; return src;
} }
...@@ -92,21 +73,21 @@ int ...@@ -92,21 +73,21 @@ int
flush_source(struct source *src) flush_source(struct source *src)
{ {
int i; int i;
for(i = 0; i < numroutes; i++) { for(i = 0; i < numroutes; i++) {
if(routes[i].src == src) if(routes[i].src == src)
return 0; return 0;
} }
memset(src, 0, sizeof(*src)); if(srcs == src) {
VALGRIND_MAKE_MEM_UNDEFINED(src, sizeof(*src)); srcs = src->next;
src->valid = 0; } else {
struct source *previous = src;
while(numsrcs > 0 && !srcs[numsrcs - 1].valid) { while(previous->next != src)
numsrcs--; previous = previous->next;
VALGRIND_MAKE_MEM_UNDEFINED(&srcs[numsrcs], sizeof(srcs[numsrcs])); previous->next = src->next;
} }
free(src);
return 1; return 1;
} }
...@@ -139,24 +120,22 @@ update_source(struct source *src, ...@@ -139,24 +120,22 @@ update_source(struct source *src,
src->time = now.tv_sec; src->time = now.tv_sec;
} }
int void
expire_sources() expire_sources()
{ {
int i, changed, rc; struct source *src;
changed = 0;
for(i = 0; i < numsrcs; i++) { src = srcs;
if(!srcs[i].valid) while(src) {
continue; if(src->time > now.tv_sec)
if(srcs[i].time > now.tv_sec)
/* clock stepped */ /* clock stepped */
srcs[i].time = now.tv_sec; src->time = now.tv_sec;
if(srcs[i].time < now.tv_sec - SOURCE_GC_TIME) { if(src->time < now.tv_sec - SOURCE_GC_TIME) {
rc = flush_source(&srcs[i]); struct source *old = src;
changed = changed || rc; src = src->next;
flush_source(old);
continue;
} }
src = src->next;
} }
return changed;
} }
...@@ -23,10 +23,10 @@ THE SOFTWARE. ...@@ -23,10 +23,10 @@ THE SOFTWARE.
#define SOURCE_GC_TIME 200 #define SOURCE_GC_TIME 200
struct source { struct source {
struct source *next;
unsigned char id[16]; unsigned char id[16];
unsigned char prefix[16]; unsigned char prefix[16];
unsigned char plen; unsigned char plen;
unsigned char valid;
unsigned short seqno; unsigned short seqno;
unsigned short metric; unsigned short metric;
time_t time; time_t time;
...@@ -41,4 +41,4 @@ struct source *find_source(const unsigned char *id, ...@@ -41,4 +41,4 @@ struct source *find_source(const unsigned char *id,
int flush_source(struct source *src); int flush_source(struct source *src);
void update_source(struct source *src, void update_source(struct source *src,
unsigned short seqno, unsigned short metric); unsigned short seqno, unsigned short metric);
int expire_sources(void); void expire_sources(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