]>
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
35 #include "babel_main.h"
38 #include "neighbour.h"
41 #include "babel_interface.h"
43 struct timeval resend_time
= {0, 0};
44 struct resend
*to_resend
= NULL
;
47 resend_match(struct resend
*resend
,
48 int kind
, const unsigned char *prefix
, unsigned char plen
)
50 return (resend
->kind
== kind
&&
51 resend
->plen
== plen
&& memcmp(resend
->prefix
, prefix
, 16) == 0);
54 /* This is called by neigh.c when a neighbour is flushed */
57 flush_resends(struct neighbour
*neigh
)
62 static struct resend
*
63 find_resend(int kind
, const unsigned char *prefix
, unsigned char plen
,
64 struct resend
**previous_return
)
66 struct resend
*current
, *previous
;
71 if(resend_match(current
, kind
, prefix
, plen
)) {
73 *previous_return
= previous
;
77 current
= current
->next
;
84 find_request(const unsigned char *prefix
, unsigned char plen
,
85 struct resend
**previous_return
)
87 return find_resend(RESEND_REQUEST
, prefix
, plen
, previous_return
);
91 record_resend(int kind
, const unsigned char *prefix
, unsigned char plen
,
92 unsigned short seqno
, const unsigned char *id
,
93 struct interface
*ifp
, int delay
)
95 struct resend
*resend
;
96 unsigned int ifindex
= ifp
? ifp
->ifindex
: 0;
98 if((kind
== RESEND_REQUEST
&&
99 input_filter(NULL
, prefix
, plen
, NULL
, ifindex
) >= INFINITY
) ||
100 (kind
== RESEND_UPDATE
&&
101 output_filter(NULL
, prefix
, plen
, ifindex
) >= INFINITY
))
107 resend
= find_resend(kind
, prefix
, plen
, NULL
);
109 if(resend
->delay
&& delay
)
110 resend
->delay
= MIN(resend
->delay
, delay
);
112 resend
->delay
= delay
;
113 resend
->time
= babel_now
;
114 resend
->max
= RESEND_MAX
;
115 if(id
&& memcmp(resend
->id
, id
, 8) == 0 &&
116 seqno_compare(resend
->seqno
, seqno
) > 0) {
120 memcpy(resend
->id
, id
, 8);
122 memset(resend
->id
, 0, 8);
123 resend
->seqno
= seqno
;
124 if(resend
->ifp
!= ifp
)
127 resend
= malloc(sizeof(struct resend
));
131 resend
->max
= RESEND_MAX
;
132 resend
->delay
= delay
;
133 memcpy(resend
->prefix
, prefix
, 16);
135 resend
->seqno
= seqno
;
137 memcpy(resend
->id
, id
, 8);
139 memset(resend
->id
, 0, 8);
141 resend
->time
= babel_now
;
142 resend
->next
= to_resend
;
147 struct timeval timeout
;
148 timeval_add_msec(&timeout
, &resend
->time
, resend
->delay
);
149 timeval_min(&resend_time
, &timeout
);
155 resend_expired(struct resend
*resend
)
157 switch(resend
->kind
) {
159 return timeval_minus_msec(&babel_now
, &resend
->time
) >= REQUEST_TIMEOUT
;
161 return resend
->max
<= 0;
166 unsatisfied_request(const unsigned char *prefix
, unsigned char plen
,
167 unsigned short seqno
, const unsigned char *id
)
169 struct resend
*request
;
171 request
= find_request(prefix
, plen
, NULL
);
172 if(request
== NULL
|| resend_expired(request
))
175 if(memcmp(request
->id
, id
, 8) != 0 ||
176 seqno_compare(request
->seqno
, seqno
) <= 0)
182 /* Determine whether a given request should be forwarded. */
184 request_redundant(struct interface
*ifp
,
185 const unsigned char *prefix
, unsigned char plen
,
186 unsigned short seqno
, const unsigned char *id
)
188 struct resend
*request
;
190 request
= find_request(prefix
, plen
, NULL
);
191 if(request
== NULL
|| resend_expired(request
))
194 if(memcmp(request
->id
, id
, 8) == 0 &&
195 seqno_compare(request
->seqno
, seqno
) > 0)
198 if(request
->ifp
!= NULL
&& request
->ifp
!= ifp
)
202 /* Will be resent. */
205 if(timeval_minus_msec(&babel_now
, &request
->time
) <
206 (ifp
? MIN(babel_get_if_nfo(ifp
)->hello_interval
, 1000) : 1000))
214 satisfy_request(const unsigned char *prefix
, unsigned char plen
,
215 unsigned short seqno
, const unsigned char *id
,
216 struct interface
*ifp
)
218 struct resend
*request
, *previous
;
220 request
= find_request(prefix
, plen
, &previous
);
224 if(ifp
!= NULL
&& request
->ifp
!= ifp
)
227 if(memcmp(request
->id
, id
, 8) != 0 ||
228 seqno_compare(request
->seqno
, seqno
) <= 0) {
229 /* We cannot remove the request, as we may be walking the list right
230 now. Mark it as expired, so that expire_resend will remove it. */
232 request
->time
.tv_sec
= 0;
233 recompute_resend_time();
243 struct resend
*current
, *previous
;
249 if(resend_expired(current
)) {
250 if(previous
== NULL
) {
251 to_resend
= current
->next
;
255 previous
->next
= current
->next
;
257 current
= previous
->next
;
262 current
= current
->next
;
266 recompute_resend_time();
270 recompute_resend_time(void)
272 struct resend
*request
;
273 struct timeval resend
= {0, 0};
277 if(!resend_expired(request
) && request
->delay
> 0 && request
->max
> 0) {
278 struct timeval timeout
;
279 timeval_add_msec(&timeout
, &request
->time
, request
->delay
);
280 timeval_min(&resend
, &timeout
);
282 request
= request
->next
;
285 resend_time
= resend
;
291 struct resend
*resend
;
295 if(!resend_expired(resend
) && resend
->delay
> 0 && resend
->max
> 0) {
296 struct timeval timeout
;
297 timeval_add_msec(&timeout
, &resend
->time
, resend
->delay
);
298 if(timeval_compare(&babel_now
, &timeout
) >= 0) {
299 switch(resend
->kind
) {
301 send_multihop_request(resend
->ifp
,
302 resend
->prefix
, resend
->plen
,
303 resend
->seqno
, resend
->id
, 127);
306 send_update(resend
->ifp
, 1,
307 resend
->prefix
, resend
->plen
);
311 resend
->delay
= MIN(0xFFFF, resend
->delay
* 2);
315 resend
= resend
->next
;
317 recompute_resend_time();