source.c 3.63 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 26

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>

27
#include "babel.h"
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
28
#include "util.h"
29
#include "source.h"
30 31
#include "network.h"
#include "route.h"
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
32

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

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

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

    if(!create)
        return NULL;

55 56 57 58
    src = malloc(sizeof(struct source));
    if(src == NULL) {
        perror("malloc(source)");
        return NULL;
59 60
    }

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

72
int
73 74
flush_source(struct source *src)
{
75
    int i;
76 77 78 79

    /* This is absolutely horrible -- it makes expire_sources quadratic.
       But it's not called very often. */

80 81 82 83 84
    for(i = 0; i < numroutes; i++) {
        if(routes[i].src == src)
            return 0;
    }

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

94
    free(src);
95
    return 1;
96 97
}

98
int
99 100
source_match(struct source *src,
             const unsigned char *p, unsigned char plen)
101
{
102
    if(src->plen != plen)
103
        return 0;
104
    if(src->prefix[15] != p[15])
105
        return 0;
106
    if(memcmp(src->prefix, p, 16) != 0)
107 108 109 110 111
        return 0;
    return 1;
}

void
112 113
update_source(struct source *src,
              unsigned short seqno, unsigned short metric)
114
{
115 116 117
    if(metric >= INFINITY)
        return;

118
    if(src->time < now.tv_sec - SOURCE_GC_TIME ||
119
       seqno_compare(src->seqno, seqno) < 0 ||
120 121 122
       (src->seqno == seqno && src->metric > metric)) {
        src->seqno = seqno;
        src->metric = metric;
123
    }
124
    src->time = now.tv_sec;
125
}
126

127
void
128
expire_sources()
129
{
130
    struct source *src;
131

132 133 134
    src = srcs;
    while(src) {
        if(src->time > now.tv_sec)
135
            /* clock stepped */
136 137 138 139 140 141
            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;
142
        }
143
        src = src->next;
144 145
    }
}