123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- /*
- Copyright (c) 2007, 2008 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 "babeld.h"
- #include "util.h"
- #include "neighbour.h"
- #include "resend.h"
- #include "message.h"
- #include "interface.h"
- #include "configuration.h"
- #include "lorauth.h"
- struct timeval resend_time = {0, 0};
- struct resend *to_resend = NULL;
- static int
- resend_match(struct resend *resend,
- int kind, const unsigned char *prefix, unsigned char plen,
- const unsigned char *src_prefix, unsigned char src_plen)
- {
- return (resend->kind == kind &&
- resend->plen == plen && memcmp(resend->prefix, prefix, 16) == 0 &&
- resend->src_plen == src_plen &&
- memcmp(resend->src_prefix, src_prefix, 16) == 0);
- }
- /* This is called by neigh.c when a neighbour is flushed */
- void
- flush_resends(struct neighbour *neigh)
- {
- /* Nothing for now */
- }
- static struct resend *
- find_resend(int kind, const unsigned char *prefix, unsigned char plen,
- const unsigned char *src_prefix, unsigned char src_plen,
- struct resend **previous_return)
- {
- struct resend *current, *previous;
- previous = NULL;
- current = to_resend;
- while(current) {
- if(resend_match(current, kind, prefix, plen, src_prefix, src_plen)) {
- if(previous_return)
- *previous_return = previous;
- return current;
- }
- previous = current;
- current = current->next;
- }
- return NULL;
- }
- struct resend *
- find_request(const unsigned char *prefix, unsigned char plen,
- const unsigned char *src_prefix, unsigned char src_plen,
- struct resend **previous_return)
- {
- return find_resend(RESEND_REQUEST, prefix, plen, src_prefix, src_plen,
- previous_return);
- }
- int
- record_resend(int kind, const unsigned char *prefix, unsigned char plen,
- const unsigned char *src_prefix, unsigned char src_plen,
- const unsigned char *cipher, unsigned short clen,
- unsigned short seqno, const unsigned char *id,
- struct interface *ifp, int delay)
- {
- struct resend *resend;
- unsigned int ifindex = ifp ? ifp->ifindex : 0;
- if((kind == RESEND_REQUEST &&
- input_filter(NULL, prefix, plen, src_prefix, src_plen, NULL,
- ifindex) >=
- INFINITY) ||
- (kind == RESEND_UPDATE &&
- output_filter(NULL, prefix, plen, src_prefix, src_plen, ifindex) >=
- INFINITY))
- return 0;
- if(delay >= 0xFFFF)
- delay = 0xFFFF;
- resend = find_resend(kind, prefix, plen, src_prefix, src_plen, NULL);
- if(resend) {
- if(resend->delay && delay)
- resend->delay = MIN(resend->delay, delay);
- else if(delay)
- resend->delay = delay;
- resend->time = now;
- resend->max = RESEND_MAX;
- if(id && memcmp(resend->id, id, 8) == 0 &&
- seqno_compare(resend->seqno, seqno) > 0) {
- return 0;
- }
- if(id)
- memcpy(resend->id, id, 8);
- else
- memset(resend->id, 0, 8);
- resend->seqno = seqno;
- if(resend->ifp != ifp)
- resend->ifp = NULL;
- // -- lorauth --
- if(clen>0) {
- memcpy(resend->cipher, cipher, LORAUTH_CIPHER_LEN);
- resend->clen = clen;
- }
- // ----
- } else {
- resend = calloc(1, sizeof(struct resend));
- if(resend == NULL)
- return -1;
- resend->kind = kind;
- resend->max = RESEND_MAX;
- resend->delay = delay;
- memcpy(resend->prefix, prefix, 16);
- resend->plen = plen;
- memcpy(resend->src_prefix, src_prefix, 16);
- resend->src_plen = src_plen;
- resend->seqno = seqno;
- if(id)
- memcpy(resend->id, id, 8);
- resend->ifp = ifp;
- resend->time = now;
- resend->next = to_resend;
- // -- lorauth --
- resend->clen = clen;
- if(clen > 0)
- memcpy(resend->cipher, cipher, LORAUTH_CIPHER_LEN);
- else
- clean_cipher(resend->cipher);
- // ----
- to_resend = resend;
- }
- if(resend->delay) {
- struct timeval timeout;
- timeval_add_msec(&timeout, &resend->time, resend->delay);
- timeval_min(&resend_time, &timeout);
- }
- return 1;
- }
- 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;
- }
- }
- int
- unsatisfied_request(const unsigned char *prefix, unsigned char plen,
- const unsigned char *src_prefix, unsigned char src_plen,
- unsigned short seqno, const unsigned char *id)
- {
- struct resend *request;
- request = find_request(prefix, plen, src_prefix, src_plen, NULL);
- if(request == NULL || resend_expired(request))
- return 0;
- if(memcmp(request->id, id, 8) != 0 ||
- seqno_compare(request->seqno, seqno) <= 0)
- return 1;
- return 0;
- }
- /* Determine whether a given request should be forwarded. */
- int
- request_redundant(struct interface *ifp,
- const unsigned char *prefix, unsigned char plen,
- const unsigned char *src_prefix, unsigned char src_plen,
- unsigned short seqno, const unsigned char *id)
- {
- struct resend *request;
- request = find_request(prefix, plen, src_prefix, src_plen, NULL);
- if(request == NULL || resend_expired(request))
- return 0;
- if(memcmp(request->id, id, 8) == 0 &&
- seqno_compare(request->seqno, seqno) > 0)
- return 0;
- if(request->ifp != NULL && request->ifp != ifp)
- return 0;
- if(request->max > 0)
- /* Will be resent. */
- return 1;
- if(timeval_minus_msec(&now, &request->time) <
- (ifp ? MIN(ifp->hello_interval, 1000) : 1000))
- /* Fairly recent. */
- return 1;
- return 0;
- }
- int
- satisfy_request(const unsigned char *prefix, unsigned char plen,
- const unsigned char *src_prefix, unsigned char src_plen,
- unsigned short seqno, const unsigned char *id,
- struct interface *ifp)
- {
- struct resend *request, *previous;
- request = find_request(prefix, plen, src_prefix, src_plen, &previous);
- if(request == NULL)
- return 0;
- if(ifp != NULL && request->ifp != ifp)
- return 0;
- if(memcmp(request->id, id, 8) != 0 ||
- seqno_compare(request->seqno, seqno) <= 0) {
- /* 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;
- recompute_resend_time();
- return 1;
- }
- return 0;
- }
- void
- expire_resend()
- {
- struct resend *current, *previous;
- int recompute = 0;
- previous = NULL;
- current = to_resend;
- while(current) {
- if(resend_expired(current)) {
- if(previous == NULL) {
- to_resend = current->next;
- free(current);
- current = to_resend;
- } else {
- previous->next = current->next;
- free(current);
- current = previous->next;
- }
- recompute = 1;
- } else {
- previous = current;
- current = current->next;
- }
- }
- if(recompute)
- recompute_resend_time();
- }
- void
- recompute_resend_time()
- {
- struct resend *request;
- struct timeval resend = {0, 0};
- request = to_resend;
- while(request) {
- if(!resend_expired(request) && request->delay > 0 && request->max > 0) {
- struct timeval timeout;
- timeval_add_msec(&timeout, &request->time, request->delay);
- timeval_min(&resend, &timeout);
- }
- request = request->next;
- }
- resend_time = resend;
- }
- void
- do_resend()
- {
- struct resend *resend;
- resend = to_resend;
- while(resend) {
- if(!resend_expired(resend) && resend->delay > 0 && resend->max > 0) {
- struct timeval timeout;
- timeval_add_msec(&timeout, &resend->time, resend->delay);
- if(timeval_compare(&now, &timeout) >= 0) {
- switch(resend->kind) {
- case RESEND_REQUEST:
- send_multihop_request(resend->ifp,
- resend->prefix, resend->plen,
- resend->src_prefix, resend->src_plen,
- resend->seqno, resend->id, 127);
- break;
- case RESEND_UPDATE:
- send_update(resend->ifp, 1,
- resend->prefix, resend->plen,
- resend->src_prefix, resend->src_plen,
- // -- lorauth --
- resend->cipher, resend->clen
- // ----
- );
- break;
- default: abort();
- }
- resend->delay = MIN(0xFFFF, resend->delay * 2);
- resend->max--;
- }
- }
- resend = resend->next;
- }
- recompute_resend_time();
- }
|