]>
git.proxmox.com Git - mirror_frr.git/blob - babeld/resend.c
2 Copyright (c) 2007, 2008 by Juliusz Chroboczek
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
31 #include "babel_main.h"
34 #include "neighbour.h"
37 #include "babel_interface.h"
39 struct timeval resend_time
= {0, 0};
40 struct resend
*to_resend
= NULL
;
43 resend_match(struct resend
*resend
,
44 int kind
, const unsigned char *prefix
, unsigned char plen
)
46 return (resend
->kind
== kind
&&
47 resend
->plen
== plen
&& memcmp(resend
->prefix
, prefix
, 16) == 0);
50 /* This is called by neigh.c when a neighbour is flushed */
53 flush_resends(struct neighbour
*neigh
)
58 static struct resend
*
59 find_resend(int kind
, const unsigned char *prefix
, unsigned char plen
,
60 struct resend
**previous_return
)
62 struct resend
*current
, *previous
;
67 if(resend_match(current
, kind
, prefix
, plen
)) {
69 *previous_return
= previous
;
73 current
= current
->next
;
80 find_request(const unsigned char *prefix
, unsigned char plen
,
81 struct resend
**previous_return
)
83 return find_resend(RESEND_REQUEST
, prefix
, plen
, previous_return
);
87 record_resend(int kind
, const unsigned char *prefix
, unsigned char plen
,
88 unsigned short seqno
, const unsigned char *id
,
89 struct interface
*ifp
, int delay
)
91 struct resend
*resend
;
92 unsigned int ifindex
= ifp
? ifp
->ifindex
: 0;
94 if((kind
== RESEND_REQUEST
&&
95 input_filter(NULL
, prefix
, plen
, NULL
, ifindex
) >= INFINITY
) ||
96 (kind
== RESEND_UPDATE
&&
97 output_filter(NULL
, prefix
, plen
, ifindex
) >= INFINITY
))
103 resend
= find_resend(kind
, prefix
, plen
, NULL
);
105 if(resend
->delay
&& delay
)
106 resend
->delay
= MIN(resend
->delay
, delay
);
108 resend
->delay
= delay
;
109 resend
->time
= babel_now
;
110 resend
->max
= RESEND_MAX
;
111 if(id
&& memcmp(resend
->id
, id
, 8) == 0 &&
112 seqno_compare(resend
->seqno
, seqno
) > 0) {
116 memcpy(resend
->id
, id
, 8);
118 memset(resend
->id
, 0, 8);
119 resend
->seqno
= seqno
;
120 if(resend
->ifp
!= ifp
)
123 resend
= malloc(sizeof(struct resend
));
127 resend
->max
= RESEND_MAX
;
128 resend
->delay
= delay
;
129 memcpy(resend
->prefix
, prefix
, 16);
131 resend
->seqno
= seqno
;
133 memcpy(resend
->id
, id
, 8);
135 memset(resend
->id
, 0, 8);
137 resend
->time
= babel_now
;
138 resend
->next
= to_resend
;
143 struct timeval timeout
;
144 timeval_add_msec(&timeout
, &resend
->time
, resend
->delay
);
145 timeval_min(&resend_time
, &timeout
);
151 resend_expired(struct resend
*resend
)
153 switch(resend
->kind
) {
155 return timeval_minus_msec(&babel_now
, &resend
->time
) >= REQUEST_TIMEOUT
;
157 return resend
->max
<= 0;
162 unsatisfied_request(const unsigned char *prefix
, unsigned char plen
,
163 unsigned short seqno
, const unsigned char *id
)
165 struct resend
*request
;
167 request
= find_request(prefix
, plen
, NULL
);
168 if(request
== NULL
|| resend_expired(request
))
171 if(memcmp(request
->id
, id
, 8) != 0 ||
172 seqno_compare(request
->seqno
, seqno
) <= 0)
178 /* Determine whether a given request should be forwarded. */
180 request_redundant(struct interface
*ifp
,
181 const unsigned char *prefix
, unsigned char plen
,
182 unsigned short seqno
, const unsigned char *id
)
184 struct resend
*request
;
186 request
= find_request(prefix
, plen
, NULL
);
187 if(request
== NULL
|| resend_expired(request
))
190 if(memcmp(request
->id
, id
, 8) == 0 &&
191 seqno_compare(request
->seqno
, seqno
) > 0)
194 if(request
->ifp
!= NULL
&& request
->ifp
!= ifp
)
198 /* Will be resent. */
201 if(timeval_minus_msec(&babel_now
, &request
->time
) <
202 (ifp
? MIN(babel_get_if_nfo(ifp
)->hello_interval
, 1000) : 1000))
210 satisfy_request(const unsigned char *prefix
, unsigned char plen
,
211 unsigned short seqno
, const unsigned char *id
,
212 struct interface
*ifp
)
214 struct resend
*request
, *previous
;
216 request
= find_request(prefix
, plen
, &previous
);
220 if(ifp
!= NULL
&& request
->ifp
!= ifp
)
223 if(memcmp(request
->id
, id
, 8) != 0 ||
224 seqno_compare(request
->seqno
, seqno
) <= 0) {
225 /* We cannot remove the request, as we may be walking the list right
226 now. Mark it as expired, so that expire_resend will remove it. */
228 request
->time
.tv_sec
= 0;
229 recompute_resend_time();
239 struct resend
*current
, *previous
;
245 if(resend_expired(current
)) {
246 if(previous
== NULL
) {
247 to_resend
= current
->next
;
251 previous
->next
= current
->next
;
253 current
= previous
->next
;
258 current
= current
->next
;
262 recompute_resend_time();
266 recompute_resend_time()
268 struct resend
*request
;
269 struct timeval resend
= {0, 0};
273 if(!resend_expired(request
) && request
->delay
> 0 && request
->max
> 0) {
274 struct timeval timeout
;
275 timeval_add_msec(&timeout
, &request
->time
, request
->delay
);
276 timeval_min(&resend
, &timeout
);
278 request
= request
->next
;
281 resend_time
= resend
;
287 struct resend
*resend
;
291 if(!resend_expired(resend
) && resend
->delay
> 0 && resend
->max
> 0) {
292 struct timeval timeout
;
293 timeval_add_msec(&timeout
, &resend
->time
, resend
->delay
);
294 if(timeval_compare(&babel_now
, &timeout
) >= 0) {
295 switch(resend
->kind
) {
297 send_multihop_request(resend
->ifp
,
298 resend
->prefix
, resend
->plen
,
299 resend
->seqno
, resend
->id
, 127);
302 send_update(resend
->ifp
, 1,
303 resend
->prefix
, resend
->plen
);
307 resend
->delay
= MIN(0xFFFF, resend
->delay
* 2);
311 resend
= resend
->next
;
313 recompute_resend_time();