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

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 <sys/time.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>

28
#include "babeld.h"
29 30
#include "util.h"
#include "neighbour.h"
31
#include "resend.h"
32
#include "message.h"
33
#include "interface.h"
34
#include "configuration.h"
35

36
struct timeval resend_time = {0, 0};
37
struct resend *to_resend = NULL;
38 39

static int
40
resend_match(struct resend *resend,
41 42
             int kind, const unsigned char *prefix, unsigned char plen,
             const unsigned char *src_prefix, unsigned char src_plen)
43
{
44
    return (resend->kind == kind &&
45 46
            resend->plen == plen && memcmp(resend->prefix, prefix, 16) == 0 &&
            resend->src_plen == src_plen &&
47
            memcmp(resend->src_prefix, src_prefix, 16) == 0);
48 49
}

Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
50 51
/* This is called by neigh.c when a neighbour is flushed */

Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
52 53 54 55 56 57
void
flush_resends(struct neighbour *neigh)
{
    /* Nothing for now */
}

58 59
static struct resend *
find_resend(int kind, const unsigned char *prefix, unsigned char plen,
60 61
            const unsigned char *src_prefix, unsigned char src_plen,
            struct resend **previous_return)
62
{
63
    struct resend *current, *previous;
64 65

    previous = NULL;
66 67
    current = to_resend;
    while(current) {
68
        if(resend_match(current, kind, prefix, plen, src_prefix, src_plen)) {
69 70
            if(previous_return)
                *previous_return = previous;
71
            return current;
72
        }
73 74
        previous = current;
        current = current->next;
75 76 77 78 79
    }

    return NULL;
}

80 81
struct resend *
find_request(const unsigned char *prefix, unsigned char plen,
82
             const unsigned char *src_prefix, unsigned char src_plen,
83 84
             struct resend **previous_return)
{
85 86
    return find_resend(RESEND_REQUEST, prefix, plen, src_prefix, src_plen,
                       previous_return);
87 88
}

89
int
90
record_resend(int kind, const unsigned char *prefix, unsigned char plen,
91
              const unsigned char *src_prefix, unsigned char src_plen,
92
              unsigned short seqno, const unsigned char *id,
93
              struct interface *ifp, int delay)
94
{
95
    struct resend *resend;
96
    unsigned int ifindex = ifp ? ifp->ifindex : 0;
97

98
    if((kind == RESEND_REQUEST &&
99
        input_filter(NULL, prefix, plen, src_prefix, src_plen, NULL,
100
                     ifindex) >=
101
        INFINITY) ||
102
       (kind == RESEND_UPDATE &&
103
        output_filter(NULL, prefix, plen, src_prefix, src_plen, ifindex) >=
104
        INFINITY))
105 106
        return 0;

107 108 109
    if(delay >= 0xFFFF)
        delay = 0xFFFF;

110
    resend = find_resend(kind, prefix, plen, src_prefix, src_plen, NULL);
111 112 113
    if(resend) {
        if(resend->delay && delay)
            resend->delay = MIN(resend->delay, delay);
114
        else if(delay)
115 116
            resend->delay = delay;
        resend->time = now;
117
        resend->max = RESEND_MAX;
118
        if(id && memcmp(resend->id, id, 8) == 0 &&
119
           seqno_compare(resend->seqno, seqno) > 0) {
120 121
            return 0;
        }
122 123 124 125
        if(id)
            memcpy(resend->id, id, 8);
        else
            memset(resend->id, 0, 8);
126
        resend->seqno = seqno;
127 128
        if(resend->ifp != ifp)
            resend->ifp = NULL;
129
    } else {
130 131
        resend = malloc(sizeof(struct resend));
        if(resend == NULL)
132
            return -1;
133
        resend->kind = kind;
134
        resend->max = RESEND_MAX;
135 136 137
        resend->delay = delay;
        memcpy(resend->prefix, prefix, 16);
        resend->plen = plen;
138 139
        memcpy(resend->src_prefix, src_prefix, 16);
        resend->src_plen = src_plen;
140
        resend->seqno = seqno;
141 142 143 144
        if(id)
            memcpy(resend->id, id, 8);
        else
            memset(resend->id, 0, 8);
145
        resend->ifp = ifp;
146 147 148
        resend->time = now;
        resend->next = to_resend;
        to_resend = resend;
149
    }
150

151
    if(resend->delay) {
152
        struct timeval timeout;
153
        timeval_add_msec(&timeout, &resend->time, resend->delay);
154
        timeval_min(&resend_time, &timeout);
155 156
    }
    return 1;
157 158
}

159 160 161 162 163 164 165 166 167 168 169
static int
resend_expired(struct resend *resend)
{
    switch(resend->kind) {
    case RESEND_REQUEST:
        return timeval_minus_msec(&now, &resend->time) >= REQUEST_TIMEOUT;
    default:
        return resend->max <= 0;
    }
}

