Debian Codegraph @

ax25rtd/cache_ctl.c

raw symbols SCIP
1 /*
2 * Copyright (c) 1996 Joerg Reuter ([email protected])
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 */
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include <time.h>
24 #include <sys/socket.h>
25
26 #include <config.h>
27 #include <netax25/ax25.h>
28
29 #include "ax25rtd.h"
30
31 /* Hmm, we really should implement this in C++ */
32 /* But I HATE it. */
33
34 /* (later: haven't I seen this statement elsewere? hmm...) */
35
36 int update_ip_route(config * config, unsigned long ip, int ipmode,
37 ax25_address * call, time_t timestamp)
38 {
39 ip_rt_entry *bp = ip_routes;
40 ip_rt_entry *bp_prev = ip_routes;
41 char *iface;
42 int action = 0;
43
44 if (((ip ^ config->ip) & config->netmask) != 0)
45 return 0;
46
47 iface = config->dev;
48
49 while (bp) {
50 if (bp->ip == ip) {
51 if (bp->timestamp == 0 && timestamp != 0)
52 return 0;
53
54 if (strcmp(bp->iface, iface)) {
55 action |= NEW_ROUTE;
56 strcpy(bp->iface, iface);
57 }
58
59 if (memcmp(&bp->call, call, AXLEN)) {
60 action |= NEW_ARP;
61 memcpy(&bp->call, call, AXLEN);
62 }
63
64 if (ipmode != bp->ipmode) {
65 action |= NEW_IPMODE;
66 bp->ipmode = ipmode;
67 }
68
69 bp->timestamp = timestamp;
70
71 if (bp != ip_routes) {
72 if (bp->next)
73 bp->next->prev = bp->prev;
74 if (bp->prev)
75 bp->prev->next = bp->next;
76
77 bp->next = ip_routes;
78 bp->prev = NULL;
79 ip_routes->prev = bp;
80 ip_routes = bp;
81 }
82
83 return action;
84 }
85
86 bp_prev = bp;
87 bp = bp->next;
88 }
89
90 if (ip_routes_cnt >= ip_maxroutes) {
91 if (bp_prev == NULL) /* error */
92 return 0;
93
94 bp_prev->prev->next = NULL;
95 free(bp_prev);
96 ip_routes_cnt--;
97 }
98
99 bp = (ip_rt_entry *) malloc(sizeof(ip_rt_entry));
100 if (bp == NULL)
101 return 0;
102
103 ip_routes_cnt++;
104 action = NEW_ROUTE | NEW_ARP | NEW_IPMODE;
105 bp->ipmode = ipmode;
106 bp->ip = ip;
107 bp->invalid = 0;
108
109 bp->timestamp = timestamp;
110 strcpy(bp->iface, iface);
111 memcpy(&bp->call, call, AXLEN);
112
113 if (ip_routes == NULL) {
114 ip_routes = bp;
115 bp->next = bp->prev = NULL;
116 return action;
117 }
118
119 bp->next = ip_routes;
120 bp->prev = NULL;
121 ip_routes->prev = bp;
122 ip_routes = bp;
123
124 return action;
125 }
126
127 ax25_rt_entry *update_ax25_route(config * config, ax25_address * call,
128 int ndigi, ax25_address * digi,
129 time_t timestamp)
130 {
131 ax25_rt_entry *bp = ax25_routes;
132 ax25_rt_entry *bp_prev = ax25_routes;
133 char *iface = config->dev;
134 int action = 0;
135
136 while (bp) {
137 if (!memcmp(call, &bp->call, AXLEN)) {
138 if (bp->timestamp == 0 && timestamp != 0)
139 return NULL;
140
141 if (strcmp(bp->iface, iface)) {
142 del_kernel_ax25_route(bp->iface,
143 &bp->call);
144 action |= NEW_ROUTE;
145 strcpy(bp->iface, iface);
146 }
147
148 if (ndigi != bp->ndigi
149 || memcmp(bp->digipeater, digi,
150 bp->ndigi * AXLEN)) {
151 action |= NEW_ROUTE;
152 memcpy(bp->digipeater, digi,
153 ndigi * AXLEN);
154 bp->ndigi = ndigi;
155 }
156
157 bp->timestamp = timestamp;
158
159 if (bp != ax25_routes) {
160 if (bp->next)
161 bp->next->prev = bp->prev;
162 if (bp->prev)
163 bp->prev->next = bp->next;
164
165 bp->next = ax25_routes;
166 bp->prev = NULL;
167 ax25_routes->prev = bp;
168 ax25_routes = bp;
169 }
170
171 if (action)
172 return bp;
173 else
174 return NULL;
175 }
176
177 bp_prev = bp;
178 bp = bp->next;
179 }
180
181 if (ax25_routes_cnt >= ax25_maxroutes) {
182 if (bp_prev == NULL) /* error */
183 return NULL;
184
185 bp_prev->prev->next = NULL;
186 free(bp_prev);
187 ax25_routes_cnt--;
188 }
189
190 bp = (ax25_rt_entry *) malloc(sizeof(ax25_rt_entry));
191 if (bp == NULL)
192 return NULL;
193
194 ax25_routes_cnt++;
195
196 bp->timestamp = timestamp;
197 strcpy(bp->iface, iface);
198 bp->call = *call;
199
200 if (ndigi)
201 memcpy(bp->digipeater, digi, ndigi * AXLEN);
202
203 bp->ndigi = ndigi;
204
205 if (ax25_routes == NULL) {
206 ax25_routes = bp;
207 bp->next = bp->prev = NULL;
208 return bp;
209 }
210
211 bp->next = ax25_routes;
212 bp->prev = NULL;
213 ax25_routes->prev = bp;
214 ax25_routes = bp;
215
216 return bp;
217 }
218
219 static ip_rt_entry *remove_ip_route(ip_rt_entry * bp)
220 {
221 ip_rt_entry *bp2;
222
223 bp2 = bp->next;
224 if (bp2)
225 bp2->prev = bp->prev;
226 if (bp->prev)
227 bp->prev->next = bp2;
228 if (bp == ip_routes)
229 ip_routes = bp2;
230
231 del_kernel_ip_route(bp->iface, bp->ip);
232
233 free(bp);
234 ip_routes_cnt--;
235 return bp2;
236 }
237
238 static ax25_rt_entry *remove_ax25_route(ax25_rt_entry * bp)
239 {
240 ax25_rt_entry *bp2;
241 ip_rt_entry *iprt;
242
243 bp2 = bp->next;
244 if (bp2)
245 bp2->prev = bp->prev;
246 if (bp->prev)
247 bp->prev->next = bp2;
248 if (bp == ax25_routes)
249 ax25_routes = bp2;
250
251 for (iprt = ip_routes; iprt; iprt = iprt->next)
252 if (!memcmp(&iprt->call, &bp->call, AXLEN))
253 remove_ip_route(iprt);
254
255 del_kernel_ax25_route(bp->iface, &bp->call);
256
257 free(bp);
258 ax25_routes_cnt--;
259 return bp2;
260 }
261
262 int del_ip_route(unsigned long ip)
263 {
264 ip_rt_entry *bp;
265
266 if (ip == 0)
267 return 1;
268
269 for (bp = ip_routes; bp; bp = bp->next)
270 if (bp->ip == ip) {
271 remove_ip_route(bp);
272 return 0;
273 }
274
275 return 1;
276 }
277
278 int invalidate_ip_route(unsigned long ip)
279 {
280 ip_rt_entry *bp;
281
282 for (bp = ip_routes; bp; bp = bp->next)
283 if (bp->ip == ip) {
284 bp->invalid = 1;
285 return 1;
286 }
287
288 return 0;
289 }
290
291 int del_ax25_route(config * config, ax25_address * call)
292 {
293 ax25_rt_entry *bp;
294
295 for (bp = ax25_routes; bp; bp = bp->next)
296 if (!memcmp(call, &bp->call, AXLEN)
297 && !strcmp(config->dev, bp->iface)) {
298 remove_ax25_route(bp);
299 return 0;
300 }
301
302 return 1;
303 }
304
305 void expire_ax25_route(time_t when)
306 {
307 ax25_rt_entry *bp;
308 time_t now = time(NULL);
309
310 for (bp = ax25_routes; bp;)
311 if (bp->timestamp != 0 && bp->timestamp + when <= now)
312 bp = remove_ax25_route(bp);
313 else
314 bp = bp->next;
315 }
316
317 void expire_ip_route(time_t when)
318 {
319 ip_rt_entry *bp;
320 time_t now = time(NULL);
321
322 for (bp = ip_routes; bp;)
323 if (bp->timestamp != 0 && bp->timestamp + when <= now)
324 bp = remove_ip_route(bp);
325 else
326 bp = bp->next;
327 }