xref: /original-bsd/sbin/routed/timer.c (revision c3e32dec)
1 /*
2  * Copyright (c) 1983, 1988, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)timer.c	8.1 (Berkeley) 06/05/93";
10 #endif /* not lint */
11 
12 /*
13  * Routing Table Management Daemon
14  */
15 #include "defs.h"
16 
17 int	faketime;
18 
19 /*
20  * Timer routine.  Performs routing information supply
21  * duties and manages timers on routing table entries.
22  * Management of the RTS_CHANGED bit assumes that we broadcast
23  * each time called.
24  */
25 void
26 timer()
27 {
28 	register struct rthash *rh;
29 	register struct rt_entry *rt;
30 	struct rthash *base = hosthash;
31 	int doinghost = 1, timetobroadcast;
32 	extern int externalinterfaces;
33 
34 	(void) gettimeofday(&now, (struct timezone *)NULL);
35 	faketime += TIMER_RATE;
36 	if (lookforinterfaces && (faketime % CHECK_INTERVAL) == 0)
37 		ifinit();
38 	timetobroadcast = supplier && (faketime % SUPPLY_INTERVAL) == 0;
39 again:
40 	for (rh = base; rh < &base[ROUTEHASHSIZ]; rh++) {
41 		rt = rh->rt_forw;
42 		for (; rt != (struct rt_entry *)rh; rt = rt->rt_forw) {
43 			/*
44 			 * We don't advance time on a routing entry for
45 			 * a passive gateway, or any interface if we're
46 			 * not acting as supplier.
47 			 */
48 			if (!(rt->rt_state & RTS_PASSIVE) &&
49 			    (supplier || !(rt->rt_state & RTS_INTERFACE)))
50 				rt->rt_timer += TIMER_RATE;
51 			if (rt->rt_timer >= GARBAGE_TIME) {
52 				rt = rt->rt_back;
53 				rtdelete(rt->rt_forw);
54 				continue;
55 			}
56 			if (rt->rt_timer >= EXPIRE_TIME &&
57 			    rt->rt_metric < HOPCNT_INFINITY)
58 				rtchange(rt, &rt->rt_router, HOPCNT_INFINITY);
59 			rt->rt_state &= ~RTS_CHANGED;
60 		}
61 	}
62 	if (doinghost) {
63 		doinghost = 0;
64 		base = nethash;
65 		goto again;
66 	}
67 	if (timetobroadcast) {
68 		toall(supply, 0, (struct interface *)NULL);
69 		lastbcast = now;
70 		lastfullupdate = now;
71 		needupdate = 0;		/* cancel any pending dynamic update */
72 		nextbcast.tv_sec = 0;
73 	}
74 }
75 
76 /*
77  * On hangup, let everyone know we're going away.
78  */
79 hup()
80 {
81 	register struct rthash *rh;
82 	register struct rt_entry *rt;
83 	struct rthash *base = hosthash;
84 	int doinghost = 1;
85 
86 	if (supplier) {
87 again:
88 		for (rh = base; rh < &base[ROUTEHASHSIZ]; rh++) {
89 			rt = rh->rt_forw;
90 			for (; rt != (struct rt_entry *)rh; rt = rt->rt_forw)
91 				rt->rt_metric = HOPCNT_INFINITY;
92 		}
93 		if (doinghost) {
94 			doinghost = 0;
95 			base = nethash;
96 			goto again;
97 		}
98 		toall(supply, 0, (struct interface *)NULL);
99 	}
100 	exit(1);
101 }
102