]>
git.proxmox.com Git - mirror_frr.git/blob - babeld/source.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
28 #include "babel_main.h"
32 #include "babel_interface.h"
35 struct source
*srcs
= NULL
;
38 find_source(const unsigned char *id
, const unsigned char *p
, unsigned char plen
,
39 int create
, unsigned short seqno
)
43 for(src
= srcs
; src
; src
= src
->next
) {
44 /* This should really be a hash table. For now, check the
46 if(src
->id
[7] != id
[7])
48 if(memcmp(src
->id
, id
, 8) != 0)
52 if(memcmp(src
->prefix
, p
, 16) == 0)
59 src
= malloc(sizeof(struct source
));
61 zlog_err("malloc(source): %s", safe_strerror(errno
));
65 memcpy(src
->id
, id
, 8);
66 memcpy(src
->prefix
, p
, 16);
69 src
->metric
= INFINITY
;
70 src
->time
= babel_now
.tv_sec
;
78 retain_source(struct source
*src
)
80 assert(src
->route_count
< 0xffff);
86 release_source(struct source
*src
)
88 assert(src
->route_count
> 0);
93 flush_source(struct source
*src
)
95 if(src
->route_count
> 0)
96 /* The source is in use by a route. */
102 struct source
*previous
= srcs
;
103 while(previous
->next
!= src
)
104 previous
= previous
->next
;
105 previous
->next
= src
->next
;
113 update_source(struct source
*src
,
114 unsigned short seqno
, unsigned short metric
)
116 if(metric
>= INFINITY
)
119 /* If a source is expired, pretend that it doesn't exist and update
120 it unconditionally. This makes ensures that old data will
121 eventually be overridden, and prevents us from getting stuck if
122 a router loses its sequence number. */
123 if(src
->time
< babel_now
.tv_sec
- SOURCE_GC_TIME
||
124 seqno_compare(src
->seqno
, seqno
) < 0 ||
125 (src
->seqno
== seqno
&& src
->metric
> metric
)) {
127 src
->metric
= metric
;
129 src
->time
= babel_now
.tv_sec
;
139 if(src
->time
> babel_now
.tv_sec
)
141 src
->time
= babel_now
.tv_sec
;
142 if(src
->time
< babel_now
.tv_sec
- SOURCE_GC_TIME
) {
143 struct source
*old
= src
;
153 check_sources_released(void)
157 for(src
= srcs
; src
; src
= src
->next
) {
158 if(src
->route_count
!= 0)
159 fprintf(stderr
, "Warning: source %s %s has refcount %d.\n",
160 format_eui64(src
->id
),
161 format_prefix(src
->prefix
, src
->plen
),
162 (int)src
->route_count
);