output.c revision 1.6 1 /*
2 * Copyright (c) 1983, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 /*static char sccsid[] = "from: @(#)output.c 8.1 (Berkeley) 6/5/93";*/
36 static char *rcsid = "$Id: output.c,v 1.6 1994/09/23 14:28:06 mycroft Exp $";
37 #endif /* not lint */
38
39 /*
40 * Routing Table Management Daemon
41 */
42 #include "defs.h"
43
44 /*
45 * Apply the function "f" to all non-passive
46 * interfaces. If the interface supports the
47 * use of broadcasting use it, otherwise address
48 * the output to the known router.
49 */
50 toall(f, rtstate, skipif)
51 int (*f)();
52 int rtstate;
53 struct interface *skipif;
54 {
55 register struct interface *ifp;
56 register struct sockaddr *dst;
57 register int flags;
58 extern struct interface *ifnet;
59
60 for (ifp = ifnet; ifp; ifp = ifp->int_next) {
61 if (ifp->int_flags & IFF_PASSIVE || ifp == skipif)
62 continue;
63 dst = ifp->int_flags & IFF_BROADCAST ? &ifp->int_broadaddr :
64 ifp->int_flags & IFF_POINTOPOINT ? &ifp->int_dstaddr :
65 &ifp->int_addr;
66 flags = ifp->int_flags & IFF_INTERFACE ? MSG_DONTROUTE : 0;
67 (*f)(dst, flags, ifp, rtstate);
68 }
69 }
70
71 /*
72 * Output a preformed packet.
73 */
74 /*ARGSUSED*/
75 sndmsg(dst, flags, ifp, rtstate)
76 struct sockaddr *dst;
77 int flags;
78 struct interface *ifp;
79 int rtstate;
80 {
81
82 (*afswitch[dst->sa_family].af_output)(s, flags,
83 dst, sizeof (struct rip));
84 TRACE_OUTPUT(ifp, dst, sizeof (struct rip));
85 }
86
87 /*
88 * Supply dst with the contents of the routing tables.
89 * If this won't fit in one packet, chop it up into several.
90 */
91 supply(dst, flags, ifp, rtstate)
92 struct sockaddr *dst;
93 int flags;
94 register struct interface *ifp;
95 int rtstate;
96 {
97 register struct rt_entry *rt;
98 register struct netinfo *n = msg->rip_nets;
99 register struct rthash *rh;
100 struct rthash *base = hosthash;
101 int doinghost = 1, size;
102 int (*output)() = afswitch[dst->sa_family].af_output;
103 int (*sendroute)() = afswitch[dst->sa_family].af_sendroute;
104 int npackets = 0;
105
106 msg->rip_cmd = RIPCMD_RESPONSE;
107 msg->rip_vers = RIPVERSION;
108 memset(msg->rip_res1, 0, sizeof(msg->rip_res1));
109 again:
110 for (rh = base; rh < &base[ROUTEHASHSIZ]; rh++)
111 for (rt = rh->rt_forw; rt != (struct rt_entry *)rh; rt = rt->rt_forw) {
112 /*
113 * Don't resend the information on the network
114 * from which it was received (unless sending
115 * in response to a query).
116 */
117 if (ifp && rt->rt_ifp == ifp &&
118 (rt->rt_state & RTS_INTERFACE) == 0)
119 continue;
120 if (rt->rt_state & RTS_EXTERNAL)
121 continue;
122 /*
123 * For dynamic updates, limit update to routes
124 * with the specified state.
125 */
126 if (rtstate && (rt->rt_state & rtstate) == 0)
127 continue;
128 /*
129 * Limit the spread of subnet information
130 * to those who are interested.
131 */
132 if (doinghost == 0 && rt->rt_state & RTS_SUBNET) {
133 if (rt->rt_dst.sa_family != dst->sa_family)
134 continue;
135 if ((*sendroute)(rt, dst) == 0)
136 continue;
137 }
138 size = (char *)n - packet;
139 if (size > MAXPACKETSIZE - sizeof (struct netinfo)) {
140 TRACE_OUTPUT(ifp, dst, size);
141 (*output)(s, flags, dst, size);
142 /*
143 * If only sending to ourselves,
144 * one packet is enough to monitor interface.
145 */
146 if (ifp && (ifp->int_flags &
147 (IFF_BROADCAST | IFF_POINTOPOINT | IFF_REMOTE)) == 0)
148 return;
149 n = msg->rip_nets;
150 npackets++;
151 }
152 n->rip_dst = rt->rt_dst;
153 #if BSD < 198810
154 if (sizeof(n->rip_dst.sa_family) > 1) /* XXX */
155 n->rip_dst.sa_family = htons(n->rip_dst.sa_family);
156 #else
157 #define osa(x) ((struct osockaddr *)(&(x)))
158 osa(n->rip_dst)->sa_family = htons(n->rip_dst.sa_family);
159 #endif
160 n->rip_metric = htonl(rt->rt_metric);
161 n++;
162 }
163 if (doinghost) {
164 doinghost = 0;
165 base = nethash;
166 goto again;
167 }
168 if (n != msg->rip_nets || (npackets == 0 && rtstate == 0)) {
169 size = (char *)n - packet;
170 TRACE_OUTPUT(ifp, dst, size);
171 (*output)(s, flags, dst, size);
172 }
173 }
174