source.c 3.4 KB
Newer Older
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
1
/*
2
Copyright (c) 2007, 2008 by Juliusz Chroboczek
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
26
#include <sys/time.h>
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
27

28
#include "babeld.h"
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
29
#include "util.h"
30
#include "source.h"
31
#include "interface.h"
32
#include "route.h"
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
33

34
struct source *srcs = NULL;
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
35

36
struct source*
37
find_source(const unsigned char *id, const unsigned char *p, unsigned char plen,
38
            int create, unsigned short seqno)
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
39
{
40 41
    struct source *src;

42
    for(src = srcs; src; src = src->next) {
43 44
        /* This should really be a hash table.  For now, check the
           last byte first. */
45
        if(src->id[7] != id[7])
46
            continue;
47
        if(memcmp(src->id, id, 8) != 0)
48
            continue;
49 50 51 52
        if(src->plen != plen)
            continue;
        if(memcmp(src->prefix, p, 16) == 0)
            return src;
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
53 54 55 56 57
    }

    if(!create)
        return NULL;

58 59 60 61
    src = malloc(sizeof(struct source));
    if(src == NULL) {
        perror("malloc(source)");
        return NULL;
62 63
    }

64
    memcpy(src->id, id, 8);
65 66 67 68 69
    memcpy(src->prefix, p, 16);
    src->plen = plen;
    src->seqno = seqno;
    src->metric = INFINITY;
    src->time = now.tv_sec;
70 71
    src->next = srcs;
    srcs = src;
72
    return src;
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
73 74
}

75
int
76 77
flush_source(struct source *src)
{
78 79
    /* This is absolutely horrible -- it makes expire_sources quadratic.
       But it's not called very often. */
80 81
    if(find_route_with_source(src))
        return 0;
82

83 84 85
    if(srcs == src) {
        srcs = src->next;
    } else {
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
86
        struct source *previous = srcs;
87 88 89
        while(previous->next != src)
            previous = previous->next;
        previous->next = src->next;
90 91
    }

92
    free(src);
93
    return 1;
94 95
}

96
void
97 98
update_source(struct source *src,
              unsigned short seqno, unsigned short metric)
99
{
100 101 102
    if(metric >= INFINITY)
        return;

103
    if(src->time < now.tv_sec - SOURCE_GC_TIME ||
104
       seqno_compare(src->seqno, seqno) < 0 ||
105 106 107
       (src->seqno == seqno && src->metric > metric)) {
        src->seqno = seqno;
        src->metric = metric;
108
    }
109
    src->time = now.tv_sec;
110
}
111

112
void
113
expire_sources()
114
{
115
    struct source *src;
116

117 118 119
    src = srcs;
    while(src) {
        if(src->time > now.tv_sec)
120
            /* clock stepped */
121 122 123 124 125 126
            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;
127
        }
128
        src = src->next;
129 130
    }
}