]>
git.proxmox.com Git - mirror_frr.git/blob - babeld/source.c
1 // SPDX-License-Identifier: MIT
3 Copyright (c) 2007, 2008 by Juliusz Chroboczek
15 #include "babel_main.h"
19 #include "babel_interface.h"
21 #include "babel_errors.h"
23 struct source
*srcs
= NULL
;
26 find_source(const unsigned char *id
, const unsigned char *p
, unsigned char plen
,
27 int create
, unsigned short seqno
)
31 for(src
= srcs
; src
; src
= src
->next
) {
32 /* This should really be a hash table. For now, check the
34 if(src
->id
[7] != id
[7])
36 if(memcmp(src
->id
, id
, 8) != 0)
40 if(memcmp(src
->prefix
, p
, 16) == 0)
47 src
= malloc(sizeof(struct source
));
49 flog_err(EC_BABEL_MEMORY
, "malloc(source): %s", safe_strerror(errno
));
53 memcpy(src
->id
, id
, 8);
54 memcpy(src
->prefix
, p
, 16);
57 src
->metric
= INFINITY
;
58 src
->time
= babel_now
.tv_sec
;
66 retain_source(struct source
*src
)
68 assert(src
->route_count
< 0xffff);
74 release_source(struct source
*src
)
76 assert(src
->route_count
> 0);
81 flush_source(struct source
*src
)
83 if(src
->route_count
> 0)
84 /* The source is in use by a route. */
90 struct source
*previous
= srcs
;
91 while(previous
->next
!= src
)
92 previous
= previous
->next
;
93 previous
->next
= src
->next
;
101 update_source(struct source
*src
,
102 unsigned short seqno
, unsigned short metric
)
104 if(metric
>= INFINITY
)
107 /* If a source is expired, pretend that it doesn't exist and update
108 it unconditionally. This makes ensures that old data will
109 eventually be overridden, and prevents us from getting stuck if
110 a router loses its sequence number. */
111 if(src
->time
< babel_now
.tv_sec
- SOURCE_GC_TIME
||
112 seqno_compare(src
->seqno
, seqno
) < 0 ||
113 (src
->seqno
== seqno
&& src
->metric
> metric
)) {
115 src
->metric
= metric
;
117 src
->time
= babel_now
.tv_sec
;
127 if(src
->time
> babel_now
.tv_sec
)
129 src
->time
= babel_now
.tv_sec
;
130 if(src
->time
< babel_now
.tv_sec
- SOURCE_GC_TIME
) {
131 struct source
*old
= src
;
141 check_sources_released(void)
145 for(src
= srcs
; src
; src
= src
->next
) {
146 if(src
->route_count
!= 0)
147 fprintf(stderr
, "Warning: source %s %s has refcount %d.\n",
148 format_eui64(src
->id
),
149 format_prefix(src
->prefix
, src
->plen
),
150 (int)src
->route_count
);