170 171
int
unsatisfied_request(const unsigned char *prefix, unsigned char plen,
172
                    const unsigned char *src_prefix, unsigned char src_plen,
173
                    unsigned short seqno, const unsigned char *id)
174
{
175
    struct resend *request;
176

177
    request = find_request(prefix, plen, src_prefix, src_plen, NULL);
178
    if(request == NULL || resend_expired(request))
179 180
        return 0;

181
    if(memcmp(request->id, id, 8) != 0 ||
182 183 184 185 186 187
       seqno_compare(request->seqno, seqno) <= 0)
        return 1;

    return 0;
}

188 189
/* Determine whether a given request should be forwarded. */
int
190
request_redundant(struct interface *ifp,
191
                  const unsigned char *prefix, unsigned char plen,
192
                  const unsigned char *src_prefix, unsigned char src_plen,
193
                  unsigned short seqno, const unsigned char *id)
194 195 196
{
    struct resend *request;

197
    request = find_request(prefix, plen, src_prefix, src_plen, NULL);
198 199 200
    if(request == NULL || resend_expired(request))
        return 0;

201
    if(memcmp(request->id, id, 8) == 0 &&
202 203 204
       seqno_compare(request->seqno, seqno) > 0)
        return 0;

205
    if(request->ifp != NULL && request->ifp != ifp)
206 207 208 209 210 211 212
        return 0;

    if(request->max > 0)
        /* Will be resent. */
        return 1;

    if(timeval_minus_msec(&now, &request->time) <
213
       (ifp ? MIN(ifp->hello_interval, 1000) : 1000))
214 215 216 217 218 219
        /* Fairly recent. */
        return 1;

    return 0;
}

220 221
int
satisfy_request(const unsigned char *prefix, unsigned char plen,
222
                const unsigned char *src_prefix, unsigned char src_plen,
223
                unsigned short seqno, const unsigned char *id,
224
                struct interface *ifp)
225
{
226
    struct resend *request, *previous;
227

228
    request = find_request(prefix, plen, src_prefix, src_plen, &previous);
229 230 231
    if(request == NULL)
        return 0;

232
    if(ifp != NULL && request->ifp != ifp)
233 234
        return 0;

235
    if(memcmp(request->id, id, 8) != 0 ||
236
       seqno_compare(request->seqno, seqno) <= 0) {
237 238 239 240
        /* We cannot remove the request, as we may be walking the list right
           now.  Mark it as expired, so that expire_resend will remove it. */
        request->max = 0;
        request->time.tv_sec = 0;
241
        recompute_resend_time();
242 243 244 245 246 247 248
        return 1;
    }

    return 0;
}

void
249
expire_resend()
250
{
251
    struct resend *current, *previous;
252
    int recompute = 0;
253 254

    previous = NULL;
255 256 257
    current = to_resend;
    while(current) {
        if(resend_expired(current)) {
258
            if(previous == NULL) {
259 260 261
                to_resend = current->next;
                free(current);
                current = to_resend;
262
            } else {
263 264 265
                previous->next = current->next;
                free(current);
                current = previous->next;
266
            }
267
            recompute = 1;
268
        } else {
269
            previous = current;
270
            current = current->next;
271 272
        }
    }
273
    if(recompute)
274
        recompute_resend_time();
275 276
}

277
void
278
recompute_resend_time()
279
{
280
    struct resend *request;
281
    struct timeval resend = {0, 0};
282

283
    request = to_resend;
284
    while(request) {
285
        if(!resend_expired(request) && request->delay > 0 && request->max > 0) {
286
            struct timeval timeout;
287
            timeval_add_msec(&timeout, &request->time, request->delay);
288
            timeval_min(&resend, &timeout);
289
        }
290 291 292
        request = request->next;
    }

293
    resend_time = resend;
294 295 296
}

void
297
do_resend()
298
{
299
    struct resend *resend;
300

301 302 303
    resend = to_resend;
    while(resend) {
        if(!resend_expired(resend) && resend->delay > 0 && resend->max > 0) {
304
            struct timeval timeout;
305
            timeval_add_msec(&timeout, &resend->time, resend->delay);
306
            if(timeval_compare(&now, &timeout) >= 0) {
307 308
                switch(resend->kind) {
                case RESEND_REQUEST:
309
                    send_multihop_request(resend->ifp,
310
                                          resend->prefix, resend->plen,
311
                                          resend->src_prefix, resend->src_plen,
312
                                          resend->seqno, resend->id, 127);
313 314
                    break;
                case RESEND_UPDATE:
315
                    send_update(resend->ifp, 1,
316
                                resend->prefix, resend->plen,
317
                                resend->src_prefix, resend->src_plen);
318 319 320
                    break;
                default: abort();
                }
321
                resend->delay = MIN(0xFFFF, resend->delay * 2);
322
                resend->max--;
323
            }
324
        }
325
        resend = resend->next;
326
    }
327
    recompute_resend_time();
328
}