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