many cleanups; avoid interface timing out; put it back asap
[unix-history] / usr / src / sbin / routed / timer.c
CommitLineData
5919db1b 1#ifndef lint
b7e4f8be 2static char sccsid[] = "@(#)timer.c 4.5 (Berkeley) %G%";
5919db1b
SL
3#endif
4
5/*
6 * Routing Table Management Daemon
7 */
7fe7fe74 8#include "defs.h"
5919db1b
SL
9
10int timeval = -TIMER_RATE;
11
12/*
13 * Timer routine. Performs routing information supply
14 * duties and manages timers on routing table entries.
15 */
16timer()
17{
18 register struct rthash *rh;
19 register struct rt_entry *rt;
20 struct rthash *base = hosthash;
21 int doinghost = 1, timetobroadcast;
22
23 timeval += TIMER_RATE;
24 if (lookforinterfaces && (timeval % CHECK_INTERVAL) == 0)
25 ifinit();
26 timetobroadcast = supplier && (timeval % SUPPLY_INTERVAL) == 0;
27again:
28 for (rh = base; rh < &base[ROUTEHASHSIZ]; rh++) {
29 rt = rh->rt_forw;
30 for (; rt != (struct rt_entry *)rh; rt = rt->rt_forw) {
31 /*
32 * We don't advance time on a routing entry for
33 * a passive gateway or that for our only interface.
34 * The latter is excused because we don't act as
35 * a routing information supplier and hence would
36 * time it out. This is fair as if it's down
37 * we're cut off from the world anyway and it's
38 * not likely we'll grow any new hardware in
39 * the mean time.
40 */
41 if (!(rt->rt_state & RTS_PASSIVE) &&
42 (supplier || !(rt->rt_state & RTS_INTERFACE)))
43 rt->rt_timer += TIMER_RATE;
44 if (rt->rt_timer >= EXPIRE_TIME)
45 rt->rt_metric = HOPCNT_INFINITY;
46 if (rt->rt_timer >= GARBAGE_TIME) {
47 rt = rt->rt_back;
48 rtdelete(rt->rt_forw);
49 continue;
50 }
51 if (rt->rt_state & RTS_CHANGED) {
52 rt->rt_state &= ~RTS_CHANGED;
53 /* don't send extraneous packets */
54 if (!supplier || timetobroadcast)
55 continue;
56 msg->rip_cmd = RIPCMD_RESPONSE;
55d340a4 57 msg->rip_vers = RIPVERSION;
5919db1b 58 msg->rip_nets[0].rip_dst = rt->rt_dst;
55d340a4
SL
59 msg->rip_nets[0].rip_dst.sa_family =
60 htons(msg->rip_nets[0].rip_dst.sa_family);
61 msg->rip_nets[0].rip_metric =
9b1c54bf 62 htonl(min(rt->rt_metric+1, HOPCNT_INFINITY));
5919db1b
SL
63 toall(sendmsg);
64 }
65 }
66 }
67 if (doinghost) {
68 doinghost = 0;
69 base = nethash;
70 goto again;
71 }
72 if (timetobroadcast)
73 toall(supply);
74 alarm(TIMER_RATE);
75}
b7e4f8be
MK
76
77/*
78 * On hangup, let everyone know we're going away.
79 */
80hup()
81{
82 register struct rthash *rh;
83 register struct rt_entry *rt;
84 struct rthash *base = hosthash;
85 int doinghost = 1;
86
87 if (supplier) {
88again:
89 for (rh = base; rh < &base[ROUTEHASHSIZ]; rh++) {
90 rt = rh->rt_forw;
91 for (; rt != (struct rt_entry *)rh; rt = rt->rt_forw)
92 rt->rt_metric = HOPCNT_INFINITY;
93 }
94 if (doinghost) {
95 doinghost = 0;
96 base = nethash;
97 goto again;
98 }
99 toall(supply);
100 }
101 exit(1);
102}