blob: cc4ed445be771f61791491d48e09ed162177e73c [file] [log] [blame]
Paul Jakma57345092011-12-25 17:52:09 +01001/*
2 * This file is free software: you may copy, redistribute and/or modify it
3 * under the terms of the GNU General Public License as published by the
4 * Free Software Foundation, either version 2 of the License, or (at your
5 * option) any later version.
6 *
7 * This file is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *
15 * This file incorporates work covered by the following copyright and
16 * permission notice:
17 *
18Copyright (c) 2007, 2008 by Juliusz Chroboczek
19
20Permission is hereby granted, free of charge, to any person obtaining a copy
21of this software and associated documentation files (the "Software"), to deal
22in the Software without restriction, including without limitation the rights
23to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
24copies of the Software, and to permit persons to whom the Software is
25furnished to do so, subject to the following conditions:
26
27The above copyright notice and this permission notice shall be included in
28all copies or substantial portions of the Software.
29
30THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
36THE SOFTWARE.
37*/
38
39#include <stdlib.h>
40#include <stdio.h>
41#include <string.h>
42#include <sys/time.h>
43
44#include "babel_main.h"
45#include "babeld.h"
46#include "util.h"
47#include "source.h"
48#include "babel_interface.h"
49#include "route.h"
50
51struct source *srcs = NULL;
52
53struct source*
54find_source(const unsigned char *id, const unsigned char *p, unsigned char plen,
55 int create, unsigned short seqno)
56{
57 struct source *src;
58
59 for(src = srcs; src; src = src->next) {
60 /* This should really be a hash table. For now, check the
61 last byte first. */
62 if(src->id[7] != id[7])
63 continue;
64 if(memcmp(src->id, id, 8) != 0)
65 continue;
66 if(source_match(src, p, plen))
67 return src;
68 }
69
70 if(!create)
71 return NULL;
72
73 src = malloc(sizeof(struct source));
74 if(src == NULL) {
75 zlog_err("malloc(source): %s", safe_strerror(errno));
76 return NULL;
77 }
78
79 memcpy(src->id, id, 8);
80 memcpy(src->prefix, p, 16);
81 src->plen = plen;
82 src->seqno = seqno;
83 src->metric = INFINITY;
84 src->time = babel_now.tv_sec;
85 src->next = srcs;
86 srcs = src;
87 return src;
88}
89
90int
91flush_source(struct source *src)
92{
93 /* This is absolutely horrible -- it makes expire_sources quadratic.
94 But it's not called very often. */
95
96 if (babel_route_get_by_source(src) != NULL)
97 return 0;
98
99 if(srcs == src) {
100 srcs = src->next;
101 } else {
102 struct source *previous = srcs;
103 while(previous->next != src)
104 previous = previous->next;
105 previous->next = src->next;
106 }
107
108 free(src);
109 return 1;
110}
111
112int
113source_match(struct source *src,
114 const unsigned char *p, unsigned char plen)
115{
116 if(src->plen != plen)
117 return 0;
118 if(src->prefix[15] != p[15])
119 return 0;
120 if(memcmp(src->prefix, p, 16) != 0)
121 return 0;
122 return 1;
123}
124
125void
126update_source(struct source *src,
127 unsigned short seqno, unsigned short metric)
128{
129 if(metric >= INFINITY)
130 return;
131
132 if(src->time < babel_now.tv_sec - SOURCE_GC_TIME ||
133 seqno_compare(src->seqno, seqno) < 0 ||
134 (src->seqno == seqno && src->metric > metric)) {
135 src->seqno = seqno;
136 src->metric = metric;
137 }
138 src->time = babel_now.tv_sec;
139}
140
141void
142expire_sources()
143{
144 struct source *src;
145
146 src = srcs;
147 while(src) {
148 if(src->time > babel_now.tv_sec)
149 /* clock stepped */
150 src->time = babel_now.tv_sec;
151 if(src->time < babel_now.tv_sec - SOURCE_GC_TIME) {
152 struct source *old = src;
153 src = src->next;
154 flush_source(old);
155 continue;
156 }
157 src = src->next;
158 }
159}