request.c 5.99 KB
Newer Older
1 2 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 28 29 30 31
/*
Copyright (c) 2007 by Juliusz Chroboczek

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>

#include "babel.h"
#include "util.h"
#include "neighbour.h"
#include "request.h"
32
#include "message.h"
33
#include "network.h"
34
#include "filter.h"
35

36
int request_resend_time = 0;
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
struct request *recorded_requests = NULL;

static int
request_match(struct request *request,
              const unsigned char *prefix, unsigned char plen)
{
    return request->plen == plen && memcmp(request->prefix, prefix, 16) == 0;
}

struct request *
find_request(const unsigned char *prefix, unsigned char plen,
             struct request **previous_return)
{
    struct request *request, *previous;

    previous = NULL;
    request = recorded_requests;
    while(request) {
        if(request_match(request, prefix, plen)) {
            if(previous_return)
                *previous_return = previous;
            return request;
        }
        previous = request;
        request = request->next;
    }

    return NULL;
}

int
record_request(const unsigned char *prefix, unsigned char plen,
               unsigned short seqno, unsigned short router_hash,
70
               struct network *network, int resend)
71 72
{
    struct request *request;
73
    unsigned int ifindex = network ? network->ifindex : 0;
74

75 76
    if(input_filter(NULL, prefix, plen, NULL, ifindex) >= INFINITY ||
       output_filter(NULL, prefix, plen, ifindex) >= INFINITY)
77 78
        return 0;

79 80
    request = find_request(prefix, plen, NULL);
    if(request) {
81 82 83 84 85 86 87
        if(request->resend && resend)
            request->resend = MIN(request->resend, resend);
        else if(resend)
            request->resend = resend;
        request->time = now.tv_sec;
        request_resend_time = MIN(request_resend_time,
                                  request->time + request->resend);
88
        if(request->router_hash == router_hash &&
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
           seqno_compare(request->seqno, seqno) > 0) {
            return 0;
        } else {
            request->router_hash = router_hash;
            request->seqno = seqno;
            if(request->network != network)
                request->network = NULL;
            return 1;
        }
    } else {
        request = malloc(sizeof(struct request));
        if(request == NULL)
            return -1;
        memcpy(request->prefix, prefix, 16);
        request->plen = plen;
        request->seqno = seqno;
        request->router_hash = router_hash;
        request->network = network;
107 108 109 110
        request->time = now.tv_sec;
        request->resend = resend;
        if(resend)
            request_resend_time = MIN(request_resend_time, now.tv_sec + resend);
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
        request->next = recorded_requests;
        recorded_requests = request;
        return 1;
    }
}

int
satisfy_request(const unsigned char *prefix, unsigned char plen,
                unsigned short seqno, unsigned short router_hash,
                struct network *network)
{
    struct request *request, *previous;

    request = find_request(prefix, plen, &previous);
    if(request == NULL)
        return 0;

    if(network != NULL && request->network != network)
        return 0;

    if(request->router_hash != router_hash ||
       seqno_compare(request->seqno, seqno) <= 0) {
        if(previous == NULL)
            recorded_requests = request->next;
        else
            previous->next = request->next;
        free(request);
138
        recompute_request_resend_time();
139 140 141 142 143 144 145 146 147 148
        return 1;
    }

    return 0;
}

void
expire_requests()
{
    struct request *request, *previous;
149
    int recompute = 0;
150 151 152 153

    previous = NULL;
    request = recorded_requests;
    while(request) {
154
        if(request->time < now.tv_sec - REQUEST_TIMEOUT) {
155 156
            if(previous == NULL) {
                recorded_requests = request->next;
157
                free(request);
158 159 160
                request = recorded_requests;
            } else {
                previous->next = request->next;
161
                free(request);
162 163
                request = previous->next;
            }
164
            recompute = 1;
165 166 167 168
        } else {
            request = request->next;
        }
    }
169 170
    if(recompute)
        recompute_request_resend_time();
171 172
}

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
int
recompute_request_resend_time()
{
    struct request *request;
    int resend = 0;

    request = recorded_requests;
    while(request) {
        if(request->resend) {
            if(resend)
                resend = MIN(resend, request->time + request->resend);
            else
                resend = request->time + request->resend;
        }
        request = request->next;
    }

    request_resend_time = resend;
    return resend;
}

void
resend_requests()
{
    struct request *request;

    request = recorded_requests;
    while(request) {
        if(request->resend && now.tv_sec >= request->time + request->resend) {
            send_request(NULL, request->prefix, request->plen, 127,
                         request->seqno, request->router_hash);
            request->resend = 2 * request->resend;
        }
        request = request->next;
    }
208
    recompute_request_resend_time();
209
}