source.c 4.51 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>
27
#include <assert.h>
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
28

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

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

37
struct source*
38 39 40
find_source(const unsigned char *id,
            const unsigned char *prefix, unsigned char plen,
            const unsigned char *src_prefix, unsigned char src_plen,
41
            int create, unsigned short seqno)
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
42
{
43 44
    struct source *src;

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

    if(!create)
        return NULL;

65 66 67 68
    src = malloc(sizeof(struct source));
    if(src == NULL) {
        perror("malloc(source)");
        return NULL;
69 70
    }

71
    memcpy(src->id, id, 8);
72
    memcpy(src->prefix, prefix, 16);
73
    src->plen = plen;
74 75
    memcpy(src->src_prefix, src_prefix, 16);
    src->src_plen = src_plen;
76 77 78
    src->seqno = seqno;
    src->metric = INFINITY;
    src->time = now.tv_sec;
79
    src->route_count = 0;
80 81
    src->next = srcs;
    srcs = src;
82
    return src;
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
83 84
}

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
struct source *
retain_source(struct source *src)
{
    assert(src->route_count < 0xffff);
    src->route_count++;
    return src;
}

void
release_source(struct source *src)
{
    assert(src->route_count > 0);
    src->route_count--;
}

100
int
101 102
flush_source(struct source *src)
{
103 104
    if(src->route_count > 0)
        /* The source is in use by a route. */
105
        return 0;
106

107 108 109
    if(srcs == src) {
        srcs = src->next;
    } else {
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
110
        struct source *previous = srcs;
111 112 113
        while(previous->next != src)
            previous = previous->next;
        previous->next = src->next;
114 115
    }

116
    free(src);
117
    return 1;
118 119
}

120
void
121 122
update_source(struct source *src,
              unsigned short seqno, unsigned short metric)
123
{
124 125 126
    if(metric >= INFINITY)
        return;

127 128 129 130
    /* If a source is expired, pretend that it doesn't exist and update
       it unconditionally.  This makes ensures that old data will
       eventually be overridden, and prevents us from getting stuck if
       a router loses its sequence number. */
131
    if(src->time < now.tv_sec - SOURCE_GC_TIME ||
132
       seqno_compare(src->seqno, seqno) < 0 ||
133 134 135
       (src->seqno == seqno && src->metric > metric)) {
        src->seqno = seqno;
        src->metric = metric;
136
    }
137
    src->time = now.tv_sec;
138
}
139

140
void
141
expire_sources()
142
{
143
    struct source *src;
144

145 146 147
    src = srcs;
    while(src) {
        if(src->time > now.tv_sec)
148
            /* clock stepped */
149 150 151 152 153 154
            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;
155
        }
156
        src = src->next;
157 158
    }
}
159 160 161 162 163 164 165 166 167 168 169 170 171 172

void
check_sources_released(void)
{
    struct source *src;

    for(src = srcs; src; src = src->next) {
        if(src->route_count != 0)
            fprintf(stderr, "Warning: source %s %s has refcount %d.\n",
                    format_eui64(src->id),
                    format_prefix(src->prefix, src->plen),
                    (int)src->route_count);
    }
}